traps.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /*
  2. * arch/ppc/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@cs.anu.edu.au)
  13. */
  14. /*
  15. * This file handles the architecture-dependent parts of hardware exceptions
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/sched.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/stddef.h>
  22. #include <linux/unistd.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/slab.h>
  25. #include <linux/user.h>
  26. #include <linux/a.out.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/config.h>
  29. #include <linux/init.h>
  30. #include <linux/module.h>
  31. #include <linux/prctl.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/system.h>
  35. #include <asm/io.h>
  36. #include <asm/reg.h>
  37. #include <asm/xmon.h>
  38. #ifdef CONFIG_PMAC_BACKLIGHT
  39. #include <asm/backlight.h>
  40. #endif
  41. #include <asm/perfmon.h>
  42. #ifdef CONFIG_XMON
  43. void (*debugger)(struct pt_regs *regs) = xmon;
  44. int (*debugger_bpt)(struct pt_regs *regs) = xmon_bpt;
  45. int (*debugger_sstep)(struct pt_regs *regs) = xmon_sstep;
  46. int (*debugger_iabr_match)(struct pt_regs *regs) = xmon_iabr_match;
  47. int (*debugger_dabr_match)(struct pt_regs *regs) = xmon_dabr_match;
  48. void (*debugger_fault_handler)(struct pt_regs *regs);
  49. #else
  50. #ifdef CONFIG_KGDB
  51. void (*debugger)(struct pt_regs *regs);
  52. int (*debugger_bpt)(struct pt_regs *regs);
  53. int (*debugger_sstep)(struct pt_regs *regs);
  54. int (*debugger_iabr_match)(struct pt_regs *regs);
  55. int (*debugger_dabr_match)(struct pt_regs *regs);
  56. void (*debugger_fault_handler)(struct pt_regs *regs);
  57. #else
  58. #define debugger(regs) do { } while (0)
  59. #define debugger_bpt(regs) 0
  60. #define debugger_sstep(regs) 0
  61. #define debugger_iabr_match(regs) 0
  62. #define debugger_dabr_match(regs) 0
  63. #define debugger_fault_handler ((void (*)(struct pt_regs *))0)
  64. #endif
  65. #endif
  66. /*
  67. * Trap & Exception support
  68. */
  69. DEFINE_SPINLOCK(die_lock);
  70. void die(const char * str, struct pt_regs * fp, long err)
  71. {
  72. static int die_counter;
  73. int nl = 0;
  74. console_verbose();
  75. spin_lock_irq(&die_lock);
  76. #ifdef CONFIG_PMAC_BACKLIGHT
  77. if (_machine == _MACH_Pmac) {
  78. set_backlight_enable(1);
  79. set_backlight_level(BACKLIGHT_MAX);
  80. }
  81. #endif
  82. printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
  83. #ifdef CONFIG_PREEMPT
  84. printk("PREEMPT ");
  85. nl = 1;
  86. #endif
  87. #ifdef CONFIG_SMP
  88. printk("SMP NR_CPUS=%d ", NR_CPUS);
  89. nl = 1;
  90. #endif
  91. if (nl)
  92. printk("\n");
  93. show_regs(fp);
  94. spin_unlock_irq(&die_lock);
  95. /* do_exit() should take care of panic'ing from an interrupt
  96. * context so we don't handle it here
  97. */
  98. do_exit(err);
  99. }
  100. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  101. {
  102. siginfo_t info;
  103. if (!user_mode(regs)) {
  104. debugger(regs);
  105. die("Exception in kernel mode", regs, signr);
  106. }
  107. info.si_signo = signr;
  108. info.si_errno = 0;
  109. info.si_code = code;
  110. info.si_addr = (void __user *) addr;
  111. force_sig_info(signr, &info, current);
  112. }
  113. /*
  114. * I/O accesses can cause machine checks on powermacs.
  115. * Check if the NIP corresponds to the address of a sync
  116. * instruction for which there is an entry in the exception
  117. * table.
  118. * Note that the 601 only takes a machine check on TEA
  119. * (transfer error ack) signal assertion, and does not
  120. * set any of the top 16 bits of SRR1.
  121. * -- paulus.
  122. */
  123. static inline int check_io_access(struct pt_regs *regs)
  124. {
  125. #ifdef CONFIG_PPC_PMAC
  126. unsigned long msr = regs->msr;
  127. const struct exception_table_entry *entry;
  128. unsigned int *nip = (unsigned int *)regs->nip;
  129. if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
  130. && (entry = search_exception_tables(regs->nip)) != NULL) {
  131. /*
  132. * Check that it's a sync instruction, or somewhere
  133. * in the twi; isync; nop sequence that inb/inw/inl uses.
  134. * As the address is in the exception table
  135. * we should be able to read the instr there.
  136. * For the debug message, we look at the preceding
  137. * load or store.
  138. */
  139. if (*nip == 0x60000000) /* nop */
  140. nip -= 2;
  141. else if (*nip == 0x4c00012c) /* isync */
  142. --nip;
  143. if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
  144. /* sync or twi */
  145. unsigned int rb;
  146. --nip;
  147. rb = (*nip >> 11) & 0x1f;
  148. printk(KERN_DEBUG "%s bad port %lx at %p\n",
  149. (*nip & 0x100)? "OUT to": "IN from",
  150. regs->gpr[rb] - _IO_BASE, nip);
  151. regs->msr |= MSR_RI;
  152. regs->nip = entry->fixup;
  153. return 1;
  154. }
  155. }
  156. #endif /* CONFIG_PPC_PMAC */
  157. return 0;
  158. }
  159. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  160. /* On 4xx, the reason for the machine check or program exception
  161. is in the ESR. */
  162. #define get_reason(regs) ((regs)->dsisr)
  163. #ifndef CONFIG_E500
  164. #define get_mc_reason(regs) ((regs)->dsisr)
  165. #else
  166. #define get_mc_reason(regs) (mfspr(SPRN_MCSR))
  167. #endif
  168. #define REASON_FP ESR_FP
  169. #define REASON_ILLEGAL ESR_PIL
  170. #define REASON_PRIVILEGED ESR_PPR
  171. #define REASON_TRAP ESR_PTR
  172. /* single-step stuff */
  173. #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
  174. #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
  175. #else
  176. /* On non-4xx, the reason for the machine check or program
  177. exception is in the MSR. */
  178. #define get_reason(regs) ((regs)->msr)
  179. #define get_mc_reason(regs) ((regs)->msr)
  180. #define REASON_FP 0x100000
  181. #define REASON_ILLEGAL 0x80000
  182. #define REASON_PRIVILEGED 0x40000
  183. #define REASON_TRAP 0x20000
  184. #define single_stepping(regs) ((regs)->msr & MSR_SE)
  185. #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
  186. #endif
  187. /*
  188. * This is "fall-back" implementation for configurations
  189. * which don't provide platform-specific machine check info
  190. */
  191. void __attribute__ ((weak))
  192. platform_machine_check(struct pt_regs *regs)
  193. {
  194. }
  195. void MachineCheckException(struct pt_regs *regs)
  196. {
  197. unsigned long reason = get_mc_reason(regs);
  198. if (user_mode(regs)) {
  199. regs->msr |= MSR_RI;
  200. _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
  201. return;
  202. }
  203. #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
  204. /* the qspan pci read routines can cause machine checks -- Cort */
  205. bad_page_fault(regs, regs->dar, SIGBUS);
  206. return;
  207. #endif
  208. if (debugger_fault_handler) {
  209. debugger_fault_handler(regs);
  210. regs->msr |= MSR_RI;
  211. return;
  212. }
  213. if (check_io_access(regs))
  214. return;
  215. #if defined(CONFIG_4xx) && !defined(CONFIG_440A)
  216. if (reason & ESR_IMCP) {
  217. printk("Instruction");
  218. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  219. } else
  220. printk("Data");
  221. printk(" machine check in kernel mode.\n");
  222. #elif defined(CONFIG_440A)
  223. printk("Machine check in kernel mode.\n");
  224. if (reason & ESR_IMCP){
  225. printk("Instruction Synchronous Machine Check exception\n");
  226. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  227. }
  228. else {
  229. u32 mcsr = mfspr(SPRN_MCSR);
  230. if (mcsr & MCSR_IB)
  231. printk("Instruction Read PLB Error\n");
  232. if (mcsr & MCSR_DRB)
  233. printk("Data Read PLB Error\n");
  234. if (mcsr & MCSR_DWB)
  235. printk("Data Write PLB Error\n");
  236. if (mcsr & MCSR_TLBP)
  237. printk("TLB Parity Error\n");
  238. if (mcsr & MCSR_ICP){
  239. flush_instruction_cache();
  240. printk("I-Cache Parity Error\n");
  241. }
  242. if (mcsr & MCSR_DCSP)
  243. printk("D-Cache Search Parity Error\n");
  244. if (mcsr & MCSR_DCFP)
  245. printk("D-Cache Flush Parity Error\n");
  246. if (mcsr & MCSR_IMPE)
  247. printk("Machine Check exception is imprecise\n");
  248. /* Clear MCSR */
  249. mtspr(SPRN_MCSR, mcsr);
  250. }
  251. #elif defined (CONFIG_E500)
  252. printk("Machine check in kernel mode.\n");
  253. printk("Caused by (from MCSR=%lx): ", reason);
  254. if (reason & MCSR_MCP)
  255. printk("Machine Check Signal\n");
  256. if (reason & MCSR_ICPERR)
  257. printk("Instruction Cache Parity Error\n");
  258. if (reason & MCSR_DCP_PERR)
  259. printk("Data Cache Push Parity Error\n");
  260. if (reason & MCSR_DCPERR)
  261. printk("Data Cache Parity Error\n");
  262. if (reason & MCSR_GL_CI)
  263. printk("Guarded Load or Cache-Inhibited stwcx.\n");
  264. if (reason & MCSR_BUS_IAERR)
  265. printk("Bus - Instruction Address Error\n");
  266. if (reason & MCSR_BUS_RAERR)
  267. printk("Bus - Read Address Error\n");
  268. if (reason & MCSR_BUS_WAERR)
  269. printk("Bus - Write Address Error\n");
  270. if (reason & MCSR_BUS_IBERR)
  271. printk("Bus - Instruction Data Error\n");
  272. if (reason & MCSR_BUS_RBERR)
  273. printk("Bus - Read Data Bus Error\n");
  274. if (reason & MCSR_BUS_WBERR)
  275. printk("Bus - Read Data Bus Error\n");
  276. if (reason & MCSR_BUS_IPERR)
  277. printk("Bus - Instruction Parity Error\n");
  278. if (reason & MCSR_BUS_RPERR)
  279. printk("Bus - Read Parity Error\n");
  280. #else /* !CONFIG_4xx && !CONFIG_E500 */
  281. printk("Machine check in kernel mode.\n");
  282. printk("Caused by (from SRR1=%lx): ", reason);
  283. switch (reason & 0x601F0000) {
  284. case 0x80000:
  285. printk("Machine check signal\n");
  286. break;
  287. case 0: /* for 601 */
  288. case 0x40000:
  289. case 0x140000: /* 7450 MSS error and TEA */
  290. printk("Transfer error ack signal\n");
  291. break;
  292. case 0x20000:
  293. printk("Data parity error signal\n");
  294. break;
  295. case 0x10000:
  296. printk("Address parity error signal\n");
  297. break;
  298. case 0x20000000:
  299. printk("L1 Data Cache error\n");
  300. break;
  301. case 0x40000000:
  302. printk("L1 Instruction Cache error\n");
  303. break;
  304. case 0x00100000:
  305. printk("L2 data cache parity error\n");
  306. break;
  307. default:
  308. printk("Unknown values in msr\n");
  309. }
  310. #endif /* CONFIG_4xx */
  311. /*
  312. * Optional platform-provided routine to print out
  313. * additional info, e.g. bus error registers.
  314. */
  315. platform_machine_check(regs);
  316. debugger(regs);
  317. die("machine check", regs, SIGBUS);
  318. }
  319. void SMIException(struct pt_regs *regs)
  320. {
  321. debugger(regs);
  322. #if !(defined(CONFIG_XMON) || defined(CONFIG_KGDB))
  323. show_regs(regs);
  324. panic("System Management Interrupt");
  325. #endif
  326. }
  327. void UnknownException(struct pt_regs *regs)
  328. {
  329. printk("Bad trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  330. regs->nip, regs->msr, regs->trap, print_tainted());
  331. _exception(SIGTRAP, regs, 0, 0);
  332. }
  333. void InstructionBreakpoint(struct pt_regs *regs)
  334. {
  335. if (debugger_iabr_match(regs))
  336. return;
  337. _exception(SIGTRAP, regs, TRAP_BRKPT, 0);
  338. }
  339. void RunModeException(struct pt_regs *regs)
  340. {
  341. _exception(SIGTRAP, regs, 0, 0);
  342. }
  343. /* Illegal instruction emulation support. Originally written to
  344. * provide the PVR to user applications using the mfspr rd, PVR.
  345. * Return non-zero if we can't emulate, or -EFAULT if the associated
  346. * memory access caused an access fault. Return zero on success.
  347. *
  348. * There are a couple of ways to do this, either "decode" the instruction
  349. * or directly match lots of bits. In this case, matching lots of
  350. * bits is faster and easier.
  351. *
  352. */
  353. #define INST_MFSPR_PVR 0x7c1f42a6
  354. #define INST_MFSPR_PVR_MASK 0xfc1fffff
  355. #define INST_DCBA 0x7c0005ec
  356. #define INST_DCBA_MASK 0x7c0007fe
  357. #define INST_MCRXR 0x7c000400
  358. #define INST_MCRXR_MASK 0x7c0007fe
  359. #define INST_STRING 0x7c00042a
  360. #define INST_STRING_MASK 0x7c0007fe
  361. #define INST_STRING_GEN_MASK 0x7c00067e
  362. #define INST_LSWI 0x7c0004aa
  363. #define INST_LSWX 0x7c00042a
  364. #define INST_STSWI 0x7c0005aa
  365. #define INST_STSWX 0x7c00052a
  366. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  367. {
  368. u8 rT = (instword >> 21) & 0x1f;
  369. u8 rA = (instword >> 16) & 0x1f;
  370. u8 NB_RB = (instword >> 11) & 0x1f;
  371. u32 num_bytes;
  372. unsigned long EA;
  373. int pos = 0;
  374. /* Early out if we are an invalid form of lswx */
  375. if ((instword & INST_STRING_MASK) == INST_LSWX)
  376. if ((rT == rA) || (rT == NB_RB))
  377. return -EINVAL;
  378. EA = (rA == 0) ? 0 : regs->gpr[rA];
  379. switch (instword & INST_STRING_MASK) {
  380. case INST_LSWX:
  381. case INST_STSWX:
  382. EA += NB_RB;
  383. num_bytes = regs->xer & 0x7f;
  384. break;
  385. case INST_LSWI:
  386. case INST_STSWI:
  387. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  388. break;
  389. default:
  390. return -EINVAL;
  391. }
  392. while (num_bytes != 0)
  393. {
  394. u8 val;
  395. u32 shift = 8 * (3 - (pos & 0x3));
  396. switch ((instword & INST_STRING_MASK)) {
  397. case INST_LSWX:
  398. case INST_LSWI:
  399. if (get_user(val, (u8 __user *)EA))
  400. return -EFAULT;
  401. /* first time updating this reg,
  402. * zero it out */
  403. if (pos == 0)
  404. regs->gpr[rT] = 0;
  405. regs->gpr[rT] |= val << shift;
  406. break;
  407. case INST_STSWI:
  408. case INST_STSWX:
  409. val = regs->gpr[rT] >> shift;
  410. if (put_user(val, (u8 __user *)EA))
  411. return -EFAULT;
  412. break;
  413. }
  414. /* move EA to next address */
  415. EA += 1;
  416. num_bytes--;
  417. /* manage our position within the register */
  418. if (++pos == 4) {
  419. pos = 0;
  420. if (++rT == 32)
  421. rT = 0;
  422. }
  423. }
  424. return 0;
  425. }
  426. static int emulate_instruction(struct pt_regs *regs)
  427. {
  428. u32 instword;
  429. u32 rd;
  430. if (!user_mode(regs))
  431. return -EINVAL;
  432. CHECK_FULL_REGS(regs);
  433. if (get_user(instword, (u32 __user *)(regs->nip)))
  434. return -EFAULT;
  435. /* Emulate the mfspr rD, PVR.
  436. */
  437. if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
  438. rd = (instword >> 21) & 0x1f;
  439. regs->gpr[rd] = mfspr(SPRN_PVR);
  440. return 0;
  441. }
  442. /* Emulating the dcba insn is just a no-op. */
  443. if ((instword & INST_DCBA_MASK) == INST_DCBA)
  444. return 0;
  445. /* Emulate the mcrxr insn. */
  446. if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
  447. int shift = (instword >> 21) & 0x1c;
  448. unsigned long msk = 0xf0000000UL >> shift;
  449. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  450. regs->xer &= ~0xf0000000UL;
  451. return 0;
  452. }
  453. /* Emulate load/store string insn. */
  454. if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
  455. return emulate_string_inst(regs, instword);
  456. return -EINVAL;
  457. }
  458. /*
  459. * After we have successfully emulated an instruction, we have to
  460. * check if the instruction was being single-stepped, and if so,
  461. * pretend we got a single-step exception. This was pointed out
  462. * by Kumar Gala. -- paulus
  463. */
  464. static void emulate_single_step(struct pt_regs *regs)
  465. {
  466. if (single_stepping(regs)) {
  467. clear_single_step(regs);
  468. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  469. }
  470. }
  471. /*
  472. * Look through the list of trap instructions that are used for BUG(),
  473. * BUG_ON() and WARN_ON() and see if we hit one. At this point we know
  474. * that the exception was caused by a trap instruction of some kind.
  475. * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
  476. * otherwise.
  477. */
  478. extern struct bug_entry __start___bug_table[], __stop___bug_table[];
  479. #ifndef CONFIG_MODULES
  480. #define module_find_bug(x) NULL
  481. #endif
  482. static struct bug_entry *find_bug(unsigned long bugaddr)
  483. {
  484. struct bug_entry *bug;
  485. for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
  486. if (bugaddr == bug->bug_addr)
  487. return bug;
  488. return module_find_bug(bugaddr);
  489. }
  490. int check_bug_trap(struct pt_regs *regs)
  491. {
  492. struct bug_entry *bug;
  493. unsigned long addr;
  494. if (regs->msr & MSR_PR)
  495. return 0; /* not in kernel */
  496. addr = regs->nip; /* address of trap instruction */
  497. if (addr < PAGE_OFFSET)
  498. return 0;
  499. bug = find_bug(regs->nip);
  500. if (bug == NULL)
  501. return 0;
  502. if (bug->line & BUG_WARNING_TRAP) {
  503. /* this is a WARN_ON rather than BUG/BUG_ON */
  504. #ifdef CONFIG_XMON
  505. xmon_printf(KERN_ERR "Badness in %s at %s:%d\n",
  506. bug->function, bug->file,
  507. bug->line & ~BUG_WARNING_TRAP);
  508. #endif /* CONFIG_XMON */
  509. printk(KERN_ERR "Badness in %s at %s:%d\n",
  510. bug->function, bug->file,
  511. bug->line & ~BUG_WARNING_TRAP);
  512. dump_stack();
  513. return 1;
  514. }
  515. #ifdef CONFIG_XMON
  516. xmon_printf(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
  517. bug->function, bug->file, bug->line);
  518. xmon(regs);
  519. #endif /* CONFIG_XMON */
  520. printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
  521. bug->function, bug->file, bug->line);
  522. return 0;
  523. }
  524. void ProgramCheckException(struct pt_regs *regs)
  525. {
  526. unsigned int reason = get_reason(regs);
  527. extern int do_mathemu(struct pt_regs *regs);
  528. #ifdef CONFIG_MATH_EMULATION
  529. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  530. * but there seems to be a hardware bug on the 405GP (RevD)
  531. * that means ESR is sometimes set incorrectly - either to
  532. * ESR_DST (!?) or 0. In the process of chasing this with the
  533. * hardware people - not sure if it can happen on any illegal
  534. * instruction or only on FP instructions, whether there is a
  535. * pattern to occurences etc. -dgibson 31/Mar/2003 */
  536. if (!(reason & REASON_TRAP) && do_mathemu(regs) == 0) {
  537. emulate_single_step(regs);
  538. return;
  539. }
  540. #endif /* CONFIG_MATH_EMULATION */
  541. if (reason & REASON_FP) {
  542. /* IEEE FP exception */
  543. int code = 0;
  544. u32 fpscr;
  545. /* We must make sure the FP state is consistent with
  546. * our MSR_FP in regs
  547. */
  548. preempt_disable();
  549. if (regs->msr & MSR_FP)
  550. giveup_fpu(current);
  551. preempt_enable();
  552. fpscr = current->thread.fpscr;
  553. fpscr &= fpscr << 22; /* mask summary bits with enables */
  554. if (fpscr & FPSCR_VX)
  555. code = FPE_FLTINV;
  556. else if (fpscr & FPSCR_OX)
  557. code = FPE_FLTOVF;
  558. else if (fpscr & FPSCR_UX)
  559. code = FPE_FLTUND;
  560. else if (fpscr & FPSCR_ZX)
  561. code = FPE_FLTDIV;
  562. else if (fpscr & FPSCR_XX)
  563. code = FPE_FLTRES;
  564. _exception(SIGFPE, regs, code, regs->nip);
  565. return;
  566. }
  567. if (reason & REASON_TRAP) {
  568. /* trap exception */
  569. if (debugger_bpt(regs))
  570. return;
  571. if (check_bug_trap(regs)) {
  572. regs->nip += 4;
  573. return;
  574. }
  575. _exception(SIGTRAP, regs, TRAP_BRKPT, 0);
  576. return;
  577. }
  578. /* Try to emulate it if we should. */
  579. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  580. switch (emulate_instruction(regs)) {
  581. case 0:
  582. regs->nip += 4;
  583. emulate_single_step(regs);
  584. return;
  585. case -EFAULT:
  586. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  587. return;
  588. }
  589. }
  590. if (reason & REASON_PRIVILEGED)
  591. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  592. else
  593. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  594. }
  595. void SingleStepException(struct pt_regs *regs)
  596. {
  597. regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
  598. if (debugger_sstep(regs))
  599. return;
  600. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  601. }
  602. void AlignmentException(struct pt_regs *regs)
  603. {
  604. int fixed;
  605. fixed = fix_alignment(regs);
  606. if (fixed == 1) {
  607. regs->nip += 4; /* skip over emulated instruction */
  608. emulate_single_step(regs);
  609. return;
  610. }
  611. if (fixed == -EFAULT) {
  612. /* fixed == -EFAULT means the operand address was bad */
  613. if (user_mode(regs))
  614. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->dar);
  615. else
  616. bad_page_fault(regs, regs->dar, SIGSEGV);
  617. return;
  618. }
  619. _exception(SIGBUS, regs, BUS_ADRALN, regs->dar);
  620. }
  621. void StackOverflow(struct pt_regs *regs)
  622. {
  623. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  624. current, regs->gpr[1]);
  625. debugger(regs);
  626. show_regs(regs);
  627. panic("kernel stack overflow");
  628. }
  629. void nonrecoverable_exception(struct pt_regs *regs)
  630. {
  631. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  632. regs->nip, regs->msr);
  633. debugger(regs);
  634. die("nonrecoverable exception", regs, SIGKILL);
  635. }
  636. void trace_syscall(struct pt_regs *regs)
  637. {
  638. printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
  639. current, current->pid, regs->nip, regs->link, regs->gpr[0],
  640. regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
  641. }
  642. #ifdef CONFIG_8xx
  643. void SoftwareEmulation(struct pt_regs *regs)
  644. {
  645. extern int do_mathemu(struct pt_regs *);
  646. extern int Soft_emulate_8xx(struct pt_regs *);
  647. int errcode;
  648. CHECK_FULL_REGS(regs);
  649. if (!user_mode(regs)) {
  650. debugger(regs);
  651. die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
  652. }
  653. #ifdef CONFIG_MATH_EMULATION
  654. errcode = do_mathemu(regs);
  655. #else
  656. errcode = Soft_emulate_8xx(regs);
  657. #endif
  658. if (errcode) {
  659. if (errcode > 0)
  660. _exception(SIGFPE, regs, 0, 0);
  661. else if (errcode == -EFAULT)
  662. _exception(SIGSEGV, regs, 0, 0);
  663. else
  664. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  665. } else
  666. emulate_single_step(regs);
  667. }
  668. #endif /* CONFIG_8xx */
  669. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  670. void DebugException(struct pt_regs *regs, unsigned long debug_status)
  671. {
  672. if (debug_status & DBSR_IC) { /* instruction completion */
  673. regs->msr &= ~MSR_DE;
  674. if (user_mode(regs)) {
  675. current->thread.dbcr0 &= ~DBCR0_IC;
  676. } else {
  677. /* Disable instruction completion */
  678. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  679. /* Clear the instruction completion event */
  680. mtspr(SPRN_DBSR, DBSR_IC);
  681. if (debugger_sstep(regs))
  682. return;
  683. }
  684. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  685. }
  686. }
  687. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  688. #if !defined(CONFIG_TAU_INT)
  689. void TAUException(struct pt_regs *regs)
  690. {
  691. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  692. regs->nip, regs->msr, regs->trap, print_tainted());
  693. }
  694. #endif /* CONFIG_INT_TAU */
  695. void AltivecUnavailException(struct pt_regs *regs)
  696. {
  697. static int kernel_altivec_count;
  698. #ifndef CONFIG_ALTIVEC
  699. if (user_mode(regs)) {
  700. /* A user program has executed an altivec instruction,
  701. but this kernel doesn't support altivec. */
  702. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  703. return;
  704. }
  705. #endif
  706. /* The kernel has executed an altivec instruction without
  707. first enabling altivec. Whinge but let it do it. */
  708. if (++kernel_altivec_count < 10)
  709. printk(KERN_ERR "AltiVec used in kernel (task=%p, pc=%lx)\n",
  710. current, regs->nip);
  711. regs->msr |= MSR_VEC;
  712. }
  713. #ifdef CONFIG_ALTIVEC
  714. void AltivecAssistException(struct pt_regs *regs)
  715. {
  716. int err;
  717. preempt_disable();
  718. if (regs->msr & MSR_VEC)
  719. giveup_altivec(current);
  720. preempt_enable();
  721. if (!user_mode(regs)) {
  722. printk(KERN_ERR "altivec assist exception in kernel mode"
  723. " at %lx\n", regs->nip);
  724. debugger(regs);
  725. die("altivec assist exception", regs, SIGFPE);
  726. return;
  727. }
  728. err = emulate_altivec(regs);
  729. if (err == 0) {
  730. regs->nip += 4; /* skip emulated instruction */
  731. emulate_single_step(regs);
  732. return;
  733. }
  734. if (err == -EFAULT) {
  735. /* got an error reading the instruction */
  736. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  737. } else {
  738. /* didn't recognize the instruction */
  739. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  740. printk(KERN_ERR "unrecognized altivec instruction "
  741. "in %s at %lx\n", current->comm, regs->nip);
  742. current->thread.vscr.u[3] |= 0x10000;
  743. }
  744. }
  745. #endif /* CONFIG_ALTIVEC */
  746. void PerformanceMonitorException(struct pt_regs *regs)
  747. {
  748. perf_irq(regs);
  749. }
  750. #ifdef CONFIG_FSL_BOOKE
  751. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  752. unsigned long error_code)
  753. {
  754. /* We treat cache locking instructions from the user
  755. * as priv ops, in the future we could try to do
  756. * something smarter
  757. */
  758. if (error_code & (ESR_DLK|ESR_ILK))
  759. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  760. return;
  761. }
  762. #endif /* CONFIG_FSL_BOOKE */
  763. #ifdef CONFIG_SPE
  764. void SPEFloatingPointException(struct pt_regs *regs)
  765. {
  766. unsigned long spefscr;
  767. int fpexc_mode;
  768. int code = 0;
  769. spefscr = current->thread.spefscr;
  770. fpexc_mode = current->thread.fpexc_mode;
  771. /* Hardware does not neccessarily set sticky
  772. * underflow/overflow/invalid flags */
  773. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  774. code = FPE_FLTOVF;
  775. spefscr |= SPEFSCR_FOVFS;
  776. }
  777. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  778. code = FPE_FLTUND;
  779. spefscr |= SPEFSCR_FUNFS;
  780. }
  781. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  782. code = FPE_FLTDIV;
  783. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  784. code = FPE_FLTINV;
  785. spefscr |= SPEFSCR_FINVS;
  786. }
  787. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  788. code = FPE_FLTRES;
  789. current->thread.spefscr = spefscr;
  790. _exception(SIGFPE, regs, code, regs->nip);
  791. return;
  792. }
  793. #endif
  794. void __init trap_init(void)
  795. {
  796. }