mkfs: move directory entry manipulation
[minix.git] / kernel / arch / i386 / exception.c
blobd885cfcd1c84f91bcf5582f0fde748c16ce7646c
1 /* This file contains a simple exception handler. Exceptions in user
2 * processes are converted to signals. Exceptions in a kernel task cause
3 * a panic.
4 */
6 #include "kernel/kernel.h"
7 #include "arch_proto.h"
8 #include <signal.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <machine/vm.h>
13 struct ex_s {
14 char *msg;
15 int signum;
16 int minprocessor;
19 static struct ex_s ex_data[] = {
20 { "Divide error", SIGFPE, 86 },
21 { "Debug exception", SIGTRAP, 86 },
22 { "Nonmaskable interrupt", SIGBUS, 86 },
23 { "Breakpoint", SIGEMT, 86 },
24 { "Overflow", SIGFPE, 86 },
25 { "Bounds check", SIGFPE, 186 },
26 { "Invalid opcode", SIGILL, 186 },
27 { "Coprocessor not available", SIGFPE, 186 },
28 { "Double fault", SIGBUS, 286 },
29 { "Coprocessor segment overrun", SIGSEGV, 286 },
30 { "Invalid TSS", SIGSEGV, 286 },
31 { "Segment not present", SIGSEGV, 286 },
32 { "Stack exception", SIGSEGV, 286 }, /* STACK_FAULT already used */
33 { "General protection", SIGSEGV, 286 },
34 { "Page fault", SIGSEGV, 386 }, /* not close */
35 { NULL, SIGILL, 0 }, /* probably software trap */
36 { "Coprocessor error", SIGFPE, 386 },
37 { "Alignment check", SIGBUS, 386 },
38 { "Machine check", SIGBUS, 386 },
39 { "SIMD exception", SIGFPE, 386 },
42 static void inkernel_disaster(struct proc *saved_proc,
43 struct exception_frame *frame, struct ex_s *ep, int is_nested);
45 extern int catch_pagefaults;
47 static void proc_stacktrace_execute(struct proc *whichproc, reg_t v_bp, reg_t pc);
49 static void pagefault( struct proc *pr,
50 struct exception_frame * frame,
51 int is_nested)
53 int in_physcopy = 0, in_memset = 0;
55 reg_t pagefaultcr2;
56 message m_pagefault;
57 int err;
59 pagefaultcr2 = read_cr2();
61 #if 0
62 printf("kernel: pagefault in pr %d, addr 0x%lx, his cr3 0x%lx, actual cr3 0x%lx\n",
63 pr->p_endpoint, pagefaultcr2, pr->p_seg.p_cr3, read_cr3());
64 #endif
66 in_physcopy = (frame->eip > (vir_bytes) phys_copy) &&
67 (frame->eip < (vir_bytes) phys_copy_fault);
69 in_memset = (frame->eip > (vir_bytes) phys_memset) &&
70 (frame->eip < (vir_bytes) memset_fault);
72 if((is_nested || iskernelp(pr)) &&
73 catch_pagefaults && (in_physcopy || in_memset)) {
74 #if 0
75 printf("pf caught! addr 0x%lx\n", pagefaultcr2);
76 #endif
77 if (is_nested) {
78 if(in_physcopy) {
79 assert(!in_memset);
80 frame->eip = (reg_t) phys_copy_fault_in_kernel;
81 } else {
82 frame->eip = (reg_t) memset_fault_in_kernel;
85 else {
86 pr->p_reg.pc = (reg_t) phys_copy_fault;
87 pr->p_reg.retreg = pagefaultcr2;
90 return;
93 if(is_nested) {
94 printf("pagefault in kernel at pc 0x%lx address 0x%lx\n",
95 frame->eip, pagefaultcr2);
96 inkernel_disaster(pr, frame, NULL, is_nested);
99 /* VM can't handle page faults. */
100 if(pr->p_endpoint == VM_PROC_NR) {
101 /* Page fault we can't / don't want to
102 * handle.
104 printf("pagefault for VM on CPU %d, "
105 "pc = 0x%x, addr = 0x%x, flags = 0x%x, is_nested %d\n",
106 cpuid, pr->p_reg.pc, pagefaultcr2, frame->errcode,
107 is_nested);
108 proc_stacktrace(pr);
109 printf("pc of pagefault: 0x%lx\n", frame->eip);
110 panic("pagefault in VM");
112 return;
115 /* Don't schedule this process until pagefault is handled. */
116 RTS_SET(pr, RTS_PAGEFAULT);
118 /* tell Vm about the pagefault */
119 m_pagefault.m_source = pr->p_endpoint;
120 m_pagefault.m_type = VM_PAGEFAULT;
121 m_pagefault.VPF_ADDR = pagefaultcr2;
122 m_pagefault.VPF_FLAGS = frame->errcode;
124 if ((err = mini_send(pr, VM_PROC_NR,
125 &m_pagefault, FROM_KERNEL))) {
126 panic("WARNING: pagefault: mini_send returned %d\n", err);
129 return;
132 static void inkernel_disaster(struct proc *saved_proc,
133 struct exception_frame * frame, struct ex_s *ep,
134 int is_nested)
136 #if USE_SYSDEBUG
137 if(ep) {
138 if (ep->msg == NULL)
139 printf("\nIntel-reserved exception %d\n", frame->vector);
140 else
141 printf("\n%s\n", ep->msg);
144 printf("cpu %d is_nested = %d ", cpuid, is_nested);
146 printf("vec_nr= %d, trap_errno= 0x%x, eip= 0x%x, "
147 "cs= 0x%x, eflags= 0x%x trap_esp 0x%08x\n",
148 frame->vector, frame->errcode, frame->eip,
149 frame->cs, frame->eflags, frame);
150 printf("KERNEL registers :\n");
151 #define REG(n) (((u32_t *)frame)[-n])
152 printf(
153 "\t%%eax 0x%08x %%ebx 0x%08x %%ecx 0x%08x %%edx 0x%08x\n"
154 "\t%%esp 0x%08x %%ebp 0x%08x %%esi 0x%08x %%edi 0x%08x\n",
155 REG(1), REG(2), REG(3), REG(4),
156 REG(5), REG(6), REG(7), REG(8));
159 reg_t k_ebp = REG(6);
160 printf("KERNEL stacktrace, starting with ebp = 0x%lx:\n", k_ebp);
161 proc_stacktrace_execute(proc_addr(SYSTEM), k_ebp, frame->eip);
164 if (saved_proc) {
165 printf("scheduled was: process %d (%s), ", saved_proc->p_endpoint, saved_proc->p_name);
166 printf("pc = 0x%x\n", (unsigned) saved_proc->p_reg.pc);
167 proc_stacktrace(saved_proc);
169 panic("Unhandled kernel exception");
172 /* in an early stage of boot process we don't have processes yet */
173 panic("exception in kernel while booting, no saved_proc yet");
174 #endif /* USE_SYSDEBUG */
177 /*===========================================================================*
178 * exception *
179 *===========================================================================*/
180 void exception_handler(int is_nested, struct exception_frame * frame)
182 /* An exception or unexpected interrupt has occurred. */
183 register struct ex_s *ep;
184 struct proc *saved_proc;
186 /* Save proc_ptr, because it may be changed by debug statements. */
187 saved_proc = get_cpulocal_var(proc_ptr);
189 ep = &ex_data[frame->vector];
191 if (frame->vector == 2) { /* spurious NMI on some machines */
192 printf("got spurious NMI\n");
193 return;
197 * handle special cases for nested problems as they might be tricky or filter
198 * them out quickly if the traps are not nested
200 if (is_nested) {
202 * if a problem occured while copying a message from userspace because
203 * of a wrong pointer supplied by userland, handle it the only way we
204 * can handle it ...
206 if (((void*)frame->eip >= (void*)copy_msg_to_user &&
207 (void*)frame->eip <= (void*)__copy_msg_to_user_end) ||
208 ((void*)frame->eip >= (void*)copy_msg_from_user &&
209 (void*)frame->eip <= (void*)__copy_msg_from_user_end)) {
210 switch(frame->vector) {
211 /* these error are expected */
212 case PAGE_FAULT_VECTOR:
213 case PROTECTION_VECTOR:
214 frame->eip = (reg_t) __user_copy_msg_pointer_failure;
215 return;
216 default:
217 panic("Copy involving a user pointer failed unexpectedly!");
221 /* Pass any error resulting from restoring FPU state, as a FPU
222 * exception to the process.
224 if (((void*)frame->eip >= (void*)fxrstor &&
225 (void *)frame->eip <= (void*)__fxrstor_end) ||
226 ((void*)frame->eip >= (void*)frstor &&
227 (void *)frame->eip <= (void*)__frstor_end)) {
228 frame->eip = (reg_t) __frstor_failure;
229 return;
233 if(frame->vector == PAGE_FAULT_VECTOR) {
234 pagefault(saved_proc, frame, is_nested);
235 return;
238 /* If an exception occurs while running a process, the is_nested variable
239 * will be zero. Exceptions in interrupt handlers or system traps will make
240 * is_nested non-zero.
242 if (is_nested == 0 && ! iskernelp(saved_proc)) {
243 #if 0
246 printf(
247 "vec_nr= %d, trap_errno= 0x%lx, eip= 0x%lx, cs= 0x%x, eflags= 0x%lx\n",
248 frame->vector, (unsigned long)frame->errcode,
249 (unsigned long)frame->eip, frame->cs,
250 (unsigned long)frame->eflags);
251 proc_stacktrace(saved_proc);
254 #endif
255 cause_sig(proc_nr(saved_proc), ep->signum);
256 return;
259 /* Exception in system code. This is not supposed to happen. */
260 inkernel_disaster(saved_proc, frame, ep, is_nested);
262 panic("return from inkernel_disaster");
265 #if USE_SYSDEBUG
266 /*===========================================================================*
267 * proc_stacktrace_execute *
268 *===========================================================================*/
269 static void proc_stacktrace_execute(struct proc *whichproc, reg_t v_bp, reg_t pc)
271 reg_t v_hbp;
272 int iskernel;
273 int n = 0;
275 iskernel = iskernelp(whichproc);
277 printf("%-8.8s %6d 0x%lx ",
278 whichproc->p_name, whichproc->p_endpoint, pc);
280 while(v_bp) {
281 reg_t v_pc;
283 #define PRCOPY(pr, pv, v, n) \
284 (iskernel ? (memcpy((char *) v, (char *) pv, n), OK) : \
285 data_copy(pr->p_endpoint, pv, KERNEL, (vir_bytes) (v), n))
287 if(PRCOPY(whichproc, v_bp, &v_hbp, sizeof(v_hbp)) != OK) {
288 printf("(v_bp 0x%lx ?)", v_bp);
289 break;
291 if(PRCOPY(whichproc, v_bp + sizeof(v_pc), &v_pc, sizeof(v_pc)) != OK) {
292 printf("(v_pc 0x%lx ?)", v_bp + sizeof(v_pc));
293 break;
295 printf("0x%lx ", (unsigned long) v_pc);
296 if(v_hbp != 0 && v_hbp <= v_bp) {
297 printf("(hbp %lx ?)", v_hbp);
298 break;
300 v_bp = v_hbp;
301 if(n++ > 50) {
302 printf("(truncated after %d steps) ", n);
303 break;
306 printf("\n");
308 #endif /* USE_SYSDEBUG */
310 /*===========================================================================*
311 * proc_stacktrace *
312 *===========================================================================*/
313 void proc_stacktrace(struct proc *whichproc)
315 #if USE_SYSDEBUG
316 proc_stacktrace_execute(whichproc, whichproc->p_reg.fp, whichproc->p_reg.pc);
317 #endif /* USE_SYSDEBUG */
320 void enable_fpu_exception(void)
322 u32_t cr0 = read_cr0();
323 if(!(cr0 & I386_CR0_TS))
324 write_cr0(cr0 | I386_CR0_TS);
327 void disable_fpu_exception(void)
329 clts();