3daff29bbd1733ca39dbcee73db6d42301e1c982
[bootcensus] / src / panic.c
1 /*
2 pcboot - bootable PC demo/game kernel
3 Copyright (C) 2018  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY, without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdarg.h>
21 #include "asmops.h"
22
23 struct all_registers {
24         uint32_t eax, ebx, ecx, edx;
25         uint32_t esp, ebp, esi, edi;
26         uint32_t eflags;
27         uint32_t cs, ss, ds, es, fs, gs;
28         uint32_t cr0, cr1, cr2, cr3;
29 };
30
31 /* defined in regs.S */
32 void get_regs(struct all_registers *regs);
33
34 void panic(const char *fmt, ...)
35 {
36         va_list ap;
37         struct all_registers regs;
38         uint32_t eip;
39
40         disable_intr();
41
42         memset(&regs, 0, sizeof regs);
43         get_regs(&regs);
44
45         CALLER_EIP(eip);
46
47         printf("~~~~~ pcboot panic ~~~~~\n");
48         va_start(ap, fmt);
49         vprintf(fmt, ap);
50         va_end(ap);
51
52         printf("\nRegisters:\n");
53         printf("eax: %x, ebx: %x, ecx: %x, edx: %x\n", regs.eax, regs.ebx, regs.ecx, regs.edx);
54         printf("esp: %x, ebp: %x, esi: %x, edi: %x\n", regs.esp, regs.ebp, regs.esi, regs.edi);
55         printf("eip: %x, eflags: %x\n", eip, regs.eflags);
56         printf("cr0: %x, cr1: %x, cr2: %x, cr3: %x\n", regs.cr0, regs.cr1, regs.cr2, regs.cr3);
57         printf("cs: %x (%d|%d)\n", regs.cs, regs.cs >> 3, regs.cs & 3);
58         printf("ss: %x (%d|%d)\n", regs.ss, regs.ss >> 3, regs.ss & 3);
59         printf("ds: %x (%d|%d)\n", regs.ds, regs.ds >> 3, regs.ds & 3);
60         printf("es: %x (%d|%d)\n", regs.es, regs.es >> 3, regs.es & 3);
61         printf("fs: %x (%d|%d)\n", regs.fs, regs.fs >> 3, regs.fs & 3);
62         printf("gs: %x (%d|%d)\n", regs.gs, regs.gs >> 3, regs.gs & 3);
63
64         for(;;) halt_cpu();
65 }