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/sysctl.h> 59 #include <sys/sysent.h> 60 #include <sys/vnode.h> 61 62 #include <vm/vm.h> 63 #include <vm/vm_kern.h> 64 #include <vm/vm_param.h> 65 #include <vm/pmap.h> 66 #include <vm/vm_map.h> 67 #include <vm/vm_object.h> 68 #include <vm/vm_extern.h> 69 70 #include <compat/freebsd32/freebsd32_signal.h> 71 #include <compat/freebsd32/freebsd32_util.h> 72 #include <compat/freebsd32/freebsd32_proto.h> 73 #include <compat/ia32/ia32_signal.h> 74 #include <machine/psl.h> 75 #include <machine/segments.h> 76 #include <machine/specialreg.h> 77 #include <machine/frame.h> 78 #include <machine/md_var.h> 79 #include <machine/pcb.h> 80 #include <machine/cpufunc.h> 81 82 #ifdef COMPAT_FREEBSD4 83 static void freebsd4_ia32_sendsig(sig_t, ksiginfo_t *, sigset_t *); 84 #endif 85 static void ia32_get_fpcontext(struct thread *td, struct ia32_mcontext *mcp); 86 static int ia32_set_fpcontext(struct thread *td, const struct ia32_mcontext *mcp); 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 { 94 95 mcp->mc_ownedfp = fpugetregs(td, (struct savefpu *)&mcp->mc_fpstate); 96 mcp->mc_fpformat = fpuformat(); 97 } 98 99 static int 100 ia32_set_fpcontext(struct thread *td, const struct ia32_mcontext *mcp) 101 { 102 103 if (mcp->mc_fpformat == _MC_FPFMT_NODEV) 104 return (0); 105 else if (mcp->mc_fpformat != _MC_FPFMT_XMM) 106 return (EINVAL); 107 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE) 108 /* We don't care what state is left in the FPU or PCB. */ 109 fpstate_drop(td); 110 else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU || 111 mcp->mc_ownedfp == _MC_FPOWNED_PCB) { 112 /* 113 * XXX we violate the dubious requirement that fpusetregs() 114 * be called with interrupts disabled. 115 */ 116 fpusetregs(td, (struct savefpu *)&mcp->mc_fpstate); 117 } else 118 return (EINVAL); 119 return (0); 120 } 121 122 /* 123 * Get machine context. 124 */ 125 static int 126 ia32_get_mcontext(struct thread *td, struct ia32_mcontext *mcp, int flags) 127 { 128 struct trapframe *tp; 129 130 tp = td->td_frame; 131 132 PROC_LOCK(curthread->td_proc); 133 mcp->mc_onstack = sigonstack(tp->tf_rsp); 134 PROC_UNLOCK(curthread->td_proc); 135 /* Entry into kernel always sets TF_HASSEGS */ 136 mcp->mc_gs = tp->tf_gs; 137 mcp->mc_fs = tp->tf_fs; 138 mcp->mc_es = tp->tf_es; 139 mcp->mc_ds = tp->tf_ds; 140 mcp->mc_edi = tp->tf_rdi; 141 mcp->mc_esi = tp->tf_rsi; 142 mcp->mc_ebp = tp->tf_rbp; 143 mcp->mc_isp = tp->tf_rsp; 144 if (flags & GET_MC_CLEAR_RET) { 145 mcp->mc_eax = 0; 146 mcp->mc_edx = 0; 147 } else { 148 mcp->mc_eax = tp->tf_rax; 149 mcp->mc_edx = tp->tf_rdx; 150 } 151 mcp->mc_ebx = tp->tf_rbx; 152 mcp->mc_ecx = tp->tf_rcx; 153 mcp->mc_eip = tp->tf_rip; 154 mcp->mc_cs = tp->tf_cs; 155 mcp->mc_eflags = tp->tf_rflags; 156 mcp->mc_esp = tp->tf_rsp; 157 mcp->mc_ss = tp->tf_ss; 158 mcp->mc_len = sizeof(*mcp); 159 ia32_get_fpcontext(td, mcp); 160 mcp->mc_fsbase = td->td_pcb->pcb_fsbase; 161 mcp->mc_gsbase = td->td_pcb->pcb_gsbase; 162 td->td_pcb->pcb_full_iret = 1; 163 return (0); 164 } 165 166 /* 167 * Set machine context. 168 * 169 * However, we don't set any but the user modifiable flags, and we won't 170 * touch the cs selector. 171 */ 172 static int 173 ia32_set_mcontext(struct thread *td, const struct ia32_mcontext *mcp) 174 { 175 struct trapframe *tp; 176 long rflags; 177 int ret; 178 179 tp = td->td_frame; 180 if (mcp->mc_len != sizeof(*mcp)) 181 return (EINVAL); 182 rflags = (mcp->mc_eflags & PSL_USERCHANGE) | 183 (tp->tf_rflags & ~PSL_USERCHANGE); 184 ret = ia32_set_fpcontext(td, mcp); 185 if (ret != 0) 186 return (ret); 187 tp->tf_gs = mcp->mc_gs; 188 tp->tf_fs = mcp->mc_fs; 189 tp->tf_es = mcp->mc_es; 190 tp->tf_ds = mcp->mc_ds; 191 tp->tf_flags = TF_HASSEGS; 192 tp->tf_rdi = mcp->mc_edi; 193 tp->tf_rsi = mcp->mc_esi; 194 tp->tf_rbp = mcp->mc_ebp; 195 tp->tf_rbx = mcp->mc_ebx; 196 tp->tf_rdx = mcp->mc_edx; 197 tp->tf_rcx = mcp->mc_ecx; 198 tp->tf_rax = mcp->mc_eax; 199 /* trapno, err */ 200 tp->tf_rip = mcp->mc_eip; 201 tp->tf_rflags = rflags; 202 tp->tf_rsp = mcp->mc_esp; 203 tp->tf_ss = mcp->mc_ss; 204 td->td_pcb->pcb_flags |= PCB_FULLCTX; 205 td->td_pcb->pcb_full_iret = 1; 206 return (0); 207 } 208 209 /* 210 * The first two fields of a ucontext_t are the signal mask and 211 * the machine context. The next field is uc_link; we want to 212 * avoid destroying the link when copying out contexts. 213 */ 214 #define UC_COPY_SIZE offsetof(struct ia32_ucontext, uc_link) 215 216 int 217 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap) 218 { 219 struct ia32_ucontext uc; 220 int ret; 221 222 if (uap->ucp == NULL) 223 ret = EINVAL; 224 else { 225 ia32_get_mcontext(td, &uc.uc_mcontext, GET_MC_CLEAR_RET); 226 PROC_LOCK(td->td_proc); 227 uc.uc_sigmask = td->td_sigmask; 228 PROC_UNLOCK(td->td_proc); 229 ret = copyout(&uc, uap->ucp, UC_COPY_SIZE); 230 } 231 return (ret); 232 } 233 234 int 235 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap) 236 { 237 struct ia32_ucontext uc; 238 int ret; 239 240 if (uap->ucp == NULL) 241 ret = EINVAL; 242 else { 243 ret = copyin(uap->ucp, &uc, UC_COPY_SIZE); 244 if (ret == 0) { 245 ret = ia32_set_mcontext(td, &uc.uc_mcontext); 246 if (ret == 0) { 247 kern_sigprocmask(td, SIG_SETMASK, 248 &uc.uc_sigmask, NULL, 0); 249 } 250 } 251 } 252 return (ret == 0 ? EJUSTRETURN : ret); 253 } 254 255 int 256 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap) 257 { 258 struct ia32_ucontext uc; 259 int ret; 260 261 if (uap->oucp == NULL || 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 ret = copyout(&uc, uap->oucp, UC_COPY_SIZE); 269 if (ret == 0) { 270 ret = copyin(uap->ucp, &uc, UC_COPY_SIZE); 271 if (ret == 0) { 272 ret = ia32_set_mcontext(td, &uc.uc_mcontext); 273 if (ret == 0) { 274 kern_sigprocmask(td, SIG_SETMASK, 275 &uc.uc_sigmask, NULL, 0); 276 } 277 } 278 } 279 } 280 return (ret == 0 ? EJUSTRETURN : ret); 281 } 282 283 /* 284 * Send an interrupt to process. 285 * 286 * Stack is set up to allow sigcode stored 287 * at top to call routine, followed by kcall 288 * to sigreturn routine below. After sigreturn 289 * resets the signal mask, the stack, and the 290 * frame pointer, it returns to the user 291 * specified pc, psl. 292 */ 293 #ifdef COMPAT_FREEBSD4 294 static void 295 freebsd4_ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 296 { 297 struct ia32_sigframe4 sf, *sfp; 298 struct siginfo32 siginfo; 299 struct proc *p; 300 struct thread *td; 301 struct sigacts *psp; 302 struct trapframe *regs; 303 int oonstack; 304 int sig; 305 306 td = curthread; 307 p = td->td_proc; 308 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo); 309 310 PROC_LOCK_ASSERT(p, MA_OWNED); 311 sig = siginfo.si_signo; 312 psp = p->p_sigacts; 313 mtx_assert(&psp->ps_mtx, MA_OWNED); 314 regs = td->td_frame; 315 oonstack = sigonstack(regs->tf_rsp); 316 317 /* Save user context. */ 318 bzero(&sf, sizeof(sf)); 319 sf.sf_uc.uc_sigmask = *mask; 320 sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp; 321 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size; 322 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 323 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 324 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0; 325 sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi; 326 sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi; 327 sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp; 328 sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */ 329 sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx; 330 sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx; 331 sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx; 332 sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax; 333 sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno; 334 sf.sf_uc.uc_mcontext.mc_err = regs->tf_err; 335 sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip; 336 sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs; 337 sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags; 338 sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp; 339 sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss; 340 sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds; 341 sf.sf_uc.uc_mcontext.mc_es = regs->tf_es; 342 sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs; 343 sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs; 344 345 /* Allocate space for the signal handler context. */ 346 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack && 347 SIGISMEMBER(psp->ps_sigonstack, sig)) { 348 sfp = (struct ia32_sigframe4 *)(td->td_sigstk.ss_sp + 349 td->td_sigstk.ss_size - sizeof(sf)); 350 } else 351 sfp = (struct ia32_sigframe4 *)regs->tf_rsp - 1; 352 PROC_UNLOCK(p); 353 354 /* Translate the signal if appropriate. */ 355 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize) 356 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)]; 357 358 /* Build the argument list for the signal handler. */ 359 sf.sf_signum = sig; 360 sf.sf_ucontext = (register_t)&sfp->sf_uc; 361 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 362 /* Signal handler installed with SA_SIGINFO. */ 363 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si; 364 sf.sf_ah = (u_int32_t)(uintptr_t)catcher; 365 366 /* Fill in POSIX parts */ 367 sf.sf_si = siginfo; 368 sf.sf_si.si_signo = sig; 369 } else { 370 /* Old FreeBSD-style arguments. */ 371 sf.sf_siginfo = siginfo.si_code; 372 sf.sf_addr = (u_int32_t)siginfo.si_addr; 373 sf.sf_ah = (u_int32_t)(uintptr_t)catcher; 374 } 375 mtx_unlock(&psp->ps_mtx); 376 377 /* 378 * Copy the sigframe out to the user's stack. 379 */ 380 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) { 381 #ifdef DEBUG 382 printf("process %ld has trashed its stack\n", (long)p->p_pid); 383 #endif 384 PROC_LOCK(p); 385 sigexit(td, SIGILL); 386 } 387 388 regs->tf_rsp = (uintptr_t)sfp; 389 regs->tf_rip = FREEBSD32_PS_STRINGS - sz_freebsd4_ia32_sigcode; 390 regs->tf_rflags &= ~(PSL_T | PSL_D); 391 regs->tf_cs = _ucode32sel; 392 regs->tf_ss = _udatasel; 393 regs->tf_ds = _udatasel; 394 regs->tf_es = _udatasel; 395 td->td_pcb->pcb_full_iret = 1; 396 /* leave user %fs and %gs untouched */ 397 PROC_LOCK(p); 398 mtx_lock(&psp->ps_mtx); 399 } 400 #endif /* COMPAT_FREEBSD4 */ 401 402 void 403 ia32_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 404 { 405 struct ia32_sigframe sf, *sfp; 406 struct siginfo32 siginfo; 407 struct proc *p; 408 struct thread *td; 409 struct sigacts *psp; 410 char *sp; 411 struct trapframe *regs; 412 int oonstack; 413 int sig; 414 415 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo); 416 td = curthread; 417 p = td->td_proc; 418 PROC_LOCK_ASSERT(p, MA_OWNED); 419 sig = siginfo.si_signo; 420 psp = p->p_sigacts; 421 #ifdef COMPAT_FREEBSD4 422 if (SIGISMEMBER(psp->ps_freebsd4, sig)) { 423 freebsd4_ia32_sendsig(catcher, ksi, mask); 424 return; 425 } 426 #endif 427 mtx_assert(&psp->ps_mtx, MA_OWNED); 428 regs = td->td_frame; 429 oonstack = sigonstack(regs->tf_rsp); 430 431 /* Save user context. */ 432 bzero(&sf, sizeof(sf)); 433 sf.sf_uc.uc_sigmask = *mask; 434 sf.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp; 435 sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size; 436 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 437 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 438 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0; 439 sf.sf_uc.uc_mcontext.mc_edi = regs->tf_rdi; 440 sf.sf_uc.uc_mcontext.mc_esi = regs->tf_rsi; 441 sf.sf_uc.uc_mcontext.mc_ebp = regs->tf_rbp; 442 sf.sf_uc.uc_mcontext.mc_isp = regs->tf_rsp; /* XXX */ 443 sf.sf_uc.uc_mcontext.mc_ebx = regs->tf_rbx; 444 sf.sf_uc.uc_mcontext.mc_edx = regs->tf_rdx; 445 sf.sf_uc.uc_mcontext.mc_ecx = regs->tf_rcx; 446 sf.sf_uc.uc_mcontext.mc_eax = regs->tf_rax; 447 sf.sf_uc.uc_mcontext.mc_trapno = regs->tf_trapno; 448 sf.sf_uc.uc_mcontext.mc_err = regs->tf_err; 449 sf.sf_uc.uc_mcontext.mc_eip = regs->tf_rip; 450 sf.sf_uc.uc_mcontext.mc_cs = regs->tf_cs; 451 sf.sf_uc.uc_mcontext.mc_eflags = regs->tf_rflags; 452 sf.sf_uc.uc_mcontext.mc_esp = regs->tf_rsp; 453 sf.sf_uc.uc_mcontext.mc_ss = regs->tf_ss; 454 sf.sf_uc.uc_mcontext.mc_ds = regs->tf_ds; 455 sf.sf_uc.uc_mcontext.mc_es = regs->tf_es; 456 sf.sf_uc.uc_mcontext.mc_fs = regs->tf_fs; 457 sf.sf_uc.uc_mcontext.mc_gs = regs->tf_gs; 458 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */ 459 ia32_get_fpcontext(td, &sf.sf_uc.uc_mcontext); 460 fpstate_drop(td); 461 sf.sf_uc.uc_mcontext.mc_fsbase = td->td_pcb->pcb_fsbase; 462 sf.sf_uc.uc_mcontext.mc_gsbase = td->td_pcb->pcb_gsbase; 463 464 /* Allocate space for the signal handler context. */ 465 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack && 466 SIGISMEMBER(psp->ps_sigonstack, sig)) { 467 sp = td->td_sigstk.ss_sp + 468 td->td_sigstk.ss_size - sizeof(sf); 469 } else 470 sp = (char *)regs->tf_rsp - sizeof(sf); 471 /* Align to 16 bytes. */ 472 sfp = (struct ia32_sigframe *)((uintptr_t)sp & ~0xF); 473 PROC_UNLOCK(p); 474 475 /* Translate the signal if appropriate. */ 476 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize) 477 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)]; 478 479 /* Build the argument list for the signal handler. */ 480 sf.sf_signum = sig; 481 sf.sf_ucontext = (register_t)&sfp->sf_uc; 482 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 483 /* Signal handler installed with SA_SIGINFO. */ 484 sf.sf_siginfo = (u_int32_t)(uintptr_t)&sfp->sf_si; 485 sf.sf_ah = (u_int32_t)(uintptr_t)catcher; 486 487 /* Fill in POSIX parts */ 488 sf.sf_si = siginfo; 489 sf.sf_si.si_signo = sig; 490 } else { 491 /* Old FreeBSD-style arguments. */ 492 sf.sf_siginfo = siginfo.si_code; 493 sf.sf_addr = (u_int32_t)siginfo.si_addr; 494 sf.sf_ah = (u_int32_t)(uintptr_t)catcher; 495 } 496 mtx_unlock(&psp->ps_mtx); 497 498 /* 499 * Copy the sigframe out to the user's stack. 500 */ 501 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) { 502 #ifdef DEBUG 503 printf("process %ld has trashed its stack\n", (long)p->p_pid); 504 #endif 505 PROC_LOCK(p); 506 sigexit(td, SIGILL); 507 } 508 509 regs->tf_rsp = (uintptr_t)sfp; 510 regs->tf_rip = FREEBSD32_PS_STRINGS - *(p->p_sysent->sv_szsigcode); 511 regs->tf_rflags &= ~(PSL_T | PSL_D); 512 regs->tf_cs = _ucode32sel; 513 regs->tf_ss = _udatasel; 514 regs->tf_ds = _udatasel; 515 regs->tf_es = _udatasel; 516 td->td_pcb->pcb_full_iret = 1; 517 /* XXXKIB leave user %fs and %gs untouched */ 518 PROC_LOCK(p); 519 mtx_lock(&psp->ps_mtx); 520 } 521 522 /* 523 * System call to cleanup state after a signal 524 * has been taken. Reset signal mask and 525 * stack state from context left by sendsig (above). 526 * Return to previous pc and psl as specified by 527 * context left by sendsig. Check carefully to 528 * make sure that the user has not modified the 529 * state to gain improper privileges. 530 */ 531 #ifdef COMPAT_FREEBSD4 532 /* 533 * MPSAFE 534 */ 535 int 536 freebsd4_freebsd32_sigreturn(td, uap) 537 struct thread *td; 538 struct freebsd4_freebsd32_sigreturn_args /* { 539 const struct freebsd4_freebsd32_ucontext *sigcntxp; 540 } */ *uap; 541 { 542 struct ia32_ucontext4 uc; 543 struct trapframe *regs; 544 struct ia32_ucontext4 *ucp; 545 int cs, eflags, error; 546 ksiginfo_t ksi; 547 548 error = copyin(uap->sigcntxp, &uc, sizeof(uc)); 549 if (error != 0) 550 return (error); 551 ucp = &uc; 552 regs = td->td_frame; 553 eflags = ucp->uc_mcontext.mc_eflags; 554 /* 555 * Don't allow users to change privileged or reserved flags. 556 */ 557 /* 558 * XXX do allow users to change the privileged flag PSL_RF. 559 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers 560 * should sometimes set it there too. tf_eflags is kept in 561 * the signal context during signal handling and there is no 562 * other place to remember it, so the PSL_RF bit may be 563 * corrupted by the signal handler without us knowing. 564 * Corruption of the PSL_RF bit at worst causes one more or 565 * one less debugger trap, so allowing it is fairly harmless. 566 */ 567 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) { 568 printf("freebsd4_freebsd32_sigreturn: eflags = 0x%x\n", eflags); 569 return (EINVAL); 570 } 571 572 /* 573 * Don't allow users to load a valid privileged %cs. Let the 574 * hardware check for invalid selectors, excess privilege in 575 * other selectors, invalid %eip's and invalid %esp's. 576 */ 577 cs = ucp->uc_mcontext.mc_cs; 578 if (!CS_SECURE(cs)) { 579 printf("freebsd4_sigreturn: cs = 0x%x\n", cs); 580 ksiginfo_init_trap(&ksi); 581 ksi.ksi_signo = SIGBUS; 582 ksi.ksi_code = BUS_OBJERR; 583 ksi.ksi_trapno = T_PROTFLT; 584 ksi.ksi_addr = (void *)regs->tf_rip; 585 trapsignal(td, &ksi); 586 return (EINVAL); 587 } 588 589 regs->tf_rdi = ucp->uc_mcontext.mc_edi; 590 regs->tf_rsi = ucp->uc_mcontext.mc_esi; 591 regs->tf_rbp = ucp->uc_mcontext.mc_ebp; 592 regs->tf_rbx = ucp->uc_mcontext.mc_ebx; 593 regs->tf_rdx = ucp->uc_mcontext.mc_edx; 594 regs->tf_rcx = ucp->uc_mcontext.mc_ecx; 595 regs->tf_rax = ucp->uc_mcontext.mc_eax; 596 regs->tf_trapno = ucp->uc_mcontext.mc_trapno; 597 regs->tf_err = ucp->uc_mcontext.mc_err; 598 regs->tf_rip = ucp->uc_mcontext.mc_eip; 599 regs->tf_cs = cs; 600 regs->tf_rflags = ucp->uc_mcontext.mc_eflags; 601 regs->tf_rsp = ucp->uc_mcontext.mc_esp; 602 regs->tf_ss = ucp->uc_mcontext.mc_ss; 603 regs->tf_ds = ucp->uc_mcontext.mc_ds; 604 regs->tf_es = ucp->uc_mcontext.mc_es; 605 regs->tf_fs = ucp->uc_mcontext.mc_fs; 606 regs->tf_gs = ucp->uc_mcontext.mc_gs; 607 608 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0); 609 td->td_pcb->pcb_full_iret = 1; 610 return (EJUSTRETURN); 611 } 612 #endif /* COMPAT_FREEBSD4 */ 613 614 /* 615 * MPSAFE 616 */ 617 int 618 freebsd32_sigreturn(td, uap) 619 struct thread *td; 620 struct freebsd32_sigreturn_args /* { 621 const struct freebsd32_ucontext *sigcntxp; 622 } */ *uap; 623 { 624 struct ia32_ucontext uc; 625 struct trapframe *regs; 626 struct ia32_ucontext *ucp; 627 int cs, eflags, error, ret; 628 ksiginfo_t ksi; 629 630 error = copyin(uap->sigcntxp, &uc, sizeof(uc)); 631 if (error != 0) 632 return (error); 633 ucp = &uc; 634 regs = td->td_frame; 635 eflags = ucp->uc_mcontext.mc_eflags; 636 /* 637 * Don't allow users to change privileged or reserved flags. 638 */ 639 /* 640 * XXX do allow users to change the privileged flag PSL_RF. 641 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers 642 * should sometimes set it there too. tf_eflags is kept in 643 * the signal context during signal handling and there is no 644 * other place to remember it, so the PSL_RF bit may be 645 * corrupted by the signal handler without us knowing. 646 * Corruption of the PSL_RF bit at worst causes one more or 647 * one less debugger trap, so allowing it is fairly harmless. 648 */ 649 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) { 650 printf("freebsd32_sigreturn: eflags = 0x%x\n", eflags); 651 return (EINVAL); 652 } 653 654 /* 655 * Don't allow users to load a valid privileged %cs. Let the 656 * hardware check for invalid selectors, excess privilege in 657 * other selectors, invalid %eip's and invalid %esp's. 658 */ 659 cs = ucp->uc_mcontext.mc_cs; 660 if (!CS_SECURE(cs)) { 661 printf("sigreturn: cs = 0x%x\n", cs); 662 ksiginfo_init_trap(&ksi); 663 ksi.ksi_signo = SIGBUS; 664 ksi.ksi_code = BUS_OBJERR; 665 ksi.ksi_trapno = T_PROTFLT; 666 ksi.ksi_addr = (void *)regs->tf_rip; 667 trapsignal(td, &ksi); 668 return (EINVAL); 669 } 670 671 ret = ia32_set_fpcontext(td, &ucp->uc_mcontext); 672 if (ret != 0) 673 return (ret); 674 675 regs->tf_rdi = ucp->uc_mcontext.mc_edi; 676 regs->tf_rsi = ucp->uc_mcontext.mc_esi; 677 regs->tf_rbp = ucp->uc_mcontext.mc_ebp; 678 regs->tf_rbx = ucp->uc_mcontext.mc_ebx; 679 regs->tf_rdx = ucp->uc_mcontext.mc_edx; 680 regs->tf_rcx = ucp->uc_mcontext.mc_ecx; 681 regs->tf_rax = ucp->uc_mcontext.mc_eax; 682 regs->tf_trapno = ucp->uc_mcontext.mc_trapno; 683 regs->tf_err = ucp->uc_mcontext.mc_err; 684 regs->tf_rip = ucp->uc_mcontext.mc_eip; 685 regs->tf_cs = cs; 686 regs->tf_rflags = ucp->uc_mcontext.mc_eflags; 687 regs->tf_rsp = ucp->uc_mcontext.mc_esp; 688 regs->tf_ss = ucp->uc_mcontext.mc_ss; 689 regs->tf_ds = ucp->uc_mcontext.mc_ds; 690 regs->tf_es = ucp->uc_mcontext.mc_es; 691 regs->tf_fs = ucp->uc_mcontext.mc_fs; 692 regs->tf_gs = ucp->uc_mcontext.mc_gs; 693 regs->tf_flags = TF_HASSEGS; 694 695 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0); 696 td->td_pcb->pcb_full_iret = 1; 697 return (EJUSTRETURN); 698 } 699 700 /* 701 * Clear registers on exec 702 */ 703 void 704 ia32_setregs(td, entry, stack, ps_strings) 705 struct thread *td; 706 u_long entry; 707 u_long stack; 708 u_long ps_strings; 709 { 710 struct trapframe *regs = td->td_frame; 711 struct pcb *pcb = td->td_pcb; 712 713 mtx_lock(&dt_lock); 714 if (td->td_proc->p_md.md_ldt != NULL) 715 user_ldt_free(td); 716 else 717 mtx_unlock(&dt_lock); 718 719 pcb->pcb_fsbase = 0; 720 pcb->pcb_gsbase = 0; 721 pcb->pcb_initial_fpucw = __INITIAL_FPUCW_I386__; 722 723 bzero((char *)regs, sizeof(struct trapframe)); 724 regs->tf_rip = entry; 725 regs->tf_rsp = stack; 726 regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T); 727 regs->tf_ss = _udatasel; 728 regs->tf_cs = _ucode32sel; 729 regs->tf_rbx = ps_strings; 730 regs->tf_ds = _udatasel; 731 regs->tf_es = _udatasel; 732 regs->tf_fs = _ufssel; 733 regs->tf_gs = _ugssel; 734 regs->tf_flags = TF_HASSEGS; 735 736 load_cr0(rcr0() | CR0_MP | CR0_TS); 737 fpstate_drop(td); 738 739 /* Return via doreti so that we can change to a different %cs */ 740 pcb->pcb_flags |= PCB_FULLCTX | PCB_32BIT; 741 pcb->pcb_flags &= ~PCB_GS32BIT; 742 td->td_pcb->pcb_full_iret = 1; 743 td->td_retval[1] = 0; 744 } 745