traps_32.c 11 KB

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