linux_machdep.c revision 1.2 1 /* $NetBSD: linux_machdep.c,v 1.2 1995/04/22 20:26:25 christos Exp $ */
2
3 /*
4 * Copyright (c) 1995 Frank van der Linden
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project
18 * by Frank van der Linden
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/signalvar.h>
37 #include <sys/kernel.h>
38 #include <sys/map.h>
39 #include <sys/proc.h>
40 #include <sys/user.h>
41 #include <sys/exec.h>
42 #include <sys/buf.h>
43 #include <sys/reboot.h>
44 #include <sys/conf.h>
45 #include <sys/file.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/msgbuf.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/device.h>
53 #include <sys/sysctl.h>
54 #include <sys/syscallargs.h>
55 #include <compat/linux/linux_types.h>
56 #include <compat/linux/linux_syscallargs.h>
57
58 #include <machine/cpu.h>
59 #include <machine/cpufunc.h>
60 #include <machine/psl.h>
61 #include <machine/reg.h>
62 #include <machine/specialreg.h>
63 #include <machine/linux_machdep.h>
64
65 /*
66 * Deal with some i386-specific things in the Linux emulation code.
67 * This means just signals for now, will include stuff like
68 * I/O map permissions and V86 mode sometime.
69 */
70
71 /*
72 * Send an interrupt to process.
73 *
74 * Stack is set up to allow sigcode stored
75 * in u. to call routine, followed by kcall
76 * to sigreturn routine below. After sigreturn
77 * resets the signal mask, the stack, and the
78 * frame pointer, it returns to the user
79 * specified pc, psl.
80 */
81
82 void
83 linux_sendsig(catcher, sig, mask, code)
84 sig_t catcher;
85 int sig, mask;
86 u_long code;
87 {
88 register struct proc *p = curproc;
89 register struct trapframe *tf;
90 struct linux_sigframe *fp, frame;
91 struct sigacts *psp = p->p_sigacts;
92 int oonstack;
93 extern char linux_sigcode[], linux_esigcode[];
94
95 tf = (struct trapframe *)p->p_md.md_regs;
96 oonstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
97
98 /*
99 * Allocate space for the signal handler context.
100 */
101 if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
102 (psp->ps_sigonstack & sigmask(sig))) {
103 fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_base +
104 psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe));
105 psp->ps_sigstk.ss_flags |= SA_ONSTACK;
106 } else {
107 fp = (struct linux_sigframe *)tf->tf_esp - 1;
108 }
109
110 frame.ls_handler = catcher;
111 frame.ls_sig = bsd_to_linux_sig(sig);
112
113 /*
114 * Build the signal context to be used by sigreturn.
115 */
116 frame.ls_sc.lsc_mask = mask;
117 frame.ls_sc.lsc_es = tf->tf_es;
118 frame.ls_sc.lsc_fs = LSEL(LUDATA_SEL, SEL_UPL);
119 frame.ls_sc.lsc_gs = LSEL(LUDATA_SEL, SEL_UPL);
120 frame.ls_sc.lsc_ds = tf->tf_ds;
121 frame.ls_sc.lsc_edi = tf->tf_edi;
122 frame.ls_sc.lsc_esi = tf->tf_esi;
123 frame.ls_sc.lsc_ebp = tf->tf_ebp;
124 frame.ls_sc.lsc_ebx = tf->tf_ebx;
125 frame.ls_sc.lsc_edx = tf->tf_edx;
126 frame.ls_sc.lsc_ecx = tf->tf_ecx;
127 frame.ls_sc.lsc_eax = tf->tf_eax;
128 frame.ls_sc.lsc_eip = tf->tf_eip;
129 frame.ls_sc.lsc_cs = tf->tf_cs;
130 frame.ls_sc.lsc_eflags = tf->tf_eflags;
131 frame.ls_sc.lsc_esp = tf->tf_esp;
132 frame.ls_sc.lsc_ss = tf->tf_ss;
133 frame.ls_sc.lsc_err = tf->tf_err;
134 frame.ls_sc.lsc_trapno = tf->tf_trapno;
135
136 if (copyout(&frame, fp, sizeof(frame)) != 0) {
137 /*
138 * Process has trashed its stack; give it an illegal
139 * instruction to halt it in its tracks.
140 */
141 sigexit(p, SIGILL);
142 /* NOTREACHED */
143 }
144
145 /*
146 * Build context to run handler in.
147 */
148 tf->tf_esp = (int)fp;
149 tf->tf_eip = (int)(((char *)PS_STRINGS) -
150 (linux_esigcode - linux_sigcode));
151 tf->tf_eflags &= ~PSL_VM;
152 tf->tf_cs = LSEL(LUCODE_SEL, SEL_UPL);
153 tf->tf_ds = LSEL(LUDATA_SEL, SEL_UPL);
154
155 tf->tf_es = LSEL(LUDATA_SEL, SEL_UPL);
156 tf->tf_ss = LSEL(LUDATA_SEL, SEL_UPL);
157 }
158
159 /*
160 * System call to cleanup state after a signal
161 * has been taken. Reset signal mask and
162 * stack state from context left by sendsig (above).
163 * Return to previous pc and psl as specified by
164 * context left by sendsig. Check carefully to
165 * make sure that the user has not modified the
166 * psl to gain improper privileges or to cause
167 * a machine fault.
168 */
169 int
170 linux_sigreturn(p, uap, retval)
171 struct proc *p;
172 struct linux_sigreturn_args /* {
173 syscallarg(struct linux_sigcontext *) scp;
174 } */ *uap;
175 register_t *retval;
176 {
177 struct linux_sigcontext *scp, context;
178 register struct trapframe *tf;
179
180 tf = (struct trapframe *)p->p_md.md_regs;
181
182 /*
183 * The trampoline code hands us the context.
184 * It is unsafe to keep track of it ourselves, in the event that a
185 * program jumps out of a signal handler.
186 */
187 scp = SCARG(uap, scp);
188 if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
189 return (EFAULT);
190
191 /*
192 * Check for security violations.
193 */
194 if (((context.lsc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
195 ISPL(context.lsc_cs) != SEL_UPL)
196 return (EINVAL);
197
198 p->p_sigacts->ps_sigstk.ss_flags &= ~SA_ONSTACK;
199 p->p_sigmask = context.lsc_mask & ~sigcantmask;
200
201 /*
202 * Restore signal context.
203 */
204 tf->tf_es = context.lsc_es;
205 tf->tf_ds = context.lsc_ds;
206 tf->tf_edi = context.lsc_edi;
207 tf->tf_esi = context.lsc_esi;
208 tf->tf_ebp = context.lsc_ebp;
209 tf->tf_ebx = context.lsc_ebx;
210 tf->tf_edx = context.lsc_edx;
211 tf->tf_ecx = context.lsc_ecx;
212 tf->tf_eax = context.lsc_eax;
213 tf->tf_eip = context.lsc_eip;
214 tf->tf_cs = context.lsc_cs;
215 tf->tf_eflags = context.lsc_eflags;
216 tf->tf_esp = context.lsc_esp;
217 tf->tf_ss = context.lsc_ss;
218
219 return (EJUSTRETURN);
220 }
221