1 /*- 2 * Copyright (c) 2014 Andrew Turner 3 * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com> 4 * All rights reserved. 5 * 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. 9 * 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). 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 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. 22 * 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 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/exec.h> 42 #include <sys/imgact.h> 43 #include <sys/kdb.h> 44 #include <sys/kernel.h> 45 #include <sys/ktr.h> 46 #include <sys/limits.h> 47 #include <sys/lock.h> 48 #include <sys/mutex.h> 49 #include <sys/proc.h> 50 #include <sys/ptrace.h> 51 #include <sys/reg.h> 52 #include <sys/rwlock.h> 53 #include <sys/sched.h> 54 #include <sys/signalvar.h> 55 #include <sys/syscallsubr.h> 56 #include <sys/sysent.h> 57 #include <sys/sysproto.h> 58 #include <sys/ucontext.h> 59 60 #include <machine/cpu.h> 61 #include <machine/kdb.h> 62 #include <machine/pcb.h> 63 #include <machine/pte.h> 64 #include <machine/riscvreg.h> 65 #include <machine/sbi.h> 66 #include <machine/trap.h> 67 68 #include <vm/vm.h> 69 #include <vm/vm_param.h> 70 #include <vm/pmap.h> 71 #include <vm/vm_map.h> 72 73 #ifdef FPE 74 #include <machine/fpe.h> 75 #endif 76 77 static void get_fpcontext(struct thread *td, mcontext_t *mcp); 78 static void set_fpcontext(struct thread *td, mcontext_t *mcp); 79 80 _Static_assert(sizeof(mcontext_t) == 864, "mcontext_t size incorrect"); 81 _Static_assert(sizeof(ucontext_t) == 936, "ucontext_t size incorrect"); 82 _Static_assert(sizeof(siginfo_t) == 80, "siginfo_t size incorrect"); 83 84 int 85 fill_regs(struct thread *td, struct reg *regs) 86 { 87 struct trapframe *frame; 88 89 frame = td->td_frame; 90 regs->sepc = frame->tf_sepc; 91 regs->sstatus = frame->tf_sstatus; 92 regs->ra = frame->tf_ra; 93 regs->sp = frame->tf_sp; 94 regs->gp = frame->tf_gp; 95 regs->tp = frame->tf_tp; 96 97 memcpy(regs->t, frame->tf_t, sizeof(regs->t)); 98 memcpy(regs->s, frame->tf_s, sizeof(regs->s)); 99 memcpy(regs->a, frame->tf_a, sizeof(regs->a)); 100 101 return (0); 102 } 103 104 int 105 set_regs(struct thread *td, struct reg *regs) 106 { 107 struct trapframe *frame; 108 109 frame = td->td_frame; 110 frame->tf_sepc = regs->sepc; 111 frame->tf_ra = regs->ra; 112 frame->tf_sp = regs->sp; 113 frame->tf_gp = regs->gp; 114 frame->tf_tp = regs->tp; 115 116 memcpy(frame->tf_t, regs->t, sizeof(frame->tf_t)); 117 memcpy(frame->tf_s, regs->s, sizeof(frame->tf_s)); 118 memcpy(frame->tf_a, regs->a, sizeof(frame->tf_a)); 119 120 return (0); 121 } 122 123 int 124 fill_fpregs(struct thread *td, struct fpreg *regs) 125 { 126 #ifdef FPE 127 struct pcb *pcb; 128 129 pcb = td->td_pcb; 130 131 if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) { 132 /* 133 * If we have just been running FPE instructions we will 134 * need to save the state to memcpy it below. 135 */ 136 if (td == curthread) 137 fpe_state_save(td); 138 139 memcpy(regs->fp_x, pcb->pcb_x, sizeof(regs->fp_x)); 140 regs->fp_fcsr = pcb->pcb_fcsr; 141 } else 142 #endif 143 memset(regs, 0, sizeof(*regs)); 144 145 return (0); 146 } 147 148 int 149 set_fpregs(struct thread *td, struct fpreg *regs) 150 { 151 #ifdef FPE 152 struct trapframe *frame; 153 struct pcb *pcb; 154 155 frame = td->td_frame; 156 pcb = td->td_pcb; 157 158 memcpy(pcb->pcb_x, regs->fp_x, sizeof(regs->fp_x)); 159 pcb->pcb_fcsr = regs->fp_fcsr; 160 pcb->pcb_fpflags |= PCB_FP_STARTED; 161 frame->tf_sstatus &= ~SSTATUS_FS_MASK; 162 frame->tf_sstatus |= SSTATUS_FS_CLEAN; 163 #endif 164 165 return (0); 166 } 167 168 int 169 fill_dbregs(struct thread *td, struct dbreg *regs) 170 { 171 172 panic("fill_dbregs"); 173 } 174 175 int 176 set_dbregs(struct thread *td, struct dbreg *regs) 177 { 178 179 panic("set_dbregs"); 180 } 181 182 void 183 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack) 184 { 185 struct trapframe *tf; 186 struct pcb *pcb; 187 188 tf = td->td_frame; 189 pcb = td->td_pcb; 190 191 memset(tf, 0, sizeof(struct trapframe)); 192 193 tf->tf_a[0] = stack; 194 tf->tf_sp = STACKALIGN(stack); 195 tf->tf_ra = imgp->entry_addr; 196 tf->tf_sepc = imgp->entry_addr; 197 198 pcb->pcb_fpflags &= ~PCB_FP_STARTED; 199 } 200 201 /* Sanity check these are the same size, they will be memcpy'd to and from */ 202 CTASSERT(sizeof(((struct trapframe *)0)->tf_a) == 203 sizeof((struct gpregs *)0)->gp_a); 204 CTASSERT(sizeof(((struct trapframe *)0)->tf_s) == 205 sizeof((struct gpregs *)0)->gp_s); 206 CTASSERT(sizeof(((struct trapframe *)0)->tf_t) == 207 sizeof((struct gpregs *)0)->gp_t); 208 CTASSERT(sizeof(((struct trapframe *)0)->tf_a) == 209 sizeof((struct reg *)0)->a); 210 CTASSERT(sizeof(((struct trapframe *)0)->tf_s) == 211 sizeof((struct reg *)0)->s); 212 CTASSERT(sizeof(((struct trapframe *)0)->tf_t) == 213 sizeof((struct reg *)0)->t); 214 215 int 216 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret) 217 { 218 struct trapframe *tf = td->td_frame; 219 220 memcpy(mcp->mc_gpregs.gp_t, tf->tf_t, sizeof(mcp->mc_gpregs.gp_t)); 221 memcpy(mcp->mc_gpregs.gp_s, tf->tf_s, sizeof(mcp->mc_gpregs.gp_s)); 222 memcpy(mcp->mc_gpregs.gp_a, tf->tf_a, sizeof(mcp->mc_gpregs.gp_a)); 223 224 if (clear_ret & GET_MC_CLEAR_RET) { 225 mcp->mc_gpregs.gp_a[0] = 0; 226 mcp->mc_gpregs.gp_t[0] = 0; /* clear syscall error */ 227 } 228 229 mcp->mc_gpregs.gp_ra = tf->tf_ra; 230 mcp->mc_gpregs.gp_sp = tf->tf_sp; 231 mcp->mc_gpregs.gp_gp = tf->tf_gp; 232 mcp->mc_gpregs.gp_tp = tf->tf_tp; 233 mcp->mc_gpregs.gp_sepc = tf->tf_sepc; 234 mcp->mc_gpregs.gp_sstatus = tf->tf_sstatus; 235 get_fpcontext(td, mcp); 236 237 return (0); 238 } 239 240 int 241 set_mcontext(struct thread *td, mcontext_t *mcp) 242 { 243 struct trapframe *tf; 244 245 tf = td->td_frame; 246 247 /* 248 * Permit changes to the USTATUS bits of SSTATUS. 249 * 250 * Ignore writes to read-only bits (SD, XS). 251 * 252 * Ignore writes to the FS field as set_fpcontext() will set 253 * it explicitly. 254 */ 255 if (((mcp->mc_gpregs.gp_sstatus ^ tf->tf_sstatus) & 256 ~(SSTATUS_SD | SSTATUS_XS_MASK | SSTATUS_FS_MASK | SSTATUS_UPIE | 257 SSTATUS_UIE)) != 0) 258 return (EINVAL); 259 260 memcpy(tf->tf_t, mcp->mc_gpregs.gp_t, sizeof(tf->tf_t)); 261 memcpy(tf->tf_s, mcp->mc_gpregs.gp_s, sizeof(tf->tf_s)); 262 memcpy(tf->tf_a, mcp->mc_gpregs.gp_a, sizeof(tf->tf_a)); 263 264 tf->tf_ra = mcp->mc_gpregs.gp_ra; 265 tf->tf_sp = mcp->mc_gpregs.gp_sp; 266 tf->tf_gp = mcp->mc_gpregs.gp_gp; 267 tf->tf_sepc = mcp->mc_gpregs.gp_sepc; 268 tf->tf_sstatus = mcp->mc_gpregs.gp_sstatus; 269 set_fpcontext(td, mcp); 270 271 return (0); 272 } 273 274 static void 275 get_fpcontext(struct thread *td, mcontext_t *mcp) 276 { 277 #ifdef FPE 278 struct pcb *curpcb; 279 280 critical_enter(); 281 282 curpcb = curthread->td_pcb; 283 284 KASSERT(td->td_pcb == curpcb, ("Invalid fpe pcb")); 285 286 if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) { 287 /* 288 * If we have just been running FPE instructions we will 289 * need to save the state to memcpy it below. 290 */ 291 fpe_state_save(td); 292 293 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0, 294 ("Non-userspace FPE flags set in get_fpcontext")); 295 memcpy(mcp->mc_fpregs.fp_x, curpcb->pcb_x, 296 sizeof(mcp->mc_fpregs.fp_x)); 297 mcp->mc_fpregs.fp_fcsr = curpcb->pcb_fcsr; 298 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags; 299 mcp->mc_flags |= _MC_FP_VALID; 300 } 301 302 critical_exit(); 303 #endif 304 } 305 306 static void 307 set_fpcontext(struct thread *td, mcontext_t *mcp) 308 { 309 #ifdef FPE 310 struct pcb *curpcb; 311 #endif 312 313 td->td_frame->tf_sstatus &= ~SSTATUS_FS_MASK; 314 td->td_frame->tf_sstatus |= SSTATUS_FS_OFF; 315 316 #ifdef FPE 317 critical_enter(); 318 319 if ((mcp->mc_flags & _MC_FP_VALID) != 0) { 320 curpcb = curthread->td_pcb; 321 /* FPE usage is enabled, override registers. */ 322 memcpy(curpcb->pcb_x, mcp->mc_fpregs.fp_x, 323 sizeof(mcp->mc_fpregs.fp_x)); 324 curpcb->pcb_fcsr = mcp->mc_fpregs.fp_fcsr; 325 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK; 326 td->td_frame->tf_sstatus |= SSTATUS_FS_CLEAN; 327 } 328 329 critical_exit(); 330 #endif 331 } 332 333 int 334 sys_sigreturn(struct thread *td, struct sigreturn_args *uap) 335 { 336 ucontext_t uc; 337 int error; 338 339 if (copyin(uap->sigcntxp, &uc, sizeof(uc))) 340 return (EFAULT); 341 342 error = set_mcontext(td, &uc.uc_mcontext); 343 if (error != 0) 344 return (error); 345 346 /* Restore signal mask. */ 347 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0); 348 349 return (EJUSTRETURN); 350 } 351 352 void 353 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 354 { 355 struct sigframe *fp, frame; 356 struct sysentvec *sysent; 357 struct trapframe *tf; 358 struct sigacts *psp; 359 struct thread *td; 360 struct proc *p; 361 int onstack; 362 int sig; 363 364 td = curthread; 365 p = td->td_proc; 366 PROC_LOCK_ASSERT(p, MA_OWNED); 367 368 sig = ksi->ksi_signo; 369 psp = p->p_sigacts; 370 mtx_assert(&psp->ps_mtx, MA_OWNED); 371 372 tf = td->td_frame; 373 onstack = sigonstack(tf->tf_sp); 374 375 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm, 376 catcher, sig); 377 378 /* Allocate and validate space for the signal handler context. */ 379 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack && 380 SIGISMEMBER(psp->ps_sigonstack, sig)) { 381 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp + 382 td->td_sigstk.ss_size); 383 } else { 384 fp = (struct sigframe *)td->td_frame->tf_sp; 385 } 386 387 /* Make room, keeping the stack aligned */ 388 fp--; 389 fp = (struct sigframe *)STACKALIGN(fp); 390 391 /* Fill in the frame to copy out */ 392 bzero(&frame, sizeof(frame)); 393 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0); 394 frame.sf_si = ksi->ksi_info; 395 frame.sf_uc.uc_sigmask = *mask; 396 frame.sf_uc.uc_stack = td->td_sigstk; 397 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ? 398 (onstack ? SS_ONSTACK : 0) : SS_DISABLE; 399 mtx_unlock(&psp->ps_mtx); 400 PROC_UNLOCK(td->td_proc); 401 402 /* Copy the sigframe out to the user's stack. */ 403 if (copyout(&frame, fp, sizeof(*fp)) != 0) { 404 /* Process has trashed its stack. Kill it. */ 405 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp); 406 PROC_LOCK(p); 407 sigexit(td, SIGILL); 408 } 409 410 tf->tf_a[0] = sig; 411 tf->tf_a[1] = (register_t)&fp->sf_si; 412 tf->tf_a[2] = (register_t)&fp->sf_uc; 413 414 tf->tf_sepc = (register_t)catcher; 415 tf->tf_sp = (register_t)fp; 416 417 sysent = p->p_sysent; 418 if (PROC_HAS_SHP(p)) 419 tf->tf_ra = (register_t)PROC_SIGCODE(p); 420 else 421 tf->tf_ra = (register_t)(PROC_PS_STRINGS(p) - 422 *(sysent->sv_szsigcode)); 423 424 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_sepc, 425 tf->tf_sp); 426 427 PROC_LOCK(p); 428 mtx_lock(&psp->ps_mtx); 429 } 430