[25] | 1 | /* |
---|
[142] | 2 | * hal_exception.c - Implementation of exception handler for x86_64 |
---|
[25] | 3 | * |
---|
[142] | 4 | * Copyright (c) 2017 Maxime Villard |
---|
[25] | 5 | * |
---|
| 6 | * This file is part of ALMOS-MKH. |
---|
| 7 | * |
---|
| 8 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 9 | * under the terms of the GNU General Public License as published by |
---|
| 10 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 11 | * |
---|
| 12 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 15 | * General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
[234] | 18 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[25] | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 20 | */ |
---|
| 21 | |
---|
[457] | 22 | #include <hal_kernel_types.h> |
---|
[25] | 23 | #include <hal_irqmask.h> |
---|
| 24 | #include <hal_exception.h> |
---|
| 25 | #include <thread.h> |
---|
| 26 | #include <errno.h> |
---|
| 27 | #include <core.h> |
---|
| 28 | |
---|
[142] | 29 | #include <hal_kentry.h> |
---|
[48] | 30 | #include <hal_internal.h> |
---|
[276] | 31 | #include <hal_segmentation.h> |
---|
[48] | 32 | |
---|
[142] | 33 | static const char *exc_type[] = { |
---|
| 34 | [T_PRIVINFLT]= "privileged instruction fault", |
---|
| 35 | [T_BPTFLT] = "breakpoint trap", |
---|
| 36 | [T_ARITHTRAP] = "arithmetic trap", |
---|
| 37 | [T_ASTFLT] = "asynchronous system trap", |
---|
| 38 | [T_PROTFLT] = "protection fault", |
---|
| 39 | [T_TRCTRAP] = "trace trap", |
---|
| 40 | [T_PAGEFLT] = "page fault", |
---|
| 41 | [T_ALIGNFLT] = "alignment fault", |
---|
| 42 | [T_DIVIDE] = "integer divide fault", |
---|
| 43 | [T_NMI] = "non-maskable interrupt", |
---|
| 44 | [T_OFLOW] = "overflow trap", |
---|
| 45 | [T_BOUND] = "bounds check fault", |
---|
| 46 | [T_DNA] = "FPU not available fault", |
---|
| 47 | [T_DOUBLEFLT] = "double fault", |
---|
| 48 | [T_FPOPFLT] = "FPU operand fetch fault", |
---|
| 49 | [T_TSSFLT] = "invalid TSS fault", |
---|
| 50 | [T_SEGNPFLT] = "segment not present fault", |
---|
| 51 | [T_STKFLT] = "stack fault", |
---|
| 52 | [T_MCA] = "machine check fault", |
---|
| 53 | [T_XMM] = "SSE FP exception", |
---|
| 54 | }; |
---|
| 55 | int exc_types = __arraycount(exc_type); |
---|
| 56 | |
---|
[371] | 57 | static void hal_exception_kern(hal_trapframe_t *tf) |
---|
[142] | 58 | { |
---|
[371] | 59 | uint64_t trapno = tf->tf_trapno; |
---|
[142] | 60 | const char *buf; |
---|
| 61 | |
---|
[371] | 62 | if (trapno < exc_types) { |
---|
| 63 | buf = exc_type[trapno]; |
---|
[142] | 64 | } else { |
---|
| 65 | buf = "unknown exception"; |
---|
| 66 | } |
---|
| 67 | |
---|
[242] | 68 | x86_lock(); |
---|
[371] | 69 | x86_printf("\n****** EXCEPTION OCCURRED IN KERNEL MODE ******\n"); |
---|
[142] | 70 | x86_printf("%s\n", (char *)buf); |
---|
[371] | 71 | x86_printf("-> rip = %Z\n", tf->tf_rip); |
---|
| 72 | x86_printf("-> rdi = %Z\n", tf->tf_rdi); |
---|
| 73 | x86_printf("-> rbp = %Z\n", tf->tf_rbp); |
---|
[336] | 74 | x86_printf("-> tls = %Z (gid=%Z)\n", (uint64_t)curtls(), |
---|
[276] | 75 | (uint64_t)hal_get_gid()); |
---|
[371] | 76 | x86_printf("-> err = %Z\n", tf->tf_err); |
---|
| 77 | if (trapno == T_PAGEFLT) |
---|
[142] | 78 | x86_printf("-> va = %Z\n", rcr2()); |
---|
[371] | 79 | x86_printf("\n***********************************************\n"); |
---|
[242] | 80 | x86_unlock(); |
---|
[142] | 81 | |
---|
| 82 | while (1); |
---|
| 83 | } |
---|
| 84 | |
---|
[371] | 85 | static void hal_exception_user(hal_trapframe_t *tf) |
---|
| 86 | { |
---|
[392] | 87 | thread_t *thread = curtls()->tls_thr; |
---|
| 88 | process_t *process = thread->process; |
---|
| 89 | intptr_t bad_vaddr; |
---|
| 90 | error_t error; |
---|
| 91 | |
---|
[371] | 92 | switch (tf->tf_trapno) { |
---|
| 93 | case T_PAGEFLT: |
---|
[392] | 94 | bad_vaddr = rcr2(); |
---|
[371] | 95 | |
---|
[403] | 96 | error = vmm_handle_page_fault(process, |
---|
[392] | 97 | bad_vaddr >> CONFIG_PPM_PAGE_SHIFT); |
---|
| 98 | |
---|
| 99 | x86_printf("VA=%Z ERROR=%Z\n", bad_vaddr, (uint64_t)error); |
---|
| 100 | break; |
---|
| 101 | |
---|
[371] | 102 | default: |
---|
| 103 | x86_panic("userland not yet handled"); |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /* |
---|
| 108 | * Exception handler. |
---|
| 109 | */ |
---|
| 110 | void hal_exception_entry(hal_trapframe_t *tf) |
---|
| 111 | { |
---|
| 112 | if ((tf->tf_cs & SEL_RPL) == SEL_UPL) |
---|
| 113 | hal_exception_user(tf); |
---|
| 114 | else |
---|
| 115 | hal_exception_kern(tf); |
---|
| 116 | } |
---|
| 117 | |
---|
[142] | 118 | /* -------------------------------------------------------------------------- */ |
---|
| 119 | |
---|
| 120 | void hal_do_exception( thread_t * this, |
---|
[25] | 121 | reg_t * regs_tbl ) |
---|
| 122 | { |
---|
[48] | 123 | x86_panic((char *)__func__); |
---|
[25] | 124 | } |
---|
| 125 | |
---|
| 126 | void hal_exception_dump( thread_t * this, |
---|
| 127 | reg_t * regs_tbl ) |
---|
| 128 | { |
---|
[48] | 129 | x86_panic((char *)__func__); |
---|
[25] | 130 | } |
---|
| 131 | |
---|