traps.c 13 KB

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