traps.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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/interrupt.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/prctl.h>
  28. #include <linux/delay.h>
  29. #include <linux/kprobes.h>
  30. #include <linux/kexec.h>
  31. #include <linux/backlight.h>
  32. #include <linux/bug.h>
  33. #include <linux/kdebug.h>
  34. #include <linux/debugfs.h>
  35. #include <asm/emulated_ops.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/system.h>
  39. #include <asm/io.h>
  40. #include <asm/machdep.h>
  41. #include <asm/rtas.h>
  42. #include <asm/pmc.h>
  43. #ifdef CONFIG_PPC32
  44. #include <asm/reg.h>
  45. #endif
  46. #ifdef CONFIG_PMAC_BACKLIGHT
  47. #include <asm/backlight.h>
  48. #endif
  49. #ifdef CONFIG_PPC64
  50. #include <asm/firmware.h>
  51. #include <asm/processor.h>
  52. #endif
  53. #include <asm/kexec.h>
  54. #include <asm/ppc-opcode.h>
  55. #ifdef CONFIG_FSL_BOOKE
  56. #include <asm/dbell.h>
  57. #endif
  58. #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
  59. int (*__debugger)(struct pt_regs *regs) __read_mostly;
  60. int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
  61. int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
  62. int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
  63. int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
  64. int (*__debugger_dabr_match)(struct pt_regs *regs) __read_mostly;
  65. int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
  66. EXPORT_SYMBOL(__debugger);
  67. EXPORT_SYMBOL(__debugger_ipi);
  68. EXPORT_SYMBOL(__debugger_bpt);
  69. EXPORT_SYMBOL(__debugger_sstep);
  70. EXPORT_SYMBOL(__debugger_iabr_match);
  71. EXPORT_SYMBOL(__debugger_dabr_match);
  72. EXPORT_SYMBOL(__debugger_fault_handler);
  73. #endif
  74. /*
  75. * Trap & Exception support
  76. */
  77. #ifdef CONFIG_PMAC_BACKLIGHT
  78. static void pmac_backlight_unblank(void)
  79. {
  80. mutex_lock(&pmac_backlight_mutex);
  81. if (pmac_backlight) {
  82. struct backlight_properties *props;
  83. props = &pmac_backlight->props;
  84. props->brightness = props->max_brightness;
  85. props->power = FB_BLANK_UNBLANK;
  86. backlight_update_status(pmac_backlight);
  87. }
  88. mutex_unlock(&pmac_backlight_mutex);
  89. }
  90. #else
  91. static inline void pmac_backlight_unblank(void) { }
  92. #endif
  93. int die(const char *str, struct pt_regs *regs, long err)
  94. {
  95. static struct {
  96. spinlock_t lock;
  97. u32 lock_owner;
  98. int lock_owner_depth;
  99. } die = {
  100. .lock = __SPIN_LOCK_UNLOCKED(die.lock),
  101. .lock_owner = -1,
  102. .lock_owner_depth = 0
  103. };
  104. static int die_counter;
  105. unsigned long flags;
  106. if (debugger(regs))
  107. return 1;
  108. oops_enter();
  109. if (die.lock_owner != raw_smp_processor_id()) {
  110. console_verbose();
  111. spin_lock_irqsave(&die.lock, flags);
  112. die.lock_owner = smp_processor_id();
  113. die.lock_owner_depth = 0;
  114. bust_spinlocks(1);
  115. if (machine_is(powermac))
  116. pmac_backlight_unblank();
  117. } else {
  118. local_save_flags(flags);
  119. }
  120. if (++die.lock_owner_depth < 3) {
  121. printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
  122. #ifdef CONFIG_PREEMPT
  123. printk("PREEMPT ");
  124. #endif
  125. #ifdef CONFIG_SMP
  126. printk("SMP NR_CPUS=%d ", NR_CPUS);
  127. #endif
  128. #ifdef CONFIG_DEBUG_PAGEALLOC
  129. printk("DEBUG_PAGEALLOC ");
  130. #endif
  131. #ifdef CONFIG_NUMA
  132. printk("NUMA ");
  133. #endif
  134. printk("%s\n", ppc_md.name ? ppc_md.name : "");
  135. sysfs_printk_last_file();
  136. if (notify_die(DIE_OOPS, str, regs, err, 255,
  137. SIGSEGV) == NOTIFY_STOP)
  138. return 1;
  139. print_modules();
  140. show_regs(regs);
  141. } else {
  142. printk("Recursive die() failure, output suppressed\n");
  143. }
  144. bust_spinlocks(0);
  145. die.lock_owner = -1;
  146. add_taint(TAINT_DIE);
  147. spin_unlock_irqrestore(&die.lock, flags);
  148. if (kexec_should_crash(current) ||
  149. kexec_sr_activated(smp_processor_id()))
  150. crash_kexec(regs);
  151. crash_kexec_secondary(regs);
  152. if (in_interrupt())
  153. panic("Fatal exception in interrupt");
  154. if (panic_on_oops)
  155. panic("Fatal exception");
  156. oops_exit();
  157. do_exit(err);
  158. return 0;
  159. }
  160. void user_single_step_siginfo(struct task_struct *tsk,
  161. struct pt_regs *regs, siginfo_t *info)
  162. {
  163. memset(info, 0, sizeof(*info));
  164. info->si_signo = SIGTRAP;
  165. info->si_code = TRAP_TRACE;
  166. info->si_addr = (void __user *)regs->nip;
  167. }
  168. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  169. {
  170. siginfo_t info;
  171. const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
  172. "at %08lx nip %08lx lr %08lx code %x\n";
  173. const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
  174. "at %016lx nip %016lx lr %016lx code %x\n";
  175. if (!user_mode(regs)) {
  176. if (die("Exception in kernel mode", regs, signr))
  177. return;
  178. } else if (show_unhandled_signals &&
  179. unhandled_signal(current, signr) &&
  180. printk_ratelimit()) {
  181. printk(regs->msr & MSR_SF ? fmt64 : fmt32,
  182. current->comm, current->pid, signr,
  183. addr, regs->nip, regs->link, code);
  184. }
  185. memset(&info, 0, sizeof(info));
  186. info.si_signo = signr;
  187. info.si_code = code;
  188. info.si_addr = (void __user *) addr;
  189. force_sig_info(signr, &info, current);
  190. }
  191. #ifdef CONFIG_PPC64
  192. void system_reset_exception(struct pt_regs *regs)
  193. {
  194. /* See if any machine dependent calls */
  195. if (ppc_md.system_reset_exception) {
  196. if (ppc_md.system_reset_exception(regs))
  197. return;
  198. }
  199. #ifdef CONFIG_KEXEC
  200. cpu_set(smp_processor_id(), cpus_in_sr);
  201. #endif
  202. die("System Reset", regs, SIGABRT);
  203. /*
  204. * Some CPUs when released from the debugger will execute this path.
  205. * These CPUs entered the debugger via a soft-reset. If the CPU was
  206. * hung before entering the debugger it will return to the hung
  207. * state when exiting this function. This causes a problem in
  208. * kdump since the hung CPU(s) will not respond to the IPI sent
  209. * from kdump. To prevent the problem we call crash_kexec_secondary()
  210. * here. If a kdump had not been initiated or we exit the debugger
  211. * with the "exit and recover" command (x) crash_kexec_secondary()
  212. * will return after 5ms and the CPU returns to its previous state.
  213. */
  214. crash_kexec_secondary(regs);
  215. /* Must die if the interrupt is not recoverable */
  216. if (!(regs->msr & MSR_RI))
  217. panic("Unrecoverable System Reset");
  218. /* What should we do here? We could issue a shutdown or hard reset. */
  219. }
  220. #endif
  221. /*
  222. * I/O accesses can cause machine checks on powermacs.
  223. * Check if the NIP corresponds to the address of a sync
  224. * instruction for which there is an entry in the exception
  225. * table.
  226. * Note that the 601 only takes a machine check on TEA
  227. * (transfer error ack) signal assertion, and does not
  228. * set any of the top 16 bits of SRR1.
  229. * -- paulus.
  230. */
  231. static inline int check_io_access(struct pt_regs *regs)
  232. {
  233. #ifdef CONFIG_PPC32
  234. unsigned long msr = regs->msr;
  235. const struct exception_table_entry *entry;
  236. unsigned int *nip = (unsigned int *)regs->nip;
  237. if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
  238. && (entry = search_exception_tables(regs->nip)) != NULL) {
  239. /*
  240. * Check that it's a sync instruction, or somewhere
  241. * in the twi; isync; nop sequence that inb/inw/inl uses.
  242. * As the address is in the exception table
  243. * we should be able to read the instr there.
  244. * For the debug message, we look at the preceding
  245. * load or store.
  246. */
  247. if (*nip == 0x60000000) /* nop */
  248. nip -= 2;
  249. else if (*nip == 0x4c00012c) /* isync */
  250. --nip;
  251. if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
  252. /* sync or twi */
  253. unsigned int rb;
  254. --nip;
  255. rb = (*nip >> 11) & 0x1f;
  256. printk(KERN_DEBUG "%s bad port %lx at %p\n",
  257. (*nip & 0x100)? "OUT to": "IN from",
  258. regs->gpr[rb] - _IO_BASE, nip);
  259. regs->msr |= MSR_RI;
  260. regs->nip = entry->fixup;
  261. return 1;
  262. }
  263. }
  264. #endif /* CONFIG_PPC32 */
  265. return 0;
  266. }
  267. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  268. /* On 4xx, the reason for the machine check or program exception
  269. is in the ESR. */
  270. #define get_reason(regs) ((regs)->dsisr)
  271. #ifndef CONFIG_FSL_BOOKE
  272. #define get_mc_reason(regs) ((regs)->dsisr)
  273. #else
  274. #define get_mc_reason(regs) (mfspr(SPRN_MCSR) & MCSR_MASK)
  275. #endif
  276. #define REASON_FP ESR_FP
  277. #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
  278. #define REASON_PRIVILEGED ESR_PPR
  279. #define REASON_TRAP ESR_PTR
  280. /* single-step stuff */
  281. #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
  282. #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
  283. #else
  284. /* On non-4xx, the reason for the machine check or program
  285. exception is in the MSR. */
  286. #define get_reason(regs) ((regs)->msr)
  287. #define get_mc_reason(regs) ((regs)->msr)
  288. #define REASON_FP 0x100000
  289. #define REASON_ILLEGAL 0x80000
  290. #define REASON_PRIVILEGED 0x40000
  291. #define REASON_TRAP 0x20000
  292. #define single_stepping(regs) ((regs)->msr & MSR_SE)
  293. #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
  294. #endif
  295. #if defined(CONFIG_4xx)
  296. int machine_check_4xx(struct pt_regs *regs)
  297. {
  298. unsigned long reason = get_mc_reason(regs);
  299. if (reason & ESR_IMCP) {
  300. printk("Instruction");
  301. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  302. } else
  303. printk("Data");
  304. printk(" machine check in kernel mode.\n");
  305. return 0;
  306. }
  307. int machine_check_440A(struct pt_regs *regs)
  308. {
  309. unsigned long reason = get_mc_reason(regs);
  310. printk("Machine check in kernel mode.\n");
  311. if (reason & ESR_IMCP){
  312. printk("Instruction Synchronous Machine Check exception\n");
  313. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  314. }
  315. else {
  316. u32 mcsr = mfspr(SPRN_MCSR);
  317. if (mcsr & MCSR_IB)
  318. printk("Instruction Read PLB Error\n");
  319. if (mcsr & MCSR_DRB)
  320. printk("Data Read PLB Error\n");
  321. if (mcsr & MCSR_DWB)
  322. printk("Data Write PLB Error\n");
  323. if (mcsr & MCSR_TLBP)
  324. printk("TLB Parity Error\n");
  325. if (mcsr & MCSR_ICP){
  326. flush_instruction_cache();
  327. printk("I-Cache Parity Error\n");
  328. }
  329. if (mcsr & MCSR_DCSP)
  330. printk("D-Cache Search Parity Error\n");
  331. if (mcsr & MCSR_DCFP)
  332. printk("D-Cache Flush Parity Error\n");
  333. if (mcsr & MCSR_IMPE)
  334. printk("Machine Check exception is imprecise\n");
  335. /* Clear MCSR */
  336. mtspr(SPRN_MCSR, mcsr);
  337. }
  338. return 0;
  339. }
  340. #elif defined(CONFIG_E500)
  341. int machine_check_e500(struct pt_regs *regs)
  342. {
  343. unsigned long reason = get_mc_reason(regs);
  344. printk("Machine check in kernel mode.\n");
  345. printk("Caused by (from MCSR=%lx): ", reason);
  346. if (reason & MCSR_MCP)
  347. printk("Machine Check Signal\n");
  348. if (reason & MCSR_ICPERR)
  349. printk("Instruction Cache Parity Error\n");
  350. if (reason & MCSR_DCP_PERR)
  351. printk("Data Cache Push Parity Error\n");
  352. if (reason & MCSR_DCPERR)
  353. printk("Data Cache Parity Error\n");
  354. if (reason & MCSR_BUS_IAERR)
  355. printk("Bus - Instruction Address Error\n");
  356. if (reason & MCSR_BUS_RAERR)
  357. printk("Bus - Read Address Error\n");
  358. if (reason & MCSR_BUS_WAERR)
  359. printk("Bus - Write Address Error\n");
  360. if (reason & MCSR_BUS_IBERR)
  361. printk("Bus - Instruction Data Error\n");
  362. if (reason & MCSR_BUS_RBERR)
  363. printk("Bus - Read Data Bus Error\n");
  364. if (reason & MCSR_BUS_WBERR)
  365. printk("Bus - Read Data Bus Error\n");
  366. if (reason & MCSR_BUS_IPERR)
  367. printk("Bus - Instruction Parity Error\n");
  368. if (reason & MCSR_BUS_RPERR)
  369. printk("Bus - Read Parity Error\n");
  370. return 0;
  371. }
  372. #elif defined(CONFIG_E200)
  373. int machine_check_e200(struct pt_regs *regs)
  374. {
  375. unsigned long reason = get_mc_reason(regs);
  376. printk("Machine check in kernel mode.\n");
  377. printk("Caused by (from MCSR=%lx): ", reason);
  378. if (reason & MCSR_MCP)
  379. printk("Machine Check Signal\n");
  380. if (reason & MCSR_CP_PERR)
  381. printk("Cache Push Parity Error\n");
  382. if (reason & MCSR_CPERR)
  383. printk("Cache Parity Error\n");
  384. if (reason & MCSR_EXCP_ERR)
  385. printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
  386. if (reason & MCSR_BUS_IRERR)
  387. printk("Bus - Read Bus Error on instruction fetch\n");
  388. if (reason & MCSR_BUS_DRERR)
  389. printk("Bus - Read Bus Error on data load\n");
  390. if (reason & MCSR_BUS_WRERR)
  391. printk("Bus - Write Bus Error on buffered store or cache line push\n");
  392. return 0;
  393. }
  394. #else
  395. int machine_check_generic(struct pt_regs *regs)
  396. {
  397. unsigned long reason = get_mc_reason(regs);
  398. printk("Machine check in kernel mode.\n");
  399. printk("Caused by (from SRR1=%lx): ", reason);
  400. switch (reason & 0x601F0000) {
  401. case 0x80000:
  402. printk("Machine check signal\n");
  403. break;
  404. case 0: /* for 601 */
  405. case 0x40000:
  406. case 0x140000: /* 7450 MSS error and TEA */
  407. printk("Transfer error ack signal\n");
  408. break;
  409. case 0x20000:
  410. printk("Data parity error signal\n");
  411. break;
  412. case 0x10000:
  413. printk("Address parity error signal\n");
  414. break;
  415. case 0x20000000:
  416. printk("L1 Data Cache error\n");
  417. break;
  418. case 0x40000000:
  419. printk("L1 Instruction Cache error\n");
  420. break;
  421. case 0x00100000:
  422. printk("L2 data cache parity error\n");
  423. break;
  424. default:
  425. printk("Unknown values in msr\n");
  426. }
  427. return 0;
  428. }
  429. #endif /* everything else */
  430. void machine_check_exception(struct pt_regs *regs)
  431. {
  432. int recover = 0;
  433. /* See if any machine dependent calls. In theory, we would want
  434. * to call the CPU first, and call the ppc_md. one if the CPU
  435. * one returns a positive number. However there is existing code
  436. * that assumes the board gets a first chance, so let's keep it
  437. * that way for now and fix things later. --BenH.
  438. */
  439. if (ppc_md.machine_check_exception)
  440. recover = ppc_md.machine_check_exception(regs);
  441. else if (cur_cpu_spec->machine_check)
  442. recover = cur_cpu_spec->machine_check(regs);
  443. if (recover > 0)
  444. return;
  445. if (user_mode(regs)) {
  446. regs->msr |= MSR_RI;
  447. _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
  448. return;
  449. }
  450. #if defined(CONFIG_8xx) && defined(CONFIG_PCI)
  451. /* the qspan pci read routines can cause machine checks -- Cort
  452. *
  453. * yuck !!! that totally needs to go away ! There are better ways
  454. * to deal with that than having a wart in the mcheck handler.
  455. * -- BenH
  456. */
  457. bad_page_fault(regs, regs->dar, SIGBUS);
  458. return;
  459. #endif
  460. if (debugger_fault_handler(regs)) {
  461. regs->msr |= MSR_RI;
  462. return;
  463. }
  464. if (check_io_access(regs))
  465. return;
  466. if (debugger_fault_handler(regs))
  467. return;
  468. die("Machine check", regs, SIGBUS);
  469. /* Must die if the interrupt is not recoverable */
  470. if (!(regs->msr & MSR_RI))
  471. panic("Unrecoverable Machine check");
  472. }
  473. void SMIException(struct pt_regs *regs)
  474. {
  475. die("System Management Interrupt", regs, SIGABRT);
  476. }
  477. void unknown_exception(struct pt_regs *regs)
  478. {
  479. printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
  480. regs->nip, regs->msr, regs->trap);
  481. _exception(SIGTRAP, regs, 0, 0);
  482. }
  483. void instruction_breakpoint_exception(struct pt_regs *regs)
  484. {
  485. if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
  486. 5, SIGTRAP) == NOTIFY_STOP)
  487. return;
  488. if (debugger_iabr_match(regs))
  489. return;
  490. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  491. }
  492. void RunModeException(struct pt_regs *regs)
  493. {
  494. _exception(SIGTRAP, regs, 0, 0);
  495. }
  496. void __kprobes single_step_exception(struct pt_regs *regs)
  497. {
  498. regs->msr &= ~(MSR_SE | MSR_BE); /* Turn off 'trace' bits */
  499. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  500. 5, SIGTRAP) == NOTIFY_STOP)
  501. return;
  502. if (debugger_sstep(regs))
  503. return;
  504. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  505. }
  506. /*
  507. * After we have successfully emulated an instruction, we have to
  508. * check if the instruction was being single-stepped, and if so,
  509. * pretend we got a single-step exception. This was pointed out
  510. * by Kumar Gala. -- paulus
  511. */
  512. static void emulate_single_step(struct pt_regs *regs)
  513. {
  514. if (single_stepping(regs)) {
  515. clear_single_step(regs);
  516. _exception(SIGTRAP, regs, TRAP_TRACE, 0);
  517. }
  518. }
  519. static inline int __parse_fpscr(unsigned long fpscr)
  520. {
  521. int ret = 0;
  522. /* Invalid operation */
  523. if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
  524. ret = FPE_FLTINV;
  525. /* Overflow */
  526. else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
  527. ret = FPE_FLTOVF;
  528. /* Underflow */
  529. else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
  530. ret = FPE_FLTUND;
  531. /* Divide by zero */
  532. else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
  533. ret = FPE_FLTDIV;
  534. /* Inexact result */
  535. else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
  536. ret = FPE_FLTRES;
  537. return ret;
  538. }
  539. static void parse_fpe(struct pt_regs *regs)
  540. {
  541. int code = 0;
  542. flush_fp_to_thread(current);
  543. code = __parse_fpscr(current->thread.fpscr.val);
  544. _exception(SIGFPE, regs, code, regs->nip);
  545. }
  546. /*
  547. * Illegal instruction emulation support. Originally written to
  548. * provide the PVR to user applications using the mfspr rd, PVR.
  549. * Return non-zero if we can't emulate, or -EFAULT if the associated
  550. * memory access caused an access fault. Return zero on success.
  551. *
  552. * There are a couple of ways to do this, either "decode" the instruction
  553. * or directly match lots of bits. In this case, matching lots of
  554. * bits is faster and easier.
  555. *
  556. */
  557. static int emulate_string_inst(struct pt_regs *regs, u32 instword)
  558. {
  559. u8 rT = (instword >> 21) & 0x1f;
  560. u8 rA = (instword >> 16) & 0x1f;
  561. u8 NB_RB = (instword >> 11) & 0x1f;
  562. u32 num_bytes;
  563. unsigned long EA;
  564. int pos = 0;
  565. /* Early out if we are an invalid form of lswx */
  566. if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
  567. if ((rT == rA) || (rT == NB_RB))
  568. return -EINVAL;
  569. EA = (rA == 0) ? 0 : regs->gpr[rA];
  570. switch (instword & PPC_INST_STRING_MASK) {
  571. case PPC_INST_LSWX:
  572. case PPC_INST_STSWX:
  573. EA += NB_RB;
  574. num_bytes = regs->xer & 0x7f;
  575. break;
  576. case PPC_INST_LSWI:
  577. case PPC_INST_STSWI:
  578. num_bytes = (NB_RB == 0) ? 32 : NB_RB;
  579. break;
  580. default:
  581. return -EINVAL;
  582. }
  583. while (num_bytes != 0)
  584. {
  585. u8 val;
  586. u32 shift = 8 * (3 - (pos & 0x3));
  587. switch ((instword & PPC_INST_STRING_MASK)) {
  588. case PPC_INST_LSWX:
  589. case PPC_INST_LSWI:
  590. if (get_user(val, (u8 __user *)EA))
  591. return -EFAULT;
  592. /* first time updating this reg,
  593. * zero it out */
  594. if (pos == 0)
  595. regs->gpr[rT] = 0;
  596. regs->gpr[rT] |= val << shift;
  597. break;
  598. case PPC_INST_STSWI:
  599. case PPC_INST_STSWX:
  600. val = regs->gpr[rT] >> shift;
  601. if (put_user(val, (u8 __user *)EA))
  602. return -EFAULT;
  603. break;
  604. }
  605. /* move EA to next address */
  606. EA += 1;
  607. num_bytes--;
  608. /* manage our position within the register */
  609. if (++pos == 4) {
  610. pos = 0;
  611. if (++rT == 32)
  612. rT = 0;
  613. }
  614. }
  615. return 0;
  616. }
  617. static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
  618. {
  619. u32 ra,rs;
  620. unsigned long tmp;
  621. ra = (instword >> 16) & 0x1f;
  622. rs = (instword >> 21) & 0x1f;
  623. tmp = regs->gpr[rs];
  624. tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
  625. tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
  626. tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
  627. regs->gpr[ra] = tmp;
  628. return 0;
  629. }
  630. static int emulate_isel(struct pt_regs *regs, u32 instword)
  631. {
  632. u8 rT = (instword >> 21) & 0x1f;
  633. u8 rA = (instword >> 16) & 0x1f;
  634. u8 rB = (instword >> 11) & 0x1f;
  635. u8 BC = (instword >> 6) & 0x1f;
  636. u8 bit;
  637. unsigned long tmp;
  638. tmp = (rA == 0) ? 0 : regs->gpr[rA];
  639. bit = (regs->ccr >> (31 - BC)) & 0x1;
  640. regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
  641. return 0;
  642. }
  643. static int emulate_instruction(struct pt_regs *regs)
  644. {
  645. u32 instword;
  646. u32 rd;
  647. if (!user_mode(regs) || (regs->msr & MSR_LE))
  648. return -EINVAL;
  649. CHECK_FULL_REGS(regs);
  650. if (get_user(instword, (u32 __user *)(regs->nip)))
  651. return -EFAULT;
  652. /* Emulate the mfspr rD, PVR. */
  653. if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
  654. PPC_WARN_EMULATED(mfpvr, regs);
  655. rd = (instword >> 21) & 0x1f;
  656. regs->gpr[rd] = mfspr(SPRN_PVR);
  657. return 0;
  658. }
  659. /* Emulating the dcba insn is just a no-op. */
  660. if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
  661. PPC_WARN_EMULATED(dcba, regs);
  662. return 0;
  663. }
  664. /* Emulate the mcrxr insn. */
  665. if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
  666. int shift = (instword >> 21) & 0x1c;
  667. unsigned long msk = 0xf0000000UL >> shift;
  668. PPC_WARN_EMULATED(mcrxr, regs);
  669. regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
  670. regs->xer &= ~0xf0000000UL;
  671. return 0;
  672. }
  673. /* Emulate load/store string insn. */
  674. if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
  675. PPC_WARN_EMULATED(string, regs);
  676. return emulate_string_inst(regs, instword);
  677. }
  678. /* Emulate the popcntb (Population Count Bytes) instruction. */
  679. if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
  680. PPC_WARN_EMULATED(popcntb, regs);
  681. return emulate_popcntb_inst(regs, instword);
  682. }
  683. /* Emulate isel (Integer Select) instruction */
  684. if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
  685. PPC_WARN_EMULATED(isel, regs);
  686. return emulate_isel(regs, instword);
  687. }
  688. return -EINVAL;
  689. }
  690. int is_valid_bugaddr(unsigned long addr)
  691. {
  692. return is_kernel_addr(addr);
  693. }
  694. void __kprobes program_check_exception(struct pt_regs *regs)
  695. {
  696. unsigned int reason = get_reason(regs);
  697. extern int do_mathemu(struct pt_regs *regs);
  698. /* We can now get here via a FP Unavailable exception if the core
  699. * has no FPU, in that case the reason flags will be 0 */
  700. if (reason & REASON_FP) {
  701. /* IEEE FP exception */
  702. parse_fpe(regs);
  703. return;
  704. }
  705. if (reason & REASON_TRAP) {
  706. /* trap exception */
  707. if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
  708. == NOTIFY_STOP)
  709. return;
  710. if (debugger_bpt(regs))
  711. return;
  712. if (!(regs->msr & MSR_PR) && /* not user-mode */
  713. report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
  714. regs->nip += 4;
  715. return;
  716. }
  717. _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
  718. return;
  719. }
  720. local_irq_enable();
  721. #ifdef CONFIG_MATH_EMULATION
  722. /* (reason & REASON_ILLEGAL) would be the obvious thing here,
  723. * but there seems to be a hardware bug on the 405GP (RevD)
  724. * that means ESR is sometimes set incorrectly - either to
  725. * ESR_DST (!?) or 0. In the process of chasing this with the
  726. * hardware people - not sure if it can happen on any illegal
  727. * instruction or only on FP instructions, whether there is a
  728. * pattern to occurences etc. -dgibson 31/Mar/2003 */
  729. switch (do_mathemu(regs)) {
  730. case 0:
  731. emulate_single_step(regs);
  732. return;
  733. case 1: {
  734. int code = 0;
  735. code = __parse_fpscr(current->thread.fpscr.val);
  736. _exception(SIGFPE, regs, code, regs->nip);
  737. return;
  738. }
  739. case -EFAULT:
  740. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  741. return;
  742. }
  743. /* fall through on any other errors */
  744. #endif /* CONFIG_MATH_EMULATION */
  745. /* Try to emulate it if we should. */
  746. if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
  747. switch (emulate_instruction(regs)) {
  748. case 0:
  749. regs->nip += 4;
  750. emulate_single_step(regs);
  751. return;
  752. case -EFAULT:
  753. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  754. return;
  755. }
  756. }
  757. if (reason & REASON_PRIVILEGED)
  758. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  759. else
  760. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  761. }
  762. void alignment_exception(struct pt_regs *regs)
  763. {
  764. int sig, code, fixed = 0;
  765. /* we don't implement logging of alignment exceptions */
  766. if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
  767. fixed = fix_alignment(regs);
  768. if (fixed == 1) {
  769. regs->nip += 4; /* skip over emulated instruction */
  770. emulate_single_step(regs);
  771. return;
  772. }
  773. /* Operand address was bad */
  774. if (fixed == -EFAULT) {
  775. sig = SIGSEGV;
  776. code = SEGV_ACCERR;
  777. } else {
  778. sig = SIGBUS;
  779. code = BUS_ADRALN;
  780. }
  781. if (user_mode(regs))
  782. _exception(sig, regs, code, regs->dar);
  783. else
  784. bad_page_fault(regs, regs->dar, sig);
  785. }
  786. void StackOverflow(struct pt_regs *regs)
  787. {
  788. printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
  789. current, regs->gpr[1]);
  790. debugger(regs);
  791. show_regs(regs);
  792. panic("kernel stack overflow");
  793. }
  794. void nonrecoverable_exception(struct pt_regs *regs)
  795. {
  796. printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
  797. regs->nip, regs->msr);
  798. debugger(regs);
  799. die("nonrecoverable exception", regs, SIGKILL);
  800. }
  801. void trace_syscall(struct pt_regs *regs)
  802. {
  803. printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
  804. current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
  805. regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
  806. }
  807. void kernel_fp_unavailable_exception(struct pt_regs *regs)
  808. {
  809. printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
  810. "%lx at %lx\n", regs->trap, regs->nip);
  811. die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
  812. }
  813. void altivec_unavailable_exception(struct pt_regs *regs)
  814. {
  815. if (user_mode(regs)) {
  816. /* A user program has executed an altivec instruction,
  817. but this kernel doesn't support altivec. */
  818. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  819. return;
  820. }
  821. printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
  822. "%lx at %lx\n", regs->trap, regs->nip);
  823. die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
  824. }
  825. void vsx_unavailable_exception(struct pt_regs *regs)
  826. {
  827. if (user_mode(regs)) {
  828. /* A user program has executed an vsx instruction,
  829. but this kernel doesn't support vsx. */
  830. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  831. return;
  832. }
  833. printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
  834. "%lx at %lx\n", regs->trap, regs->nip);
  835. die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
  836. }
  837. void performance_monitor_exception(struct pt_regs *regs)
  838. {
  839. perf_irq(regs);
  840. }
  841. #ifdef CONFIG_8xx
  842. void SoftwareEmulation(struct pt_regs *regs)
  843. {
  844. extern int do_mathemu(struct pt_regs *);
  845. extern int Soft_emulate_8xx(struct pt_regs *);
  846. #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
  847. int errcode;
  848. #endif
  849. CHECK_FULL_REGS(regs);
  850. if (!user_mode(regs)) {
  851. debugger(regs);
  852. die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
  853. }
  854. #ifdef CONFIG_MATH_EMULATION
  855. errcode = do_mathemu(regs);
  856. if (errcode >= 0)
  857. PPC_WARN_EMULATED(math, regs);
  858. switch (errcode) {
  859. case 0:
  860. emulate_single_step(regs);
  861. return;
  862. case 1: {
  863. int code = 0;
  864. code = __parse_fpscr(current->thread.fpscr.val);
  865. _exception(SIGFPE, regs, code, regs->nip);
  866. return;
  867. }
  868. case -EFAULT:
  869. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  870. return;
  871. default:
  872. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  873. return;
  874. }
  875. #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
  876. errcode = Soft_emulate_8xx(regs);
  877. if (errcode >= 0)
  878. PPC_WARN_EMULATED(8xx, regs);
  879. switch (errcode) {
  880. case 0:
  881. emulate_single_step(regs);
  882. return;
  883. case 1:
  884. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  885. return;
  886. case -EFAULT:
  887. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  888. return;
  889. }
  890. #else
  891. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  892. #endif
  893. }
  894. #endif /* CONFIG_8xx */
  895. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  896. void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
  897. {
  898. /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
  899. * on server, it stops on the target of the branch. In order to simulate
  900. * the server behaviour, we thus restart right away with a single step
  901. * instead of stopping here when hitting a BT
  902. */
  903. if (debug_status & DBSR_BT) {
  904. regs->msr &= ~MSR_DE;
  905. /* Disable BT */
  906. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
  907. /* Clear the BT event */
  908. mtspr(SPRN_DBSR, DBSR_BT);
  909. /* Do the single step trick only when coming from userspace */
  910. if (user_mode(regs)) {
  911. current->thread.dbcr0 &= ~DBCR0_BT;
  912. current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
  913. regs->msr |= MSR_DE;
  914. return;
  915. }
  916. if (notify_die(DIE_SSTEP, "block_step", regs, 5,
  917. 5, SIGTRAP) == NOTIFY_STOP) {
  918. return;
  919. }
  920. if (debugger_sstep(regs))
  921. return;
  922. } else if (debug_status & DBSR_IC) { /* Instruction complete */
  923. regs->msr &= ~MSR_DE;
  924. /* Disable instruction completion */
  925. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  926. /* Clear the instruction completion event */
  927. mtspr(SPRN_DBSR, DBSR_IC);
  928. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  929. 5, SIGTRAP) == NOTIFY_STOP) {
  930. return;
  931. }
  932. if (debugger_sstep(regs))
  933. return;
  934. if (user_mode(regs))
  935. current->thread.dbcr0 &= ~(DBCR0_IC);
  936. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  937. } else if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
  938. regs->msr &= ~MSR_DE;
  939. if (user_mode(regs)) {
  940. current->thread.dbcr0 &= ~(DBSR_DAC1R | DBSR_DAC1W |
  941. DBCR0_IDM);
  942. } else {
  943. /* Disable DAC interupts */
  944. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~(DBSR_DAC1R |
  945. DBSR_DAC1W | DBCR0_IDM));
  946. /* Clear the DAC event */
  947. mtspr(SPRN_DBSR, (DBSR_DAC1R | DBSR_DAC1W));
  948. }
  949. /* Setup and send the trap to the handler */
  950. do_dabr(regs, mfspr(SPRN_DAC1), debug_status);
  951. }
  952. }
  953. #endif /* CONFIG_4xx || CONFIG_BOOKE */
  954. #if !defined(CONFIG_TAU_INT)
  955. void TAUException(struct pt_regs *regs)
  956. {
  957. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  958. regs->nip, regs->msr, regs->trap, print_tainted());
  959. }
  960. #endif /* CONFIG_INT_TAU */
  961. #ifdef CONFIG_ALTIVEC
  962. void altivec_assist_exception(struct pt_regs *regs)
  963. {
  964. int err;
  965. if (!user_mode(regs)) {
  966. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  967. " at %lx\n", regs->nip);
  968. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  969. }
  970. flush_altivec_to_thread(current);
  971. PPC_WARN_EMULATED(altivec, regs);
  972. err = emulate_altivec(regs);
  973. if (err == 0) {
  974. regs->nip += 4; /* skip emulated instruction */
  975. emulate_single_step(regs);
  976. return;
  977. }
  978. if (err == -EFAULT) {
  979. /* got an error reading the instruction */
  980. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  981. } else {
  982. /* didn't recognize the instruction */
  983. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  984. if (printk_ratelimit())
  985. printk(KERN_ERR "Unrecognized altivec instruction "
  986. "in %s at %lx\n", current->comm, regs->nip);
  987. current->thread.vscr.u[3] |= 0x10000;
  988. }
  989. }
  990. #endif /* CONFIG_ALTIVEC */
  991. #ifdef CONFIG_VSX
  992. void vsx_assist_exception(struct pt_regs *regs)
  993. {
  994. if (!user_mode(regs)) {
  995. printk(KERN_EMERG "VSX assist exception in kernel mode"
  996. " at %lx\n", regs->nip);
  997. die("Kernel VSX assist exception", regs, SIGILL);
  998. }
  999. flush_vsx_to_thread(current);
  1000. printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
  1001. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1002. }
  1003. #endif /* CONFIG_VSX */
  1004. #ifdef CONFIG_FSL_BOOKE
  1005. void doorbell_exception(struct pt_regs *regs)
  1006. {
  1007. #ifdef CONFIG_SMP
  1008. int cpu = smp_processor_id();
  1009. int msg;
  1010. if (num_online_cpus() < 2)
  1011. return;
  1012. for (msg = 0; msg < 4; msg++)
  1013. if (test_and_clear_bit(msg, &dbell_smp_message[cpu]))
  1014. smp_message_recv(msg);
  1015. #else
  1016. printk(KERN_WARNING "Received doorbell on non-smp system\n");
  1017. #endif
  1018. }
  1019. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  1020. unsigned long error_code)
  1021. {
  1022. /* We treat cache locking instructions from the user
  1023. * as priv ops, in the future we could try to do
  1024. * something smarter
  1025. */
  1026. if (error_code & (ESR_DLK|ESR_ILK))
  1027. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  1028. return;
  1029. }
  1030. #endif /* CONFIG_FSL_BOOKE */
  1031. #ifdef CONFIG_SPE
  1032. void SPEFloatingPointException(struct pt_regs *regs)
  1033. {
  1034. extern int do_spe_mathemu(struct pt_regs *regs);
  1035. unsigned long spefscr;
  1036. int fpexc_mode;
  1037. int code = 0;
  1038. int err;
  1039. preempt_disable();
  1040. if (regs->msr & MSR_SPE)
  1041. giveup_spe(current);
  1042. preempt_enable();
  1043. spefscr = current->thread.spefscr;
  1044. fpexc_mode = current->thread.fpexc_mode;
  1045. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  1046. code = FPE_FLTOVF;
  1047. }
  1048. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  1049. code = FPE_FLTUND;
  1050. }
  1051. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  1052. code = FPE_FLTDIV;
  1053. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  1054. code = FPE_FLTINV;
  1055. }
  1056. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  1057. code = FPE_FLTRES;
  1058. err = do_spe_mathemu(regs);
  1059. if (err == 0) {
  1060. regs->nip += 4; /* skip emulated instruction */
  1061. emulate_single_step(regs);
  1062. return;
  1063. }
  1064. if (err == -EFAULT) {
  1065. /* got an error reading the instruction */
  1066. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1067. } else if (err == -EINVAL) {
  1068. /* didn't recognize the instruction */
  1069. printk(KERN_ERR "unrecognized spe instruction "
  1070. "in %s at %lx\n", current->comm, regs->nip);
  1071. } else {
  1072. _exception(SIGFPE, regs, code, regs->nip);
  1073. }
  1074. return;
  1075. }
  1076. void SPEFloatingPointRoundException(struct pt_regs *regs)
  1077. {
  1078. extern int speround_handler(struct pt_regs *regs);
  1079. int err;
  1080. preempt_disable();
  1081. if (regs->msr & MSR_SPE)
  1082. giveup_spe(current);
  1083. preempt_enable();
  1084. regs->nip -= 4;
  1085. err = speround_handler(regs);
  1086. if (err == 0) {
  1087. regs->nip += 4; /* skip emulated instruction */
  1088. emulate_single_step(regs);
  1089. return;
  1090. }
  1091. if (err == -EFAULT) {
  1092. /* got an error reading the instruction */
  1093. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1094. } else if (err == -EINVAL) {
  1095. /* didn't recognize the instruction */
  1096. printk(KERN_ERR "unrecognized spe instruction "
  1097. "in %s at %lx\n", current->comm, regs->nip);
  1098. } else {
  1099. _exception(SIGFPE, regs, 0, regs->nip);
  1100. return;
  1101. }
  1102. }
  1103. #endif
  1104. /*
  1105. * We enter here if we get an unrecoverable exception, that is, one
  1106. * that happened at a point where the RI (recoverable interrupt) bit
  1107. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  1108. * we therefore lost state by taking this exception.
  1109. */
  1110. void unrecoverable_exception(struct pt_regs *regs)
  1111. {
  1112. printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
  1113. regs->trap, regs->nip);
  1114. die("Unrecoverable exception", regs, SIGABRT);
  1115. }
  1116. #ifdef CONFIG_BOOKE_WDT
  1117. /*
  1118. * Default handler for a Watchdog exception,
  1119. * spins until a reboot occurs
  1120. */
  1121. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  1122. {
  1123. /* Generic WatchdogHandler, implement your own */
  1124. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  1125. return;
  1126. }
  1127. void WatchdogException(struct pt_regs *regs)
  1128. {
  1129. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  1130. WatchdogHandler(regs);
  1131. }
  1132. #endif
  1133. /*
  1134. * We enter here if we discover during exception entry that we are
  1135. * running in supervisor mode with a userspace value in the stack pointer.
  1136. */
  1137. void kernel_bad_stack(struct pt_regs *regs)
  1138. {
  1139. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  1140. regs->gpr[1], regs->nip);
  1141. die("Bad kernel stack pointer", regs, SIGABRT);
  1142. }
  1143. void __init trap_init(void)
  1144. {
  1145. }
  1146. #ifdef CONFIG_PPC_EMULATED_STATS
  1147. #define WARN_EMULATED_SETUP(type) .type = { .name = #type }
  1148. struct ppc_emulated ppc_emulated = {
  1149. #ifdef CONFIG_ALTIVEC
  1150. WARN_EMULATED_SETUP(altivec),
  1151. #endif
  1152. WARN_EMULATED_SETUP(dcba),
  1153. WARN_EMULATED_SETUP(dcbz),
  1154. WARN_EMULATED_SETUP(fp_pair),
  1155. WARN_EMULATED_SETUP(isel),
  1156. WARN_EMULATED_SETUP(mcrxr),
  1157. WARN_EMULATED_SETUP(mfpvr),
  1158. WARN_EMULATED_SETUP(multiple),
  1159. WARN_EMULATED_SETUP(popcntb),
  1160. WARN_EMULATED_SETUP(spe),
  1161. WARN_EMULATED_SETUP(string),
  1162. WARN_EMULATED_SETUP(unaligned),
  1163. #ifdef CONFIG_MATH_EMULATION
  1164. WARN_EMULATED_SETUP(math),
  1165. #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
  1166. WARN_EMULATED_SETUP(8xx),
  1167. #endif
  1168. #ifdef CONFIG_VSX
  1169. WARN_EMULATED_SETUP(vsx),
  1170. #endif
  1171. };
  1172. u32 ppc_warn_emulated;
  1173. void ppc_warn_emulated_print(const char *type)
  1174. {
  1175. if (printk_ratelimit())
  1176. pr_warning("%s used emulated %s instruction\n", current->comm,
  1177. type);
  1178. }
  1179. static int __init ppc_warn_emulated_init(void)
  1180. {
  1181. struct dentry *dir, *d;
  1182. unsigned int i;
  1183. struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
  1184. if (!powerpc_debugfs_root)
  1185. return -ENODEV;
  1186. dir = debugfs_create_dir("emulated_instructions",
  1187. powerpc_debugfs_root);
  1188. if (!dir)
  1189. return -ENOMEM;
  1190. d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
  1191. &ppc_warn_emulated);
  1192. if (!d)
  1193. goto fail;
  1194. for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
  1195. d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
  1196. (u32 *)&entries[i].val.counter);
  1197. if (!d)
  1198. goto fail;
  1199. }
  1200. return 0;
  1201. fail:
  1202. debugfs_remove_recursive(dir);
  1203. return -ENOMEM;
  1204. }
  1205. device_initcall(ppc_warn_emulated_init);
  1206. #endif /* CONFIG_PPC_EMULATED_STATS */