traps_32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 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);
  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. extern int do_mathemu(struct pt_regs *, struct task_struct *);
  191. void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  192. unsigned long psr)
  193. {
  194. static int calls;
  195. siginfo_t info;
  196. unsigned long fsr;
  197. int ret = 0;
  198. #ifndef CONFIG_SMP
  199. struct task_struct *fpt = last_task_used_math;
  200. #else
  201. struct task_struct *fpt = current;
  202. #endif
  203. put_psr(get_psr() | PSR_EF);
  204. /* If nobody owns the fpu right now, just clear the
  205. * error into our fake static buffer and hope it don't
  206. * happen again. Thank you crashme...
  207. */
  208. #ifndef CONFIG_SMP
  209. if(!fpt) {
  210. #else
  211. if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) {
  212. #endif
  213. fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
  214. regs->psr &= ~PSR_EF;
  215. return;
  216. }
  217. fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
  218. &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
  219. #ifdef DEBUG_FPU
  220. printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
  221. #endif
  222. switch ((fpt->thread.fsr & 0x1c000)) {
  223. /* switch on the contents of the ftt [floating point trap type] field */
  224. #ifdef DEBUG_FPU
  225. case (1 << 14):
  226. printk("IEEE_754_exception\n");
  227. break;
  228. #endif
  229. case (2 << 14): /* unfinished_FPop (underflow & co) */
  230. case (3 << 14): /* unimplemented_FPop (quad stuff, maybe sqrt) */
  231. ret = do_mathemu(regs, fpt);
  232. break;
  233. #ifdef DEBUG_FPU
  234. case (4 << 14):
  235. printk("sequence_error (OS bug...)\n");
  236. break;
  237. case (5 << 14):
  238. printk("hardware_error (uhoh!)\n");
  239. break;
  240. case (6 << 14):
  241. printk("invalid_fp_register (user error)\n");
  242. break;
  243. #endif /* DEBUG_FPU */
  244. }
  245. /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
  246. if (ret) {
  247. fpload(&current->thread.float_regs[0], &current->thread.fsr);
  248. return;
  249. }
  250. /* nope, better SIGFPE the offending process... */
  251. #ifdef CONFIG_SMP
  252. clear_tsk_thread_flag(fpt, TIF_USEDFPU);
  253. #endif
  254. if(psr & PSR_PS) {
  255. /* The first fsr store/load we tried trapped,
  256. * the second one will not (we hope).
  257. */
  258. printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
  259. regs->pc);
  260. regs->pc = regs->npc;
  261. regs->npc += 4;
  262. calls++;
  263. if(calls > 2)
  264. die_if_kernel("Too many Penguin-FPU traps from kernel mode",
  265. regs);
  266. return;
  267. }
  268. fsr = fpt->thread.fsr;
  269. info.si_signo = SIGFPE;
  270. info.si_errno = 0;
  271. info.si_addr = (void __user *)pc;
  272. info.si_trapno = 0;
  273. info.si_code = __SI_FAULT;
  274. if ((fsr & 0x1c000) == (1 << 14)) {
  275. if (fsr & 0x10)
  276. info.si_code = FPE_FLTINV;
  277. else if (fsr & 0x08)
  278. info.si_code = FPE_FLTOVF;
  279. else if (fsr & 0x04)
  280. info.si_code = FPE_FLTUND;
  281. else if (fsr & 0x02)
  282. info.si_code = FPE_FLTDIV;
  283. else if (fsr & 0x01)
  284. info.si_code = FPE_FLTRES;
  285. }
  286. send_sig_info(SIGFPE, &info, fpt);
  287. #ifndef CONFIG_SMP
  288. last_task_used_math = NULL;
  289. #endif
  290. regs->psr &= ~PSR_EF;
  291. if(calls > 0)
  292. calls=0;
  293. }
  294. void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  295. unsigned long psr)
  296. {
  297. siginfo_t info;
  298. if(psr & PSR_PS)
  299. die_if_kernel("Penguin overflow trap from kernel mode", regs);
  300. info.si_signo = SIGEMT;
  301. info.si_errno = 0;
  302. info.si_code = EMT_TAGOVF;
  303. info.si_addr = (void __user *)pc;
  304. info.si_trapno = 0;
  305. send_sig_info(SIGEMT, &info, current);
  306. }
  307. void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  308. unsigned long psr)
  309. {
  310. #ifdef TRAP_DEBUG
  311. printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
  312. pc, npc, psr);
  313. #endif
  314. if(psr & PSR_PS)
  315. panic("Tell me what a watchpoint trap is, and I'll then deal "
  316. "with such a beast...");
  317. }
  318. void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  319. unsigned long psr)
  320. {
  321. siginfo_t info;
  322. #ifdef TRAP_DEBUG
  323. printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
  324. pc, npc, psr);
  325. #endif
  326. info.si_signo = SIGBUS;
  327. info.si_errno = 0;
  328. info.si_code = BUS_OBJERR;
  329. info.si_addr = (void __user *)pc;
  330. info.si_trapno = 0;
  331. force_sig_info(SIGBUS, &info, current);
  332. }
  333. void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  334. unsigned long psr)
  335. {
  336. siginfo_t info;
  337. info.si_signo = SIGILL;
  338. info.si_errno = 0;
  339. info.si_code = ILL_COPROC;
  340. info.si_addr = (void __user *)pc;
  341. info.si_trapno = 0;
  342. send_sig_info(SIGILL, &info, current);
  343. }
  344. void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  345. unsigned long psr)
  346. {
  347. siginfo_t info;
  348. #ifdef TRAP_DEBUG
  349. printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
  350. pc, npc, psr);
  351. #endif
  352. info.si_signo = SIGILL;
  353. info.si_errno = 0;
  354. info.si_code = ILL_COPROC;
  355. info.si_addr = (void __user *)pc;
  356. info.si_trapno = 0;
  357. send_sig_info(SIGILL, &info, current);
  358. }
  359. void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  360. unsigned long psr)
  361. {
  362. siginfo_t info;
  363. info.si_signo = SIGFPE;
  364. info.si_errno = 0;
  365. info.si_code = FPE_INTDIV;
  366. info.si_addr = (void __user *)pc;
  367. info.si_trapno = 0;
  368. send_sig_info(SIGFPE, &info, current);
  369. }
  370. #ifdef CONFIG_DEBUG_BUGVERBOSE
  371. void do_BUG(const char *file, int line)
  372. {
  373. // bust_spinlocks(1); XXX Not in our original BUG()
  374. printk("kernel BUG at %s:%d!\n", file, line);
  375. }
  376. EXPORT_SYMBOL(do_BUG);
  377. #endif
  378. /* Since we have our mappings set up, on multiprocessors we can spin them
  379. * up here so that timer interrupts work during initialization.
  380. */
  381. void trap_init(void)
  382. {
  383. extern void thread_info_offsets_are_bolixed_pete(void);
  384. /* Force linker to barf if mismatched */
  385. if (TI_UWINMASK != offsetof(struct thread_info, uwinmask) ||
  386. TI_TASK != offsetof(struct thread_info, task) ||
  387. TI_EXECDOMAIN != offsetof(struct thread_info, exec_domain) ||
  388. TI_FLAGS != offsetof(struct thread_info, flags) ||
  389. TI_CPU != offsetof(struct thread_info, cpu) ||
  390. TI_PREEMPT != offsetof(struct thread_info, preempt_count) ||
  391. TI_SOFTIRQ != offsetof(struct thread_info, softirq_count) ||
  392. TI_HARDIRQ != offsetof(struct thread_info, hardirq_count) ||
  393. TI_KSP != offsetof(struct thread_info, ksp) ||
  394. TI_KPC != offsetof(struct thread_info, kpc) ||
  395. TI_KPSR != offsetof(struct thread_info, kpsr) ||
  396. TI_KWIM != offsetof(struct thread_info, kwim) ||
  397. TI_REG_WINDOW != offsetof(struct thread_info, reg_window) ||
  398. TI_RWIN_SPTRS != offsetof(struct thread_info, rwbuf_stkptrs) ||
  399. TI_W_SAVED != offsetof(struct thread_info, w_saved))
  400. thread_info_offsets_are_bolixed_pete();
  401. /* Attach to the address space of init_task. */
  402. atomic_inc(&init_mm.mm_count);
  403. current->active_mm = &init_mm;
  404. /* NOTE: Other cpus have this done as they are started
  405. * up on SMP.
  406. */
  407. }