1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2003 Peter Wemm 5 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * William Jolitz. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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/exec.h> 41 #include <sys/fcntl.h> 42 #include <sys/imgact.h> 43 #include <sys/kernel.h> 44 #include <sys/lock.h> 45 #include <sys/malloc.h> 46 #include <sys/mutex.h> 47 #include <sys/mman.h> 48 #include <sys/namei.h> 49 #include <sys/pioctl.h> 50 #include <sys/proc.h> 51 #include <sys/procfs.h> 52 #include <sys/resourcevar.h> 53 #include <sys/systm.h> 54 #include <sys/signalvar.h> 55 #include <sys/stat.h> 56 #include <sys/sx.h> 57 #include <sys/syscall.h> 58 #include <sys/syscallsubr.h> 59 #include <sys/sysctl.h> 60 #include <sys/sysent.h> 61 #include <sys/vnode.h> 62 63 #include <vm/vm.h> 64 #include <vm/vm_kern.h> 65 #include <vm/vm_param.h> 66 #include <vm/pmap.h> 67 #include <vm/vm_map.h> 68 #include <vm/vm_object.h> 69 #include <vm/vm_extern.h> 70 71 #include <compat/freebsd32/freebsd32_signal.h> 72 #include <compat/freebsd32/freebsd32_util.h> 73 #include <compat/freebsd32/freebsd32_proto.h> 74 #include <compat/freebsd32/freebsd32.h> 75 #include <compat/ia32/ia32_signal.h> 76 #include <machine/psl.h> 77 #include <machine/segments.h> 78 #include <machine/specialreg.h> 79 #include <machine/frame.h> 80 #include <machine/md_var.h> 81 #include <machine/pcb.h> 82 #include <machine/cpufunc.h> 83 #include <machine/trap.h> 84 85 #ifdef COMPAT_FREEBSD4 86 static void freebsd4_ia32_sendsig(sig_t, ksiginfo_t *, sigset_t *); 87 #endif 88 89 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 90 #define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 91 92 static void 93 ia32_get_fpcontext(struct thread *td, struct ia32_mcontext *mcp, 94 char *xfpusave, size_t xfpusave_len) 95 { 96 size_t max_len, len; 97 98 /* 99 * XXX Format of 64bit and 32bit FXSAVE areas differs. FXSAVE 100 * in 32bit mode saves %cs and %ds, while on 64bit it saves 101 * 64bit instruction and data pointers. Ignore the difference 102 * for now, it should be irrelevant for most applications. 103 */ 104 mcp->mc_ownedfp = fpugetregs(td); 105 bcopy(get_pcb_user_save_td(td), &mcp->mc_fpstate[0], 106 sizeof(mcp->mc_fpstate)); 107 mcp->mc_fpformat = fpuformat(); 108 if (!use_xsave || xfpusave_len == 0) 109 return; 110 max_len = cpu_max_ext_state_size - sizeof(struct savefpu); 111 len = xfpusave_len; 112 if (len > max_len) { 113 len = max_len; 114 bzero(xfpusave + max_len, len - max_len); 115 } 116 mcp->mc_flags |= _MC_IA32_HASFPXSTATE; 117 mcp->mc_xfpustate_len = len; 118 bcopy(get_pcb_user_save_td(td) + 1, xfpusave, len); 119 } 120 121 static int 122 ia32_set_fpcontext(struct thread *td, struct ia32_mcontext *mcp, 123 char *xfpustate, size_t xfpustate_len) 124 { 125 int error; 126 127 if (mcp->mc_fpformat == _MC_FPFMT_NODEV) 128 return (0); 129 else if (mcp->mc_fpformat != _MC_FPFMT_XMM) 130 return (EINVAL); 131 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE) { 132 /* We don't care what state is left in the FPU or PCB. */ 133 fpstate_drop(td); 134 error = 0; 135 } else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU || 136 mcp->mc_ownedfp == _MC_FPOWNED_PCB) { 137 error = fpusetregs(td, (struct savefpu *)&mcp->mc_fpstate, 138 xfpustate, xfpustate_len); 139 } else 140 return (EINVAL); 141 return (error); 142 } 143 144 /* 145 * Get machine context. 146 */ 147 static int 148 ia32_get_mcontext(struct thread *td, struct ia32_mcontext *mcp, int flags) 149 { 150 struct pcb *pcb; 151 struct trapframe *tp; 152 153 pcb = td->td_pcb; 154 tp = td->td_frame; 155 156 PROC_LOCK(curthread->td_proc); 157 mcp->mc_onstack = sigonstack(tp->tf_rsp); 158 PROC_UNLOCK(curthread->td_proc); 159 /* Entry into kernel always sets TF_HASSEGS */ 160 mcp->mc_gs = tp->tf_gs; 161 mcp->mc_fs = tp->tf_fs; 162 mcp->mc_es = tp->tf_es; 163 mcp->mc_ds = tp->tf_ds; 164 mcp->mc_edi = tp->tf_rdi; 165 mcp->mc_esi = tp->tf_rsi; 166 mcp->mc_ebp = tp->tf_rbp; 167 mcp->mc_isp = tp->tf_rsp; 168 mcp->mc_eflags = tp->tf_rflags; 169 if (flags & GET_MC_CLEAR_RET) { 170 mcp->mc_eax = 0; 171 mcp->mc_edx = 0; 172 mcp->mc_eflags &= ~PSL_C; 173 } else { 174 mcp->mc_eax = tp->tf_rax; 175 mcp->mc_edx = tp->tf_rdx; 176 } 177 mcp->mc_ebx = tp->tf_rbx; 178 mcp->mc_ecx = tp->tf_rcx; 179 mcp->mc_eip = tp->tf_rip; 180 mcp->mc_cs = tp->tf_cs; 181 mcp->mc_esp = tp->tf_rsp; 182 mcp->mc_ss = tp->tf_ss; 183 mcp->mc_len = sizeof(*mcp); 184 mcp->mc_flags = tp->tf_flags; 185 ia32_get_fpcontext(td, mcp, NULL, 0); 186 mcp->mc_fsbase = pcb->pcb_fsbase; 187 mcp->mc_gsbase = pcb->pcb_gsbase; 188 mcp->mc_xfpustate = 0; 189 mcp->mc_xfpustate_len = 0; 190 bzero(mcp->mc_spare2, sizeof(mcp->mc_spare2)); 191 return (0); 192 } 193 194 /* 195 * Set machine context. 196 * 197 * However, we don't set any but the user modifiable flags, and we won't 198 * touch the cs selector. 199 */ 200 static int 201 ia32_set_mcontext(struct thread *td, struct ia32_mcontext *mcp) 202 { 203 struct trapframe *tp; 204 char *xfpustate; 205 long rflags; 206 int ret; 207 208 tp = td->td_frame; 209 if (mcp->mc_len != sizeof(*mcp)) 210 return (EINVAL); 211 rflags = (mcp->mc_eflags & PSL_USERCHANGE) | 212 (tp->tf_rflags & ~PSL_USERCHANGE); 213 if (mcp->mc_flags & _MC_IA32_HASFPXSTATE) { 214 if (mcp->mc_xfpustate_len > cpu_max_ext_state_size - 215 sizeof(struct savefpu)) 216 return (EINVAL); 217 xfpustate = __builtin_alloca(mcp->mc_xfpustate_len); 218 ret = copyin(PTRIN(mcp->mc_xfpustate), xfpustate, 219 mcp->mc_xfpustate_len); 220 if (ret != 0) 221 return (ret); 222 } else 223 xfpustate = NULL; 224 ret = ia32_set_fpcontext(td, mcp, xfpustate, mcp->mc_xfpustate_len); 225 if (ret != 0) 226 return (ret); 227 tp->tf_gs = mcp->mc_gs; 228 tp->tf_fs = mcp->mc_fs; 229 tp->tf_es = mcp->mc_es; 230 tp->tf_ds = mcp->mc_ds; 231 tp->tf_flags = TF_HASSEGS; 232 tp->tf_rdi = mcp->mc_edi; 233 tp->tf_rsi = mcp->mc_esi; 234 tp->tf_rbp = mcp->mc_ebp; 235 tp->tf_rbx = mcp->mc_ebx; 236 tp->tf_rdx = mcp->mc_edx; 237 tp->tf_rcx = mcp->mc_ecx; 238 tp->tf_rax = mcp->mc_eax; 239 /* trapno, err */ 240 tp->tf_rip = mcp->mc_eip; 241 tp->tf_rflags = rflags; 242 tp->tf_rsp = mcp->mc_esp; 243 tp->tf_ss = mcp->mc_ss; 244 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 245 return (0); 246 } 247 248 /* 249 * The first two fields of a ucontext_t are the signal mask and 250 * the machine context. The next field is uc_link; we want to 251 * avoid destroying the link when copying out contexts. 252 */ 253 #define UC_COPY_SIZE offsetof(struct ia32_ucontext, uc_link) 254 255 int 256 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap) 257 { 258 struct ia32_ucontext uc; 259 int ret; 260 261 if (uap->ucp == NULL) 262 ret = EINVAL; 263 else { 264 ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET); 265 PROC_LOCK(td->td_proc); 266 uc.uc_sigmask = td->td_sigmask; 267 PROC_UNLOCK(td->td_proc); 268 bzero(&uc.__spare__, sizeof(uc.__spare__)); 269 ret = copyout(&uc, uap->ucp, UC_COPY_SIZE); 270 } 271 return (ret); 272 } 273 274 int 275 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap) 276 { 277 struct ia32_ucontext uc; 278 int ret; 279 280 if (uap->ucp == NULL) 281 ret = EINVAL; 282 else { 283 ret = copyin(uap->ucp, &uc, UC_COPY_SIZE); 284 if (ret == 0) { 285 ret = ia32_set_mcontext(td, &uc.uc_mcontext); 286 if (ret == 0) { 287 kern_sigprocmask(td, SIG_SETMASK, 288 &uc.uc_sigmask, NULL, 0); 289 } 290 } 291 } 292 return (ret == 0 ? EJUSTRETURN : ret); 293 } 294 295 int 296 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap) 297 { 298 struct ia32_ucontext uc; 299 int ret; 300 301 if (uap->oucp == NULL || uap->ucp == NULL) 302 ret = EINVAL; 303 else { 304 ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET); 305 PROC_LOCK(td->td_proc); 306 uc.uc_sigmask = td->td_sigmask; 307 PROC_UNLOCK(td->td_proc); 308 ret = copyout(&uc, uap->oucp, UC_COPY_SIZE); 309 if (ret == 0) { 310 ret = copyin(uap->ucp, &uc, UC_COPY_SIZE); 311 if (ret == 0) { 312 ret = ia32_set_mcontext(td, &uc.uc_mcontext); 313 if (ret == 0) { 314 kern_sigprocmask(td, SIG_SETMASK, 315 &uc.uc_sigmask, NULL, 0); 316 } 317 } 318 } 319 } 320 return (ret == 0 ? EJUSTRETURN : ret); 321 } 322 323 /* 324 * Send an interrupt to process. 325 * 326 * Stack is set up to allow sigcode stored 327 * at top to call routine, followed by kcall 328 * to sigreturn routine below. After sigreturn 329 * resets the signal mask, the stack, and the 330 * frame pointer, it returns to the user 331 * specified pc, psl. 332 */ 333 334 #ifdef COMPAT_43 335 static void 336 ia32_osendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 337 { 338 struct ia32_sigframe3 sf, *fp; 339 struct proc *p; 340 struct thread *td; 341 struct sigacts *psp; 342 struct trapframe *regs; 343 int sig; 344 int oonstack; 345 346 td = curthread; 347 p = td->td_proc; 348 PROC_LOCK_ASSERT(p, MA_OWNED); 349 sig = ksi->ksi_signo; 350 psp = p->p_sigacts; 351 mtx_assert(&psp->ps_mtx, MA_OWNED); 352 regs = td->td_frame; 353 oonstack = sigonstack(regs->tf_rsp); 354 355 /* Allocate space for the signal handler context. */ 356 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack && 357 SIGISMEMBER(psp->ps_sigonstack, sig)) { 358 fp = (struct ia32_sigframe3 *)((uintptr_t)td->td_sigstk.ss_sp + 359 td->td_sigstk.ss_size - sizeof(sf)); 360 td->td_sigstk.ss_flags |= SS_ONSTACK; 361 } else 362 fp = (struct ia32_sigframe3 *)regs->tf_rsp - 1; 363 364 /* Build the argument list for the signal handler. */ 365 sf.sf_signum = sig; 366 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc; 367 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 368 /* Signal handler installed with SA_SIGINFO. */ 369 sf.sf_arg2 = (register_t)&fp->sf_siginfo; 370 sf.sf_siginfo.si_signo = sig; 371 sf.sf_siginfo.si_code = ksi->ksi_code; 372 sf.sf_ah = (uintptr_t)catcher; 373 } else { 374 /* Old FreeBSD-style arguments. */ 375 sf.sf_arg2 = ksi->ksi_code; 376 sf.sf_addr = (register_t)ksi->ksi_addr; 377 sf.sf_ah = (uintptr_t)catcher; 378 } 379 mtx_unlock(&psp->ps_mtx); 380 PROC_UNLOCK(p); 381 382 /* Save most if not all of trap frame. */ 383 sf.sf_siginfo.si_sc.sc_eax = regs->tf_rax; 384 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_rbx; 385 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_rcx; 386 sf.sf_siginfo.si_sc.sc_edx = regs->tf_rdx; 387 sf.sf_siginfo.si_sc.sc_esi = regs->tf_rsi; 388 sf.sf_siginfo.si_sc.sc_edi = regs->tf_rdi; 389 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs; 390 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds; 391 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss; 392 sf.sf_siginfo.si_sc.sc_es = regs->tf_es; 393 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs; 394 sf.sf_siginfo.si_sc.sc_gs = regs->tf_gs; 395 sf.sf_siginfo.si_sc.sc_isp = regs->tf_rsp; 396 397 /* Build the signal context to be used by osigreturn(). */ 398 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0; 399 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask); 400 sf.sf_siginfo.si_sc.sc_esp = regs->tf_rsp; 401 sf.sf_siginfo.si_sc.sc_ebp = regs->tf_rbp; 402 sf.sf_siginfo.si_sc.sc_eip = regs->tf_rip; 403 sf.sf_siginfo.si_sc.sc_eflags = regs->tf_rflags; 404 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno; 405 sf.sf_siginfo.si_sc.sc_err = regs->tf_err; 406 407 /* 408 * Copy the sigframe out to the user's stack. 409 */ 410 if (copyout(&sf, fp, sizeof(*fp)) != 0) { 411 #ifdef DEBUG 412 printf("process %ld has trashed its stack\n", (long)p->p_pid); 413 #endif 414 PROC_LOCK(p); 415 sigexit(td, SIGILL); 416 } 417 418 regs->tf_rsp = (uintptr_t)fp; 419 regs->tf_rip = p->p_sysent->sv_psstrings - sz_ia32_osigcode; 420 regs->tf_rflags &= ~(PSL_T | PSL_D); 421 regs->tf_cs = _ucode32sel; 422 regs->tf_ds = _udatasel; 423 regs->tf_es = _udatasel; 424 regs->tf_fs = _udatasel; 425 regs->tf_ss = _udatasel; 426 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 427 PROC_LOCK(p); 428 mtx_lock(&psp->ps_mtx); 429 } 430 #endif 431 432 #ifdef COMPAT_FREEBSD4 433 static void 434 freebsd4_ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 435 { 436 struct ia32_sigframe4 sf, *sfp; 437 struct siginfo32 siginfo; 438 struct proc *p; 439 struct thread *td; 440 struct sigacts *psp; 441 struct trapframe *regs; 442 int oonstack; 443 int sig; 444 445 td = curthread; 446 p = td->td_proc; 447 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo); 448 449 PROC_LOCK_ASSERT(p, MA_OWNED); 450 sig = siginfo.si_signo; 451 psp = p->p_sigacts; 452 mtx_assert(&psp->ps_mtx, MA_OWNED); 453 regs = td->td_frame; 454 oonstack = sigonstack(regs->tf_rsp); 455 456 /* Save user context. */ 457 bzero(&sf, sizeof(sf)); 458 sf.sf_uc.uc_sigmask = *mask; 459 sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp; 460 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size; 461 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 462 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 463 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0; 464 sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi; 465 sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi; 466 sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp; 467 sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */ 468 sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx; 469 sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx; 470 sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx; 471 sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax; 472 sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno; 473 sf.sf_uc.uc_mcontext.mc_err = regs->tf_err; 474 sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip; 475 sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs; 476 sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags; 477 sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp; 478 sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss; 479 sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds; 480 sf.sf_uc.uc_mcontext.mc_es = regs->tf_es; 481 sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs; 482 sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs; 483 bzero(sf.sf_uc.uc_mcontext.mc_fpregs, 484 sizeof(sf.sf_uc.uc_mcontext.mc_fpregs)); 485 bzero(sf.sf_uc.uc_mcontext.__spare__, 486 sizeof(sf.sf_uc.uc_mcontext.__spare__)); 487 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__)); 488 489 /* Allocate space for the signal handler context. */ 490 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack && 491 SIGISMEMBER(psp->ps_sigonstack, sig)) { 492 sfp = (struct ia32_sigframe4 *)((uintptr_t)td->td_sigstk.ss_sp + 493 td->td_sigstk.ss_size - sizeof(sf)); 494 } else 495 sfp = (struct ia32_sigframe4 *)regs->tf_rsp - 1; 496 PROC_UNLOCK(p); 497 498 /* Build the argument list for the signal handler. */ 499 sf.sf_signum = sig; 500 sf.sf_ucontext = (register_t)&sfp->sf_uc; 501 bzero(&sf.sf_si, sizeof(sf.sf_si)); 502 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 503 /* Signal handler installed with SA_SIGINFO. */ 504 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si; 505 sf.sf_ah = (u_int32_t)(uintptr_t)catcher; 506 507 /* Fill in POSIX parts */ 508 sf.sf_si = siginfo; 509 sf.sf_si.si_signo = sig; 510 } else { 511 /* Old FreeBSD-style arguments. */ 512 sf.sf_siginfo = siginfo.si_code; 513 sf.sf_addr = (u_int32_t)siginfo.si_addr; 514 sf.sf_ah = (u_int32_t)(uintptr_t)catcher; 515 } 516 mtx_unlock(&psp->ps_mtx); 517 518 /* 519 * Copy the sigframe out to the user's stack. 520 */ 521 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) { 522 #ifdef DEBUG 523 printf("process %ld has trashed its stack\n", (long)p->p_pid); 524 #endif 525 PROC_LOCK(p); 526 sigexit(td, SIGILL); 527 } 528 529 regs->tf_rsp = (uintptr_t)sfp; 530 regs->tf_rip = p->p_sysent->sv_sigcode_base + sz_ia32_sigcode - 531 sz_freebsd4_ia32_sigcode; 532 regs->tf_rflags &= ~(PSL_T | PSL_D); 533 regs->tf_cs = _ucode32sel; 534 regs->tf_ss = _udatasel; 535 regs->tf_ds = _udatasel; 536 regs->tf_es = _udatasel; 537 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 538 /* leave user %fs and %gs untouched */ 539 PROC_LOCK(p); 540 mtx_lock(&psp->ps_mtx); 541 } 542 #endif /* COMPAT_FREEBSD4 */ 543 544 void 545 ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 546 { 547 struct ia32_sigframe sf, *sfp; 548 struct siginfo32 siginfo; 549 struct proc *p; 550 struct thread *td; 551 struct sigacts *psp; 552 char *sp; 553 struct trapframe *regs; 554 char *xfpusave; 555 size_t xfpusave_len; 556 int oonstack; 557 int sig; 558 559 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo); 560 td = curthread; 561 p = td->td_proc; 562 PROC_LOCK_ASSERT(p, MA_OWNED); 563 sig = siginfo.si_signo; 564 psp = p->p_sigacts; 565 #ifdef COMPAT_FREEBSD4 566 if (SIGISMEMBER(psp->ps_freebsd4, sig)) { 567 freebsd4_ia32_sendsig(catcher, ksi, mask); 568 return; 569 } 570 #endif 571 #ifdef COMPAT_43 572 if (SIGISMEMBER(psp->ps_osigset, sig)) { 573 ia32_osendsig(catcher, ksi, mask); 574 return; 575 } 576 #endif 577 mtx_assert(&psp->ps_mtx, MA_OWNED); 578 regs = td->td_frame; 579 oonstack = sigonstack(regs->tf_rsp); 580 581 if (cpu_max_ext_state_size > sizeof(struct savefpu) && use_xsave) { 582 xfpusave_len = cpu_max_ext_state_size - sizeof(struct savefpu); 583 xfpusave = __builtin_alloca(xfpusave_len); 584 } else { 585 xfpusave_len = 0; 586 xfpusave = NULL; 587 } 588 589 /* Save user context. */ 590 bzero(&sf, sizeof(sf)); 591 sf.sf_uc.uc_sigmask = *mask; 592 sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp; 593 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size; 594 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 595 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 596 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0; 597 sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi; 598 sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi; 599 sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp; 600 sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */ 601 sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx; 602 sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx; 603 sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx; 604 sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax; 605 sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno; 606 sf.sf_uc.uc_mcontext.mc_err = regs->tf_err; 607 sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip; 608 sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs; 609 sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags; 610 sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp; 611 sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss; 612 sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds; 613 sf.sf_uc.uc_mcontext.mc_es = regs->tf_es; 614 sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs; 615 sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs; 616 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */ 617 ia32_get_fpcontext(td, &sf.sf_uc.uc_mcontext, xfpusave, xfpusave_len); 618 fpstate_drop(td); 619 sf.sf_uc.uc_mcontext.mc_fsbase = td->td_pcb->pcb_fsbase; 620 sf.sf_uc.uc_mcontext.mc_gsbase = td->td_pcb->pcb_gsbase; 621 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__)); 622 623 /* Allocate space for the signal handler context. */ 624 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack && 625 SIGISMEMBER(psp->ps_sigonstack, sig)) 626 sp = (char *)td->td_sigstk.ss_sp + td->td_sigstk.ss_size; 627 else 628 sp = (char *)regs->tf_rsp; 629 if (xfpusave != NULL) { 630 sp -= xfpusave_len; 631 sp = (char *)((unsigned long)sp & ~0x3Ful); 632 sf.sf_uc.uc_mcontext.mc_xfpustate = (register_t)sp; 633 } 634 sp -= sizeof(sf); 635 /* Align to 16 bytes. */ 636 sfp = (struct ia32_sigframe *)((uintptr_t)sp & ~0xF); 637 PROC_UNLOCK(p); 638 639 /* Build the argument list for the signal handler. */ 640 sf.sf_signum = sig; 641 sf.sf_ucontext = (register_t)&sfp->sf_uc; 642 bzero(&sf.sf_si, sizeof(sf.sf_si)); 643 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 644 /* Signal handler installed with SA_SIGINFO. */ 645 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si; 646 sf.sf_ah = (u_int32_t)(uintptr_t)catcher; 647 648 /* Fill in POSIX parts */ 649 sf.sf_si = siginfo; 650 sf.sf_si.si_signo = sig; 651 } else { 652 /* Old FreeBSD-style arguments. */ 653 sf.sf_siginfo = siginfo.si_code; 654 sf.sf_addr = (u_int32_t)siginfo.si_addr; 655 sf.sf_ah = (u_int32_t)(uintptr_t)catcher; 656 } 657 mtx_unlock(&psp->ps_mtx); 658 659 /* 660 * Copy the sigframe out to the user's stack. 661 */ 662 if (copyout(&sf, sfp, sizeof(*sfp)) != 0 || 663 (xfpusave != NULL && copyout(xfpusave, 664 PTRIN(sf.sf_uc.uc_mcontext.mc_xfpustate), xfpusave_len) 665 != 0)) { 666 #ifdef DEBUG 667 printf("process %ld has trashed its stack\n", (long)p->p_pid); 668 #endif 669 PROC_LOCK(p); 670 sigexit(td, SIGILL); 671 } 672 673 regs->tf_rsp = (uintptr_t)sfp; 674 regs->tf_rip = p->p_sysent->sv_sigcode_base; 675 regs->tf_rflags &= ~(PSL_T | PSL_D); 676 regs->tf_cs = _ucode32sel; 677 regs->tf_ss = _udatasel; 678 regs->tf_ds = _udatasel; 679 regs->tf_es = _udatasel; 680 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 681 /* XXXKIB leave user %fs and %gs untouched */ 682 PROC_LOCK(p); 683 mtx_lock(&psp->ps_mtx); 684 } 685 686 /* 687 * System call to cleanup state after a signal 688 * has been taken. Reset signal mask and 689 * stack state from context left by sendsig (above). 690 * Return to previous pc and psl as specified by 691 * context left by sendsig. Check carefully to 692 * make sure that the user has not modified the 693 * state to gain improper privileges. 694 */ 695 696 #ifdef COMPAT_43 697 int 698 ofreebsd32_sigreturn(struct thread *td, struct ofreebsd32_sigreturn_args *uap) 699 { 700 struct ia32_sigcontext3 sc, *scp; 701 struct trapframe *regs; 702 int eflags, error; 703 ksiginfo_t ksi; 704 705 regs = td->td_frame; 706 error = copyin(uap->sigcntxp, &sc, sizeof(sc)); 707 if (error != 0) 708 return (error); 709 scp = ≻ 710 eflags = scp->sc_eflags; 711 if (!EFL_SECURE(eflags, regs->tf_rflags)) { 712 return (EINVAL); 713 } 714 if (!CS_SECURE(scp->sc_cs)) { 715 ksiginfo_init_trap(&ksi); 716 ksi.ksi_signo = SIGBUS; 717 ksi.ksi_code = BUS_OBJERR; 718 ksi.ksi_trapno = T_PROTFLT; 719 ksi.ksi_addr = (void *)regs->tf_rip; 720 trapsignal(td, &ksi); 721 return (EINVAL); 722 } 723 regs->tf_ds = scp->sc_ds; 724 regs->tf_es = scp->sc_es; 725 regs->tf_fs = scp->sc_fs; 726 regs->tf_gs = scp->sc_gs; 727 728 regs->tf_rax = scp->sc_eax; 729 regs->tf_rbx = scp->sc_ebx; 730 regs->tf_rcx = scp->sc_ecx; 731 regs->tf_rdx = scp->sc_edx; 732 regs->tf_rsi = scp->sc_esi; 733 regs->tf_rdi = scp->sc_edi; 734 regs->tf_cs = scp->sc_cs; 735 regs->tf_ss = scp->sc_ss; 736 regs->tf_rbp = scp->sc_ebp; 737 regs->tf_rsp = scp->sc_esp; 738 regs->tf_rip = scp->sc_eip; 739 regs->tf_rflags = eflags; 740 741 if (scp->sc_onstack & 1) 742 td->td_sigstk.ss_flags |= SS_ONSTACK; 743 else 744 td->td_sigstk.ss_flags &= ~SS_ONSTACK; 745 746 kern_sigprocmask(td, SIG_SETMASK, (sigset_t *)&scp->sc_mask, NULL, 747 SIGPROCMASK_OLD); 748 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 749 return (EJUSTRETURN); 750 } 751 #endif 752 753 #ifdef COMPAT_FREEBSD4 754 /* 755 * MPSAFE 756 */ 757 int 758 freebsd4_freebsd32_sigreturn(td, uap) 759 struct thread *td; 760 struct freebsd4_freebsd32_sigreturn_args /* { 761 const struct freebsd4_freebsd32_ucontext *sigcntxp; 762 } */ *uap; 763 { 764 struct ia32_ucontext4 uc; 765 struct trapframe *regs; 766 struct ia32_ucontext4 *ucp; 767 int cs, eflags, error; 768 ksiginfo_t ksi; 769 770 error = copyin(uap->sigcntxp, &uc, sizeof(uc)); 771 if (error != 0) 772 return (error); 773 ucp = &uc; 774 regs = td->td_frame; 775 eflags = ucp->uc_mcontext.mc_eflags; 776 /* 777 * Don't allow users to change privileged or reserved flags. 778 */ 779 if (!EFL_SECURE(eflags, regs->tf_rflags)) { 780 uprintf("pid %d (%s): freebsd4_freebsd32_sigreturn eflags = 0x%x\n", 781 td->td_proc->p_pid, td->td_name, eflags); 782 return (EINVAL); 783 } 784 785 /* 786 * Don't allow users to load a valid privileged %cs. Let the 787 * hardware check for invalid selectors, excess privilege in 788 * other selectors, invalid %eip's and invalid %esp's. 789 */ 790 cs = ucp->uc_mcontext.mc_cs; 791 if (!CS_SECURE(cs)) { 792 uprintf("pid %d (%s): freebsd4_sigreturn cs = 0x%x\n", 793 td->td_proc->p_pid, td->td_name, cs); 794 ksiginfo_init_trap(&ksi); 795 ksi.ksi_signo = SIGBUS; 796 ksi.ksi_code = BUS_OBJERR; 797 ksi.ksi_trapno = T_PROTFLT; 798 ksi.ksi_addr = (void *)regs->tf_rip; 799 trapsignal(td, &ksi); 800 return (EINVAL); 801 } 802 803 regs->tf_rdi = ucp->uc_mcontext.mc_edi; 804 regs->tf_rsi = ucp->uc_mcontext.mc_esi; 805 regs->tf_rbp = ucp->uc_mcontext.mc_ebp; 806 regs->tf_rbx = ucp->uc_mcontext.mc_ebx; 807 regs->tf_rdx = ucp->uc_mcontext.mc_edx; 808 regs->tf_rcx = ucp->uc_mcontext.mc_ecx; 809 regs->tf_rax = ucp->uc_mcontext.mc_eax; 810 regs->tf_trapno = ucp->uc_mcontext.mc_trapno; 811 regs->tf_err = ucp->uc_mcontext.mc_err; 812 regs->tf_rip = ucp->uc_mcontext.mc_eip; 813 regs->tf_cs = cs; 814 regs->tf_rflags = ucp->uc_mcontext.mc_eflags; 815 regs->tf_rsp = ucp->uc_mcontext.mc_esp; 816 regs->tf_ss = ucp->uc_mcontext.mc_ss; 817 regs->tf_ds = ucp->uc_mcontext.mc_ds; 818 regs->tf_es = ucp->uc_mcontext.mc_es; 819 regs->tf_fs = ucp->uc_mcontext.mc_fs; 820 regs->tf_gs = ucp->uc_mcontext.mc_gs; 821 822 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0); 823 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 824 return (EJUSTRETURN); 825 } 826 #endif /* COMPAT_FREEBSD4 */ 827 828 /* 829 * MPSAFE 830 */ 831 int 832 freebsd32_sigreturn(td, uap) 833 struct thread *td; 834 struct freebsd32_sigreturn_args /* { 835 const struct freebsd32_ucontext *sigcntxp; 836 } */ *uap; 837 { 838 struct ia32_ucontext uc; 839 struct trapframe *regs; 840 struct ia32_ucontext *ucp; 841 char *xfpustate; 842 size_t xfpustate_len; 843 int cs, eflags, error, ret; 844 ksiginfo_t ksi; 845 846 error = copyin(uap->sigcntxp, &uc, sizeof(uc)); 847 if (error != 0) 848 return (error); 849 ucp = &uc; 850 regs = td->td_frame; 851 eflags = ucp->uc_mcontext.mc_eflags; 852 /* 853 * Don't allow users to change privileged or reserved flags. 854 */ 855 if (!EFL_SECURE(eflags, regs->tf_rflags)) { 856 uprintf("pid %d (%s): freebsd32_sigreturn eflags = 0x%x\n", 857 td->td_proc->p_pid, td->td_name, eflags); 858 return (EINVAL); 859 } 860 861 /* 862 * Don't allow users to load a valid privileged %cs. Let the 863 * hardware check for invalid selectors, excess privilege in 864 * other selectors, invalid %eip's and invalid %esp's. 865 */ 866 cs = ucp->uc_mcontext.mc_cs; 867 if (!CS_SECURE(cs)) { 868 uprintf("pid %d (%s): sigreturn cs = 0x%x\n", 869 td->td_proc->p_pid, td->td_name, cs); 870 ksiginfo_init_trap(&ksi); 871 ksi.ksi_signo = SIGBUS; 872 ksi.ksi_code = BUS_OBJERR; 873 ksi.ksi_trapno = T_PROTFLT; 874 ksi.ksi_addr = (void *)regs->tf_rip; 875 trapsignal(td, &ksi); 876 return (EINVAL); 877 } 878 879 if ((ucp->uc_mcontext.mc_flags & _MC_HASFPXSTATE) != 0) { 880 xfpustate_len = uc.uc_mcontext.mc_xfpustate_len; 881 if (xfpustate_len > cpu_max_ext_state_size - 882 sizeof(struct savefpu)) { 883 uprintf("pid %d (%s): sigreturn xfpusave_len = 0x%zx\n", 884 td->td_proc->p_pid, td->td_name, xfpustate_len); 885 return (EINVAL); 886 } 887 xfpustate = __builtin_alloca(xfpustate_len); 888 error = copyin(PTRIN(ucp->uc_mcontext.mc_xfpustate), 889 xfpustate, xfpustate_len); 890 if (error != 0) { 891 uprintf( 892 "pid %d (%s): sigreturn copying xfpustate failed\n", 893 td->td_proc->p_pid, td->td_name); 894 return (error); 895 } 896 } else { 897 xfpustate = NULL; 898 xfpustate_len = 0; 899 } 900 ret = ia32_set_fpcontext(td, &ucp->uc_mcontext, xfpustate, 901 xfpustate_len); 902 if (ret != 0) { 903 uprintf("pid %d (%s): sigreturn set_fpcontext err %d\n", 904 td->td_proc->p_pid, td->td_name, ret); 905 return (ret); 906 } 907 908 regs->tf_rdi = ucp->uc_mcontext.mc_edi; 909 regs->tf_rsi = ucp->uc_mcontext.mc_esi; 910 regs->tf_rbp = ucp->uc_mcontext.mc_ebp; 911 regs->tf_rbx = ucp->uc_mcontext.mc_ebx; 912 regs->tf_rdx = ucp->uc_mcontext.mc_edx; 913 regs->tf_rcx = ucp->uc_mcontext.mc_ecx; 914 regs->tf_rax = ucp->uc_mcontext.mc_eax; 915 regs->tf_trapno = ucp->uc_mcontext.mc_trapno; 916 regs->tf_err = ucp->uc_mcontext.mc_err; 917 regs->tf_rip = ucp->uc_mcontext.mc_eip; 918 regs->tf_cs = cs; 919 regs->tf_rflags = ucp->uc_mcontext.mc_eflags; 920 regs->tf_rsp = ucp->uc_mcontext.mc_esp; 921 regs->tf_ss = ucp->uc_mcontext.mc_ss; 922 regs->tf_ds = ucp->uc_mcontext.mc_ds; 923 regs->tf_es = ucp->uc_mcontext.mc_es; 924 regs->tf_fs = ucp->uc_mcontext.mc_fs; 925 regs->tf_gs = ucp->uc_mcontext.mc_gs; 926 regs->tf_flags = TF_HASSEGS; 927 928 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0); 929 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 930 return (EJUSTRETURN); 931 } 932 933 /* 934 * Clear registers on exec 935 */ 936 void 937 ia32_setregs(struct thread *td, struct image_params *imgp, u_long stack) 938 { 939 struct trapframe *regs; 940 struct pcb *pcb; 941 register_t saved_rflags; 942 943 regs = td->td_frame; 944 pcb = td->td_pcb; 945 946 if (td->td_proc->p_md.md_ldt != NULL) 947 user_ldt_free(td); 948 #ifdef COMPAT_43 949 setup_lcall_gate(); 950 #endif 951 952 pcb->pcb_fsbase = 0; 953 pcb->pcb_gsbase = 0; 954 pcb->pcb_initial_fpucw = __INITIAL_FPUCW_I386__; 955 956 saved_rflags = regs->tf_rflags & PSL_T; 957 bzero((char *)regs, sizeof(struct trapframe)); 958 regs->tf_rip = imgp->entry_addr; 959 regs->tf_rsp = stack; 960 regs->tf_rflags = PSL_USER | saved_rflags; 961 regs->tf_ss = _udatasel; 962 regs->tf_cs = _ucode32sel; 963 regs->tf_rbx = imgp->ps_strings; 964 regs->tf_ds = _udatasel; 965 regs->tf_es = _udatasel; 966 regs->tf_fs = _ufssel; 967 regs->tf_gs = _ugssel; 968 regs->tf_flags = TF_HASSEGS; 969 970 fpstate_drop(td); 971 972 /* Return via doreti so that we can change to a different %cs */ 973 set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET); 974 } 975