traps.c 29 KB

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