kgdb_machdep.c revision 1.2 1 /* $NetBSD: kgdb_machdep.c,v 1.2 2000/06/29 07:44:04 mrg Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1996 Matthias Pfaller.
42 * All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by Matthias Pfaller.
55 * 4. The name of the author may not be used to endorse or promote products
56 * derived from this software without specific prior written permission
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
62 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
63 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 */
69
70 #if defined(DDB)
71 #error "Can't build DDB and KGDB together."
72 #endif
73
74 /*
75 * Machine-dependent functions for remote KGDB. Originally written
76 * for NetBSD/pc532 by Matthias Pfaller. Modified for NetBSD/i386
77 * by Jason R. Thorpe.
78 */
79
80 #include <sys/param.h>
81 #include <sys/kgdb.h>
82 #include <sys/systm.h>
83
84 #include <uvm/uvm_extern.h>
85
86 #include <machine/pte.h>
87 #include <machine/reg.h>
88 #include <machine/trap.h>
89
90 /* XXX Should be in <machine/pmap.h> */
91 pt_entry_t *pmap_pte __P((pmap_t, vaddr_t));
92
93 /*
94 * Determine if the memory at va..(va+len) is valid.
95 */
96 int
97 kgdb_acc(va, len)
98 vaddr_t va;
99 size_t len;
100 {
101 vaddr_t last_va;
102 pt_entry_t pte;
103
104 last_va = va + len;
105 va &= ~PGOFSET;
106 last_va &= ~PGOFSET;
107
108 do {
109 pte = *(pt_entry_t *)pmap_pte(pmap_kernel(), va);
110 if ((pte & PG_V) == 0)
111 return (0);
112 va += NBPG;
113 } while (va < last_va);
114
115 return (1);
116 }
117
118 /*
119 * Translate a trap number into a unix compatible signal value.
120 * (gdb only understands unix signal numbers).
121 */
122 int
123 kgdb_signal(type)
124 int type;
125 {
126 switch (type) {
127 case T_NMI:
128 return (SIGINT);
129
130 case T_ALIGNFLT:
131 return (SIGILL);
132
133 case T_BPTFLT:
134 case T_TRCTRAP:
135 return (SIGTRAP);
136
137 case T_ASTFLT:
138 case T_DOUBLEFLT:
139 return (SIGEMT);
140
141 case T_ARITHTRAP:
142 case T_DIVIDE:
143 case T_OFLOW:
144 case T_DNA:
145 case T_FPOPFLT:
146 return (SIGFPE);
147
148 case T_PRIVINFLT:
149 case T_PROTFLT:
150 case T_PAGEFLT:
151 case T_TSSFLT:
152 case T_SEGNPFLT:
153 case T_STKFLT:
154 return (SIGSEGV);
155
156 case T_BOUND:
157 return (SIGURG);
158
159 default:
160 return (SIGEMT);
161 }
162 }
163
164 /*
165 * Translate the values stored in the kernel regs struct to the format
166 * understood by gdb.
167 */
168 void
169 kgdb_getregs(regs, gdb_regs)
170 db_regs_t *regs;
171 kgdb_reg_t *gdb_regs;
172 {
173
174 gdb_regs[ 0] = regs->tf_eax;
175 gdb_regs[ 1] = regs->tf_ecx;
176 gdb_regs[ 2] = regs->tf_edx;
177 gdb_regs[ 3] = regs->tf_ebx;
178 gdb_regs[ 5] = regs->tf_ebp;
179 gdb_regs[ 6] = regs->tf_esi;
180 gdb_regs[ 7] = regs->tf_edi;
181 gdb_regs[ 8] = regs->tf_eip;
182 gdb_regs[ 9] = regs->tf_eflags;
183 gdb_regs[10] = regs->tf_cs;
184 gdb_regs[12] = regs->tf_ds;
185 gdb_regs[13] = regs->tf_es;
186
187 if (KERNELMODE(regs->tf_cs, regs->tf_eflags)) {
188 /*
189 * Kernel mode - esp and ss not saved.
190 */
191 gdb_regs[ 4] = (kgdb_reg_t)®s->tf_esp; /* kernel stack
192 pointer */
193 __asm __volatile("movw %%ss,%w0" : "=r" (gdb_regs[11]));
194 }
195 }
196
197 /*
198 * Reverse the above.
199 */
200 void
201 kgdb_setregs(regs, gdb_regs)
202 db_regs_t *regs;
203 kgdb_reg_t *gdb_regs;
204 {
205
206 regs->tf_eax = gdb_regs[ 0];
207 regs->tf_ecx = gdb_regs[ 1];
208 regs->tf_edx = gdb_regs[ 2];
209 regs->tf_ebx = gdb_regs[ 3];
210 regs->tf_ebp = gdb_regs[ 5];
211 regs->tf_esi = gdb_regs[ 6];
212 regs->tf_edi = gdb_regs[ 7];
213 regs->tf_eip = gdb_regs[ 8];
214 regs->tf_eflags = gdb_regs[ 9];
215 regs->tf_cs = gdb_regs[10];
216 regs->tf_ds = gdb_regs[12];
217 regs->tf_es = gdb_regs[13];
218
219 if (KERNELMODE(regs->tf_cs, regs->tf_eflags) == 0) {
220 /*
221 * Trapped in user mode - restore esp and ss.
222 */
223 regs->tf_esp = gdb_regs[ 4];
224 regs->tf_ss = gdb_regs[11];
225 }
226 }
227
228 /*
229 * Trap into kgdb to wait for debugger to connect,
230 * noting on the console why nothing else is going on.
231 */
232 void
233 kgdb_connect(verbose)
234 int verbose;
235 {
236
237 if (kgdb_dev < 0)
238 return;
239
240 if (verbose)
241 printf("kgdb waiting...");
242
243 breakpoint();
244
245 if (verbose)
246 printf("connected.\n");
247
248 kgdb_debug_panic = 1;
249 }
250
251 /*
252 * Decide what to do on panic.
253 * (This is called by panic, like Debugger())
254 */
255 void
256 kgdb_panic()
257 {
258 if (kgdb_dev >= 0 && kgdb_debug_panic) {
259 printf("entering kgdb\n");
260 kgdb_connect(kgdb_active == 0);
261 }
262 }
263