Linux Audio

Check our new training course

Loading...
v5.9
  1/*
  2 * PowerPC backend to the KGDB stub.
  3 *
  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.
 11 *
 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.
 15 */
 16
 17#include <linux/kernel.h>
 
 18#include <linux/kgdb.h>
 19#include <linux/smp.h>
 20#include <linux/signal.h>
 21#include <linux/ptrace.h>
 22#include <linux/kdebug.h>
 23#include <asm/current.h>
 24#include <asm/processor.h>
 25#include <asm/machdep.h>
 26#include <asm/debug.h>
 27#include <asm/code-patching.h>
 28#include <linux/slab.h>
 29#include <asm/inst.h>
 30
 31/*
 32 * This table contains the mapping between PowerPC hardware trap types, and
 33 * signals, which are primarily what GDB understands.  GDB and the kernel
 34 * don't always agree on values, so we use constants taken from gdb-6.2.
 35 */
 36static struct hard_trap_info
 37{
 38	unsigned int tt;		/* Trap type code for powerpc */
 39	unsigned char signo;		/* Signal that we map this trap into */
 40} hard_trap_info[] = {
 41	{ 0x0100, 0x02 /* SIGINT */  },		/* system reset */
 42	{ 0x0200, 0x0b /* SIGSEGV */ },		/* machine check */
 43	{ 0x0300, 0x0b /* SIGSEGV */ },		/* data access */
 44	{ 0x0400, 0x0b /* SIGSEGV */ },		/* instruction access */
 45	{ 0x0500, 0x02 /* SIGINT */  },		/* external interrupt */
 46	{ 0x0600, 0x0a /* SIGBUS */  },		/* alignment */
 47	{ 0x0700, 0x05 /* SIGTRAP */ },		/* program check */
 48	{ 0x0800, 0x08 /* SIGFPE */  },		/* fp unavailable */
 49	{ 0x0900, 0x0e /* SIGALRM */ },		/* decrementer */
 50	{ 0x0c00, 0x14 /* SIGCHLD */ },		/* system call */
 51#if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
 52	{ 0x2002, 0x05 /* SIGTRAP */ },		/* debug */
 53#if defined(CONFIG_FSL_BOOKE)
 54	{ 0x2010, 0x08 /* SIGFPE */  },		/* spe unavailable */
 55	{ 0x2020, 0x08 /* SIGFPE */  },		/* spe unavailable */
 56	{ 0x2030, 0x08 /* SIGFPE */  },		/* spe fp data */
 57	{ 0x2040, 0x08 /* SIGFPE */  },		/* spe fp data */
 58	{ 0x2050, 0x08 /* SIGFPE */  },		/* spe fp round */
 59	{ 0x2060, 0x0e /* SIGILL */  },		/* performance monitor */
 60	{ 0x2900, 0x08 /* SIGFPE */  },		/* apu unavailable */
 61	{ 0x3100, 0x0e /* SIGALRM */ },		/* fixed interval timer */
 62	{ 0x3200, 0x02 /* SIGINT */  }, 	/* watchdog */
 63#else /* ! CONFIG_FSL_BOOKE */
 64	{ 0x1000, 0x0e /* SIGALRM */ },		/* prog interval timer */
 65	{ 0x1010, 0x0e /* SIGALRM */ },		/* fixed interval timer */
 66	{ 0x1020, 0x02 /* SIGINT */  }, 	/* watchdog */
 67	{ 0x2010, 0x08 /* SIGFPE */  },		/* fp unavailable */
 68	{ 0x2020, 0x08 /* SIGFPE */  },		/* ap unavailable */
 69#endif
 70#else /* ! (defined(CONFIG_40x) || defined(CONFIG_BOOKE)) */
 71	{ 0x0d00, 0x05 /* SIGTRAP */ },		/* single-step */
 72#if defined(CONFIG_PPC_8xx)
 73	{ 0x1000, 0x04 /* SIGILL */  },		/* software emulation */
 74#else /* ! CONFIG_PPC_8xx */
 75	{ 0x0f00, 0x04 /* SIGILL */  },		/* performance monitor */
 76	{ 0x0f20, 0x08 /* SIGFPE */  },		/* altivec unavailable */
 77	{ 0x1300, 0x05 /* SIGTRAP */ }, 	/* instruction address break */
 78#if defined(CONFIG_PPC64)
 79	{ 0x1200, 0x05 /* SIGILL */  },		/* system error */
 80	{ 0x1500, 0x04 /* SIGILL */  },		/* soft patch */
 81	{ 0x1600, 0x04 /* SIGILL */  },		/* maintenance */
 82	{ 0x1700, 0x08 /* SIGFPE */  },		/* altivec assist */
 83	{ 0x1800, 0x04 /* SIGILL */  },		/* thermal */
 84#else /* ! CONFIG_PPC64 */
 85	{ 0x1400, 0x02 /* SIGINT */  },		/* SMI */
 86	{ 0x1600, 0x08 /* SIGFPE */  },		/* altivec assist */
 87	{ 0x1700, 0x04 /* SIGILL */  },		/* TAU */
 88	{ 0x2000, 0x05 /* SIGTRAP */ },		/* run mode */
 89#endif
 90#endif
 91#endif
 92	{ 0x0000, 0x00 }			/* Must be last */
 93};
 94
 95static int computeSignal(unsigned int tt)
 96{
 97	struct hard_trap_info *ht;
 98
 99	for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
100		if (ht->tt == tt)
101			return ht->signo;
102
103	return SIGHUP;		/* default for things we don't know about */
104}
105
106/**
107 *
108 *	kgdb_skipexception - Bail out of KGDB when we've been triggered.
109 *	@exception: Exception vector number
110 *	@regs: Current &struct pt_regs.
111 *
112 *	On some architectures we need to skip a breakpoint exception when
113 *	it occurs after a breakpoint has been removed.
114 *
115 */
116int kgdb_skipexception(int exception, struct pt_regs *regs)
117{
118	return kgdb_isremovedbreak(regs->nip);
119}
120
121static int kgdb_debugger_ipi(struct pt_regs *regs)
122{
123	kgdb_nmicallback(raw_smp_processor_id(), regs);
124	return 0;
125}
126
127#ifdef CONFIG_SMP
128void kgdb_roundup_cpus(void)
129{
130	smp_send_debugger_break();
131}
132#endif
133
134/* KGDB functions to use existing PowerPC64 hooks. */
135static int kgdb_debugger(struct pt_regs *regs)
136{
137	return !kgdb_handle_exception(1, computeSignal(TRAP(regs)),
138				      DIE_OOPS, regs);
139}
140
141static int kgdb_handle_breakpoint(struct pt_regs *regs)
142{
143	if (user_mode(regs))
144		return 0;
145
146	if (kgdb_handle_exception(1, SIGTRAP, 0, regs) != 0)
147		return 0;
148
149	if (*(u32 *)regs->nip == BREAK_INSTR)
150		regs->nip += BREAK_INSTR_SIZE;
151
152	return 1;
153}
154
155static int kgdb_singlestep(struct pt_regs *regs)
156{
 
 
157	if (user_mode(regs))
158		return 0;
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160	kgdb_handle_exception(0, SIGTRAP, 0, regs);
161
 
 
 
162	return 1;
163}
164
165static int kgdb_iabr_match(struct pt_regs *regs)
166{
167	if (user_mode(regs))
168		return 0;
169
170	if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0)
171		return 0;
172	return 1;
173}
174
175static int kgdb_break_match(struct pt_regs *regs)
176{
177	if (user_mode(regs))
178		return 0;
179
180	if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0)
181		return 0;
182	return 1;
183}
184
185#define PACK64(ptr, src) do { *(ptr++) = (src); } while (0)
186
187#define PACK32(ptr, src) do {          \
188	u32 *ptr32;                   \
189	ptr32 = (u32 *)ptr;           \
190	*(ptr32++) = (src);           \
191	ptr = (unsigned long *)ptr32; \
192	} while (0)
193
194void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
195{
196	struct pt_regs *regs = (struct pt_regs *)(p->thread.ksp +
197						  STACK_FRAME_OVERHEAD);
198	unsigned long *ptr = gdb_regs;
199	int reg;
200
201	memset(gdb_regs, 0, NUMREGBYTES);
202
203	/* Regs GPR0-2 */
204	for (reg = 0; reg < 3; reg++)
205		PACK64(ptr, regs->gpr[reg]);
206
207	/* Regs GPR3-13 are caller saved, not in regs->gpr[] */
208	ptr += 11;
209
210	/* Regs GPR14-31 */
211	for (reg = 14; reg < 32; reg++)
212		PACK64(ptr, regs->gpr[reg]);
213
214#ifdef CONFIG_FSL_BOOKE
215#ifdef CONFIG_SPE
216	for (reg = 0; reg < 32; reg++)
217		PACK64(ptr, p->thread.evr[reg]);
218#else
219	ptr += 32;
220#endif
221#else
222	/* fp registers not used by kernel, leave zero */
223	ptr += 32 * 8 / sizeof(long);
224#endif
225
226	PACK64(ptr, regs->nip);
227	PACK64(ptr, regs->msr);
228	PACK32(ptr, regs->ccr);
229	PACK64(ptr, regs->link);
230	PACK64(ptr, regs->ctr);
231	PACK32(ptr, regs->xer);
232
233	BUG_ON((unsigned long)ptr >
234	       (unsigned long)(((void *)gdb_regs) + NUMREGBYTES));
235}
236
237#define GDB_SIZEOF_REG sizeof(unsigned long)
238#define GDB_SIZEOF_REG_U32 sizeof(u32)
239
240#ifdef CONFIG_FSL_BOOKE
241#define GDB_SIZEOF_FLOAT_REG sizeof(unsigned long)
242#else
243#define GDB_SIZEOF_FLOAT_REG sizeof(u64)
244#endif
245
246struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
247{
248	{ "r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[0]) },
249	{ "r1", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[1]) },
250	{ "r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[2]) },
251	{ "r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[3]) },
252	{ "r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[4]) },
253	{ "r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[5]) },
254	{ "r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[6]) },
255	{ "r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[7]) },
256	{ "r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[8]) },
257	{ "r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[9]) },
258	{ "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[10]) },
259	{ "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[11]) },
260	{ "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[12]) },
261	{ "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[13]) },
262	{ "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[14]) },
263	{ "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[15]) },
264	{ "r16", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[16]) },
265	{ "r17", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[17]) },
266	{ "r18", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[18]) },
267	{ "r19", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[19]) },
268	{ "r20", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[20]) },
269	{ "r21", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[21]) },
270	{ "r22", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[22]) },
271	{ "r23", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[23]) },
272	{ "r24", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[24]) },
273	{ "r25", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[25]) },
274	{ "r26", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[26]) },
275	{ "r27", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[27]) },
276	{ "r28", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[28]) },
277	{ "r29", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[29]) },
278	{ "r30", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[30]) },
279	{ "r31", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[31]) },
280
281	{ "f0", GDB_SIZEOF_FLOAT_REG, 0 },
282	{ "f1", GDB_SIZEOF_FLOAT_REG, 1 },
283	{ "f2", GDB_SIZEOF_FLOAT_REG, 2 },
284	{ "f3", GDB_SIZEOF_FLOAT_REG, 3 },
285	{ "f4", GDB_SIZEOF_FLOAT_REG, 4 },
286	{ "f5", GDB_SIZEOF_FLOAT_REG, 5 },
287	{ "f6", GDB_SIZEOF_FLOAT_REG, 6 },
288	{ "f7", GDB_SIZEOF_FLOAT_REG, 7 },
289	{ "f8", GDB_SIZEOF_FLOAT_REG, 8 },
290	{ "f9", GDB_SIZEOF_FLOAT_REG, 9 },
291	{ "f10", GDB_SIZEOF_FLOAT_REG, 10 },
292	{ "f11", GDB_SIZEOF_FLOAT_REG, 11 },
293	{ "f12", GDB_SIZEOF_FLOAT_REG, 12 },
294	{ "f13", GDB_SIZEOF_FLOAT_REG, 13 },
295	{ "f14", GDB_SIZEOF_FLOAT_REG, 14 },
296	{ "f15", GDB_SIZEOF_FLOAT_REG, 15 },
297	{ "f16", GDB_SIZEOF_FLOAT_REG, 16 },
298	{ "f17", GDB_SIZEOF_FLOAT_REG, 17 },
299	{ "f18", GDB_SIZEOF_FLOAT_REG, 18 },
300	{ "f19", GDB_SIZEOF_FLOAT_REG, 19 },
301	{ "f20", GDB_SIZEOF_FLOAT_REG, 20 },
302	{ "f21", GDB_SIZEOF_FLOAT_REG, 21 },
303	{ "f22", GDB_SIZEOF_FLOAT_REG, 22 },
304	{ "f23", GDB_SIZEOF_FLOAT_REG, 23 },
305	{ "f24", GDB_SIZEOF_FLOAT_REG, 24 },
306	{ "f25", GDB_SIZEOF_FLOAT_REG, 25 },
307	{ "f26", GDB_SIZEOF_FLOAT_REG, 26 },
308	{ "f27", GDB_SIZEOF_FLOAT_REG, 27 },
309	{ "f28", GDB_SIZEOF_FLOAT_REG, 28 },
310	{ "f29", GDB_SIZEOF_FLOAT_REG, 29 },
311	{ "f30", GDB_SIZEOF_FLOAT_REG, 30 },
312	{ "f31", GDB_SIZEOF_FLOAT_REG, 31 },
313
314	{ "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, nip) },
315	{ "msr", GDB_SIZEOF_REG, offsetof(struct pt_regs, msr) },
316	{ "cr", GDB_SIZEOF_REG_U32, offsetof(struct pt_regs, ccr) },
317	{ "lr", GDB_SIZEOF_REG, offsetof(struct pt_regs, link) },
318	{ "ctr", GDB_SIZEOF_REG_U32, offsetof(struct pt_regs, ctr) },
319	{ "xer", GDB_SIZEOF_REG, offsetof(struct pt_regs, xer) },
320};
321
322char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
323{
324	if (regno >= DBG_MAX_REG_NUM || regno < 0)
325		return NULL;
326
327	if (regno < 32 || regno >= 64)
328		/* First 0 -> 31 gpr registers*/
329		/* pc, msr, ls... registers 64 -> 69 */
330		memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
331				dbg_reg_def[regno].size);
332
333	if (regno >= 32 && regno < 64) {
334		/* FP registers 32 -> 63 */
335#if defined(CONFIG_FSL_BOOKE) && defined(CONFIG_SPE)
336		if (current)
337			memcpy(mem, &current->thread.evr[regno-32],
338					dbg_reg_def[regno].size);
339#else
340		/* fp registers not used by kernel, leave zero */
341		memset(mem, 0, dbg_reg_def[regno].size);
342#endif
343	}
344
345	return dbg_reg_def[regno].name;
346}
347
348int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
349{
350	if (regno >= DBG_MAX_REG_NUM || regno < 0)
351		return -EINVAL;
352
353	if (regno < 32 || regno >= 64)
354		/* First 0 -> 31 gpr registers*/
355		/* pc, msr, ls... registers 64 -> 69 */
356		memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
357				dbg_reg_def[regno].size);
358
359	if (regno >= 32 && regno < 64) {
360		/* FP registers 32 -> 63 */
361#if defined(CONFIG_FSL_BOOKE) && defined(CONFIG_SPE)
362		memcpy(&current->thread.evr[regno-32], mem,
363				dbg_reg_def[regno].size);
364#else
365		/* fp registers not used by kernel, leave zero */
366		return 0;
367#endif
368	}
369
370	return 0;
371}
372
373void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
374{
375	regs->nip = pc;
376}
377
378/*
379 * This function does PowerPC specific procesing for interfacing to gdb.
380 */
381int kgdb_arch_handle_exception(int vector, int signo, int err_code,
382			       char *remcom_in_buffer, char *remcom_out_buffer,
383			       struct pt_regs *linux_regs)
384{
385	char *ptr = &remcom_in_buffer[1];
386	unsigned long addr;
387
388	switch (remcom_in_buffer[0]) {
389		/*
390		 * sAA..AA   Step one instruction from AA..AA
391		 * This will return an error to gdb ..
392		 */
393	case 's':
394	case 'c':
395		/* handle the optional parameter */
396		if (kgdb_hex2long(&ptr, &addr))
397			linux_regs->nip = addr;
398
399		atomic_set(&kgdb_cpu_doing_single_step, -1);
400		/* set the trace bit if we're stepping */
401		if (remcom_in_buffer[0] == 's') {
402#ifdef CONFIG_PPC_ADV_DEBUG_REGS
403			mtspr(SPRN_DBCR0,
404			      mfspr(SPRN_DBCR0) | DBCR0_IC | DBCR0_IDM);
405			linux_regs->msr |= MSR_DE;
406#else
407			linux_regs->msr |= MSR_SE;
408#endif
 
409			atomic_set(&kgdb_cpu_doing_single_step,
410				   raw_smp_processor_id());
411		}
412		return 0;
413	}
414
415	return -1;
416}
417
418int kgdb_arch_set_breakpoint(struct kgdb_bkpt *bpt)
419{
420	int err;
421	unsigned int instr;
422	struct ppc_inst *addr = (struct ppc_inst *)bpt->bpt_addr;
423
424	err = get_kernel_nofault(instr, (unsigned *) addr);
425	if (err)
426		return err;
427
428	err = patch_instruction(addr, ppc_inst(BREAK_INSTR));
429	if (err)
430		return -EFAULT;
431
432	*(unsigned int *)bpt->saved_instr = instr;
433
434	return 0;
435}
436
437int kgdb_arch_remove_breakpoint(struct kgdb_bkpt *bpt)
438{
439	int err;
440	unsigned int instr = *(unsigned int *)bpt->saved_instr;
441	struct ppc_inst *addr = (struct ppc_inst *)bpt->bpt_addr;
442
443	err = patch_instruction(addr, ppc_inst(instr));
444	if (err)
445		return -EFAULT;
446
447	return 0;
448}
449
450/*
451 * Global data
452 */
453const struct kgdb_arch arch_kgdb_ops;
 
 
454
455static int kgdb_not_implemented(struct pt_regs *regs)
456{
457	return 0;
458}
459
460static void *old__debugger_ipi;
461static void *old__debugger;
462static void *old__debugger_bpt;
463static void *old__debugger_sstep;
464static void *old__debugger_iabr_match;
465static void *old__debugger_break_match;
466static void *old__debugger_fault_handler;
467
468int kgdb_arch_init(void)
469{
470	old__debugger_ipi = __debugger_ipi;
471	old__debugger = __debugger;
472	old__debugger_bpt = __debugger_bpt;
473	old__debugger_sstep = __debugger_sstep;
474	old__debugger_iabr_match = __debugger_iabr_match;
475	old__debugger_break_match = __debugger_break_match;
476	old__debugger_fault_handler = __debugger_fault_handler;
477
478	__debugger_ipi = kgdb_debugger_ipi;
479	__debugger = kgdb_debugger;
480	__debugger_bpt = kgdb_handle_breakpoint;
481	__debugger_sstep = kgdb_singlestep;
482	__debugger_iabr_match = kgdb_iabr_match;
483	__debugger_break_match = kgdb_break_match;
484	__debugger_fault_handler = kgdb_not_implemented;
485
486	return 0;
487}
488
489void kgdb_arch_exit(void)
490{
491	__debugger_ipi = old__debugger_ipi;
492	__debugger = old__debugger;
493	__debugger_bpt = old__debugger_bpt;
494	__debugger_sstep = old__debugger_sstep;
495	__debugger_iabr_match = old__debugger_iabr_match;
496	__debugger_break_match = old__debugger_break_match;
497	__debugger_fault_handler = old__debugger_fault_handler;
498}
v3.5.6
  1/*
  2 * PowerPC backend to the KGDB stub.
  3 *
  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.
 11 *
 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.
 15 */
 16
 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 <linux/kdebug.h>
 24#include <asm/current.h>
 25#include <asm/processor.h>
 26#include <asm/machdep.h>
 27#include <asm/debug.h>
 
 
 
 28
 29/*
 30 * This table contains the mapping between PowerPC hardware trap types, and
 31 * signals, which are primarily what GDB understands.  GDB and the kernel
 32 * don't always agree on values, so we use constants taken from gdb-6.2.
 33 */
 34static struct hard_trap_info
 35{
 36	unsigned int tt;		/* Trap type code for powerpc */
 37	unsigned char signo;		/* Signal that we map this trap into */
 38} hard_trap_info[] = {
 39	{ 0x0100, 0x02 /* SIGINT */  },		/* system reset */
 40	{ 0x0200, 0x0b /* SIGSEGV */ },		/* machine check */
 41	{ 0x0300, 0x0b /* SIGSEGV */ },		/* data access */
 42	{ 0x0400, 0x0b /* SIGSEGV */ },		/* instruction access */
 43	{ 0x0500, 0x02 /* SIGINT */  },		/* external interrupt */
 44	{ 0x0600, 0x0a /* SIGBUS */  },		/* alignment */
 45	{ 0x0700, 0x05 /* SIGTRAP */ },		/* program check */
 46	{ 0x0800, 0x08 /* SIGFPE */  },		/* fp unavailable */
 47	{ 0x0900, 0x0e /* SIGALRM */ },		/* decrementer */
 48	{ 0x0c00, 0x14 /* SIGCHLD */ },		/* system call */
 49#if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
 50	{ 0x2002, 0x05 /* SIGTRAP */ },		/* debug */
 51#if defined(CONFIG_FSL_BOOKE)
 52	{ 0x2010, 0x08 /* SIGFPE */  },		/* spe unavailable */
 53	{ 0x2020, 0x08 /* SIGFPE */  },		/* spe unavailable */
 54	{ 0x2030, 0x08 /* SIGFPE */  },		/* spe fp data */
 55	{ 0x2040, 0x08 /* SIGFPE */  },		/* spe fp data */
 56	{ 0x2050, 0x08 /* SIGFPE */  },		/* spe fp round */
 57	{ 0x2060, 0x0e /* SIGILL */  },		/* performance monitor */
 58	{ 0x2900, 0x08 /* SIGFPE */  },		/* apu unavailable */
 59	{ 0x3100, 0x0e /* SIGALRM */ },		/* fixed interval timer */
 60	{ 0x3200, 0x02 /* SIGINT */  }, 	/* watchdog */
 61#else /* ! CONFIG_FSL_BOOKE */
 62	{ 0x1000, 0x0e /* SIGALRM */ },		/* prog interval timer */
 63	{ 0x1010, 0x0e /* SIGALRM */ },		/* fixed interval timer */
 64	{ 0x1020, 0x02 /* SIGINT */  }, 	/* watchdog */
 65	{ 0x2010, 0x08 /* SIGFPE */  },		/* fp unavailable */
 66	{ 0x2020, 0x08 /* SIGFPE */  },		/* ap unavailable */
 67#endif
 68#else /* ! (defined(CONFIG_40x) || defined(CONFIG_BOOKE)) */
 69	{ 0x0d00, 0x05 /* SIGTRAP */ },		/* single-step */
 70#if defined(CONFIG_8xx)
 71	{ 0x1000, 0x04 /* SIGILL */  },		/* software emulation */
 72#else /* ! CONFIG_8xx */
 73	{ 0x0f00, 0x04 /* SIGILL */  },		/* performance monitor */
 74	{ 0x0f20, 0x08 /* SIGFPE */  },		/* altivec unavailable */
 75	{ 0x1300, 0x05 /* SIGTRAP */ }, 	/* instruction address break */
 76#if defined(CONFIG_PPC64)
 77	{ 0x1200, 0x05 /* SIGILL */  },		/* system error */
 78	{ 0x1500, 0x04 /* SIGILL */  },		/* soft patch */
 79	{ 0x1600, 0x04 /* SIGILL */  },		/* maintenance */
 80	{ 0x1700, 0x08 /* SIGFPE */  },		/* altivec assist */
 81	{ 0x1800, 0x04 /* SIGILL */  },		/* thermal */
 82#else /* ! CONFIG_PPC64 */
 83	{ 0x1400, 0x02 /* SIGINT */  },		/* SMI */
 84	{ 0x1600, 0x08 /* SIGFPE */  },		/* altivec assist */
 85	{ 0x1700, 0x04 /* SIGILL */  },		/* TAU */
 86	{ 0x2000, 0x05 /* SIGTRAP */ },		/* run mode */
 87#endif
 88#endif
 89#endif
 90	{ 0x0000, 0x00 }			/* Must be last */
 91};
 92
 93static int computeSignal(unsigned int tt)
 94{
 95	struct hard_trap_info *ht;
 96
 97	for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
 98		if (ht->tt == tt)
 99			return ht->signo;
100
101	return SIGHUP;		/* default for things we don't know about */
102}
103
104static int kgdb_call_nmi_hook(struct pt_regs *regs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105{
106	kgdb_nmicallback(raw_smp_processor_id(), regs);
107	return 0;
108}
109
110#ifdef CONFIG_SMP
111void kgdb_roundup_cpus(unsigned long flags)
112{
113	smp_send_debugger_break();
114}
115#endif
116
117/* KGDB functions to use existing PowerPC64 hooks. */
118static int kgdb_debugger(struct pt_regs *regs)
119{
120	return !kgdb_handle_exception(1, computeSignal(TRAP(regs)),
121				      DIE_OOPS, regs);
122}
123
124static int kgdb_handle_breakpoint(struct pt_regs *regs)
125{
126	if (user_mode(regs))
127		return 0;
128
129	if (kgdb_handle_exception(1, SIGTRAP, 0, regs) != 0)
130		return 0;
131
132	if (*(u32 *) (regs->nip) == *(u32 *) (&arch_kgdb_ops.gdb_bpt_instr))
133		regs->nip += BREAK_INSTR_SIZE;
134
135	return 1;
136}
137
138static int kgdb_singlestep(struct pt_regs *regs)
139{
140	struct thread_info *thread_info, *exception_thread_info;
141
142	if (user_mode(regs))
143		return 0;
144
145	/*
146	 * On Book E and perhaps other processors, singlestep is handled on
147	 * the critical exception stack.  This causes current_thread_info()
148	 * to fail, since it it locates the thread_info by masking off
149	 * the low bits of the current stack pointer.  We work around
150	 * this issue by copying the thread_info from the kernel stack
151	 * before calling kgdb_handle_exception, and copying it back
152	 * afterwards.  On most processors the copy is avoided since
153	 * exception_thread_info == thread_info.
154	 */
155	thread_info = (struct thread_info *)(regs->gpr[1] & ~(THREAD_SIZE-1));
156	exception_thread_info = current_thread_info();
157
158	if (thread_info != exception_thread_info)
159		memcpy(exception_thread_info, thread_info, sizeof *thread_info);
160
161	kgdb_handle_exception(0, SIGTRAP, 0, regs);
162
163	if (thread_info != exception_thread_info)
164		memcpy(thread_info, exception_thread_info, sizeof *thread_info);
165
166	return 1;
167}
168
169static int kgdb_iabr_match(struct pt_regs *regs)
170{
171	if (user_mode(regs))
172		return 0;
173
174	if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0)
175		return 0;
176	return 1;
177}
178
179static int kgdb_dabr_match(struct pt_regs *regs)
180{
181	if (user_mode(regs))
182		return 0;
183
184	if (kgdb_handle_exception(0, computeSignal(TRAP(regs)), 0, regs) != 0)
185		return 0;
186	return 1;
187}
188
189#define PACK64(ptr, src) do { *(ptr++) = (src); } while (0)
190
191#define PACK32(ptr, src) do {          \
192	u32 *ptr32;                   \
193	ptr32 = (u32 *)ptr;           \
194	*(ptr32++) = (src);           \
195	ptr = (unsigned long *)ptr32; \
196	} while (0)
197
198void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
199{
200	struct pt_regs *regs = (struct pt_regs *)(p->thread.ksp +
201						  STACK_FRAME_OVERHEAD);
202	unsigned long *ptr = gdb_regs;
203	int reg;
204
205	memset(gdb_regs, 0, NUMREGBYTES);
206
207	/* Regs GPR0-2 */
208	for (reg = 0; reg < 3; reg++)
209		PACK64(ptr, regs->gpr[reg]);
210
211	/* Regs GPR3-13 are caller saved, not in regs->gpr[] */
212	ptr += 11;
213
214	/* Regs GPR14-31 */
215	for (reg = 14; reg < 32; reg++)
216		PACK64(ptr, regs->gpr[reg]);
217
218#ifdef CONFIG_FSL_BOOKE
219#ifdef CONFIG_SPE
220	for (reg = 0; reg < 32; reg++)
221		PACK64(ptr, p->thread.evr[reg]);
222#else
223	ptr += 32;
224#endif
225#else
226	/* fp registers not used by kernel, leave zero */
227	ptr += 32 * 8 / sizeof(long);
228#endif
229
230	PACK64(ptr, regs->nip);
231	PACK64(ptr, regs->msr);
232	PACK32(ptr, regs->ccr);
233	PACK64(ptr, regs->link);
234	PACK64(ptr, regs->ctr);
235	PACK32(ptr, regs->xer);
236
237	BUG_ON((unsigned long)ptr >
238	       (unsigned long)(((void *)gdb_regs) + NUMREGBYTES));
239}
240
241#define GDB_SIZEOF_REG sizeof(unsigned long)
242#define GDB_SIZEOF_REG_U32 sizeof(u32)
243
244#ifdef CONFIG_FSL_BOOKE
245#define GDB_SIZEOF_FLOAT_REG sizeof(unsigned long)
246#else
247#define GDB_SIZEOF_FLOAT_REG sizeof(u64)
248#endif
249
250struct dbg_reg_def_t dbg_reg_def[DBG_MAX_REG_NUM] =
251{
252	{ "r0", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[0]) },
253	{ "r1", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[1]) },
254	{ "r2", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[2]) },
255	{ "r3", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[3]) },
256	{ "r4", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[4]) },
257	{ "r5", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[5]) },
258	{ "r6", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[6]) },
259	{ "r7", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[7]) },
260	{ "r8", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[8]) },
261	{ "r9", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[9]) },
262	{ "r10", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[10]) },
263	{ "r11", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[11]) },
264	{ "r12", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[12]) },
265	{ "r13", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[13]) },
266	{ "r14", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[14]) },
267	{ "r15", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[15]) },
268	{ "r16", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[16]) },
269	{ "r17", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[17]) },
270	{ "r18", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[18]) },
271	{ "r19", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[19]) },
272	{ "r20", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[20]) },
273	{ "r21", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[21]) },
274	{ "r22", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[22]) },
275	{ "r23", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[23]) },
276	{ "r24", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[24]) },
277	{ "r25", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[25]) },
278	{ "r26", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[26]) },
279	{ "r27", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[27]) },
280	{ "r28", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[28]) },
281	{ "r29", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[29]) },
282	{ "r30", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[30]) },
283	{ "r31", GDB_SIZEOF_REG, offsetof(struct pt_regs, gpr[31]) },
284
285	{ "f0", GDB_SIZEOF_FLOAT_REG, 0 },
286	{ "f1", GDB_SIZEOF_FLOAT_REG, 1 },
287	{ "f2", GDB_SIZEOF_FLOAT_REG, 2 },
288	{ "f3", GDB_SIZEOF_FLOAT_REG, 3 },
289	{ "f4", GDB_SIZEOF_FLOAT_REG, 4 },
290	{ "f5", GDB_SIZEOF_FLOAT_REG, 5 },
291	{ "f6", GDB_SIZEOF_FLOAT_REG, 6 },
292	{ "f7", GDB_SIZEOF_FLOAT_REG, 7 },
293	{ "f8", GDB_SIZEOF_FLOAT_REG, 8 },
294	{ "f9", GDB_SIZEOF_FLOAT_REG, 9 },
295	{ "f10", GDB_SIZEOF_FLOAT_REG, 10 },
296	{ "f11", GDB_SIZEOF_FLOAT_REG, 11 },
297	{ "f12", GDB_SIZEOF_FLOAT_REG, 12 },
298	{ "f13", GDB_SIZEOF_FLOAT_REG, 13 },
299	{ "f14", GDB_SIZEOF_FLOAT_REG, 14 },
300	{ "f15", GDB_SIZEOF_FLOAT_REG, 15 },
301	{ "f16", GDB_SIZEOF_FLOAT_REG, 16 },
302	{ "f17", GDB_SIZEOF_FLOAT_REG, 17 },
303	{ "f18", GDB_SIZEOF_FLOAT_REG, 18 },
304	{ "f19", GDB_SIZEOF_FLOAT_REG, 19 },
305	{ "f20", GDB_SIZEOF_FLOAT_REG, 20 },
306	{ "f21", GDB_SIZEOF_FLOAT_REG, 21 },
307	{ "f22", GDB_SIZEOF_FLOAT_REG, 22 },
308	{ "f23", GDB_SIZEOF_FLOAT_REG, 23 },
309	{ "f24", GDB_SIZEOF_FLOAT_REG, 24 },
310	{ "f25", GDB_SIZEOF_FLOAT_REG, 25 },
311	{ "f26", GDB_SIZEOF_FLOAT_REG, 26 },
312	{ "f27", GDB_SIZEOF_FLOAT_REG, 27 },
313	{ "f28", GDB_SIZEOF_FLOAT_REG, 28 },
314	{ "f29", GDB_SIZEOF_FLOAT_REG, 29 },
315	{ "f30", GDB_SIZEOF_FLOAT_REG, 30 },
316	{ "f31", GDB_SIZEOF_FLOAT_REG, 31 },
317
318	{ "pc", GDB_SIZEOF_REG, offsetof(struct pt_regs, nip) },
319	{ "msr", GDB_SIZEOF_REG, offsetof(struct pt_regs, msr) },
320	{ "cr", GDB_SIZEOF_REG_U32, offsetof(struct pt_regs, ccr) },
321	{ "lr", GDB_SIZEOF_REG, offsetof(struct pt_regs, link) },
322	{ "ctr", GDB_SIZEOF_REG_U32, offsetof(struct pt_regs, ctr) },
323	{ "xer", GDB_SIZEOF_REG, offsetof(struct pt_regs, xer) },
324};
325
326char *dbg_get_reg(int regno, void *mem, struct pt_regs *regs)
327{
328	if (regno >= DBG_MAX_REG_NUM || regno < 0)
329		return NULL;
330
331	if (regno < 32 || regno >= 64)
332		/* First 0 -> 31 gpr registers*/
333		/* pc, msr, ls... registers 64 -> 69 */
334		memcpy(mem, (void *)regs + dbg_reg_def[regno].offset,
335				dbg_reg_def[regno].size);
336
337	if (regno >= 32 && regno < 64) {
338		/* FP registers 32 -> 63 */
339#if defined(CONFIG_FSL_BOOKE) && defined(CONFIG_SPE)
340		if (current)
341			memcpy(mem, &current->thread.evr[regno-32],
342					dbg_reg_def[regno].size);
343#else
344		/* fp registers not used by kernel, leave zero */
345		memset(mem, 0, dbg_reg_def[regno].size);
346#endif
347	}
348
349	return dbg_reg_def[regno].name;
350}
351
352int dbg_set_reg(int regno, void *mem, struct pt_regs *regs)
353{
354	if (regno >= DBG_MAX_REG_NUM || regno < 0)
355		return -EINVAL;
356
357	if (regno < 32 || regno >= 64)
358		/* First 0 -> 31 gpr registers*/
359		/* pc, msr, ls... registers 64 -> 69 */
360		memcpy((void *)regs + dbg_reg_def[regno].offset, mem,
361				dbg_reg_def[regno].size);
362
363	if (regno >= 32 && regno < 64) {
364		/* FP registers 32 -> 63 */
365#if defined(CONFIG_FSL_BOOKE) && defined(CONFIG_SPE)
366		memcpy(&current->thread.evr[regno-32], mem,
367				dbg_reg_def[regno].size);
368#else
369		/* fp registers not used by kernel, leave zero */
370		return 0;
371#endif
372	}
373
374	return 0;
375}
376
377void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long pc)
378{
379	regs->nip = pc;
380}
381
382/*
383 * This function does PowerPC specific procesing for interfacing to gdb.
384 */
385int kgdb_arch_handle_exception(int vector, int signo, int err_code,
386			       char *remcom_in_buffer, char *remcom_out_buffer,
387			       struct pt_regs *linux_regs)
388{
389	char *ptr = &remcom_in_buffer[1];
390	unsigned long addr;
391
392	switch (remcom_in_buffer[0]) {
393		/*
394		 * sAA..AA   Step one instruction from AA..AA
395		 * This will return an error to gdb ..
396		 */
397	case 's':
398	case 'c':
399		/* handle the optional parameter */
400		if (kgdb_hex2long(&ptr, &addr))
401			linux_regs->nip = addr;
402
403		atomic_set(&kgdb_cpu_doing_single_step, -1);
404		/* set the trace bit if we're stepping */
405		if (remcom_in_buffer[0] == 's') {
406#ifdef CONFIG_PPC_ADV_DEBUG_REGS
407			mtspr(SPRN_DBCR0,
408			      mfspr(SPRN_DBCR0) | DBCR0_IC | DBCR0_IDM);
409			linux_regs->msr |= MSR_DE;
410#else
411			linux_regs->msr |= MSR_SE;
412#endif
413			kgdb_single_step = 1;
414			atomic_set(&kgdb_cpu_doing_single_step,
415				   raw_smp_processor_id());
416		}
417		return 0;
418	}
419
420	return -1;
421}
422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
423/*
424 * Global data
425 */
426struct kgdb_arch arch_kgdb_ops = {
427	.gdb_bpt_instr = {0x7d, 0x82, 0x10, 0x08},
428};
429
430static int kgdb_not_implemented(struct pt_regs *regs)
431{
432	return 0;
433}
434
435static void *old__debugger_ipi;
436static void *old__debugger;
437static void *old__debugger_bpt;
438static void *old__debugger_sstep;
439static void *old__debugger_iabr_match;
440static void *old__debugger_dabr_match;
441static void *old__debugger_fault_handler;
442
443int kgdb_arch_init(void)
444{
445	old__debugger_ipi = __debugger_ipi;
446	old__debugger = __debugger;
447	old__debugger_bpt = __debugger_bpt;
448	old__debugger_sstep = __debugger_sstep;
449	old__debugger_iabr_match = __debugger_iabr_match;
450	old__debugger_dabr_match = __debugger_dabr_match;
451	old__debugger_fault_handler = __debugger_fault_handler;
452
453	__debugger_ipi = kgdb_call_nmi_hook;
454	__debugger = kgdb_debugger;
455	__debugger_bpt = kgdb_handle_breakpoint;
456	__debugger_sstep = kgdb_singlestep;
457	__debugger_iabr_match = kgdb_iabr_match;
458	__debugger_dabr_match = kgdb_dabr_match;
459	__debugger_fault_handler = kgdb_not_implemented;
460
461	return 0;
462}
463
464void kgdb_arch_exit(void)
465{
466	__debugger_ipi = old__debugger_ipi;
467	__debugger = old__debugger;
468	__debugger_bpt = old__debugger_bpt;
469	__debugger_sstep = old__debugger_sstep;
470	__debugger_iabr_match = old__debugger_iabr_match;
471	__debugger_dabr_match = old__debugger_dabr_match;
472	__debugger_fault_handler = old__debugger_fault_handler;
473}