2 * Copyright (c) 2014 Andrew Turner
3 * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com>
6 * Portions of this software were developed by SRI International and the
7 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
8 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
10 * Portions of this software were developed by the University of Cambridge
11 * Computer Laboratory as part of the CTSRD Project, with support from the
12 * UK Higher Education Innovation Fund (HEIF).
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/imgact.h>
41 #include <sys/kernel.h>
43 #include <sys/limits.h>
45 #include <sys/mutex.h>
47 #include <sys/ptrace.h>
49 #include <sys/rwlock.h>
50 #include <sys/sched.h>
51 #include <sys/signalvar.h>
52 #include <sys/syscallsubr.h>
53 #include <sys/sysent.h>
54 #include <sys/sysproto.h>
55 #include <sys/ucontext.h>
57 #include <machine/cpu.h>
58 #include <machine/fpe.h>
59 #include <machine/kdb.h>
60 #include <machine/pcb.h>
61 #include <machine/pte.h>
62 #include <machine/riscvreg.h>
63 #include <machine/sbi.h>
64 #include <machine/trap.h>
67 #include <vm/vm_param.h>
69 #include <vm/vm_map.h>
71 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
72 static void set_fpcontext(struct thread *td, mcontext_t *mcp);
74 _Static_assert(sizeof(mcontext_t) == 864, "mcontext_t size incorrect");
75 _Static_assert(sizeof(ucontext_t) == 936, "ucontext_t size incorrect");
76 _Static_assert(sizeof(siginfo_t) == 80, "siginfo_t size incorrect");
79 fill_regs(struct thread *td, struct reg *regs)
81 struct trapframe *frame;
84 regs->sepc = frame->tf_sepc;
85 regs->sstatus = frame->tf_sstatus;
86 regs->ra = frame->tf_ra;
87 regs->sp = frame->tf_sp;
88 regs->gp = frame->tf_gp;
89 regs->tp = frame->tf_tp;
91 memcpy(regs->t, frame->tf_t, sizeof(regs->t));
92 memcpy(regs->s, frame->tf_s, sizeof(regs->s));
93 memcpy(regs->a, frame->tf_a, sizeof(regs->a));
99 set_regs(struct thread *td, struct reg *regs)
101 struct trapframe *frame;
103 frame = td->td_frame;
104 frame->tf_sepc = regs->sepc;
105 frame->tf_ra = regs->ra;
106 frame->tf_sp = regs->sp;
107 frame->tf_gp = regs->gp;
108 frame->tf_tp = regs->tp;
110 memcpy(frame->tf_t, regs->t, sizeof(frame->tf_t));
111 memcpy(frame->tf_s, regs->s, sizeof(frame->tf_s));
112 memcpy(frame->tf_a, regs->a, sizeof(frame->tf_a));
118 fill_fpregs(struct thread *td, struct fpreg *regs)
124 if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
126 * If we have just been running FPE instructions we will
127 * need to save the state to memcpy it below.
132 memcpy(regs->fp_x, pcb->pcb_x, sizeof(regs->fp_x));
133 regs->fp_fcsr = pcb->pcb_fcsr;
135 memset(regs, 0, sizeof(*regs));
141 set_fpregs(struct thread *td, struct fpreg *regs)
143 struct trapframe *frame;
146 frame = td->td_frame;
149 memcpy(pcb->pcb_x, regs->fp_x, sizeof(regs->fp_x));
150 pcb->pcb_fcsr = regs->fp_fcsr;
151 pcb->pcb_fpflags |= PCB_FP_STARTED;
152 frame->tf_sstatus &= ~SSTATUS_FS_MASK;
153 frame->tf_sstatus |= SSTATUS_FS_CLEAN;
159 fill_dbregs(struct thread *td, struct dbreg *regs)
162 panic("fill_dbregs");
166 set_dbregs(struct thread *td, struct dbreg *regs)
173 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
175 struct trapframe *tf;
181 memset(tf, 0, sizeof(struct trapframe));
184 tf->tf_sp = STACKALIGN(stack);
185 tf->tf_ra = imgp->entry_addr;
186 tf->tf_sepc = imgp->entry_addr;
188 pcb->pcb_fpflags &= ~PCB_FP_STARTED;
191 /* Sanity check these are the same size, they will be memcpy'd to and from */
192 CTASSERT(sizeof(((struct trapframe *)0)->tf_a) ==
193 sizeof((struct gpregs *)0)->gp_a);
194 CTASSERT(sizeof(((struct trapframe *)0)->tf_s) ==
195 sizeof((struct gpregs *)0)->gp_s);
196 CTASSERT(sizeof(((struct trapframe *)0)->tf_t) ==
197 sizeof((struct gpregs *)0)->gp_t);
198 CTASSERT(sizeof(((struct trapframe *)0)->tf_a) ==
199 sizeof((struct reg *)0)->a);
200 CTASSERT(sizeof(((struct trapframe *)0)->tf_s) ==
201 sizeof((struct reg *)0)->s);
202 CTASSERT(sizeof(((struct trapframe *)0)->tf_t) ==
203 sizeof((struct reg *)0)->t);
206 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
208 struct trapframe *tf = td->td_frame;
210 memcpy(mcp->mc_gpregs.gp_t, tf->tf_t, sizeof(mcp->mc_gpregs.gp_t));
211 memcpy(mcp->mc_gpregs.gp_s, tf->tf_s, sizeof(mcp->mc_gpregs.gp_s));
212 memcpy(mcp->mc_gpregs.gp_a, tf->tf_a, sizeof(mcp->mc_gpregs.gp_a));
214 if (clear_ret & GET_MC_CLEAR_RET) {
215 mcp->mc_gpregs.gp_a[0] = 0;
216 mcp->mc_gpregs.gp_t[0] = 0; /* clear syscall error */
219 mcp->mc_gpregs.gp_ra = tf->tf_ra;
220 mcp->mc_gpregs.gp_sp = tf->tf_sp;
221 mcp->mc_gpregs.gp_gp = tf->tf_gp;
222 mcp->mc_gpregs.gp_tp = tf->tf_tp;
223 mcp->mc_gpregs.gp_sepc = tf->tf_sepc;
224 mcp->mc_gpregs.gp_sstatus = tf->tf_sstatus;
225 get_fpcontext(td, mcp);
231 set_mcontext(struct thread *td, mcontext_t *mcp)
233 struct trapframe *tf;
238 * Permit changes to the USTATUS bits of SSTATUS.
240 * Ignore writes to read-only bits (SD, XS).
242 * Ignore writes to the FS field as set_fpcontext() will set
245 if (((mcp->mc_gpregs.gp_sstatus ^ tf->tf_sstatus) &
246 ~(SSTATUS_SD | SSTATUS_XS_MASK | SSTATUS_FS_MASK | SSTATUS_UPIE |
250 memcpy(tf->tf_t, mcp->mc_gpregs.gp_t, sizeof(tf->tf_t));
251 memcpy(tf->tf_s, mcp->mc_gpregs.gp_s, sizeof(tf->tf_s));
252 memcpy(tf->tf_a, mcp->mc_gpregs.gp_a, sizeof(tf->tf_a));
254 tf->tf_ra = mcp->mc_gpregs.gp_ra;
255 tf->tf_sp = mcp->mc_gpregs.gp_sp;
256 tf->tf_gp = mcp->mc_gpregs.gp_gp;
257 tf->tf_sepc = mcp->mc_gpregs.gp_sepc;
258 tf->tf_sstatus = mcp->mc_gpregs.gp_sstatus;
259 set_fpcontext(td, mcp);
265 get_fpcontext(struct thread *td, mcontext_t *mcp)
271 curpcb = curthread->td_pcb;
273 KASSERT(td->td_pcb == curpcb, ("Invalid fpe pcb"));
275 if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
277 * If we have just been running FPE instructions we will
278 * need to save the state to memcpy it below.
282 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
283 ("Non-userspace FPE flags set in get_fpcontext"));
284 memcpy(mcp->mc_fpregs.fp_x, curpcb->pcb_x,
285 sizeof(mcp->mc_fpregs.fp_x));
286 mcp->mc_fpregs.fp_fcsr = curpcb->pcb_fcsr;
287 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
288 mcp->mc_flags |= _MC_FP_VALID;
295 set_fpcontext(struct thread *td, mcontext_t *mcp)
299 td->td_frame->tf_sstatus &= ~SSTATUS_FS_MASK;
300 td->td_frame->tf_sstatus |= SSTATUS_FS_OFF;
304 if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
305 curpcb = curthread->td_pcb;
306 /* FPE usage is enabled, override registers. */
307 memcpy(curpcb->pcb_x, mcp->mc_fpregs.fp_x,
308 sizeof(mcp->mc_fpregs.fp_x));
309 curpcb->pcb_fcsr = mcp->mc_fpregs.fp_fcsr;
310 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
311 td->td_frame->tf_sstatus |= SSTATUS_FS_CLEAN;
318 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
323 if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
326 error = set_mcontext(td, &uc.uc_mcontext);
330 /* Restore signal mask. */
331 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
333 return (EJUSTRETURN);
337 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
339 struct sigframe *fp, frame;
340 struct sysentvec *sysent;
341 struct trapframe *tf;
350 PROC_LOCK_ASSERT(p, MA_OWNED);
352 sig = ksi->ksi_signo;
354 mtx_assert(&psp->ps_mtx, MA_OWNED);
357 onstack = sigonstack(tf->tf_sp);
359 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
362 /* Allocate and validate space for the signal handler context. */
363 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
364 SIGISMEMBER(psp->ps_sigonstack, sig)) {
365 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
366 td->td_sigstk.ss_size);
368 fp = (struct sigframe *)td->td_frame->tf_sp;
371 /* Make room, keeping the stack aligned */
373 fp = (struct sigframe *)STACKALIGN(fp);
375 /* Fill in the frame to copy out */
376 bzero(&frame, sizeof(frame));
377 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
378 frame.sf_si = ksi->ksi_info;
379 frame.sf_uc.uc_sigmask = *mask;
380 frame.sf_uc.uc_stack = td->td_sigstk;
381 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
382 (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
383 mtx_unlock(&psp->ps_mtx);
384 PROC_UNLOCK(td->td_proc);
386 /* Copy the sigframe out to the user's stack. */
387 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
388 /* Process has trashed its stack. Kill it. */
389 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
395 tf->tf_a[1] = (register_t)&fp->sf_si;
396 tf->tf_a[2] = (register_t)&fp->sf_uc;
398 tf->tf_sepc = (register_t)catcher;
399 tf->tf_sp = (register_t)fp;
401 sysent = p->p_sysent;
403 tf->tf_ra = (register_t)PROC_SIGCODE(p);
405 tf->tf_ra = (register_t)(PROC_PS_STRINGS(p) -
406 *(sysent->sv_szsigcode));
408 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_sepc,
412 mtx_lock(&psp->ps_mtx);