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