]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/linux/linux_sysvec.c
Upgrade to OpenPAM Ximenia.
[FreeBSD/FreeBSD.git] / sys / amd64 / linux / linux_sysvec.c
1 /*-
2  * Copyright (c) 2004 Tim J. Robbins
3  * Copyright (c) 2003 Peter Wemm
4  * Copyright (c) 2002 Doug Rabson
5  * Copyright (c) 1998-1999 Andrew Gallatin
6  * Copyright (c) 1994-1996 Søren Schmidt
7  * All rights reserved.
8  * Copyright (c) 2013, 2021 Dmitry Chagin <dchagin@FreeBSD.org>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer
15  *    in this position and unchanged.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #define __ELF_WORD_SIZE 64
38
39 #include <sys/param.h>
40 #include <sys/exec.h>
41 #include <sys/imgact.h>
42 #include <sys/imgact_elf.h>
43 #include <sys/kernel.h>
44 #include <sys/ktr.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/proc.h>
50 #include <sys/stddef.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/sysctl.h>
53 #include <sys/sysent.h>
54
55 #include <vm/pmap.h>
56 #include <vm/vm.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_page.h>
59
60 #include <machine/cpu.h>
61 #include <machine/md_var.h>
62 #include <machine/pcb.h>
63 #include <machine/specialreg.h>
64 #include <machine/trap.h>
65
66 #include <x86/linux/linux_x86.h>
67 #include <amd64/linux/linux.h>
68 #include <amd64/linux/linux_proto.h>
69 #include <compat/linux/linux_elf.h>
70 #include <compat/linux/linux_emul.h>
71 #include <compat/linux/linux_fork.h>
72 #include <compat/linux/linux_ioctl.h>
73 #include <compat/linux/linux_mib.h>
74 #include <compat/linux/linux_misc.h>
75 #include <compat/linux/linux_signal.h>
76 #include <compat/linux/linux_sysproto.h>
77 #include <compat/linux/linux_util.h>
78 #include <compat/linux/linux_vdso.h>
79
80 #include <x86/linux/linux_x86_sigframe.h>
81
82 _Static_assert(sizeof(struct l_fpstate) ==
83     sizeof(__typeof(((mcontext_t *)0)->mc_fpstate)),
84     "fxsave area size incorrect");
85
86 MODULE_VERSION(linux64, 1);
87
88 #define LINUX_VDSOPAGE_SIZE     PAGE_SIZE * 2
89 #define LINUX_VDSOPAGE_LA48     (VM_MAXUSER_ADDRESS_LA48 - \
90                                     LINUX_VDSOPAGE_SIZE)
91 #define LINUX_SHAREDPAGE_LA48   (LINUX_VDSOPAGE_LA48 - PAGE_SIZE)
92                                 /*
93                                  * PAGE_SIZE - the size
94                                  * of the native SHAREDPAGE
95                                  */
96 #define LINUX_USRSTACK_LA48     LINUX_SHAREDPAGE_LA48
97 #define LINUX_PS_STRINGS_LA48   (LINUX_USRSTACK_LA48 - \
98                                     sizeof(struct ps_strings))
99
100 static int linux_szsigcode;
101 static vm_object_t linux_vdso_obj;
102 static char *linux_vdso_mapping;
103 extern char _binary_linux_vdso_so_o_start;
104 extern char _binary_linux_vdso_so_o_end;
105 static vm_offset_t linux_vdso_base;
106
107 extern struct sysent linux_sysent[LINUX_SYS_MAXSYSCALL];
108 extern const char *linux_syscallnames[];
109
110 SET_DECLARE(linux_ioctl_handler_set, struct linux_ioctl_handler);
111
112 static void     linux_vdso_install(const void *param);
113 static void     linux_vdso_deinstall(const void *param);
114 static void     linux_vdso_reloc(char *mapping, Elf_Addr offset);
115 static void     linux_set_syscall_retval(struct thread *td, int error);
116 static int      linux_fetch_syscall_args(struct thread *td);
117 static void     linux_exec_setregs(struct thread *td, struct image_params *imgp,
118                     uintptr_t stack);
119 static void     linux_exec_sysvec_init(void *param);
120 static int      linux_on_exec_vmspace(struct proc *p,
121                     struct image_params *imgp);
122 static void     linux_set_fork_retval(struct thread *td);
123 static int      linux_vsyscall(struct thread *td);
124
125 LINUX_VDSO_SYM_INTPTR(linux_rt_sigcode);
126 LINUX_VDSO_SYM_CHAR(linux_platform);
127 LINUX_VDSO_SYM_INTPTR(kern_timekeep_base);
128 LINUX_VDSO_SYM_INTPTR(kern_tsc_selector);
129 LINUX_VDSO_SYM_INTPTR(kern_cpu_selector);
130
131 /*
132  * According to the Intel x86 ISA 64-bit syscall
133  * saves %rip to %rcx and rflags to %r11. Registers on syscall entry:
134  * %rax  system call number
135  * %rcx  return address
136  * %r11  saved rflags
137  * %rdi  arg1
138  * %rsi  arg2
139  * %rdx  arg3
140  * %r10  arg4
141  * %r8   arg5
142  * %r9   arg6
143  *
144  * Then FreeBSD fast_syscall() move registers:
145  * %rcx -> trapframe.tf_rip
146  * %r10 -> trapframe.tf_rcx
147  */
148 static int
149 linux_fetch_syscall_args(struct thread *td)
150 {
151         struct proc *p;
152         struct trapframe *frame;
153         struct syscall_args *sa;
154
155         p = td->td_proc;
156         frame = td->td_frame;
157         sa = &td->td_sa;
158
159         sa->args[0] = frame->tf_rdi;
160         sa->args[1] = frame->tf_rsi;
161         sa->args[2] = frame->tf_rdx;
162         sa->args[3] = frame->tf_rcx;
163         sa->args[4] = frame->tf_r8;
164         sa->args[5] = frame->tf_r9;
165         sa->code = frame->tf_rax;
166         sa->original_code = sa->code;
167
168         if (sa->code >= p->p_sysent->sv_size)
169                 /* nosys */
170                 sa->callp = &p->p_sysent->sv_table[p->p_sysent->sv_size - 1];
171         else
172                 sa->callp = &p->p_sysent->sv_table[sa->code];
173
174         /* Restore r10 earlier to avoid doing this multiply times. */
175         frame->tf_r10 = frame->tf_rcx;
176         /* Restore %rcx for machine context. */
177         frame->tf_rcx = frame->tf_rip;
178
179         td->td_retval[0] = 0;
180         return (0);
181 }
182
183 static void
184 linux_set_syscall_retval(struct thread *td, int error)
185 {
186         struct trapframe *frame;
187
188         frame = td->td_frame;
189
190         switch (error) {
191         case 0:
192                 frame->tf_rax = td->td_retval[0];
193                 break;
194
195         case ERESTART:
196                 /*
197                  * Reconstruct pc, we know that 'syscall' is 2 bytes,
198                  * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
199                  * We saved this in tf_err.
200                  *
201                  */
202                 frame->tf_rip -= frame->tf_err;
203                 break;
204
205         case EJUSTRETURN:
206                 break;
207
208         default:
209                 frame->tf_rax = bsd_to_linux_errno(error);
210                 break;
211         }
212
213         /*
214          * Differently from FreeBSD native ABI, on Linux only %rcx
215          * and %r11 values are not preserved across the syscall.
216          * Require full context restore to get all registers except
217          * those two restored at return to usermode.
218          */
219         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
220 }
221
222 static void
223 linux_set_fork_retval(struct thread *td)
224 {
225         struct trapframe *frame = td->td_frame;
226
227         frame->tf_rax = 0;
228 }
229
230 void
231 linux64_arch_copyout_auxargs(struct image_params *imgp, Elf_Auxinfo **pos)
232 {
233
234         AUXARGS_ENTRY((*pos), LINUX_AT_SYSINFO_EHDR, linux_vdso_base);
235         AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP, cpu_feature);
236         AUXARGS_ENTRY((*pos), LINUX_AT_HWCAP2, 0);
237         AUXARGS_ENTRY((*pos), LINUX_AT_PLATFORM, PTROUT(linux_platform));
238 }
239
240 /*
241  * Reset registers to default values on exec.
242  */
243 static void
244 linux_exec_setregs(struct thread *td, struct image_params *imgp,
245     uintptr_t stack)
246 {
247         struct trapframe *regs;
248         struct pcb *pcb;
249         register_t saved_rflags;
250
251         regs = td->td_frame;
252         pcb = td->td_pcb;
253
254         if (td->td_proc->p_md.md_ldt != NULL)
255                 user_ldt_free(td);
256
257         pcb->pcb_fsbase = 0;
258         pcb->pcb_gsbase = 0;
259         clear_pcb_flags(pcb, PCB_32BIT);
260         pcb->pcb_initial_fpucw = __LINUX_NPXCW__;
261         set_pcb_flags(pcb, PCB_FULL_IRET);
262
263         saved_rflags = regs->tf_rflags & PSL_T;
264         bzero((char *)regs, sizeof(struct trapframe));
265         regs->tf_rip = imgp->entry_addr;
266         regs->tf_rsp = stack;
267         regs->tf_rflags = PSL_USER | saved_rflags;
268         regs->tf_ss = _udatasel;
269         regs->tf_cs = _ucodesel;
270         regs->tf_ds = _udatasel;
271         regs->tf_es = _udatasel;
272         regs->tf_fs = _ufssel;
273         regs->tf_gs = _ugssel;
274         regs->tf_flags = TF_HASSEGS;
275
276         x86_clear_dbregs(pcb);
277
278         /*
279          * Drop the FP state if we hold it, so that the process gets a
280          * clean FP state if it uses the FPU again.
281          */
282         fpstate_drop(td);
283 }
284
285 static int
286 linux_fxrstor(struct thread *td, mcontext_t *mcp, struct l_sigcontext *sc)
287 {
288         struct savefpu *fp = (struct savefpu *)&mcp->mc_fpstate[0];
289         int error;
290
291         error = copyin(PTRIN(sc->sc_fpstate), fp, sizeof(mcp->mc_fpstate));
292         if (error != 0)
293                 return (error);
294         bzero(&fp->sv_pad[0], sizeof(fp->sv_pad));
295         return (set_fpcontext(td, mcp, NULL, 0));
296 }
297
298 static int
299 linux_xrstor(struct thread *td, mcontext_t *mcp, struct l_sigcontext *sc)
300 {
301         struct savefpu *fp = (struct savefpu *)&mcp->mc_fpstate[0];
302         char *xfpustate;
303         struct proc *p;
304         uint32_t magic2;
305         int error;
306
307         p = td->td_proc;
308         mcp->mc_xfpustate_len = cpu_max_ext_state_size - sizeof(struct savefpu);
309
310         /* Legacy region of an xsave area. */
311         error = copyin(PTRIN(sc->sc_fpstate), fp, sizeof(mcp->mc_fpstate));
312         if (error != 0)
313                 return (error);
314         bzero(&fp->sv_pad[0], sizeof(fp->sv_pad));
315
316         /* Extended region of an xsave area. */
317         sc->sc_fpstate += sizeof(mcp->mc_fpstate);
318         xfpustate = (char *)fpu_save_area_alloc();
319         error = copyin(PTRIN(sc->sc_fpstate), xfpustate, mcp->mc_xfpustate_len);
320         if (error != 0) {
321                 fpu_save_area_free((struct savefpu *)xfpustate);
322                 uprintf("pid %d (%s): linux xrstor failed\n", p->p_pid,
323                     td->td_name);
324                 return (error);
325         }
326
327         /* Linux specific end of xsave area marker. */
328         sc->sc_fpstate += mcp->mc_xfpustate_len;
329         error = copyin(PTRIN(sc->sc_fpstate), &magic2, LINUX_FP_XSTATE_MAGIC2_SIZE);
330         if (error != 0 || magic2 != LINUX_FP_XSTATE_MAGIC2) {
331                 fpu_save_area_free((struct savefpu *)xfpustate);
332                 uprintf("pid %d (%s): sigreturn magic2 0x%x error %d\n",
333                     p->p_pid, td->td_name, magic2, error);
334                 return (error);
335         }
336
337         error = set_fpcontext(td, mcp, xfpustate, mcp->mc_xfpustate_len);
338         fpu_save_area_free((struct savefpu *)xfpustate);
339         if (error != 0) {
340                 uprintf("pid %d (%s): sigreturn set_fpcontext error %d\n",
341                     p->p_pid, td->td_name, error);
342         }
343         return (error);
344 }
345
346 static int
347 linux_copyin_fpstate(struct thread *td, struct l_ucontext *uc)
348 {
349         mcontext_t mc;
350
351         bzero(&mc, sizeof(mc));
352         mc.mc_ownedfp = _MC_FPOWNED_FPU;
353         mc.mc_fpformat = _MC_FPFMT_XMM;
354
355         if ((uc->uc_flags & LINUX_UC_FP_XSTATE) != 0)
356                 return (linux_xrstor(td, &mc, &uc->uc_mcontext));
357         else
358                 return (linux_fxrstor(td, &mc, &uc->uc_mcontext));
359 }
360
361 /*
362  * Copied from amd64/amd64/machdep.c
363  */
364 int
365 linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args)
366 {
367         struct proc *p;
368         struct l_rt_sigframe sf;
369         struct l_sigcontext *context;
370         struct trapframe *regs;
371         unsigned long rflags;
372         sigset_t bmask;
373         int error;
374         ksiginfo_t ksi;
375
376         regs = td->td_frame;
377         error = copyin((void *)regs->tf_rbx, &sf, sizeof(sf));
378         if (error != 0)
379                 return (error);
380
381         p = td->td_proc;
382         context = &sf.sf_uc.uc_mcontext;
383         rflags = context->sc_rflags;
384
385         /*
386          * Don't allow users to change privileged or reserved flags.
387          */
388         /*
389          * XXX do allow users to change the privileged flag PSL_RF.
390          * The cpu sets PSL_RF in tf_rflags for faults.  Debuggers
391          * should sometimes set it there too.  tf_rflags is kept in
392          * the signal context during signal handling and there is no
393          * other place to remember it, so the PSL_RF bit may be
394          * corrupted by the signal handler without us knowing.
395          * Corruption of the PSL_RF bit at worst causes one more or
396          * one less debugger trap, so allowing it is fairly harmless.
397          */
398         if (!EFL_SECURE(rflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) {
399                 uprintf("pid %d comm %s linux mangled rflags %#lx\n",
400                     p->p_pid, p->p_comm, rflags);
401                 return (EINVAL);
402         }
403
404         /*
405          * Don't allow users to load a valid privileged %cs.  Let the
406          * hardware check for invalid selectors, excess privilege in
407          * other selectors, invalid %eip's and invalid %esp's.
408          */
409         if (!CS_SECURE(context->sc_cs)) {
410                 uprintf("pid %d comm %s linux mangled cs %#x\n",
411                     p->p_pid, p->p_comm, context->sc_cs);
412                 ksiginfo_init_trap(&ksi);
413                 ksi.ksi_signo = SIGBUS;
414                 ksi.ksi_code = BUS_OBJERR;
415                 ksi.ksi_trapno = T_PROTFLT;
416                 ksi.ksi_addr = (void *)regs->tf_rip;
417                 trapsignal(td, &ksi);
418                 return (EINVAL);
419         }
420
421         linux_to_bsd_sigset(&sf.sf_uc.uc_sigmask, &bmask);
422         kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0);
423
424         regs->tf_rdi    = context->sc_rdi;
425         regs->tf_rsi    = context->sc_rsi;
426         regs->tf_rdx    = context->sc_rdx;
427         regs->tf_rbp    = context->sc_rbp;
428         regs->tf_rbx    = context->sc_rbx;
429         regs->tf_rcx    = context->sc_rcx;
430         regs->tf_rax    = context->sc_rax;
431         regs->tf_rip    = context->sc_rip;
432         regs->tf_rsp    = context->sc_rsp;
433         regs->tf_r8     = context->sc_r8;
434         regs->tf_r9     = context->sc_r9;
435         regs->tf_r10    = context->sc_r10;
436         regs->tf_r11    = context->sc_r11;
437         regs->tf_r12    = context->sc_r12;
438         regs->tf_r13    = context->sc_r13;
439         regs->tf_r14    = context->sc_r14;
440         regs->tf_r15    = context->sc_r15;
441         regs->tf_cs     = context->sc_cs;
442         regs->tf_err    = context->sc_err;
443         regs->tf_rflags = rflags;
444
445         error = linux_copyin_fpstate(td, &sf.sf_uc);
446         if (error != 0) {
447                 uprintf("pid %d comm %s linux can't restore fpu state %d\n",
448                     p->p_pid, p->p_comm, error);
449                 return (error);
450         }
451
452         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
453         return (EJUSTRETURN);
454 }
455
456 static int
457 linux_fxsave(mcontext_t *mcp, void *ufp)
458 {
459         struct l_fpstate *fx = (struct l_fpstate *)&mcp->mc_fpstate[0];
460
461         bzero(&fx->reserved2[0], sizeof(fx->reserved2));
462         return (copyout(fx, ufp, sizeof(*fx)));
463 }
464
465 static int
466 linux_xsave(mcontext_t *mcp, char *xfpusave, char *ufp)
467 {
468         struct l_fpstate *fx = (struct l_fpstate *)&mcp->mc_fpstate[0];
469         uint32_t magic2;
470         int error;
471
472         /* Legacy region of an xsave area. */
473         fx->sw_reserved.magic1 = LINUX_FP_XSTATE_MAGIC1;
474         fx->sw_reserved.xstate_size = mcp->mc_xfpustate_len + sizeof(*fx);
475         fx->sw_reserved.extended_size = fx->sw_reserved.xstate_size +
476             LINUX_FP_XSTATE_MAGIC2_SIZE;
477         fx->sw_reserved.xfeatures = xsave_mask;
478
479         error = copyout(fx, ufp, sizeof(*fx));
480         if (error != 0)
481                 return (error);
482         ufp += sizeof(*fx);
483
484         /* Extended region of an xsave area. */
485         error = copyout(xfpusave, ufp, mcp->mc_xfpustate_len);
486         if (error != 0)
487                 return (error);
488
489         /* Linux specific end of xsave area marker. */
490         ufp += mcp->mc_xfpustate_len;
491         magic2 = LINUX_FP_XSTATE_MAGIC2;
492         return (copyout(&magic2, ufp, LINUX_FP_XSTATE_MAGIC2_SIZE));
493 }
494
495 static int
496 linux_copyout_fpstate(struct thread *td, struct l_ucontext *uc, char **sp)
497 {
498         size_t xfpusave_len;
499         char *xfpusave;
500         mcontext_t mc;
501         char *ufp = *sp;
502
503         get_fpcontext(td, &mc, &xfpusave, &xfpusave_len);
504         KASSERT(mc.mc_fpformat != _MC_FPFMT_NODEV, ("fpu not present"));
505
506         /* Room for fxsave area. */
507         ufp -= sizeof(struct l_fpstate);
508         if (xfpusave != NULL) {
509                 /* Room for xsave area. */
510                 ufp -= (xfpusave_len + LINUX_FP_XSTATE_MAGIC2_SIZE);
511                 uc->uc_flags |= LINUX_UC_FP_XSTATE;
512         }
513         *sp = ufp = (char *)((unsigned long)ufp & ~0x3Ful);
514
515         if (xfpusave != NULL)
516                 return (linux_xsave(&mc, xfpusave, ufp));
517         else
518                 return (linux_fxsave(&mc, ufp));
519 }
520
521 /*
522  * copied from amd64/amd64/machdep.c
523  *
524  * Send an interrupt to process.
525  */
526 static void
527 linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
528 {
529         struct l_rt_sigframe sf, *sfp;
530         struct proc *p;
531         struct thread *td;
532         struct sigacts *psp;
533         char *sp;
534         struct trapframe *regs;
535         int sig, code;
536         int oonstack, issiginfo;
537
538         td = curthread;
539         p = td->td_proc;
540         PROC_LOCK_ASSERT(p, MA_OWNED);
541         sig = linux_translate_traps(ksi->ksi_signo, ksi->ksi_trapno);
542         psp = p->p_sigacts;
543         issiginfo = SIGISMEMBER(psp->ps_siginfo, sig);
544         code = ksi->ksi_code;
545         mtx_assert(&psp->ps_mtx, MA_OWNED);
546         regs = td->td_frame;
547         oonstack = sigonstack(regs->tf_rsp);
548
549         LINUX_CTR4(rt_sendsig, "%p, %d, %p, %u",
550             catcher, sig, mask, code);
551
552         bzero(&sf, sizeof(sf));
553         sf.sf_uc.uc_stack.ss_sp = PTROUT(td->td_sigstk.ss_sp);
554         sf.sf_uc.uc_stack.ss_size = td->td_sigstk.ss_size;
555         sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
556             ? ((oonstack) ? LINUX_SS_ONSTACK : 0) : LINUX_SS_DISABLE;
557
558         /* Allocate space for the signal handler context. */
559         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
560             SIGISMEMBER(psp->ps_sigonstack, sig)) {
561                 sp = (char *)td->td_sigstk.ss_sp + td->td_sigstk.ss_size;
562         } else
563                 sp = (char *)regs->tf_rsp - 128;
564
565         mtx_unlock(&psp->ps_mtx);
566         PROC_UNLOCK(p);
567
568         if (linux_copyout_fpstate(td, &sf.sf_uc, &sp) != 0) {
569                 uprintf("pid %d comm %s linux can't save fpu state, killing\n",
570                     p->p_pid, p->p_comm);
571                 PROC_LOCK(p);
572                 sigexit(td, SIGILL);
573         }
574         sf.sf_uc.uc_mcontext.sc_fpstate = (register_t)sp;
575
576         /* Make room, keeping the stack aligned. */
577         sp -= sizeof(struct l_rt_sigframe);
578         sfp = (struct l_rt_sigframe *)((unsigned long)sp & ~0xFul);
579
580         /* Save user context. */
581         bsd_to_linux_sigset(mask, &sf.sf_uc.uc_sigmask);
582         sf.sf_uc.uc_mcontext.sc_mask   = sf.sf_uc.uc_sigmask;
583         sf.sf_uc.uc_mcontext.sc_rdi    = regs->tf_rdi;
584         sf.sf_uc.uc_mcontext.sc_rsi    = regs->tf_rsi;
585         sf.sf_uc.uc_mcontext.sc_rdx    = regs->tf_rdx;
586         sf.sf_uc.uc_mcontext.sc_rbp    = regs->tf_rbp;
587         sf.sf_uc.uc_mcontext.sc_rbx    = regs->tf_rbx;
588         sf.sf_uc.uc_mcontext.sc_rcx    = regs->tf_rcx;
589         sf.sf_uc.uc_mcontext.sc_rax    = regs->tf_rax;
590         sf.sf_uc.uc_mcontext.sc_rip    = regs->tf_rip;
591         sf.sf_uc.uc_mcontext.sc_rsp    = regs->tf_rsp;
592         sf.sf_uc.uc_mcontext.sc_r8     = regs->tf_r8;
593         sf.sf_uc.uc_mcontext.sc_r9     = regs->tf_r9;
594         sf.sf_uc.uc_mcontext.sc_r10    = regs->tf_r10;
595         sf.sf_uc.uc_mcontext.sc_r11    = regs->tf_r11;
596         sf.sf_uc.uc_mcontext.sc_r12    = regs->tf_r12;
597         sf.sf_uc.uc_mcontext.sc_r13    = regs->tf_r13;
598         sf.sf_uc.uc_mcontext.sc_r14    = regs->tf_r14;
599         sf.sf_uc.uc_mcontext.sc_r15    = regs->tf_r15;
600         sf.sf_uc.uc_mcontext.sc_cs     = regs->tf_cs;
601         sf.sf_uc.uc_mcontext.sc_rflags = regs->tf_rflags;
602         sf.sf_uc.uc_mcontext.sc_err    = regs->tf_err;
603         sf.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code);
604         sf.sf_uc.uc_mcontext.sc_cr2    = (register_t)ksi->ksi_addr;
605
606         /* Translate the signal. */
607         sig = bsd_to_linux_signal(sig);
608         /* Fill in POSIX parts. */
609         siginfo_to_lsiginfo(&ksi->ksi_info, &sf.sf_si, sig);
610
611         /* Copy the sigframe out to the user's stack. */
612         if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
613                 uprintf("pid %d comm %s has trashed its stack, killing\n",
614                     p->p_pid, p->p_comm);
615                 PROC_LOCK(p);
616                 sigexit(td, SIGILL);
617         }
618
619         fpstate_drop(td);
620         /* Build the argument list for the signal handler. */
621         regs->tf_rdi = sig;                     /* arg 1 in %rdi */
622         regs->tf_rax = 0;
623         if (issiginfo) {
624                 regs->tf_rsi = (register_t)&sfp->sf_si; /* arg 2 in %rsi */
625                 regs->tf_rdx = (register_t)&sfp->sf_uc; /* arg 3 in %rdx */
626         } else {
627                 regs->tf_rsi = 0;
628                 regs->tf_rdx = 0;
629         }
630         regs->tf_rcx = (register_t)catcher;
631         regs->tf_rsp = (long)sfp;
632         regs->tf_rip = linux_rt_sigcode;
633         regs->tf_rflags &= ~(PSL_T | PSL_D);
634         regs->tf_cs = _ucodesel;
635         set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
636         PROC_LOCK(p);
637         mtx_lock(&psp->ps_mtx);
638 }
639
640 #define LINUX_VSYSCALL_START            (-10UL << 20)
641 #define LINUX_VSYSCALL_SZ               1024
642
643 const unsigned long linux_vsyscall_vector[] = {
644         LINUX_SYS_gettimeofday,
645         LINUX_SYS_linux_time,
646         LINUX_SYS_linux_getcpu,
647 };
648
649 static int
650 linux_vsyscall(struct thread *td)
651 {
652         struct trapframe *frame;
653         uint64_t retqaddr;
654         int code, traced;
655         int error;
656
657         frame = td->td_frame;
658
659         /* Check %rip for vsyscall area. */
660         if (__predict_true(frame->tf_rip < LINUX_VSYSCALL_START))
661                 return (EINVAL);
662         if ((frame->tf_rip & (LINUX_VSYSCALL_SZ - 1)) != 0)
663                 return (EINVAL);
664         code = (frame->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SZ;
665         if (code >= nitems(linux_vsyscall_vector))
666                 return (EINVAL);
667
668         /*
669          * vsyscall called as callq *(%rax), so we must
670          * use return address from %rsp and also fixup %rsp.
671          */
672         error = copyin((void *)frame->tf_rsp, &retqaddr, sizeof(retqaddr));
673         if (error)
674                 return (error);
675
676         frame->tf_rip = retqaddr;
677         frame->tf_rax = linux_vsyscall_vector[code];
678         frame->tf_rsp += 8;
679
680         traced = (frame->tf_flags & PSL_T);
681
682         amd64_syscall(td, traced);
683
684         return (0);
685 }
686
687 struct sysentvec elf_linux_sysvec = {
688         .sv_size        = LINUX_SYS_MAXSYSCALL,
689         .sv_table       = linux_sysent,
690         .sv_fixup       = __elfN(freebsd_fixup),
691         .sv_sendsig     = linux_rt_sendsig,
692         .sv_sigcode     = &_binary_linux_vdso_so_o_start,
693         .sv_szsigcode   = &linux_szsigcode,
694         .sv_name        = "Linux ELF64",
695         .sv_coredump    = elf64_coredump,
696         .sv_elf_core_osabi = ELFOSABI_NONE,
697         .sv_elf_core_abi_vendor = LINUX_ABI_VENDOR,
698         .sv_elf_core_prepare_notes = linux64_prepare_notes,
699         .sv_minsigstksz = LINUX_MINSIGSTKSZ,
700         .sv_minuser     = VM_MIN_ADDRESS,
701         .sv_maxuser     = VM_MAXUSER_ADDRESS_LA48,
702         .sv_usrstack    = LINUX_USRSTACK_LA48,
703         .sv_psstrings   = LINUX_PS_STRINGS_LA48,
704         .sv_psstringssz = sizeof(struct ps_strings),
705         .sv_stackprot   = VM_PROT_ALL,
706         .sv_copyout_auxargs = __linuxN(copyout_auxargs),
707         .sv_copyout_strings = __linuxN(copyout_strings),
708         .sv_setregs     = linux_exec_setregs,
709         .sv_fixlimit    = NULL,
710         .sv_maxssiz     = NULL,
711         .sv_flags       = SV_ABI_LINUX | SV_LP64 | SV_SHP | SV_SIG_DISCIGN |
712             SV_SIG_WAITNDQ | SV_TIMEKEEP,
713         .sv_set_syscall_retval = linux_set_syscall_retval,
714         .sv_fetch_syscall_args = linux_fetch_syscall_args,
715         .sv_syscallnames = linux_syscallnames,
716         .sv_shared_page_base = LINUX_SHAREDPAGE_LA48,
717         .sv_shared_page_len = PAGE_SIZE,
718         .sv_schedtail   = linux_schedtail,
719         .sv_thread_detach = linux_thread_detach,
720         .sv_trap        = linux_vsyscall,
721         .sv_hwcap       = NULL,
722         .sv_hwcap2      = NULL,
723         .sv_onexec      = linux_on_exec_vmspace,
724         .sv_onexit      = linux_on_exit,
725         .sv_ontdexit    = linux_thread_dtor,
726         .sv_setid_allowed = &linux_setid_allowed_query,
727         .sv_set_fork_retval = linux_set_fork_retval,
728 };
729
730 static int
731 linux_on_exec_vmspace(struct proc *p, struct image_params *imgp)
732 {
733         int error;
734
735         error = linux_map_vdso(p, linux_vdso_obj, linux_vdso_base,
736             LINUX_VDSOPAGE_SIZE, imgp);
737         if (error == 0)
738                 error = linux_on_exec(p, imgp);
739         return (error);
740 }
741
742 /*
743  * linux_vdso_install() and linux_exec_sysvec_init() must be called
744  * after exec_sysvec_init() which is SI_SUB_EXEC (SI_ORDER_ANY).
745  */
746 static void
747 linux_exec_sysvec_init(void *param)
748 {
749         l_uintptr_t *ktimekeep_base, *ktsc_selector;
750         struct sysentvec *sv;
751         ptrdiff_t tkoff;
752
753         sv = param;
754         amd64_lower_shared_page(sv);
755         /* Fill timekeep_base */
756         exec_sysvec_init(sv);
757
758         tkoff = kern_timekeep_base - linux_vdso_base;
759         ktimekeep_base = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
760         *ktimekeep_base = sv->sv_shared_page_base + sv->sv_timekeep_offset;
761
762         tkoff = kern_tsc_selector - linux_vdso_base;
763         ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
764         *ktsc_selector = linux_vdso_tsc_selector_idx();
765         if (bootverbose)
766                 printf("Linux x86-64 vDSO tsc_selector: %lu\n", *ktsc_selector);
767
768         tkoff = kern_cpu_selector - linux_vdso_base;
769         ktsc_selector = (l_uintptr_t *)(linux_vdso_mapping + tkoff);
770         *ktsc_selector = linux_vdso_cpu_selector_idx();
771         if (bootverbose)
772                 printf("Linux x86-64 vDSO cpu_selector: %lu\n", *ktsc_selector);
773 }
774 SYSINIT(elf_linux_exec_sysvec_init, SI_SUB_EXEC + 1, SI_ORDER_ANY,
775     linux_exec_sysvec_init, &elf_linux_sysvec);
776
777 static void
778 linux_vdso_install(const void *param)
779 {
780         char *vdso_start = &_binary_linux_vdso_so_o_start;
781         char *vdso_end = &_binary_linux_vdso_so_o_end;
782
783         linux_szsigcode = vdso_end - vdso_start;
784         MPASS(linux_szsigcode <= LINUX_VDSOPAGE_SIZE);
785
786         linux_vdso_base = LINUX_VDSOPAGE_LA48;
787         if (hw_lower_amd64_sharedpage != 0)
788                 linux_vdso_base -= PAGE_SIZE;
789
790         __elfN(linux_vdso_fixup)(vdso_start, linux_vdso_base);
791
792         linux_vdso_obj = __elfN(linux_shared_page_init)
793             (&linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
794         bcopy(vdso_start, linux_vdso_mapping, linux_szsigcode);
795
796         linux_vdso_reloc(linux_vdso_mapping, linux_vdso_base);
797 }
798 SYSINIT(elf_linux_vdso_init, SI_SUB_EXEC + 1, SI_ORDER_FIRST,
799     linux_vdso_install, NULL);
800
801 static void
802 linux_vdso_deinstall(const void *param)
803 {
804
805         __elfN(linux_shared_page_fini)(linux_vdso_obj,
806             linux_vdso_mapping, LINUX_VDSOPAGE_SIZE);
807 }
808 SYSUNINIT(elf_linux_vdso_uninit, SI_SUB_EXEC, SI_ORDER_FIRST,
809     linux_vdso_deinstall, NULL);
810
811 static void
812 linux_vdso_reloc(char *mapping, Elf_Addr offset)
813 {
814         const Elf_Ehdr *ehdr;
815         const Elf_Shdr *shdr;
816         Elf64_Addr *where, val;
817         Elf_Size rtype, symidx;
818         const Elf_Rela *rela;
819         Elf_Addr addr, addend;
820         int relacnt;
821         int i, j;
822
823         MPASS(offset != 0);
824
825         relacnt = 0;
826         ehdr = (const Elf_Ehdr *)mapping;
827         shdr = (const Elf_Shdr *)(mapping + ehdr->e_shoff);
828         for (i = 0; i < ehdr->e_shnum; i++)
829         {
830                 switch (shdr[i].sh_type) {
831                 case SHT_REL:
832                         printf("Linux x86_64 vDSO: unexpected Rel section\n");
833                         break;
834                 case SHT_RELA:
835                         rela = (const Elf_Rela *)(mapping + shdr[i].sh_offset);
836                         relacnt = shdr[i].sh_size / sizeof(*rela);
837                 }
838         }
839
840         for (j = 0; j < relacnt; j++, rela++) {
841                 where = (Elf_Addr *)(mapping + rela->r_offset);
842                 addend = rela->r_addend;
843                 rtype = ELF_R_TYPE(rela->r_info);
844                 symidx = ELF_R_SYM(rela->r_info);
845
846                 switch (rtype) {
847                 case R_X86_64_NONE:     /* none */
848                         break;
849
850                 case R_X86_64_RELATIVE: /* B + A */
851                         addr = (Elf_Addr)(offset + addend);
852                         val = addr;
853                         if (*where != val)
854                                 *where = val;
855                         break;
856                 case R_X86_64_IRELATIVE:
857                         printf("Linux x86_64 vDSO: unexpected ifunc relocation, "
858                             "symbol index %ld\n", symidx);
859                         break;
860                 default:
861                         printf("Linux x86_64 vDSO: unexpected relocation type %ld, "
862                             "symbol index %ld\n", rtype, symidx);
863                 }
864         }
865 }
866
867 static Elf_Brandnote linux64_brandnote = {
868         .hdr.n_namesz   = sizeof(GNU_ABI_VENDOR),
869         .hdr.n_descsz   = 16,
870         .hdr.n_type     = 1,
871         .vendor         = GNU_ABI_VENDOR,
872         .flags          = BN_TRANSLATE_OSREL,
873         .trans_osrel    = linux_trans_osrel
874 };
875
876 static Elf64_Brandinfo linux_glibc2brand = {
877         .brand          = ELFOSABI_LINUX,
878         .machine        = EM_X86_64,
879         .compat_3_brand = "Linux",
880         .interp_path    = "/lib64/ld-linux-x86-64.so.2",
881         .sysvec         = &elf_linux_sysvec,
882         .interp_newpath = NULL,
883         .brand_note     = &linux64_brandnote,
884         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
885 };
886
887 static Elf64_Brandinfo linux_glibc2brandshort = {
888         .brand          = ELFOSABI_LINUX,
889         .machine        = EM_X86_64,
890         .compat_3_brand = "Linux",
891         .interp_path    = "/lib64/ld-linux.so.2",
892         .sysvec         = &elf_linux_sysvec,
893         .interp_newpath = NULL,
894         .brand_note     = &linux64_brandnote,
895         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
896 };
897
898 static Elf64_Brandinfo linux_muslbrand = {
899         .brand          = ELFOSABI_LINUX,
900         .machine        = EM_X86_64,
901         .compat_3_brand = "Linux",
902         .interp_path    = "/lib/ld-musl-x86_64.so.1",
903         .sysvec         = &elf_linux_sysvec,
904         .interp_newpath = NULL,
905         .brand_note     = &linux64_brandnote,
906         .flags          = BI_CAN_EXEC_DYN | BI_BRAND_NOTE |
907                             LINUX_BI_FUTEX_REQUEUE
908 };
909
910 Elf64_Brandinfo *linux_brandlist[] = {
911         &linux_glibc2brand,
912         &linux_glibc2brandshort,
913         &linux_muslbrand,
914         NULL
915 };
916
917 static int
918 linux64_elf_modevent(module_t mod, int type, void *data)
919 {
920         Elf64_Brandinfo **brandinfo;
921         int error;
922         struct linux_ioctl_handler **lihp;
923
924         error = 0;
925
926         switch(type) {
927         case MOD_LOAD:
928                 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
929                      ++brandinfo)
930                         if (elf64_insert_brand_entry(*brandinfo) < 0)
931                                 error = EINVAL;
932                 if (error == 0) {
933                         SET_FOREACH(lihp, linux_ioctl_handler_set)
934                                 linux_ioctl_register_handler(*lihp);
935                         stclohz = (stathz ? stathz : hz);
936                         if (bootverbose)
937                                 printf("Linux x86-64 ELF exec handler installed\n");
938                 } else
939                         printf("cannot insert Linux x86-64 ELF brand handler\n");
940                 break;
941         case MOD_UNLOAD:
942                 for (brandinfo = &linux_brandlist[0]; *brandinfo != NULL;
943                      ++brandinfo)
944                         if (elf64_brand_inuse(*brandinfo))
945                                 error = EBUSY;
946                 if (error == 0) {
947                         for (brandinfo = &linux_brandlist[0];
948                              *brandinfo != NULL; ++brandinfo)
949                                 if (elf64_remove_brand_entry(*brandinfo) < 0)
950                                         error = EINVAL;
951                 }
952                 if (error == 0) {
953                         SET_FOREACH(lihp, linux_ioctl_handler_set)
954                                 linux_ioctl_unregister_handler(*lihp);
955                         if (bootverbose)
956                                 printf("Linux x86_64 ELF exec handler removed\n");
957                 } else
958                         printf("Could not deinstall Linux x86_64 ELF interpreter entry\n");
959                 break;
960         default:
961                 return (EOPNOTSUPP);
962         }
963         return (error);
964 }
965
966 static moduledata_t linux64_elf_mod = {
967         "linux64elf",
968         linux64_elf_modevent,
969         0
970 };
971
972 DECLARE_MODULE_TIED(linux64elf, linux64_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);
973 MODULE_DEPEND(linux64elf, linux_common, 1, 1, 1);
974 FEATURE(linux64, "Linux 64bit support");