kgdb_machdep.c revision 1.4 1 1.4 uch /* $NetBSD: kgdb_machdep.c,v 1.4 2002/02/12 15:26:50 uch 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.4 uch pt_entry_t *pmap_pte(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.4 uch kgdb_acc(vaddr_t va, size_t len)
101 1.1 itojun {
102 1.1 itojun vaddr_t last_va;
103 1.1 itojun pt_entry_t pte;
104 1.1 itojun
105 1.1 itojun last_va = va + len;
106 1.1 itojun va &= ~PGOFSET;
107 1.1 itojun last_va &= ~PGOFSET;
108 1.1 itojun
109 1.1 itojun do {
110 1.1 itojun pte = *(pt_entry_t *)pmap_pte(pmap_kernel(), va);
111 1.1 itojun if ((pte & PG_V) == 0)
112 1.1 itojun return (0);
113 1.1 itojun va += NBPG;
114 1.1 itojun } while (va < last_va);
115 1.1 itojun
116 1.1 itojun return (1);
117 1.1 itojun }
118 1.1 itojun
119 1.1 itojun /*
120 1.1 itojun * Translate a trap number into a unix compatible signal value.
121 1.1 itojun * (gdb only understands unix signal numbers).
122 1.1 itojun */
123 1.1 itojun int
124 1.4 uch kgdb_signal(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.4 uch kgdb_getregs(db_regs_t *regs, kgdb_reg_t *gdb_regs)
170 1.1 itojun {
171 1.1 itojun
172 1.1 itojun gdb_regs[ 0] = regs->tf_eax;
173 1.1 itojun gdb_regs[ 1] = regs->tf_ecx;
174 1.1 itojun gdb_regs[ 2] = regs->tf_edx;
175 1.1 itojun gdb_regs[ 3] = regs->tf_ebx;
176 1.1 itojun gdb_regs[ 5] = regs->tf_ebp;
177 1.1 itojun gdb_regs[ 6] = regs->tf_esi;
178 1.1 itojun gdb_regs[ 7] = regs->tf_edi;
179 1.1 itojun gdb_regs[ 8] = regs->tf_eip;
180 1.1 itojun gdb_regs[ 9] = regs->tf_eflags;
181 1.1 itojun gdb_regs[10] = regs->tf_cs;
182 1.1 itojun gdb_regs[12] = regs->tf_ds;
183 1.1 itojun gdb_regs[13] = regs->tf_es;
184 1.1 itojun
185 1.1 itojun if (KERNELMODE(regs->tf_cs, regs->tf_eflags)) {
186 1.1 itojun /*
187 1.1 itojun * Kernel mode - esp and ss not saved.
188 1.1 itojun */
189 1.1 itojun gdb_regs[ 4] = (kgdb_reg_t)®s->tf_esp; /* kernel stack
190 1.1 itojun pointer */
191 1.1 itojun __asm __volatile("movw %%ss,%w0" : "=r" (gdb_regs[11]));
192 1.1 itojun }
193 1.1 itojun }
194 1.1 itojun
195 1.1 itojun /*
196 1.1 itojun * Reverse the above.
197 1.1 itojun */
198 1.1 itojun void
199 1.4 uch kgdb_setregs(db_regs_t *regs, kgdb_reg_t *gdb_regs)
200 1.1 itojun {
201 1.1 itojun
202 1.1 itojun regs->tf_eax = gdb_regs[ 0];
203 1.1 itojun regs->tf_ecx = gdb_regs[ 1];
204 1.1 itojun regs->tf_edx = gdb_regs[ 2];
205 1.1 itojun regs->tf_ebx = gdb_regs[ 3];
206 1.1 itojun regs->tf_ebp = gdb_regs[ 5];
207 1.1 itojun regs->tf_esi = gdb_regs[ 6];
208 1.1 itojun regs->tf_edi = gdb_regs[ 7];
209 1.1 itojun regs->tf_eip = gdb_regs[ 8];
210 1.1 itojun regs->tf_eflags = gdb_regs[ 9];
211 1.1 itojun regs->tf_cs = gdb_regs[10];
212 1.1 itojun regs->tf_ds = gdb_regs[12];
213 1.1 itojun regs->tf_es = gdb_regs[13];
214 1.1 itojun
215 1.1 itojun if (KERNELMODE(regs->tf_cs, regs->tf_eflags) == 0) {
216 1.1 itojun /*
217 1.1 itojun * Trapped in user mode - restore esp and ss.
218 1.1 itojun */
219 1.1 itojun regs->tf_esp = gdb_regs[ 4];
220 1.1 itojun regs->tf_ss = gdb_regs[11];
221 1.1 itojun }
222 1.1 itojun }
223 1.1 itojun
224 1.1 itojun /*
225 1.1 itojun * Trap into kgdb to wait for debugger to connect,
226 1.1 itojun * noting on the console why nothing else is going on.
227 1.1 itojun */
228 1.1 itojun void
229 1.4 uch kgdb_connect(int verbose)
230 1.1 itojun {
231 1.1 itojun
232 1.1 itojun if (kgdb_dev < 0)
233 1.1 itojun return;
234 1.1 itojun
235 1.1 itojun if (verbose)
236 1.1 itojun printf("kgdb waiting...");
237 1.1 itojun
238 1.1 itojun breakpoint();
239 1.1 itojun
240 1.1 itojun if (verbose)
241 1.1 itojun printf("connected.\n");
242 1.1 itojun
243 1.1 itojun kgdb_debug_panic = 1;
244 1.1 itojun }
245 1.1 itojun
246 1.1 itojun /*
247 1.1 itojun * Decide what to do on panic.
248 1.1 itojun * (This is called by panic, like Debugger())
249 1.1 itojun */
250 1.1 itojun void
251 1.1 itojun kgdb_panic()
252 1.1 itojun {
253 1.1 itojun if (kgdb_dev >= 0 && kgdb_debug_panic) {
254 1.1 itojun printf("entering kgdb\n");
255 1.1 itojun kgdb_connect(kgdb_active == 0);
256 1.1 itojun }
257 1.1 itojun }
258