traps.c 27 KB

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