traps.c 27 KB

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