traps.c 13 KB

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