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