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