traps.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * linux/arch/ppc64/kernel/traps.c
  3. *
  4. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Modified by Cort Dougan (cort@cs.nmt.edu)
  12. * and Paul Mackerras (paulus@cs.anu.edu.au)
  13. */
  14. /*
  15. * This file handles the architecture-dependent parts of hardware exceptions
  16. */
  17. #include <linux/config.h>
  18. #include <linux/errno.h>
  19. #include <linux/sched.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/stddef.h>
  23. #include <linux/unistd.h>
  24. #include <linux/slab.h>
  25. #include <linux/user.h>
  26. #include <linux/a.out.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/delay.h>
  31. #include <asm/kdebug.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/system.h>
  35. #include <asm/io.h>
  36. #include <asm/processor.h>
  37. #include <asm/ppcdebug.h>
  38. #include <asm/rtas.h>
  39. #include <asm/systemcfg.h>
  40. #include <asm/machdep.h>
  41. #include <asm/pmc.h>
  42. #ifdef CONFIG_DEBUGGER
  43. int (*__debugger)(struct pt_regs *regs);
  44. int (*__debugger_ipi)(struct pt_regs *regs);
  45. int (*__debugger_bpt)(struct pt_regs *regs);
  46. int (*__debugger_sstep)(struct pt_regs *regs);
  47. int (*__debugger_iabr_match)(struct pt_regs *regs);
  48. int (*__debugger_dabr_match)(struct pt_regs *regs);
  49. int (*__debugger_fault_handler)(struct pt_regs *regs);
  50. EXPORT_SYMBOL(__debugger);
  51. EXPORT_SYMBOL(__debugger_ipi);
  52. EXPORT_SYMBOL(__debugger_bpt);
  53. EXPORT_SYMBOL(__debugger_sstep);
  54. EXPORT_SYMBOL(__debugger_iabr_match);
  55. EXPORT_SYMBOL(__debugger_dabr_match);
  56. EXPORT_SYMBOL(__debugger_fault_handler);
  57. #endif
  58. struct notifier_block *ppc64_die_chain;
  59. static DEFINE_SPINLOCK(die_notifier_lock);
  60. int register_die_notifier(struct notifier_block *nb)
  61. {
  62. int err = 0;
  63. unsigned long flags;
  64. spin_lock_irqsave(&die_notifier_lock, flags);
  65. err = notifier_chain_register(&ppc64_die_chain, nb);
  66. spin_unlock_irqrestore(&die_notifier_lock, flags);
  67. return err;
  68. }
  69. /*
  70. * Trap & Exception support
  71. */
  72. static DEFINE_SPINLOCK(die_lock);
  73. int die(const char *str, struct pt_regs *regs, long err)
  74. {
  75. static int die_counter;
  76. int nl = 0;
  77. if (debugger(regs))
  78. return 1;
  79. console_verbose();
  80. spin_lock_irq(&die_lock);
  81. bust_spinlocks(1);
  82. printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
  83. #ifdef CONFIG_PREEMPT
  84. printk("PREEMPT ");
  85. nl = 1;
  86. #endif
  87. #ifdef CONFIG_SMP
  88. printk("SMP NR_CPUS=%d ", NR_CPUS);
  89. nl = 1;
  90. #endif
  91. #ifdef CONFIG_DEBUG_PAGEALLOC
  92. printk("DEBUG_PAGEALLOC ");
  93. nl = 1;
  94. #endif
  95. #ifdef CONFIG_NUMA
  96. printk("NUMA ");
  97. nl = 1;
  98. #endif
  99. switch(systemcfg->platform) {
  100. case PLATFORM_PSERIES:
  101. printk("PSERIES ");
  102. nl = 1;
  103. break;
  104. case PLATFORM_PSERIES_LPAR:
  105. printk("PSERIES LPAR ");
  106. nl = 1;
  107. break;
  108. case PLATFORM_ISERIES_LPAR:
  109. printk("ISERIES LPAR ");
  110. nl = 1;
  111. break;
  112. case PLATFORM_POWERMAC:
  113. printk("POWERMAC ");
  114. nl = 1;
  115. break;
  116. case PLATFORM_BPA:
  117. printk("BPA ");
  118. nl = 1;
  119. break;
  120. }
  121. if (nl)
  122. printk("\n");
  123. print_modules();
  124. show_regs(regs);
  125. bust_spinlocks(0);
  126. spin_unlock_irq(&die_lock);
  127. if (in_interrupt())
  128. panic("Fatal exception in interrupt");
  129. if (panic_on_oops) {
  130. printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
  131. ssleep(5);
  132. panic("Fatal exception");
  133. }
  134. do_exit(SIGSEGV);
  135. return 0;
  136. }
  137. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  138. {
  139. siginfo_t info;
  140. if (!user_mode(regs)) {
  141. if (die("Exception in kernel mode", regs, signr))
  142. return;
  143. }
  144. memset(&info, 0, sizeof(info));
  145. info.si_signo = signr;
  146. info.si_code = code;
  147. info.si_addr = (void __user *) addr;
  148. force_sig_info(signr, &info, current);
  149. }
  150. void system_reset_exception(struct pt_regs *regs)
  151. {
  152. /* See if any machine dependent calls */
  153. if (ppc_md.system_reset_exception)
  154. ppc_md.system_reset_exception(regs);
  155. die("System Reset", regs, 0);
  156. /* Must die if the interrupt is not recoverable */
  157. if (!(regs->msr & MSR_RI))
  158. panic("Unrecoverable System Reset");
  159. /* What should we do here? We could issue a shutdown or hard reset. */
  160. }
  161. void machine_check_exception(struct pt_regs *regs)
  162. {
  163. int recover = 0;
  164. /* See if any machine dependent calls */
  165. if (ppc_md.machine_check_exception)
  166. recover = ppc_md.machine_check_exception(regs);
  167. if (recover)
  168. return;
  169. if (debugger_fault_handler(regs))
  170. return;
  171. die("Machine check", regs, 0);
  172. /* Must die if the interrupt is not recoverable */
  173. if (!(regs->msr & MSR_RI))
  174. panic("Unrecoverable Machine check");
  175. }
  176. void unknown_exception(struct pt_regs *regs)
  177. {
  178. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  179. regs->nip, regs->msr, regs->trap);
  180. _exception(SIGTRAP, regs, 0, 0);
  181. }
  182. void instruction_breakpoint_exception(struct pt_regs *regs)
  183. {
  184. if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
  185. 5, SIGTRAP) == NOTIFY_STOP)
  186. return;
  187. if (debugger_iabr_match(regs))
  188. return;
  189. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  190. }
  191. void single_step_exception(struct pt_regs *regs)
  192. {
  193. regs->msr &= ~MSR_SE; /* Turn off 'trace' bit */
  194. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  195. 5, SIGTRAP) == NOTIFY_STOP)
  196. return;
  197. if (debugger_sstep(regs))
  198. return;
  199. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  200. }
  201. /*
  202. * After we have successfully emulated an instruction, we have to
  203. * check if the instruction was being single-stepped, and if so,
  204. * pretend we got a single-step exception. This was pointed out
  205. * by Kumar Gala. -- paulus
  206. */
  207. static inline void emulate_single_step(struct pt_regs *regs)
  208. {
  209. if (regs->msr & MSR_SE)
  210. single_step_exception(regs);
  211. }
  212. static void parse_fpe(struct pt_regs *regs)
  213. {
  214. int code = 0;
  215. unsigned long fpscr;
  216. flush_fp_to_thread(current);
  217. fpscr = current->thread.fpscr;
  218. /* Invalid operation */
  219. if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
  220. code = FPE_FLTINV;
  221. /* Overflow */
  222. else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
  223. code = FPE_FLTOVF;
  224. /* Underflow */
  225. else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
  226. code = FPE_FLTUND;
  227. /* Divide by zero */
  228. else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
  229. code = FPE_FLTDIV;
  230. /* Inexact result */
  231. else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
  232. code = FPE_FLTRES;
  233. _exception(SIGFPE, regs, code, regs->nip);
  234. }
  235. /*
  236. * Illegal instruction emulation support. Return non-zero if we can't
  237. * emulate, or -EFAULT if the associated memory access caused an access
  238. * fault. Return zero on success.
  239. */
  240. #define INST_MFSPR_PVR 0x7c1f42a6
  241. #define INST_MFSPR_PVR_MASK 0xfc1fffff
  242. #define INST_DCBA 0x7c0005ec
  243. #define INST_DCBA_MASK 0x7c0007fe
  244. #define INST_MCRXR 0x7c000400
  245. #define INST_MCRXR_MASK 0x7c0007fe
  246. static int emulate_instruction(struct pt_regs *regs)
  247. {
  248. unsigned int instword;
  249. if (!user_mode(regs))
  250. return -EINVAL;
  251. CHECK_FULL_REGS(regs);
  252. if (get_user(instword, (unsigned int __user *)(regs->nip)))
  253. return -EFAULT;
  254. /* Emulate the mfspr rD, PVR. */
  255. if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
  256. unsigned int rd;
  257. rd = (instword >> 21) & 0x1f;
  258. regs->gpr[rd] = mfspr(SPRN_PVR);
  259. return 0;
  260. }
  261. /* Emulating the dcba insn is just a no-op. */
  262. if ((instword & INST_DCBA_MASK) == INST_DCBA) {
  263. static int warned;
  264. if (!warned) {
  265. printk(KERN_WARNING
  266. "process %d (%s) uses obsolete 'dcba' insn\n",
  267. current->pid, current->comm);
  268. warned = 1;
  269. }
  270. return 0;
  271. }
  272. /* Emulate the mcrxr insn. */
  273. if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
  274. static int warned;
  275. unsigned int shift;
  276. if (!warned) {
  277. printk(KERN_WARNING
  278. "process %d (%s) uses obsolete 'mcrxr' insn\n",
  279. current->pid, current->comm);
  280. warned = 1;
  281. }
  282. shift = (instword >> 21) & 0x1c;
  283. regs->ccr &= ~(0xf0000000 >> shift);
  284. regs->ccr |= (regs->xer & 0xf0000000) >> shift;
  285. regs->xer &= ~0xf0000000;
  286. return 0;
  287. }
  288. return -EINVAL;
  289. }
  290. /*
  291. * Look through the list of trap instructions that are used for BUG(),
  292. * BUG_ON() and WARN_ON() and see if we hit one. At this point we know
  293. * that the exception was caused by a trap instruction of some kind.
  294. * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
  295. * otherwise.
  296. */
  297. extern struct bug_entry __start___bug_table[], __stop___bug_table[];
  298. #ifndef CONFIG_MODULES
  299. #define module_find_bug(x) NULL
  300. #endif
  301. struct bug_entry *find_bug(unsigned long bugaddr)
  302. {
  303. struct bug_entry *bug;
  304. for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
  305. if (bugaddr == bug->bug_addr)
  306. return bug;
  307. return module_find_bug(bugaddr);
  308. }
  309. static int
  310. check_bug_trap(struct pt_regs *regs)
  311. {
  312. struct bug_entry *bug;
  313. unsigned long addr;
  314. if (regs->msr & MSR_PR)
  315. return 0; /* not in kernel */
  316. addr = regs->nip; /* address of trap instruction */
  317. if (addr < PAGE_OFFSET)
  318. return 0;
  319. bug = find_bug(regs->nip);
  320. if (bug == NULL)
  321. return 0;
  322. if (bug->line & BUG_WARNING_TRAP) {
  323. /* this is a WARN_ON rather than BUG/BUG_ON */
  324. printk(KERN_ERR "Badness in %s at %s:%d\n",
  325. bug->function, bug->file,
  326. (unsigned int)bug->line & ~BUG_WARNING_TRAP);
  327. show_stack(current, (void *)regs->gpr[1]);
  328. return 1;
  329. }
  330. printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
  331. bug->function, bug->file, (unsigned int)bug->line);
  332. return 0;
  333. }
  334. void program_check_exception(struct pt_regs *regs)
  335. {
  336. if (debugger_fault_handler(regs))
  337. return;
  338. if (regs->msr & 0x100000) {
  339. /* IEEE FP exception */
  340. parse_fpe(regs);
  341. } else if (regs->msr & 0x20000) {
  342. /* trap exception */
  343. if (notify_die(DIE_BPT, "breakpoint", regs, 5,
  344. 5, SIGTRAP) == NOTIFY_STOP)
  345. return;
  346. if (debugger_bpt(regs))
  347. return;
  348. if (check_bug_trap(regs)) {
  349. regs->nip += 4;
  350. return;
  351. }
  352. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  353. } else {
  354. /* Privileged or illegal instruction; try to emulate it. */
  355. switch (emulate_instruction(regs)) {
  356. case 0:
  357. regs->nip += 4;
  358. emulate_single_step(regs);
  359. break;
  360. case -EFAULT:
  361. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  362. break;
  363. default:
  364. if (regs->msr & 0x40000)
  365. /* priveleged */
  366. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  367. else
  368. /* illegal */
  369. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  370. break;
  371. }
  372. }
  373. }
  374. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  375. {
  376. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  377. "%lx at %lx\n", regs->trap, regs->nip);
  378. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  379. }
  380. void altivec_unavailable_exception(struct pt_regs *regs)
  381. {
  382. if (user_mode(regs)) {
  383. /* A user program has executed an altivec instruction,
  384. but this kernel doesn't support altivec. */
  385. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  386. return;
  387. }
  388. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  389. "%lx at %lx\n", regs->trap, regs->nip);
  390. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  391. }
  392. extern perf_irq_t perf_irq;
  393. void performance_monitor_exception(struct pt_regs *regs)
  394. {
  395. perf_irq(regs);
  396. }
  397. void alignment_exception(struct pt_regs *regs)
  398. {
  399. int fixed;
  400. fixed = fix_alignment(regs);
  401. if (fixed == 1) {
  402. regs->nip += 4; /* skip over emulated instruction */
  403. emulate_single_step(regs);
  404. return;
  405. }
  406. /* Operand address was bad */
  407. if (fixed == -EFAULT) {
  408. if (user_mode(regs)) {
  409. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->dar);
  410. } else {
  411. /* Search exception table */
  412. bad_page_fault(regs, regs->dar, SIGSEGV);
  413. }
  414. return;
  415. }
  416. _exception(SIGBUS, regs, BUS_ADRALN, regs->nip);
  417. }
  418. #ifdef CONFIG_ALTIVEC
  419. void altivec_assist_exception(struct pt_regs *regs)
  420. {
  421. int err;
  422. siginfo_t info;
  423. if (!user_mode(regs)) {
  424. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  425. " at %lx\n", regs->nip);
  426. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  427. }
  428. flush_altivec_to_thread(current);
  429. err = emulate_altivec(regs);
  430. if (err == 0) {
  431. regs->nip += 4; /* skip emulated instruction */
  432. emulate_single_step(regs);
  433. return;
  434. }
  435. if (err == -EFAULT) {
  436. /* got an error reading the instruction */
  437. info.si_signo = SIGSEGV;
  438. info.si_errno = 0;
  439. info.si_code = SEGV_MAPERR;
  440. info.si_addr = (void __user *) regs->nip;
  441. force_sig_info(SIGSEGV, &info, current);
  442. } else {
  443. /* didn't recognize the instruction */
  444. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  445. if (printk_ratelimit())
  446. printk(KERN_ERR "Unrecognized altivec instruction "
  447. "in %s at %lx\n", current->comm, regs->nip);
  448. current->thread.vscr.u[3] |= 0x10000;
  449. }
  450. }
  451. #endif /* CONFIG_ALTIVEC */
  452. /*
  453. * We enter here if we get an unrecoverable exception, that is, one
  454. * that happened at a point where the RI (recoverable interrupt) bit
  455. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  456. * we therefore lost state by taking this exception.
  457. */
  458. void unrecoverable_exception(struct pt_regs *regs)
  459. {
  460. printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
  461. regs->trap, regs->nip);
  462. die("Unrecoverable exception", regs, SIGABRT);
  463. }
  464. /*
  465. * We enter here if we discover during exception entry that we are
  466. * running in supervisor mode with a userspace value in the stack pointer.
  467. */
  468. void kernel_bad_stack(struct pt_regs *regs)
  469. {
  470. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  471. regs->gpr[1], regs->nip);
  472. die("Bad kernel stack pointer", regs, SIGABRT);
  473. }
  474. void __init trap_init(void)
  475. {
  476. }