Add IA64_MAX_FP_REGISTER_SIZE
[deliverable/binutils-gdb.git] / gdb / x86-linux-nat.c
CommitLineData
040baaf6
GB
1/* Native-dependent code for GNU/Linux x86 (i386 and x86-64).
2
61baf725 3 Copyright (C) 1999-2017 Free Software Foundation, Inc.
040baaf6
GB
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
20#include "defs.h"
21#include "inferior.h"
22#include "elf/common.h"
23#include "gdb_proc_service.h"
5826e159 24#include "nat/gdb_ptrace.h"
040baaf6
GB
25#include <sys/user.h>
26#include <sys/procfs.h>
72fde3df 27#include <sys/uio.h>
040baaf6 28
df7e5265 29#include "x86-nat.h"
040baaf6
GB
30#include "linux-nat.h"
31#ifndef __x86_64__
32#include "i386-linux-nat.h"
33#endif
34#include "x86-linux-nat.h"
35#include "i386-linux-tdep.h"
36#ifdef __x86_64__
37#include "amd64-linux-tdep.h"
38#endif
df7e5265 39#include "x86-xstate.h"
040baaf6 40#include "nat/linux-btrace.h"
7b669087 41#include "nat/linux-nat.h"
4b134ca1 42#include "nat/x86-linux.h"
8e5d4070 43#include "nat/x86-linux-dregs.h"
ca9b78ce 44#include "nat/linux-ptrace.h"
040baaf6 45
040baaf6
GB
46/* linux_nat_new_fork hook. */
47
48static void
49x86_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
50{
51 pid_t parent_pid;
df7e5265
GB
52 struct x86_debug_reg_state *parent_state;
53 struct x86_debug_reg_state *child_state;
040baaf6
GB
54
55 /* NULL means no watchpoint has ever been set in the parent. In
56 that case, there's nothing to do. */
57 if (parent->arch_private == NULL)
58 return;
59
60 /* Linux kernel before 2.6.33 commit
61 72f674d203cd230426437cdcf7dd6f681dad8b0d
62 will inherit hardware debug registers from parent
63 on fork/vfork/clone. Newer Linux kernels create such tasks with
64 zeroed debug registers.
65
66 GDB core assumes the child inherits the watchpoints/hw
67 breakpoints of the parent, and will remove them all from the
68 forked off process. Copy the debug registers mirrors into the
69 new process so that all breakpoints and watchpoints can be
70 removed together. The debug registers mirror will become zeroed
71 in the end before detaching the forked off process, thus making
72 this compatible with older Linux kernels too. */
73
74 parent_pid = ptid_get_pid (parent->ptid);
df7e5265
GB
75 parent_state = x86_debug_reg_state (parent_pid);
76 child_state = x86_debug_reg_state (child_pid);
040baaf6
GB
77 *child_state = *parent_state;
78}
79\f
80
81static void (*super_post_startup_inferior) (struct target_ops *self,
82 ptid_t ptid);
83
84static void
85x86_linux_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
86{
df7e5265 87 x86_cleanup_dregs ();
040baaf6
GB
88 super_post_startup_inferior (self, ptid);
89}
90
91#ifdef __x86_64__
92/* Value of CS segment register:
93 64bit process: 0x33
94 32bit process: 0x23 */
95#define AMD64_LINUX_USER64_CS 0x33
96
97/* Value of DS segment register:
98 LP64 process: 0x0
99 X32 process: 0x2b */
100#define AMD64_LINUX_X32_DS 0x2b
101#endif
102
103/* Get Linux/x86 target description from running target. */
104
105static const struct target_desc *
106x86_linux_read_description (struct target_ops *ops)
107{
108 int tid;
109 int is_64bit = 0;
110#ifdef __x86_64__
111 int is_x32;
112#endif
113 static uint64_t xcr0;
114 uint64_t xcr0_features_bits;
115
116 /* GNU/Linux LWP ID's are process ID's. */
117 tid = ptid_get_lwp (inferior_ptid);
118 if (tid == 0)
119 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
120
121#ifdef __x86_64__
122 {
123 unsigned long cs;
124 unsigned long ds;
125
126 /* Get CS register. */
127 errno = 0;
128 cs = ptrace (PTRACE_PEEKUSER, tid,
129 offsetof (struct user_regs_struct, cs), 0);
130 if (errno != 0)
131 perror_with_name (_("Couldn't get CS register"));
132
133 is_64bit = cs == AMD64_LINUX_USER64_CS;
134
135 /* Get DS register. */
136 errno = 0;
137 ds = ptrace (PTRACE_PEEKUSER, tid,
138 offsetof (struct user_regs_struct, ds), 0);
139 if (errno != 0)
140 perror_with_name (_("Couldn't get DS register"));
141
142 is_x32 = ds == AMD64_LINUX_X32_DS;
143
144 if (sizeof (void *) == 4 && is_64bit && !is_x32)
145 error (_("Can't debug 64-bit process with 32-bit GDB"));
146 }
147#elif HAVE_PTRACE_GETFPXREGS
148 if (have_ptrace_getfpxregs == -1)
149 {
150 elf_fpxregset_t fpxregs;
151
152 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
153 {
154 have_ptrace_getfpxregs = 0;
0bdb2f78 155 have_ptrace_getregset = TRIBOOL_FALSE;
040baaf6
GB
156 return tdesc_i386_mmx_linux;
157 }
158 }
159#endif
160
0bdb2f78 161 if (have_ptrace_getregset == TRIBOOL_UNKNOWN)
040baaf6 162 {
df7e5265 163 uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))];
040baaf6
GB
164 struct iovec iov;
165
166 iov.iov_base = xstateregs;
167 iov.iov_len = sizeof (xstateregs);
168
169 /* Check if PTRACE_GETREGSET works. */
170 if (ptrace (PTRACE_GETREGSET, tid,
171 (unsigned int) NT_X86_XSTATE, &iov) < 0)
0bdb2f78 172 have_ptrace_getregset = TRIBOOL_FALSE;
040baaf6
GB
173 else
174 {
0bdb2f78 175 have_ptrace_getregset = TRIBOOL_TRUE;
040baaf6
GB
176
177 /* Get XCR0 from XSAVE extended state. */
178 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
179 / sizeof (uint64_t))];
180 }
181 }
182
183 /* Check the native XCR0 only if PTRACE_GETREGSET is available. If
184 PTRACE_GETREGSET is not available then set xcr0_features_bits to
185 zero so that the "no-features" descriptions are returned by the
186 switches below. */
0bdb2f78 187 if (have_ptrace_getregset == TRIBOOL_TRUE)
df7e5265 188 xcr0_features_bits = xcr0 & X86_XSTATE_ALL_MASK;
040baaf6
GB
189 else
190 xcr0_features_bits = 0;
191
192 if (is_64bit)
193 {
194#ifdef __x86_64__
195 switch (xcr0_features_bits)
196 {
51547df6 197 case X86_XSTATE_AVX_MPX_AVX512_PKU_MASK:
040baaf6 198 if (is_x32)
51547df6 199 /* No MPX, PKU on x32, fall back to AVX-AVX512. */
a1fa17ee 200 return tdesc_x32_avx_avx512_linux;
040baaf6 201 else
51547df6 202 return tdesc_amd64_avx_mpx_avx512_pku_linux;
a1fa17ee
MS
203 case X86_XSTATE_AVX_AVX512_MASK:
204 if (is_x32)
205 return tdesc_x32_avx_avx512_linux;
206 else
207 return tdesc_amd64_avx_avx512_linux;
df7e5265 208 case X86_XSTATE_MPX_MASK:
040baaf6
GB
209 if (is_x32)
210 return tdesc_x32_avx_linux; /* No MPX on x32 using AVX. */
211 else
212 return tdesc_amd64_mpx_linux;
2b863f51
WT
213 case X86_XSTATE_AVX_MPX_MASK:
214 if (is_x32)
215 return tdesc_x32_avx_linux; /* No MPX on x32 using AVX. */
216 else
217 return tdesc_amd64_avx_mpx_linux;
df7e5265 218 case X86_XSTATE_AVX_MASK:
040baaf6
GB
219 if (is_x32)
220 return tdesc_x32_avx_linux;
221 else
222 return tdesc_amd64_avx_linux;
223 default:
224 if (is_x32)
225 return tdesc_x32_linux;
226 else
227 return tdesc_amd64_linux;
228 }
229#endif
230 }
231 else
232 {
233 switch (xcr0_features_bits)
234 {
51547df6
MS
235 case X86_XSTATE_AVX_MPX_AVX512_PKU_MASK:
236 return tdesc_i386_avx_mpx_avx512_pku_linux;
a1fa17ee
MS
237 case X86_XSTATE_AVX_AVX512_MASK:
238 return tdesc_i386_avx_avx512_linux;
df7e5265 239 case X86_XSTATE_MPX_MASK:
040baaf6 240 return tdesc_i386_mpx_linux;
2b863f51
WT
241 case X86_XSTATE_AVX_MPX_MASK:
242 return tdesc_i386_avx_mpx_linux;
df7e5265 243 case X86_XSTATE_AVX_MASK:
040baaf6
GB
244 return tdesc_i386_avx_linux;
245 default:
246 return tdesc_i386_linux;
247 }
248 }
249
250 gdb_assert_not_reached ("failed to return tdesc");
251}
252\f
253
254/* Enable branch tracing. */
255
256static struct btrace_target_info *
f4abbc16
MM
257x86_linux_enable_btrace (struct target_ops *self, ptid_t ptid,
258 const struct btrace_config *conf)
040baaf6
GB
259{
260 struct btrace_target_info *tinfo;
040baaf6
GB
261
262 errno = 0;
f4abbc16 263 tinfo = linux_enable_btrace (ptid, conf);
040baaf6
GB
264
265 if (tinfo == NULL)
266 error (_("Could not enable branch tracing for %s: %s."),
267 target_pid_to_str (ptid), safe_strerror (errno));
268
040baaf6
GB
269 return tinfo;
270}
271
272/* Disable branch tracing. */
273
274static void
275x86_linux_disable_btrace (struct target_ops *self,
276 struct btrace_target_info *tinfo)
277{
278 enum btrace_error errcode = linux_disable_btrace (tinfo);
279
280 if (errcode != BTRACE_ERR_NONE)
281 error (_("Could not disable branch tracing."));
282}
283
284/* Teardown branch tracing. */
285
286static void
287x86_linux_teardown_btrace (struct target_ops *self,
288 struct btrace_target_info *tinfo)
289{
290 /* Ignore errors. */
291 linux_disable_btrace (tinfo);
292}
293
294static enum btrace_error
295x86_linux_read_btrace (struct target_ops *self,
734b0e4b 296 struct btrace_data *data,
040baaf6
GB
297 struct btrace_target_info *btinfo,
298 enum btrace_read_type type)
299{
300 return linux_read_btrace (data, btinfo, type);
301}
f4abbc16
MM
302
303/* See to_btrace_conf in target.h. */
304
305static const struct btrace_config *
306x86_linux_btrace_conf (struct target_ops *self,
307 const struct btrace_target_info *btinfo)
308{
309 return linux_btrace_conf (btinfo);
310}
311
040baaf6
GB
312\f
313
314/* Helper for ps_get_thread_area. Sets BASE_ADDR to a pointer to
315 the thread local storage (or its descriptor) and returns PS_OK
316 on success. Returns PS_ERR on failure. */
317
318ps_err_e
319x86_linux_get_thread_area (pid_t pid, void *addr, unsigned int *base_addr)
320{
321 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
322 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
323 4 byte integers in size: `entry_number', `base_addr', `limit',
324 and a bunch of status bits.
325
326 The values returned by this ptrace call should be part of the
327 regcache buffer, and ps_get_thread_area should channel its
328 request through the regcache. That way remote targets could
329 provide the value using the remote protocol and not this direct
330 call.
331
332 Is this function needed? I'm guessing that the `base' is the
333 address of a descriptor that libthread_db uses to find the
334 thread local address base that GDB needs. Perhaps that
335 descriptor is defined by the ABI. Anyway, given that
336 libthread_db calls this function without prompting (gdb
337 requesting tls base) I guess it needs info in there anyway. */
338 unsigned int desc[4];
339
340 /* This code assumes that "int" is 32 bits and that
341 GET_THREAD_AREA returns no more than 4 int values. */
342 gdb_assert (sizeof (int) == 4);
343
344#ifndef PTRACE_GET_THREAD_AREA
345#define PTRACE_GET_THREAD_AREA 25
346#endif
347
348 if (ptrace (PTRACE_GET_THREAD_AREA, pid, addr, &desc) < 0)
349 return PS_ERR;
350
351 *base_addr = desc[1];
352 return PS_OK;
353}
354\f
355
356/* Create an x86 GNU/Linux target. */
357
358struct target_ops *
359x86_linux_create_target (void)
360{
361 /* Fill in the generic GNU/Linux methods. */
362 struct target_ops *t = linux_target ();
363
364 /* Initialize the debug register function vectors. */
df7e5265
GB
365 x86_use_watchpoints (t);
366 x86_dr_low.set_control = x86_linux_dr_set_control;
367 x86_dr_low.set_addr = x86_linux_dr_set_addr;
368 x86_dr_low.get_addr = x86_linux_dr_get_addr;
369 x86_dr_low.get_status = x86_linux_dr_get_status;
370 x86_dr_low.get_control = x86_linux_dr_get_control;
371 x86_set_debug_register_length (sizeof (void *));
040baaf6
GB
372
373 /* Override the GNU/Linux inferior startup hook. */
374 super_post_startup_inferior = t->to_post_startup_inferior;
375 t->to_post_startup_inferior = x86_linux_child_post_startup_inferior;
376
377 /* Add the description reader. */
378 t->to_read_description = x86_linux_read_description;
379
380 /* Add btrace methods. */
381 t->to_supports_btrace = linux_supports_btrace;
382 t->to_enable_btrace = x86_linux_enable_btrace;
383 t->to_disable_btrace = x86_linux_disable_btrace;
384 t->to_teardown_btrace = x86_linux_teardown_btrace;
385 t->to_read_btrace = x86_linux_read_btrace;
f4abbc16 386 t->to_btrace_conf = x86_linux_btrace_conf;
040baaf6
GB
387
388 return t;
389}
390
391/* Add an x86 GNU/Linux target. */
392
393void
394x86_linux_add_target (struct target_ops *t)
395{
396 linux_nat_add_target (t);
397 linux_nat_set_new_thread (t, x86_linux_new_thread);
398 linux_nat_set_new_fork (t, x86_linux_new_fork);
df7e5265 399 linux_nat_set_forget_process (t, x86_forget_process);
040baaf6
GB
400 linux_nat_set_prepare_to_resume (t, x86_linux_prepare_to_resume);
401}
This page took 0.426783 seconds and 4 git commands to generate.