kgdb_machdep.c revision 1.1 1 1.1 martin /* $NetBSD: kgdb_machdep.c,v 1.1 2003/05/18 22:11:32 martin 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.1 martin
90 1.1 martin #include "opt_kgdb.h"
91 1.1 martin #include "opt_multiprocessor.h"
92 1.1 martin #include "opt_sparc_arch.h"
93 1.1 martin
94 1.1 martin #ifdef KGDB
95 1.1 martin
96 1.1 martin #include <sys/param.h>
97 1.1 martin #include <sys/systm.h>
98 1.1 martin #include <sys/buf.h>
99 1.1 martin #include <sys/kgdb.h>
100 1.1 martin
101 1.1 martin #include <machine/ctlreg.h>
102 1.1 martin #include <machine/psl.h>
103 1.1 martin #include <machine/reg.h>
104 1.1 martin #include <machine/trap.h>
105 1.1 martin #include <machine/cpu.h>
106 1.1 martin
107 1.1 martin #include <sparc/sparc/asm.h>
108 1.1 martin
109 1.1 martin extern int64_t pseg_get __P((struct pmap *, vaddr_t));
110 1.1 martin
111 1.1 martin static __inline void kgdb_copy __P((char *, char *, int));
112 1.1 martin static __inline void kgdb_zero __P((char *, int));
113 1.1 martin
114 1.1 martin /*
115 1.1 martin * This little routine exists simply so that bcopy() can be debugged.
116 1.1 martin */
117 1.1 martin static __inline void
118 1.1 martin kgdb_copy(src, dst, len)
119 1.1 martin register char *src, *dst;
120 1.1 martin register int len;
121 1.1 martin {
122 1.1 martin
123 1.1 martin while (--len >= 0)
124 1.1 martin *dst++ = *src++;
125 1.1 martin }
126 1.1 martin
127 1.1 martin /* ditto for bzero */
128 1.1 martin static __inline void
129 1.1 martin kgdb_zero(ptr, len)
130 1.1 martin register char *ptr;
131 1.1 martin register int len;
132 1.1 martin {
133 1.1 martin while (--len >= 0)
134 1.1 martin *ptr++ = (char) 0;
135 1.1 martin }
136 1.1 martin
137 1.1 martin /*
138 1.1 martin * Deal with KGDB in a MP environment. XXX need to have "mach cpu" equiv.
139 1.1 martin */
140 1.1 martin #ifdef MULTIPROCESSOR
141 1.1 martin
142 1.1 martin #define NOCPU -1
143 1.1 martin
144 1.1 martin static int kgdb_suspend_others(void);
145 1.1 martin static void kgdb_resume_others(void);
146 1.1 martin static void kgdb_suspend(void);
147 1.1 martin
148 1.1 martin __cpu_simple_lock_t kgdb_lock;
149 1.1 martin int kgdb_cpu = NOCPU;
150 1.1 martin
151 1.1 martin static int
152 1.1 martin kgdb_suspend_others(void)
153 1.1 martin {
154 1.1 martin int cpu_me = cpu_number();
155 1.1 martin int win;
156 1.1 martin
157 1.1 martin if (cpus == NULL)
158 1.1 martin return 1;
159 1.1 martin
160 1.1 martin __cpu_simple_lock(&kgdb_lock);
161 1.1 martin if (kgdb_cpu == NOCPU)
162 1.1 martin kgdb_cpu = cpu_me;
163 1.1 martin win = (kgdb_cpu == cpu_me);
164 1.1 martin __cpu_simple_unlock(&kgdb_lock);
165 1.1 martin
166 1.1 martin if (win)
167 1.1 martin mp_pause_cpus();
168 1.1 martin
169 1.1 martin return win;
170 1.1 martin }
171 1.1 martin
172 1.1 martin static void
173 1.1 martin kgdb_resume_others(void)
174 1.1 martin {
175 1.1 martin
176 1.1 martin mp_resume_cpus();
177 1.1 martin
178 1.1 martin __cpu_simple_lock(&kgdb_lock);
179 1.1 martin kgdb_cpu = NOCPU;
180 1.1 martin __cpu_simple_unlock(&kgdb_lock);
181 1.1 martin }
182 1.1 martin
183 1.1 martin static void
184 1.1 martin kgdb_suspend()
185 1.1 martin {
186 1.1 martin
187 1.1 martin while (cpuinfo.flags & CPUFLG_PAUSED)
188 1.1 martin cpuinfo.cache_flush((caddr_t)&cpuinfo.flags, sizeof(cpuinfo.flags));
189 1.1 martin }
190 1.1 martin #endif
191 1.1 martin
192 1.1 martin /*
193 1.1 martin * Trap into kgdb to wait for debugger to connect,
194 1.1 martin * noting on the console why nothing else is going on.
195 1.1 martin */
196 1.1 martin void
197 1.1 martin kgdb_connect(verbose)
198 1.1 martin int verbose;
199 1.1 martin {
200 1.1 martin
201 1.1 martin if (kgdb_dev < 0)
202 1.1 martin return;
203 1.1 martin #if NFB > 0
204 1.1 martin fb_unblank();
205 1.1 martin #endif
206 1.1 martin #ifdef MULTIPROCESSOR
207 1.1 martin /* While we're in the debugger, pause all other CPUs */
208 1.1 martin if (!kgdb_suspend_others()) {
209 1.1 martin kgdb_suspend();
210 1.1 martin } else {
211 1.1 martin #endif
212 1.1 martin if (verbose)
213 1.1 martin printf("kgdb waiting...");
214 1.1 martin __asm("ta %0" :: "n" (T_KGDB_EXEC)); /* trap into kgdb */
215 1.1 martin
216 1.1 martin kgdb_debug_panic = 1;
217 1.1 martin
218 1.1 martin #ifdef MULTIPROCESSOR
219 1.1 martin /* Other CPUs can continue now */
220 1.1 martin kgdb_resume_others();
221 1.1 martin }
222 1.1 martin #endif
223 1.1 martin }
224 1.1 martin
225 1.1 martin /*
226 1.1 martin * Decide what to do on panic.
227 1.1 martin */
228 1.1 martin void
229 1.1 martin kgdb_panic()
230 1.1 martin {
231 1.1 martin
232 1.1 martin if (kgdb_dev >= 0 && kgdb_debug_panic)
233 1.1 martin kgdb_connect(kgdb_active == 0);
234 1.1 martin }
235 1.1 martin
236 1.1 martin /*
237 1.1 martin * Translate a trap number into a unix compatible signal value.
238 1.1 martin * (gdb only understands unix signal numbers).
239 1.1 martin * XXX should this be done at the other end?
240 1.1 martin */
241 1.1 martin int
242 1.1 martin kgdb_signal(type)
243 1.1 martin int type;
244 1.1 martin {
245 1.1 martin int sigval;
246 1.1 martin
247 1.1 martin switch (type) {
248 1.1 martin
249 1.1 martin case T_AST:
250 1.1 martin sigval = SIGINT;
251 1.1 martin break;
252 1.1 martin
253 1.1 martin case T_TEXTFAULT:
254 1.1 martin case T_DATAFAULT:
255 1.1 martin sigval = SIGSEGV;
256 1.1 martin break;
257 1.1 martin
258 1.1 martin case T_ALIGN:
259 1.1 martin sigval = SIGBUS;
260 1.1 martin break;
261 1.1 martin
262 1.1 martin case T_ILLINST:
263 1.1 martin case T_PRIVINST:
264 1.1 martin case T_DIV0:
265 1.1 martin sigval = SIGILL;
266 1.1 martin break;
267 1.1 martin
268 1.1 martin case T_FP_IEEE_754:
269 1.1 martin case T_FP_OTHER:
270 1.1 martin sigval = SIGFPE;
271 1.1 martin break;
272 1.1 martin
273 1.1 martin case T_BREAKPOINT:
274 1.1 martin sigval = SIGTRAP;
275 1.1 martin break;
276 1.1 martin
277 1.1 martin case T_KGDB_EXEC:
278 1.1 martin sigval = SIGIOT;
279 1.1 martin break;
280 1.1 martin
281 1.1 martin default:
282 1.1 martin sigval = SIGEMT;
283 1.1 martin break;
284 1.1 martin }
285 1.1 martin return (sigval);
286 1.1 martin }
287 1.1 martin
288 1.1 martin /*
289 1.1 martin * Definitions exported from gdb (& then made prettier).
290 1.1 martin * (see gnu/dist/toolchain/gdb/config/sparc/tm-sp64.h)
291 1.1 martin */
292 1.1 martin #define GDB_G0 0
293 1.1 martin #define GDB_O0 8
294 1.1 martin #define GDB_L0 16
295 1.1 martin #define GDB_I0 24
296 1.1 martin #define GDB_FP0 32
297 1.1 martin #define GDB_PC 80
298 1.1 martin #define GDB_NPC 81
299 1.1 martin #define GDB_CCR 82
300 1.1 martin #define GDB_FSR 83
301 1.1 martin #define GDB_FPRS 84
302 1.1 martin #define GDB_Y 85
303 1.1 martin #define GDB_ASI 86
304 1.1 martin
305 1.1 martin #define REGISTER_BYTES (KGDB_NUMREGS * 8)
306 1.1 martin #define REGISTER_BYTE(n) ((n) * 8)
307 1.1 martin
308 1.1 martin /*
309 1.1 martin * Translate the values stored in the kernel regs struct to the format
310 1.1 martin * understood by gdb.
311 1.1 martin */
312 1.1 martin void
313 1.1 martin kgdb_getregs(regs, gdb_regs)
314 1.1 martin db_regs_t *regs;
315 1.1 martin kgdb_reg_t *gdb_regs;
316 1.1 martin {
317 1.1 martin struct trapframe64 *tf = ®s->ddb_tf;
318 1.1 martin
319 1.1 martin /* %g0..%g7 and %o0..%o7: from trapframe */
320 1.1 martin gdb_regs[0] = 0;
321 1.1 martin kgdb_copy((caddr_t)&tf->tf_global[1], (caddr_t)&gdb_regs[1], 15 * 8);
322 1.1 martin
323 1.1 martin /* %l0..%l7 and %i0..%i7: from stack */
324 1.1 martin kgdb_copy((caddr_t)tf->tf_out[6], (caddr_t)&gdb_regs[GDB_L0], 16 * 8);
325 1.1 martin
326 1.1 martin /* %f0..%f31 -- fake, kernel does not use FP */
327 1.1 martin kgdb_zero((caddr_t)&gdb_regs[GDB_FP0], 32 * 8);
328 1.1 martin
329 1.1 martin /* %y, %psr, %wim, %tbr, %pc, %npc, %fsr, %csr */
330 1.1 martin gdb_regs[GDB_PC] = tf->tf_pc;
331 1.1 martin gdb_regs[GDB_NPC] = tf->tf_npc;
332 1.1 martin }
333 1.1 martin
334 1.1 martin /*
335 1.1 martin * Reverse the above.
336 1.1 martin */
337 1.1 martin void
338 1.1 martin kgdb_setregs(regs, gdb_regs)
339 1.1 martin db_regs_t *regs;
340 1.1 martin kgdb_reg_t *gdb_regs;
341 1.1 martin {
342 1.1 martin struct trapframe64 *tf = ®s->ddb_tf;
343 1.1 martin
344 1.1 martin kgdb_copy((caddr_t)&gdb_regs[1], (caddr_t)&tf->tf_global[1], 15 * 8);
345 1.1 martin kgdb_copy((caddr_t)&gdb_regs[GDB_L0], (caddr_t)tf->tf_out[6], 16 * 8);
346 1.1 martin tf->tf_pc = gdb_regs[GDB_PC];
347 1.1 martin tf->tf_npc = gdb_regs[GDB_NPC];
348 1.1 martin }
349 1.1 martin
350 1.1 martin /*
351 1.1 martin * Determine if memory at [va..(va+len)] is valid.
352 1.1 martin */
353 1.1 martin int
354 1.1 martin kgdb_acc(va, len)
355 1.1 martin vaddr_t va;
356 1.1 martin size_t len;
357 1.1 martin {
358 1.1 martin int64_t data;
359 1.1 martin vaddr_t eva;
360 1.1 martin struct pmap *pm = &kernel_pmap_;
361 1.1 martin
362 1.1 martin eva = round_page(va + len);
363 1.1 martin va = trunc_page(va);
364 1.1 martin
365 1.1 martin simple_lock(&pm->pm_lock);
366 1.1 martin for (; va < eva; va += PAGE_SIZE) {
367 1.1 martin data = pseg_get(pm, va);
368 1.1 martin if ((data & TLB_V) == 0) {
369 1.1 martin simple_unlock(&pm->pm_lock);
370 1.1 martin return 0;
371 1.1 martin }
372 1.1 martin }
373 1.1 martin simple_unlock(&pm->pm_lock);
374 1.1 martin
375 1.1 martin return (1);
376 1.1 martin }
377 1.1 martin #endif
378