xref: /netbsd-src/sys/compat/linux/arch/i386/linux_machdep.c (revision 07bae7edddbb1ce4c926b2e8db425804589074c9)
1 /*	$NetBSD: linux_machdep.c,v 1.8 1995/05/07 03:27:37 mycroft 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/buf.h>
42 #include <sys/reboot.h>
43 #include <sys/conf.h>
44 #include <sys/file.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/msgbuf.h>
49 #include <sys/mount.h>
50 #include <sys/vnode.h>
51 #include <sys/device.h>
52 #include <sys/sysctl.h>
53 #include <sys/syscallargs.h>
54 
55 #include <compat/linux/linux_types.h>
56 #include <compat/linux/linux_syscallargs.h>
57 #include <compat/linux/linux_util.h>
58 
59 #include <machine/cpu.h>
60 #include <machine/cpufunc.h>
61 #include <machine/psl.h>
62 #include <machine/reg.h>
63 #include <machine/segments.h>
64 #include <machine/specialreg.h>
65 #include <machine/sysarch.h>
66 #include <machine/linux_machdep.h>
67 
68 /*
69  * Deal with some i386-specific things in the Linux emulation code.
70  * This means just signals for now, will include stuff like
71  * I/O map permissions and V86 mode sometime.
72  */
73 
74 /*
75  * Send an interrupt to process.
76  *
77  * Stack is set up to allow sigcode stored
78  * in u. to call routine, followed by kcall
79  * to sigreturn routine below.  After sigreturn
80  * resets the signal mask, the stack, and the
81  * frame pointer, it returns to the user
82  * specified pc, psl.
83  */
84 
85 void
86 linux_sendsig(catcher, sig, mask, code)
87 	sig_t catcher;
88 	int sig, mask;
89 	u_long code;
90 {
91 	register struct proc *p = curproc;
92 	register struct trapframe *tf;
93 	struct linux_sigframe *fp, frame;
94 	struct sigacts *psp = p->p_sigacts;
95 	int oonstack;
96 	extern char linux_sigcode[], linux_esigcode[];
97 
98 	tf = p->p_md.md_regs;
99 	oonstack = psp->ps_sigstk.ss_flags & SA_ONSTACK;
100 
101 	/*
102 	 * Allocate space for the signal handler context.
103 	 */
104 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
105 	    (psp->ps_sigonstack & sigmask(sig))) {
106 		fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_base +
107 		    psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe));
108 		psp->ps_sigstk.ss_flags |= SA_ONSTACK;
109 	} else {
110 		fp = (struct linux_sigframe *)tf->tf_esp - 1;
111 	}
112 
113 	frame.sf_handler = catcher;
114 	frame.sf_sig = bsd_to_linux_sig(sig);
115 
116 	/*
117 	 * Build the signal context to be used by sigreturn.
118 	 */
119 	frame.sf_sc.sc_mask   = mask;
120 #ifdef VM86
121 	if (tf->tf_eflags & PSL_VM) {
122 		frame.sf_sc.sc_gs = tf->tf_vm86_gs;
123 		frame.sf_sc.sc_fs = tf->tf_vm86_fs;
124 		frame.sf_sc.sc_es = tf->tf_vm86_es;
125 		frame.sf_sc.sc_ds = tf->tf_vm86_ds;
126 	} else
127 #else
128 	{
129 		__asm("movl %%gs,%w0" : "=r" (frame.sf_sc.sc_gs));
130 		__asm("movl %%fs,%w0" : "=r" (frame.sf_sc.sc_fs));
131 		frame.sf_sc.sc_es = tf->tf_es;
132 		frame.sf_sc.sc_ds = tf->tf_ds;
133 	}
134 #endif
135 	frame.sf_sc.sc_edi    = tf->tf_edi;
136 	frame.sf_sc.sc_esi    = tf->tf_esi;
137 	frame.sf_sc.sc_ebp    = tf->tf_ebp;
138 	frame.sf_sc.sc_ebx    = tf->tf_ebx;
139 	frame.sf_sc.sc_edx    = tf->tf_edx;
140 	frame.sf_sc.sc_ecx    = tf->tf_ecx;
141 	frame.sf_sc.sc_eax    = tf->tf_eax;
142 	frame.sf_sc.sc_eip    = tf->tf_eip;
143 	frame.sf_sc.sc_cs     = tf->tf_cs;
144 	frame.sf_sc.sc_eflags = tf->tf_eflags;
145 	frame.sf_sc.sc_esp_at_signal = tf->tf_esp;
146 	frame.sf_sc.sc_ss     = tf->tf_ss;
147 	frame.sf_sc.sc_err    = tf->tf_err;
148 	frame.sf_sc.sc_trapno = tf->tf_trapno;
149 
150 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
151 		/*
152 		 * Process has trashed its stack; give it an illegal
153 		 * instruction to halt it in its tracks.
154 		 */
155 		sigexit(p, SIGILL);
156 		/* NOTREACHED */
157 	}
158 
159 	/*
160 	 * Build context to run handler in.
161 	 */
162 	tf->tf_esp = (int)fp;
163 	tf->tf_eip = (int)(((char *)PS_STRINGS) -
164 	     (linux_esigcode - linux_sigcode));
165 #ifdef VM86
166 	tf->tf_eflags &= ~PSL_VM;
167 #endif
168 	tf->tf_cs = LSEL(LUCODE_SEL, SEL_UPL);
169 	tf->tf_ds = LSEL(LUDATA_SEL, SEL_UPL);
170 	tf->tf_es = LSEL(LUDATA_SEL, SEL_UPL);
171 	tf->tf_ss = LSEL(LUDATA_SEL, SEL_UPL);
172 }
173 
174 /*
175  * System call to cleanup state after a signal
176  * has been taken.  Reset signal mask and
177  * stack state from context left by sendsig (above).
178  * Return to previous pc and psl as specified by
179  * context left by sendsig. Check carefully to
180  * make sure that the user has not modified the
181  * psl to gain improper privileges or to cause
182  * a machine fault.
183  */
184 int
185 linux_sigreturn(p, uap, retval)
186 	struct proc *p;
187 	struct linux_sigreturn_args /* {
188 		syscallarg(struct linux_sigcontext *) scp;
189 	} */ *uap;
190 	register_t *retval;
191 {
192 	struct linux_sigcontext *scp, context;
193 	register struct trapframe *tf;
194 
195 	tf = p->p_md.md_regs;
196 
197 	/*
198 	 * The trampoline code hands us the context.
199 	 * It is unsafe to keep track of it ourselves, in the event that a
200 	 * program jumps out of a signal handler.
201 	 */
202 	scp = SCARG(uap, scp);
203 	if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
204 		return (EFAULT);
205 
206 	/*
207 	 * Check for security violations.
208 	 */
209 	if (((context.sc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
210 	    ISPL(context.sc_cs) != SEL_UPL)
211 		return (EINVAL);
212 
213 	p->p_sigacts->ps_sigstk.ss_flags &= ~SA_ONSTACK;
214 	p->p_sigmask = context.sc_mask & ~sigcantmask;
215 
216 	/*
217 	 * Restore signal context.
218 	 */
219 #ifdef VM86
220 	if (context.sc_eflags & PSL_VM) {
221 		tf->tf_vm86_gs = context.sc_gs;
222 		tf->tf_vm86_fs = context.sc_fs;
223 		tf->tf_vm86_es = context.sc_es;
224 		tf->tf_vm86_ds = context.sc_ds;
225 	} else
226 #endif
227 	{
228 		/* %fs and %gs were restored by the trampoline. */
229 		tf->tf_es = context.sc_es;
230 		tf->tf_ds = context.sc_ds;
231 	}
232 	tf->tf_edi    = context.sc_edi;
233 	tf->tf_esi    = context.sc_esi;
234 	tf->tf_ebp    = context.sc_ebp;
235 	tf->tf_ebx    = context.sc_ebx;
236 	tf->tf_edx    = context.sc_edx;
237 	tf->tf_ecx    = context.sc_ecx;
238 	tf->tf_eax    = context.sc_eax;
239 	tf->tf_eip    = context.sc_eip;
240 	tf->tf_cs     = context.sc_cs;
241 	tf->tf_eflags = context.sc_eflags;
242 	tf->tf_esp    = context.sc_esp_at_signal;
243 	tf->tf_ss     = context.sc_ss;
244 
245 	return (EJUSTRETURN);
246 }
247 
248 #ifdef USER_LDT
249 
250 int
251 linux_read_ldt(p, uap, retval)
252 	struct proc *p;
253 	struct linux_modify_ldt_args /* {
254 		syscallarg(int) func;
255 		syscallarg(void *) ptr;
256 		syscallarg(size_t) bytecount;
257 	} */ *uap;
258 	register_t *retval;
259 {
260 	struct i386_get_ldt_args gl;
261 	int error;
262 	caddr_t sg;
263 	char *parms;
264 
265 	sg = stackgap_init();
266 
267 	gl.start = 0;
268 	gl.desc = SCARG(uap, ptr);
269 	gl.num = SCARG(uap, bytecount) / sizeof(union descriptor);
270 
271 	parms = stackgap_alloc(&sg, sizeof(gl));
272 
273 	if (error = copyout(&gl, parms, sizeof(gl)))
274 		return (error);
275 
276 	if (error = i386_get_ldt(p, parms, retval))
277 		return (error);
278 
279 	*retval *= sizeof(union descriptor);
280 	return (0);
281 }
282 
283 struct linux_ldt_info {
284 	u_int entry_number;
285 	u_long base_addr;
286 	u_int limit;
287 	u_int seg_32bit:1;
288 	u_int contents:2;
289 	u_int read_exec_only:1;
290 	u_int limit_in_pages:1;
291 	u_int seg_not_present:1;
292 };
293 
294 int
295 linux_write_ldt(p, uap, retval)
296 	struct proc *p;
297 	struct linux_modify_ldt_args /* {
298 		syscallarg(int) func;
299 		syscallarg(void *) ptr;
300 		syscallarg(size_t) bytecount;
301 	} */ *uap;
302 	register_t *retval;
303 {
304 	struct linux_ldt_info ldt_info;
305 	struct segment_descriptor sd;
306 	struct i386_set_ldt_args sl;
307 	int error;
308 	caddr_t sg;
309 	char *parms;
310 
311 	if (SCARG(uap, bytecount) != sizeof(ldt_info))
312 		return (EINVAL);
313 	if (error = copyin(SCARG(uap, ptr), &ldt_info, sizeof(ldt_info)))
314 		return error;
315 	if (ldt_info.contents == 3)
316 		return (EINVAL);
317 
318 	sg = stackgap_init();
319 
320 	sd.sd_lobase = ldt_info.base_addr & 0xffffff;
321 	sd.sd_hibase = (ldt_info.base_addr >> 24) & 0xff;
322 	sd.sd_lolimit = ldt_info.limit & 0xffff;
323 	sd.sd_hilimit = (ldt_info.limit >> 16) & 0xf;
324 	sd.sd_type =
325 	    16 | (ldt_info.contents << 2) | (!ldt_info.read_exec_only << 1);
326 	sd.sd_dpl = SEL_UPL;
327 	sd.sd_p = !ldt_info.seg_not_present;
328 	sd.sd_def32 = ldt_info.seg_32bit;
329 	sd.sd_gran = ldt_info.limit_in_pages;
330 
331 	sl.start = ldt_info.entry_number;
332 	sl.desc = stackgap_alloc(&sg, sizeof(sd));
333 	sl.num = 1;
334 
335 #if 0
336 	printf("linux_write_ldt: idx=%d, base=%x, limit=%x\n",
337 	    ldt_info.entry_number, ldt_info.base_addr, ldt_info.limit);
338 #endif
339 
340 	parms = stackgap_alloc(&sg, sizeof(sl));
341 
342 	if (error = copyout(&sd, sl.desc, sizeof(sd)))
343 		return (error);
344 	if (error = copyout(&sl, parms, sizeof(sl)))
345 		return (error);
346 
347 	if (error = i386_set_ldt(p, parms, retval))
348 		return (error);
349 
350 	*retval = 0;
351 	return (0);
352 }
353 
354 #endif /* USER_LDT */
355 
356 int
357 linux_modify_ldt(p, uap, retval)
358 	struct proc *p;
359 	struct linux_modify_ldt_args /* {
360 		syscallarg(int) func;
361 		syscallarg(void *) ptr;
362 		syscallarg(size_t) bytecount;
363 	} */ *uap;
364 	register_t *retval;
365 {
366 
367 	switch (SCARG(uap, func)) {
368 #ifdef USER_LDT
369 	case 0:
370 		return (linux_read_ldt(p, uap, retval));
371 
372 	case 1:
373 		return (linux_write_ldt(p, uap, retval));
374 #endif /* USER_LDT */
375 
376 	default:
377 		return (ENOSYS);
378 	}
379 }
380