Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * arch/sparc/kernel/traps.c
0004  *
0005  * Copyright 1995, 2008 David S. Miller (davem@davemloft.net)
0006  * Copyright 2000 Jakub Jelinek (jakub@redhat.com)
0007  */
0008 
0009 /*
0010  * I hate traps on the sparc, grrr...
0011  */
0012 
0013 #include <linux/sched/mm.h>
0014 #include <linux/sched/debug.h>
0015 #include <linux/mm_types.h>
0016 #include <linux/kernel.h>
0017 #include <linux/signal.h>
0018 #include <linux/smp.h>
0019 #include <linux/kdebug.h>
0020 #include <linux/export.h>
0021 #include <linux/pgtable.h>
0022 
0023 #include <asm/delay.h>
0024 #include <asm/ptrace.h>
0025 #include <asm/oplib.h>
0026 #include <asm/page.h>
0027 #include <asm/unistd.h>
0028 #include <asm/traps.h>
0029 
0030 #include "entry.h"
0031 #include "kernel.h"
0032 
0033 /* #define TRAP_DEBUG */
0034 
0035 static void instruction_dump(unsigned long *pc)
0036 {
0037     int i;
0038     
0039     if((((unsigned long) pc) & 3))
0040                 return;
0041 
0042     for(i = -3; i < 6; i++)
0043         printk("%c%08lx%c",i?' ':'<',pc[i],i?' ':'>');
0044     printk("\n");
0045 }
0046 
0047 #define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t")
0048 #define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t")
0049 
0050 void __noreturn die_if_kernel(char *str, struct pt_regs *regs)
0051 {
0052     static int die_counter;
0053     int count = 0;
0054 
0055     /* Amuse the user. */
0056     printk(
0057 "              \\|/ ____ \\|/\n"
0058 "              \"@'/ ,. \\`@\"\n"
0059 "              /_| \\__/ |_\\\n"
0060 "                 \\__U_/\n");
0061 
0062     printk("%s(%d): %s [#%d]\n", current->comm, task_pid_nr(current), str, ++die_counter);
0063     show_regs(regs);
0064     add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
0065 
0066     __SAVE; __SAVE; __SAVE; __SAVE;
0067     __SAVE; __SAVE; __SAVE; __SAVE;
0068     __RESTORE; __RESTORE; __RESTORE; __RESTORE;
0069     __RESTORE; __RESTORE; __RESTORE; __RESTORE;
0070 
0071     {
0072         struct reg_window32 *rw = (struct reg_window32 *)regs->u_regs[UREG_FP];
0073 
0074         /* Stop the back trace when we hit userland or we
0075          * find some badly aligned kernel stack. Set an upper
0076          * bound in case our stack is trashed and we loop.
0077          */
0078         while(rw                    &&
0079               count++ < 30              &&
0080                       (((unsigned long) rw) >= PAGE_OFFSET) &&
0081               !(((unsigned long) rw) & 0x7)) {
0082             printk("Caller[%08lx]: %pS\n", rw->ins[7],
0083                    (void *) rw->ins[7]);
0084             rw = (struct reg_window32 *)rw->ins[6];
0085         }
0086     }
0087     printk("Instruction DUMP:");
0088     instruction_dump ((unsigned long *) regs->pc);
0089     make_task_dead((regs->psr & PSR_PS) ? SIGKILL : SIGSEGV);
0090 }
0091 
0092 void do_hw_interrupt(struct pt_regs *regs, unsigned long type)
0093 {
0094     if(type < 0x80) {
0095         /* Sun OS's puke from bad traps, Linux survives! */
0096         printk("Unimplemented Sparc TRAP, type = %02lx\n", type);
0097         die_if_kernel("Whee... Hello Mr. Penguin", regs);
0098     }   
0099 
0100     if(regs->psr & PSR_PS)
0101         die_if_kernel("Kernel bad trap", regs);
0102 
0103     force_sig_fault_trapno(SIGILL, ILL_ILLTRP,
0104                    (void __user *)regs->pc, type - 0x80);
0105 }
0106 
0107 void do_illegal_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
0108                 unsigned long psr)
0109 {
0110     if(psr & PSR_PS)
0111         die_if_kernel("Kernel illegal instruction", regs);
0112 #ifdef TRAP_DEBUG
0113     printk("Ill instr. at pc=%08lx instruction is %08lx\n",
0114            regs->pc, *(unsigned long *)regs->pc);
0115 #endif
0116 
0117     send_sig_fault(SIGILL, ILL_ILLOPC, (void __user *)pc, current);
0118 }
0119 
0120 void do_priv_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
0121              unsigned long psr)
0122 {
0123     if(psr & PSR_PS)
0124         die_if_kernel("Penguin instruction from Penguin mode??!?!", regs);
0125     send_sig_fault(SIGILL, ILL_PRVOPC, (void __user *)pc, current);
0126 }
0127 
0128 /* XXX User may want to be allowed to do this. XXX */
0129 
0130 void do_memaccess_unaligned(struct pt_regs *regs, unsigned long pc, unsigned long npc,
0131                 unsigned long psr)
0132 {
0133     if(regs->psr & PSR_PS) {
0134         printk("KERNEL MNA at pc %08lx npc %08lx called by %08lx\n", pc, npc,
0135                regs->u_regs[UREG_RETPC]);
0136         die_if_kernel("BOGUS", regs);
0137         /* die_if_kernel("Kernel MNA access", regs); */
0138     }
0139 #if 0
0140     show_regs (regs);
0141     instruction_dump ((unsigned long *) regs->pc);
0142     printk ("do_MNA!\n");
0143 #endif
0144     send_sig_fault(SIGBUS, BUS_ADRALN,
0145                /* FIXME: Should dig out mna address */ (void *)0,
0146                current);
0147 }
0148 
0149 static unsigned long init_fsr = 0x0UL;
0150 static unsigned long init_fregs[32] __attribute__ ((aligned (8))) =
0151                 { ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
0152           ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
0153           ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
0154           ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL };
0155 
0156 void do_fpd_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
0157          unsigned long psr)
0158 {
0159     /* Sanity check... */
0160     if(psr & PSR_PS)
0161         die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs);
0162 
0163     put_psr(get_psr() | PSR_EF);    /* Allow FPU ops. */
0164     regs->psr |= PSR_EF;
0165 #ifndef CONFIG_SMP
0166     if(last_task_used_math == current)
0167         return;
0168     if(last_task_used_math) {
0169         /* Other processes fpu state, save away */
0170         struct task_struct *fptask = last_task_used_math;
0171         fpsave(&fptask->thread.float_regs[0], &fptask->thread.fsr,
0172                &fptask->thread.fpqueue[0], &fptask->thread.fpqdepth);
0173     }
0174     last_task_used_math = current;
0175     if(used_math()) {
0176         fpload(&current->thread.float_regs[0], &current->thread.fsr);
0177     } else {
0178         /* Set initial sane state. */
0179         fpload(&init_fregs[0], &init_fsr);
0180         set_used_math();
0181     }
0182 #else
0183     if(!used_math()) {
0184         fpload(&init_fregs[0], &init_fsr);
0185         set_used_math();
0186     } else {
0187         fpload(&current->thread.float_regs[0], &current->thread.fsr);
0188     }
0189     set_thread_flag(TIF_USEDFPU);
0190 #endif
0191 }
0192 
0193 static unsigned long fake_regs[32] __attribute__ ((aligned (8)));
0194 static unsigned long fake_fsr;
0195 static unsigned long fake_queue[32] __attribute__ ((aligned (8)));
0196 static unsigned long fake_depth;
0197 
0198 void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
0199          unsigned long psr)
0200 {
0201     static int calls;
0202     unsigned long fsr;
0203     int ret = 0;
0204     int code;
0205 #ifndef CONFIG_SMP
0206     struct task_struct *fpt = last_task_used_math;
0207 #else
0208     struct task_struct *fpt = current;
0209 #endif
0210     put_psr(get_psr() | PSR_EF);
0211     /* If nobody owns the fpu right now, just clear the
0212      * error into our fake static buffer and hope it don't
0213      * happen again.  Thank you crashme...
0214      */
0215 #ifndef CONFIG_SMP
0216     if(!fpt) {
0217 #else
0218     if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) {
0219 #endif
0220         fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
0221         regs->psr &= ~PSR_EF;
0222         return;
0223     }
0224     fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
0225            &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
0226 #ifdef DEBUG_FPU
0227     printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
0228 #endif
0229 
0230     switch ((fpt->thread.fsr & 0x1c000)) {
0231     /* switch on the contents of the ftt [floating point trap type] field */
0232 #ifdef DEBUG_FPU
0233     case (1 << 14):
0234         printk("IEEE_754_exception\n");
0235         break;
0236 #endif
0237     case (2 << 14):  /* unfinished_FPop (underflow & co) */
0238     case (3 << 14):  /* unimplemented_FPop (quad stuff, maybe sqrt) */
0239         ret = do_mathemu(regs, fpt);
0240         break;
0241 #ifdef DEBUG_FPU
0242     case (4 << 14):
0243         printk("sequence_error (OS bug...)\n");
0244         break;
0245     case (5 << 14):
0246         printk("hardware_error (uhoh!)\n");
0247         break;
0248     case (6 << 14):
0249         printk("invalid_fp_register (user error)\n");
0250         break;
0251 #endif /* DEBUG_FPU */
0252     }
0253     /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
0254     if (ret) {
0255         fpload(&current->thread.float_regs[0], &current->thread.fsr);
0256         return;
0257     }
0258     /* nope, better SIGFPE the offending process... */
0259            
0260 #ifdef CONFIG_SMP
0261     clear_tsk_thread_flag(fpt, TIF_USEDFPU);
0262 #endif
0263     if(psr & PSR_PS) {
0264         /* The first fsr store/load we tried trapped,
0265          * the second one will not (we hope).
0266          */
0267         printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
0268                regs->pc);
0269         regs->pc = regs->npc;
0270         regs->npc += 4;
0271         calls++;
0272         if(calls > 2)
0273             die_if_kernel("Too many Penguin-FPU traps from kernel mode",
0274                       regs);
0275         return;
0276     }
0277 
0278     fsr = fpt->thread.fsr;
0279     code = FPE_FLTUNK;
0280     if ((fsr & 0x1c000) == (1 << 14)) {
0281         if (fsr & 0x10)
0282             code = FPE_FLTINV;
0283         else if (fsr & 0x08)
0284             code = FPE_FLTOVF;
0285         else if (fsr & 0x04)
0286             code = FPE_FLTUND;
0287         else if (fsr & 0x02)
0288             code = FPE_FLTDIV;
0289         else if (fsr & 0x01)
0290             code = FPE_FLTRES;
0291     }
0292     send_sig_fault(SIGFPE, code, (void __user *)pc, fpt);
0293 #ifndef CONFIG_SMP
0294     last_task_used_math = NULL;
0295 #endif
0296     regs->psr &= ~PSR_EF;
0297     if(calls > 0)
0298         calls=0;
0299 }
0300 
0301 void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
0302              unsigned long psr)
0303 {
0304     if(psr & PSR_PS)
0305         die_if_kernel("Penguin overflow trap from kernel mode", regs);
0306     send_sig_fault(SIGEMT, EMT_TAGOVF, (void __user *)pc, current);
0307 }
0308 
0309 void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
0310                unsigned long psr)
0311 {
0312 #ifdef TRAP_DEBUG
0313     printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
0314            pc, npc, psr);
0315 #endif
0316     if(psr & PSR_PS)
0317         panic("Tell me what a watchpoint trap is, and I'll then deal "
0318               "with such a beast...");
0319 }
0320 
0321 void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
0322                unsigned long psr)
0323 {
0324 #ifdef TRAP_DEBUG
0325     printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
0326            pc, npc, psr);
0327 #endif
0328     force_sig_fault(SIGBUS, BUS_OBJERR, (void __user *)pc);
0329 }
0330 
0331 void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
0332             unsigned long psr)
0333 {
0334     send_sig_fault(SIGILL, ILL_COPROC, (void __user *)pc, current);
0335 }
0336 
0337 void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
0338              unsigned long psr)
0339 {
0340 #ifdef TRAP_DEBUG
0341     printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
0342            pc, npc, psr);
0343 #endif
0344     send_sig_fault(SIGILL, ILL_COPROC, (void __user *)pc, current);
0345 }
0346 
0347 void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
0348                unsigned long psr)
0349 {
0350     send_sig_fault(SIGFPE, FPE_INTDIV, (void __user *)pc, current);
0351 }
0352 
0353 #ifdef CONFIG_DEBUG_BUGVERBOSE
0354 void do_BUG(const char *file, int line)
0355 {
0356         // bust_spinlocks(1);   XXX Not in our original BUG()
0357         printk("kernel BUG at %s:%d!\n", file, line);
0358 }
0359 EXPORT_SYMBOL(do_BUG);
0360 #endif
0361 
0362 /* Since we have our mappings set up, on multiprocessors we can spin them
0363  * up here so that timer interrupts work during initialization.
0364  */
0365 
0366 void trap_init(void)
0367 {
0368     extern void thread_info_offsets_are_bolixed_pete(void);
0369 
0370     /* Force linker to barf if mismatched */
0371     if (TI_UWINMASK    != offsetof(struct thread_info, uwinmask) ||
0372         TI_TASK        != offsetof(struct thread_info, task) ||
0373         TI_FLAGS       != offsetof(struct thread_info, flags) ||
0374         TI_CPU         != offsetof(struct thread_info, cpu) ||
0375         TI_PREEMPT     != offsetof(struct thread_info, preempt_count) ||
0376         TI_SOFTIRQ     != offsetof(struct thread_info, softirq_count) ||
0377         TI_HARDIRQ     != offsetof(struct thread_info, hardirq_count) ||
0378         TI_KSP         != offsetof(struct thread_info, ksp) ||
0379         TI_KPC         != offsetof(struct thread_info, kpc) ||
0380         TI_KPSR        != offsetof(struct thread_info, kpsr) ||
0381         TI_KWIM        != offsetof(struct thread_info, kwim) ||
0382         TI_REG_WINDOW  != offsetof(struct thread_info, reg_window) ||
0383         TI_RWIN_SPTRS  != offsetof(struct thread_info, rwbuf_stkptrs) ||
0384         TI_W_SAVED     != offsetof(struct thread_info, w_saved))
0385         thread_info_offsets_are_bolixed_pete();
0386 
0387     /* Attach to the address space of init_task. */
0388     mmgrab(&init_mm);
0389     current->active_mm = &init_mm;
0390 
0391     /* NOTE: Other cpus have this done as they are started
0392      *       up on SMP.
0393      */
0394 }