linux_sysvec.c revision 40237
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.35 1998/10/05 16:37:36 jfieber 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 <machine/cpu.h>
56
57#include <i386/linux/linux.h>
58#include <i386/linux/linux_proto.h>
59
60static int	linux_fixup __P((long **stack_base,
61				 struct image_params *iparams));
62static int	elf_linux_fixup __P((long **stack_base,
63				     struct image_params *iparams));
64static void	linux_prepsyscall __P((struct trapframe *tf, int *args,
65				       u_int *code, caddr_t *params));
66static void     linux_sendsig __P((sig_t catcher, int sig, int mask,
67				   u_long code));
68
69/*
70 * Linux syscalls return negative errno's, we do positive and map them
71 */
72static int bsd_to_linux_errno[ELAST] = {
73  	-0,  -1,  -2,  -3,  -4,  -5,  -6,  -7,  -8,  -9,
74 	-10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
75 	-20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
76 	-30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
77 	-90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
78	-100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
79	-110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
80	-116, -66,  -6,  -6,  -6,  -6,  -6, -37, -38,  -9,
81  	-6, -43, -42
82};
83
84int bsd_to_linux_signal[NSIG] = {
85	0, LINUX_SIGHUP, LINUX_SIGINT, LINUX_SIGQUIT,
86	LINUX_SIGILL, LINUX_SIGTRAP, LINUX_SIGABRT, 0,
87	LINUX_SIGFPE, LINUX_SIGKILL, LINUX_SIGBUS, LINUX_SIGSEGV,
88	0, LINUX_SIGPIPE, LINUX_SIGALRM, LINUX_SIGTERM,
89	LINUX_SIGURG, LINUX_SIGSTOP, LINUX_SIGTSTP, LINUX_SIGCONT,
90	LINUX_SIGCHLD, LINUX_SIGTTIN, LINUX_SIGTTOU, LINUX_SIGIO,
91	LINUX_SIGXCPU, LINUX_SIGXFSZ, LINUX_SIGVTALRM, LINUX_SIGPROF,
92	LINUX_SIGWINCH, 0, LINUX_SIGUSR1, LINUX_SIGUSR2
93};
94
95int linux_to_bsd_signal[LINUX_NSIG] = {
96	0, SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGBUS,
97	SIGFPE, SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2, SIGPIPE, SIGALRM, SIGTERM,
98	SIGBUS, SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGURG,
99	SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, SIGWINCH, SIGIO, SIGURG, 0
100};
101
102/*
103 * If FreeBSD & Linux have a difference of opinion about what a trap
104 * means, deal with it here.
105 */
106static int
107translate_traps(int signal, int trap_code)
108{
109	if (signal != SIGBUS)
110		return signal;
111	switch (trap_code) {
112	case T_PROTFLT:
113	case T_TSSFLT:
114	case T_DOUBLEFLT:
115	case T_PAGEFLT:
116		return SIGSEGV;
117	default:
118		return signal;
119	}
120}
121
122static int
123linux_fixup(long **stack_base, struct image_params *imgp)
124{
125	long *argv, *envp;
126
127	argv = *stack_base;
128	envp = *stack_base + (imgp->argc + 1);
129	(*stack_base)--;
130	**stack_base = (intptr_t)(void *)envp;
131	(*stack_base)--;
132	**stack_base = (intptr_t)(void *)argv;
133	(*stack_base)--;
134	**stack_base = imgp->argc;
135	return 0;
136}
137
138static int
139elf_linux_fixup(long **stack_base, struct image_params *imgp)
140{
141	Elf32_Auxargs *args = (Elf32_Auxargs *)imgp->auxargs;
142	long *pos;
143
144	pos = *stack_base + (imgp->argc + imgp->envc + 2);
145
146	if (args->trace) {
147		AUXARGS_ENTRY(pos, AT_DEBUG, 1);
148	}
149	if (args->execfd != -1) {
150		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
151	}
152	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
153	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
154	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
155	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
156	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
157	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
158	AUXARGS_ENTRY(pos, AT_BASE, args->base);
159	AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_cred->p_ruid);
160	AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_cred->p_svuid);
161	AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_cred->p_rgid);
162	AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_cred->p_svgid);
163	AUXARGS_ENTRY(pos, AT_NULL, 0);
164
165	free(imgp->auxargs, M_TEMP);
166	imgp->auxargs = NULL;
167
168	(*stack_base)--;
169	**stack_base = (long)imgp->argc;
170	return 0;
171}
172
173extern int _ucodesel, _udatasel;
174
175/*
176 * Send an interrupt to process.
177 *
178 * Stack is set up to allow sigcode stored
179 * in u. to call routine, followed by kcall
180 * to sigreturn routine below.  After sigreturn
181 * resets the signal mask, the stack, and the
182 * frame pointer, it returns to the user
183 * specified pc, psl.
184 */
185
186static void
187linux_sendsig(sig_t catcher, int sig, int mask, u_long code)
188{
189	register struct proc *p = curproc;
190	register struct trapframe *regs;
191	struct linux_sigframe *fp, frame;
192	struct sigacts *psp = p->p_sigacts;
193	int oonstack;
194
195	regs = p->p_md.md_regs;
196	oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
197
198#ifdef DEBUG
199	printf("Linux-emul(%ld): linux_sendsig(%p, %d, %d, %lu)\n",
200	    (long)p->p_pid, catcher, sig, mask, code);
201#endif
202	/*
203	 * Allocate space for the signal handler context.
204	 */
205	if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
206	    (psp->ps_sigonstack & sigmask(sig))) {
207		fp = (struct linux_sigframe *)(psp->ps_sigstk.ss_sp +
208		    psp->ps_sigstk.ss_size - sizeof(struct linux_sigframe));
209		psp->ps_sigstk.ss_flags |= SS_ONSTACK;
210	} else {
211		fp = (struct linux_sigframe *)regs->tf_esp - 1;
212	}
213
214	/*
215	 * grow() will return FALSE if the fp will not fit inside the stack
216	 *	and the stack can not be grown. useracc will return FALSE
217	 *	if access is denied.
218	 */
219	if ((grow(p, (int)fp) == FALSE) ||
220	    (useracc((caddr_t)fp, sizeof (struct linux_sigframe), B_WRITE) == FALSE)) {
221		/*
222		 * Process has trashed its stack; give it an illegal
223		 * instruction to halt it in its tracks.
224		 */
225		SIGACTION(p, SIGILL) = SIG_DFL;
226		sig = sigmask(SIGILL);
227		p->p_sigignore &= ~sig;
228		p->p_sigcatch &= ~sig;
229		p->p_sigmask &= ~sig;
230		psignal(p, SIGILL);
231		return;
232	}
233
234	/*
235	 * Build the argument list for the signal handler.
236	 */
237	if (p->p_sysent->sv_sigtbl) {
238		if (sig < p->p_sysent->sv_sigsize)
239			sig = p->p_sysent->sv_sigtbl[sig];
240		else
241			sig = p->p_sysent->sv_sigsize + 1;
242	}
243
244	frame.sf_handler = catcher;
245	frame.sf_sig = sig;
246
247	/*
248	 * Build the signal context to be used by sigreturn.
249	 */
250	frame.sf_sc.sc_mask   = mask;
251	__asm("movl %%gs,%w0" : "=r" (frame.sf_sc.sc_gs));
252	__asm("movl %%fs,%w0" : "=r" (frame.sf_sc.sc_fs));
253	frame.sf_sc.sc_es     = regs->tf_es;
254	frame.sf_sc.sc_ds     = regs->tf_ds;
255	frame.sf_sc.sc_edi    = regs->tf_edi;
256	frame.sf_sc.sc_esi    = regs->tf_esi;
257	frame.sf_sc.sc_ebp    = regs->tf_ebp;
258	frame.sf_sc.sc_ebx    = regs->tf_ebx;
259	frame.sf_sc.sc_edx    = regs->tf_edx;
260	frame.sf_sc.sc_ecx    = regs->tf_ecx;
261	frame.sf_sc.sc_eax    = regs->tf_eax;
262	frame.sf_sc.sc_eip    = regs->tf_eip;
263	frame.sf_sc.sc_cs     = regs->tf_cs;
264	frame.sf_sc.sc_eflags = regs->tf_eflags;
265	frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
266	frame.sf_sc.sc_ss     = regs->tf_ss;
267	frame.sf_sc.sc_err    = regs->tf_err;
268	frame.sf_sc.sc_trapno = code;	/* XXX ???? */
269
270	if (copyout(&frame, fp, sizeof(frame)) != 0) {
271		/*
272		 * Process has trashed its stack; give it an illegal
273		 * instruction to halt it in its tracks.
274		 */
275		sigexit(p, SIGILL);
276		/* NOTREACHED */
277	}
278
279	/*
280	 * Build context to run handler in.
281	 */
282	regs->tf_esp = (int)fp;
283	regs->tf_eip = (int)(((char *)PS_STRINGS) - *(p->p_sysent->sv_szsigcode));
284	regs->tf_eflags &= ~PSL_VM;
285	regs->tf_cs = _ucodesel;
286	regs->tf_ds = _udatasel;
287	regs->tf_es = _udatasel;
288	regs->tf_ss = _udatasel;
289}
290
291/*
292 * System call to cleanup state after a signal
293 * has been taken.  Reset signal mask and
294 * stack state from context left by sendsig (above).
295 * Return to previous pc and psl as specified by
296 * context left by sendsig. Check carefully to
297 * make sure that the user has not modified the
298 * psl to gain improper privileges or to cause
299 * a machine fault.
300 */
301int
302linux_sigreturn(p, args)
303	struct proc *p;
304	struct linux_sigreturn_args *args;
305{
306	struct linux_sigcontext *scp, context;
307	register struct trapframe *regs;
308	int eflags;
309
310	regs = p->p_md.md_regs;
311
312#ifdef DEBUG
313	printf("Linux-emul(%ld): linux_sigreturn(%p)\n",
314	    (long)p->p_pid, (void *)args->scp);
315#endif
316	/*
317	 * The trampoline code hands us the context.
318	 * It is unsafe to keep track of it ourselves, in the event that a
319	 * program jumps out of a signal handler.
320	 */
321	scp = SCARG(args,scp);
322	if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
323		return (EFAULT);
324
325	/*
326	 * Check for security violations.
327	 */
328#define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
329	eflags = context.sc_eflags;
330	/*
331	 * XXX do allow users to change the privileged flag PSL_RF.  The
332	 * cpu sets PSL_RF in tf_eflags for faults.  Debuggers should
333	 * sometimes set it there too.  tf_eflags is kept in the signal
334	 * context during signal handling and there is no other place
335	 * to remember it, so the PSL_RF bit may be corrupted by the
336	 * signal handler without us knowing.  Corruption of the PSL_RF
337	 * bit at worst causes one more or one less debugger trap, so
338	 * allowing it is fairly harmless.
339	 */
340	if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
341    		return(EINVAL);
342	}
343
344	/*
345	 * Don't allow users to load a valid privileged %cs.  Let the
346	 * hardware check for invalid selectors, excess privilege in
347	 * other selectors, invalid %eip's and invalid %esp's.
348	 */
349#define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
350	if (!CS_SECURE(context.sc_cs)) {
351		trapsignal(p, SIGBUS, T_PROTFLT);
352		return(EINVAL);
353	}
354
355	p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
356	p->p_sigmask = context.sc_mask &~
357		(sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP));
358	/*
359	 * Restore signal context.
360	 */
361	/* %fs and %gs were restored by the trampoline. */
362	regs->tf_es     = context.sc_es;
363	regs->tf_ds     = context.sc_ds;
364	regs->tf_edi    = context.sc_edi;
365	regs->tf_esi    = context.sc_esi;
366	regs->tf_ebp    = context.sc_ebp;
367	regs->tf_ebx    = context.sc_ebx;
368	regs->tf_edx    = context.sc_edx;
369	regs->tf_ecx    = context.sc_ecx;
370	regs->tf_eax    = context.sc_eax;
371	regs->tf_eip    = context.sc_eip;
372	regs->tf_cs     = context.sc_cs;
373	regs->tf_eflags = eflags;
374	regs->tf_esp    = context.sc_esp_at_signal;
375	regs->tf_ss     = context.sc_ss;
376
377	return (EJUSTRETURN);
378}
379
380static void
381linux_prepsyscall(struct trapframe *tf, int *args, u_int *code, caddr_t *params)
382{
383	args[0] = tf->tf_ebx;
384	args[1] = tf->tf_ecx;
385	args[2] = tf->tf_edx;
386	args[3] = tf->tf_esi;
387	args[4] = tf->tf_edi;
388	*params = NULL;		/* no copyin */
389}
390
391struct sysentvec linux_sysvec = {
392	LINUX_SYS_MAXSYSCALL,
393	linux_sysent,
394	0xff,
395	NSIG,
396	bsd_to_linux_signal,
397	ELAST,
398	bsd_to_linux_errno,
399	translate_traps,
400	linux_fixup,
401	linux_sendsig,
402	linux_sigcode,
403	&linux_szsigcode,
404	linux_prepsyscall,
405	"Linux a.out",
406	aout_coredump
407};
408
409struct sysentvec elf_linux_sysvec = {
410        LINUX_SYS_MAXSYSCALL,
411        linux_sysent,
412        0xff,
413        NSIG,
414        bsd_to_linux_signal,
415        ELAST,
416        bsd_to_linux_errno,
417        translate_traps,
418        elf_linux_fixup,
419        linux_sendsig,
420        linux_sigcode,
421        &linux_szsigcode,
422        linux_prepsyscall,
423	"Linux ELF",
424	elf_coredump
425};
426
427/*
428 * Installed either via SYSINIT() or via LKM stubs.
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
450#ifndef LKM
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 */
455static void linux_elf_init __P((void *dummy));
456
457static void
458linux_elf_init(dummy)
459	void *dummy;
460{
461	Elf32_Brandinfo **brandinfo;
462	int error;
463
464	error = 0;
465
466	for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL; ++brandinfo)
467		if (elf_insert_brand_entry(*brandinfo) < 0)
468			error = 1;
469
470	if (error)
471		printf("cannot insert Linux elf brand handler\n");
472	else if (bootverbose)
473		printf("Linux-ELF exec handler installed\n");
474}
475
476SYSINIT(linuxelf, SI_SUB_VFS, SI_ORDER_ANY, linux_elf_init, NULL);
477#endif
478