traps.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /*
  2. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Modified by Cort Dougan (cort@cs.nmt.edu)
  10. * and Paul Mackerras (paulus@samba.org)
  11. */
  12. /*
  13. * This file handles the architecture-dependent parts of hardware exceptions
  14. */
  15. #include <linux/errno.h>
  16. #include <linux/sched.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/stddef.h>
  20. #include <linux/unistd.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/slab.h>
  23. #include <linux/user.h>
  24. #include <linux/a.out.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/prctl.h>
  29. #include <linux/delay.h>
  30. #include <linux/kprobes.h>
  31. #include <linux/kexec.h>
  32. #include <linux/backlight.h>
  33. #include <asm/kdebug.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/system.h>
  37. #include <asm/io.h>
  38. #include <asm/machdep.h>
  39. #include <asm/rtas.h>
  40. #include <asm/pmc.h>
  41. #ifdef CONFIG_PPC32
  42. #include <asm/reg.h>
  43. #endif
  44. #ifdef CONFIG_PMAC_BACKLIGHT
  45. #include <asm/backlight.h>
  46. #endif
  47. #ifdef CONFIG_PPC64
  48. #include <asm/firmware.h>
  49. #include <asm/processor.h>
  50. #endif
  51. #include <asm/kexec.h>
  52. #ifdef CONFIG_PPC64 /* XXX */
  53. #define _IO_BASE pci_io_base
  54. #endif
  55. #ifdef CONFIG_DEBUGGER
  56. int (*__debugger)(struct pt_regs *regs);
  57. int (*__debugger_ipi)(struct pt_regs *regs);
  58. int (*__debugger_bpt)(struct pt_regs *regs);
  59. int (*__debugger_sstep)(struct pt_regs *regs);
  60. int (*__debugger_iabr_match)(struct pt_regs *regs);
  61. int (*__debugger_dabr_match)(struct pt_regs *regs);
  62. int (*__debugger_fault_handler)(struct pt_regs *regs);
  63. EXPORT_SYMBOL(__debugger);
  64. EXPORT_SYMBOL(__debugger_ipi);
  65. EXPORT_SYMBOL(__debugger_bpt);
  66. EXPORT_SYMBOL(__debugger_sstep);
  67. EXPORT_SYMBOL(__debugger_iabr_match);
  68. EXPORT_SYMBOL(__debugger_dabr_match);
  69. EXPORT_SYMBOL(__debugger_fault_handler);
  70. #endif
  71. ATOMIC_NOTIFIER_HEAD(powerpc_die_chain);
  72. int register_die_notifier(struct notifier_block *nb)
  73. {
  74. return atomic_notifier_chain_register(&powerpc_die_chain, nb);
  75. }
  76. EXPORT_SYMBOL(register_die_notifier);
  77. int unregister_die_notifier(struct notifier_block *nb)
  78. {
  79. return atomic_notifier_chain_unregister(&powerpc_die_chain, nb);
  80. }
  81. EXPORT_SYMBOL(unregister_die_notifier);
  82. /*
  83. * Trap & Exception support
  84. */
  85. static DEFINE_SPINLOCK(die_lock);
  86. int die(const char *str, struct pt_regs *regs, long err)
  87. {
  88. static int die_counter;
  89. if (debugger(regs))
  90. return 1;
  91. console_verbose();
  92. spin_lock_irq(&die_lock);
  93. bust_spinlocks(1);
  94. #ifdef CONFIG_PMAC_BACKLIGHT
  95. mutex_lock(&pmac_backlight_mutex);
  96. if (machine_is(powermac) && pmac_backlight) {
  97. struct backlight_properties *props;
  98. down(&pmac_backlight->sem);
  99. props = pmac_backlight->props;
  100. props->brightness = props->max_brightness;
  101. props->power = FB_BLANK_UNBLANK;
  102. props->update_status(pmac_backlight);
  103. up(&pmac_backlight->sem);
  104. }
  105. mutex_unlock(&pmac_backlight_mutex);
  106. #endif
  107. printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
  108. #ifdef CONFIG_PREEMPT
  109. printk("PREEMPT ");
  110. #endif
  111. #ifdef CONFIG_SMP
  112. printk("SMP NR_CPUS=%d ", NR_CPUS);
  113. #endif
  114. #ifdef CONFIG_DEBUG_PAGEALLOC
  115. printk("DEBUG_PAGEALLOC ");
  116. #endif
  117. #ifdef CONFIG_NUMA
  118. printk("NUMA ");
  119. #endif
  120. printk("%s\n", ppc_md.name ? "" : ppc_md.name);
  121. print_modules();
  122. show_regs(regs);
  123. bust_spinlocks(0);
  124. spin_unlock_irq(&die_lock);
  125. if (kexec_should_crash(current) ||
  126. kexec_sr_activated(smp_processor_id()))
  127. crash_kexec(regs);
  128. crash_kexec_secondary(regs);
  129. if (in_interrupt())
  130. panic("Fatal exception in interrupt");
  131. if (panic_on_oops)
  132. panic("Fatal exception");
  133. do_exit(err);
  134. return 0;
  135. }
  136. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  137. {
  138. siginfo_t info;
  139. if (!user_mode(regs)) {
  140. if (die("Exception in kernel mode", regs, signr))
  141. return;
  142. }
  143. memset(&info, 0, sizeof(info));
  144. info.si_signo = signr;
  145. info.si_code = code;
  146. info.si_addr = (void __user *) addr;
  147. force_sig_info(signr, &info, current);
  148. /*
  149. * Init gets no signals that it doesn't have a handler for.
  150. * That's all very well, but if it has caused a synchronous
  151. * exception and we ignore the resulting signal, it will just
  152. * generate the same exception over and over again and we get
  153. * nowhere. Better to kill it and let the kernel panic.
  154. */
  155. if (current->pid == 1) {
  156. __sighandler_t handler;
  157. spin_lock_irq(&current->sighand->siglock);
  158. handler = current->sighand->action[signr-1].sa.sa_handler;
  159. spin_unlock_irq(&current->sighand->siglock);
  160. if (handler == SIG_DFL) {
  161. /* init has generated a synchronous exception
  162. and it doesn't have a handler for the signal */
  163. printk(KERN_CRIT "init has generated signal %d "
  164. "but has no handler for it\n", signr);
  165. do_exit(signr);
  166. }
  167. }
  168. }
  169. #ifdef CONFIG_PPC64
  170. void system_reset_exception(struct pt_regs *regs)
  171. {
  172. /* See if any machine dependent calls */
  173. if (ppc_md.system_reset_exception) {
  174. if (ppc_md.system_reset_exception(regs))
  175. return;
  176. }
  177. #ifdef CONFIG_KEXEC
  178. cpu_set(smp_processor_id(), cpus_in_sr);
  179. #endif
  180. die("System Reset", regs, SIGABRT);
  181. /*
  182. * Some CPUs when released from the debugger will execute this path.
  183. * These CPUs entered the debugger via a soft-reset. If the CPU was
  184. * hung before entering the debugger it will return to the hung
  185. * state when exiting this function. This causes a problem in
  186. * kdump since the hung CPU(s) will not respond to the IPI sent
  187. * from kdump. To prevent the problem we call crash_kexec_secondary()
  188. * here. If a kdump had not been initiated or we exit the debugger
  189. * with the "exit and recover" command (x) crash_kexec_secondary()
  190. * will return after 5ms and the CPU returns to its previous state.
  191. */
  192. crash_kexec_secondary(regs);
  193. /* Must die if the interrupt is not recoverable */
  194. if (!(regs->msr & MSR_RI))
  195. panic("Unrecoverable System Reset");
  196. /* What should we do here? We could issue a shutdown or hard reset. */
  197. }
  198. #endif
  199. /*
  200. * I/O accesses can cause machine checks on powermacs.
  201. * Check if the NIP corresponds to the address of a sync
  202. * instruction for which there is an entry in the exception
  203. * table.
  204. * Note that the 601 only takes a machine check on TEA
  205. * (transfer error ack) signal assertion, and does not
  206. * set any of the top 16 bits of SRR1.
  207. * -- paulus.
  208. */
  209. static inline int check_io_access(struct pt_regs *regs)
  210. {
  211. #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
  212. unsigned long msr = regs->msr;
  213. const struct exception_table_entry *entry;
  214. unsigned int *nip = (unsigned int *)regs->nip;
  215. if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
  216. && (entry = search_exception_tables(regs->nip)) != NULL) {
  217. /*
  218. * Check that it's a sync instruction, or somewhere
  219. * in the twi; isync; nop sequence that inb/inw/inl uses.
  220. * As the address is in the exception table
  221. * we should be able to read the instr there.
  222. * For the debug message, we look at the preceding
  223. * load or store.
  224. */
  225. if (*nip == 0x60000000) /* nop */
  226. nip -= 2;
  227. else if (*nip == 0x4c00012c) /* isync */
  228. --nip;
  229. if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
  230. /* sync or twi */
  231. unsigned int rb;
  232. --nip;
  233. rb = (*nip >> 11) & 0x1f;
  234. printk(KERN_DEBUG "%s bad port %lx at %p\n",
  235. (*nip & 0x100)? "OUT to": "IN from",
  236. regs->gpr[rb] - _IO_BASE, nip);
  237. regs->msr |= MSR_RI;
  238. regs->nip = entry->fixup;
  239. return 1;
  240. }
  241. }
  242. #endif /* CONFIG_PPC_PMAC && CONFIG_PPC32 */
  243. return 0;
  244. }
  245. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  246. /* On 4xx, the reason for the machine check or program exception
  247. is in the ESR. */
  248. #define get_reason(regs) ((regs)->dsisr)
  249. #ifndef CONFIG_FSL_BOOKE
  250. #define get_mc_reason(regs) ((regs)->dsisr)
  251. #else
  252. #define get_mc_reason(regs) (mfspr(SPRN_MCSR))
  253. #endif
  254. #define REASON_FP ESR_FP
  255. #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
  256. #define REASON_PRIVILEGED ESR_PPR
  257. #define REASON_TRAP ESR_PTR
  258. /* single-step stuff */
  259. #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
  260. #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
  261. #else
  262. /* On non-4xx, the reason for the machine check or program
  263. exception is in the MSR. */
  264. #define get_reason(regs) ((regs)->msr)
  265. #define get_mc_reason(regs) ((regs)->msr)
  266. #define REASON_FP 0x100000
  267. #define REASON_ILLEGAL 0x80000
  268. #define REASON_PRIVILEGED 0x40000
  269. #define REASON_TRAP 0x20000
  270. #define single_stepping(regs) ((regs)->msr & MSR_SE)
  271. #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
  272. #endif
  273. /*
  274. * This is "fall-back" implementation for configurations
  275. * which don't provide platform-specific machine check info
  276. */
  277. void __attribute__ ((weak))
  278. platform_machine_check(struct pt_regs *regs)
  279. {
  280. }
  281. void machine_check_exception(struct pt_regs *regs)
  282. {
  283. int recover = 0;
  284. unsigned long reason = get_mc_reason(regs);
  285. /* See if any machine dependent calls */
  286. if (ppc_md.machine_check_exception)
  287. recover = ppc_md.machine_check_exception(regs);
  288. if (recover)
  289. return;
  290. if (user_mode(regs)) {
  291. regs->msr |= MSR_RI;
  292. _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
  293. return;
  294. }
  295. #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
  296. /* the qspan pci read routines can cause machine checks -- Cort */
  297. bad_page_fault(regs, regs->dar, SIGBUS);
  298. return;
  299. #endif
  300. if (debugger_fault_handler(regs)) {
  301. regs->msr |= MSR_RI;
  302. return;
  303. }
  304. if (check_io_access(regs))
  305. return;
  306. #if defined(CONFIG_4xx) && !defined(CONFIG_440A)
  307. if (reason & ESR_IMCP) {
  308. printk("Instruction");
  309. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  310. } else
  311. printk("Data");
  312. printk(" machine check in kernel mode.\n");
  313. #elif defined(CONFIG_440A)
  314. printk("Machine check in kernel mode.\n");
  315. if (reason & ESR_IMCP){
  316. printk("Instruction Synchronous Machine Check exception\n");
  317. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  318. }
  319. else {
  320. u32 mcsr = mfspr(SPRN_MCSR);
  321. if (mcsr & MCSR_IB)
  322. printk("Instruction Read PLB Error\n");
  323. if (mcsr & MCSR_DRB)
  324. printk("Data Read PLB Error\n");
  325. if (mcsr & MCSR_DWB)
  326. printk("Data Write PLB Error\n");
  327. if (mcsr & MCSR_TLBP)
  328. printk("TLB Parity Error\n");
  329. if (mcsr & MCSR_ICP){
  330. flush_instruction_cache();
  331. printk("I-Cache Parity Error\n");
  332. }
  333. if (mcsr & MCSR_DCSP)
  334. printk("D-Cache Search Parity Error\n");
  335. if (mcsr & MCSR_DCFP)
  336. printk("D-Cache Flush Parity Error\n");
  337. if (mcsr & MCSR_IMPE)
  338. printk("Machine Check exception is imprecise\n");
  339. /* Clear MCSR */
  340. mtspr(SPRN_MCSR, mcsr);
  341. }
  342. #elif defined (CONFIG_E500)
  343. printk("Machine check in kernel mode.\n");
  344. printk("Caused by (from MCSR=%lx): ", reason);
  345. if (reason & MCSR_MCP)
  346. printk("Machine Check Signal\n");
  347. if (reason & MCSR_ICPERR)
  348. printk("Instruction Cache Parity Error\n");
  349. if (reason & MCSR_DCP_PERR)
  350. printk("Data Cache Push Parity Error\n");
  351. if (reason & MCSR_DCPERR)
  352. printk("Data Cache Parity Error\n");
  353. if (reason & MCSR_GL_CI)
  354. printk("Guarded Load or Cache-Inhibited stwcx.\n");
  355. if (reason & MCSR_BUS_IAERR)
  356. printk("Bus - Instruction Address Error\n");
  357. if (reason & MCSR_BUS_RAERR)
  358. printk("Bus - Read Address Error\n");
  359. if (reason & MCSR_BUS_WAERR)
  360. printk("Bus - Write Address Error\n");
  361. if (reason & MCSR_BUS_IBERR)
  362. printk("Bus - Instruction Data Error\n");
  363. if (reason & MCSR_BUS_RBERR)
  364. printk("Bus - Read Data Bus Error\n");
  365. if (reason & MCSR_BUS_WBERR)
  366. printk("Bus - Read Data Bus Error\n");
  367. if (reason & MCSR_BUS_IPERR)
  368. printk("Bus - Instruction Parity Error\n");
  369. if (reason & MCSR_BUS_RPERR)
  370. printk("Bus - Read Parity Error\n");
  371. #elif defined (CONFIG_E200)
  372. printk("Machine check in kernel mode.\n");
  373. printk("Caused by (from MCSR=%lx): ", reason);
  374. if (reason & MCSR_MCP)
  375. printk("Machine Check Signal\n");
  376. if (reason & MCSR_CP_PERR)
  377. printk("Cache Push Parity Error\n");
  378. if (reason & MCSR_CPERR)
  379. printk("Cache Parity Error\n");
  380. if (reason & MCSR_EXCP_ERR)
  381. printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
  382. if (reason & MCSR_BUS_IRERR)
  383. printk("Bus - Read Bus Error on instruction fetch\n");
  384. if (reason & MCSR_BUS_DRERR)
  385. printk("Bus - Read Bus Error on data load\n");
  386. if (reason & MCSR_BUS_WRERR)
  387. printk("Bus - Write Bus Error on buffered store or cache line push\n");
  388. #else /* !CONFIG_4xx && !CONFIG_E500 && !CONFIG_E200 */
  389. printk("Machine check in kernel mode.\n");
  390. printk("Caused by (from SRR1=%lx): ", reason);
  391. switch (reason & 0x601F0000) {
  392. case 0x80000:
  393. printk("Machine check signal\n");
  394. break;
  395. case 0: /* for 601 */
  396. case 0x40000:
  397. case 0x140000: /* 7450 MSS error and TEA */
  398. printk("Transfer error ack signal\n");
  399. break;
  400. case 0x20000:
  401. printk("Data parity error signal\n");
  402. break;
  403. case 0x10000:
  404. printk("Address parity error signal\n");
  405. break;
  406. case 0x20000000:
  407. printk("L1 Data Cache error\n");
  408. break;
  409. case 0x40000000:
  410. printk("L1 Instruction Cache error\n");
  411. break;
  412. case 0x00100000:
  413. printk("L2 data cache parity error\n");
  414. break;
  415. default:
  416. printk("Unknown values in msr\n");
  417. }
  418. #endif /* CONFIG_4xx */
  419. /*
  420. * Optional platform-provided routine to print out
  421. * additional info, e.g. bus error registers.
  422. */
  423. platform_machine_check(regs);
  424. if (debugger_fault_handler(regs))
  425. return;
  426. die("Machine check", regs, SIGBUS);
  427. /* Must die if the interrupt is not recoverable */
  428. if (!(regs->msr & MSR_RI))
  429. panic("Unrecoverable Machine check");
  430. }
  431. void SMIException(struct pt_regs *regs)
  432. {
  433. die("System Management Interrupt", regs, SIGABRT);
  434. }
  435. void unknown_exception(struct pt_regs *regs)
  436. {
  437. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  438. regs->nip, regs->msr, regs->trap);
  439. _exception(SIGTRAP, regs, 0, 0);
  440. }
  441. void instruction_breakpoint_exception(struct pt_regs *regs)
  442. {
  443. if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
  444. 5, SIGTRAP) == NOTIFY_STOP)
  445. return;
  446. if (debugger_iabr_match(regs))
  447. return;
  448. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  449. }
  450. void RunModeException(struct pt_regs *regs)
  451. {
  452. _exception(SIGTRAP, regs, 0, 0);
  453. }
  454. void __kprobes single_step_exception(struct pt_regs *regs)
  455. {
  456. regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
  457. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  458. 5, SIGTRAP) == NOTIFY_STOP)
  459. return;
  460. if (debugger_sstep(regs))
  461. return;
  462. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  463. }
  464. /*
  465. * After we have successfully emulated an instruction, we have to
  466. * check if the instruction was being single-stepped, and if so,
  467. * pretend we got a single-step exception. This was pointed out
  468. * by Kumar Gala. -- paulus
  469. */
  470. static void emulate_single_step(struct pt_regs *regs)
  471. {
  472. if (single_stepping(regs)) {
  473. clear_single_step(regs);
  474. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  475. }
  476. }
  477. static void parse_fpe(struct pt_regs *regs)
  478. {
  479. int code = 0;
  480. unsigned long fpscr;
  481. flush_fp_to_thread(current);
  482. fpscr = current->thread.fpscr.val;
  483. /* Invalid operation */
  484. if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
  485. code = FPE_FLTINV;
  486. /* Overflow */
  487. else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
  488. code = FPE_FLTOVF;
  489. /* Underflow */
  490. else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
  491. code = FPE_FLTUND;
  492. /* Divide by zero */
  493. else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
  494. code = FPE_FLTDIV;
  495. /* Inexact result */
  496. else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
  497. code = FPE_FLTRES;
  498. _exception(SIGFPE, regs, code, regs->nip);
  499. }
  500. /*
  501. * Illegal instruction emulation support. Originally written to
  502. * provide the PVR to user applications using the mfspr rd, PVR.
  503. * Return non-zero if we can't emulate, or -EFAULT if the associated
  504. * memory access caused an access fault. Return zero on success.
  505. *
  506. * There are a couple of ways to do this, either "decode" the instruction
  507. * or directly match lots of bits. In this case, matching lots of
  508. * bits is faster and easier.
  509. *
  510. */
  511. #define INST_MFSPR_PVR 0x7c1f42a6
  512. #define INST_MFSPR_PVR_MASK 0xfc1fffff
  513. #define INST_DCBA 0x7c0005ec
  514. #define INST_DCBA_MASK 0xfc0007fe
  515. #define INST_MCRXR 0x7c000400
  516. #define INST_MCRXR_MASK 0xfc0007fe
  517. #define INST_STRING 0x7c00042a
  518. #define INST_STRING_MASK 0xfc0007fe
  519. #define INST_STRING_GEN_MASK 0xfc00067e
  520. #define INST_LSWI 0x7c0004aa
  521. #define INST_LSWX 0x7c00042a
  522. #define INST_STSWI 0x7c0005aa
  523. #define INST_STSWX 0x7c00052a
  524. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  525. {
  526. u8 rT = (instword >> 21) & 0x1f;
  527. u8 rA = (instword >> 16) & 0x1f;
  528. u8 NB_RB = (instword >> 11) & 0x1f;
  529. u32 num_bytes;
  530. unsigned long EA;
  531. int pos = 0;
  532. /* Early out if we are an invalid form of lswx */
  533. if ((instword & INST_STRING_MASK) == INST_LSWX)
  534. if ((rT == rA) || (rT == NB_RB))
  535. return -EINVAL;
  536. EA = (rA == 0) ? 0 : regs->gpr[rA];
  537. switch (instword & INST_STRING_MASK) {
  538. case INST_LSWX:
  539. case INST_STSWX:
  540. EA += NB_RB;
  541. num_bytes = regs->xer & 0x7f;
  542. break;
  543. case INST_LSWI:
  544. case INST_STSWI:
  545. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  546. break;
  547. default:
  548. return -EINVAL;
  549. }
  550. while (num_bytes != 0)
  551. {
  552. u8 val;
  553. u32 shift = 8 * (3 - (pos & 0x3));
  554. switch ((instword & INST_STRING_MASK)) {
  555. case INST_LSWX:
  556. case INST_LSWI:
  557. if (get_user(val, (u8 __user *)EA))
  558. return -EFAULT;
  559. /* first time updating this reg,
  560. * zero it out */
  561. if (pos == 0)
  562. regs->gpr[rT] = 0;
  563. regs->gpr[rT] |= val << shift;
  564. break;
  565. case INST_STSWI:
  566. case INST_STSWX:
  567. val = regs->gpr[rT] >> shift;
  568. if (put_user(val, (u8 __user *)EA))
  569. return -EFAULT;
  570. break;
  571. }
  572. /* move EA to next address */
  573. EA += 1;
  574. num_bytes--;
  575. /* manage our position within the register */
  576. if (++pos == 4) {
  577. pos = 0;
  578. if (++rT == 32)
  579. rT = 0;
  580. }
  581. }
  582. return 0;
  583. }
  584. static int emulate_instruction(struct pt_regs *regs)
  585. {
  586. u32 instword;
  587. u32 rd;
  588. if (!user_mode(regs) || (regs->msr & MSR_LE))
  589. return -EINVAL;
  590. CHECK_FULL_REGS(regs);
  591. if (get_user(instword, (u32 __user *)(regs->nip)))
  592. return -EFAULT;
  593. /* Emulate the mfspr rD, PVR. */
  594. if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
  595. rd = (instword >> 21) & 0x1f;
  596. regs->gpr[rd] = mfspr(SPRN_PVR);
  597. return 0;
  598. }
  599. /* Emulating the dcba insn is just a no-op. */
  600. if ((instword & INST_DCBA_MASK) == INST_DCBA)
  601. return 0;
  602. /* Emulate the mcrxr insn. */
  603. if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
  604. int shift = (instword >> 21) & 0x1c;
  605. unsigned long msk = 0xf0000000UL >> shift;
  606. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  607. regs->xer &= ~0xf0000000UL;
  608. return 0;
  609. }
  610. /* Emulate load/store string insn. */
  611. if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
  612. return emulate_string_inst(regs, instword);
  613. return -EINVAL;
  614. }
  615. /*
  616. * Look through the list of trap instructions that are used for BUG(),
  617. * BUG_ON() and WARN_ON() and see if we hit one. At this point we know
  618. * that the exception was caused by a trap instruction of some kind.
  619. * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
  620. * otherwise.
  621. */
  622. extern struct bug_entry __start___bug_table[], __stop___bug_table[];
  623. #ifndef CONFIG_MODULES
  624. #define module_find_bug(x) NULL
  625. #endif
  626. struct bug_entry *find_bug(unsigned long bugaddr)
  627. {
  628. struct bug_entry *bug;
  629. for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
  630. if (bugaddr == bug->bug_addr)
  631. return bug;
  632. return module_find_bug(bugaddr);
  633. }
  634. static int check_bug_trap(struct pt_regs *regs)
  635. {
  636. struct bug_entry *bug;
  637. unsigned long addr;
  638. if (regs->msr & MSR_PR)
  639. return 0; /* not in kernel */
  640. addr = regs->nip; /* address of trap instruction */
  641. if (addr < PAGE_OFFSET)
  642. return 0;
  643. bug = find_bug(regs->nip);
  644. if (bug == NULL)
  645. return 0;
  646. if (bug->line & BUG_WARNING_TRAP) {
  647. /* this is a WARN_ON rather than BUG/BUG_ON */
  648. printk(KERN_ERR "Badness in %s at %s:%ld\n",
  649. bug->function, bug->file,
  650. bug->line & ~BUG_WARNING_TRAP);
  651. dump_stack();
  652. return 1;
  653. }
  654. printk(KERN_CRIT "kernel BUG in %s at %s:%ld!\n",
  655. bug->function, bug->file, bug->line);
  656. return 0;
  657. }
  658. void __kprobes program_check_exception(struct pt_regs *regs)
  659. {
  660. unsigned int reason = get_reason(regs);
  661. extern int do_mathemu(struct pt_regs *regs);
  662. #ifdef CONFIG_MATH_EMULATION
  663. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  664. * but there seems to be a hardware bug on the 405GP (RevD)
  665. * that means ESR is sometimes set incorrectly - either to
  666. * ESR_DST (!?) or 0. In the process of chasing this with the
  667. * hardware people - not sure if it can happen on any illegal
  668. * instruction or only on FP instructions, whether there is a
  669. * pattern to occurences etc. -dgibson 31/Mar/2003 */
  670. if (!(reason & REASON_TRAP) && do_mathemu(regs) == 0) {
  671. emulate_single_step(regs);
  672. return;
  673. }
  674. #endif /* CONFIG_MATH_EMULATION */
  675. if (reason & REASON_FP) {
  676. /* IEEE FP exception */
  677. parse_fpe(regs);
  678. return;
  679. }
  680. if (reason & REASON_TRAP) {
  681. /* trap exception */
  682. if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
  683. == NOTIFY_STOP)
  684. return;
  685. if (debugger_bpt(regs))
  686. return;
  687. if (check_bug_trap(regs)) {
  688. regs->nip += 4;
  689. return;
  690. }
  691. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  692. return;
  693. }
  694. local_irq_enable();
  695. /* Try to emulate it if we should. */
  696. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  697. switch (emulate_instruction(regs)) {
  698. case 0:
  699. regs->nip += 4;
  700. emulate_single_step(regs);
  701. return;
  702. case -EFAULT:
  703. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  704. return;
  705. }
  706. }
  707. if (reason & REASON_PRIVILEGED)
  708. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  709. else
  710. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  711. }
  712. void alignment_exception(struct pt_regs *regs)
  713. {
  714. int fixed = 0;
  715. /* we don't implement logging of alignment exceptions */
  716. if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
  717. fixed = fix_alignment(regs);
  718. if (fixed == 1) {
  719. regs->nip += 4; /* skip over emulated instruction */
  720. emulate_single_step(regs);
  721. return;
  722. }
  723. /* Operand address was bad */
  724. if (fixed == -EFAULT) {
  725. if (user_mode(regs))
  726. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->dar);
  727. else
  728. /* Search exception table */
  729. bad_page_fault(regs, regs->dar, SIGSEGV);
  730. return;
  731. }
  732. _exception(SIGBUS, regs, BUS_ADRALN, regs->dar);
  733. }
  734. void StackOverflow(struct pt_regs *regs)
  735. {
  736. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  737. current, regs->gpr[1]);
  738. debugger(regs);
  739. show_regs(regs);
  740. panic("kernel stack overflow");
  741. }
  742. void nonrecoverable_exception(struct pt_regs *regs)
  743. {
  744. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  745. regs->nip, regs->msr);
  746. debugger(regs);
  747. die("nonrecoverable exception", regs, SIGKILL);
  748. }
  749. void trace_syscall(struct pt_regs *regs)
  750. {
  751. printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
  752. current, current->pid, regs->nip, regs->link, regs->gpr[0],
  753. regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
  754. }
  755. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  756. {
  757. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  758. "%lx at %lx\n", regs->trap, regs->nip);
  759. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  760. }
  761. void altivec_unavailable_exception(struct pt_regs *regs)
  762. {
  763. #if !defined(CONFIG_ALTIVEC)
  764. if (user_mode(regs)) {
  765. /* A user program has executed an altivec instruction,
  766. but this kernel doesn't support altivec. */
  767. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  768. return;
  769. }
  770. #endif
  771. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  772. "%lx at %lx\n", regs->trap, regs->nip);
  773. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  774. }
  775. void performance_monitor_exception(struct pt_regs *regs)
  776. {
  777. perf_irq(regs);
  778. }
  779. #ifdef CONFIG_8xx
  780. void SoftwareEmulation(struct pt_regs *regs)
  781. {
  782. extern int do_mathemu(struct pt_regs *);
  783. extern int Soft_emulate_8xx(struct pt_regs *);
  784. int errcode;
  785. CHECK_FULL_REGS(regs);
  786. if (!user_mode(regs)) {
  787. debugger(regs);
  788. die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
  789. }
  790. #ifdef CONFIG_MATH_EMULATION
  791. errcode = do_mathemu(regs);
  792. #else
  793. errcode = Soft_emulate_8xx(regs);
  794. #endif
  795. if (errcode) {
  796. if (errcode > 0)
  797. _exception(SIGFPE, regs, 0, 0);
  798. else if (errcode == -EFAULT)
  799. _exception(SIGSEGV, regs, 0, 0);
  800. else
  801. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  802. } else
  803. emulate_single_step(regs);
  804. }
  805. #endif /* CONFIG_8xx */
  806. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  807. void DebugException(struct pt_regs *regs, unsigned long debug_status)
  808. {
  809. if (debug_status & DBSR_IC) { /* instruction completion */
  810. regs->msr &= ~MSR_DE;
  811. if (user_mode(regs)) {
  812. current->thread.dbcr0 &= ~DBCR0_IC;
  813. } else {
  814. /* Disable instruction completion */
  815. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  816. /* Clear the instruction completion event */
  817. mtspr(SPRN_DBSR, DBSR_IC);
  818. if (debugger_sstep(regs))
  819. return;
  820. }
  821. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  822. }
  823. }
  824. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  825. #if !defined(CONFIG_TAU_INT)
  826. void TAUException(struct pt_regs *regs)
  827. {
  828. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  829. regs->nip, regs->msr, regs->trap, print_tainted());
  830. }
  831. #endif /* CONFIG_INT_TAU */
  832. #ifdef CONFIG_ALTIVEC
  833. void altivec_assist_exception(struct pt_regs *regs)
  834. {
  835. int err;
  836. if (!user_mode(regs)) {
  837. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  838. " at %lx\n", regs->nip);
  839. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  840. }
  841. flush_altivec_to_thread(current);
  842. err = emulate_altivec(regs);
  843. if (err == 0) {
  844. regs->nip += 4; /* skip emulated instruction */
  845. emulate_single_step(regs);
  846. return;
  847. }
  848. if (err == -EFAULT) {
  849. /* got an error reading the instruction */
  850. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  851. } else {
  852. /* didn't recognize the instruction */
  853. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  854. if (printk_ratelimit())
  855. printk(KERN_ERR "Unrecognized altivec instruction "
  856. "in %s at %lx\n", current->comm, regs->nip);
  857. current->thread.vscr.u[3] |= 0x10000;
  858. }
  859. }
  860. #endif /* CONFIG_ALTIVEC */
  861. #ifdef CONFIG_FSL_BOOKE
  862. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  863. unsigned long error_code)
  864. {
  865. /* We treat cache locking instructions from the user
  866. * as priv ops, in the future we could try to do
  867. * something smarter
  868. */
  869. if (error_code & (ESR_DLK|ESR_ILK))
  870. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  871. return;
  872. }
  873. #endif /* CONFIG_FSL_BOOKE */
  874. #ifdef CONFIG_SPE
  875. void SPEFloatingPointException(struct pt_regs *regs)
  876. {
  877. unsigned long spefscr;
  878. int fpexc_mode;
  879. int code = 0;
  880. spefscr = current->thread.spefscr;
  881. fpexc_mode = current->thread.fpexc_mode;
  882. /* Hardware does not neccessarily set sticky
  883. * underflow/overflow/invalid flags */
  884. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  885. code = FPE_FLTOVF;
  886. spefscr |= SPEFSCR_FOVFS;
  887. }
  888. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  889. code = FPE_FLTUND;
  890. spefscr |= SPEFSCR_FUNFS;
  891. }
  892. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  893. code = FPE_FLTDIV;
  894. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  895. code = FPE_FLTINV;
  896. spefscr |= SPEFSCR_FINVS;
  897. }
  898. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  899. code = FPE_FLTRES;
  900. current->thread.spefscr = spefscr;
  901. _exception(SIGFPE, regs, code, regs->nip);
  902. return;
  903. }
  904. #endif
  905. /*
  906. * We enter here if we get an unrecoverable exception, that is, one
  907. * that happened at a point where the RI (recoverable interrupt) bit
  908. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  909. * we therefore lost state by taking this exception.
  910. */
  911. void unrecoverable_exception(struct pt_regs *regs)
  912. {
  913. printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
  914. regs->trap, regs->nip);
  915. die("Unrecoverable exception", regs, SIGABRT);
  916. }
  917. #ifdef CONFIG_BOOKE_WDT
  918. /*
  919. * Default handler for a Watchdog exception,
  920. * spins until a reboot occurs
  921. */
  922. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  923. {
  924. /* Generic WatchdogHandler, implement your own */
  925. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  926. return;
  927. }
  928. void WatchdogException(struct pt_regs *regs)
  929. {
  930. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  931. WatchdogHandler(regs);
  932. }
  933. #endif
  934. /*
  935. * We enter here if we discover during exception entry that we are
  936. * running in supervisor mode with a userspace value in the stack pointer.
  937. */
  938. void kernel_bad_stack(struct pt_regs *regs)
  939. {
  940. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  941. regs->gpr[1], regs->nip);
  942. die("Bad kernel stack pointer", regs, SIGABRT);
  943. }
  944. void __init trap_init(void)
  945. {
  946. }