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