traps.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. }
  117. if (nl)
  118. printk("\n");
  119. print_modules();
  120. show_regs(regs);
  121. bust_spinlocks(0);
  122. spin_unlock_irq(&die_lock);
  123. if (in_interrupt())
  124. panic("Fatal exception in interrupt");
  125. if (panic_on_oops) {
  126. printk(KERN_EMERG "Fatal exception: panic in 5 seconds\n");
  127. ssleep(5);
  128. panic("Fatal exception");
  129. }
  130. do_exit(SIGSEGV);
  131. return 0;
  132. }
  133. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  134. {
  135. siginfo_t info;
  136. if (!user_mode(regs)) {
  137. if (die("Exception in kernel mode", regs, signr))
  138. return;
  139. }
  140. memset(&info, 0, sizeof(info));
  141. info.si_signo = signr;
  142. info.si_code = code;
  143. info.si_addr = (void __user *) addr;
  144. force_sig_info(signr, &info, current);
  145. }
  146. void system_reset_exception(struct pt_regs *regs)
  147. {
  148. /* See if any machine dependent calls */
  149. if (ppc_md.system_reset_exception)
  150. ppc_md.system_reset_exception(regs);
  151. die("System Reset", regs, 0);
  152. /* Must die if the interrupt is not recoverable */
  153. if (!(regs->msr & MSR_RI))
  154. panic("Unrecoverable System Reset");
  155. /* What should we do here? We could issue a shutdown or hard reset. */
  156. }
  157. void machine_check_exception(struct pt_regs *regs)
  158. {
  159. int recover = 0;
  160. /* See if any machine dependent calls */
  161. if (ppc_md.machine_check_exception)
  162. recover = ppc_md.machine_check_exception(regs);
  163. if (recover)
  164. return;
  165. if (debugger_fault_handler(regs))
  166. return;
  167. die("Machine check", regs, 0);
  168. /* Must die if the interrupt is not recoverable */
  169. if (!(regs->msr & MSR_RI))
  170. panic("Unrecoverable Machine check");
  171. }
  172. void unknown_exception(struct pt_regs *regs)
  173. {
  174. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  175. regs->nip, regs->msr, regs->trap);
  176. _exception(SIGTRAP, regs, 0, 0);
  177. }
  178. void instruction_breakpoint_exception(struct pt_regs *regs)
  179. {
  180. if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
  181. 5, SIGTRAP) == NOTIFY_STOP)
  182. return;
  183. if (debugger_iabr_match(regs))
  184. return;
  185. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  186. }
  187. void single_step_exception(struct pt_regs *regs)
  188. {
  189. regs->msr &= ~MSR_SE; /* Turn off 'trace' bit */
  190. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  191. 5, SIGTRAP) == NOTIFY_STOP)
  192. return;
  193. if (debugger_sstep(regs))
  194. return;
  195. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  196. }
  197. /*
  198. * After we have successfully emulated an instruction, we have to
  199. * check if the instruction was being single-stepped, and if so,
  200. * pretend we got a single-step exception. This was pointed out
  201. * by Kumar Gala. -- paulus
  202. */
  203. static inline void emulate_single_step(struct pt_regs *regs)
  204. {
  205. if (regs->msr & MSR_SE)
  206. single_step_exception(regs);
  207. }
  208. static void parse_fpe(struct pt_regs *regs)
  209. {
  210. int code = 0;
  211. unsigned long fpscr;
  212. flush_fp_to_thread(current);
  213. fpscr = current->thread.fpscr;
  214. /* Invalid operation */
  215. if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
  216. code = FPE_FLTINV;
  217. /* Overflow */
  218. else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
  219. code = FPE_FLTOVF;
  220. /* Underflow */
  221. else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
  222. code = FPE_FLTUND;
  223. /* Divide by zero */
  224. else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
  225. code = FPE_FLTDIV;
  226. /* Inexact result */
  227. else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
  228. code = FPE_FLTRES;
  229. _exception(SIGFPE, regs, code, regs->nip);
  230. }
  231. /*
  232. * Illegal instruction emulation support. Return non-zero if we can't
  233. * emulate, or -EFAULT if the associated memory access caused an access
  234. * fault. Return zero on success.
  235. */
  236. #define INST_MFSPR_PVR 0x7c1f42a6
  237. #define INST_MFSPR_PVR_MASK 0xfc1fffff
  238. #define INST_DCBA 0x7c0005ec
  239. #define INST_DCBA_MASK 0x7c0007fe
  240. #define INST_MCRXR 0x7c000400
  241. #define INST_MCRXR_MASK 0x7c0007fe
  242. static int emulate_instruction(struct pt_regs *regs)
  243. {
  244. unsigned int instword;
  245. if (!user_mode(regs))
  246. return -EINVAL;
  247. CHECK_FULL_REGS(regs);
  248. if (get_user(instword, (unsigned int __user *)(regs->nip)))
  249. return -EFAULT;
  250. /* Emulate the mfspr rD, PVR. */
  251. if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
  252. unsigned int rd;
  253. rd = (instword >> 21) & 0x1f;
  254. regs->gpr[rd] = mfspr(SPRN_PVR);
  255. return 0;
  256. }
  257. /* Emulating the dcba insn is just a no-op. */
  258. if ((instword & INST_DCBA_MASK) == INST_DCBA) {
  259. static int warned;
  260. if (!warned) {
  261. printk(KERN_WARNING
  262. "process %d (%s) uses obsolete 'dcba' insn\n",
  263. current->pid, current->comm);
  264. warned = 1;
  265. }
  266. return 0;
  267. }
  268. /* Emulate the mcrxr insn. */
  269. if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
  270. static int warned;
  271. unsigned int shift;
  272. if (!warned) {
  273. printk(KERN_WARNING
  274. "process %d (%s) uses obsolete 'mcrxr' insn\n",
  275. current->pid, current->comm);
  276. warned = 1;
  277. }
  278. shift = (instword >> 21) & 0x1c;
  279. regs->ccr &= ~(0xf0000000 >> shift);
  280. regs->ccr |= (regs->xer & 0xf0000000) >> shift;
  281. regs->xer &= ~0xf0000000;
  282. return 0;
  283. }
  284. return -EINVAL;
  285. }
  286. /*
  287. * Look through the list of trap instructions that are used for BUG(),
  288. * BUG_ON() and WARN_ON() and see if we hit one. At this point we know
  289. * that the exception was caused by a trap instruction of some kind.
  290. * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
  291. * otherwise.
  292. */
  293. extern struct bug_entry __start___bug_table[], __stop___bug_table[];
  294. #ifndef CONFIG_MODULES
  295. #define module_find_bug(x) NULL
  296. #endif
  297. struct bug_entry *find_bug(unsigned long bugaddr)
  298. {
  299. struct bug_entry *bug;
  300. for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
  301. if (bugaddr == bug->bug_addr)
  302. return bug;
  303. return module_find_bug(bugaddr);
  304. }
  305. static int
  306. check_bug_trap(struct pt_regs *regs)
  307. {
  308. struct bug_entry *bug;
  309. unsigned long addr;
  310. if (regs->msr & MSR_PR)
  311. return 0; /* not in kernel */
  312. addr = regs->nip; /* address of trap instruction */
  313. if (addr < PAGE_OFFSET)
  314. return 0;
  315. bug = find_bug(regs->nip);
  316. if (bug == NULL)
  317. return 0;
  318. if (bug->line & BUG_WARNING_TRAP) {
  319. /* this is a WARN_ON rather than BUG/BUG_ON */
  320. printk(KERN_ERR "Badness in %s at %s:%d\n",
  321. bug->function, bug->file,
  322. (unsigned int)bug->line & ~BUG_WARNING_TRAP);
  323. show_stack(current, (void *)regs->gpr[1]);
  324. return 1;
  325. }
  326. printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
  327. bug->function, bug->file, (unsigned int)bug->line);
  328. return 0;
  329. }
  330. void program_check_exception(struct pt_regs *regs)
  331. {
  332. if (debugger_fault_handler(regs))
  333. return;
  334. if (regs->msr & 0x100000) {
  335. /* IEEE FP exception */
  336. parse_fpe(regs);
  337. } else if (regs->msr & 0x20000) {
  338. /* trap exception */
  339. if (notify_die(DIE_BPT, "breakpoint", regs, 5,
  340. 5, SIGTRAP) == NOTIFY_STOP)
  341. return;
  342. if (debugger_bpt(regs))
  343. return;
  344. if (check_bug_trap(regs)) {
  345. regs->nip += 4;
  346. return;
  347. }
  348. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  349. } else {
  350. /* Privileged or illegal instruction; try to emulate it. */
  351. switch (emulate_instruction(regs)) {
  352. case 0:
  353. regs->nip += 4;
  354. emulate_single_step(regs);
  355. break;
  356. case -EFAULT:
  357. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  358. break;
  359. default:
  360. if (regs->msr & 0x40000)
  361. /* priveleged */
  362. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  363. else
  364. /* illegal */
  365. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  366. break;
  367. }
  368. }
  369. }
  370. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  371. {
  372. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  373. "%lx at %lx\n", regs->trap, regs->nip);
  374. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  375. }
  376. void altivec_unavailable_exception(struct pt_regs *regs)
  377. {
  378. if (user_mode(regs)) {
  379. /* A user program has executed an altivec instruction,
  380. but this kernel doesn't support altivec. */
  381. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  382. return;
  383. }
  384. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  385. "%lx at %lx\n", regs->trap, regs->nip);
  386. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  387. }
  388. extern perf_irq_t perf_irq;
  389. void performance_monitor_exception(struct pt_regs *regs)
  390. {
  391. perf_irq(regs);
  392. }
  393. void alignment_exception(struct pt_regs *regs)
  394. {
  395. int fixed;
  396. fixed = fix_alignment(regs);
  397. if (fixed == 1) {
  398. regs->nip += 4; /* skip over emulated instruction */
  399. emulate_single_step(regs);
  400. return;
  401. }
  402. /* Operand address was bad */
  403. if (fixed == -EFAULT) {
  404. if (user_mode(regs)) {
  405. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->dar);
  406. } else {
  407. /* Search exception table */
  408. bad_page_fault(regs, regs->dar, SIGSEGV);
  409. }
  410. return;
  411. }
  412. _exception(SIGBUS, regs, BUS_ADRALN, regs->nip);
  413. }
  414. #ifdef CONFIG_ALTIVEC
  415. void altivec_assist_exception(struct pt_regs *regs)
  416. {
  417. int err;
  418. siginfo_t info;
  419. if (!user_mode(regs)) {
  420. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  421. " at %lx\n", regs->nip);
  422. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  423. }
  424. flush_altivec_to_thread(current);
  425. err = emulate_altivec(regs);
  426. if (err == 0) {
  427. regs->nip += 4; /* skip emulated instruction */
  428. emulate_single_step(regs);
  429. return;
  430. }
  431. if (err == -EFAULT) {
  432. /* got an error reading the instruction */
  433. info.si_signo = SIGSEGV;
  434. info.si_errno = 0;
  435. info.si_code = SEGV_MAPERR;
  436. info.si_addr = (void __user *) regs->nip;
  437. force_sig_info(SIGSEGV, &info, current);
  438. } else {
  439. /* didn't recognize the instruction */
  440. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  441. if (printk_ratelimit())
  442. printk(KERN_ERR "Unrecognized altivec instruction "
  443. "in %s at %lx\n", current->comm, regs->nip);
  444. current->thread.vscr.u[3] |= 0x10000;
  445. }
  446. }
  447. #endif /* CONFIG_ALTIVEC */
  448. /*
  449. * We enter here if we get an unrecoverable exception, that is, one
  450. * that happened at a point where the RI (recoverable interrupt) bit
  451. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  452. * we therefore lost state by taking this exception.
  453. */
  454. void unrecoverable_exception(struct pt_regs *regs)
  455. {
  456. printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
  457. regs->trap, regs->nip);
  458. die("Unrecoverable exception", regs, SIGABRT);
  459. }
  460. /*
  461. * We enter here if we discover during exception entry that we are
  462. * running in supervisor mode with a userspace value in the stack pointer.
  463. */
  464. void kernel_bad_stack(struct pt_regs *regs)
  465. {
  466. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  467. regs->gpr[1], regs->nip);
  468. die("Bad kernel stack pointer", regs, SIGABRT);
  469. }
  470. void __init trap_init(void)
  471. {
  472. }