xref: /netbsd/sys/arch/sh3/sh3/kgdb_machdep.c (revision 6550d01e)
1 /*	$NetBSD: kgdb_machdep.c,v 1.17 2009/11/21 17:40:28 rmind Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1996 Matthias Pfaller.
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
50  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
55  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  */
57 
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: kgdb_machdep.c,v 1.17 2009/11/21 17:40:28 rmind Exp $");
60 
61 #include "opt_ddb.h"
62 
63 #if defined(DDB)
64 #error "Can't build DDB and KGDB together."
65 #endif
66 
67 /*
68  * Machine-dependent functions for remote KGDB.  Originally written
69  * for NetBSD/pc532 by Matthias Pfaller.  Modified for NetBSD/i386
70  * by Jason R. Thorpe.  Modified for NetBSD/mips by Ethan Solomita.
71  * Modified for NetBSD/sh3 by UCHIYAMA Yasushi.
72  */
73 
74 #include <sys/types.h>
75 #include <sys/systm.h>
76 #include <sys/param.h>
77 #include <sys/proc.h>
78 #include <sys/reboot.h>
79 #include <sys/kgdb.h>
80 
81 #include <uvm/uvm_extern.h>
82 
83 #include <sh3/cpu.h>
84 
85 #include <machine/db_machdep.h>
86 #include <ddb/db_access.h>
87 
88 /*
89  * Is kva a valid address to access?  This is used by KGDB.
90  */
91 static int
92 kvacc(vaddr_t kva)
93 {
94 	pt_entry_t *pte;
95 
96 	if (kva < SH3_P1SEG_BASE)
97 		return (0);
98 
99 	if (kva < SH3_P2SEG_BASE)
100 		return (1);
101 
102 	if (kva >= VM_MAX_KERNEL_ADDRESS)
103 		return (0);
104 
105 	/* check kva is kernel virtual. */
106 	if ((kva < VM_MIN_KERNEL_ADDRESS) ||
107 	    (kva >= VM_MAX_KERNEL_ADDRESS))
108 		return (0);
109 
110 	/* check page which related kva is valid. */
111 	pte = __pmap_kpte_lookup(kva);
112 	if (!(*pte & PG_V))
113 		return (0);
114 
115 	return (1);
116 }
117 
118 /*
119  * Determine if the memory at va..(va+len) is valid.
120  */
121 int
122 kgdb_acc(vaddr_t va, size_t len)
123 {
124 	vaddr_t last_va;
125 
126 	last_va = va + len + PAGE_SIZE - 1;
127 	va  &= ~PGOFSET;
128 	last_va &= ~PGOFSET;
129 
130 	for (; va < last_va; va += PAGE_SIZE) {
131 		if (kvacc(va) == 0)
132 			return 0;
133 	}
134 
135 	return (1);
136 }
137 
138 /*
139  * Translate a trap number into a unix compatible signal value.
140  * (gdb only understands unix signal numbers).
141  */
142 int
143 kgdb_signal(int type)
144 {
145 
146 	switch (type) {
147 	case EXPEVT_TLB_MISS_LD:
148 	case EXPEVT_TLB_MISS_ST:
149 	case EXPEVT_TLB_MOD:
150 	case EXPEVT_TLB_PROT_LD:
151 	case EXPEVT_TLB_PROT_ST:
152 	case EXPEVT_ADDR_ERR_LD:
153 	case EXPEVT_ADDR_ERR_ST:
154 	case EXPEVT_TLB_MISS_LD | EXP_USER:
155 	case EXPEVT_TLB_MISS_ST | EXP_USER:
156 	case EXPEVT_TLB_MOD | EXP_USER:
157 	case EXPEVT_TLB_PROT_LD | EXP_USER:
158 	case EXPEVT_TLB_PROT_ST | EXP_USER:
159 	case EXPEVT_ADDR_ERR_LD | EXP_USER:
160 	case EXPEVT_ADDR_ERR_ST | EXP_USER:
161 		return (SIGSEGV);
162 
163 	case EXPEVT_TRAPA:
164 	case EXPEVT_BREAK:
165 	case EXPEVT_TRAPA | EXP_USER:
166 	case EXPEVT_BREAK | EXP_USER:
167 		return (SIGTRAP);
168 
169 	case EXPEVT_RES_INST:
170 	case EXPEVT_SLOT_INST:
171 	case EXPEVT_RES_INST | EXP_USER:
172 	case EXPEVT_SLOT_INST | EXP_USER:
173 		return (SIGILL);
174 
175 	default:
176 		return (SIGEMT);
177 	}
178 }
179 
180 /*
181  * Translate the values stored in the db_regs_t struct to the format
182  * understood by gdb. (gdb-5.1.1/gdb/config/sh/tm-sh.h)
183  */
184 void
185 kgdb_getregs(db_regs_t *regs, kgdb_reg_t *gdb_regs)
186 {
187 	uint32_t r;
188 
189 	memset(gdb_regs, 0, KGDB_NUMREGS * sizeof(kgdb_reg_t));
190 	gdb_regs[ 0] = regs->tf_r0;
191 	gdb_regs[ 1] = regs->tf_r1;
192 	gdb_regs[ 2] = regs->tf_r2;
193 	gdb_regs[ 3] = regs->tf_r3;
194 	gdb_regs[ 4] = regs->tf_r4;
195 	gdb_regs[ 5] = regs->tf_r5;
196 	gdb_regs[ 6] = regs->tf_r6;
197 	gdb_regs[ 7] = regs->tf_r7;
198 	gdb_regs[ 8] = regs->tf_r8;
199 	gdb_regs[ 9] = regs->tf_r9;
200 	gdb_regs[10] = regs->tf_r10;
201 	gdb_regs[11] = regs->tf_r11;
202 	gdb_regs[12] = regs->tf_r12;
203 	gdb_regs[13] = regs->tf_r13;
204 	gdb_regs[14] = regs->tf_r14;
205 	gdb_regs[15] = regs->tf_r15;
206 	gdb_regs[16] = regs->tf_spc;
207 	gdb_regs[17] = regs->tf_pr;
208 	__asm volatile("stc vbr, %0" : "=r"(r));
209 	gdb_regs[19] = r;
210 	gdb_regs[20] = regs->tf_mach;
211 	gdb_regs[21] = regs->tf_macl;
212 	gdb_regs[22] = regs->tf_ssr;
213 
214 	/* How treat register bank 1 ? */
215 }
216 
217 /*
218  * Reverse the above.
219  */
220 void
221 kgdb_setregs(db_regs_t *regs, kgdb_reg_t *gdb_regs)
222 {
223 
224 	regs->tf_r0	= gdb_regs[ 0];
225 	regs->tf_r1	= gdb_regs[ 1];
226 	regs->tf_r2	= gdb_regs[ 2];
227 	regs->tf_r3	= gdb_regs[ 3];
228 	regs->tf_r4	= gdb_regs[ 4];
229 	regs->tf_r5	= gdb_regs[ 5];
230 	regs->tf_r6	= gdb_regs[ 6];
231 	regs->tf_r7	= gdb_regs[ 7];
232 	regs->tf_r8	= gdb_regs[ 8];
233 	regs->tf_r9	= gdb_regs[ 9];
234 	regs->tf_r10	= gdb_regs[10];
235 	regs->tf_r11	= gdb_regs[11];
236 	regs->tf_r12	= gdb_regs[12];
237 	regs->tf_r13	= gdb_regs[13];
238 	regs->tf_r14	= gdb_regs[14];
239 	regs->tf_r15	= gdb_regs[15];
240 	regs->tf_spc	= gdb_regs[16];
241 	regs->tf_pr	= gdb_regs[17];
242 	__asm volatile("ldc %0, vbr" :: "r"(gdb_regs[19]));
243 	regs->tf_mach	= gdb_regs[20];
244 	regs->tf_macl	= gdb_regs[21];
245 	regs->tf_ssr	= gdb_regs[22];
246 }
247 
248 /*
249  * Trap into kgdb to wait for debugger to connect,
250  * noting on the console why nothing else is going on.
251  */
252 void
253 kgdb_connect(int verbose)
254 {
255 
256 	if (kgdb_dev == NODEV) {
257 		printf("kgdb_dev=%"PRId64"\n", kgdb_dev);
258 		return;
259 	}
260 
261 	if (verbose)
262 		printf("kgdb waiting...");
263 
264 	__asm volatile("trapa %0" :: "i"(_SH_TRA_BREAK));
265 
266 	if (verbose)
267 		printf("connected.\n");
268 
269 	kgdb_debug_panic = 1;
270 }
271 
272 /*
273  * Decide what to do on panic.
274  * (This is called by panic, like Debugger())
275  */
276 void
277 kgdb_panic(void)
278 {
279 
280 	if (kgdb_dev != NODEV && kgdb_debug_panic) {
281 		printf("entering kgdb\n");
282 		kgdb_connect(kgdb_active == 0);
283 	}
284 }
285