xref: /freebsd/sys/i386/linux/linux_sysvec.c (revision 2a58b312)
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 #define __ELF_WORD_SIZE	32
33 
34 #include <sys/param.h>
35 #include <sys/exec.h>
36 #include <sys/fcntl.h>
37 #include <sys/imgact.h>
38 #include <sys/imgact_aout.h>
39 #include <sys/imgact_elf.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/proc.h>
46 #include <sys/stddef.h>
47 #include <sys/syscallsubr.h>
48 #include <sys/sysctl.h>
49 #include <sys/sysent.h>
50 #include <sys/sysproto.h>
51 
52 #include <vm/pmap.h>
53 #include <vm/vm.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_page.h>
56 
57 #include <machine/cpu.h>
58 #include <machine/cputypes.h>
59 #include <machine/md_var.h>
60 #include <machine/pcb.h>
61 #include <machine/trap.h>
62 
63 #include <x86/linux/linux_x86.h>
64 #include <i386/linux/linux.h>
65 #include <i386/linux/linux_proto.h>
66 #include <compat/linux/linux_elf.h>
67 #include <compat/linux/linux_emul.h>
68 #include <compat/linux/linux_fork.h>
69 #include <compat/linux/linux_ioctl.h>
70 #include <compat/linux/linux_mib.h>
71 #include <compat/linux/linux_misc.h>
72 #include <compat/linux/linux_signal.h>
73 #include <compat/linux/linux_util.h>
74 #include <compat/linux/linux_vdso.h>
75 
76 #include <x86/linux/linux_x86_sigframe.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 extern const char *linux_syscallnames[];
99 
100 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
101 
102 static int	linux_fixup(uintptr_t *stack_base,
103 		    struct image_params *iparams);
104 static void     linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
105 static void	linux_exec_setregs(struct thread *td,
106 		    struct image_params *imgp, uintptr_t stack);
107 static void	linux_exec_sysvec_init(void *param);
108 static int	linux_on_exec_vmspace(struct proc *p,
109 		    struct image_params *imgp);
110 static void	linux_set_fork_retval(struct thread *td);
111 static void	linux_vdso_install(const void *param);
112 static void	linux_vdso_deinstall(const void *param);
113 static void	linux_vdso_reloc(char *mapping, Elf_Addr offset);
114 
115 LINUX_VDSO_SYM_CHAR(linux_platform);
116 LINUX_VDSO_SYM_INTPTR(__kernel_vsyscall);
117 LINUX_VDSO_SYM_INTPTR(linux_vdso_sigcode);
118 LINUX_VDSO_SYM_INTPTR(linux_vdso_rt_sigcode);
119 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
120 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
121 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
122 
123 static int
124 linux_fixup(uintptr_t *stack_base, struct image_params *imgp)
125 {
126 	register_t *base, *argv, *envp;
127 
128 	base = (register_t *)*stack_base;
129 	argv = base;
130 	envp = base + (imgp->args->argc + 1);
131 	base--;
132 	suword(base, (intptr_t)envp);
133 	base--;
134 	suword(base, (intptr_t)argv);
135 	base--;
136 	suword(base, imgp->args->argc);
137 	*stack_base = (uintptr_t)base;
138 	return (0);
139 }
140 
141 static int
142 linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
143 {
144 	Elf32_Auxargs *args;
145 	Elf32_Auxinfo *argarray, *pos;
146 	int error, issetugid;
147 
148 	issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
149 	args = (Elf32_Auxargs *)imgp->auxargs;
150 	argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
151 	    M_WAITOK | M_ZERO);
152 
153 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
154 	AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, __kernel_vsyscall);
155 	AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
156 
157 	/*
158 	 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
159 	 * as it has appeared in the 2.4.0-rc7 first time.
160 	 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
161 	 * glibc falls back to the hard-coded CLK_TCK value when aux entry
162 	 * is not present.
163 	 * Also see linux_times() implementation.
164 	 */
165 	if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
166 		AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
167 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
168 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
169 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
170 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
171 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
172 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
173 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
174 	AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
175 	AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
176 	AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
177 	AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
178 	AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
179 	AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(linux_platform));
180 	AUXARGS_ENTRY_PTR(pos, LINUX_AT_RANDOM, imgp->canary);
181 	if (imgp->execpathp != 0)
182 		AUXARGS_ENTRY_PTR(pos, LINUX_AT_EXECFN, imgp->execpathp);
183 	if (args->execfd != -1)
184 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
185 	AUXARGS_ENTRY(pos, AT_NULL, 0);
186 
187 	free(imgp->auxargs, M_TEMP);
188 	imgp->auxargs = NULL;
189 	KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
190 
191 	error = copyout(argarray, (void *)base,
192 	    sizeof(*argarray) * LINUX_AT_COUNT);
193 	free(argarray, M_TEMP);
194 	return (error);
195 }
196 
197 static void
198 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
199 {
200 	struct thread *td = curthread;
201 	struct proc *p = td->td_proc;
202 	struct sigacts *psp;
203 	struct trapframe *regs;
204 	struct l_rt_sigframe *fp, frame;
205 	int sig, code;
206 	int oonstack;
207 
208 	sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
209 	code = ksi->ksi_code;
210 	PROC_LOCK_ASSERT(p, MA_OWNED);
211 	psp = p->p_sigacts;
212 	mtx_assert(&psp->ps_mtx, MA_OWNED);
213 	regs = td->td_frame;
214 	oonstack = sigonstack(regs->tf_esp);
215 
216 	/* Allocate space for the signal handler context. */
217 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
218 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
219 		fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
220 		    td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
221 	} else
222 		fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
223 	mtx_unlock(&psp->ps_mtx);
224 
225 	/* Build the argument list for the signal handler. */
226 	sig = bsd_to_linux_signal(sig);
227 
228 	bzero(&frame, sizeof(frame));
229 
230 	frame.sf_sig = sig;
231 	frame.sf_siginfo = PTROUT(&fp->sf_si);
232 	frame.sf_ucontext = PTROUT(&fp->sf_uc);
233 
234 	/* Fill in POSIX parts. */
235 	siginfo_to_lsiginfo(&ksi->ksi_info, &frame.sf_si, sig);
236 
237 	/* Build the signal context to be used by sigreturn. */
238 	frame.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
239 	frame.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
240 	frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
241 	    ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
242 	PROC_UNLOCK(p);
243 
244 	bsd_to_linux_sigset(mask, &frame.sf_uc.uc_sigmask);
245 
246 	frame.sf_uc.uc_mcontext.sc_mask   = frame.sf_uc.uc_sigmask.__mask;
247 	frame.sf_uc.uc_mcontext.sc_gs     = rgs();
248 	frame.sf_uc.uc_mcontext.sc_fs     = regs->tf_fs;
249 	frame.sf_uc.uc_mcontext.sc_es     = regs->tf_es;
250 	frame.sf_uc.uc_mcontext.sc_ds     = regs->tf_ds;
251 	frame.sf_uc.uc_mcontext.sc_edi    = regs->tf_edi;
252 	frame.sf_uc.uc_mcontext.sc_esi    = regs->tf_esi;
253 	frame.sf_uc.uc_mcontext.sc_ebp    = regs->tf_ebp;
254 	frame.sf_uc.uc_mcontext.sc_ebx    = regs->tf_ebx;
255 	frame.sf_uc.uc_mcontext.sc_esp    = regs->tf_esp;
256 	frame.sf_uc.uc_mcontext.sc_edx    = regs->tf_edx;
257 	frame.sf_uc.uc_mcontext.sc_ecx    = regs->tf_ecx;
258 	frame.sf_uc.uc_mcontext.sc_eax    = regs->tf_eax;
259 	frame.sf_uc.uc_mcontext.sc_eip    = regs->tf_eip;
260 	frame.sf_uc.uc_mcontext.sc_cs     = regs->tf_cs;
261 	frame.sf_uc.uc_mcontext.sc_eflags = regs->tf_eflags;
262 	frame.sf_uc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
263 	frame.sf_uc.uc_mcontext.sc_ss     = regs->tf_ss;
264 	frame.sf_uc.uc_mcontext.sc_err    = regs->tf_err;
265 	frame.sf_uc.uc_mcontext.sc_cr2    = (register_t)ksi->ksi_addr;
266 	frame.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
267 
268 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
269 		/*
270 		 * Process has trashed its stack; give it an illegal
271 		 * instruction to halt it in its tracks.
272 		 */
273 		PROC_LOCK(p);
274 		sigexit(td, SIGILL);
275 	}
276 
277 	/* Build context to run handler in. */
278 	regs->tf_esp = PTROUT(fp);
279 	regs->tf_eip = linux_vdso_rt_sigcode;
280 	regs->tf_edi = PTROUT(catcher);
281 	regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
282 	regs->tf_cs = _ucodesel;
283 	regs->tf_ds = _udatasel;
284 	regs->tf_es = _udatasel;
285 	regs->tf_fs = _udatasel;
286 	regs->tf_ss = _udatasel;
287 	PROC_LOCK(p);
288 	mtx_lock(&psp->ps_mtx);
289 }
290 
291 /*
292  * Send an interrupt to process.
293  *
294  * Stack is set up to allow sigcode stored
295  * in u. to call routine, followed by kcall
296  * to sigreturn routine below.  After sigreturn
297  * resets the signal mask, the stack, and the
298  * frame pointer, it returns to the user
299  * specified pc, psl.
300  */
301 static void
302 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
303 {
304 	struct thread *td = curthread;
305 	struct proc *p = td->td_proc;
306 	struct sigacts *psp;
307 	struct trapframe *regs;
308 	struct l_sigframe *fp, frame;
309 	l_sigset_t lmask;
310 	int sig;
311 	int oonstack;
312 
313 	PROC_LOCK_ASSERT(p, MA_OWNED);
314 	psp = p->p_sigacts;
315 	sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
316 	mtx_assert(&psp->ps_mtx, MA_OWNED);
317 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
318 		/* Signal handler installed with SA_SIGINFO. */
319 		linux_rt_sendsig(catcher, ksi, mask);
320 		return;
321 	}
322 	regs = td->td_frame;
323 	oonstack = sigonstack(regs->tf_esp);
324 
325 	/* Allocate space for the signal handler context. */
326 	if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
327 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
328 		fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
329 		    td->td_sigstk.ss_size - sizeof(struct l_sigframe));
330 	} else
331 		fp = (struct l_sigframe *)regs->tf_esp - 1;
332 	mtx_unlock(&psp->ps_mtx);
333 	PROC_UNLOCK(p);
334 
335 	/* Build the argument list for the signal handler. */
336 	sig = bsd_to_linux_signal(sig);
337 
338 	bzero(&frame, sizeof(frame));
339 
340 	frame.sf_sig = sig;
341 	frame.sf_sigmask = *mask;
342 	bsd_to_linux_sigset(mask, &lmask);
343 
344 	/* Build the signal context to be used by sigreturn. */
345 	frame.sf_sc.sc_mask   = lmask.__mask;
346 	frame.sf_sc.sc_gs     = rgs();
347 	frame.sf_sc.sc_fs     = regs->tf_fs;
348 	frame.sf_sc.sc_es     = regs->tf_es;
349 	frame.sf_sc.sc_ds     = regs->tf_ds;
350 	frame.sf_sc.sc_edi    = regs->tf_edi;
351 	frame.sf_sc.sc_esi    = regs->tf_esi;
352 	frame.sf_sc.sc_ebp    = regs->tf_ebp;
353 	frame.sf_sc.sc_ebx    = regs->tf_ebx;
354 	frame.sf_sc.sc_esp    = regs->tf_esp;
355 	frame.sf_sc.sc_edx    = regs->tf_edx;
356 	frame.sf_sc.sc_ecx    = regs->tf_ecx;
357 	frame.sf_sc.sc_eax    = regs->tf_eax;
358 	frame.sf_sc.sc_eip    = regs->tf_eip;
359 	frame.sf_sc.sc_cs     = regs->tf_cs;
360 	frame.sf_sc.sc_eflags = regs->tf_eflags;
361 	frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
362 	frame.sf_sc.sc_ss     = regs->tf_ss;
363 	frame.sf_sc.sc_err    = regs->tf_err;
364 	frame.sf_sc.sc_cr2    = (register_t)ksi->ksi_addr;
365 	frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno);
366 
367 	if (copyout(&frame, fp, sizeof(frame)) != 0) {
368 		/*
369 		 * Process has trashed its stack; give it an illegal
370 		 * instruction to halt it in its tracks.
371 		 */
372 		PROC_LOCK(p);
373 		sigexit(td, SIGILL);
374 	}
375 
376 	/* Build context to run handler in. */
377 	regs->tf_esp = PTROUT(fp);
378 	regs->tf_eip = linux_vdso_sigcode;
379 	regs->tf_edi = PTROUT(catcher);
380 	regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
381 	regs->tf_cs = _ucodesel;
382 	regs->tf_ds = _udatasel;
383 	regs->tf_es = _udatasel;
384 	regs->tf_fs = _udatasel;
385 	regs->tf_ss = _udatasel;
386 	PROC_LOCK(p);
387 	mtx_lock(&psp->ps_mtx);
388 }
389 
390 /*
391  * System call to cleanup state after a signal
392  * has been taken.  Reset signal mask and
393  * stack state from context left by sendsig (above).
394  * Return to previous pc and psl as specified by
395  * context left by sendsig. Check carefully to
396  * make sure that the user has not modified the
397  * psl to gain improper privileges or to cause
398  * a machine fault.
399  */
400 int
401 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
402 {
403 	struct l_sigframe frame;
404 	struct trapframe *regs;
405 	int eflags;
406 	ksiginfo_t ksi;
407 
408 	regs = td->td_frame;
409 
410 	/*
411 	 * The trampoline code hands us the sigframe.
412 	 * It is unsafe to keep track of it ourselves, in the event that a
413 	 * program jumps out of a signal handler.
414 	 */
415 	if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
416 		return (EFAULT);
417 
418 	/* Check for security violations. */
419 #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
420 	eflags = frame.sf_sc.sc_eflags;
421 	if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
422 		return (EINVAL);
423 
424 	/*
425 	 * Don't allow users to load a valid privileged %cs.  Let the
426 	 * hardware check for invalid selectors, excess privilege in
427 	 * other selectors, invalid %eip's and invalid %esp's.
428 	 */
429 #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
430 	if (!CS_SECURE(frame.sf_sc.sc_cs)) {
431 		ksiginfo_init_trap(&ksi);
432 		ksi.ksi_signo = SIGBUS;
433 		ksi.ksi_code = BUS_OBJERR;
434 		ksi.ksi_trapno = T_PROTFLT;
435 		ksi.ksi_addr = (void *)regs->tf_eip;
436 		trapsignal(td, &ksi);
437 		return (EINVAL);
438 	}
439 
440 	kern_sigprocmask(td, SIG_SETMASK, &frame.sf_sigmask, NULL, 0);
441 
442 	/* Restore signal context. */
443 	/* %gs was restored by the trampoline. */
444 	regs->tf_fs     = frame.sf_sc.sc_fs;
445 	regs->tf_es     = frame.sf_sc.sc_es;
446 	regs->tf_ds     = frame.sf_sc.sc_ds;
447 	regs->tf_edi    = frame.sf_sc.sc_edi;
448 	regs->tf_esi    = frame.sf_sc.sc_esi;
449 	regs->tf_ebp    = frame.sf_sc.sc_ebp;
450 	regs->tf_ebx    = frame.sf_sc.sc_ebx;
451 	regs->tf_edx    = frame.sf_sc.sc_edx;
452 	regs->tf_ecx    = frame.sf_sc.sc_ecx;
453 	regs->tf_eax    = frame.sf_sc.sc_eax;
454 	regs->tf_eip    = frame.sf_sc.sc_eip;
455 	regs->tf_cs     = frame.sf_sc.sc_cs;
456 	regs->tf_eflags = eflags;
457 	regs->tf_esp    = frame.sf_sc.sc_esp_at_signal;
458 	regs->tf_ss     = frame.sf_sc.sc_ss;
459 
460 	return (EJUSTRETURN);
461 }
462 
463 /*
464  * System call to cleanup state after a signal
465  * has been taken.  Reset signal mask and
466  * stack state from context left by rt_sendsig (above).
467  * Return to previous pc and psl as specified by
468  * context left by sendsig. Check carefully to
469  * make sure that the user has not modified the
470  * psl to gain improper privileges or to cause
471  * a machine fault.
472  */
473 int
474 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
475 {
476 	struct l_ucontext uc;
477 	struct l_sigcontext *context;
478 	sigset_t bmask;
479 	l_stack_t *lss;
480 	stack_t ss;
481 	struct trapframe *regs;
482 	int eflags;
483 	ksiginfo_t ksi;
484 
485 	regs = td->td_frame;
486 
487 	/*
488 	 * The trampoline code hands us the ucontext.
489 	 * It is unsafe to keep track of it ourselves, in the event that a
490 	 * program jumps out of a signal handler.
491 	 */
492 	if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
493 		return (EFAULT);
494 
495 	context = &uc.uc_mcontext;
496 
497 	/* Check for security violations. */
498 #define	EFLAGS_SECURE(ef, oef)	((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
499 	eflags = context->sc_eflags;
500 	if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
501 		return (EINVAL);
502 
503 	/*
504 	 * Don't allow users to load a valid privileged %cs.  Let the
505 	 * hardware check for invalid selectors, excess privilege in
506 	 * other selectors, invalid %eip's and invalid %esp's.
507 	 */
508 #define	CS_SECURE(cs)	(ISPL(cs) == SEL_UPL)
509 	if (!CS_SECURE(context->sc_cs)) {
510 		ksiginfo_init_trap(&ksi);
511 		ksi.ksi_signo = SIGBUS;
512 		ksi.ksi_code = BUS_OBJERR;
513 		ksi.ksi_trapno = T_PROTFLT;
514 		ksi.ksi_addr = (void *)regs->tf_eip;
515 		trapsignal(td, &ksi);
516 		return (EINVAL);
517 	}
518 
519 	linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
520 	kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
521 
522 	/* Restore signal context. */
523 	/* %gs was restored by the trampoline. */
524 	regs->tf_fs     = context->sc_fs;
525 	regs->tf_es     = context->sc_es;
526 	regs->tf_ds     = context->sc_ds;
527 	regs->tf_edi    = context->sc_edi;
528 	regs->tf_esi    = context->sc_esi;
529 	regs->tf_ebp    = context->sc_ebp;
530 	regs->tf_ebx    = context->sc_ebx;
531 	regs->tf_edx    = context->sc_edx;
532 	regs->tf_ecx    = context->sc_ecx;
533 	regs->tf_eax    = context->sc_eax;
534 	regs->tf_eip    = context->sc_eip;
535 	regs->tf_cs     = context->sc_cs;
536 	regs->tf_eflags = eflags;
537 	regs->tf_esp    = context->sc_esp_at_signal;
538 	regs->tf_ss     = context->sc_ss;
539 
540 	/* Call sigaltstack & ignore results. */
541 	lss = &uc.uc_stack;
542 	ss.ss_sp = PTRIN(lss->ss_sp);
543 	ss.ss_size = lss->ss_size;
544 	ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
545 
546 	(void)kern_sigaltstack(td, &ss, NULL);
547 
548 	return (EJUSTRETURN);
549 }
550 
551 static int
552 linux_fetch_syscall_args(struct thread *td)
553 {
554 	struct proc *p;
555 	struct trapframe *frame;
556 	struct syscall_args *sa;
557 
558 	p = td->td_proc;
559 	frame = td->td_frame;
560 	sa = &td->td_sa;
561 
562 	sa->code = frame->tf_eax;
563 	sa->original_code = sa->code;
564 	sa->args[0] = frame->tf_ebx;
565 	sa->args[1] = frame->tf_ecx;
566 	sa->args[2] = frame->tf_edx;
567 	sa->args[3] = frame->tf_esi;
568 	sa->args[4] = frame->tf_edi;
569 	sa->args[5] = frame->tf_ebp;	/* Unconfirmed */
570 
571 	if (sa->code >= p->p_sysent->sv_size)
572 		/* nosys */
573 		sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
574 	else
575 		sa->callp = &p->p_sysent->sv_table[sa->code];
576 
577 	td->td_retval[0] = 0;
578 	td->td_retval[1] = frame->tf_edx;
579 
580 	return (0);
581 }
582 
583 static void
584 linux_set_syscall_retval(struct thread *td, int error)
585 {
586 	struct trapframe *frame = td->td_frame;
587 
588 	cpu_set_syscall_retval(td, error);
589 
590 	if (__predict_false(error != 0)) {
591 		if (error != ERESTART && error != EJUSTRETURN)
592 			frame->tf_eax = bsd_to_linux_errno(error);
593 	}
594 }
595 
596 static void
597 linux_set_fork_retval(struct thread *td)
598 {
599 	struct trapframe *frame = td->td_frame;
600 
601 	frame->tf_eax = 0;
602 }
603 
604 /*
605  * exec_setregs may initialize some registers differently than Linux
606  * does, thus potentially confusing Linux binaries. If necessary, we
607  * override the exec_setregs default(s) here.
608  */
609 static void
610 linux_exec_setregs(struct thread *td, struct image_params *imgp,
611     uintptr_t stack)
612 {
613 	struct pcb *pcb = td->td_pcb;
614 
615 	exec_setregs(td, imgp, stack);
616 
617 	/* Linux sets %gs to 0, we default to _udatasel. */
618 	pcb->pcb_gs = 0;
619 	load_gs(0);
620 
621 	pcb->pcb_initial_npxcw = __LINUX_NPXCW__;
622 }
623 
624 struct sysentvec linux_sysvec = {
625 	.sv_size	= LINUX_SYS_MAXSYSCALL,
626 	.sv_table	= linux_sysent,
627 	.sv_fixup	= linux_fixup,
628 	.sv_sendsig	= linux_sendsig,
629 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
630 	.sv_szsigcode	= &linux_szsigcode,
631 	.sv_name	= "Linux a.out",
632 	.sv_coredump	= NULL,
633 	.sv_imgact_try	= linux_exec_imgact_try,
634 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
635 	.sv_minuser	= VM_MIN_ADDRESS,
636 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
637 	.sv_usrstack	= LINUX_USRSTACK,
638 	.sv_psstrings	= PS_STRINGS,
639 	.sv_psstringssz	= sizeof(struct ps_strings),
640 	.sv_stackprot	= VM_PROT_ALL,
641 	.sv_copyout_strings = exec_copyout_strings,
642 	.sv_setregs	= linux_exec_setregs,
643 	.sv_fixlimit	= NULL,
644 	.sv_maxssiz	= NULL,
645 	.sv_flags	= SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32 |
646 	    SV_SIG_DISCIGN | SV_SIG_WAITNDQ,
647 	.sv_set_syscall_retval = linux_set_syscall_retval,
648 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
649 	.sv_syscallnames = linux_syscallnames,
650 	.sv_schedtail	= linux_schedtail,
651 	.sv_thread_detach = linux_thread_detach,
652 	.sv_trap	= NULL,
653 	.sv_onexec	= linux_on_exec_vmspace,
654 	.sv_onexit	= linux_on_exit,
655 	.sv_ontdexit	= linux_thread_dtor,
656 	.sv_setid_allowed = &linux_setid_allowed_query,
657 	.sv_set_fork_retval = linux_set_fork_retval,
658 };
659 INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
660 
661 struct sysentvec elf_linux_sysvec = {
662 	.sv_size	= LINUX_SYS_MAXSYSCALL,
663 	.sv_table	= linux_sysent,
664 	.sv_fixup	= __elfN(freebsd_fixup),
665 	.sv_sendsig	= linux_sendsig,
666 	.sv_sigcode	= &_binary_linux_vdso_so_o_start,
667 	.sv_szsigcode	= &linux_szsigcode,
668 	.sv_name	= "Linux ELF32",
669 	.sv_coredump	= elf32_coredump,
670 	.sv_elf_core_osabi = ELFOSABI_NONE,
671 	.sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
672 	.sv_elf_core_prepare_notes = __linuxN(prepare_notes),
673 	.sv_imgact_try	= linux_exec_imgact_try,
674 	.sv_minsigstksz	= LINUX_MINSIGSTKSZ,
675 	.sv_minuser	= VM_MIN_ADDRESS,
676 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
677 	.sv_usrstack	= LINUX_USRSTACK,
678 	.sv_psstrings	= LINUX_PS_STRINGS,
679 	.sv_psstringssz	= sizeof(struct ps_strings),
680 	.sv_stackprot	= VM_PROT_ALL,
681 	.sv_copyout_auxargs = linux_copyout_auxargs,
682 	.sv_copyout_strings = __linuxN(copyout_strings),
683 	.sv_setregs	= linux_exec_setregs,
684 	.sv_fixlimit	= NULL,
685 	.sv_maxssiz	= NULL,
686 	.sv_flags	= SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP |
687 	    SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_TIMEKEEP,
688 	.sv_set_syscall_retval = linux_set_syscall_retval,
689 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
690 	.sv_syscallnames = NULL,
691 	.sv_shared_page_base = LINUX_SHAREDPAGE,
692 	.sv_shared_page_len = PAGE_SIZE,
693 	.sv_schedtail	= linux_schedtail,
694 	.sv_thread_detach = linux_thread_detach,
695 	.sv_trap	= NULL,
696 	.sv_onexec	= linux_on_exec_vmspace,
697 	.sv_onexit	= linux_on_exit,
698 	.sv_ontdexit	= linux_thread_dtor,
699 	.sv_setid_allowed = &linux_setid_allowed_query,
700 	.sv_set_fork_retval = linux_set_fork_retval,
701 };
702 
703 static int
704 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
705 {
706 	int error = 0;
707 
708 	if (SV_PROC_FLAG(p, SV_SHP) != 0)
709 		error = linux_map_vdso(p, linux_vdso_obj,
710 		    linux_vdso_base, LINUX_VDSOPAGE_SIZE, imgp);
711 	if (error == 0)
712 		linux_on_exec(p, imgp);
713 	return (error);
714 }
715 
716 /*
717  * linux_vdso_install() and linux_exec_sysvec_init() must be called
718  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
719  */
720 static void
721 linux_exec_sysvec_init(void *param)
722 {
723 	l_uintptr_t *ktimekeep_base, *ktsc_selector;
724 	struct sysentvec *sv;
725 	ptrdiff_t tkoff;
726 
727 	sv = param;
728 	/* Fill timekeep_base */
729 	exec_sysvec_init(sv);
730 
731 	tkoff = kern_timekeep_base - linux_vdso_base;
732 	ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
733 	*ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
734 
735 	tkoff = kern_tsc_selector - linux_vdso_base;
736 	ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
737 	*ktsc_selector = linux_vdso_tsc_selector_idx();
738 	if (bootverbose)
739 		printf("Linux i386 vDSO tsc_selector: %u\n", *ktsc_selector);
740 
741 	tkoff = kern_cpu_selector - linux_vdso_base;
742 	ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
743 	*ktsc_selector = linux_vdso_cpu_selector_idx();
744 	if (bootverbose)
745 		printf("Linux i386 vDSO cpu_selector: %u\n", *ktsc_selector);
746 }
747 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
748     linux_exec_sysvec_init, &elf_linux_sysvec);
749 
750 static void
751 linux_vdso_install(const void *param)
752 {
753 	char *vdso_start = &_binary_linux_vdso_so_o_start;
754 	char *vdso_end = &_binary_linux_vdso_so_o_end;
755 
756 	linux_szsigcode = vdso_end - vdso_start;
757 	MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
758 
759 	linux_vdso_base = LINUX_VDSOPAGE;
760 
761 	__elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
762 
763 	linux_vdso_obj = __elfN(linux_shared_page_init)
764 	    (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
765 	bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
766 
767 	linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
768 }
769 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
770     linux_vdso_install, NULL);
771 
772 static void
773 linux_vdso_deinstall(const void *param)
774 {
775 
776 	__elfN(linux_shared_page_fini)(linux_vdso_obj,
777 	    linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
778 }
779 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
780     linux_vdso_deinstall, NULL);
781 
782 static void
783 linux_vdso_reloc(char *mapping, Elf_Addr offset)
784 {
785 	const Elf_Shdr *shdr;
786 	const Elf_Rel *rel;
787 	const Elf_Ehdr *ehdr;
788 	Elf_Addr *where;
789 	Elf_Size rtype, symidx;
790 	Elf_Addr addr, addend;
791 	int i, relcnt;
792 
793 	MPASS(offset != 0);
794 
795 	relcnt = 0;
796 	ehdr = (const Elf_Ehdr *)mapping;
797 	shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
798 	for (i = 0; i < ehdr->e_shnum; i++)
799 	{
800 		switch (shdr[i].sh_type) {
801 		case SHT_REL:
802 			rel = (const Elf_Rel *)(mapping + shdr[i].sh_offset);
803 			relcnt = shdr[i].sh_size / sizeof(*rel);
804 			break;
805 		case SHT_RELA:
806 			printf("Linux i386 vDSO: unexpected Rela section\n");
807 			break;
808 		}
809 	}
810 
811 	for (i = 0; i < relcnt; i++, rel++) {
812 		where = (Elf_Addr *)(mapping + rel->r_offset);
813 		addend = *where;
814 		rtype = ELF_R_TYPE(rel->r_info);
815 		symidx = ELF_R_SYM(rel->r_info);
816 
817 		switch (rtype) {
818 		case R_386_NONE:	/* none */
819 			break;
820 
821 		case R_386_RELATIVE:	/* B + A */
822 			addr = (Elf_Addr)PTROUT(offset + addend);
823 			if (*where != addr)
824 				*where = addr;
825 			break;
826 
827 		case R_386_IRELATIVE:
828 			printf("Linux i386 vDSO: unexpected ifunc relocation, "
829 			    "symbol index %d\n", symidx);
830 			break;
831 		default:
832 			printf("Linux i386 vDSO: unexpected relocation type %d, "
833 			    "symbol index %d\n", rtype, symidx);
834 		}
835 	}
836 }
837 
838 static Elf_Brandnote linux_brandnote = {
839 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
840 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
841 	.hdr.n_type	= 1,
842 	.vendor		= GNU_ABI_VENDOR,
843 	.flags		= BN_TRANSLATE_OSREL,
844 	.trans_osrel	= linux_trans_osrel
845 };
846 
847 static Elf32_Brandinfo linux_brand = {
848 	.brand		= ELFOSABI_LINUX,
849 	.machine	= EM_386,
850 	.compat_3_brand	= "Linux",
851 	.emul_path	= linux_emul_path,
852 	.interp_path	= "/lib/ld-linux.so.1",
853 	.sysvec		= &elf_linux_sysvec,
854 	.interp_newpath	= NULL,
855 	.brand_note	= &linux_brandnote,
856 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
857 };
858 
859 static Elf32_Brandinfo linux_glibc2brand = {
860 	.brand		= ELFOSABI_LINUX,
861 	.machine	= EM_386,
862 	.compat_3_brand	= "Linux",
863 	.emul_path	= linux_emul_path,
864 	.interp_path	= "/lib/ld-linux.so.2",
865 	.sysvec		= &elf_linux_sysvec,
866 	.interp_newpath	= NULL,
867 	.brand_note	= &linux_brandnote,
868 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
869 };
870 
871 static Elf32_Brandinfo linux_muslbrand = {
872 	.brand		= ELFOSABI_LINUX,
873 	.machine	= EM_386,
874 	.compat_3_brand	= "Linux",
875 	.emul_path	= linux_emul_path,
876 	.interp_path	= "/lib/ld-musl-i386.so.1",
877 	.sysvec		= &elf_linux_sysvec,
878 	.interp_newpath	= NULL,
879 	.brand_note	= &linux_brandnote,
880 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
881 			    LINUX_BI_FUTEX_REQUEUE
882 };
883 
884 Elf32_Brandinfo *linux_brandlist[] = {
885 	&linux_brand,
886 	&linux_glibc2brand,
887 	&linux_muslbrand,
888 	NULL
889 };
890 
891 static int
892 linux_elf_modevent(module_t mod, int type, void *data)
893 {
894 	Elf32_Brandinfo **brandinfo;
895 	int error;
896 	struct linux_ioctl_handler **lihp;
897 
898 	error = 0;
899 
900 	switch(type) {
901 	case MOD_LOAD:
902 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
903 		     ++brandinfo)
904 			if (elf32_insert_brand_entry(*brandinfo) < 0)
905 				error = EINVAL;
906 		if (error == 0) {
907 			SET_FOREACH(lihp, linux_ioctl_handler_set)
908 				linux_ioctl_register_handler(*lihp);
909 			linux_dev_shm_create();
910 			linux_osd_jail_register();
911 			linux_netlink_register();
912 			stclohz = (stathz ? stathz : hz);
913 			if (bootverbose)
914 				printf("Linux ELF exec handler installed\n");
915 		} else
916 			printf("cannot insert Linux ELF brand handler\n");
917 		break;
918 	case MOD_UNLOAD:
919 		for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
920 		     ++brandinfo)
921 			if (elf32_brand_inuse(*brandinfo))
922 				error = EBUSY;
923 		if (error == 0) {
924 			for (brandinfo = &linux_brandlist[0];
925 			     *brandinfo != NULL; ++brandinfo)
926 				if (elf32_remove_brand_entry(*brandinfo) < 0)
927 					error = EINVAL;
928 		}
929 		if (error == 0) {
930 			SET_FOREACH(lihp, linux_ioctl_handler_set)
931 				linux_ioctl_unregister_handler(*lihp);
932 			linux_netlink_deregister();
933 			linux_dev_shm_destroy();
934 			linux_osd_jail_deregister();
935 			if (bootverbose)
936 				printf("Linux ELF exec handler removed\n");
937 		} else
938 			printf("Could not deinstall ELF interpreter entry\n");
939 		break;
940 	default:
941 		return (EOPNOTSUPP);
942 	}
943 	return (error);
944 }
945 
946 static moduledata_t linux_elf_mod = {
947 	"linuxelf",
948 	linux_elf_modevent,
949 	0
950 };
951 
952 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
953 MODULE_DEPEND(linuxelf, netlink, 1, 1, 1);
954 FEATURE(linux, "Linux 32bit support");
955