traps_32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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/mm.h>
  11. #include <linux/sched/debug.h>
  12. #include <linux/mm_types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/signal.h>
  15. #include <linux/smp.h>
  16. #include <linux/kdebug.h>
  17. #include <linux/export.h>
  18. #include <asm/delay.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/oplib.h>
  21. #include <asm/page.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/unistd.h>
  24. #include <asm/traps.h>
  25. #include "entry.h"
  26. #include "kernel.h"
  27. /* #define TRAP_DEBUG */
  28. static void instruction_dump(unsigned long *pc)
  29. {
  30. int i;
  31. if((((unsigned long) pc) & 3))
  32. return;
  33. for(i = -3; i < 6; i++)
  34. printk("%c%08lx%c",i?' ':'<',pc[i],i?' ':'>');
  35. printk("\n");
  36. }
  37. #define __SAVE __asm__ __volatile__("save %sp, -0x40, %sp\n\t")
  38. #define __RESTORE __asm__ __volatile__("restore %g0, %g0, %g0\n\t")
  39. void __noreturn die_if_kernel(char *str, struct pt_regs *regs)
  40. {
  41. static int die_counter;
  42. int count = 0;
  43. /* Amuse the user. */
  44. printk(
  45. " \\|/ ____ \\|/\n"
  46. " \"@'/ ,. \\`@\"\n"
  47. " /_| \\__/ |_\\\n"
  48. " \\__U_/\n");
  49. printk("%s(%d): %s [#%d]\n", current->comm, task_pid_nr(current), str, ++die_counter);
  50. show_regs(regs);
  51. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  52. __SAVE; __SAVE; __SAVE; __SAVE;
  53. __SAVE; __SAVE; __SAVE; __SAVE;
  54. __RESTORE; __RESTORE; __RESTORE; __RESTORE;
  55. __RESTORE; __RESTORE; __RESTORE; __RESTORE;
  56. {
  57. struct reg_window32 *rw = (struct reg_window32 *)regs->u_regs[UREG_FP];
  58. /* Stop the back trace when we hit userland or we
  59. * find some badly aligned kernel stack. Set an upper
  60. * bound in case our stack is trashed and we loop.
  61. */
  62. while(rw &&
  63. count++ < 30 &&
  64. (((unsigned long) rw) >= PAGE_OFFSET) &&
  65. !(((unsigned long) rw) & 0x7)) {
  66. printk("Caller[%08lx]: %pS\n", rw->ins[7],
  67. (void *) rw->ins[7]);
  68. rw = (struct reg_window32 *)rw->ins[6];
  69. }
  70. }
  71. printk("Instruction DUMP:");
  72. instruction_dump ((unsigned long *) regs->pc);
  73. if(regs->psr & PSR_PS)
  74. do_exit(SIGKILL);
  75. do_exit(SIGSEGV);
  76. }
  77. void do_hw_interrupt(struct pt_regs *regs, unsigned long type)
  78. {
  79. siginfo_t info;
  80. if(type < 0x80) {
  81. /* Sun OS's puke from bad traps, Linux survives! */
  82. printk("Unimplemented Sparc TRAP, type = %02lx\n", type);
  83. die_if_kernel("Whee... Hello Mr. Penguin", regs);
  84. }
  85. if(regs->psr & PSR_PS)
  86. die_if_kernel("Kernel bad trap", regs);
  87. info.si_signo = SIGILL;
  88. info.si_errno = 0;
  89. info.si_code = ILL_ILLTRP;
  90. info.si_addr = (void __user *)regs->pc;
  91. info.si_trapno = type - 0x80;
  92. force_sig_info(SIGILL, &info, current);
  93. }
  94. void do_illegal_instruction(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  95. unsigned long psr)
  96. {
  97. siginfo_t info;
  98. if(psr & PSR_PS)
  99. die_if_kernel("Kernel illegal instruction", regs);
  100. #ifdef TRAP_DEBUG
  101. printk("Ill instr. at pc=%08lx instruction is %08lx\n",
  102. regs->pc, *(unsigned long *)regs->pc);
  103. #endif
  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. void do_fpe_trap(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  193. unsigned long psr)
  194. {
  195. static int calls;
  196. siginfo_t info;
  197. unsigned long fsr;
  198. int ret = 0;
  199. #ifndef CONFIG_SMP
  200. struct task_struct *fpt = last_task_used_math;
  201. #else
  202. struct task_struct *fpt = current;
  203. #endif
  204. put_psr(get_psr() | PSR_EF);
  205. /* If nobody owns the fpu right now, just clear the
  206. * error into our fake static buffer and hope it don't
  207. * happen again. Thank you crashme...
  208. */
  209. #ifndef CONFIG_SMP
  210. if(!fpt) {
  211. #else
  212. if (!test_tsk_thread_flag(fpt, TIF_USEDFPU)) {
  213. #endif
  214. fpsave(&fake_regs[0], &fake_fsr, &fake_queue[0], &fake_depth);
  215. regs->psr &= ~PSR_EF;
  216. return;
  217. }
  218. fpsave(&fpt->thread.float_regs[0], &fpt->thread.fsr,
  219. &fpt->thread.fpqueue[0], &fpt->thread.fpqdepth);
  220. #ifdef DEBUG_FPU
  221. printk("Hmm, FP exception, fsr was %016lx\n", fpt->thread.fsr);
  222. #endif
  223. switch ((fpt->thread.fsr & 0x1c000)) {
  224. /* switch on the contents of the ftt [floating point trap type] field */
  225. #ifdef DEBUG_FPU
  226. case (1 << 14):
  227. printk("IEEE_754_exception\n");
  228. break;
  229. #endif
  230. case (2 << 14): /* unfinished_FPop (underflow & co) */
  231. case (3 << 14): /* unimplemented_FPop (quad stuff, maybe sqrt) */
  232. ret = do_mathemu(regs, fpt);
  233. break;
  234. #ifdef DEBUG_FPU
  235. case (4 << 14):
  236. printk("sequence_error (OS bug...)\n");
  237. break;
  238. case (5 << 14):
  239. printk("hardware_error (uhoh!)\n");
  240. break;
  241. case (6 << 14):
  242. printk("invalid_fp_register (user error)\n");
  243. break;
  244. #endif /* DEBUG_FPU */
  245. }
  246. /* If we successfully emulated the FPop, we pretend the trap never happened :-> */
  247. if (ret) {
  248. fpload(&current->thread.float_regs[0], &current->thread.fsr);
  249. return;
  250. }
  251. /* nope, better SIGFPE the offending process... */
  252. #ifdef CONFIG_SMP
  253. clear_tsk_thread_flag(fpt, TIF_USEDFPU);
  254. #endif
  255. if(psr & PSR_PS) {
  256. /* The first fsr store/load we tried trapped,
  257. * the second one will not (we hope).
  258. */
  259. printk("WARNING: FPU exception from kernel mode. at pc=%08lx\n",
  260. regs->pc);
  261. regs->pc = regs->npc;
  262. regs->npc += 4;
  263. calls++;
  264. if(calls > 2)
  265. die_if_kernel("Too many Penguin-FPU traps from kernel mode",
  266. regs);
  267. return;
  268. }
  269. fsr = fpt->thread.fsr;
  270. info.si_signo = SIGFPE;
  271. info.si_errno = 0;
  272. info.si_addr = (void __user *)pc;
  273. info.si_trapno = 0;
  274. info.si_code = __SI_FAULT;
  275. if ((fsr & 0x1c000) == (1 << 14)) {
  276. if (fsr & 0x10)
  277. info.si_code = FPE_FLTINV;
  278. else if (fsr & 0x08)
  279. info.si_code = FPE_FLTOVF;
  280. else if (fsr & 0x04)
  281. info.si_code = FPE_FLTUND;
  282. else if (fsr & 0x02)
  283. info.si_code = FPE_FLTDIV;
  284. else if (fsr & 0x01)
  285. info.si_code = FPE_FLTRES;
  286. }
  287. send_sig_info(SIGFPE, &info, fpt);
  288. #ifndef CONFIG_SMP
  289. last_task_used_math = NULL;
  290. #endif
  291. regs->psr &= ~PSR_EF;
  292. if(calls > 0)
  293. calls=0;
  294. }
  295. void handle_tag_overflow(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  296. unsigned long psr)
  297. {
  298. siginfo_t info;
  299. if(psr & PSR_PS)
  300. die_if_kernel("Penguin overflow trap from kernel mode", regs);
  301. info.si_signo = SIGEMT;
  302. info.si_errno = 0;
  303. info.si_code = EMT_TAGOVF;
  304. info.si_addr = (void __user *)pc;
  305. info.si_trapno = 0;
  306. send_sig_info(SIGEMT, &info, current);
  307. }
  308. void handle_watchpoint(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  309. unsigned long psr)
  310. {
  311. #ifdef TRAP_DEBUG
  312. printk("Watchpoint detected at PC %08lx NPC %08lx PSR %08lx\n",
  313. pc, npc, psr);
  314. #endif
  315. if(psr & PSR_PS)
  316. panic("Tell me what a watchpoint trap is, and I'll then deal "
  317. "with such a beast...");
  318. }
  319. void handle_reg_access(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  320. unsigned long psr)
  321. {
  322. siginfo_t info;
  323. #ifdef TRAP_DEBUG
  324. printk("Register Access Exception at PC %08lx NPC %08lx PSR %08lx\n",
  325. pc, npc, psr);
  326. #endif
  327. info.si_signo = SIGBUS;
  328. info.si_errno = 0;
  329. info.si_code = BUS_OBJERR;
  330. info.si_addr = (void __user *)pc;
  331. info.si_trapno = 0;
  332. force_sig_info(SIGBUS, &info, current);
  333. }
  334. void handle_cp_disabled(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  335. unsigned long psr)
  336. {
  337. siginfo_t info;
  338. info.si_signo = SIGILL;
  339. info.si_errno = 0;
  340. info.si_code = ILL_COPROC;
  341. info.si_addr = (void __user *)pc;
  342. info.si_trapno = 0;
  343. send_sig_info(SIGILL, &info, current);
  344. }
  345. void handle_cp_exception(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  346. unsigned long psr)
  347. {
  348. siginfo_t info;
  349. #ifdef TRAP_DEBUG
  350. printk("Co-Processor Exception at PC %08lx NPC %08lx PSR %08lx\n",
  351. pc, npc, psr);
  352. #endif
  353. info.si_signo = SIGILL;
  354. info.si_errno = 0;
  355. info.si_code = ILL_COPROC;
  356. info.si_addr = (void __user *)pc;
  357. info.si_trapno = 0;
  358. send_sig_info(SIGILL, &info, current);
  359. }
  360. void handle_hw_divzero(struct pt_regs *regs, unsigned long pc, unsigned long npc,
  361. unsigned long psr)
  362. {
  363. siginfo_t info;
  364. info.si_signo = SIGFPE;
  365. info.si_errno = 0;
  366. info.si_code = FPE_INTDIV;
  367. info.si_addr = (void __user *)pc;
  368. info.si_trapno = 0;
  369. send_sig_info(SIGFPE, &info, current);
  370. }
  371. #ifdef CONFIG_DEBUG_BUGVERBOSE
  372. void do_BUG(const char *file, int line)
  373. {
  374. // bust_spinlocks(1); XXX Not in our original BUG()
  375. printk("kernel BUG at %s:%d!\n", file, line);
  376. }
  377. EXPORT_SYMBOL(do_BUG);
  378. #endif
  379. /* Since we have our mappings set up, on multiprocessors we can spin them
  380. * up here so that timer interrupts work during initialization.
  381. */
  382. void trap_init(void)
  383. {
  384. extern void thread_info_offsets_are_bolixed_pete(void);
  385. /* Force linker to barf if mismatched */
  386. if (TI_UWINMASK != offsetof(struct thread_info, uwinmask) ||
  387. TI_TASK != offsetof(struct thread_info, task) ||
  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. mmgrab(&init_mm);
  403. current->active_mm = &init_mm;
  404. /* NOTE: Other cpus have this done as they are started
  405. * up on SMP.
  406. */
  407. }