traps.c 26 KB

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