traps.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Architecture-specific trap handling.
  4. *
  5. * Copyright (C) 1998-2003 Hewlett-Packard Co
  6. * David Mosberger-Tang <[email protected]>
  7. *
  8. * 05/12/00 grao <[email protected]> : added isr in siginfo for SIGFPE
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/sched/signal.h>
  13. #include <linux/sched/debug.h>
  14. #include <linux/tty.h>
  15. #include <linux/vt_kern.h> /* For unblank_screen() */
  16. #include <linux/export.h>
  17. #include <linux/extable.h>
  18. #include <linux/hardirq.h>
  19. #include <linux/kprobes.h>
  20. #include <linux/delay.h> /* for ssleep() */
  21. #include <linux/kdebug.h>
  22. #include <linux/uaccess.h>
  23. #include <asm/fpswa.h>
  24. #include <asm/intrinsics.h>
  25. #include <asm/processor.h>
  26. #include <asm/exception.h>
  27. #include <asm/setup.h>
  28. fpswa_interface_t *fpswa_interface;
  29. EXPORT_SYMBOL(fpswa_interface);
  30. void __init
  31. trap_init (void)
  32. {
  33. if (ia64_boot_param->fpswa)
  34. /* FPSWA fixup: make the interface pointer a kernel virtual address: */
  35. fpswa_interface = __va(ia64_boot_param->fpswa);
  36. }
  37. int
  38. die (const char *str, struct pt_regs *regs, long err)
  39. {
  40. static struct {
  41. spinlock_t lock;
  42. u32 lock_owner;
  43. int lock_owner_depth;
  44. } die = {
  45. .lock = __SPIN_LOCK_UNLOCKED(die.lock),
  46. .lock_owner = -1,
  47. .lock_owner_depth = 0
  48. };
  49. static int die_counter;
  50. int cpu = get_cpu();
  51. if (die.lock_owner != cpu) {
  52. console_verbose();
  53. spin_lock_irq(&die.lock);
  54. die.lock_owner = cpu;
  55. die.lock_owner_depth = 0;
  56. bust_spinlocks(1);
  57. }
  58. put_cpu();
  59. if (++die.lock_owner_depth < 3) {
  60. printk("%s[%d]: %s %ld [%d]\n",
  61. current->comm, task_pid_nr(current), str, err, ++die_counter);
  62. if (notify_die(DIE_OOPS, str, regs, err, 255, SIGSEGV)
  63. != NOTIFY_STOP)
  64. show_regs(regs);
  65. else
  66. regs = NULL;
  67. } else
  68. printk(KERN_ERR "Recursive die() failure, output suppressed\n");
  69. bust_spinlocks(0);
  70. die.lock_owner = -1;
  71. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  72. spin_unlock_irq(&die.lock);
  73. if (!regs)
  74. return 1;
  75. if (panic_on_oops)
  76. panic("Fatal exception");
  77. make_task_dead(SIGSEGV);
  78. return 0;
  79. }
  80. int
  81. die_if_kernel (char *str, struct pt_regs *regs, long err)
  82. {
  83. if (!user_mode(regs))
  84. return die(str, regs, err);
  85. return 0;
  86. }
  87. void
  88. __kprobes ia64_bad_break (unsigned long break_num, struct pt_regs *regs)
  89. {
  90. int sig, code;
  91. switch (break_num) {
  92. case 0: /* unknown error (used by GCC for __builtin_abort()) */
  93. if (notify_die(DIE_BREAK, "break 0", regs, break_num, TRAP_BRKPT, SIGTRAP)
  94. == NOTIFY_STOP)
  95. return;
  96. if (die_if_kernel("bugcheck!", regs, break_num))
  97. return;
  98. sig = SIGILL; code = ILL_ILLOPC;
  99. break;
  100. case 1: /* integer divide by zero */
  101. sig = SIGFPE; code = FPE_INTDIV;
  102. break;
  103. case 2: /* integer overflow */
  104. sig = SIGFPE; code = FPE_INTOVF;
  105. break;
  106. case 3: /* range check/bounds check */
  107. sig = SIGFPE; code = FPE_FLTSUB;
  108. break;
  109. case 4: /* null pointer dereference */
  110. sig = SIGSEGV; code = SEGV_MAPERR;
  111. break;
  112. case 5: /* misaligned data */
  113. sig = SIGSEGV; code = BUS_ADRALN;
  114. break;
  115. case 6: /* decimal overflow */
  116. sig = SIGFPE; code = __FPE_DECOVF;
  117. break;
  118. case 7: /* decimal divide by zero */
  119. sig = SIGFPE; code = __FPE_DECDIV;
  120. break;
  121. case 8: /* packed decimal error */
  122. sig = SIGFPE; code = __FPE_DECERR;
  123. break;
  124. case 9: /* invalid ASCII digit */
  125. sig = SIGFPE; code = __FPE_INVASC;
  126. break;
  127. case 10: /* invalid decimal digit */
  128. sig = SIGFPE; code = __FPE_INVDEC;
  129. break;
  130. case 11: /* paragraph stack overflow */
  131. sig = SIGSEGV; code = __SEGV_PSTKOVF;
  132. break;
  133. case 0x3f000 ... 0x3ffff: /* bundle-update in progress */
  134. sig = SIGILL; code = __ILL_BNDMOD;
  135. break;
  136. default:
  137. if ((break_num < 0x40000 || break_num > 0x100000)
  138. && die_if_kernel("Bad break", regs, break_num))
  139. return;
  140. if (break_num < 0x80000) {
  141. sig = SIGILL; code = __ILL_BREAK;
  142. } else {
  143. if (notify_die(DIE_BREAK, "bad break", regs, break_num, TRAP_BRKPT, SIGTRAP)
  144. == NOTIFY_STOP)
  145. return;
  146. sig = SIGTRAP; code = TRAP_BRKPT;
  147. }
  148. }
  149. force_sig_fault(sig, code,
  150. (void __user *) (regs->cr_iip + ia64_psr(regs)->ri),
  151. break_num, 0 /* clear __ISR_VALID */, 0);
  152. }
  153. /*
  154. * disabled_fph_fault() is called when a user-level process attempts to access f32..f127
  155. * and it doesn't own the fp-high register partition. When this happens, we save the
  156. * current fph partition in the task_struct of the fpu-owner (if necessary) and then load
  157. * the fp-high partition of the current task (if necessary). Note that the kernel has
  158. * access to fph by the time we get here, as the IVT's "Disabled FP-Register" handler takes
  159. * care of clearing psr.dfh.
  160. */
  161. static inline void
  162. disabled_fph_fault (struct pt_regs *regs)
  163. {
  164. struct ia64_psr *psr = ia64_psr(regs);
  165. /* first, grant user-level access to fph partition: */
  166. psr->dfh = 0;
  167. /*
  168. * Make sure that no other task gets in on this processor
  169. * while we're claiming the FPU
  170. */
  171. preempt_disable();
  172. #ifndef CONFIG_SMP
  173. {
  174. struct task_struct *fpu_owner
  175. = (struct task_struct *)ia64_get_kr(IA64_KR_FPU_OWNER);
  176. if (ia64_is_local_fpu_owner(current)) {
  177. preempt_enable_no_resched();
  178. return;
  179. }
  180. if (fpu_owner)
  181. ia64_flush_fph(fpu_owner);
  182. }
  183. #endif /* !CONFIG_SMP */
  184. ia64_set_local_fpu_owner(current);
  185. if ((current->thread.flags & IA64_THREAD_FPH_VALID) != 0) {
  186. __ia64_load_fpu(current->thread.fph);
  187. psr->mfh = 0;
  188. } else {
  189. __ia64_init_fpu();
  190. /*
  191. * Set mfh because the state in thread.fph does not match the state in
  192. * the fph partition.
  193. */
  194. psr->mfh = 1;
  195. }
  196. preempt_enable_no_resched();
  197. }
  198. static inline int
  199. fp_emulate (int fp_fault, void *bundle, long *ipsr, long *fpsr, long *isr, long *pr, long *ifs,
  200. struct pt_regs *regs)
  201. {
  202. fp_state_t fp_state;
  203. fpswa_ret_t ret;
  204. if (!fpswa_interface)
  205. return -1;
  206. memset(&fp_state, 0, sizeof(fp_state_t));
  207. /*
  208. * compute fp_state. only FP registers f6 - f11 are used by the
  209. * kernel, so set those bits in the mask and set the low volatile
  210. * pointer to point to these registers.
  211. */
  212. fp_state.bitmask_low64 = 0xfc0; /* bit6..bit11 */
  213. fp_state.fp_state_low_volatile = (fp_state_low_volatile_t *) &regs->f6;
  214. /*
  215. * unsigned long (*EFI_FPSWA) (
  216. * unsigned long trap_type,
  217. * void *Bundle,
  218. * unsigned long *pipsr,
  219. * unsigned long *pfsr,
  220. * unsigned long *pisr,
  221. * unsigned long *ppreds,
  222. * unsigned long *pifs,
  223. * void *fp_state);
  224. */
  225. ret = (*fpswa_interface->fpswa)((unsigned long) fp_fault, bundle,
  226. (unsigned long *) ipsr, (unsigned long *) fpsr,
  227. (unsigned long *) isr, (unsigned long *) pr,
  228. (unsigned long *) ifs, &fp_state);
  229. return ret.status;
  230. }
  231. struct fpu_swa_msg {
  232. unsigned long count;
  233. unsigned long time;
  234. };
  235. static DEFINE_PER_CPU(struct fpu_swa_msg, cpulast);
  236. DECLARE_PER_CPU(struct fpu_swa_msg, cpulast);
  237. static struct fpu_swa_msg last __cacheline_aligned;
  238. /*
  239. * Handle floating-point assist faults and traps.
  240. */
  241. static int
  242. handle_fpu_swa (int fp_fault, struct pt_regs *regs, unsigned long isr)
  243. {
  244. long exception, bundle[2];
  245. unsigned long fault_ip;
  246. fault_ip = regs->cr_iip;
  247. if (!fp_fault && (ia64_psr(regs)->ri == 0))
  248. fault_ip -= 16;
  249. if (copy_from_user(bundle, (void __user *) fault_ip, sizeof(bundle)))
  250. return -1;
  251. if (!(current->thread.flags & IA64_THREAD_FPEMU_NOPRINT)) {
  252. unsigned long count, current_jiffies = jiffies;
  253. struct fpu_swa_msg *cp = this_cpu_ptr(&cpulast);
  254. if (unlikely(current_jiffies > cp->time))
  255. cp->count = 0;
  256. if (unlikely(cp->count < 5)) {
  257. cp->count++;
  258. cp->time = current_jiffies + 5 * HZ;
  259. /* minimize races by grabbing a copy of count BEFORE checking last.time. */
  260. count = last.count;
  261. barrier();
  262. /*
  263. * Lower 4 bits are used as a count. Upper bits are a sequence
  264. * number that is updated when count is reset. The cmpxchg will
  265. * fail is seqno has changed. This minimizes multiple cpus
  266. * resetting the count.
  267. */
  268. if (current_jiffies > last.time)
  269. (void) cmpxchg_acq(&last.count, count, 16 + (count & ~15));
  270. /* used fetchadd to atomically update the count */
  271. if ((last.count & 15) < 5 && (ia64_fetchadd(1, &last.count, acq) & 15) < 5) {
  272. last.time = current_jiffies + 5 * HZ;
  273. printk(KERN_WARNING
  274. "%s(%d): floating-point assist fault at ip %016lx, isr %016lx\n",
  275. current->comm, task_pid_nr(current), regs->cr_iip + ia64_psr(regs)->ri, isr);
  276. }
  277. }
  278. }
  279. exception = fp_emulate(fp_fault, bundle, &regs->cr_ipsr, &regs->ar_fpsr, &isr, &regs->pr,
  280. &regs->cr_ifs, regs);
  281. if (fp_fault) {
  282. if (exception == 0) {
  283. /* emulation was successful */
  284. ia64_increment_ip(regs);
  285. } else if (exception == -1) {
  286. printk(KERN_ERR "handle_fpu_swa: fp_emulate() returned -1\n");
  287. return -1;
  288. } else {
  289. /* is next instruction a trap? */
  290. int si_code;
  291. if (exception & 2) {
  292. ia64_increment_ip(regs);
  293. }
  294. si_code = FPE_FLTUNK; /* default code */
  295. if (isr & 0x11) {
  296. si_code = FPE_FLTINV;
  297. } else if (isr & 0x22) {
  298. /* denormal operand gets the same si_code as underflow
  299. * see arch/i386/kernel/traps.c:math_error() */
  300. si_code = FPE_FLTUND;
  301. } else if (isr & 0x44) {
  302. si_code = FPE_FLTDIV;
  303. }
  304. force_sig_fault(SIGFPE, si_code,
  305. (void __user *) (regs->cr_iip + ia64_psr(regs)->ri),
  306. 0, __ISR_VALID, isr);
  307. }
  308. } else {
  309. if (exception == -1) {
  310. printk(KERN_ERR "handle_fpu_swa: fp_emulate() returned -1\n");
  311. return -1;
  312. } else if (exception != 0) {
  313. /* raise exception */
  314. int si_code;
  315. si_code = FPE_FLTUNK; /* default code */
  316. if (isr & 0x880) {
  317. si_code = FPE_FLTOVF;
  318. } else if (isr & 0x1100) {
  319. si_code = FPE_FLTUND;
  320. } else if (isr & 0x2200) {
  321. si_code = FPE_FLTRES;
  322. }
  323. force_sig_fault(SIGFPE, si_code,
  324. (void __user *) (regs->cr_iip + ia64_psr(regs)->ri),
  325. 0, __ISR_VALID, isr);
  326. }
  327. }
  328. return 0;
  329. }
  330. struct illegal_op_return {
  331. unsigned long fkt, arg1, arg2, arg3;
  332. };
  333. struct illegal_op_return
  334. ia64_illegal_op_fault (unsigned long ec, long arg1, long arg2, long arg3,
  335. long arg4, long arg5, long arg6, long arg7,
  336. struct pt_regs regs)
  337. {
  338. struct illegal_op_return rv;
  339. char buf[128];
  340. #ifdef CONFIG_IA64_BRL_EMU
  341. {
  342. extern struct illegal_op_return ia64_emulate_brl (struct pt_regs *, unsigned long);
  343. rv = ia64_emulate_brl(&regs, ec);
  344. if (rv.fkt != (unsigned long) -1)
  345. return rv;
  346. }
  347. #endif
  348. sprintf(buf, "IA-64 Illegal operation fault");
  349. rv.fkt = 0;
  350. if (die_if_kernel(buf, &regs, 0))
  351. return rv;
  352. force_sig_fault(SIGILL, ILL_ILLOPC,
  353. (void __user *) (regs.cr_iip + ia64_psr(&regs)->ri),
  354. 0, 0, 0);
  355. return rv;
  356. }
  357. void __kprobes
  358. ia64_fault (unsigned long vector, unsigned long isr, unsigned long ifa,
  359. unsigned long iim, unsigned long itir, long arg5, long arg6,
  360. long arg7, struct pt_regs regs)
  361. {
  362. unsigned long code, error = isr, iip;
  363. char buf[128];
  364. int result, sig, si_code;
  365. static const char *reason[] = {
  366. "IA-64 Illegal Operation fault",
  367. "IA-64 Privileged Operation fault",
  368. "IA-64 Privileged Register fault",
  369. "IA-64 Reserved Register/Field fault",
  370. "Disabled Instruction Set Transition fault",
  371. "Unknown fault 5", "Unknown fault 6", "Unknown fault 7", "Illegal Hazard fault",
  372. "Unknown fault 9", "Unknown fault 10", "Unknown fault 11", "Unknown fault 12",
  373. "Unknown fault 13", "Unknown fault 14", "Unknown fault 15"
  374. };
  375. if ((isr & IA64_ISR_NA) && ((isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH)) {
  376. /*
  377. * This fault was due to lfetch.fault, set "ed" bit in the psr to cancel
  378. * the lfetch.
  379. */
  380. ia64_psr(&regs)->ed = 1;
  381. return;
  382. }
  383. iip = regs.cr_iip + ia64_psr(&regs)->ri;
  384. switch (vector) {
  385. case 24: /* General Exception */
  386. code = (isr >> 4) & 0xf;
  387. sprintf(buf, "General Exception: %s%s", reason[code],
  388. (code == 3) ? ((isr & (1UL << 37))
  389. ? " (RSE access)" : " (data access)") : "");
  390. if (code == 8) {
  391. # ifdef CONFIG_IA64_PRINT_HAZARDS
  392. printk("%s[%d]: possible hazard @ ip=%016lx (pr = %016lx)\n",
  393. current->comm, task_pid_nr(current),
  394. regs.cr_iip + ia64_psr(&regs)->ri, regs.pr);
  395. # endif
  396. return;
  397. }
  398. break;
  399. case 25: /* Disabled FP-Register */
  400. if (isr & 2) {
  401. disabled_fph_fault(&regs);
  402. return;
  403. }
  404. sprintf(buf, "Disabled FPL fault---not supposed to happen!");
  405. break;
  406. case 26: /* NaT Consumption */
  407. if (user_mode(&regs)) {
  408. void __user *addr;
  409. if (((isr >> 4) & 0xf) == 2) {
  410. /* NaT page consumption */
  411. sig = SIGSEGV;
  412. code = SEGV_ACCERR;
  413. addr = (void __user *) ifa;
  414. } else {
  415. /* register NaT consumption */
  416. sig = SIGILL;
  417. code = ILL_ILLOPN;
  418. addr = (void __user *) (regs.cr_iip
  419. + ia64_psr(&regs)->ri);
  420. }
  421. force_sig_fault(sig, code, addr,
  422. vector, __ISR_VALID, isr);
  423. return;
  424. } else if (ia64_done_with_exception(&regs))
  425. return;
  426. sprintf(buf, "NaT consumption");
  427. break;
  428. case 31: /* Unsupported Data Reference */
  429. if (user_mode(&regs)) {
  430. force_sig_fault(SIGILL, ILL_ILLOPN, (void __user *) iip,
  431. vector, __ISR_VALID, isr);
  432. return;
  433. }
  434. sprintf(buf, "Unsupported data reference");
  435. break;
  436. case 29: /* Debug */
  437. case 35: /* Taken Branch Trap */
  438. case 36: /* Single Step Trap */
  439. if (fsys_mode(current, &regs)) {
  440. extern char __kernel_syscall_via_break[];
  441. /*
  442. * Got a trap in fsys-mode: Taken Branch Trap
  443. * and Single Step trap need special handling;
  444. * Debug trap is ignored (we disable it here
  445. * and re-enable it in the lower-privilege trap).
  446. */
  447. if (unlikely(vector == 29)) {
  448. set_thread_flag(TIF_DB_DISABLED);
  449. ia64_psr(&regs)->db = 0;
  450. ia64_psr(&regs)->lp = 1;
  451. return;
  452. }
  453. /* re-do the system call via break 0x100000: */
  454. regs.cr_iip = (unsigned long) __kernel_syscall_via_break;
  455. ia64_psr(&regs)->ri = 0;
  456. ia64_psr(&regs)->cpl = 3;
  457. return;
  458. }
  459. switch (vector) {
  460. default:
  461. case 29:
  462. si_code = TRAP_HWBKPT;
  463. #ifdef CONFIG_ITANIUM
  464. /*
  465. * Erratum 10 (IFA may contain incorrect address) now has
  466. * "NoFix" status. There are no plans for fixing this.
  467. */
  468. if (ia64_psr(&regs)->is == 0)
  469. ifa = regs.cr_iip;
  470. #endif
  471. break;
  472. case 35: si_code = TRAP_BRANCH; ifa = 0; break;
  473. case 36: si_code = TRAP_TRACE; ifa = 0; break;
  474. }
  475. if (notify_die(DIE_FAULT, "ia64_fault", &regs, vector, si_code, SIGTRAP)
  476. == NOTIFY_STOP)
  477. return;
  478. force_sig_fault(SIGTRAP, si_code, (void __user *) ifa,
  479. 0, __ISR_VALID, isr);
  480. return;
  481. case 32: /* fp fault */
  482. case 33: /* fp trap */
  483. result = handle_fpu_swa((vector == 32) ? 1 : 0, &regs, isr);
  484. if ((result < 0) || (current->thread.flags & IA64_THREAD_FPEMU_SIGFPE)) {
  485. force_sig_fault(SIGFPE, FPE_FLTINV, (void __user *) iip,
  486. 0, __ISR_VALID, isr);
  487. }
  488. return;
  489. case 34:
  490. if (isr & 0x2) {
  491. /* Lower-Privilege Transfer Trap */
  492. /* If we disabled debug traps during an fsyscall,
  493. * re-enable them here.
  494. */
  495. if (test_thread_flag(TIF_DB_DISABLED)) {
  496. clear_thread_flag(TIF_DB_DISABLED);
  497. ia64_psr(&regs)->db = 1;
  498. }
  499. /*
  500. * Just clear PSR.lp and then return immediately:
  501. * all the interesting work (e.g., signal delivery)
  502. * is done in the kernel exit path.
  503. */
  504. ia64_psr(&regs)->lp = 0;
  505. return;
  506. } else {
  507. /* Unimplemented Instr. Address Trap */
  508. if (user_mode(&regs)) {
  509. force_sig_fault(SIGILL, ILL_BADIADDR,
  510. (void __user *) iip,
  511. 0, 0, 0);
  512. return;
  513. }
  514. sprintf(buf, "Unimplemented Instruction Address fault");
  515. }
  516. break;
  517. case 45:
  518. printk(KERN_ERR "Unexpected IA-32 exception (Trap 45)\n");
  519. printk(KERN_ERR " iip - 0x%lx, ifa - 0x%lx, isr - 0x%lx\n",
  520. iip, ifa, isr);
  521. force_sig(SIGSEGV);
  522. return;
  523. case 46:
  524. printk(KERN_ERR "Unexpected IA-32 intercept trap (Trap 46)\n");
  525. printk(KERN_ERR " iip - 0x%lx, ifa - 0x%lx, isr - 0x%lx, iim - 0x%lx\n",
  526. iip, ifa, isr, iim);
  527. force_sig(SIGSEGV);
  528. return;
  529. case 47:
  530. sprintf(buf, "IA-32 Interruption Fault (int 0x%lx)", isr >> 16);
  531. break;
  532. default:
  533. sprintf(buf, "Fault %lu", vector);
  534. break;
  535. }
  536. if (!die_if_kernel(buf, &regs, error))
  537. force_sig(SIGILL);
  538. }