2 * Copyright (c) 2013 Dmitry Chagin
3 * Copyright (c) 2004 Tim J. Robbins
4 * Copyright (c) 2002 Doug Rabson
5 * Copyright (c) 2000 Marcel Moolenaar
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/capsicum.h>
37 #include <sys/clock.h>
38 #include <sys/dirent.h>
39 #include <sys/fcntl.h>
41 #include <sys/filedesc.h>
42 #include <sys/imgact.h>
43 #include <sys/kernel.h>
45 #include <sys/limits.h>
47 #include <sys/malloc.h>
49 #include <sys/mutex.h>
52 #include <sys/resource.h>
53 #include <sys/resourcevar.h>
54 #include <sys/sched.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysproto.h>
57 #include <sys/systm.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
62 #include <security/mac/mac_framework.h>
64 #include <ufs/ufs/extattr.h>
65 #include <ufs/ufs/quota.h>
66 #include <ufs/ufs/ufsmount.h>
68 #include <machine/frame.h>
69 #include <machine/md_var.h>
70 #include <machine/pcb.h>
71 #include <machine/psl.h>
72 #include <machine/segments.h>
73 #include <machine/specialreg.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_kern.h>
79 #include <vm/vm_map.h>
81 #include <x86/ifunc.h>
82 #include <x86/sysarch.h>
84 #include <security/audit/audit.h>
86 #include <amd64/linux/linux.h>
87 #include <amd64/linux/linux_proto.h>
88 #include <compat/linux/linux_emul.h>
89 #include <compat/linux/linux_file.h>
90 #include <compat/linux/linux_ipc.h>
91 #include <compat/linux/linux_misc.h>
92 #include <compat/linux/linux_mmap.h>
93 #include <compat/linux/linux_signal.h>
94 #include <compat/linux/linux_util.h>
97 linux_execve(struct thread *td, struct linux_execve_args *args)
99 struct image_args eargs;
105 if (!LUSECONVPATH(td)) {
106 error = exec_copyin_args(&eargs, args->path, UIO_USERSPACE,
107 args->argp, args->envp);
109 LCONVPATHEXIST(td, args->path, &path);
110 error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp,
115 error = linux_common_execve(td, &eargs);
116 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
121 linux_set_upcall_kse(struct thread *td, register_t stack)
125 td->td_frame->tf_rsp = stack;
128 * The newly created Linux thread returns
129 * to the user space by the same path that a parent does.
131 td->td_frame->tf_rax = 0;
136 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
139 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
140 args->flags, args->fd, args->pgoff));
144 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
147 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
151 linux_madvise(struct thread *td, struct linux_madvise_args *uap)
154 return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav));
158 linux_iopl(struct thread *td, struct linux_iopl_args *args)
166 if ((error = priv_check(td, PRIV_IO)) != 0)
168 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
170 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
171 (args->level * (PSL_IOPL / 3));
177 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
183 LINUX_CTR2(rt_sigsuspend, "%p, %ld",
184 uap->newset, uap->sigsetsize);
186 if (uap->sigsetsize != sizeof(l_sigset_t))
189 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
193 linux_to_bsd_sigset(&lmask, &sigmask);
194 return (kern_sigsuspend(td, sigmask));
198 linux_pause(struct thread *td, struct linux_pause_args *args)
200 struct proc *p = td->td_proc;
206 sigmask = td->td_sigmask;
208 return (kern_sigsuspend(td, sigmask));
212 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
218 memset(&lss, 0, sizeof(lss));
219 LINUX_CTR2(sigaltstack, "%p, %p", uap->uss, uap->uoss);
221 if (uap->uss != NULL) {
222 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
226 ss.ss_sp = PTRIN(lss.ss_sp);
227 ss.ss_size = lss.ss_size;
228 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
230 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
231 (uap->uoss != NULL) ? &oss : NULL);
232 if (!error && uap->uoss != NULL) {
233 lss.ss_sp = PTROUT(oss.ss_sp);
234 lss.ss_size = oss.ss_size;
235 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
236 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
243 linux_arch_prctl(struct thread *td, struct linux_arch_prctl_args *args)
249 LINUX_CTR2(arch_prctl, "0x%x, %p", args->code, args->addr);
251 switch (args->code) {
252 case LINUX_ARCH_SET_GS:
253 if (args->addr < VM_MAXUSER_ADDRESS) {
254 set_pcb_flags(pcb, PCB_FULL_IRET);
255 pcb->pcb_gsbase = args->addr;
256 td->td_frame->tf_gs = _ugssel;
261 case LINUX_ARCH_SET_FS:
262 if (args->addr < VM_MAXUSER_ADDRESS) {
263 set_pcb_flags(pcb, PCB_FULL_IRET);
264 pcb->pcb_fsbase = args->addr;
265 td->td_frame->tf_fs = _ufssel;
270 case LINUX_ARCH_GET_FS:
271 error = copyout(&pcb->pcb_fsbase, PTRIN(args->addr),
274 case LINUX_ARCH_GET_GS:
275 error = copyout(&pcb->pcb_gsbase, PTRIN(args->addr),
285 linux_set_cloned_tls(struct thread *td, void *desc)
289 if ((uint64_t)desc >= VM_MAXUSER_ADDRESS)
293 pcb->pcb_fsbase = (register_t)desc;
294 td->td_frame->tf_fs = _ufssel;
299 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
300 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval);
301 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *))
304 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
305 futex_xchgl_smap : futex_xchgl_nosmap);
308 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
309 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval);
310 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *))
313 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
314 futex_addl_smap : futex_addl_nosmap);
317 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
318 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval);
319 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *))
322 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
323 futex_orl_smap : futex_orl_nosmap);
326 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
327 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval);
328 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *))
331 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
332 futex_andl_smap : futex_andl_nosmap);
335 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
336 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval);
337 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *))
340 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
341 futex_xorl_smap : futex_xorl_nosmap);