kgdb_machdep.c revision 1.3 1 1.3 lukem /* $NetBSD: kgdb_machdep.c,v 1.3 2001/11/20 08:43:34 lukem Exp $ */
2 1.1 itojun
3 1.1 itojun /*-
4 1.1 itojun * Copyright (c) 1997 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.3 lukem
70 1.3 lukem #include "opt_ddb.h"
71 1.3 lukem #include "opt_kgdb.h"
72 1.1 itojun
73 1.1 itojun #if defined(DDB)
74 1.1 itojun #error "Can't build DDB and KGDB together."
75 1.1 itojun #endif
76 1.1 itojun
77 1.1 itojun /*
78 1.1 itojun * Machine-dependent functions for remote KGDB. Originally written
79 1.1 itojun * for NetBSD/pc532 by Matthias Pfaller. Modified for NetBSD/i386
80 1.1 itojun * by Jason R. Thorpe.
81 1.1 itojun */
82 1.1 itojun
83 1.1 itojun #include <sys/param.h>
84 1.1 itojun #include <sys/kgdb.h>
85 1.1 itojun #include <sys/systm.h>
86 1.1 itojun
87 1.2 mrg #include <uvm/uvm_extern.h>
88 1.1 itojun
89 1.1 itojun #include <machine/pte.h>
90 1.1 itojun #include <machine/reg.h>
91 1.1 itojun #include <machine/trap.h>
92 1.1 itojun
93 1.1 itojun /* XXX Should be in <machine/pmap.h> */
94 1.1 itojun pt_entry_t *pmap_pte __P((pmap_t, vaddr_t));
95 1.1 itojun
96 1.1 itojun /*
97 1.1 itojun * Determine if the memory at va..(va+len) is valid.
98 1.1 itojun */
99 1.1 itojun int
100 1.1 itojun kgdb_acc(va, len)
101 1.1 itojun vaddr_t va;
102 1.1 itojun size_t len;
103 1.1 itojun {
104 1.1 itojun vaddr_t last_va;
105 1.1 itojun pt_entry_t pte;
106 1.1 itojun
107 1.1 itojun last_va = va + len;
108 1.1 itojun va &= ~PGOFSET;
109 1.1 itojun last_va &= ~PGOFSET;
110 1.1 itojun
111 1.1 itojun do {
112 1.1 itojun pte = *(pt_entry_t *)pmap_pte(pmap_kernel(), va);
113 1.1 itojun if ((pte & PG_V) == 0)
114 1.1 itojun return (0);
115 1.1 itojun va += NBPG;
116 1.1 itojun } while (va < last_va);
117 1.1 itojun
118 1.1 itojun return (1);
119 1.1 itojun }
120 1.1 itojun
121 1.1 itojun /*
122 1.1 itojun * Translate a trap number into a unix compatible signal value.
123 1.1 itojun * (gdb only understands unix signal numbers).
124 1.1 itojun */
125 1.1 itojun int
126 1.1 itojun kgdb_signal(type)
127 1.1 itojun int type;
128 1.1 itojun {
129 1.1 itojun switch (type) {
130 1.1 itojun case T_NMI:
131 1.1 itojun return (SIGINT);
132 1.1 itojun
133 1.1 itojun case T_ALIGNFLT:
134 1.1 itojun return (SIGILL);
135 1.1 itojun
136 1.1 itojun case T_BPTFLT:
137 1.1 itojun case T_TRCTRAP:
138 1.1 itojun return (SIGTRAP);
139 1.1 itojun
140 1.1 itojun case T_ASTFLT:
141 1.1 itojun case T_DOUBLEFLT:
142 1.1 itojun return (SIGEMT);
143 1.1 itojun
144 1.1 itojun case T_ARITHTRAP:
145 1.1 itojun case T_DIVIDE:
146 1.1 itojun case T_OFLOW:
147 1.1 itojun case T_DNA:
148 1.1 itojun case T_FPOPFLT:
149 1.1 itojun return (SIGFPE);
150 1.1 itojun
151 1.1 itojun case T_PRIVINFLT:
152 1.1 itojun case T_PROTFLT:
153 1.1 itojun case T_PAGEFLT:
154 1.1 itojun case T_TSSFLT:
155 1.1 itojun case T_SEGNPFLT:
156 1.1 itojun case T_STKFLT:
157 1.1 itojun return (SIGSEGV);
158 1.1 itojun
159 1.1 itojun case T_BOUND:
160 1.1 itojun return (SIGURG);
161 1.1 itojun
162 1.1 itojun default:
163 1.1 itojun return (SIGEMT);
164 1.1 itojun }
165 1.1 itojun }
166 1.1 itojun
167 1.1 itojun /*
168 1.1 itojun * Translate the values stored in the kernel regs struct to the format
169 1.1 itojun * understood by gdb.
170 1.1 itojun */
171 1.1 itojun void
172 1.1 itojun kgdb_getregs(regs, gdb_regs)
173 1.1 itojun db_regs_t *regs;
174 1.1 itojun kgdb_reg_t *gdb_regs;
175 1.1 itojun {
176 1.1 itojun
177 1.1 itojun gdb_regs[ 0] = regs->tf_eax;
178 1.1 itojun gdb_regs[ 1] = regs->tf_ecx;
179 1.1 itojun gdb_regs[ 2] = regs->tf_edx;
180 1.1 itojun gdb_regs[ 3] = regs->tf_ebx;
181 1.1 itojun gdb_regs[ 5] = regs->tf_ebp;
182 1.1 itojun gdb_regs[ 6] = regs->tf_esi;
183 1.1 itojun gdb_regs[ 7] = regs->tf_edi;
184 1.1 itojun gdb_regs[ 8] = regs->tf_eip;
185 1.1 itojun gdb_regs[ 9] = regs->tf_eflags;
186 1.1 itojun gdb_regs[10] = regs->tf_cs;
187 1.1 itojun gdb_regs[12] = regs->tf_ds;
188 1.1 itojun gdb_regs[13] = regs->tf_es;
189 1.1 itojun
190 1.1 itojun if (KERNELMODE(regs->tf_cs, regs->tf_eflags)) {
191 1.1 itojun /*
192 1.1 itojun * Kernel mode - esp and ss not saved.
193 1.1 itojun */
194 1.1 itojun gdb_regs[ 4] = (kgdb_reg_t)®s->tf_esp; /* kernel stack
195 1.1 itojun pointer */
196 1.1 itojun __asm __volatile("movw %%ss,%w0" : "=r" (gdb_regs[11]));
197 1.1 itojun }
198 1.1 itojun }
199 1.1 itojun
200 1.1 itojun /*
201 1.1 itojun * Reverse the above.
202 1.1 itojun */
203 1.1 itojun void
204 1.1 itojun kgdb_setregs(regs, gdb_regs)
205 1.1 itojun db_regs_t *regs;
206 1.1 itojun kgdb_reg_t *gdb_regs;
207 1.1 itojun {
208 1.1 itojun
209 1.1 itojun regs->tf_eax = gdb_regs[ 0];
210 1.1 itojun regs->tf_ecx = gdb_regs[ 1];
211 1.1 itojun regs->tf_edx = gdb_regs[ 2];
212 1.1 itojun regs->tf_ebx = gdb_regs[ 3];
213 1.1 itojun regs->tf_ebp = gdb_regs[ 5];
214 1.1 itojun regs->tf_esi = gdb_regs[ 6];
215 1.1 itojun regs->tf_edi = gdb_regs[ 7];
216 1.1 itojun regs->tf_eip = gdb_regs[ 8];
217 1.1 itojun regs->tf_eflags = gdb_regs[ 9];
218 1.1 itojun regs->tf_cs = gdb_regs[10];
219 1.1 itojun regs->tf_ds = gdb_regs[12];
220 1.1 itojun regs->tf_es = gdb_regs[13];
221 1.1 itojun
222 1.1 itojun if (KERNELMODE(regs->tf_cs, regs->tf_eflags) == 0) {
223 1.1 itojun /*
224 1.1 itojun * Trapped in user mode - restore esp and ss.
225 1.1 itojun */
226 1.1 itojun regs->tf_esp = gdb_regs[ 4];
227 1.1 itojun regs->tf_ss = gdb_regs[11];
228 1.1 itojun }
229 1.1 itojun }
230 1.1 itojun
231 1.1 itojun /*
232 1.1 itojun * Trap into kgdb to wait for debugger to connect,
233 1.1 itojun * noting on the console why nothing else is going on.
234 1.1 itojun */
235 1.1 itojun void
236 1.1 itojun kgdb_connect(verbose)
237 1.1 itojun int verbose;
238 1.1 itojun {
239 1.1 itojun
240 1.1 itojun if (kgdb_dev < 0)
241 1.1 itojun return;
242 1.1 itojun
243 1.1 itojun if (verbose)
244 1.1 itojun printf("kgdb waiting...");
245 1.1 itojun
246 1.1 itojun breakpoint();
247 1.1 itojun
248 1.1 itojun if (verbose)
249 1.1 itojun printf("connected.\n");
250 1.1 itojun
251 1.1 itojun kgdb_debug_panic = 1;
252 1.1 itojun }
253 1.1 itojun
254 1.1 itojun /*
255 1.1 itojun * Decide what to do on panic.
256 1.1 itojun * (This is called by panic, like Debugger())
257 1.1 itojun */
258 1.1 itojun void
259 1.1 itojun kgdb_panic()
260 1.1 itojun {
261 1.1 itojun if (kgdb_dev >= 0 && kgdb_debug_panic) {
262 1.1 itojun printf("entering kgdb\n");
263 1.1 itojun kgdb_connect(kgdb_active == 0);
264 1.1 itojun }
265 1.1 itojun }
266