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