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