xref: /freebsd/sys/i386/linux/linux_sysvec.c (revision a654d072959982a88cfbd609921641d85a8eb50c)
1 /*-
2  * Copyright (c) 1994-1996 S�ren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software withough specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *  $Id: linux_sysvec.c,v 1.36 1998/10/11 21:08:02 alex Exp $
29  */
30 
31 /* XXX we use functions that might not exist. */
32 #include "opt_compat.h"
33 
34 #ifndef COMPAT_43
35 #error "Unable to compile Linux-emulator due to missing COMPAT_43 option!"
36 #endif
37 
38 #include <sys/param.h>
39 #include <sys/buf.h>
40 #include <sys/proc.h>
41 #include <sys/systm.h>
42 #include <sys/sysent.h>
43 #include <sys/imgact.h>
44 #include <sys/imgact_aout.h>
45 #include <sys/imgact_elf.h>
46 #include <sys/signalvar.h>
47 #include <sys/malloc.h>
48 #include <vm/vm.h>
49 #include <vm/vm_param.h>
50 #include <vm/vm_prot.h>
51 #include <vm/vm_page.h>
52 #include <vm/vm_extern.h>
53 #include <sys/exec.h>
54 #include <sys/kernel.h>
55 #include <sys/module.h>
56 #include <machine/cpu.h>
57 
58 #include <i386/linux/linux.h>
59 #include <i386/linux/linux_proto.h>
60 
61 static int	linux_fixup __P((long **stack_base,
62 				 struct image_params *iparams));
63 static int	elf_linux_fixup __P((long **stack_base,
64 				     struct image_params *iparams));
65 static void	linux_prepsyscall __P((struct trapframe *tf, int *args,
66 				       u_int *code, caddr_t *params));
67 static void     linux_sendsig __P((sig_t catcher, int sig, int mask,
68 				   u_long code));
69 
70 /*
71  * Linux syscalls return negative errno's, we do positive and map them
72  */
73 static int bsd_to_linux_errno[ELAST] = {
74   	-0,  -1,  -2,  -3,  -4,  -5,  -6,  -7,  -8,  -9,
75  	-10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
76  	-20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
77  	-30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
78  	-90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
79 	-100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
80 	-110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
81 	-116, -66,  -6,  -6,  -6,  -6,  -6, -37, -38,  -9,
82   	-6, -43, -42
83 };
84 
85 int bsd_to_linux_signal[NSIG] = {
86 	0, LINUX_SIGHUP, LINUX_SIGINT, LINUX_SIGQUIT,
87 	LINUX_SIGILL, LINUX_SIGTRAP, LINUX_SIGABRT, 0,
88 	LINUX_SIGFPE, LINUX_SIGKILL, LINUX_SIGBUS, LINUX_SIGSEGV,
89 	0, LINUX_SIGPIPE, LINUX_SIGALRM, LINUX_SIGTERM,
90 	LINUX_SIGURG, LINUX_SIGSTOP, LINUX_SIGTSTP, LINUX_SIGCONT,
91 	LINUX_SIGCHLD, LINUX_SIGTTIN, LINUX_SIGTTOU, LINUX_SIGIO,
92 	LINUX_SIGXCPU, LINUX_SIGXFSZ, LINUX_SIGVTALRM, LINUX_SIGPROF,
93 	LINUX_SIGWINCH, 0, LINUX_SIGUSR1, LINUX_SIGUSR2
94 };
95 
96 int linux_to_bsd_signal[LINUX_NSIG] = {
97 	0, SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGBUS,
98 	SIGFPE, SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2, SIGPIPE, SIGALRM, SIGTERM,
99 	SIGBUS, SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGURG,
100 	SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, SIGWINCH, SIGIO, SIGURG, 0
101 };
102 
103 /*
104  * If FreeBSD & Linux have a difference of opinion about what a trap
105  * means, deal with it here.
106  */
107 static int
108 translate_traps(int signal, int trap_code)
109 {
110 	if (signal != SIGBUS)
111 		return signal;
112 	switch (trap_code) {
113 	case T_PROTFLT:
114 	case T_TSSFLT:
115 	case T_DOUBLEFLT:
116 	case T_PAGEFLT:
117 		return SIGSEGV;
118 	default:
119 		return signal;
120 	}
121 }
122 
123 static int
124 linux_fixup(long **stack_base, struct image_params *imgp)
125 {
126 	long *argv, *envp;
127 
128 	argv = *stack_base;
129 	envp = *stack_base + (imgp->argc + 1);
130 	(*stack_base)--;
131 	**stack_base = (intptr_t)(void *)envp;
132 	(*stack_base)--;
133 	**stack_base = (intptr_t)(void *)argv;
134 	(*stack_base)--;
135 	**stack_base = imgp->argc;
136 	return 0;
137 }
138 
139 static int
140 elf_linux_fixup(long **stack_base, struct image_params *imgp)
141 {
142 	Elf32_Auxargs *args = (Elf32_Auxargs *)imgp->auxargs;
143 	long *pos;
144 
145 	pos = *stack_base + (imgp->argc + imgp->envc + 2);
146 
147 	if (args->trace) {
148 		AUXARGS_ENTRY(pos, AT_DEBUG, 1);
149 	}
150 	if (args->execfd != -1) {
151 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
152 	}
153 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
154 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
155 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
156 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
157 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
158 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
159 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
160 	AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_cred->p_ruid);
161 	AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_cred->p_svuid);
162 	AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_cred->p_rgid);
163 	AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_cred->p_svgid);
164 	AUXARGS_ENTRY(pos, AT_NULL, 0);
165 
166 	free(imgp->auxargs, M_TEMP);
167 	imgp->auxargs = NULL;
168 
169 	(*stack_base)--;
170 	**stack_base = (long)imgp->argc;
171 	return 0;
172 }
173 
174 extern int _ucodesel, _udatasel;
175 
176 /*
177  * Send an interrupt to process.
178  *
179  * Stack is set up to allow sigcode stored
180  * in u. to call routine, followed by kcall
181  * to sigreturn routine below.  After sigreturn
182  * resets the signal mask, the stack, and the
183  * frame pointer, it returns to the user
184  * specified pc, psl.
185  */
186 
187 static void
188 linux_sendsig(sig_t catcher, int sig, int mask, u_long code)
189 {
190 	register struct proc *p = curproc;
191 	register struct trapframe *regs;
192 	struct linux_sigframe *fp, frame;
193 	struct sigacts *psp = p->p_sigacts;
194 	int oonstack;
195 
196 	regs = p->p_md.md_regs;
197 	oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
198 
199 #ifdef DEBUG
200 	printf("Linux-emul(%ld): linux_sendsig(%p, %d, %d, %lu)\n",
201 	    (long)p->p_pid, catcher, sig, mask, code);
202 #endif
203 	/*
204 	 * Allocate space for the signal handler context.
205 	 */
206 	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
207 	    (psp->ps_sigonstack & sigmask(sig))) {
208 		fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_sp +
209 		    psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe));
210 		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
211 	} else {
212 		fp = (struct linux_sigframe *)regs->tf_esp - 1;
213 	}
214 
215 	/*
216 	 * grow() will return FALSE if the fp will not fit inside the stack
217 	 *	and the stack can not be grown. useracc will return FALSE
218 	 *	if access is denied.
219 	 */
220 	if ((grow(p, (int)fp) == FALSE) ||
221 	    (useracc((caddr_t)fp, sizeof (struct linux_sigframe), B_WRITE) == FALSE)) {
222 		/*
223 		 * Process has trashed its stack; give it an illegal
224 		 * instruction to halt it in its tracks.
225 		 */
226 		SIGACTION(p, SIGILL) = SIG_DFL;
227 		sig = sigmask(SIGILL);
228 		p->p_sigignore &= ~sig;
229 		p->p_sigcatch &= ~sig;
230 		p->p_sigmask &= ~sig;
231 		psignal(p, SIGILL);
232 		return;
233 	}
234 
235 	/*
236 	 * Build the argument list for the signal handler.
237 	 */
238 	if (p->p_sysent->sv_sigtbl) {
239 		if (sig < p->p_sysent->sv_sigsize)
240 			sig = p->p_sysent->sv_sigtbl[sig];
241 		else
242 			sig = p->p_sysent->sv_sigsize + 1;
243 	}
244 
245 	frame.sf_handler = catcher;
246 	frame.sf_sig = sig;
247 
248 	/*
249 	 * Build the signal context to be used by sigreturn.
250 	 */
251 	frame.sf_sc.sc_mask   = mask;
252 	__asm("movl %%gs,%w0" : "=r" (frame.sf_sc.sc_gs));
253 	__asm("movl %%fs,%w0" : "=r" (frame.sf_sc.sc_fs));
254 	frame.sf_sc.sc_es     = regs->tf_es;
255 	frame.sf_sc.sc_ds     = regs->tf_ds;
256 	frame.sf_sc.sc_edi    = regs->tf_edi;
257 	frame.sf_sc.sc_esi    = regs->tf_esi;
258 	frame.sf_sc.sc_ebp    = regs->tf_ebp;
259 	frame.sf_sc.sc_ebx    = regs->tf_ebx;
260 	frame.sf_sc.sc_edx    = regs->tf_edx;
261 	frame.sf_sc.sc_ecx    = regs->tf_ecx;
262 	frame.sf_sc.sc_eax    = regs->tf_eax;
263 	frame.sf_sc.sc_eip    = regs->tf_eip;
264 	frame.sf_sc.sc_cs     = regs->tf_cs;
265 	frame.sf_sc.sc_eflags = regs->tf_eflags;
266 	frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
267 	frame.sf_sc.sc_ss     = regs->tf_ss;
268 	frame.sf_sc.sc_err    = regs->tf_err;
269 	frame.sf_sc.sc_trapno = code;	/* XXX ???? */
270 
271 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
272 		/*
273 		 * Process has trashed its stack; give it an illegal
274 		 * instruction to halt it in its tracks.
275 		 */
276 		sigexit(p, SIGILL);
277 		/* NOTREACHED */
278 	}
279 
280 	/*
281 	 * Build context to run handler in.
282 	 */
283 	regs->tf_esp = (int)fp;
284 	regs->tf_eip = (int)(((char *)PS_STRINGS) - *(p->p_sysent->sv_szsigcode));
285 	regs->tf_eflags &= ~PSL_VM;
286 	regs->tf_cs = _ucodesel;
287 	regs->tf_ds = _udatasel;
288 	regs->tf_es = _udatasel;
289 	regs->tf_ss = _udatasel;
290 }
291 
292 /*
293  * System call to cleanup state after a signal
294  * has been taken.  Reset signal mask and
295  * stack state from context left by sendsig (above).
296  * Return to previous pc and psl as specified by
297  * context left by sendsig. Check carefully to
298  * make sure that the user has not modified the
299  * psl to gain improper privileges or to cause
300  * a machine fault.
301  */
302 int
303 linux_sigreturn(p, args)
304 	struct proc *p;
305 	struct linux_sigreturn_args *args;
306 {
307 	struct linux_sigcontext *scp, context;
308 	register struct trapframe *regs;
309 	int eflags;
310 
311 	regs = p->p_md.md_regs;
312 
313 #ifdef DEBUG
314 	printf("Linux-emul(%ld): linux_sigreturn(%p)\n",
315 	    (long)p->p_pid, (void *)args->scp);
316 #endif
317 	/*
318 	 * The trampoline code hands us the context.
319 	 * It is unsafe to keep track of it ourselves, in the event that a
320 	 * program jumps out of a signal handler.
321 	 */
322 	scp = SCARG(args,scp);
323 	if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
324 		return (EFAULT);
325 
326 	/*
327 	 * Check for security violations.
328 	 */
329 #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
330 	eflags = context.sc_eflags;
331 	/*
332 	 * XXX do allow users to change the privileged flag PSL_RF.  The
333 	 * cpu sets PSL_RF in tf_eflags for faults.  Debuggers should
334 	 * sometimes set it there too.  tf_eflags is kept in the signal
335 	 * context during signal handling and there is no other place
336 	 * to remember it, so the PSL_RF bit may be corrupted by the
337 	 * signal handler without us knowing.  Corruption of the PSL_RF
338 	 * bit at worst causes one more or one less debugger trap, so
339 	 * allowing it is fairly harmless.
340 	 */
341 	if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
342     		return(EINVAL);
343 	}
344 
345 	/*
346 	 * Don't allow users to load a valid privileged %cs.  Let the
347 	 * hardware check for invalid selectors, excess privilege in
348 	 * other selectors, invalid %eip's and invalid %esp's.
349 	 */
350 #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
351 	if (!CS_SECURE(context.sc_cs)) {
352 		trapsignal(p, SIGBUS, T_PROTFLT);
353 		return(EINVAL);
354 	}
355 
356 	p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
357 	p->p_sigmask = context.sc_mask &~
358 		(sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP));
359 	/*
360 	 * Restore signal context.
361 	 */
362 	/* %fs and %gs were restored by the trampoline. */
363 	regs->tf_es     = context.sc_es;
364 	regs->tf_ds     = context.sc_ds;
365 	regs->tf_edi    = context.sc_edi;
366 	regs->tf_esi    = context.sc_esi;
367 	regs->tf_ebp    = context.sc_ebp;
368 	regs->tf_ebx    = context.sc_ebx;
369 	regs->tf_edx    = context.sc_edx;
370 	regs->tf_ecx    = context.sc_ecx;
371 	regs->tf_eax    = context.sc_eax;
372 	regs->tf_eip    = context.sc_eip;
373 	regs->tf_cs     = context.sc_cs;
374 	regs->tf_eflags = eflags;
375 	regs->tf_esp    = context.sc_esp_at_signal;
376 	regs->tf_ss     = context.sc_ss;
377 
378 	return (EJUSTRETURN);
379 }
380 
381 static void
382 linux_prepsyscall(struct trapframe *tf, int *args, u_int *code, caddr_t *params)
383 {
384 	args[0] = tf->tf_ebx;
385 	args[1] = tf->tf_ecx;
386 	args[2] = tf->tf_edx;
387 	args[3] = tf->tf_esi;
388 	args[4] = tf->tf_edi;
389 	*params = NULL;		/* no copyin */
390 }
391 
392 struct sysentvec linux_sysvec = {
393 	LINUX_SYS_MAXSYSCALL,
394 	linux_sysent,
395 	0xff,
396 	NSIG,
397 	bsd_to_linux_signal,
398 	ELAST,
399 	bsd_to_linux_errno,
400 	translate_traps,
401 	linux_fixup,
402 	linux_sendsig,
403 	linux_sigcode,
404 	&linux_szsigcode,
405 	linux_prepsyscall,
406 	"Linux a.out",
407 	aout_coredump
408 };
409 
410 struct sysentvec elf_linux_sysvec = {
411         LINUX_SYS_MAXSYSCALL,
412         linux_sysent,
413         0xff,
414         NSIG,
415         bsd_to_linux_signal,
416         ELAST,
417         bsd_to_linux_errno,
418         translate_traps,
419         elf_linux_fixup,
420         linux_sendsig,
421         linux_sigcode,
422         &linux_szsigcode,
423         linux_prepsyscall,
424 	"Linux ELF",
425 	elf_coredump
426 };
427 
428 /*
429  * Installed either via SYSINIT() or via LKM stubs.
430  */
431 static Elf32_Brandinfo linux_brand = {
432 					"Linux",
433 					"/compat/linux",
434 					"/lib/ld-linux.so.1",
435 					&elf_linux_sysvec
436 				 };
437 
438 static Elf32_Brandinfo linux_glibc2brand = {
439 					"Linux",
440 					"/compat/linux",
441 					"/lib/ld-linux.so.2",
442 					&elf_linux_sysvec
443 				 };
444 
445 Elf32_Brandinfo *linux_brandlist[] = {
446 					&linux_brand,
447 					&linux_glibc2brand,
448 					NULL
449 				};
450 
451 /*
452  * XXX: this is WRONG, it needs to be SI_SUB_EXEC, but this is just at the
453  * "proof of concept" stage and will be fixed shortly
454  */
455 static int linux_elf_modevent __P((module_t mod, modeventtype_t type, void *data));
456 
457 static int
458 linux_elf_modevent(module_t mod, modeventtype_t type, void *data)
459 {
460 	Elf32_Brandinfo **brandinfo;
461 	int error;
462 
463 	error = 0;
464 
465 	switch(type) {
466 	case MOD_LOAD:
467 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
468 		    ++brandinfo)
469 			if (elf_insert_brand_entry(*brandinfo) < 0)
470 				error = EINVAL;
471 		if (error)
472 			printf("cannot insert Linux elf brand handler\n");
473 		else if (bootverbose)
474 			printf("Linux-ELF exec handler installed\n");
475 		break;
476 	case MOD_UNLOAD:
477 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
478 		    ++brandinfo)
479 			if (elf_remove_brand_entry(*brandinfo) < 0)
480 				error = EINVAL;
481 		if (error)
482 			printf("Could not deinstall ELF interpreter entry\n");
483 		else if (bootverbose)
484 			printf("Linux-elf exec handler removed\n");
485 		break;
486 	default:
487 		break;
488 	}
489 	return error;
490 }
491 static moduledata_t linux_elf_mod = {
492 	"linuxelf",
493 	linux_elf_modevent,
494 	0
495 };
496 DECLARE_MODULE(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
497