traps.c 24 KB

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