kgdb_machdep.c revision 1.6 1 1.6 perry /* $NetBSD: kgdb_machdep.c,v 1.6 2005/12/24 20:07:37 perry Exp $ */
2 1.1 martin /*-
3 1.1 martin * Copyright (c) 1997 The NetBSD Foundation, Inc.
4 1.1 martin * All rights reserved.
5 1.1 martin *
6 1.1 martin * This code is derived from software contributed to The NetBSD Foundation
7 1.1 martin * by Paul Kranenburg.
8 1.1 martin *
9 1.1 martin * Redistribution and use in source and binary forms, with or without
10 1.1 martin * modification, are permitted provided that the following conditions
11 1.1 martin * are met:
12 1.1 martin * 1. Redistributions of source code must retain the above copyright
13 1.1 martin * notice, this list of conditions and the following disclaimer.
14 1.1 martin * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 martin * notice, this list of conditions and the following disclaimer in the
16 1.1 martin * documentation and/or other materials provided with the distribution.
17 1.1 martin * 3. All advertising materials mentioning features or use of this software
18 1.1 martin * must display the following acknowledgement:
19 1.1 martin * This product includes software developed by the NetBSD
20 1.1 martin * Foundation, Inc. and its contributors.
21 1.1 martin * 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.1 martin * contributors may be used to endorse or promote products derived
23 1.1 martin * from this software without specific prior written permission.
24 1.1 martin *
25 1.1 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.1 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.1 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 martin * POSSIBILITY OF SUCH DAMAGE.
36 1.1 martin */
37 1.1 martin
38 1.1 martin /*
39 1.1 martin * Copyright (c) 1992, 1993
40 1.1 martin * The Regents of the University of California. All rights reserved.
41 1.4 agc *
42 1.4 agc * This software was developed by the Computer Systems Engineering group
43 1.4 agc * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
44 1.4 agc * contributed to Berkeley.
45 1.4 agc *
46 1.4 agc * All advertising materials mentioning features or use of this software
47 1.4 agc * must display the following acknowledgements:
48 1.4 agc * This product includes software developed by the University of
49 1.4 agc * California, Lawrence Berkeley Laboratory.
50 1.4 agc *
51 1.4 agc * This product includes software developed by Harvard University.
52 1.4 agc *
53 1.4 agc * Redistribution and use in source and binary forms, with or without
54 1.4 agc * modification, are permitted provided that the following conditions
55 1.4 agc * are met:
56 1.4 agc * 1. Redistributions of source code must retain the above copyright
57 1.4 agc * notice, this list of conditions and the following disclaimer.
58 1.4 agc * 2. Redistributions in binary form must reproduce the above copyright
59 1.4 agc * notice, this list of conditions and the following disclaimer in the
60 1.4 agc * documentation and/or other materials provided with the distribution.
61 1.4 agc * 3. Neither the name of the University nor the names of its contributors
62 1.4 agc * may be used to endorse or promote products derived from this software
63 1.4 agc * without specific prior written permission.
64 1.4 agc *
65 1.4 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
66 1.4 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 1.4 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 1.4 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
69 1.4 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 1.4 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 1.4 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 1.4 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73 1.4 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74 1.4 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75 1.4 agc * SUCH DAMAGE.
76 1.4 agc *
77 1.4 agc * @(#)kgdb_stub.c 8.1 (Berkeley) 6/11/93
78 1.4 agc */
79 1.4 agc
80 1.4 agc /*
81 1.1 martin * Copyright (c) 1995
82 1.1 martin * The President and Fellows of Harvard College. All rights reserved.
83 1.1 martin *
84 1.1 martin * This software was developed by the Computer Systems Engineering group
85 1.1 martin * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
86 1.1 martin * contributed to Berkeley.
87 1.1 martin *
88 1.1 martin * All advertising materials mentioning features or use of this software
89 1.1 martin * must display the following acknowledgements:
90 1.1 martin * This product includes software developed by the University of
91 1.1 martin * California, Lawrence Berkeley Laboratory.
92 1.1 martin *
93 1.1 martin * This product includes software developed by Harvard University.
94 1.1 martin *
95 1.1 martin * Redistribution and use in source and binary forms, with or without
96 1.1 martin * modification, are permitted provided that the following conditions
97 1.1 martin * are met:
98 1.1 martin * 1. Redistributions of source code must retain the above copyright
99 1.1 martin * notice, this list of conditions and the following disclaimer.
100 1.1 martin * 2. Redistributions in binary form must reproduce the above copyright
101 1.1 martin * notice, this list of conditions and the following disclaimer in the
102 1.1 martin * documentation and/or other materials provided with the distribution.
103 1.1 martin * 3. All advertising materials mentioning features or use of this software
104 1.1 martin * must display the following acknowledgement:
105 1.1 martin * This product includes software developed by the University of
106 1.1 martin * California, Berkeley and its contributors.
107 1.1 martin * 4. Neither the name of the University nor the names of its contributors
108 1.1 martin * may be used to endorse or promote products derived from this software
109 1.1 martin * without specific prior written permission.
110 1.1 martin *
111 1.1 martin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
112 1.1 martin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
113 1.1 martin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
114 1.1 martin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
115 1.1 martin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
116 1.1 martin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
117 1.1 martin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
118 1.1 martin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
119 1.1 martin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
120 1.1 martin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
121 1.1 martin * SUCH DAMAGE.
122 1.1 martin *
123 1.1 martin * @(#)kgdb_stub.c 8.1 (Berkeley) 6/11/93
124 1.1 martin */
125 1.1 martin
126 1.1 martin /*
127 1.1 martin * Machine dependent routines needed by kern/kgdb_stub.c
128 1.1 martin */
129 1.3 lukem
130 1.3 lukem #include <sys/cdefs.h>
131 1.6 perry __KERNEL_RCSID(0, "$NetBSD: kgdb_machdep.c,v 1.6 2005/12/24 20:07:37 perry Exp $");
132 1.1 martin
133 1.1 martin #include "opt_kgdb.h"
134 1.1 martin #include "opt_multiprocessor.h"
135 1.1 martin #include "opt_sparc_arch.h"
136 1.1 martin
137 1.1 martin #ifdef KGDB
138 1.1 martin
139 1.1 martin #include <sys/param.h>
140 1.1 martin #include <sys/systm.h>
141 1.1 martin #include <sys/buf.h>
142 1.1 martin #include <sys/kgdb.h>
143 1.1 martin
144 1.1 martin #include <machine/ctlreg.h>
145 1.1 martin #include <machine/psl.h>
146 1.1 martin #include <machine/reg.h>
147 1.1 martin #include <machine/trap.h>
148 1.1 martin #include <machine/cpu.h>
149 1.1 martin
150 1.1 martin #include <sparc/sparc/asm.h>
151 1.1 martin
152 1.1 martin extern int64_t pseg_get __P((struct pmap *, vaddr_t));
153 1.1 martin
154 1.6 perry static inline void kgdb_copy __P((char *, char *, int));
155 1.6 perry static inline void kgdb_zero __P((char *, int));
156 1.1 martin
157 1.1 martin /*
158 1.1 martin * This little routine exists simply so that bcopy() can be debugged.
159 1.1 martin */
160 1.6 perry static inline void
161 1.1 martin kgdb_copy(src, dst, len)
162 1.1 martin register char *src, *dst;
163 1.1 martin register int len;
164 1.1 martin {
165 1.1 martin
166 1.1 martin while (--len >= 0)
167 1.1 martin *dst++ = *src++;
168 1.1 martin }
169 1.1 martin
170 1.1 martin /* ditto for bzero */
171 1.6 perry static inline void
172 1.1 martin kgdb_zero(ptr, len)
173 1.1 martin register char *ptr;
174 1.1 martin register int len;
175 1.1 martin {
176 1.1 martin while (--len >= 0)
177 1.1 martin *ptr++ = (char) 0;
178 1.1 martin }
179 1.1 martin
180 1.1 martin /*
181 1.1 martin * Deal with KGDB in a MP environment. XXX need to have "mach cpu" equiv.
182 1.1 martin */
183 1.1 martin #ifdef MULTIPROCESSOR
184 1.1 martin
185 1.1 martin #define NOCPU -1
186 1.1 martin
187 1.1 martin static int kgdb_suspend_others(void);
188 1.1 martin static void kgdb_resume_others(void);
189 1.1 martin static void kgdb_suspend(void);
190 1.1 martin
191 1.1 martin __cpu_simple_lock_t kgdb_lock;
192 1.1 martin int kgdb_cpu = NOCPU;
193 1.1 martin
194 1.1 martin static int
195 1.1 martin kgdb_suspend_others(void)
196 1.1 martin {
197 1.1 martin int cpu_me = cpu_number();
198 1.1 martin int win;
199 1.1 martin
200 1.1 martin if (cpus == NULL)
201 1.1 martin return 1;
202 1.1 martin
203 1.1 martin __cpu_simple_lock(&kgdb_lock);
204 1.1 martin if (kgdb_cpu == NOCPU)
205 1.1 martin kgdb_cpu = cpu_me;
206 1.1 martin win = (kgdb_cpu == cpu_me);
207 1.1 martin __cpu_simple_unlock(&kgdb_lock);
208 1.1 martin
209 1.1 martin if (win)
210 1.1 martin mp_pause_cpus();
211 1.1 martin
212 1.1 martin return win;
213 1.1 martin }
214 1.1 martin
215 1.1 martin static void
216 1.1 martin kgdb_resume_others(void)
217 1.1 martin {
218 1.1 martin
219 1.1 martin mp_resume_cpus();
220 1.1 martin
221 1.1 martin __cpu_simple_lock(&kgdb_lock);
222 1.1 martin kgdb_cpu = NOCPU;
223 1.1 martin __cpu_simple_unlock(&kgdb_lock);
224 1.1 martin }
225 1.1 martin
226 1.1 martin static void
227 1.1 martin kgdb_suspend()
228 1.1 martin {
229 1.1 martin
230 1.1 martin while (cpuinfo.flags & CPUFLG_PAUSED)
231 1.1 martin cpuinfo.cache_flush((caddr_t)&cpuinfo.flags, sizeof(cpuinfo.flags));
232 1.1 martin }
233 1.1 martin #endif
234 1.1 martin
235 1.1 martin /*
236 1.1 martin * Trap into kgdb to wait for debugger to connect,
237 1.1 martin * noting on the console why nothing else is going on.
238 1.1 martin */
239 1.1 martin void
240 1.1 martin kgdb_connect(verbose)
241 1.1 martin int verbose;
242 1.1 martin {
243 1.1 martin
244 1.1 martin if (kgdb_dev < 0)
245 1.1 martin return;
246 1.1 martin #if NFB > 0
247 1.1 martin fb_unblank();
248 1.1 martin #endif
249 1.1 martin #ifdef MULTIPROCESSOR
250 1.1 martin /* While we're in the debugger, pause all other CPUs */
251 1.1 martin if (!kgdb_suspend_others()) {
252 1.1 martin kgdb_suspend();
253 1.1 martin } else {
254 1.1 martin #endif
255 1.1 martin if (verbose)
256 1.1 martin printf("kgdb waiting...");
257 1.1 martin __asm("ta %0" :: "n" (T_KGDB_EXEC)); /* trap into kgdb */
258 1.1 martin
259 1.1 martin kgdb_debug_panic = 1;
260 1.1 martin
261 1.1 martin #ifdef MULTIPROCESSOR
262 1.1 martin /* Other CPUs can continue now */
263 1.1 martin kgdb_resume_others();
264 1.1 martin }
265 1.1 martin #endif
266 1.1 martin }
267 1.1 martin
268 1.1 martin /*
269 1.1 martin * Decide what to do on panic.
270 1.1 martin */
271 1.1 martin void
272 1.1 martin kgdb_panic()
273 1.1 martin {
274 1.1 martin
275 1.1 martin if (kgdb_dev >= 0 && kgdb_debug_panic)
276 1.1 martin kgdb_connect(kgdb_active == 0);
277 1.1 martin }
278 1.1 martin
279 1.1 martin /*
280 1.1 martin * Translate a trap number into a unix compatible signal value.
281 1.1 martin * (gdb only understands unix signal numbers).
282 1.1 martin * XXX should this be done at the other end?
283 1.1 martin */
284 1.1 martin int
285 1.1 martin kgdb_signal(type)
286 1.1 martin int type;
287 1.1 martin {
288 1.1 martin int sigval;
289 1.1 martin
290 1.1 martin switch (type) {
291 1.1 martin
292 1.1 martin case T_AST:
293 1.1 martin sigval = SIGINT;
294 1.1 martin break;
295 1.1 martin
296 1.1 martin case T_TEXTFAULT:
297 1.1 martin case T_DATAFAULT:
298 1.1 martin sigval = SIGSEGV;
299 1.1 martin break;
300 1.1 martin
301 1.1 martin case T_ALIGN:
302 1.1 martin sigval = SIGBUS;
303 1.1 martin break;
304 1.1 martin
305 1.1 martin case T_ILLINST:
306 1.1 martin case T_PRIVINST:
307 1.1 martin case T_DIV0:
308 1.1 martin sigval = SIGILL;
309 1.1 martin break;
310 1.1 martin
311 1.1 martin case T_FP_IEEE_754:
312 1.1 martin case T_FP_OTHER:
313 1.1 martin sigval = SIGFPE;
314 1.1 martin break;
315 1.1 martin
316 1.1 martin case T_BREAKPOINT:
317 1.1 martin sigval = SIGTRAP;
318 1.1 martin break;
319 1.1 martin
320 1.1 martin case T_KGDB_EXEC:
321 1.1 martin sigval = SIGIOT;
322 1.1 martin break;
323 1.1 martin
324 1.1 martin default:
325 1.1 martin sigval = SIGEMT;
326 1.1 martin break;
327 1.1 martin }
328 1.1 martin return (sigval);
329 1.1 martin }
330 1.1 martin
331 1.1 martin /*
332 1.1 martin * Definitions exported from gdb (& then made prettier).
333 1.1 martin * (see gnu/dist/toolchain/gdb/config/sparc/tm-sp64.h)
334 1.1 martin */
335 1.1 martin #define GDB_G0 0
336 1.1 martin #define GDB_O0 8
337 1.1 martin #define GDB_L0 16
338 1.1 martin #define GDB_I0 24
339 1.1 martin #define GDB_FP0 32
340 1.1 martin #define GDB_PC 80
341 1.1 martin #define GDB_NPC 81
342 1.1 martin #define GDB_CCR 82
343 1.1 martin #define GDB_FSR 83
344 1.1 martin #define GDB_FPRS 84
345 1.1 martin #define GDB_Y 85
346 1.1 martin #define GDB_ASI 86
347 1.1 martin
348 1.1 martin #define REGISTER_BYTES (KGDB_NUMREGS * 8)
349 1.1 martin #define REGISTER_BYTE(n) ((n) * 8)
350 1.1 martin
351 1.1 martin /*
352 1.1 martin * Translate the values stored in the kernel regs struct to the format
353 1.1 martin * understood by gdb.
354 1.1 martin */
355 1.1 martin void
356 1.1 martin kgdb_getregs(regs, gdb_regs)
357 1.1 martin db_regs_t *regs;
358 1.1 martin kgdb_reg_t *gdb_regs;
359 1.1 martin {
360 1.1 martin struct trapframe64 *tf = ®s->ddb_tf;
361 1.1 martin
362 1.1 martin /* %g0..%g7 and %o0..%o7: from trapframe */
363 1.1 martin gdb_regs[0] = 0;
364 1.1 martin kgdb_copy((caddr_t)&tf->tf_global[1], (caddr_t)&gdb_regs[1], 15 * 8);
365 1.1 martin
366 1.1 martin /* %l0..%l7 and %i0..%i7: from stack */
367 1.2 agc kgdb_copy((caddr_t)(long)tf->tf_out[6], (caddr_t)&gdb_regs[GDB_L0], 16 * 8);
368 1.1 martin
369 1.1 martin /* %f0..%f31 -- fake, kernel does not use FP */
370 1.1 martin kgdb_zero((caddr_t)&gdb_regs[GDB_FP0], 32 * 8);
371 1.1 martin
372 1.1 martin /* %y, %psr, %wim, %tbr, %pc, %npc, %fsr, %csr */
373 1.1 martin gdb_regs[GDB_PC] = tf->tf_pc;
374 1.1 martin gdb_regs[GDB_NPC] = tf->tf_npc;
375 1.1 martin }
376 1.1 martin
377 1.1 martin /*
378 1.1 martin * Reverse the above.
379 1.1 martin */
380 1.1 martin void
381 1.1 martin kgdb_setregs(regs, gdb_regs)
382 1.1 martin db_regs_t *regs;
383 1.1 martin kgdb_reg_t *gdb_regs;
384 1.1 martin {
385 1.1 martin struct trapframe64 *tf = ®s->ddb_tf;
386 1.1 martin
387 1.1 martin kgdb_copy((caddr_t)&gdb_regs[1], (caddr_t)&tf->tf_global[1], 15 * 8);
388 1.2 agc kgdb_copy((caddr_t)&gdb_regs[GDB_L0], (caddr_t)(long)tf->tf_out[6], 16 * 8);
389 1.1 martin tf->tf_pc = gdb_regs[GDB_PC];
390 1.1 martin tf->tf_npc = gdb_regs[GDB_NPC];
391 1.1 martin }
392 1.1 martin
393 1.1 martin /*
394 1.1 martin * Determine if memory at [va..(va+len)] is valid.
395 1.1 martin */
396 1.1 martin int
397 1.1 martin kgdb_acc(va, len)
398 1.1 martin vaddr_t va;
399 1.1 martin size_t len;
400 1.1 martin {
401 1.1 martin int64_t data;
402 1.1 martin vaddr_t eva;
403 1.1 martin struct pmap *pm = &kernel_pmap_;
404 1.1 martin
405 1.1 martin eva = round_page(va + len);
406 1.1 martin va = trunc_page(va);
407 1.1 martin
408 1.1 martin simple_lock(&pm->pm_lock);
409 1.1 martin for (; va < eva; va += PAGE_SIZE) {
410 1.1 martin data = pseg_get(pm, va);
411 1.1 martin if ((data & TLB_V) == 0) {
412 1.1 martin simple_unlock(&pm->pm_lock);
413 1.1 martin return 0;
414 1.1 martin }
415 1.1 martin }
416 1.1 martin simple_unlock(&pm->pm_lock);
417 1.1 martin
418 1.1 martin return (1);
419 1.1 martin }
420 1.1 martin #endif
421