4 #include <arch/exception.h>
20 EAX = 0, ECX, EDX, EBX, ESP, EBP, ESI, EDI,
23 CS, SS, DS, ES, FS, GS,
27 static uint32_t gdb_stub_registers[NUM_REGS];
40 #define GDB_SIGSEGV 11
42 #define GDB_SIGPIPE 13
43 #define GDB_SIGALRM 14
44 #define GDB_SIGTERM 15
46 #define GDB_SIGSTOP 17
47 #define GDB_SIGTSTP 18
48 #define GDB_SIGCONT 19
49 #define GDB_SIGCHLD 20
50 #define GDB_SIGTTIN 21
51 #define GDB_SIGTTOU 22
53 #define GDB_SIGXCPU 24
54 #define GDB_SIGXFSZ 25
55 #define GDB_SIGVTALRM 26
56 #define GDB_SIGPROF 27
57 #define GDB_SIGWINCH 28
58 #define GDB_SIGLOST 29
59 #define GDB_SIGUSR1 30
60 #define GDB_SUGUSR2 31
62 #define GDB_SIGPOLL 33
63 #define GDB_SIGWIND 34
64 #define GDB_SIGPHONE 35
65 #define GDB_SIGWAITING 36
67 #define GDB_SIGDANGER 38
68 #define GDB_SIGGRANT 39
69 #define GDB_SIGRETRACT 40
71 #define GDB_SIGSOUND 42
73 #define GDB_SIGPRIO 44
106 #define GDB_SIGCANCEL 76
130 #define GDB_SIG86 100
131 #define GDB_SIG87 101
132 #define GDB_SIG88 102
133 #define GDB_SIG89 103
134 #define GDB_SIG90 104
135 #define GDB_SIG91 105
136 #define GDB_SIG92 106
137 #define GDB_SIG93 107
138 #define GDB_SIG94 108
139 #define GDB_SIG95 109
140 #define GDB_SIG96 110
141 #define GDB_SIG97 111
142 #define GDB_SIG98 112
143 #define GDB_SIG99 113
144 #define GDB_SIG100 114
145 #define GDB_SIG101 115
146 #define GDB_SIG102 116
147 #define GDB_SIG103 117
148 #define GDB_SIG104 118
149 #define GDB_SIG105 119
150 #define GDB_SIG106 120
151 #define GDB_SIG107 121
152 #define GDB_SIG108 122
153 #define GDB_SIG109 123
154 #define GDB_SIG110 124
155 #define GDB_SIG111 125
156 #define GDB_SIG112 126
157 #define GDB_SIG113 127
158 #define GDB_SIG114 128
159 #define GDB_SIG115 129
160 #define GDB_SIG116 130
161 #define GDB_SIG117 131
162 #define GDB_SIG118 132
163 #define GDB_SIG119 133
164 #define GDB_SIG120 134
165 #define GDB_SIG121 135
166 #define GDB_SIG122 136
167 #define GDB_SIG123 137
168 #define GDB_SIG124 138
169 #define GDB_SIG125 139
170 #define GDB_SIG126 140
171 #define GDB_SIG127 141
172 #define GDB_SIGINFO 142
173 #define GDB_UNKNOWN 143
174 #define GDB_DEFAULT 144
176 #define GDB_EXC_BAD_ACCESS 145
177 #define GDB_EXC_BAD_INSTRCTION 146
178 #define GDB_EXC_ARITHMETIC 147
179 #define GDB_EXC_EMULATION 148
180 #define GDB_EXC_SOFTWARE 149
181 #define GDB_EXC_BREAKPOINT 150
183 static unsigned char exception_to_signal[] = {
219 static const char hexchars[] =
"0123456789abcdef";
220 static char in_buffer[BUFMAX];
221 static char out_buffer[BUFMAX];
223 static inline void stub_putc(
int ch)
228 static inline void stub_flush(
void)
233 static inline int stub_getc(
void)
238 static int hex(
char ch)
240 if ((
ch >=
'a') && (
ch <=
'f'))
241 return (
ch -
'a' + 10);
242 if ((
ch >=
'0') && (
ch <=
'9'))
244 if ((
ch >=
'A') && (
ch <=
'F'))
245 return (
ch -
'A' + 10);
253 static int parse_ulong(
char **ptr,
unsigned long *
value)
261 while ((digit = hex(**ptr)) >= 0) {
262 *
value = ((*value) << 4) | digit;
265 return start != *ptr;
270 static void copy_to_hex(
char *
buf,
void *
addr,
unsigned long count)
277 *
buf++ = hexchars[
ch >> 4];
278 *
buf++ = hexchars[
ch & 0x0f];
285 static void copy_from_hex(
void *
addr,
char *
buf,
unsigned long count)
291 ch = hex(*
buf++) << 4;
299 static int get_packet(
char *
buffer)
302 unsigned char xmitcsum;
311 while ((
ch = (stub_getc() & 0x7f)) !=
'$')
319 while (
count < BUFMAX) {
320 ch = stub_getc() & 0x7f;
330 xmitcsum = hex(stub_getc() & 0x7f) << 4;
331 xmitcsum += hex(stub_getc() & 0x7f);
346 static void put_packet(
char *
buffer)
369 }
while ((stub_getc() & 0x7f) !=
'+');
383 gdb_stub_registers[PC] =
info->eip;
384 gdb_stub_registers[CS] =
info->cs;
385 gdb_stub_registers[PS] =
info->eflags;
388 signo = exception_to_signal[
info->vector];
392 out_buffer[1] = hexchars[(signo>>4) & 0xf];
393 out_buffer[2] = hexchars[signo & 0xf];
394 out_buffer[3] =
'\0';
395 put_packet(out_buffer);
400 out_buffer[0] =
'\0';
401 out_buffer[1] =
'\0';
402 if (!get_packet(in_buffer))
404 switch (in_buffer[0]) {
407 out_buffer[1] = hexchars[(signo >> 4) & 0xf];
408 out_buffer[2] = hexchars[signo & 0xf];
409 out_buffer[3] =
'\0';
412 copy_to_hex(out_buffer, &gdb_stub_registers,
413 sizeof(gdb_stub_registers));
416 copy_from_hex(&gdb_stub_registers, in_buffer + 1,
417 sizeof(gdb_stub_registers));
419 info->eip = gdb_stub_registers[PC];
420 info->cs = gdb_stub_registers[CS];
421 info->eflags = gdb_stub_registers[PS];
422 memcpy(out_buffer,
"OK", 3);
427 if (parse_ulong(&ptr, &
addr) &&
429 parse_ulong(&ptr, &
length)) {
430 copy_to_hex(out_buffer, (
void *)
addr,
length);
432 memcpy(out_buffer,
"E01", 4);
439 if (parse_ulong(&ptr, &
addr) &&
441 parse_ulong(&ptr, &
length) &&
444 memcpy(out_buffer,
"OK", 3);
446 memcpy(out_buffer,
"E02", 4);
454 if (parse_ulong(&ptr, &
addr))
458 info->eflags &= ~(1 << 8);
460 if (in_buffer[0] ==
's')
461 info->eflags |= (1 << 8);
464 memcpy(out_buffer,
"OK", 3);
484 put_packet(out_buffer);
488 int logical_processor = 0;
496 #define MDUMP_SIZE 0x100
498 "CPU Index %d - APIC %d Unexpected Exception:\n"
499 "%lld @ %02llx:%016llx - Halting\n"
500 "Code: %lld rflags: %016llx cr2: %016llx\n"
501 "rax: %016llx rbx: %016llx\n"
502 "rcx: %016llx rdx: %016llx\n"
503 "rdi: %016llx rsi: %016llx\n"
504 "rbp: %016llx rsp: %016llx\n"
505 "r08: %016llx r09: %016llx\n"
506 "r10: %016llx r11: %016llx\n"
507 "r12: %016llx r13: %016llx\n"
508 "r14: %016llx r15: %016llx\n",
509 logical_processor, apic_id,
518 #define MDUMP_SIZE 0x80
521 "CPU Index %d - APIC %d Unexpected Exception:"
522 "%d @ %02x:%08x - Halting\n"
523 "Code: %d eflags: %08x cr2: %08x\n"
524 "eax: %08x ebx: %08x ecx: %08x edx: %08x\n"
525 "edi: %08x esi: %08x ebp: %08x esp: %08x\n",
526 logical_processor, apic_id,
558 #define GATE_P (1 << 15)
559 #define GATE_DPL(x) (((x) & 0x3) << 13)
560 #define GATE_SIZE_16 (0 << 11)
561 #define GATE_SIZE_32 (1 << 11)
563 #define IGATE_FLAGS (GATE_P | GATE_DPL(0) | GATE_SIZE_32 | (0x6 << 8))
651 idt[i].segsel = segment;
void * memcpy(void *dest, const void *src, size_t n)
void exception_init(void)
#define printk(level,...)
void __noreturn die(const char *fmt,...)
static __always_inline CRx_TYPE read_cr2(void)
static struct smmstore_params_info info
static uint8_t checksum(uint8_t *data, int offset)
static __always_inline unsigned int lapicid(void)
#define BIOS_EMERG
BIOS_EMERG - Emergency / Fatal.
static struct dramc_channel const ch[2]
u8 buffer[C2P_BUFFER_MAXSIZE]
unsigned long long uint64_t
void gdb_tx_byte(unsigned char byte)
unsigned char gdb_rx_byte(void)
static uint16_t get_cs(void)
static struct intr_gate idt[ARRAY_SIZE(intr_entries)] __aligned(8)
void x86_exception(struct eregs *info)
static const uintptr_t intr_entries[]
static void load_idt(void *table, size_t sz)
struct intr_gate __packed