2 * PowerPC backend to the KGDB stub.
4 * 1998 (c) Michael AK Tesch (tesch@cs.wisc.edu)
5 * Copyright (C) 2003 Timesys Corporation.
6 * Copyright (C) 2004-2006 MontaVista Software, Inc.
7 * PPC64 Mods (C) 2005 Frank Rowand (frowand@mvista.com)
8 * PPC32 support restored by Vitaly Wool <vwool@ru.mvista.com> and
9 * Sergei Shtylyov <sshtylyov@ru.mvista.com>
10 * Copyright (C) 2007-2008 Wind River Systems, Inc.
12 * This file is licensed under the terms of the GNU General Public License
13 * version 2. This program as licensed "as is" without any warranty of any
14 * kind, whether express or implied.
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/kgdb.h>
20 #include <linux/smp.h>
21 #include <linux/signal.h>
22 #include <linux/ptrace.h>
23 #include <asm/current.h>
24 #include <asm/processor.h>
25 #include <asm/machdep.h>
28 * This table contains the mapping between PowerPC hardware trap types, and
29 * signals, which are primarily what GDB understands. GDB and the kernel
30 * don't always agree on values, so we use constants taken from gdb-6.2.
32 static struct hard_trap_info
34 unsigned int tt
; /* Trap type code for powerpc */
35 unsigned char signo
; /* Signal that we map this trap into */
36 } hard_trap_info
[] = {
37 { 0x0100, 0x02 /* SIGINT */ }, /* system reset */
38 { 0x0200, 0x0b /* SIGSEGV */ }, /* machine check */
39 { 0x0300, 0x0b /* SIGSEGV */ }, /* data access */
40 { 0x0400, 0x0b /* SIGSEGV */ }, /* instruction access */
41 { 0x0500, 0x02 /* SIGINT */ }, /* external interrupt */
42 { 0x0600, 0x0a /* SIGBUS */ }, /* alignment */
43 { 0x0700, 0x05 /* SIGTRAP */ }, /* program check */
44 { 0x0800, 0x08 /* SIGFPE */ }, /* fp unavailable */
45 { 0x0900, 0x0e /* SIGALRM */ }, /* decrementer */
46 { 0x0c00, 0x14 /* SIGCHLD */ }, /* system call */
47 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
48 { 0x2002, 0x05 /* SIGTRAP */ }, /* debug */
49 #if defined(CONFIG_FSL_BOOKE)
50 { 0x2010, 0x08 /* SIGFPE */ }, /* spe unavailable */
51 { 0x2020, 0x08 /* SIGFPE */ }, /* spe unavailable */
52 { 0x2030, 0x08 /* SIGFPE */ }, /* spe fp data */
53 { 0x2040, 0x08 /* SIGFPE */ }, /* spe fp data */
54 { 0x2050, 0x08 /* SIGFPE */ }, /* spe fp round */
55 { 0x2060, 0x0e /* SIGILL */ }, /* performance monitor */
56 { 0x2900, 0x08 /* SIGFPE */ }, /* apu unavailable */
57 { 0x3100, 0x0e /* SIGALRM */ }, /* fixed interval timer */
58 { 0x3200, 0x02 /* SIGINT */ }, /* watchdog */
59 #else /* ! CONFIG_FSL_BOOKE */
60 { 0x1000, 0x0e /* SIGALRM */ }, /* prog interval timer */
61 { 0x1010, 0x0e /* SIGALRM */ }, /* fixed interval timer */
62 { 0x1020, 0x02 /* SIGINT */ }, /* watchdog */
63 { 0x2010, 0x08 /* SIGFPE */ }, /* fp unavailable */
64 { 0x2020, 0x08 /* SIGFPE */ }, /* ap unavailable */
66 #else /* ! (defined(CONFIG_40x) || defined(CONFIG_BOOKE)) */
67 { 0x0d00, 0x05 /* SIGTRAP */ }, /* single-step */
68 #if defined(CONFIG_8xx)
69 { 0x1000, 0x04 /* SIGILL */ }, /* software emulation */
70 #else /* ! CONFIG_8xx */
71 { 0x0f00, 0x04 /* SIGILL */ }, /* performance monitor */
72 { 0x0f20, 0x08 /* SIGFPE */ }, /* altivec unavailable */
73 { 0x1300, 0x05 /* SIGTRAP */ }, /* instruction address break */
74 #if defined(CONFIG_PPC64)
75 { 0x1200, 0x05 /* SIGILL */ }, /* system error */
76 { 0x1500, 0x04 /* SIGILL */ }, /* soft patch */
77 { 0x1600, 0x04 /* SIGILL */ }, /* maintenance */
78 { 0x1700, 0x08 /* SIGFPE */ }, /* altivec assist */
79 { 0x1800, 0x04 /* SIGILL */ }, /* thermal */
80 #else /* ! CONFIG_PPC64 */
81 { 0x1400, 0x02 /* SIGINT */ }, /* SMI */
82 { 0x1600, 0x08 /* SIGFPE */ }, /* altivec assist */
83 { 0x1700, 0x04 /* SIGILL */ }, /* TAU */
84 { 0x2000, 0x05 /* SIGTRAP */ }, /* run mode */
88 { 0x0000, 0x00 } /* Must be last */
91 static int computeSignal(unsigned int tt
)
93 struct hard_trap_info
*ht
;
95 for (ht
= hard_trap_info
; ht
->tt
&& ht
->signo
; ht
++)
99 return SIGHUP
; /* default for things we don't know about */
102 static int kgdb_call_nmi_hook(struct pt_regs
*regs
)
104 kgdb_nmicallback(raw_smp_processor_id(), regs
);
109 void kgdb_roundup_cpus(unsigned long flags
)
111 smp_send_debugger_break(MSG_ALL_BUT_SELF
);
115 /* KGDB functions to use existing PowerPC64 hooks. */
116 static int kgdb_debugger(struct pt_regs
*regs
)
118 return kgdb_handle_exception(0, computeSignal(TRAP(regs
)), 0, regs
);
121 static int kgdb_handle_breakpoint(struct pt_regs
*regs
)
126 if (kgdb_handle_exception(0, SIGTRAP
, 0, regs
) != 0)
129 if (*(u32
*) (regs
->nip
) == *(u32
*) (&arch_kgdb_ops
.gdb_bpt_instr
))
135 static int kgdb_singlestep(struct pt_regs
*regs
)
137 struct thread_info
*thread_info
, *exception_thread_info
;
143 * On Book E and perhaps other processsors, singlestep is handled on
144 * the critical exception stack. This causes current_thread_info()
145 * to fail, since it it locates the thread_info by masking off
146 * the low bits of the current stack pointer. We work around
147 * this issue by copying the thread_info from the kernel stack
148 * before calling kgdb_handle_exception, and copying it back
149 * afterwards. On most processors the copy is avoided since
150 * exception_thread_info == thread_info.
152 thread_info
= (struct thread_info
*)(regs
->gpr
[1] & ~(THREAD_SIZE
-1));
153 exception_thread_info
= current_thread_info();
155 if (thread_info
!= exception_thread_info
)
156 memcpy(exception_thread_info
, thread_info
, sizeof *thread_info
);
158 kgdb_handle_exception(0, SIGTRAP
, 0, regs
);
160 if (thread_info
!= exception_thread_info
)
161 memcpy(thread_info
, exception_thread_info
, sizeof *thread_info
);
166 static int kgdb_iabr_match(struct pt_regs
*regs
)
171 if (kgdb_handle_exception(0, computeSignal(TRAP(regs
)), 0, regs
) != 0)
176 static int kgdb_dabr_match(struct pt_regs
*regs
)
181 if (kgdb_handle_exception(0, computeSignal(TRAP(regs
)), 0, regs
) != 0)
186 #define PACK64(ptr, src) do { *(ptr++) = (src); } while (0)
188 #define PACK32(ptr, src) do { \
190 ptr32 = (u32 *)ptr; \
191 *(ptr32++) = (src); \
192 ptr = (unsigned long *)ptr32; \
196 void pt_regs_to_gdb_regs(unsigned long *gdb_regs
, struct pt_regs
*regs
)
198 unsigned long *ptr
= gdb_regs
;
201 memset(gdb_regs
, 0, NUMREGBYTES
);
203 for (reg
= 0; reg
< 32; reg
++)
204 PACK64(ptr
, regs
->gpr
[reg
]);
206 #ifdef CONFIG_FSL_BOOKE
208 for (reg
= 0; reg
< 32; reg
++)
209 PACK64(ptr
, current
->thread
.evr
[reg
]);
214 /* fp registers not used by kernel, leave zero */
215 ptr
+= 32 * 8 / sizeof(long);
218 PACK64(ptr
, regs
->nip
);
219 PACK64(ptr
, regs
->msr
);
220 PACK32(ptr
, regs
->ccr
);
221 PACK64(ptr
, regs
->link
);
222 PACK64(ptr
, regs
->ctr
);
223 PACK32(ptr
, regs
->xer
);
225 BUG_ON((unsigned long)ptr
>
226 (unsigned long)(((void *)gdb_regs
) + NUMREGBYTES
));
229 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs
, struct task_struct
*p
)
231 struct pt_regs
*regs
= (struct pt_regs
*)(p
->thread
.ksp
+
232 STACK_FRAME_OVERHEAD
);
233 unsigned long *ptr
= gdb_regs
;
236 memset(gdb_regs
, 0, NUMREGBYTES
);
239 for (reg
= 0; reg
< 3; reg
++)
240 PACK64(ptr
, regs
->gpr
[reg
]);
242 /* Regs GPR3-13 are caller saved, not in regs->gpr[] */
246 for (reg
= 14; reg
< 32; reg
++)
247 PACK64(ptr
, regs
->gpr
[reg
]);
249 #ifdef CONFIG_FSL_BOOKE
251 for (reg
= 0; reg
< 32; reg
++)
252 PACK64(ptr
, p
->thread
.evr
[reg
]);
257 /* fp registers not used by kernel, leave zero */
258 ptr
+= 32 * 8 / sizeof(long);
261 PACK64(ptr
, regs
->nip
);
262 PACK64(ptr
, regs
->msr
);
263 PACK32(ptr
, regs
->ccr
);
264 PACK64(ptr
, regs
->link
);
265 PACK64(ptr
, regs
->ctr
);
266 PACK32(ptr
, regs
->xer
);
268 BUG_ON((unsigned long)ptr
>
269 (unsigned long)(((void *)gdb_regs
) + NUMREGBYTES
));
272 #define UNPACK64(dest, ptr) do { dest = *(ptr++); } while (0)
274 #define UNPACK32(dest, ptr) do { \
276 ptr32 = (u32 *)ptr; \
278 ptr = (unsigned long *)ptr32; \
281 void gdb_regs_to_pt_regs(unsigned long *gdb_regs
, struct pt_regs
*regs
)
283 unsigned long *ptr
= gdb_regs
;
286 for (reg
= 0; reg
< 32; reg
++)
287 UNPACK64(regs
->gpr
[reg
], ptr
);
289 #ifdef CONFIG_FSL_BOOKE
291 for (reg
= 0; reg
< 32; reg
++)
292 UNPACK64(current
->thread
.evr
[reg
], ptr
);
297 /* fp registers not used by kernel, leave zero */
298 ptr
+= 32 * 8 / sizeof(int);
301 UNPACK64(regs
->nip
, ptr
);
302 UNPACK64(regs
->msr
, ptr
);
303 UNPACK32(regs
->ccr
, ptr
);
304 UNPACK64(regs
->link
, ptr
);
305 UNPACK64(regs
->ctr
, ptr
);
306 UNPACK32(regs
->xer
, ptr
);
308 BUG_ON((unsigned long)ptr
>
309 (unsigned long)(((void *)gdb_regs
) + NUMREGBYTES
));
313 * This function does PowerPC specific procesing for interfacing to gdb.
315 int kgdb_arch_handle_exception(int vector
, int signo
, int err_code
,
316 char *remcom_in_buffer
, char *remcom_out_buffer
,
317 struct pt_regs
*linux_regs
)
319 char *ptr
= &remcom_in_buffer
[1];
322 switch (remcom_in_buffer
[0]) {
324 * sAA..AA Step one instruction from AA..AA
325 * This will return an error to gdb ..
329 /* handle the optional parameter */
330 if (kgdb_hex2long(&ptr
, &addr
))
331 linux_regs
->nip
= addr
;
333 atomic_set(&kgdb_cpu_doing_single_step
, -1);
334 /* set the trace bit if we're stepping */
335 if (remcom_in_buffer
[0] == 's') {
336 #ifdef CONFIG_PPC_ADV_DEBUG_REGS
338 mfspr(SPRN_DBCR0
) | DBCR0_IC
| DBCR0_IDM
);
339 linux_regs
->msr
|= MSR_DE
;
341 linux_regs
->msr
|= MSR_SE
;
343 kgdb_single_step
= 1;
344 atomic_set(&kgdb_cpu_doing_single_step
,
345 raw_smp_processor_id());
356 struct kgdb_arch arch_kgdb_ops
= {
357 .gdb_bpt_instr
= {0x7d, 0x82, 0x10, 0x08},
360 static int kgdb_not_implemented(struct pt_regs
*regs
)
365 static void *old__debugger_ipi
;
366 static void *old__debugger
;
367 static void *old__debugger_bpt
;
368 static void *old__debugger_sstep
;
369 static void *old__debugger_iabr_match
;
370 static void *old__debugger_dabr_match
;
371 static void *old__debugger_fault_handler
;
373 int kgdb_arch_init(void)
375 old__debugger_ipi
= __debugger_ipi
;
376 old__debugger
= __debugger
;
377 old__debugger_bpt
= __debugger_bpt
;
378 old__debugger_sstep
= __debugger_sstep
;
379 old__debugger_iabr_match
= __debugger_iabr_match
;
380 old__debugger_dabr_match
= __debugger_dabr_match
;
381 old__debugger_fault_handler
= __debugger_fault_handler
;
383 __debugger_ipi
= kgdb_call_nmi_hook
;
384 __debugger
= kgdb_debugger
;
385 __debugger_bpt
= kgdb_handle_breakpoint
;
386 __debugger_sstep
= kgdb_singlestep
;
387 __debugger_iabr_match
= kgdb_iabr_match
;
388 __debugger_dabr_match
= kgdb_dabr_match
;
389 __debugger_fault_handler
= kgdb_not_implemented
;
394 void kgdb_arch_exit(void)
396 __debugger_ipi
= old__debugger_ipi
;
397 __debugger
= old__debugger
;
398 __debugger_bpt
= old__debugger_bpt
;
399 __debugger_sstep
= old__debugger_sstep
;
400 __debugger_iabr_match
= old__debugger_iabr_match
;
401 __debugger_dabr_match
= old__debugger_dabr_match
;
402 __debugger_fault_handler
= old__debugger_fault_handler
;