1a9643ea8Slogwang /*-
2a9643ea8Slogwang * Copyright (c) 2013 Dmitry Chagin
3a9643ea8Slogwang * Copyright (c) 2004 Tim J. Robbins
4a9643ea8Slogwang * Copyright (c) 2002 Doug Rabson
5a9643ea8Slogwang * Copyright (c) 2000 Marcel Moolenaar
6a9643ea8Slogwang * All rights reserved.
7a9643ea8Slogwang *
8a9643ea8Slogwang * Redistribution and use in source and binary forms, with or without
9a9643ea8Slogwang * modification, are permitted provided that the following conditions
10a9643ea8Slogwang * are met:
11a9643ea8Slogwang * 1. Redistributions of source code must retain the above copyright
12a9643ea8Slogwang * notice, this list of conditions and the following disclaimer
13a9643ea8Slogwang * in this position and unchanged.
14a9643ea8Slogwang * 2. Redistributions in binary form must reproduce the above copyright
15a9643ea8Slogwang * notice, this list of conditions and the following disclaimer in the
16a9643ea8Slogwang * documentation and/or other materials provided with the distribution.
17a9643ea8Slogwang * 3. The name of the author may not be used to endorse or promote products
18a9643ea8Slogwang * derived from this software without specific prior written permission.
19a9643ea8Slogwang *
20a9643ea8Slogwang * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21a9643ea8Slogwang * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22a9643ea8Slogwang * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23a9643ea8Slogwang * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24a9643ea8Slogwang * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25a9643ea8Slogwang * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26a9643ea8Slogwang * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27a9643ea8Slogwang * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28a9643ea8Slogwang * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29a9643ea8Slogwang * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30a9643ea8Slogwang */
31a9643ea8Slogwang
32a9643ea8Slogwang #include <sys/cdefs.h>
33a9643ea8Slogwang __FBSDID("$FreeBSD$");
34a9643ea8Slogwang
35a9643ea8Slogwang #include <sys/param.h>
36*22ce4affSfengbojiang #include <sys/capsicum.h>
37a9643ea8Slogwang #include <sys/clock.h>
38*22ce4affSfengbojiang #include <sys/dirent.h>
39*22ce4affSfengbojiang #include <sys/fcntl.h>
40*22ce4affSfengbojiang #include <sys/file.h>
41*22ce4affSfengbojiang #include <sys/filedesc.h>
42a9643ea8Slogwang #include <sys/imgact.h>
43*22ce4affSfengbojiang #include <sys/kernel.h>
44a9643ea8Slogwang #include <sys/ktr.h>
45a9643ea8Slogwang #include <sys/limits.h>
46a9643ea8Slogwang #include <sys/lock.h>
47a9643ea8Slogwang #include <sys/malloc.h>
48a9643ea8Slogwang #include <sys/mman.h>
49a9643ea8Slogwang #include <sys/mutex.h>
50a9643ea8Slogwang #include <sys/priv.h>
51a9643ea8Slogwang #include <sys/proc.h>
52a9643ea8Slogwang #include <sys/resource.h>
53a9643ea8Slogwang #include <sys/resourcevar.h>
54a9643ea8Slogwang #include <sys/sched.h>
55a9643ea8Slogwang #include <sys/syscallsubr.h>
56a9643ea8Slogwang #include <sys/sysproto.h>
57*22ce4affSfengbojiang #include <sys/systm.h>
58a9643ea8Slogwang #include <sys/unistd.h>
59*22ce4affSfengbojiang #include <sys/vnode.h>
60a9643ea8Slogwang #include <sys/wait.h>
61a9643ea8Slogwang
62a9643ea8Slogwang #include <security/mac/mac_framework.h>
63a9643ea8Slogwang
64a9643ea8Slogwang #include <ufs/ufs/extattr.h>
65a9643ea8Slogwang #include <ufs/ufs/quota.h>
66a9643ea8Slogwang #include <ufs/ufs/ufsmount.h>
67a9643ea8Slogwang
68a9643ea8Slogwang #include <machine/frame.h>
69a9643ea8Slogwang #include <machine/md_var.h>
70a9643ea8Slogwang #include <machine/pcb.h>
71a9643ea8Slogwang #include <machine/psl.h>
72a9643ea8Slogwang #include <machine/segments.h>
73a9643ea8Slogwang #include <machine/specialreg.h>
74a9643ea8Slogwang
75a9643ea8Slogwang #include <vm/pmap.h>
76*22ce4affSfengbojiang #include <vm/vm.h>
77a9643ea8Slogwang #include <vm/vm_extern.h>
78a9643ea8Slogwang #include <vm/vm_kern.h>
79a9643ea8Slogwang #include <vm/vm_map.h>
80a9643ea8Slogwang
81*22ce4affSfengbojiang #include <x86/ifunc.h>
82*22ce4affSfengbojiang #include <x86/sysarch.h>
83*22ce4affSfengbojiang
84*22ce4affSfengbojiang #include <security/audit/audit.h>
85*22ce4affSfengbojiang
86a9643ea8Slogwang #include <amd64/linux/linux.h>
87a9643ea8Slogwang #include <amd64/linux/linux_proto.h>
88*22ce4affSfengbojiang #include <compat/linux/linux_emul.h>
89a9643ea8Slogwang #include <compat/linux/linux_file.h>
90*22ce4affSfengbojiang #include <compat/linux/linux_ipc.h>
91a9643ea8Slogwang #include <compat/linux/linux_misc.h>
92a9643ea8Slogwang #include <compat/linux/linux_mmap.h>
93a9643ea8Slogwang #include <compat/linux/linux_signal.h>
94a9643ea8Slogwang #include <compat/linux/linux_util.h>
95a9643ea8Slogwang
96a9643ea8Slogwang int
linux_execve(struct thread * td,struct linux_execve_args * args)97a9643ea8Slogwang linux_execve(struct thread *td, struct linux_execve_args *args)
98a9643ea8Slogwang {
99a9643ea8Slogwang struct image_args eargs;
100a9643ea8Slogwang char *path;
101a9643ea8Slogwang int error;
102a9643ea8Slogwang
103a9643ea8Slogwang LINUX_CTR(execve);
104a9643ea8Slogwang
105*22ce4affSfengbojiang if (!LUSECONVPATH(td)) {
106*22ce4affSfengbojiang error = exec_copyin_args(&eargs, args->path, UIO_USERSPACE,
107*22ce4affSfengbojiang args->argp, args->envp);
108*22ce4affSfengbojiang } else {
109*22ce4affSfengbojiang LCONVPATHEXIST(td, args->path, &path);
110a9643ea8Slogwang error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp,
111a9643ea8Slogwang args->envp);
112*22ce4affSfengbojiang LFREEPATH(path);
113*22ce4affSfengbojiang }
114a9643ea8Slogwang if (error == 0)
115a9643ea8Slogwang error = linux_common_execve(td, &eargs);
116*22ce4affSfengbojiang AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
117a9643ea8Slogwang return (error);
118a9643ea8Slogwang }
119a9643ea8Slogwang
120a9643ea8Slogwang int
linux_set_upcall_kse(struct thread * td,register_t stack)121a9643ea8Slogwang linux_set_upcall_kse(struct thread *td, register_t stack)
122a9643ea8Slogwang {
123a9643ea8Slogwang
124a9643ea8Slogwang if (stack)
125a9643ea8Slogwang td->td_frame->tf_rsp = stack;
126a9643ea8Slogwang
127a9643ea8Slogwang /*
128a9643ea8Slogwang * The newly created Linux thread returns
129*22ce4affSfengbojiang * to the user space by the same path that a parent does.
130a9643ea8Slogwang */
131a9643ea8Slogwang td->td_frame->tf_rax = 0;
132a9643ea8Slogwang return (0);
133a9643ea8Slogwang }
134a9643ea8Slogwang
135a9643ea8Slogwang int
linux_mmap2(struct thread * td,struct linux_mmap2_args * args)136a9643ea8Slogwang linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
137a9643ea8Slogwang {
138a9643ea8Slogwang
139a9643ea8Slogwang return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
140a9643ea8Slogwang args->flags, args->fd, args->pgoff));
141a9643ea8Slogwang }
142a9643ea8Slogwang
143a9643ea8Slogwang int
linux_mprotect(struct thread * td,struct linux_mprotect_args * uap)144a9643ea8Slogwang linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
145a9643ea8Slogwang {
146a9643ea8Slogwang
147a9643ea8Slogwang return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
148a9643ea8Slogwang }
149a9643ea8Slogwang
150a9643ea8Slogwang int
linux_madvise(struct thread * td,struct linux_madvise_args * uap)151*22ce4affSfengbojiang linux_madvise(struct thread *td, struct linux_madvise_args *uap)
152*22ce4affSfengbojiang {
153*22ce4affSfengbojiang
154*22ce4affSfengbojiang return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav));
155*22ce4affSfengbojiang }
156*22ce4affSfengbojiang
157*22ce4affSfengbojiang int
linux_iopl(struct thread * td,struct linux_iopl_args * args)158a9643ea8Slogwang linux_iopl(struct thread *td, struct linux_iopl_args *args)
159a9643ea8Slogwang {
160a9643ea8Slogwang int error;
161a9643ea8Slogwang
162a9643ea8Slogwang LINUX_CTR(iopl);
163a9643ea8Slogwang
164a9643ea8Slogwang if (args->level > 3)
165a9643ea8Slogwang return (EINVAL);
166a9643ea8Slogwang if ((error = priv_check(td, PRIV_IO)) != 0)
167a9643ea8Slogwang return (error);
168a9643ea8Slogwang if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
169a9643ea8Slogwang return (error);
170a9643ea8Slogwang td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
171a9643ea8Slogwang (args->level * (PSL_IOPL / 3));
172a9643ea8Slogwang
173a9643ea8Slogwang return (0);
174a9643ea8Slogwang }
175a9643ea8Slogwang
176a9643ea8Slogwang int
linux_rt_sigsuspend(struct thread * td,struct linux_rt_sigsuspend_args * uap)177a9643ea8Slogwang linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
178a9643ea8Slogwang {
179a9643ea8Slogwang l_sigset_t lmask;
180a9643ea8Slogwang sigset_t sigmask;
181a9643ea8Slogwang int error;
182a9643ea8Slogwang
183a9643ea8Slogwang LINUX_CTR2(rt_sigsuspend, "%p, %ld",
184a9643ea8Slogwang uap->newset, uap->sigsetsize);
185a9643ea8Slogwang
186a9643ea8Slogwang if (uap->sigsetsize != sizeof(l_sigset_t))
187a9643ea8Slogwang return (EINVAL);
188a9643ea8Slogwang
189a9643ea8Slogwang error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
190a9643ea8Slogwang if (error)
191a9643ea8Slogwang return (error);
192a9643ea8Slogwang
193a9643ea8Slogwang linux_to_bsd_sigset(&lmask, &sigmask);
194a9643ea8Slogwang return (kern_sigsuspend(td, sigmask));
195a9643ea8Slogwang }
196a9643ea8Slogwang
197a9643ea8Slogwang int
linux_pause(struct thread * td,struct linux_pause_args * args)198a9643ea8Slogwang linux_pause(struct thread *td, struct linux_pause_args *args)
199a9643ea8Slogwang {
200a9643ea8Slogwang struct proc *p = td->td_proc;
201a9643ea8Slogwang sigset_t sigmask;
202a9643ea8Slogwang
203a9643ea8Slogwang LINUX_CTR(pause);
204a9643ea8Slogwang
205a9643ea8Slogwang PROC_LOCK(p);
206a9643ea8Slogwang sigmask = td->td_sigmask;
207a9643ea8Slogwang PROC_UNLOCK(p);
208a9643ea8Slogwang return (kern_sigsuspend(td, sigmask));
209a9643ea8Slogwang }
210a9643ea8Slogwang
211a9643ea8Slogwang int
linux_sigaltstack(struct thread * td,struct linux_sigaltstack_args * uap)212a9643ea8Slogwang linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
213a9643ea8Slogwang {
214a9643ea8Slogwang stack_t ss, oss;
215a9643ea8Slogwang l_stack_t lss;
216a9643ea8Slogwang int error;
217a9643ea8Slogwang
218*22ce4affSfengbojiang memset(&lss, 0, sizeof(lss));
219a9643ea8Slogwang LINUX_CTR2(sigaltstack, "%p, %p", uap->uss, uap->uoss);
220a9643ea8Slogwang
221a9643ea8Slogwang if (uap->uss != NULL) {
222a9643ea8Slogwang error = copyin(uap->uss, &lss, sizeof(l_stack_t));
223a9643ea8Slogwang if (error)
224a9643ea8Slogwang return (error);
225a9643ea8Slogwang
226a9643ea8Slogwang ss.ss_sp = PTRIN(lss.ss_sp);
227a9643ea8Slogwang ss.ss_size = lss.ss_size;
228a9643ea8Slogwang ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
229a9643ea8Slogwang }
230a9643ea8Slogwang error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
231a9643ea8Slogwang (uap->uoss != NULL) ? &oss : NULL);
232a9643ea8Slogwang if (!error && uap->uoss != NULL) {
233a9643ea8Slogwang lss.ss_sp = PTROUT(oss.ss_sp);
234a9643ea8Slogwang lss.ss_size = oss.ss_size;
235a9643ea8Slogwang lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
236a9643ea8Slogwang error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
237a9643ea8Slogwang }
238a9643ea8Slogwang
239a9643ea8Slogwang return (error);
240a9643ea8Slogwang }
241a9643ea8Slogwang
242a9643ea8Slogwang int
linux_arch_prctl(struct thread * td,struct linux_arch_prctl_args * args)243a9643ea8Slogwang linux_arch_prctl(struct thread *td, struct linux_arch_prctl_args *args)
244a9643ea8Slogwang {
245a9643ea8Slogwang struct pcb *pcb;
246*22ce4affSfengbojiang int error;
247a9643ea8Slogwang
248*22ce4affSfengbojiang pcb = td->td_pcb;
249a9643ea8Slogwang LINUX_CTR2(arch_prctl, "0x%x, %p", args->code, args->addr);
250a9643ea8Slogwang
251a9643ea8Slogwang switch (args->code) {
252a9643ea8Slogwang case LINUX_ARCH_SET_GS:
253*22ce4affSfengbojiang if (args->addr < VM_MAXUSER_ADDRESS) {
254*22ce4affSfengbojiang set_pcb_flags(pcb, PCB_FULL_IRET);
255*22ce4affSfengbojiang pcb->pcb_gsbase = args->addr;
256*22ce4affSfengbojiang td->td_frame->tf_gs = _ugssel;
257*22ce4affSfengbojiang error = 0;
258*22ce4affSfengbojiang } else
259*22ce4affSfengbojiang error = EPERM;
260a9643ea8Slogwang break;
261a9643ea8Slogwang case LINUX_ARCH_SET_FS:
262*22ce4affSfengbojiang if (args->addr < VM_MAXUSER_ADDRESS) {
263*22ce4affSfengbojiang set_pcb_flags(pcb, PCB_FULL_IRET);
264*22ce4affSfengbojiang pcb->pcb_fsbase = args->addr;
265*22ce4affSfengbojiang td->td_frame->tf_fs = _ufssel;
266*22ce4affSfengbojiang error = 0;
267*22ce4affSfengbojiang } else
268*22ce4affSfengbojiang error = EPERM;
269*22ce4affSfengbojiang break;
270*22ce4affSfengbojiang case LINUX_ARCH_GET_FS:
271*22ce4affSfengbojiang error = copyout(&pcb->pcb_fsbase, PTRIN(args->addr),
272*22ce4affSfengbojiang sizeof(args->addr));
273*22ce4affSfengbojiang break;
274*22ce4affSfengbojiang case LINUX_ARCH_GET_GS:
275*22ce4affSfengbojiang error = copyout(&pcb->pcb_gsbase, PTRIN(args->addr),
276*22ce4affSfengbojiang sizeof(args->addr));
277a9643ea8Slogwang break;
278a9643ea8Slogwang default:
279a9643ea8Slogwang error = EINVAL;
280a9643ea8Slogwang }
281a9643ea8Slogwang return (error);
282a9643ea8Slogwang }
283a9643ea8Slogwang
284a9643ea8Slogwang int
linux_set_cloned_tls(struct thread * td,void * desc)285a9643ea8Slogwang linux_set_cloned_tls(struct thread *td, void *desc)
286a9643ea8Slogwang {
287a9643ea8Slogwang struct pcb *pcb;
288a9643ea8Slogwang
289a9643ea8Slogwang if ((uint64_t)desc >= VM_MAXUSER_ADDRESS)
290a9643ea8Slogwang return (EPERM);
291a9643ea8Slogwang
292a9643ea8Slogwang pcb = td->td_pcb;
293a9643ea8Slogwang pcb->pcb_fsbase = (register_t)desc;
294a9643ea8Slogwang td->td_frame->tf_fs = _ufssel;
295a9643ea8Slogwang
296a9643ea8Slogwang return (0);
297a9643ea8Slogwang }
298*22ce4affSfengbojiang
299*22ce4affSfengbojiang int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
300*22ce4affSfengbojiang int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval);
301*22ce4affSfengbojiang DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *))
302*22ce4affSfengbojiang {
303*22ce4affSfengbojiang
304*22ce4affSfengbojiang return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
305*22ce4affSfengbojiang futex_xchgl_smap : futex_xchgl_nosmap);
306*22ce4affSfengbojiang }
307*22ce4affSfengbojiang
308*22ce4affSfengbojiang int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
309*22ce4affSfengbojiang int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval);
310*22ce4affSfengbojiang DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *))
311*22ce4affSfengbojiang {
312*22ce4affSfengbojiang
313*22ce4affSfengbojiang return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
314*22ce4affSfengbojiang futex_addl_smap : futex_addl_nosmap);
315*22ce4affSfengbojiang }
316*22ce4affSfengbojiang
317*22ce4affSfengbojiang int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
318*22ce4affSfengbojiang int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval);
319*22ce4affSfengbojiang DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *))
320*22ce4affSfengbojiang {
321*22ce4affSfengbojiang
322*22ce4affSfengbojiang return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
323*22ce4affSfengbojiang futex_orl_smap : futex_orl_nosmap);
324*22ce4affSfengbojiang }
325*22ce4affSfengbojiang
326*22ce4affSfengbojiang int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
327*22ce4affSfengbojiang int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval);
328*22ce4affSfengbojiang DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *))
329*22ce4affSfengbojiang {
330*22ce4affSfengbojiang
331*22ce4affSfengbojiang return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
332*22ce4affSfengbojiang futex_andl_smap : futex_andl_nosmap);
333*22ce4affSfengbojiang }
334*22ce4affSfengbojiang
335*22ce4affSfengbojiang int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
336*22ce4affSfengbojiang int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval);
337*22ce4affSfengbojiang DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *))
338*22ce4affSfengbojiang {
339*22ce4affSfengbojiang
340*22ce4affSfengbojiang return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
341*22ce4affSfengbojiang futex_xorl_smap : futex_xorl_nosmap);
342*22ce4affSfengbojiang }
343