]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/linux/linux_sysvec.c
MFC r331356 (by emaste@):
[FreeBSD/FreeBSD.git] / sys / i386 / linux / 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 without 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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/exec.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact.h>
37 #include <sys/imgact_aout.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/proc.h>
45 #include <sys/signalvar.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/sysctl.h>
48 #include <sys/sysent.h>
49 #include <sys/sysproto.h>
50 #include <sys/vnode.h>
51 #include <sys/eventhandler.h>
52
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55 #include <vm/vm_extern.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_param.h>
60
61 #include <machine/cpu.h>
62 #include <machine/cputypes.h>
63 #include <machine/md_var.h>
64 #include <machine/pcb.h>
65
66 #include <i386/linux/linux.h>
67 #include <i386/linux/linux_proto.h>
68 #include <compat/linux/linux_emul.h>
69 #include <compat/linux/linux_futex.h>
70 #include <compat/linux/linux_ioctl.h>
71 #include <compat/linux/linux_mib.h>
72 #include <compat/linux/linux_misc.h>
73 #include <compat/linux/linux_signal.h>
74 #include <compat/linux/linux_util.h>
75 #include <compat/linux/linux_vdso.h>
76
77 MODULE_VERSION(linux, 1);
78
79 #if BYTE_ORDER == LITTLE_ENDIAN
80 #define SHELLMAGIC      0x2123 /* #! */
81 #else
82 #define SHELLMAGIC      0x2321
83 #endif
84
85 #if defined(DEBUG)
86 SYSCTL_PROC(_compat_linux, OID_AUTO, debug, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
87     linux_sysctl_debug, "A", "Linux debugging control");
88 #endif
89
90 /*
91  * Allow the sendsig functions to use the ldebug() facility even though they
92  * are not syscalls themselves.  Map them to syscall 0.  This is slightly less
93  * bogus than using ldebug(sigreturn).
94  */
95 #define LINUX_SYS_linux_rt_sendsig      0
96 #define LINUX_SYS_linux_sendsig         0
97
98 #define LINUX_PS_STRINGS        (LINUX_USRSTACK - sizeof(struct ps_strings))
99
100 static int linux_szsigcode;
101 static vm_object_t linux_shared_page_obj;
102 static char *linux_shared_page_mapping;
103 extern char _binary_linux_locore_o_start;
104 extern char _binary_linux_locore_o_end;
105
106 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
107
108 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
109
110 static int      linux_fixup(register_t **stack_base,
111                     struct image_params *iparams);
112 static int      linux_fixup_elf(register_t **stack_base,
113                     struct image_params *iparams);
114 static void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
115 static int      linux_exec_imgact_try(struct image_params *iparams);
116 static void     linux_exec_setregs(struct thread *td,
117                     struct image_params *imgp, u_long stack);
118 static register_t *linux_copyout_strings(struct image_params *imgp);
119 static bool     linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
120 static void     linux_vdso_install(void *param);
121 static void     linux_vdso_deinstall(void *param);
122
123 static int linux_szplatform;
124 const char *linux_kplatform;
125
126 static eventhandler_tag linux_exit_tag;
127 static eventhandler_tag linux_exec_tag;
128 static eventhandler_tag linux_thread_dtor_tag;
129
130 #define LINUX_T_UNKNOWN  255
131 static int _bsd_to_linux_trapcode[] = {
132         LINUX_T_UNKNOWN,        /* 0 */
133         6,                      /* 1  T_PRIVINFLT */
134         LINUX_T_UNKNOWN,        /* 2 */
135         3,                      /* 3  T_BPTFLT */
136         LINUX_T_UNKNOWN,        /* 4 */
137         LINUX_T_UNKNOWN,        /* 5 */
138         16,                     /* 6  T_ARITHTRAP */
139         254,                    /* 7  T_ASTFLT */
140         LINUX_T_UNKNOWN,        /* 8 */
141         13,                     /* 9  T_PROTFLT */
142         1,                      /* 10 T_TRCTRAP */
143         LINUX_T_UNKNOWN,        /* 11 */
144         14,                     /* 12 T_PAGEFLT */
145         LINUX_T_UNKNOWN,        /* 13 */
146         17,                     /* 14 T_ALIGNFLT */
147         LINUX_T_UNKNOWN,        /* 15 */
148         LINUX_T_UNKNOWN,        /* 16 */
149         LINUX_T_UNKNOWN,        /* 17 */
150         0,                      /* 18 T_DIVIDE */
151         2,                      /* 19 T_NMI */
152         4,                      /* 20 T_OFLOW */
153         5,                      /* 21 T_BOUND */
154         7,                      /* 22 T_DNA */
155         8,                      /* 23 T_DOUBLEFLT */
156         9,                      /* 24 T_FPOPFLT */
157         10,                     /* 25 T_TSSFLT */
158         11,                     /* 26 T_SEGNPFLT */
159         12,                     /* 27 T_STKFLT */
160         18,                     /* 28 T_MCHK */
161         19,                     /* 29 T_XMMFLT */
162         15                      /* 30 T_RESERVED */
163 };
164 #define bsd_to_linux_trapcode(code) \
165     ((code)<nitems(_bsd_to_linux_trapcode)? \
166      _bsd_to_linux_trapcode[(code)]: \
167      LINUX_T_UNKNOWN)
168
169 LINUX_VDSO_SYM_INTPTR(linux_sigcode);
170 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode);
171 LINUX_VDSO_SYM_INTPTR(linux_vsyscall);
172
173 /*
174  * If FreeBSD & Linux have a difference of opinion about what a trap
175  * means, deal with it here.
176  *
177  * MPSAFE
178  */
179 static int
180 linux_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         suword(*stack_base, (intptr_t)(void *)envp);
204         (*stack_base)--;
205         suword(*stack_base, (intptr_t)(void *)argv);
206         (*stack_base)--;
207         suword(*stack_base, imgp->args->argc);
208         return (0);
209 }
210
211 static int
212 linux_fixup_elf(register_t **stack_base, struct image_params *imgp)
213 {
214         struct proc *p;
215         Elf32_Auxargs *args;
216         Elf32_Addr *uplatform;
217         struct ps_strings *arginfo;
218         register_t *pos;
219         int issetugid;
220
221         KASSERT(curthread->td_proc == imgp->proc,
222             ("unsafe linux_fixup_elf(), should be curproc"));
223
224         p = imgp->proc;
225         issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
226         arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
227         uplatform = (Elf32_Addr *)((caddr_t)arginfo - linux_szplatform);
228         args = (Elf32_Auxargs *)imgp->auxargs;
229         pos = *stack_base + (imgp->args->argc + imgp->args->envc + 2);
230
231         AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR,
232             imgp->proc->p_sysent->sv_shared_page_base);
233         AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, linux_vsyscall);
234         AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
235
236         /*
237          * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
238          * as it has appeared in the 2.4.0-rc7 first time.
239          * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
240          * glibc falls back to the hard-coded CLK_TCK value when aux entry
241          * is not present.
242          * Also see linux_times() implementation.
243          */
244         if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
245                 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
246         AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
247         AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
248         AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
249         AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
250         AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
251         AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
252         AUXARGS_ENTRY(pos, AT_BASE, args->base);
253         AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
254         AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
255         AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
256         AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
257         AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
258         AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(uplatform));
259         AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, imgp->canary);
260         if (imgp->execpathp != 0)
261                 AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, imgp->execpathp);
262         if (args->execfd != -1)
263                 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
264         AUXARGS_ENTRY(pos, AT_NULL, 0);
265
266         free(imgp->auxargs, M_TEMP);
267         imgp->auxargs = NULL;
268
269         (*stack_base)--;
270         suword(*stack_base, (register_t)imgp->args->argc);
271         return (0);
272 }
273
274 /*
275  * Copied from kern/kern_exec.c
276  */
277 static register_t *
278 linux_copyout_strings(struct image_params *imgp)
279 {
280         int argc, envc;
281         char **vectp;
282         char *stringp, *destp;
283         register_t *stack_base;
284         struct ps_strings *arginfo;
285         char canary[LINUX_AT_RANDOM_LEN];
286         size_t execpath_len;
287         struct proc *p;
288
289         /* Calculate string base and vector table pointers. */
290         p = imgp->proc;
291         if (imgp->execpath != NULL && imgp->auxargs != NULL)
292                 execpath_len = strlen(imgp->execpath) + 1;
293         else
294                 execpath_len = 0;
295         arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
296         destp = (caddr_t)arginfo - SPARE_USRSPACE - linux_szplatform -
297             roundup(sizeof(canary), sizeof(char *)) -
298             roundup(execpath_len, sizeof(char *)) -
299             roundup(ARG_MAX - imgp->args->stringspace, sizeof(char *));
300
301         /* Install LINUX_PLATFORM. */
302         copyout(linux_kplatform, ((caddr_t)arginfo - linux_szplatform),
303             linux_szplatform);
304
305         if (execpath_len != 0) {
306                 imgp->execpathp = (uintptr_t)arginfo -
307                 linux_szplatform - execpath_len;
308                 copyout(imgp->execpath, (void *)imgp->execpathp, execpath_len);
309         }
310
311         /* Prepare the canary for SSP. */
312         arc4rand(canary, sizeof(canary), 0);
313         imgp->canary = (uintptr_t)arginfo - linux_szplatform -
314             roundup(execpath_len, sizeof(char *)) -
315             roundup(sizeof(canary), sizeof(char *));
316         copyout(canary, (void *)imgp->canary, sizeof(canary));
317
318         vectp = (char **)destp;
319         if (imgp->auxargs) {
320                 /*
321                  * Allocate room on the stack for the ELF auxargs
322                  * array.  It has LINUX_AT_COUNT entries.
323                  */
324                 vectp -= howmany(LINUX_AT_COUNT * sizeof(Elf32_Auxinfo),
325                     sizeof(*vectp));
326         }
327
328         /*
329          * Allocate room for the argv[] and env vectors including the
330          * terminating NULL pointers.
331          */
332         vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
333
334         /* vectp also becomes our initial stack base. */
335         stack_base = (register_t *)vectp;
336
337         stringp = imgp->args->begin_argv;
338         argc = imgp->args->argc;
339         envc = imgp->args->envc;
340
341         /* Copy out strings - arguments and environment. */
342         copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
343
344         /* Fill in "ps_strings" struct for ps, w, etc. */
345         suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
346         suword(&arginfo->ps_nargvstr, argc);
347
348         /* Fill in argument portion of vector table. */
349         for (; argc > 0; --argc) {
350                 suword(vectp++, (long)(intptr_t)destp);
351                 while (*stringp++ != 0)
352                         destp++;
353                 destp++;
354         }
355
356         /* A null vector table pointer separates the argp's from the envp's. */
357         suword(vectp++, 0);
358
359         suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
360         suword(&arginfo->ps_nenvstr, envc);
361
362         /* Fill in environment portion of vector table. */
363         for (; envc > 0; --envc) {
364                 suword(vectp++, (long)(intptr_t)destp);
365                 while (*stringp++ != 0)
366                         destp++;
367                 destp++;
368         }
369
370         /* The end of the vector table is a null pointer. */
371         suword(vectp, 0);
372
373         return (stack_base);
374 }
375
376 static void
377 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
378 {
379         struct thread *td = curthread;
380         struct proc *p = td->td_proc;
381         struct sigacts *psp;
382         struct trapframe *regs;
383         struct l_rt_sigframe *fp, frame;
384         int sig, code;
385         int oonstack;
386
387         sig = ksi->ksi_signo;
388         code = ksi->ksi_code;
389         PROC_LOCK_ASSERT(p, MA_OWNED);
390         psp = p->p_sigacts;
391         mtx_assert(&psp->ps_mtx, MA_OWNED);
392         regs = td->td_frame;
393         oonstack = sigonstack(regs->tf_esp);
394
395 #ifdef DEBUG
396         if (ldebug(rt_sendsig))
397                 printf(ARGS(rt_sendsig, "%p, %d, %p, %u"),
398                     catcher, sig, (void*)mask, code);
399 #endif
400         /* Allocate space for the signal handler context. */
401         if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
402             SIGISMEMBER(psp->ps_sigonstack, sig)) {
403                 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
404                     td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
405         } else
406                 fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
407         mtx_unlock(&psp->ps_mtx);
408
409         /* Build the argument list for the signal handler. */
410         sig = bsd_to_linux_signal(sig);
411
412         bzero(&frame, sizeof(frame));
413
414         frame.sf_handler = catcher;
415         frame.sf_sig = sig;
416         frame.sf_siginfo = &fp->sf_si;
417         frame.sf_ucontext = &fp->sf_sc;
418
419         /* Fill in POSIX parts. */
420         ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
421
422         /* Build the signal context to be used by sigreturn. */
423         frame.sf_sc.uc_flags = 0;               /* XXX ??? */
424         frame.sf_sc.uc_link = NULL;             /* XXX ??? */
425
426         frame.sf_sc.uc_stack.ss_sp = td->td_sigstk.ss_sp;
427         frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
428         frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
429             ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
430         PROC_UNLOCK(p);
431
432         bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
433
434         frame.sf_sc.uc_mcontext.sc_mask   = frame.sf_sc.uc_sigmask.__mask;
435         frame.sf_sc.uc_mcontext.sc_gs     = rgs();
436         frame.sf_sc.uc_mcontext.sc_fs     = regs->tf_fs;
437         frame.sf_sc.uc_mcontext.sc_es     = regs->tf_es;
438         frame.sf_sc.uc_mcontext.sc_ds     = regs->tf_ds;
439         frame.sf_sc.uc_mcontext.sc_edi    = regs->tf_edi;
440         frame.sf_sc.uc_mcontext.sc_esi    = regs->tf_esi;
441         frame.sf_sc.uc_mcontext.sc_ebp    = regs->tf_ebp;
442         frame.sf_sc.uc_mcontext.sc_ebx    = regs->tf_ebx;
443         frame.sf_sc.uc_mcontext.sc_esp    = regs->tf_esp;
444         frame.sf_sc.uc_mcontext.sc_edx    = regs->tf_edx;
445         frame.sf_sc.uc_mcontext.sc_ecx    = regs->tf_ecx;
446         frame.sf_sc.uc_mcontext.sc_eax    = regs->tf_eax;
447         frame.sf_sc.uc_mcontext.sc_eip    = regs->tf_eip;
448         frame.sf_sc.uc_mcontext.sc_cs     = regs->tf_cs;
449         frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_eflags;
450         frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
451         frame.sf_sc.uc_mcontext.sc_ss     = regs->tf_ss;
452         frame.sf_sc.uc_mcontext.sc_err    = regs->tf_err;
453         frame.sf_sc.uc_mcontext.sc_cr2    = (register_t)ksi->ksi_addr;
454         frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
455
456 #ifdef DEBUG
457         if (ldebug(rt_sendsig))
458                 printf(LMSG("rt_sendsig flags: 0x%x, sp: %p, ss: 0x%x, mask: 0x%x"),
459                     frame.sf_sc.uc_stack.ss_flags, td->td_sigstk.ss_sp,
460                     td->td_sigstk.ss_size, frame.sf_sc.uc_mcontext.sc_mask);
461 #endif
462
463         if (copyout(&frame, fp, sizeof(frame)) != 0) {
464                 /*
465                  * Process has trashed its stack; give it an illegal
466                  * instruction to halt it in its tracks.
467                  */
468 #ifdef DEBUG
469                 if (ldebug(rt_sendsig))
470                         printf(LMSG("rt_sendsig: bad stack %p, oonstack=%x"),
471                             fp, oonstack);
472 #endif
473                 PROC_LOCK(p);
474                 sigexit(td, SIGILL);
475         }
476
477         /* Build context to run handler in. */
478         regs->tf_esp = (int)fp;
479         regs->tf_eip = linux_rt_sigcode;
480         regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
481         regs->tf_cs = _ucodesel;
482         regs->tf_ds = _udatasel;
483         regs->tf_es = _udatasel;
484         regs->tf_fs = _udatasel;
485         regs->tf_ss = _udatasel;
486         PROC_LOCK(p);
487         mtx_lock(&psp->ps_mtx);
488 }
489
490
491 /*
492  * Send an interrupt to process.
493  *
494  * Stack is set up to allow sigcode stored
495  * in u. to call routine, followed by kcall
496  * to sigreturn routine below.  After sigreturn
497  * resets the signal mask, the stack, and the
498  * frame pointer, it returns to the user
499  * specified pc, psl.
500  */
501 static void
502 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
503 {
504         struct thread *td = curthread;
505         struct proc *p = td->td_proc;
506         struct sigacts *psp;
507         struct trapframe *regs;
508         struct l_sigframe *fp, frame;
509         l_sigset_t lmask;
510         int sig, code;
511         int oonstack;
512
513         PROC_LOCK_ASSERT(p, MA_OWNED);
514         psp = p->p_sigacts;
515         sig = ksi->ksi_signo;
516         code = ksi->ksi_code;
517         mtx_assert(&psp->ps_mtx, MA_OWNED);
518         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
519                 /* Signal handler installed with SA_SIGINFO. */
520                 linux_rt_sendsig(catcher, ksi, mask);
521                 return;
522         }
523         regs = td->td_frame;
524         oonstack = sigonstack(regs->tf_esp);
525
526 #ifdef DEBUG
527         if (ldebug(sendsig))
528                 printf(ARGS(sendsig, "%p, %d, %p, %u"),
529                     catcher, sig, (void*)mask, code);
530 #endif
531
532         /* Allocate space for the signal handler context. */
533         if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
534             SIGISMEMBER(psp->ps_sigonstack, sig)) {
535                 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
536                     td->td_sigstk.ss_size - sizeof(struct l_sigframe));
537         } else
538                 fp = (struct l_sigframe *)regs->tf_esp - 1;
539         mtx_unlock(&psp->ps_mtx);
540         PROC_UNLOCK(p);
541
542         /* Build the argument list for the signal handler. */
543         sig = bsd_to_linux_signal(sig);
544
545         bzero(&frame, sizeof(frame));
546
547         frame.sf_handler = catcher;
548         frame.sf_sig = sig;
549
550         bsd_to_linux_sigset(mask, &lmask);
551
552         /* Build the signal context to be used by sigreturn. */
553         frame.sf_sc.sc_mask   = lmask.__mask;
554         frame.sf_sc.sc_gs     = rgs();
555         frame.sf_sc.sc_fs     = regs->tf_fs;
556         frame.sf_sc.sc_es     = regs->tf_es;
557         frame.sf_sc.sc_ds     = regs->tf_ds;
558         frame.sf_sc.sc_edi    = regs->tf_edi;
559         frame.sf_sc.sc_esi    = regs->tf_esi;
560         frame.sf_sc.sc_ebp    = regs->tf_ebp;
561         frame.sf_sc.sc_ebx    = regs->tf_ebx;
562         frame.sf_sc.sc_esp    = regs->tf_esp;
563         frame.sf_sc.sc_edx    = regs->tf_edx;
564         frame.sf_sc.sc_ecx    = regs->tf_ecx;
565         frame.sf_sc.sc_eax    = regs->tf_eax;
566         frame.sf_sc.sc_eip    = regs->tf_eip;
567         frame.sf_sc.sc_cs     = regs->tf_cs;
568         frame.sf_sc.sc_eflags = regs->tf_eflags;
569         frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
570         frame.sf_sc.sc_ss     = regs->tf_ss;
571         frame.sf_sc.sc_err    = regs->tf_err;
572         frame.sf_sc.sc_cr2    = (register_t)ksi->ksi_addr;
573         frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno);
574
575         frame.sf_extramask[0] = lmask.__mask;
576
577         if (copyout(&frame, fp, sizeof(frame)) != 0) {
578                 /*
579                  * Process has trashed its stack; give it an illegal
580                  * instruction to halt it in its tracks.
581                  */
582                 PROC_LOCK(p);
583                 sigexit(td, SIGILL);
584         }
585
586         /* Build context to run handler in. */
587         regs->tf_esp = (int)fp;
588         regs->tf_eip = linux_sigcode;
589         regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
590         regs->tf_cs = _ucodesel;
591         regs->tf_ds = _udatasel;
592         regs->tf_es = _udatasel;
593         regs->tf_fs = _udatasel;
594         regs->tf_ss = _udatasel;
595         PROC_LOCK(p);
596         mtx_lock(&psp->ps_mtx);
597 }
598
599 /*
600  * System call to cleanup state after a signal
601  * has been taken.  Reset signal mask and
602  * stack state from context left by sendsig (above).
603  * Return to previous pc and psl as specified by
604  * context left by sendsig. Check carefully to
605  * make sure that the user has not modified the
606  * psl to gain improper privileges or to cause
607  * a machine fault.
608  */
609 int
610 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
611 {
612         struct l_sigframe frame;
613         struct trapframe *regs;
614         l_sigset_t lmask;
615         sigset_t bmask;
616         int eflags;
617         ksiginfo_t ksi;
618
619         regs = td->td_frame;
620
621 #ifdef DEBUG
622         if (ldebug(sigreturn))
623                 printf(ARGS(sigreturn, "%p"), (void *)args->sfp);
624 #endif
625         /*
626          * The trampoline code hands us the sigframe.
627          * It is unsafe to keep track of it ourselves, in the event that a
628          * program jumps out of a signal handler.
629          */
630         if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
631                 return (EFAULT);
632
633         /* Check for security violations. */
634 #define EFLAGS_SECURE(ef, oef)  ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
635         eflags = frame.sf_sc.sc_eflags;
636         if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
637                 return (EINVAL);
638
639         /*
640          * Don't allow users to load a valid privileged %cs.  Let the
641          * hardware check for invalid selectors, excess privilege in
642          * other selectors, invalid %eip's and invalid %esp's.
643          */
644 #define CS_SECURE(cs)   (ISPL(cs) == SEL_UPL)
645         if (!CS_SECURE(frame.sf_sc.sc_cs)) {
646                 ksiginfo_init_trap(&ksi);
647                 ksi.ksi_signo = SIGBUS;
648                 ksi.ksi_code = BUS_OBJERR;
649                 ksi.ksi_trapno = T_PROTFLT;
650                 ksi.ksi_addr = (void *)regs->tf_eip;
651                 trapsignal(td, &ksi);
652                 return (EINVAL);
653         }
654
655         lmask.__mask = frame.sf_sc.sc_mask;
656         linux_to_bsd_sigset(&lmask, &bmask);
657         kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
658
659         /* Restore signal context. */
660         /* %gs was restored by the trampoline. */
661         regs->tf_fs     = frame.sf_sc.sc_fs;
662         regs->tf_es     = frame.sf_sc.sc_es;
663         regs->tf_ds     = frame.sf_sc.sc_ds;
664         regs->tf_edi    = frame.sf_sc.sc_edi;
665         regs->tf_esi    = frame.sf_sc.sc_esi;
666         regs->tf_ebp    = frame.sf_sc.sc_ebp;
667         regs->tf_ebx    = frame.sf_sc.sc_ebx;
668         regs->tf_edx    = frame.sf_sc.sc_edx;
669         regs->tf_ecx    = frame.sf_sc.sc_ecx;
670         regs->tf_eax    = frame.sf_sc.sc_eax;
671         regs->tf_eip    = frame.sf_sc.sc_eip;
672         regs->tf_cs     = frame.sf_sc.sc_cs;
673         regs->tf_eflags = eflags;
674         regs->tf_esp    = frame.sf_sc.sc_esp_at_signal;
675         regs->tf_ss     = frame.sf_sc.sc_ss;
676
677         return (EJUSTRETURN);
678 }
679
680 /*
681  * System call to cleanup state after a signal
682  * has been taken.  Reset signal mask and
683  * stack state from context left by rt_sendsig (above).
684  * Return to previous pc and psl as specified by
685  * context left by sendsig. Check carefully to
686  * make sure that the user has not modified the
687  * psl to gain improper privileges or to cause
688  * a machine fault.
689  */
690 int
691 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
692 {
693         struct l_ucontext uc;
694         struct l_sigcontext *context;
695         sigset_t bmask;
696         l_stack_t *lss;
697         stack_t ss;
698         struct trapframe *regs;
699         int eflags;
700         ksiginfo_t ksi;
701
702         regs = td->td_frame;
703
704 #ifdef DEBUG
705         if (ldebug(rt_sigreturn))
706                 printf(ARGS(rt_sigreturn, "%p"), (void *)args->ucp);
707 #endif
708         /*
709          * The trampoline code hands us the ucontext.
710          * It is unsafe to keep track of it ourselves, in the event that a
711          * program jumps out of a signal handler.
712          */
713         if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
714                 return (EFAULT);
715
716         context = &uc.uc_mcontext;
717
718         /* Check for security violations. */
719 #define EFLAGS_SECURE(ef, oef)  ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
720         eflags = context->sc_eflags;
721         if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
722                 return (EINVAL);
723
724         /*
725          * Don't allow users to load a valid privileged %cs.  Let the
726          * hardware check for invalid selectors, excess privilege in
727          * other selectors, invalid %eip's and invalid %esp's.
728          */
729 #define CS_SECURE(cs)   (ISPL(cs) == SEL_UPL)
730         if (!CS_SECURE(context->sc_cs)) {
731                 ksiginfo_init_trap(&ksi);
732                 ksi.ksi_signo = SIGBUS;
733                 ksi.ksi_code = BUS_OBJERR;
734                 ksi.ksi_trapno = T_PROTFLT;
735                 ksi.ksi_addr = (void *)regs->tf_eip;
736                 trapsignal(td, &ksi);
737                 return (EINVAL);
738         }
739
740         linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
741         kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
742
743         /* Restore signal context. */
744         /* %gs was restored by the trampoline. */
745         regs->tf_fs     = context->sc_fs;
746         regs->tf_es     = context->sc_es;
747         regs->tf_ds     = context->sc_ds;
748         regs->tf_edi    = context->sc_edi;
749         regs->tf_esi    = context->sc_esi;
750         regs->tf_ebp    = context->sc_ebp;
751         regs->tf_ebx    = context->sc_ebx;
752         regs->tf_edx    = context->sc_edx;
753         regs->tf_ecx    = context->sc_ecx;
754         regs->tf_eax    = context->sc_eax;
755         regs->tf_eip    = context->sc_eip;
756         regs->tf_cs     = context->sc_cs;
757         regs->tf_eflags = eflags;
758         regs->tf_esp    = context->sc_esp_at_signal;
759         regs->tf_ss     = context->sc_ss;
760
761         /* Call sigaltstack & ignore results. */
762         lss = &uc.uc_stack;
763         ss.ss_sp = lss->ss_sp;
764         ss.ss_size = lss->ss_size;
765         ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
766
767 #ifdef DEBUG
768         if (ldebug(rt_sigreturn))
769                 printf(LMSG("rt_sigret flags: 0x%x, sp: %p, ss: 0x%x, mask: 0x%x"),
770                     ss.ss_flags, ss.ss_sp, ss.ss_size, context->sc_mask);
771 #endif
772         (void)kern_sigaltstack(td, &ss, NULL);
773
774         return (EJUSTRETURN);
775 }
776
777 static int
778 linux_fetch_syscall_args(struct thread *td)
779 {
780         struct proc *p;
781         struct trapframe *frame;
782         struct syscall_args *sa;
783
784         p = td->td_proc;
785         frame = td->td_frame;
786         sa = &td->td_sa;
787
788         sa->code = frame->tf_eax;
789         sa->args[0] = frame->tf_ebx;
790         sa->args[1] = frame->tf_ecx;
791         sa->args[2] = frame->tf_edx;
792         sa->args[3] = frame->tf_esi;
793         sa->args[4] = frame->tf_edi;
794         sa->args[5] = frame->tf_ebp;    /* Unconfirmed */
795
796         if (sa->code >= p->p_sysent->sv_size)
797                 /* nosys */
798                 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
799         else
800                 sa->callp = &p->p_sysent->sv_table[sa->code];
801         sa->narg = sa->callp->sy_narg;
802
803         td->td_retval[0] = 0;
804         td->td_retval[1] = frame->tf_edx;
805
806         return (0);
807 }
808
809 /*
810  * If a Linux binary is exec'ing something, try this image activator
811  * first.  We override standard shell script execution in order to
812  * be able to modify the interpreter path.  We only do this if a Linux
813  * binary is doing the exec, so we do not create an EXEC module for it.
814  */
815 static int
816 linux_exec_imgact_try(struct image_params *imgp)
817 {
818         const char *head = (const char *)imgp->image_header;
819         char *rpath;
820         int error = -1;
821
822         /*
823          * The interpreter for shell scripts run from a Linux binary needs
824          * to be located in /compat/linux if possible in order to recursively
825          * maintain Linux path emulation.
826          */
827         if (((const short *)head)[0] == SHELLMAGIC) {
828                 /*
829                  * Run our normal shell image activator.  If it succeeds then
830                  * attempt to use the alternate path for the interpreter.  If
831                  * an alternate path is found, use our stringspace to store it.
832                  */
833                 if ((error = exec_shell_imgact(imgp)) == 0) {
834                         linux_emul_convpath(FIRST_THREAD_IN_PROC(imgp->proc),
835                             imgp->interpreter_name, UIO_SYSSPACE, &rpath, 0,
836                             AT_FDCWD);
837                         if (rpath != NULL)
838                                 imgp->args->fname_buf =
839                                     imgp->interpreter_name = rpath;
840                 }
841         }
842         return (error);
843 }
844
845 /*
846  * exec_setregs may initialize some registers differently than Linux
847  * does, thus potentially confusing Linux binaries. If necessary, we
848  * override the exec_setregs default(s) here.
849  */
850 static void
851 linux_exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
852 {
853         struct pcb *pcb = td->td_pcb;
854
855         exec_setregs(td, imgp, stack);
856
857         /* Linux sets %gs to 0, we default to _udatasel. */
858         pcb->pcb_gs = 0;
859         load_gs(0);
860
861         pcb->pcb_initial_npxcw = __LINUX_NPXCW__;
862 }
863
864 static void
865 linux_get_machine(const char **dst)
866 {
867
868         switch (cpu_class) {
869         case CPUCLASS_686:
870                 *dst = "i686";
871                 break;
872         case CPUCLASS_586:
873                 *dst = "i586";
874                 break;
875         case CPUCLASS_486:
876                 *dst = "i486";
877                 break;
878         default:
879                 *dst = "i386";
880         }
881 }
882
883 struct sysentvec linux_sysvec = {
884         .sv_size        = LINUX_SYS_MAXSYSCALL,
885         .sv_table       = linux_sysent,
886         .sv_mask        = 0,
887         .sv_errsize     = ELAST + 1,
888         .sv_errtbl      = linux_errtbl,
889         .sv_transtrap   = linux_translate_traps,
890         .sv_fixup       = linux_fixup,
891         .sv_sendsig     = linux_sendsig,
892         .sv_sigcode     = &_binary_linux_locore_o_start,
893         .sv_szsigcode   = &linux_szsigcode,
894         .sv_name        = "Linux a.out",
895         .sv_coredump    = NULL,
896         .sv_imgact_try  = linux_exec_imgact_try,
897         .sv_minsigstksz = LINUX_MINSIGSTKSZ,
898         .sv_pagesize    = PAGE_SIZE,
899         .sv_minuser     = VM_MIN_ADDRESS,
900         .sv_maxuser     = VM_MAXUSER_ADDRESS,
901         .sv_usrstack    = LINUX_USRSTACK,
902         .sv_psstrings   = PS_STRINGS,
903         .sv_stackprot   = VM_PROT_ALL,
904         .sv_copyout_strings = exec_copyout_strings,
905         .sv_setregs     = linux_exec_setregs,
906         .sv_fixlimit    = NULL,
907         .sv_maxssiz     = NULL,
908         .sv_flags       = SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32,
909         .sv_set_syscall_retval = cpu_set_syscall_retval,
910         .sv_fetch_syscall_args = linux_fetch_syscall_args,
911         .sv_syscallnames = NULL,
912         .sv_shared_page_base = LINUX_SHAREDPAGE,
913         .sv_shared_page_len = PAGE_SIZE,
914         .sv_schedtail   = linux_schedtail,
915         .sv_thread_detach = linux_thread_detach,
916         .sv_trap        = NULL,
917 };
918 INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
919
920 struct sysentvec elf_linux_sysvec = {
921         .sv_size        = LINUX_SYS_MAXSYSCALL,
922         .sv_table       = linux_sysent,
923         .sv_mask        = 0,
924         .sv_errsize     = ELAST + 1,
925         .sv_errtbl      = linux_errtbl,
926         .sv_transtrap   = linux_translate_traps,
927         .sv_fixup       = linux_fixup_elf,
928         .sv_sendsig     = linux_sendsig,
929         .sv_sigcode     = &_binary_linux_locore_o_start,
930         .sv_szsigcode   = &linux_szsigcode,
931         .sv_name        = "Linux ELF",
932         .sv_coredump    = elf32_coredump,
933         .sv_imgact_try  = linux_exec_imgact_try,
934         .sv_minsigstksz = LINUX_MINSIGSTKSZ,
935         .sv_pagesize    = PAGE_SIZE,
936         .sv_minuser     = VM_MIN_ADDRESS,
937         .sv_maxuser     = VM_MAXUSER_ADDRESS,
938         .sv_usrstack    = LINUX_USRSTACK,
939         .sv_psstrings   = LINUX_PS_STRINGS,
940         .sv_stackprot   = VM_PROT_ALL,
941         .sv_copyout_strings = linux_copyout_strings,
942         .sv_setregs     = linux_exec_setregs,
943         .sv_fixlimit    = NULL,
944         .sv_maxssiz     = NULL,
945         .sv_flags       = SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP,
946         .sv_set_syscall_retval = cpu_set_syscall_retval,
947         .sv_fetch_syscall_args = linux_fetch_syscall_args,
948         .sv_syscallnames = NULL,
949         .sv_shared_page_base = LINUX_SHAREDPAGE,
950         .sv_shared_page_len = PAGE_SIZE,
951         .sv_schedtail   = linux_schedtail,
952         .sv_thread_detach = linux_thread_detach,
953         .sv_trap        = NULL,
954 };
955
956 static void
957 linux_vdso_install(void *param)
958 {
959
960         linux_szsigcode = (&_binary_linux_locore_o_end -
961             &_binary_linux_locore_o_start);
962
963         if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
964                 panic("Linux invalid vdso size\n");
965
966         __elfN(linux_vdso_fixup)(&elf_linux_sysvec);
967
968         linux_shared_page_obj = __elfN(linux_shared_page_init)
969             (&linux_shared_page_mapping);
970
971         __elfN(linux_vdso_reloc)(&elf_linux_sysvec);
972
973         bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
974             linux_szsigcode);
975         elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
976 }
977 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
978     (sysinit_cfunc_t)linux_vdso_install, NULL);
979
980 static void
981 linux_vdso_deinstall(void *param)
982 {
983
984         __elfN(linux_shared_page_fini)(linux_shared_page_obj);
985 }
986 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
987     (sysinit_cfunc_t)linux_vdso_deinstall, NULL);
988
989 static char GNU_ABI_VENDOR[] = "GNU";
990 static int GNULINUX_ABI_DESC = 0;
991
992 static bool
993 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
994 {
995         const Elf32_Word *desc;
996         uintptr_t p;
997
998         p = (uintptr_t)(note + 1);
999         p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
1000
1001         desc = (const Elf32_Word *)p;
1002         if (desc[0] != GNULINUX_ABI_DESC)
1003                 return (false);
1004
1005         /*
1006          * For Linux we encode osrel as follows (see linux_mib.c):
1007          * VVVMMMIII (version, major, minor), see linux_mib.c.
1008          */
1009         *osrel = desc[1] * 1000000 + desc[2] * 1000 + desc[3];
1010
1011         return (true);
1012 }
1013
1014 static Elf_Brandnote linux_brandnote = {
1015         .hdr.n_namesz   = sizeof(GNU_ABI_VENDOR),
1016         .hdr.n_descsz   = 16,   /* XXX at least 16 */
1017         .hdr.n_type     = 1,
1018         .vendor         = GNU_ABI_VENDOR,
1019         .flags          = BN_TRANSLATE_OSREL,
1020         .trans_osrel    = linux_trans_osrel
1021 };
1022
1023 static Elf32_Brandinfo linux_brand = {
1024         .brand          = ELFOSABI_LINUX,
1025         .machine        = EM_386,
1026         .compat_3_brand = "Linux",
1027         .emul_path      = "/compat/linux",
1028         .interp_path    = "/lib/ld-linux.so.1",
1029         .sysvec         = &elf_linux_sysvec,
1030         .interp_newpath = NULL,
1031         .brand_note     = &linux_brandnote,
1032         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1033 };
1034
1035 static Elf32_Brandinfo linux_glibc2brand = {
1036         .brand          = ELFOSABI_LINUX,
1037         .machine        = EM_386,
1038         .compat_3_brand = "Linux",
1039         .emul_path      = "/compat/linux",
1040         .interp_path    = "/lib/ld-linux.so.2",
1041         .sysvec         = &elf_linux_sysvec,
1042         .interp_newpath = NULL,
1043         .brand_note     = &linux_brandnote,
1044         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1045 };
1046
1047 static Elf32_Brandinfo linux_muslbrand = {
1048         .brand          = ELFOSABI_LINUX,
1049         .machine        = EM_386,
1050         .compat_3_brand = "Linux",
1051         .emul_path      = "/compat/linux",
1052         .interp_path    = "/lib/ld-musl-i386.so.1",
1053         .sysvec         = &elf_linux_sysvec,
1054         .interp_newpath = NULL,
1055         .brand_note     = &linux_brandnote,
1056         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1057 };
1058
1059 Elf32_Brandinfo *linux_brandlist[] = {
1060         &linux_brand,
1061         &linux_glibc2brand,
1062         &linux_muslbrand,
1063         NULL
1064 };
1065
1066 static int
1067 linux_elf_modevent(module_t mod, int type, void *data)
1068 {
1069         Elf32_Brandinfo **brandinfo;
1070         int error;
1071         struct linux_ioctl_handler **lihp;
1072
1073         error = 0;
1074
1075         switch(type) {
1076         case MOD_LOAD:
1077                 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1078                      ++brandinfo)
1079                         if (elf32_insert_brand_entry(*brandinfo) < 0)
1080                                 error = EINVAL;
1081                 if (error == 0) {
1082                         SET_FOREACH(lihp, linux_ioctl_handler_set)
1083                                 linux_ioctl_register_handler(*lihp);
1084                         LIST_INIT(&futex_list);
1085                         mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF);
1086                         linux_exit_tag = EVENTHANDLER_REGISTER(process_exit, linux_proc_exit,
1087                               NULL, 1000);
1088                         linux_exec_tag = EVENTHANDLER_REGISTER(process_exec, linux_proc_exec,
1089                               NULL, 1000);
1090                         linux_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor,
1091                             linux_thread_dtor, NULL, EVENTHANDLER_PRI_ANY);
1092                         linux_get_machine(&linux_kplatform);
1093                         linux_szplatform = roundup(strlen(linux_kplatform) + 1,
1094                             sizeof(char *));
1095                         linux_osd_jail_register();
1096                         stclohz = (stathz ? stathz : hz);
1097                         if (bootverbose)
1098                                 printf("Linux ELF exec handler installed\n");
1099                 } else
1100                         printf("cannot insert Linux ELF brand handler\n");
1101                 break;
1102         case MOD_UNLOAD:
1103                 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1104                      ++brandinfo)
1105                         if (elf32_brand_inuse(*brandinfo))
1106                                 error = EBUSY;
1107                 if (error == 0) {
1108                         for (brandinfo = &linux_brandlist[0];
1109                              *brandinfo != NULL; ++brandinfo)
1110                                 if (elf32_remove_brand_entry(*brandinfo) < 0)
1111                                         error = EINVAL;
1112                 }
1113                 if (error == 0) {
1114                         SET_FOREACH(lihp, linux_ioctl_handler_set)
1115                                 linux_ioctl_unregister_handler(*lihp);
1116                         mtx_destroy(&futex_mtx);
1117                         EVENTHANDLER_DEREGISTER(process_exit, linux_exit_tag);
1118                         EVENTHANDLER_DEREGISTER(process_exec, linux_exec_tag);
1119                         EVENTHANDLER_DEREGISTER(thread_dtor, linux_thread_dtor_tag);
1120                         linux_osd_jail_deregister();
1121                         if (bootverbose)
1122                                 printf("Linux ELF exec handler removed\n");
1123                 } else
1124                         printf("Could not deinstall ELF interpreter entry\n");
1125                 break;
1126         default:
1127                 return (EOPNOTSUPP);
1128         }
1129         return (error);
1130 }
1131
1132 static moduledata_t linux_elf_mod = {
1133         "linuxelf",
1134         linux_elf_modevent,
1135         0
1136 };
1137
1138 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1139 FEATURE(linux, "Linux 32bit support");