traps.c 28 KB

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