traps.c 27 KB

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