traps.c 25 KB

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