Commit | Line | Data |
---|---|---|
ebd99d54 | 1 | /* Intel 386 native support for SYSV systems (pre-SVR4). |
6d40175d | 2 | Copyright (C) 1988, 1989, 1991, 1992, 1994 Free Software Foundation, Inc. |
c7c94073 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
6c9638b4 | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
c7c94073 RP |
19 | |
20 | #include "defs.h" | |
21 | #include "frame.h" | |
22 | #include "inferior.h" | |
23 | #include "language.h" | |
24 | #include "gdbcore.h" | |
25 | ||
26 | #ifdef USG | |
27 | #include <sys/types.h> | |
28 | #endif | |
29 | ||
30 | #include <sys/param.h> | |
31 | #include <sys/dir.h> | |
32 | #include <signal.h> | |
33 | #include <sys/user.h> | |
34 | #include <sys/ioctl.h> | |
35 | #include <fcntl.h> | |
36 | ||
f5a8f1a6 | 37 | #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS |
6d40175d SS |
38 | #include <sys/debugreg.h> |
39 | #endif | |
40 | ||
c7c94073 | 41 | #include <sys/file.h> |
2b576293 | 42 | #include "gdb_stat.h" |
c7c94073 | 43 | |
ebd99d54 | 44 | #ifndef NO_SYS_REG_H |
c7c94073 | 45 | #include <sys/reg.h> |
ebd99d54 FF |
46 | #endif |
47 | ||
eae3f093 | 48 | #include "floatformat.h" |
c7c94073 RP |
49 | |
50 | #include "target.h" | |
51 | ||
c7c94073 RP |
52 | \f |
53 | /* this table must line up with REGISTER_NAMES in tm-i386v.h */ | |
54 | /* symbols like 'EAX' come from <sys/reg.h> */ | |
55 | static int regmap[] = | |
56 | { | |
57 | EAX, ECX, EDX, EBX, | |
58 | UESP, EBP, ESI, EDI, | |
59 | EIP, EFL, CS, SS, | |
60 | DS, ES, FS, GS, | |
61 | }; | |
62 | ||
63 | /* blockend is the value of u.u_ar0, and points to the | |
64 | * place where GS is stored | |
65 | */ | |
66 | ||
67 | int | |
68 | i386_register_u_addr (blockend, regnum) | |
69 | int blockend; | |
70 | int regnum; | |
71 | { | |
183e1f0d FF |
72 | struct user u; |
73 | int fpstate; | |
74 | int ubase; | |
75 | ||
76 | ubase = blockend; | |
77 | /* FIXME: Should have better way to test floating point range */ | |
78 | if (regnum >= FP0_REGNUM && regnum <= (FP0_REGNUM + 7)) | |
c7c94073 | 79 | { |
183e1f0d FF |
80 | #ifdef KSTKSZ /* SCO, and others? */ |
81 | ubase += 4 * (SS + 1) - KSTKSZ; | |
82 | fpstate = ubase + ((char *)&u.u_fps.u_fpstate - (char *)&u); | |
c7c94073 | 83 | return (fpstate + 0x1c + 10 * (regnum - FP0_REGNUM)); |
183e1f0d FF |
84 | #else |
85 | fpstate = ubase + ((char *)&u.i387.st_space - (char *)&u); | |
86 | return (fpstate + 10 * (regnum - FP0_REGNUM)); | |
87 | #endif | |
c7c94073 RP |
88 | } |
89 | else | |
183e1f0d FF |
90 | { |
91 | return (ubase + 4 * regmap[regnum]); | |
92 | } | |
c7c94073 RP |
93 | |
94 | } | |
6d40175d | 95 | \f |
f5a8f1a6 | 96 | #ifdef TARGET_HAS_HARDWARE_WATCHPOINTS |
6d40175d SS |
97 | |
98 | #if !defined (offsetof) | |
99 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) | |
100 | #endif | |
101 | ||
102 | /* Record the value of the debug control register. */ | |
103 | static int debug_control_mirror; | |
104 | ||
105 | /* Record which address associates with which register. */ | |
106 | static CORE_ADDR address_lookup[DR_LASTADDR - DR_FIRSTADDR + 1]; | |
107 | ||
108 | static int | |
95618211 | 109 | i386_insert_aligned_watchpoint PARAMS ((int, CORE_ADDR, CORE_ADDR, int, |
04dd69e1 FF |
110 | int)); |
111 | ||
112 | static int | |
95618211 SS |
113 | i386_insert_nonaligned_watchpoint PARAMS ((int, CORE_ADDR, CORE_ADDR, int, |
114 | int)); | |
6d40175d SS |
115 | |
116 | /* Insert a watchpoint. */ | |
117 | ||
118 | int | |
119 | i386_insert_watchpoint (pid, addr, len, rw) | |
120 | int pid; | |
121 | CORE_ADDR addr; | |
122 | int len; | |
123 | int rw; | |
04dd69e1 FF |
124 | { |
125 | return i386_insert_aligned_watchpoint (pid, addr, addr, len, rw); | |
126 | } | |
127 | ||
128 | static int | |
129 | i386_insert_aligned_watchpoint (pid, waddr, addr, len, rw) | |
130 | int pid; | |
131 | CORE_ADDR waddr; | |
132 | CORE_ADDR addr; | |
133 | int len; | |
134 | int rw; | |
6d40175d SS |
135 | { |
136 | int i; | |
137 | int read_write_bits, len_bits; | |
138 | int free_debug_register; | |
139 | int register_number; | |
140 | ||
141 | /* Look for a free debug register. */ | |
142 | for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++) | |
143 | { | |
144 | if (address_lookup[i - DR_FIRSTADDR] == 0) | |
145 | break; | |
146 | } | |
147 | ||
148 | /* No more debug registers! */ | |
149 | if (i > DR_LASTADDR) | |
150 | return -1; | |
151 | ||
152 | read_write_bits = ((rw & 1) ? DR_RW_READ : 0) | ((rw & 2) ? DR_RW_WRITE : 0); | |
153 | ||
154 | if (len == 1) | |
155 | len_bits = DR_LEN_1; | |
156 | else if (len == 2) | |
157 | { | |
158 | if (addr % 2) | |
04dd69e1 | 159 | return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw); |
6d40175d SS |
160 | len_bits = DR_LEN_2; |
161 | } | |
162 | ||
163 | else if (len == 4) | |
164 | { | |
165 | if (addr % 4) | |
04dd69e1 | 166 | return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw); |
6d40175d SS |
167 | len_bits = DR_LEN_4; |
168 | } | |
169 | else | |
04dd69e1 | 170 | return i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw); |
6d40175d SS |
171 | |
172 | free_debug_register = i; | |
173 | register_number = free_debug_register - DR_FIRSTADDR; | |
174 | debug_control_mirror |= | |
175 | ((read_write_bits | len_bits) | |
176 | << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * register_number)); | |
177 | debug_control_mirror |= | |
178 | (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number)); | |
179 | debug_control_mirror |= DR_LOCAL_SLOWDOWN; | |
180 | debug_control_mirror &= ~DR_CONTROL_RESERVED; | |
181 | ||
182 | ptrace (6, pid, offsetof (struct user, u_debugreg[DR_CONTROL]), | |
183 | debug_control_mirror); | |
184 | ptrace (6, pid, offsetof (struct user, u_debugreg[free_debug_register]), | |
185 | addr); | |
186 | ||
187 | /* Record where we came from. */ | |
188 | address_lookup[register_number] = addr; | |
189 | return 0; | |
190 | } | |
191 | ||
192 | static int | |
04dd69e1 FF |
193 | i386_insert_nonaligned_watchpoint (pid, waddr, addr, len, rw) |
194 | int pid; | |
195 | CORE_ADDR waddr; | |
196 | CORE_ADDR addr; | |
197 | int len; | |
198 | int rw; | |
6d40175d SS |
199 | { |
200 | int align; | |
201 | int size; | |
202 | int rv; | |
203 | ||
204 | static int size_try_array[16] = { | |
205 | 1, 1, 1, 1, /* trying size one */ | |
206 | 2, 1, 2, 1, /* trying size two */ | |
207 | 2, 1, 2, 1, /* trying size three */ | |
208 | 4, 1, 2, 1 /* trying size four */ | |
209 | }; | |
210 | ||
211 | rv = 0; | |
212 | while (len > 0) | |
213 | { | |
214 | align = addr % 4; | |
215 | /* Four is the maximum length for 386. */ | |
216 | size = (len > 4) ? 3 : len - 1; | |
217 | size = size_try_array[size * 4 + align]; | |
218 | ||
95618211 | 219 | rv = i386_insert_aligned_watchpoint (pid, waddr, addr, size, rw); |
6d40175d SS |
220 | if (rv) |
221 | { | |
04dd69e1 | 222 | i386_remove_watchpoint (pid, waddr, size); |
6d40175d SS |
223 | return rv; |
224 | } | |
225 | addr += size; | |
226 | len -= size; | |
227 | } | |
228 | return rv; | |
229 | } | |
230 | ||
231 | /* Remove a watchpoint. */ | |
232 | ||
233 | int | |
234 | i386_remove_watchpoint (pid, addr, len) | |
235 | int pid; | |
236 | CORE_ADDR addr; | |
237 | int len; | |
238 | { | |
239 | int i; | |
240 | int register_number; | |
241 | ||
242 | for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++) | |
243 | { | |
244 | register_number = i - DR_FIRSTADDR; | |
245 | if (address_lookup[register_number] == addr) | |
246 | { | |
247 | debug_control_mirror &= | |
248 | ~(1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * register_number)); | |
249 | address_lookup[register_number] = 0; | |
250 | } | |
251 | } | |
252 | ptrace (6, pid, offsetof (struct user, u_debugreg[DR_CONTROL]), | |
253 | debug_control_mirror); | |
254 | ptrace (6, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0); | |
255 | ||
256 | return 0; | |
257 | } | |
258 | ||
259 | /* Check if stopped by a watchpoint. */ | |
260 | ||
261 | CORE_ADDR | |
262 | i386_stopped_by_watchpoint (pid) | |
263 | int pid; | |
264 | { | |
265 | int i; | |
266 | int status; | |
267 | ||
268 | status = ptrace (3, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0); | |
269 | ptrace (6, pid, offsetof (struct user, u_debugreg[DR_STATUS]), 0); | |
270 | ||
271 | for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++) | |
272 | { | |
273 | if (status & (1 << (i - DR_FIRSTADDR))) | |
274 | return address_lookup[i - DR_FIRSTADDR]; | |
275 | } | |
276 | ||
277 | return 0; | |
278 | } | |
279 | ||
f5a8f1a6 | 280 | #endif /* TARGET_HAS_HARDWARE_WATCHPOINTS */ |
c7c94073 RP |
281 | |
282 | #if 0 | |
283 | /* using FLOAT_INFO as is would be a problem. FLOAT_INFO is called | |
284 | via a command xxx and eventually calls ptrace without ever having | |
285 | traversed the target vector. This would be terribly impolite | |
286 | behaviour for a sun4 hosted remote gdb. | |
287 | ||
288 | A fix might be to move this code into the "info registers" command. | |
289 | rich@cygnus.com 15 Sept 92. */ | |
290 | i386_float_info () | |
291 | { | |
292 | struct user u; /* just for address computations */ | |
293 | int i; | |
294 | /* fpstate defined in <sys/user.h> */ | |
295 | struct fpstate *fpstatep; | |
296 | char buf[sizeof (struct fpstate) + 2 * sizeof (int)]; | |
297 | unsigned int uaddr; | |
298 | char fpvalid = 0; | |
299 | unsigned int rounded_addr; | |
300 | unsigned int rounded_size; | |
301 | extern int corechan; | |
302 | int skip; | |
303 | ||
304 | uaddr = (char *)&u.u_fpvalid - (char *)&u; | |
305 | if (target_has_execution) | |
306 | { | |
307 | unsigned int data; | |
308 | unsigned int mask; | |
309 | ||
310 | rounded_addr = uaddr & -sizeof (int); | |
311 | data = ptrace (3, inferior_pid, (PTRACE_ARG3_TYPE) rounded_addr, 0); | |
312 | mask = 0xff << ((uaddr - rounded_addr) * 8); | |
313 | ||
314 | fpvalid = ((data & mask) != 0); | |
315 | } | |
316 | #if 0 | |
317 | else | |
318 | { | |
319 | if (lseek (corechan, uaddr, 0) < 0) | |
320 | perror ("seek on core file"); | |
321 | if (myread (corechan, &fpvalid, 1) < 0) | |
322 | perror ("read on core file"); | |
323 | ||
324 | } | |
325 | #endif /* no core support yet */ | |
326 | ||
327 | if (fpvalid == 0) | |
328 | { | |
199b2450 | 329 | printf_unfiltered ("no floating point status saved\n"); |
c7c94073 RP |
330 | return; |
331 | } | |
332 | ||
333 | uaddr = (char *)&U_FPSTATE(u) - (char *)&u; | |
334 | if (target_has_execution) | |
335 | { | |
336 | int *ip; | |
337 | ||
338 | rounded_addr = uaddr & -sizeof (int); | |
339 | rounded_size = (((uaddr + sizeof (struct fpstate)) - uaddr) + | |
340 | sizeof (int) - 1) / sizeof (int); | |
341 | skip = uaddr - rounded_addr; | |
342 | ||
343 | ip = (int *)buf; | |
344 | for (i = 0; i < rounded_size; i++) | |
345 | { | |
346 | *ip++ = ptrace (3, inferior_pid, (PTRACE_ARG3_TYPE) rounded_addr, 0); | |
347 | rounded_addr += sizeof (int); | |
348 | } | |
349 | } | |
350 | #if 0 | |
351 | else | |
352 | { | |
353 | if (lseek (corechan, uaddr, 0) < 0) | |
354 | perror_with_name ("seek on core file"); | |
355 | if (myread (corechan, buf, sizeof (struct fpstate)) < 0) | |
356 | perror_with_name ("read from core file"); | |
357 | skip = 0; | |
358 | } | |
3496b745 | 359 | #endif /* 0 */ |
c7c94073 RP |
360 | |
361 | fpstatep = (struct fpstate *)(buf + skip); | |
362 | print_387_status (fpstatep->status, (struct env387 *)fpstatep->state); | |
363 | } | |
364 | ||
365 | #endif /* never */ |