mb/asus/p8z77-m: Drop GPIO by I/O
[coreboot2.git] / src / arch / x86 / exception.c
blob224f0e1d41641e8bd2d2ee2d186964b1639cc749
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #include <arch/cpu.h>
3 #include <arch/breakpoint.h>
4 #include <arch/null_breakpoint.h>
5 #include <arch/stack_canary_breakpoint.h>
6 #include <arch/exception.h>
7 #include <arch/registers.h>
8 #include <commonlib/helpers.h>
9 #include <console/console.h>
10 #include <console/streams.h>
11 #include <cpu/x86/cr.h>
12 #include <cpu/x86/lapic.h>
13 #include <stdint.h>
14 #include <string.h>
16 #if CONFIG(GDB_STUB)
18 /* BUFMAX defines the maximum number of characters in inbound/outbound buffers.
19 * At least NUM_REGBYTES*2 are needed for register packets
21 #define BUFMAX 400
22 enum regnames {
23 EAX = 0, ECX, EDX, EBX, ESP, EBP, ESI, EDI,
24 PC /* also known as eip */,
25 PS /* also known as eflags */,
26 CS, SS, DS, ES, FS, GS,
27 NUM_REGS /* Number of registers. */
30 static uint32_t gdb_stub_registers[NUM_REGS];
32 #define GDB_SIG0 0 /* Signal 0 */
33 #define GDB_SIGHUP 1 /* Hangup */
34 #define GDB_SIGINT 2 /* Interrupt */
35 #define GDB_SIGQUIT 3 /* Quit */
36 #define GDB_SIGILL 4 /* Illegal instruction */
37 #define GDB_SIGTRAP 5 /* Trace/breakpoint trap */
38 #define GDB_SIGABRT 6 /* Aborted */
39 #define GDB_SIGEMT 7 /* Emulation trap */
40 #define GDB_SIGFPE 8 /* Arithmetic exception */
41 #define GDB_SIGKILL 9 /* Killed */
42 #define GDB_SIGBUS 10 /* Bus error */
43 #define GDB_SIGSEGV 11 /* Segmentation fault */
44 #define GDB_SIGSYS 12 /* Bad system call */
45 #define GDB_SIGPIPE 13 /* Broken pipe */
46 #define GDB_SIGALRM 14 /* Alarm clock */
47 #define GDB_SIGTERM 15 /* Terminated */
48 #define GDB_SIGURG 16 /* Urgent I/O condition */
49 #define GDB_SIGSTOP 17 /* Stopped (signal) */
50 #define GDB_SIGTSTP 18 /* Stopped (user) */
51 #define GDB_SIGCONT 19 /* Continued */
52 #define GDB_SIGCHLD 20 /* Child status changed */
53 #define GDB_SIGTTIN 21 /* Stopped (tty input) */
54 #define GDB_SIGTTOU 22 /* Stopped (tty output) */
55 #define GDB_SIGIO 23 /* I/O possible */
56 #define GDB_SIGXCPU 24 /* CPU time limit exceeded */
57 #define GDB_SIGXFSZ 25 /* File size limit exceeded */
58 #define GDB_SIGVTALRM 26 /* Virtual timer expired */
59 #define GDB_SIGPROF 27 /* Profiling timer expired */
60 #define GDB_SIGWINCH 28 /* Window size changed */
61 #define GDB_SIGLOST 29 /* Resource lost */
62 #define GDB_SIGUSR1 30 /* User defined signal 1 */
63 #define GDB_SUGUSR2 31 /* User defined signal 2 */
64 #define GDB_SIGPWR 32 /* Power fail/restart */
65 #define GDB_SIGPOLL 33 /* Pollable event occurred */
66 #define GDB_SIGWIND 34 /* SIGWIND */
67 #define GDB_SIGPHONE 35 /* SIGPHONE */
68 #define GDB_SIGWAITING 36 /* Process's LWPs are blocked */
69 #define GDB_SIGLWP 37 /* Signal LWP */
70 #define GDB_SIGDANGER 38 /* Swap space dangerously low */
71 #define GDB_SIGGRANT 39 /* Monitor mode granted */
72 #define GDB_SIGRETRACT 40 /* Need to relinquish monitor mode */
73 #define GDB_SIGMSG 41 /* Monitor mode data available */
74 #define GDB_SIGSOUND 42 /* Sound completed */
75 #define GDB_SIGSAK 43 /* Secure attention */
76 #define GDB_SIGPRIO 44 /* SIGPRIO */
78 #define GDB_SIG33 45 /* Real-time event 33 */
79 #define GDB_SIG34 46 /* Real-time event 34 */
80 #define GDB_SIG35 47 /* Real-time event 35 */
81 #define GDB_SIG36 48 /* Real-time event 36 */
82 #define GDB_SIG37 49 /* Real-time event 37 */
83 #define GDB_SIG38 50 /* Real-time event 38 */
84 #define GDB_SIG39 51 /* Real-time event 39 */
85 #define GDB_SIG40 52 /* Real-time event 40 */
86 #define GDB_SIG41 53 /* Real-time event 41 */
87 #define GDB_SIG42 54 /* Real-time event 42 */
88 #define GDB_SIG43 55 /* Real-time event 43 */
89 #define GDB_SIG44 56 /* Real-time event 44 */
90 #define GDB_SIG45 57 /* Real-time event 45 */
91 #define GDB_SIG46 58 /* Real-time event 46 */
92 #define GDB_SIG47 59 /* Real-time event 47 */
93 #define GDB_SIG48 60 /* Real-time event 48 */
94 #define GDB_SIG49 61 /* Real-time event 49 */
95 #define GDB_SIG50 62 /* Real-time event 50 */
96 #define GDB_SIG51 63 /* Real-time event 51 */
97 #define GDB_SIG52 64 /* Real-time event 52 */
98 #define GDB_SIG53 65 /* Real-time event 53 */
99 #define GDB_SIG54 66 /* Real-time event 54 */
100 #define GDB_SIG55 67 /* Real-time event 55 */
101 #define GDB_SIG56 68 /* Real-time event 56 */
102 #define GDB_SIG57 69 /* Real-time event 57 */
103 #define GDB_SIG58 70 /* Real-time event 58 */
104 #define GDB_SIG59 71 /* Real-time event 59 */
105 #define GDB_SIG60 72 /* Real-time event 60 */
106 #define GDB_SIG61 73 /* Real-time event 61 */
107 #define GDB_SIG62 74 /* Real-time event 62 */
108 #define GDB_SIG63 75 /* Real-time event 63 */
109 #define GDB_SIGCANCEL 76 /* LWP internal signal */
110 #define GDB_SIG32 77 /* Real-time event 32 */
111 #define GDB_SIG64 78 /* Real-time event 64 */
112 #define GDB_SIG65 79 /* Real-time event 65 */
113 #define GDB_SIG66 80 /* Real-time event 66 */
114 #define GDB_SIG67 81 /* Real-time event 67 */
115 #define GDB_SIG68 82 /* Real-time event 68 */
116 #define GDB_SIG69 83 /* Real-time event 69 */
117 #define GDB_SIG70 84 /* Real-time event 70 */
118 #define GDB_SIG71 85 /* Real-time event 71 */
119 #define GDB_SIG72 86 /* Real-time event 72 */
120 #define GDB_SIG73 87 /* Real-time event 73 */
121 #define GDB_SIG74 88 /* Real-time event 74 */
122 #define GDB_SIG75 89 /* Real-time event 75 */
123 #define GDB_SIG76 90 /* Real-time event 76 */
124 #define GDB_SIG77 91 /* Real-time event 77 */
125 #define GDB_SIG78 92 /* Real-time event 78 */
126 #define GDB_SIG79 93 /* Real-time event 79 */
127 #define GDB_SIG80 94 /* Real-time event 80 */
128 #define GDB_SIG81 95 /* Real-time event 81 */
129 #define GDB_SIG82 96 /* Real-time event 82 */
130 #define GDB_SIG83 97 /* Real-time event 83 */
131 #define GDB_SIG84 98 /* Real-time event 84 */
132 #define GDB_SIG85 99 /* Real-time event 85 */
133 #define GDB_SIG86 100 /* Real-time event 86 */
134 #define GDB_SIG87 101 /* Real-time event 87 */
135 #define GDB_SIG88 102 /* Real-time event 88 */
136 #define GDB_SIG89 103 /* Real-time event 89 */
137 #define GDB_SIG90 104 /* Real-time event 90 */
138 #define GDB_SIG91 105 /* Real-time event 91 */
139 #define GDB_SIG92 106 /* Real-time event 92 */
140 #define GDB_SIG93 107 /* Real-time event 93 */
141 #define GDB_SIG94 108 /* Real-time event 94 */
142 #define GDB_SIG95 109 /* Real-time event 95 */
143 #define GDB_SIG96 110 /* Real-time event 96 */
144 #define GDB_SIG97 111 /* Real-time event 97 */
145 #define GDB_SIG98 112 /* Real-time event 98 */
146 #define GDB_SIG99 113 /* Real-time event 99 */
147 #define GDB_SIG100 114 /* Real-time event 100 */
148 #define GDB_SIG101 115 /* Real-time event 101 */
149 #define GDB_SIG102 116 /* Real-time event 102 */
150 #define GDB_SIG103 117 /* Real-time event 103 */
151 #define GDB_SIG104 118 /* Real-time event 104 */
152 #define GDB_SIG105 119 /* Real-time event 105 */
153 #define GDB_SIG106 120 /* Real-time event 106 */
154 #define GDB_SIG107 121 /* Real-time event 107 */
155 #define GDB_SIG108 122 /* Real-time event 108 */
156 #define GDB_SIG109 123 /* Real-time event 109 */
157 #define GDB_SIG110 124 /* Real-time event 110 */
158 #define GDB_SIG111 125 /* Real-time event 111 */
159 #define GDB_SIG112 126 /* Real-time event 112 */
160 #define GDB_SIG113 127 /* Real-time event 113 */
161 #define GDB_SIG114 128 /* Real-time event 114 */
162 #define GDB_SIG115 129 /* Real-time event 115 */
163 #define GDB_SIG116 130 /* Real-time event 116 */
164 #define GDB_SIG117 131 /* Real-time event 117 */
165 #define GDB_SIG118 132 /* Real-time event 118 */
166 #define GDB_SIG119 133 /* Real-time event 119 */
167 #define GDB_SIG120 134 /* Real-time event 120 */
168 #define GDB_SIG121 135 /* Real-time event 121 */
169 #define GDB_SIG122 136 /* Real-time event 122 */
170 #define GDB_SIG123 137 /* Real-time event 123 */
171 #define GDB_SIG124 138 /* Real-time event 124 */
172 #define GDB_SIG125 139 /* Real-time event 125 */
173 #define GDB_SIG126 140 /* Real-time event 126 */
174 #define GDB_SIG127 141 /* Real-time event 127 */
175 #define GDB_SIGINFO 142 /* Information request */
176 #define GDB_UNKNOWN 143 /* Unknown signal */
177 #define GDB_DEFAULT 144 /* error: default signal */
178 /* Mach exceptions */
179 #define GDB_EXC_BAD_ACCESS 145 /* Could not access memory */
180 #define GDB_EXC_BAD_INSTRCTION 146 /* Illegal instruction/operand */
181 #define GDB_EXC_ARITHMETIC 147 /* Arithmetic exception */
182 #define GDB_EXC_EMULATION 148 /* Emulation instruction */
183 #define GDB_EXC_SOFTWARE 149 /* Software generated exception */
184 #define GDB_EXC_BREAKPOINT 150 /* Breakpoint */
186 static unsigned char exception_to_signal[] = {
187 [0] = GDB_SIGFPE, /* divide by zero */
188 [1] = GDB_SIGTRAP, /* debug exception */
189 [2] = GDB_SIGSEGV, /* NMI Interrupt */
190 [3] = GDB_SIGTRAP, /* Breakpoint */
191 [4] = GDB_SIGSEGV, /* into instruction (overflow) */
192 [5] = GDB_SIGSEGV, /* bound instruction */
193 [6] = GDB_SIGILL, /* Invalid opcode */
194 [7] = GDB_SIGSEGV, /* coprocessor not available */
195 [8] = GDB_SIGSEGV, /* double fault */
196 [9] = GDB_SIGFPE, /* coprocessor segment overrun */
197 [10] = GDB_SIGSEGV, /* Invalid TSS */
198 [11] = GDB_SIGBUS, /* Segment not present */
199 [12] = GDB_SIGBUS, /* stack exception */
200 [13] = GDB_SIGSEGV, /* general protection */
201 [14] = GDB_SIGSEGV, /* page fault */
202 [15] = GDB_UNKNOWN, /* reserved */
203 [16] = GDB_SIGEMT, /* coprocessor error */
204 [17] = GDB_SIGBUS, /* alignment check */
205 [18] = GDB_SIGSEGV, /* machine check */
206 [19] = GDB_SIGFPE, /* simd floating point exception */
207 [20] = GDB_UNKNOWN,
208 [21] = GDB_UNKNOWN,
209 [22] = GDB_UNKNOWN,
210 [23] = GDB_UNKNOWN,
211 [24] = GDB_UNKNOWN,
212 [25] = GDB_UNKNOWN,
213 [26] = GDB_UNKNOWN,
214 [27] = GDB_UNKNOWN,
215 [28] = GDB_UNKNOWN,
216 [29] = GDB_UNKNOWN,
217 [30] = GDB_UNKNOWN,
218 [31] = GDB_UNKNOWN,
219 [32] = GDB_SIGINT, /* User interrupt */
222 static const char hexchars[] = "0123456789abcdef";
223 static char in_buffer[BUFMAX];
224 static char out_buffer[BUFMAX];
226 static inline void stub_putc(int ch)
228 gdb_tx_byte(ch);
231 static inline void stub_flush(void)
233 gdb_tx_flush();
236 static inline int stub_getc(void)
238 return gdb_rx_byte();
241 static int hex(char ch)
243 if ((ch >= 'a') && (ch <= 'f'))
244 return (ch - 'a' + 10);
245 if ((ch >= '0') && (ch <= '9'))
246 return (ch - '0');
247 if ((ch >= 'A') && (ch <= 'F'))
248 return (ch - 'A' + 10);
249 return (-1);
253 * While we find hexadecimal digits, build an int.
254 * Fals is returned if nothing is parsed true otherwise.
256 static int parse_ulong(char **ptr, unsigned long *value)
258 int digit;
259 char *start;
261 start = *ptr;
262 *value = 0;
264 while ((digit = hex(**ptr)) >= 0) {
265 *value = ((*value) << 4) | digit;
266 (*ptr)++;
268 return start != *ptr;
271 /* convert the memory pointed to by mem into hex, placing result in buf */
272 /* return a pointer to the last char put in buf (null) */
273 static void copy_to_hex(char *buf, void *addr, unsigned long count)
275 unsigned char ch;
276 char *mem = addr;
278 while (count--) {
279 ch = *mem++;
280 *buf++ = hexchars[ch >> 4];
281 *buf++ = hexchars[ch & 0x0f];
283 *buf = 0;
286 /* convert the hex array pointed to by buf into binary to be placed in mem */
287 /* return a pointer to the character AFTER the last byte written */
288 static void copy_from_hex(void *addr, char *buf, unsigned long count)
290 unsigned char ch;
291 char *mem = addr;
293 while (count--) {
294 ch = hex(*buf++) << 4;
295 ch = ch + hex(*buf++);
296 *mem++ = ch;
300 /* scan for the sequence $<data>#<checksum> */
302 static int get_packet(char *buffer)
304 unsigned char checksum;
305 unsigned char xmitcsum;
306 int count;
307 char ch;
309 /* TODO: implement a timeout in get_packet */
310 do {
311 /* wait around for the start character, ignore all other
312 * characters
314 while ((ch = (stub_getc() & 0x7f)) != '$')
316 checksum = 0;
317 xmitcsum = -1;
319 count = 0;
321 /* now, read until a # or end of buffer is found */
322 while (count < BUFMAX) {
323 ch = stub_getc() & 0x7f;
324 if (ch == '#')
325 break;
326 checksum = checksum + ch;
327 buffer[count] = ch;
328 count = count + 1;
330 buffer[count] = 0;
332 if (ch == '#') {
333 xmitcsum = hex(stub_getc() & 0x7f) << 4;
334 xmitcsum += hex(stub_getc() & 0x7f);
336 if (checksum != xmitcsum) {
337 stub_putc('-'); /* failed checksum */
338 stub_flush();
339 } else {
340 stub_putc('+'); /* successful transfer */
341 stub_flush();
344 } while (checksum != xmitcsum);
345 return 1;
348 /* send the packet in buffer.*/
349 static void put_packet(char *buffer)
351 unsigned char checksum;
352 int count;
353 char ch;
355 /* $<packet info>#<checksum>. */
356 do {
357 stub_putc('$');
358 checksum = 0;
359 count = 0;
361 while ((ch = buffer[count])) {
362 stub_putc(ch);
363 checksum += ch;
364 count += 1;
367 stub_putc('#');
368 stub_putc(hexchars[checksum >> 4]);
369 stub_putc(hexchars[checksum % 16]);
370 stub_flush();
372 } while ((stub_getc() & 0x7f) != '+');
374 #endif /* CONFIG_GDB_STUB */
376 #define DEBUG_VECTOR 1
378 void x86_exception(struct eregs *info);
380 void x86_exception(struct eregs *info)
382 #if CONFIG(GDB_STUB)
383 /* TODO implement 64bit mode */
384 int signo;
385 memcpy(gdb_stub_registers, info, 8*sizeof(uint32_t));
386 gdb_stub_registers[PC] = info->eip;
387 gdb_stub_registers[CS] = info->cs;
388 gdb_stub_registers[PS] = info->eflags;
389 signo = GDB_UNKNOWN;
390 if (info->vector < ARRAY_SIZE(exception_to_signal))
391 signo = exception_to_signal[info->vector];
393 /* reply to the host that an exception has occurred */
394 out_buffer[0] = 'S';
395 out_buffer[1] = hexchars[(signo>>4) & 0xf];
396 out_buffer[2] = hexchars[signo & 0xf];
397 out_buffer[3] = '\0';
398 put_packet(out_buffer);
400 while (1) {
401 unsigned long addr, length;
402 char *ptr;
403 out_buffer[0] = '\0';
404 out_buffer[1] = '\0';
405 if (!get_packet(in_buffer))
406 break;
407 switch (in_buffer[0]) {
408 case '?': /* last signal */
409 out_buffer[0] = 'S';
410 out_buffer[1] = hexchars[(signo >> 4) & 0xf];
411 out_buffer[2] = hexchars[signo & 0xf];
412 out_buffer[3] = '\0';
413 break;
414 case 'g': /* return the value of the CPU registers */
415 copy_to_hex(out_buffer, &gdb_stub_registers,
416 sizeof(gdb_stub_registers));
417 break;
418 case 'G': /* set the value of the CPU registers - return OK */
419 copy_from_hex(&gdb_stub_registers, in_buffer + 1,
420 sizeof(gdb_stub_registers));
421 memcpy(info, gdb_stub_registers, 8*sizeof(uint32_t));
422 info->eip = gdb_stub_registers[PC];
423 info->cs = gdb_stub_registers[CS];
424 info->eflags = gdb_stub_registers[PS];
425 memcpy(out_buffer, "OK", 3);
426 break;
427 case 'm':
428 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
429 ptr = &in_buffer[1];
430 if (parse_ulong(&ptr, &addr) &&
431 (*ptr++ == ',') &&
432 parse_ulong(&ptr, &length)) {
433 copy_to_hex(out_buffer, (void *)addr, length);
434 } else
435 memcpy(out_buffer, "E01", 4);
436 break;
437 case 'M':
438 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA
439 * return OK
441 ptr = &in_buffer[1];
442 if (parse_ulong(&ptr, &addr) &&
443 (*(ptr++) == ',') &&
444 parse_ulong(&ptr, &length) &&
445 (*(ptr++) == ':')) {
446 copy_from_hex((void *)addr, ptr, length);
447 memcpy(out_buffer, "OK", 3);
448 } else
449 memcpy(out_buffer, "E02", 4);
450 break;
451 case 's':
452 case 'c':
453 /* cAA..AA Continue at address AA..AA(optional)
454 * sAA..AA Step one instruction from AA..AA(optional)
456 ptr = &in_buffer[1];
457 if (parse_ulong(&ptr, &addr))
458 info->eip = addr;
460 /* Clear the trace bit */
461 info->eflags &= ~(1 << 8);
462 /* Set the trace bit if we are single stepping */
463 if (in_buffer[0] == 's')
464 info->eflags |= (1 << 8);
465 return;
466 case 'D':
467 memcpy(out_buffer, "OK", 3);
468 break;
469 case 'k': /* kill request? */
470 break;
471 case 'q': /* query */
472 break;
473 case 'z': /* z0AAAA,LLLL remove memory breakpoint */
474 /* z1AAAA,LLLL remove hardware breakpoint */
475 /* z2AAAA,LLLL remove write watchpoint */
476 /* z3AAAA,LLLL remove read watchpoint */
477 /* z4AAAA,LLLL remove access watchpoint */
478 case 'Z': /* Z0AAAA,LLLL insert memory breakpoint */
479 /* Z1AAAA,LLLL insert hardware breakpoint */
480 /* Z2AAAA,LLLL insert write watchpoint */
481 /* Z3AAAA,LLLL insert read watchpoint */
482 /* Z4AAAA,LLLL insert access watchpoint */
483 break;
484 default:
485 break;
487 put_packet(out_buffer);
489 #else /* !CONFIG_GDB_STUB */
491 int logical_processor = 0;
493 if (info->vector == DEBUG_VECTOR) {
494 if (breakpoint_dispatch_handler(info) == 0)
495 return;
498 #if ENV_RAMSTAGE
499 logical_processor = cpu_index();
500 #endif
501 u8 *code;
502 #if ENV_X86_64
503 #define MDUMP_SIZE 0x100
504 printk(BIOS_EMERG,
505 "CPU Index %d - APIC %d Unexpected Exception:\n"
506 "%lld @ %02llx:%016llx - Halting\n"
507 "Code: %lld rflags: %016llx cr2: %016llx\n"
508 "rax: %016llx rbx: %016llx\n"
509 "rcx: %016llx rdx: %016llx\n"
510 "rdi: %016llx rsi: %016llx\n"
511 "rbp: %016llx rsp: %016llx\n"
512 "r08: %016llx r09: %016llx\n"
513 "r10: %016llx r11: %016llx\n"
514 "r12: %016llx r13: %016llx\n"
515 "r14: %016llx r15: %016llx\n",
516 logical_processor, early_lapicid(),
517 info->vector, info->cs, info->rip,
518 info->error_code, info->rflags, read_cr2(),
519 info->rax, info->rbx, info->rcx, info->rdx,
520 info->rdi, info->rsi, info->rbp, info->rsp,
521 info->r8, info->r9, info->r10, info->r11,
522 info->r12, info->r13, info->r14, info->r15);
523 code = (u8 *)((uintptr_t)info->rip - (MDUMP_SIZE >> 2));
524 #else
525 #define MDUMP_SIZE 0x80
527 printk(BIOS_EMERG,
528 "CPU Index %d - APIC %d Unexpected Exception:"
529 "%d @ %02x:%08x - Halting\n"
530 "Code: %d eflags: %08x cr2: %08x\n"
531 "eax: %08x ebx: %08x ecx: %08x edx: %08x\n"
532 "edi: %08x esi: %08x ebp: %08x esp: %08x\n",
533 logical_processor, early_lapicid(),
534 info->vector, info->cs, info->eip,
535 info->error_code, info->eflags, read_cr2(),
536 info->eax, info->ebx, info->ecx, info->edx,
537 info->edi, info->esi, info->ebp, info->esp);
538 code = (u8 *)((uintptr_t)info->eip - (MDUMP_SIZE >> 1));
539 #endif
540 /* Align to 8-byte boundary please, and print eight bytes per row.
541 * This is done to make DRAM burst timing/reordering errors more
542 * evident from the looking at the dump */
543 code = (u8 *)((uintptr_t)code & ~0x7);
544 int i;
545 for (i = 0; i < MDUMP_SIZE; i++) {
546 if ((i & 0x07) == 0)
547 printk(BIOS_EMERG, "\n%p:\t", code + i);
548 printk(BIOS_EMERG, "%.2x ", code[i]);
551 /* Align to 4-byte boundary and up the stack. */
552 u32 *ptr = (u32 *)(ALIGN_DOWN((uintptr_t)info->esp, sizeof(u32)) + MDUMP_SIZE - 4);
553 for (i = 0; i < MDUMP_SIZE / sizeof(u32); ++i, --ptr) {
554 printk(BIOS_EMERG, "\n%p:\t0x%08x", ptr, *ptr);
555 if ((uintptr_t)ptr == info->ebp)
556 printk(BIOS_EMERG, " <-ebp");
557 else if ((uintptr_t)ptr == info->esp)
558 printk(BIOS_EMERG, " <-esp");
561 die("\n");
562 #endif
565 #define GATE_P (1 << 15)
566 #define GATE_DPL(x) (((x) & 0x3) << 13)
567 #define GATE_SIZE_16 (0 << 11)
568 #define GATE_SIZE_32 (1 << 11)
570 #define IGATE_FLAGS (GATE_P | GATE_DPL(0) | GATE_SIZE_32 | (0x6 << 8))
572 struct intr_gate {
573 uint16_t offset_0;
574 uint16_t segsel;
575 uint16_t flags;
576 uint16_t offset_1;
577 #if ENV_X86_64
578 uint32_t offset_2;
579 uint32_t reserved;
580 #endif
581 } __packed;
583 /* Even though the vecX symbols are interrupt entry points just treat them
584 like data to more easily get the pointer values in C. Because IDT entries
585 format splits the offset field up, one can't use the linker to resolve
586 parts of a relocation on x86 ABI. An array of pointers is used to gather
587 the symbols. The IDT is initialized at runtime when exception_init() is
588 called. */
589 extern u8 vec0[], vec1[], vec2[], vec3[], vec4[], vec5[], vec6[], vec7[];
590 extern u8 vec8[], vec9[], vec10[], vec11[], vec12[], vec13[], vec14[], vec15[];
591 extern u8 vec16[], vec17[], vec18[], vec19[];
593 static const uintptr_t intr_entries[] = {
594 (uintptr_t)vec0, (uintptr_t)vec1, (uintptr_t)vec2, (uintptr_t)vec3,
595 (uintptr_t)vec4, (uintptr_t)vec5, (uintptr_t)vec6, (uintptr_t)vec7,
596 (uintptr_t)vec8, (uintptr_t)vec9, (uintptr_t)vec10, (uintptr_t)vec11,
597 (uintptr_t)vec12, (uintptr_t)vec13, (uintptr_t)vec14, (uintptr_t)vec15,
598 (uintptr_t)vec16, (uintptr_t)vec17, (uintptr_t)vec18, (uintptr_t)vec19,
601 static struct intr_gate idt[ARRAY_SIZE(intr_entries)] __aligned(8);
603 static inline uint16_t get_cs(void)
605 uint16_t segment;
607 asm volatile (
608 "mov %%cs, %0\n"
609 : "=r" (segment)
611 : "memory"
614 return segment;
617 struct lidtarg {
618 uint16_t limit;
619 #if ENV_X86_32
620 uint32_t base;
621 #else
622 uint64_t base;
623 #endif
624 } __packed;
626 /* This global is for src/cpu/x86/lapic/secondary.S usage which is only
627 used during ramstage. */
628 struct lidtarg idtarg;
630 static void load_idt(void *table, size_t sz)
632 struct lidtarg lidtarg = {
633 .limit = sz - 1,
634 .base = (uintptr_t)table,
637 asm volatile (
638 "lidt %0"
640 : "m" (lidtarg)
641 : "memory"
644 if (ENV_RAMSTAGE)
645 memcpy(&idtarg, &lidtarg, sizeof(idtarg));
648 asmlinkage void exception_init(void)
650 int i;
651 uint16_t segment;
653 segment = get_cs();
655 /* Initialize IDT. */
656 for (i = 0; i < ARRAY_SIZE(idt); i++) {
657 idt[i].offset_0 = intr_entries[i];
658 idt[i].segsel = segment;
659 idt[i].flags = IGATE_FLAGS;
660 idt[i].offset_1 = intr_entries[i] >> 16;
661 #if ENV_X86_64
662 idt[i].offset_2 = intr_entries[i] >> 32;
663 #endif
666 load_idt(idt, sizeof(idt));
668 null_breakpoint_init();
669 stack_canary_breakpoint_init();