traps.c 27 KB

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