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>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
39 #include <sys/ptrace.h>
40 #include <sys/sysent.h>
43 #include <machine/md_var.h>
44 #include <machine/pcb.h>
45 #include <machine/frame.h>
46 #include <machine/vmparam.h>
48 #ifdef COMPAT_FREEBSD32
49 struct ptrace_xstate_info32 {
50 uint32_t xsave_mask1, xsave_mask2;
56 cpu_ptrace_xstate(struct thread *td, int req, void *addr, int data)
58 struct ptrace_xstate_info info;
59 #ifdef COMPAT_FREEBSD32
60 struct ptrace_xstate_info32 info32;
69 case PT_GETXSTATE_OLD:
71 savefpu = (char *)(get_pcb_user_save_td(td) + 1);
72 error = copyout(savefpu, addr,
73 cpu_max_ext_state_size - sizeof(struct savefpu));
76 case PT_SETXSTATE_OLD:
77 if (data > cpu_max_ext_state_size - sizeof(struct savefpu)) {
81 savefpu = malloc(data, M_TEMP, M_WAITOK);
82 error = copyin(addr, savefpu, data);
85 error = fpusetxstate(td, savefpu, data);
87 free(savefpu, M_TEMP);
90 case PT_GETXSTATE_INFO:
91 #ifdef COMPAT_FREEBSD32
92 if (SV_CURPROC_FLAG(SV_ILP32)) {
93 if (data != sizeof(info32)) {
96 info32.xsave_len = cpu_max_ext_state_size;
97 info32.xsave_mask1 = xsave_mask;
98 info32.xsave_mask2 = xsave_mask >> 32;
99 error = copyout(&info32, addr, data);
104 if (data != sizeof(info)) {
107 bzero(&info, sizeof(info));
108 info.xsave_len = cpu_max_ext_state_size;
109 info.xsave_mask = xsave_mask;
110 error = copyout(&info, addr, data);
117 savefpu = (char *)(get_pcb_user_save_td(td));
118 error = copyout(savefpu, addr, cpu_max_ext_state_size);
122 if (data < sizeof(struct savefpu) ||
123 data > cpu_max_ext_state_size) {
127 savefpu = malloc(data, M_TEMP, M_WAITOK);
128 error = copyin(addr, savefpu, data);
130 error = fpusetregs(td, (struct savefpu *)savefpu,
131 savefpu + sizeof(struct savefpu), data -
132 sizeof(struct savefpu));
133 free(savefpu, M_TEMP);
145 cpu_ptrace_setbase(struct thread *td, int req, register_t r)
150 set_pcb_flags(pcb, PCB_FULL_IRET);
151 if (req == PT_SETFSBASE) {
153 td->td_frame->tf_fs = _ufssel;
156 td->td_frame->tf_gs = _ugssel;
160 #ifdef COMPAT_FREEBSD32
161 #define PT_I386_GETXMMREGS (PT_FIRSTMACH + 0)
162 #define PT_I386_SETXMMREGS (PT_FIRSTMACH + 1)
165 cpu32_ptrace(struct thread *td, int req, void *addr, int data)
167 struct savefpu *fpstate;
173 case PT_I386_GETXMMREGS:
175 error = copyout(get_pcb_user_save_td(td), addr,
179 case PT_I386_SETXMMREGS:
181 fpstate = get_pcb_user_save_td(td);
182 error = copyin(addr, fpstate, sizeof(*fpstate));
183 fpstate->sv_env.en_mxcsr &= cpu_mxcsr_mask;
186 case PT_GETXSTATE_OLD:
187 case PT_SETXSTATE_OLD:
188 case PT_GETXSTATE_INFO:
191 error = cpu_ptrace_xstate(td, req, addr, data);
196 if (!SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
202 update_pcb_bases(pcb);
203 r = req == PT_GETFSBASE ? pcb->pcb_fsbase : pcb->pcb_gsbase;
204 error = copyout(&r, addr, sizeof(r));
209 if (!SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
213 error = copyin(addr, &r, sizeof(r));
216 cpu_ptrace_setbase(td, req, r);
229 cpu_ptrace(struct thread *td, int req, void *addr, int data)
235 #ifdef COMPAT_FREEBSD32
236 if (SV_CURPROC_FLAG(SV_ILP32))
237 return (cpu32_ptrace(td, req, addr, data));
240 /* Support old values of PT_GETXSTATE_OLD and PT_SETXSTATE_OLD. */
241 if (req == PT_FIRSTMACH + 0)
242 req = PT_GETXSTATE_OLD;
243 if (req == PT_FIRSTMACH + 1)
244 req = PT_SETXSTATE_OLD;
247 case PT_GETXSTATE_OLD:
248 case PT_SETXSTATE_OLD:
249 case PT_GETXSTATE_INFO:
252 error = cpu_ptrace_xstate(td, req, addr, data);
259 update_pcb_bases(pcb);
260 r = req == PT_GETFSBASE ? &pcb->pcb_fsbase : &pcb->pcb_gsbase;
261 error = copyout(r, addr, sizeof(*r));
266 error = copyin(addr, &rv, sizeof(rv));
269 if (rv >= td->td_proc->p_sysent->sv_maxuser) {
273 cpu_ptrace_setbase(td, req, rv);
285 ptrace_set_pc(struct thread *td, unsigned long addr)
288 td->td_frame->tf_rip = addr;
289 set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
294 ptrace_single_step(struct thread *td)
297 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
298 if ((td->td_frame->tf_rflags & PSL_T) == 0) {
299 td->td_frame->tf_rflags |= PSL_T;
300 td->td_dbgflags |= TDB_STEP;
306 ptrace_clear_single_step(struct thread *td)
309 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
310 td->td_frame->tf_rflags &= ~PSL_T;
311 td->td_dbgflags &= ~TDB_STEP;