traps.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/alpha/kernel/traps.c
  4. *
  5. * (C) Copyright 1994 Linus Torvalds
  6. */
  7. /*
  8. * This file initializes the trap entry points
  9. */
  10. #include <linux/jiffies.h>
  11. #include <linux/mm.h>
  12. #include <linux/sched/signal.h>
  13. #include <linux/sched/debug.h>
  14. #include <linux/tty.h>
  15. #include <linux/delay.h>
  16. #include <linux/extable.h>
  17. #include <linux/kallsyms.h>
  18. #include <linux/ratelimit.h>
  19. #include <asm/gentrap.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/unaligned.h>
  22. #include <asm/sysinfo.h>
  23. #include <asm/hwrpb.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/special_insns.h>
  26. #include "proto.h"
  27. /* Work-around for some SRMs which mishandle opDEC faults. */
  28. static int opDEC_fix;
  29. static void
  30. opDEC_check(void)
  31. {
  32. __asm__ __volatile__ (
  33. /* Load the address of... */
  34. " br $16, 1f\n"
  35. /* A stub instruction fault handler. Just add 4 to the
  36. pc and continue. */
  37. " ldq $16, 8($sp)\n"
  38. " addq $16, 4, $16\n"
  39. " stq $16, 8($sp)\n"
  40. " call_pal %[rti]\n"
  41. /* Install the instruction fault handler. */
  42. "1: lda $17, 3\n"
  43. " call_pal %[wrent]\n"
  44. /* With that in place, the fault from the round-to-minf fp
  45. insn will arrive either at the "lda 4" insn (bad) or one
  46. past that (good). This places the correct fixup in %0. */
  47. " lda %[fix], 0\n"
  48. " cvttq/svm $f31,$f31\n"
  49. " lda %[fix], 4"
  50. : [fix] "=r" (opDEC_fix)
  51. : [rti] "n" (PAL_rti), [wrent] "n" (PAL_wrent)
  52. : "$0", "$1", "$16", "$17", "$22", "$23", "$24", "$25");
  53. if (opDEC_fix)
  54. printk("opDEC fixup enabled.\n");
  55. }
  56. void
  57. dik_show_regs(struct pt_regs *regs, unsigned long *r9_15)
  58. {
  59. printk("pc = [<%016lx>] ra = [<%016lx>] ps = %04lx %s\n",
  60. regs->pc, regs->r26, regs->ps, print_tainted());
  61. printk("pc is at %pSR\n", (void *)regs->pc);
  62. printk("ra is at %pSR\n", (void *)regs->r26);
  63. printk("v0 = %016lx t0 = %016lx t1 = %016lx\n",
  64. regs->r0, regs->r1, regs->r2);
  65. printk("t2 = %016lx t3 = %016lx t4 = %016lx\n",
  66. regs->r3, regs->r4, regs->r5);
  67. printk("t5 = %016lx t6 = %016lx t7 = %016lx\n",
  68. regs->r6, regs->r7, regs->r8);
  69. if (r9_15) {
  70. printk("s0 = %016lx s1 = %016lx s2 = %016lx\n",
  71. r9_15[9], r9_15[10], r9_15[11]);
  72. printk("s3 = %016lx s4 = %016lx s5 = %016lx\n",
  73. r9_15[12], r9_15[13], r9_15[14]);
  74. printk("s6 = %016lx\n", r9_15[15]);
  75. }
  76. printk("a0 = %016lx a1 = %016lx a2 = %016lx\n",
  77. regs->r16, regs->r17, regs->r18);
  78. printk("a3 = %016lx a4 = %016lx a5 = %016lx\n",
  79. regs->r19, regs->r20, regs->r21);
  80. printk("t8 = %016lx t9 = %016lx t10= %016lx\n",
  81. regs->r22, regs->r23, regs->r24);
  82. printk("t11= %016lx pv = %016lx at = %016lx\n",
  83. regs->r25, regs->r27, regs->r28);
  84. printk("gp = %016lx sp = %p\n", regs->gp, regs+1);
  85. #if 0
  86. __halt();
  87. #endif
  88. }
  89. #if 0
  90. static char * ireg_name[] = {"v0", "t0", "t1", "t2", "t3", "t4", "t5", "t6",
  91. "t7", "s0", "s1", "s2", "s3", "s4", "s5", "s6",
  92. "a0", "a1", "a2", "a3", "a4", "a5", "t8", "t9",
  93. "t10", "t11", "ra", "pv", "at", "gp", "sp", "zero"};
  94. #endif
  95. static void
  96. dik_show_code(unsigned int *pc)
  97. {
  98. long i;
  99. printk("Code:");
  100. for (i = -6; i < 2; i++) {
  101. unsigned int insn;
  102. if (__get_user(insn, (unsigned int __user *)pc + i))
  103. break;
  104. printk("%c%08x%c", i ? ' ' : '<', insn, i ? ' ' : '>');
  105. }
  106. printk("\n");
  107. }
  108. static void
  109. dik_show_trace(unsigned long *sp, const char *loglvl)
  110. {
  111. long i = 0;
  112. printk("%sTrace:\n", loglvl);
  113. while (0x1ff8 & (unsigned long) sp) {
  114. extern char _stext[], _etext[];
  115. unsigned long tmp = *sp;
  116. sp++;
  117. if (!is_kernel_text(tmp))
  118. continue;
  119. printk("%s[<%lx>] %pSR\n", loglvl, tmp, (void *)tmp);
  120. if (i > 40) {
  121. printk("%s ...", loglvl);
  122. break;
  123. }
  124. }
  125. printk("%s\n", loglvl);
  126. }
  127. static int kstack_depth_to_print = 24;
  128. void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
  129. {
  130. unsigned long *stack;
  131. int i;
  132. /*
  133. * debugging aid: "show_stack(NULL, NULL, KERN_EMERG);" prints the
  134. * back trace for this cpu.
  135. */
  136. if(sp==NULL)
  137. sp=(unsigned long*)&sp;
  138. stack = sp;
  139. for(i=0; i < kstack_depth_to_print; i++) {
  140. if (((long) stack & (THREAD_SIZE-1)) == 0)
  141. break;
  142. if ((i % 4) == 0) {
  143. if (i)
  144. pr_cont("\n");
  145. printk("%s ", loglvl);
  146. } else {
  147. pr_cont(" ");
  148. }
  149. pr_cont("%016lx", *stack++);
  150. }
  151. pr_cont("\n");
  152. dik_show_trace(sp, loglvl);
  153. }
  154. void
  155. die_if_kernel(char * str, struct pt_regs *regs, long err, unsigned long *r9_15)
  156. {
  157. if (regs->ps & 8)
  158. return;
  159. #ifdef CONFIG_SMP
  160. printk("CPU %d ", hard_smp_processor_id());
  161. #endif
  162. printk("%s(%d): %s %ld\n", current->comm, task_pid_nr(current), str, err);
  163. dik_show_regs(regs, r9_15);
  164. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  165. dik_show_trace((unsigned long *)(regs+1), KERN_DEFAULT);
  166. dik_show_code((unsigned int *)regs->pc);
  167. if (test_and_set_thread_flag (TIF_DIE_IF_KERNEL)) {
  168. printk("die_if_kernel recursion detected.\n");
  169. local_irq_enable();
  170. while (1);
  171. }
  172. make_task_dead(SIGSEGV);
  173. }
  174. #ifndef CONFIG_MATHEMU
  175. static long dummy_emul(void) { return 0; }
  176. long (*alpha_fp_emul_imprecise)(struct pt_regs *regs, unsigned long writemask)
  177. = (void *)dummy_emul;
  178. EXPORT_SYMBOL_GPL(alpha_fp_emul_imprecise);
  179. long (*alpha_fp_emul) (unsigned long pc)
  180. = (void *)dummy_emul;
  181. EXPORT_SYMBOL_GPL(alpha_fp_emul);
  182. #else
  183. long alpha_fp_emul_imprecise(struct pt_regs *regs, unsigned long writemask);
  184. long alpha_fp_emul (unsigned long pc);
  185. #endif
  186. asmlinkage void
  187. do_entArith(unsigned long summary, unsigned long write_mask,
  188. struct pt_regs *regs)
  189. {
  190. long si_code = FPE_FLTINV;
  191. if (summary & 1) {
  192. /* Software-completion summary bit is set, so try to
  193. emulate the instruction. If the processor supports
  194. precise exceptions, we don't have to search. */
  195. if (!amask(AMASK_PRECISE_TRAP))
  196. si_code = alpha_fp_emul(regs->pc - 4);
  197. else
  198. si_code = alpha_fp_emul_imprecise(regs, write_mask);
  199. if (si_code == 0)
  200. return;
  201. }
  202. die_if_kernel("Arithmetic fault", regs, 0, NULL);
  203. send_sig_fault_trapno(SIGFPE, si_code, (void __user *) regs->pc, 0, current);
  204. }
  205. asmlinkage void
  206. do_entIF(unsigned long type, struct pt_regs *regs)
  207. {
  208. int signo, code;
  209. if (type == 3) { /* FEN fault */
  210. /* Irritating users can call PAL_clrfen to disable the
  211. FPU for the process. The kernel will then trap in
  212. do_switch_stack and undo_switch_stack when we try
  213. to save and restore the FP registers.
  214. Given that GCC by default generates code that uses the
  215. FP registers, PAL_clrfen is not useful except for DoS
  216. attacks. So turn the bleeding FPU back on and be done
  217. with it. */
  218. current_thread_info()->pcb.flags |= 1;
  219. __reload_thread(&current_thread_info()->pcb);
  220. return;
  221. }
  222. if (!user_mode(regs)) {
  223. if (type == 1) {
  224. const unsigned int *data
  225. = (const unsigned int *) regs->pc;
  226. printk("Kernel bug at %s:%d\n",
  227. (const char *)(data[1] | (long)data[2] << 32),
  228. data[0]);
  229. }
  230. #ifdef CONFIG_ALPHA_WTINT
  231. if (type == 4) {
  232. /* If CALL_PAL WTINT is totally unsupported by the
  233. PALcode, e.g. MILO, "emulate" it by overwriting
  234. the insn. */
  235. unsigned int *pinsn
  236. = (unsigned int *) regs->pc - 1;
  237. if (*pinsn == PAL_wtint) {
  238. *pinsn = 0x47e01400; /* mov 0,$0 */
  239. imb();
  240. regs->r0 = 0;
  241. return;
  242. }
  243. }
  244. #endif /* ALPHA_WTINT */
  245. die_if_kernel((type == 1 ? "Kernel Bug" : "Instruction fault"),
  246. regs, type, NULL);
  247. }
  248. switch (type) {
  249. case 0: /* breakpoint */
  250. if (ptrace_cancel_bpt(current)) {
  251. regs->pc -= 4; /* make pc point to former bpt */
  252. }
  253. send_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->pc,
  254. current);
  255. return;
  256. case 1: /* bugcheck */
  257. send_sig_fault_trapno(SIGTRAP, TRAP_UNK,
  258. (void __user *) regs->pc, 0, current);
  259. return;
  260. case 2: /* gentrap */
  261. switch ((long) regs->r16) {
  262. case GEN_INTOVF:
  263. signo = SIGFPE;
  264. code = FPE_INTOVF;
  265. break;
  266. case GEN_INTDIV:
  267. signo = SIGFPE;
  268. code = FPE_INTDIV;
  269. break;
  270. case GEN_FLTOVF:
  271. signo = SIGFPE;
  272. code = FPE_FLTOVF;
  273. break;
  274. case GEN_FLTDIV:
  275. signo = SIGFPE;
  276. code = FPE_FLTDIV;
  277. break;
  278. case GEN_FLTUND:
  279. signo = SIGFPE;
  280. code = FPE_FLTUND;
  281. break;
  282. case GEN_FLTINV:
  283. signo = SIGFPE;
  284. code = FPE_FLTINV;
  285. break;
  286. case GEN_FLTINE:
  287. signo = SIGFPE;
  288. code = FPE_FLTRES;
  289. break;
  290. case GEN_ROPRAND:
  291. signo = SIGFPE;
  292. code = FPE_FLTUNK;
  293. break;
  294. case GEN_DECOVF:
  295. case GEN_DECDIV:
  296. case GEN_DECINV:
  297. case GEN_ASSERTERR:
  298. case GEN_NULPTRERR:
  299. case GEN_STKOVF:
  300. case GEN_STRLENERR:
  301. case GEN_SUBSTRERR:
  302. case GEN_RANGERR:
  303. case GEN_SUBRNG:
  304. case GEN_SUBRNG1:
  305. case GEN_SUBRNG2:
  306. case GEN_SUBRNG3:
  307. case GEN_SUBRNG4:
  308. case GEN_SUBRNG5:
  309. case GEN_SUBRNG6:
  310. case GEN_SUBRNG7:
  311. default:
  312. signo = SIGTRAP;
  313. code = TRAP_UNK;
  314. break;
  315. }
  316. send_sig_fault_trapno(signo, code, (void __user *) regs->pc,
  317. regs->r16, current);
  318. return;
  319. case 4: /* opDEC */
  320. if (implver() == IMPLVER_EV4) {
  321. long si_code;
  322. /* The some versions of SRM do not handle
  323. the opDEC properly - they return the PC of the
  324. opDEC fault, not the instruction after as the
  325. Alpha architecture requires. Here we fix it up.
  326. We do this by intentionally causing an opDEC
  327. fault during the boot sequence and testing if
  328. we get the correct PC. If not, we set a flag
  329. to correct it every time through. */
  330. regs->pc += opDEC_fix;
  331. /* EV4 does not implement anything except normal
  332. rounding. Everything else will come here as
  333. an illegal instruction. Emulate them. */
  334. si_code = alpha_fp_emul(regs->pc - 4);
  335. if (si_code == 0)
  336. return;
  337. if (si_code > 0) {
  338. send_sig_fault_trapno(SIGFPE, si_code,
  339. (void __user *) regs->pc,
  340. 0, current);
  341. return;
  342. }
  343. }
  344. break;
  345. case 5: /* illoc */
  346. default: /* unexpected instruction-fault type */
  347. ;
  348. }
  349. send_sig_fault(SIGILL, ILL_ILLOPC, (void __user *)regs->pc, current);
  350. }
  351. /* There is an ifdef in the PALcode in MILO that enables a
  352. "kernel debugging entry point" as an unprivileged call_pal.
  353. We don't want to have anything to do with it, but unfortunately
  354. several versions of MILO included in distributions have it enabled,
  355. and if we don't put something on the entry point we'll oops. */
  356. asmlinkage void
  357. do_entDbg(struct pt_regs *regs)
  358. {
  359. die_if_kernel("Instruction fault", regs, 0, NULL);
  360. force_sig_fault(SIGILL, ILL_ILLOPC, (void __user *)regs->pc);
  361. }
  362. /*
  363. * entUna has a different register layout to be reasonably simple. It
  364. * needs access to all the integer registers (the kernel doesn't use
  365. * fp-regs), and it needs to have them in order for simpler access.
  366. *
  367. * Due to the non-standard register layout (and because we don't want
  368. * to handle floating-point regs), user-mode unaligned accesses are
  369. * handled separately by do_entUnaUser below.
  370. *
  371. * Oh, btw, we don't handle the "gp" register correctly, but if we fault
  372. * on a gp-register unaligned load/store, something is _very_ wrong
  373. * in the kernel anyway..
  374. */
  375. struct allregs {
  376. unsigned long regs[32];
  377. unsigned long ps, pc, gp, a0, a1, a2;
  378. };
  379. struct unaligned_stat {
  380. unsigned long count, va, pc;
  381. } unaligned[2];
  382. /* Macro for exception fixup code to access integer registers. */
  383. #define una_reg(r) (_regs[(r) >= 16 && (r) <= 18 ? (r)+19 : (r)])
  384. asmlinkage void
  385. do_entUna(void * va, unsigned long opcode, unsigned long reg,
  386. struct allregs *regs)
  387. {
  388. long error, tmp1, tmp2, tmp3, tmp4;
  389. unsigned long pc = regs->pc - 4;
  390. unsigned long *_regs = regs->regs;
  391. const struct exception_table_entry *fixup;
  392. unaligned[0].count++;
  393. unaligned[0].va = (unsigned long) va;
  394. unaligned[0].pc = pc;
  395. /* We don't want to use the generic get/put unaligned macros as
  396. we want to trap exceptions. Only if we actually get an
  397. exception will we decide whether we should have caught it. */
  398. switch (opcode) {
  399. case 0x0c: /* ldwu */
  400. __asm__ __volatile__(
  401. "1: ldq_u %1,0(%3)\n"
  402. "2: ldq_u %2,1(%3)\n"
  403. " extwl %1,%3,%1\n"
  404. " extwh %2,%3,%2\n"
  405. "3:\n"
  406. EXC(1b,3b,%1,%0)
  407. EXC(2b,3b,%2,%0)
  408. : "=r"(error), "=&r"(tmp1), "=&r"(tmp2)
  409. : "r"(va), "0"(0));
  410. if (error)
  411. goto got_exception;
  412. una_reg(reg) = tmp1|tmp2;
  413. return;
  414. case 0x28: /* ldl */
  415. __asm__ __volatile__(
  416. "1: ldq_u %1,0(%3)\n"
  417. "2: ldq_u %2,3(%3)\n"
  418. " extll %1,%3,%1\n"
  419. " extlh %2,%3,%2\n"
  420. "3:\n"
  421. EXC(1b,3b,%1,%0)
  422. EXC(2b,3b,%2,%0)
  423. : "=r"(error), "=&r"(tmp1), "=&r"(tmp2)
  424. : "r"(va), "0"(0));
  425. if (error)
  426. goto got_exception;
  427. una_reg(reg) = (int)(tmp1|tmp2);
  428. return;
  429. case 0x29: /* ldq */
  430. __asm__ __volatile__(
  431. "1: ldq_u %1,0(%3)\n"
  432. "2: ldq_u %2,7(%3)\n"
  433. " extql %1,%3,%1\n"
  434. " extqh %2,%3,%2\n"
  435. "3:\n"
  436. EXC(1b,3b,%1,%0)
  437. EXC(2b,3b,%2,%0)
  438. : "=r"(error), "=&r"(tmp1), "=&r"(tmp2)
  439. : "r"(va), "0"(0));
  440. if (error)
  441. goto got_exception;
  442. una_reg(reg) = tmp1|tmp2;
  443. return;
  444. /* Note that the store sequences do not indicate that they change
  445. memory because it _should_ be affecting nothing in this context.
  446. (Otherwise we have other, much larger, problems.) */
  447. case 0x0d: /* stw */
  448. __asm__ __volatile__(
  449. "1: ldq_u %2,1(%5)\n"
  450. "2: ldq_u %1,0(%5)\n"
  451. " inswh %6,%5,%4\n"
  452. " inswl %6,%5,%3\n"
  453. " mskwh %2,%5,%2\n"
  454. " mskwl %1,%5,%1\n"
  455. " or %2,%4,%2\n"
  456. " or %1,%3,%1\n"
  457. "3: stq_u %2,1(%5)\n"
  458. "4: stq_u %1,0(%5)\n"
  459. "5:\n"
  460. EXC(1b,5b,%2,%0)
  461. EXC(2b,5b,%1,%0)
  462. EXC(3b,5b,$31,%0)
  463. EXC(4b,5b,$31,%0)
  464. : "=r"(error), "=&r"(tmp1), "=&r"(tmp2),
  465. "=&r"(tmp3), "=&r"(tmp4)
  466. : "r"(va), "r"(una_reg(reg)), "0"(0));
  467. if (error)
  468. goto got_exception;
  469. return;
  470. case 0x2c: /* stl */
  471. __asm__ __volatile__(
  472. "1: ldq_u %2,3(%5)\n"
  473. "2: ldq_u %1,0(%5)\n"
  474. " inslh %6,%5,%4\n"
  475. " insll %6,%5,%3\n"
  476. " msklh %2,%5,%2\n"
  477. " mskll %1,%5,%1\n"
  478. " or %2,%4,%2\n"
  479. " or %1,%3,%1\n"
  480. "3: stq_u %2,3(%5)\n"
  481. "4: stq_u %1,0(%5)\n"
  482. "5:\n"
  483. EXC(1b,5b,%2,%0)
  484. EXC(2b,5b,%1,%0)
  485. EXC(3b,5b,$31,%0)
  486. EXC(4b,5b,$31,%0)
  487. : "=r"(error), "=&r"(tmp1), "=&r"(tmp2),
  488. "=&r"(tmp3), "=&r"(tmp4)
  489. : "r"(va), "r"(una_reg(reg)), "0"(0));
  490. if (error)
  491. goto got_exception;
  492. return;
  493. case 0x2d: /* stq */
  494. __asm__ __volatile__(
  495. "1: ldq_u %2,7(%5)\n"
  496. "2: ldq_u %1,0(%5)\n"
  497. " insqh %6,%5,%4\n"
  498. " insql %6,%5,%3\n"
  499. " mskqh %2,%5,%2\n"
  500. " mskql %1,%5,%1\n"
  501. " or %2,%4,%2\n"
  502. " or %1,%3,%1\n"
  503. "3: stq_u %2,7(%5)\n"
  504. "4: stq_u %1,0(%5)\n"
  505. "5:\n"
  506. EXC(1b,5b,%2,%0)
  507. EXC(2b,5b,%1,%0)
  508. EXC(3b,5b,$31,%0)
  509. EXC(4b,5b,$31,%0)
  510. : "=r"(error), "=&r"(tmp1), "=&r"(tmp2),
  511. "=&r"(tmp3), "=&r"(tmp4)
  512. : "r"(va), "r"(una_reg(reg)), "0"(0));
  513. if (error)
  514. goto got_exception;
  515. return;
  516. }
  517. printk("Bad unaligned kernel access at %016lx: %p %lx %lu\n",
  518. pc, va, opcode, reg);
  519. make_task_dead(SIGSEGV);
  520. got_exception:
  521. /* Ok, we caught the exception, but we don't want it. Is there
  522. someone to pass it along to? */
  523. if ((fixup = search_exception_tables(pc)) != 0) {
  524. unsigned long newpc;
  525. newpc = fixup_exception(una_reg, fixup, pc);
  526. printk("Forwarding unaligned exception at %lx (%lx)\n",
  527. pc, newpc);
  528. regs->pc = newpc;
  529. return;
  530. }
  531. /*
  532. * Yikes! No one to forward the exception to.
  533. * Since the registers are in a weird format, dump them ourselves.
  534. */
  535. printk("%s(%d): unhandled unaligned exception\n",
  536. current->comm, task_pid_nr(current));
  537. printk("pc = [<%016lx>] ra = [<%016lx>] ps = %04lx\n",
  538. pc, una_reg(26), regs->ps);
  539. printk("r0 = %016lx r1 = %016lx r2 = %016lx\n",
  540. una_reg(0), una_reg(1), una_reg(2));
  541. printk("r3 = %016lx r4 = %016lx r5 = %016lx\n",
  542. una_reg(3), una_reg(4), una_reg(5));
  543. printk("r6 = %016lx r7 = %016lx r8 = %016lx\n",
  544. una_reg(6), una_reg(7), una_reg(8));
  545. printk("r9 = %016lx r10= %016lx r11= %016lx\n",
  546. una_reg(9), una_reg(10), una_reg(11));
  547. printk("r12= %016lx r13= %016lx r14= %016lx\n",
  548. una_reg(12), una_reg(13), una_reg(14));
  549. printk("r15= %016lx\n", una_reg(15));
  550. printk("r16= %016lx r17= %016lx r18= %016lx\n",
  551. una_reg(16), una_reg(17), una_reg(18));
  552. printk("r19= %016lx r20= %016lx r21= %016lx\n",
  553. una_reg(19), una_reg(20), una_reg(21));
  554. printk("r22= %016lx r23= %016lx r24= %016lx\n",
  555. una_reg(22), una_reg(23), una_reg(24));
  556. printk("r25= %016lx r27= %016lx r28= %016lx\n",
  557. una_reg(25), una_reg(27), una_reg(28));
  558. printk("gp = %016lx sp = %p\n", regs->gp, regs+1);
  559. dik_show_code((unsigned int *)pc);
  560. dik_show_trace((unsigned long *)(regs+1), KERN_DEFAULT);
  561. if (test_and_set_thread_flag (TIF_DIE_IF_KERNEL)) {
  562. printk("die_if_kernel recursion detected.\n");
  563. local_irq_enable();
  564. while (1);
  565. }
  566. make_task_dead(SIGSEGV);
  567. }
  568. /*
  569. * Convert an s-floating point value in memory format to the
  570. * corresponding value in register format. The exponent
  571. * needs to be remapped to preserve non-finite values
  572. * (infinities, not-a-numbers, denormals).
  573. */
  574. static inline unsigned long
  575. s_mem_to_reg (unsigned long s_mem)
  576. {
  577. unsigned long frac = (s_mem >> 0) & 0x7fffff;
  578. unsigned long sign = (s_mem >> 31) & 0x1;
  579. unsigned long exp_msb = (s_mem >> 30) & 0x1;
  580. unsigned long exp_low = (s_mem >> 23) & 0x7f;
  581. unsigned long exp;
  582. exp = (exp_msb << 10) | exp_low; /* common case */
  583. if (exp_msb) {
  584. if (exp_low == 0x7f) {
  585. exp = 0x7ff;
  586. }
  587. } else {
  588. if (exp_low == 0x00) {
  589. exp = 0x000;
  590. } else {
  591. exp |= (0x7 << 7);
  592. }
  593. }
  594. return (sign << 63) | (exp << 52) | (frac << 29);
  595. }
  596. /*
  597. * Convert an s-floating point value in register format to the
  598. * corresponding value in memory format.
  599. */
  600. static inline unsigned long
  601. s_reg_to_mem (unsigned long s_reg)
  602. {
  603. return ((s_reg >> 62) << 30) | ((s_reg << 5) >> 34);
  604. }
  605. /*
  606. * Handle user-level unaligned fault. Handling user-level unaligned
  607. * faults is *extremely* slow and produces nasty messages. A user
  608. * program *should* fix unaligned faults ASAP.
  609. *
  610. * Notice that we have (almost) the regular kernel stack layout here,
  611. * so finding the appropriate registers is a little more difficult
  612. * than in the kernel case.
  613. *
  614. * Finally, we handle regular integer load/stores only. In
  615. * particular, load-linked/store-conditionally and floating point
  616. * load/stores are not supported. The former make no sense with
  617. * unaligned faults (they are guaranteed to fail) and I don't think
  618. * the latter will occur in any decent program.
  619. *
  620. * Sigh. We *do* have to handle some FP operations, because GCC will
  621. * uses them as temporary storage for integer memory to memory copies.
  622. * However, we need to deal with stt/ldt and sts/lds only.
  623. */
  624. #define OP_INT_MASK ( 1L << 0x28 | 1L << 0x2c /* ldl stl */ \
  625. | 1L << 0x29 | 1L << 0x2d /* ldq stq */ \
  626. | 1L << 0x0c | 1L << 0x0d /* ldwu stw */ \
  627. | 1L << 0x0a | 1L << 0x0e ) /* ldbu stb */
  628. #define OP_WRITE_MASK ( 1L << 0x26 | 1L << 0x27 /* sts stt */ \
  629. | 1L << 0x2c | 1L << 0x2d /* stl stq */ \
  630. | 1L << 0x0d | 1L << 0x0e ) /* stw stb */
  631. #define R(x) ((size_t) &((struct pt_regs *)0)->x)
  632. static int unauser_reg_offsets[32] = {
  633. R(r0), R(r1), R(r2), R(r3), R(r4), R(r5), R(r6), R(r7), R(r8),
  634. /* r9 ... r15 are stored in front of regs. */
  635. -56, -48, -40, -32, -24, -16, -8,
  636. R(r16), R(r17), R(r18),
  637. R(r19), R(r20), R(r21), R(r22), R(r23), R(r24), R(r25), R(r26),
  638. R(r27), R(r28), R(gp),
  639. 0, 0
  640. };
  641. #undef R
  642. asmlinkage void
  643. do_entUnaUser(void __user * va, unsigned long opcode,
  644. unsigned long reg, struct pt_regs *regs)
  645. {
  646. static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 5);
  647. unsigned long tmp1, tmp2, tmp3, tmp4;
  648. unsigned long fake_reg, *reg_addr = &fake_reg;
  649. int si_code;
  650. long error;
  651. /* Check the UAC bits to decide what the user wants us to do
  652. with the unaligned access. */
  653. if (!(current_thread_info()->status & TS_UAC_NOPRINT)) {
  654. if (__ratelimit(&ratelimit)) {
  655. printk("%s(%d): unaligned trap at %016lx: %p %lx %ld\n",
  656. current->comm, task_pid_nr(current),
  657. regs->pc - 4, va, opcode, reg);
  658. }
  659. }
  660. if ((current_thread_info()->status & TS_UAC_SIGBUS))
  661. goto give_sigbus;
  662. /* Not sure why you'd want to use this, but... */
  663. if ((current_thread_info()->status & TS_UAC_NOFIX))
  664. return;
  665. /* Don't bother reading ds in the access check since we already
  666. know that this came from the user. Also rely on the fact that
  667. the page at TASK_SIZE is unmapped and so can't be touched anyway. */
  668. if ((unsigned long)va >= TASK_SIZE)
  669. goto give_sigsegv;
  670. ++unaligned[1].count;
  671. unaligned[1].va = (unsigned long)va;
  672. unaligned[1].pc = regs->pc - 4;
  673. if ((1L << opcode) & OP_INT_MASK) {
  674. /* it's an integer load/store */
  675. if (reg < 30) {
  676. reg_addr = (unsigned long *)
  677. ((char *)regs + unauser_reg_offsets[reg]);
  678. } else if (reg == 30) {
  679. /* usp in PAL regs */
  680. fake_reg = rdusp();
  681. } else {
  682. /* zero "register" */
  683. fake_reg = 0;
  684. }
  685. }
  686. /* We don't want to use the generic get/put unaligned macros as
  687. we want to trap exceptions. Only if we actually get an
  688. exception will we decide whether we should have caught it. */
  689. switch (opcode) {
  690. case 0x0c: /* ldwu */
  691. __asm__ __volatile__(
  692. "1: ldq_u %1,0(%3)\n"
  693. "2: ldq_u %2,1(%3)\n"
  694. " extwl %1,%3,%1\n"
  695. " extwh %2,%3,%2\n"
  696. "3:\n"
  697. EXC(1b,3b,%1,%0)
  698. EXC(2b,3b,%2,%0)
  699. : "=r"(error), "=&r"(tmp1), "=&r"(tmp2)
  700. : "r"(va), "0"(0));
  701. if (error)
  702. goto give_sigsegv;
  703. *reg_addr = tmp1|tmp2;
  704. break;
  705. case 0x22: /* lds */
  706. __asm__ __volatile__(
  707. "1: ldq_u %1,0(%3)\n"
  708. "2: ldq_u %2,3(%3)\n"
  709. " extll %1,%3,%1\n"
  710. " extlh %2,%3,%2\n"
  711. "3:\n"
  712. EXC(1b,3b,%1,%0)
  713. EXC(2b,3b,%2,%0)
  714. : "=r"(error), "=&r"(tmp1), "=&r"(tmp2)
  715. : "r"(va), "0"(0));
  716. if (error)
  717. goto give_sigsegv;
  718. alpha_write_fp_reg(reg, s_mem_to_reg((int)(tmp1|tmp2)));
  719. return;
  720. case 0x23: /* ldt */
  721. __asm__ __volatile__(
  722. "1: ldq_u %1,0(%3)\n"
  723. "2: ldq_u %2,7(%3)\n"
  724. " extql %1,%3,%1\n"
  725. " extqh %2,%3,%2\n"
  726. "3:\n"
  727. EXC(1b,3b,%1,%0)
  728. EXC(2b,3b,%2,%0)
  729. : "=r"(error), "=&r"(tmp1), "=&r"(tmp2)
  730. : "r"(va), "0"(0));
  731. if (error)
  732. goto give_sigsegv;
  733. alpha_write_fp_reg(reg, tmp1|tmp2);
  734. return;
  735. case 0x28: /* ldl */
  736. __asm__ __volatile__(
  737. "1: ldq_u %1,0(%3)\n"
  738. "2: ldq_u %2,3(%3)\n"
  739. " extll %1,%3,%1\n"
  740. " extlh %2,%3,%2\n"
  741. "3:\n"
  742. EXC(1b,3b,%1,%0)
  743. EXC(2b,3b,%2,%0)
  744. : "=r"(error), "=&r"(tmp1), "=&r"(tmp2)
  745. : "r"(va), "0"(0));
  746. if (error)
  747. goto give_sigsegv;
  748. *reg_addr = (int)(tmp1|tmp2);
  749. break;
  750. case 0x29: /* ldq */
  751. __asm__ __volatile__(
  752. "1: ldq_u %1,0(%3)\n"
  753. "2: ldq_u %2,7(%3)\n"
  754. " extql %1,%3,%1\n"
  755. " extqh %2,%3,%2\n"
  756. "3:\n"
  757. EXC(1b,3b,%1,%0)
  758. EXC(2b,3b,%2,%0)
  759. : "=r"(error), "=&r"(tmp1), "=&r"(tmp2)
  760. : "r"(va), "0"(0));
  761. if (error)
  762. goto give_sigsegv;
  763. *reg_addr = tmp1|tmp2;
  764. break;
  765. /* Note that the store sequences do not indicate that they change
  766. memory because it _should_ be affecting nothing in this context.
  767. (Otherwise we have other, much larger, problems.) */
  768. case 0x0d: /* stw */
  769. __asm__ __volatile__(
  770. "1: ldq_u %2,1(%5)\n"
  771. "2: ldq_u %1,0(%5)\n"
  772. " inswh %6,%5,%4\n"
  773. " inswl %6,%5,%3\n"
  774. " mskwh %2,%5,%2\n"
  775. " mskwl %1,%5,%1\n"
  776. " or %2,%4,%2\n"
  777. " or %1,%3,%1\n"
  778. "3: stq_u %2,1(%5)\n"
  779. "4: stq_u %1,0(%5)\n"
  780. "5:\n"
  781. EXC(1b,5b,%2,%0)
  782. EXC(2b,5b,%1,%0)
  783. EXC(3b,5b,$31,%0)
  784. EXC(4b,5b,$31,%0)
  785. : "=r"(error), "=&r"(tmp1), "=&r"(tmp2),
  786. "=&r"(tmp3), "=&r"(tmp4)
  787. : "r"(va), "r"(*reg_addr), "0"(0));
  788. if (error)
  789. goto give_sigsegv;
  790. return;
  791. case 0x26: /* sts */
  792. fake_reg = s_reg_to_mem(alpha_read_fp_reg(reg));
  793. fallthrough;
  794. case 0x2c: /* stl */
  795. __asm__ __volatile__(
  796. "1: ldq_u %2,3(%5)\n"
  797. "2: ldq_u %1,0(%5)\n"
  798. " inslh %6,%5,%4\n"
  799. " insll %6,%5,%3\n"
  800. " msklh %2,%5,%2\n"
  801. " mskll %1,%5,%1\n"
  802. " or %2,%4,%2\n"
  803. " or %1,%3,%1\n"
  804. "3: stq_u %2,3(%5)\n"
  805. "4: stq_u %1,0(%5)\n"
  806. "5:\n"
  807. EXC(1b,5b,%2,%0)
  808. EXC(2b,5b,%1,%0)
  809. EXC(3b,5b,$31,%0)
  810. EXC(4b,5b,$31,%0)
  811. : "=r"(error), "=&r"(tmp1), "=&r"(tmp2),
  812. "=&r"(tmp3), "=&r"(tmp4)
  813. : "r"(va), "r"(*reg_addr), "0"(0));
  814. if (error)
  815. goto give_sigsegv;
  816. return;
  817. case 0x27: /* stt */
  818. fake_reg = alpha_read_fp_reg(reg);
  819. fallthrough;
  820. case 0x2d: /* stq */
  821. __asm__ __volatile__(
  822. "1: ldq_u %2,7(%5)\n"
  823. "2: ldq_u %1,0(%5)\n"
  824. " insqh %6,%5,%4\n"
  825. " insql %6,%5,%3\n"
  826. " mskqh %2,%5,%2\n"
  827. " mskql %1,%5,%1\n"
  828. " or %2,%4,%2\n"
  829. " or %1,%3,%1\n"
  830. "3: stq_u %2,7(%5)\n"
  831. "4: stq_u %1,0(%5)\n"
  832. "5:\n"
  833. EXC(1b,5b,%2,%0)
  834. EXC(2b,5b,%1,%0)
  835. EXC(3b,5b,$31,%0)
  836. EXC(4b,5b,$31,%0)
  837. : "=r"(error), "=&r"(tmp1), "=&r"(tmp2),
  838. "=&r"(tmp3), "=&r"(tmp4)
  839. : "r"(va), "r"(*reg_addr), "0"(0));
  840. if (error)
  841. goto give_sigsegv;
  842. return;
  843. default:
  844. /* What instruction were you trying to use, exactly? */
  845. goto give_sigbus;
  846. }
  847. /* Only integer loads should get here; everyone else returns early. */
  848. if (reg == 30)
  849. wrusp(fake_reg);
  850. return;
  851. give_sigsegv:
  852. regs->pc -= 4; /* make pc point to faulting insn */
  853. /* We need to replicate some of the logic in mm/fault.c,
  854. since we don't have access to the fault code in the
  855. exception handling return path. */
  856. if ((unsigned long)va >= TASK_SIZE)
  857. si_code = SEGV_ACCERR;
  858. else {
  859. struct mm_struct *mm = current->mm;
  860. mmap_read_lock(mm);
  861. if (find_vma(mm, (unsigned long)va))
  862. si_code = SEGV_ACCERR;
  863. else
  864. si_code = SEGV_MAPERR;
  865. mmap_read_unlock(mm);
  866. }
  867. send_sig_fault(SIGSEGV, si_code, va, current);
  868. return;
  869. give_sigbus:
  870. regs->pc -= 4;
  871. send_sig_fault(SIGBUS, BUS_ADRALN, va, current);
  872. return;
  873. }
  874. void
  875. trap_init(void)
  876. {
  877. /* Tell PAL-code what global pointer we want in the kernel. */
  878. register unsigned long gptr __asm__("$29");
  879. wrkgp(gptr);
  880. /* Hack for Multia (UDB) and JENSEN: some of their SRMs have
  881. a bug in the handling of the opDEC fault. Fix it up if so. */
  882. if (implver() == IMPLVER_EV4)
  883. opDEC_check();
  884. wrent(entArith, 1);
  885. wrent(entMM, 2);
  886. wrent(entIF, 3);
  887. wrent(entUna, 4);
  888. wrent(entSys, 5);
  889. wrent(entDbg, 6);
  890. }