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