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