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>
35 #include <sys/malloc.h>
37 #include <sys/ptrace.h>
38 #include <sys/sysent.h>
41 #include <machine/md_var.h>
42 #include <machine/pcb.h>
43 #include <machine/frame.h>
44 #include <machine/vmparam.h>
46 #ifdef COMPAT_FREEBSD32
47 struct ptrace_xstate_info32 {
48 uint32_t xsave_mask1, xsave_mask2;
54 cpu_ptrace_xstate(struct thread *td, int req, void *addr, int data)
56 struct ptrace_xstate_info info;
57 #ifdef COMPAT_FREEBSD32
58 struct ptrace_xstate_info32 info32;
67 case PT_GETXSTATE_OLD:
69 savefpu = (char *)(get_pcb_user_save_td(td) + 1);
70 error = copyout(savefpu, addr,
71 cpu_max_ext_state_size - sizeof(struct savefpu));
74 case PT_SETXSTATE_OLD:
75 if (data > cpu_max_ext_state_size - sizeof(struct savefpu)) {
79 savefpu = malloc(data, M_TEMP, M_WAITOK);
80 error = copyin(addr, savefpu, data);
83 error = fpusetxstate(td, savefpu, data);
85 free(savefpu, M_TEMP);
88 case PT_GETXSTATE_INFO:
89 #ifdef COMPAT_FREEBSD32
90 if (SV_CURPROC_FLAG(SV_ILP32)) {
91 if (data != sizeof(info32)) {
94 info32.xsave_len = cpu_max_ext_state_size;
95 info32.xsave_mask1 = xsave_mask;
96 info32.xsave_mask2 = xsave_mask >> 32;
97 error = copyout(&info32, addr, data);
102 if (data != sizeof(info)) {
105 bzero(&info, sizeof(info));
106 info.xsave_len = cpu_max_ext_state_size;
107 info.xsave_mask = xsave_mask;
108 error = copyout(&info, addr, data);
115 savefpu = (char *)(get_pcb_user_save_td(td));
116 error = copyout(savefpu, addr, cpu_max_ext_state_size);
120 if (data < sizeof(struct savefpu) ||
121 data > cpu_max_ext_state_size) {
125 savefpu = malloc(data, M_TEMP, M_WAITOK);
126 error = copyin(addr, savefpu, data);
128 error = fpusetregs(td, (struct savefpu *)savefpu,
129 savefpu + sizeof(struct savefpu), data -
130 sizeof(struct savefpu));
131 free(savefpu, M_TEMP);
143 cpu_ptrace_setbase(struct thread *td, int req, register_t r)
148 set_pcb_flags(pcb, PCB_FULL_IRET);
149 if (req == PT_SETFSBASE) {
151 td->td_frame->tf_fs = _ufssel;
154 td->td_frame->tf_gs = _ugssel;
158 #ifdef COMPAT_FREEBSD32
159 #define PT_I386_GETXMMREGS (PT_FIRSTMACH + 0)
160 #define PT_I386_SETXMMREGS (PT_FIRSTMACH + 1)
163 cpu32_ptrace(struct thread *td, int req, void *addr, int data)
165 struct savefpu *fpstate;
171 case PT_I386_GETXMMREGS:
173 error = copyout(get_pcb_user_save_td(td), addr,
177 case PT_I386_SETXMMREGS:
179 fpstate = get_pcb_user_save_td(td);
180 error = copyin(addr, fpstate, sizeof(*fpstate));
181 fpstate->sv_env.en_mxcsr &= cpu_mxcsr_mask;
184 case PT_GETXSTATE_OLD:
185 case PT_SETXSTATE_OLD:
186 case PT_GETXSTATE_INFO:
189 error = cpu_ptrace_xstate(td, req, addr, data);
194 if (!SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
200 update_pcb_bases(pcb);
201 r = req == PT_GETFSBASE ? pcb->pcb_fsbase : pcb->pcb_gsbase;
202 error = copyout(&r, addr, sizeof(r));
207 if (!SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
211 error = copyin(addr, &r, sizeof(r));
214 cpu_ptrace_setbase(td, req, r);
227 cpu_ptrace(struct thread *td, int req, void *addr, int data)
233 #ifdef COMPAT_FREEBSD32
234 if (SV_CURPROC_FLAG(SV_ILP32))
235 return (cpu32_ptrace(td, req, addr, data));
238 /* Support old values of PT_GETXSTATE_OLD and PT_SETXSTATE_OLD. */
239 if (req == PT_FIRSTMACH + 0)
240 req = PT_GETXSTATE_OLD;
241 if (req == PT_FIRSTMACH + 1)
242 req = PT_SETXSTATE_OLD;
245 case PT_GETXSTATE_OLD:
246 case PT_SETXSTATE_OLD:
247 case PT_GETXSTATE_INFO:
250 error = cpu_ptrace_xstate(td, req, addr, data);
257 update_pcb_bases(pcb);
258 r = req == PT_GETFSBASE ? &pcb->pcb_fsbase : &pcb->pcb_gsbase;
259 error = copyout(r, addr, sizeof(*r));
264 error = copyin(addr, &rv, sizeof(rv));
267 if (rv >= td->td_proc->p_sysent->sv_maxuser) {
271 cpu_ptrace_setbase(td, req, rv);