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