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