traps.c 21 KB

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