[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 | |
---|
| 22 | #include <hal_types.h> |
---|
| 23 | #include <hal_irqmask.h> |
---|
| 24 | #include <hal_exception.h> |
---|
| 25 | #include <thread.h> |
---|
| 26 | #include <errno.h> |
---|
| 27 | #include <core.h> |
---|
| 28 | #include <do_exception.h> |
---|
| 29 | |
---|
[142] | 30 | #include <hal_kentry.h> |
---|
[48] | 31 | #include <hal_internal.h> |
---|
[276] | 32 | #include <hal_segmentation.h> |
---|
[48] | 33 | |
---|
[142] | 34 | static const char *exc_type[] = { |
---|
| 35 | [T_PRIVINFLT]= "privileged instruction fault", |
---|
| 36 | [T_BPTFLT] = "breakpoint trap", |
---|
| 37 | [T_ARITHTRAP] = "arithmetic trap", |
---|
| 38 | [T_ASTFLT] = "asynchronous system trap", |
---|
| 39 | [T_PROTFLT] = "protection fault", |
---|
| 40 | [T_TRCTRAP] = "trace trap", |
---|
| 41 | [T_PAGEFLT] = "page fault", |
---|
| 42 | [T_ALIGNFLT] = "alignment fault", |
---|
| 43 | [T_DIVIDE] = "integer divide fault", |
---|
| 44 | [T_NMI] = "non-maskable interrupt", |
---|
| 45 | [T_OFLOW] = "overflow trap", |
---|
| 46 | [T_BOUND] = "bounds check fault", |
---|
| 47 | [T_DNA] = "FPU not available fault", |
---|
| 48 | [T_DOUBLEFLT] = "double fault", |
---|
| 49 | [T_FPOPFLT] = "FPU operand fetch fault", |
---|
| 50 | [T_TSSFLT] = "invalid TSS fault", |
---|
| 51 | [T_SEGNPFLT] = "segment not present fault", |
---|
| 52 | [T_STKFLT] = "stack fault", |
---|
| 53 | [T_MCA] = "machine check fault", |
---|
| 54 | [T_XMM] = "SSE FP exception", |
---|
| 55 | }; |
---|
| 56 | int exc_types = __arraycount(exc_type); |
---|
| 57 | |
---|
| 58 | /* |
---|
| 59 | * Hexception handler. |
---|
| 60 | */ |
---|
[233] | 61 | void hal_exception_entry(hal_cpu_context_t *ctx) |
---|
[142] | 62 | { |
---|
[233] | 63 | uint64_t excno = ctx->tf_trapno; |
---|
[142] | 64 | const char *buf; |
---|
| 65 | |
---|
| 66 | if (excno < exc_types) { |
---|
| 67 | buf = exc_type[excno]; |
---|
| 68 | } else { |
---|
| 69 | buf = "unknown exception"; |
---|
| 70 | } |
---|
| 71 | |
---|
[242] | 72 | x86_lock(); |
---|
[143] | 73 | x86_printf("\n****** EXCEPTION OCCURRED ******\n"); |
---|
[142] | 74 | x86_printf("%s\n", (char *)buf); |
---|
[233] | 75 | x86_printf("-> rip = %Z\n", ctx->tf_rip); |
---|
[308] | 76 | x86_printf("-> rdi = %Z\n", ctx->tf_rdi); |
---|
| 77 | x86_printf("-> rbp = %Z\n", ctx->tf_rbp); |
---|
[276] | 78 | x86_printf("-> tls = %Z (gid=%Z)\n", (uint64_t)curcpu(), |
---|
| 79 | (uint64_t)hal_get_gid()); |
---|
[233] | 80 | x86_printf("-> err = %Z\n", ctx->tf_err); |
---|
[142] | 81 | if (excno == T_PAGEFLT) |
---|
| 82 | x86_printf("-> va = %Z\n", rcr2()); |
---|
[143] | 83 | x86_printf("****** EXCEPTION OCCURRED ******\n\n"); |
---|
[242] | 84 | x86_unlock(); |
---|
[142] | 85 | |
---|
| 86 | while (1); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /* -------------------------------------------------------------------------- */ |
---|
| 90 | |
---|
| 91 | void hal_do_exception( thread_t * this, |
---|
[25] | 92 | reg_t * regs_tbl ) |
---|
| 93 | { |
---|
[48] | 94 | x86_panic((char *)__func__); |
---|
[25] | 95 | } |
---|
| 96 | |
---|
| 97 | void hal_exception_dump( thread_t * this, |
---|
| 98 | reg_t * regs_tbl ) |
---|
| 99 | { |
---|
[48] | 100 | x86_panic((char *)__func__); |
---|
[25] | 101 | } |
---|
| 102 | |
---|