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>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
42 #include <sys/ptrace.h>
44 #include <machine/frame.h>
45 #include <machine/md_var.h>
46 #include <machine/pcb.h>
49 get_segbase(struct segment_descriptor *sdp)
51 return (sdp->sd_hibase << 24 | sdp->sd_lobase);
55 get_segbases(struct regset *rs, struct thread *td, void *buf,
58 struct segbasereg *reg;
61 KASSERT(*sizep == sizeof(*reg), ("%s: invalid size", __func__));
63 reg->r_fsbase = get_segbase(&td->td_pcb->pcb_fsd);
64 reg->r_gsbase = get_segbase(&td->td_pcb->pcb_gsd);
66 *sizep = sizeof(*reg);
71 set_segbases(struct regset *rs, struct thread *td, void *buf,
74 struct segbasereg *reg;
76 KASSERT(size == sizeof(*reg), ("%s: invalid size", __func__));
79 fill_based_sd(&td->td_pcb->pcb_fsd, reg->r_fsbase);
80 td->td_frame->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
81 fill_based_sd(&td->td_pcb->pcb_gsd, reg->r_gsbase);
82 td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
87 static struct regset regset_segbases = {
88 .note = NT_X86_SEGBASES,
89 .size = sizeof(struct segbasereg),
93 ELF_REGSET(regset_segbases);
96 cpu_ptrace_xstate(struct thread *td, int req, void *addr, int data)
98 struct ptrace_xstate_info info;
106 case PT_GETXSTATE_OLD:
108 savefpu = (char *)(get_pcb_user_save_td(td) + 1);
109 error = copyout(savefpu, addr,
110 cpu_max_ext_state_size - sizeof(union savefpu));
113 case PT_SETXSTATE_OLD:
114 if (data > cpu_max_ext_state_size - sizeof(union savefpu)) {
118 savefpu = malloc(data, M_TEMP, M_WAITOK);
119 error = copyin(addr, savefpu, data);
122 error = npxsetxstate(td, savefpu, data);
124 free(savefpu, M_TEMP);
127 case PT_GETXSTATE_INFO:
128 if (data != sizeof(info)) {
132 info.xsave_len = cpu_max_ext_state_size;
133 info.xsave_mask = xsave_mask;
134 error = copyout(&info, addr, data);
139 savefpu = (char *)(get_pcb_user_save_td(td));
140 error = copyout(savefpu, addr, cpu_max_ext_state_size);
144 if (data < sizeof(union savefpu) ||
145 data > cpu_max_ext_state_size) {
149 savefpu = malloc(data, M_TEMP, M_WAITOK);
150 error = copyin(addr, savefpu, data);
152 error = npxsetregs(td, (union savefpu *)savefpu,
153 savefpu + sizeof(union savefpu), data -
154 sizeof(union savefpu));
155 free(savefpu, M_TEMP);
167 cpu_ptrace_xmm(struct thread *td, int req, void *addr, int data)
169 struct savexmm *fpstate;
175 fpstate = &get_pcb_user_save_td(td)->sv_xmm;
179 error = copyout(fpstate, addr, sizeof(*fpstate));
184 error = copyin(addr, fpstate, sizeof(*fpstate));
185 fpstate->sv_env.en_mxcsr &= cpu_mxcsr_mask;
188 case PT_GETXSTATE_OLD:
189 case PT_SETXSTATE_OLD:
190 case PT_GETXSTATE_INFO:
193 error = cpu_ptrace_xstate(td, req, addr, data);
204 cpu_ptrace(struct thread *td, int req, void *addr, int data)
206 struct segment_descriptor *sdp, sd;
213 case PT_GETXSTATE_OLD:
214 case PT_SETXSTATE_OLD:
215 case PT_GETXSTATE_INFO:
218 error = cpu_ptrace_xmm(td, req, addr, data);
223 sdp = req == PT_GETFSBASE ? &td->td_pcb->pcb_fsd :
224 &td->td_pcb->pcb_gsd;
225 r = get_segbase(sdp);
226 error = copyout(&r, addr, sizeof(r));
231 error = copyin(addr, &r, sizeof(r));
234 fill_based_sd(&sd, r);
235 if (req == PT_SETFSBASE) {
236 td->td_pcb->pcb_fsd = sd;
237 td->td_frame->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
239 td->td_pcb->pcb_gsd = sd;
240 td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
252 ptrace_set_pc(struct thread *td, u_long addr)
255 td->td_frame->tf_eip = addr;
260 ptrace_single_step(struct thread *td)
263 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
264 if ((td->td_frame->tf_eflags & PSL_T) == 0) {
265 td->td_frame->tf_eflags |= PSL_T;
266 td->td_dbgflags |= TDB_STEP;
272 ptrace_clear_single_step(struct thread *td)
275 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
276 td->td_frame->tf_eflags &= ~PSL_T;
277 td->td_dbgflags &= ~TDB_STEP;