2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 Konstantin Belousov <kib@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
40 #include <sys/ptrace.h>
42 #include <sys/sysent.h>
45 #include <machine/md_var.h>
46 #include <machine/pcb.h>
47 #include <machine/frame.h>
48 #include <machine/vmparam.h>
50 #ifdef COMPAT_FREEBSD32
51 struct ptrace_xstate_info32 {
52 uint32_t xsave_mask1, xsave_mask2;
58 get_segbases(struct regset *rs, struct thread *td, void *buf,
61 struct segbasereg *reg;
65 KASSERT(*sizep == sizeof(*reg), ("%s: invalid size", __func__));
70 update_pcb_bases(pcb);
71 reg->r_fsbase = pcb->pcb_fsbase;
72 reg->r_gsbase = pcb->pcb_gsbase;
74 *sizep = sizeof(*reg);
79 set_segbases(struct regset *rs, struct thread *td, void *buf,
82 struct segbasereg *reg;
85 KASSERT(size == sizeof(*reg), ("%s: invalid size", __func__));
89 set_pcb_flags(pcb, PCB_FULL_IRET);
90 pcb->pcb_fsbase = reg->r_fsbase;
91 td->td_frame->tf_fs = _ufssel;
92 pcb->pcb_gsbase = reg->r_gsbase;
93 td->td_frame->tf_gs = _ugssel;
98 static struct regset regset_segbases = {
99 .note = NT_X86_SEGBASES,
100 .size = sizeof(struct segbasereg),
104 ELF_REGSET(regset_segbases);
106 #ifdef COMPAT_FREEBSD32
108 get_segbases32(struct regset *rs, struct thread *td, void *buf,
111 struct segbasereg32 *reg;
115 KASSERT(*sizep == sizeof(*reg), ("%s: invalid size", __func__));
120 update_pcb_bases(pcb);
121 reg->r_fsbase = (uint32_t)pcb->pcb_fsbase;
122 reg->r_gsbase = (uint32_t)pcb->pcb_gsbase;
124 *sizep = sizeof(*reg);
129 set_segbases32(struct regset *rs, struct thread *td, void *buf,
132 struct segbasereg32 *reg;
135 KASSERT(size == sizeof(*reg), ("%s: invalid size", __func__));
139 set_pcb_flags(pcb, PCB_FULL_IRET);
140 pcb->pcb_fsbase = reg->r_fsbase;
141 td->td_frame->tf_fs = _ufssel;
142 pcb->pcb_gsbase = reg->r_gsbase;
143 td->td_frame->tf_gs = _ugssel;
148 static struct regset regset_segbases32 = {
149 .note = NT_X86_SEGBASES,
150 .size = sizeof(struct segbasereg32),
151 .get = get_segbases32,
152 .set = set_segbases32,
154 ELF32_REGSET(regset_segbases32);
158 cpu_ptrace_xstate(struct thread *td, int req, void *addr, int data)
160 struct ptrace_xstate_info info;
161 #ifdef COMPAT_FREEBSD32
162 struct ptrace_xstate_info32 info32;
171 case PT_GETXSTATE_OLD:
173 savefpu = (char *)(get_pcb_user_save_td(td) + 1);
174 error = copyout(savefpu, addr,
175 cpu_max_ext_state_size - sizeof(struct savefpu));
178 case PT_SETXSTATE_OLD:
179 if (data > cpu_max_ext_state_size - sizeof(struct savefpu)) {
183 savefpu = malloc(data, M_TEMP, M_WAITOK);
184 error = copyin(addr, savefpu, data);
187 error = fpusetxstate(td, savefpu, data);
189 free(savefpu, M_TEMP);
192 case PT_GETXSTATE_INFO:
193 #ifdef COMPAT_FREEBSD32
194 if (SV_CURPROC_FLAG(SV_ILP32)) {
195 if (data != sizeof(info32)) {
198 info32.xsave_len = cpu_max_ext_state_size;
199 info32.xsave_mask1 = xsave_mask;
200 info32.xsave_mask2 = xsave_mask >> 32;
201 error = copyout(&info32, addr, data);
206 if (data != sizeof(info)) {
209 bzero(&info, sizeof(info));
210 info.xsave_len = cpu_max_ext_state_size;
211 info.xsave_mask = xsave_mask;
212 error = copyout(&info, addr, data);
219 savefpu = (char *)(get_pcb_user_save_td(td));
220 error = copyout(savefpu, addr, cpu_max_ext_state_size);
224 if (data < sizeof(struct savefpu) ||
225 data > cpu_max_ext_state_size) {
229 savefpu = malloc(data, M_TEMP, M_WAITOK);
230 error = copyin(addr, savefpu, data);
232 error = fpusetregs(td, (struct savefpu *)savefpu,
233 savefpu + sizeof(struct savefpu), data -
234 sizeof(struct savefpu));
235 free(savefpu, M_TEMP);
247 cpu_ptrace_setbase(struct thread *td, int req, register_t r)
252 set_pcb_flags(pcb, PCB_FULL_IRET);
253 if (req == PT_SETFSBASE) {
255 td->td_frame->tf_fs = _ufssel;
258 td->td_frame->tf_gs = _ugssel;
262 #ifdef COMPAT_FREEBSD32
263 #define PT_I386_GETXMMREGS (PT_FIRSTMACH + 0)
264 #define PT_I386_SETXMMREGS (PT_FIRSTMACH + 1)
267 cpu32_ptrace(struct thread *td, int req, void *addr, int data)
269 struct savefpu *fpstate;
275 case PT_I386_GETXMMREGS:
277 error = copyout(get_pcb_user_save_td(td), addr,
281 case PT_I386_SETXMMREGS:
283 fpstate = get_pcb_user_save_td(td);
284 error = copyin(addr, fpstate, sizeof(*fpstate));
285 fpstate->sv_env.en_mxcsr &= cpu_mxcsr_mask;
288 case PT_GETXSTATE_OLD:
289 case PT_SETXSTATE_OLD:
290 case PT_GETXSTATE_INFO:
293 error = cpu_ptrace_xstate(td, req, addr, data);
298 if (!SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
304 update_pcb_bases(pcb);
305 r = req == PT_GETFSBASE ? pcb->pcb_fsbase : pcb->pcb_gsbase;
306 error = copyout(&r, addr, sizeof(r));
311 if (!SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
315 error = copyin(addr, &r, sizeof(r));
318 cpu_ptrace_setbase(td, req, r);
331 cpu_ptrace(struct thread *td, int req, void *addr, int data)
337 #ifdef COMPAT_FREEBSD32
338 if (SV_CURPROC_FLAG(SV_ILP32))
339 return (cpu32_ptrace(td, req, addr, data));
342 /* Support old values of PT_GETXSTATE_OLD and PT_SETXSTATE_OLD. */
343 if (req == PT_FIRSTMACH + 0)
344 req = PT_GETXSTATE_OLD;
345 if (req == PT_FIRSTMACH + 1)
346 req = PT_SETXSTATE_OLD;
349 case PT_GETXSTATE_OLD:
350 case PT_SETXSTATE_OLD:
351 case PT_GETXSTATE_INFO:
354 error = cpu_ptrace_xstate(td, req, addr, data);
361 update_pcb_bases(pcb);
362 r = req == PT_GETFSBASE ? &pcb->pcb_fsbase : &pcb->pcb_gsbase;
363 error = copyout(r, addr, sizeof(*r));
368 error = copyin(addr, &rv, sizeof(rv));
371 if (rv >= td->td_proc->p_sysent->sv_maxuser) {
375 cpu_ptrace_setbase(td, req, rv);
387 ptrace_set_pc(struct thread *td, unsigned long addr)
390 td->td_frame->tf_rip = addr;
391 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
396 ptrace_single_step(struct thread *td)
399 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
400 if ((td->td_frame->tf_rflags & PSL_T) == 0) {
401 td->td_frame->tf_rflags |= PSL_T;
402 td->td_dbgflags |= TDB_STEP;
408 ptrace_clear_single_step(struct thread *td)
411 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
412 td->td_frame->tf_rflags &= ~PSL_T;
413 td->td_dbgflags &= ~TDB_STEP;