traps.c 25 KB

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