[29] | 1 | /* |
---|
| 2 | * hal_trap.c - Trap handler |
---|
| 3 | * |
---|
| 4 | * Copyright (c) 2017 Maxime Villard |
---|
| 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 |
---|
| 18 | * along with ALMOS-MKH.; if not, write to the Free Software Foundation, |
---|
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include <hal_types.h> |
---|
| 23 | #include <hal_kentry.h> |
---|
[91] | 24 | #include <hal_internal.h> |
---|
[29] | 25 | |
---|
| 26 | static const char *trap_type[] = { |
---|
| 27 | [T_PRIVINFLT]= "privileged instruction fault", |
---|
| 28 | [T_BPTFLT] = "breakpoint trap", |
---|
| 29 | [T_ARITHTRAP] = "arithmetic trap", |
---|
| 30 | [T_ASTFLT] = "asynchronous system trap", |
---|
| 31 | [T_PROTFLT] = "protection fault", |
---|
| 32 | [T_TRCTRAP] = "trace trap", |
---|
| 33 | [T_PAGEFLT] = "page fault", |
---|
| 34 | [T_ALIGNFLT] = "alignment fault", |
---|
| 35 | [T_DIVIDE] = "integer divide fault", |
---|
| 36 | [T_NMI] = "non-maskable interrupt", |
---|
| 37 | [T_OFLOW] = "overflow trap", |
---|
| 38 | [T_BOUND] = "bounds check fault", |
---|
| 39 | [T_DNA] = "FPU not available fault", |
---|
| 40 | [T_DOUBLEFLT] = "double fault", |
---|
| 41 | [T_FPOPFLT] = "FPU operand fetch fault", |
---|
| 42 | [T_TSSFLT] = "invalid TSS fault", |
---|
| 43 | [T_SEGNPFLT] = "segment not present fault", |
---|
| 44 | [T_STKFLT] = "stack fault", |
---|
| 45 | [T_MCA] = "machine check fault", |
---|
| 46 | [T_XMM] = "SSE FP exception", |
---|
| 47 | }; |
---|
| 48 | int trap_types = __arraycount(trap_type); |
---|
| 49 | |
---|
[31] | 50 | void x86_printf(char *s, ...); |
---|
[29] | 51 | |
---|
| 52 | /* |
---|
| 53 | * Trap handler. |
---|
| 54 | */ |
---|
[86] | 55 | void hal_trap_entry(struct small_trapframe *tf) |
---|
[29] | 56 | { |
---|
| 57 | uint64_t trapno = tf->tf_trapno; |
---|
[31] | 58 | const char *buf; |
---|
[29] | 59 | |
---|
| 60 | if (trapno < trap_types) { |
---|
| 61 | buf = trap_type[trapno]; |
---|
| 62 | } else { |
---|
| 63 | buf = "unknown trap"; |
---|
| 64 | } |
---|
| 65 | |
---|
[78] | 66 | x86_printf("\n****** FAULT OCCURRED ******\n"); |
---|
| 67 | x86_printf("%s\n", (char *)buf); |
---|
| 68 | x86_printf("-> rip = %Z\n", tf->tf_rip); |
---|
[82] | 69 | x86_printf("-> rsp = %Z\n", tf->tf_rsp); |
---|
[86] | 70 | x86_printf("-> err = %Z\n", tf->tf_err); |
---|
[78] | 71 | x86_printf("****** FAULT OCCURRED ******\n\n"); |
---|
[29] | 72 | |
---|
| 73 | while (1); |
---|
| 74 | } |
---|
| 75 | |
---|
[86] | 76 | /* |
---|
| 77 | * Timer interrupt |
---|
| 78 | */ |
---|
| 79 | void hal_timer_intr(struct trapframe *tf) |
---|
| 80 | { |
---|
| 81 | x86_printf("-> got timer: rip=%Z\n", tf->tf_rip); |
---|
| 82 | return; |
---|
| 83 | } |
---|
[29] | 84 | |
---|
[91] | 85 | /* -------------------------------------------------------------------------- */ |
---|
| 86 | |
---|
| 87 | #define PS2_DATA 0x60 |
---|
| 88 | |
---|
| 89 | #define PS2_STATUS 0x64 |
---|
| 90 | # define STATUS_OUT_FULL 0x00 |
---|
| 91 | # define STATUS_IN_FULL 0x01 |
---|
| 92 | # define STATUS_SYS_FLAG 0x02 |
---|
| 93 | |
---|
| 94 | #define PS2_CMD 0x64 |
---|
| 95 | |
---|
[89] | 96 | /* |
---|
[91] | 97 | * US scancode table, found on the internet. |
---|
[89] | 98 | */ |
---|
[91] | 99 | unsigned char scancode_US[128] = |
---|
| 100 | { |
---|
| 101 | 0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', |
---|
| 102 | '\b', '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', |
---|
| 103 | '\n', |
---|
| 104 | 0, /* 29 - Control */ |
---|
| 105 | 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', |
---|
| 106 | 0, /* Left shift */ |
---|
| 107 | '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', |
---|
| 108 | 0, /* Right shift */ |
---|
| 109 | '*', |
---|
| 110 | 0, /* Alt */ |
---|
| 111 | ' ', /* Space bar */ |
---|
| 112 | 0, /* Caps lock */ |
---|
| 113 | 0, /* 59 - F1 key ... > */ |
---|
| 114 | 0, 0, 0, 0, 0, 0, 0, 0, |
---|
| 115 | 0, /* < ... F10 */ |
---|
| 116 | 0, /* 69 - Num lock*/ |
---|
| 117 | 0, /* Scroll Lock */ |
---|
| 118 | 0, /* Home key */ |
---|
| 119 | 0, /* Up Arrow */ |
---|
| 120 | 0, /* Page Up */ |
---|
| 121 | '-', |
---|
| 122 | 0, /* Left Arrow */ |
---|
| 123 | 0, |
---|
| 124 | 0, /* Right Arrow */ |
---|
| 125 | '+', |
---|
| 126 | 0, /* 79 - End key*/ |
---|
| 127 | 0, /* Down Arrow */ |
---|
| 128 | 0, /* Page Down */ |
---|
| 129 | 0, /* Insert Key */ |
---|
| 130 | 0, /* Delete Key */ |
---|
| 131 | 0, 0, 0, |
---|
| 132 | 0, /* F11 Key */ |
---|
| 133 | 0, /* F12 Key */ |
---|
| 134 | 0, /* All other keys are undefined */ |
---|
| 135 | }; |
---|
| 136 | |
---|
| 137 | /* |
---|
| 138 | * Keyboard interrupt (8042 PS/2) |
---|
| 139 | */ |
---|
[89] | 140 | void hal_keyboard_intr(struct trapframe *tf) |
---|
| 141 | { |
---|
[91] | 142 | uint64_t val; |
---|
| 143 | |
---|
| 144 | do { |
---|
| 145 | val = in8(PS2_STATUS); |
---|
| 146 | } while ((val & STATUS_IN_FULL) == 0); |
---|
| 147 | |
---|
| 148 | val = in8(PS2_DATA); |
---|
| 149 | |
---|
| 150 | x86_printf("-> got keyboard: rip=%Z key=%c ", tf->tf_rip, scancode_US[val]); |
---|
| 151 | |
---|
| 152 | if (val & 0x80) |
---|
| 153 | x86_printf("[released]\n"); |
---|
| 154 | else |
---|
| 155 | x86_printf("[pressed]\n"); |
---|
| 156 | |
---|
[89] | 157 | return; |
---|
| 158 | } |
---|
| 159 | |
---|