traps.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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. static int generic_machine_check_exception(struct pt_regs *regs)
  289. {
  290. unsigned long reason = get_mc_reason(regs);
  291. #if defined(CONFIG_4xx) && !defined(CONFIG_440A)
  292. if (reason & ESR_IMCP) {
  293. printk("Instruction");
  294. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  295. } else
  296. printk("Data");
  297. printk(" machine check in kernel mode.\n");
  298. #elif defined(CONFIG_440A)
  299. printk("Machine check in kernel mode.\n");
  300. if (reason & ESR_IMCP){
  301. printk("Instruction Synchronous Machine Check exception\n");
  302. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  303. }
  304. else {
  305. u32 mcsr = mfspr(SPRN_MCSR);
  306. if (mcsr & MCSR_IB)
  307. printk("Instruction Read PLB Error\n");
  308. if (mcsr & MCSR_DRB)
  309. printk("Data Read PLB Error\n");
  310. if (mcsr & MCSR_DWB)
  311. printk("Data Write PLB Error\n");
  312. if (mcsr & MCSR_TLBP)
  313. printk("TLB Parity Error\n");
  314. if (mcsr & MCSR_ICP){
  315. flush_instruction_cache();
  316. printk("I-Cache Parity Error\n");
  317. }
  318. if (mcsr & MCSR_DCSP)
  319. printk("D-Cache Search Parity Error\n");
  320. if (mcsr & MCSR_DCFP)
  321. printk("D-Cache Flush Parity Error\n");
  322. if (mcsr & MCSR_IMPE)
  323. printk("Machine Check exception is imprecise\n");
  324. /* Clear MCSR */
  325. mtspr(SPRN_MCSR, mcsr);
  326. }
  327. #elif defined (CONFIG_E500)
  328. printk("Machine check in kernel mode.\n");
  329. printk("Caused by (from MCSR=%lx): ", reason);
  330. if (reason & MCSR_MCP)
  331. printk("Machine Check Signal\n");
  332. if (reason & MCSR_ICPERR)
  333. printk("Instruction Cache Parity Error\n");
  334. if (reason & MCSR_DCP_PERR)
  335. printk("Data Cache Push Parity Error\n");
  336. if (reason & MCSR_DCPERR)
  337. printk("Data Cache Parity Error\n");
  338. if (reason & MCSR_BUS_IAERR)
  339. printk("Bus - Instruction Address Error\n");
  340. if (reason & MCSR_BUS_RAERR)
  341. printk("Bus - Read Address Error\n");
  342. if (reason & MCSR_BUS_WAERR)
  343. printk("Bus - Write Address Error\n");
  344. if (reason & MCSR_BUS_IBERR)
  345. printk("Bus - Instruction Data Error\n");
  346. if (reason & MCSR_BUS_RBERR)
  347. printk("Bus - Read Data Bus Error\n");
  348. if (reason & MCSR_BUS_WBERR)
  349. printk("Bus - Read Data Bus Error\n");
  350. if (reason & MCSR_BUS_IPERR)
  351. printk("Bus - Instruction Parity Error\n");
  352. if (reason & MCSR_BUS_RPERR)
  353. printk("Bus - Read Parity Error\n");
  354. #elif defined (CONFIG_E200)
  355. printk("Machine check in kernel mode.\n");
  356. printk("Caused by (from MCSR=%lx): ", reason);
  357. if (reason & MCSR_MCP)
  358. printk("Machine Check Signal\n");
  359. if (reason & MCSR_CP_PERR)
  360. printk("Cache Push Parity Error\n");
  361. if (reason & MCSR_CPERR)
  362. printk("Cache Parity Error\n");
  363. if (reason & MCSR_EXCP_ERR)
  364. printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
  365. if (reason & MCSR_BUS_IRERR)
  366. printk("Bus - Read Bus Error on instruction fetch\n");
  367. if (reason & MCSR_BUS_DRERR)
  368. printk("Bus - Read Bus Error on data load\n");
  369. if (reason & MCSR_BUS_WRERR)
  370. printk("Bus - Write Bus Error on buffered store or cache line push\n");
  371. #else /* !CONFIG_4xx && !CONFIG_E500 && !CONFIG_E200 */
  372. printk("Machine check in kernel mode.\n");
  373. printk("Caused by (from SRR1=%lx): ", reason);
  374. switch (reason & 0x601F0000) {
  375. case 0x80000:
  376. printk("Machine check signal\n");
  377. break;
  378. case 0: /* for 601 */
  379. case 0x40000:
  380. case 0x140000: /* 7450 MSS error and TEA */
  381. printk("Transfer error ack signal\n");
  382. break;
  383. case 0x20000:
  384. printk("Data parity error signal\n");
  385. break;
  386. case 0x10000:
  387. printk("Address parity error signal\n");
  388. break;
  389. case 0x20000000:
  390. printk("L1 Data Cache error\n");
  391. break;
  392. case 0x40000000:
  393. printk("L1 Instruction Cache error\n");
  394. break;
  395. case 0x00100000:
  396. printk("L2 data cache parity error\n");
  397. break;
  398. default:
  399. printk("Unknown values in msr\n");
  400. }
  401. #endif /* CONFIG_4xx */
  402. return 0;
  403. }
  404. void machine_check_exception(struct pt_regs *regs)
  405. {
  406. int recover = 0;
  407. /* See if any machine dependent calls */
  408. if (ppc_md.machine_check_exception)
  409. recover = ppc_md.machine_check_exception(regs);
  410. else
  411. recover = generic_machine_check_exception(regs);
  412. if (recover)
  413. return;
  414. if (user_mode(regs)) {
  415. regs->msr |= MSR_RI;
  416. _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
  417. return;
  418. }
  419. #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
  420. /* the qspan pci read routines can cause machine checks -- Cort */
  421. bad_page_fault(regs, regs->dar, SIGBUS);
  422. return;
  423. #endif
  424. if (debugger_fault_handler(regs)) {
  425. regs->msr |= MSR_RI;
  426. return;
  427. }
  428. if (check_io_access(regs))
  429. return;
  430. if (debugger_fault_handler(regs))
  431. return;
  432. die("Machine check", regs, SIGBUS);
  433. /* Must die if the interrupt is not recoverable */
  434. if (!(regs->msr & MSR_RI))
  435. panic("Unrecoverable Machine check");
  436. }
  437. void SMIException(struct pt_regs *regs)
  438. {
  439. die("System Management Interrupt", regs, SIGABRT);
  440. }
  441. void unknown_exception(struct pt_regs *regs)
  442. {
  443. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  444. regs->nip, regs->msr, regs->trap);
  445. _exception(SIGTRAP, regs, 0, 0);
  446. }
  447. void instruction_breakpoint_exception(struct pt_regs *regs)
  448. {
  449. if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
  450. 5, SIGTRAP) == NOTIFY_STOP)
  451. return;
  452. if (debugger_iabr_match(regs))
  453. return;
  454. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  455. }
  456. void RunModeException(struct pt_regs *regs)
  457. {
  458. _exception(SIGTRAP, regs, 0, 0);
  459. }
  460. void __kprobes single_step_exception(struct pt_regs *regs)
  461. {
  462. regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
  463. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  464. 5, SIGTRAP) == NOTIFY_STOP)
  465. return;
  466. if (debugger_sstep(regs))
  467. return;
  468. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  469. }
  470. /*
  471. * After we have successfully emulated an instruction, we have to
  472. * check if the instruction was being single-stepped, and if so,
  473. * pretend we got a single-step exception. This was pointed out
  474. * by Kumar Gala. -- paulus
  475. */
  476. static void emulate_single_step(struct pt_regs *regs)
  477. {
  478. if (single_stepping(regs)) {
  479. clear_single_step(regs);
  480. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  481. }
  482. }
  483. static inline int __parse_fpscr(unsigned long fpscr)
  484. {
  485. int ret = 0;
  486. /* Invalid operation */
  487. if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
  488. ret = FPE_FLTINV;
  489. /* Overflow */
  490. else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
  491. ret = FPE_FLTOVF;
  492. /* Underflow */
  493. else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
  494. ret = FPE_FLTUND;
  495. /* Divide by zero */
  496. else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
  497. ret = FPE_FLTDIV;
  498. /* Inexact result */
  499. else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
  500. ret = FPE_FLTRES;
  501. return ret;
  502. }
  503. static void parse_fpe(struct pt_regs *regs)
  504. {
  505. int code = 0;
  506. flush_fp_to_thread(current);
  507. code = __parse_fpscr(current->thread.fpscr.val);
  508. _exception(SIGFPE, regs, code, regs->nip);
  509. }
  510. /*
  511. * Illegal instruction emulation support. Originally written to
  512. * provide the PVR to user applications using the mfspr rd, PVR.
  513. * Return non-zero if we can't emulate, or -EFAULT if the associated
  514. * memory access caused an access fault. Return zero on success.
  515. *
  516. * There are a couple of ways to do this, either "decode" the instruction
  517. * or directly match lots of bits. In this case, matching lots of
  518. * bits is faster and easier.
  519. *
  520. */
  521. #define INST_MFSPR_PVR 0x7c1f42a6
  522. #define INST_MFSPR_PVR_MASK 0xfc1fffff
  523. #define INST_DCBA 0x7c0005ec
  524. #define INST_DCBA_MASK 0xfc0007fe
  525. #define INST_MCRXR 0x7c000400
  526. #define INST_MCRXR_MASK 0xfc0007fe
  527. #define INST_STRING 0x7c00042a
  528. #define INST_STRING_MASK 0xfc0007fe
  529. #define INST_STRING_GEN_MASK 0xfc00067e
  530. #define INST_LSWI 0x7c0004aa
  531. #define INST_LSWX 0x7c00042a
  532. #define INST_STSWI 0x7c0005aa
  533. #define INST_STSWX 0x7c00052a
  534. #define INST_POPCNTB 0x7c0000f4
  535. #define INST_POPCNTB_MASK 0xfc0007fe
  536. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  537. {
  538. u8 rT = (instword >> 21) & 0x1f;
  539. u8 rA = (instword >> 16) & 0x1f;
  540. u8 NB_RB = (instword >> 11) & 0x1f;
  541. u32 num_bytes;
  542. unsigned long EA;
  543. int pos = 0;
  544. /* Early out if we are an invalid form of lswx */
  545. if ((instword & INST_STRING_MASK) == INST_LSWX)
  546. if ((rT == rA) || (rT == NB_RB))
  547. return -EINVAL;
  548. EA = (rA == 0) ? 0 : regs->gpr[rA];
  549. switch (instword & INST_STRING_MASK) {
  550. case INST_LSWX:
  551. case INST_STSWX:
  552. EA += NB_RB;
  553. num_bytes = regs->xer & 0x7f;
  554. break;
  555. case INST_LSWI:
  556. case INST_STSWI:
  557. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  558. break;
  559. default:
  560. return -EINVAL;
  561. }
  562. while (num_bytes != 0)
  563. {
  564. u8 val;
  565. u32 shift = 8 * (3 - (pos & 0x3));
  566. switch ((instword & INST_STRING_MASK)) {
  567. case INST_LSWX:
  568. case INST_LSWI:
  569. if (get_user(val, (u8 __user *)EA))
  570. return -EFAULT;
  571. /* first time updating this reg,
  572. * zero it out */
  573. if (pos == 0)
  574. regs->gpr[rT] = 0;
  575. regs->gpr[rT] |= val << shift;
  576. break;
  577. case INST_STSWI:
  578. case INST_STSWX:
  579. val = regs->gpr[rT] >> shift;
  580. if (put_user(val, (u8 __user *)EA))
  581. return -EFAULT;
  582. break;
  583. }
  584. /* move EA to next address */
  585. EA += 1;
  586. num_bytes--;
  587. /* manage our position within the register */
  588. if (++pos == 4) {
  589. pos = 0;
  590. if (++rT == 32)
  591. rT = 0;
  592. }
  593. }
  594. return 0;
  595. }
  596. static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
  597. {
  598. u32 ra,rs;
  599. unsigned long tmp;
  600. ra = (instword >> 16) & 0x1f;
  601. rs = (instword >> 21) & 0x1f;
  602. tmp = regs->gpr[rs];
  603. tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
  604. tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
  605. tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
  606. regs->gpr[ra] = tmp;
  607. return 0;
  608. }
  609. static int emulate_instruction(struct pt_regs *regs)
  610. {
  611. u32 instword;
  612. u32 rd;
  613. if (!user_mode(regs) || (regs->msr & MSR_LE))
  614. return -EINVAL;
  615. CHECK_FULL_REGS(regs);
  616. if (get_user(instword, (u32 __user *)(regs->nip)))
  617. return -EFAULT;
  618. /* Emulate the mfspr rD, PVR. */
  619. if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
  620. rd = (instword >> 21) & 0x1f;
  621. regs->gpr[rd] = mfspr(SPRN_PVR);
  622. return 0;
  623. }
  624. /* Emulating the dcba insn is just a no-op. */
  625. if ((instword & INST_DCBA_MASK) == INST_DCBA)
  626. return 0;
  627. /* Emulate the mcrxr insn. */
  628. if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
  629. int shift = (instword >> 21) & 0x1c;
  630. unsigned long msk = 0xf0000000UL >> shift;
  631. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  632. regs->xer &= ~0xf0000000UL;
  633. return 0;
  634. }
  635. /* Emulate load/store string insn. */
  636. if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
  637. return emulate_string_inst(regs, instword);
  638. /* Emulate the popcntb (Population Count Bytes) instruction. */
  639. if ((instword & INST_POPCNTB_MASK) == INST_POPCNTB) {
  640. return emulate_popcntb_inst(regs, instword);
  641. }
  642. return -EINVAL;
  643. }
  644. int is_valid_bugaddr(unsigned long addr)
  645. {
  646. return is_kernel_addr(addr);
  647. }
  648. void __kprobes program_check_exception(struct pt_regs *regs)
  649. {
  650. unsigned int reason = get_reason(regs);
  651. extern int do_mathemu(struct pt_regs *regs);
  652. /* We can now get here via a FP Unavailable exception if the core
  653. * has no FPU, in that case the reason flags will be 0 */
  654. if (reason & REASON_FP) {
  655. /* IEEE FP exception */
  656. parse_fpe(regs);
  657. return;
  658. }
  659. if (reason & REASON_TRAP) {
  660. /* trap exception */
  661. if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
  662. == NOTIFY_STOP)
  663. return;
  664. if (debugger_bpt(regs))
  665. return;
  666. if (!(regs->msr & MSR_PR) && /* not user-mode */
  667. report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
  668. regs->nip += 4;
  669. return;
  670. }
  671. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  672. return;
  673. }
  674. local_irq_enable();
  675. #ifdef CONFIG_MATH_EMULATION
  676. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  677. * but there seems to be a hardware bug on the 405GP (RevD)
  678. * that means ESR is sometimes set incorrectly - either to
  679. * ESR_DST (!?) or 0. In the process of chasing this with the
  680. * hardware people - not sure if it can happen on any illegal
  681. * instruction or only on FP instructions, whether there is a
  682. * pattern to occurences etc. -dgibson 31/Mar/2003 */
  683. switch (do_mathemu(regs)) {
  684. case 0:
  685. emulate_single_step(regs);
  686. return;
  687. case 1: {
  688. int code = 0;
  689. code = __parse_fpscr(current->thread.fpscr.val);
  690. _exception(SIGFPE, regs, code, regs->nip);
  691. return;
  692. }
  693. case -EFAULT:
  694. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  695. return;
  696. }
  697. /* fall through on any other errors */
  698. #endif /* CONFIG_MATH_EMULATION */
  699. /* Try to emulate it if we should. */
  700. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  701. switch (emulate_instruction(regs)) {
  702. case 0:
  703. regs->nip += 4;
  704. emulate_single_step(regs);
  705. return;
  706. case -EFAULT:
  707. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  708. return;
  709. }
  710. }
  711. if (reason & REASON_PRIVILEGED)
  712. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  713. else
  714. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  715. }
  716. void alignment_exception(struct pt_regs *regs)
  717. {
  718. int sig, code, fixed = 0;
  719. /* we don't implement logging of alignment exceptions */
  720. if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
  721. fixed = fix_alignment(regs);
  722. if (fixed == 1) {
  723. regs->nip += 4; /* skip over emulated instruction */
  724. emulate_single_step(regs);
  725. return;
  726. }
  727. /* Operand address was bad */
  728. if (fixed == -EFAULT) {
  729. sig = SIGSEGV;
  730. code = SEGV_ACCERR;
  731. } else {
  732. sig = SIGBUS;
  733. code = BUS_ADRALN;
  734. }
  735. if (user_mode(regs))
  736. _exception(sig, regs, code, regs->dar);
  737. else
  738. bad_page_fault(regs, regs->dar, sig);
  739. }
  740. void StackOverflow(struct pt_regs *regs)
  741. {
  742. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  743. current, regs->gpr[1]);
  744. debugger(regs);
  745. show_regs(regs);
  746. panic("kernel stack overflow");
  747. }
  748. void nonrecoverable_exception(struct pt_regs *regs)
  749. {
  750. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  751. regs->nip, regs->msr);
  752. debugger(regs);
  753. die("nonrecoverable exception", regs, SIGKILL);
  754. }
  755. void trace_syscall(struct pt_regs *regs)
  756. {
  757. printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
  758. current, current->pid, regs->nip, regs->link, regs->gpr[0],
  759. regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
  760. }
  761. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  762. {
  763. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  764. "%lx at %lx\n", regs->trap, regs->nip);
  765. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  766. }
  767. void altivec_unavailable_exception(struct pt_regs *regs)
  768. {
  769. if (user_mode(regs)) {
  770. /* A user program has executed an altivec instruction,
  771. but this kernel doesn't support altivec. */
  772. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  773. return;
  774. }
  775. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  776. "%lx at %lx\n", regs->trap, regs->nip);
  777. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  778. }
  779. void performance_monitor_exception(struct pt_regs *regs)
  780. {
  781. perf_irq(regs);
  782. }
  783. #ifdef CONFIG_8xx
  784. void SoftwareEmulation(struct pt_regs *regs)
  785. {
  786. extern int do_mathemu(struct pt_regs *);
  787. extern int Soft_emulate_8xx(struct pt_regs *);
  788. int errcode;
  789. CHECK_FULL_REGS(regs);
  790. if (!user_mode(regs)) {
  791. debugger(regs);
  792. die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
  793. }
  794. #ifdef CONFIG_MATH_EMULATION
  795. errcode = do_mathemu(regs);
  796. switch (errcode) {
  797. case 0:
  798. emulate_single_step(regs);
  799. return;
  800. case 1: {
  801. int code = 0;
  802. code = __parse_fpscr(current->thread.fpscr.val);
  803. _exception(SIGFPE, regs, code, regs->nip);
  804. return;
  805. }
  806. case -EFAULT:
  807. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  808. return;
  809. default:
  810. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  811. return;
  812. }
  813. #else
  814. errcode = Soft_emulate_8xx(regs);
  815. switch (errcode) {
  816. case 0:
  817. emulate_single_step(regs);
  818. return;
  819. case 1:
  820. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  821. return;
  822. case -EFAULT:
  823. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  824. return;
  825. }
  826. #endif
  827. }
  828. #endif /* CONFIG_8xx */
  829. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  830. void DebugException(struct pt_regs *regs, unsigned long debug_status)
  831. {
  832. if (debug_status & DBSR_IC) { /* instruction completion */
  833. regs->msr &= ~MSR_DE;
  834. if (user_mode(regs)) {
  835. current->thread.dbcr0 &= ~DBCR0_IC;
  836. } else {
  837. /* Disable instruction completion */
  838. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  839. /* Clear the instruction completion event */
  840. mtspr(SPRN_DBSR, DBSR_IC);
  841. if (debugger_sstep(regs))
  842. return;
  843. }
  844. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  845. }
  846. }
  847. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  848. #if !defined(CONFIG_TAU_INT)
  849. void TAUException(struct pt_regs *regs)
  850. {
  851. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  852. regs->nip, regs->msr, regs->trap, print_tainted());
  853. }
  854. #endif /* CONFIG_INT_TAU */
  855. #ifdef CONFIG_ALTIVEC
  856. void altivec_assist_exception(struct pt_regs *regs)
  857. {
  858. int err;
  859. if (!user_mode(regs)) {
  860. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  861. " at %lx\n", regs->nip);
  862. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  863. }
  864. flush_altivec_to_thread(current);
  865. err = emulate_altivec(regs);
  866. if (err == 0) {
  867. regs->nip += 4; /* skip emulated instruction */
  868. emulate_single_step(regs);
  869. return;
  870. }
  871. if (err == -EFAULT) {
  872. /* got an error reading the instruction */
  873. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  874. } else {
  875. /* didn't recognize the instruction */
  876. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  877. if (printk_ratelimit())
  878. printk(KERN_ERR "Unrecognized altivec instruction "
  879. "in %s at %lx\n", current->comm, regs->nip);
  880. current->thread.vscr.u[3] |= 0x10000;
  881. }
  882. }
  883. #endif /* CONFIG_ALTIVEC */
  884. #ifdef CONFIG_FSL_BOOKE
  885. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  886. unsigned long error_code)
  887. {
  888. /* We treat cache locking instructions from the user
  889. * as priv ops, in the future we could try to do
  890. * something smarter
  891. */
  892. if (error_code & (ESR_DLK|ESR_ILK))
  893. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  894. return;
  895. }
  896. #endif /* CONFIG_FSL_BOOKE */
  897. #ifdef CONFIG_SPE
  898. void SPEFloatingPointException(struct pt_regs *regs)
  899. {
  900. unsigned long spefscr;
  901. int fpexc_mode;
  902. int code = 0;
  903. spefscr = current->thread.spefscr;
  904. fpexc_mode = current->thread.fpexc_mode;
  905. /* Hardware does not neccessarily set sticky
  906. * underflow/overflow/invalid flags */
  907. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  908. code = FPE_FLTOVF;
  909. spefscr |= SPEFSCR_FOVFS;
  910. }
  911. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  912. code = FPE_FLTUND;
  913. spefscr |= SPEFSCR_FUNFS;
  914. }
  915. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  916. code = FPE_FLTDIV;
  917. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  918. code = FPE_FLTINV;
  919. spefscr |= SPEFSCR_FINVS;
  920. }
  921. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  922. code = FPE_FLTRES;
  923. current->thread.spefscr = spefscr;
  924. _exception(SIGFPE, regs, code, regs->nip);
  925. return;
  926. }
  927. #endif
  928. /*
  929. * We enter here if we get an unrecoverable exception, that is, one
  930. * that happened at a point where the RI (recoverable interrupt) bit
  931. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  932. * we therefore lost state by taking this exception.
  933. */
  934. void unrecoverable_exception(struct pt_regs *regs)
  935. {
  936. printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
  937. regs->trap, regs->nip);
  938. die("Unrecoverable exception", regs, SIGABRT);
  939. }
  940. #ifdef CONFIG_BOOKE_WDT
  941. /*
  942. * Default handler for a Watchdog exception,
  943. * spins until a reboot occurs
  944. */
  945. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  946. {
  947. /* Generic WatchdogHandler, implement your own */
  948. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  949. return;
  950. }
  951. void WatchdogException(struct pt_regs *regs)
  952. {
  953. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  954. WatchdogHandler(regs);
  955. }
  956. #endif
  957. /*
  958. * We enter here if we discover during exception entry that we are
  959. * running in supervisor mode with a userspace value in the stack pointer.
  960. */
  961. void kernel_bad_stack(struct pt_regs *regs)
  962. {
  963. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  964. regs->gpr[1], regs->nip);
  965. die("Bad kernel stack pointer", regs, SIGABRT);
  966. }
  967. void __init trap_init(void)
  968. {
  969. }