linux_sysvec.c revision 48303
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.47 1999/04/28 01:04:20 luoqi 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/systm.h>
40#include <sys/buf.h>
41#include <sys/proc.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
61static int	linux_fixup __P((long **stack_base,
62				 struct image_params *iparams));
63static int	elf_linux_fixup __P((long **stack_base,
64				     struct image_params *iparams));
65static void	linux_prepsyscall __P((struct trapframe *tf, int *args,
66				       u_int *code, caddr_t *params));
67static 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 */
73static int bsd_to_linux_errno[ELAST + 1] = {
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, -6, -43, -42, -75, -6, -84
83};
84
85int 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
96int 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 */
107static int
108translate_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
123static int
124linux_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
139static int
140elf_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
174extern 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
187static void
188linux_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_stack (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	frame.sf_sc.sc_gs     = rgs();
253	frame.sf_sc.sc_fs     = regs->tf_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 = 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_fs = _udatasel;
290	regs->tf_ss = _udatasel;
291}
292
293/*
294 * System call to cleanup state after a signal
295 * has been taken.  Reset signal mask and
296 * stack state from context left by sendsig (above).
297 * Return to previous pc and psl as specified by
298 * context left by sendsig. Check carefully to
299 * make sure that the user has not modified the
300 * psl to gain improper privileges or to cause
301 * a machine fault.
302 */
303int
304linux_sigreturn(p, args)
305	struct proc *p;
306	struct linux_sigreturn_args *args;
307{
308	struct linux_sigcontext *scp, context;
309	register struct trapframe *regs;
310	int eflags;
311
312	regs = p->p_md.md_regs;
313
314#ifdef DEBUG
315	printf("Linux-emul(%ld): linux_sigreturn(%p)\n",
316	    (long)p->p_pid, (void *)args->scp);
317#endif
318	/*
319	 * The trampoline code hands us the context.
320	 * It is unsafe to keep track of it ourselves, in the event that a
321	 * program jumps out of a signal handler.
322	 */
323	scp = SCARG(args,scp);
324	if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
325		return (EFAULT);
326
327	/*
328	 * Check for security violations.
329	 */
330#define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
331	eflags = context.sc_eflags;
332	/*
333	 * XXX do allow users to change the privileged flag PSL_RF.  The
334	 * cpu sets PSL_RF in tf_eflags for faults.  Debuggers should
335	 * sometimes set it there too.  tf_eflags is kept in the signal
336	 * context during signal handling and there is no other place
337	 * to remember it, so the PSL_RF bit may be corrupted by the
338	 * signal handler without us knowing.  Corruption of the PSL_RF
339	 * bit at worst causes one more or one less debugger trap, so
340	 * allowing it is fairly harmless.
341	 */
342	if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
343    		return(EINVAL);
344	}
345
346	/*
347	 * Don't allow users to load a valid privileged %cs.  Let the
348	 * hardware check for invalid selectors, excess privilege in
349	 * other selectors, invalid %eip's and invalid %esp's.
350	 */
351#define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
352	if (!CS_SECURE(context.sc_cs)) {
353		trapsignal(p, SIGBUS, T_PROTFLT);
354		return(EINVAL);
355	}
356
357	p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
358	p->p_sigmask = context.sc_mask &~
359		(sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP));
360	/*
361	 * Restore signal context.
362	 */
363	/* %gs was restored by the trampoline. */
364	regs->tf_fs     = context.sc_fs;
365	regs->tf_es     = context.sc_es;
366	regs->tf_ds     = context.sc_ds;
367	regs->tf_edi    = context.sc_edi;
368	regs->tf_esi    = context.sc_esi;
369	regs->tf_ebp    = context.sc_ebp;
370	regs->tf_ebx    = context.sc_ebx;
371	regs->tf_edx    = context.sc_edx;
372	regs->tf_ecx    = context.sc_ecx;
373	regs->tf_eax    = context.sc_eax;
374	regs->tf_eip    = context.sc_eip;
375	regs->tf_cs     = context.sc_cs;
376	regs->tf_eflags = eflags;
377	regs->tf_esp    = context.sc_esp_at_signal;
378	regs->tf_ss     = context.sc_ss;
379
380	return (EJUSTRETURN);
381}
382
383static void
384linux_prepsyscall(struct trapframe *tf, int *args, u_int *code, caddr_t *params)
385{
386	args[0] = tf->tf_ebx;
387	args[1] = tf->tf_ecx;
388	args[2] = tf->tf_edx;
389	args[3] = tf->tf_esi;
390	args[4] = tf->tf_edi;
391	*params = NULL;		/* no copyin */
392}
393
394struct sysentvec linux_sysvec = {
395	LINUX_SYS_MAXSYSCALL,
396	linux_sysent,
397	0xff,
398	NSIG,
399	bsd_to_linux_signal,
400	ELAST + 1,
401	bsd_to_linux_errno,
402	translate_traps,
403	linux_fixup,
404	linux_sendsig,
405	linux_sigcode,
406	&linux_szsigcode,
407	linux_prepsyscall,
408	"Linux a.out",
409	aout_coredump
410};
411
412struct sysentvec elf_linux_sysvec = {
413        LINUX_SYS_MAXSYSCALL,
414        linux_sysent,
415        0xff,
416        NSIG,
417        bsd_to_linux_signal,
418        ELAST + 1,
419        bsd_to_linux_errno,
420        translate_traps,
421        elf_linux_fixup,
422        linux_sendsig,
423        linux_sigcode,
424        &linux_szsigcode,
425        linux_prepsyscall,
426	"Linux ELF",
427	elf_coredump
428};
429
430static Elf32_Brandinfo linux_brand = {
431					"Linux",
432					"/compat/linux",
433					"/lib/ld-linux.so.1",
434					&elf_linux_sysvec
435				 };
436
437static Elf32_Brandinfo linux_glibc2brand = {
438					"Linux",
439					"/compat/linux",
440					"/lib/ld-linux.so.2",
441					&elf_linux_sysvec
442				 };
443
444Elf32_Brandinfo *linux_brandlist[] = {
445					&linux_brand,
446					&linux_glibc2brand,
447					NULL
448				};
449
450static int
451linux_elf_modevent(module_t mod, int type, void *data)
452{
453	Elf32_Brandinfo **brandinfo;
454	int error;
455
456	error = 0;
457
458	switch(type) {
459	case MOD_LOAD:
460		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
461		    ++brandinfo)
462			if (elf_insert_brand_entry(*brandinfo) < 0)
463				error = EINVAL;
464		if (error)
465			printf("cannot insert Linux elf brand handler\n");
466		else if (bootverbose)
467			printf("Linux-ELF exec handler installed\n");
468		break;
469	case MOD_UNLOAD:
470		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
471		    ++brandinfo)
472			if (elf_brand_inuse(*brandinfo)) {
473				error = EBUSY;
474			}
475
476		if (error == 0) {
477			for (brandinfo = &linux_brandlist[0];
478			    *brandinfo != NULL; ++brandinfo)
479				if (elf_remove_brand_entry(*brandinfo) < 0)
480					error = EINVAL;
481		}
482		if (error)
483			printf("Could not deinstall ELF interpreter entry\n");
484		else if (bootverbose)
485			printf("Linux-elf exec handler removed\n");
486		break;
487	default:
488		break;
489	}
490	return error;
491}
492static moduledata_t linux_elf_mod = {
493	"linuxelf",
494	linux_elf_modevent,
495	0
496};
497DECLARE_MODULE(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
498