1 /* Target description related code for GNU/Linux x86 (i386 and x86-64).
3 Copyright (C) 2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "nat/x86-linux-tdesc.h"
22 #include "arch/amd64.h"
23 #include "arch/amd64-linux-tdesc.h"
25 #include "arch/i386.h"
26 #include "arch/i386-linux-tdesc.h"
28 #include "nat/x86-linux.h"
29 #include "nat/gdb_ptrace.h"
30 #include "nat/x86-xstate.h"
31 #include "gdbsupport/x86-xstate.h"
34 #include <sys/procfs.h>
35 #include "nat/i386-linux.h"
41 #ifndef IN_PROCESS_AGENT
43 /* See nat/x86-linux-tdesc.h. */
46 x86_linux_tdesc_for_tid (int tid
, uint64_t *xcr0_storage
,
47 x86_xsave_layout
*xsave_layout_storage
)
50 x86_linux_arch_size arch_size
= x86_linux_ptrace_get_arch_size (tid
);
51 bool is_64bit
= arch_size
.is_64bit ();
52 bool is_x32
= arch_size
.is_x32 ();
54 if (sizeof (void *) == 4 && is_64bit
&& !is_x32
)
57 error (_("Can't debug 64-bit process with 32-bit GDBserver"));
59 error (_("Can't debug 64-bit process with 32-bit GDB"));
63 #elif HAVE_PTRACE_GETFPXREGS
64 if (have_ptrace_getfpxregs
== TRIBOOL_UNKNOWN
)
66 elf_fpxregset_t fpxregs
;
68 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
70 have_ptrace_getfpxregs
= TRIBOOL_FALSE
;
71 have_ptrace_getregset
= TRIBOOL_FALSE
;
74 have_ptrace_getfpxregs
= TRIBOOL_TRUE
;
77 if (have_ptrace_getfpxregs
== TRIBOOL_FALSE
)
78 return i386_linux_read_description (X86_XSTATE_X87_MASK
);
81 if (have_ptrace_getregset
== TRIBOOL_UNKNOWN
)
83 uint64_t xstateregs
[(X86_XSTATE_SSE_SIZE
/ sizeof (uint64_t))];
86 iov
.iov_base
= xstateregs
;
87 iov
.iov_len
= sizeof (xstateregs
);
89 /* Check if PTRACE_GETREGSET works. */
90 if (ptrace (PTRACE_GETREGSET
, tid
,
91 (unsigned int) NT_X86_XSTATE
, &iov
) < 0)
93 /* Can't fetch the xcr0 value so pick a simple default. This
94 default has x87 and sse bits set. These bits are ignored for
95 amd64 and x32 targets, but are checked for on i386. Without
96 these bits being set we generate a completely empty tdesc for
97 i386 which will be rejected by GDB. */
98 have_ptrace_getregset
= TRIBOOL_FALSE
;
99 *xcr0_storage
= X86_XSTATE_SSE_MASK
;
103 have_ptrace_getregset
= TRIBOOL_TRUE
;
105 /* Get XCR0 from XSAVE extended state. */
106 *xcr0_storage
= xstateregs
[(I386_LINUX_XSAVE_XCR0_OFFSET
107 / sizeof (uint64_t))];
109 *xsave_layout_storage
110 = x86_fetch_xsave_layout (*xcr0_storage
, x86_xsave_length ());
114 /* Use cached xcr0 value. */
115 uint64_t xcr0_features_bits
= *xcr0_storage
& X86_XSTATE_ALL_MASK
;
119 return amd64_linux_read_description (xcr0_features_bits
, is_x32
);
122 return i386_linux_read_description (xcr0_features_bits
);
125 #endif /* !IN_PROCESS_AGENT */