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 | #include <arch/arch_ops.h> |
| 28 | |
| 29 | static void dump_fault_frame(struct x86_iframe *frame) |
| 30 | { |
| 31 | |
| 32 | dprintf(CRITICAL, " CS: %04llx EIP: %08llx EFL: %08llx CR2: %08llx\n", |
| 33 | frame->cs, frame->rip, frame->rflags, x86_get_cr2()); |
| 34 | /* dprintf(CRITICAL, "EAX: %08x ECX: %08x EDX: %08x EBX: %08x\n", |
| 35 | frame->rax, frame->rcx, frame->rdx, frame->rbx); |
| 36 | dprintf(CRITICAL, "ESP: %08x EBP: %08x ESI: %08x EDI: %08x\n", |
| 37 | frame->rsp, frame->rbp, frame->rsi, frame->rdi); |
| 38 | dprintf(CRITICAL, " DS: %04x ES: %04x FS: %04x GS: %04x\n", |
| 39 | frame->ds, frame->es, frame->fs, frame->gs); |
| 40 | */ |
| 41 | |
| 42 | // dump the bottom of the current stack |
| 43 | addr_t stack = (addr_t) frame; //(addr_t) (((uint32_t *) frame) + (sizeof(struct x86_iframe) / sizeof(uint32_t) - 1)); |
| 44 | |
| 45 | if (stack != 0) { |
| 46 | dprintf(CRITICAL, "bottom of stack at 0x%08x:\n", (unsigned int)stack); |
| 47 | hexdump((void *)stack, 192); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static void exception_die(struct x86_iframe *frame, const char *msg) |
| 52 | { |
| 53 | dprintf(CRITICAL, msg); |
| 54 | dump_fault_frame(frame); |
| 55 | |
| 56 | for (;;) { |
| 57 | x86_cli(); |
| 58 | x86_hlt(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void x86_syscall_handler(struct x86_iframe *frame) |
| 63 | { |
| 64 | exception_die(frame, "unhandled syscall, halting\n"); |
| 65 | } |
| 66 | |
| 67 | void x86_gpf_handler(struct x86_iframe *frame) |
| 68 | { |
| 69 | exception_die(frame, "unhandled gpf, halting\n"); |
| 70 | } |
| 71 | |
| 72 | void x86_invop_handler(struct x86_iframe *frame) |
| 73 | { |
| 74 | exception_die(frame, "unhandled invalid op, halting\n"); |
| 75 | } |
| 76 | |
| 77 | void x86_unhandled_exception(struct x86_iframe *frame) |
| 78 | { |
| 79 | exception_die(frame, "unhandled exception, halting\n"); |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Page fault handler for x86-64 |
| 84 | */ |
| 85 | void x86_pfe_handler(struct x86_iframe *frame) |
| 86 | { |
| 87 | /* Handle a page fault exception */ |
| 88 | uint32_t error_code; |
| 89 | thread_t *current_thread; |
| 90 | error_code = frame->err_code; |
| 91 | |
| 92 | #ifdef PAGE_FAULT_DEBUG_INFO |
| 93 | uint64_t v_addr, ssp, esp, ip, rip; |
| 94 | |
| 95 | v_addr = x86_get_cr2(); |
| 96 | ssp = frame->user_ss & X86_8BYTE_MASK; |
| 97 | esp = frame->user_rsp; |
| 98 | ip = frame->cs & X86_8BYTE_MASK; |
| 99 | rip = frame->rip; |
| 100 | |
| 101 | dprintf(SPEW, "<PAGE FAULT> Instruction Pointer = 0x%x:0x%x\n", |
| 102 | (unsigned int)ip, |
| 103 | (unsigned int)rip); |
| 104 | dprintf(SPEW, "<PAGE FAULT> Stack Pointer = 0x%x:0x%x\n", |
| 105 | (unsigned int)ssp, |
| 106 | (unsigned int)esp); |
| 107 | dprintf(SPEW, "<PAGE FAULT> Fault Linear Address = 0x%x\n", |
| 108 | (unsigned int)v_addr); |
| 109 | dprintf(SPEW, "<PAGE FAULT> Error Code Value = 0x%x\n", |
| 110 | error_code); |
| 111 | dprintf(SPEW, "<PAGE FAULT> Error Code Type = %s %s %s%s, %s\n", |
| 112 | error_code & PFEX_U ? "user" : "supervisor", |
| 113 | error_code & PFEX_W ? "write" : "read", |
| 114 | error_code & PFEX_I ? "instruction" : "data", |
| 115 | error_code & PFEX_RSV ? " rsv" : "", |
| 116 | error_code & PFEX_P ? "protection violation" : "page not present"); |
| 117 | #endif |
| 118 | |
| 119 | current_thread = get_current_thread(); |
| 120 | dump_thread(current_thread); |
| 121 | |
| 122 | if (error_code & PFEX_U) { |
| 123 | // User mode page fault |
| 124 | switch (error_code) { |
| 125 | case 4: |
| 126 | case 5: |
| 127 | case 6: |
| 128 | case 7: |
| 129 | #ifdef PAGE_FAULT_DEBUG_INFO |
| 130 | thread_detach(current_thread); |
| 131 | #else |
| 132 | thread_exit(current_thread->retcode); |
| 133 | #endif |
| 134 | break; |
| 135 | } |
| 136 | } else { |
| 137 | // Supervisor mode page fault |
| 138 | switch (error_code) { |
| 139 | case 0: |
| 140 | case 1: |
| 141 | case 2: |
| 142 | case 3: |
| 143 | exception_die(frame, "Page Fault exception, halting\n"); |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | } |