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