Commit | Line | Data |
---|---|---|
cced5e27 MK |
1 | /* Native-dependent code for AMD64 BSD's. |
2 | ||
42a4f53d | 3 | Copyright (C) 2003-2019 Free Software Foundation, Inc. |
cced5e27 MK |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
cced5e27 MK |
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 | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
cced5e27 MK |
19 | |
20 | #include "defs.h" | |
d55e5aa6 TT |
21 | |
22 | /* Local non-gdb includes. */ | |
cced5e27 MK |
23 | #include "inferior.h" |
24 | #include "regcache.h" | |
6a5c78a3 | 25 | #include "target.h" |
cced5e27 MK |
26 | |
27 | /* We include <signal.h> to make sure `struct fxsave64' is defined on | |
28 | NetBSD, since NetBSD's <machine/reg.h> needs it. */ | |
cced5e27 MK |
29 | #include <signal.h> |
30 | #include <sys/types.h> | |
31 | #include <sys/ptrace.h> | |
32 | #include <machine/reg.h> | |
33 | ||
85be1ca6 | 34 | #include "amd64-tdep.h" |
cced5e27 | 35 | #include "amd64-nat.h" |
03b62bbb | 36 | #include "x86-bsd-nat.h" |
6a5c78a3 | 37 | #include "inf-ptrace.h" |
cced5e27 MK |
38 | \f |
39 | ||
40 | /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this | |
41 | for all registers (including the floating-point registers). */ | |
42 | ||
f6ac5f3d PA |
43 | void |
44 | amd64bsd_fetch_inferior_registers (struct regcache *regcache, int regnum) | |
cced5e27 | 45 | { |
ac7936df | 46 | struct gdbarch *gdbarch = regcache->arch (); |
222312d3 | 47 | pid_t pid = get_ptrace_pid (regcache->ptid ()); |
dd6876c9 JB |
48 | #if defined(PT_GETFSBASE) || defined(PT_GETGSBASE) |
49 | const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
50 | #endif | |
f8028488 MD |
51 | |
52 | if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum)) | |
cced5e27 MK |
53 | { |
54 | struct reg regs; | |
55 | ||
49907934 | 56 | if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) ®s, 0) == -1) |
edefbb7c | 57 | perror_with_name (_("Couldn't get registers")); |
cced5e27 | 58 | |
56be3814 | 59 | amd64_supply_native_gregset (regcache, ®s, -1); |
cced5e27 MK |
60 | if (regnum != -1) |
61 | return; | |
62 | } | |
63 | ||
0aa37b65 | 64 | #ifdef PT_GETFSBASE |
dd6876c9 | 65 | if (regnum == -1 || regnum == tdep->fsbase_regnum) |
0aa37b65 JB |
66 | { |
67 | register_t base; | |
68 | ||
69 | if (ptrace (PT_GETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1) | |
70 | perror_with_name (_("Couldn't get segment register fs_base")); | |
71 | ||
dd6876c9 | 72 | regcache->raw_supply (tdep->fsbase_regnum, &base); |
0aa37b65 JB |
73 | if (regnum != -1) |
74 | return; | |
75 | } | |
76 | #endif | |
77 | #ifdef PT_GETGSBASE | |
dd6876c9 | 78 | if (regnum == -1 || regnum == tdep->fsbase_regnum + 1) |
0aa37b65 JB |
79 | { |
80 | register_t base; | |
81 | ||
82 | if (ptrace (PT_GETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1) | |
83 | perror_with_name (_("Couldn't get segment register gs_base")); | |
84 | ||
dd6876c9 | 85 | regcache->raw_supply (tdep->fsbase_regnum + 1, &base); |
0aa37b65 JB |
86 | if (regnum != -1) |
87 | return; | |
88 | } | |
89 | #endif | |
90 | ||
f8028488 | 91 | if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum)) |
cced5e27 MK |
92 | { |
93 | struct fpreg fpregs; | |
97de3545 | 94 | #ifdef PT_GETXSTATE_INFO |
21002a63 | 95 | void *xstateregs; |
97de3545 | 96 | |
a3405d12 | 97 | if (x86bsd_xsave_len != 0) |
97de3545 | 98 | { |
a3405d12 | 99 | xstateregs = alloca (x86bsd_xsave_len); |
49907934 JB |
100 | if (ptrace (PT_GETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, 0) |
101 | == -1) | |
97de3545 JB |
102 | perror_with_name (_("Couldn't get extended state status")); |
103 | ||
104 | amd64_supply_xsave (regcache, -1, xstateregs); | |
105 | return; | |
106 | } | |
107 | #endif | |
cced5e27 | 108 | |
49907934 | 109 | if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1) |
edefbb7c | 110 | perror_with_name (_("Couldn't get floating point status")); |
cced5e27 | 111 | |
56be3814 | 112 | amd64_supply_fxsave (regcache, -1, &fpregs); |
cced5e27 MK |
113 | } |
114 | } | |
115 | ||
116 | /* Store register REGNUM back into the inferior. If REGNUM is -1, do | |
117 | this for all registers (including the floating-point registers). */ | |
118 | ||
f6ac5f3d PA |
119 | void |
120 | amd64bsd_store_inferior_registers (struct regcache *regcache, int regnum) | |
cced5e27 | 121 | { |
ac7936df | 122 | struct gdbarch *gdbarch = regcache->arch (); |
222312d3 | 123 | pid_t pid = get_ptrace_pid (regcache->ptid ()); |
dd6876c9 JB |
124 | #if defined(PT_SETFSBASE) || defined(PT_SETGSBASE) |
125 | const struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
126 | #endif | |
f8028488 MD |
127 | |
128 | if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum)) | |
cced5e27 MK |
129 | { |
130 | struct reg regs; | |
131 | ||
49907934 | 132 | if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) ®s, 0) == -1) |
edefbb7c | 133 | perror_with_name (_("Couldn't get registers")); |
cced5e27 | 134 | |
56be3814 | 135 | amd64_collect_native_gregset (regcache, ®s, regnum); |
cced5e27 | 136 | |
49907934 | 137 | if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) ®s, 0) == -1) |
edefbb7c | 138 | perror_with_name (_("Couldn't write registers")); |
cced5e27 MK |
139 | |
140 | if (regnum != -1) | |
141 | return; | |
142 | } | |
143 | ||
0aa37b65 | 144 | #ifdef PT_SETFSBASE |
dd6876c9 | 145 | if (regnum == -1 || regnum == tdep->fsbase_regnum) |
0aa37b65 JB |
146 | { |
147 | register_t base; | |
148 | ||
dd6876c9 JB |
149 | /* Clear the full base value to support 32-bit targets. */ |
150 | base = 0; | |
151 | regcache->raw_collect (tdep->fsbase_regnum, &base); | |
0aa37b65 JB |
152 | |
153 | if (ptrace (PT_SETFSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1) | |
154 | perror_with_name (_("Couldn't write segment register fs_base")); | |
155 | if (regnum != -1) | |
156 | return; | |
157 | } | |
158 | #endif | |
159 | #ifdef PT_SETGSBASE | |
dd6876c9 | 160 | if (regnum == -1 || regnum == tdep->fsbase_regnum + 1) |
0aa37b65 JB |
161 | { |
162 | register_t base; | |
163 | ||
dd6876c9 JB |
164 | /* Clear the full base value to support 32-bit targets. */ |
165 | base = 0; | |
166 | regcache->raw_collect (tdep->fsbase_regnum + 1, &base); | |
0aa37b65 JB |
167 | |
168 | if (ptrace (PT_SETGSBASE, pid, (PTRACE_TYPE_ARG3) &base, 0) == -1) | |
169 | perror_with_name (_("Couldn't write segment register gs_base")); | |
170 | if (regnum != -1) | |
171 | return; | |
172 | } | |
173 | #endif | |
174 | ||
f8028488 | 175 | if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum)) |
cced5e27 MK |
176 | { |
177 | struct fpreg fpregs; | |
97de3545 | 178 | #ifdef PT_GETXSTATE_INFO |
21002a63 | 179 | void *xstateregs; |
97de3545 | 180 | |
a3405d12 | 181 | if (x86bsd_xsave_len != 0) |
97de3545 | 182 | { |
a3405d12 | 183 | xstateregs = alloca (x86bsd_xsave_len); |
49907934 JB |
184 | if (ptrace (PT_GETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, 0) |
185 | == -1) | |
97de3545 JB |
186 | perror_with_name (_("Couldn't get extended state status")); |
187 | ||
188 | amd64_collect_xsave (regcache, regnum, xstateregs, 0); | |
189 | ||
49907934 JB |
190 | if (ptrace (PT_SETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, |
191 | x86bsd_xsave_len) == -1) | |
97de3545 JB |
192 | perror_with_name (_("Couldn't write extended state status")); |
193 | return; | |
194 | } | |
195 | #endif | |
cced5e27 | 196 | |
49907934 | 197 | if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1) |
edefbb7c | 198 | perror_with_name (_("Couldn't get floating point status")); |
cced5e27 | 199 | |
56be3814 | 200 | amd64_collect_fxsave (regcache, regnum, &fpregs); |
cced5e27 | 201 | |
49907934 | 202 | if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1) |
edefbb7c | 203 | perror_with_name (_("Couldn't write floating point status")); |
cced5e27 MK |
204 | } |
205 | } |