2 * SPDX-License-Identifier: BSD-2-Clause
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>
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 <machine/frame.h>
43 #include <machine/md_var.h>
44 #include <machine/pcb.h>
47 get_segbase(struct segment_descriptor *sdp)
49 return (sdp->sd_hibase << 24 | sdp->sd_lobase);
53 get_segbases(struct regset *rs, struct thread *td, void *buf,
56 struct segbasereg *reg;
59 KASSERT(*sizep == sizeof(*reg), ("%s: invalid size", __func__));
61 reg->r_fsbase = get_segbase(&td->td_pcb->pcb_fsd);
62 reg->r_gsbase = get_segbase(&td->td_pcb->pcb_gsd);
64 *sizep = sizeof(*reg);
69 set_segbases(struct regset *rs, struct thread *td, void *buf,
72 struct segbasereg *reg;
74 KASSERT(size == sizeof(*reg), ("%s: invalid size", __func__));
77 fill_based_sd(&td->td_pcb->pcb_fsd, reg->r_fsbase);
78 td->td_frame->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
79 fill_based_sd(&td->td_pcb->pcb_gsd, reg->r_gsbase);
80 td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
85 static struct regset regset_segbases = {
86 .note = NT_X86_SEGBASES,
87 .size = sizeof(struct segbasereg),
91 ELF_REGSET(regset_segbases);
94 cpu_ptrace_xstate(struct thread *td, int req, void *addr, int data)
96 struct ptrace_xstate_info info;
104 case PT_GETXSTATE_OLD:
106 savefpu = (char *)(get_pcb_user_save_td(td) + 1);
107 error = copyout(savefpu, addr,
108 cpu_max_ext_state_size - sizeof(union savefpu));
111 case PT_SETXSTATE_OLD:
112 if (data > cpu_max_ext_state_size - sizeof(union savefpu)) {
116 savefpu = malloc(data, M_TEMP, M_WAITOK);
117 error = copyin(addr, savefpu, data);
120 error = npxsetxstate(td, savefpu, data);
122 free(savefpu, M_TEMP);
125 case PT_GETXSTATE_INFO:
126 if (data != sizeof(info)) {
130 info.xsave_len = cpu_max_ext_state_size;
131 info.xsave_mask = xsave_mask;
132 error = copyout(&info, addr, data);
137 savefpu = (char *)(get_pcb_user_save_td(td));
138 error = copyout(savefpu, addr, cpu_max_ext_state_size);
142 if (data < sizeof(union savefpu) ||
143 data > cpu_max_ext_state_size) {
147 savefpu = malloc(data, M_TEMP, M_WAITOK);
148 error = copyin(addr, savefpu, data);
150 error = npxsetregs(td, (union savefpu *)savefpu,
151 savefpu + sizeof(union savefpu), data -
152 sizeof(union savefpu));
153 free(savefpu, M_TEMP);
165 cpu_ptrace_xmm(struct thread *td, int req, void *addr, int data)
167 struct savexmm *fpstate;
173 fpstate = &get_pcb_user_save_td(td)->sv_xmm;
177 error = copyout(fpstate, addr, sizeof(*fpstate));
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);
202 cpu_ptrace(struct thread *td, int req, void *addr, int data)
204 struct segment_descriptor *sdp, sd;
211 case PT_GETXSTATE_OLD:
212 case PT_SETXSTATE_OLD:
213 case PT_GETXSTATE_INFO:
216 error = cpu_ptrace_xmm(td, req, addr, data);
221 sdp = req == PT_GETFSBASE ? &td->td_pcb->pcb_fsd :
222 &td->td_pcb->pcb_gsd;
223 r = get_segbase(sdp);
224 error = copyout(&r, addr, sizeof(r));
229 error = copyin(addr, &r, sizeof(r));
232 fill_based_sd(&sd, r);
233 if (req == PT_SETFSBASE) {
234 td->td_pcb->pcb_fsd = sd;
235 td->td_frame->tf_fs = GSEL(GUFS_SEL, SEL_UPL);
237 td->td_pcb->pcb_gsd = sd;
238 td->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
250 ptrace_set_pc(struct thread *td, u_long addr)
253 td->td_frame->tf_eip = addr;
258 ptrace_single_step(struct thread *td)
261 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
262 if ((td->td_frame->tf_eflags & PSL_T) == 0) {
263 td->td_frame->tf_eflags |= PSL_T;
264 td->td_dbgflags |= TDB_STEP;
270 ptrace_clear_single_step(struct thread *td)
273 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
274 td->td_frame->tf_eflags &= ~PSL_T;
275 td->td_dbgflags &= ~TDB_STEP;