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 */ 5 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> 12 13 struct ex_s { 14 char *msg; 15 int signum; 16 int minprocessor; 17 }; 18 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 }, 40 }; 41 42 static void inkernel_disaster(struct proc *saved_proc, 43 struct exception_frame *frame, struct ex_s *ep, int is_nested); 44 45 extern int catch_pagefaults; 46 47 static void proc_stacktrace_execute(struct proc *whichproc, reg_t v_bp, reg_t pc); 48 49 static void pagefault( struct proc *pr, 50 struct exception_frame * frame, 51 int is_nested) 52 { 53 int in_physcopy = 0, in_memset = 0; 54 55 reg_t pagefaultcr2; 56 message m_pagefault; 57 int err; 58 59 pagefaultcr2 = read_cr2(); 60 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 65 66 in_physcopy = (frame->eip > (vir_bytes) phys_copy) && 67 (frame->eip < (vir_bytes) phys_copy_fault); 68 69 in_memset = (frame->eip > (vir_bytes) phys_memset) && 70 (frame->eip < (vir_bytes) memset_fault); 71 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; 83 } 84 } 85 else { 86 pr->p_reg.pc = (reg_t) phys_copy_fault; 87 pr->p_reg.retreg = pagefaultcr2; 88 } 89 90 return; 91 } 92 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); 97 } 98 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. 103 */ 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"); 111 112 return; 113 } 114 115 /* Don't schedule this process until pagefault is handled. */ 116 RTS_SET(pr, RTS_PAGEFAULT); 117 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; 123 124 if ((err = mini_send(pr, VM_PROC_NR, 125 &m_pagefault, FROM_KERNEL))) { 126 panic("WARNING: pagefault: mini_send returned %d\n", err); 127 } 128 129 return; 130 } 131 132 static void inkernel_disaster(struct proc *saved_proc, 133 struct exception_frame * frame, struct ex_s *ep, 134 int is_nested) 135 { 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); 142 } 143 144 printf("cpu %d is_nested = %d ", cpuid, is_nested); 145 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)); 157 158 { 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); 162 } 163 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); 168 169 panic("Unhandled kernel exception"); 170 } 171 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 */ 175 } 176 177 /*===========================================================================* 178 * exception * 179 *===========================================================================*/ 180 void exception_handler(int is_nested, struct exception_frame * frame) 181 { 182 /* An exception or unexpected interrupt has occurred. */ 183 register struct ex_s *ep; 184 struct proc *saved_proc; 185 186 /* Save proc_ptr, because it may be changed by debug statements. */ 187 saved_proc = get_cpulocal_var(proc_ptr); 188 189 ep = &ex_data[frame->vector]; 190 191 if (frame->vector == 2) { /* spurious NMI on some machines */ 192 printf("got spurious NMI\n"); 193 return; 194 } 195 196 /* 197 * handle special cases for nested problems as they might be tricky or filter 198 * them out quickly if the traps are not nested 199 */ 200 if (is_nested) { 201 /* 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 ... 205 */ 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!"); 218 } 219 } 220 221 /* Pass any error resulting from restoring FPU state, as a FPU 222 * exception to the process. 223 */ 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; 230 } 231 232 if(frame->vector == DEBUG_VECTOR 233 && (saved_proc->p_reg.psw & TRACEBIT) 234 && (saved_proc->p_seg.p_kern_trap_style == KTS_NONE)) { 235 /* Getting a debug trap in the kernel is legitimate 236 * if a traced process entered the kernel using sysenter 237 * or syscall; the trap flag is not cleared then. 238 * 239 * It triggers on the first kernel entry so the trap 240 * style is still KTS_NONE. 241 */ 242 243 frame->eflags &= ~TRACEBIT; 244 245 return; 246 247 /* If control passes, this case is not recognized as legitimate 248 * and we panic later on after all. 249 */ 250 } 251 } 252 253 if(frame->vector == PAGE_FAULT_VECTOR) { 254 pagefault(saved_proc, frame, is_nested); 255 return; 256 } 257 258 /* If an exception occurs while running a process, the is_nested variable 259 * will be zero. Exceptions in interrupt handlers or system traps will make 260 * is_nested non-zero. 261 */ 262 if (is_nested == 0 && ! iskernelp(saved_proc)) { 263 #if 0 264 { 265 266 printf( 267 "vec_nr= %d, trap_errno= 0x%lx, eip= 0x%lx, cs= 0x%x, eflags= 0x%lx\n", 268 frame->vector, (unsigned long)frame->errcode, 269 (unsigned long)frame->eip, frame->cs, 270 (unsigned long)frame->eflags); 271 proc_stacktrace(saved_proc); 272 } 273 274 #endif 275 cause_sig(proc_nr(saved_proc), ep->signum); 276 return; 277 } 278 279 /* Exception in system code. This is not supposed to happen. */ 280 inkernel_disaster(saved_proc, frame, ep, is_nested); 281 282 panic("return from inkernel_disaster"); 283 } 284 285 #if USE_SYSDEBUG 286 /*===========================================================================* 287 * proc_stacktrace_execute * 288 *===========================================================================*/ 289 static void proc_stacktrace_execute(struct proc *whichproc, reg_t v_bp, reg_t pc) 290 { 291 reg_t v_hbp; 292 int iskernel; 293 int n = 0; 294 295 iskernel = iskernelp(whichproc); 296 297 printf("%-8.8s %6d 0x%lx ", 298 whichproc->p_name, whichproc->p_endpoint, pc); 299 300 while(v_bp) { 301 reg_t v_pc; 302 303 #define PRCOPY(pr, pv, v, n) \ 304 (iskernel ? (memcpy((char *) v, (char *) pv, n), OK) : \ 305 data_copy(pr->p_endpoint, pv, KERNEL, (vir_bytes) (v), n)) 306 307 if(PRCOPY(whichproc, v_bp, &v_hbp, sizeof(v_hbp)) != OK) { 308 printf("(v_bp 0x%lx ?)", v_bp); 309 break; 310 } 311 if(PRCOPY(whichproc, v_bp + sizeof(v_pc), &v_pc, sizeof(v_pc)) != OK) { 312 printf("(v_pc 0x%lx ?)", v_bp + sizeof(v_pc)); 313 break; 314 } 315 printf("0x%lx ", (unsigned long) v_pc); 316 if(v_hbp != 0 && v_hbp <= v_bp) { 317 printf("(hbp %lx ?)", v_hbp); 318 break; 319 } 320 v_bp = v_hbp; 321 if(n++ > 50) { 322 printf("(truncated after %d steps) ", n); 323 break; 324 } 325 } 326 printf("\n"); 327 } 328 #endif /* USE_SYSDEBUG */ 329 330 /*===========================================================================* 331 * proc_stacktrace * 332 *===========================================================================*/ 333 void proc_stacktrace(struct proc *whichproc) 334 { 335 u32_t use_bp; 336 337 if(whichproc->p_seg.p_kern_trap_style == KTS_NONE) { 338 printf("WARNING: stacktrace of running proecss\n"); 339 } 340 341 switch(whichproc->p_seg.p_kern_trap_style) { 342 case KTS_SYSENTER: 343 case KTS_SYSCALL: 344 { 345 u32_t sp = whichproc->p_reg.sp; 346 347 /* Full context is not available in the p_reg 348 * struct. Obtain it from the user's stack. 349 * The use stack pointer is always available. 350 * The fact that it's there, and the 16 byte offset, 351 * is a dependency on the trap code in 352 * kernel/arch/i386/usermapped_glo_ipc.S. 353 */ 354 355 if(data_copy(whichproc->p_endpoint, sp+16, 356 KERNEL, (vir_bytes) &use_bp, 357 sizeof(use_bp)) != OK) { 358 printf("stacktrace: aborting, copy failed\n"); 359 return; 360 } 361 362 break; 363 } 364 default: 365 /* Full context is available; use the stored ebp */ 366 use_bp = whichproc->p_reg.fp; 367 break; 368 } 369 370 #if USE_SYSDEBUG 371 proc_stacktrace_execute(whichproc, use_bp, whichproc->p_reg.pc); 372 #endif /* USE_SYSDEBUG */ 373 } 374 375 void enable_fpu_exception(void) 376 { 377 u32_t cr0 = read_cr0(); 378 if(!(cr0 & I386_CR0_TS)) 379 write_cr0(cr0 | I386_CR0_TS); 380 } 381 382 void disable_fpu_exception(void) 383 { 384 clts(); 385 } 386 387