]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/linux32/linux32_sysvec.c
linux(4): Deduplicate linux_fixup_elf().
[FreeBSD/FreeBSD.git] / sys / amd64 / linux32 / linux32_sysvec.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2004 Tim J. Robbins
5  * Copyright (c) 2003 Peter Wemm
6  * Copyright (c) 2002 Doug Rabson
7  * Copyright (c) 1998-1999 Andrew Gallatin
8  * Copyright (c) 1994-1996 Søren Schmidt
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer
16  *    in this position and unchanged.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include "opt_compat.h"
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #ifndef COMPAT_FREEBSD32
41 #error "Unable to compile Linux-emulator due to missing COMPAT_FREEBSD32 option!"
42 #endif
43
44 #define __ELF_WORD_SIZE 32
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/exec.h>
49 #include <sys/fcntl.h>
50 #include <sys/imgact.h>
51 #include <sys/imgact_elf.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/malloc.h>
55 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/proc.h>
58 #include <sys/resourcevar.h>
59 #include <sys/stddef.h>
60 #include <sys/signalvar.h>
61 #include <sys/syscallsubr.h>
62 #include <sys/sysctl.h>
63 #include <sys/sysent.h>
64 #include <sys/sysproto.h>
65 #include <sys/vnode.h>
66 #include <sys/eventhandler.h>
67
68 #include <vm/vm.h>
69 #include <vm/pmap.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_param.h>
75
76 #include <machine/cpu.h>
77 #include <machine/md_var.h>
78 #include <machine/pcb.h>
79 #include <machine/specialreg.h>
80 #include <machine/trap.h>
81
82 #include <x86/linux/linux_x86.h>
83 #include <amd64/linux32/linux.h>
84 #include <amd64/linux32/linux32_proto.h>
85 #include <compat/linux/linux_elf.h>
86 #include <compat/linux/linux_emul.h>
87 #include <compat/linux/linux_fork.h>
88 #include <compat/linux/linux_ioctl.h>
89 #include <compat/linux/linux_mib.h>
90 #include <compat/linux/linux_misc.h>
91 #include <compat/linux/linux_signal.h>
92 #include <compat/linux/linux_util.h>
93 #include <compat/linux/linux_vdso.h>
94
95 #include <x86/linux/linux_x86_sigframe.h>
96
97 MODULE_VERSION(linux, 1);
98
99 #define LINUX32_MAXUSER         ((1ul << 32) - PAGE_SIZE)
100 #define LINUX32_VDSOPAGE_SIZE   PAGE_SIZE * 2
101 #define LINUX32_VDSOPAGE        (LINUX32_MAXUSER - LINUX32_VDSOPAGE_SIZE)
102 #define LINUX32_SHAREDPAGE      (LINUX32_VDSOPAGE - PAGE_SIZE)
103                                 /*
104                                  * PAGE_SIZE - the size
105                                  * of the native SHAREDPAGE
106                                  */
107 #define LINUX32_USRSTACK        LINUX32_SHAREDPAGE
108
109 static int linux_szsigcode;
110 static vm_object_t linux_vdso_obj;
111 static char *linux_vdso_mapping;
112 extern char _binary_linux32_vdso_so_o_start;
113 extern char _binary_linux32_vdso_so_o_end;
114 static vm_offset_t linux_vdso_base;
115
116 extern struct sysent linux32_sysent[LINUX32_SYS_MAXSYSCALL];
117 extern const char *linux32_syscallnames[];
118
119 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
120
121 static int      linux_copyout_strings(struct image_params *imgp,
122                     uintptr_t *stack_base);
123 static void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
124 static void     linux_exec_setregs(struct thread *td,
125                                    struct image_params *imgp, uintptr_t stack);
126 static void     linux_exec_sysvec_init(void *param);
127 static int      linux_on_exec_vmspace(struct proc *p,
128                     struct image_params *imgp);
129 static void     linux32_fixlimit(struct rlimit *rl, int which);
130 static bool     linux32_trans_osrel(const Elf_Note *note, int32_t *osrel);
131 static void     linux_vdso_install(const void *param);
132 static void     linux_vdso_deinstall(const void *param);
133 static void     linux_vdso_reloc(char *mapping, Elf_Addr offset);
134 static void     linux32_set_fork_retval(struct thread *td);
135 static void     linux32_set_syscall_retval(struct thread *td, int error);
136
137 struct linux32_ps_strings {
138         u_int32_t ps_argvstr;   /* first of 0 or more argument strings */
139         u_int ps_nargvstr;      /* the number of argument strings */
140         u_int32_t ps_envstr;    /* first of 0 or more environment strings */
141         u_int ps_nenvstr;       /* the number of environment strings */
142 };
143 #define LINUX32_PS_STRINGS      (LINUX32_USRSTACK - \
144                                     sizeof(struct linux32_ps_strings))
145
146 LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall);
147 LINUX_VDSO_SYM_INTPTR(linux32_vdso_sigcode);
148 LINUX_VDSO_SYM_INTPTR(linux32_vdso_rt_sigcode);
149 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
150 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
151 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
152 LINUX_VDSO_SYM_CHAR(linux_platform);
153
154 static int
155 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
156 {
157         Elf32_Auxargs *args;
158         Elf32_Auxinfo *argarray, *pos;
159         int error, issetugid;
160
161         args = (Elf32_Auxargs *)imgp->auxargs;
162         argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
163             M_WAITOK | M_ZERO);
164
165         issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
166         AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, __kernel_vsyscall);
167         AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
168         AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
169         AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
170
171         /*
172          * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
173          * as it has appeared in the 2.4.0-rc7 first time.
174          * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
175          * glibc falls back to the hard-coded CLK_TCK value when aux entry
176          * is not present.
177          * Also see linux_times() implementation.
178          */
179         if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
180                 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
181         AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
182         AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
183         AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
184         AUXARGS_ENTRY(pos, AT_BASE, args->base);
185         AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
186         AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
187         AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
188         AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
189         AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
190         AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
191         AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
192         AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, PTROUT(imgp->canary));
193         AUXARGS_ENTRY(pos, LINUX_AT_HWCAP2, 0);
194         if (imgp->execpathp != 0)
195                 AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, PTROUT(imgp->execpathp));
196         if (args->execfd != -1)
197                 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
198         AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
199         AUXARGS_ENTRY(pos, AT_NULL, 0);
200
201         free(imgp->auxargs, M_TEMP);
202         imgp->auxargs = NULL;
203         KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
204
205         error = copyout(argarray, (void *)base,
206             sizeof(*argarray) * LINUX_AT_COUNT);
207         free(argarray, M_TEMP);
208         return (error);
209 }
210
211 static void
212 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
213 {
214         struct thread *td = curthread;
215         struct proc *p = td->td_proc;
216         struct sigacts *psp;
217         struct trapframe *regs;
218         struct l_rt_sigframe *fp, frame;
219         int oonstack;
220         int sig;
221         int code;
222
223         sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
224         code = ksi->ksi_code;
225         PROC_LOCK_ASSERT(p, MA_OWNED);
226         psp = p->p_sigacts;
227         mtx_assert(&psp->ps_mtx, MA_OWNED);
228         regs = td->td_frame;
229         oonstack = sigonstack(regs->tf_rsp);
230
231         /* Allocate space for the signal handler context. */
232         if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
233             SIGISMEMBER(psp->ps_sigonstack, sig)) {
234                 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
235                     td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
236         } else
237                 fp = (struct l_rt_sigframe *)regs->tf_rsp - 1;
238         mtx_unlock(&psp->ps_mtx);
239
240         /* Build the argument list for the signal handler. */
241         sig = bsd_to_linux_signal(sig);
242
243         bzero(&frame, sizeof(frame));
244
245         frame.sf_sig = sig;
246         frame.sf_siginfo = PTROUT(&fp->sf_si);
247         frame.sf_ucontext = PTROUT(&fp->sf_uc);
248
249         /* Fill in POSIX parts. */
250         siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
251
252         /*
253          * Build the signal context to be used by sigreturn and libgcc unwind.
254          */
255         frame.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
256         frame.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
257         frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
258             ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
259         PROC_UNLOCK(p);
260
261         bsd_to_linux_sigset(mask, &frame.sf_uc.uc_sigmask);
262
263         frame.sf_uc.uc_mcontext.sc_mask   = frame.sf_uc.uc_sigmask.__mask;
264         frame.sf_uc.uc_mcontext.sc_edi    = regs->tf_rdi;
265         frame.sf_uc.uc_mcontext.sc_esi    = regs->tf_rsi;
266         frame.sf_uc.uc_mcontext.sc_ebp    = regs->tf_rbp;
267         frame.sf_uc.uc_mcontext.sc_ebx    = regs->tf_rbx;
268         frame.sf_uc.uc_mcontext.sc_esp    = regs->tf_rsp;
269         frame.sf_uc.uc_mcontext.sc_edx    = regs->tf_rdx;
270         frame.sf_uc.uc_mcontext.sc_ecx    = regs->tf_rcx;
271         frame.sf_uc.uc_mcontext.sc_eax    = regs->tf_rax;
272         frame.sf_uc.uc_mcontext.sc_eip    = regs->tf_rip;
273         frame.sf_uc.uc_mcontext.sc_cs     = regs->tf_cs;
274         frame.sf_uc.uc_mcontext.sc_gs     = regs->tf_gs;
275         frame.sf_uc.uc_mcontext.sc_fs     = regs->tf_fs;
276         frame.sf_uc.uc_mcontext.sc_es     = regs->tf_es;
277         frame.sf_uc.uc_mcontext.sc_ds     = regs->tf_ds;
278         frame.sf_uc.uc_mcontext.sc_eflags = regs->tf_rflags;
279         frame.sf_uc.uc_mcontext.sc_esp_at_signal = regs->tf_rsp;
280         frame.sf_uc.uc_mcontext.sc_ss     = regs->tf_ss;
281         frame.sf_uc.uc_mcontext.sc_err    = regs->tf_err;
282         frame.sf_uc.uc_mcontext.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
283         frame.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
284
285         if (copyout(&frame, fp, sizeof(frame)) != 0) {
286                 /*
287                  * Process has trashed its stack; give it an illegal
288                  * instruction to halt it in its tracks.
289                  */
290                 PROC_LOCK(p);
291                 sigexit(td, SIGILL);
292         }
293
294         /* Build context to run handler in. */
295         regs->tf_rsp = PTROUT(fp);
296         regs->tf_rip = linux32_vdso_rt_sigcode;
297         regs->tf_rdi = PTROUT(catcher);
298         regs->tf_rflags &= ~(PSL_T | PSL_D);
299         regs->tf_cs = _ucode32sel;
300         regs->tf_ss = _udatasel;
301         regs->tf_ds = _udatasel;
302         regs->tf_es = _udatasel;
303         regs->tf_fs = _ufssel;
304         regs->tf_gs = _ugssel;
305         regs->tf_flags = TF_HASSEGS;
306         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
307         PROC_LOCK(p);
308         mtx_lock(&psp->ps_mtx);
309 }
310
311 /*
312  * Send an interrupt to process.
313  *
314  * Stack is set up to allow sigcode stored
315  * in u. to call routine, followed by kcall
316  * to sigreturn routine below.  After sigreturn
317  * resets the signal mask, the stack, and the
318  * frame pointer, it returns to the user
319  * specified pc, psl.
320  */
321 static void
322 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
323 {
324         struct thread *td = curthread;
325         struct proc *p = td->td_proc;
326         struct sigacts *psp;
327         struct trapframe *regs;
328         struct l_sigframe *fp, frame;
329         l_sigset_t lmask;
330         int oonstack;
331         int sig, code;
332
333         sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
334         code = ksi->ksi_code;
335         PROC_LOCK_ASSERT(p, MA_OWNED);
336         psp = p->p_sigacts;
337         mtx_assert(&psp->ps_mtx, MA_OWNED);
338         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
339                 /* Signal handler installed with SA_SIGINFO. */
340                 linux_rt_sendsig(catcher, ksi, mask);
341                 return;
342         }
343
344         regs = td->td_frame;
345         oonstack = sigonstack(regs->tf_rsp);
346
347         /* Allocate space for the signal handler context. */
348         if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
349             SIGISMEMBER(psp->ps_sigonstack, sig)) {
350                 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
351                     td->td_sigstk.ss_size - sizeof(struct l_sigframe));
352         } else
353                 fp = (struct l_sigframe *)regs->tf_rsp - 1;
354         mtx_unlock(&psp->ps_mtx);
355         PROC_UNLOCK(p);
356
357         /* Build the argument list for the signal handler. */
358         sig = bsd_to_linux_signal(sig);
359
360         bzero(&frame, sizeof(frame));
361
362         frame.sf_sig = sig;
363         frame.sf_sigmask = *mask;
364         bsd_to_linux_sigset(mask, &lmask);
365
366         /* Build the signal context to be used by sigreturn. */
367         frame.sf_sc.sc_mask   = lmask.__mask;
368         frame.sf_sc.sc_gs     = regs->tf_gs;
369         frame.sf_sc.sc_fs     = regs->tf_fs;
370         frame.sf_sc.sc_es     = regs->tf_es;
371         frame.sf_sc.sc_ds     = regs->tf_ds;
372         frame.sf_sc.sc_edi    = regs->tf_rdi;
373         frame.sf_sc.sc_esi    = regs->tf_rsi;
374         frame.sf_sc.sc_ebp    = regs->tf_rbp;
375         frame.sf_sc.sc_ebx    = regs->tf_rbx;
376         frame.sf_sc.sc_esp    = regs->tf_rsp;
377         frame.sf_sc.sc_edx    = regs->tf_rdx;
378         frame.sf_sc.sc_ecx    = regs->tf_rcx;
379         frame.sf_sc.sc_eax    = regs->tf_rax;
380         frame.sf_sc.sc_eip    = regs->tf_rip;
381         frame.sf_sc.sc_cs     = regs->tf_cs;
382         frame.sf_sc.sc_eflags = regs->tf_rflags;
383         frame.sf_sc.sc_esp_at_signal = regs->tf_rsp;
384         frame.sf_sc.sc_ss     = regs->tf_ss;
385         frame.sf_sc.sc_err    = regs->tf_err;
386         frame.sf_sc.sc_cr2    = (u_int32_t)(uintptr_t)ksi->ksi_addr;
387         frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(code);
388
389         if (copyout(&frame, fp, sizeof(frame)) != 0) {
390                 /*
391                  * Process has trashed its stack; give it an illegal
392                  * instruction to halt it in its tracks.
393                  */
394                 PROC_LOCK(p);
395                 sigexit(td, SIGILL);
396         }
397
398         /* Build context to run handler in. */
399         regs->tf_rsp = PTROUT(fp);
400         regs->tf_rip = linux32_vdso_sigcode;
401         regs->tf_rdi = PTROUT(catcher);
402         regs->tf_rflags &= ~(PSL_T | PSL_D);
403         regs->tf_cs = _ucode32sel;
404         regs->tf_ss = _udatasel;
405         regs->tf_ds = _udatasel;
406         regs->tf_es = _udatasel;
407         regs->tf_fs = _ufssel;
408         regs->tf_gs = _ugssel;
409         regs->tf_flags = TF_HASSEGS;
410         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
411         PROC_LOCK(p);
412         mtx_lock(&psp->ps_mtx);
413 }
414
415 /*
416  * System call to cleanup state after a signal
417  * has been taken.  Reset signal mask and
418  * stack state from context left by sendsig (above).
419  * Return to previous pc and psl as specified by
420  * context left by sendsig. Check carefully to
421  * make sure that the user has not modified the
422  * psl to gain improper privileges or to cause
423  * a machine fault.
424  */
425 int
426 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
427 {
428         struct l_sigframe frame;
429         struct trapframe *regs;
430         int eflags;
431         ksiginfo_t ksi;
432
433         regs = td->td_frame;
434
435         /*
436          * The trampoline code hands us the sigframe.
437          * It is unsafe to keep track of it ourselves, in the event that a
438          * program jumps out of a signal handler.
439          */
440         if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
441                 return (EFAULT);
442
443         /* Check for security violations. */
444         eflags = frame.sf_sc.sc_eflags;
445         if (!EFL_SECURE(eflags, regs->tf_rflags))
446                 return(EINVAL);
447
448         /*
449          * Don't allow users to load a valid privileged %cs.  Let the
450          * hardware check for invalid selectors, excess privilege in
451          * other selectors, invalid %eip's and invalid %esp's.
452          */
453         if (!CS_SECURE(frame.sf_sc.sc_cs)) {
454                 ksiginfo_init_trap(&ksi);
455                 ksi.ksi_signo = SIGBUS;
456                 ksi.ksi_code = BUS_OBJERR;
457                 ksi.ksi_trapno = T_PROTFLT;
458                 ksi.ksi_addr = (void *)regs->tf_rip;
459                 trapsignal(td, &ksi);
460                 return(EINVAL);
461         }
462
463         kern_sigprocmask(td, SIG_SETMASK, &frame.sf_sigmask, NULL, 0);
464
465         /* Restore signal context. */
466         regs->tf_rdi    = frame.sf_sc.sc_edi;
467         regs->tf_rsi    = frame.sf_sc.sc_esi;
468         regs->tf_rbp    = frame.sf_sc.sc_ebp;
469         regs->tf_rbx    = frame.sf_sc.sc_ebx;
470         regs->tf_rdx    = frame.sf_sc.sc_edx;
471         regs->tf_rcx    = frame.sf_sc.sc_ecx;
472         regs->tf_rax    = frame.sf_sc.sc_eax;
473         regs->tf_rip    = frame.sf_sc.sc_eip;
474         regs->tf_cs     = frame.sf_sc.sc_cs;
475         regs->tf_ds     = frame.sf_sc.sc_ds;
476         regs->tf_es     = frame.sf_sc.sc_es;
477         regs->tf_fs     = frame.sf_sc.sc_fs;
478         regs->tf_gs     = frame.sf_sc.sc_gs;
479         regs->tf_rflags = eflags;
480         regs->tf_rsp    = frame.sf_sc.sc_esp_at_signal;
481         regs->tf_ss     = frame.sf_sc.sc_ss;
482         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
483
484         return (EJUSTRETURN);
485 }
486
487 /*
488  * System call to cleanup state after a signal
489  * has been taken.  Reset signal mask and
490  * stack state from context left by rt_sendsig (above).
491  * Return to previous pc and psl as specified by
492  * context left by sendsig. Check carefully to
493  * make sure that the user has not modified the
494  * psl to gain improper privileges or to cause
495  * a machine fault.
496  */
497 int
498 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
499 {
500         struct l_ucontext uc;
501         struct l_sigcontext *context;
502         sigset_t bmask;
503         l_stack_t *lss;
504         stack_t ss;
505         struct trapframe *regs;
506         int eflags;
507         ksiginfo_t ksi;
508
509         regs = td->td_frame;
510
511         /*
512          * The trampoline code hands us the ucontext.
513          * It is unsafe to keep track of it ourselves, in the event that a
514          * program jumps out of a signal handler.
515          */
516         if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
517                 return (EFAULT);
518
519         context = &uc.uc_mcontext;
520
521         /* Check for security violations. */
522         eflags = context->sc_eflags;
523         if (!EFL_SECURE(eflags, regs->tf_rflags))
524                 return(EINVAL);
525
526         /*
527          * Don't allow users to load a valid privileged %cs.  Let the
528          * hardware check for invalid selectors, excess privilege in
529          * other selectors, invalid %eip's and invalid %esp's.
530          */
531         if (!CS_SECURE(context->sc_cs)) {
532                 ksiginfo_init_trap(&ksi);
533                 ksi.ksi_signo = SIGBUS;
534                 ksi.ksi_code = BUS_OBJERR;
535                 ksi.ksi_trapno = T_PROTFLT;
536                 ksi.ksi_addr = (void *)regs->tf_rip;
537                 trapsignal(td, &ksi);
538                 return(EINVAL);
539         }
540
541         linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
542         kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
543
544         /*
545          * Restore signal context
546          */
547         regs->tf_gs     = context->sc_gs;
548         regs->tf_fs     = context->sc_fs;
549         regs->tf_es     = context->sc_es;
550         regs->tf_ds     = context->sc_ds;
551         regs->tf_rdi    = context->sc_edi;
552         regs->tf_rsi    = context->sc_esi;
553         regs->tf_rbp    = context->sc_ebp;
554         regs->tf_rbx    = context->sc_ebx;
555         regs->tf_rdx    = context->sc_edx;
556         regs->tf_rcx    = context->sc_ecx;
557         regs->tf_rax    = context->sc_eax;
558         regs->tf_rip    = context->sc_eip;
559         regs->tf_cs     = context->sc_cs;
560         regs->tf_rflags = eflags;
561         regs->tf_rsp    = context->sc_esp_at_signal;
562         regs->tf_ss     = context->sc_ss;
563         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
564
565         /*
566          * call sigaltstack & ignore results..
567          */
568         lss = &uc.uc_stack;
569         ss.ss_sp = PTRIN(lss->ss_sp);
570         ss.ss_size = lss->ss_size;
571         ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
572
573         (void)kern_sigaltstack(td, &ss, NULL);
574
575         return (EJUSTRETURN);
576 }
577
578 static int
579 linux32_fetch_syscall_args(struct thread *td)
580 {
581         struct proc *p;
582         struct trapframe *frame;
583         struct syscall_args *sa;
584
585         p = td->td_proc;
586         frame = td->td_frame;
587         sa = &td->td_sa;
588
589         sa->args[0] = frame->tf_rbx;
590         sa->args[1] = frame->tf_rcx;
591         sa->args[2] = frame->tf_rdx;
592         sa->args[3] = frame->tf_rsi;
593         sa->args[4] = frame->tf_rdi;
594         sa->args[5] = frame->tf_rbp;    /* Unconfirmed */
595         sa->code = frame->tf_rax;
596         sa->original_code = sa->code;
597
598         if (sa->code >= p->p_sysent->sv_size)
599                 /* nosys */
600                 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
601         else
602                 sa->callp = &p->p_sysent->sv_table[sa->code];
603
604         td->td_retval[0] = 0;
605         td->td_retval[1] = frame->tf_rdx;
606
607         return (0);
608 }
609
610 static void
611 linux32_set_syscall_retval(struct thread *td, int error)
612 {
613         struct trapframe *frame = td->td_frame;
614
615         cpu_set_syscall_retval(td, error);
616
617         if (__predict_false(error != 0)) {
618                 if (error != ERESTART && error != EJUSTRETURN)
619                         frame->tf_rax = bsd_to_linux_errno(error);
620         }
621 }
622
623 static void
624 linux32_set_fork_retval(struct thread *td)
625 {
626         struct trapframe *frame = td->td_frame;
627
628         frame->tf_rax = 0;
629 }
630
631 /*
632  * Clear registers on exec
633  * XXX copied from ia32_signal.c.
634  */
635 static void
636 linux_exec_setregs(struct thread *td, struct image_params *imgp,
637     uintptr_t stack)
638 {
639         struct trapframe *regs = td->td_frame;
640         struct pcb *pcb = td->td_pcb;
641         register_t saved_rflags;
642
643         regs = td->td_frame;
644         pcb = td->td_pcb;
645
646         if (td->td_proc->p_md.md_ldt != NULL)
647                 user_ldt_free(td);
648
649         critical_enter();
650         wrmsr(MSR_FSBASE, 0);
651         wrmsr(MSR_KGSBASE, 0);  /* User value while we're in the kernel */
652         pcb->pcb_fsbase = 0;
653         pcb->pcb_gsbase = 0;
654         critical_exit();
655         pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
656
657         saved_rflags = regs->tf_rflags & PSL_T;
658         bzero((char *)regs, sizeof(struct trapframe));
659         regs->tf_rip = imgp->entry_addr;
660         regs->tf_rsp = stack;
661         regs->tf_rflags = PSL_USER | saved_rflags;
662         regs->tf_gs = _ugssel;
663         regs->tf_fs = _ufssel;
664         regs->tf_es = _udatasel;
665         regs->tf_ds = _udatasel;
666         regs->tf_ss = _udatasel;
667         regs->tf_flags = TF_HASSEGS;
668         regs->tf_cs = _ucode32sel;
669         regs->tf_rbx = (register_t)imgp->ps_strings;
670
671         x86_clear_dbregs(pcb);
672
673         fpstate_drop(td);
674
675         /* Do full restore on return so that we can change to a different %cs */
676         set_pcb_flags(pcb, PCB_32BIT | PCB_FULL_IRET);
677 }
678
679 /*
680  * XXX copied from ia32_sysvec.c.
681  */
682 static int
683 linux_copyout_strings(struct image_params *imgp, uintptr_t *stack_base)
684 {
685         int argc, envc, error;
686         u_int32_t *vectp;
687         char *stringp;
688         uintptr_t destp, ustringp;
689         struct linux32_ps_strings *arginfo;
690         char canary[LINUX_AT_RANDOM_LEN];
691         size_t execpath_len;
692
693         arginfo = (struct linux32_ps_strings *)PROC_PS_STRINGS(imgp->proc);
694         destp = (uintptr_t)arginfo;
695
696         if (imgp->execpath != NULL && imgp->auxargs != NULL) {
697                 execpath_len = strlen(imgp->execpath) + 1;
698                 destp -= execpath_len;
699                 destp = rounddown2(destp, sizeof(uint32_t));
700                 imgp->execpathp = (void *)destp;
701                 error = copyout(imgp->execpath, imgp->execpathp, execpath_len);
702                 if (error != 0)
703                         return (error);
704         }
705
706         /* Prepare the canary for SSP. */
707         arc4rand(canary, sizeof(canary), 0);
708         destp -= roundup(sizeof(canary), sizeof(uint32_t));
709         imgp->canary = (void *)destp;
710         error = copyout(canary, imgp->canary, sizeof(canary));
711         if (error != 0)
712                 return (error);
713
714         /* Allocate room for the argument and environment strings. */
715         destp -= ARG_MAX - imgp->args->stringspace;
716         destp = rounddown2(destp, sizeof(uint32_t));
717         ustringp = destp;
718
719         if (imgp->auxargs) {
720                 /*
721                  * Allocate room on the stack for the ELF auxargs
722                  * array.  It has LINUX_AT_COUNT entries.
723                  */
724                 destp -= LINUX_AT_COUNT * sizeof(Elf32_Auxinfo);
725                 destp = rounddown2(destp, sizeof(uint32_t));
726         }
727
728         vectp = (uint32_t *)destp;
729
730         /*
731          * Allocate room for the argv[] and env vectors including the
732          * terminating NULL pointers.
733          */
734         vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
735
736         /* vectp also becomes our initial stack base. */
737         *stack_base = (uintptr_t)vectp;
738
739         stringp = imgp->args->begin_argv;
740         argc = imgp->args->argc;
741         envc = imgp->args->envc;
742
743         /* Copy out strings - arguments and environment. */
744         error = copyout(stringp, (void *)ustringp,
745             ARG_MAX - imgp->args->stringspace);
746         if (error != 0)
747                 return (error);
748
749         /* Fill in "ps_strings" struct for ps, w, etc. */
750         if (suword32(&arginfo->ps_argvstr, (uint32_t)(intptr_t)vectp) != 0 ||
751             suword32(&arginfo->ps_nargvstr, argc) != 0)
752                 return (EFAULT);
753
754         /* Fill in argument portion of vector table. */
755         for (; argc > 0; --argc) {
756                 if (suword32(vectp++, ustringp) != 0)
757                         return (EFAULT);
758                 while (*stringp++ != 0)
759                         ustringp++;
760                 ustringp++;
761         }
762
763         /* A null vector table pointer separates the argp's from the envp's. */
764         if (suword32(vectp++, 0) != 0)
765                 return (EFAULT);
766
767         if (suword32(&arginfo->ps_envstr, (uint32_t)(intptr_t)vectp) != 0 ||
768             suword32(&arginfo->ps_nenvstr, envc) != 0)
769                 return (EFAULT);
770
771         /* Fill in environment portion of vector table. */
772         for (; envc > 0; --envc) {
773                 if (suword32(vectp++, ustringp) != 0)
774                         return (EFAULT);
775                 while (*stringp++ != 0)
776                         ustringp++;
777                 ustringp++;
778         }
779
780         /* The end of the vector table is a null pointer. */
781         if (suword32(vectp, 0) != 0)
782                 return (EFAULT);
783
784         if (imgp->auxargs) {
785                 vectp++;
786                 error = imgp->sysent->sv_copyout_auxargs(imgp,
787                     (uintptr_t)vectp);
788                 if (error != 0)
789                         return (error);
790         }
791
792         return (0);
793 }
794
795 static SYSCTL_NODE(_compat, OID_AUTO, linux32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
796     "32-bit Linux emulation");
797
798 static u_long   linux32_maxdsiz = LINUX32_MAXDSIZ;
799 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxdsiz, CTLFLAG_RW,
800     &linux32_maxdsiz, 0, "");
801 static u_long   linux32_maxssiz = LINUX32_MAXSSIZ;
802 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxssiz, CTLFLAG_RW,
803     &linux32_maxssiz, 0, "");
804 static u_long   linux32_maxvmem = LINUX32_MAXVMEM;
805 SYSCTL_ULONG(_compat_linux32, OID_AUTO, maxvmem, CTLFLAG_RW,
806     &linux32_maxvmem, 0, "");
807 bool linux32_emulate_i386 = false;
808 SYSCTL_BOOL(_compat_linux32, OID_AUTO, emulate_i386, CTLFLAG_RWTUN,
809     &linux32_emulate_i386, 0, "Emulate the real i386");
810
811 static void
812 linux32_fixlimit(struct rlimit *rl, int which)
813 {
814
815         switch (which) {
816         case RLIMIT_DATA:
817                 if (linux32_maxdsiz != 0) {
818                         if (rl->rlim_cur > linux32_maxdsiz)
819                                 rl->rlim_cur = linux32_maxdsiz;
820                         if (rl->rlim_max > linux32_maxdsiz)
821                                 rl->rlim_max = linux32_maxdsiz;
822                 }
823                 break;
824         case RLIMIT_STACK:
825                 if (linux32_maxssiz != 0) {
826                         if (rl->rlim_cur > linux32_maxssiz)
827                                 rl->rlim_cur = linux32_maxssiz;
828                         if (rl->rlim_max > linux32_maxssiz)
829                                 rl->rlim_max = linux32_maxssiz;
830                 }
831                 break;
832         case RLIMIT_VMEM:
833                 if (linux32_maxvmem != 0) {
834                         if (rl->rlim_cur > linux32_maxvmem)
835                                 rl->rlim_cur = linux32_maxvmem;
836                         if (rl->rlim_max > linux32_maxvmem)
837                                 rl->rlim_max = linux32_maxvmem;
838                 }
839                 break;
840         }
841 }
842
843 struct sysentvec elf_linux_sysvec = {
844         .sv_size        = LINUX32_SYS_MAXSYSCALL,
845         .sv_table       = linux32_sysent,
846         .sv_fixup       = elf32_freebsd_fixup,
847         .sv_sendsig     = linux_sendsig,
848         .sv_sigcode     = &_binary_linux32_vdso_so_o_start,
849         .sv_szsigcode   = &linux_szsigcode,
850         .sv_name        = "Linux ELF32",
851         .sv_coredump    = elf32_coredump,
852         .sv_elf_core_osabi = ELFOSABI_NONE,
853         .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
854         .sv_elf_core_prepare_notes = linux32_prepare_notes,
855         .sv_imgact_try  = linux_exec_imgact_try,
856         .sv_minsigstksz = LINUX_MINSIGSTKSZ,
857         .sv_minuser     = VM_MIN_ADDRESS,
858         .sv_maxuser     = LINUX32_MAXUSER,
859         .sv_usrstack    = LINUX32_USRSTACK,
860         .sv_psstrings   = LINUX32_PS_STRINGS,
861         .sv_psstringssz = sizeof(struct linux32_ps_strings),
862         .sv_stackprot   = VM_PROT_ALL,
863         .sv_copyout_auxargs = linux_copyout_auxargs,
864         .sv_copyout_strings = linux_copyout_strings,
865         .sv_setregs     = linux_exec_setregs,
866         .sv_fixlimit    = linux32_fixlimit,
867         .sv_maxssiz     = &linux32_maxssiz,
868         .sv_flags       = SV_ABI_LINUX | SV_ILP32 | SV_IA32 | SV_SHP |
869             SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP,
870         .sv_set_syscall_retval = linux32_set_syscall_retval,
871         .sv_fetch_syscall_args = linux32_fetch_syscall_args,
872         .sv_syscallnames = linux32_syscallnames,
873         .sv_shared_page_base = LINUX32_SHAREDPAGE,
874         .sv_shared_page_len = PAGE_SIZE,
875         .sv_schedtail   = linux_schedtail,
876         .sv_thread_detach = linux_thread_detach,
877         .sv_trap        = NULL,
878         .sv_onexec      = linux_on_exec_vmspace,
879         .sv_onexit      = linux_on_exit,
880         .sv_ontdexit    = linux_thread_dtor,
881         .sv_setid_allowed = &linux_setid_allowed_query,
882         .sv_set_fork_retval = linux32_set_fork_retval,
883 };
884
885 static int
886 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
887 {
888         int error;
889
890         error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
891             LINUX32_VDSOPAGE_SIZE, imgp);
892         if (error == 0)
893                 linux_on_exec(p, imgp);
894         return (error);
895 }
896
897 /*
898  * linux_vdso_install() and linux_exec_sysvec_init() must be called
899  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
900  */
901 static void
902 linux_exec_sysvec_init(void *param)
903 {
904         l_uintptr_t *ktimekeep_base, *ktsc_selector;
905         struct sysentvec *sv;
906         ptrdiff_t tkoff;
907
908         sv = param;
909         /* Fill timekeep_base */
910         exec_sysvec_init(sv);
911
912         tkoff = kern_timekeep_base - linux_vdso_base;
913         ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
914         *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
915
916         tkoff = kern_tsc_selector - linux_vdso_base;
917         ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
918         *ktsc_selector = linux_vdso_tsc_selector_idx();
919         if (bootverbose)
920                 printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector);
921
922         tkoff = kern_cpu_selector - linux_vdso_base;
923         ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
924         *ktsc_selector = linux_vdso_cpu_selector_idx();
925         if (bootverbose)
926                 printf("Linux i386 vDSO cpu_selector: %u\n", *ktsc_selector);
927 }
928 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
929     linux_exec_sysvec_init, &elf_linux_sysvec);
930
931 static void
932 linux_vdso_install(const void *param)
933 {
934         char *vdso_start = &_binary_linux32_vdso_so_o_start;
935         char *vdso_end = &_binary_linux32_vdso_so_o_end;
936
937         linux_szsigcode = vdso_end - vdso_start;
938         MPASS(linux_szsigcode <= LINUX32_VDSOPAGE_SIZE);
939
940         linux_vdso_base = LINUX32_VDSOPAGE;
941
942         __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
943
944         linux_vdso_obj = __elfN(linux_shared_page_init)
945             (&linux_vdso_mapping, LINUX32_VDSOPAGE_SIZE);
946         bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
947
948         linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
949 }
950 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
951     linux_vdso_install, NULL);
952
953 static void
954 linux_vdso_deinstall(const void *param)
955 {
956
957         __elfN(linux_shared_page_fini)(linux_vdso_obj,
958             linux_vdso_mapping, LINUX32_VDSOPAGE_SIZE);
959 }
960 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
961     linux_vdso_deinstall, NULL);
962
963 static void
964 linux_vdso_reloc(char *mapping, Elf_Addr offset)
965 {
966         const Elf_Shdr *shdr;
967         const Elf_Rel *rel;
968         const Elf_Ehdr *ehdr;
969         Elf32_Addr *where;
970         Elf_Size rtype, symidx;
971         Elf32_Addr addr, addend;
972         int i, relcnt;
973
974         MPASS(offset != 0);
975
976         relcnt = 0;
977         ehdr = (const Elf_Ehdr *)mapping;
978         shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
979         for (i = 0; i < ehdr->e_shnum; i++)
980         {
981                 switch (shdr[i].sh_type) {
982                 case SHT_REL:
983                         rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset);
984                         relcnt = shdr[i].sh_size / sizeof(*rel);
985                         break;
986                 case SHT_RELA:
987                         printf("Linux i386 vDSO: unexpected Rela section\n");
988                         break;
989                 }
990         }
991
992         for (i = 0; i < relcnt; i++, rel++) {
993                 where = (Elf32_Addr *)(mapping + rel->r_offset);
994                 addend = *where;
995                 rtype = ELF_R_TYPE(rel->r_info);
996                 symidx = ELF_R_SYM(rel->r_info);
997
998                 switch (rtype) {
999                 case R_386_NONE:        /* none */
1000                         break;
1001
1002                 case R_386_RELATIVE:    /* B + A */
1003                         addr = (Elf32_Addr)PTROUT(offset + addend);
1004                         if (*where != addr)
1005                                 *where = addr;
1006                         break;
1007
1008                 case R_386_IRELATIVE:
1009                         printf("Linux i386 vDSO: unexpected ifunc relocation, "
1010                             "symbol index %ld\n", (intmax_t)symidx);
1011                         break;
1012                 default:
1013                         printf("Linux i386 vDSO: unexpected relocation type %ld, "
1014                             "symbol index %ld\n", (intmax_t)rtype, (intmax_t)symidx);
1015                 }
1016         }
1017 }
1018
1019 static char GNU_ABI_VENDOR[] = "GNU";
1020 static int GNULINUX_ABI_DESC = 0;
1021
1022 static bool
1023 linux32_trans_osrel(const Elf_Note *note, int32_t *osrel)
1024 {
1025         const Elf32_Word *desc;
1026         uintptr_t p;
1027
1028         p = (uintptr_t)(note + 1);
1029         p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
1030
1031         desc = (const Elf32_Word *)p;
1032         if (desc[0] != GNULINUX_ABI_DESC)
1033                 return (false);
1034
1035         /*
1036          * For Linux we encode osrel using the Linux convention of
1037          *      (version << 16) | (major << 8) | (minor)
1038          * See macro in linux_mib.h
1039          */
1040         *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
1041
1042         return (true);
1043 }
1044
1045 static Elf_Brandnote linux32_brandnote = {
1046         .hdr.n_namesz   = sizeof(GNU_ABI_VENDOR),
1047         .hdr.n_descsz   = 16,   /* XXX at least 16 */
1048         .hdr.n_type     = 1,
1049         .vendor         = GNU_ABI_VENDOR,
1050         .flags          = BN_TRANSLATE_OSREL,
1051         .trans_osrel    = linux32_trans_osrel
1052 };
1053
1054 static Elf32_Brandinfo linux_brand = {
1055         .brand          = ELFOSABI_LINUX,
1056         .machine        = EM_386,
1057         .compat_3_brand = "Linux",
1058         .emul_path      = linux_emul_path,
1059         .interp_path    = "/lib/ld-linux.so.1",
1060         .sysvec         = &elf_linux_sysvec,
1061         .interp_newpath = NULL,
1062         .brand_note     = &linux32_brandnote,
1063         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1064 };
1065
1066 static Elf32_Brandinfo linux_glibc2brand = {
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.2",
1072         .sysvec         = &elf_linux_sysvec,
1073         .interp_newpath = NULL,
1074         .brand_note     = &linux32_brandnote,
1075         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
1076 };
1077
1078 static Elf32_Brandinfo linux_muslbrand = {
1079         .brand          = ELFOSABI_LINUX,
1080         .machine        = EM_386,
1081         .compat_3_brand = "Linux",
1082         .emul_path      = linux_emul_path,
1083         .interp_path    = "/lib/ld-musl-i386.so.1",
1084         .sysvec         = &elf_linux_sysvec,
1085         .interp_newpath = NULL,
1086         .brand_note     = &linux32_brandnote,
1087         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
1088                             LINUX_BI_FUTEX_REQUEUE
1089 };
1090
1091 Elf32_Brandinfo *linux_brandlist[] = {
1092         &linux_brand,
1093         &linux_glibc2brand,
1094         &linux_muslbrand,
1095         NULL
1096 };
1097
1098 static int
1099 linux_elf_modevent(module_t mod, int type, void *data)
1100 {
1101         Elf32_Brandinfo **brandinfo;
1102         int error;
1103         struct linux_ioctl_handler **lihp;
1104
1105         error = 0;
1106
1107         switch(type) {
1108         case MOD_LOAD:
1109                 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1110                      ++brandinfo)
1111                         if (elf32_insert_brand_entry(*brandinfo) < 0)
1112                                 error = EINVAL;
1113                 if (error == 0) {
1114                         SET_FOREACH(lihp, linux_ioctl_handler_set)
1115                                 linux32_ioctl_register_handler(*lihp);
1116                         stclohz = (stathz ? stathz : hz);
1117                         if (bootverbose)
1118                                 printf("Linux i386 ELF exec handler installed\n");
1119                 } else
1120                         printf("cannot insert Linux i386 ELF brand handler\n");
1121                 break;
1122         case MOD_UNLOAD:
1123                 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1124                      ++brandinfo)
1125                         if (elf32_brand_inuse(*brandinfo))
1126                                 error = EBUSY;
1127                 if (error == 0) {
1128                         for (brandinfo = &linux_brandlist[0];
1129                              *brandinfo != NULL; ++brandinfo)
1130                                 if (elf32_remove_brand_entry(*brandinfo) < 0)
1131                                         error = EINVAL;
1132                 }
1133                 if (error == 0) {
1134                         SET_FOREACH(lihp, linux_ioctl_handler_set)
1135                                 linux32_ioctl_unregister_handler(*lihp);
1136                         if (bootverbose)
1137                                 printf("Linux i386 ELF exec handler removed\n");
1138                 } else
1139                         printf("Could not deinstall Linux i386 ELF interpreter entry\n");
1140                 break;
1141         default:
1142                 return (EOPNOTSUPP);
1143         }
1144         return (error);
1145 }
1146
1147 static moduledata_t linux_elf_mod = {
1148         "linuxelf",
1149         linux_elf_modevent,
1150         0
1151 };
1152
1153 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1154 MODULE_DEPEND(linuxelf, linux_common, 1, 1, 1);
1155 FEATURE(linux, "Linux 32bit support");