Commit | Line | Data |
---|---|---|
d0722149 DE |
1 | /* GNU/Linux/x86-64 specific low level interface, for the remote server |
2 | for GDB. | |
42a4f53d | 3 | Copyright (C) 2002-2019 Free Software Foundation, Inc. |
d0722149 DE |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
d41f6d8e | 20 | #include "server.h" |
d0722149 | 21 | #include <signal.h> |
6a271cae | 22 | #include <limits.h> |
f4647387 | 23 | #include <inttypes.h> |
d0722149 DE |
24 | #include "linux-low.h" |
25 | #include "i387-fp.h" | |
df7e5265 | 26 | #include "x86-low.h" |
0747795c | 27 | #include "common/x86-xstate.h" |
5826e159 | 28 | #include "nat/gdb_ptrace.h" |
d0722149 | 29 | |
93813b37 WT |
30 | #ifdef __x86_64__ |
31 | #include "nat/amd64-linux-siginfo.h" | |
32 | #endif | |
33 | ||
d0722149 | 34 | #include "gdb_proc_service.h" |
b5737fa9 PA |
35 | /* Don't include elf/common.h if linux/elf.h got included by |
36 | gdb_proc_service.h. */ | |
37 | #ifndef ELFMAG0 | |
38 | #include "elf/common.h" | |
39 | #endif | |
40 | ||
0747795c | 41 | #include "common/agent.h" |
3aee8918 | 42 | #include "tdesc.h" |
c144c7a0 | 43 | #include "tracepoint.h" |
f699aaba | 44 | #include "ax.h" |
7b669087 | 45 | #include "nat/linux-nat.h" |
4b134ca1 | 46 | #include "nat/x86-linux.h" |
8e5d4070 | 47 | #include "nat/x86-linux-dregs.h" |
ae91f625 | 48 | #include "linux-x86-tdesc.h" |
a196ebeb | 49 | |
3aee8918 PA |
50 | #ifdef __x86_64__ |
51 | static struct target_desc *tdesc_amd64_linux_no_xml; | |
52 | #endif | |
53 | static struct target_desc *tdesc_i386_linux_no_xml; | |
54 | ||
1570b33e | 55 | |
fa593d66 | 56 | static unsigned char jump_insn[] = { 0xe9, 0, 0, 0, 0 }; |
405f8e94 | 57 | static unsigned char small_jump_insn[] = { 0x66, 0xe9, 0, 0 }; |
fa593d66 | 58 | |
1570b33e L |
59 | /* Backward compatibility for gdb without XML support. */ |
60 | ||
61 | static const char *xmltarget_i386_linux_no_xml = "@<target>\ | |
62 | <architecture>i386</architecture>\ | |
63 | <osabi>GNU/Linux</osabi>\ | |
64 | </target>"; | |
f6d1620c L |
65 | |
66 | #ifdef __x86_64__ | |
1570b33e L |
67 | static const char *xmltarget_amd64_linux_no_xml = "@<target>\ |
68 | <architecture>i386:x86-64</architecture>\ | |
69 | <osabi>GNU/Linux</osabi>\ | |
70 | </target>"; | |
f6d1620c | 71 | #endif |
d0722149 DE |
72 | |
73 | #include <sys/reg.h> | |
74 | #include <sys/procfs.h> | |
1570b33e L |
75 | #include <sys/uio.h> |
76 | ||
d0722149 DE |
77 | #ifndef PTRACE_GET_THREAD_AREA |
78 | #define PTRACE_GET_THREAD_AREA 25 | |
79 | #endif | |
80 | ||
81 | /* This definition comes from prctl.h, but some kernels may not have it. */ | |
82 | #ifndef PTRACE_ARCH_PRCTL | |
83 | #define PTRACE_ARCH_PRCTL 30 | |
84 | #endif | |
85 | ||
86 | /* The following definitions come from prctl.h, but may be absent | |
87 | for certain configurations. */ | |
88 | #ifndef ARCH_GET_FS | |
89 | #define ARCH_SET_GS 0x1001 | |
90 | #define ARCH_SET_FS 0x1002 | |
91 | #define ARCH_GET_FS 0x1003 | |
92 | #define ARCH_GET_GS 0x1004 | |
93 | #endif | |
94 | ||
aa5ca48f DE |
95 | /* Per-process arch-specific data we want to keep. */ |
96 | ||
97 | struct arch_process_info | |
98 | { | |
df7e5265 | 99 | struct x86_debug_reg_state debug_reg_state; |
aa5ca48f DE |
100 | }; |
101 | ||
d0722149 DE |
102 | #ifdef __x86_64__ |
103 | ||
104 | /* Mapping between the general-purpose registers in `struct user' | |
105 | format and GDB's register array layout. | |
106 | Note that the transfer layout uses 64-bit regs. */ | |
107 | static /*const*/ int i386_regmap[] = | |
108 | { | |
109 | RAX * 8, RCX * 8, RDX * 8, RBX * 8, | |
110 | RSP * 8, RBP * 8, RSI * 8, RDI * 8, | |
111 | RIP * 8, EFLAGS * 8, CS * 8, SS * 8, | |
112 | DS * 8, ES * 8, FS * 8, GS * 8 | |
113 | }; | |
114 | ||
115 | #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0])) | |
116 | ||
117 | /* So code below doesn't have to care, i386 or amd64. */ | |
118 | #define ORIG_EAX ORIG_RAX | |
bc9540e8 | 119 | #define REGSIZE 8 |
d0722149 DE |
120 | |
121 | static const int x86_64_regmap[] = | |
122 | { | |
123 | RAX * 8, RBX * 8, RCX * 8, RDX * 8, | |
124 | RSI * 8, RDI * 8, RBP * 8, RSP * 8, | |
125 | R8 * 8, R9 * 8, R10 * 8, R11 * 8, | |
126 | R12 * 8, R13 * 8, R14 * 8, R15 * 8, | |
127 | RIP * 8, EFLAGS * 8, CS * 8, SS * 8, | |
128 | DS * 8, ES * 8, FS * 8, GS * 8, | |
129 | -1, -1, -1, -1, -1, -1, -1, -1, | |
130 | -1, -1, -1, -1, -1, -1, -1, -1, | |
131 | -1, -1, -1, -1, -1, -1, -1, -1, | |
a196ebeb WT |
132 | -1, |
133 | -1, -1, -1, -1, -1, -1, -1, -1, | |
134 | ORIG_RAX * 8, | |
2735833d WT |
135 | #ifdef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE |
136 | 21 * 8, 22 * 8, | |
137 | #else | |
138 | -1, -1, | |
139 | #endif | |
a196ebeb | 140 | -1, -1, -1, -1, /* MPX registers BND0 ... BND3. */ |
01f9f808 MS |
141 | -1, -1, /* MPX registers BNDCFGU, BNDSTATUS. */ |
142 | -1, -1, -1, -1, -1, -1, -1, -1, /* xmm16 ... xmm31 (AVX512) */ | |
143 | -1, -1, -1, -1, -1, -1, -1, -1, | |
144 | -1, -1, -1, -1, -1, -1, -1, -1, /* ymm16 ... ymm31 (AVX512) */ | |
145 | -1, -1, -1, -1, -1, -1, -1, -1, | |
146 | -1, -1, -1, -1, -1, -1, -1, -1, /* k0 ... k7 (AVX512) */ | |
147 | -1, -1, -1, -1, -1, -1, -1, -1, /* zmm0 ... zmm31 (AVX512) */ | |
148 | -1, -1, -1, -1, -1, -1, -1, -1, | |
149 | -1, -1, -1, -1, -1, -1, -1, -1, | |
51547df6 MS |
150 | -1, -1, -1, -1, -1, -1, -1, -1, |
151 | -1 /* pkru */ | |
d0722149 DE |
152 | }; |
153 | ||
154 | #define X86_64_NUM_REGS (sizeof (x86_64_regmap) / sizeof (x86_64_regmap[0])) | |
9e0aa64f | 155 | #define X86_64_USER_REGS (GS + 1) |
d0722149 DE |
156 | |
157 | #else /* ! __x86_64__ */ | |
158 | ||
159 | /* Mapping between the general-purpose registers in `struct user' | |
160 | format and GDB's register array layout. */ | |
161 | static /*const*/ int i386_regmap[] = | |
162 | { | |
163 | EAX * 4, ECX * 4, EDX * 4, EBX * 4, | |
164 | UESP * 4, EBP * 4, ESI * 4, EDI * 4, | |
165 | EIP * 4, EFL * 4, CS * 4, SS * 4, | |
166 | DS * 4, ES * 4, FS * 4, GS * 4 | |
167 | }; | |
168 | ||
169 | #define I386_NUM_REGS (sizeof (i386_regmap) / sizeof (i386_regmap[0])) | |
170 | ||
bc9540e8 PA |
171 | #define REGSIZE 4 |
172 | ||
d0722149 | 173 | #endif |
3aee8918 PA |
174 | |
175 | #ifdef __x86_64__ | |
176 | ||
177 | /* Returns true if the current inferior belongs to a x86-64 process, | |
178 | per the tdesc. */ | |
179 | ||
180 | static int | |
181 | is_64bit_tdesc (void) | |
182 | { | |
0bfdf32f | 183 | struct regcache *regcache = get_thread_regcache (current_thread, 0); |
3aee8918 PA |
184 | |
185 | return register_size (regcache->tdesc, 0) == 8; | |
186 | } | |
187 | ||
188 | #endif | |
189 | ||
d0722149 DE |
190 | \f |
191 | /* Called by libthread_db. */ | |
192 | ||
193 | ps_err_e | |
754653a7 | 194 | ps_get_thread_area (struct ps_prochandle *ph, |
d0722149 DE |
195 | lwpid_t lwpid, int idx, void **base) |
196 | { | |
197 | #ifdef __x86_64__ | |
3aee8918 | 198 | int use_64bit = is_64bit_tdesc (); |
d0722149 DE |
199 | |
200 | if (use_64bit) | |
201 | { | |
202 | switch (idx) | |
203 | { | |
204 | case FS: | |
205 | if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0) | |
206 | return PS_OK; | |
207 | break; | |
208 | case GS: | |
209 | if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0) | |
210 | return PS_OK; | |
211 | break; | |
212 | default: | |
213 | return PS_BADADDR; | |
214 | } | |
215 | return PS_ERR; | |
216 | } | |
217 | #endif | |
218 | ||
219 | { | |
220 | unsigned int desc[4]; | |
221 | ||
222 | if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, | |
223 | (void *) (intptr_t) idx, (unsigned long) &desc) < 0) | |
224 | return PS_ERR; | |
225 | ||
d1ec4ce7 DE |
226 | /* Ensure we properly extend the value to 64-bits for x86_64. */ |
227 | *base = (void *) (uintptr_t) desc[1]; | |
d0722149 DE |
228 | return PS_OK; |
229 | } | |
230 | } | |
fa593d66 PA |
231 | |
232 | /* Get the thread area address. This is used to recognize which | |
233 | thread is which when tracing with the in-process agent library. We | |
234 | don't read anything from the address, and treat it as opaque; it's | |
235 | the address itself that we assume is unique per-thread. */ | |
236 | ||
237 | static int | |
238 | x86_get_thread_area (int lwpid, CORE_ADDR *addr) | |
239 | { | |
240 | #ifdef __x86_64__ | |
3aee8918 | 241 | int use_64bit = is_64bit_tdesc (); |
fa593d66 PA |
242 | |
243 | if (use_64bit) | |
244 | { | |
245 | void *base; | |
246 | if (ptrace (PTRACE_ARCH_PRCTL, lwpid, &base, ARCH_GET_FS) == 0) | |
247 | { | |
248 | *addr = (CORE_ADDR) (uintptr_t) base; | |
249 | return 0; | |
250 | } | |
251 | ||
252 | return -1; | |
253 | } | |
254 | #endif | |
255 | ||
256 | { | |
f2907e49 | 257 | struct lwp_info *lwp = find_lwp_pid (ptid_t (lwpid)); |
d86d4aaf DE |
258 | struct thread_info *thr = get_lwp_thread (lwp); |
259 | struct regcache *regcache = get_thread_regcache (thr, 1); | |
fa593d66 PA |
260 | unsigned int desc[4]; |
261 | ULONGEST gs = 0; | |
262 | const int reg_thread_area = 3; /* bits to scale down register value. */ | |
263 | int idx; | |
264 | ||
265 | collect_register_by_name (regcache, "gs", &gs); | |
266 | ||
267 | idx = gs >> reg_thread_area; | |
268 | ||
269 | if (ptrace (PTRACE_GET_THREAD_AREA, | |
d86d4aaf | 270 | lwpid_of (thr), |
493e2a69 | 271 | (void *) (long) idx, (unsigned long) &desc) < 0) |
fa593d66 PA |
272 | return -1; |
273 | ||
274 | *addr = desc[1]; | |
275 | return 0; | |
276 | } | |
277 | } | |
278 | ||
279 | ||
d0722149 DE |
280 | \f |
281 | static int | |
3aee8918 | 282 | x86_cannot_store_register (int regno) |
d0722149 | 283 | { |
3aee8918 PA |
284 | #ifdef __x86_64__ |
285 | if (is_64bit_tdesc ()) | |
286 | return 0; | |
287 | #endif | |
288 | ||
d0722149 DE |
289 | return regno >= I386_NUM_REGS; |
290 | } | |
291 | ||
292 | static int | |
3aee8918 | 293 | x86_cannot_fetch_register (int regno) |
d0722149 | 294 | { |
3aee8918 PA |
295 | #ifdef __x86_64__ |
296 | if (is_64bit_tdesc ()) | |
297 | return 0; | |
298 | #endif | |
299 | ||
d0722149 DE |
300 | return regno >= I386_NUM_REGS; |
301 | } | |
302 | ||
303 | static void | |
442ea881 | 304 | x86_fill_gregset (struct regcache *regcache, void *buf) |
d0722149 DE |
305 | { |
306 | int i; | |
307 | ||
308 | #ifdef __x86_64__ | |
3aee8918 | 309 | if (register_size (regcache->tdesc, 0) == 8) |
d0722149 DE |
310 | { |
311 | for (i = 0; i < X86_64_NUM_REGS; i++) | |
312 | if (x86_64_regmap[i] != -1) | |
442ea881 | 313 | collect_register (regcache, i, ((char *) buf) + x86_64_regmap[i]); |
2735833d WT |
314 | |
315 | #ifndef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE | |
316 | { | |
317 | unsigned long base; | |
318 | int lwpid = lwpid_of (current_thread); | |
319 | ||
320 | collect_register_by_name (regcache, "fs_base", &base); | |
321 | ptrace (PTRACE_ARCH_PRCTL, lwpid, &base, ARCH_SET_FS); | |
322 | ||
323 | collect_register_by_name (regcache, "gs_base", &base); | |
324 | ptrace (PTRACE_ARCH_PRCTL, lwpid, &base, ARCH_SET_GS); | |
325 | } | |
326 | #endif | |
327 | ||
d0722149 DE |
328 | return; |
329 | } | |
9e0aa64f JK |
330 | |
331 | /* 32-bit inferior registers need to be zero-extended. | |
332 | Callers would read uninitialized memory otherwise. */ | |
333 | memset (buf, 0x00, X86_64_USER_REGS * 8); | |
d0722149 DE |
334 | #endif |
335 | ||
336 | for (i = 0; i < I386_NUM_REGS; i++) | |
442ea881 | 337 | collect_register (regcache, i, ((char *) buf) + i386_regmap[i]); |
d0722149 | 338 | |
442ea881 | 339 | collect_register_by_name (regcache, "orig_eax", |
bc9540e8 | 340 | ((char *) buf) + ORIG_EAX * REGSIZE); |
d0722149 DE |
341 | } |
342 | ||
343 | static void | |
442ea881 | 344 | x86_store_gregset (struct regcache *regcache, const void *buf) |
d0722149 DE |
345 | { |
346 | int i; | |
347 | ||
348 | #ifdef __x86_64__ | |
3aee8918 | 349 | if (register_size (regcache->tdesc, 0) == 8) |
d0722149 DE |
350 | { |
351 | for (i = 0; i < X86_64_NUM_REGS; i++) | |
352 | if (x86_64_regmap[i] != -1) | |
442ea881 | 353 | supply_register (regcache, i, ((char *) buf) + x86_64_regmap[i]); |
2735833d WT |
354 | |
355 | #ifndef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE | |
356 | { | |
357 | unsigned long base; | |
358 | int lwpid = lwpid_of (current_thread); | |
359 | ||
360 | if (ptrace (PTRACE_ARCH_PRCTL, lwpid, &base, ARCH_GET_FS) == 0) | |
361 | supply_register_by_name (regcache, "fs_base", &base); | |
362 | ||
363 | if (ptrace (PTRACE_ARCH_PRCTL, lwpid, &base, ARCH_GET_GS) == 0) | |
364 | supply_register_by_name (regcache, "gs_base", &base); | |
365 | } | |
366 | #endif | |
d0722149 DE |
367 | return; |
368 | } | |
369 | #endif | |
370 | ||
371 | for (i = 0; i < I386_NUM_REGS; i++) | |
442ea881 | 372 | supply_register (regcache, i, ((char *) buf) + i386_regmap[i]); |
d0722149 | 373 | |
442ea881 | 374 | supply_register_by_name (regcache, "orig_eax", |
bc9540e8 | 375 | ((char *) buf) + ORIG_EAX * REGSIZE); |
d0722149 DE |
376 | } |
377 | ||
378 | static void | |
442ea881 | 379 | x86_fill_fpregset (struct regcache *regcache, void *buf) |
d0722149 DE |
380 | { |
381 | #ifdef __x86_64__ | |
442ea881 | 382 | i387_cache_to_fxsave (regcache, buf); |
d0722149 | 383 | #else |
442ea881 | 384 | i387_cache_to_fsave (regcache, buf); |
d0722149 DE |
385 | #endif |
386 | } | |
387 | ||
388 | static void | |
442ea881 | 389 | x86_store_fpregset (struct regcache *regcache, const void *buf) |
d0722149 DE |
390 | { |
391 | #ifdef __x86_64__ | |
442ea881 | 392 | i387_fxsave_to_cache (regcache, buf); |
d0722149 | 393 | #else |
442ea881 | 394 | i387_fsave_to_cache (regcache, buf); |
d0722149 DE |
395 | #endif |
396 | } | |
397 | ||
398 | #ifndef __x86_64__ | |
399 | ||
400 | static void | |
442ea881 | 401 | x86_fill_fpxregset (struct regcache *regcache, void *buf) |
d0722149 | 402 | { |
442ea881 | 403 | i387_cache_to_fxsave (regcache, buf); |
d0722149 DE |
404 | } |
405 | ||
406 | static void | |
442ea881 | 407 | x86_store_fpxregset (struct regcache *regcache, const void *buf) |
d0722149 | 408 | { |
442ea881 | 409 | i387_fxsave_to_cache (regcache, buf); |
d0722149 DE |
410 | } |
411 | ||
412 | #endif | |
413 | ||
1570b33e L |
414 | static void |
415 | x86_fill_xstateregset (struct regcache *regcache, void *buf) | |
416 | { | |
417 | i387_cache_to_xsave (regcache, buf); | |
418 | } | |
419 | ||
420 | static void | |
421 | x86_store_xstateregset (struct regcache *regcache, const void *buf) | |
422 | { | |
423 | i387_xsave_to_cache (regcache, buf); | |
424 | } | |
425 | ||
d0722149 DE |
426 | /* ??? The non-biarch i386 case stores all the i387 regs twice. |
427 | Once in i387_.*fsave.* and once in i387_.*fxsave.*. | |
428 | This is, presumably, to handle the case where PTRACE_[GS]ETFPXREGS | |
429 | doesn't work. IWBN to avoid the duplication in the case where it | |
430 | does work. Maybe the arch_setup routine could check whether it works | |
3aee8918 | 431 | and update the supported regsets accordingly. */ |
d0722149 | 432 | |
3aee8918 | 433 | static struct regset_info x86_regsets[] = |
d0722149 DE |
434 | { |
435 | #ifdef HAVE_PTRACE_GETREGS | |
1570b33e | 436 | { PTRACE_GETREGS, PTRACE_SETREGS, 0, sizeof (elf_gregset_t), |
d0722149 DE |
437 | GENERAL_REGS, |
438 | x86_fill_gregset, x86_store_gregset }, | |
1570b33e L |
439 | { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_X86_XSTATE, 0, |
440 | EXTENDED_REGS, x86_fill_xstateregset, x86_store_xstateregset }, | |
d0722149 DE |
441 | # ifndef __x86_64__ |
442 | # ifdef HAVE_PTRACE_GETFPXREGS | |
1570b33e | 443 | { PTRACE_GETFPXREGS, PTRACE_SETFPXREGS, 0, sizeof (elf_fpxregset_t), |
d0722149 DE |
444 | EXTENDED_REGS, |
445 | x86_fill_fpxregset, x86_store_fpxregset }, | |
446 | # endif | |
447 | # endif | |
1570b33e | 448 | { PTRACE_GETFPREGS, PTRACE_SETFPREGS, 0, sizeof (elf_fpregset_t), |
d0722149 DE |
449 | FP_REGS, |
450 | x86_fill_fpregset, x86_store_fpregset }, | |
451 | #endif /* HAVE_PTRACE_GETREGS */ | |
50bc912a | 452 | NULL_REGSET |
d0722149 DE |
453 | }; |
454 | ||
455 | static CORE_ADDR | |
442ea881 | 456 | x86_get_pc (struct regcache *regcache) |
d0722149 | 457 | { |
3aee8918 | 458 | int use_64bit = register_size (regcache->tdesc, 0) == 8; |
d0722149 DE |
459 | |
460 | if (use_64bit) | |
461 | { | |
6598661d PA |
462 | uint64_t pc; |
463 | ||
442ea881 | 464 | collect_register_by_name (regcache, "rip", &pc); |
d0722149 DE |
465 | return (CORE_ADDR) pc; |
466 | } | |
467 | else | |
468 | { | |
6598661d PA |
469 | uint32_t pc; |
470 | ||
442ea881 | 471 | collect_register_by_name (regcache, "eip", &pc); |
d0722149 DE |
472 | return (CORE_ADDR) pc; |
473 | } | |
474 | } | |
475 | ||
476 | static void | |
442ea881 | 477 | x86_set_pc (struct regcache *regcache, CORE_ADDR pc) |
d0722149 | 478 | { |
3aee8918 | 479 | int use_64bit = register_size (regcache->tdesc, 0) == 8; |
d0722149 DE |
480 | |
481 | if (use_64bit) | |
482 | { | |
6598661d PA |
483 | uint64_t newpc = pc; |
484 | ||
442ea881 | 485 | supply_register_by_name (regcache, "rip", &newpc); |
d0722149 DE |
486 | } |
487 | else | |
488 | { | |
6598661d PA |
489 | uint32_t newpc = pc; |
490 | ||
442ea881 | 491 | supply_register_by_name (regcache, "eip", &newpc); |
d0722149 DE |
492 | } |
493 | } | |
494 | \f | |
dd373349 | 495 | static const gdb_byte x86_breakpoint[] = { 0xCC }; |
d0722149 DE |
496 | #define x86_breakpoint_len 1 |
497 | ||
498 | static int | |
499 | x86_breakpoint_at (CORE_ADDR pc) | |
500 | { | |
501 | unsigned char c; | |
502 | ||
fc7238bb | 503 | (*the_target->read_memory) (pc, &c, 1); |
d0722149 DE |
504 | if (c == 0xCC) |
505 | return 1; | |
506 | ||
507 | return 0; | |
508 | } | |
509 | \f | |
42995dbd | 510 | /* Low-level function vector. */ |
df7e5265 | 511 | struct x86_dr_low_type x86_dr_low = |
42995dbd | 512 | { |
d33472ad GB |
513 | x86_linux_dr_set_control, |
514 | x86_linux_dr_set_addr, | |
515 | x86_linux_dr_get_addr, | |
516 | x86_linux_dr_get_status, | |
517 | x86_linux_dr_get_control, | |
42995dbd GB |
518 | sizeof (void *), |
519 | }; | |
aa5ca48f | 520 | \f |
90d74c30 | 521 | /* Breakpoint/Watchpoint support. */ |
aa5ca48f DE |
522 | |
523 | static int | |
802e8e6d PA |
524 | x86_supports_z_point_type (char z_type) |
525 | { | |
526 | switch (z_type) | |
527 | { | |
528 | case Z_PACKET_SW_BP: | |
529 | case Z_PACKET_HW_BP: | |
530 | case Z_PACKET_WRITE_WP: | |
531 | case Z_PACKET_ACCESS_WP: | |
532 | return 1; | |
533 | default: | |
534 | return 0; | |
535 | } | |
536 | } | |
537 | ||
538 | static int | |
539 | x86_insert_point (enum raw_bkpt_type type, CORE_ADDR addr, | |
540 | int size, struct raw_breakpoint *bp) | |
aa5ca48f DE |
541 | { |
542 | struct process_info *proc = current_process (); | |
802e8e6d | 543 | |
aa5ca48f DE |
544 | switch (type) |
545 | { | |
802e8e6d PA |
546 | case raw_bkpt_type_hw: |
547 | case raw_bkpt_type_write_wp: | |
548 | case raw_bkpt_type_access_wp: | |
a4165e94 | 549 | { |
802e8e6d PA |
550 | enum target_hw_bp_type hw_type |
551 | = raw_bkpt_type_to_target_hw_bp_type (type); | |
df7e5265 | 552 | struct x86_debug_reg_state *state |
fe978cb0 | 553 | = &proc->priv->arch_private->debug_reg_state; |
a4165e94 | 554 | |
df7e5265 | 555 | return x86_dr_insert_watchpoint (state, hw_type, addr, size); |
a4165e94 | 556 | } |
961bd387 | 557 | |
aa5ca48f DE |
558 | default: |
559 | /* Unsupported. */ | |
560 | return 1; | |
561 | } | |
562 | } | |
563 | ||
564 | static int | |
802e8e6d PA |
565 | x86_remove_point (enum raw_bkpt_type type, CORE_ADDR addr, |
566 | int size, struct raw_breakpoint *bp) | |
aa5ca48f DE |
567 | { |
568 | struct process_info *proc = current_process (); | |
802e8e6d | 569 | |
aa5ca48f DE |
570 | switch (type) |
571 | { | |
802e8e6d PA |
572 | case raw_bkpt_type_hw: |
573 | case raw_bkpt_type_write_wp: | |
574 | case raw_bkpt_type_access_wp: | |
a4165e94 | 575 | { |
802e8e6d PA |
576 | enum target_hw_bp_type hw_type |
577 | = raw_bkpt_type_to_target_hw_bp_type (type); | |
df7e5265 | 578 | struct x86_debug_reg_state *state |
fe978cb0 | 579 | = &proc->priv->arch_private->debug_reg_state; |
a4165e94 | 580 | |
df7e5265 | 581 | return x86_dr_remove_watchpoint (state, hw_type, addr, size); |
a4165e94 | 582 | } |
aa5ca48f DE |
583 | default: |
584 | /* Unsupported. */ | |
585 | return 1; | |
586 | } | |
587 | } | |
588 | ||
589 | static int | |
590 | x86_stopped_by_watchpoint (void) | |
591 | { | |
592 | struct process_info *proc = current_process (); | |
fe978cb0 | 593 | return x86_dr_stopped_by_watchpoint (&proc->priv->arch_private->debug_reg_state); |
aa5ca48f DE |
594 | } |
595 | ||
596 | static CORE_ADDR | |
597 | x86_stopped_data_address (void) | |
598 | { | |
599 | struct process_info *proc = current_process (); | |
600 | CORE_ADDR addr; | |
fe978cb0 | 601 | if (x86_dr_stopped_data_address (&proc->priv->arch_private->debug_reg_state, |
df7e5265 | 602 | &addr)) |
aa5ca48f DE |
603 | return addr; |
604 | return 0; | |
605 | } | |
606 | \f | |
607 | /* Called when a new process is created. */ | |
608 | ||
609 | static struct arch_process_info * | |
610 | x86_linux_new_process (void) | |
611 | { | |
ed859da7 | 612 | struct arch_process_info *info = XCNEW (struct arch_process_info); |
aa5ca48f | 613 | |
df7e5265 | 614 | x86_low_init_dregs (&info->debug_reg_state); |
aa5ca48f DE |
615 | |
616 | return info; | |
617 | } | |
618 | ||
04ec7890 SM |
619 | /* Called when a process is being deleted. */ |
620 | ||
621 | static void | |
622 | x86_linux_delete_process (struct arch_process_info *info) | |
623 | { | |
624 | xfree (info); | |
625 | } | |
626 | ||
3a8a0396 DB |
627 | /* Target routine for linux_new_fork. */ |
628 | ||
629 | static void | |
630 | x86_linux_new_fork (struct process_info *parent, struct process_info *child) | |
631 | { | |
632 | /* These are allocated by linux_add_process. */ | |
633 | gdb_assert (parent->priv != NULL | |
634 | && parent->priv->arch_private != NULL); | |
635 | gdb_assert (child->priv != NULL | |
636 | && child->priv->arch_private != NULL); | |
637 | ||
638 | /* Linux kernel before 2.6.33 commit | |
639 | 72f674d203cd230426437cdcf7dd6f681dad8b0d | |
640 | will inherit hardware debug registers from parent | |
641 | on fork/vfork/clone. Newer Linux kernels create such tasks with | |
642 | zeroed debug registers. | |
643 | ||
644 | GDB core assumes the child inherits the watchpoints/hw | |
645 | breakpoints of the parent, and will remove them all from the | |
646 | forked off process. Copy the debug registers mirrors into the | |
647 | new process so that all breakpoints and watchpoints can be | |
648 | removed together. The debug registers mirror will become zeroed | |
649 | in the end before detaching the forked off process, thus making | |
650 | this compatible with older Linux kernels too. */ | |
651 | ||
652 | *child->priv->arch_private = *parent->priv->arch_private; | |
653 | } | |
654 | ||
70a0bb6b GB |
655 | /* See nat/x86-dregs.h. */ |
656 | ||
657 | struct x86_debug_reg_state * | |
658 | x86_debug_reg_state (pid_t pid) | |
659 | { | |
660 | struct process_info *proc = find_process_pid (pid); | |
661 | ||
662 | return &proc->priv->arch_private->debug_reg_state; | |
663 | } | |
aa5ca48f | 664 | \f |
d0722149 DE |
665 | /* When GDBSERVER is built as a 64-bit application on linux, the |
666 | PTRACE_GETSIGINFO data is always presented in 64-bit layout. Since | |
667 | debugging a 32-bit inferior with a 64-bit GDBSERVER should look the same | |
668 | as debugging it with a 32-bit GDBSERVER, we do the 32-bit <-> 64-bit | |
669 | conversion in-place ourselves. */ | |
670 | ||
9cf12d57 | 671 | /* Convert a ptrace/host siginfo object, into/from the siginfo in the |
d0722149 DE |
672 | layout of the inferiors' architecture. Returns true if any |
673 | conversion was done; false otherwise. If DIRECTION is 1, then copy | |
9cf12d57 | 674 | from INF to PTRACE. If DIRECTION is 0, copy from PTRACE to |
d0722149 DE |
675 | INF. */ |
676 | ||
677 | static int | |
9cf12d57 | 678 | x86_siginfo_fixup (siginfo_t *ptrace, gdb_byte *inf, int direction) |
d0722149 DE |
679 | { |
680 | #ifdef __x86_64__ | |
760256f9 | 681 | unsigned int machine; |
0bfdf32f | 682 | int tid = lwpid_of (current_thread); |
760256f9 PA |
683 | int is_elf64 = linux_pid_exe_is_elf_64_file (tid, &machine); |
684 | ||
d0722149 | 685 | /* Is the inferior 32-bit? If so, then fixup the siginfo object. */ |
3aee8918 | 686 | if (!is_64bit_tdesc ()) |
9cf12d57 | 687 | return amd64_linux_siginfo_fixup_common (ptrace, inf, direction, |
c23bbc1c | 688 | FIXUP_32); |
c92b5177 | 689 | /* No fixup for native x32 GDB. */ |
760256f9 | 690 | else if (!is_elf64 && sizeof (void *) == 8) |
9cf12d57 | 691 | return amd64_linux_siginfo_fixup_common (ptrace, inf, direction, |
c23bbc1c | 692 | FIXUP_X32); |
d0722149 DE |
693 | #endif |
694 | ||
695 | return 0; | |
696 | } | |
697 | \f | |
1570b33e L |
698 | static int use_xml; |
699 | ||
3aee8918 PA |
700 | /* Format of XSAVE extended state is: |
701 | struct | |
702 | { | |
703 | fxsave_bytes[0..463] | |
704 | sw_usable_bytes[464..511] | |
705 | xstate_hdr_bytes[512..575] | |
706 | avx_bytes[576..831] | |
707 | future_state etc | |
708 | }; | |
709 | ||
710 | Same memory layout will be used for the coredump NT_X86_XSTATE | |
711 | representing the XSAVE extended state registers. | |
712 | ||
713 | The first 8 bytes of the sw_usable_bytes[464..467] is the OS enabled | |
714 | extended state mask, which is the same as the extended control register | |
715 | 0 (the XFEATURE_ENABLED_MASK register), XCR0. We can use this mask | |
716 | together with the mask saved in the xstate_hdr_bytes to determine what | |
717 | states the processor/OS supports and what state, used or initialized, | |
718 | the process/thread is in. */ | |
719 | #define I386_LINUX_XSAVE_XCR0_OFFSET 464 | |
720 | ||
721 | /* Does the current host support the GETFPXREGS request? The header | |
722 | file may or may not define it, and even if it is defined, the | |
723 | kernel will return EIO if it's running on a pre-SSE processor. */ | |
724 | int have_ptrace_getfpxregs = | |
725 | #ifdef HAVE_PTRACE_GETFPXREGS | |
726 | -1 | |
727 | #else | |
728 | 0 | |
729 | #endif | |
730 | ; | |
1570b33e | 731 | |
3aee8918 PA |
732 | /* Get Linux/x86 target description from running target. */ |
733 | ||
734 | static const struct target_desc * | |
735 | x86_linux_read_description (void) | |
1570b33e | 736 | { |
3aee8918 PA |
737 | unsigned int machine; |
738 | int is_elf64; | |
a196ebeb | 739 | int xcr0_features; |
3aee8918 PA |
740 | int tid; |
741 | static uint64_t xcr0; | |
3a13a53b | 742 | struct regset_info *regset; |
1570b33e | 743 | |
0bfdf32f | 744 | tid = lwpid_of (current_thread); |
1570b33e | 745 | |
3aee8918 | 746 | is_elf64 = linux_pid_exe_is_elf_64_file (tid, &machine); |
45ba0d02 | 747 | |
3aee8918 | 748 | if (sizeof (void *) == 4) |
3a13a53b | 749 | { |
3aee8918 PA |
750 | if (is_elf64 > 0) |
751 | error (_("Can't debug 64-bit process with 32-bit GDBserver")); | |
752 | #ifndef __x86_64__ | |
753 | else if (machine == EM_X86_64) | |
754 | error (_("Can't debug x86-64 process with 32-bit GDBserver")); | |
755 | #endif | |
756 | } | |
3a13a53b | 757 | |
3aee8918 PA |
758 | #if !defined __x86_64__ && defined HAVE_PTRACE_GETFPXREGS |
759 | if (machine == EM_386 && have_ptrace_getfpxregs == -1) | |
760 | { | |
761 | elf_fpxregset_t fpxregs; | |
3a13a53b | 762 | |
3aee8918 | 763 | if (ptrace (PTRACE_GETFPXREGS, tid, 0, (long) &fpxregs) < 0) |
3a13a53b | 764 | { |
3aee8918 PA |
765 | have_ptrace_getfpxregs = 0; |
766 | have_ptrace_getregset = 0; | |
f49ff000 | 767 | return i386_linux_read_description (X86_XSTATE_X87); |
3a13a53b | 768 | } |
3aee8918 PA |
769 | else |
770 | have_ptrace_getfpxregs = 1; | |
3a13a53b | 771 | } |
1570b33e L |
772 | #endif |
773 | ||
774 | if (!use_xml) | |
775 | { | |
df7e5265 | 776 | x86_xcr0 = X86_XSTATE_SSE_MASK; |
3aee8918 | 777 | |
1570b33e L |
778 | /* Don't use XML. */ |
779 | #ifdef __x86_64__ | |
3aee8918 PA |
780 | if (machine == EM_X86_64) |
781 | return tdesc_amd64_linux_no_xml; | |
1570b33e | 782 | else |
1570b33e | 783 | #endif |
3aee8918 | 784 | return tdesc_i386_linux_no_xml; |
1570b33e L |
785 | } |
786 | ||
1570b33e L |
787 | if (have_ptrace_getregset == -1) |
788 | { | |
df7e5265 | 789 | uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))]; |
1570b33e | 790 | struct iovec iov; |
1570b33e L |
791 | |
792 | iov.iov_base = xstateregs; | |
793 | iov.iov_len = sizeof (xstateregs); | |
794 | ||
795 | /* Check if PTRACE_GETREGSET works. */ | |
3aee8918 PA |
796 | if (ptrace (PTRACE_GETREGSET, tid, |
797 | (unsigned int) NT_X86_XSTATE, (long) &iov) < 0) | |
798 | have_ptrace_getregset = 0; | |
799 | else | |
1570b33e | 800 | { |
3aee8918 PA |
801 | have_ptrace_getregset = 1; |
802 | ||
803 | /* Get XCR0 from XSAVE extended state. */ | |
804 | xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET | |
805 | / sizeof (uint64_t))]; | |
806 | ||
807 | /* Use PTRACE_GETREGSET if it is available. */ | |
808 | for (regset = x86_regsets; | |
809 | regset->fill_function != NULL; regset++) | |
810 | if (regset->get_request == PTRACE_GETREGSET) | |
df7e5265 | 811 | regset->size = X86_XSTATE_SIZE (xcr0); |
3aee8918 PA |
812 | else if (regset->type != GENERAL_REGS) |
813 | regset->size = 0; | |
1570b33e | 814 | } |
1570b33e L |
815 | } |
816 | ||
3aee8918 | 817 | /* Check the native XCR0 only if PTRACE_GETREGSET is available. */ |
a196ebeb | 818 | xcr0_features = (have_ptrace_getregset |
2e1e43e1 | 819 | && (xcr0 & X86_XSTATE_ALL_MASK)); |
3aee8918 | 820 | |
a196ebeb | 821 | if (xcr0_features) |
3aee8918 | 822 | x86_xcr0 = xcr0; |
1570b33e | 823 | |
3aee8918 PA |
824 | if (machine == EM_X86_64) |
825 | { | |
1570b33e | 826 | #ifdef __x86_64__ |
b4570e4b | 827 | const target_desc *tdesc = NULL; |
a196ebeb | 828 | |
b4570e4b | 829 | if (xcr0_features) |
3aee8918 | 830 | { |
b4570e4b YQ |
831 | tdesc = amd64_linux_read_description (xcr0 & X86_XSTATE_ALL_MASK, |
832 | !is_elf64); | |
1570b33e | 833 | } |
b4570e4b YQ |
834 | |
835 | if (tdesc == NULL) | |
836 | tdesc = amd64_linux_read_description (X86_XSTATE_SSE_MASK, !is_elf64); | |
837 | return tdesc; | |
3aee8918 | 838 | #endif |
1570b33e | 839 | } |
3aee8918 PA |
840 | else |
841 | { | |
f49ff000 | 842 | const target_desc *tdesc = NULL; |
a1fa17ee | 843 | |
f49ff000 YQ |
844 | if (xcr0_features) |
845 | tdesc = i386_linux_read_description (xcr0 & X86_XSTATE_ALL_MASK); | |
2b863f51 | 846 | |
f49ff000 YQ |
847 | if (tdesc == NULL) |
848 | tdesc = i386_linux_read_description (X86_XSTATE_SSE); | |
a196ebeb | 849 | |
f49ff000 | 850 | return tdesc; |
3aee8918 PA |
851 | } |
852 | ||
853 | gdb_assert_not_reached ("failed to return tdesc"); | |
854 | } | |
855 | ||
3aee8918 PA |
856 | /* Update all the target description of all processes; a new GDB |
857 | connected, and it may or not support xml target descriptions. */ | |
858 | ||
859 | static void | |
860 | x86_linux_update_xmltarget (void) | |
861 | { | |
0bfdf32f | 862 | struct thread_info *saved_thread = current_thread; |
3aee8918 PA |
863 | |
864 | /* Before changing the register cache's internal layout, flush the | |
865 | contents of the current valid caches back to the threads, and | |
866 | release the current regcache objects. */ | |
867 | regcache_release (); | |
868 | ||
9179355e SM |
869 | for_each_process ([] (process_info *proc) { |
870 | int pid = proc->pid; | |
871 | ||
872 | /* Look up any thread of this process. */ | |
873 | current_thread = find_any_thread_of_pid (pid); | |
874 | ||
875 | the_low_target.arch_setup (); | |
876 | }); | |
3aee8918 | 877 | |
0bfdf32f | 878 | current_thread = saved_thread; |
1570b33e L |
879 | } |
880 | ||
881 | /* Process qSupported query, "xmlRegisters=". Update the buffer size for | |
882 | PTRACE_GETREGSET. */ | |
883 | ||
884 | static void | |
06e03fff | 885 | x86_linux_process_qsupported (char **features, int count) |
1570b33e | 886 | { |
06e03fff PA |
887 | int i; |
888 | ||
1570b33e L |
889 | /* Return if gdb doesn't support XML. If gdb sends "xmlRegisters=" |
890 | with "i386" in qSupported query, it supports x86 XML target | |
891 | descriptions. */ | |
892 | use_xml = 0; | |
06e03fff | 893 | for (i = 0; i < count; i++) |
1570b33e | 894 | { |
06e03fff | 895 | const char *feature = features[i]; |
1570b33e | 896 | |
06e03fff | 897 | if (startswith (feature, "xmlRegisters=")) |
1570b33e | 898 | { |
06e03fff PA |
899 | char *copy = xstrdup (feature + 13); |
900 | char *p; | |
901 | ||
902 | for (p = strtok (copy, ","); p != NULL; p = strtok (NULL, ",")) | |
1570b33e | 903 | { |
06e03fff PA |
904 | if (strcmp (p, "i386") == 0) |
905 | { | |
906 | use_xml = 1; | |
907 | break; | |
908 | } | |
1570b33e | 909 | } |
1570b33e | 910 | |
06e03fff PA |
911 | free (copy); |
912 | } | |
1570b33e | 913 | } |
1570b33e L |
914 | x86_linux_update_xmltarget (); |
915 | } | |
916 | ||
3aee8918 | 917 | /* Common for x86/x86-64. */ |
d0722149 | 918 | |
3aee8918 PA |
919 | static struct regsets_info x86_regsets_info = |
920 | { | |
921 | x86_regsets, /* regsets */ | |
922 | 0, /* num_regsets */ | |
923 | NULL, /* disabled_regsets */ | |
924 | }; | |
214d508e L |
925 | |
926 | #ifdef __x86_64__ | |
3aee8918 PA |
927 | static struct regs_info amd64_linux_regs_info = |
928 | { | |
929 | NULL, /* regset_bitmap */ | |
930 | NULL, /* usrregs_info */ | |
931 | &x86_regsets_info | |
932 | }; | |
d0722149 | 933 | #endif |
3aee8918 PA |
934 | static struct usrregs_info i386_linux_usrregs_info = |
935 | { | |
936 | I386_NUM_REGS, | |
937 | i386_regmap, | |
938 | }; | |
d0722149 | 939 | |
3aee8918 PA |
940 | static struct regs_info i386_linux_regs_info = |
941 | { | |
942 | NULL, /* regset_bitmap */ | |
943 | &i386_linux_usrregs_info, | |
944 | &x86_regsets_info | |
945 | }; | |
d0722149 | 946 | |
3aee8918 PA |
947 | const struct regs_info * |
948 | x86_linux_regs_info (void) | |
949 | { | |
950 | #ifdef __x86_64__ | |
951 | if (is_64bit_tdesc ()) | |
952 | return &amd64_linux_regs_info; | |
953 | else | |
954 | #endif | |
955 | return &i386_linux_regs_info; | |
956 | } | |
d0722149 | 957 | |
3aee8918 PA |
958 | /* Initialize the target description for the architecture of the |
959 | inferior. */ | |
1570b33e | 960 | |
3aee8918 PA |
961 | static void |
962 | x86_arch_setup (void) | |
963 | { | |
964 | current_process ()->tdesc = x86_linux_read_description (); | |
d0722149 DE |
965 | } |
966 | ||
82075af2 JS |
967 | /* Fill *SYSNO and *SYSRET with the syscall nr trapped and the syscall return |
968 | code. This should only be called if LWP got a SYSCALL_SIGTRAP. */ | |
969 | ||
970 | static void | |
4cc32bec | 971 | x86_get_syscall_trapinfo (struct regcache *regcache, int *sysno) |
82075af2 JS |
972 | { |
973 | int use_64bit = register_size (regcache->tdesc, 0) == 8; | |
974 | ||
975 | if (use_64bit) | |
976 | { | |
977 | long l_sysno; | |
82075af2 JS |
978 | |
979 | collect_register_by_name (regcache, "orig_rax", &l_sysno); | |
82075af2 | 980 | *sysno = (int) l_sysno; |
82075af2 JS |
981 | } |
982 | else | |
4cc32bec | 983 | collect_register_by_name (regcache, "orig_eax", sysno); |
82075af2 JS |
984 | } |
985 | ||
219f2f23 PA |
986 | static int |
987 | x86_supports_tracepoints (void) | |
988 | { | |
989 | return 1; | |
990 | } | |
991 | ||
fa593d66 PA |
992 | static void |
993 | append_insns (CORE_ADDR *to, size_t len, const unsigned char *buf) | |
994 | { | |
995 | write_inferior_memory (*to, buf, len); | |
996 | *to += len; | |
997 | } | |
998 | ||
999 | static int | |
a121b7c1 | 1000 | push_opcode (unsigned char *buf, const char *op) |
fa593d66 PA |
1001 | { |
1002 | unsigned char *buf_org = buf; | |
1003 | ||
1004 | while (1) | |
1005 | { | |
1006 | char *endptr; | |
1007 | unsigned long ul = strtoul (op, &endptr, 16); | |
1008 | ||
1009 | if (endptr == op) | |
1010 | break; | |
1011 | ||
1012 | *buf++ = ul; | |
1013 | op = endptr; | |
1014 | } | |
1015 | ||
1016 | return buf - buf_org; | |
1017 | } | |
1018 | ||
1019 | #ifdef __x86_64__ | |
1020 | ||
1021 | /* Build a jump pad that saves registers and calls a collection | |
1022 | function. Writes a jump instruction to the jump pad to | |
1023 | JJUMPAD_INSN. The caller is responsible to write it in at the | |
1024 | tracepoint address. */ | |
1025 | ||
1026 | static int | |
1027 | amd64_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint, CORE_ADDR tpaddr, | |
1028 | CORE_ADDR collector, | |
1029 | CORE_ADDR lockaddr, | |
1030 | ULONGEST orig_size, | |
1031 | CORE_ADDR *jump_entry, | |
405f8e94 SS |
1032 | CORE_ADDR *trampoline, |
1033 | ULONGEST *trampoline_size, | |
fa593d66 PA |
1034 | unsigned char *jjump_pad_insn, |
1035 | ULONGEST *jjump_pad_insn_size, | |
1036 | CORE_ADDR *adjusted_insn_addr, | |
405f8e94 SS |
1037 | CORE_ADDR *adjusted_insn_addr_end, |
1038 | char *err) | |
fa593d66 PA |
1039 | { |
1040 | unsigned char buf[40]; | |
1041 | int i, offset; | |
f4647387 YQ |
1042 | int64_t loffset; |
1043 | ||
fa593d66 PA |
1044 | CORE_ADDR buildaddr = *jump_entry; |
1045 | ||
1046 | /* Build the jump pad. */ | |
1047 | ||
1048 | /* First, do tracepoint data collection. Save registers. */ | |
1049 | i = 0; | |
1050 | /* Need to ensure stack pointer saved first. */ | |
1051 | buf[i++] = 0x54; /* push %rsp */ | |
1052 | buf[i++] = 0x55; /* push %rbp */ | |
1053 | buf[i++] = 0x57; /* push %rdi */ | |
1054 | buf[i++] = 0x56; /* push %rsi */ | |
1055 | buf[i++] = 0x52; /* push %rdx */ | |
1056 | buf[i++] = 0x51; /* push %rcx */ | |
1057 | buf[i++] = 0x53; /* push %rbx */ | |
1058 | buf[i++] = 0x50; /* push %rax */ | |
1059 | buf[i++] = 0x41; buf[i++] = 0x57; /* push %r15 */ | |
1060 | buf[i++] = 0x41; buf[i++] = 0x56; /* push %r14 */ | |
1061 | buf[i++] = 0x41; buf[i++] = 0x55; /* push %r13 */ | |
1062 | buf[i++] = 0x41; buf[i++] = 0x54; /* push %r12 */ | |
1063 | buf[i++] = 0x41; buf[i++] = 0x53; /* push %r11 */ | |
1064 | buf[i++] = 0x41; buf[i++] = 0x52; /* push %r10 */ | |
1065 | buf[i++] = 0x41; buf[i++] = 0x51; /* push %r9 */ | |
1066 | buf[i++] = 0x41; buf[i++] = 0x50; /* push %r8 */ | |
1067 | buf[i++] = 0x9c; /* pushfq */ | |
c8ef42ee | 1068 | buf[i++] = 0x48; /* movabs <addr>,%rdi */ |
fa593d66 | 1069 | buf[i++] = 0xbf; |
c8ef42ee PA |
1070 | memcpy (buf + i, &tpaddr, 8); |
1071 | i += 8; | |
fa593d66 PA |
1072 | buf[i++] = 0x57; /* push %rdi */ |
1073 | append_insns (&buildaddr, i, buf); | |
1074 | ||
1075 | /* Stack space for the collecting_t object. */ | |
1076 | i = 0; | |
1077 | i += push_opcode (&buf[i], "48 83 ec 18"); /* sub $0x18,%rsp */ | |
1078 | i += push_opcode (&buf[i], "48 b8"); /* mov <tpoint>,%rax */ | |
1079 | memcpy (buf + i, &tpoint, 8); | |
1080 | i += 8; | |
1081 | i += push_opcode (&buf[i], "48 89 04 24"); /* mov %rax,(%rsp) */ | |
1082 | i += push_opcode (&buf[i], | |
1083 | "64 48 8b 04 25 00 00 00 00"); /* mov %fs:0x0,%rax */ | |
1084 | i += push_opcode (&buf[i], "48 89 44 24 08"); /* mov %rax,0x8(%rsp) */ | |
1085 | append_insns (&buildaddr, i, buf); | |
1086 | ||
1087 | /* spin-lock. */ | |
1088 | i = 0; | |
1089 | i += push_opcode (&buf[i], "48 be"); /* movl <lockaddr>,%rsi */ | |
1090 | memcpy (&buf[i], (void *) &lockaddr, 8); | |
1091 | i += 8; | |
1092 | i += push_opcode (&buf[i], "48 89 e1"); /* mov %rsp,%rcx */ | |
1093 | i += push_opcode (&buf[i], "31 c0"); /* xor %eax,%eax */ | |
1094 | i += push_opcode (&buf[i], "f0 48 0f b1 0e"); /* lock cmpxchg %rcx,(%rsi) */ | |
1095 | i += push_opcode (&buf[i], "48 85 c0"); /* test %rax,%rax */ | |
1096 | i += push_opcode (&buf[i], "75 f4"); /* jne <again> */ | |
1097 | append_insns (&buildaddr, i, buf); | |
1098 | ||
1099 | /* Set up the gdb_collect call. */ | |
1100 | /* At this point, (stack pointer + 0x18) is the base of our saved | |
1101 | register block. */ | |
1102 | ||
1103 | i = 0; | |
1104 | i += push_opcode (&buf[i], "48 89 e6"); /* mov %rsp,%rsi */ | |
1105 | i += push_opcode (&buf[i], "48 83 c6 18"); /* add $0x18,%rsi */ | |
1106 | ||
1107 | /* tpoint address may be 64-bit wide. */ | |
1108 | i += push_opcode (&buf[i], "48 bf"); /* movl <addr>,%rdi */ | |
1109 | memcpy (buf + i, &tpoint, 8); | |
1110 | i += 8; | |
1111 | append_insns (&buildaddr, i, buf); | |
1112 | ||
1113 | /* The collector function being in the shared library, may be | |
1114 | >31-bits away off the jump pad. */ | |
1115 | i = 0; | |
1116 | i += push_opcode (&buf[i], "48 b8"); /* mov $collector,%rax */ | |
1117 | memcpy (buf + i, &collector, 8); | |
1118 | i += 8; | |
1119 | i += push_opcode (&buf[i], "ff d0"); /* callq *%rax */ | |
1120 | append_insns (&buildaddr, i, buf); | |
1121 | ||
1122 | /* Clear the spin-lock. */ | |
1123 | i = 0; | |
1124 | i += push_opcode (&buf[i], "31 c0"); /* xor %eax,%eax */ | |
1125 | i += push_opcode (&buf[i], "48 a3"); /* mov %rax, lockaddr */ | |
1126 | memcpy (buf + i, &lockaddr, 8); | |
1127 | i += 8; | |
1128 | append_insns (&buildaddr, i, buf); | |
1129 | ||
1130 | /* Remove stack that had been used for the collect_t object. */ | |
1131 | i = 0; | |
1132 | i += push_opcode (&buf[i], "48 83 c4 18"); /* add $0x18,%rsp */ | |
1133 | append_insns (&buildaddr, i, buf); | |
1134 | ||
1135 | /* Restore register state. */ | |
1136 | i = 0; | |
1137 | buf[i++] = 0x48; /* add $0x8,%rsp */ | |
1138 | buf[i++] = 0x83; | |
1139 | buf[i++] = 0xc4; | |
1140 | buf[i++] = 0x08; | |
1141 | buf[i++] = 0x9d; /* popfq */ | |
1142 | buf[i++] = 0x41; buf[i++] = 0x58; /* pop %r8 */ | |
1143 | buf[i++] = 0x41; buf[i++] = 0x59; /* pop %r9 */ | |
1144 | buf[i++] = 0x41; buf[i++] = 0x5a; /* pop %r10 */ | |
1145 | buf[i++] = 0x41; buf[i++] = 0x5b; /* pop %r11 */ | |
1146 | buf[i++] = 0x41; buf[i++] = 0x5c; /* pop %r12 */ | |
1147 | buf[i++] = 0x41; buf[i++] = 0x5d; /* pop %r13 */ | |
1148 | buf[i++] = 0x41; buf[i++] = 0x5e; /* pop %r14 */ | |
1149 | buf[i++] = 0x41; buf[i++] = 0x5f; /* pop %r15 */ | |
1150 | buf[i++] = 0x58; /* pop %rax */ | |
1151 | buf[i++] = 0x5b; /* pop %rbx */ | |
1152 | buf[i++] = 0x59; /* pop %rcx */ | |
1153 | buf[i++] = 0x5a; /* pop %rdx */ | |
1154 | buf[i++] = 0x5e; /* pop %rsi */ | |
1155 | buf[i++] = 0x5f; /* pop %rdi */ | |
1156 | buf[i++] = 0x5d; /* pop %rbp */ | |
1157 | buf[i++] = 0x5c; /* pop %rsp */ | |
1158 | append_insns (&buildaddr, i, buf); | |
1159 | ||
1160 | /* Now, adjust the original instruction to execute in the jump | |
1161 | pad. */ | |
1162 | *adjusted_insn_addr = buildaddr; | |
1163 | relocate_instruction (&buildaddr, tpaddr); | |
1164 | *adjusted_insn_addr_end = buildaddr; | |
1165 | ||
1166 | /* Finally, write a jump back to the program. */ | |
f4647387 YQ |
1167 | |
1168 | loffset = (tpaddr + orig_size) - (buildaddr + sizeof (jump_insn)); | |
1169 | if (loffset > INT_MAX || loffset < INT_MIN) | |
1170 | { | |
1171 | sprintf (err, | |
1172 | "E.Jump back from jump pad too far from tracepoint " | |
1173 | "(offset 0x%" PRIx64 " > int32).", loffset); | |
1174 | return 1; | |
1175 | } | |
1176 | ||
1177 | offset = (int) loffset; | |
fa593d66 PA |
1178 | memcpy (buf, jump_insn, sizeof (jump_insn)); |
1179 | memcpy (buf + 1, &offset, 4); | |
1180 | append_insns (&buildaddr, sizeof (jump_insn), buf); | |
1181 | ||
1182 | /* The jump pad is now built. Wire in a jump to our jump pad. This | |
1183 | is always done last (by our caller actually), so that we can | |
1184 | install fast tracepoints with threads running. This relies on | |
1185 | the agent's atomic write support. */ | |
f4647387 YQ |
1186 | loffset = *jump_entry - (tpaddr + sizeof (jump_insn)); |
1187 | if (loffset > INT_MAX || loffset < INT_MIN) | |
1188 | { | |
1189 | sprintf (err, | |
1190 | "E.Jump pad too far from tracepoint " | |
1191 | "(offset 0x%" PRIx64 " > int32).", loffset); | |
1192 | return 1; | |
1193 | } | |
1194 | ||
1195 | offset = (int) loffset; | |
1196 | ||
fa593d66 PA |
1197 | memcpy (buf, jump_insn, sizeof (jump_insn)); |
1198 | memcpy (buf + 1, &offset, 4); | |
1199 | memcpy (jjump_pad_insn, buf, sizeof (jump_insn)); | |
1200 | *jjump_pad_insn_size = sizeof (jump_insn); | |
1201 | ||
1202 | /* Return the end address of our pad. */ | |
1203 | *jump_entry = buildaddr; | |
1204 | ||
1205 | return 0; | |
1206 | } | |
1207 | ||
1208 | #endif /* __x86_64__ */ | |
1209 | ||
1210 | /* Build a jump pad that saves registers and calls a collection | |
1211 | function. Writes a jump instruction to the jump pad to | |
1212 | JJUMPAD_INSN. The caller is responsible to write it in at the | |
1213 | tracepoint address. */ | |
1214 | ||
1215 | static int | |
1216 | i386_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint, CORE_ADDR tpaddr, | |
1217 | CORE_ADDR collector, | |
1218 | CORE_ADDR lockaddr, | |
1219 | ULONGEST orig_size, | |
1220 | CORE_ADDR *jump_entry, | |
405f8e94 SS |
1221 | CORE_ADDR *trampoline, |
1222 | ULONGEST *trampoline_size, | |
fa593d66 PA |
1223 | unsigned char *jjump_pad_insn, |
1224 | ULONGEST *jjump_pad_insn_size, | |
1225 | CORE_ADDR *adjusted_insn_addr, | |
405f8e94 SS |
1226 | CORE_ADDR *adjusted_insn_addr_end, |
1227 | char *err) | |
fa593d66 PA |
1228 | { |
1229 | unsigned char buf[0x100]; | |
1230 | int i, offset; | |
1231 | CORE_ADDR buildaddr = *jump_entry; | |
1232 | ||
1233 | /* Build the jump pad. */ | |
1234 | ||
1235 | /* First, do tracepoint data collection. Save registers. */ | |
1236 | i = 0; | |
1237 | buf[i++] = 0x60; /* pushad */ | |
1238 | buf[i++] = 0x68; /* push tpaddr aka $pc */ | |
1239 | *((int *)(buf + i)) = (int) tpaddr; | |
1240 | i += 4; | |
1241 | buf[i++] = 0x9c; /* pushf */ | |
1242 | buf[i++] = 0x1e; /* push %ds */ | |
1243 | buf[i++] = 0x06; /* push %es */ | |
1244 | buf[i++] = 0x0f; /* push %fs */ | |
1245 | buf[i++] = 0xa0; | |
1246 | buf[i++] = 0x0f; /* push %gs */ | |
1247 | buf[i++] = 0xa8; | |
1248 | buf[i++] = 0x16; /* push %ss */ | |
1249 | buf[i++] = 0x0e; /* push %cs */ | |
1250 | append_insns (&buildaddr, i, buf); | |
1251 | ||
1252 | /* Stack space for the collecting_t object. */ | |
1253 | i = 0; | |
1254 | i += push_opcode (&buf[i], "83 ec 08"); /* sub $0x8,%esp */ | |
1255 | ||
1256 | /* Build the object. */ | |
1257 | i += push_opcode (&buf[i], "b8"); /* mov <tpoint>,%eax */ | |
1258 | memcpy (buf + i, &tpoint, 4); | |
1259 | i += 4; | |
1260 | i += push_opcode (&buf[i], "89 04 24"); /* mov %eax,(%esp) */ | |
1261 | ||
1262 | i += push_opcode (&buf[i], "65 a1 00 00 00 00"); /* mov %gs:0x0,%eax */ | |
1263 | i += push_opcode (&buf[i], "89 44 24 04"); /* mov %eax,0x4(%esp) */ | |
1264 | append_insns (&buildaddr, i, buf); | |
1265 | ||
1266 | /* spin-lock. Note this is using cmpxchg, which leaves i386 behind. | |
1267 | If we cared for it, this could be using xchg alternatively. */ | |
1268 | ||
1269 | i = 0; | |
1270 | i += push_opcode (&buf[i], "31 c0"); /* xor %eax,%eax */ | |
1271 | i += push_opcode (&buf[i], "f0 0f b1 25"); /* lock cmpxchg | |
1272 | %esp,<lockaddr> */ | |
1273 | memcpy (&buf[i], (void *) &lockaddr, 4); | |
1274 | i += 4; | |
1275 | i += push_opcode (&buf[i], "85 c0"); /* test %eax,%eax */ | |
1276 | i += push_opcode (&buf[i], "75 f2"); /* jne <again> */ | |
1277 | append_insns (&buildaddr, i, buf); | |
1278 | ||
1279 | ||
1280 | /* Set up arguments to the gdb_collect call. */ | |
1281 | i = 0; | |
1282 | i += push_opcode (&buf[i], "89 e0"); /* mov %esp,%eax */ | |
1283 | i += push_opcode (&buf[i], "83 c0 08"); /* add $0x08,%eax */ | |
1284 | i += push_opcode (&buf[i], "89 44 24 fc"); /* mov %eax,-0x4(%esp) */ | |
1285 | append_insns (&buildaddr, i, buf); | |
1286 | ||
1287 | i = 0; | |
1288 | i += push_opcode (&buf[i], "83 ec 08"); /* sub $0x8,%esp */ | |
1289 | append_insns (&buildaddr, i, buf); | |
1290 | ||
1291 | i = 0; | |
1292 | i += push_opcode (&buf[i], "c7 04 24"); /* movl <addr>,(%esp) */ | |
1293 | memcpy (&buf[i], (void *) &tpoint, 4); | |
1294 | i += 4; | |
1295 | append_insns (&buildaddr, i, buf); | |
1296 | ||
1297 | buf[0] = 0xe8; /* call <reladdr> */ | |
1298 | offset = collector - (buildaddr + sizeof (jump_insn)); | |
1299 | memcpy (buf + 1, &offset, 4); | |
1300 | append_insns (&buildaddr, 5, buf); | |
1301 | /* Clean up after the call. */ | |
1302 | buf[0] = 0x83; /* add $0x8,%esp */ | |
1303 | buf[1] = 0xc4; | |
1304 | buf[2] = 0x08; | |
1305 | append_insns (&buildaddr, 3, buf); | |
1306 | ||
1307 | ||
1308 | /* Clear the spin-lock. This would need the LOCK prefix on older | |
1309 | broken archs. */ | |
1310 | i = 0; | |
1311 | i += push_opcode (&buf[i], "31 c0"); /* xor %eax,%eax */ | |
1312 | i += push_opcode (&buf[i], "a3"); /* mov %eax, lockaddr */ | |
1313 | memcpy (buf + i, &lockaddr, 4); | |
1314 | i += 4; | |
1315 | append_insns (&buildaddr, i, buf); | |
1316 | ||
1317 | ||
1318 | /* Remove stack that had been used for the collect_t object. */ | |
1319 | i = 0; | |
1320 | i += push_opcode (&buf[i], "83 c4 08"); /* add $0x08,%esp */ | |
1321 | append_insns (&buildaddr, i, buf); | |
1322 | ||
1323 | i = 0; | |
1324 | buf[i++] = 0x83; /* add $0x4,%esp (no pop of %cs, assume unchanged) */ | |
1325 | buf[i++] = 0xc4; | |
1326 | buf[i++] = 0x04; | |
1327 | buf[i++] = 0x17; /* pop %ss */ | |
1328 | buf[i++] = 0x0f; /* pop %gs */ | |
1329 | buf[i++] = 0xa9; | |
1330 | buf[i++] = 0x0f; /* pop %fs */ | |
1331 | buf[i++] = 0xa1; | |
1332 | buf[i++] = 0x07; /* pop %es */ | |
405f8e94 | 1333 | buf[i++] = 0x1f; /* pop %ds */ |
fa593d66 PA |
1334 | buf[i++] = 0x9d; /* popf */ |
1335 | buf[i++] = 0x83; /* add $0x4,%esp (pop of tpaddr aka $pc) */ | |
1336 | buf[i++] = 0xc4; | |
1337 | buf[i++] = 0x04; | |
1338 | buf[i++] = 0x61; /* popad */ | |
1339 | append_insns (&buildaddr, i, buf); | |
1340 | ||
1341 | /* Now, adjust the original instruction to execute in the jump | |
1342 | pad. */ | |
1343 | *adjusted_insn_addr = buildaddr; | |
1344 | relocate_instruction (&buildaddr, tpaddr); | |
1345 | *adjusted_insn_addr_end = buildaddr; | |
1346 | ||
1347 | /* Write the jump back to the program. */ | |
1348 | offset = (tpaddr + orig_size) - (buildaddr + sizeof (jump_insn)); | |
1349 | memcpy (buf, jump_insn, sizeof (jump_insn)); | |
1350 | memcpy (buf + 1, &offset, 4); | |
1351 | append_insns (&buildaddr, sizeof (jump_insn), buf); | |
1352 | ||
1353 | /* The jump pad is now built. Wire in a jump to our jump pad. This | |
1354 | is always done last (by our caller actually), so that we can | |
1355 | install fast tracepoints with threads running. This relies on | |
1356 | the agent's atomic write support. */ | |
405f8e94 SS |
1357 | if (orig_size == 4) |
1358 | { | |
1359 | /* Create a trampoline. */ | |
1360 | *trampoline_size = sizeof (jump_insn); | |
1361 | if (!claim_trampoline_space (*trampoline_size, trampoline)) | |
1362 | { | |
1363 | /* No trampoline space available. */ | |
1364 | strcpy (err, | |
1365 | "E.Cannot allocate trampoline space needed for fast " | |
1366 | "tracepoints on 4-byte instructions."); | |
1367 | return 1; | |
1368 | } | |
1369 | ||
1370 | offset = *jump_entry - (*trampoline + sizeof (jump_insn)); | |
1371 | memcpy (buf, jump_insn, sizeof (jump_insn)); | |
1372 | memcpy (buf + 1, &offset, 4); | |
1373 | write_inferior_memory (*trampoline, buf, sizeof (jump_insn)); | |
1374 | ||
1375 | /* Use a 16-bit relative jump instruction to jump to the trampoline. */ | |
1376 | offset = (*trampoline - (tpaddr + sizeof (small_jump_insn))) & 0xffff; | |
1377 | memcpy (buf, small_jump_insn, sizeof (small_jump_insn)); | |
1378 | memcpy (buf + 2, &offset, 2); | |
1379 | memcpy (jjump_pad_insn, buf, sizeof (small_jump_insn)); | |
1380 | *jjump_pad_insn_size = sizeof (small_jump_insn); | |
1381 | } | |
1382 | else | |
1383 | { | |
1384 | /* Else use a 32-bit relative jump instruction. */ | |
1385 | offset = *jump_entry - (tpaddr + sizeof (jump_insn)); | |
1386 | memcpy (buf, jump_insn, sizeof (jump_insn)); | |
1387 | memcpy (buf + 1, &offset, 4); | |
1388 | memcpy (jjump_pad_insn, buf, sizeof (jump_insn)); | |
1389 | *jjump_pad_insn_size = sizeof (jump_insn); | |
1390 | } | |
fa593d66 PA |
1391 | |
1392 | /* Return the end address of our pad. */ | |
1393 | *jump_entry = buildaddr; | |
1394 | ||
1395 | return 0; | |
1396 | } | |
1397 | ||
1398 | static int | |
1399 | x86_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint, CORE_ADDR tpaddr, | |
1400 | CORE_ADDR collector, | |
1401 | CORE_ADDR lockaddr, | |
1402 | ULONGEST orig_size, | |
1403 | CORE_ADDR *jump_entry, | |
405f8e94 SS |
1404 | CORE_ADDR *trampoline, |
1405 | ULONGEST *trampoline_size, | |
fa593d66 PA |
1406 | unsigned char *jjump_pad_insn, |
1407 | ULONGEST *jjump_pad_insn_size, | |
1408 | CORE_ADDR *adjusted_insn_addr, | |
405f8e94 SS |
1409 | CORE_ADDR *adjusted_insn_addr_end, |
1410 | char *err) | |
fa593d66 PA |
1411 | { |
1412 | #ifdef __x86_64__ | |
3aee8918 | 1413 | if (is_64bit_tdesc ()) |
fa593d66 PA |
1414 | return amd64_install_fast_tracepoint_jump_pad (tpoint, tpaddr, |
1415 | collector, lockaddr, | |
1416 | orig_size, jump_entry, | |
405f8e94 | 1417 | trampoline, trampoline_size, |
fa593d66 PA |
1418 | jjump_pad_insn, |
1419 | jjump_pad_insn_size, | |
1420 | adjusted_insn_addr, | |
405f8e94 SS |
1421 | adjusted_insn_addr_end, |
1422 | err); | |
fa593d66 PA |
1423 | #endif |
1424 | ||
1425 | return i386_install_fast_tracepoint_jump_pad (tpoint, tpaddr, | |
1426 | collector, lockaddr, | |
1427 | orig_size, jump_entry, | |
405f8e94 | 1428 | trampoline, trampoline_size, |
fa593d66 PA |
1429 | jjump_pad_insn, |
1430 | jjump_pad_insn_size, | |
1431 | adjusted_insn_addr, | |
405f8e94 SS |
1432 | adjusted_insn_addr_end, |
1433 | err); | |
1434 | } | |
1435 | ||
1436 | /* Return the minimum instruction length for fast tracepoints on x86/x86-64 | |
1437 | architectures. */ | |
1438 | ||
1439 | static int | |
1440 | x86_get_min_fast_tracepoint_insn_len (void) | |
1441 | { | |
1442 | static int warned_about_fast_tracepoints = 0; | |
1443 | ||
1444 | #ifdef __x86_64__ | |
1445 | /* On x86-64, 5-byte jump instructions with a 4-byte offset are always | |
1446 | used for fast tracepoints. */ | |
3aee8918 | 1447 | if (is_64bit_tdesc ()) |
405f8e94 SS |
1448 | return 5; |
1449 | #endif | |
1450 | ||
58b4daa5 | 1451 | if (agent_loaded_p ()) |
405f8e94 SS |
1452 | { |
1453 | char errbuf[IPA_BUFSIZ]; | |
1454 | ||
1455 | errbuf[0] = '\0'; | |
1456 | ||
1457 | /* On x86, if trampolines are available, then 4-byte jump instructions | |
1458 | with a 2-byte offset may be used, otherwise 5-byte jump instructions | |
1459 | with a 4-byte offset are used instead. */ | |
1460 | if (have_fast_tracepoint_trampoline_buffer (errbuf)) | |
1461 | return 4; | |
1462 | else | |
1463 | { | |
1464 | /* GDB has no channel to explain to user why a shorter fast | |
1465 | tracepoint is not possible, but at least make GDBserver | |
1466 | mention that something has gone awry. */ | |
1467 | if (!warned_about_fast_tracepoints) | |
1468 | { | |
1469 | warning ("4-byte fast tracepoints not available; %s\n", errbuf); | |
1470 | warned_about_fast_tracepoints = 1; | |
1471 | } | |
1472 | return 5; | |
1473 | } | |
1474 | } | |
1475 | else | |
1476 | { | |
1477 | /* Indicate that the minimum length is currently unknown since the IPA | |
1478 | has not loaded yet. */ | |
1479 | return 0; | |
1480 | } | |
fa593d66 PA |
1481 | } |
1482 | ||
6a271cae PA |
1483 | static void |
1484 | add_insns (unsigned char *start, int len) | |
1485 | { | |
1486 | CORE_ADDR buildaddr = current_insn_ptr; | |
1487 | ||
1488 | if (debug_threads) | |
87ce2a04 DE |
1489 | debug_printf ("Adding %d bytes of insn at %s\n", |
1490 | len, paddress (buildaddr)); | |
6a271cae PA |
1491 | |
1492 | append_insns (&buildaddr, len, start); | |
1493 | current_insn_ptr = buildaddr; | |
1494 | } | |
1495 | ||
6a271cae PA |
1496 | /* Our general strategy for emitting code is to avoid specifying raw |
1497 | bytes whenever possible, and instead copy a block of inline asm | |
1498 | that is embedded in the function. This is a little messy, because | |
1499 | we need to keep the compiler from discarding what looks like dead | |
1500 | code, plus suppress various warnings. */ | |
1501 | ||
9e4344e5 PA |
1502 | #define EMIT_ASM(NAME, INSNS) \ |
1503 | do \ | |
1504 | { \ | |
1505 | extern unsigned char start_ ## NAME, end_ ## NAME; \ | |
1506 | add_insns (&start_ ## NAME, &end_ ## NAME - &start_ ## NAME); \ | |
493e2a69 | 1507 | __asm__ ("jmp end_" #NAME "\n" \ |
9e4344e5 PA |
1508 | "\t" "start_" #NAME ":" \ |
1509 | "\t" INSNS "\n" \ | |
1510 | "\t" "end_" #NAME ":"); \ | |
1511 | } while (0) | |
6a271cae PA |
1512 | |
1513 | #ifdef __x86_64__ | |
1514 | ||
1515 | #define EMIT_ASM32(NAME,INSNS) \ | |
9e4344e5 PA |
1516 | do \ |
1517 | { \ | |
1518 | extern unsigned char start_ ## NAME, end_ ## NAME; \ | |
1519 | add_insns (&start_ ## NAME, &end_ ## NAME - &start_ ## NAME); \ | |
1520 | __asm__ (".code32\n" \ | |
1521 | "\t" "jmp end_" #NAME "\n" \ | |
1522 | "\t" "start_" #NAME ":\n" \ | |
1523 | "\t" INSNS "\n" \ | |
1524 | "\t" "end_" #NAME ":\n" \ | |
1525 | ".code64\n"); \ | |
1526 | } while (0) | |
6a271cae PA |
1527 | |
1528 | #else | |
1529 | ||
1530 | #define EMIT_ASM32(NAME,INSNS) EMIT_ASM(NAME,INSNS) | |
1531 | ||
1532 | #endif | |
1533 | ||
1534 | #ifdef __x86_64__ | |
1535 | ||
1536 | static void | |
1537 | amd64_emit_prologue (void) | |
1538 | { | |
1539 | EMIT_ASM (amd64_prologue, | |
1540 | "pushq %rbp\n\t" | |
1541 | "movq %rsp,%rbp\n\t" | |
1542 | "sub $0x20,%rsp\n\t" | |
1543 | "movq %rdi,-8(%rbp)\n\t" | |
1544 | "movq %rsi,-16(%rbp)"); | |
1545 | } | |
1546 | ||
1547 | ||
1548 | static void | |
1549 | amd64_emit_epilogue (void) | |
1550 | { | |
1551 | EMIT_ASM (amd64_epilogue, | |
1552 | "movq -16(%rbp),%rdi\n\t" | |
1553 | "movq %rax,(%rdi)\n\t" | |
1554 | "xor %rax,%rax\n\t" | |
1555 | "leave\n\t" | |
1556 | "ret"); | |
1557 | } | |
1558 | ||
1559 | static void | |
1560 | amd64_emit_add (void) | |
1561 | { | |
1562 | EMIT_ASM (amd64_add, | |
1563 | "add (%rsp),%rax\n\t" | |
1564 | "lea 0x8(%rsp),%rsp"); | |
1565 | } | |
1566 | ||
1567 | static void | |
1568 | amd64_emit_sub (void) | |
1569 | { | |
1570 | EMIT_ASM (amd64_sub, | |
1571 | "sub %rax,(%rsp)\n\t" | |
1572 | "pop %rax"); | |
1573 | } | |
1574 | ||
1575 | static void | |
1576 | amd64_emit_mul (void) | |
1577 | { | |
1578 | emit_error = 1; | |
1579 | } | |
1580 | ||
1581 | static void | |
1582 | amd64_emit_lsh (void) | |
1583 | { | |
1584 | emit_error = 1; | |
1585 | } | |
1586 | ||
1587 | static void | |
1588 | amd64_emit_rsh_signed (void) | |
1589 | { | |
1590 | emit_error = 1; | |
1591 | } | |
1592 | ||
1593 | static void | |
1594 | amd64_emit_rsh_unsigned (void) | |
1595 | { | |
1596 | emit_error = 1; | |
1597 | } | |
1598 | ||
1599 | static void | |
1600 | amd64_emit_ext (int arg) | |
1601 | { | |
1602 | switch (arg) | |
1603 | { | |
1604 | case 8: | |
1605 | EMIT_ASM (amd64_ext_8, | |
1606 | "cbtw\n\t" | |
1607 | "cwtl\n\t" | |
1608 | "cltq"); | |
1609 | break; | |
1610 | case 16: | |
1611 | EMIT_ASM (amd64_ext_16, | |
1612 | "cwtl\n\t" | |
1613 | "cltq"); | |
1614 | break; | |
1615 | case 32: | |
1616 | EMIT_ASM (amd64_ext_32, | |
1617 | "cltq"); | |
1618 | break; | |
1619 | default: | |
1620 | emit_error = 1; | |
1621 | } | |
1622 | } | |
1623 | ||
1624 | static void | |
1625 | amd64_emit_log_not (void) | |
1626 | { | |
1627 | EMIT_ASM (amd64_log_not, | |
1628 | "test %rax,%rax\n\t" | |
1629 | "sete %cl\n\t" | |
1630 | "movzbq %cl,%rax"); | |
1631 | } | |
1632 | ||
1633 | static void | |
1634 | amd64_emit_bit_and (void) | |
1635 | { | |
1636 | EMIT_ASM (amd64_and, | |
1637 | "and (%rsp),%rax\n\t" | |
1638 | "lea 0x8(%rsp),%rsp"); | |
1639 | } | |
1640 | ||
1641 | static void | |
1642 | amd64_emit_bit_or (void) | |
1643 | { | |
1644 | EMIT_ASM (amd64_or, | |
1645 | "or (%rsp),%rax\n\t" | |
1646 | "lea 0x8(%rsp),%rsp"); | |
1647 | } | |
1648 | ||
1649 | static void | |
1650 | amd64_emit_bit_xor (void) | |
1651 | { | |
1652 | EMIT_ASM (amd64_xor, | |
1653 | "xor (%rsp),%rax\n\t" | |
1654 | "lea 0x8(%rsp),%rsp"); | |
1655 | } | |
1656 | ||
1657 | static void | |
1658 | amd64_emit_bit_not (void) | |
1659 | { | |
1660 | EMIT_ASM (amd64_bit_not, | |
1661 | "xorq $0xffffffffffffffff,%rax"); | |
1662 | } | |
1663 | ||
1664 | static void | |
1665 | amd64_emit_equal (void) | |
1666 | { | |
1667 | EMIT_ASM (amd64_equal, | |
1668 | "cmp %rax,(%rsp)\n\t" | |
1669 | "je .Lamd64_equal_true\n\t" | |
1670 | "xor %rax,%rax\n\t" | |
1671 | "jmp .Lamd64_equal_end\n\t" | |
1672 | ".Lamd64_equal_true:\n\t" | |
1673 | "mov $0x1,%rax\n\t" | |
1674 | ".Lamd64_equal_end:\n\t" | |
1675 | "lea 0x8(%rsp),%rsp"); | |
1676 | } | |
1677 | ||
1678 | static void | |
1679 | amd64_emit_less_signed (void) | |
1680 | { | |
1681 | EMIT_ASM (amd64_less_signed, | |
1682 | "cmp %rax,(%rsp)\n\t" | |
1683 | "jl .Lamd64_less_signed_true\n\t" | |
1684 | "xor %rax,%rax\n\t" | |
1685 | "jmp .Lamd64_less_signed_end\n\t" | |
1686 | ".Lamd64_less_signed_true:\n\t" | |
1687 | "mov $1,%rax\n\t" | |
1688 | ".Lamd64_less_signed_end:\n\t" | |
1689 | "lea 0x8(%rsp),%rsp"); | |
1690 | } | |
1691 | ||
1692 | static void | |
1693 | amd64_emit_less_unsigned (void) | |
1694 | { | |
1695 | EMIT_ASM (amd64_less_unsigned, | |
1696 | "cmp %rax,(%rsp)\n\t" | |
1697 | "jb .Lamd64_less_unsigned_true\n\t" | |
1698 | "xor %rax,%rax\n\t" | |
1699 | "jmp .Lamd64_less_unsigned_end\n\t" | |
1700 | ".Lamd64_less_unsigned_true:\n\t" | |
1701 | "mov $1,%rax\n\t" | |
1702 | ".Lamd64_less_unsigned_end:\n\t" | |
1703 | "lea 0x8(%rsp),%rsp"); | |
1704 | } | |
1705 | ||
1706 | static void | |
1707 | amd64_emit_ref (int size) | |
1708 | { | |
1709 | switch (size) | |
1710 | { | |
1711 | case 1: | |
1712 | EMIT_ASM (amd64_ref1, | |
1713 | "movb (%rax),%al"); | |
1714 | break; | |
1715 | case 2: | |
1716 | EMIT_ASM (amd64_ref2, | |
1717 | "movw (%rax),%ax"); | |
1718 | break; | |
1719 | case 4: | |
1720 | EMIT_ASM (amd64_ref4, | |
1721 | "movl (%rax),%eax"); | |
1722 | break; | |
1723 | case 8: | |
1724 | EMIT_ASM (amd64_ref8, | |
1725 | "movq (%rax),%rax"); | |
1726 | break; | |
1727 | } | |
1728 | } | |
1729 | ||
1730 | static void | |
1731 | amd64_emit_if_goto (int *offset_p, int *size_p) | |
1732 | { | |
1733 | EMIT_ASM (amd64_if_goto, | |
1734 | "mov %rax,%rcx\n\t" | |
1735 | "pop %rax\n\t" | |
1736 | "cmp $0,%rcx\n\t" | |
1737 | ".byte 0x0f, 0x85, 0x0, 0x0, 0x0, 0x0"); | |
1738 | if (offset_p) | |
1739 | *offset_p = 10; | |
1740 | if (size_p) | |
1741 | *size_p = 4; | |
1742 | } | |
1743 | ||
1744 | static void | |
1745 | amd64_emit_goto (int *offset_p, int *size_p) | |
1746 | { | |
1747 | EMIT_ASM (amd64_goto, | |
1748 | ".byte 0xe9, 0x0, 0x0, 0x0, 0x0"); | |
1749 | if (offset_p) | |
1750 | *offset_p = 1; | |
1751 | if (size_p) | |
1752 | *size_p = 4; | |
1753 | } | |
1754 | ||
1755 | static void | |
1756 | amd64_write_goto_address (CORE_ADDR from, CORE_ADDR to, int size) | |
1757 | { | |
1758 | int diff = (to - (from + size)); | |
1759 | unsigned char buf[sizeof (int)]; | |
1760 | ||
1761 | if (size != 4) | |
1762 | { | |
1763 | emit_error = 1; | |
1764 | return; | |
1765 | } | |
1766 | ||
1767 | memcpy (buf, &diff, sizeof (int)); | |
1768 | write_inferior_memory (from, buf, sizeof (int)); | |
1769 | } | |
1770 | ||
1771 | static void | |
4e29fb54 | 1772 | amd64_emit_const (LONGEST num) |
6a271cae PA |
1773 | { |
1774 | unsigned char buf[16]; | |
1775 | int i; | |
1776 | CORE_ADDR buildaddr = current_insn_ptr; | |
1777 | ||
1778 | i = 0; | |
1779 | buf[i++] = 0x48; buf[i++] = 0xb8; /* mov $<n>,%rax */ | |
b00ad6ff | 1780 | memcpy (&buf[i], &num, sizeof (num)); |
6a271cae PA |
1781 | i += 8; |
1782 | append_insns (&buildaddr, i, buf); | |
1783 | current_insn_ptr = buildaddr; | |
1784 | } | |
1785 | ||
1786 | static void | |
1787 | amd64_emit_call (CORE_ADDR fn) | |
1788 | { | |
1789 | unsigned char buf[16]; | |
1790 | int i; | |
1791 | CORE_ADDR buildaddr; | |
4e29fb54 | 1792 | LONGEST offset64; |
6a271cae PA |
1793 | |
1794 | /* The destination function being in the shared library, may be | |
1795 | >31-bits away off the compiled code pad. */ | |
1796 | ||
1797 | buildaddr = current_insn_ptr; | |
1798 | ||
1799 | offset64 = fn - (buildaddr + 1 /* call op */ + 4 /* 32-bit offset */); | |
1800 | ||
1801 | i = 0; | |
1802 | ||
1803 | if (offset64 > INT_MAX || offset64 < INT_MIN) | |
1804 | { | |
1805 | /* Offset is too large for a call. Use callq, but that requires | |
1806 | a register, so avoid it if possible. Use r10, since it is | |
1807 | call-clobbered, we don't have to push/pop it. */ | |
1808 | buf[i++] = 0x48; /* mov $fn,%r10 */ | |
1809 | buf[i++] = 0xba; | |
1810 | memcpy (buf + i, &fn, 8); | |
1811 | i += 8; | |
1812 | buf[i++] = 0xff; /* callq *%r10 */ | |
1813 | buf[i++] = 0xd2; | |
1814 | } | |
1815 | else | |
1816 | { | |
1817 | int offset32 = offset64; /* we know we can't overflow here. */ | |
ed036b40 PA |
1818 | |
1819 | buf[i++] = 0xe8; /* call <reladdr> */ | |
6a271cae PA |
1820 | memcpy (buf + i, &offset32, 4); |
1821 | i += 4; | |
1822 | } | |
1823 | ||
1824 | append_insns (&buildaddr, i, buf); | |
1825 | current_insn_ptr = buildaddr; | |
1826 | } | |
1827 | ||
1828 | static void | |
1829 | amd64_emit_reg (int reg) | |
1830 | { | |
1831 | unsigned char buf[16]; | |
1832 | int i; | |
1833 | CORE_ADDR buildaddr; | |
1834 | ||
1835 | /* Assume raw_regs is still in %rdi. */ | |
1836 | buildaddr = current_insn_ptr; | |
1837 | i = 0; | |
1838 | buf[i++] = 0xbe; /* mov $<n>,%esi */ | |
b00ad6ff | 1839 | memcpy (&buf[i], ®, sizeof (reg)); |
6a271cae PA |
1840 | i += 4; |
1841 | append_insns (&buildaddr, i, buf); | |
1842 | current_insn_ptr = buildaddr; | |
1843 | amd64_emit_call (get_raw_reg_func_addr ()); | |
1844 | } | |
1845 | ||
1846 | static void | |
1847 | amd64_emit_pop (void) | |
1848 | { | |
1849 | EMIT_ASM (amd64_pop, | |
1850 | "pop %rax"); | |
1851 | } | |
1852 | ||
1853 | static void | |
1854 | amd64_emit_stack_flush (void) | |
1855 | { | |
1856 | EMIT_ASM (amd64_stack_flush, | |
1857 | "push %rax"); | |
1858 | } | |
1859 | ||
1860 | static void | |
1861 | amd64_emit_zero_ext (int arg) | |
1862 | { | |
1863 | switch (arg) | |
1864 | { | |
1865 | case 8: | |
1866 | EMIT_ASM (amd64_zero_ext_8, | |
1867 | "and $0xff,%rax"); | |
1868 | break; | |
1869 | case 16: | |
1870 | EMIT_ASM (amd64_zero_ext_16, | |
1871 | "and $0xffff,%rax"); | |
1872 | break; | |
1873 | case 32: | |
1874 | EMIT_ASM (amd64_zero_ext_32, | |
1875 | "mov $0xffffffff,%rcx\n\t" | |
1876 | "and %rcx,%rax"); | |
1877 | break; | |
1878 | default: | |
1879 | emit_error = 1; | |
1880 | } | |
1881 | } | |
1882 | ||
1883 | static void | |
1884 | amd64_emit_swap (void) | |
1885 | { | |
1886 | EMIT_ASM (amd64_swap, | |
1887 | "mov %rax,%rcx\n\t" | |
1888 | "pop %rax\n\t" | |
1889 | "push %rcx"); | |
1890 | } | |
1891 | ||
1892 | static void | |
1893 | amd64_emit_stack_adjust (int n) | |
1894 | { | |
1895 | unsigned char buf[16]; | |
1896 | int i; | |
1897 | CORE_ADDR buildaddr = current_insn_ptr; | |
1898 | ||
1899 | i = 0; | |
1900 | buf[i++] = 0x48; /* lea $<n>(%rsp),%rsp */ | |
1901 | buf[i++] = 0x8d; | |
1902 | buf[i++] = 0x64; | |
1903 | buf[i++] = 0x24; | |
1904 | /* This only handles adjustments up to 16, but we don't expect any more. */ | |
1905 | buf[i++] = n * 8; | |
1906 | append_insns (&buildaddr, i, buf); | |
1907 | current_insn_ptr = buildaddr; | |
1908 | } | |
1909 | ||
1910 | /* FN's prototype is `LONGEST(*fn)(int)'. */ | |
1911 | ||
1912 | static void | |
1913 | amd64_emit_int_call_1 (CORE_ADDR fn, int arg1) | |
1914 | { | |
1915 | unsigned char buf[16]; | |
1916 | int i; | |
1917 | CORE_ADDR buildaddr; | |
1918 | ||
1919 | buildaddr = current_insn_ptr; | |
1920 | i = 0; | |
1921 | buf[i++] = 0xbf; /* movl $<n>,%edi */ | |
b00ad6ff | 1922 | memcpy (&buf[i], &arg1, sizeof (arg1)); |
6a271cae PA |
1923 | i += 4; |
1924 | append_insns (&buildaddr, i, buf); | |
1925 | current_insn_ptr = buildaddr; | |
1926 | amd64_emit_call (fn); | |
1927 | } | |
1928 | ||
4e29fb54 | 1929 | /* FN's prototype is `void(*fn)(int,LONGEST)'. */ |
6a271cae PA |
1930 | |
1931 | static void | |
1932 | amd64_emit_void_call_2 (CORE_ADDR fn, int arg1) | |
1933 | { | |
1934 | unsigned char buf[16]; | |
1935 | int i; | |
1936 | CORE_ADDR buildaddr; | |
1937 | ||
1938 | buildaddr = current_insn_ptr; | |
1939 | i = 0; | |
1940 | buf[i++] = 0xbf; /* movl $<n>,%edi */ | |
b00ad6ff | 1941 | memcpy (&buf[i], &arg1, sizeof (arg1)); |
6a271cae PA |
1942 | i += 4; |
1943 | append_insns (&buildaddr, i, buf); | |
1944 | current_insn_ptr = buildaddr; | |
1945 | EMIT_ASM (amd64_void_call_2_a, | |
1946 | /* Save away a copy of the stack top. */ | |
1947 | "push %rax\n\t" | |
1948 | /* Also pass top as the second argument. */ | |
1949 | "mov %rax,%rsi"); | |
1950 | amd64_emit_call (fn); | |
1951 | EMIT_ASM (amd64_void_call_2_b, | |
1952 | /* Restore the stack top, %rax may have been trashed. */ | |
1953 | "pop %rax"); | |
1954 | } | |
1955 | ||
6b9801d4 SS |
1956 | void |
1957 | amd64_emit_eq_goto (int *offset_p, int *size_p) | |
1958 | { | |
1959 | EMIT_ASM (amd64_eq, | |
1960 | "cmp %rax,(%rsp)\n\t" | |
1961 | "jne .Lamd64_eq_fallthru\n\t" | |
1962 | "lea 0x8(%rsp),%rsp\n\t" | |
1963 | "pop %rax\n\t" | |
1964 | /* jmp, but don't trust the assembler to choose the right jump */ | |
1965 | ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t" | |
1966 | ".Lamd64_eq_fallthru:\n\t" | |
1967 | "lea 0x8(%rsp),%rsp\n\t" | |
1968 | "pop %rax"); | |
1969 | ||
1970 | if (offset_p) | |
1971 | *offset_p = 13; | |
1972 | if (size_p) | |
1973 | *size_p = 4; | |
1974 | } | |
1975 | ||
1976 | void | |
1977 | amd64_emit_ne_goto (int *offset_p, int *size_p) | |
1978 | { | |
1979 | EMIT_ASM (amd64_ne, | |
1980 | "cmp %rax,(%rsp)\n\t" | |
1981 | "je .Lamd64_ne_fallthru\n\t" | |
1982 | "lea 0x8(%rsp),%rsp\n\t" | |
1983 | "pop %rax\n\t" | |
1984 | /* jmp, but don't trust the assembler to choose the right jump */ | |
1985 | ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t" | |
1986 | ".Lamd64_ne_fallthru:\n\t" | |
1987 | "lea 0x8(%rsp),%rsp\n\t" | |
1988 | "pop %rax"); | |
1989 | ||
1990 | if (offset_p) | |
1991 | *offset_p = 13; | |
1992 | if (size_p) | |
1993 | *size_p = 4; | |
1994 | } | |
1995 | ||
1996 | void | |
1997 | amd64_emit_lt_goto (int *offset_p, int *size_p) | |
1998 | { | |
1999 | EMIT_ASM (amd64_lt, | |
2000 | "cmp %rax,(%rsp)\n\t" | |
2001 | "jnl .Lamd64_lt_fallthru\n\t" | |
2002 | "lea 0x8(%rsp),%rsp\n\t" | |
2003 | "pop %rax\n\t" | |
2004 | /* jmp, but don't trust the assembler to choose the right jump */ | |
2005 | ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t" | |
2006 | ".Lamd64_lt_fallthru:\n\t" | |
2007 | "lea 0x8(%rsp),%rsp\n\t" | |
2008 | "pop %rax"); | |
2009 | ||
2010 | if (offset_p) | |
2011 | *offset_p = 13; | |
2012 | if (size_p) | |
2013 | *size_p = 4; | |
2014 | } | |
2015 | ||
2016 | void | |
2017 | amd64_emit_le_goto (int *offset_p, int *size_p) | |
2018 | { | |
2019 | EMIT_ASM (amd64_le, | |
2020 | "cmp %rax,(%rsp)\n\t" | |
2021 | "jnle .Lamd64_le_fallthru\n\t" | |
2022 | "lea 0x8(%rsp),%rsp\n\t" | |
2023 | "pop %rax\n\t" | |
2024 | /* jmp, but don't trust the assembler to choose the right jump */ | |
2025 | ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t" | |
2026 | ".Lamd64_le_fallthru:\n\t" | |
2027 | "lea 0x8(%rsp),%rsp\n\t" | |
2028 | "pop %rax"); | |
2029 | ||
2030 | if (offset_p) | |
2031 | *offset_p = 13; | |
2032 | if (size_p) | |
2033 | *size_p = 4; | |
2034 | } | |
2035 | ||
2036 | void | |
2037 | amd64_emit_gt_goto (int *offset_p, int *size_p) | |
2038 | { | |
2039 | EMIT_ASM (amd64_gt, | |
2040 | "cmp %rax,(%rsp)\n\t" | |
2041 | "jng .Lamd64_gt_fallthru\n\t" | |
2042 | "lea 0x8(%rsp),%rsp\n\t" | |
2043 | "pop %rax\n\t" | |
2044 | /* jmp, but don't trust the assembler to choose the right jump */ | |
2045 | ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t" | |
2046 | ".Lamd64_gt_fallthru:\n\t" | |
2047 | "lea 0x8(%rsp),%rsp\n\t" | |
2048 | "pop %rax"); | |
2049 | ||
2050 | if (offset_p) | |
2051 | *offset_p = 13; | |
2052 | if (size_p) | |
2053 | *size_p = 4; | |
2054 | } | |
2055 | ||
2056 | void | |
2057 | amd64_emit_ge_goto (int *offset_p, int *size_p) | |
2058 | { | |
2059 | EMIT_ASM (amd64_ge, | |
2060 | "cmp %rax,(%rsp)\n\t" | |
2061 | "jnge .Lamd64_ge_fallthru\n\t" | |
2062 | ".Lamd64_ge_jump:\n\t" | |
2063 | "lea 0x8(%rsp),%rsp\n\t" | |
2064 | "pop %rax\n\t" | |
2065 | /* jmp, but don't trust the assembler to choose the right jump */ | |
2066 | ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t" | |
2067 | ".Lamd64_ge_fallthru:\n\t" | |
2068 | "lea 0x8(%rsp),%rsp\n\t" | |
2069 | "pop %rax"); | |
2070 | ||
2071 | if (offset_p) | |
2072 | *offset_p = 13; | |
2073 | if (size_p) | |
2074 | *size_p = 4; | |
2075 | } | |
2076 | ||
6a271cae PA |
2077 | struct emit_ops amd64_emit_ops = |
2078 | { | |
2079 | amd64_emit_prologue, | |
2080 | amd64_emit_epilogue, | |
2081 | amd64_emit_add, | |
2082 | amd64_emit_sub, | |
2083 | amd64_emit_mul, | |
2084 | amd64_emit_lsh, | |
2085 | amd64_emit_rsh_signed, | |
2086 | amd64_emit_rsh_unsigned, | |
2087 | amd64_emit_ext, | |
2088 | amd64_emit_log_not, | |
2089 | amd64_emit_bit_and, | |
2090 | amd64_emit_bit_or, | |
2091 | amd64_emit_bit_xor, | |
2092 | amd64_emit_bit_not, | |
2093 | amd64_emit_equal, | |
2094 | amd64_emit_less_signed, | |
2095 | amd64_emit_less_unsigned, | |
2096 | amd64_emit_ref, | |
2097 | amd64_emit_if_goto, | |
2098 | amd64_emit_goto, | |
2099 | amd64_write_goto_address, | |
2100 | amd64_emit_const, | |
2101 | amd64_emit_call, | |
2102 | amd64_emit_reg, | |
2103 | amd64_emit_pop, | |
2104 | amd64_emit_stack_flush, | |
2105 | amd64_emit_zero_ext, | |
2106 | amd64_emit_swap, | |
2107 | amd64_emit_stack_adjust, | |
2108 | amd64_emit_int_call_1, | |
6b9801d4 SS |
2109 | amd64_emit_void_call_2, |
2110 | amd64_emit_eq_goto, | |
2111 | amd64_emit_ne_goto, | |
2112 | amd64_emit_lt_goto, | |
2113 | amd64_emit_le_goto, | |
2114 | amd64_emit_gt_goto, | |
2115 | amd64_emit_ge_goto | |
6a271cae PA |
2116 | }; |
2117 | ||
2118 | #endif /* __x86_64__ */ | |
2119 | ||
2120 | static void | |
2121 | i386_emit_prologue (void) | |
2122 | { | |
2123 | EMIT_ASM32 (i386_prologue, | |
2124 | "push %ebp\n\t" | |
bf15cbda SS |
2125 | "mov %esp,%ebp\n\t" |
2126 | "push %ebx"); | |
6a271cae PA |
2127 | /* At this point, the raw regs base address is at 8(%ebp), and the |
2128 | value pointer is at 12(%ebp). */ | |
2129 | } | |
2130 | ||
2131 | static void | |
2132 | i386_emit_epilogue (void) | |
2133 | { | |
2134 | EMIT_ASM32 (i386_epilogue, | |
2135 | "mov 12(%ebp),%ecx\n\t" | |
2136 | "mov %eax,(%ecx)\n\t" | |
2137 | "mov %ebx,0x4(%ecx)\n\t" | |
2138 | "xor %eax,%eax\n\t" | |
bf15cbda | 2139 | "pop %ebx\n\t" |
6a271cae PA |
2140 | "pop %ebp\n\t" |
2141 | "ret"); | |
2142 | } | |
2143 | ||
2144 | static void | |
2145 | i386_emit_add (void) | |
2146 | { | |
2147 | EMIT_ASM32 (i386_add, | |
2148 | "add (%esp),%eax\n\t" | |
2149 | "adc 0x4(%esp),%ebx\n\t" | |
2150 | "lea 0x8(%esp),%esp"); | |
2151 | } | |
2152 | ||
2153 | static void | |
2154 | i386_emit_sub (void) | |
2155 | { | |
2156 | EMIT_ASM32 (i386_sub, | |
2157 | "subl %eax,(%esp)\n\t" | |
2158 | "sbbl %ebx,4(%esp)\n\t" | |
2159 | "pop %eax\n\t" | |
2160 | "pop %ebx\n\t"); | |
2161 | } | |
2162 | ||
2163 | static void | |
2164 | i386_emit_mul (void) | |
2165 | { | |
2166 | emit_error = 1; | |
2167 | } | |
2168 | ||
2169 | static void | |
2170 | i386_emit_lsh (void) | |
2171 | { | |
2172 | emit_error = 1; | |
2173 | } | |
2174 | ||
2175 | static void | |
2176 | i386_emit_rsh_signed (void) | |
2177 | { | |
2178 | emit_error = 1; | |
2179 | } | |
2180 | ||
2181 | static void | |
2182 | i386_emit_rsh_unsigned (void) | |
2183 | { | |
2184 | emit_error = 1; | |
2185 | } | |
2186 | ||
2187 | static void | |
2188 | i386_emit_ext (int arg) | |
2189 | { | |
2190 | switch (arg) | |
2191 | { | |
2192 | case 8: | |
2193 | EMIT_ASM32 (i386_ext_8, | |
2194 | "cbtw\n\t" | |
2195 | "cwtl\n\t" | |
2196 | "movl %eax,%ebx\n\t" | |
2197 | "sarl $31,%ebx"); | |
2198 | break; | |
2199 | case 16: | |
2200 | EMIT_ASM32 (i386_ext_16, | |
2201 | "cwtl\n\t" | |
2202 | "movl %eax,%ebx\n\t" | |
2203 | "sarl $31,%ebx"); | |
2204 | break; | |
2205 | case 32: | |
2206 | EMIT_ASM32 (i386_ext_32, | |
2207 | "movl %eax,%ebx\n\t" | |
2208 | "sarl $31,%ebx"); | |
2209 | break; | |
2210 | default: | |
2211 | emit_error = 1; | |
2212 | } | |
2213 | } | |
2214 | ||
2215 | static void | |
2216 | i386_emit_log_not (void) | |
2217 | { | |
2218 | EMIT_ASM32 (i386_log_not, | |
2219 | "or %ebx,%eax\n\t" | |
2220 | "test %eax,%eax\n\t" | |
2221 | "sete %cl\n\t" | |
2222 | "xor %ebx,%ebx\n\t" | |
2223 | "movzbl %cl,%eax"); | |
2224 | } | |
2225 | ||
2226 | static void | |
2227 | i386_emit_bit_and (void) | |
2228 | { | |
2229 | EMIT_ASM32 (i386_and, | |
2230 | "and (%esp),%eax\n\t" | |
2231 | "and 0x4(%esp),%ebx\n\t" | |
2232 | "lea 0x8(%esp),%esp"); | |
2233 | } | |
2234 | ||
2235 | static void | |
2236 | i386_emit_bit_or (void) | |
2237 | { | |
2238 | EMIT_ASM32 (i386_or, | |
2239 | "or (%esp),%eax\n\t" | |
2240 | "or 0x4(%esp),%ebx\n\t" | |
2241 | "lea 0x8(%esp),%esp"); | |
2242 | } | |
2243 | ||
2244 | static void | |
2245 | i386_emit_bit_xor (void) | |
2246 | { | |
2247 | EMIT_ASM32 (i386_xor, | |
2248 | "xor (%esp),%eax\n\t" | |
2249 | "xor 0x4(%esp),%ebx\n\t" | |
2250 | "lea 0x8(%esp),%esp"); | |
2251 | } | |
2252 | ||
2253 | static void | |
2254 | i386_emit_bit_not (void) | |
2255 | { | |
2256 | EMIT_ASM32 (i386_bit_not, | |
2257 | "xor $0xffffffff,%eax\n\t" | |
2258 | "xor $0xffffffff,%ebx\n\t"); | |
2259 | } | |
2260 | ||
2261 | static void | |
2262 | i386_emit_equal (void) | |
2263 | { | |
2264 | EMIT_ASM32 (i386_equal, | |
2265 | "cmpl %ebx,4(%esp)\n\t" | |
2266 | "jne .Li386_equal_false\n\t" | |
2267 | "cmpl %eax,(%esp)\n\t" | |
2268 | "je .Li386_equal_true\n\t" | |
2269 | ".Li386_equal_false:\n\t" | |
2270 | "xor %eax,%eax\n\t" | |
2271 | "jmp .Li386_equal_end\n\t" | |
2272 | ".Li386_equal_true:\n\t" | |
2273 | "mov $1,%eax\n\t" | |
2274 | ".Li386_equal_end:\n\t" | |
2275 | "xor %ebx,%ebx\n\t" | |
2276 | "lea 0x8(%esp),%esp"); | |
2277 | } | |
2278 | ||
2279 | static void | |
2280 | i386_emit_less_signed (void) | |
2281 | { | |
2282 | EMIT_ASM32 (i386_less_signed, | |
2283 | "cmpl %ebx,4(%esp)\n\t" | |
2284 | "jl .Li386_less_signed_true\n\t" | |
2285 | "jne .Li386_less_signed_false\n\t" | |
2286 | "cmpl %eax,(%esp)\n\t" | |
2287 | "jl .Li386_less_signed_true\n\t" | |
2288 | ".Li386_less_signed_false:\n\t" | |
2289 | "xor %eax,%eax\n\t" | |
2290 | "jmp .Li386_less_signed_end\n\t" | |
2291 | ".Li386_less_signed_true:\n\t" | |
2292 | "mov $1,%eax\n\t" | |
2293 | ".Li386_less_signed_end:\n\t" | |
2294 | "xor %ebx,%ebx\n\t" | |
2295 | "lea 0x8(%esp),%esp"); | |
2296 | } | |
2297 | ||
2298 | static void | |
2299 | i386_emit_less_unsigned (void) | |
2300 | { | |
2301 | EMIT_ASM32 (i386_less_unsigned, | |
2302 | "cmpl %ebx,4(%esp)\n\t" | |
2303 | "jb .Li386_less_unsigned_true\n\t" | |
2304 | "jne .Li386_less_unsigned_false\n\t" | |
2305 | "cmpl %eax,(%esp)\n\t" | |
2306 | "jb .Li386_less_unsigned_true\n\t" | |
2307 | ".Li386_less_unsigned_false:\n\t" | |
2308 | "xor %eax,%eax\n\t" | |
2309 | "jmp .Li386_less_unsigned_end\n\t" | |
2310 | ".Li386_less_unsigned_true:\n\t" | |
2311 | "mov $1,%eax\n\t" | |
2312 | ".Li386_less_unsigned_end:\n\t" | |
2313 | "xor %ebx,%ebx\n\t" | |
2314 | "lea 0x8(%esp),%esp"); | |
2315 | } | |
2316 | ||
2317 | static void | |
2318 | i386_emit_ref (int size) | |
2319 | { | |
2320 | switch (size) | |
2321 | { | |
2322 | case 1: | |
2323 | EMIT_ASM32 (i386_ref1, | |
2324 | "movb (%eax),%al"); | |
2325 | break; | |
2326 | case 2: | |
2327 | EMIT_ASM32 (i386_ref2, | |
2328 | "movw (%eax),%ax"); | |
2329 | break; | |
2330 | case 4: | |
2331 | EMIT_ASM32 (i386_ref4, | |
2332 | "movl (%eax),%eax"); | |
2333 | break; | |
2334 | case 8: | |
2335 | EMIT_ASM32 (i386_ref8, | |
2336 | "movl 4(%eax),%ebx\n\t" | |
2337 | "movl (%eax),%eax"); | |
2338 | break; | |
2339 | } | |
2340 | } | |
2341 | ||
2342 | static void | |
2343 | i386_emit_if_goto (int *offset_p, int *size_p) | |
2344 | { | |
2345 | EMIT_ASM32 (i386_if_goto, | |
2346 | "mov %eax,%ecx\n\t" | |
2347 | "or %ebx,%ecx\n\t" | |
2348 | "pop %eax\n\t" | |
2349 | "pop %ebx\n\t" | |
2350 | "cmpl $0,%ecx\n\t" | |
2351 | /* Don't trust the assembler to choose the right jump */ | |
2352 | ".byte 0x0f, 0x85, 0x0, 0x0, 0x0, 0x0"); | |
2353 | ||
2354 | if (offset_p) | |
2355 | *offset_p = 11; /* be sure that this matches the sequence above */ | |
2356 | if (size_p) | |
2357 | *size_p = 4; | |
2358 | } | |
2359 | ||
2360 | static void | |
2361 | i386_emit_goto (int *offset_p, int *size_p) | |
2362 | { | |
2363 | EMIT_ASM32 (i386_goto, | |
2364 | /* Don't trust the assembler to choose the right jump */ | |
2365 | ".byte 0xe9, 0x0, 0x0, 0x0, 0x0"); | |
2366 | if (offset_p) | |
2367 | *offset_p = 1; | |
2368 | if (size_p) | |
2369 | *size_p = 4; | |
2370 | } | |
2371 | ||
2372 | static void | |
2373 | i386_write_goto_address (CORE_ADDR from, CORE_ADDR to, int size) | |
2374 | { | |
2375 | int diff = (to - (from + size)); | |
2376 | unsigned char buf[sizeof (int)]; | |
2377 | ||
2378 | /* We're only doing 4-byte sizes at the moment. */ | |
2379 | if (size != 4) | |
2380 | { | |
2381 | emit_error = 1; | |
2382 | return; | |
2383 | } | |
2384 | ||
2385 | memcpy (buf, &diff, sizeof (int)); | |
2386 | write_inferior_memory (from, buf, sizeof (int)); | |
2387 | } | |
2388 | ||
2389 | static void | |
4e29fb54 | 2390 | i386_emit_const (LONGEST num) |
6a271cae PA |
2391 | { |
2392 | unsigned char buf[16]; | |
b00ad6ff | 2393 | int i, hi, lo; |
6a271cae PA |
2394 | CORE_ADDR buildaddr = current_insn_ptr; |
2395 | ||
2396 | i = 0; | |
2397 | buf[i++] = 0xb8; /* mov $<n>,%eax */ | |
b00ad6ff NF |
2398 | lo = num & 0xffffffff; |
2399 | memcpy (&buf[i], &lo, sizeof (lo)); | |
6a271cae PA |
2400 | i += 4; |
2401 | hi = ((num >> 32) & 0xffffffff); | |
2402 | if (hi) | |
2403 | { | |
2404 | buf[i++] = 0xbb; /* mov $<n>,%ebx */ | |
b00ad6ff | 2405 | memcpy (&buf[i], &hi, sizeof (hi)); |
6a271cae PA |
2406 | i += 4; |
2407 | } | |
2408 | else | |
2409 | { | |
2410 | buf[i++] = 0x31; buf[i++] = 0xdb; /* xor %ebx,%ebx */ | |
2411 | } | |
2412 | append_insns (&buildaddr, i, buf); | |
2413 | current_insn_ptr = buildaddr; | |
2414 | } | |
2415 | ||
2416 | static void | |
2417 | i386_emit_call (CORE_ADDR fn) | |
2418 | { | |
2419 | unsigned char buf[16]; | |
2420 | int i, offset; | |
2421 | CORE_ADDR buildaddr; | |
2422 | ||
2423 | buildaddr = current_insn_ptr; | |
2424 | i = 0; | |
2425 | buf[i++] = 0xe8; /* call <reladdr> */ | |
2426 | offset = ((int) fn) - (buildaddr + 5); | |
2427 | memcpy (buf + 1, &offset, 4); | |
2428 | append_insns (&buildaddr, 5, buf); | |
2429 | current_insn_ptr = buildaddr; | |
2430 | } | |
2431 | ||
2432 | static void | |
2433 | i386_emit_reg (int reg) | |
2434 | { | |
2435 | unsigned char buf[16]; | |
2436 | int i; | |
2437 | CORE_ADDR buildaddr; | |
2438 | ||
2439 | EMIT_ASM32 (i386_reg_a, | |
2440 | "sub $0x8,%esp"); | |
2441 | buildaddr = current_insn_ptr; | |
2442 | i = 0; | |
2443 | buf[i++] = 0xb8; /* mov $<n>,%eax */ | |
b00ad6ff | 2444 | memcpy (&buf[i], ®, sizeof (reg)); |
6a271cae PA |
2445 | i += 4; |
2446 | append_insns (&buildaddr, i, buf); | |
2447 | current_insn_ptr = buildaddr; | |
2448 | EMIT_ASM32 (i386_reg_b, | |
2449 | "mov %eax,4(%esp)\n\t" | |
2450 | "mov 8(%ebp),%eax\n\t" | |
2451 | "mov %eax,(%esp)"); | |
2452 | i386_emit_call (get_raw_reg_func_addr ()); | |
2453 | EMIT_ASM32 (i386_reg_c, | |
2454 | "xor %ebx,%ebx\n\t" | |
2455 | "lea 0x8(%esp),%esp"); | |
2456 | } | |
2457 | ||
2458 | static void | |
2459 | i386_emit_pop (void) | |
2460 | { | |
2461 | EMIT_ASM32 (i386_pop, | |
2462 | "pop %eax\n\t" | |
2463 | "pop %ebx"); | |
2464 | } | |
2465 | ||
2466 | static void | |
2467 | i386_emit_stack_flush (void) | |
2468 | { | |
2469 | EMIT_ASM32 (i386_stack_flush, | |
2470 | "push %ebx\n\t" | |
2471 | "push %eax"); | |
2472 | } | |
2473 | ||
2474 | static void | |
2475 | i386_emit_zero_ext (int arg) | |
2476 | { | |
2477 | switch (arg) | |
2478 | { | |
2479 | case 8: | |
2480 | EMIT_ASM32 (i386_zero_ext_8, | |
2481 | "and $0xff,%eax\n\t" | |
2482 | "xor %ebx,%ebx"); | |
2483 | break; | |
2484 | case 16: | |
2485 | EMIT_ASM32 (i386_zero_ext_16, | |
2486 | "and $0xffff,%eax\n\t" | |
2487 | "xor %ebx,%ebx"); | |
2488 | break; | |
2489 | case 32: | |
2490 | EMIT_ASM32 (i386_zero_ext_32, | |
2491 | "xor %ebx,%ebx"); | |
2492 | break; | |
2493 | default: | |
2494 | emit_error = 1; | |
2495 | } | |
2496 | } | |
2497 | ||
2498 | static void | |
2499 | i386_emit_swap (void) | |
2500 | { | |
2501 | EMIT_ASM32 (i386_swap, | |
2502 | "mov %eax,%ecx\n\t" | |
2503 | "mov %ebx,%edx\n\t" | |
2504 | "pop %eax\n\t" | |
2505 | "pop %ebx\n\t" | |
2506 | "push %edx\n\t" | |
2507 | "push %ecx"); | |
2508 | } | |
2509 | ||
2510 | static void | |
2511 | i386_emit_stack_adjust (int n) | |
2512 | { | |
2513 | unsigned char buf[16]; | |
2514 | int i; | |
2515 | CORE_ADDR buildaddr = current_insn_ptr; | |
2516 | ||
2517 | i = 0; | |
2518 | buf[i++] = 0x8d; /* lea $<n>(%esp),%esp */ | |
2519 | buf[i++] = 0x64; | |
2520 | buf[i++] = 0x24; | |
2521 | buf[i++] = n * 8; | |
2522 | append_insns (&buildaddr, i, buf); | |
2523 | current_insn_ptr = buildaddr; | |
2524 | } | |
2525 | ||
2526 | /* FN's prototype is `LONGEST(*fn)(int)'. */ | |
2527 | ||
2528 | static void | |
2529 | i386_emit_int_call_1 (CORE_ADDR fn, int arg1) | |
2530 | { | |
2531 | unsigned char buf[16]; | |
2532 | int i; | |
2533 | CORE_ADDR buildaddr; | |
2534 | ||
2535 | EMIT_ASM32 (i386_int_call_1_a, | |
2536 | /* Reserve a bit of stack space. */ | |
2537 | "sub $0x8,%esp"); | |
2538 | /* Put the one argument on the stack. */ | |
2539 | buildaddr = current_insn_ptr; | |
2540 | i = 0; | |
2541 | buf[i++] = 0xc7; /* movl $<arg1>,(%esp) */ | |
2542 | buf[i++] = 0x04; | |
2543 | buf[i++] = 0x24; | |
b00ad6ff | 2544 | memcpy (&buf[i], &arg1, sizeof (arg1)); |
6a271cae PA |
2545 | i += 4; |
2546 | append_insns (&buildaddr, i, buf); | |
2547 | current_insn_ptr = buildaddr; | |
2548 | i386_emit_call (fn); | |
2549 | EMIT_ASM32 (i386_int_call_1_c, | |
2550 | "mov %edx,%ebx\n\t" | |
2551 | "lea 0x8(%esp),%esp"); | |
2552 | } | |
2553 | ||
4e29fb54 | 2554 | /* FN's prototype is `void(*fn)(int,LONGEST)'. */ |
6a271cae PA |
2555 | |
2556 | static void | |
2557 | i386_emit_void_call_2 (CORE_ADDR fn, int arg1) | |
2558 | { | |
2559 | unsigned char buf[16]; | |
2560 | int i; | |
2561 | CORE_ADDR buildaddr; | |
2562 | ||
2563 | EMIT_ASM32 (i386_void_call_2_a, | |
2564 | /* Preserve %eax only; we don't have to worry about %ebx. */ | |
2565 | "push %eax\n\t" | |
2566 | /* Reserve a bit of stack space for arguments. */ | |
2567 | "sub $0x10,%esp\n\t" | |
2568 | /* Copy "top" to the second argument position. (Note that | |
2569 | we can't assume function won't scribble on its | |
2570 | arguments, so don't try to restore from this.) */ | |
2571 | "mov %eax,4(%esp)\n\t" | |
2572 | "mov %ebx,8(%esp)"); | |
2573 | /* Put the first argument on the stack. */ | |
2574 | buildaddr = current_insn_ptr; | |
2575 | i = 0; | |
2576 | buf[i++] = 0xc7; /* movl $<arg1>,(%esp) */ | |
2577 | buf[i++] = 0x04; | |
2578 | buf[i++] = 0x24; | |
b00ad6ff | 2579 | memcpy (&buf[i], &arg1, sizeof (arg1)); |
6a271cae PA |
2580 | i += 4; |
2581 | append_insns (&buildaddr, i, buf); | |
2582 | current_insn_ptr = buildaddr; | |
2583 | i386_emit_call (fn); | |
2584 | EMIT_ASM32 (i386_void_call_2_b, | |
2585 | "lea 0x10(%esp),%esp\n\t" | |
2586 | /* Restore original stack top. */ | |
2587 | "pop %eax"); | |
2588 | } | |
2589 | ||
6b9801d4 SS |
2590 | |
2591 | void | |
2592 | i386_emit_eq_goto (int *offset_p, int *size_p) | |
2593 | { | |
2594 | EMIT_ASM32 (eq, | |
2595 | /* Check low half first, more likely to be decider */ | |
2596 | "cmpl %eax,(%esp)\n\t" | |
2597 | "jne .Leq_fallthru\n\t" | |
2598 | "cmpl %ebx,4(%esp)\n\t" | |
2599 | "jne .Leq_fallthru\n\t" | |
2600 | "lea 0x8(%esp),%esp\n\t" | |
2601 | "pop %eax\n\t" | |
2602 | "pop %ebx\n\t" | |
2603 | /* jmp, but don't trust the assembler to choose the right jump */ | |
2604 | ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t" | |
2605 | ".Leq_fallthru:\n\t" | |
2606 | "lea 0x8(%esp),%esp\n\t" | |
2607 | "pop %eax\n\t" | |
2608 | "pop %ebx"); | |
2609 | ||
2610 | if (offset_p) | |
2611 | *offset_p = 18; | |
2612 | if (size_p) | |
2613 | *size_p = 4; | |
2614 | } | |
2615 | ||
2616 | void | |
2617 | i386_emit_ne_goto (int *offset_p, int *size_p) | |
2618 | { | |
2619 | EMIT_ASM32 (ne, | |
2620 | /* Check low half first, more likely to be decider */ | |
2621 | "cmpl %eax,(%esp)\n\t" | |
2622 | "jne .Lne_jump\n\t" | |
2623 | "cmpl %ebx,4(%esp)\n\t" | |
2624 | "je .Lne_fallthru\n\t" | |
2625 | ".Lne_jump:\n\t" | |
2626 | "lea 0x8(%esp),%esp\n\t" | |
2627 | "pop %eax\n\t" | |
2628 | "pop %ebx\n\t" | |
2629 | /* jmp, but don't trust the assembler to choose the right jump */ | |
2630 | ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t" | |
2631 | ".Lne_fallthru:\n\t" | |
2632 | "lea 0x8(%esp),%esp\n\t" | |
2633 | "pop %eax\n\t" | |
2634 | "pop %ebx"); | |
2635 | ||
2636 | if (offset_p) | |
2637 | *offset_p = 18; | |
2638 | if (size_p) | |
2639 | *size_p = 4; | |
2640 | } | |
2641 | ||
2642 | void | |
2643 | i386_emit_lt_goto (int *offset_p, int *size_p) | |
2644 | { | |
2645 | EMIT_ASM32 (lt, | |
2646 | "cmpl %ebx,4(%esp)\n\t" | |
2647 | "jl .Llt_jump\n\t" | |
2648 | "jne .Llt_fallthru\n\t" | |
2649 | "cmpl %eax,(%esp)\n\t" | |
2650 | "jnl .Llt_fallthru\n\t" | |
2651 | ".Llt_jump:\n\t" | |
2652 | "lea 0x8(%esp),%esp\n\t" | |
2653 | "pop %eax\n\t" | |
2654 | "pop %ebx\n\t" | |
2655 | /* jmp, but don't trust the assembler to choose the right jump */ | |
2656 | ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t" | |
2657 | ".Llt_fallthru:\n\t" | |
2658 | "lea 0x8(%esp),%esp\n\t" | |
2659 | "pop %eax\n\t" | |
2660 | "pop %ebx"); | |
2661 | ||
2662 | if (offset_p) | |
2663 | *offset_p = 20; | |
2664 | if (size_p) | |
2665 | *size_p = 4; | |
2666 | } | |
2667 | ||
2668 | void | |
2669 | i386_emit_le_goto (int *offset_p, int *size_p) | |
2670 | { | |
2671 | EMIT_ASM32 (le, | |
2672 | "cmpl %ebx,4(%esp)\n\t" | |
2673 | "jle .Lle_jump\n\t" | |
2674 | "jne .Lle_fallthru\n\t" | |
2675 | "cmpl %eax,(%esp)\n\t" | |
2676 | "jnle .Lle_fallthru\n\t" | |
2677 | ".Lle_jump:\n\t" | |
2678 | "lea 0x8(%esp),%esp\n\t" | |
2679 | "pop %eax\n\t" | |
2680 | "pop %ebx\n\t" | |
2681 | /* jmp, but don't trust the assembler to choose the right jump */ | |
2682 | ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t" | |
2683 | ".Lle_fallthru:\n\t" | |
2684 | "lea 0x8(%esp),%esp\n\t" | |
2685 | "pop %eax\n\t" | |
2686 | "pop %ebx"); | |
2687 | ||
2688 | if (offset_p) | |
2689 | *offset_p = 20; | |
2690 | if (size_p) | |
2691 | *size_p = 4; | |
2692 | } | |
2693 | ||
2694 | void | |
2695 | i386_emit_gt_goto (int *offset_p, int *size_p) | |
2696 | { | |
2697 | EMIT_ASM32 (gt, | |
2698 | "cmpl %ebx,4(%esp)\n\t" | |
2699 | "jg .Lgt_jump\n\t" | |
2700 | "jne .Lgt_fallthru\n\t" | |
2701 | "cmpl %eax,(%esp)\n\t" | |
2702 | "jng .Lgt_fallthru\n\t" | |
2703 | ".Lgt_jump:\n\t" | |
2704 | "lea 0x8(%esp),%esp\n\t" | |
2705 | "pop %eax\n\t" | |
2706 | "pop %ebx\n\t" | |
2707 | /* jmp, but don't trust the assembler to choose the right jump */ | |
2708 | ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t" | |
2709 | ".Lgt_fallthru:\n\t" | |
2710 | "lea 0x8(%esp),%esp\n\t" | |
2711 | "pop %eax\n\t" | |
2712 | "pop %ebx"); | |
2713 | ||
2714 | if (offset_p) | |
2715 | *offset_p = 20; | |
2716 | if (size_p) | |
2717 | *size_p = 4; | |
2718 | } | |
2719 | ||
2720 | void | |
2721 | i386_emit_ge_goto (int *offset_p, int *size_p) | |
2722 | { | |
2723 | EMIT_ASM32 (ge, | |
2724 | "cmpl %ebx,4(%esp)\n\t" | |
2725 | "jge .Lge_jump\n\t" | |
2726 | "jne .Lge_fallthru\n\t" | |
2727 | "cmpl %eax,(%esp)\n\t" | |
2728 | "jnge .Lge_fallthru\n\t" | |
2729 | ".Lge_jump:\n\t" | |
2730 | "lea 0x8(%esp),%esp\n\t" | |
2731 | "pop %eax\n\t" | |
2732 | "pop %ebx\n\t" | |
2733 | /* jmp, but don't trust the assembler to choose the right jump */ | |
2734 | ".byte 0xe9, 0x0, 0x0, 0x0, 0x0\n\t" | |
2735 | ".Lge_fallthru:\n\t" | |
2736 | "lea 0x8(%esp),%esp\n\t" | |
2737 | "pop %eax\n\t" | |
2738 | "pop %ebx"); | |
2739 | ||
2740 | if (offset_p) | |
2741 | *offset_p = 20; | |
2742 | if (size_p) | |
2743 | *size_p = 4; | |
2744 | } | |
2745 | ||
6a271cae PA |
2746 | struct emit_ops i386_emit_ops = |
2747 | { | |
2748 | i386_emit_prologue, | |
2749 | i386_emit_epilogue, | |
2750 | i386_emit_add, | |
2751 | i386_emit_sub, | |
2752 | i386_emit_mul, | |
2753 | i386_emit_lsh, | |
2754 | i386_emit_rsh_signed, | |
2755 | i386_emit_rsh_unsigned, | |
2756 | i386_emit_ext, | |
2757 | i386_emit_log_not, | |
2758 | i386_emit_bit_and, | |
2759 | i386_emit_bit_or, | |
2760 | i386_emit_bit_xor, | |
2761 | i386_emit_bit_not, | |
2762 | i386_emit_equal, | |
2763 | i386_emit_less_signed, | |
2764 | i386_emit_less_unsigned, | |
2765 | i386_emit_ref, | |
2766 | i386_emit_if_goto, | |
2767 | i386_emit_goto, | |
2768 | i386_write_goto_address, | |
2769 | i386_emit_const, | |
2770 | i386_emit_call, | |
2771 | i386_emit_reg, | |
2772 | i386_emit_pop, | |
2773 | i386_emit_stack_flush, | |
2774 | i386_emit_zero_ext, | |
2775 | i386_emit_swap, | |
2776 | i386_emit_stack_adjust, | |
2777 | i386_emit_int_call_1, | |
6b9801d4 SS |
2778 | i386_emit_void_call_2, |
2779 | i386_emit_eq_goto, | |
2780 | i386_emit_ne_goto, | |
2781 | i386_emit_lt_goto, | |
2782 | i386_emit_le_goto, | |
2783 | i386_emit_gt_goto, | |
2784 | i386_emit_ge_goto | |
6a271cae PA |
2785 | }; |
2786 | ||
2787 | ||
2788 | static struct emit_ops * | |
2789 | x86_emit_ops (void) | |
2790 | { | |
2791 | #ifdef __x86_64__ | |
3aee8918 | 2792 | if (is_64bit_tdesc ()) |
6a271cae PA |
2793 | return &amd64_emit_ops; |
2794 | else | |
2795 | #endif | |
2796 | return &i386_emit_ops; | |
2797 | } | |
2798 | ||
dd373349 AT |
2799 | /* Implementation of linux_target_ops method "sw_breakpoint_from_kind". */ |
2800 | ||
2801 | static const gdb_byte * | |
2802 | x86_sw_breakpoint_from_kind (int kind, int *size) | |
2803 | { | |
2804 | *size = x86_breakpoint_len; | |
2805 | return x86_breakpoint; | |
2806 | } | |
2807 | ||
c2d6af84 PA |
2808 | static int |
2809 | x86_supports_range_stepping (void) | |
2810 | { | |
2811 | return 1; | |
2812 | } | |
2813 | ||
7d00775e AT |
2814 | /* Implementation of linux_target_ops method "supports_hardware_single_step". |
2815 | */ | |
2816 | ||
2817 | static int | |
2818 | x86_supports_hardware_single_step (void) | |
2819 | { | |
2820 | return 1; | |
2821 | } | |
2822 | ||
ae91f625 MK |
2823 | static int |
2824 | x86_get_ipa_tdesc_idx (void) | |
2825 | { | |
2826 | struct regcache *regcache = get_thread_regcache (current_thread, 0); | |
2827 | const struct target_desc *tdesc = regcache->tdesc; | |
2828 | ||
2829 | #ifdef __x86_64__ | |
b4570e4b | 2830 | return amd64_get_ipa_tdesc_idx (tdesc); |
ae91f625 MK |
2831 | #endif |
2832 | ||
f49ff000 | 2833 | if (tdesc == tdesc_i386_linux_no_xml) |
ae91f625 | 2834 | return X86_TDESC_SSE; |
ae91f625 | 2835 | |
f49ff000 | 2836 | return i386_get_ipa_tdesc_idx (tdesc); |
ae91f625 MK |
2837 | } |
2838 | ||
d0722149 DE |
2839 | /* This is initialized assuming an amd64 target. |
2840 | x86_arch_setup will correct it for i386 or amd64 targets. */ | |
2841 | ||
2842 | struct linux_target_ops the_low_target = | |
2843 | { | |
2844 | x86_arch_setup, | |
3aee8918 PA |
2845 | x86_linux_regs_info, |
2846 | x86_cannot_fetch_register, | |
2847 | x86_cannot_store_register, | |
c14dfd32 | 2848 | NULL, /* fetch_register */ |
d0722149 DE |
2849 | x86_get_pc, |
2850 | x86_set_pc, | |
dd373349 AT |
2851 | NULL, /* breakpoint_kind_from_pc */ |
2852 | x86_sw_breakpoint_from_kind, | |
d0722149 DE |
2853 | NULL, |
2854 | 1, | |
2855 | x86_breakpoint_at, | |
802e8e6d | 2856 | x86_supports_z_point_type, |
aa5ca48f DE |
2857 | x86_insert_point, |
2858 | x86_remove_point, | |
2859 | x86_stopped_by_watchpoint, | |
2860 | x86_stopped_data_address, | |
d0722149 DE |
2861 | /* collect_ptrace_register/supply_ptrace_register are not needed in the |
2862 | native i386 case (no registers smaller than an xfer unit), and are not | |
2863 | used in the biarch case (HAVE_LINUX_USRREGS is not defined). */ | |
2864 | NULL, | |
2865 | NULL, | |
2866 | /* need to fix up i386 siginfo if host is amd64 */ | |
2867 | x86_siginfo_fixup, | |
aa5ca48f | 2868 | x86_linux_new_process, |
04ec7890 | 2869 | x86_linux_delete_process, |
aa5ca48f | 2870 | x86_linux_new_thread, |
466eecee | 2871 | x86_linux_delete_thread, |
3a8a0396 | 2872 | x86_linux_new_fork, |
1570b33e | 2873 | x86_linux_prepare_to_resume, |
219f2f23 | 2874 | x86_linux_process_qsupported, |
fa593d66 PA |
2875 | x86_supports_tracepoints, |
2876 | x86_get_thread_area, | |
6a271cae | 2877 | x86_install_fast_tracepoint_jump_pad, |
405f8e94 SS |
2878 | x86_emit_ops, |
2879 | x86_get_min_fast_tracepoint_insn_len, | |
c2d6af84 | 2880 | x86_supports_range_stepping, |
7d00775e AT |
2881 | NULL, /* breakpoint_kind_from_current_state */ |
2882 | x86_supports_hardware_single_step, | |
82075af2 | 2883 | x86_get_syscall_trapinfo, |
ae91f625 | 2884 | x86_get_ipa_tdesc_idx, |
d0722149 | 2885 | }; |
3aee8918 PA |
2886 | |
2887 | void | |
2888 | initialize_low_arch (void) | |
2889 | { | |
2890 | /* Initialize the Linux target descriptions. */ | |
2891 | #ifdef __x86_64__ | |
cc397f3a | 2892 | tdesc_amd64_linux_no_xml = allocate_target_description (); |
b4570e4b YQ |
2893 | copy_target_description (tdesc_amd64_linux_no_xml, |
2894 | amd64_linux_read_description (X86_XSTATE_SSE_MASK, | |
2895 | false)); | |
3aee8918 PA |
2896 | tdesc_amd64_linux_no_xml->xmltarget = xmltarget_amd64_linux_no_xml; |
2897 | #endif | |
f49ff000 | 2898 | |
25a93583 | 2899 | #if GDB_SELF_TEST |
f49ff000 | 2900 | initialize_low_tdesc (); |
25a93583 | 2901 | #endif |
3aee8918 | 2902 | |
cc397f3a | 2903 | tdesc_i386_linux_no_xml = allocate_target_description (); |
f49ff000 YQ |
2904 | copy_target_description (tdesc_i386_linux_no_xml, |
2905 | i386_linux_read_description (X86_XSTATE_SSE_MASK)); | |
3aee8918 PA |
2906 | tdesc_i386_linux_no_xml->xmltarget = xmltarget_i386_linux_no_xml; |
2907 | ||
2908 | initialize_regsets_info (&x86_regsets_info); | |
2909 | } |