Home | History | Annotate | Line # | Download | only in freebsd
freebsd_machdep.c revision 1.1
      1 /*	$NetBSD: freebsd_machdep.c,v 1.1 2017/08/01 14:43:54 maxv Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: freebsd_machdep.c,v 1.1 2017/08/01 14:43:54 maxv Exp $");
     34 
     35 #if defined(_KERNEL_OPT)
     36 #include "opt_vm86.h"
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/signalvar.h>
     42 #include <sys/proc.h>
     43 #include <sys/exec.h>
     44 #include <sys/mount.h>
     45 
     46 #include <compat/sys/signal.h>
     47 
     48 #include <machine/cpufunc.h>
     49 #include <x86/fpu.h>
     50 #include <machine/reg.h>
     51 #include <machine/vm86.h>
     52 #include <machine/vmparam.h>
     53 #include <machine/freebsd_machdep.h>
     54 
     55 
     56 #include <compat/freebsd/freebsd_syscallargs.h>
     57 #include <compat/freebsd/freebsd_exec.h>
     58 #include <compat/freebsd/freebsd_signal.h>
     59 
     60 void
     61 freebsd_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
     62 {
     63 
     64 	setregs(l, epp, stack);
     65 	fpu_set_default_cw(l, __FreeBSD_NPXCW__);
     66 }
     67 
     68 /*
     69  * signal support
     70  */
     71 
     72 /*
     73  * Send an interrupt to process.
     74  *
     75  * Stack is set up to allow sigcode stored
     76  * in u. to call routine, followed by kcall
     77  * to sigreturn routine below.  After sigreturn
     78  * resets the signal mask, the stack, and the
     79  * frame pointer, it returns to the user
     80  * specified pc, psl.
     81  */
     82 void
     83 freebsd_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
     84 {
     85 	int sig = ksi->ksi_signo;
     86 	u_long code = KSI_TRAPCODE(ksi);
     87 	struct lwp *l = curlwp;
     88 	struct proc *p = l->l_proc;
     89 	int onstack, error;
     90 	struct freebsd_sigframe *fp = getframe(l, sig, &onstack), frame;
     91 	sig_t catcher = SIGACTION(p, sig).sa_handler;
     92 	struct trapframe *tf = l->l_md.md_regs;
     93 
     94 	fp--;
     95 
     96 	/* Build stack frame for signal trampoline. */
     97 	frame.sf_signum = sig;
     98 	frame.sf_code = code;
     99 	frame.sf_scp = &fp->sf_sc;
    100 	frame.sf_addr = (char *)rcr2();
    101 	frame.sf_handler = catcher;
    102 
    103 	/* Save context. */
    104 #ifdef VM86
    105 	if (tf->tf_eflags & PSL_VM) {
    106 		frame.sf_sc.sc_gs = tf->tf_vm86_gs;
    107 		frame.sf_sc.sc_fs = tf->tf_vm86_fs;
    108 		frame.sf_sc.sc_es = tf->tf_vm86_es;
    109 		frame.sf_sc.sc_ds = tf->tf_vm86_ds;
    110 		frame.sf_sc.sc_efl = get_vflags(l);
    111 		(*p->p_emul->e_syscall_intern)(p);
    112 	} else
    113 #endif
    114 	{
    115 		frame.sf_sc.sc_gs = tf->tf_gs;
    116 		frame.sf_sc.sc_fs = tf->tf_fs;
    117 		frame.sf_sc.sc_es = tf->tf_es;
    118 		frame.sf_sc.sc_ds = tf->tf_ds;
    119 		frame.sf_sc.sc_efl = tf->tf_eflags;
    120 	}
    121 	frame.sf_sc.sc_edi = tf->tf_edi;
    122 	frame.sf_sc.sc_esi = tf->tf_esi;
    123 	frame.sf_sc.sc_ebp = tf->tf_ebp;
    124 	frame.sf_sc.sc_isp = 0; /* don't have to pass kernel sp to user. */
    125 	frame.sf_sc.sc_ebx = tf->tf_ebx;
    126 	frame.sf_sc.sc_edx = tf->tf_edx;
    127 	frame.sf_sc.sc_ecx = tf->tf_ecx;
    128 	frame.sf_sc.sc_eax = tf->tf_eax;
    129 	frame.sf_sc.sc_eip = tf->tf_eip;
    130 	frame.sf_sc.sc_cs = tf->tf_cs;
    131 	frame.sf_sc.sc_esp = tf->tf_esp;
    132 	frame.sf_sc.sc_ss = tf->tf_ss;
    133 
    134 	/* Save signal stack. */
    135 	frame.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
    136 
    137 	/* Save signal mask. */
    138 	/* XXX freebsd_osigcontext compat? */
    139 	frame.sf_sc.sc_mask = *mask;
    140 
    141 	sendsig_reset(l, sig);
    142 
    143 	mutex_exit(p->p_lock);
    144 	error = copyout(&frame, fp, sizeof(frame));
    145 	mutex_enter(p->p_lock);
    146 
    147 	if (error != 0) {
    148 		/*
    149 		 * Process has trashed its stack; give it an illegal
    150 		 * instruction to halt it in its tracks.
    151 		 */
    152 		sigexit(l, SIGILL);
    153 		/* NOTREACHED */
    154 	}
    155 
    156 	buildcontext(l, GUCODEBIG_SEL, p->p_sigctx.ps_sigcode, fp);
    157 
    158 	/* Remember that we're now on the signal stack. */
    159 	if (onstack)
    160 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    161 }
    162 
    163 /*
    164  * System call to cleanup state after a signal
    165  * has been taken.  Reset signal mask and
    166  * stack state from context left by sendsig (above).
    167  * Return to previous pc and psl as specified by
    168  * context left by sendsig. Check carefully to
    169  * make sure that the user has not modified the
    170  * psl to gain improper privileges or to cause
    171  * a machine fault.
    172  */
    173 int
    174 freebsd_sys_sigreturn(struct lwp *l, const struct freebsd_sys_sigreturn_args *uap, register_t *retval)
    175 {
    176 	/* {
    177 		syscallarg(struct freebsd_sigcontext *) scp;
    178 	} */
    179 	struct proc *p = l->l_proc;
    180 	struct freebsd_sigcontext *scp, context;
    181 	struct trapframe *tf;
    182 	sigset_t mask;
    183 
    184 	/*
    185 	 * The trampoline code hands us the context.
    186 	 * It is unsafe to keep track of it ourselves, in the event that a
    187 	 * program jumps out of a signal handler.
    188 	 */
    189 	scp = SCARG(uap, scp);
    190 	if (copyin((void *)scp, &context, sizeof(*scp)) != 0)
    191 		return (EFAULT);
    192 
    193 	/* Restore register context. */
    194 	tf = l->l_md.md_regs;
    195 #ifdef VM86
    196 	if (context.sc_efl & PSL_VM) {
    197 		void syscall_vm86(struct trapframe *);
    198 
    199 		tf->tf_vm86_gs = context.sc_gs;
    200 		tf->tf_vm86_fs = context.sc_fs;
    201 		tf->tf_vm86_es = context.sc_es;
    202 		tf->tf_vm86_ds = context.sc_ds;
    203 		set_vflags(l, context.sc_efl);
    204 		p->p_md.md_syscall = syscall_vm86;
    205 	} else
    206 #endif
    207 	{
    208 		/*
    209 		 * Check for security violations.  If we're returning to
    210 		 * protected mode, the CPU will validate the segment registers
    211 		 * automatically and generate a trap on violations.  We handle
    212 		 * the trap, rather than doing all of the checking here.
    213 		 */
    214 		if (((context.sc_efl ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
    215 		    !USERMODE(context.sc_cs, context.sc_efl))
    216 			return (EINVAL);
    217 
    218 		tf->tf_gs = context.sc_gs;
    219 		tf->tf_fs = context.sc_fs;
    220 		tf->tf_es = context.sc_es;
    221 		tf->tf_ds = context.sc_ds;
    222 		tf->tf_eflags &= ~PSL_USER;
    223 		tf->tf_eflags |= context.sc_efl & PSL_USER;
    224 	}
    225 	tf->tf_edi = context.sc_edi;
    226 	tf->tf_esi = context.sc_esi;
    227 	tf->tf_ebp = context.sc_ebp;
    228 	/* FreeBSD's context.sc_isp is useless. (`popal' ignores it.) */
    229 	tf->tf_ebx = context.sc_ebx;
    230 	tf->tf_edx = context.sc_edx;
    231 	tf->tf_ecx = context.sc_ecx;
    232 	tf->tf_eax = context.sc_eax;
    233 	tf->tf_eip = context.sc_eip;
    234 	tf->tf_cs = context.sc_cs;
    235 	tf->tf_esp = context.sc_esp;
    236 	tf->tf_ss = context.sc_ss;
    237 
    238 	mutex_enter(p->p_lock);
    239 	/* Restore signal stack. */
    240 	if (context.sc_onstack & SS_ONSTACK)
    241 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    242 	else
    243 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
    244 	/* Restore signal mask. */
    245 	/* XXX freebsd_osigcontext compat? */
    246 	mask = context.sc_mask;
    247 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
    248 	mutex_exit(p->p_lock);
    249 
    250 	return (EJUSTRETURN);
    251 }
    252 
    253