traps.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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@samba.org)
  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 <linux/delay.h>
  30. #include <linux/kprobes.h>
  31. #include <linux/kexec.h>
  32. #include <linux/backlight.h>
  33. #include <linux/bug.h>
  34. #include <linux/kdebug.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/system.h>
  38. #include <asm/io.h>
  39. #include <asm/machdep.h>
  40. #include <asm/rtas.h>
  41. #include <asm/pmc.h>
  42. #ifdef CONFIG_PPC32
  43. #include <asm/reg.h>
  44. #endif
  45. #ifdef CONFIG_PMAC_BACKLIGHT
  46. #include <asm/backlight.h>
  47. #endif
  48. #ifdef CONFIG_PPC64
  49. #include <asm/firmware.h>
  50. #include <asm/processor.h>
  51. #endif
  52. #include <asm/kexec.h>
  53. #ifdef CONFIG_DEBUGGER
  54. int (*__debugger)(struct pt_regs *regs);
  55. int (*__debugger_ipi)(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. int (*__debugger_fault_handler)(struct pt_regs *regs);
  61. EXPORT_SYMBOL(__debugger);
  62. EXPORT_SYMBOL(__debugger_ipi);
  63. EXPORT_SYMBOL(__debugger_bpt);
  64. EXPORT_SYMBOL(__debugger_sstep);
  65. EXPORT_SYMBOL(__debugger_iabr_match);
  66. EXPORT_SYMBOL(__debugger_dabr_match);
  67. EXPORT_SYMBOL(__debugger_fault_handler);
  68. #endif
  69. /*
  70. * Trap & Exception support
  71. */
  72. #ifdef CONFIG_PMAC_BACKLIGHT
  73. static void pmac_backlight_unblank(void)
  74. {
  75. mutex_lock(&pmac_backlight_mutex);
  76. if (pmac_backlight) {
  77. struct backlight_properties *props;
  78. props = &pmac_backlight->props;
  79. props->brightness = props->max_brightness;
  80. props->power = FB_BLANK_UNBLANK;
  81. backlight_update_status(pmac_backlight);
  82. }
  83. mutex_unlock(&pmac_backlight_mutex);
  84. }
  85. #else
  86. static inline void pmac_backlight_unblank(void) { }
  87. #endif
  88. int die(const char *str, struct pt_regs *regs, long err)
  89. {
  90. static struct {
  91. spinlock_t lock;
  92. u32 lock_owner;
  93. int lock_owner_depth;
  94. } die = {
  95. .lock = __SPIN_LOCK_UNLOCKED(die.lock),
  96. .lock_owner = -1,
  97. .lock_owner_depth = 0
  98. };
  99. static int die_counter;
  100. unsigned long flags;
  101. if (debugger(regs))
  102. return 1;
  103. oops_enter();
  104. if (die.lock_owner != raw_smp_processor_id()) {
  105. console_verbose();
  106. spin_lock_irqsave(&die.lock, flags);
  107. die.lock_owner = smp_processor_id();
  108. die.lock_owner_depth = 0;
  109. bust_spinlocks(1);
  110. if (machine_is(powermac))
  111. pmac_backlight_unblank();
  112. } else {
  113. local_save_flags(flags);
  114. }
  115. if (++die.lock_owner_depth < 3) {
  116. printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
  117. #ifdef CONFIG_PREEMPT
  118. printk("PREEMPT ");
  119. #endif
  120. #ifdef CONFIG_SMP
  121. printk("SMP NR_CPUS=%d ", NR_CPUS);
  122. #endif
  123. #ifdef CONFIG_DEBUG_PAGEALLOC
  124. printk("DEBUG_PAGEALLOC ");
  125. #endif
  126. #ifdef CONFIG_NUMA
  127. printk("NUMA ");
  128. #endif
  129. printk("%s\n", ppc_md.name ? ppc_md.name : "");
  130. print_modules();
  131. show_regs(regs);
  132. } else {
  133. printk("Recursive die() failure, output suppressed\n");
  134. }
  135. bust_spinlocks(0);
  136. die.lock_owner = -1;
  137. spin_unlock_irqrestore(&die.lock, flags);
  138. if (kexec_should_crash(current) ||
  139. kexec_sr_activated(smp_processor_id()))
  140. crash_kexec(regs);
  141. crash_kexec_secondary(regs);
  142. if (in_interrupt())
  143. panic("Fatal exception in interrupt");
  144. if (panic_on_oops)
  145. panic("Fatal exception");
  146. oops_exit();
  147. do_exit(err);
  148. return 0;
  149. }
  150. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  151. {
  152. siginfo_t info;
  153. if (!user_mode(regs)) {
  154. if (die("Exception in kernel mode", regs, signr))
  155. return;
  156. }
  157. memset(&info, 0, sizeof(info));
  158. info.si_signo = signr;
  159. info.si_code = code;
  160. info.si_addr = (void __user *) addr;
  161. force_sig_info(signr, &info, current);
  162. /*
  163. * Init gets no signals that it doesn't have a handler for.
  164. * That's all very well, but if it has caused a synchronous
  165. * exception and we ignore the resulting signal, it will just
  166. * generate the same exception over and over again and we get
  167. * nowhere. Better to kill it and let the kernel panic.
  168. */
  169. if (is_init(current)) {
  170. __sighandler_t handler;
  171. spin_lock_irq(&current->sighand->siglock);
  172. handler = current->sighand->action[signr-1].sa.sa_handler;
  173. spin_unlock_irq(&current->sighand->siglock);
  174. if (handler == SIG_DFL) {
  175. /* init has generated a synchronous exception
  176. and it doesn't have a handler for the signal */
  177. printk(KERN_CRIT "init has generated signal %d "
  178. "but has no handler for it\n", signr);
  179. do_exit(signr);
  180. }
  181. }
  182. }
  183. #ifdef CONFIG_PPC64
  184. void system_reset_exception(struct pt_regs *regs)
  185. {
  186. /* See if any machine dependent calls */
  187. if (ppc_md.system_reset_exception) {
  188. if (ppc_md.system_reset_exception(regs))
  189. return;
  190. }
  191. #ifdef CONFIG_KEXEC
  192. cpu_set(smp_processor_id(), cpus_in_sr);
  193. #endif
  194. die("System Reset", regs, SIGABRT);
  195. /*
  196. * Some CPUs when released from the debugger will execute this path.
  197. * These CPUs entered the debugger via a soft-reset. If the CPU was
  198. * hung before entering the debugger it will return to the hung
  199. * state when exiting this function. This causes a problem in
  200. * kdump since the hung CPU(s) will not respond to the IPI sent
  201. * from kdump. To prevent the problem we call crash_kexec_secondary()
  202. * here. If a kdump had not been initiated or we exit the debugger
  203. * with the "exit and recover" command (x) crash_kexec_secondary()
  204. * will return after 5ms and the CPU returns to its previous state.
  205. */
  206. crash_kexec_secondary(regs);
  207. /* Must die if the interrupt is not recoverable */
  208. if (!(regs->msr & MSR_RI))
  209. panic("Unrecoverable System Reset");
  210. /* What should we do here? We could issue a shutdown or hard reset. */
  211. }
  212. #endif
  213. /*
  214. * I/O accesses can cause machine checks on powermacs.
  215. * Check if the NIP corresponds to the address of a sync
  216. * instruction for which there is an entry in the exception
  217. * table.
  218. * Note that the 601 only takes a machine check on TEA
  219. * (transfer error ack) signal assertion, and does not
  220. * set any of the top 16 bits of SRR1.
  221. * -- paulus.
  222. */
  223. static inline int check_io_access(struct pt_regs *regs)
  224. {
  225. #ifdef CONFIG_PPC32
  226. unsigned long msr = regs->msr;
  227. const struct exception_table_entry *entry;
  228. unsigned int *nip = (unsigned int *)regs->nip;
  229. if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
  230. && (entry = search_exception_tables(regs->nip)) != NULL) {
  231. /*
  232. * Check that it's a sync instruction, or somewhere
  233. * in the twi; isync; nop sequence that inb/inw/inl uses.
  234. * As the address is in the exception table
  235. * we should be able to read the instr there.
  236. * For the debug message, we look at the preceding
  237. * load or store.
  238. */
  239. if (*nip == 0x60000000) /* nop */
  240. nip -= 2;
  241. else if (*nip == 0x4c00012c) /* isync */
  242. --nip;
  243. if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
  244. /* sync or twi */
  245. unsigned int rb;
  246. --nip;
  247. rb = (*nip >> 11) & 0x1f;
  248. printk(KERN_DEBUG "%s bad port %lx at %p\n",
  249. (*nip & 0x100)? "OUT to": "IN from",
  250. regs->gpr[rb] - _IO_BASE, nip);
  251. regs->msr |= MSR_RI;
  252. regs->nip = entry->fixup;
  253. return 1;
  254. }
  255. }
  256. #endif /* CONFIG_PPC32 */
  257. return 0;
  258. }
  259. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  260. /* On 4xx, the reason for the machine check or program exception
  261. is in the ESR. */
  262. #define get_reason(regs) ((regs)->dsisr)
  263. #ifndef CONFIG_FSL_BOOKE
  264. #define get_mc_reason(regs) ((regs)->dsisr)
  265. #else
  266. #define get_mc_reason(regs) (mfspr(SPRN_MCSR))
  267. #endif
  268. #define REASON_FP ESR_FP
  269. #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
  270. #define REASON_PRIVILEGED ESR_PPR
  271. #define REASON_TRAP ESR_PTR
  272. /* single-step stuff */
  273. #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
  274. #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
  275. #else
  276. /* On non-4xx, the reason for the machine check or program
  277. exception is in the MSR. */
  278. #define get_reason(regs) ((regs)->msr)
  279. #define get_mc_reason(regs) ((regs)->msr)
  280. #define REASON_FP 0x100000
  281. #define REASON_ILLEGAL 0x80000
  282. #define REASON_PRIVILEGED 0x40000
  283. #define REASON_TRAP 0x20000
  284. #define single_stepping(regs) ((regs)->msr & MSR_SE)
  285. #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
  286. #endif
  287. /*
  288. * This is "fall-back" implementation for configurations
  289. * which don't provide platform-specific machine check info
  290. */
  291. void __attribute__ ((weak))
  292. platform_machine_check(struct pt_regs *regs)
  293. {
  294. }
  295. void machine_check_exception(struct pt_regs *regs)
  296. {
  297. int recover = 0;
  298. unsigned long reason = get_mc_reason(regs);
  299. /* See if any machine dependent calls */
  300. if (ppc_md.machine_check_exception)
  301. recover = ppc_md.machine_check_exception(regs);
  302. if (recover)
  303. return;
  304. if (user_mode(regs)) {
  305. regs->msr |= MSR_RI;
  306. _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
  307. return;
  308. }
  309. #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
  310. /* the qspan pci read routines can cause machine checks -- Cort */
  311. bad_page_fault(regs, regs->dar, SIGBUS);
  312. return;
  313. #endif
  314. if (debugger_fault_handler(regs)) {
  315. regs->msr |= MSR_RI;
  316. return;
  317. }
  318. if (check_io_access(regs))
  319. return;
  320. #if defined(CONFIG_4xx) && !defined(CONFIG_440A)
  321. if (reason & ESR_IMCP) {
  322. printk("Instruction");
  323. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  324. } else
  325. printk("Data");
  326. printk(" machine check in kernel mode.\n");
  327. #elif defined(CONFIG_440A)
  328. printk("Machine check in kernel mode.\n");
  329. if (reason & ESR_IMCP){
  330. printk("Instruction Synchronous Machine Check exception\n");
  331. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  332. }
  333. else {
  334. u32 mcsr = mfspr(SPRN_MCSR);
  335. if (mcsr & MCSR_IB)
  336. printk("Instruction Read PLB Error\n");
  337. if (mcsr & MCSR_DRB)
  338. printk("Data Read PLB Error\n");
  339. if (mcsr & MCSR_DWB)
  340. printk("Data Write PLB Error\n");
  341. if (mcsr & MCSR_TLBP)
  342. printk("TLB Parity Error\n");
  343. if (mcsr & MCSR_ICP){
  344. flush_instruction_cache();
  345. printk("I-Cache Parity Error\n");
  346. }
  347. if (mcsr & MCSR_DCSP)
  348. printk("D-Cache Search Parity Error\n");
  349. if (mcsr & MCSR_DCFP)
  350. printk("D-Cache Flush Parity Error\n");
  351. if (mcsr & MCSR_IMPE)
  352. printk("Machine Check exception is imprecise\n");
  353. /* Clear MCSR */
  354. mtspr(SPRN_MCSR, mcsr);
  355. }
  356. #elif defined (CONFIG_E500)
  357. printk("Machine check in kernel mode.\n");
  358. printk("Caused by (from MCSR=%lx): ", reason);
  359. if (reason & MCSR_MCP)
  360. printk("Machine Check Signal\n");
  361. if (reason & MCSR_ICPERR)
  362. printk("Instruction Cache Parity Error\n");
  363. if (reason & MCSR_DCP_PERR)
  364. printk("Data Cache Push Parity Error\n");
  365. if (reason & MCSR_DCPERR)
  366. printk("Data Cache Parity Error\n");
  367. if (reason & MCSR_GL_CI)
  368. printk("Guarded Load or Cache-Inhibited stwcx.\n");
  369. if (reason & MCSR_BUS_IAERR)
  370. printk("Bus - Instruction Address Error\n");
  371. if (reason & MCSR_BUS_RAERR)
  372. printk("Bus - Read Address Error\n");
  373. if (reason & MCSR_BUS_WAERR)
  374. printk("Bus - Write Address Error\n");
  375. if (reason & MCSR_BUS_IBERR)
  376. printk("Bus - Instruction Data Error\n");
  377. if (reason & MCSR_BUS_RBERR)
  378. printk("Bus - Read Data Bus Error\n");
  379. if (reason & MCSR_BUS_WBERR)
  380. printk("Bus - Read Data Bus Error\n");
  381. if (reason & MCSR_BUS_IPERR)
  382. printk("Bus - Instruction Parity Error\n");
  383. if (reason & MCSR_BUS_RPERR)
  384. printk("Bus - Read Parity Error\n");
  385. #elif defined (CONFIG_E200)
  386. printk("Machine check in kernel mode.\n");
  387. printk("Caused by (from MCSR=%lx): ", reason);
  388. if (reason & MCSR_MCP)
  389. printk("Machine Check Signal\n");
  390. if (reason & MCSR_CP_PERR)
  391. printk("Cache Push Parity Error\n");
  392. if (reason & MCSR_CPERR)
  393. printk("Cache Parity Error\n");
  394. if (reason & MCSR_EXCP_ERR)
  395. printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
  396. if (reason & MCSR_BUS_IRERR)
  397. printk("Bus - Read Bus Error on instruction fetch\n");
  398. if (reason & MCSR_BUS_DRERR)
  399. printk("Bus - Read Bus Error on data load\n");
  400. if (reason & MCSR_BUS_WRERR)
  401. printk("Bus - Write Bus Error on buffered store or cache line push\n");
  402. #else /* !CONFIG_4xx && !CONFIG_E500 && !CONFIG_E200 */
  403. printk("Machine check in kernel mode.\n");
  404. printk("Caused by (from SRR1=%lx): ", reason);
  405. switch (reason & 0x601F0000) {
  406. case 0x80000:
  407. printk("Machine check signal\n");
  408. break;
  409. case 0: /* for 601 */
  410. case 0x40000:
  411. case 0x140000: /* 7450 MSS error and TEA */
  412. printk("Transfer error ack signal\n");
  413. break;
  414. case 0x20000:
  415. printk("Data parity error signal\n");
  416. break;
  417. case 0x10000:
  418. printk("Address parity error signal\n");
  419. break;
  420. case 0x20000000:
  421. printk("L1 Data Cache error\n");
  422. break;
  423. case 0x40000000:
  424. printk("L1 Instruction Cache error\n");
  425. break;
  426. case 0x00100000:
  427. printk("L2 data cache parity error\n");
  428. break;
  429. default:
  430. printk("Unknown values in msr\n");
  431. }
  432. #endif /* CONFIG_4xx */
  433. /*
  434. * Optional platform-provided routine to print out
  435. * additional info, e.g. bus error registers.
  436. */
  437. platform_machine_check(regs);
  438. if (debugger_fault_handler(regs))
  439. return;
  440. die("Machine check", regs, SIGBUS);
  441. /* Must die if the interrupt is not recoverable */
  442. if (!(regs->msr & MSR_RI))
  443. panic("Unrecoverable Machine check");
  444. }
  445. void SMIException(struct pt_regs *regs)
  446. {
  447. die("System Management Interrupt", regs, SIGABRT);
  448. }
  449. void unknown_exception(struct pt_regs *regs)
  450. {
  451. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  452. regs->nip, regs->msr, regs->trap);
  453. _exception(SIGTRAP, regs, 0, 0);
  454. }
  455. void instruction_breakpoint_exception(struct pt_regs *regs)
  456. {
  457. if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
  458. 5, SIGTRAP) == NOTIFY_STOP)
  459. return;
  460. if (debugger_iabr_match(regs))
  461. return;
  462. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  463. }
  464. void RunModeException(struct pt_regs *regs)
  465. {
  466. _exception(SIGTRAP, regs, 0, 0);
  467. }
  468. void __kprobes single_step_exception(struct pt_regs *regs)
  469. {
  470. regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
  471. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  472. 5, SIGTRAP) == NOTIFY_STOP)
  473. return;
  474. if (debugger_sstep(regs))
  475. return;
  476. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  477. }
  478. /*
  479. * After we have successfully emulated an instruction, we have to
  480. * check if the instruction was being single-stepped, and if so,
  481. * pretend we got a single-step exception. This was pointed out
  482. * by Kumar Gala. -- paulus
  483. */
  484. static void emulate_single_step(struct pt_regs *regs)
  485. {
  486. if (single_stepping(regs)) {
  487. clear_single_step(regs);
  488. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  489. }
  490. }
  491. static inline int __parse_fpscr(unsigned long fpscr)
  492. {
  493. int ret = 0;
  494. /* Invalid operation */
  495. if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
  496. ret = FPE_FLTINV;
  497. /* Overflow */
  498. else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
  499. ret = FPE_FLTOVF;
  500. /* Underflow */
  501. else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
  502. ret = FPE_FLTUND;
  503. /* Divide by zero */
  504. else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
  505. ret = FPE_FLTDIV;
  506. /* Inexact result */
  507. else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
  508. ret = FPE_FLTRES;
  509. return ret;
  510. }
  511. static void parse_fpe(struct pt_regs *regs)
  512. {
  513. int code = 0;
  514. flush_fp_to_thread(current);
  515. code = __parse_fpscr(current->thread.fpscr.val);
  516. _exception(SIGFPE, regs, code, regs->nip);
  517. }
  518. /*
  519. * Illegal instruction emulation support. Originally written to
  520. * provide the PVR to user applications using the mfspr rd, PVR.
  521. * Return non-zero if we can't emulate, or -EFAULT if the associated
  522. * memory access caused an access fault. Return zero on success.
  523. *
  524. * There are a couple of ways to do this, either "decode" the instruction
  525. * or directly match lots of bits. In this case, matching lots of
  526. * bits is faster and easier.
  527. *
  528. */
  529. #define INST_MFSPR_PVR 0x7c1f42a6
  530. #define INST_MFSPR_PVR_MASK 0xfc1fffff
  531. #define INST_DCBA 0x7c0005ec
  532. #define INST_DCBA_MASK 0xfc0007fe
  533. #define INST_MCRXR 0x7c000400
  534. #define INST_MCRXR_MASK 0xfc0007fe
  535. #define INST_STRING 0x7c00042a
  536. #define INST_STRING_MASK 0xfc0007fe
  537. #define INST_STRING_GEN_MASK 0xfc00067e
  538. #define INST_LSWI 0x7c0004aa
  539. #define INST_LSWX 0x7c00042a
  540. #define INST_STSWI 0x7c0005aa
  541. #define INST_STSWX 0x7c00052a
  542. #define INST_POPCNTB 0x7c0000f4
  543. #define INST_POPCNTB_MASK 0xfc0007fe
  544. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  545. {
  546. u8 rT = (instword >> 21) & 0x1f;
  547. u8 rA = (instword >> 16) & 0x1f;
  548. u8 NB_RB = (instword >> 11) & 0x1f;
  549. u32 num_bytes;
  550. unsigned long EA;
  551. int pos = 0;
  552. /* Early out if we are an invalid form of lswx */
  553. if ((instword & INST_STRING_MASK) == INST_LSWX)
  554. if ((rT == rA) || (rT == NB_RB))
  555. return -EINVAL;
  556. EA = (rA == 0) ? 0 : regs->gpr[rA];
  557. switch (instword & INST_STRING_MASK) {
  558. case INST_LSWX:
  559. case INST_STSWX:
  560. EA += NB_RB;
  561. num_bytes = regs->xer & 0x7f;
  562. break;
  563. case INST_LSWI:
  564. case INST_STSWI:
  565. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  566. break;
  567. default:
  568. return -EINVAL;
  569. }
  570. while (num_bytes != 0)
  571. {
  572. u8 val;
  573. u32 shift = 8 * (3 - (pos & 0x3));
  574. switch ((instword & INST_STRING_MASK)) {
  575. case INST_LSWX:
  576. case INST_LSWI:
  577. if (get_user(val, (u8 __user *)EA))
  578. return -EFAULT;
  579. /* first time updating this reg,
  580. * zero it out */
  581. if (pos == 0)
  582. regs->gpr[rT] = 0;
  583. regs->gpr[rT] |= val << shift;
  584. break;
  585. case INST_STSWI:
  586. case INST_STSWX:
  587. val = regs->gpr[rT] >> shift;
  588. if (put_user(val, (u8 __user *)EA))
  589. return -EFAULT;
  590. break;
  591. }
  592. /* move EA to next address */
  593. EA += 1;
  594. num_bytes--;
  595. /* manage our position within the register */
  596. if (++pos == 4) {
  597. pos = 0;
  598. if (++rT == 32)
  599. rT = 0;
  600. }
  601. }
  602. return 0;
  603. }
  604. static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
  605. {
  606. u32 ra,rs;
  607. unsigned long tmp;
  608. ra = (instword >> 16) & 0x1f;
  609. rs = (instword >> 21) & 0x1f;
  610. tmp = regs->gpr[rs];
  611. tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
  612. tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
  613. tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
  614. regs->gpr[ra] = tmp;
  615. return 0;
  616. }
  617. static int emulate_instruction(struct pt_regs *regs)
  618. {
  619. u32 instword;
  620. u32 rd;
  621. if (!user_mode(regs) || (regs->msr & MSR_LE))
  622. return -EINVAL;
  623. CHECK_FULL_REGS(regs);
  624. if (get_user(instword, (u32 __user *)(regs->nip)))
  625. return -EFAULT;
  626. /* Emulate the mfspr rD, PVR. */
  627. if ((instword & INST_MFSPR_PVR_MASK) == INST_MFSPR_PVR) {
  628. rd = (instword >> 21) & 0x1f;
  629. regs->gpr[rd] = mfspr(SPRN_PVR);
  630. return 0;
  631. }
  632. /* Emulating the dcba insn is just a no-op. */
  633. if ((instword & INST_DCBA_MASK) == INST_DCBA)
  634. return 0;
  635. /* Emulate the mcrxr insn. */
  636. if ((instword & INST_MCRXR_MASK) == INST_MCRXR) {
  637. int shift = (instword >> 21) & 0x1c;
  638. unsigned long msk = 0xf0000000UL >> shift;
  639. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  640. regs->xer &= ~0xf0000000UL;
  641. return 0;
  642. }
  643. /* Emulate load/store string insn. */
  644. if ((instword & INST_STRING_GEN_MASK) == INST_STRING)
  645. return emulate_string_inst(regs, instword);
  646. /* Emulate the popcntb (Population Count Bytes) instruction. */
  647. if ((instword & INST_POPCNTB_MASK) == INST_POPCNTB) {
  648. return emulate_popcntb_inst(regs, instword);
  649. }
  650. return -EINVAL;
  651. }
  652. int is_valid_bugaddr(unsigned long addr)
  653. {
  654. return is_kernel_addr(addr);
  655. }
  656. void __kprobes program_check_exception(struct pt_regs *regs)
  657. {
  658. unsigned int reason = get_reason(regs);
  659. extern int do_mathemu(struct pt_regs *regs);
  660. /* We can now get here via a FP Unavailable exception if the core
  661. * has no FPU, in that case the reason flags will be 0 */
  662. if (reason & REASON_FP) {
  663. /* IEEE FP exception */
  664. parse_fpe(regs);
  665. return;
  666. }
  667. if (reason & REASON_TRAP) {
  668. /* trap exception */
  669. if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
  670. == NOTIFY_STOP)
  671. return;
  672. if (debugger_bpt(regs))
  673. return;
  674. if (!(regs->msr & MSR_PR) && /* not user-mode */
  675. report_bug(regs->nip) == BUG_TRAP_TYPE_WARN) {
  676. regs->nip += 4;
  677. return;
  678. }
  679. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  680. return;
  681. }
  682. local_irq_enable();
  683. #ifdef CONFIG_MATH_EMULATION
  684. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  685. * but there seems to be a hardware bug on the 405GP (RevD)
  686. * that means ESR is sometimes set incorrectly - either to
  687. * ESR_DST (!?) or 0. In the process of chasing this with the
  688. * hardware people - not sure if it can happen on any illegal
  689. * instruction or only on FP instructions, whether there is a
  690. * pattern to occurences etc. -dgibson 31/Mar/2003 */
  691. switch (do_mathemu(regs)) {
  692. case 0:
  693. emulate_single_step(regs);
  694. return;
  695. case 1: {
  696. int code = 0;
  697. code = __parse_fpscr(current->thread.fpscr.val);
  698. _exception(SIGFPE, regs, code, regs->nip);
  699. return;
  700. }
  701. case -EFAULT:
  702. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  703. return;
  704. }
  705. /* fall through on any other errors */
  706. #endif /* CONFIG_MATH_EMULATION */
  707. /* Try to emulate it if we should. */
  708. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  709. switch (emulate_instruction(regs)) {
  710. case 0:
  711. regs->nip += 4;
  712. emulate_single_step(regs);
  713. return;
  714. case -EFAULT:
  715. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  716. return;
  717. }
  718. }
  719. if (reason & REASON_PRIVILEGED)
  720. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  721. else
  722. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  723. }
  724. void alignment_exception(struct pt_regs *regs)
  725. {
  726. int sig, code, fixed = 0;
  727. /* we don't implement logging of alignment exceptions */
  728. if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
  729. fixed = fix_alignment(regs);
  730. if (fixed == 1) {
  731. regs->nip += 4; /* skip over emulated instruction */
  732. emulate_single_step(regs);
  733. return;
  734. }
  735. /* Operand address was bad */
  736. if (fixed == -EFAULT) {
  737. sig = SIGSEGV;
  738. code = SEGV_ACCERR;
  739. } else {
  740. sig = SIGBUS;
  741. code = BUS_ADRALN;
  742. }
  743. if (user_mode(regs))
  744. _exception(sig, regs, code, regs->dar);
  745. else
  746. bad_page_fault(regs, regs->dar, sig);
  747. }
  748. void StackOverflow(struct pt_regs *regs)
  749. {
  750. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  751. current, regs->gpr[1]);
  752. debugger(regs);
  753. show_regs(regs);
  754. panic("kernel stack overflow");
  755. }
  756. void nonrecoverable_exception(struct pt_regs *regs)
  757. {
  758. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  759. regs->nip, regs->msr);
  760. debugger(regs);
  761. die("nonrecoverable exception", regs, SIGKILL);
  762. }
  763. void trace_syscall(struct pt_regs *regs)
  764. {
  765. printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
  766. current, current->pid, regs->nip, regs->link, regs->gpr[0],
  767. regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
  768. }
  769. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  770. {
  771. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  772. "%lx at %lx\n", regs->trap, regs->nip);
  773. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  774. }
  775. void altivec_unavailable_exception(struct pt_regs *regs)
  776. {
  777. if (user_mode(regs)) {
  778. /* A user program has executed an altivec instruction,
  779. but this kernel doesn't support altivec. */
  780. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  781. return;
  782. }
  783. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  784. "%lx at %lx\n", regs->trap, regs->nip);
  785. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  786. }
  787. void performance_monitor_exception(struct pt_regs *regs)
  788. {
  789. perf_irq(regs);
  790. }
  791. #ifdef CONFIG_8xx
  792. void SoftwareEmulation(struct pt_regs *regs)
  793. {
  794. extern int do_mathemu(struct pt_regs *);
  795. extern int Soft_emulate_8xx(struct pt_regs *);
  796. int errcode;
  797. CHECK_FULL_REGS(regs);
  798. if (!user_mode(regs)) {
  799. debugger(regs);
  800. die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
  801. }
  802. #ifdef CONFIG_MATH_EMULATION
  803. errcode = do_mathemu(regs);
  804. switch (errcode) {
  805. case 0:
  806. emulate_single_step(regs);
  807. return;
  808. case 1: {
  809. int code = 0;
  810. code = __parse_fpscr(current->thread.fpscr.val);
  811. _exception(SIGFPE, regs, code, regs->nip);
  812. return;
  813. }
  814. case -EFAULT:
  815. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  816. return;
  817. default:
  818. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  819. return;
  820. }
  821. #else
  822. errcode = Soft_emulate_8xx(regs);
  823. switch (errcode) {
  824. case 0:
  825. emulate_single_step(regs);
  826. return;
  827. case 1:
  828. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  829. return;
  830. case -EFAULT:
  831. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  832. return;
  833. }
  834. #endif
  835. }
  836. #endif /* CONFIG_8xx */
  837. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  838. void DebugException(struct pt_regs *regs, unsigned long debug_status)
  839. {
  840. if (debug_status & DBSR_IC) { /* instruction completion */
  841. regs->msr &= ~MSR_DE;
  842. if (user_mode(regs)) {
  843. current->thread.dbcr0 &= ~DBCR0_IC;
  844. } else {
  845. /* Disable instruction completion */
  846. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  847. /* Clear the instruction completion event */
  848. mtspr(SPRN_DBSR, DBSR_IC);
  849. if (debugger_sstep(regs))
  850. return;
  851. }
  852. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  853. }
  854. }
  855. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  856. #if !defined(CONFIG_TAU_INT)
  857. void TAUException(struct pt_regs *regs)
  858. {
  859. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  860. regs->nip, regs->msr, regs->trap, print_tainted());
  861. }
  862. #endif /* CONFIG_INT_TAU */
  863. #ifdef CONFIG_ALTIVEC
  864. void altivec_assist_exception(struct pt_regs *regs)
  865. {
  866. int err;
  867. if (!user_mode(regs)) {
  868. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  869. " at %lx\n", regs->nip);
  870. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  871. }
  872. flush_altivec_to_thread(current);
  873. err = emulate_altivec(regs);
  874. if (err == 0) {
  875. regs->nip += 4; /* skip emulated instruction */
  876. emulate_single_step(regs);
  877. return;
  878. }
  879. if (err == -EFAULT) {
  880. /* got an error reading the instruction */
  881. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  882. } else {
  883. /* didn't recognize the instruction */
  884. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  885. if (printk_ratelimit())
  886. printk(KERN_ERR "Unrecognized altivec instruction "
  887. "in %s at %lx\n", current->comm, regs->nip);
  888. current->thread.vscr.u[3] |= 0x10000;
  889. }
  890. }
  891. #endif /* CONFIG_ALTIVEC */
  892. #ifdef CONFIG_FSL_BOOKE
  893. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  894. unsigned long error_code)
  895. {
  896. /* We treat cache locking instructions from the user
  897. * as priv ops, in the future we could try to do
  898. * something smarter
  899. */
  900. if (error_code & (ESR_DLK|ESR_ILK))
  901. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  902. return;
  903. }
  904. #endif /* CONFIG_FSL_BOOKE */
  905. #ifdef CONFIG_SPE
  906. void SPEFloatingPointException(struct pt_regs *regs)
  907. {
  908. unsigned long spefscr;
  909. int fpexc_mode;
  910. int code = 0;
  911. spefscr = current->thread.spefscr;
  912. fpexc_mode = current->thread.fpexc_mode;
  913. /* Hardware does not neccessarily set sticky
  914. * underflow/overflow/invalid flags */
  915. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  916. code = FPE_FLTOVF;
  917. spefscr |= SPEFSCR_FOVFS;
  918. }
  919. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  920. code = FPE_FLTUND;
  921. spefscr |= SPEFSCR_FUNFS;
  922. }
  923. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  924. code = FPE_FLTDIV;
  925. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  926. code = FPE_FLTINV;
  927. spefscr |= SPEFSCR_FINVS;
  928. }
  929. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  930. code = FPE_FLTRES;
  931. current->thread.spefscr = spefscr;
  932. _exception(SIGFPE, regs, code, regs->nip);
  933. return;
  934. }
  935. #endif
  936. /*
  937. * We enter here if we get an unrecoverable exception, that is, one
  938. * that happened at a point where the RI (recoverable interrupt) bit
  939. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  940. * we therefore lost state by taking this exception.
  941. */
  942. void unrecoverable_exception(struct pt_regs *regs)
  943. {
  944. printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
  945. regs->trap, regs->nip);
  946. die("Unrecoverable exception", regs, SIGABRT);
  947. }
  948. #ifdef CONFIG_BOOKE_WDT
  949. /*
  950. * Default handler for a Watchdog exception,
  951. * spins until a reboot occurs
  952. */
  953. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  954. {
  955. /* Generic WatchdogHandler, implement your own */
  956. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  957. return;
  958. }
  959. void WatchdogException(struct pt_regs *regs)
  960. {
  961. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  962. WatchdogHandler(regs);
  963. }
  964. #endif
  965. /*
  966. * We enter here if we discover during exception entry that we are
  967. * running in supervisor mode with a userspace value in the stack pointer.
  968. */
  969. void kernel_bad_stack(struct pt_regs *regs)
  970. {
  971. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  972. regs->gpr[1], regs->nip);
  973. die("Bad kernel stack pointer", regs, SIGABRT);
  974. }
  975. void __init trap_init(void)
  976. {
  977. }