xref: /netbsd/sys/arch/i386/i386/compat_13_machdep.c (revision bf9ec67e)
1 /*	$NetBSD: compat_13_machdep.c,v 1.7 2001/11/15 07:03:29 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997, 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: compat_13_machdep.c,v 1.7 2001/11/15 07:03:29 lukem Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/signalvar.h>
45 #include <sys/kernel.h>
46 #include <sys/map.h>
47 #include <sys/proc.h>
48 #include <sys/user.h>
49 #include <sys/mount.h>
50 #include <sys/syscallargs.h>
51 
52 #ifdef VM86
53 #include <machine/vm86.h>
54 #endif
55 
56 int
57 compat_13_sys_sigreturn(p, v, retval)
58 	struct proc *p;
59 	void *v;
60 	register_t *retval;
61 {
62 	struct compat_13_sys_sigreturn_args /* {
63 		syscallarg(struct sigcontext13 *) sigcntxp;
64 	} */ *uap = v;
65 	struct sigcontext13 *scp, context;
66 	struct trapframe *tf;
67 	sigset_t mask;
68 
69 	/*
70 	 * The trampoline code hands us the context.
71 	 * It is unsafe to keep track of it ourselves, in the event that a
72 	 * program jumps out of a signal handler.
73 	 */
74 	scp = SCARG(uap, sigcntxp);
75 	if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
76 		return (EFAULT);
77 
78 	/* Restore register context. */
79 	tf = p->p_md.md_regs;
80 #ifdef VM86
81 	if (context.sc_eflags & PSL_VM) {
82 		void syscall_vm86 __P((struct trapframe));
83 
84 		tf->tf_vm86_gs = context.sc_gs;
85 		tf->tf_vm86_fs = context.sc_fs;
86 		tf->tf_vm86_es = context.sc_es;
87 		tf->tf_vm86_ds = context.sc_ds;
88 		set_vflags(p, context.sc_eflags);
89 		p->p_md.md_syscall = syscall_vm86;
90 	} else
91 #endif
92 	{
93 		/*
94 		 * Check for security violations.  If we're returning to
95 		 * protected mode, the CPU will validate the segment registers
96 		 * automatically and generate a trap on violations.  We handle
97 		 * the trap, rather than doing all of the checking here.
98 		 */
99 		if (((context.sc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
100 		    !USERMODE(context.sc_cs, context.sc_eflags))
101 			return (EINVAL);
102 
103 		tf->tf_gs = context.sc_gs;
104 		tf->tf_fs = context.sc_fs;
105 		tf->tf_es = context.sc_es;
106 		tf->tf_ds = context.sc_ds;
107 		tf->tf_eflags = context.sc_eflags;
108 	}
109 	tf->tf_edi = context.sc_edi;
110 	tf->tf_esi = context.sc_esi;
111 	tf->tf_ebp = context.sc_ebp;
112 	tf->tf_ebx = context.sc_ebx;
113 	tf->tf_edx = context.sc_edx;
114 	tf->tf_ecx = context.sc_ecx;
115 	tf->tf_eax = context.sc_eax;
116 	tf->tf_eip = context.sc_eip;
117 	tf->tf_cs = context.sc_cs;
118 	tf->tf_esp = context.sc_esp;
119 	tf->tf_ss = context.sc_ss;
120 
121 	/* Restore signal stack. */
122 	if (context.sc_onstack & SS_ONSTACK)
123 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
124 	else
125 		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
126 
127 	/* Restore signal mask. */
128 	native_sigset13_to_sigset(&context.sc_mask, &mask);
129 	(void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
130 
131 	return (EJUSTRETURN);
132 }
133