traps.c 14 KB

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