xref: /netbsd-src/sys/arch/arm/arm32/kgdb_machdep.c (revision de4fa6c51a9708fc05f88b618fa6fad87c9508ec)
1 /*	$NetBSD: kgdb_machdep.c,v 1.6 2009/03/14 15:36:01 dsl Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 Matthias Pfaller.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Matthias Pfaller.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: kgdb_machdep.c,v 1.6 2009/03/14 15:36:01 dsl Exp $");
35 
36 #include "opt_ddb.h"
37 #include "opt_kgdb.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kgdb.h>
42 
43 #include <uvm/uvm_extern.h>
44 
45 #include <machine/frame.h>
46 #include <machine/reg.h>
47 #include <machine/trap.h>
48 
49 /*
50  * Determine if the memory at va..(va+len) is valid.
51  */
52 int
53 kgdb_acc(vaddr_t va, size_t len)
54 {
55 	vaddr_t last_va;
56 
57 	last_va = va + len;
58 	va  &= ~PGOFSET;
59 	last_va &= ~PGOFSET;
60 
61 	do {
62 		if (db_validate_address(va))
63 			return (0);
64 		va  += PAGE_SIZE;
65 	} while (va < last_va);
66 
67 	return (1);
68 }
69 
70 /*
71  * Translate a trap number into a unix compatible signal value.
72  * (gdb only understands unix signal numbers).
73  */
74 int
75 kgdb_signal(int type)
76 {
77 
78 	switch (type) {
79 	case T_BREAKPOINT:
80 		return(SIGTRAP);
81 	case -1:
82 		return(SIGSEGV);
83 	default:
84 		return(SIGINT);
85 	}
86 }
87 
88 /*
89  * Definitions exported from gdb.
90  */
91 
92 /*
93  * Translate the values stored in the kernel regs struct to the format
94  * understood by gdb.
95  */
96 void
97 kgdb_getregs(db_regs_t *regs, kgdb_reg_t *gdb_regs)
98 {
99 
100 	gdb_regs[KGDB_REGNUM_R0 +  0] = regs->tf_r0;
101 	gdb_regs[KGDB_REGNUM_R0 +  1] = regs->tf_r1;
102 	gdb_regs[KGDB_REGNUM_R0 +  2] = regs->tf_r2;
103 	gdb_regs[KGDB_REGNUM_R0 +  3] = regs->tf_r3;
104 	gdb_regs[KGDB_REGNUM_R0 +  4] = regs->tf_r4;
105 	gdb_regs[KGDB_REGNUM_R0 +  5] = regs->tf_r5;
106 	gdb_regs[KGDB_REGNUM_R0 +  6] = regs->tf_r6;
107 	gdb_regs[KGDB_REGNUM_R0 +  7] = regs->tf_r7;
108 	gdb_regs[KGDB_REGNUM_R0 +  8] = regs->tf_r8;
109 	gdb_regs[KGDB_REGNUM_R0 +  9] = regs->tf_r9;
110 	gdb_regs[KGDB_REGNUM_R0 + 10] = regs->tf_r10;
111 	gdb_regs[KGDB_REGNUM_R0 + 11] = regs->tf_r11;
112 	gdb_regs[KGDB_REGNUM_R0 + 12] = regs->tf_r12;
113 	gdb_regs[KGDB_REGNUM_R0 + 13] = regs->tf_svc_sp;
114 	gdb_regs[KGDB_REGNUM_R0 + 14] = regs->tf_svc_lr;
115 	gdb_regs[KGDB_REGNUM_R0 + 15] = regs->tf_pc;
116 
117 	gdb_regs[KGDB_REGNUM_SPSR] = regs->tf_spsr;
118 }
119 
120 /*
121  * Reverse the above.
122  */
123 void
124 kgdb_setregs(db_regs_t *regs, kgdb_reg_t *gdb_regs)
125 {
126 	regs->tf_r0     = gdb_regs[KGDB_REGNUM_R0 +  0];
127 	regs->tf_r1     = gdb_regs[KGDB_REGNUM_R0 +  1];
128 	regs->tf_r2     = gdb_regs[KGDB_REGNUM_R0 +  2];
129 	regs->tf_r3     = gdb_regs[KGDB_REGNUM_R0 +  3];
130 	regs->tf_r4     = gdb_regs[KGDB_REGNUM_R0 +  4];
131 	regs->tf_r5     = gdb_regs[KGDB_REGNUM_R0 +  5];
132 	regs->tf_r6     = gdb_regs[KGDB_REGNUM_R0 +  6];
133 	regs->tf_r7     = gdb_regs[KGDB_REGNUM_R0 +  7];
134 	regs->tf_r8     = gdb_regs[KGDB_REGNUM_R0 +  8];
135 	regs->tf_r9     = gdb_regs[KGDB_REGNUM_R0 +  9];
136 	regs->tf_r10    = gdb_regs[KGDB_REGNUM_R0 + 10];
137 	regs->tf_r11    = gdb_regs[KGDB_REGNUM_R0 + 11];
138 	regs->tf_r12    = gdb_regs[KGDB_REGNUM_R0 + 12];
139 	regs->tf_svc_sp = gdb_regs[KGDB_REGNUM_R0 + 13];
140 	regs->tf_svc_lr = gdb_regs[KGDB_REGNUM_R0 + 14];
141 	regs->tf_pc     = gdb_regs[KGDB_REGNUM_R0 + 15];
142 
143 	regs->tf_spsr = gdb_regs[KGDB_REGNUM_SPSR];
144 }
145 
146 /*
147  * Trap into kgdb to wait for debugger to connect,
148  * noting on the console why nothing else is going on.
149  */
150 void
151 kgdb_connect(int verbose)
152 {
153 
154 	if (kgdb_dev == NODEV)
155 		return;
156 
157 	if (verbose)
158 		printf("kgdb waiting...");
159 
160 	__asm volatile(KBPT_ASM);
161 
162 	if (verbose)
163 		printf("connected.\n");
164 
165 	kgdb_debug_panic = 1;
166 }
167 
168 /*
169  * Decide what to do on panic.
170  * (This is called by panic, like Debugger())
171  */
172 void
173 kgdb_panic(void)
174 {
175 
176 	if (kgdb_dev != NODEV && kgdb_debug_panic) {
177 		printf("entering kgdb\n");
178 		kgdb_connect(kgdb_active == 0);
179 	}
180 }
181