traps.c 14 KB

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