1*433d6423SLionel Sambuc /* This file contains a simple exception handler. Exceptions in user 2*433d6423SLionel Sambuc * processes are converted to signals. Exceptions in a kernel task cause 3*433d6423SLionel Sambuc * a panic. 4*433d6423SLionel Sambuc */ 5*433d6423SLionel Sambuc 6*433d6423SLionel Sambuc #include "kernel/kernel.h" 7*433d6423SLionel Sambuc #include "arch_proto.h" 8*433d6423SLionel Sambuc #include <signal.h> 9*433d6423SLionel Sambuc #include <string.h> 10*433d6423SLionel Sambuc #include <assert.h> 11*433d6423SLionel Sambuc #include "kernel/proc.h" 12*433d6423SLionel Sambuc #include "kernel/proto.h" 13*433d6423SLionel Sambuc #include <machine/vm.h> 14*433d6423SLionel Sambuc 15*433d6423SLionel Sambuc struct ex_s { 16*433d6423SLionel Sambuc char *msg; 17*433d6423SLionel Sambuc int signum; 18*433d6423SLionel Sambuc }; 19*433d6423SLionel Sambuc 20*433d6423SLionel Sambuc static struct ex_s ex_data[] = { 21*433d6423SLionel Sambuc { "Reset", 0}, 22*433d6423SLionel Sambuc { "Undefined instruction", SIGILL}, 23*433d6423SLionel Sambuc { "Supervisor call", 0}, 24*433d6423SLionel Sambuc { "Prefetch Abort", SIGILL}, 25*433d6423SLionel Sambuc { "Data Abort", SIGSEGV}, 26*433d6423SLionel Sambuc { "Hypervisor call", 0}, 27*433d6423SLionel Sambuc { "Interrupt", 0}, 28*433d6423SLionel Sambuc { "Fast Interrupt", 0}, 29*433d6423SLionel Sambuc }; 30*433d6423SLionel Sambuc 31*433d6423SLionel Sambuc static void inkernel_disaster(struct proc *saved_proc, 32*433d6423SLionel Sambuc reg_t *saved_lr, struct ex_s *ep, int is_nested); 33*433d6423SLionel Sambuc 34*433d6423SLionel Sambuc extern int catch_pagefaults; 35*433d6423SLionel Sambuc 36*433d6423SLionel Sambuc static void proc_stacktrace_execute(struct proc *whichproc, reg_t v_bp, reg_t pc); 37*433d6423SLionel Sambuc 38*433d6423SLionel Sambuc static void pagefault( struct proc *pr, 39*433d6423SLionel Sambuc reg_t *saved_lr, 40*433d6423SLionel Sambuc int is_nested, 41*433d6423SLionel Sambuc u32_t pagefault_addr, 42*433d6423SLionel Sambuc u32_t pagefault_status) 43*433d6423SLionel Sambuc { 44*433d6423SLionel Sambuc int in_physcopy = 0, in_memset = 0; 45*433d6423SLionel Sambuc 46*433d6423SLionel Sambuc message m_pagefault; 47*433d6423SLionel Sambuc int err; 48*433d6423SLionel Sambuc 49*433d6423SLionel Sambuc in_physcopy = (*saved_lr > (vir_bytes) phys_copy) && 50*433d6423SLionel Sambuc (*saved_lr < (vir_bytes) phys_copy_fault); 51*433d6423SLionel Sambuc 52*433d6423SLionel Sambuc in_memset = (*saved_lr > (vir_bytes) phys_memset) && 53*433d6423SLionel Sambuc (*saved_lr < (vir_bytes) memset_fault); 54*433d6423SLionel Sambuc 55*433d6423SLionel Sambuc if((is_nested || iskernelp(pr)) && 56*433d6423SLionel Sambuc catch_pagefaults && (in_physcopy || in_memset)) { 57*433d6423SLionel Sambuc if (is_nested) { 58*433d6423SLionel Sambuc if(in_physcopy) { 59*433d6423SLionel Sambuc assert(!in_memset); 60*433d6423SLionel Sambuc *saved_lr = (reg_t) phys_copy_fault_in_kernel; 61*433d6423SLionel Sambuc } else { 62*433d6423SLionel Sambuc *saved_lr = (reg_t) memset_fault_in_kernel; 63*433d6423SLionel Sambuc } 64*433d6423SLionel Sambuc } 65*433d6423SLionel Sambuc else { 66*433d6423SLionel Sambuc pr->p_reg.pc = (reg_t) phys_copy_fault; 67*433d6423SLionel Sambuc pr->p_reg.retreg = pagefault_addr; 68*433d6423SLionel Sambuc } 69*433d6423SLionel Sambuc 70*433d6423SLionel Sambuc return; 71*433d6423SLionel Sambuc } 72*433d6423SLionel Sambuc 73*433d6423SLionel Sambuc if(is_nested) { 74*433d6423SLionel Sambuc printf("pagefault in kernel at pc 0x%lx address 0x%lx\n", 75*433d6423SLionel Sambuc *saved_lr, pagefault_addr); 76*433d6423SLionel Sambuc inkernel_disaster(pr, saved_lr, NULL, is_nested); 77*433d6423SLionel Sambuc } 78*433d6423SLionel Sambuc 79*433d6423SLionel Sambuc /* VM can't handle page faults. */ 80*433d6423SLionel Sambuc if(pr->p_endpoint == VM_PROC_NR) { 81*433d6423SLionel Sambuc /* Page fault we can't / don't want to 82*433d6423SLionel Sambuc * handle. 83*433d6423SLionel Sambuc */ 84*433d6423SLionel Sambuc printf("pagefault for VM on CPU %d, " 85*433d6423SLionel Sambuc "pc = 0x%x, addr = 0x%x, flags = 0x%x, is_nested %d\n", 86*433d6423SLionel Sambuc cpuid, pr->p_reg.pc, pagefault_addr, pagefault_status, 87*433d6423SLionel Sambuc is_nested); 88*433d6423SLionel Sambuc proc_stacktrace(pr); 89*433d6423SLionel Sambuc printf("pc of pagefault: 0x%lx\n", pr->p_reg.pc); 90*433d6423SLionel Sambuc panic("pagefault in VM"); 91*433d6423SLionel Sambuc 92*433d6423SLionel Sambuc return; 93*433d6423SLionel Sambuc } 94*433d6423SLionel Sambuc 95*433d6423SLionel Sambuc /* Don't schedule this process until pagefault is handled. */ 96*433d6423SLionel Sambuc RTS_SET(pr, RTS_PAGEFAULT); 97*433d6423SLionel Sambuc 98*433d6423SLionel Sambuc /* tell Vm about the pagefault */ 99*433d6423SLionel Sambuc m_pagefault.m_source = pr->p_endpoint; 100*433d6423SLionel Sambuc m_pagefault.m_type = VM_PAGEFAULT; 101*433d6423SLionel Sambuc m_pagefault.VPF_ADDR = pagefault_addr; 102*433d6423SLionel Sambuc m_pagefault.VPF_FLAGS = pagefault_status; 103*433d6423SLionel Sambuc 104*433d6423SLionel Sambuc if ((err = mini_send(pr, VM_PROC_NR, 105*433d6423SLionel Sambuc &m_pagefault, FROM_KERNEL))) { 106*433d6423SLionel Sambuc panic("WARNING: pagefault: mini_send returned %d\n", err); 107*433d6423SLionel Sambuc } 108*433d6423SLionel Sambuc 109*433d6423SLionel Sambuc return; 110*433d6423SLionel Sambuc } 111*433d6423SLionel Sambuc 112*433d6423SLionel Sambuc static void inkernel_disaster(struct proc *saved_proc, 113*433d6423SLionel Sambuc reg_t *saved_lr, struct ex_s *ep, 114*433d6423SLionel Sambuc int is_nested) 115*433d6423SLionel Sambuc { 116*433d6423SLionel Sambuc #if USE_SYSDEBUG 117*433d6423SLionel Sambuc if(ep) 118*433d6423SLionel Sambuc printf("\n%s\n", ep->msg); 119*433d6423SLionel Sambuc 120*433d6423SLionel Sambuc printf("cpu %d is_nested = %d ", cpuid, is_nested); 121*433d6423SLionel Sambuc 122*433d6423SLionel Sambuc if (saved_proc) { 123*433d6423SLionel Sambuc printf("scheduled was: process %d (%s), ", saved_proc->p_endpoint, saved_proc->p_name); 124*433d6423SLionel Sambuc printf("pc = 0x%x\n", (unsigned) saved_proc->p_reg.pc); 125*433d6423SLionel Sambuc proc_stacktrace(saved_proc); 126*433d6423SLionel Sambuc 127*433d6423SLionel Sambuc panic("Unhandled kernel exception"); 128*433d6423SLionel Sambuc } 129*433d6423SLionel Sambuc 130*433d6423SLionel Sambuc /* in an early stage of boot process we don't have processes yet */ 131*433d6423SLionel Sambuc panic("exception in kernel while booting, no saved_proc yet"); 132*433d6423SLionel Sambuc 133*433d6423SLionel Sambuc #endif /* USE_SYSDEBUG */ 134*433d6423SLionel Sambuc } 135*433d6423SLionel Sambuc 136*433d6423SLionel Sambuc void exception_handler(int is_nested, reg_t *saved_lr, int vector) 137*433d6423SLionel Sambuc { 138*433d6423SLionel Sambuc /* An exception or unexpected interrupt has occurred. */ 139*433d6423SLionel Sambuc struct ex_s *ep; 140*433d6423SLionel Sambuc struct proc *saved_proc; 141*433d6423SLionel Sambuc 142*433d6423SLionel Sambuc saved_proc = get_cpulocal_var(proc_ptr); 143*433d6423SLionel Sambuc 144*433d6423SLionel Sambuc ep = &ex_data[vector]; 145*433d6423SLionel Sambuc 146*433d6423SLionel Sambuc assert((vir_bytes) saved_lr >= kinfo.vir_kern_start); 147*433d6423SLionel Sambuc 148*433d6423SLionel Sambuc /* 149*433d6423SLionel Sambuc * handle special cases for nested problems as they might be tricky or filter 150*433d6423SLionel Sambuc * them out quickly if the traps are not nested 151*433d6423SLionel Sambuc */ 152*433d6423SLionel Sambuc if (is_nested) { 153*433d6423SLionel Sambuc /* 154*433d6423SLionel Sambuc * if a problem occurred while copying a message from userspace because 155*433d6423SLionel Sambuc * of a wrong pointer supplied by userland, handle it the only way we 156*433d6423SLionel Sambuc * can handle it ... 157*433d6423SLionel Sambuc */ 158*433d6423SLionel Sambuc if (((void*)*saved_lr >= (void*)copy_msg_to_user && 159*433d6423SLionel Sambuc (void*)*saved_lr <= (void*)__copy_msg_to_user_end) || 160*433d6423SLionel Sambuc ((void*)*saved_lr >= (void*)copy_msg_from_user && 161*433d6423SLionel Sambuc (void*)*saved_lr <= (void*)__copy_msg_from_user_end)) { 162*433d6423SLionel Sambuc switch(vector) { 163*433d6423SLionel Sambuc /* these error are expected */ 164*433d6423SLionel Sambuc case DATA_ABORT_VECTOR: 165*433d6423SLionel Sambuc *saved_lr = (reg_t) __user_copy_msg_pointer_failure; 166*433d6423SLionel Sambuc return; 167*433d6423SLionel Sambuc default: 168*433d6423SLionel Sambuc panic("Copy involving a user pointer failed unexpectedly!"); 169*433d6423SLionel Sambuc } 170*433d6423SLionel Sambuc } 171*433d6423SLionel Sambuc } 172*433d6423SLionel Sambuc 173*433d6423SLionel Sambuc if (vector == DATA_ABORT_VECTOR) { 174*433d6423SLionel Sambuc pagefault(saved_proc, saved_lr, is_nested, read_dfar(), read_dfsr()); 175*433d6423SLionel Sambuc return; 176*433d6423SLionel Sambuc } 177*433d6423SLionel Sambuc 178*433d6423SLionel Sambuc if (!is_nested && vector == PREFETCH_ABORT_VECTOR) { 179*433d6423SLionel Sambuc reg_t ifar = read_ifar(), ifsr = read_ifsr(); 180*433d6423SLionel Sambuc 181*433d6423SLionel Sambuc /* The saved_lr is the instruction we're going to execute after 182*433d6423SLionel Sambuc * the fault is handled; IFAR is the address that pagefaulted 183*433d6423SLionel Sambuc * while fetching the instruction. As far as we know the two 184*433d6423SLionel Sambuc * should be the same, if not this assumption will lead to very 185*433d6423SLionel Sambuc * hard to debug problems (instruction executing being off by one) 186*433d6423SLionel Sambuc * and this assumption needs re-examining, hence the assert. 187*433d6423SLionel Sambuc */ 188*433d6423SLionel Sambuc assert(*saved_lr == ifar); 189*433d6423SLionel Sambuc pagefault(saved_proc, saved_lr, is_nested, ifar, ifsr); 190*433d6423SLionel Sambuc return; 191*433d6423SLionel Sambuc } 192*433d6423SLionel Sambuc 193*433d6423SLionel Sambuc /* If an exception occurs while running a process, the is_nested variable 194*433d6423SLionel Sambuc * will be zero. Exceptions in interrupt handlers or system traps will make 195*433d6423SLionel Sambuc * is_nested non-zero. 196*433d6423SLionel Sambuc */ 197*433d6423SLionel Sambuc if (is_nested == 0 && ! iskernelp(saved_proc)) { 198*433d6423SLionel Sambuc cause_sig(proc_nr(saved_proc), ep->signum); 199*433d6423SLionel Sambuc return; 200*433d6423SLionel Sambuc } 201*433d6423SLionel Sambuc 202*433d6423SLionel Sambuc /* Exception in system code. This is not supposed to happen. */ 203*433d6423SLionel Sambuc inkernel_disaster(saved_proc, saved_lr, ep, is_nested); 204*433d6423SLionel Sambuc 205*433d6423SLionel Sambuc panic("return from inkernel_disaster"); 206*433d6423SLionel Sambuc } 207*433d6423SLionel Sambuc 208*433d6423SLionel Sambuc #if USE_SYSDEBUG 209*433d6423SLionel Sambuc /*===========================================================================* 210*433d6423SLionel Sambuc * proc_stacktrace_execute * 211*433d6423SLionel Sambuc *===========================================================================*/ 212*433d6423SLionel Sambuc static void proc_stacktrace_execute(struct proc *whichproc, reg_t v_bp, reg_t pc) 213*433d6423SLionel Sambuc { 214*433d6423SLionel Sambuc printf("%-8.8s %6d 0x%lx \n", 215*433d6423SLionel Sambuc whichproc->p_name, whichproc->p_endpoint, pc); 216*433d6423SLionel Sambuc } 217*433d6423SLionel Sambuc #endif 218*433d6423SLionel Sambuc 219*433d6423SLionel Sambuc void proc_stacktrace(struct proc *whichproc) 220*433d6423SLionel Sambuc { 221*433d6423SLionel Sambuc #if USE_SYSDEBUG 222*433d6423SLionel Sambuc proc_stacktrace_execute(whichproc, whichproc->p_reg.fp, whichproc->p_reg.pc); 223*433d6423SLionel Sambuc #endif /* USE_SYSDEBUG */ 224*433d6423SLionel Sambuc } 225*433d6423SLionel Sambuc 226*433d6423SLionel Sambuc void enable_fpu_exception(void) 227*433d6423SLionel Sambuc { 228*433d6423SLionel Sambuc } 229*433d6423SLionel Sambuc 230*433d6423SLionel Sambuc void disable_fpu_exception(void) 231*433d6423SLionel Sambuc { 232*433d6423SLionel Sambuc } 233