traps.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * arch/sparc/kernel/traps.c
  3. *
  4. * Copyright 1995 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright 2000 Jakub Jelinek (jakub@redhat.com)
  6. */
  7. /*
  8. * I hate traps on the sparc, grrr...
  9. */
  10. #include <linux/sched.h> /* for jiffies */
  11. #include <linux/kernel.h>
  12. #include <linux/kallsyms.h>
  13. #include <linux/signal.h>
  14. #include <linux/smp.h>
  15. #include <linux/smp_lock.h>
  16. #include <linux/kdebug.h>
  17. #include <asm/delay.h>
  18. #include <asm/system.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/oplib.h>
  21. #include <asm/page.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/unistd.h>
  24. #include <asm/traps.h>
  25. /* #define TRAP_DEBUG */
  26. struct trap_trace_entry {
  27. unsigned long pc;
  28. unsigned long type;
  29. };
  30. void syscall_trace_entry(struct pt_regs *regs)
  31. {
  32. printk("%s[%d]: ", current->comm, task_pid_nr(current));
  33. printk("scall<%d> (could be %d)\n", (int) regs->u_regs[UREG_G1],
  34. (int) regs->u_regs[UREG_I0]);
  35. }
  36. void syscall_trace_exit(struct pt_regs *regs)
  37. {
  38. }
  39. void sun4m_nmi(struct pt_regs *regs)
  40. {
  41. unsigned long afsr, afar;
  42. printk("Aieee: sun4m NMI received!\n");
  43. /* XXX HyperSparc hack XXX */
  44. __asm__ __volatile__("mov 0x500, %%g1\n\t"
  45. "lda [%%g1] 0x4, %0\n\t"
  46. "mov 0x600, %%g1\n\t"
  47. "lda [%%g1] 0x4, %1\n\t" :
  48. "=r" (afsr), "=r" (afar));
  49. printk("afsr=%08lx afar=%08lx\n", afsr, afar);
  50. printk("you lose buddy boy...\n");
  51. show_regs(regs);
  52. prom_halt();
  53. }
  54. void sun4d_nmi(struct pt_regs *regs)
  55. {
  56. printk("Aieee: sun4d NMI received!\n");
  57. printk("you lose buddy boy...\n");
  58. show_regs(regs);
  59. prom_halt();
  60. }
  61. static void instruction_dump(unsigned long *pc)
  62. {
  63. int i;
  64. if((((unsigned long) pc) & 3))
  65. return;
  66. for(i = -3; i < 6; i++)
  67. printk("%c%08lx%c",i?' ':'<',pc[i],i?' ':'>');
  68. printk("\n");
  69. }
  70. #define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t")
  71. #define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t")
  72. void die_if_kernel(char *str, struct pt_regs *regs)
  73. {
  74. static int die_counter;
  75. int count = 0;
  76. /* Amuse the user. */
  77. printk(
  78. " \\|/ ____ \\|/\n"
  79. " \"@'/ ,. \\`@\"\n"
  80. " /_| \\__/ |_\\\n"
  81. " \\__U_/\n");
  82. printk("%s(%d): %s [#%d]\n", current->comm, task_pid_nr(current), str, ++die_counter);
  83. show_regs(regs);
  84. add_taint(TAINT_DIE);
  85. __SAVE; __SAVE; __SAVE; __SAVE;
  86. __SAVE; __SAVE; __SAVE; __SAVE;
  87. __RESTORE; __RESTORE; __RESTORE; __RESTORE;
  88. __RESTORE; __RESTORE; __RESTORE; __RESTORE;
  89. {
  90. struct reg_window *rw = (struct reg_window *)regs->u_regs[UREG_FP];
  91. /* Stop the back trace when we hit userland or we
  92. * find some badly aligned kernel stack. Set an upper
  93. * bound in case our stack is trashed and we loop.
  94. */
  95. while(rw &&
  96. count++ < 30 &&
  97. (((unsigned long) rw) >= PAGE_OFFSET) &&
  98. !(((unsigned long) rw) & 0x7)) {
  99. printk("Caller[%08lx]", rw->ins[7]);
  100. print_symbol(": %s\n", rw->ins[7]);
  101. rw = (struct reg_window *)rw->ins[6];
  102. }
  103. }
  104. printk("Instruction DUMP:");
  105. instruction_dump ((unsigned long *) regs->pc);
  106. if(regs->psr & PSR_PS)
  107. do_exit(SIGKILL);
  108. do_exit(SIGSEGV);
  109. }
  110. void do_hw_interrupt(struct pt_regs *regs, unsigned long type)
  111. {
  112. siginfo_t info;
  113. if(type < 0x80) {
  114. /* Sun OS's puke from bad traps, Linux survives! */
  115. printk("Unimplemented Sparc TRAP, type = %02lx\n", type);
  116. die_if_kernel("Whee... Hello Mr. Penguin", regs);
  117. }
  118. if(regs->psr & PSR_PS)
  119. die_if_kernel("Kernel bad trap", regs);
  120. info.si_signo = SIGILL;
  121. info.si_errno = 0;
  122. info.si_code = ILL_ILLTRP;
  123. info.si_addr = (void __user *)regs->pc;
  124. info.si_trapno = type - 0x80;
  125. force_sig_info(SIGILL, &info, current);
  126. }
  127. void do_illegal_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  128. unsigned long psr)
  129. {
  130. extern int do_user_muldiv (struct pt_regs *, unsigned long);
  131. siginfo_t info;
  132. if(psr & PSR_PS)
  133. die_if_kernel("Kernel illegal instruction", regs);
  134. #ifdef TRAP_DEBUG
  135. printk("Ill instr. at pc=%08lx instruction is %08lx\n",
  136. regs->pc, *(unsigned long *)regs->pc);
  137. #endif
  138. if (!do_user_muldiv (regs, pc))
  139. return;
  140. info.si_signo = SIGILL;
  141. info.si_errno = 0;
  142. info.si_code = ILL_ILLOPC;
  143. info.si_addr = (void __user *)pc;
  144. info.si_trapno = 0;
  145. send_sig_info(SIGILL, &info, current);
  146. }
  147. void do_priv_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  148. unsigned long psr)
  149. {
  150. siginfo_t info;
  151. if(psr & PSR_PS)
  152. die_if_kernel("Penguin instruction from Penguin mode??!?!", regs);
  153. info.si_signo = SIGILL;
  154. info.si_errno = 0;
  155. info.si_code = ILL_PRVOPC;
  156. info.si_addr = (void __user *)pc;
  157. info.si_trapno = 0;
  158. send_sig_info(SIGILL, &info, current);
  159. }
  160. /* XXX User may want to be allowed to do this. XXX */
  161. void do_memaccess_unaligned(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  162. unsigned long psr)
  163. {
  164. siginfo_t info;
  165. if(regs->psr & PSR_PS) {
  166. printk("KERNEL MNA at pc %08lx npc %08lx called by %08lx\n", pc, npc,
  167. regs->u_regs[UREG_RETPC]);
  168. die_if_kernel("BOGUS", regs);
  169. /* die_if_kernel("Kernel MNA access", regs); */
  170. }
  171. #if 0
  172. show_regs (regs);
  173. instruction_dump ((unsigned long *) regs->pc);
  174. printk ("do_MNA!\n");
  175. #endif
  176. info.si_signo = SIGBUS;
  177. info.si_errno = 0;
  178. info.si_code = BUS_ADRALN;
  179. info.si_addr = /* FIXME: Should dig out mna address */ (void *)0;
  180. info.si_trapno = 0;
  181. send_sig_info(SIGBUS, &info, current);
  182. }
  183. extern void fpsave(unsigned long *fpregs, unsigned long *fsr,
  184. void *fpqueue, unsigned long *fpqdepth);
  185. extern void fpload(unsigned long *fpregs, unsigned long *fsr);
  186. static unsigned long init_fsr = 0x0UL;
  187. static unsigned long init_fregs[32] __attribute__ ((aligned (8))) =
  188. { ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
  189. ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
  190. ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
  191. ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL };
  192. void do_fpd_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  193. unsigned long psr)
  194. {
  195. /* Sanity check... */
  196. if(psr & PSR_PS)
  197. die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs);
  198. put_psr(get_psr() | PSR_EF); /* Allow FPU ops. */
  199. regs->psr |= PSR_EF;
  200. #ifndef CONFIG_SMP
  201. if(last_task_used_math == current)
  202. return;
  203. if(last_task_used_math) {
  204. /* Other processes fpu state, save away */
  205. struct task_struct *fptask = last_task_used_math;
  206. fpsave(&fptask->thread.float_regs[0], &fptask->thread.fsr,
  207. &fptask->thread.fpqueue[0], &fptask->thread.fpqdepth);
  208. }
  209. last_task_used_math = current;
  210. if(used_math()) {
  211. fpload(&current->thread.float_regs[0], &current->thread.fsr);
  212. } else {
  213. /* Set initial sane state. */
  214. fpload(&init_fregs[0], &init_fsr);
  215. set_used_math();
  216. }
  217. #else
  218. if(!used_math()) {
  219. fpload(&init_fregs[0], &init_fsr);
  220. set_used_math();
  221. } else {
  222. fpload(&current->thread.float_regs[0], &current->thread.fsr);
  223. }
  224. set_thread_flag(TIF_USEDFPU);
  225. #endif
  226. }
  227. static unsigned long fake_regs[32] __attribute__ ((aligned (8)));
  228. static unsigned long fake_fsr;
  229. static unsigned long fake_queue[32] __attribute__ ((aligned (8)));
  230. static unsigned long fake_depth;
  231. extern int do_mathemu(struct pt_regs *, struct task_struct *);
  232. void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  233. unsigned long psr)
  234. {
  235. static int calls;
  236. siginfo_t info;
  237. unsigned long fsr;
  238. int ret = 0;
  239. #ifndef CONFIG_SMP
  240. struct task_struct *fpt = last_task_used_math;
  241. #else
  242. struct task_struct *fpt = current;
  243. #endif
  244. put_psr(get_psr() | PSR_EF);
  245. /* If nobody owns the fpu right now, just clear the
  246. * error into our fake static buffer and hope it don't
  247. * happen again. Thank you crashme...
  248. */
  249. #ifndef CONFIG_SMP
  250. if(!fpt) {
  251. #else
  252. if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) {
  253. #endif
  254. fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
  255. regs->psr &= ~PSR_EF;
  256. return;
  257. }
  258. fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
  259. &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
  260. #ifdef DEBUG_FPU
  261. printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
  262. #endif
  263. switch ((fpt->thread.fsr & 0x1c000)) {
  264. /* switch on the contents of the ftt [floating point trap type] field */
  265. #ifdef DEBUG_FPU
  266. case (1 << 14):
  267. printk("IEEE_754_exception\n");
  268. break;
  269. #endif
  270. case (2 << 14): /* unfinished_FPop (underflow & co) */
  271. case (3 << 14): /* unimplemented_FPop (quad stuff, maybe sqrt) */
  272. ret = do_mathemu(regs, fpt);
  273. break;
  274. #ifdef DEBUG_FPU
  275. case (4 << 14):
  276. printk("sequence_error (OS bug...)\n");
  277. break;
  278. case (5 << 14):
  279. printk("hardware_error (uhoh!)\n");
  280. break;
  281. case (6 << 14):
  282. printk("invalid_fp_register (user error)\n");
  283. break;
  284. #endif /* DEBUG_FPU */
  285. }
  286. /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
  287. if (ret) {
  288. fpload(&current->thread.float_regs[0], &current->thread.fsr);
  289. return;
  290. }
  291. /* nope, better SIGFPE the offending process... */
  292. #ifdef CONFIG_SMP
  293. clear_tsk_thread_flag(fpt, TIF_USEDFPU);
  294. #endif
  295. if(psr & PSR_PS) {
  296. /* The first fsr store/load we tried trapped,
  297. * the second one will not (we hope).
  298. */
  299. printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
  300. regs->pc);
  301. regs->pc = regs->npc;
  302. regs->npc += 4;
  303. calls++;
  304. if(calls > 2)
  305. die_if_kernel("Too many Penguin-FPU traps from kernel mode",
  306. regs);
  307. return;
  308. }
  309. fsr = fpt->thread.fsr;
  310. info.si_signo = SIGFPE;
  311. info.si_errno = 0;
  312. info.si_addr = (void __user *)pc;
  313. info.si_trapno = 0;
  314. info.si_code = __SI_FAULT;
  315. if ((fsr & 0x1c000) == (1 << 14)) {
  316. if (fsr & 0x10)
  317. info.si_code = FPE_FLTINV;
  318. else if (fsr & 0x08)
  319. info.si_code = FPE_FLTOVF;
  320. else if (fsr & 0x04)
  321. info.si_code = FPE_FLTUND;
  322. else if (fsr & 0x02)
  323. info.si_code = FPE_FLTDIV;
  324. else if (fsr & 0x01)
  325. info.si_code = FPE_FLTRES;
  326. }
  327. send_sig_info(SIGFPE, &info, fpt);
  328. #ifndef CONFIG_SMP
  329. last_task_used_math = NULL;
  330. #endif
  331. regs->psr &= ~PSR_EF;
  332. if(calls > 0)
  333. calls=0;
  334. }
  335. void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  336. unsigned long psr)
  337. {
  338. siginfo_t info;
  339. if(psr & PSR_PS)
  340. die_if_kernel("Penguin overflow trap from kernel mode", regs);
  341. info.si_signo = SIGEMT;
  342. info.si_errno = 0;
  343. info.si_code = EMT_TAGOVF;
  344. info.si_addr = (void __user *)pc;
  345. info.si_trapno = 0;
  346. send_sig_info(SIGEMT, &info, current);
  347. }
  348. void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  349. unsigned long psr)
  350. {
  351. #ifdef TRAP_DEBUG
  352. printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
  353. pc, npc, psr);
  354. #endif
  355. if(psr & PSR_PS)
  356. panic("Tell me what a watchpoint trap is, and I'll then deal "
  357. "with such a beast...");
  358. }
  359. void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  360. unsigned long psr)
  361. {
  362. siginfo_t info;
  363. #ifdef TRAP_DEBUG
  364. printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
  365. pc, npc, psr);
  366. #endif
  367. info.si_signo = SIGBUS;
  368. info.si_errno = 0;
  369. info.si_code = BUS_OBJERR;
  370. info.si_addr = (void __user *)pc;
  371. info.si_trapno = 0;
  372. force_sig_info(SIGBUS, &info, current);
  373. }
  374. void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  375. unsigned long psr)
  376. {
  377. siginfo_t info;
  378. info.si_signo = SIGILL;
  379. info.si_errno = 0;
  380. info.si_code = ILL_COPROC;
  381. info.si_addr = (void __user *)pc;
  382. info.si_trapno = 0;
  383. send_sig_info(SIGILL, &info, current);
  384. }
  385. void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  386. unsigned long psr)
  387. {
  388. siginfo_t info;
  389. #ifdef TRAP_DEBUG
  390. printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
  391. pc, npc, psr);
  392. #endif
  393. info.si_signo = SIGILL;
  394. info.si_errno = 0;
  395. info.si_code = ILL_COPROC;
  396. info.si_addr = (void __user *)pc;
  397. info.si_trapno = 0;
  398. send_sig_info(SIGILL, &info, current);
  399. }
  400. void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  401. unsigned long psr)
  402. {
  403. siginfo_t info;
  404. info.si_signo = SIGFPE;
  405. info.si_errno = 0;
  406. info.si_code = FPE_INTDIV;
  407. info.si_addr = (void __user *)pc;
  408. info.si_trapno = 0;
  409. send_sig_info(SIGFPE, &info, current);
  410. }
  411. #ifdef CONFIG_DEBUG_BUGVERBOSE
  412. void do_BUG(const char *file, int line)
  413. {
  414. // bust_spinlocks(1); XXX Not in our original BUG()
  415. printk("kernel BUG at %s:%d!\n", file, line);
  416. }
  417. #endif
  418. /* Since we have our mappings set up, on multiprocessors we can spin them
  419. * up here so that timer interrupts work during initialization.
  420. */
  421. extern void sparc_cpu_startup(void);
  422. void trap_init(void)
  423. {
  424. extern void thread_info_offsets_are_bolixed_pete(void);
  425. /* Force linker to barf if mismatched */
  426. if (TI_UWINMASK != offsetof(struct thread_info, uwinmask) ||
  427. TI_TASK != offsetof(struct thread_info, task) ||
  428. TI_EXECDOMAIN != offsetof(struct thread_info, exec_domain) ||
  429. TI_FLAGS != offsetof(struct thread_info, flags) ||
  430. TI_CPU != offsetof(struct thread_info, cpu) ||
  431. TI_PREEMPT != offsetof(struct thread_info, preempt_count) ||
  432. TI_SOFTIRQ != offsetof(struct thread_info, softirq_count) ||
  433. TI_HARDIRQ != offsetof(struct thread_info, hardirq_count) ||
  434. TI_KSP != offsetof(struct thread_info, ksp) ||
  435. TI_KPC != offsetof(struct thread_info, kpc) ||
  436. TI_KPSR != offsetof(struct thread_info, kpsr) ||
  437. TI_KWIM != offsetof(struct thread_info, kwim) ||
  438. TI_REG_WINDOW != offsetof(struct thread_info, reg_window) ||
  439. TI_RWIN_SPTRS != offsetof(struct thread_info, rwbuf_stkptrs) ||
  440. TI_W_SAVED != offsetof(struct thread_info, w_saved))
  441. thread_info_offsets_are_bolixed_pete();
  442. /* Attach to the address space of init_task. */
  443. atomic_inc(&init_mm.mm_count);
  444. current->active_mm = &init_mm;
  445. /* NOTE: Other cpus have this done as they are started
  446. * up on SMP.
  447. */
  448. }