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