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