source: trunk/hal/x86_64/core/hal_trap.c @ 113

Last change on this file since 113 was 99, checked in by max@…, 7 years ago

add the irq functions; we will forbid nested critical sections, so
the argument will disappear soon

File size: 3.9 KB
Line 
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>
24#include <hal_internal.h>
25
26static 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};
48int     trap_types = __arraycount(trap_type);
49
50void x86_printf(char *s, ...);
51
52/*
53 * Trap handler.
54 */
55void hal_trap_entry(struct small_trapframe *tf)
56{
57        uint64_t trapno = tf->tf_trapno;
58        const char *buf;
59
60        if (trapno < trap_types) {
61                buf = trap_type[trapno];
62        } else {
63                buf = "unknown trap";
64        }
65
66        x86_printf("\n****** FAULT OCCURRED ******\n");
67        x86_printf("%s\n", (char *)buf);
68        x86_printf("-> rip = %Z\n", tf->tf_rip);
69        x86_printf("-> rsp = %Z\n", tf->tf_rsp);
70        x86_printf("-> err = %Z\n", tf->tf_err);
71        if (trapno == T_PAGEFLT)
72                x86_printf("-> va  = %Z\n", rcr2());
73        x86_printf("****** FAULT OCCURRED ******\n\n");
74
75        while (1);
76}
77
78/*
79 * Timer interrupt
80 */
81void hal_timer_intr(struct trapframe *tf)
82{
83        x86_printf("-> got timer: rip=%Z\n", tf->tf_rip);
84        return;
85}
86
87/* -------------------------------------------------------------------------- */
88
89#define PS2_DATA        0x60
90
91#define PS2_STATUS      0x64
92#       define STATUS_OUT_FULL  0x00
93#       define STATUS_IN_FULL   0x01
94#       define STATUS_SYS_FLAG  0x02
95
96#define PS2_CMD 0x64
97
98/*
99 * US scancode table, found on the internet.
100 */
101unsigned char scancode_US[128] =
102{
103         0,     27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
104        '\b', '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']',
105        '\n',
106         0,     /* 29   - Control */
107        'a',   's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`',
108         0,     /* Left shift */
109        '\\',  'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/',
110         0,     /* Right shift */
111        '*',
112         0,     /* Alt */
113        ' ',    /* Space bar */
114         0,     /* Caps lock */
115         0,     /* 59 - F1 key ... > */
116         0,   0,   0,   0,   0,   0,   0,   0,
117         0,     /* < ... F10 */
118         0,     /* 69 - Num lock*/
119         0,     /* Scroll Lock */
120         0,     /* Home key */
121         0,     /* Up Arrow */
122         0,     /* Page Up */
123        '-',
124         0,     /* Left Arrow */
125         0,
126         0,     /* Right Arrow */
127        '+',
128         0,     /* 79 - End key*/
129         0,     /* Down Arrow */
130         0,     /* Page Down */
131         0,     /* Insert Key */
132         0,     /* Delete Key */
133         0,   0,   0,
134         0,     /* F11 Key */
135         0,     /* F12 Key */
136         0,     /* All other keys are undefined */
137};     
138
139/*
140 * Keyboard interrupt (8042 PS/2)
141 */
142void hal_keyboard_intr(struct trapframe *tf)
143{
144        uint64_t val;
145
146        do {
147                val = in8(PS2_STATUS);
148        } while ((val & STATUS_IN_FULL) == 0);
149
150        val = in8(PS2_DATA);
151
152        x86_printf("-> got keyboard: rip=%Z key=%c ", tf->tf_rip, scancode_US[val]);
153
154        if (val & 0x80)
155                x86_printf("[released]\n");
156        else
157                x86_printf("[pressed]\n");
158
159        return;
160}
161
Note: See TracBrowser for help on using the repository browser.