Implement linux_mmap2().
[dragonfly.git] / sys / emulation / linux / i386 / linux_sysvec.c
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  * $FreeBSD: src/sys/i386/linux/linux_sysvec.c,v 1.55.2.9 2002/01/12 11:03:30 bde Exp $
29  * $DragonFly: src/sys/emulation/linux/i386/linux_sysvec.c,v 1.15 2003/11/15 03:52:33 daver Exp $
30  */
31
32 /* XXX we use functions that might not exist. */
33 #include "opt_compat.h"
34
35 #ifndef COMPAT_43
36 #error "Unable to compile Linux-emulator due to missing COMPAT_43 option!"
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/imgact.h>
42 #include <sys/imgact_aout.h>
43 #include <sys/imgact_elf.h>
44 #include <sys/kern_syscall.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/proc.h>
48 #include <sys/signalvar.h>
49 #include <sys/sysent.h>
50 #include <sys/sysproto.h>
51
52 #include <vm/vm.h>
53 #include <vm/vm_param.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_extern.h>
56 #include <sys/exec.h>
57 #include <sys/kernel.h>
58 #include <sys/module.h>
59 #include <machine/cpu.h>
60
61 #include "linux.h"
62 #include "linux_proto.h"
63 #include "../linux_signal.h"
64 #include "../linux_util.h"
65
66 MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures");
67
68 #if BYTE_ORDER == LITTLE_ENDIAN
69 #define SHELLMAGIC      0x2123 /* #! */
70 #else
71 #define SHELLMAGIC      0x2321
72 #endif
73
74 /*
75  * Allow the sendsig functions to use the ldebug() facility
76  * even though they are not syscalls themselves. Map them
77  * to syscall 0. This is slightly less bogus than using
78  * ldebug(sigreturn).
79  */
80 #define LINUX_SYS_linux_rt_sendsig      0
81 #define LINUX_SYS_linux_sendsig         0
82
83 extern char linux_sigcode[];
84 extern int linux_szsigcode;
85
86 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
87
88 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
89
90 static int      linux_fixup (register_t **stack_base,
91                                  struct image_params *iparams);
92 static int      elf_linux_fixup (register_t **stack_base,
93                                      struct image_params *iparams);
94 static void     linux_prepsyscall (struct trapframe *tf, int *args,
95                                        u_int *code, caddr_t *params);
96 static void     linux_sendsig (sig_t catcher, int sig, sigset_t *mask,
97                                    u_long code);
98
99 /*
100  * Linux syscalls return negative errno's, we do positive and map them
101  */
102 static int bsd_to_linux_errno[ELAST + 1] = {
103         -0,  -1,  -2,  -3,  -4,  -5,  -6,  -7,  -8,  -9,
104         -10, -35, -12, -13, -14, -15, -16, -17, -18, -19,
105         -20, -21, -22, -23, -24, -25, -26, -27, -28, -29,
106         -30, -31, -32, -33, -34, -11,-115,-114, -88, -89,
107         -90, -91, -92, -93, -94, -95, -96, -97, -98, -99,
108         -100,-101,-102,-103,-104,-105,-106,-107,-108,-109,
109         -110,-111, -40, -36,-112,-113, -39, -11, -87,-122,
110         -116, -66,  -6,  -6,  -6,  -6,  -6, -37, -38,  -9,
111         -6, -6, -43, -42, -75, -6, -84
112 };
113
114 int bsd_to_linux_signal[LINUX_SIGTBLSZ] = {
115         LINUX_SIGHUP, LINUX_SIGINT, LINUX_SIGQUIT, LINUX_SIGILL,
116         LINUX_SIGTRAP, LINUX_SIGABRT, 0, LINUX_SIGFPE,
117         LINUX_SIGKILL, LINUX_SIGBUS, LINUX_SIGSEGV, 0,
118         LINUX_SIGPIPE, LINUX_SIGALRM, LINUX_SIGTERM, LINUX_SIGURG,
119         LINUX_SIGSTOP, LINUX_SIGTSTP, LINUX_SIGCONT, LINUX_SIGCHLD,
120         LINUX_SIGTTIN, LINUX_SIGTTOU, LINUX_SIGIO, LINUX_SIGXCPU,
121         LINUX_SIGXFSZ, LINUX_SIGVTALRM, LINUX_SIGPROF, LINUX_SIGWINCH,
122         0, LINUX_SIGUSR1, LINUX_SIGUSR2
123 };
124
125 int linux_to_bsd_signal[LINUX_SIGTBLSZ] = {
126         SIGHUP, SIGINT, SIGQUIT, SIGILL,
127         SIGTRAP, SIGABRT, SIGBUS, SIGFPE,
128         SIGKILL, SIGUSR1, SIGSEGV, SIGUSR2,
129         SIGPIPE, SIGALRM, SIGTERM, SIGBUS,
130         SIGCHLD, SIGCONT, SIGSTOP, SIGTSTP,
131         SIGTTIN, SIGTTOU, SIGURG, SIGXCPU,
132         SIGXFSZ, SIGVTALRM, SIGPROF, SIGWINCH,
133         SIGIO, SIGURG, 0
134 };
135
136 #define LINUX_T_UNKNOWN  255
137 static int _bsd_to_linux_trapcode[] = {
138         LINUX_T_UNKNOWN,        /* 0 */
139         6,                      /* 1  T_PRIVINFLT */
140         LINUX_T_UNKNOWN,        /* 2 */
141         3,                      /* 3  T_BPTFLT */
142         LINUX_T_UNKNOWN,        /* 4 */
143         LINUX_T_UNKNOWN,        /* 5 */
144         16,                     /* 6  T_ARITHTRAP */
145         254,                    /* 7  T_ASTFLT */
146         LINUX_T_UNKNOWN,        /* 8 */
147         13,                     /* 9  T_PROTFLT */
148         1,                      /* 10 T_TRCTRAP */
149         LINUX_T_UNKNOWN,        /* 11 */
150         14,                     /* 12 T_PAGEFLT */
151         LINUX_T_UNKNOWN,        /* 13 */
152         17,                     /* 14 T_ALIGNFLT */
153         LINUX_T_UNKNOWN,        /* 15 */
154         LINUX_T_UNKNOWN,        /* 16 */
155         LINUX_T_UNKNOWN,        /* 17 */
156         0,                      /* 18 T_DIVIDE */
157         2,                      /* 19 T_NMI */
158         4,                      /* 20 T_OFLOW */
159         5,                      /* 21 T_BOUND */
160         7,                      /* 22 T_DNA */
161         8,                      /* 23 T_DOUBLEFLT */
162         9,                      /* 24 T_FPOPFLT */
163         10,                     /* 25 T_TSSFLT */
164         11,                     /* 26 T_SEGNPFLT */
165         12,                     /* 27 T_STKFLT */
166         18,                     /* 28 T_MCHK */
167         19,                     /* 29 T_XMMFLT */
168         15                      /* 30 T_RESERVED */
169 };
170 #define bsd_to_linux_trapcode(code) \
171     ((code)<sizeof(_bsd_to_linux_trapcode)/sizeof(*_bsd_to_linux_trapcode)? \
172      _bsd_to_linux_trapcode[(code)]: \
173      LINUX_T_UNKNOWN)
174
175 /*
176  * If FreeBSD & Linux have a difference of opinion about what a trap
177  * means, deal with it here.
178  */
179 static int
180 translate_traps(int signal, int trap_code)
181 {
182         if (signal != SIGBUS)
183                 return signal;
184         switch (trap_code) {
185         case T_PROTFLT:
186         case T_TSSFLT:
187         case T_DOUBLEFLT:
188         case T_PAGEFLT:
189                 return SIGSEGV;
190         default:
191                 return signal;
192         }
193 }
194
195 static int
196 linux_fixup(register_t **stack_base, struct image_params *imgp)
197 {
198         register_t *argv, *envp;
199
200         argv = *stack_base;
201         envp = *stack_base + (imgp->args->argc + 1);
202         (*stack_base)--;
203         **stack_base = (intptr_t)(void *)envp;
204         (*stack_base)--;
205         **stack_base = (intptr_t)(void *)argv;
206         (*stack_base)--;
207         **stack_base = imgp->args->argc;
208         return 0;
209 }
210
211 static int
212 elf_linux_fixup(register_t **stack_base, struct image_params *imgp)
213 {
214         Elf32_Auxargs *args = (Elf32_Auxargs *)imgp->auxargs;
215         register_t *pos;
216              
217         pos = *stack_base + (imgp->args->argc + imgp->args->envc + 2);  
218     
219         if (args->trace) {
220                 AUXARGS_ENTRY(pos, AT_DEBUG, 1);
221         }
222         if (args->execfd != -1) {
223                 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
224         }       
225         AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
226         AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
227         AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
228         AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
229         AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
230         AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
231         AUXARGS_ENTRY(pos, AT_BASE, args->base);
232         AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
233         AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
234         AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
235         AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
236         AUXARGS_ENTRY(pos, AT_NULL, 0);
237         
238         free(imgp->auxargs, M_TEMP);      
239         imgp->auxargs = NULL;
240
241         (*stack_base)--;
242         **stack_base = (long)imgp->args->argc;
243         return 0;
244 }
245
246 extern int _ucodesel, _udatasel;
247 extern unsigned long linux_sznonrtsigcode;
248
249 static void
250 linux_rt_sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
251 {
252         struct proc *p = curproc;
253         struct trapframe *regs;
254         struct l_rt_sigframe *fp, frame;
255         int oonstack;
256
257         regs = p->p_md.md_regs;
258         oonstack = p->p_sigstk.ss_flags & SS_ONSTACK;
259
260 #ifdef DEBUG
261         if (ldebug(rt_sendsig))
262                 printf(ARGS(rt_sendsig, "%p, %d, %p, %lu"),
263                     catcher, sig, (void*)mask, code);
264 #endif
265         /*
266          * Allocate space for the signal handler context.
267          */
268         if ((p->p_flag & P_ALTSTACK) && !oonstack &&
269             SIGISMEMBER(p->p_sigacts->ps_sigonstack, sig)) {
270                 fp = (struct l_rt_sigframe *)(p->p_sigstk.ss_sp +
271                     p->p_sigstk.ss_size - sizeof(struct l_rt_sigframe));
272                 p->p_sigstk.ss_flags |= SS_ONSTACK;
273         } else
274                 fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
275
276         /*
277          * grow() will return FALSE if the fp will not fit inside the stack
278          *      and the stack can not be grown. useracc will return FALSE
279          *      if access is denied.
280          */
281         if ((grow_stack (p, (int)fp) == FALSE) ||
282             !useracc((caddr_t)fp, sizeof (struct l_rt_sigframe), 
283             VM_PROT_WRITE)) {
284                 /*
285                  * Process has trashed its stack; give it an illegal
286                  * instruction to halt it in its tracks.
287                  */
288                 SIGACTION(p, SIGILL) = SIG_DFL;
289                 SIGDELSET(p->p_sigignore, SIGILL);
290                 SIGDELSET(p->p_sigcatch, SIGILL);
291                 SIGDELSET(p->p_sigmask, SIGILL);
292 #ifdef DEBUG
293                 if (ldebug(rt_sendsig))
294                         printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"),
295                             fp, oonstack);
296 #endif
297                 psignal(p, SIGILL);
298                 return;
299         }
300
301         /*
302          * Build the argument list for the signal handler.
303          */
304         if (p->p_sysent->sv_sigtbl)
305                 if (sig <= p->p_sysent->sv_sigsize)
306                         sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
307
308         frame.sf_handler = catcher;
309         frame.sf_sig = sig;
310         frame.sf_siginfo = &fp->sf_si;
311         frame.sf_ucontext = &fp->sf_sc;
312
313         /* Fill siginfo structure. */
314         frame.sf_si.lsi_signo = sig;
315         frame.sf_si.lsi_code = code;
316         frame.sf_si.lsi_addr = (void *)regs->tf_err;
317
318         /*
319          * Build the signal context to be used by sigreturn.
320          */
321         frame.sf_sc.uc_flags = 0;               /* XXX ??? */
322         frame.sf_sc.uc_link = NULL;             /* XXX ??? */
323
324         frame.sf_sc.uc_stack.ss_sp = p->p_sigstk.ss_sp;
325         frame.sf_sc.uc_stack.ss_size = p->p_sigstk.ss_size;
326         frame.sf_sc.uc_stack.ss_flags = (p->p_flag & P_ALTSTACK)
327             ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
328
329         bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
330
331         frame.sf_sc.uc_mcontext.sc_mask   = frame.sf_sc.uc_sigmask.__bits[0];
332         frame.sf_sc.uc_mcontext.sc_gs     = rgs();
333         frame.sf_sc.uc_mcontext.sc_fs     = regs->tf_fs;
334         frame.sf_sc.uc_mcontext.sc_es     = regs->tf_es;
335         frame.sf_sc.uc_mcontext.sc_ds     = regs->tf_ds;
336         frame.sf_sc.uc_mcontext.sc_edi    = regs->tf_edi;
337         frame.sf_sc.uc_mcontext.sc_esi    = regs->tf_esi;
338         frame.sf_sc.uc_mcontext.sc_ebp    = regs->tf_ebp;
339         frame.sf_sc.uc_mcontext.sc_ebx    = regs->tf_ebx;
340         frame.sf_sc.uc_mcontext.sc_edx    = regs->tf_edx;
341         frame.sf_sc.uc_mcontext.sc_ecx    = regs->tf_ecx;
342         frame.sf_sc.uc_mcontext.sc_eax    = regs->tf_eax;
343         frame.sf_sc.uc_mcontext.sc_eip    = regs->tf_eip;
344         frame.sf_sc.uc_mcontext.sc_cs     = regs->tf_cs;
345         frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_eflags;
346         frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
347         frame.sf_sc.uc_mcontext.sc_ss     = regs->tf_ss;
348         frame.sf_sc.uc_mcontext.sc_err    = regs->tf_err;
349         frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
350
351 #ifdef DEBUG
352         if (ldebug(rt_sendsig))
353                 printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%x, mask: 0x%x"),
354                     frame.sf_sc.uc_stack.ss_flags, p->p_sigstk.ss_sp,
355                     p->p_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask);
356 #endif
357
358         if (copyout(&frame, fp, sizeof(frame)) != 0) {
359                 /*
360                  * Process has trashed its stack; give it an illegal
361                  * instruction to halt it in its tracks.
362                  */
363                 sigexit(p, SIGILL);
364                 /* NOTREACHED */
365         }
366
367         /*
368          * Build context to run handler in.
369          */
370         regs->tf_esp = (int)fp;
371         regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode) + 
372             linux_sznonrtsigcode;
373         regs->tf_eflags &= ~(PSL_T | PSL_VM);
374         regs->tf_cs = _ucodesel;
375         regs->tf_ds = _udatasel;
376         regs->tf_es = _udatasel;
377         regs->tf_fs = _udatasel;
378         regs->tf_ss = _udatasel;
379 }
380
381
382 /*
383  * Send an interrupt to process.
384  *
385  * Stack is set up to allow sigcode stored
386  * in u. to call routine, followed by kcall
387  * to sigreturn routine below.  After sigreturn
388  * resets the signal mask, the stack, and the
389  * frame pointer, it returns to the user
390  * specified pc, psl.
391  */
392
393 static void
394 linux_sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
395 {
396         struct proc *p = curproc;
397         struct trapframe *regs;
398         struct l_sigframe *fp, frame;
399         l_sigset_t lmask;
400         int oonstack, i;
401
402         if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) {
403                 /* Signal handler installed with SA_SIGINFO. */
404                 linux_rt_sendsig(catcher, sig, mask, code);
405                 return;
406         }
407
408         regs = p->p_md.md_regs;
409         oonstack = p->p_sigstk.ss_flags & SS_ONSTACK;
410
411 #ifdef DEBUG
412         if (ldebug(sendsig))
413                 printf(ARGS(sendsig, "%p, %d, %p, %lu"),
414                     catcher, sig, (void*)mask, code);
415 #endif
416
417         /*
418          * Allocate space for the signal handler context.
419          */
420         if ((p->p_flag & P_ALTSTACK) && !oonstack &&
421             SIGISMEMBER(p->p_sigacts->ps_sigonstack, sig)) {
422                 fp = (struct l_sigframe *)(p->p_sigstk.ss_sp +
423                     p->p_sigstk.ss_size - sizeof(struct l_sigframe));
424                 p->p_sigstk.ss_flags |= SS_ONSTACK;
425         } else
426                 fp = (struct l_sigframe *)regs->tf_esp - 1;
427
428         /*
429          * grow() will return FALSE if the fp will not fit inside the stack
430          *      and the stack can not be grown. useracc will return FALSE
431          *      if access is denied.
432          */
433         if ((grow_stack (p, (int)fp) == FALSE) ||
434             !useracc((caddr_t)fp, sizeof (struct l_sigframe), 
435             VM_PROT_WRITE)) {
436                 /*
437                  * Process has trashed its stack; give it an illegal
438                  * instruction to halt it in its tracks.
439                  */
440                 SIGACTION(p, SIGILL) = SIG_DFL;
441                 SIGDELSET(p->p_sigignore, SIGILL);
442                 SIGDELSET(p->p_sigcatch, SIGILL);
443                 SIGDELSET(p->p_sigmask, SIGILL);
444                 psignal(p, SIGILL);
445                 return;
446         }
447
448         /*
449          * Build the argument list for the signal handler.
450          */
451         if (p->p_sysent->sv_sigtbl)
452                 if (sig <= p->p_sysent->sv_sigsize)
453                         sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
454
455         frame.sf_handler = catcher;
456         frame.sf_sig = sig;
457
458         bsd_to_linux_sigset(mask, &lmask);
459
460         /*
461          * Build the signal context to be used by sigreturn.
462          */
463         frame.sf_sc.sc_mask   = lmask.__bits[0];
464         frame.sf_sc.sc_gs     = rgs();
465         frame.sf_sc.sc_fs     = regs->tf_fs;
466         frame.sf_sc.sc_es     = regs->tf_es;
467         frame.sf_sc.sc_ds     = regs->tf_ds;
468         frame.sf_sc.sc_edi    = regs->tf_edi;
469         frame.sf_sc.sc_esi    = regs->tf_esi;
470         frame.sf_sc.sc_ebp    = regs->tf_ebp;
471         frame.sf_sc.sc_ebx    = regs->tf_ebx;
472         frame.sf_sc.sc_edx    = regs->tf_edx;
473         frame.sf_sc.sc_ecx    = regs->tf_ecx;
474         frame.sf_sc.sc_eax    = regs->tf_eax;
475         frame.sf_sc.sc_eip    = regs->tf_eip;
476         frame.sf_sc.sc_cs     = regs->tf_cs;
477         frame.sf_sc.sc_eflags = regs->tf_eflags;
478         frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
479         frame.sf_sc.sc_ss     = regs->tf_ss;
480         frame.sf_sc.sc_err    = regs->tf_err;
481         frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
482
483         bzero(&frame.sf_fpstate, sizeof(struct l_fpstate));
484
485         for (i = 0; i < (LINUX_NSIG_WORDS-1); i++)
486                 frame.sf_extramask[i] = lmask.__bits[i+1];
487
488         if (copyout(&frame, fp, sizeof(frame)) != 0) {
489                 /*
490                  * Process has trashed its stack; give it an illegal
491                  * instruction to halt it in its tracks.
492                  */
493                 sigexit(p, SIGILL);
494                 /* NOTREACHED */
495         }
496
497         /*
498          * Build context to run handler in.
499          */
500         regs->tf_esp = (int)fp;
501         regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
502         regs->tf_eflags &= ~(PSL_T | PSL_VM);
503         regs->tf_cs = _ucodesel;
504         regs->tf_ds = _udatasel;
505         regs->tf_es = _udatasel;
506         regs->tf_fs = _udatasel;
507         regs->tf_ss = _udatasel;
508 }
509
510 /*
511  * System call to cleanup state after a signal
512  * has been taken.  Reset signal mask and
513  * stack state from context left by sendsig (above).
514  * Return to previous pc and psl as specified by
515  * context left by sendsig. Check carefully to
516  * make sure that the user has not modified the
517  * psl to gain improper privileges or to cause
518  * a machine fault.
519  */
520 int
521 linux_sigreturn(struct linux_sigreturn_args *args)
522 {
523         struct proc *p = curproc;
524         struct l_sigframe frame;
525         struct trapframe *regs;
526         l_sigset_t lmask;
527         int eflags, i;
528
529         regs = p->p_md.md_regs;
530
531 #ifdef DEBUG
532         if (ldebug(sigreturn))
533                 printf(ARGS(sigreturn, "%p"), (void *)args->sfp);
534 #endif
535         /*
536          * The trampoline code hands us the sigframe.
537          * It is unsafe to keep track of it ourselves, in the event that a
538          * program jumps out of a signal handler.
539          */
540         if (copyin((caddr_t)args->sfp, &frame, sizeof(frame)) != 0)
541                 return (EFAULT);
542
543         /*
544          * Check for security violations.
545          */
546 #define EFLAGS_SECURE(ef, oef)  ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
547         eflags = frame.sf_sc.sc_eflags;
548         /*
549          * XXX do allow users to change the privileged flag PSL_RF.  The
550          * cpu sets PSL_RF in tf_eflags for faults.  Debuggers should
551          * sometimes set it there too.  tf_eflags is kept in the signal
552          * context during signal handling and there is no other place
553          * to remember it, so the PSL_RF bit may be corrupted by the
554          * signal handler without us knowing.  Corruption of the PSL_RF
555          * bit at worst causes one more or one less debugger trap, so
556          * allowing it is fairly harmless.
557          */
558         if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
559                 return(EINVAL);
560         }
561
562         /*
563          * Don't allow users to load a valid privileged %cs.  Let the
564          * hardware check for invalid selectors, excess privilege in
565          * other selectors, invalid %eip's and invalid %esp's.
566          */
567 #define CS_SECURE(cs)   (ISPL(cs) == SEL_UPL)
568         if (!CS_SECURE(frame.sf_sc.sc_cs)) {
569                 trapsignal(p, SIGBUS, T_PROTFLT);
570                 return(EINVAL);
571         }
572
573         p->p_sigstk.ss_flags &= ~SS_ONSTACK;
574         lmask.__bits[0] = frame.sf_sc.sc_mask;
575         for (i = 0; i < (LINUX_NSIG_WORDS-1); i++)
576                 lmask.__bits[i+1] = frame.sf_extramask[i];
577         linux_to_bsd_sigset(&lmask, &p->p_sigmask);
578         SIG_CANTMASK(p->p_sigmask);
579
580         /*
581          * Restore signal context.
582          */
583         /* %gs was restored by the trampoline. */
584         regs->tf_fs     = frame.sf_sc.sc_fs;
585         regs->tf_es     = frame.sf_sc.sc_es;
586         regs->tf_ds     = frame.sf_sc.sc_ds;
587         regs->tf_edi    = frame.sf_sc.sc_edi;
588         regs->tf_esi    = frame.sf_sc.sc_esi;
589         regs->tf_ebp    = frame.sf_sc.sc_ebp;
590         regs->tf_ebx    = frame.sf_sc.sc_ebx;
591         regs->tf_edx    = frame.sf_sc.sc_edx;
592         regs->tf_ecx    = frame.sf_sc.sc_ecx;
593         regs->tf_eax    = frame.sf_sc.sc_eax;
594         regs->tf_eip    = frame.sf_sc.sc_eip;
595         regs->tf_cs     = frame.sf_sc.sc_cs;
596         regs->tf_eflags = eflags;
597         regs->tf_esp    = frame.sf_sc.sc_esp_at_signal;
598         regs->tf_ss     = frame.sf_sc.sc_ss;
599
600         return (EJUSTRETURN);
601 }
602
603 /*
604  * System call to cleanup state after a signal
605  * has been taken.  Reset signal mask and
606  * stack state from context left by rt_sendsig (above).
607  * Return to previous pc and psl as specified by
608  * context left by sendsig. Check carefully to
609  * make sure that the user has not modified the
610  * psl to gain improper privileges or to cause
611  * a machine fault.
612  */
613 int
614 linux_rt_sigreturn(struct linux_rt_sigreturn_args *args)
615 {
616         struct proc *p = curproc;
617         struct l_ucontext uc;
618         struct l_sigcontext *context;
619         l_stack_t *lss;
620         stack_t ss;
621         struct trapframe *regs;
622         int eflags;
623
624         regs = p->p_md.md_regs;
625
626 #ifdef DEBUG
627         if (ldebug(rt_sigreturn))
628                 printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp);
629 #endif
630         /*
631          * The trampoline code hands us the ucontext.
632          * It is unsafe to keep track of it ourselves, in the event that a
633          * program jumps out of a signal handler.
634          */
635         if (copyin((caddr_t)args->ucp, &uc, sizeof(uc)) != 0)
636                 return (EFAULT);
637
638         context = &uc.uc_mcontext;
639
640         /*
641          * Check for security violations.
642          */
643 #define EFLAGS_SECURE(ef, oef)  ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
644         eflags = context->sc_eflags;
645         /*
646          * XXX do allow users to change the privileged flag PSL_RF.  The
647          * cpu sets PSL_RF in tf_eflags for faults.  Debuggers should
648          * sometimes set it there too.  tf_eflags is kept in the signal
649          * context during signal handling and there is no other place
650          * to remember it, so the PSL_RF bit may be corrupted by the
651          * signal handler without us knowing.  Corruption of the PSL_RF
652          * bit at worst causes one more or one less debugger trap, so
653          * allowing it is fairly harmless.
654          */
655         if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
656                 return(EINVAL);
657         }
658
659         /*
660          * Don't allow users to load a valid privileged %cs.  Let the
661          * hardware check for invalid selectors, excess privilege in
662          * other selectors, invalid %eip's and invalid %esp's.
663          */
664 #define CS_SECURE(cs)   (ISPL(cs) == SEL_UPL)
665         if (!CS_SECURE(context->sc_cs)) {
666                 trapsignal(p, SIGBUS, T_PROTFLT);
667                 return(EINVAL);
668         }
669
670         p->p_sigstk.ss_flags &= ~SS_ONSTACK;
671         linux_to_bsd_sigset(&uc.uc_sigmask, &p->p_sigmask);
672         SIG_CANTMASK(p->p_sigmask);
673
674         /*
675          * Restore signal context
676          */
677         /* %gs was restored by the trampoline. */
678         regs->tf_fs     = context->sc_fs;
679         regs->tf_es     = context->sc_es;
680         regs->tf_ds     = context->sc_ds;
681         regs->tf_edi    = context->sc_edi;
682         regs->tf_esi    = context->sc_esi;
683         regs->tf_ebp    = context->sc_ebp;
684         regs->tf_ebx    = context->sc_ebx;
685         regs->tf_edx    = context->sc_edx;
686         regs->tf_ecx    = context->sc_ecx;
687         regs->tf_eax    = context->sc_eax;
688         regs->tf_eip    = context->sc_eip;
689         regs->tf_cs     = context->sc_cs;
690         regs->tf_eflags = eflags;
691         regs->tf_esp    = context->sc_esp_at_signal;
692         regs->tf_ss     = context->sc_ss;
693
694         /*
695          * call sigaltstack & ignore results..
696          */
697         lss = &uc.uc_stack;
698         ss.ss_sp = lss->ss_sp;
699         ss.ss_size = lss->ss_size;
700         ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
701
702 #ifdef DEBUG
703         if (ldebug(rt_sigreturn))
704                 printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%x, mask: 0x%x"),
705                     ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask);
706 #endif
707         kern_sigaltstack(&ss, NULL);
708
709         return (EJUSTRETURN);
710 }
711
712 static void
713 linux_prepsyscall(struct trapframe *tf, int *args, u_int *code, caddr_t *params)
714 {
715         args[0] = tf->tf_ebx;
716         args[1] = tf->tf_ecx;
717         args[2] = tf->tf_edx;
718         args[3] = tf->tf_esi;
719         args[4] = tf->tf_edi;
720         args[5] = tf->tf_ebp;
721         *params = NULL;         /* no copyin */
722 }
723
724 /*
725  * If a linux binary is exec'ing something, try this image activator 
726  * first.  We override standard shell script execution in order to
727  * be able to modify the interpreter path.  We only do this if a linux
728  * binary is doing the exec, so we do not create an EXEC module for it.
729  */
730 static int      exec_linux_imgact_try (struct image_params *iparams);
731
732 static int
733 exec_linux_imgact_try(imgp)
734     struct image_params *imgp;
735 {
736     const char *head = (const char *)imgp->image_header;
737     int error = -1;
738
739     /*
740      * The interpreter for shell scripts run from a linux binary needs
741      * to be located in /compat/linux if possible in order to recursively
742      * maintain linux path emulation.
743      */
744     if (((const short *)head)[0] == SHELLMAGIC) {
745             /*
746              * Run our normal shell image activator.  If it succeeds attempt
747              * to use the alternate path for the interpreter.  If an alternate
748              * path is found, use our stringspace to store it.
749              */
750             if ((error = exec_shell_imgact(imgp)) == 0) {
751                     linux_translate_path(imgp->interpreter_name,
752                         MAXSHELLCMDLEN);
753             }
754     }
755     return(error);
756 }
757
758 struct sysentvec linux_sysvec = {
759         LINUX_SYS_MAXSYSCALL,
760         linux_sysent,
761         0xff,
762         LINUX_SIGTBLSZ,
763         bsd_to_linux_signal,
764         ELAST + 1, 
765         bsd_to_linux_errno,
766         translate_traps,
767         linux_fixup,
768         linux_sendsig,
769         linux_sigcode,  
770         &linux_szsigcode,
771         linux_prepsyscall,
772         "Linux a.out",
773         aout_coredump,
774         exec_linux_imgact_try,
775         LINUX_MINSIGSTKSZ
776 };
777
778 struct sysentvec elf_linux_sysvec = {
779         LINUX_SYS_MAXSYSCALL,
780         linux_sysent,
781         0xff,
782         LINUX_SIGTBLSZ,
783         bsd_to_linux_signal,
784         ELAST + 1,
785         bsd_to_linux_errno,
786         translate_traps,
787         elf_linux_fixup,
788         linux_sendsig,
789         linux_sigcode,
790         &linux_szsigcode,
791         linux_prepsyscall,
792         "Linux ELF",
793         elf_coredump,
794         exec_linux_imgact_try,
795         LINUX_MINSIGSTKSZ
796 };
797
798 static Elf32_Brandinfo linux_brand = {
799                                         ELFOSABI_LINUX,
800                                         "Linux",
801                                         "/compat/linux",
802                                         "/lib/ld-linux.so.1",
803                                         &elf_linux_sysvec
804                                  };
805
806 static Elf32_Brandinfo linux_glibc2brand = {
807                                         ELFOSABI_LINUX,
808                                         "Linux",
809                                         "/compat/linux",
810                                         "/lib/ld-linux.so.2",
811                                         &elf_linux_sysvec
812                                  };
813
814 Elf32_Brandinfo *linux_brandlist[] = {
815                                         &linux_brand,
816                                         &linux_glibc2brand,
817                                         NULL
818                                 };
819
820 static int
821 linux_elf_modevent(module_t mod, int type, void *data)
822 {
823         Elf32_Brandinfo **brandinfo;
824         int error;
825         struct linux_ioctl_handler **lihp;
826
827         error = 0;
828
829         switch(type) {
830         case MOD_LOAD:
831                 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
832                      ++brandinfo)
833                         if (elf_insert_brand_entry(*brandinfo) < 0)
834                                 error = EINVAL;
835                 if (error == 0) {
836                         SET_FOREACH(lihp, linux_ioctl_handler_set)
837                                 linux_ioctl_register_handler(*lihp);
838                         if (bootverbose)
839                                 printf("Linux ELF exec handler installed\n");
840                 } else
841                         printf("cannot insert Linux ELF brand handler\n");
842                 break;
843         case MOD_UNLOAD:
844                 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
845                      ++brandinfo)
846                         if (elf_brand_inuse(*brandinfo))
847                                 error = EBUSY;
848                 if (error == 0) {
849                         for (brandinfo = &linux_brandlist[0];
850                              *brandinfo != NULL; ++brandinfo)
851                                 if (elf_remove_brand_entry(*brandinfo) < 0)
852                                         error = EINVAL;
853                 }
854                 if (error == 0) {
855                         SET_FOREACH(lihp, linux_ioctl_handler_set)
856                                 linux_ioctl_unregister_handler(*lihp);
857                         if (bootverbose)
858                                 printf("Linux ELF exec handler removed\n");
859                 } else
860                         printf("Could not deinstall ELF interpreter entry\n");
861                 break;
862         default:
863                 break;
864         }
865         return error;
866 }
867
868 static moduledata_t linux_elf_mod = {
869         "linuxelf",
870         linux_elf_modevent,
871         0
872 };
873
874 DECLARE_MODULE(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);