traps_32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. if (!do_user_muldiv (regs, pc))
  103. return;
  104. info.si_signo = SIGILL;
  105. info.si_errno = 0;
  106. info.si_code = ILL_ILLOPC;
  107. info.si_addr = (void __user *)pc;
  108. info.si_trapno = 0;
  109. send_sig_info(SIGILL, &info, current);
  110. }
  111. void do_priv_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  112. unsigned long psr)
  113. {
  114. siginfo_t info;
  115. if(psr & PSR_PS)
  116. die_if_kernel("Penguin instruction from Penguin mode??!?!", regs);
  117. info.si_signo = SIGILL;
  118. info.si_errno = 0;
  119. info.si_code = ILL_PRVOPC;
  120. info.si_addr = (void __user *)pc;
  121. info.si_trapno = 0;
  122. send_sig_info(SIGILL, &info, current);
  123. }
  124. /* XXX User may want to be allowed to do this. XXX */
  125. void do_memaccess_unaligned(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  126. unsigned long psr)
  127. {
  128. siginfo_t info;
  129. if(regs->psr & PSR_PS) {
  130. printk("KERNEL MNA at pc %08lx npc %08lx called by %08lx\n", pc, npc,
  131. regs->u_regs[UREG_RETPC]);
  132. die_if_kernel("BOGUS", regs);
  133. /* die_if_kernel("Kernel MNA access", regs); */
  134. }
  135. #if 0
  136. show_regs (regs);
  137. instruction_dump ((unsigned long *) regs->pc);
  138. printk ("do_MNA!\n");
  139. #endif
  140. info.si_signo = SIGBUS;
  141. info.si_errno = 0;
  142. info.si_code = BUS_ADRALN;
  143. info.si_addr = /* FIXME: Should dig out mna address */ (void *)0;
  144. info.si_trapno = 0;
  145. send_sig_info(SIGBUS, &info, current);
  146. }
  147. static unsigned long init_fsr = 0x0UL;
  148. static unsigned long init_fregs[32] __attribute__ ((aligned (8))) =
  149. { ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
  150. ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
  151. ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL,
  152. ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL, ~0UL };
  153. void do_fpd_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  154. unsigned long psr)
  155. {
  156. /* Sanity check... */
  157. if(psr & PSR_PS)
  158. die_if_kernel("Kernel gets FloatingPenguinUnit disabled trap", regs);
  159. put_psr(get_psr() | PSR_EF); /* Allow FPU ops. */
  160. regs->psr |= PSR_EF;
  161. #ifndef CONFIG_SMP
  162. if(last_task_used_math == current)
  163. return;
  164. if(last_task_used_math) {
  165. /* Other processes fpu state, save away */
  166. struct task_struct *fptask = last_task_used_math;
  167. fpsave(&fptask->thread.float_regs[0], &fptask->thread.fsr,
  168. &fptask->thread.fpqueue[0], &fptask->thread.fpqdepth);
  169. }
  170. last_task_used_math = current;
  171. if(used_math()) {
  172. fpload(&current->thread.float_regs[0], &current->thread.fsr);
  173. } else {
  174. /* Set initial sane state. */
  175. fpload(&init_fregs[0], &init_fsr);
  176. set_used_math();
  177. }
  178. #else
  179. if(!used_math()) {
  180. fpload(&init_fregs[0], &init_fsr);
  181. set_used_math();
  182. } else {
  183. fpload(&current->thread.float_regs[0], &current->thread.fsr);
  184. }
  185. set_thread_flag(TIF_USEDFPU);
  186. #endif
  187. }
  188. static unsigned long fake_regs[32] __attribute__ ((aligned (8)));
  189. static unsigned long fake_fsr;
  190. static unsigned long fake_queue[32] __attribute__ ((aligned (8)));
  191. static unsigned long fake_depth;
  192. extern int do_mathemu(struct pt_regs *, struct task_struct *);
  193. void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  194. unsigned long psr)
  195. {
  196. static int calls;
  197. siginfo_t info;
  198. unsigned long fsr;
  199. int ret = 0;
  200. #ifndef CONFIG_SMP
  201. struct task_struct *fpt = last_task_used_math;
  202. #else
  203. struct task_struct *fpt = current;
  204. #endif
  205. put_psr(get_psr() | PSR_EF);
  206. /* If nobody owns the fpu right now, just clear the
  207. * error into our fake static buffer and hope it don't
  208. * happen again. Thank you crashme...
  209. */
  210. #ifndef CONFIG_SMP
  211. if(!fpt) {
  212. #else
  213. if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) {
  214. #endif
  215. fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
  216. regs->psr &= ~PSR_EF;
  217. return;
  218. }
  219. fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
  220. &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
  221. #ifdef DEBUG_FPU
  222. printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
  223. #endif
  224. switch ((fpt->thread.fsr & 0x1c000)) {
  225. /* switch on the contents of the ftt [floating point trap type] field */
  226. #ifdef DEBUG_FPU
  227. case (1 << 14):
  228. printk("IEEE_754_exception\n");
  229. break;
  230. #endif
  231. case (2 << 14): /* unfinished_FPop (underflow & co) */
  232. case (3 << 14): /* unimplemented_FPop (quad stuff, maybe sqrt) */
  233. ret = do_mathemu(regs, fpt);
  234. break;
  235. #ifdef DEBUG_FPU
  236. case (4 << 14):
  237. printk("sequence_error (OS bug...)\n");
  238. break;
  239. case (5 << 14):
  240. printk("hardware_error (uhoh!)\n");
  241. break;
  242. case (6 << 14):
  243. printk("invalid_fp_register (user error)\n");
  244. break;
  245. #endif /* DEBUG_FPU */
  246. }
  247. /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
  248. if (ret) {
  249. fpload(&current->thread.float_regs[0], &current->thread.fsr);
  250. return;
  251. }
  252. /* nope, better SIGFPE the offending process... */
  253. #ifdef CONFIG_SMP
  254. clear_tsk_thread_flag(fpt, TIF_USEDFPU);
  255. #endif
  256. if(psr & PSR_PS) {
  257. /* The first fsr store/load we tried trapped,
  258. * the second one will not (we hope).
  259. */
  260. printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
  261. regs->pc);
  262. regs->pc = regs->npc;
  263. regs->npc += 4;
  264. calls++;
  265. if(calls > 2)
  266. die_if_kernel("Too many Penguin-FPU traps from kernel mode",
  267. regs);
  268. return;
  269. }
  270. fsr = fpt->thread.fsr;
  271. info.si_signo = SIGFPE;
  272. info.si_errno = 0;
  273. info.si_addr = (void __user *)pc;
  274. info.si_trapno = 0;
  275. info.si_code = __SI_FAULT;
  276. if ((fsr & 0x1c000) == (1 << 14)) {
  277. if (fsr & 0x10)
  278. info.si_code = FPE_FLTINV;
  279. else if (fsr & 0x08)
  280. info.si_code = FPE_FLTOVF;
  281. else if (fsr & 0x04)
  282. info.si_code = FPE_FLTUND;
  283. else if (fsr & 0x02)
  284. info.si_code = FPE_FLTDIV;
  285. else if (fsr & 0x01)
  286. info.si_code = FPE_FLTRES;
  287. }
  288. send_sig_info(SIGFPE, &info, fpt);
  289. #ifndef CONFIG_SMP
  290. last_task_used_math = NULL;
  291. #endif
  292. regs->psr &= ~PSR_EF;
  293. if(calls > 0)
  294. calls=0;
  295. }
  296. void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  297. unsigned long psr)
  298. {
  299. siginfo_t info;
  300. if(psr & PSR_PS)
  301. die_if_kernel("Penguin overflow trap from kernel mode", regs);
  302. info.si_signo = SIGEMT;
  303. info.si_errno = 0;
  304. info.si_code = EMT_TAGOVF;
  305. info.si_addr = (void __user *)pc;
  306. info.si_trapno = 0;
  307. send_sig_info(SIGEMT, &info, current);
  308. }
  309. void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  310. unsigned long psr)
  311. {
  312. #ifdef TRAP_DEBUG
  313. printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
  314. pc, npc, psr);
  315. #endif
  316. if(psr & PSR_PS)
  317. panic("Tell me what a watchpoint trap is, and I'll then deal "
  318. "with such a beast...");
  319. }
  320. void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  321. unsigned long psr)
  322. {
  323. siginfo_t info;
  324. #ifdef TRAP_DEBUG
  325. printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
  326. pc, npc, psr);
  327. #endif
  328. info.si_signo = SIGBUS;
  329. info.si_errno = 0;
  330. info.si_code = BUS_OBJERR;
  331. info.si_addr = (void __user *)pc;
  332. info.si_trapno = 0;
  333. force_sig_info(SIGBUS, &info, current);
  334. }
  335. void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  336. unsigned long psr)
  337. {
  338. siginfo_t info;
  339. info.si_signo = SIGILL;
  340. info.si_errno = 0;
  341. info.si_code = ILL_COPROC;
  342. info.si_addr = (void __user *)pc;
  343. info.si_trapno = 0;
  344. send_sig_info(SIGILL, &info, current);
  345. }
  346. void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  347. unsigned long psr)
  348. {
  349. siginfo_t info;
  350. #ifdef TRAP_DEBUG
  351. printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
  352. pc, npc, psr);
  353. #endif
  354. info.si_signo = SIGILL;
  355. info.si_errno = 0;
  356. info.si_code = ILL_COPROC;
  357. info.si_addr = (void __user *)pc;
  358. info.si_trapno = 0;
  359. send_sig_info(SIGILL, &info, current);
  360. }
  361. void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  362. unsigned long psr)
  363. {
  364. siginfo_t info;
  365. info.si_signo = SIGFPE;
  366. info.si_errno = 0;
  367. info.si_code = FPE_INTDIV;
  368. info.si_addr = (void __user *)pc;
  369. info.si_trapno = 0;
  370. send_sig_info(SIGFPE, &info, current);
  371. }
  372. #ifdef CONFIG_DEBUG_BUGVERBOSE
  373. void do_BUG(const char *file, int line)
  374. {
  375. // bust_spinlocks(1); XXX Not in our original BUG()
  376. printk("kernel BUG at %s:%d!\n", file, line);
  377. }
  378. EXPORT_SYMBOL(do_BUG);
  379. #endif
  380. /* Since we have our mappings set up, on multiprocessors we can spin them
  381. * up here so that timer interrupts work during initialization.
  382. */
  383. void trap_init(void)
  384. {
  385. extern void thread_info_offsets_are_bolixed_pete(void);
  386. /* Force linker to barf if mismatched */
  387. if (TI_UWINMASK != offsetof(struct thread_info, uwinmask) ||
  388. TI_TASK != offsetof(struct thread_info, task) ||
  389. TI_EXECDOMAIN != offsetof(struct thread_info, exec_domain) ||
  390. TI_FLAGS != offsetof(struct thread_info, flags) ||
  391. TI_CPU != offsetof(struct thread_info, cpu) ||
  392. TI_PREEMPT != offsetof(struct thread_info, preempt_count) ||
  393. TI_SOFTIRQ != offsetof(struct thread_info, softirq_count) ||
  394. TI_HARDIRQ != offsetof(struct thread_info, hardirq_count) ||
  395. TI_KSP != offsetof(struct thread_info, ksp) ||
  396. TI_KPC != offsetof(struct thread_info, kpc) ||
  397. TI_KPSR != offsetof(struct thread_info, kpsr) ||
  398. TI_KWIM != offsetof(struct thread_info, kwim) ||
  399. TI_REG_WINDOW != offsetof(struct thread_info, reg_window) ||
  400. TI_RWIN_SPTRS != offsetof(struct thread_info, rwbuf_stkptrs) ||
  401. TI_W_SAVED != offsetof(struct thread_info, w_saved))
  402. thread_info_offsets_are_bolixed_pete();
  403. /* Attach to the address space of init_task. */
  404. atomic_inc(&init_mm.mm_count);
  405. current->active_mm = &init_mm;
  406. /* NOTE: Other cpus have this done as they are started
  407. * up on SMP.
  408. */
  409. }