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