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