traps.c 25 KB

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