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/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/capsicum.h>
39 #include <sys/dirent.h>
41 #include <sys/fcntl.h>
42 #include <sys/filedesc.h>
43 #include <sys/clock.h>
44 #include <sys/imgact.h>
46 #include <sys/limits.h>
48 #include <sys/malloc.h>
50 #include <sys/mutex.h>
53 #include <sys/resource.h>
54 #include <sys/resourcevar.h>
55 #include <sys/sched.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/sysproto.h>
58 #include <sys/vnode.h>
59 #include <sys/unistd.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 <amd64/linux/linux.h>
82 #include <amd64/linux/linux_proto.h>
83 #include <compat/linux/linux_ipc.h>
84 #include <compat/linux/linux_file.h>
85 #include <compat/linux/linux_misc.h>
86 #include <compat/linux/linux_mmap.h>
87 #include <compat/linux/linux_signal.h>
88 #include <compat/linux/linux_util.h>
89 #include <compat/linux/linux_emul.h>
91 #include <x86/include/sysarch.h>
94 linux_execve(struct thread *td, struct linux_execve_args *args)
96 struct image_args eargs;
100 LCONVPATHEXIST(td, args->path, &path);
104 error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp,
108 error = linux_common_execve(td, &eargs);
113 linux_set_upcall_kse(struct thread *td, register_t stack)
117 td->td_frame->tf_rsp = stack;
120 * The newly created Linux thread returns
121 * to the user space by the same path that a parent do.
123 td->td_frame->tf_rax = 0;
128 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
131 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
132 args->flags, args->fd, args->pgoff));
136 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
139 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
143 linux_iopl(struct thread *td, struct linux_iopl_args *args)
151 if ((error = priv_check(td, PRIV_IO)) != 0)
153 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
155 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
156 (args->level * (PSL_IOPL / 3));
162 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
168 LINUX_CTR2(rt_sigsuspend, "%p, %ld",
169 uap->newset, uap->sigsetsize);
171 if (uap->sigsetsize != sizeof(l_sigset_t))
174 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
178 linux_to_bsd_sigset(&lmask, &sigmask);
179 return (kern_sigsuspend(td, sigmask));
183 linux_pause(struct thread *td, struct linux_pause_args *args)
185 struct proc *p = td->td_proc;
191 sigmask = td->td_sigmask;
193 return (kern_sigsuspend(td, sigmask));
197 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
203 LINUX_CTR2(sigaltstack, "%p, %p", uap->uss, uap->uoss);
205 if (uap->uss != NULL) {
206 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
210 ss.ss_sp = PTRIN(lss.ss_sp);
211 ss.ss_size = lss.ss_size;
212 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
214 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
215 (uap->uoss != NULL) ? &oss : NULL);
216 if (!error && uap->uoss != NULL) {
217 lss.ss_sp = PTROUT(oss.ss_sp);
218 lss.ss_size = oss.ss_size;
219 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
220 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
227 linux_arch_prctl(struct thread *td, struct linux_arch_prctl_args *args)
230 struct sysarch_args bsd_args;
232 LINUX_CTR2(arch_prctl, "0x%x, %p", args->code, args->addr);
234 switch (args->code) {
235 case LINUX_ARCH_SET_GS:
236 bsd_args.op = AMD64_SET_GSBASE;
237 bsd_args.parms = (void *)args->addr;
238 error = sysarch(td, &bsd_args);
242 case LINUX_ARCH_SET_FS:
243 bsd_args.op = AMD64_SET_FSBASE;
244 bsd_args.parms = (void *)args->addr;
245 error = sysarch(td, &bsd_args);
249 case LINUX_ARCH_GET_FS:
250 bsd_args.op = AMD64_GET_FSBASE;
251 bsd_args.parms = (void *)args->addr;
252 error = sysarch(td, &bsd_args);
254 case LINUX_ARCH_GET_GS:
255 bsd_args.op = AMD64_GET_GSBASE;
256 bsd_args.parms = (void *)args->addr;
257 error = sysarch(td, &bsd_args);
266 linux_set_cloned_tls(struct thread *td, void *desc)
270 if ((uint64_t)desc >= VM_MAXUSER_ADDRESS)
274 pcb->pcb_fsbase = (register_t)desc;
275 td->td_frame->tf_fs = _ufssel;