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