traps.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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/user.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/prctl.h>
  27. #include <linux/delay.h>
  28. #include <linux/kprobes.h>
  29. #include <linux/kexec.h>
  30. #include <linux/backlight.h>
  31. #include <linux/bug.h>
  32. #include <linux/kdebug.h>
  33. #include <linux/debugfs.h>
  34. #include <asm/emulated_ops.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. #include <asm/ppc-opcode.h>
  54. #ifdef CONFIG_FSL_BOOKE
  55. #include <asm/dbell.h>
  56. #endif
  57. #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
  58. int (*__debugger)(struct pt_regs *regs) __read_mostly;
  59. int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
  60. int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
  61. int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
  62. int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
  63. int (*__debugger_dabr_match)(struct pt_regs *regs) __read_mostly;
  64. int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
  65. EXPORT_SYMBOL(__debugger);
  66. EXPORT_SYMBOL(__debugger_ipi);
  67. EXPORT_SYMBOL(__debugger_bpt);
  68. EXPORT_SYMBOL(__debugger_sstep);
  69. EXPORT_SYMBOL(__debugger_iabr_match);
  70. EXPORT_SYMBOL(__debugger_dabr_match);
  71. EXPORT_SYMBOL(__debugger_fault_handler);
  72. #endif
  73. /*
  74. * Trap & Exception support
  75. */
  76. #ifdef CONFIG_PMAC_BACKLIGHT
  77. static void pmac_backlight_unblank(void)
  78. {
  79. mutex_lock(&pmac_backlight_mutex);
  80. if (pmac_backlight) {
  81. struct backlight_properties *props;
  82. props = &pmac_backlight->props;
  83. props->brightness = props->max_brightness;
  84. props->power = FB_BLANK_UNBLANK;
  85. backlight_update_status(pmac_backlight);
  86. }
  87. mutex_unlock(&pmac_backlight_mutex);
  88. }
  89. #else
  90. static inline void pmac_backlight_unblank(void) { }
  91. #endif
  92. int die(const char *str, struct pt_regs *regs, long err)
  93. {
  94. static struct {
  95. raw_spinlock_t lock;
  96. u32 lock_owner;
  97. int lock_owner_depth;
  98. } die = {
  99. .lock = __RAW_SPIN_LOCK_UNLOCKED(die.lock),
  100. .lock_owner = -1,
  101. .lock_owner_depth = 0
  102. };
  103. static int die_counter;
  104. unsigned long flags;
  105. if (debugger(regs))
  106. return 1;
  107. oops_enter();
  108. if (die.lock_owner != raw_smp_processor_id()) {
  109. console_verbose();
  110. raw_spin_lock_irqsave(&die.lock, flags);
  111. die.lock_owner = smp_processor_id();
  112. die.lock_owner_depth = 0;
  113. bust_spinlocks(1);
  114. if (machine_is(powermac))
  115. pmac_backlight_unblank();
  116. } else {
  117. local_save_flags(flags);
  118. }
  119. if (++die.lock_owner_depth < 3) {
  120. printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
  121. #ifdef CONFIG_PREEMPT
  122. printk("PREEMPT ");
  123. #endif
  124. #ifdef CONFIG_SMP
  125. printk("SMP NR_CPUS=%d ", NR_CPUS);
  126. #endif
  127. #ifdef CONFIG_DEBUG_PAGEALLOC
  128. printk("DEBUG_PAGEALLOC ");
  129. #endif
  130. #ifdef CONFIG_NUMA
  131. printk("NUMA ");
  132. #endif
  133. printk("%s\n", ppc_md.name ? ppc_md.name : "");
  134. sysfs_printk_last_file();
  135. if (notify_die(DIE_OOPS, str, regs, err, 255,
  136. SIGSEGV) == NOTIFY_STOP)
  137. return 1;
  138. print_modules();
  139. show_regs(regs);
  140. } else {
  141. printk("Recursive die() failure, output suppressed\n");
  142. }
  143. bust_spinlocks(0);
  144. die.lock_owner = -1;
  145. add_taint(TAINT_DIE);
  146. raw_spin_unlock_irqrestore(&die.lock, flags);
  147. if (kexec_should_crash(current) ||
  148. kexec_sr_activated(smp_processor_id()))
  149. crash_kexec(regs);
  150. crash_kexec_secondary(regs);
  151. if (in_interrupt())
  152. panic("Fatal exception in interrupt");
  153. if (panic_on_oops)
  154. panic("Fatal exception");
  155. oops_exit();
  156. do_exit(err);
  157. return 0;
  158. }
  159. void user_single_step_siginfo(struct task_struct *tsk,
  160. struct pt_regs *regs, siginfo_t *info)
  161. {
  162. memset(info, 0, sizeof(*info));
  163. info->si_signo = SIGTRAP;
  164. info->si_code = TRAP_TRACE;
  165. info->si_addr = (void __user *)regs->nip;
  166. }
  167. void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
  168. {
  169. siginfo_t info;
  170. const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
  171. "at %08lx nip %08lx lr %08lx code %x\n";
  172. const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
  173. "at %016lx nip %016lx lr %016lx code %x\n";
  174. if (!user_mode(regs)) {
  175. if (die("Exception in kernel mode", regs, signr))
  176. return;
  177. } else if (show_unhandled_signals &&
  178. unhandled_signal(current, signr) &&
  179. printk_ratelimit()) {
  180. printk(regs->msr & MSR_SF ? fmt64 : fmt32,
  181. current->comm, current->pid, signr,
  182. addr, regs->nip, regs->link, code);
  183. }
  184. memset(&info, 0, sizeof(info));
  185. info.si_signo = signr;
  186. info.si_code = code;
  187. info.si_addr = (void __user *) addr;
  188. force_sig_info(signr, &info, current);
  189. }
  190. #ifdef CONFIG_PPC64
  191. void system_reset_exception(struct pt_regs *regs)
  192. {
  193. /* See if any machine dependent calls */
  194. if (ppc_md.system_reset_exception) {
  195. if (ppc_md.system_reset_exception(regs))
  196. return;
  197. }
  198. #ifdef CONFIG_KEXEC
  199. cpu_set(smp_processor_id(), cpus_in_sr);
  200. #endif
  201. die("System Reset", regs, SIGABRT);
  202. /*
  203. * Some CPUs when released from the debugger will execute this path.
  204. * These CPUs entered the debugger via a soft-reset. If the CPU was
  205. * hung before entering the debugger it will return to the hung
  206. * state when exiting this function. This causes a problem in
  207. * kdump since the hung CPU(s) will not respond to the IPI sent
  208. * from kdump. To prevent the problem we call crash_kexec_secondary()
  209. * here. If a kdump had not been initiated or we exit the debugger
  210. * with the "exit and recover" command (x) crash_kexec_secondary()
  211. * will return after 5ms and the CPU returns to its previous state.
  212. */
  213. crash_kexec_secondary(regs);
  214. /* Must die if the interrupt is not recoverable */
  215. if (!(regs->msr & MSR_RI))
  216. panic("Unrecoverable System Reset");
  217. /* What should we do here? We could issue a shutdown or hard reset. */
  218. }
  219. #endif
  220. /*
  221. * I/O accesses can cause machine checks on powermacs.
  222. * Check if the NIP corresponds to the address of a sync
  223. * instruction for which there is an entry in the exception
  224. * table.
  225. * Note that the 601 only takes a machine check on TEA
  226. * (transfer error ack) signal assertion, and does not
  227. * set any of the top 16 bits of SRR1.
  228. * -- paulus.
  229. */
  230. static inline int check_io_access(struct pt_regs *regs)
  231. {
  232. #ifdef CONFIG_PPC32
  233. unsigned long msr = regs->msr;
  234. const struct exception_table_entry *entry;
  235. unsigned int *nip = (unsigned int *)regs->nip;
  236. if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
  237. && (entry = search_exception_tables(regs->nip)) != NULL) {
  238. /*
  239. * Check that it's a sync instruction, or somewhere
  240. * in the twi; isync; nop sequence that inb/inw/inl uses.
  241. * As the address is in the exception table
  242. * we should be able to read the instr there.
  243. * For the debug message, we look at the preceding
  244. * load or store.
  245. */
  246. if (*nip == 0x60000000) /* nop */
  247. nip -= 2;
  248. else if (*nip == 0x4c00012c) /* isync */
  249. --nip;
  250. if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
  251. /* sync or twi */
  252. unsigned int rb;
  253. --nip;
  254. rb = (*nip >> 11) & 0x1f;
  255. printk(KERN_DEBUG "%s bad port %lx at %p\n",
  256. (*nip & 0x100)? "OUT to": "IN from",
  257. regs->gpr[rb] - _IO_BASE, nip);
  258. regs->msr |= MSR_RI;
  259. regs->nip = entry->fixup;
  260. return 1;
  261. }
  262. }
  263. #endif /* CONFIG_PPC32 */
  264. return 0;
  265. }
  266. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  267. /* On 4xx, the reason for the machine check or program exception
  268. is in the ESR. */
  269. #define get_reason(regs) ((regs)->dsisr)
  270. #ifndef CONFIG_FSL_BOOKE
  271. #define get_mc_reason(regs) ((regs)->dsisr)
  272. #else
  273. #define get_mc_reason(regs) (mfspr(SPRN_MCSR) & MCSR_MASK)
  274. #endif
  275. #define REASON_FP ESR_FP
  276. #define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
  277. #define REASON_PRIVILEGED ESR_PPR
  278. #define REASON_TRAP ESR_PTR
  279. /* single-step stuff */
  280. #define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
  281. #define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
  282. #else
  283. /* On non-4xx, the reason for the machine check or program
  284. exception is in the MSR. */
  285. #define get_reason(regs) ((regs)->msr)
  286. #define get_mc_reason(regs) ((regs)->msr)
  287. #define REASON_FP 0x100000
  288. #define REASON_ILLEGAL 0x80000
  289. #define REASON_PRIVILEGED 0x40000
  290. #define REASON_TRAP 0x20000
  291. #define single_stepping(regs) ((regs)->msr & MSR_SE)
  292. #define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
  293. #endif
  294. #if defined(CONFIG_4xx)
  295. int machine_check_4xx(struct pt_regs *regs)
  296. {
  297. unsigned long reason = get_mc_reason(regs);
  298. if (reason & ESR_IMCP) {
  299. printk("Instruction");
  300. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  301. } else
  302. printk("Data");
  303. printk(" machine check in kernel mode.\n");
  304. return 0;
  305. }
  306. int machine_check_440A(struct pt_regs *regs)
  307. {
  308. unsigned long reason = get_mc_reason(regs);
  309. printk("Machine check in kernel mode.\n");
  310. if (reason & ESR_IMCP){
  311. printk("Instruction Synchronous Machine Check exception\n");
  312. mtspr(SPRN_ESR, reason & ~ESR_IMCP);
  313. }
  314. else {
  315. u32 mcsr = mfspr(SPRN_MCSR);
  316. if (mcsr & MCSR_IB)
  317. printk("Instruction Read PLB Error\n");
  318. if (mcsr & MCSR_DRB)
  319. printk("Data Read PLB Error\n");
  320. if (mcsr & MCSR_DWB)
  321. printk("Data Write PLB Error\n");
  322. if (mcsr & MCSR_TLBP)
  323. printk("TLB Parity Error\n");
  324. if (mcsr & MCSR_ICP){
  325. flush_instruction_cache();
  326. printk("I-Cache Parity Error\n");
  327. }
  328. if (mcsr & MCSR_DCSP)
  329. printk("D-Cache Search Parity Error\n");
  330. if (mcsr & MCSR_DCFP)
  331. printk("D-Cache Flush Parity Error\n");
  332. if (mcsr & MCSR_IMPE)
  333. printk("Machine Check exception is imprecise\n");
  334. /* Clear MCSR */
  335. mtspr(SPRN_MCSR, mcsr);
  336. }
  337. return 0;
  338. }
  339. #elif defined(CONFIG_E500)
  340. int machine_check_e500(struct pt_regs *regs)
  341. {
  342. unsigned long reason = get_mc_reason(regs);
  343. printk("Machine check in kernel mode.\n");
  344. printk("Caused by (from MCSR=%lx): ", reason);
  345. if (reason & MCSR_MCP)
  346. printk("Machine Check Signal\n");
  347. if (reason & MCSR_ICPERR)
  348. printk("Instruction Cache Parity Error\n");
  349. if (reason & MCSR_DCP_PERR)
  350. printk("Data Cache Push Parity Error\n");
  351. if (reason & MCSR_DCPERR)
  352. printk("Data Cache Parity Error\n");
  353. if (reason & MCSR_BUS_IAERR)
  354. printk("Bus - Instruction Address Error\n");
  355. if (reason & MCSR_BUS_RAERR)
  356. printk("Bus - Read Address Error\n");
  357. if (reason & MCSR_BUS_WAERR)
  358. printk("Bus - Write Address Error\n");
  359. if (reason & MCSR_BUS_IBERR)
  360. printk("Bus - Instruction Data Error\n");
  361. if (reason & MCSR_BUS_RBERR)
  362. printk("Bus - Read Data Bus Error\n");
  363. if (reason & MCSR_BUS_WBERR)
  364. printk("Bus - Read Data Bus Error\n");
  365. if (reason & MCSR_BUS_IPERR)
  366. printk("Bus - Instruction Parity Error\n");
  367. if (reason & MCSR_BUS_RPERR)
  368. printk("Bus - Read Parity Error\n");
  369. return 0;
  370. }
  371. #elif defined(CONFIG_E200)
  372. int machine_check_e200(struct pt_regs *regs)
  373. {
  374. unsigned long reason = get_mc_reason(regs);
  375. printk("Machine check in kernel mode.\n");
  376. printk("Caused by (from MCSR=%lx): ", reason);
  377. if (reason & MCSR_MCP)
  378. printk("Machine Check Signal\n");
  379. if (reason & MCSR_CP_PERR)
  380. printk("Cache Push Parity Error\n");
  381. if (reason & MCSR_CPERR)
  382. printk("Cache Parity Error\n");
  383. if (reason & MCSR_EXCP_ERR)
  384. printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
  385. if (reason & MCSR_BUS_IRERR)
  386. printk("Bus - Read Bus Error on instruction fetch\n");
  387. if (reason & MCSR_BUS_DRERR)
  388. printk("Bus - Read Bus Error on data load\n");
  389. if (reason & MCSR_BUS_WRERR)
  390. printk("Bus - Write Bus Error on buffered store or cache line push\n");
  391. return 0;
  392. }
  393. #else
  394. int machine_check_generic(struct pt_regs *regs)
  395. {
  396. unsigned long reason = get_mc_reason(regs);
  397. printk("Machine check in kernel mode.\n");
  398. printk("Caused by (from SRR1=%lx): ", reason);
  399. switch (reason & 0x601F0000) {
  400. case 0x80000:
  401. printk("Machine check signal\n");
  402. break;
  403. case 0: /* for 601 */
  404. case 0x40000:
  405. case 0x140000: /* 7450 MSS error and TEA */
  406. printk("Transfer error ack signal\n");
  407. break;
  408. case 0x20000:
  409. printk("Data parity error signal\n");
  410. break;
  411. case 0x10000:
  412. printk("Address parity error signal\n");
  413. break;
  414. case 0x20000000:
  415. printk("L1 Data Cache error\n");
  416. break;
  417. case 0x40000000:
  418. printk("L1 Instruction Cache error\n");
  419. break;
  420. case 0x00100000:
  421. printk("L2 data cache parity error\n");
  422. break;
  423. default:
  424. printk("Unknown values in msr\n");
  425. }
  426. return 0;
  427. }
  428. #endif /* everything else */
  429. void machine_check_exception(struct pt_regs *regs)
  430. {
  431. int recover = 0;
  432. __get_cpu_var(irq_stat).mce_exceptions++;
  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. __get_cpu_var(irq_stat).pmu_irqs++;
  840. perf_irq(regs);
  841. }
  842. #ifdef CONFIG_8xx
  843. void SoftwareEmulation(struct pt_regs *regs)
  844. {
  845. extern int do_mathemu(struct pt_regs *);
  846. extern int Soft_emulate_8xx(struct pt_regs *);
  847. #if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
  848. int errcode;
  849. #endif
  850. CHECK_FULL_REGS(regs);
  851. if (!user_mode(regs)) {
  852. debugger(regs);
  853. die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
  854. }
  855. #ifdef CONFIG_MATH_EMULATION
  856. errcode = do_mathemu(regs);
  857. if (errcode >= 0)
  858. PPC_WARN_EMULATED(math, regs);
  859. switch (errcode) {
  860. case 0:
  861. emulate_single_step(regs);
  862. return;
  863. case 1: {
  864. int code = 0;
  865. code = __parse_fpscr(current->thread.fpscr.val);
  866. _exception(SIGFPE, regs, code, regs->nip);
  867. return;
  868. }
  869. case -EFAULT:
  870. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  871. return;
  872. default:
  873. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  874. return;
  875. }
  876. #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
  877. errcode = Soft_emulate_8xx(regs);
  878. if (errcode >= 0)
  879. PPC_WARN_EMULATED(8xx, regs);
  880. switch (errcode) {
  881. case 0:
  882. emulate_single_step(regs);
  883. return;
  884. case 1:
  885. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  886. return;
  887. case -EFAULT:
  888. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
  889. return;
  890. }
  891. #else
  892. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  893. #endif
  894. }
  895. #endif /* CONFIG_8xx */
  896. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  897. static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
  898. {
  899. int changed = 0;
  900. /*
  901. * Determine the cause of the debug event, clear the
  902. * event flags and send a trap to the handler. Torez
  903. */
  904. if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
  905. dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
  906. #ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
  907. current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
  908. #endif
  909. do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
  910. 5);
  911. changed |= 0x01;
  912. } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
  913. dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
  914. do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
  915. 6);
  916. changed |= 0x01;
  917. } else if (debug_status & DBSR_IAC1) {
  918. current->thread.dbcr0 &= ~DBCR0_IAC1;
  919. dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
  920. do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
  921. 1);
  922. changed |= 0x01;
  923. } else if (debug_status & DBSR_IAC2) {
  924. current->thread.dbcr0 &= ~DBCR0_IAC2;
  925. do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
  926. 2);
  927. changed |= 0x01;
  928. } else if (debug_status & DBSR_IAC3) {
  929. current->thread.dbcr0 &= ~DBCR0_IAC3;
  930. dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
  931. do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
  932. 3);
  933. changed |= 0x01;
  934. } else if (debug_status & DBSR_IAC4) {
  935. current->thread.dbcr0 &= ~DBCR0_IAC4;
  936. do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
  937. 4);
  938. changed |= 0x01;
  939. }
  940. /*
  941. * At the point this routine was called, the MSR(DE) was turned off.
  942. * Check all other debug flags and see if that bit needs to be turned
  943. * back on or not.
  944. */
  945. if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
  946. regs->msr |= MSR_DE;
  947. else
  948. /* Make sure the IDM flag is off */
  949. current->thread.dbcr0 &= ~DBCR0_IDM;
  950. if (changed & 0x01)
  951. mtspr(SPRN_DBCR0, current->thread.dbcr0);
  952. }
  953. void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
  954. {
  955. current->thread.dbsr = debug_status;
  956. /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
  957. * on server, it stops on the target of the branch. In order to simulate
  958. * the server behaviour, we thus restart right away with a single step
  959. * instead of stopping here when hitting a BT
  960. */
  961. if (debug_status & DBSR_BT) {
  962. regs->msr &= ~MSR_DE;
  963. /* Disable BT */
  964. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
  965. /* Clear the BT event */
  966. mtspr(SPRN_DBSR, DBSR_BT);
  967. /* Do the single step trick only when coming from userspace */
  968. if (user_mode(regs)) {
  969. current->thread.dbcr0 &= ~DBCR0_BT;
  970. current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
  971. regs->msr |= MSR_DE;
  972. return;
  973. }
  974. if (notify_die(DIE_SSTEP, "block_step", regs, 5,
  975. 5, SIGTRAP) == NOTIFY_STOP) {
  976. return;
  977. }
  978. if (debugger_sstep(regs))
  979. return;
  980. } else if (debug_status & DBSR_IC) { /* Instruction complete */
  981. regs->msr &= ~MSR_DE;
  982. /* Disable instruction completion */
  983. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
  984. /* Clear the instruction completion event */
  985. mtspr(SPRN_DBSR, DBSR_IC);
  986. if (notify_die(DIE_SSTEP, "single_step", regs, 5,
  987. 5, SIGTRAP) == NOTIFY_STOP) {
  988. return;
  989. }
  990. if (debugger_sstep(regs))
  991. return;
  992. if (user_mode(regs)) {
  993. current->thread.dbcr0 &= ~DBCR0_IC;
  994. #ifdef CONFIG_PPC_ADV_DEBUG_REGS
  995. if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
  996. current->thread.dbcr1))
  997. regs->msr |= MSR_DE;
  998. else
  999. /* Make sure the IDM bit is off */
  1000. current->thread.dbcr0 &= ~DBCR0_IDM;
  1001. #endif
  1002. }
  1003. _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
  1004. } else
  1005. handle_debug(regs, debug_status);
  1006. }
  1007. #endif /* CONFIG_PPC_ADV_DEBUG_REGS */
  1008. #if !defined(CONFIG_TAU_INT)
  1009. void TAUException(struct pt_regs *regs)
  1010. {
  1011. printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
  1012. regs->nip, regs->msr, regs->trap, print_tainted());
  1013. }
  1014. #endif /* CONFIG_INT_TAU */
  1015. #ifdef CONFIG_ALTIVEC
  1016. void altivec_assist_exception(struct pt_regs *regs)
  1017. {
  1018. int err;
  1019. if (!user_mode(regs)) {
  1020. printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
  1021. " at %lx\n", regs->nip);
  1022. die("Kernel VMX/Altivec assist exception", regs, SIGILL);
  1023. }
  1024. flush_altivec_to_thread(current);
  1025. PPC_WARN_EMULATED(altivec, regs);
  1026. err = emulate_altivec(regs);
  1027. if (err == 0) {
  1028. regs->nip += 4; /* skip emulated instruction */
  1029. emulate_single_step(regs);
  1030. return;
  1031. }
  1032. if (err == -EFAULT) {
  1033. /* got an error reading the instruction */
  1034. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1035. } else {
  1036. /* didn't recognize the instruction */
  1037. /* XXX quick hack for now: set the non-Java bit in the VSCR */
  1038. if (printk_ratelimit())
  1039. printk(KERN_ERR "Unrecognized altivec instruction "
  1040. "in %s at %lx\n", current->comm, regs->nip);
  1041. current->thread.vscr.u[3] |= 0x10000;
  1042. }
  1043. }
  1044. #endif /* CONFIG_ALTIVEC */
  1045. #ifdef CONFIG_VSX
  1046. void vsx_assist_exception(struct pt_regs *regs)
  1047. {
  1048. if (!user_mode(regs)) {
  1049. printk(KERN_EMERG "VSX assist exception in kernel mode"
  1050. " at %lx\n", regs->nip);
  1051. die("Kernel VSX assist exception", regs, SIGILL);
  1052. }
  1053. flush_vsx_to_thread(current);
  1054. printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
  1055. _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
  1056. }
  1057. #endif /* CONFIG_VSX */
  1058. #ifdef CONFIG_FSL_BOOKE
  1059. void doorbell_exception(struct pt_regs *regs)
  1060. {
  1061. #ifdef CONFIG_SMP
  1062. int cpu = smp_processor_id();
  1063. int msg;
  1064. if (num_online_cpus() < 2)
  1065. return;
  1066. for (msg = 0; msg < 4; msg++)
  1067. if (test_and_clear_bit(msg, &dbell_smp_message[cpu]))
  1068. smp_message_recv(msg);
  1069. #else
  1070. printk(KERN_WARNING "Received doorbell on non-smp system\n");
  1071. #endif
  1072. }
  1073. void CacheLockingException(struct pt_regs *regs, unsigned long address,
  1074. unsigned long error_code)
  1075. {
  1076. /* We treat cache locking instructions from the user
  1077. * as priv ops, in the future we could try to do
  1078. * something smarter
  1079. */
  1080. if (error_code & (ESR_DLK|ESR_ILK))
  1081. _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
  1082. return;
  1083. }
  1084. #endif /* CONFIG_FSL_BOOKE */
  1085. #ifdef CONFIG_SPE
  1086. void SPEFloatingPointException(struct pt_regs *regs)
  1087. {
  1088. extern int do_spe_mathemu(struct pt_regs *regs);
  1089. unsigned long spefscr;
  1090. int fpexc_mode;
  1091. int code = 0;
  1092. int err;
  1093. preempt_disable();
  1094. if (regs->msr & MSR_SPE)
  1095. giveup_spe(current);
  1096. preempt_enable();
  1097. spefscr = current->thread.spefscr;
  1098. fpexc_mode = current->thread.fpexc_mode;
  1099. if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
  1100. code = FPE_FLTOVF;
  1101. }
  1102. else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
  1103. code = FPE_FLTUND;
  1104. }
  1105. else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
  1106. code = FPE_FLTDIV;
  1107. else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
  1108. code = FPE_FLTINV;
  1109. }
  1110. else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
  1111. code = FPE_FLTRES;
  1112. err = do_spe_mathemu(regs);
  1113. if (err == 0) {
  1114. regs->nip += 4; /* skip emulated instruction */
  1115. emulate_single_step(regs);
  1116. return;
  1117. }
  1118. if (err == -EFAULT) {
  1119. /* got an error reading the instruction */
  1120. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1121. } else if (err == -EINVAL) {
  1122. /* didn't recognize the instruction */
  1123. printk(KERN_ERR "unrecognized spe instruction "
  1124. "in %s at %lx\n", current->comm, regs->nip);
  1125. } else {
  1126. _exception(SIGFPE, regs, code, regs->nip);
  1127. }
  1128. return;
  1129. }
  1130. void SPEFloatingPointRoundException(struct pt_regs *regs)
  1131. {
  1132. extern int speround_handler(struct pt_regs *regs);
  1133. int err;
  1134. preempt_disable();
  1135. if (regs->msr & MSR_SPE)
  1136. giveup_spe(current);
  1137. preempt_enable();
  1138. regs->nip -= 4;
  1139. err = speround_handler(regs);
  1140. if (err == 0) {
  1141. regs->nip += 4; /* skip emulated instruction */
  1142. emulate_single_step(regs);
  1143. return;
  1144. }
  1145. if (err == -EFAULT) {
  1146. /* got an error reading the instruction */
  1147. _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
  1148. } else if (err == -EINVAL) {
  1149. /* didn't recognize the instruction */
  1150. printk(KERN_ERR "unrecognized spe instruction "
  1151. "in %s at %lx\n", current->comm, regs->nip);
  1152. } else {
  1153. _exception(SIGFPE, regs, 0, regs->nip);
  1154. return;
  1155. }
  1156. }
  1157. #endif
  1158. /*
  1159. * We enter here if we get an unrecoverable exception, that is, one
  1160. * that happened at a point where the RI (recoverable interrupt) bit
  1161. * in the MSR is 0. This indicates that SRR0/1 are live, and that
  1162. * we therefore lost state by taking this exception.
  1163. */
  1164. void unrecoverable_exception(struct pt_regs *regs)
  1165. {
  1166. printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
  1167. regs->trap, regs->nip);
  1168. die("Unrecoverable exception", regs, SIGABRT);
  1169. }
  1170. #ifdef CONFIG_BOOKE_WDT
  1171. /*
  1172. * Default handler for a Watchdog exception,
  1173. * spins until a reboot occurs
  1174. */
  1175. void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
  1176. {
  1177. /* Generic WatchdogHandler, implement your own */
  1178. mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
  1179. return;
  1180. }
  1181. void WatchdogException(struct pt_regs *regs)
  1182. {
  1183. printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
  1184. WatchdogHandler(regs);
  1185. }
  1186. #endif
  1187. /*
  1188. * We enter here if we discover during exception entry that we are
  1189. * running in supervisor mode with a userspace value in the stack pointer.
  1190. */
  1191. void kernel_bad_stack(struct pt_regs *regs)
  1192. {
  1193. printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
  1194. regs->gpr[1], regs->nip);
  1195. die("Bad kernel stack pointer", regs, SIGABRT);
  1196. }
  1197. void __init trap_init(void)
  1198. {
  1199. }
  1200. #ifdef CONFIG_PPC_EMULATED_STATS
  1201. #define WARN_EMULATED_SETUP(type) .type = { .name = #type }
  1202. struct ppc_emulated ppc_emulated = {
  1203. #ifdef CONFIG_ALTIVEC
  1204. WARN_EMULATED_SETUP(altivec),
  1205. #endif
  1206. WARN_EMULATED_SETUP(dcba),
  1207. WARN_EMULATED_SETUP(dcbz),
  1208. WARN_EMULATED_SETUP(fp_pair),
  1209. WARN_EMULATED_SETUP(isel),
  1210. WARN_EMULATED_SETUP(mcrxr),
  1211. WARN_EMULATED_SETUP(mfpvr),
  1212. WARN_EMULATED_SETUP(multiple),
  1213. WARN_EMULATED_SETUP(popcntb),
  1214. WARN_EMULATED_SETUP(spe),
  1215. WARN_EMULATED_SETUP(string),
  1216. WARN_EMULATED_SETUP(unaligned),
  1217. #ifdef CONFIG_MATH_EMULATION
  1218. WARN_EMULATED_SETUP(math),
  1219. #elif defined(CONFIG_8XX_MINIMAL_FPEMU)
  1220. WARN_EMULATED_SETUP(8xx),
  1221. #endif
  1222. #ifdef CONFIG_VSX
  1223. WARN_EMULATED_SETUP(vsx),
  1224. #endif
  1225. };
  1226. u32 ppc_warn_emulated;
  1227. void ppc_warn_emulated_print(const char *type)
  1228. {
  1229. if (printk_ratelimit())
  1230. pr_warning("%s used emulated %s instruction\n", current->comm,
  1231. type);
  1232. }
  1233. static int __init ppc_warn_emulated_init(void)
  1234. {
  1235. struct dentry *dir, *d;
  1236. unsigned int i;
  1237. struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
  1238. if (!powerpc_debugfs_root)
  1239. return -ENODEV;
  1240. dir = debugfs_create_dir("emulated_instructions",
  1241. powerpc_debugfs_root);
  1242. if (!dir)
  1243. return -ENOMEM;
  1244. d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
  1245. &ppc_warn_emulated);
  1246. if (!d)
  1247. goto fail;
  1248. for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
  1249. d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
  1250. (u32 *)&entries[i].val.counter);
  1251. if (!d)
  1252. goto fail;
  1253. }
  1254. return 0;
  1255. fail:
  1256. debugfs_remove_recursive(dir);
  1257. return -ENOMEM;
  1258. }
  1259. device_initcall(ppc_warn_emulated_init);
  1260. #endif /* CONFIG_PPC_EMULATED_STATS */