traps.c 27 KB

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