traps_32.c 12 KB

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