1 /*
2 * Copyright (c) 2009 Corey Tabaka
3 * Copyright (c) 2015 Intel Corporation
4 *
5 * Use of this source code is governed by a MIT-style
6 * license that can be found in the LICENSE file or at
7 * https://opensource.org/licenses/MIT
8 */
9 #include <lk/debug.h>
10 #include <lk/trace.h>
11 #include <arch/x86.h>
12 #include <arch/fpu.h>
13 #include <kernel/thread.h>
14
15 /* exceptions */
16 #define INT_DIVIDE_0 0x00
17 #define INT_DEBUG_EX 0x01
18 #define INT_INVALID_OP 0x06
19 #define INT_DEV_NA_EX 0x07
20 #define INT_STACK_FAULT 0x0c
21 #define INT_GP_FAULT 0x0d
22 #define INT_PAGE_FAULT 0x0e
23 #define INT_MF 0x10
24 #define INT_XM 0x13
25
26 extern enum handler_return platform_irq(x86_iframe_t *frame);
27
dump_fault_frame(x86_iframe_t * frame)28 static void dump_fault_frame(x86_iframe_t *frame) {
29 #if ARCH_X86_32
30 dprintf(CRITICAL, " CS: %04x EIP: %08x EFL: %08x CR2: %08lx\n",
31 frame->cs, frame->ip, frame->flags, x86_get_cr2());
32 dprintf(CRITICAL, "EAX: %08x ECX: %08x EDX: %08x EBX: %08x\n",
33 frame->ax, frame->cx, frame->dx, frame->bx);
34 dprintf(CRITICAL, "ESP: %08x EBP: %08x ESI: %08x EDI: %08x\n",
35 frame->sp, frame->bp, frame->si, frame->di);
36 dprintf(CRITICAL, " DS: %04x ES: %04x FS: %04x GS: %04x\n",
37 frame->ds, frame->es, frame->fs, frame->gs);
38 #elif ARCH_X86_64
39 dprintf(CRITICAL, " CS: %4llx RIP: %16llx EFL: %16llx CR2: %16lx\n",
40 frame->cs, frame->ip, frame->flags, x86_get_cr2());
41 dprintf(CRITICAL, " RAX: %16llx RBX: %16llx RCX: %16llx RDX: %16llx\n",
42 frame->ax, frame->bx, frame->cx, frame->dx);
43 dprintf(CRITICAL, " RSI: %16llx RDI: %16llx RBP: %16llx RSP: %16llx\n",
44 frame->si, frame->di, frame->bp, frame->user_sp);
45 dprintf(CRITICAL, " R8: %16llx R9: %16llx R10: %16llx R11: %16llx\n",
46 frame->r8, frame->r9, frame->r10, frame->r11);
47 dprintf(CRITICAL, " R12: %16llx R13: %16llx R14: %16llx R15: %16llx\n",
48 frame->r12, frame->r13, frame->r14, frame->r15);
49 dprintf(CRITICAL, "errc: %16llx\n",
50 frame->err_code);
51 #endif
52
53 // dump the bottom of the current stack
54 addr_t stack = (addr_t) frame;
55
56 if (stack != 0) {
57 dprintf(CRITICAL, "bottom of stack at 0x%08x:\n", (unsigned int)stack);
58 hexdump((void *)stack, 512);
59 }
60 }
61
exception_die(x86_iframe_t * frame,const char * msg)62 static void exception_die(x86_iframe_t *frame, const char *msg) {
63 dprintf(CRITICAL, msg);
64 dump_fault_frame(frame);
65
66 for (;;) {
67 x86_cli();
68 x86_hlt();
69 }
70 }
71
x86_syscall_handler(x86_iframe_t * frame)72 static void x86_syscall_handler(x86_iframe_t *frame) {
73 exception_die(frame, "unhandled syscall, halting\n");
74 }
75
x86_gpf_handler(x86_iframe_t * frame)76 static void x86_gpf_handler(x86_iframe_t *frame) {
77 exception_die(frame, "unhandled gpf, halting\n");
78 }
79
x86_invop_handler(x86_iframe_t * frame)80 static void x86_invop_handler(x86_iframe_t *frame) {
81 exception_die(frame, "unhandled invalid op, halting\n");
82 }
83
x86_unhandled_exception(x86_iframe_t * frame)84 static void x86_unhandled_exception(x86_iframe_t *frame) {
85 printf("vector %u\n", (uint)frame->vector);
86 exception_die(frame, "unhandled exception, halting\n");
87 }
88
x86_pfe_handler(x86_iframe_t * frame)89 static void x86_pfe_handler(x86_iframe_t *frame) {
90 /* Handle a page fault exception */
91 uint32_t error_code;
92 thread_t *current_thread;
93 error_code = frame->err_code;
94
95 #ifdef PAGE_FAULT_DEBUG_INFO
96 addr_t v_addr, ssp, esp, ip, rip;
97 v_addr = x86_get_cr2();
98
99 ssp = frame->user_ss & X86_8BYTE_MASK;
100 esp = frame->user_sp;
101 ip = frame->cs & X86_8BYTE_MASK;
102 rip = frame->ip;
103
104 dprintf(CRITICAL, "<PAGE FAULT> Instruction Pointer = 0x%x:0x%x\n",
105 (unsigned int)ip,
106 (unsigned int)rip);
107 dprintf(CRITICAL, "<PAGE FAULT> Stack Pointer = 0x%x:0x%x\n",
108 (unsigned int)ssp,
109 (unsigned int)esp);
110 dprintf(CRITICAL, "<PAGE FAULT> Fault Linear Address = 0x%x\n",
111 (unsigned int)v_addr);
112 dprintf(CRITICAL, "<PAGE FAULT> Error Code Value = 0x%x\n",
113 error_code);
114 dprintf(CRITICAL, "<PAGE FAULT> Error Code Type = %s %s %s%s, %s\n",
115 error_code & PFEX_U ? "user" : "supervisor",
116 error_code & PFEX_W ? "write" : "read",
117 error_code & PFEX_I ? "instruction" : "data",
118 error_code & PFEX_RSV ? " rsv" : "",
119 error_code & PFEX_P ? "protection violation" : "page not present");
120 #endif
121
122 current_thread = get_current_thread();
123 dump_thread(current_thread);
124
125 if (error_code & PFEX_U) {
126 // User mode page fault
127 switch (error_code) {
128 case 4:
129 case 5:
130 case 6:
131 case 7:
132 #ifdef PAGE_FAULT_DEBUG_INFO
133 thread_detach(current_thread);
134 #else
135 thread_exit(current_thread->retcode);
136 #endif
137 break;
138 }
139 } else {
140 // Supervisor mode page fault
141 switch (error_code) {
142
143 case 0:
144 case 1:
145 case 2:
146 case 3:
147 exception_die(frame, "Page Fault exception, halting\n");
148 break;
149 }
150 }
151 }
152
153 /* top level x86 exception handler for most exceptions and irqs, called from asm */
154 void x86_exception_handler(x86_iframe_t *frame);
x86_exception_handler(x86_iframe_t * frame)155 void x86_exception_handler(x86_iframe_t *frame) {
156 // get the current vector
157 unsigned int vector = frame->vector;
158
159 THREAD_STATS_INC(interrupts);
160
161 // deliver the interrupt
162 enum handler_return ret = INT_NO_RESCHEDULE;
163
164 switch (vector) {
165 case INT_GP_FAULT:
166 x86_gpf_handler(frame);
167 break;
168
169 case INT_INVALID_OP:
170 x86_invop_handler(frame);
171 break;
172
173 case INT_PAGE_FAULT:
174 x86_pfe_handler(frame);
175 break;
176
177 case INT_DEV_NA_EX:
178 #if X86_WITH_FPU
179 fpu_dev_na_handler();
180 #endif
181 break;
182
183 case INT_MF: { /* x87 floating point math fault */
184 uint16_t fsw;
185 __asm__ __volatile__("fnstsw %0" : "=m" (fsw));
186 TRACEF("fsw 0x%hx\n", fsw);
187 exception_die(frame, "x87 math fault\n");
188 //asm volatile("fnclex");
189 break;
190 }
191 case INT_XM: { /* simd math fault */
192 uint32_t mxcsr;
193 __asm__ __volatile__("stmxcsr %0" : "=m" (mxcsr));
194 TRACEF("mxcsr 0x%x\n", mxcsr);
195 exception_die(frame, "simd math fault\n");
196 break;
197 }
198 case INT_DIVIDE_0:
199 case INT_DEBUG_EX:
200 case INT_STACK_FAULT:
201 case 3:
202 default:
203 x86_unhandled_exception(frame);
204 break;
205
206 /* pass the rest of the irq vectors to the platform */
207 case 0x20 ... 255:
208 ret = platform_irq(frame);
209 }
210
211 if (ret != INT_NO_RESCHEDULE)
212 thread_preempt();
213 }
214
215