| 1 |
|
| 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> |
| 15 |
|
| 16 |
#if CONFIG(GDB_STUB) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#define BUFMAX 400 |
| 22 |
enum regnames { |
| 23 |
EAX = 0, ECX, EDX, EBX, ESP, EBP, ESI, EDI, |
| 24 |
PC , |
| 25 |
PS , |
| 26 |
CS, SS, DS, ES, FS, GS, |
| 27 |
NUM_REGS |
| 28 |
}; |
| 29 |
|
| 30 |
static uint32_t gdb_stub_registers[NUM_REGS]; |
| 31 |
|
| 32 |
#define GDB_SIG0 0 |
| 33 |
#define GDB_SIGHUP 1 |
| 34 |
#define GDB_SIGINT 2 |
| 35 |
#define GDB_SIGQUIT 3 |
| 36 |
#define GDB_SIGILL 4 |
| 37 |
#define GDB_SIGTRAP 5 |
| 38 |
#define GDB_SIGABRT 6 |
| 39 |
#define GDB_SIGEMT 7 |
| 40 |
#define GDB_SIGFPE 8 |
| 41 |
#define GDB_SIGKILL 9 |
| 42 |
#define GDB_SIGBUS 10 |
| 43 |
#define GDB_SIGSEGV 11 |
| 44 |
#define GDB_SIGSYS 12 |
| 45 |
#define GDB_SIGPIPE 13 |
| 46 |
#define GDB_SIGALRM 14 |
| 47 |
#define GDB_SIGTERM 15 |
| 48 |
#define GDB_SIGURG 16 |
| 49 |
#define GDB_SIGSTOP 17 |
| 50 |
#define GDB_SIGTSTP 18 |
| 51 |
#define GDB_SIGCONT 19 |
| 52 |
#define GDB_SIGCHLD 20 |
| 53 |
#define GDB_SIGTTIN 21 |
| 54 |
#define GDB_SIGTTOU 22 |
| 55 |
#define GDB_SIGIO 23 |
| 56 |
#define GDB_SIGXCPU 24 |
| 57 |
#define GDB_SIGXFSZ 25 |
| 58 |
#define GDB_SIGVTALRM 26 |
| 59 |
#define GDB_SIGPROF 27 |
| 60 |
#define GDB_SIGWINCH 28 |
| 61 |
#define GDB_SIGLOST 29 |
| 62 |
#define GDB_SIGUSR1 30 |
| 63 |
#define GDB_SUGUSR2 31 |
| 64 |
#define GDB_SIGPWR 32 |
| 65 |
#define GDB_SIGPOLL 33 |
| 66 |
#define GDB_SIGWIND 34 |
| 67 |
#define GDB_SIGPHONE 35 |
| 68 |
#define GDB_SIGWAITING 36 |
| 69 |
#define GDB_SIGLWP 37 |
| 70 |
#define GDB_SIGDANGER 38 |
| 71 |
#define GDB_SIGGRANT 39 |
| 72 |
#define GDB_SIGRETRACT 40 |
| 73 |
#define GDB_SIGMSG 41 |
| 74 |
#define GDB_SIGSOUND 42 |
| 75 |
#define GDB_SIGSAK 43 |
| 76 |
#define GDB_SIGPRIO 44 |
| 77 |
|
| 78 |
#define GDB_SIG33 45 |
| 79 |
#define GDB_SIG34 46 |
| 80 |
#define GDB_SIG35 47 |
| 81 |
#define GDB_SIG36 48 |
| 82 |
#define GDB_SIG37 49 |
| 83 |
#define GDB_SIG38 50 |
| 84 |
#define GDB_SIG39 51 |
| 85 |
#define GDB_SIG40 52 |
| 86 |
#define GDB_SIG41 53 |
| 87 |
#define GDB_SIG42 54 |
| 88 |
#define GDB_SIG43 55 |
| 89 |
#define GDB_SIG44 56 |
| 90 |
#define GDB_SIG45 57 |
| 91 |
#define GDB_SIG46 58 |
| 92 |
#define GDB_SIG47 59 |
| 93 |
#define GDB_SIG48 60 |
| 94 |
#define GDB_SIG49 61 |
| 95 |
#define GDB_SIG50 62 |
| 96 |
#define GDB_SIG51 63 |
| 97 |
#define GDB_SIG52 64 |
| 98 |
#define GDB_SIG53 65 |
| 99 |
#define GDB_SIG54 66 |
| 100 |
#define GDB_SIG55 67 |
| 101 |
#define GDB_SIG56 68 |
| 102 |
#define GDB_SIG57 69 |
| 103 |
#define GDB_SIG58 70 |
| 104 |
#define GDB_SIG59 71 |
| 105 |
#define GDB_SIG60 72 |
| 106 |
#define GDB_SIG61 73 |
| 107 |
#define GDB_SIG62 74 |
| 108 |
#define GDB_SIG63 75 |
| 109 |
#define GDB_SIGCANCEL 76 |
| 110 |
#define GDB_SIG32 77 |
| 111 |
#define GDB_SIG64 78 |
| 112 |
#define GDB_SIG65 79 |
| 113 |
#define GDB_SIG66 80 |
| 114 |
#define GDB_SIG67 81 |
| 115 |
#define GDB_SIG68 82 |
| 116 |
#define GDB_SIG69 83 |
| 117 |
#define GDB_SIG70 84 |
| 118 |
#define GDB_SIG71 85 |
| 119 |
#define GDB_SIG72 86 |
| 120 |
#define GDB_SIG73 87 |
| 121 |
#define GDB_SIG74 88 |
| 122 |
#define GDB_SIG75 89 |
| 123 |
#define GDB_SIG76 90 |
| 124 |
#define GDB_SIG77 91 |
| 125 |
#define GDB_SIG78 92 |
| 126 |
#define GDB_SIG79 93 |
| 127 |
#define GDB_SIG80 94 |
| 128 |
#define GDB_SIG81 95 |
| 129 |
#define GDB_SIG82 96 |
| 130 |
#define GDB_SIG83 97 |
| 131 |
#define GDB_SIG84 98 |
| 132 |
#define GDB_SIG85 99 |
| 133 |
#define GDB_SIG86 100 |
| 134 |
#define GDB_SIG87 101 |
| 135 |
#define GDB_SIG88 102 |
| 136 |
#define GDB_SIG89 103 |
| 137 |
#define GDB_SIG90 104 |
| 138 |
#define GDB_SIG91 105 |
| 139 |
#define GDB_SIG92 106 |
| 140 |
#define GDB_SIG93 107 |
| 141 |
#define GDB_SIG94 108 |
| 142 |
#define GDB_SIG95 109 |
| 143 |
#define GDB_SIG96 110 |
| 144 |
#define GDB_SIG97 111 |
| 145 |
#define GDB_SIG98 112 |
| 146 |
#define GDB_SIG99 113 |
| 147 |
#define GDB_SIG100 114 |
| 148 |
#define GDB_SIG101 115 |
| 149 |
#define GDB_SIG102 116 |
| 150 |
#define GDB_SIG103 117 |
| 151 |
#define GDB_SIG104 118 |
| 152 |
#define GDB_SIG105 119 |
| 153 |
#define GDB_SIG106 120 |
| 154 |
#define GDB_SIG107 121 |
| 155 |
#define GDB_SIG108 122 |
| 156 |
#define GDB_SIG109 123 |
| 157 |
#define GDB_SIG110 124 |
| 158 |
#define GDB_SIG111 125 |
| 159 |
#define GDB_SIG112 126 |
| 160 |
#define GDB_SIG113 127 |
| 161 |
#define GDB_SIG114 128 |
| 162 |
#define GDB_SIG115 129 |
| 163 |
#define GDB_SIG116 130 |
| 164 |
#define GDB_SIG117 131 |
| 165 |
#define GDB_SIG118 132 |
| 166 |
#define GDB_SIG119 133 |
| 167 |
#define GDB_SIG120 134 |
| 168 |
#define GDB_SIG121 135 |
| 169 |
#define GDB_SIG122 136 |
| 170 |
#define GDB_SIG123 137 |
| 171 |
#define GDB_SIG124 138 |
| 172 |
#define GDB_SIG125 139 |
| 173 |
#define GDB_SIG126 140 |
| 174 |
#define GDB_SIG127 141 |
| 175 |
#define GDB_SIGINFO 142 |
| 176 |
#define GDB_UNKNOWN 143 |
| 177 |
#define GDB_DEFAULT 144 |
| 178 |
|
| 179 |
#define GDB_EXC_BAD_ACCESS 145 |
| 180 |
#define GDB_EXC_BAD_INSTRCTION 146 |
| 181 |
#define GDB_EXC_ARITHMETIC 147 |
| 182 |
#define GDB_EXC_EMULATION 148 |
| 183 |
#define GDB_EXC_SOFTWARE 149 |
| 184 |
#define GDB_EXC_BREAKPOINT 150 |
| 185 |
|
| 186 |
static unsigned char exception_to_signal[] = { |
| 187 |
[0] = GDB_SIGFPE, |
| 188 |
[1] = GDB_SIGTRAP, |
| 189 |
[2] = GDB_SIGSEGV, |
| 190 |
[3] = GDB_SIGTRAP, |
| 191 |
[4] = GDB_SIGSEGV, |
| 192 |
[5] = GDB_SIGSEGV, |
| 193 |
[6] = GDB_SIGILL, |
| 194 |
[7] = GDB_SIGSEGV, |
| 195 |
[8] = GDB_SIGSEGV, |
| 196 |
[9] = GDB_SIGFPE, |
| 197 |
[10] = GDB_SIGSEGV, |
| 198 |
[11] = GDB_SIGBUS, |
| 199 |
[12] = GDB_SIGBUS, |
| 200 |
[13] = GDB_SIGSEGV, |
| 201 |
[14] = GDB_SIGSEGV, |
| 202 |
[15] = GDB_UNKNOWN, |
| 203 |
[16] = GDB_SIGEMT, |
| 204 |
[17] = GDB_SIGBUS, |
| 205 |
[18] = GDB_SIGSEGV, |
| 206 |
[19] = GDB_SIGFPE, |
| 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, |
| 220 |
}; |
| 221 |
|
| 222 |
static const char hexchars[] = "0123456789abcdef"; |
| 223 |
static char in_buffer[BUFMAX]; |
| 224 |
static char out_buffer[BUFMAX]; |
| 225 |
|
| 226 |
static inline void stub_putc(int ch) |
| 227 |
{ |
| 228 |
gdb_tx_byte(ch); |
| 229 |
} |
| 230 |
|
| 231 |
static inline void stub_flush(void) |
| 232 |
{ |
| 233 |
gdb_tx_flush(); |
| 234 |
} |
| 235 |
|
| 236 |
static inline int stub_getc(void) |
| 237 |
{ |
| 238 |
return gdb_rx_byte(); |
| 239 |
} |
| 240 |
|
| 241 |
static int hex(char ch) |
| 242 |
{ |
| 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); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
static int parse_ulong(char **ptr, unsigned long *value) |
| 257 |
{ |
| 258 |
int digit; |
| 259 |
char *start; |
| 260 |
|
| 261 |
start = *ptr; |
| 262 |
*value = 0; |
| 263 |
|
| 264 |
while ((digit = hex(**ptr)) >= 0) { |
| 265 |
*value = ((*value) << 4) | digit; |
| 266 |
(*ptr)++; |
| 267 |
} |
| 268 |
return start != *ptr; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
static void copy_to_hex(char *buf, void *addr, unsigned long count) |
| 274 |
{ |
| 275 |
unsigned char ch; |
| 276 |
char *mem = addr; |
| 277 |
|
| 278 |
while (count--) { |
| 279 |
ch = *mem++; |
| 280 |
*buf++ = hexchars[ch >> 4]; |
| 281 |
*buf++ = hexchars[ch & 0x0f]; |
| 282 |
} |
| 283 |
*buf = 0; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
static void copy_from_hex(void *addr, char *buf, unsigned long count) |
| 289 |
{ |
| 290 |
unsigned char ch; |
| 291 |
char *mem = addr; |
| 292 |
|
| 293 |
while (count--) { |
| 294 |
ch = hex(*buf++) << 4; |
| 295 |
ch = ch + hex(*buf++); |
| 296 |
*mem++ = ch; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
static int get_packet(char *buffer) |
| 303 |
{ |
| 304 |
unsigned char checksum; |
| 305 |
unsigned char xmitcsum; |
| 306 |
int count; |
| 307 |
char ch; |
| 308 |
|
| 309 |
|
| 310 |
do { |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
while ((ch = (stub_getc() & 0x7f)) != '$') |
| 315 |
; |
| 316 |
checksum = 0; |
| 317 |
xmitcsum = -1; |
| 318 |
|
| 319 |
count = 0; |
| 320 |
|
| 321 |
|
| 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; |
| 329 |
} |
| 330 |
buffer[count] = 0; |
| 331 |
|
| 332 |
if (ch == '#') { |
| 333 |
xmitcsum = hex(stub_getc() & 0x7f) << 4; |
| 334 |
xmitcsum += hex(stub_getc() & 0x7f); |
| 335 |
|
| 336 |
if (checksum != xmitcsum) { |
| 337 |
stub_putc('-'); |
| 338 |
stub_flush(); |
| 339 |
} else { |
| 340 |
stub_putc('+'); |
| 341 |
stub_flush(); |
| 342 |
} |
| 343 |
} |
| 344 |
} while (checksum != xmitcsum); |
| 345 |
return 1; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
static void put_packet(char *buffer) |
| 350 |
{ |
| 351 |
unsigned char checksum; |
| 352 |
int count; |
| 353 |
char ch; |
| 354 |
|
| 355 |
|
| 356 |
do { |
| 357 |
stub_putc('$'); |
| 358 |
checksum = 0; |
| 359 |
count = 0; |
| 360 |
|
| 361 |
while ((ch = buffer[count])) { |
| 362 |
stub_putc(ch); |
| 363 |
checksum += ch; |
| 364 |
count += 1; |
| 365 |
} |
| 366 |
|
| 367 |
stub_putc('#'); |
| 368 |
stub_putc(hexchars[checksum >> 4]); |
| 369 |
stub_putc(hexchars[checksum % 16]); |
| 370 |
stub_flush(); |
| 371 |
|
| 372 |
} while ((stub_getc() & 0x7f) != '+'); |
| 373 |
} |
| 374 |
#endif |
| 375 |
|
| 376 |
#define DEBUG_VECTOR 1 |
| 377 |
|
| 378 |
void x86_exception(struct eregs *info); |
| 379 |
|
| 380 |
void x86_exception(struct eregs *info) |
| 381 |
{ |
| 382 |
#if CONFIG(GDB_STUB) |
| 383 |
|
| 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]; |
| 392 |
|
| 393 |
|
| 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); |
| 399 |
|
| 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 '?': |
| 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': |
| 415 |
copy_to_hex(out_buffer, &gdb_stub_registers, |
| 416 |
sizeof(gdb_stub_registers)); |
| 417 |
break; |
| 418 |
case 'G': |
| 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 |
|
| 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 |
|
| 439 |
|
| 440 |
|
| 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 |
|
| 454 |
|
| 455 |
|
| 456 |
ptr = &in_buffer[1]; |
| 457 |
if (parse_ulong(&ptr, &addr)) |
| 458 |
info->eip = addr; |
| 459 |
|
| 460 |
|
| 461 |
info->eflags &= ~(1 << 8); |
| 462 |
|
| 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': |
| 470 |
break; |
| 471 |
case 'q': |
| 472 |
break; |
| 473 |
case 'z': |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
case 'Z': |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
break; |
| 484 |
default: |
| 485 |
break; |
| 486 |
} |
| 487 |
put_packet(out_buffer); |
| 488 |
} |
| 489 |
#else |
| 490 |
|
| 491 |
int logical_processor = 0; |
| 492 |
|
| 493 |
if (info->vector == DEBUG_VECTOR) { |
| 494 |
if (breakpoint_dispatch_handler(info) == 0) |
| 495 |
return; |
| 496 |
} |
| 497 |
|
| 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 |
| 526 |
|
| 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 |
|
| 541 |
|
| 542 |
|
| 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]); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 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"); |
| 559 |
} |
| 560 |
|
| 561 |
die("\n"); |
| 562 |
#endif |
| 563 |
} |
| 564 |
|
| 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) |
| 569 |
|
| 570 |
#define IGATE_FLAGS (GATE_P | GATE_DPL(0) | GATE_SIZE_32 | (0x6 << 8)) |
| 571 |
|
| 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; |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 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[]; |
| 592 |
|
| 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, |
| 599 |
}; |
| 600 |
|
| 601 |
static struct intr_gate idt[ARRAY_SIZE(intr_entries)] __aligned(8); |
| 602 |
|
| 603 |
static inline uint16_t get_cs(void) |
| 604 |
{ |
| 605 |
uint16_t segment; |
| 606 |
|
| 607 |
asm volatile ( |
| 608 |
"mov %%cs, %0\n" |
| 609 |
: "=r" (segment) |
| 610 |
: |
| 611 |
: "memory" |
| 612 |
); |
| 613 |
|
| 614 |
return segment; |
| 615 |
} |
| 616 |
|
| 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; |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
struct lidtarg idtarg; |
| 629 |
|
| 630 |
static void load_idt(void *table, size_t sz) |
| 631 |
{ |
| 632 |
struct lidtarg lidtarg = { |
| 633 |
.limit = sz - 1, |
| 634 |
.base = (uintptr_t)table, |
| 635 |
}; |
| 636 |
|
| 637 |
asm volatile ( |
| 638 |
"lidt %0" |
| 639 |
: |
| 640 |
: "m" (lidtarg) |
| 641 |
: "memory" |
| 642 |
); |
| 643 |
|
| 644 |
if (ENV_RAMSTAGE) |
| 645 |
memcpy(&idtarg, &lidtarg, sizeof(idtarg)); |
| 646 |
} |
| 647 |
|
| 648 |
asmlinkage void exception_init(void) |
| 649 |
{ |
| 650 |
int i; |
| 651 |
uint16_t segment; |
| 652 |
|
| 653 |
segment = get_cs(); |
| 654 |
|
| 655 |
|
| 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 |
| 664 |
} |
| 665 |
|
| 666 |
load_idt(idt, sizeof(idt)); |
| 667 |
|
| 668 |
#if !ENV_SMM |
| 669 |
null_breakpoint_init(); |
| 670 |
stack_canary_breakpoint_init(); |
| 671 |
#endif |
| 672 |
} |