]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - arch/powerpc/kernel/traps.c
powerpc, hw_breakpoint: Handle concurrent alignment interrupts
[mirror_ubuntu-zesty-kernel.git] / arch / powerpc / kernel / traps.c
CommitLineData
14cf11af 1/*
14cf11af 2 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
fe04b112 3 * Copyright 2007-2010 Freescale Semiconductor, Inc.
14cf11af
PM
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 * Modified by Cort Dougan (cort@cs.nmt.edu)
11 * and Paul Mackerras (paulus@samba.org)
12 */
13
14/*
15 * This file handles the architecture-dependent parts of hardware exceptions
16 */
17
14cf11af
PM
18#include <linux/errno.h>
19#include <linux/sched.h>
20#include <linux/kernel.h>
21#include <linux/mm.h>
22#include <linux/stddef.h>
23#include <linux/unistd.h>
8dad3f92 24#include <linux/ptrace.h>
14cf11af 25#include <linux/user.h>
14cf11af 26#include <linux/interrupt.h>
14cf11af
PM
27#include <linux/init.h>
28#include <linux/module.h>
8dad3f92 29#include <linux/prctl.h>
14cf11af
PM
30#include <linux/delay.h>
31#include <linux/kprobes.h>
cc532915 32#include <linux/kexec.h>
5474c120 33#include <linux/backlight.h>
73c9ceab 34#include <linux/bug.h>
1eeb66a1 35#include <linux/kdebug.h>
80947e7c 36#include <linux/debugfs.h>
14cf11af 37
80947e7c 38#include <asm/emulated_ops.h>
14cf11af
PM
39#include <asm/pgtable.h>
40#include <asm/uaccess.h>
41#include <asm/system.h>
42#include <asm/io.h>
86417780
PM
43#include <asm/machdep.h>
44#include <asm/rtas.h>
f7f6f4fe 45#include <asm/pmc.h>
dc1c1ca3 46#ifdef CONFIG_PPC32
14cf11af 47#include <asm/reg.h>
86417780 48#endif
14cf11af
PM
49#ifdef CONFIG_PMAC_BACKLIGHT
50#include <asm/backlight.h>
51#endif
dc1c1ca3 52#ifdef CONFIG_PPC64
86417780 53#include <asm/firmware.h>
dc1c1ca3 54#include <asm/processor.h>
dc1c1ca3 55#endif
c0ce7d08 56#include <asm/kexec.h>
16c57b36 57#include <asm/ppc-opcode.h>
620165f9
KG
58#ifdef CONFIG_FSL_BOOKE
59#include <asm/dbell.h>
60#endif
dc1c1ca3 61
7dbb922c 62#if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
5be3492f
AB
63int (*__debugger)(struct pt_regs *regs) __read_mostly;
64int (*__debugger_ipi)(struct pt_regs *regs) __read_mostly;
65int (*__debugger_bpt)(struct pt_regs *regs) __read_mostly;
66int (*__debugger_sstep)(struct pt_regs *regs) __read_mostly;
67int (*__debugger_iabr_match)(struct pt_regs *regs) __read_mostly;
68int (*__debugger_dabr_match)(struct pt_regs *regs) __read_mostly;
69int (*__debugger_fault_handler)(struct pt_regs *regs) __read_mostly;
14cf11af
PM
70
71EXPORT_SYMBOL(__debugger);
72EXPORT_SYMBOL(__debugger_ipi);
73EXPORT_SYMBOL(__debugger_bpt);
74EXPORT_SYMBOL(__debugger_sstep);
75EXPORT_SYMBOL(__debugger_iabr_match);
76EXPORT_SYMBOL(__debugger_dabr_match);
77EXPORT_SYMBOL(__debugger_fault_handler);
78#endif
79
14cf11af
PM
80/*
81 * Trap & Exception support
82 */
83
6031d9d9 84#ifdef CONFIG_PMAC_BACKLIGHT
85static void pmac_backlight_unblank(void)
86{
87 mutex_lock(&pmac_backlight_mutex);
88 if (pmac_backlight) {
89 struct backlight_properties *props;
90
91 props = &pmac_backlight->props;
92 props->brightness = props->max_brightness;
93 props->power = FB_BLANK_UNBLANK;
94 backlight_update_status(pmac_backlight);
95 }
96 mutex_unlock(&pmac_backlight_mutex);
97}
98#else
99static inline void pmac_backlight_unblank(void) { }
100#endif
101
14cf11af
PM
102int die(const char *str, struct pt_regs *regs, long err)
103{
34c2a14f 104 static struct {
b8f87782 105 raw_spinlock_t lock;
34c2a14f 106 u32 lock_owner;
107 int lock_owner_depth;
108 } die = {
b8f87782 109 .lock = __RAW_SPIN_LOCK_UNLOCKED(die.lock),
34c2a14f 110 .lock_owner = -1,
111 .lock_owner_depth = 0
112 };
c0ce7d08 113 static int die_counter;
34c2a14f 114 unsigned long flags;
14cf11af
PM
115
116 if (debugger(regs))
117 return 1;
118
293e4688 119 oops_enter();
120
34c2a14f 121 if (die.lock_owner != raw_smp_processor_id()) {
122 console_verbose();
b8f87782 123 raw_spin_lock_irqsave(&die.lock, flags);
34c2a14f 124 die.lock_owner = smp_processor_id();
125 die.lock_owner_depth = 0;
126 bust_spinlocks(1);
127 if (machine_is(powermac))
128 pmac_backlight_unblank();
129 } else {
130 local_save_flags(flags);
131 }
5474c120 132
34c2a14f 133 if (++die.lock_owner_depth < 3) {
134 printk("Oops: %s, sig: %ld [#%d]\n", str, err, ++die_counter);
14cf11af 135#ifdef CONFIG_PREEMPT
34c2a14f 136 printk("PREEMPT ");
14cf11af
PM
137#endif
138#ifdef CONFIG_SMP
34c2a14f 139 printk("SMP NR_CPUS=%d ", NR_CPUS);
14cf11af
PM
140#endif
141#ifdef CONFIG_DEBUG_PAGEALLOC
34c2a14f 142 printk("DEBUG_PAGEALLOC ");
14cf11af
PM
143#endif
144#ifdef CONFIG_NUMA
34c2a14f 145 printk("NUMA ");
14cf11af 146#endif
ae7f4463 147 printk("%s\n", ppc_md.name ? ppc_md.name : "");
34c2a14f 148
66fcb105
AB
149 sysfs_printk_last_file();
150 if (notify_die(DIE_OOPS, str, regs, err, 255,
151 SIGSEGV) == NOTIFY_STOP)
152 return 1;
153
34c2a14f 154 print_modules();
155 show_regs(regs);
156 } else {
157 printk("Recursive die() failure, output suppressed\n");
158 }
e8222502 159
14cf11af 160 bust_spinlocks(0);
34c2a14f 161 die.lock_owner = -1;
bcdcd8e7 162 add_taint(TAINT_DIE);
b8f87782 163 raw_spin_unlock_irqrestore(&die.lock, flags);
cc532915 164
c0ce7d08
DW
165 if (kexec_should_crash(current) ||
166 kexec_sr_activated(smp_processor_id()))
cc532915 167 crash_kexec(regs);
c0ce7d08 168 crash_kexec_secondary(regs);
14cf11af
PM
169
170 if (in_interrupt())
171 panic("Fatal exception in interrupt");
172
cea6a4ba 173 if (panic_on_oops)
012c437d 174 panic("Fatal exception");
cea6a4ba 175
293e4688 176 oops_exit();
14cf11af
PM
177 do_exit(err);
178
179 return 0;
180}
181
25baa35b
ON
182void user_single_step_siginfo(struct task_struct *tsk,
183 struct pt_regs *regs, siginfo_t *info)
184{
185 memset(info, 0, sizeof(*info));
186 info->si_signo = SIGTRAP;
187 info->si_code = TRAP_TRACE;
188 info->si_addr = (void __user *)regs->nip;
189}
190
14cf11af
PM
191void _exception(int signr, struct pt_regs *regs, int code, unsigned long addr)
192{
193 siginfo_t info;
d0c3d534
OJ
194 const char fmt32[] = KERN_INFO "%s[%d]: unhandled signal %d " \
195 "at %08lx nip %08lx lr %08lx code %x\n";
196 const char fmt64[] = KERN_INFO "%s[%d]: unhandled signal %d " \
197 "at %016lx nip %016lx lr %016lx code %x\n";
14cf11af
PM
198
199 if (!user_mode(regs)) {
200 if (die("Exception in kernel mode", regs, signr))
201 return;
d0c3d534
OJ
202 } else if (show_unhandled_signals &&
203 unhandled_signal(current, signr) &&
204 printk_ratelimit()) {
205 printk(regs->msr & MSR_SF ? fmt64 : fmt32,
206 current->comm, current->pid, signr,
207 addr, regs->nip, regs->link, code);
208 }
14cf11af
PM
209
210 memset(&info, 0, sizeof(info));
211 info.si_signo = signr;
212 info.si_code = code;
213 info.si_addr = (void __user *) addr;
214 force_sig_info(signr, &info, current);
14cf11af
PM
215}
216
217#ifdef CONFIG_PPC64
218void system_reset_exception(struct pt_regs *regs)
219{
220 /* See if any machine dependent calls */
c902be71
AB
221 if (ppc_md.system_reset_exception) {
222 if (ppc_md.system_reset_exception(regs))
223 return;
224 }
14cf11af 225
c0ce7d08
DW
226#ifdef CONFIG_KEXEC
227 cpu_set(smp_processor_id(), cpus_in_sr);
228#endif
229
8dad3f92 230 die("System Reset", regs, SIGABRT);
14cf11af 231
eac8392f
DW
232 /*
233 * Some CPUs when released from the debugger will execute this path.
234 * These CPUs entered the debugger via a soft-reset. If the CPU was
235 * hung before entering the debugger it will return to the hung
236 * state when exiting this function. This causes a problem in
237 * kdump since the hung CPU(s) will not respond to the IPI sent
238 * from kdump. To prevent the problem we call crash_kexec_secondary()
239 * here. If a kdump had not been initiated or we exit the debugger
240 * with the "exit and recover" command (x) crash_kexec_secondary()
241 * will return after 5ms and the CPU returns to its previous state.
242 */
243 crash_kexec_secondary(regs);
244
14cf11af
PM
245 /* Must die if the interrupt is not recoverable */
246 if (!(regs->msr & MSR_RI))
247 panic("Unrecoverable System Reset");
248
249 /* What should we do here? We could issue a shutdown or hard reset. */
250}
251#endif
252
253/*
254 * I/O accesses can cause machine checks on powermacs.
255 * Check if the NIP corresponds to the address of a sync
256 * instruction for which there is an entry in the exception
257 * table.
258 * Note that the 601 only takes a machine check on TEA
259 * (transfer error ack) signal assertion, and does not
260 * set any of the top 16 bits of SRR1.
261 * -- paulus.
262 */
263static inline int check_io_access(struct pt_regs *regs)
264{
68a64357 265#ifdef CONFIG_PPC32
14cf11af
PM
266 unsigned long msr = regs->msr;
267 const struct exception_table_entry *entry;
268 unsigned int *nip = (unsigned int *)regs->nip;
269
270 if (((msr & 0xffff0000) == 0 || (msr & (0x80000 | 0x40000)))
271 && (entry = search_exception_tables(regs->nip)) != NULL) {
272 /*
273 * Check that it's a sync instruction, or somewhere
274 * in the twi; isync; nop sequence that inb/inw/inl uses.
275 * As the address is in the exception table
276 * we should be able to read the instr there.
277 * For the debug message, we look at the preceding
278 * load or store.
279 */
280 if (*nip == 0x60000000) /* nop */
281 nip -= 2;
282 else if (*nip == 0x4c00012c) /* isync */
283 --nip;
284 if (*nip == 0x7c0004ac || (*nip >> 26) == 3) {
285 /* sync or twi */
286 unsigned int rb;
287
288 --nip;
289 rb = (*nip >> 11) & 0x1f;
290 printk(KERN_DEBUG "%s bad port %lx at %p\n",
291 (*nip & 0x100)? "OUT to": "IN from",
292 regs->gpr[rb] - _IO_BASE, nip);
293 regs->msr |= MSR_RI;
294 regs->nip = entry->fixup;
295 return 1;
296 }
297 }
68a64357 298#endif /* CONFIG_PPC32 */
14cf11af
PM
299 return 0;
300}
301
172ae2e7 302#ifdef CONFIG_PPC_ADV_DEBUG_REGS
14cf11af
PM
303/* On 4xx, the reason for the machine check or program exception
304 is in the ESR. */
305#define get_reason(regs) ((regs)->dsisr)
306#ifndef CONFIG_FSL_BOOKE
307#define get_mc_reason(regs) ((regs)->dsisr)
308#else
fe04b112 309#define get_mc_reason(regs) (mfspr(SPRN_MCSR))
14cf11af
PM
310#endif
311#define REASON_FP ESR_FP
312#define REASON_ILLEGAL (ESR_PIL | ESR_PUO)
313#define REASON_PRIVILEGED ESR_PPR
314#define REASON_TRAP ESR_PTR
315
316/* single-step stuff */
317#define single_stepping(regs) (current->thread.dbcr0 & DBCR0_IC)
318#define clear_single_step(regs) (current->thread.dbcr0 &= ~DBCR0_IC)
319
320#else
321/* On non-4xx, the reason for the machine check or program
322 exception is in the MSR. */
323#define get_reason(regs) ((regs)->msr)
324#define get_mc_reason(regs) ((regs)->msr)
325#define REASON_FP 0x100000
326#define REASON_ILLEGAL 0x80000
327#define REASON_PRIVILEGED 0x40000
328#define REASON_TRAP 0x20000
329
330#define single_stepping(regs) ((regs)->msr & MSR_SE)
331#define clear_single_step(regs) ((regs)->msr &= ~MSR_SE)
332#endif
333
47c0bd1a
BH
334#if defined(CONFIG_4xx)
335int machine_check_4xx(struct pt_regs *regs)
14cf11af 336{
1a6a4ffe 337 unsigned long reason = get_mc_reason(regs);
14cf11af 338
14cf11af
PM
339 if (reason & ESR_IMCP) {
340 printk("Instruction");
341 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
342 } else
343 printk("Data");
344 printk(" machine check in kernel mode.\n");
47c0bd1a
BH
345
346 return 0;
347}
348
349int machine_check_440A(struct pt_regs *regs)
350{
351 unsigned long reason = get_mc_reason(regs);
352
14cf11af
PM
353 printk("Machine check in kernel mode.\n");
354 if (reason & ESR_IMCP){
355 printk("Instruction Synchronous Machine Check exception\n");
356 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
357 }
358 else {
359 u32 mcsr = mfspr(SPRN_MCSR);
360 if (mcsr & MCSR_IB)
361 printk("Instruction Read PLB Error\n");
362 if (mcsr & MCSR_DRB)
363 printk("Data Read PLB Error\n");
364 if (mcsr & MCSR_DWB)
365 printk("Data Write PLB Error\n");
366 if (mcsr & MCSR_TLBP)
367 printk("TLB Parity Error\n");
368 if (mcsr & MCSR_ICP){
369 flush_instruction_cache();
370 printk("I-Cache Parity Error\n");
371 }
372 if (mcsr & MCSR_DCSP)
373 printk("D-Cache Search Parity Error\n");
374 if (mcsr & MCSR_DCFP)
375 printk("D-Cache Flush Parity Error\n");
376 if (mcsr & MCSR_IMPE)
377 printk("Machine Check exception is imprecise\n");
378
379 /* Clear MCSR */
380 mtspr(SPRN_MCSR, mcsr);
381 }
47c0bd1a
BH
382 return 0;
383}
fc5e7097
DK
384
385int machine_check_47x(struct pt_regs *regs)
386{
387 unsigned long reason = get_mc_reason(regs);
388 u32 mcsr;
389
390 printk(KERN_ERR "Machine check in kernel mode.\n");
391 if (reason & ESR_IMCP) {
392 printk(KERN_ERR
393 "Instruction Synchronous Machine Check exception\n");
394 mtspr(SPRN_ESR, reason & ~ESR_IMCP);
395 return 0;
396 }
397 mcsr = mfspr(SPRN_MCSR);
398 if (mcsr & MCSR_IB)
399 printk(KERN_ERR "Instruction Read PLB Error\n");
400 if (mcsr & MCSR_DRB)
401 printk(KERN_ERR "Data Read PLB Error\n");
402 if (mcsr & MCSR_DWB)
403 printk(KERN_ERR "Data Write PLB Error\n");
404 if (mcsr & MCSR_TLBP)
405 printk(KERN_ERR "TLB Parity Error\n");
406 if (mcsr & MCSR_ICP) {
407 flush_instruction_cache();
408 printk(KERN_ERR "I-Cache Parity Error\n");
409 }
410 if (mcsr & MCSR_DCSP)
411 printk(KERN_ERR "D-Cache Search Parity Error\n");
412 if (mcsr & PPC47x_MCSR_GPR)
413 printk(KERN_ERR "GPR Parity Error\n");
414 if (mcsr & PPC47x_MCSR_FPR)
415 printk(KERN_ERR "FPR Parity Error\n");
416 if (mcsr & PPC47x_MCSR_IPR)
417 printk(KERN_ERR "Machine Check exception is imprecise\n");
418
419 /* Clear MCSR */
420 mtspr(SPRN_MCSR, mcsr);
421
422 return 0;
423}
47c0bd1a 424#elif defined(CONFIG_E500)
fe04b112
SW
425int machine_check_e500mc(struct pt_regs *regs)
426{
427 unsigned long mcsr = mfspr(SPRN_MCSR);
428 unsigned long reason = mcsr;
429 int recoverable = 1;
430
431 printk("Machine check in kernel mode.\n");
432 printk("Caused by (from MCSR=%lx): ", reason);
433
434 if (reason & MCSR_MCP)
435 printk("Machine Check Signal\n");
436
437 if (reason & MCSR_ICPERR) {
438 printk("Instruction Cache Parity Error\n");
439
440 /*
441 * This is recoverable by invalidating the i-cache.
442 */
443 mtspr(SPRN_L1CSR1, mfspr(SPRN_L1CSR1) | L1CSR1_ICFI);
444 while (mfspr(SPRN_L1CSR1) & L1CSR1_ICFI)
445 ;
446
447 /*
448 * This will generally be accompanied by an instruction
449 * fetch error report -- only treat MCSR_IF as fatal
450 * if it wasn't due to an L1 parity error.
451 */
452 reason &= ~MCSR_IF;
453 }
454
455 if (reason & MCSR_DCPERR_MC) {
456 printk("Data Cache Parity Error\n");
457 recoverable = 0;
458 }
459
460 if (reason & MCSR_L2MMU_MHIT) {
461 printk("Hit on multiple TLB entries\n");
462 recoverable = 0;
463 }
464
465 if (reason & MCSR_NMI)
466 printk("Non-maskable interrupt\n");
467
468 if (reason & MCSR_IF) {
469 printk("Instruction Fetch Error Report\n");
470 recoverable = 0;
471 }
472
473 if (reason & MCSR_LD) {
474 printk("Load Error Report\n");
475 recoverable = 0;
476 }
477
478 if (reason & MCSR_ST) {
479 printk("Store Error Report\n");
480 recoverable = 0;
481 }
482
483 if (reason & MCSR_LDG) {
484 printk("Guarded Load Error Report\n");
485 recoverable = 0;
486 }
487
488 if (reason & MCSR_TLBSYNC)
489 printk("Simultaneous tlbsync operations\n");
490
491 if (reason & MCSR_BSL2_ERR) {
492 printk("Level 2 Cache Error\n");
493 recoverable = 0;
494 }
495
496 if (reason & MCSR_MAV) {
497 u64 addr;
498
499 addr = mfspr(SPRN_MCAR);
500 addr |= (u64)mfspr(SPRN_MCARU) << 32;
501
502 printk("Machine Check %s Address: %#llx\n",
503 reason & MCSR_MEA ? "Effective" : "Physical", addr);
504 }
505
506 mtspr(SPRN_MCSR, mcsr);
507 return mfspr(SPRN_MCSR) == 0 && recoverable;
508}
509
47c0bd1a
BH
510int machine_check_e500(struct pt_regs *regs)
511{
512 unsigned long reason = get_mc_reason(regs);
513
14cf11af
PM
514 printk("Machine check in kernel mode.\n");
515 printk("Caused by (from MCSR=%lx): ", reason);
516
517 if (reason & MCSR_MCP)
518 printk("Machine Check Signal\n");
519 if (reason & MCSR_ICPERR)
520 printk("Instruction Cache Parity Error\n");
521 if (reason & MCSR_DCP_PERR)
522 printk("Data Cache Push Parity Error\n");
523 if (reason & MCSR_DCPERR)
524 printk("Data Cache Parity Error\n");
14cf11af
PM
525 if (reason & MCSR_BUS_IAERR)
526 printk("Bus - Instruction Address Error\n");
527 if (reason & MCSR_BUS_RAERR)
528 printk("Bus - Read Address Error\n");
529 if (reason & MCSR_BUS_WAERR)
530 printk("Bus - Write Address Error\n");
531 if (reason & MCSR_BUS_IBERR)
532 printk("Bus - Instruction Data Error\n");
533 if (reason & MCSR_BUS_RBERR)
534 printk("Bus - Read Data Bus Error\n");
535 if (reason & MCSR_BUS_WBERR)
536 printk("Bus - Read Data Bus Error\n");
537 if (reason & MCSR_BUS_IPERR)
538 printk("Bus - Instruction Parity Error\n");
539 if (reason & MCSR_BUS_RPERR)
540 printk("Bus - Read Parity Error\n");
47c0bd1a
BH
541
542 return 0;
543}
544#elif defined(CONFIG_E200)
545int machine_check_e200(struct pt_regs *regs)
546{
547 unsigned long reason = get_mc_reason(regs);
548
14cf11af
PM
549 printk("Machine check in kernel mode.\n");
550 printk("Caused by (from MCSR=%lx): ", reason);
551
552 if (reason & MCSR_MCP)
553 printk("Machine Check Signal\n");
554 if (reason & MCSR_CP_PERR)
555 printk("Cache Push Parity Error\n");
556 if (reason & MCSR_CPERR)
557 printk("Cache Parity Error\n");
558 if (reason & MCSR_EXCP_ERR)
559 printk("ISI, ITLB, or Bus Error on first instruction fetch for an exception handler\n");
560 if (reason & MCSR_BUS_IRERR)
561 printk("Bus - Read Bus Error on instruction fetch\n");
562 if (reason & MCSR_BUS_DRERR)
563 printk("Bus - Read Bus Error on data load\n");
564 if (reason & MCSR_BUS_WRERR)
565 printk("Bus - Write Bus Error on buffered store or cache line push\n");
47c0bd1a
BH
566
567 return 0;
568}
569#else
570int machine_check_generic(struct pt_regs *regs)
571{
572 unsigned long reason = get_mc_reason(regs);
573
14cf11af
PM
574 printk("Machine check in kernel mode.\n");
575 printk("Caused by (from SRR1=%lx): ", reason);
576 switch (reason & 0x601F0000) {
577 case 0x80000:
578 printk("Machine check signal\n");
579 break;
580 case 0: /* for 601 */
581 case 0x40000:
582 case 0x140000: /* 7450 MSS error and TEA */
583 printk("Transfer error ack signal\n");
584 break;
585 case 0x20000:
586 printk("Data parity error signal\n");
587 break;
588 case 0x10000:
589 printk("Address parity error signal\n");
590 break;
591 case 0x20000000:
592 printk("L1 Data Cache error\n");
593 break;
594 case 0x40000000:
595 printk("L1 Instruction Cache error\n");
596 break;
597 case 0x00100000:
598 printk("L2 data cache parity error\n");
599 break;
600 default:
601 printk("Unknown values in msr\n");
602 }
75918a4b
OJ
603 return 0;
604}
47c0bd1a 605#endif /* everything else */
75918a4b
OJ
606
607void machine_check_exception(struct pt_regs *regs)
608{
609 int recover = 0;
610
89713ed1
AB
611 __get_cpu_var(irq_stat).mce_exceptions++;
612
47c0bd1a
BH
613 /* See if any machine dependent calls. In theory, we would want
614 * to call the CPU first, and call the ppc_md. one if the CPU
615 * one returns a positive number. However there is existing code
616 * that assumes the board gets a first chance, so let's keep it
617 * that way for now and fix things later. --BenH.
618 */
75918a4b
OJ
619 if (ppc_md.machine_check_exception)
620 recover = ppc_md.machine_check_exception(regs);
47c0bd1a
BH
621 else if (cur_cpu_spec->machine_check)
622 recover = cur_cpu_spec->machine_check(regs);
75918a4b 623
47c0bd1a 624 if (recover > 0)
75918a4b
OJ
625 return;
626
627 if (user_mode(regs)) {
628 regs->msr |= MSR_RI;
629 _exception(SIGBUS, regs, BUS_ADRERR, regs->nip);
630 return;
631 }
632
633#if defined(CONFIG_8xx) && defined(CONFIG_PCI)
47c0bd1a
BH
634 /* the qspan pci read routines can cause machine checks -- Cort
635 *
636 * yuck !!! that totally needs to go away ! There are better ways
637 * to deal with that than having a wart in the mcheck handler.
638 * -- BenH
639 */
75918a4b
OJ
640 bad_page_fault(regs, regs->dar, SIGBUS);
641 return;
642#endif
643
644 if (debugger_fault_handler(regs)) {
645 regs->msr |= MSR_RI;
646 return;
647 }
648
649 if (check_io_access(regs))
650 return;
651
14cf11af
PM
652 if (debugger_fault_handler(regs))
653 return;
8dad3f92 654 die("Machine check", regs, SIGBUS);
14cf11af
PM
655
656 /* Must die if the interrupt is not recoverable */
657 if (!(regs->msr & MSR_RI))
658 panic("Unrecoverable Machine check");
659}
660
661void SMIException(struct pt_regs *regs)
662{
663 die("System Management Interrupt", regs, SIGABRT);
664}
665
dc1c1ca3 666void unknown_exception(struct pt_regs *regs)
14cf11af
PM
667{
668 printk("Bad trap at PC: %lx, SR: %lx, vector=%lx\n",
669 regs->nip, regs->msr, regs->trap);
670
671 _exception(SIGTRAP, regs, 0, 0);
672}
673
dc1c1ca3 674void instruction_breakpoint_exception(struct pt_regs *regs)
14cf11af
PM
675{
676 if (notify_die(DIE_IABR_MATCH, "iabr_match", regs, 5,
677 5, SIGTRAP) == NOTIFY_STOP)
678 return;
679 if (debugger_iabr_match(regs))
680 return;
681 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
682}
683
684void RunModeException(struct pt_regs *regs)
685{
686 _exception(SIGTRAP, regs, 0, 0);
687}
688
8dad3f92 689void __kprobes single_step_exception(struct pt_regs *regs)
14cf11af 690{
2538c2d0 691 clear_single_step(regs);
14cf11af
PM
692
693 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
694 5, SIGTRAP) == NOTIFY_STOP)
695 return;
696 if (debugger_sstep(regs))
697 return;
698
699 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
700}
701
702/*
703 * After we have successfully emulated an instruction, we have to
704 * check if the instruction was being single-stepped, and if so,
705 * pretend we got a single-step exception. This was pointed out
706 * by Kumar Gala. -- paulus
707 */
8dad3f92 708static void emulate_single_step(struct pt_regs *regs)
14cf11af 709{
2538c2d0
P
710 if (single_stepping(regs))
711 single_step_exception(regs);
14cf11af
PM
712}
713
5fad293b 714static inline int __parse_fpscr(unsigned long fpscr)
dc1c1ca3 715{
5fad293b 716 int ret = 0;
dc1c1ca3
SR
717
718 /* Invalid operation */
719 if ((fpscr & FPSCR_VE) && (fpscr & FPSCR_VX))
5fad293b 720 ret = FPE_FLTINV;
dc1c1ca3
SR
721
722 /* Overflow */
723 else if ((fpscr & FPSCR_OE) && (fpscr & FPSCR_OX))
5fad293b 724 ret = FPE_FLTOVF;
dc1c1ca3
SR
725
726 /* Underflow */
727 else if ((fpscr & FPSCR_UE) && (fpscr & FPSCR_UX))
5fad293b 728 ret = FPE_FLTUND;
dc1c1ca3
SR
729
730 /* Divide by zero */
731 else if ((fpscr & FPSCR_ZE) && (fpscr & FPSCR_ZX))
5fad293b 732 ret = FPE_FLTDIV;
dc1c1ca3
SR
733
734 /* Inexact result */
735 else if ((fpscr & FPSCR_XE) && (fpscr & FPSCR_XX))
5fad293b
KG
736 ret = FPE_FLTRES;
737
738 return ret;
739}
740
741static void parse_fpe(struct pt_regs *regs)
742{
743 int code = 0;
744
745 flush_fp_to_thread(current);
746
747 code = __parse_fpscr(current->thread.fpscr.val);
dc1c1ca3
SR
748
749 _exception(SIGFPE, regs, code, regs->nip);
750}
751
752/*
753 * Illegal instruction emulation support. Originally written to
14cf11af
PM
754 * provide the PVR to user applications using the mfspr rd, PVR.
755 * Return non-zero if we can't emulate, or -EFAULT if the associated
756 * memory access caused an access fault. Return zero on success.
757 *
758 * There are a couple of ways to do this, either "decode" the instruction
759 * or directly match lots of bits. In this case, matching lots of
760 * bits is faster and easier.
86417780 761 *
14cf11af 762 */
14cf11af
PM
763static int emulate_string_inst(struct pt_regs *regs, u32 instword)
764{
765 u8 rT = (instword >> 21) & 0x1f;
766 u8 rA = (instword >> 16) & 0x1f;
767 u8 NB_RB = (instword >> 11) & 0x1f;
768 u32 num_bytes;
769 unsigned long EA;
770 int pos = 0;
771
772 /* Early out if we are an invalid form of lswx */
16c57b36 773 if ((instword & PPC_INST_STRING_MASK) == PPC_INST_LSWX)
14cf11af
PM
774 if ((rT == rA) || (rT == NB_RB))
775 return -EINVAL;
776
777 EA = (rA == 0) ? 0 : regs->gpr[rA];
778
16c57b36
KG
779 switch (instword & PPC_INST_STRING_MASK) {
780 case PPC_INST_LSWX:
781 case PPC_INST_STSWX:
14cf11af
PM
782 EA += NB_RB;
783 num_bytes = regs->xer & 0x7f;
784 break;
16c57b36
KG
785 case PPC_INST_LSWI:
786 case PPC_INST_STSWI:
14cf11af
PM
787 num_bytes = (NB_RB == 0) ? 32 : NB_RB;
788 break;
789 default:
790 return -EINVAL;
791 }
792
793 while (num_bytes != 0)
794 {
795 u8 val;
796 u32 shift = 8 * (3 - (pos & 0x3));
797
16c57b36
KG
798 switch ((instword & PPC_INST_STRING_MASK)) {
799 case PPC_INST_LSWX:
800 case PPC_INST_LSWI:
14cf11af
PM
801 if (get_user(val, (u8 __user *)EA))
802 return -EFAULT;
803 /* first time updating this reg,
804 * zero it out */
805 if (pos == 0)
806 regs->gpr[rT] = 0;
807 regs->gpr[rT] |= val << shift;
808 break;
16c57b36
KG
809 case PPC_INST_STSWI:
810 case PPC_INST_STSWX:
14cf11af
PM
811 val = regs->gpr[rT] >> shift;
812 if (put_user(val, (u8 __user *)EA))
813 return -EFAULT;
814 break;
815 }
816 /* move EA to next address */
817 EA += 1;
818 num_bytes--;
819
820 /* manage our position within the register */
821 if (++pos == 4) {
822 pos = 0;
823 if (++rT == 32)
824 rT = 0;
825 }
826 }
827
828 return 0;
829}
830
c3412dcb
WS
831static int emulate_popcntb_inst(struct pt_regs *regs, u32 instword)
832{
833 u32 ra,rs;
834 unsigned long tmp;
835
836 ra = (instword >> 16) & 0x1f;
837 rs = (instword >> 21) & 0x1f;
838
839 tmp = regs->gpr[rs];
840 tmp = tmp - ((tmp >> 1) & 0x5555555555555555ULL);
841 tmp = (tmp & 0x3333333333333333ULL) + ((tmp >> 2) & 0x3333333333333333ULL);
842 tmp = (tmp + (tmp >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
843 regs->gpr[ra] = tmp;
844
845 return 0;
846}
847
c1469f13
KG
848static int emulate_isel(struct pt_regs *regs, u32 instword)
849{
850 u8 rT = (instword >> 21) & 0x1f;
851 u8 rA = (instword >> 16) & 0x1f;
852 u8 rB = (instword >> 11) & 0x1f;
853 u8 BC = (instword >> 6) & 0x1f;
854 u8 bit;
855 unsigned long tmp;
856
857 tmp = (rA == 0) ? 0 : regs->gpr[rA];
858 bit = (regs->ccr >> (31 - BC)) & 0x1;
859
860 regs->gpr[rT] = bit ? tmp : regs->gpr[rB];
861
862 return 0;
863}
864
14cf11af
PM
865static int emulate_instruction(struct pt_regs *regs)
866{
867 u32 instword;
868 u32 rd;
869
fab5db97 870 if (!user_mode(regs) || (regs->msr & MSR_LE))
14cf11af
PM
871 return -EINVAL;
872 CHECK_FULL_REGS(regs);
873
874 if (get_user(instword, (u32 __user *)(regs->nip)))
875 return -EFAULT;
876
877 /* Emulate the mfspr rD, PVR. */
16c57b36 878 if ((instword & PPC_INST_MFSPR_PVR_MASK) == PPC_INST_MFSPR_PVR) {
eecff81d 879 PPC_WARN_EMULATED(mfpvr, regs);
14cf11af
PM
880 rd = (instword >> 21) & 0x1f;
881 regs->gpr[rd] = mfspr(SPRN_PVR);
882 return 0;
883 }
884
885 /* Emulating the dcba insn is just a no-op. */
80947e7c 886 if ((instword & PPC_INST_DCBA_MASK) == PPC_INST_DCBA) {
eecff81d 887 PPC_WARN_EMULATED(dcba, regs);
14cf11af 888 return 0;
80947e7c 889 }
14cf11af
PM
890
891 /* Emulate the mcrxr insn. */
16c57b36 892 if ((instword & PPC_INST_MCRXR_MASK) == PPC_INST_MCRXR) {
86417780 893 int shift = (instword >> 21) & 0x1c;
14cf11af
PM
894 unsigned long msk = 0xf0000000UL >> shift;
895
eecff81d 896 PPC_WARN_EMULATED(mcrxr, regs);
14cf11af
PM
897 regs->ccr = (regs->ccr & ~msk) | ((regs->xer >> shift) & msk);
898 regs->xer &= ~0xf0000000UL;
899 return 0;
900 }
901
902 /* Emulate load/store string insn. */
80947e7c 903 if ((instword & PPC_INST_STRING_GEN_MASK) == PPC_INST_STRING) {
eecff81d 904 PPC_WARN_EMULATED(string, regs);
14cf11af 905 return emulate_string_inst(regs, instword);
80947e7c 906 }
14cf11af 907
c3412dcb 908 /* Emulate the popcntb (Population Count Bytes) instruction. */
16c57b36 909 if ((instword & PPC_INST_POPCNTB_MASK) == PPC_INST_POPCNTB) {
eecff81d 910 PPC_WARN_EMULATED(popcntb, regs);
c3412dcb
WS
911 return emulate_popcntb_inst(regs, instword);
912 }
913
c1469f13 914 /* Emulate isel (Integer Select) instruction */
16c57b36 915 if ((instword & PPC_INST_ISEL_MASK) == PPC_INST_ISEL) {
eecff81d 916 PPC_WARN_EMULATED(isel, regs);
c1469f13
KG
917 return emulate_isel(regs, instword);
918 }
919
14cf11af
PM
920 return -EINVAL;
921}
922
73c9ceab 923int is_valid_bugaddr(unsigned long addr)
14cf11af 924{
73c9ceab 925 return is_kernel_addr(addr);
14cf11af
PM
926}
927
8dad3f92 928void __kprobes program_check_exception(struct pt_regs *regs)
14cf11af
PM
929{
930 unsigned int reason = get_reason(regs);
931 extern int do_mathemu(struct pt_regs *regs);
932
aa42c69c 933 /* We can now get here via a FP Unavailable exception if the core
04903a30 934 * has no FPU, in that case the reason flags will be 0 */
14cf11af 935
dc1c1ca3
SR
936 if (reason & REASON_FP) {
937 /* IEEE FP exception */
938 parse_fpe(regs);
8dad3f92
PM
939 return;
940 }
941 if (reason & REASON_TRAP) {
ba797b28
JW
942 /* Debugger is first in line to stop recursive faults in
943 * rcu_lock, notify_die, or atomic_notifier_call_chain */
944 if (debugger_bpt(regs))
945 return;
946
14cf11af 947 /* trap exception */
dc1c1ca3
SR
948 if (notify_die(DIE_BPT, "breakpoint", regs, 5, 5, SIGTRAP)
949 == NOTIFY_STOP)
950 return;
73c9ceab
JF
951
952 if (!(regs->msr & MSR_PR) && /* not user-mode */
608e2619 953 report_bug(regs->nip, regs) == BUG_TRAP_TYPE_WARN) {
14cf11af
PM
954 regs->nip += 4;
955 return;
956 }
8dad3f92
PM
957 _exception(SIGTRAP, regs, TRAP_BRKPT, regs->nip);
958 return;
959 }
960
cd8a5673
PM
961 local_irq_enable();
962
04903a30
KG
963#ifdef CONFIG_MATH_EMULATION
964 /* (reason & REASON_ILLEGAL) would be the obvious thing here,
965 * but there seems to be a hardware bug on the 405GP (RevD)
966 * that means ESR is sometimes set incorrectly - either to
967 * ESR_DST (!?) or 0. In the process of chasing this with the
968 * hardware people - not sure if it can happen on any illegal
969 * instruction or only on FP instructions, whether there is a
970 * pattern to occurences etc. -dgibson 31/Mar/2003 */
5fad293b
KG
971 switch (do_mathemu(regs)) {
972 case 0:
04903a30
KG
973 emulate_single_step(regs);
974 return;
5fad293b
KG
975 case 1: {
976 int code = 0;
977 code = __parse_fpscr(current->thread.fpscr.val);
978 _exception(SIGFPE, regs, code, regs->nip);
979 return;
980 }
981 case -EFAULT:
982 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
983 return;
04903a30 984 }
5fad293b 985 /* fall through on any other errors */
04903a30
KG
986#endif /* CONFIG_MATH_EMULATION */
987
8dad3f92
PM
988 /* Try to emulate it if we should. */
989 if (reason & (REASON_ILLEGAL | REASON_PRIVILEGED)) {
14cf11af
PM
990 switch (emulate_instruction(regs)) {
991 case 0:
992 regs->nip += 4;
993 emulate_single_step(regs);
8dad3f92 994 return;
14cf11af
PM
995 case -EFAULT:
996 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
8dad3f92 997 return;
14cf11af
PM
998 }
999 }
8dad3f92
PM
1000
1001 if (reason & REASON_PRIVILEGED)
1002 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1003 else
1004 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
14cf11af
PM
1005}
1006
dc1c1ca3 1007void alignment_exception(struct pt_regs *regs)
14cf11af 1008{
4393c4f6 1009 int sig, code, fixed = 0;
14cf11af 1010
e9370ae1
PM
1011 /* we don't implement logging of alignment exceptions */
1012 if (!(current->thread.align_ctl & PR_UNALIGN_SIGBUS))
1013 fixed = fix_alignment(regs);
14cf11af
PM
1014
1015 if (fixed == 1) {
1016 regs->nip += 4; /* skip over emulated instruction */
1017 emulate_single_step(regs);
1018 return;
1019 }
1020
dc1c1ca3 1021 /* Operand address was bad */
14cf11af 1022 if (fixed == -EFAULT) {
4393c4f6
BH
1023 sig = SIGSEGV;
1024 code = SEGV_ACCERR;
1025 } else {
1026 sig = SIGBUS;
1027 code = BUS_ADRALN;
14cf11af 1028 }
4393c4f6
BH
1029 if (user_mode(regs))
1030 _exception(sig, regs, code, regs->dar);
1031 else
1032 bad_page_fault(regs, regs->dar, sig);
14cf11af
PM
1033}
1034
1035void StackOverflow(struct pt_regs *regs)
1036{
1037 printk(KERN_CRIT "Kernel stack overflow in process %p, r1=%lx\n",
1038 current, regs->gpr[1]);
1039 debugger(regs);
1040 show_regs(regs);
1041 panic("kernel stack overflow");
1042}
1043
1044void nonrecoverable_exception(struct pt_regs *regs)
1045{
1046 printk(KERN_ERR "Non-recoverable exception at PC=%lx MSR=%lx\n",
1047 regs->nip, regs->msr);
1048 debugger(regs);
1049 die("nonrecoverable exception", regs, SIGKILL);
1050}
1051
1052void trace_syscall(struct pt_regs *regs)
1053{
1054 printk("Task: %p(%d), PC: %08lX/%08lX, Syscall: %3ld, Result: %s%ld %s\n",
19c5870c 1055 current, task_pid_nr(current), regs->nip, regs->link, regs->gpr[0],
14cf11af
PM
1056 regs->ccr&0x10000000?"Error=":"", regs->gpr[3], print_tainted());
1057}
dc1c1ca3 1058
dc1c1ca3
SR
1059void kernel_fp_unavailable_exception(struct pt_regs *regs)
1060{
1061 printk(KERN_EMERG "Unrecoverable FP Unavailable Exception "
1062 "%lx at %lx\n", regs->trap, regs->nip);
1063 die("Unrecoverable FP Unavailable Exception", regs, SIGABRT);
1064}
dc1c1ca3
SR
1065
1066void altivec_unavailable_exception(struct pt_regs *regs)
1067{
dc1c1ca3
SR
1068 if (user_mode(regs)) {
1069 /* A user program has executed an altivec instruction,
1070 but this kernel doesn't support altivec. */
1071 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1072 return;
1073 }
6c4841c2 1074
dc1c1ca3
SR
1075 printk(KERN_EMERG "Unrecoverable VMX/Altivec Unavailable Exception "
1076 "%lx at %lx\n", regs->trap, regs->nip);
1077 die("Unrecoverable VMX/Altivec Unavailable Exception", regs, SIGABRT);
dc1c1ca3
SR
1078}
1079
ce48b210
MN
1080void vsx_unavailable_exception(struct pt_regs *regs)
1081{
1082 if (user_mode(regs)) {
1083 /* A user program has executed an vsx instruction,
1084 but this kernel doesn't support vsx. */
1085 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1086 return;
1087 }
1088
1089 printk(KERN_EMERG "Unrecoverable VSX Unavailable Exception "
1090 "%lx at %lx\n", regs->trap, regs->nip);
1091 die("Unrecoverable VSX Unavailable Exception", regs, SIGABRT);
1092}
1093
dc1c1ca3
SR
1094void performance_monitor_exception(struct pt_regs *regs)
1095{
89713ed1
AB
1096 __get_cpu_var(irq_stat).pmu_irqs++;
1097
dc1c1ca3
SR
1098 perf_irq(regs);
1099}
dc1c1ca3 1100
8dad3f92 1101#ifdef CONFIG_8xx
14cf11af
PM
1102void SoftwareEmulation(struct pt_regs *regs)
1103{
1104 extern int do_mathemu(struct pt_regs *);
1105 extern int Soft_emulate_8xx(struct pt_regs *);
5dd57a13 1106#if defined(CONFIG_MATH_EMULATION) || defined(CONFIG_8XX_MINIMAL_FPEMU)
14cf11af 1107 int errcode;
5dd57a13 1108#endif
14cf11af
PM
1109
1110 CHECK_FULL_REGS(regs);
1111
1112 if (!user_mode(regs)) {
1113 debugger(regs);
1114 die("Kernel Mode Software FPU Emulation", regs, SIGFPE);
1115 }
1116
1117#ifdef CONFIG_MATH_EMULATION
1118 errcode = do_mathemu(regs);
80947e7c 1119 if (errcode >= 0)
eecff81d 1120 PPC_WARN_EMULATED(math, regs);
5fad293b
KG
1121
1122 switch (errcode) {
1123 case 0:
1124 emulate_single_step(regs);
1125 return;
1126 case 1: {
1127 int code = 0;
1128 code = __parse_fpscr(current->thread.fpscr.val);
1129 _exception(SIGFPE, regs, code, regs->nip);
1130 return;
1131 }
1132 case -EFAULT:
1133 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1134 return;
1135 default:
1136 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1137 return;
1138 }
1139
5dd57a13 1140#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
14cf11af 1141 errcode = Soft_emulate_8xx(regs);
80947e7c 1142 if (errcode >= 0)
eecff81d 1143 PPC_WARN_EMULATED(8xx, regs);
80947e7c 1144
5fad293b
KG
1145 switch (errcode) {
1146 case 0:
14cf11af 1147 emulate_single_step(regs);
5fad293b
KG
1148 return;
1149 case 1:
1150 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1151 return;
1152 case -EFAULT:
1153 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->nip);
1154 return;
1155 }
5dd57a13
SW
1156#else
1157 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
5fad293b 1158#endif
14cf11af 1159}
8dad3f92 1160#endif /* CONFIG_8xx */
14cf11af 1161
172ae2e7 1162#ifdef CONFIG_PPC_ADV_DEBUG_REGS
3bffb652
DK
1163static void handle_debug(struct pt_regs *regs, unsigned long debug_status)
1164{
1165 int changed = 0;
1166 /*
1167 * Determine the cause of the debug event, clear the
1168 * event flags and send a trap to the handler. Torez
1169 */
1170 if (debug_status & (DBSR_DAC1R | DBSR_DAC1W)) {
1171 dbcr_dac(current) &= ~(DBCR_DAC1R | DBCR_DAC1W);
1172#ifdef CONFIG_PPC_ADV_DEBUG_DAC_RANGE
1173 current->thread.dbcr2 &= ~DBCR2_DAC12MODE;
1174#endif
1175 do_send_trap(regs, mfspr(SPRN_DAC1), debug_status, TRAP_HWBKPT,
1176 5);
1177 changed |= 0x01;
1178 } else if (debug_status & (DBSR_DAC2R | DBSR_DAC2W)) {
1179 dbcr_dac(current) &= ~(DBCR_DAC2R | DBCR_DAC2W);
1180 do_send_trap(regs, mfspr(SPRN_DAC2), debug_status, TRAP_HWBKPT,
1181 6);
1182 changed |= 0x01;
1183 } else if (debug_status & DBSR_IAC1) {
1184 current->thread.dbcr0 &= ~DBCR0_IAC1;
1185 dbcr_iac_range(current) &= ~DBCR_IAC12MODE;
1186 do_send_trap(regs, mfspr(SPRN_IAC1), debug_status, TRAP_HWBKPT,
1187 1);
1188 changed |= 0x01;
1189 } else if (debug_status & DBSR_IAC2) {
1190 current->thread.dbcr0 &= ~DBCR0_IAC2;
1191 do_send_trap(regs, mfspr(SPRN_IAC2), debug_status, TRAP_HWBKPT,
1192 2);
1193 changed |= 0x01;
1194 } else if (debug_status & DBSR_IAC3) {
1195 current->thread.dbcr0 &= ~DBCR0_IAC3;
1196 dbcr_iac_range(current) &= ~DBCR_IAC34MODE;
1197 do_send_trap(regs, mfspr(SPRN_IAC3), debug_status, TRAP_HWBKPT,
1198 3);
1199 changed |= 0x01;
1200 } else if (debug_status & DBSR_IAC4) {
1201 current->thread.dbcr0 &= ~DBCR0_IAC4;
1202 do_send_trap(regs, mfspr(SPRN_IAC4), debug_status, TRAP_HWBKPT,
1203 4);
1204 changed |= 0x01;
1205 }
1206 /*
1207 * At the point this routine was called, the MSR(DE) was turned off.
1208 * Check all other debug flags and see if that bit needs to be turned
1209 * back on or not.
1210 */
1211 if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0, current->thread.dbcr1))
1212 regs->msr |= MSR_DE;
1213 else
1214 /* Make sure the IDM flag is off */
1215 current->thread.dbcr0 &= ~DBCR0_IDM;
1216
1217 if (changed & 0x01)
1218 mtspr(SPRN_DBCR0, current->thread.dbcr0);
1219}
14cf11af 1220
f8279621 1221void __kprobes DebugException(struct pt_regs *regs, unsigned long debug_status)
14cf11af 1222{
3bffb652
DK
1223 current->thread.dbsr = debug_status;
1224
ec097c84
RM
1225 /* Hack alert: On BookE, Branch Taken stops on the branch itself, while
1226 * on server, it stops on the target of the branch. In order to simulate
1227 * the server behaviour, we thus restart right away with a single step
1228 * instead of stopping here when hitting a BT
1229 */
1230 if (debug_status & DBSR_BT) {
1231 regs->msr &= ~MSR_DE;
1232
1233 /* Disable BT */
1234 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_BT);
1235 /* Clear the BT event */
1236 mtspr(SPRN_DBSR, DBSR_BT);
1237
1238 /* Do the single step trick only when coming from userspace */
1239 if (user_mode(regs)) {
1240 current->thread.dbcr0 &= ~DBCR0_BT;
1241 current->thread.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1242 regs->msr |= MSR_DE;
1243 return;
1244 }
1245
1246 if (notify_die(DIE_SSTEP, "block_step", regs, 5,
1247 5, SIGTRAP) == NOTIFY_STOP) {
1248 return;
1249 }
1250 if (debugger_sstep(regs))
1251 return;
1252 } else if (debug_status & DBSR_IC) { /* Instruction complete */
14cf11af 1253 regs->msr &= ~MSR_DE;
f8279621
KG
1254
1255 /* Disable instruction completion */
1256 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) & ~DBCR0_IC);
1257 /* Clear the instruction completion event */
1258 mtspr(SPRN_DBSR, DBSR_IC);
1259
1260 if (notify_die(DIE_SSTEP, "single_step", regs, 5,
1261 5, SIGTRAP) == NOTIFY_STOP) {
1262 return;
1263 }
1264
1265 if (debugger_sstep(regs))
1266 return;
1267
d6a61bfc 1268 if (user_mode(regs)) {
3bffb652
DK
1269 current->thread.dbcr0 &= ~DBCR0_IC;
1270#ifdef CONFIG_PPC_ADV_DEBUG_REGS
1271 if (DBCR_ACTIVE_EVENTS(current->thread.dbcr0,
1272 current->thread.dbcr1))
1273 regs->msr |= MSR_DE;
1274 else
1275 /* Make sure the IDM bit is off */
1276 current->thread.dbcr0 &= ~DBCR0_IDM;
1277#endif
d6a61bfc 1278 }
3bffb652
DK
1279
1280 _exception(SIGTRAP, regs, TRAP_TRACE, regs->nip);
1281 } else
1282 handle_debug(regs, debug_status);
14cf11af 1283}
172ae2e7 1284#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
14cf11af
PM
1285
1286#if !defined(CONFIG_TAU_INT)
1287void TAUException(struct pt_regs *regs)
1288{
1289 printk("TAU trap at PC: %lx, MSR: %lx, vector=%lx %s\n",
1290 regs->nip, regs->msr, regs->trap, print_tainted());
1291}
1292#endif /* CONFIG_INT_TAU */
14cf11af
PM
1293
1294#ifdef CONFIG_ALTIVEC
dc1c1ca3 1295void altivec_assist_exception(struct pt_regs *regs)
14cf11af
PM
1296{
1297 int err;
1298
14cf11af
PM
1299 if (!user_mode(regs)) {
1300 printk(KERN_EMERG "VMX/Altivec assist exception in kernel mode"
1301 " at %lx\n", regs->nip);
8dad3f92 1302 die("Kernel VMX/Altivec assist exception", regs, SIGILL);
14cf11af
PM
1303 }
1304
dc1c1ca3 1305 flush_altivec_to_thread(current);
dc1c1ca3 1306
eecff81d 1307 PPC_WARN_EMULATED(altivec, regs);
14cf11af
PM
1308 err = emulate_altivec(regs);
1309 if (err == 0) {
1310 regs->nip += 4; /* skip emulated instruction */
1311 emulate_single_step(regs);
1312 return;
1313 }
1314
1315 if (err == -EFAULT) {
1316 /* got an error reading the instruction */
1317 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1318 } else {
1319 /* didn't recognize the instruction */
1320 /* XXX quick hack for now: set the non-Java bit in the VSCR */
1321 if (printk_ratelimit())
1322 printk(KERN_ERR "Unrecognized altivec instruction "
1323 "in %s at %lx\n", current->comm, regs->nip);
1324 current->thread.vscr.u[3] |= 0x10000;
1325 }
1326}
1327#endif /* CONFIG_ALTIVEC */
1328
ce48b210
MN
1329#ifdef CONFIG_VSX
1330void vsx_assist_exception(struct pt_regs *regs)
1331{
1332 if (!user_mode(regs)) {
1333 printk(KERN_EMERG "VSX assist exception in kernel mode"
1334 " at %lx\n", regs->nip);
1335 die("Kernel VSX assist exception", regs, SIGILL);
1336 }
1337
1338 flush_vsx_to_thread(current);
1339 printk(KERN_INFO "VSX assist not supported at %lx\n", regs->nip);
1340 _exception(SIGILL, regs, ILL_ILLOPC, regs->nip);
1341}
1342#endif /* CONFIG_VSX */
1343
14cf11af 1344#ifdef CONFIG_FSL_BOOKE
620165f9
KG
1345
1346void doorbell_exception(struct pt_regs *regs)
1347{
1348#ifdef CONFIG_SMP
1349 int cpu = smp_processor_id();
1350 int msg;
1351
1352 if (num_online_cpus() < 2)
1353 return;
1354
1355 for (msg = 0; msg < 4; msg++)
1356 if (test_and_clear_bit(msg, &dbell_smp_message[cpu]))
1357 smp_message_recv(msg);
1358#else
1359 printk(KERN_WARNING "Received doorbell on non-smp system\n");
1360#endif
1361}
1362
14cf11af
PM
1363void CacheLockingException(struct pt_regs *regs, unsigned long address,
1364 unsigned long error_code)
1365{
1366 /* We treat cache locking instructions from the user
1367 * as priv ops, in the future we could try to do
1368 * something smarter
1369 */
1370 if (error_code & (ESR_DLK|ESR_ILK))
1371 _exception(SIGILL, regs, ILL_PRVOPC, regs->nip);
1372 return;
1373}
1374#endif /* CONFIG_FSL_BOOKE */
1375
1376#ifdef CONFIG_SPE
1377void SPEFloatingPointException(struct pt_regs *regs)
1378{
6a800f36 1379 extern int do_spe_mathemu(struct pt_regs *regs);
14cf11af
PM
1380 unsigned long spefscr;
1381 int fpexc_mode;
1382 int code = 0;
6a800f36
LY
1383 int err;
1384
1385 preempt_disable();
1386 if (regs->msr & MSR_SPE)
1387 giveup_spe(current);
1388 preempt_enable();
14cf11af
PM
1389
1390 spefscr = current->thread.spefscr;
1391 fpexc_mode = current->thread.fpexc_mode;
1392
14cf11af
PM
1393 if ((spefscr & SPEFSCR_FOVF) && (fpexc_mode & PR_FP_EXC_OVF)) {
1394 code = FPE_FLTOVF;
14cf11af
PM
1395 }
1396 else if ((spefscr & SPEFSCR_FUNF) && (fpexc_mode & PR_FP_EXC_UND)) {
1397 code = FPE_FLTUND;
14cf11af
PM
1398 }
1399 else if ((spefscr & SPEFSCR_FDBZ) && (fpexc_mode & PR_FP_EXC_DIV))
1400 code = FPE_FLTDIV;
1401 else if ((spefscr & SPEFSCR_FINV) && (fpexc_mode & PR_FP_EXC_INV)) {
1402 code = FPE_FLTINV;
14cf11af
PM
1403 }
1404 else if ((spefscr & (SPEFSCR_FG | SPEFSCR_FX)) && (fpexc_mode & PR_FP_EXC_RES))
1405 code = FPE_FLTRES;
1406
6a800f36
LY
1407 err = do_spe_mathemu(regs);
1408 if (err == 0) {
1409 regs->nip += 4; /* skip emulated instruction */
1410 emulate_single_step(regs);
1411 return;
1412 }
1413
1414 if (err == -EFAULT) {
1415 /* got an error reading the instruction */
1416 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1417 } else if (err == -EINVAL) {
1418 /* didn't recognize the instruction */
1419 printk(KERN_ERR "unrecognized spe instruction "
1420 "in %s at %lx\n", current->comm, regs->nip);
1421 } else {
1422 _exception(SIGFPE, regs, code, regs->nip);
1423 }
14cf11af 1424
14cf11af
PM
1425 return;
1426}
6a800f36
LY
1427
1428void SPEFloatingPointRoundException(struct pt_regs *regs)
1429{
1430 extern int speround_handler(struct pt_regs *regs);
1431 int err;
1432
1433 preempt_disable();
1434 if (regs->msr & MSR_SPE)
1435 giveup_spe(current);
1436 preempt_enable();
1437
1438 regs->nip -= 4;
1439 err = speround_handler(regs);
1440 if (err == 0) {
1441 regs->nip += 4; /* skip emulated instruction */
1442 emulate_single_step(regs);
1443 return;
1444 }
1445
1446 if (err == -EFAULT) {
1447 /* got an error reading the instruction */
1448 _exception(SIGSEGV, regs, SEGV_ACCERR, regs->nip);
1449 } else if (err == -EINVAL) {
1450 /* didn't recognize the instruction */
1451 printk(KERN_ERR "unrecognized spe instruction "
1452 "in %s at %lx\n", current->comm, regs->nip);
1453 } else {
1454 _exception(SIGFPE, regs, 0, regs->nip);
1455 return;
1456 }
1457}
14cf11af
PM
1458#endif
1459
dc1c1ca3
SR
1460/*
1461 * We enter here if we get an unrecoverable exception, that is, one
1462 * that happened at a point where the RI (recoverable interrupt) bit
1463 * in the MSR is 0. This indicates that SRR0/1 are live, and that
1464 * we therefore lost state by taking this exception.
1465 */
1466void unrecoverable_exception(struct pt_regs *regs)
1467{
1468 printk(KERN_EMERG "Unrecoverable exception %lx at %lx\n",
1469 regs->trap, regs->nip);
1470 die("Unrecoverable exception", regs, SIGABRT);
1471}
dc1c1ca3 1472
14cf11af
PM
1473#ifdef CONFIG_BOOKE_WDT
1474/*
1475 * Default handler for a Watchdog exception,
1476 * spins until a reboot occurs
1477 */
1478void __attribute__ ((weak)) WatchdogHandler(struct pt_regs *regs)
1479{
1480 /* Generic WatchdogHandler, implement your own */
1481 mtspr(SPRN_TCR, mfspr(SPRN_TCR)&(~TCR_WIE));
1482 return;
1483}
1484
1485void WatchdogException(struct pt_regs *regs)
1486{
1487 printk (KERN_EMERG "PowerPC Book-E Watchdog Exception\n");
1488 WatchdogHandler(regs);
1489}
1490#endif
dc1c1ca3 1491
dc1c1ca3
SR
1492/*
1493 * We enter here if we discover during exception entry that we are
1494 * running in supervisor mode with a userspace value in the stack pointer.
1495 */
1496void kernel_bad_stack(struct pt_regs *regs)
1497{
1498 printk(KERN_EMERG "Bad kernel stack pointer %lx at %lx\n",
1499 regs->gpr[1], regs->nip);
1500 die("Bad kernel stack pointer", regs, SIGABRT);
1501}
14cf11af
PM
1502
1503void __init trap_init(void)
1504{
1505}
80947e7c
GU
1506
1507
1508#ifdef CONFIG_PPC_EMULATED_STATS
1509
1510#define WARN_EMULATED_SETUP(type) .type = { .name = #type }
1511
1512struct ppc_emulated ppc_emulated = {
1513#ifdef CONFIG_ALTIVEC
1514 WARN_EMULATED_SETUP(altivec),
1515#endif
1516 WARN_EMULATED_SETUP(dcba),
1517 WARN_EMULATED_SETUP(dcbz),
1518 WARN_EMULATED_SETUP(fp_pair),
1519 WARN_EMULATED_SETUP(isel),
1520 WARN_EMULATED_SETUP(mcrxr),
1521 WARN_EMULATED_SETUP(mfpvr),
1522 WARN_EMULATED_SETUP(multiple),
1523 WARN_EMULATED_SETUP(popcntb),
1524 WARN_EMULATED_SETUP(spe),
1525 WARN_EMULATED_SETUP(string),
1526 WARN_EMULATED_SETUP(unaligned),
1527#ifdef CONFIG_MATH_EMULATION
1528 WARN_EMULATED_SETUP(math),
1529#elif defined(CONFIG_8XX_MINIMAL_FPEMU)
1530 WARN_EMULATED_SETUP(8xx),
1531#endif
1532#ifdef CONFIG_VSX
1533 WARN_EMULATED_SETUP(vsx),
1534#endif
1535};
1536
1537u32 ppc_warn_emulated;
1538
1539void ppc_warn_emulated_print(const char *type)
1540{
1541 if (printk_ratelimit())
1542 pr_warning("%s used emulated %s instruction\n", current->comm,
1543 type);
1544}
1545
1546static int __init ppc_warn_emulated_init(void)
1547{
1548 struct dentry *dir, *d;
1549 unsigned int i;
1550 struct ppc_emulated_entry *entries = (void *)&ppc_emulated;
1551
1552 if (!powerpc_debugfs_root)
1553 return -ENODEV;
1554
1555 dir = debugfs_create_dir("emulated_instructions",
1556 powerpc_debugfs_root);
1557 if (!dir)
1558 return -ENOMEM;
1559
1560 d = debugfs_create_u32("do_warn", S_IRUGO | S_IWUSR, dir,
1561 &ppc_warn_emulated);
1562 if (!d)
1563 goto fail;
1564
1565 for (i = 0; i < sizeof(ppc_emulated)/sizeof(*entries); i++) {
1566 d = debugfs_create_u32(entries[i].name, S_IRUGO | S_IWUSR, dir,
1567 (u32 *)&entries[i].val.counter);
1568 if (!d)
1569 goto fail;
1570 }
1571
1572 return 0;
1573
1574fail:
1575 debugfs_remove_recursive(dir);
1576 return -ENOMEM;
1577}
1578
1579device_initcall(ppc_warn_emulated_init);
1580
1581#endif /* CONFIG_PPC_EMULATED_STATS */