traps_32.c 11 KB

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