traps.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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_FSL_BOOKE
  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 | ESR_PUO)
  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. #elif defined (CONFIG_E200)
  281. printk("Machine check in kernel mode.\n");
  282. printk("Caused by (from MCSR=%lx): ", reason);
  283. if (reason & MCSR_MCP)
  284. printk("Machine Check Signal\n");
  285. if (reason & MCSR_CP_PERR)
  286. printk("Cache Push Parity Error\n");
  287. if (reason & MCSR_CPERR)
  288. printk("Cache Parity Error\n");
  289. if (reason & MCSR_EXCP_ERR)
  290. printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
  291. if (reason & MCSR_BUS_IRERR)
  292. printk("Bus - Read Bus Error on instruction fetch\n");
  293. if (reason & MCSR_BUS_DRERR)
  294. printk("Bus - Read Bus Error on data load\n");
  295. if (reason & MCSR_BUS_WRERR)
  296. printk("Bus - Write Bus Error on buffered store or cache line push\n");
  297. #else /* !CONFIG_4xx && !CONFIG_E500 && !CONFIG_E200 */
  298. printk("Machine check in kernel mode.\n");
  299. printk("Caused by (from SRR1=%lx): ", reason);
  300. switch (reason & 0x601F0000) {
  301. case 0x80000:
  302. printk("Machine check signal\n");
  303. break;
  304. case 0: /* for 601 */
  305. case 0x40000:
  306. case 0x140000: /* 7450 MSS error and TEA */
  307. printk("Transfer error ack signal\n");
  308. break;
  309. case 0x20000:
  310. printk("Data parity error signal\n");
  311. break;
  312. case 0x10000:
  313. printk("Address parity error signal\n");
  314. break;
  315. case 0x20000000:
  316. printk("L1 Data Cache error\n");
  317. break;
  318. case 0x40000000:
  319. printk("L1 Instruction Cache error\n");
  320. break;
  321. case 0x00100000:
  322. printk("L2 data cache parity error\n");
  323. break;
  324. default:
  325. printk("Unknown values in msr\n");
  326. }
  327. #endif /* CONFIG_4xx */
  328. /*
  329. * Optional platform-provided routine to print out
  330. * additional info, e.g. bus error registers.
  331. */
  332. platform_machine_check(regs);
  333. debugger(regs);
  334. die("machine check", regs, SIGBUS);
  335. }
  336. void SMIException(struct pt_regs *regs)
  337. {
  338. debugger(regs);
  339. #if !(defined(CONFIG_XMON) || defined(CONFIG_KGDB))
  340. show_regs(regs);
  341. panic("System Management Interrupt");
  342. #endif
  343. }
  344. void UnknownException(struct pt_regs *regs)
  345. {
  346. printk("Bad trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  347. regs->nip, regs->msr, regs->trap, print_tainted());
  348. _exception(SIGTRAP, regs, 0, 0);
  349. }
  350. void InstructionBreakpoint(struct pt_regs *regs)
  351. {
  352. if (debugger_iabr_match(regs))
  353. return;
  354. _exception(SIGTRAP, regs, TRAP_BRKPT, 0);
  355. }
  356. void RunModeException(struct pt_regs *regs)
  357. {
  358. _exception(SIGTRAP, regs, 0, 0);
  359. }
  360. /* Illegal instruction emulation support. Originally written to
  361. * provide the PVR to user applications using the mfspr rd, PVR.
  362. * Return non-zero if we can't emulate, or -EFAULT if the associated
  363. * memory access caused an access fault. Return zero on success.
  364. *
  365. * There are a couple of ways to do this, either "decode" the instruction
  366. * or directly match lots of bits. In this case, matching lots of
  367. * bits is faster and easier.
  368. *
  369. */
  370. #define INST_MFSPR_PVR 0x7c1f42a6
  371. #define INST_MFSPR_PVR_MASK 0xfc1fffff
  372. #define INST_DCBA 0x7c0005ec
  373. #define INST_DCBA_MASK 0x7c0007fe
  374. #define INST_MCRXR 0x7c000400
  375. #define INST_MCRXR_MASK 0x7c0007fe
  376. #define INST_STRING 0x7c00042a
  377. #define INST_STRING_MASK 0x7c0007fe
  378. #define INST_STRING_GEN_MASK 0x7c00067e
  379. #define INST_LSWI 0x7c0004aa
  380. #define INST_LSWX 0x7c00042a
  381. #define INST_STSWI 0x7c0005aa
  382. #define INST_STSWX 0x7c00052a
  383. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  384. {
  385. u8 rT = (instword >> 21) & 0x1f;
  386. u8 rA = (instword >> 16) & 0x1f;
  387. u8 NB_RB = (instword >> 11) & 0x1f;
  388. u32 num_bytes;
  389. unsigned long EA;
  390. int pos = 0;
  391. /* Early out if we are an invalid form of lswx */
  392. if ((instword & INST_STRING_MASK) == INST_LSWX)
  393. if ((rT == rA) || (rT == NB_RB))
  394. return -EINVAL;
  395. EA = (rA == 0) ? 0 : regs->gpr[rA];
  396. switch (instword & INST_STRING_MASK) {
  397. case INST_LSWX:
  398. case INST_STSWX:
  399. EA += NB_RB;
  400. num_bytes = regs->xer & 0x7f;
  401. break;
  402. case INST_LSWI:
  403. case INST_STSWI:
  404. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  405. break;
  406. default:
  407. return -EINVAL;
  408. }
  409. while (num_bytes != 0)
  410. {
  411. u8 val;
  412. u32 shift = 8 * (3 - (pos & 0x3));
  413. switch ((instword & INST_STRING_MASK)) {
  414. case INST_LSWX:
  415. case INST_LSWI:
  416. if (get_user(val, (u8 __user *)EA))
  417. return -EFAULT;
  418. /* first time updating this reg,
  419. * zero it out */
  420. if (pos == 0)
  421. regs->gpr[rT] = 0;
  422. regs->gpr[rT] |= val << shift;
  423. break;
  424. case INST_STSWI:
  425. case INST_STSWX:
  426. val = regs->gpr[rT] >> shift;
  427. if (put_user(val, (u8 __user *)EA))
  428. return -EFAULT;
  429. break;
  430. }
  431. /* move EA to next address */
  432. EA += 1;
  433. num_bytes--;
  434. /* manage our position within the register */
  435. if (++pos == 4) {
  436. pos = 0;
  437. if (++rT == 32)
  438. rT = 0;
  439. }
  440. }
  441. return 0;
  442. }
  443. static int emulate_instruction(struct pt_regs *regs)
  444. {
  445. u32 instword;
  446. u32 rd;
  447. if (!user_mode(regs))
  448. return -EINVAL;
  449. CHECK_FULL_REGS(regs);
  450. if (get_user(instword, (u32 __user *)(regs->nip)))
  451. return -EFAULT;
  452. /* Emulate the mfspr rD, PVR.
  453. */
  454. if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
  455. rd = (instword >> 21) & 0x1f;
  456. regs->gpr[rd] = mfspr(SPRN_PVR);
  457. return 0;
  458. }
  459. /* Emulating the dcba insn is just a no-op. */
  460. if ((instword & INST_DCBA_MASK) == INST_DCBA)
  461. return 0;
  462. /* Emulate the mcrxr insn. */
  463. if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
  464. int shift = (instword >> 21) & 0x1c;
  465. unsigned long msk = 0xf0000000UL >> shift;
  466. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  467. regs->xer &= ~0xf0000000UL;
  468. return 0;
  469. }
  470. /* Emulate load/store string insn. */
  471. if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
  472. return emulate_string_inst(regs, instword);
  473. return -EINVAL;
  474. }
  475. /*
  476. * After we have successfully emulated an instruction, we have to
  477. * check if the instruction was being single-stepped, and if so,
  478. * pretend we got a single-step exception. This was pointed out
  479. * by Kumar Gala. -- paulus
  480. */
  481. static void emulate_single_step(struct pt_regs *regs)
  482. {
  483. if (single_stepping(regs)) {
  484. clear_single_step(regs);
  485. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  486. }
  487. }
  488. /*
  489. * Look through the list of trap instructions that are used for BUG(),
  490. * BUG_ON() and WARN_ON() and see if we hit one. At this point we know
  491. * that the exception was caused by a trap instruction of some kind.
  492. * Returns 1 if we should continue (i.e. it was a WARN_ON) or 0
  493. * otherwise.
  494. */
  495. extern struct bug_entry __start___bug_table[], __stop___bug_table[];
  496. #ifndef CONFIG_MODULES
  497. #define module_find_bug(x) NULL
  498. #endif
  499. static struct bug_entry *find_bug(unsigned long bugaddr)
  500. {
  501. struct bug_entry *bug;
  502. for (bug = __start___bug_table; bug < __stop___bug_table; ++bug)
  503. if (bugaddr == bug->bug_addr)
  504. return bug;
  505. return module_find_bug(bugaddr);
  506. }
  507. int check_bug_trap(struct pt_regs *regs)
  508. {
  509. struct bug_entry *bug;
  510. unsigned long addr;
  511. if (regs->msr & MSR_PR)
  512. return 0; /* not in kernel */
  513. addr = regs->nip; /* address of trap instruction */
  514. if (addr < PAGE_OFFSET)
  515. return 0;
  516. bug = find_bug(regs->nip);
  517. if (bug == NULL)
  518. return 0;
  519. if (bug->line & BUG_WARNING_TRAP) {
  520. /* this is a WARN_ON rather than BUG/BUG_ON */
  521. #ifdef CONFIG_XMON
  522. xmon_printf(KERN_ERR "Badness in %s at %s:%d\n",
  523. bug->function, bug->file,
  524. bug->line & ~BUG_WARNING_TRAP);
  525. #endif /* CONFIG_XMON */
  526. printk(KERN_ERR "Badness in %s at %s:%d\n",
  527. bug->function, bug->file,
  528. bug->line & ~BUG_WARNING_TRAP);
  529. dump_stack();
  530. return 1;
  531. }
  532. #ifdef CONFIG_XMON
  533. xmon_printf(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
  534. bug->function, bug->file, bug->line);
  535. xmon(regs);
  536. #endif /* CONFIG_XMON */
  537. printk(KERN_CRIT "kernel BUG in %s at %s:%d!\n",
  538. bug->function, bug->file, bug->line);
  539. return 0;
  540. }
  541. void ProgramCheckException(struct pt_regs *regs)
  542. {
  543. unsigned int reason = get_reason(regs);
  544. extern int do_mathemu(struct pt_regs *regs);
  545. #ifdef CONFIG_MATH_EMULATION
  546. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  547. * but there seems to be a hardware bug on the 405GP (RevD)
  548. * that means ESR is sometimes set incorrectly - either to
  549. * ESR_DST (!?) or 0. In the process of chasing this with the
  550. * hardware people - not sure if it can happen on any illegal
  551. * instruction or only on FP instructions, whether there is a
  552. * pattern to occurences etc. -dgibson 31/Mar/2003 */
  553. if (!(reason & REASON_TRAP) && do_mathemu(regs) == 0) {
  554. emulate_single_step(regs);
  555. return;
  556. }
  557. #endif /* CONFIG_MATH_EMULATION */
  558. if (reason & REASON_FP) {
  559. /* IEEE FP exception */
  560. int code = 0;
  561. u32 fpscr;
  562. /* We must make sure the FP state is consistent with
  563. * our MSR_FP in regs
  564. */
  565. preempt_disable();
  566. if (regs->msr & MSR_FP)
  567. giveup_fpu(current);
  568. preempt_enable();
  569. fpscr = current->thread.fpscr;
  570. fpscr &= fpscr << 22; /* mask summary bits with enables */
  571. if (fpscr & FPSCR_VX)
  572. code = FPE_FLTINV;
  573. else if (fpscr & FPSCR_OX)
  574. code = FPE_FLTOVF;
  575. else if (fpscr & FPSCR_UX)
  576. code = FPE_FLTUND;
  577. else if (fpscr & FPSCR_ZX)
  578. code = FPE_FLTDIV;
  579. else if (fpscr & FPSCR_XX)
  580. code = FPE_FLTRES;
  581. _exception(SIGFPE, regs, code, regs->nip);
  582. return;
  583. }
  584. if (reason & REASON_TRAP) {
  585. /* trap exception */
  586. if (debugger_bpt(regs))
  587. return;
  588. if (check_bug_trap(regs)) {
  589. regs->nip += 4;
  590. return;
  591. }
  592. _exception(SIGTRAP, regs, TRAP_BRKPT, 0);
  593. return;
  594. }
  595. /* Try to emulate it if we should. */
  596. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  597. switch (emulate_instruction(regs)) {
  598. case 0:
  599. regs->nip += 4;
  600. emulate_single_step(regs);
  601. return;
  602. case -EFAULT:
  603. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  604. return;
  605. }
  606. }
  607. if (reason & REASON_PRIVILEGED)
  608. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  609. else
  610. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  611. }
  612. void SingleStepException(struct pt_regs *regs)
  613. {
  614. regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
  615. if (debugger_sstep(regs))
  616. return;
  617. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  618. }
  619. void AlignmentException(struct pt_regs *regs)
  620. {
  621. int fixed;
  622. fixed = fix_alignment(regs);
  623. if (fixed == 1) {
  624. regs->nip += 4; /* skip over emulated instruction */
  625. emulate_single_step(regs);
  626. return;
  627. }
  628. if (fixed == -EFAULT) {
  629. /* fixed == -EFAULT means the operand address was bad */
  630. if (user_mode(regs))
  631. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->dar);
  632. else
  633. bad_page_fault(regs, regs->dar, SIGSEGV);
  634. return;
  635. }
  636. _exception(SIGBUS, regs, BUS_ADRALN, regs->dar);
  637. }
  638. void StackOverflow(struct pt_regs *regs)
  639. {
  640. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  641. current, regs->gpr[1]);
  642. debugger(regs);
  643. show_regs(regs);
  644. panic("kernel stack overflow");
  645. }
  646. void nonrecoverable_exception(struct pt_regs *regs)
  647. {
  648. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  649. regs->nip, regs->msr);
  650. debugger(regs);
  651. die("nonrecoverable exception", regs, SIGKILL);
  652. }
  653. void trace_syscall(struct pt_regs *regs)
  654. {
  655. printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
  656. current, current->pid, regs->nip, regs->link, regs->gpr[0],
  657. regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
  658. }
  659. #ifdef CONFIG_8xx
  660. void SoftwareEmulation(struct pt_regs *regs)
  661. {
  662. extern int do_mathemu(struct pt_regs *);
  663. extern int Soft_emulate_8xx(struct pt_regs *);
  664. int errcode;
  665. CHECK_FULL_REGS(regs);
  666. if (!user_mode(regs)) {
  667. debugger(regs);
  668. die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
  669. }
  670. #ifdef CONFIG_MATH_EMULATION
  671. errcode = do_mathemu(regs);
  672. #else
  673. errcode = Soft_emulate_8xx(regs);
  674. #endif
  675. if (errcode) {
  676. if (errcode > 0)
  677. _exception(SIGFPE, regs, 0, 0);
  678. else if (errcode == -EFAULT)
  679. _exception(SIGSEGV, regs, 0, 0);
  680. else
  681. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  682. } else
  683. emulate_single_step(regs);
  684. }
  685. #endif /* CONFIG_8xx */
  686. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  687. void DebugException(struct pt_regs *regs, unsigned long debug_status)
  688. {
  689. if (debug_status & DBSR_IC) { /* instruction completion */
  690. regs->msr &= ~MSR_DE;
  691. if (user_mode(regs)) {
  692. current->thread.dbcr0 &= ~DBCR0_IC;
  693. } else {
  694. /* Disable instruction completion */
  695. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  696. /* Clear the instruction completion event */
  697. mtspr(SPRN_DBSR, DBSR_IC);
  698. if (debugger_sstep(regs))
  699. return;
  700. }
  701. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  702. }
  703. }
  704. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  705. #if !defined(CONFIG_TAU_INT)
  706. void TAUException(struct pt_regs *regs)
  707. {
  708. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  709. regs->nip, regs->msr, regs->trap, print_tainted());
  710. }
  711. #endif /* CONFIG_INT_TAU */
  712. void AltivecUnavailException(struct pt_regs *regs)
  713. {
  714. static int kernel_altivec_count;
  715. #ifndef CONFIG_ALTIVEC
  716. if (user_mode(regs)) {
  717. /* A user program has executed an altivec instruction,
  718. but this kernel doesn't support altivec. */
  719. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  720. return;
  721. }
  722. #endif
  723. /* The kernel has executed an altivec instruction without
  724. first enabling altivec. Whinge but let it do it. */
  725. if (++kernel_altivec_count < 10)
  726. printk(KERN_ERR "AltiVec used in kernel (task=%p, pc=%lx)\n",
  727. current, regs->nip);
  728. regs->msr |= MSR_VEC;
  729. }
  730. #ifdef CONFIG_ALTIVEC
  731. void AltivecAssistException(struct pt_regs *regs)
  732. {
  733. int err;
  734. preempt_disable();
  735. if (regs->msr & MSR_VEC)
  736. giveup_altivec(current);
  737. preempt_enable();
  738. if (!user_mode(regs)) {
  739. printk(KERN_ERR "altivec assist exception in kernel mode"
  740. " at %lx\n", regs->nip);
  741. debugger(regs);
  742. die("altivec assist exception", regs, SIGFPE);
  743. return;
  744. }
  745. err = emulate_altivec(regs);
  746. if (err == 0) {
  747. regs->nip += 4; /* skip emulated instruction */
  748. emulate_single_step(regs);
  749. return;
  750. }
  751. if (err == -EFAULT) {
  752. /* got an error reading the instruction */
  753. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  754. } else {
  755. /* didn't recognize the instruction */
  756. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  757. printk(KERN_ERR "unrecognized altivec instruction "
  758. "in %s at %lx\n", current->comm, regs->nip);
  759. current->thread.vscr.u[3] |= 0x10000;
  760. }
  761. }
  762. #endif /* CONFIG_ALTIVEC */
  763. void PerformanceMonitorException(struct pt_regs *regs)
  764. {
  765. perf_irq(regs);
  766. }
  767. #ifdef CONFIG_FSL_BOOKE
  768. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  769. unsigned long error_code)
  770. {
  771. /* We treat cache locking instructions from the user
  772. * as priv ops, in the future we could try to do
  773. * something smarter
  774. */
  775. if (error_code & (ESR_DLK|ESR_ILK))
  776. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  777. return;
  778. }
  779. #endif /* CONFIG_FSL_BOOKE */
  780. #ifdef CONFIG_SPE
  781. void SPEFloatingPointException(struct pt_regs *regs)
  782. {
  783. unsigned long spefscr;
  784. int fpexc_mode;
  785. int code = 0;
  786. spefscr = current->thread.spefscr;
  787. fpexc_mode = current->thread.fpexc_mode;
  788. /* Hardware does not neccessarily set sticky
  789. * underflow/overflow/invalid flags */
  790. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  791. code = FPE_FLTOVF;
  792. spefscr |= SPEFSCR_FOVFS;
  793. }
  794. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  795. code = FPE_FLTUND;
  796. spefscr |= SPEFSCR_FUNFS;
  797. }
  798. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  799. code = FPE_FLTDIV;
  800. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  801. code = FPE_FLTINV;
  802. spefscr |= SPEFSCR_FINVS;
  803. }
  804. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  805. code = FPE_FLTRES;
  806. current->thread.spefscr = spefscr;
  807. _exception(SIGFPE, regs, code, regs->nip);
  808. return;
  809. }
  810. #endif
  811. #ifdef CONFIG_BOOKE_WDT
  812. /*
  813. * Default handler for a Watchdog exception,
  814. * spins until a reboot occurs
  815. */
  816. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  817. {
  818. /* Generic WatchdogHandler, implement your own */
  819. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  820. return;
  821. }
  822. void WatchdogException(struct pt_regs *regs)
  823. {
  824. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  825. WatchdogHandler(regs);
  826. }
  827. #endif
  828. void __init trap_init(void)
  829. {
  830. }