2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 Doug Rabson
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$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
39 #include <sys/ptrace.h>
40 #include <machine/frame.h>
41 #include <machine/md_var.h>
42 #include <machine/pcb.h>
45 cpu_ptrace_xstate(struct thread *td, int req, void *addr, int data)
47 struct ptrace_xstate_info info;
55 case PT_GETXSTATE_OLD:
57 savefpu = (char *)(get_pcb_user_save_td(td) + 1);
58 error = copyout(savefpu, addr,
59 cpu_max_ext_state_size - sizeof(union savefpu));
62 case PT_SETXSTATE_OLD:
63 if (data > cpu_max_ext_state_size - sizeof(union savefpu)) {
67 savefpu = malloc(data, M_TEMP, M_WAITOK);
68 error = copyin(addr, savefpu, data);
71 error = npxsetxstate(td, savefpu, data);
73 free(savefpu, M_TEMP);
76 case PT_GETXSTATE_INFO:
77 if (data != sizeof(info)) {
81 info.xsave_len = cpu_max_ext_state_size;
82 info.xsave_mask = xsave_mask;
83 error = copyout(&info, addr, data);
88 savefpu = (char *)(get_pcb_user_save_td(td));
89 error = copyout(savefpu, addr, cpu_max_ext_state_size);
93 if (data < sizeof(union savefpu) ||
94 data > cpu_max_ext_state_size) {
98 savefpu = malloc(data, M_TEMP, M_WAITOK);
99 error = copyin(addr, savefpu, data);
101 error = npxsetregs(td, (union savefpu *)savefpu,
102 savefpu + sizeof(union savefpu), data -
103 sizeof(union savefpu));
104 free(savefpu, M_TEMP);
116 cpu_ptrace_xmm(struct thread *td, int req, void *addr, int data)
118 struct savexmm *fpstate;
124 fpstate = &get_pcb_user_save_td(td)->sv_xmm;
128 error = copyout(fpstate, addr, sizeof(*fpstate));
133 error = copyin(addr, fpstate, sizeof(*fpstate));
134 fpstate->sv_env.en_mxcsr &= cpu_mxcsr_mask;
137 case PT_GETXSTATE_OLD:
138 case PT_SETXSTATE_OLD:
139 case PT_GETXSTATE_INFO:
142 error = cpu_ptrace_xstate(td, req, addr, data);
153 cpu_ptrace(struct thread *td, int req, void *addr, int data)
155 struct segment_descriptor *sdp, sd;
162 case PT_GETXSTATE_OLD:
163 case PT_SETXSTATE_OLD:
164 case PT_GETXSTATE_INFO:
167 error = cpu_ptrace_xmm(td, req, addr, data);
172 sdp = req == PT_GETFSBASE ? &td->td_pcb->pcb_fsd :
173 &td->td_pcb->pcb_gsd;
174 r = sdp->sd_hibase << 24 | sdp->sd_lobase;
175 error = copyout(&r, addr, sizeof(r));
180 error = copyin(addr, &r, sizeof(r));
183 fill_based_sd(&sd, r);
184 if (req == PT_SETFSBASE) {
185 td->td_pcb->pcb_fsd = sd;
186 td->td_frame->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
188 td->td_pcb->pcb_gsd = sd;
189 td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);