rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2009 Corey Tabaka |
| 3 | * Copyright (c) 2015 Intel Corporation |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files |
| 7 | * (the "Software"), to deal in the Software without restriction, |
| 8 | * including without limitation the rights to use, copy, modify, merge, |
| 9 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 10 | * and to permit persons to whom the Software is furnished to do so, |
| 11 | * subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be |
| 14 | * included in all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 20 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 22 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | #include <debug.h> |
| 25 | #include <arch/x86.h> |
| 26 | #include <kernel/thread.h> |
| 27 | |
| 28 | |
| 29 | static void dump_fault_frame(struct x86_iframe *frame) |
| 30 | { |
| 31 | dprintf(CRITICAL, " CS: %04x EIP: %08x EFL: %08x CR2: %08x\n", |
| 32 | frame->cs, frame->eip, frame->eflags, x86_get_cr2()); |
| 33 | dprintf(CRITICAL, "EAX: %08x ECX: %08x EDX: %08x EBX: %08x\n", |
| 34 | frame->eax, frame->ecx, frame->edx, frame->ebx); |
| 35 | dprintf(CRITICAL, "ESP: %08x EBP: %08x ESI: %08x EDI: %08x\n", |
| 36 | frame->esp, frame->ebp, frame->esi, frame->edi); |
| 37 | dprintf(CRITICAL, " DS: %04x ES: %04x FS: %04x GS: %04x\n", |
| 38 | frame->ds, frame->es, frame->fs, frame->gs); |
| 39 | |
| 40 | // dump the bottom of the current stack |
| 41 | addr_t stack = (addr_t) frame; //(addr_t) (((uint32_t *) frame) + (sizeof(struct x86_iframe) / sizeof(uint32_t) - 1)); |
| 42 | |
| 43 | if (stack != 0) { |
| 44 | dprintf(CRITICAL, "bottom of stack at 0x%08x:\n", (unsigned int)stack); |
| 45 | hexdump((void *)stack, 192); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | static void exception_die(struct x86_iframe *frame, const char *msg) |
| 50 | { |
| 51 | dprintf(CRITICAL, msg); |
| 52 | dump_fault_frame(frame); |
| 53 | |
| 54 | for (;;) { |
| 55 | x86_cli(); |
| 56 | x86_hlt(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void x86_syscall_handler(struct x86_iframe *frame) |
| 61 | { |
| 62 | exception_die(frame, "unhandled syscall, halting\n"); |
| 63 | } |
| 64 | |
| 65 | void x86_gpf_handler(struct x86_iframe *frame) |
| 66 | { |
| 67 | exception_die(frame, "unhandled gpf, halting\n"); |
| 68 | } |
| 69 | |
| 70 | void x86_invop_handler(struct x86_iframe *frame) |
| 71 | { |
| 72 | exception_die(frame, "unhandled invalid op, halting\n"); |
| 73 | } |
| 74 | |
| 75 | void x86_unhandled_exception(struct x86_iframe *frame) |
| 76 | { |
| 77 | exception_die(frame, "unhandled exception, halting\n"); |
| 78 | } |
| 79 | |
| 80 | void x86_pfe_handler(struct x86_iframe *frame) |
| 81 | { |
| 82 | /* Handle a page fault exception */ |
| 83 | uint32_t error_code; |
| 84 | thread_t *current_thread; |
| 85 | error_code = frame->err_code; |
| 86 | |
| 87 | #ifdef PAGE_FAULT_DEBUG_INFO |
| 88 | addr_t v_addr, ssp, esp, ip, rip; |
| 89 | v_addr = x86_get_cr2(); |
| 90 | |
| 91 | ssp = frame->user_ss & X86_8BYTE_MASK; |
| 92 | esp = frame->user_esp; |
| 93 | ip = frame->cs & X86_8BYTE_MASK; |
| 94 | rip = frame->eip; |
| 95 | |
| 96 | dprintf(CRITICAL, "<PAGE FAULT> Instruction Pointer = 0x%x:0x%x\n", |
| 97 | (unsigned int)ip, |
| 98 | (unsigned int)rip); |
| 99 | dprintf(CRITICAL, "<PAGE FAULT> Stack Pointer = 0x%x:0x%x\n", |
| 100 | (unsigned int)ssp, |
| 101 | (unsigned int)esp); |
| 102 | dprintf(CRITICAL, "<PAGE FAULT> Fault Linear Address = 0x%x\n", |
| 103 | (unsigned int)v_addr); |
| 104 | dprintf(CRITICAL, "<PAGE FAULT> Error Code Value = 0x%x\n", |
| 105 | error_code); |
| 106 | dprintf(CRITICAL, "<PAGE FAULT> Error Code Type = %s %s %s%s, %s\n", |
| 107 | error_code & PFEX_U ? "user" : "supervisor", |
| 108 | error_code & PFEX_W ? "write" : "read", |
| 109 | error_code & PFEX_I ? "instruction" : "data", |
| 110 | error_code & PFEX_RSV ? " rsv" : "", |
| 111 | error_code & PFEX_P ? "protection violation" : "page not present"); |
| 112 | #endif |
| 113 | |
| 114 | current_thread = get_current_thread(); |
| 115 | dump_thread(current_thread); |
| 116 | |
| 117 | if (error_code & PFEX_U) { |
| 118 | // User mode page fault |
| 119 | switch (error_code) { |
| 120 | case 4: |
| 121 | case 5: |
| 122 | case 6: |
| 123 | case 7: |
| 124 | #ifdef PAGE_FAULT_DEBUG_INFO |
| 125 | thread_detach(current_thread); |
| 126 | #else |
| 127 | thread_exit(current_thread->retcode); |
| 128 | #endif |
| 129 | break; |
| 130 | } |
| 131 | } else { |
| 132 | // Supervisor mode page fault |
| 133 | switch (error_code) { |
| 134 | |
| 135 | case 0: |
| 136 | case 1: |
| 137 | case 2: |
| 138 | case 3: |
| 139 | exception_die(frame, "Page Fault exception, halting\n"); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | } |