traps.c 28 KB

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