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