1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1994-1996 Søren Schmidt
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/exec.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact.h>
37 #include <sys/imgact_aout.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/proc.h>
45 #include <sys/signalvar.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/sysctl.h>
48 #include <sys/sysent.h>
49 #include <sys/sysproto.h>
50 #include <sys/vnode.h>
51 #include <sys/eventhandler.h>
52
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55 #include <vm/vm_extern.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_param.h>
60
61 #include <machine/cpu.h>
62 #include <machine/cputypes.h>
63 #include <machine/md_var.h>
64 #include <machine/pcb.h>
65 #include <machine/trap.h>
66
67 #include <i386/linux/linux.h>
68 #include <i386/linux/linux_proto.h>
69 #include <compat/linux/linux_emul.h>
70 #include <compat/linux/linux_ioctl.h>
71 #include <compat/linux/linux_mib.h>
72 #include <compat/linux/linux_misc.h>
73 #include <compat/linux/linux_signal.h>
74 #include <compat/linux/linux_util.h>
75 #include <compat/linux/linux_vdso.h>
76
77 MODULE_VERSION(linux, 1);
78
79 #define LINUX_PS_STRINGS (LINUX_USRSTACK - sizeof(struct ps_strings))
80
81 static int linux_szsigcode;
82 static vm_object_t linux_shared_page_obj;
83 static char *linux_shared_page_mapping;
84 extern char _binary_linux_locore_o_start;
85 extern char _binary_linux_locore_o_end;
86
87 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
88
89 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
90
91 static int linux_fixup(register_t **stack_base,
92 struct image_params *iparams);
93 static int linux_fixup_elf(register_t **stack_base,
94 struct image_params *iparams);
95 static void linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask);
96 static void linux_exec_setregs(struct thread *td,
97 struct image_params *imgp, u_long stack);
98 static register_t *linux_copyout_strings(struct image_params *imgp);
99 static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
100 static void linux_vdso_install(void *param);
101 static void linux_vdso_deinstall(void *param);
102
103 static int linux_szplatform;
104 const char *linux_kplatform;
105
106 static eventhandler_tag linux_exit_tag;
107 static eventhandler_tag linux_exec_tag;
108 static eventhandler_tag linux_thread_dtor_tag;
109
110 #define LINUX_T_UNKNOWN 255
111 static int _bsd_to_linux_trapcode[] = {
112 LINUX_T_UNKNOWN, /* 0 */
113 6, /* 1 T_PRIVINFLT */
114 LINUX_T_UNKNOWN, /* 2 */
115 3, /* 3 T_BPTFLT */
116 LINUX_T_UNKNOWN, /* 4 */
117 LINUX_T_UNKNOWN, /* 5 */
118 16, /* 6 T_ARITHTRAP */
119 254, /* 7 T_ASTFLT */
120 LINUX_T_UNKNOWN, /* 8 */
121 13, /* 9 T_PROTFLT */
122 1, /* 10 T_TRCTRAP */
123 LINUX_T_UNKNOWN, /* 11 */
124 14, /* 12 T_PAGEFLT */
125 LINUX_T_UNKNOWN, /* 13 */
126 17, /* 14 T_ALIGNFLT */
127 LINUX_T_UNKNOWN, /* 15 */
128 LINUX_T_UNKNOWN, /* 16 */
129 LINUX_T_UNKNOWN, /* 17 */
130 0, /* 18 T_DIVIDE */
131 2, /* 19 T_NMI */
132 4, /* 20 T_OFLOW */
133 5, /* 21 T_BOUND */
134 7, /* 22 T_DNA */
135 8, /* 23 T_DOUBLEFLT */
136 9, /* 24 T_FPOPFLT */
137 10, /* 25 T_TSSFLT */
138 11, /* 26 T_SEGNPFLT */
139 12, /* 27 T_STKFLT */
140 18, /* 28 T_MCHK */
141 19, /* 29 T_XMMFLT */
142 15 /* 30 T_RESERVED */
143 };
144 #define bsd_to_linux_trapcode(code) \
145 ((code)<nitems(_bsd_to_linux_trapcode)? \
146 _bsd_to_linux_trapcode[(code)]: \
147 LINUX_T_UNKNOWN)
148
149 LINUX_VDSO_SYM_INTPTR(linux_sigcode);
150 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode);
151 LINUX_VDSO_SYM_INTPTR(linux_vsyscall);
152
153 /*
154 * If FreeBSD & Linux have a difference of opinion about what a trap
155 * means, deal with it here.
156 *
157 * MPSAFE
158 */
159 static int
160 linux_translate_traps(int signal, int trap_code)
161 {
162 if (signal != SIGBUS)
163 return (signal);
164 switch (trap_code) {
165 case T_PROTFLT:
166 case T_TSSFLT:
167 case T_DOUBLEFLT:
168 case T_PAGEFLT:
169 return (SIGSEGV);
170 default:
171 return (signal);
172 }
173 }
174
175 static int
176 linux_fixup(register_t **stack_base, struct image_params *imgp)
177 {
178 register_t *argv, *envp;
179
180 argv = *stack_base;
181 envp = *stack_base + (imgp->args->argc + 1);
182 (*stack_base)--;
183 suword(*stack_base, (intptr_t)(void *)envp);
184 (*stack_base)--;
185 suword(*stack_base, (intptr_t)(void *)argv);
186 (*stack_base)--;
187 suword(*stack_base, imgp->args->argc);
188 return (0);
189 }
190
191 static int
192 linux_fixup_elf(register_t **stack_base, struct image_params *imgp)
193 {
194 struct proc *p;
195 Elf32_Auxargs *args;
196 Elf32_Auxinfo *argarray, *pos;
197 Elf32_Addr *auxbase, *uplatform;
198 struct ps_strings *arginfo;
199 int error, issetugid;
200
201 KASSERT(curthread->td_proc == imgp->proc,
202 ("unsafe linux_fixup_elf(), should be curproc"));
203
204 p = imgp->proc;
205 issetugid = imgp->proc->p_flag & P_SUGID ? 1 : 0;
206 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
207 uplatform = (Elf32_Addr *)((caddr_t)arginfo - linux_szplatform);
208 args = (Elf32_Auxargs *)imgp->auxargs;
209 auxbase = *stack_base + imgp->args->argc + 1 + imgp->args->envc + 1;
210 argarray = pos = malloc(LINUX_AT_COUNT * sizeof(*pos), M_TEMP,
211 M_WAITOK | M_ZERO);
212
213 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO_EHDR,
214 imgp->proc->p_sysent->sv_shared_page_base);
215 AUXARGS_ENTRY(pos, LINUX_AT_SYSINFO, linux_vsyscall);
216 AUXARGS_ENTRY(pos, LINUX_AT_HWCAP, cpu_feature);
217
218 /*
219 * Do not export AT_CLKTCK when emulating Linux kernel prior to 2.4.0,
220 * as it has appeared in the 2.4.0-rc7 first time.
221 * Being exported, AT_CLKTCK is returned by sysconf(_SC_CLK_TCK),
222 * glibc falls back to the hard-coded CLK_TCK value when aux entry
223 * is not present.
224 * Also see linux_times() implementation.
225 */
226 if (linux_kernver(curthread) >= LINUX_KERNVER_2004000)
227 AUXARGS_ENTRY(pos, LINUX_AT_CLKTCK, stclohz);
228 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
229 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
230 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
231 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
232 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
233 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
234 AUXARGS_ENTRY(pos, AT_BASE, args->base);
235 AUXARGS_ENTRY(pos, LINUX_AT_SECURE, issetugid);
236 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
237 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
238 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
239 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
240 AUXARGS_ENTRY(pos, LINUX_AT_PLATFORM, PTROUT(uplatform));
241 AUXARGS_ENTRY(pos, LINUX_AT_RANDOM, imgp->canary);
242 if (imgp->execpathp != 0)
243 AUXARGS_ENTRY(pos, LINUX_AT_EXECFN, imgp->execpathp);
244 if (args->execfd != -1)
245 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
246 AUXARGS_ENTRY(pos, AT_NULL, 0);
247
248 free(imgp->auxargs, M_TEMP);
249 imgp->auxargs = NULL;
250 KASSERT(pos - argarray <= LINUX_AT_COUNT, ("Too many auxargs"));
251
252 error = copyout(argarray, auxbase, sizeof(*argarray) * LINUX_AT_COUNT);
253 free(argarray, M_TEMP);
254 if (error != 0)
255 return (error);
256
257 (*stack_base)--;
258 if (suword(*stack_base, (register_t)imgp->args->argc) == -1)
259 return (EFAULT);
260 return (0);
261 }
262
263 /*
264 * Copied from kern/kern_exec.c
265 */
266 static register_t *
267 linux_copyout_strings(struct image_params *imgp)
268 {
269 int argc, envc;
270 char **vectp;
271 char *stringp, *destp;
272 register_t *stack_base;
273 struct ps_strings *arginfo;
274 char canary[LINUX_AT_RANDOM_LEN];
275 size_t execpath_len;
276 struct proc *p;
277
278 /* Calculate string base and vector table pointers. */
279 p = imgp->proc;
280 if (imgp->execpath != NULL && imgp->auxargs != NULL)
281 execpath_len = strlen(imgp->execpath) + 1;
282 else
283 execpath_len = 0;
284 arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings;
285 destp = (caddr_t)arginfo - SPARE_USRSPACE - linux_szplatform -
286 roundup(sizeof(canary), sizeof(char *)) -
287 roundup(execpath_len, sizeof(char *)) -
288 roundup(ARG_MAX - imgp->args->stringspace, sizeof(char *));
289
290 /* Install LINUX_PLATFORM. */
291 copyout(linux_kplatform, ((caddr_t)arginfo - linux_szplatform),
292 linux_szplatform);
293
294 if (execpath_len != 0) {
295 imgp->execpathp = (uintptr_t)arginfo -
296 linux_szplatform - execpath_len;
297 copyout(imgp->execpath, (void *)imgp->execpathp, execpath_len);
298 }
299
300 /* Prepare the canary for SSP. */
301 arc4rand(canary, sizeof(canary), 0);
302 imgp->canary = (uintptr_t)arginfo - linux_szplatform -
303 roundup(execpath_len, sizeof(char *)) -
304 roundup(sizeof(canary), sizeof(char *));
305 copyout(canary, (void *)imgp->canary, sizeof(canary));
306
307 vectp = (char **)destp;
308 if (imgp->auxargs) {
309 /*
310 * Allocate room on the stack for the ELF auxargs
311 * array. It has LINUX_AT_COUNT entries.
312 */
313 vectp -= howmany(LINUX_AT_COUNT * sizeof(Elf32_Auxinfo),
314 sizeof(*vectp));
315 }
316
317 /*
318 * Allocate room for the argv[] and env vectors including the
319 * terminating NULL pointers.
320 */
321 vectp -= imgp->args->argc + 1 + imgp->args->envc + 1;
322
323 /* vectp also becomes our initial stack base. */
324 stack_base = (register_t *)vectp;
325
326 stringp = imgp->args->begin_argv;
327 argc = imgp->args->argc;
328 envc = imgp->args->envc;
329
330 /* Copy out strings - arguments and environment. */
331 copyout(stringp, destp, ARG_MAX - imgp->args->stringspace);
332
333 /* Fill in "ps_strings" struct for ps, w, etc. */
334 suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
335 suword(&arginfo->ps_nargvstr, argc);
336
337 /* Fill in argument portion of vector table. */
338 for (; argc > 0; --argc) {
339 suword(vectp++, (long)(intptr_t)destp);
340 while (*stringp++ != 0)
341 destp++;
342 destp++;
343 }
344
345 /* A null vector table pointer separates the argp's from the envp's. */
346 suword(vectp++, 0);
347
348 suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
349 suword(&arginfo->ps_nenvstr, envc);
350
351 /* Fill in environment portion of vector table. */
352 for (; envc > 0; --envc) {
353 suword(vectp++, (long)(intptr_t)destp);
354 while (*stringp++ != 0)
355 destp++;
356 destp++;
357 }
358
359 /* The end of the vector table is a null pointer. */
360 suword(vectp, 0);
361
362 return (stack_base);
363 }
364
365 static void
366 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
367 {
368 struct thread *td = curthread;
369 struct proc *p = td->td_proc;
370 struct sigacts *psp;
371 struct trapframe *regs;
372 struct l_rt_sigframe *fp, frame;
373 int sig, code;
374 int oonstack;
375
376 sig = ksi->ksi_signo;
377 code = ksi->ksi_code;
378 PROC_LOCK_ASSERT(p, MA_OWNED);
379 psp = p->p_sigacts;
380 mtx_assert(&psp->ps_mtx, MA_OWNED);
381 regs = td->td_frame;
382 oonstack = sigonstack(regs->tf_esp);
383
384 /* Allocate space for the signal handler context. */
385 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
386 SIGISMEMBER(psp->ps_sigonstack, sig)) {
387 fp = (struct l_rt_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
388 td->td_sigstk.ss_size - sizeof(struct l_rt_sigframe));
389 } else
390 fp = (struct l_rt_sigframe *)regs->tf_esp - 1;
391 mtx_unlock(&psp->ps_mtx);
392
393 /* Build the argument list for the signal handler. */
394 sig = bsd_to_linux_signal(sig);
395
396 bzero(&frame, sizeof(frame));
397
398 frame.sf_handler = catcher;
399 frame.sf_sig = sig;
400 frame.sf_siginfo = &fp->sf_si;
401 frame.sf_ucontext = &fp->sf_sc;
402
403 /* Fill in POSIX parts. */
404 ksiginfo_to_lsiginfo(ksi, &frame.sf_si, sig);
405
406 /* Build the signal context to be used by sigreturn. */
407 frame.sf_sc.uc_flags = 0; /* XXX ??? */
408 frame.sf_sc.uc_link = NULL; /* XXX ??? */
409
410 frame.sf_sc.uc_stack.ss_sp = td->td_sigstk.ss_sp;
411 frame.sf_sc.uc_stack.ss_size = td->td_sigstk.ss_size;
412 frame.sf_sc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
413 ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
414 PROC_UNLOCK(p);
415
416 bsd_to_linux_sigset(mask, &frame.sf_sc.uc_sigmask);
417
418 frame.sf_sc.uc_mcontext.sc_mask = frame.sf_sc.uc_sigmask.__mask;
419 frame.sf_sc.uc_mcontext.sc_gs = rgs();
420 frame.sf_sc.uc_mcontext.sc_fs = regs->tf_fs;
421 frame.sf_sc.uc_mcontext.sc_es = regs->tf_es;
422 frame.sf_sc.uc_mcontext.sc_ds = regs->tf_ds;
423 frame.sf_sc.uc_mcontext.sc_edi = regs->tf_edi;
424 frame.sf_sc.uc_mcontext.sc_esi = regs->tf_esi;
425 frame.sf_sc.uc_mcontext.sc_ebp = regs->tf_ebp;
426 frame.sf_sc.uc_mcontext.sc_ebx = regs->tf_ebx;
427 frame.sf_sc.uc_mcontext.sc_esp = regs->tf_esp;
428 frame.sf_sc.uc_mcontext.sc_edx = regs->tf_edx;
429 frame.sf_sc.uc_mcontext.sc_ecx = regs->tf_ecx;
430 frame.sf_sc.uc_mcontext.sc_eax = regs->tf_eax;
431 frame.sf_sc.uc_mcontext.sc_eip = regs->tf_eip;
432 frame.sf_sc.uc_mcontext.sc_cs = regs->tf_cs;
433 frame.sf_sc.uc_mcontext.sc_eflags = regs->tf_eflags;
434 frame.sf_sc.uc_mcontext.sc_esp_at_signal = regs->tf_esp;
435 frame.sf_sc.uc_mcontext.sc_ss = regs->tf_ss;
436 frame.sf_sc.uc_mcontext.sc_err = regs->tf_err;
437 frame.sf_sc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr;
438 frame.sf_sc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
439
440 if (copyout(&frame, fp, sizeof(frame)) != 0) {
441 /*
442 * Process has trashed its stack; give it an illegal
443 * instruction to halt it in its tracks.
444 */
445 PROC_LOCK(p);
446 sigexit(td, SIGILL);
447 }
448
449 /* Build context to run handler in. */
450 regs->tf_esp = (int)fp;
451 regs->tf_eip = linux_rt_sigcode;
452 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
453 regs->tf_cs = _ucodesel;
454 regs->tf_ds = _udatasel;
455 regs->tf_es = _udatasel;
456 regs->tf_fs = _udatasel;
457 regs->tf_ss = _udatasel;
458 PROC_LOCK(p);
459 mtx_lock(&psp->ps_mtx);
460 }
461
462
463 /*
464 * Send an interrupt to process.
465 *
466 * Stack is set up to allow sigcode stored
467 * in u. to call routine, followed by kcall
468 * to sigreturn routine below. After sigreturn
469 * resets the signal mask, the stack, and the
470 * frame pointer, it returns to the user
471 * specified pc, psl.
472 */
473 static void
474 linux_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
475 {
476 struct thread *td = curthread;
477 struct proc *p = td->td_proc;
478 struct sigacts *psp;
479 struct trapframe *regs;
480 struct l_sigframe *fp, frame;
481 l_sigset_t lmask;
482 int sig, code;
483 int oonstack;
484
485 PROC_LOCK_ASSERT(p, MA_OWNED);
486 psp = p->p_sigacts;
487 sig = ksi->ksi_signo;
488 code = ksi->ksi_code;
489 mtx_assert(&psp->ps_mtx, MA_OWNED);
490 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
491 /* Signal handler installed with SA_SIGINFO. */
492 linux_rt_sendsig(catcher, ksi, mask);
493 return;
494 }
495 regs = td->td_frame;
496 oonstack = sigonstack(regs->tf_esp);
497
498 /* Allocate space for the signal handler context. */
499 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
500 SIGISMEMBER(psp->ps_sigonstack, sig)) {
501 fp = (struct l_sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
502 td->td_sigstk.ss_size - sizeof(struct l_sigframe));
503 } else
504 fp = (struct l_sigframe *)regs->tf_esp - 1;
505 mtx_unlock(&psp->ps_mtx);
506 PROC_UNLOCK(p);
507
508 /* Build the argument list for the signal handler. */
509 sig = bsd_to_linux_signal(sig);
510
511 bzero(&frame, sizeof(frame));
512
513 frame.sf_handler = catcher;
514 frame.sf_sig = sig;
515
516 bsd_to_linux_sigset(mask, &lmask);
517
518 /* Build the signal context to be used by sigreturn. */
519 frame.sf_sc.sc_mask = lmask.__mask;
520 frame.sf_sc.sc_gs = rgs();
521 frame.sf_sc.sc_fs = regs->tf_fs;
522 frame.sf_sc.sc_es = regs->tf_es;
523 frame.sf_sc.sc_ds = regs->tf_ds;
524 frame.sf_sc.sc_edi = regs->tf_edi;
525 frame.sf_sc.sc_esi = regs->tf_esi;
526 frame.sf_sc.sc_ebp = regs->tf_ebp;
527 frame.sf_sc.sc_ebx = regs->tf_ebx;
528 frame.sf_sc.sc_esp = regs->tf_esp;
529 frame.sf_sc.sc_edx = regs->tf_edx;
530 frame.sf_sc.sc_ecx = regs->tf_ecx;
531 frame.sf_sc.sc_eax = regs->tf_eax;
532 frame.sf_sc.sc_eip = regs->tf_eip;
533 frame.sf_sc.sc_cs = regs->tf_cs;
534 frame.sf_sc.sc_eflags = regs->tf_eflags;
535 frame.sf_sc.sc_esp_at_signal = regs->tf_esp;
536 frame.sf_sc.sc_ss = regs->tf_ss;
537 frame.sf_sc.sc_err = regs->tf_err;
538 frame.sf_sc.sc_cr2 = (register_t)ksi->ksi_addr;
539 frame.sf_sc.sc_trapno = bsd_to_linux_trapcode(ksi->ksi_trapno);
540
541 frame.sf_extramask[0] = lmask.__mask;
542
543 if (copyout(&frame, fp, sizeof(frame)) != 0) {
544 /*
545 * Process has trashed its stack; give it an illegal
546 * instruction to halt it in its tracks.
547 */
548 PROC_LOCK(p);
549 sigexit(td, SIGILL);
550 }
551
552 /* Build context to run handler in. */
553 regs->tf_esp = (int)fp;
554 regs->tf_eip = linux_sigcode;
555 regs->tf_eflags &= ~(PSL_T | PSL_VM | PSL_D);
556 regs->tf_cs = _ucodesel;
557 regs->tf_ds = _udatasel;
558 regs->tf_es = _udatasel;
559 regs->tf_fs = _udatasel;
560 regs->tf_ss = _udatasel;
561 PROC_LOCK(p);
562 mtx_lock(&psp->ps_mtx);
563 }
564
565 /*
566 * System call to cleanup state after a signal
567 * has been taken. Reset signal mask and
568 * stack state from context left by sendsig (above).
569 * Return to previous pc and psl as specified by
570 * context left by sendsig. Check carefully to
571 * make sure that the user has not modified the
572 * psl to gain improper privileges or to cause
573 * a machine fault.
574 */
575 int
576 linux_sigreturn(struct thread *td, struct linux_sigreturn_args *args)
577 {
578 struct l_sigframe frame;
579 struct trapframe *regs;
580 l_sigset_t lmask;
581 sigset_t bmask;
582 int eflags;
583 ksiginfo_t ksi;
584
585 regs = td->td_frame;
586
587 /*
588 * The trampoline code hands us the sigframe.
589 * It is unsafe to keep track of it ourselves, in the event that a
590 * program jumps out of a signal handler.
591 */
592 if (copyin(args->sfp, &frame, sizeof(frame)) != 0)
593 return (EFAULT);
594
595 /* Check for security violations. */
596 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
597 eflags = frame.sf_sc.sc_eflags;
598 if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
599 return (EINVAL);
600
601 /*
602 * Don't allow users to load a valid privileged %cs. Let the
603 * hardware check for invalid selectors, excess privilege in
604 * other selectors, invalid %eip's and invalid %esp's.
605 */
606 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
607 if (!CS_SECURE(frame.sf_sc.sc_cs)) {
608 ksiginfo_init_trap(&ksi);
609 ksi.ksi_signo = SIGBUS;
610 ksi.ksi_code = BUS_OBJERR;
611 ksi.ksi_trapno = T_PROTFLT;
612 ksi.ksi_addr = (void *)regs->tf_eip;
613 trapsignal(td, &ksi);
614 return (EINVAL);
615 }
616
617 lmask.__mask = frame.sf_sc.sc_mask;
618 linux_to_bsd_sigset(&lmask, &bmask);
619 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
620
621 /* Restore signal context. */
622 /* %gs was restored by the trampoline. */
623 regs->tf_fs = frame.sf_sc.sc_fs;
624 regs->tf_es = frame.sf_sc.sc_es;
625 regs->tf_ds = frame.sf_sc.sc_ds;
626 regs->tf_edi = frame.sf_sc.sc_edi;
627 regs->tf_esi = frame.sf_sc.sc_esi;
628 regs->tf_ebp = frame.sf_sc.sc_ebp;
629 regs->tf_ebx = frame.sf_sc.sc_ebx;
630 regs->tf_edx = frame.sf_sc.sc_edx;
631 regs->tf_ecx = frame.sf_sc.sc_ecx;
632 regs->tf_eax = frame.sf_sc.sc_eax;
633 regs->tf_eip = frame.sf_sc.sc_eip;
634 regs->tf_cs = frame.sf_sc.sc_cs;
635 regs->tf_eflags = eflags;
636 regs->tf_esp = frame.sf_sc.sc_esp_at_signal;
637 regs->tf_ss = frame.sf_sc.sc_ss;
638
639 return (EJUSTRETURN);
640 }
641
642 /*
643 * System call to cleanup state after a signal
644 * has been taken. Reset signal mask and
645 * stack state from context left by rt_sendsig (above).
646 * Return to previous pc and psl as specified by
647 * context left by sendsig. Check carefully to
648 * make sure that the user has not modified the
649 * psl to gain improper privileges or to cause
650 * a machine fault.
651 */
652 int
653 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
654 {
655 struct l_ucontext uc;
656 struct l_sigcontext *context;
657 sigset_t bmask;
658 l_stack_t *lss;
659 stack_t ss;
660 struct trapframe *regs;
661 int eflags;
662 ksiginfo_t ksi;
663
664 regs = td->td_frame;
665
666 /*
667 * The trampoline code hands us the ucontext.
668 * It is unsafe to keep track of it ourselves, in the event that a
669 * program jumps out of a signal handler.
670 */
671 if (copyin(args->ucp, &uc, sizeof(uc)) != 0)
672 return (EFAULT);
673
674 context = &uc.uc_mcontext;
675
676 /* Check for security violations. */
677 #define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
678 eflags = context->sc_eflags;
679 if (!EFLAGS_SECURE(eflags, regs->tf_eflags))
680 return (EINVAL);
681
682 /*
683 * Don't allow users to load a valid privileged %cs. Let the
684 * hardware check for invalid selectors, excess privilege in
685 * other selectors, invalid %eip's and invalid %esp's.
686 */
687 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
688 if (!CS_SECURE(context->sc_cs)) {
689 ksiginfo_init_trap(&ksi);
690 ksi.ksi_signo = SIGBUS;
691 ksi.ksi_code = BUS_OBJERR;
692 ksi.ksi_trapno = T_PROTFLT;
693 ksi.ksi_addr = (void *)regs->tf_eip;
694 trapsignal(td, &ksi);
695 return (EINVAL);
696 }
697
698 linux_to_bsd_sigset(&uc.uc_sigmask, &bmask);
699 kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
700
701 /* Restore signal context. */
702 /* %gs was restored by the trampoline. */
703 regs->tf_fs = context->sc_fs;
704 regs->tf_es = context->sc_es;
705 regs->tf_ds = context->sc_ds;
706 regs->tf_edi = context->sc_edi;
707 regs->tf_esi = context->sc_esi;
708 regs->tf_ebp = context->sc_ebp;
709 regs->tf_ebx = context->sc_ebx;
710 regs->tf_edx = context->sc_edx;
711 regs->tf_ecx = context->sc_ecx;
712 regs->tf_eax = context->sc_eax;
713 regs->tf_eip = context->sc_eip;
714 regs->tf_cs = context->sc_cs;
715 regs->tf_eflags = eflags;
716 regs->tf_esp = context->sc_esp_at_signal;
717 regs->tf_ss = context->sc_ss;
718
719 /* Call sigaltstack & ignore results. */
720 lss = &uc.uc_stack;
721 ss.ss_sp = lss->ss_sp;
722 ss.ss_size = lss->ss_size;
723 ss.ss_flags = linux_to_bsd_sigaltstack(lss->ss_flags);
724
725 (void)kern_sigaltstack(td, &ss, NULL);
726
727 return (EJUSTRETURN);
728 }
729
730 static int
731 linux_fetch_syscall_args(struct thread *td)
732 {
733 struct proc *p;
734 struct trapframe *frame;
735 struct syscall_args *sa;
736
737 p = td->td_proc;
738 frame = td->td_frame;
739 sa = &td->td_sa;
740
741 sa->code = frame->tf_eax;
742 sa->args[0] = frame->tf_ebx;
743 sa->args[1] = frame->tf_ecx;
744 sa->args[2] = frame->tf_edx;
745 sa->args[3] = frame->tf_esi;
746 sa->args[4] = frame->tf_edi;
747 sa->args[5] = frame->tf_ebp; /* Unconfirmed */
748
749 if (sa->code >= p->p_sysent->sv_size)
750 /* nosys */
751 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
752 else
753 sa->callp = &p->p_sysent->sv_table[sa->code];
754 sa->narg = sa->callp->sy_narg;
755
756 td->td_retval[0] = 0;
757 td->td_retval[1] = frame->tf_edx;
758
759 return (0);
760 }
761
762 /*
763 * exec_setregs may initialize some registers differently than Linux
764 * does, thus potentially confusing Linux binaries. If necessary, we
765 * override the exec_setregs default(s) here.
766 */
767 static void
768 linux_exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
769 {
770 struct pcb *pcb = td->td_pcb;
771
772 exec_setregs(td, imgp, stack);
773
774 /* Linux sets %gs to 0, we default to _udatasel. */
775 pcb->pcb_gs = 0;
776 load_gs(0);
777
778 pcb->pcb_initial_npxcw = __LINUX_NPXCW__;
779 }
780
781 static void
782 linux_get_machine(const char **dst)
783 {
784
785 switch (cpu_class) {
786 case CPUCLASS_686:
787 *dst = "i686";
788 break;
789 case CPUCLASS_586:
790 *dst = "i586";
791 break;
792 case CPUCLASS_486:
793 *dst = "i486";
794 break;
795 default:
796 *dst = "i386";
797 }
798 }
799
800 struct sysentvec linux_sysvec = {
801 .sv_size = LINUX_SYS_MAXSYSCALL,
802 .sv_table = linux_sysent,
803 .sv_mask = 0,
804 .sv_errsize = ELAST + 1,
805 .sv_errtbl = linux_errtbl,
806 .sv_transtrap = linux_translate_traps,
807 .sv_fixup = linux_fixup,
808 .sv_sendsig = linux_sendsig,
809 .sv_sigcode = &_binary_linux_locore_o_start,
810 .sv_szsigcode = &linux_szsigcode,
811 .sv_name = "Linux a.out",
812 .sv_coredump = NULL,
813 .sv_imgact_try = linux_exec_imgact_try,
814 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
815 .sv_minuser = VM_MIN_ADDRESS,
816 .sv_maxuser = VM_MAXUSER_ADDRESS,
817 .sv_usrstack = LINUX_USRSTACK,
818 .sv_psstrings = PS_STRINGS,
819 .sv_stackprot = VM_PROT_ALL,
820 .sv_copyout_strings = exec_copyout_strings,
821 .sv_setregs = linux_exec_setregs,
822 .sv_fixlimit = NULL,
823 .sv_maxssiz = NULL,
824 .sv_flags = SV_ABI_LINUX | SV_AOUT | SV_IA32 | SV_ILP32,
825 .sv_set_syscall_retval = cpu_set_syscall_retval,
826 .sv_fetch_syscall_args = linux_fetch_syscall_args,
827 .sv_syscallnames = NULL,
828 .sv_shared_page_base = LINUX_SHAREDPAGE,
829 .sv_shared_page_len = PAGE_SIZE,
830 .sv_schedtail = linux_schedtail,
831 .sv_thread_detach = linux_thread_detach,
832 .sv_trap = NULL,
833 };
834 INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
835
836 struct sysentvec elf_linux_sysvec = {
837 .sv_size = LINUX_SYS_MAXSYSCALL,
838 .sv_table = linux_sysent,
839 .sv_mask = 0,
840 .sv_errsize = ELAST + 1,
841 .sv_errtbl = linux_errtbl,
842 .sv_transtrap = linux_translate_traps,
843 .sv_fixup = linux_fixup_elf,
844 .sv_sendsig = linux_sendsig,
845 .sv_sigcode = &_binary_linux_locore_o_start,
846 .sv_szsigcode = &linux_szsigcode,
847 .sv_name = "Linux ELF",
848 .sv_coredump = elf32_coredump,
849 .sv_imgact_try = linux_exec_imgact_try,
850 .sv_minsigstksz = LINUX_MINSIGSTKSZ,
851 .sv_minuser = VM_MIN_ADDRESS,
852 .sv_maxuser = VM_MAXUSER_ADDRESS,
853 .sv_usrstack = LINUX_USRSTACK,
854 .sv_psstrings = LINUX_PS_STRINGS,
855 .sv_stackprot = VM_PROT_ALL,
856 .sv_copyout_strings = linux_copyout_strings,
857 .sv_setregs = linux_exec_setregs,
858 .sv_fixlimit = NULL,
859 .sv_maxssiz = NULL,
860 .sv_flags = SV_ABI_LINUX | SV_IA32 | SV_ILP32 | SV_SHP,
861 .sv_set_syscall_retval = cpu_set_syscall_retval,
862 .sv_fetch_syscall_args = linux_fetch_syscall_args,
863 .sv_syscallnames = NULL,
864 .sv_shared_page_base = LINUX_SHAREDPAGE,
865 .sv_shared_page_len = PAGE_SIZE,
866 .sv_schedtail = linux_schedtail,
867 .sv_thread_detach = linux_thread_detach,
868 .sv_trap = NULL,
869 };
870
871 static void
872 linux_vdso_install(void *param)
873 {
874
875 linux_szsigcode = (&_binary_linux_locore_o_end -
876 &_binary_linux_locore_o_start);
877
878 if (linux_szsigcode > elf_linux_sysvec.sv_shared_page_len)
879 panic("Linux invalid vdso size\n");
880
881 __elfN(linux_vdso_fixup)(&elf_linux_sysvec);
882
883 linux_shared_page_obj = __elfN(linux_shared_page_init)
884 (&linux_shared_page_mapping);
885
886 __elfN(linux_vdso_reloc)(&elf_linux_sysvec);
887
888 bcopy(elf_linux_sysvec.sv_sigcode, linux_shared_page_mapping,
889 linux_szsigcode);
890 elf_linux_sysvec.sv_shared_page_obj = linux_shared_page_obj;
891 }
892 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC, SI_ORDER_ANY,
893 linux_vdso_install, NULL);
894
895 static void
896 linux_vdso_deinstall(void *param)
897 {
898
899 __elfN(linux_shared_page_fini)(linux_shared_page_obj);
900 }
901 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
902 linux_vdso_deinstall, NULL);
903
904 static char GNU_ABI_VENDOR[] = "GNU";
905 static int GNULINUX_ABI_DESC = 0;
906
907 static bool
908 linux_trans_osrel(const Elf_Note *note, int32_t *osrel)
909 {
910 const Elf32_Word *desc;
911 uintptr_t p;
912
913 p = (uintptr_t)(note + 1);
914 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
915
916 desc = (const Elf32_Word *)p;
917 if (desc[0] != GNULINUX_ABI_DESC)
918 return (false);
919
920 /*
921 * For Linux we encode osrel using the Linux convention of
922 * (version << 16) | (major << 8) | (minor)
923 * See macro in linux_mib.h
924 */
925 *osrel = LINUX_KERNVER(desc[1], desc[2], desc[3]);
926
927 return (true);
928 }
929
930 static Elf_Brandnote linux_brandnote = {
931 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
932 .hdr.n_descsz = 16, /* XXX at least 16 */
933 .hdr.n_type = 1,
934 .vendor = GNU_ABI_VENDOR,
935 .flags = BN_TRANSLATE_OSREL,
936 .trans_osrel = linux_trans_osrel
937 };
938
939 static Elf32_Brandinfo linux_brand = {
940 .brand = ELFOSABI_LINUX,
941 .machine = EM_386,
942 .compat_3_brand = "Linux",
943 .emul_path = linux_emul_path,
944 .interp_path = "/lib/ld-linux.so.1",
945 .sysvec = &elf_linux_sysvec,
946 .interp_newpath = NULL,
947 .brand_note = &linux_brandnote,
948 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
949 };
950
951 static Elf32_Brandinfo linux_glibc2brand = {
952 .brand = ELFOSABI_LINUX,
953 .machine = EM_386,
954 .compat_3_brand = "Linux",
955 .emul_path = linux_emul_path,
956 .interp_path = "/lib/ld-linux.so.2",
957 .sysvec = &elf_linux_sysvec,
958 .interp_newpath = NULL,
959 .brand_note = &linux_brandnote,
960 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
961 };
962
963 static Elf32_Brandinfo linux_muslbrand = {
964 .brand = ELFOSABI_LINUX,
965 .machine = EM_386,
966 .compat_3_brand = "Linux",
967 .emul_path = linux_emul_path,
968 .interp_path = "/lib/ld-musl-i386.so.1",
969 .sysvec = &elf_linux_sysvec,
970 .interp_newpath = NULL,
971 .brand_note = &linux_brandnote,
972 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
973 };
974
975 Elf32_Brandinfo *linux_brandlist[] = {
976 &linux_brand,
977 &linux_glibc2brand,
978 &linux_muslbrand,
979 NULL
980 };
981
982 static int
983 linux_elf_modevent(module_t mod, int type, void *data)
984 {
985 Elf32_Brandinfo **brandinfo;
986 int error;
987 struct linux_ioctl_handler **lihp;
988
989 error = 0;
990
991 switch(type) {
992 case MOD_LOAD:
993 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
994 ++brandinfo)
995 if (elf32_insert_brand_entry(*brandinfo) < 0)
996 error = EINVAL;
997 if (error == 0) {
998 SET_FOREACH(lihp, linux_ioctl_handler_set)
999 linux_ioctl_register_handler(*lihp);
1000 LIST_INIT(&futex_list);
1001 mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF);
1002 linux_exit_tag = EVENTHANDLER_REGISTER(process_exit, linux_proc_exit,
1003 NULL, 1000);
1004 linux_exec_tag = EVENTHANDLER_REGISTER(process_exec, linux_proc_exec,
1005 NULL, 1000);
1006 linux_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor,
1007 linux_thread_dtor, NULL, EVENTHANDLER_PRI_ANY);
1008 linux_get_machine(&linux_kplatform);
1009 linux_szplatform = roundup(strlen(linux_kplatform) + 1,
1010 sizeof(char *));
1011 linux_dev_shm_create();
1012 linux_osd_jail_register();
1013 stclohz = (stathz ? stathz : hz);
1014 if (bootverbose)
1015 printf("Linux ELF exec handler installed\n");
1016 } else
1017 printf("cannot insert Linux ELF brand handler\n");
1018 break;
1019 case MOD_UNLOAD:
1020 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
1021 ++brandinfo)
1022 if (elf32_brand_inuse(*brandinfo))
1023 error = EBUSY;
1024 if (error == 0) {
1025 for (brandinfo = &linux_brandlist[0];
1026 *brandinfo != NULL; ++brandinfo)
1027 if (elf32_remove_brand_entry(*brandinfo) < 0)
1028 error = EINVAL;
1029 }
1030 if (error == 0) {
1031 SET_FOREACH(lihp, linux_ioctl_handler_set)
1032 linux_ioctl_unregister_handler(*lihp);
1033 mtx_destroy(&futex_mtx);
1034 EVENTHANDLER_DEREGISTER(process_exit, linux_exit_tag);
1035 EVENTHANDLER_DEREGISTER(process_exec, linux_exec_tag);
1036 EVENTHANDLER_DEREGISTER(thread_dtor, linux_thread_dtor_tag);
1037 linux_dev_shm_destroy();
1038 linux_osd_jail_deregister();
1039 if (bootverbose)
1040 printf("Linux ELF exec handler removed\n");
1041 } else
1042 printf("Could not deinstall ELF interpreter entry\n");
1043 break;
1044 default:
1045 return (EOPNOTSUPP);
1046 }
1047 return (error);
1048 }
1049
1050 static moduledata_t linux_elf_mod = {
1051 "linuxelf",
1052 linux_elf_modevent,
1053 0
1054 };
1055
1056 DECLARE_MODULE_TIED(linuxelf, linux_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
1057 FEATURE(linux, "Linux 32bit support");
Cache object: c938d671487c1ccea616c77e60a8585b
|