traps_32.c 11 KB

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