traps.c 13 KB

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