1 /*- 2 * Copyright (c) 2003 Peter Wemm 3 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * William Jolitz. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_compat.h" 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 84 #ifdef COMPAT_FREEBSD4 85 static void freebsd4_ia32_sendsig(sig_t, ksiginfo_t *, sigset_t *); 86 #endif 87 88 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 89 #define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 90 91 static void 92 ia32_get_fpcontext(struct thread *td, struct ia32_mcontext *mcp, 93 char *xfpusave, size_t xfpusave_len) 94 { 95 size_t max_len, len; 96 97 /* 98 * XXX Format of 64bit and 32bit FXSAVE areas differs. FXSAVE 99 * in 32bit mode saves %cs and %ds, while on 64bit it saves 100 * 64bit instruction and data pointers. Ignore the difference 101 * for now, it should be irrelevant for most applications. 102 */ 103 mcp->mc_ownedfp = fpugetregs(td); 104 bcopy(get_pcb_user_save_td(td), &mcp->mc_fpstate, 105 sizeof(mcp->mc_fpstate)); 106 mcp->mc_fpformat = fpuformat(); 107 if (!use_xsave || xfpusave_len == 0) 108 return; 109 max_len = cpu_max_ext_state_size - sizeof(struct savefpu); 110 len = xfpusave_len; 111 if (len > max_len) { 112 len = max_len; 113 bzero(xfpusave + max_len, len - max_len); 114 } 115 mcp->mc_flags |= _MC_HASFPXSTATE; 116 mcp->mc_xfpustate_len = len; 117 bcopy(get_pcb_user_save_td(td) + 1, xfpusave, len); 118 } 119 120 static int 121 ia32_set_fpcontext(struct thread *td, const struct ia32_mcontext *mcp, 122 char *xfpustate, size_t xfpustate_len) 123 { 124 int error; 125 126 if (mcp->mc_fpformat == _MC_FPFMT_NODEV) 127 return (0); 128 else if (mcp->mc_fpformat != _MC_FPFMT_XMM) 129 return (EINVAL); 130 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE) { 131 /* We don't care what state is left in the FPU or PCB. */ 132 fpstate_drop(td); 133 error = 0; 134 } else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU || 135 mcp->mc_ownedfp == _MC_FPOWNED_PCB) { 136 error = fpusetregs(td, (struct savefpu *)&mcp->mc_fpstate, 137 xfpustate, xfpustate_len); 138 } else 139 return (EINVAL); 140 return (error); 141 } 142 143 /* 144 * Get machine context. 145 */ 146 static int 147 ia32_get_mcontext(struct thread *td, struct ia32_mcontext *mcp, int flags) 148 { 149 struct pcb *pcb; 150 struct trapframe *tp; 151 152 pcb = td->td_pcb; 153 tp = td->td_frame; 154 155 PROC_LOCK(curthread->td_proc); 156 mcp->mc_onstack = sigonstack(tp->tf_rsp); 157 PROC_UNLOCK(curthread->td_proc); 158 /* Entry into kernel always sets TF_HASSEGS */ 159 mcp->mc_gs = tp->tf_gs; 160 mcp->mc_fs = tp->tf_fs; 161 mcp->mc_es = tp->tf_es; 162 mcp->mc_ds = tp->tf_ds; 163 mcp->mc_edi = tp->tf_rdi; 164 mcp->mc_esi = tp->tf_rsi; 165 mcp->mc_ebp = tp->tf_rbp; 166 mcp->mc_isp = tp->tf_rsp; 167 mcp->mc_eflags = tp->tf_rflags; 168 if (flags & GET_MC_CLEAR_RET) { 169 mcp->mc_eax = 0; 170 mcp->mc_edx = 0; 171 mcp->mc_eflags &= ~PSL_C; 172 } else { 173 mcp->mc_eax = tp->tf_rax; 174 mcp->mc_edx = tp->tf_rdx; 175 } 176 mcp->mc_ebx = tp->tf_rbx; 177 mcp->mc_ecx = tp->tf_rcx; 178 mcp->mc_eip = tp->tf_rip; 179 mcp->mc_cs = tp->tf_cs; 180 mcp->mc_esp = tp->tf_rsp; 181 mcp->mc_ss = tp->tf_ss; 182 mcp->mc_len = sizeof(*mcp); 183 mcp->mc_flags = tp->tf_flags; 184 ia32_get_fpcontext(td, mcp, NULL, 0); 185 mcp->mc_fsbase = pcb->pcb_fsbase; 186 mcp->mc_gsbase = pcb->pcb_gsbase; 187 mcp->mc_xfpustate = 0; 188 mcp->mc_xfpustate_len = 0; 189 bzero(mcp->mc_spare2, sizeof(mcp->mc_spare2)); 190 set_pcb_flags(pcb, PCB_FULL_IRET); 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, const 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 *)(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 /* Translate the signal if appropriate. */ 365 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize) 366 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)]; 367 368 /* Build the argument list for the signal handler. */ 369 sf.sf_signum = sig; 370 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc; 371 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 372 /* Signal handler installed with SA_SIGINFO. */ 373 sf.sf_arg2 = (register_t)&fp->sf_siginfo; 374 sf.sf_siginfo.si_signo = sig; 375 sf.sf_siginfo.si_code = ksi->ksi_code; 376 sf.sf_ah = (uintptr_t)catcher; 377 } else { 378 /* Old FreeBSD-style arguments. */ 379 sf.sf_arg2 = ksi->ksi_code; 380 sf.sf_addr = (register_t)ksi->ksi_addr; 381 sf.sf_ah = (uintptr_t)catcher; 382 } 383 mtx_unlock(&psp->ps_mtx); 384 PROC_UNLOCK(p); 385 386 /* Save most if not all of trap frame. */ 387 sf.sf_siginfo.si_sc.sc_eax = regs->tf_rax; 388 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_rbx; 389 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_rcx; 390 sf.sf_siginfo.si_sc.sc_edx = regs->tf_rdx; 391 sf.sf_siginfo.si_sc.sc_esi = regs->tf_rsi; 392 sf.sf_siginfo.si_sc.sc_edi = regs->tf_rdi; 393 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs; 394 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds; 395 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss; 396 sf.sf_siginfo.si_sc.sc_es = regs->tf_es; 397 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs; 398 sf.sf_siginfo.si_sc.sc_gs = regs->tf_gs; 399 sf.sf_siginfo.si_sc.sc_isp = regs->tf_rsp; 400 401 /* Build the signal context to be used by osigreturn(). */ 402 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0; 403 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask); 404 sf.sf_siginfo.si_sc.sc_esp = regs->tf_rsp; 405 sf.sf_siginfo.si_sc.sc_ebp = regs->tf_rbp; 406 sf.sf_siginfo.si_sc.sc_eip = regs->tf_rip; 407 sf.sf_siginfo.si_sc.sc_eflags = regs->tf_rflags; 408 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno; 409 sf.sf_siginfo.si_sc.sc_err = regs->tf_err; 410 411 /* 412 * Copy the sigframe out to the user's stack. 413 */ 414 if (copyout(&sf, fp, sizeof(*fp)) != 0) { 415 #ifdef DEBUG 416 printf("process %ld has trashed its stack\n", (long)p->p_pid); 417 #endif 418 PROC_LOCK(p); 419 sigexit(td, SIGILL); 420 } 421 422 regs->tf_rsp = (uintptr_t)fp; 423 regs->tf_rip = p->p_sysent->sv_psstrings - sz_ia32_osigcode; 424 regs->tf_rflags &= ~(PSL_T | PSL_D); 425 regs->tf_cs = _ucode32sel; 426 regs->tf_ds = _udatasel; 427 regs->tf_es = _udatasel; 428 regs->tf_fs = _udatasel; 429 regs->tf_ss = _udatasel; 430 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 431 PROC_LOCK(p); 432 mtx_lock(&psp->ps_mtx); 433 } 434 #endif 435 436 #ifdef COMPAT_FREEBSD4 437 static void 438 freebsd4_ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 439 { 440 struct ia32_sigframe4 sf, *sfp; 441 struct siginfo32 siginfo; 442 struct proc *p; 443 struct thread *td; 444 struct sigacts *psp; 445 struct trapframe *regs; 446 int oonstack; 447 int sig; 448 449 td = curthread; 450 p = td->td_proc; 451 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo); 452 453 PROC_LOCK_ASSERT(p, MA_OWNED); 454 sig = siginfo.si_signo; 455 psp = p->p_sigacts; 456 mtx_assert(&psp->ps_mtx, MA_OWNED); 457 regs = td->td_frame; 458 oonstack = sigonstack(regs->tf_rsp); 459 460 /* Save user context. */ 461 bzero(&sf, sizeof(sf)); 462 sf.sf_uc.uc_sigmask = *mask; 463 sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp; 464 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size; 465 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 466 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 467 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0; 468 sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi; 469 sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi; 470 sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp; 471 sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */ 472 sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx; 473 sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx; 474 sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx; 475 sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax; 476 sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno; 477 sf.sf_uc.uc_mcontext.mc_err = regs->tf_err; 478 sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip; 479 sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs; 480 sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags; 481 sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp; 482 sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss; 483 sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds; 484 sf.sf_uc.uc_mcontext.mc_es = regs->tf_es; 485 sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs; 486 sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs; 487 bzero(sf.sf_uc.uc_mcontext.mc_fpregs, 488 sizeof(sf.sf_uc.uc_mcontext.mc_fpregs)); 489 bzero(sf.sf_uc.uc_mcontext.__spare__, 490 sizeof(sf.sf_uc.uc_mcontext.__spare__)); 491 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__)); 492 493 /* Allocate space for the signal handler context. */ 494 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack && 495 SIGISMEMBER(psp->ps_sigonstack, sig)) { 496 sfp = (struct ia32_sigframe4 *)(td->td_sigstk.ss_sp + 497 td->td_sigstk.ss_size - sizeof(sf)); 498 } else 499 sfp = (struct ia32_sigframe4 *)regs->tf_rsp - 1; 500 PROC_UNLOCK(p); 501 502 /* Translate the signal if appropriate. */ 503 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize) 504 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)]; 505 506 /* Build the argument list for the signal handler. */ 507 sf.sf_signum = sig; 508 sf.sf_ucontext = (register_t)&sfp->sf_uc; 509 bzero(&sf.sf_si, sizeof(sf.sf_si)); 510 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 511 /* Signal handler installed with SA_SIGINFO. */ 512 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si; 513 sf.sf_ah = (u_int32_t)(uintptr_t)catcher; 514 515 /* Fill in POSIX parts */ 516 sf.sf_si = siginfo; 517 sf.sf_si.si_signo = sig; 518 } else { 519 /* Old FreeBSD-style arguments. */ 520 sf.sf_siginfo = siginfo.si_code; 521 sf.sf_addr = (u_int32_t)siginfo.si_addr; 522 sf.sf_ah = (u_int32_t)(uintptr_t)catcher; 523 } 524 mtx_unlock(&psp->ps_mtx); 525 526 /* 527 * Copy the sigframe out to the user's stack. 528 */ 529 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) { 530 #ifdef DEBUG 531 printf("process %ld has trashed its stack\n", (long)p->p_pid); 532 #endif 533 PROC_LOCK(p); 534 sigexit(td, SIGILL); 535 } 536 537 regs->tf_rsp = (uintptr_t)sfp; 538 regs->tf_rip = p->p_sysent->sv_sigcode_base + sz_ia32_sigcode - 539 sz_freebsd4_ia32_sigcode; 540 regs->tf_rflags &= ~(PSL_T | PSL_D); 541 regs->tf_cs = _ucode32sel; 542 regs->tf_ss = _udatasel; 543 regs->tf_ds = _udatasel; 544 regs->tf_es = _udatasel; 545 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 546 /* leave user %fs and %gs untouched */ 547 PROC_LOCK(p); 548 mtx_lock(&psp->ps_mtx); 549 } 550 #endif /* COMPAT_FREEBSD4 */ 551 552 void 553 ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 554 { 555 struct ia32_sigframe sf, *sfp; 556 struct siginfo32 siginfo; 557 struct proc *p; 558 struct thread *td; 559 struct sigacts *psp; 560 char *sp; 561 struct trapframe *regs; 562 char *xfpusave; 563 size_t xfpusave_len; 564 int oonstack; 565 int sig; 566 567 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo); 568 td = curthread; 569 p = td->td_proc; 570 PROC_LOCK_ASSERT(p, MA_OWNED); 571 sig = siginfo.si_signo; 572 psp = p->p_sigacts; 573 #ifdef COMPAT_FREEBSD4 574 if (SIGISMEMBER(psp->ps_freebsd4, sig)) { 575 freebsd4_ia32_sendsig(catcher, ksi, mask); 576 return; 577 } 578 #endif 579 #ifdef COMPAT_43 580 if (SIGISMEMBER(psp->ps_osigset, sig)) { 581 ia32_osendsig(catcher, ksi, mask); 582 return; 583 } 584 #endif 585 mtx_assert(&psp->ps_mtx, MA_OWNED); 586 regs = td->td_frame; 587 oonstack = sigonstack(regs->tf_rsp); 588 589 if (cpu_max_ext_state_size > sizeof(struct savefpu) && use_xsave) { 590 xfpusave_len = cpu_max_ext_state_size - sizeof(struct savefpu); 591 xfpusave = __builtin_alloca(xfpusave_len); 592 } else { 593 xfpusave_len = 0; 594 xfpusave = NULL; 595 } 596 597 /* Save user context. */ 598 bzero(&sf, sizeof(sf)); 599 sf.sf_uc.uc_sigmask = *mask; 600 sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp; 601 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size; 602 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 603 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 604 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0; 605 sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi; 606 sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi; 607 sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp; 608 sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */ 609 sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx; 610 sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx; 611 sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx; 612 sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax; 613 sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno; 614 sf.sf_uc.uc_mcontext.mc_err = regs->tf_err; 615 sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip; 616 sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs; 617 sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags; 618 sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp; 619 sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss; 620 sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds; 621 sf.sf_uc.uc_mcontext.mc_es = regs->tf_es; 622 sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs; 623 sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs; 624 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */ 625 ia32_get_fpcontext(td, &sf.sf_uc.uc_mcontext, xfpusave, xfpusave_len); 626 fpstate_drop(td); 627 sf.sf_uc.uc_mcontext.mc_fsbase = td->td_pcb->pcb_fsbase; 628 sf.sf_uc.uc_mcontext.mc_gsbase = td->td_pcb->pcb_gsbase; 629 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__)); 630 631 /* Allocate space for the signal handler context. */ 632 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack && 633 SIGISMEMBER(psp->ps_sigonstack, sig)) 634 sp = td->td_sigstk.ss_sp + td->td_sigstk.ss_size; 635 else 636 sp = (char *)regs->tf_rsp; 637 if (xfpusave != NULL) { 638 sp -= xfpusave_len; 639 sp = (char *)((unsigned long)sp & ~0x3Ful); 640 sf.sf_uc.uc_mcontext.mc_xfpustate = (register_t)sp; 641 } 642 sp -= sizeof(sf); 643 /* Align to 16 bytes. */ 644 sfp = (struct ia32_sigframe *)((uintptr_t)sp & ~0xF); 645 PROC_UNLOCK(p); 646 647 /* Translate the signal if appropriate. */ 648 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize) 649 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)]; 650 651 /* Build the argument list for the signal handler. */ 652 sf.sf_signum = sig; 653 sf.sf_ucontext = (register_t)&sfp->sf_uc; 654 bzero(&sf.sf_si, sizeof(sf.sf_si)); 655 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 656 /* Signal handler installed with SA_SIGINFO. */ 657 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si; 658 sf.sf_ah = (u_int32_t)(uintptr_t)catcher; 659 660 /* Fill in POSIX parts */ 661 sf.sf_si = siginfo; 662 sf.sf_si.si_signo = sig; 663 } else { 664 /* Old FreeBSD-style arguments. */ 665 sf.sf_siginfo = siginfo.si_code; 666 sf.sf_addr = (u_int32_t)siginfo.si_addr; 667 sf.sf_ah = (u_int32_t)(uintptr_t)catcher; 668 } 669 mtx_unlock(&psp->ps_mtx); 670 671 /* 672 * Copy the sigframe out to the user's stack. 673 */ 674 if (copyout(&sf, sfp, sizeof(*sfp)) != 0 || 675 (xfpusave != NULL && copyout(xfpusave, 676 PTRIN(sf.sf_uc.uc_mcontext.mc_xfpustate), xfpusave_len) 677 != 0)) { 678 #ifdef DEBUG 679 printf("process %ld has trashed its stack\n", (long)p->p_pid); 680 #endif 681 PROC_LOCK(p); 682 sigexit(td, SIGILL); 683 } 684 685 regs->tf_rsp = (uintptr_t)sfp; 686 regs->tf_rip = p->p_sysent->sv_sigcode_base; 687 regs->tf_rflags &= ~(PSL_T | PSL_D); 688 regs->tf_cs = _ucode32sel; 689 regs->tf_ss = _udatasel; 690 regs->tf_ds = _udatasel; 691 regs->tf_es = _udatasel; 692 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 693 /* XXXKIB leave user %fs and %gs untouched */ 694 PROC_LOCK(p); 695 mtx_lock(&psp->ps_mtx); 696 } 697 698 /* 699 * System call to cleanup state after a signal 700 * has been taken. Reset signal mask and 701 * stack state from context left by sendsig (above). 702 * Return to previous pc and psl as specified by 703 * context left by sendsig. Check carefully to 704 * make sure that the user has not modified the 705 * state to gain improper privileges. 706 */ 707 708 #ifdef COMPAT_43 709 int 710 ofreebsd32_sigreturn(struct thread *td, struct ofreebsd32_sigreturn_args *uap) 711 { 712 struct ia32_sigcontext3 sc, *scp; 713 struct trapframe *regs; 714 int eflags, error; 715 ksiginfo_t ksi; 716 717 regs = td->td_frame; 718 error = copyin(uap->sigcntxp, &sc, sizeof(sc)); 719 if (error != 0) 720 return (error); 721 scp = ≻ 722 eflags = scp->sc_eflags; 723 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) { 724 return (EINVAL); 725 } 726 if (!CS_SECURE(scp->sc_cs)) { 727 ksiginfo_init_trap(&ksi); 728 ksi.ksi_signo = SIGBUS; 729 ksi.ksi_code = BUS_OBJERR; 730 ksi.ksi_trapno = T_PROTFLT; 731 ksi.ksi_addr = (void *)regs->tf_rip; 732 trapsignal(td, &ksi); 733 return (EINVAL); 734 } 735 regs->tf_ds = scp->sc_ds; 736 regs->tf_es = scp->sc_es; 737 regs->tf_fs = scp->sc_fs; 738 regs->tf_gs = scp->sc_gs; 739 740 regs->tf_rax = scp->sc_eax; 741 regs->tf_rbx = scp->sc_ebx; 742 regs->tf_rcx = scp->sc_ecx; 743 regs->tf_rdx = scp->sc_edx; 744 regs->tf_rsi = scp->sc_esi; 745 regs->tf_rdi = scp->sc_edi; 746 regs->tf_cs = scp->sc_cs; 747 regs->tf_ss = scp->sc_ss; 748 regs->tf_rbp = scp->sc_ebp; 749 regs->tf_rsp = scp->sc_esp; 750 regs->tf_rip = scp->sc_eip; 751 regs->tf_rflags = eflags; 752 753 if (scp->sc_onstack & 1) 754 td->td_sigstk.ss_flags |= SS_ONSTACK; 755 else 756 td->td_sigstk.ss_flags &= ~SS_ONSTACK; 757 758 kern_sigprocmask(td, SIG_SETMASK, (sigset_t *)&scp->sc_mask, NULL, 759 SIGPROCMASK_OLD); 760 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 761 return (EJUSTRETURN); 762 } 763 #endif 764 765 #ifdef COMPAT_FREEBSD4 766 /* 767 * MPSAFE 768 */ 769 int 770 freebsd4_freebsd32_sigreturn(td, uap) 771 struct thread *td; 772 struct freebsd4_freebsd32_sigreturn_args /* { 773 const struct freebsd4_freebsd32_ucontext *sigcntxp; 774 } */ *uap; 775 { 776 struct ia32_ucontext4 uc; 777 struct trapframe *regs; 778 struct ia32_ucontext4 *ucp; 779 int cs, eflags, error; 780 ksiginfo_t ksi; 781 782 error = copyin(uap->sigcntxp, &uc, sizeof(uc)); 783 if (error != 0) 784 return (error); 785 ucp = &uc; 786 regs = td->td_frame; 787 eflags = ucp->uc_mcontext.mc_eflags; 788 /* 789 * Don't allow users to change privileged or reserved flags. 790 */ 791 /* 792 * XXX do allow users to change the privileged flag PSL_RF. 793 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers 794 * should sometimes set it there too. tf_eflags is kept in 795 * the signal context during signal handling and there is no 796 * other place to remember it, so the PSL_RF bit may be 797 * corrupted by the signal handler without us knowing. 798 * Corruption of the PSL_RF bit at worst causes one more or 799 * one less debugger trap, so allowing it is fairly harmless. 800 */ 801 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) { 802 uprintf("pid %d (%s): freebsd4_freebsd32_sigreturn eflags = 0x%x\n", 803 td->td_proc->p_pid, td->td_name, eflags); 804 return (EINVAL); 805 } 806 807 /* 808 * Don't allow users to load a valid privileged %cs. Let the 809 * hardware check for invalid selectors, excess privilege in 810 * other selectors, invalid %eip's and invalid %esp's. 811 */ 812 cs = ucp->uc_mcontext.mc_cs; 813 if (!CS_SECURE(cs)) { 814 uprintf("pid %d (%s): freebsd4_sigreturn cs = 0x%x\n", 815 td->td_proc->p_pid, td->td_name, cs); 816 ksiginfo_init_trap(&ksi); 817 ksi.ksi_signo = SIGBUS; 818 ksi.ksi_code = BUS_OBJERR; 819 ksi.ksi_trapno = T_PROTFLT; 820 ksi.ksi_addr = (void *)regs->tf_rip; 821 trapsignal(td, &ksi); 822 return (EINVAL); 823 } 824 825 regs->tf_rdi = ucp->uc_mcontext.mc_edi; 826 regs->tf_rsi = ucp->uc_mcontext.mc_esi; 827 regs->tf_rbp = ucp->uc_mcontext.mc_ebp; 828 regs->tf_rbx = ucp->uc_mcontext.mc_ebx; 829 regs->tf_rdx = ucp->uc_mcontext.mc_edx; 830 regs->tf_rcx = ucp->uc_mcontext.mc_ecx; 831 regs->tf_rax = ucp->uc_mcontext.mc_eax; 832 regs->tf_trapno = ucp->uc_mcontext.mc_trapno; 833 regs->tf_err = ucp->uc_mcontext.mc_err; 834 regs->tf_rip = ucp->uc_mcontext.mc_eip; 835 regs->tf_cs = cs; 836 regs->tf_rflags = ucp->uc_mcontext.mc_eflags; 837 regs->tf_rsp = ucp->uc_mcontext.mc_esp; 838 regs->tf_ss = ucp->uc_mcontext.mc_ss; 839 regs->tf_ds = ucp->uc_mcontext.mc_ds; 840 regs->tf_es = ucp->uc_mcontext.mc_es; 841 regs->tf_fs = ucp->uc_mcontext.mc_fs; 842 regs->tf_gs = ucp->uc_mcontext.mc_gs; 843 844 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0); 845 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 846 return (EJUSTRETURN); 847 } 848 #endif /* COMPAT_FREEBSD4 */ 849 850 /* 851 * MPSAFE 852 */ 853 int 854 freebsd32_sigreturn(td, uap) 855 struct thread *td; 856 struct freebsd32_sigreturn_args /* { 857 const struct freebsd32_ucontext *sigcntxp; 858 } */ *uap; 859 { 860 struct ia32_ucontext uc; 861 struct trapframe *regs; 862 struct ia32_ucontext *ucp; 863 char *xfpustate; 864 size_t xfpustate_len; 865 int cs, eflags, error, ret; 866 ksiginfo_t ksi; 867 868 error = copyin(uap->sigcntxp, &uc, sizeof(uc)); 869 if (error != 0) 870 return (error); 871 ucp = &uc; 872 regs = td->td_frame; 873 eflags = ucp->uc_mcontext.mc_eflags; 874 /* 875 * Don't allow users to change privileged or reserved flags. 876 */ 877 /* 878 * XXX do allow users to change the privileged flag PSL_RF. 879 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers 880 * should sometimes set it there too. tf_eflags is kept in 881 * the signal context during signal handling and there is no 882 * other place to remember it, so the PSL_RF bit may be 883 * corrupted by the signal handler without us knowing. 884 * Corruption of the PSL_RF bit at worst causes one more or 885 * one less debugger trap, so allowing it is fairly harmless. 886 */ 887 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) { 888 uprintf("pid %d (%s): freebsd32_sigreturn eflags = 0x%x\n", 889 td->td_proc->p_pid, td->td_name, eflags); 890 return (EINVAL); 891 } 892 893 /* 894 * Don't allow users to load a valid privileged %cs. Let the 895 * hardware check for invalid selectors, excess privilege in 896 * other selectors, invalid %eip's and invalid %esp's. 897 */ 898 cs = ucp->uc_mcontext.mc_cs; 899 if (!CS_SECURE(cs)) { 900 uprintf("pid %d (%s): sigreturn cs = 0x%x\n", 901 td->td_proc->p_pid, td->td_name, cs); 902 ksiginfo_init_trap(&ksi); 903 ksi.ksi_signo = SIGBUS; 904 ksi.ksi_code = BUS_OBJERR; 905 ksi.ksi_trapno = T_PROTFLT; 906 ksi.ksi_addr = (void *)regs->tf_rip; 907 trapsignal(td, &ksi); 908 return (EINVAL); 909 } 910 911 if ((ucp->uc_mcontext.mc_flags & _MC_HASFPXSTATE) != 0) { 912 xfpustate_len = uc.uc_mcontext.mc_xfpustate_len; 913 if (xfpustate_len > cpu_max_ext_state_size - 914 sizeof(struct savefpu)) { 915 uprintf("pid %d (%s): sigreturn xfpusave_len = 0x%zx\n", 916 td->td_proc->p_pid, td->td_name, xfpustate_len); 917 return (EINVAL); 918 } 919 xfpustate = __builtin_alloca(xfpustate_len); 920 error = copyin(PTRIN(ucp->uc_mcontext.mc_xfpustate), 921 xfpustate, xfpustate_len); 922 if (error != 0) { 923 uprintf( 924 "pid %d (%s): sigreturn copying xfpustate failed\n", 925 td->td_proc->p_pid, td->td_name); 926 return (error); 927 } 928 } else { 929 xfpustate = NULL; 930 xfpustate_len = 0; 931 } 932 ret = ia32_set_fpcontext(td, &ucp->uc_mcontext, xfpustate, 933 xfpustate_len); 934 if (ret != 0) { 935 uprintf("pid %d (%s): sigreturn set_fpcontext err %d\n", 936 td->td_proc->p_pid, td->td_name, ret); 937 return (ret); 938 } 939 940 regs->tf_rdi = ucp->uc_mcontext.mc_edi; 941 regs->tf_rsi = ucp->uc_mcontext.mc_esi; 942 regs->tf_rbp = ucp->uc_mcontext.mc_ebp; 943 regs->tf_rbx = ucp->uc_mcontext.mc_ebx; 944 regs->tf_rdx = ucp->uc_mcontext.mc_edx; 945 regs->tf_rcx = ucp->uc_mcontext.mc_ecx; 946 regs->tf_rax = ucp->uc_mcontext.mc_eax; 947 regs->tf_trapno = ucp->uc_mcontext.mc_trapno; 948 regs->tf_err = ucp->uc_mcontext.mc_err; 949 regs->tf_rip = ucp->uc_mcontext.mc_eip; 950 regs->tf_cs = cs; 951 regs->tf_rflags = ucp->uc_mcontext.mc_eflags; 952 regs->tf_rsp = ucp->uc_mcontext.mc_esp; 953 regs->tf_ss = ucp->uc_mcontext.mc_ss; 954 regs->tf_ds = ucp->uc_mcontext.mc_ds; 955 regs->tf_es = ucp->uc_mcontext.mc_es; 956 regs->tf_fs = ucp->uc_mcontext.mc_fs; 957 regs->tf_gs = ucp->uc_mcontext.mc_gs; 958 regs->tf_flags = TF_HASSEGS; 959 960 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0); 961 set_pcb_flags(td->td_pcb, PCB_FULL_IRET); 962 return (EJUSTRETURN); 963 } 964 965 /* 966 * Clear registers on exec 967 */ 968 void 969 ia32_setregs(struct thread *td, struct image_params *imgp, u_long stack) 970 { 971 struct trapframe *regs = td->td_frame; 972 struct pcb *pcb = td->td_pcb; 973 974 mtx_lock(&dt_lock); 975 if (td->td_proc->p_md.md_ldt != NULL) 976 user_ldt_free(td); 977 else 978 mtx_unlock(&dt_lock); 979 #ifdef COMPAT_43 980 setup_lcall_gate(); 981 #endif 982 983 pcb->pcb_fsbase = 0; 984 pcb->pcb_gsbase = 0; 985 pcb->pcb_initial_fpucw = __INITIAL_FPUCW_I386__; 986 987 bzero((char *)regs, sizeof(struct trapframe)); 988 regs->tf_rip = imgp->entry_addr; 989 regs->tf_rsp = stack; 990 regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T); 991 regs->tf_ss = _udatasel; 992 regs->tf_cs = _ucode32sel; 993 regs->tf_rbx = imgp->ps_strings; 994 regs->tf_ds = _udatasel; 995 regs->tf_es = _udatasel; 996 regs->tf_fs = _ufssel; 997 regs->tf_gs = _ugssel; 998 regs->tf_flags = TF_HASSEGS; 999 1000 fpstate_drop(td); 1001 1002 /* Return via doreti so that we can change to a different %cs */ 1003 set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET); 1004 clear_pcb_flags(pcb, PCB_GS32BIT); 1005 td->td_retval[1] = 0; 1006 } 1007