traps.c 29 KB

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