traps.c 23 KB

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