2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2004 Tim J. Robbins
5 * Copyright (c) 2002 Doug Rabson
6 * Copyright (c) 2000 Marcel Moolenaar
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer
14 * in this position and unchanged.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_compat.h"
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
40 #include <sys/clock.h>
41 #include <sys/fcntl.h>
43 #include <sys/imgact.h>
44 #include <sys/kernel.h>
45 #include <sys/limits.h>
47 #include <sys/malloc.h>
49 #include <sys/mutex.h>
53 #include <sys/resource.h>
54 #include <sys/resourcevar.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysproto.h>
57 #include <sys/systm.h>
58 #include <sys/unistd.h>
61 #include <machine/frame.h>
62 #include <machine/md_var.h>
63 #include <machine/pcb.h>
64 #include <machine/psl.h>
65 #include <machine/segments.h>
66 #include <machine/specialreg.h>
67 #include <x86/ifunc.h>
71 #include <vm/vm_map.h>
73 #include <security/audit/audit.h>
75 #include <compat/freebsd32/freebsd32_util.h>
76 #include <amd64/linux32/linux.h>
77 #include <amd64/linux32/linux32_proto.h>
78 #include <compat/linux/linux_emul.h>
79 #include <compat/linux/linux_fork.h>
80 #include <compat/linux/linux_ipc.h>
81 #include <compat/linux/linux_misc.h>
82 #include <compat/linux/linux_mmap.h>
83 #include <compat/linux/linux_signal.h>
84 #include <compat/linux/linux_util.h>
86 static void bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru);
88 struct l_old_select_argv {
92 l_uintptr_t exceptfds;
97 bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru)
100 lru->ru_utime.tv_sec = ru->ru_utime.tv_sec;
101 lru->ru_utime.tv_usec = ru->ru_utime.tv_usec;
102 lru->ru_stime.tv_sec = ru->ru_stime.tv_sec;
103 lru->ru_stime.tv_usec = ru->ru_stime.tv_usec;
104 lru->ru_maxrss = ru->ru_maxrss;
105 lru->ru_ixrss = ru->ru_ixrss;
106 lru->ru_idrss = ru->ru_idrss;
107 lru->ru_isrss = ru->ru_isrss;
108 lru->ru_minflt = ru->ru_minflt;
109 lru->ru_majflt = ru->ru_majflt;
110 lru->ru_nswap = ru->ru_nswap;
111 lru->ru_inblock = ru->ru_inblock;
112 lru->ru_oublock = ru->ru_oublock;
113 lru->ru_msgsnd = ru->ru_msgsnd;
114 lru->ru_msgrcv = ru->ru_msgrcv;
115 lru->ru_nsignals = ru->ru_nsignals;
116 lru->ru_nvcsw = ru->ru_nvcsw;
117 lru->ru_nivcsw = ru->ru_nivcsw;
121 linux_copyout_rusage(struct rusage *ru, void *uaddr)
125 bsd_to_linux_rusage(ru, &lru);
127 return (copyout(&lru, uaddr, sizeof(struct l_rusage)));
131 linux_execve(struct thread *td, struct linux_execve_args *args)
133 struct image_args eargs;
137 LCONVPATHEXIST(args->path, &path);
139 error = freebsd32_exec_copyin_args(&eargs, path, UIO_SYSSPACE,
140 args->argp, args->envp);
143 error = linux_common_execve(td, &eargs);
144 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
148 CTASSERT(sizeof(struct l_iovec32) == 8);
151 linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt, struct uio **uiop)
153 struct l_iovec32 iov32;
160 if (iovcnt > UIO_MAXIOV)
162 iovlen = iovcnt * sizeof(struct iovec);
163 uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK);
164 iov = (struct iovec *)(uio + 1);
165 for (i = 0; i < iovcnt; i++) {
166 error = copyin(&iovp[i], &iov32, sizeof(struct l_iovec32));
171 iov[i].iov_base = PTRIN(iov32.iov_base);
172 iov[i].iov_len = iov32.iov_len;
175 uio->uio_iovcnt = iovcnt;
176 uio->uio_segflg = UIO_USERSPACE;
177 uio->uio_offset = -1;
179 for (i = 0; i < iovcnt; i++) {
180 if (iov->iov_len > INT_MAX - uio->uio_resid) {
184 uio->uio_resid += iov->iov_len;
192 linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt, struct iovec **iovp,
195 struct l_iovec32 iov32;
201 if (iovcnt > UIO_MAXIOV)
203 iovlen = iovcnt * sizeof(struct iovec);
204 iov = malloc(iovlen, M_IOV, M_WAITOK);
205 for (i = 0; i < iovcnt; i++) {
206 error = copyin(&iovp32[i], &iov32, sizeof(struct l_iovec32));
211 iov[i].iov_base = PTRIN(iov32.iov_base);
212 iov[i].iov_len = iov32.iov_len;
220 linux_readv(struct thread *td, struct linux_readv_args *uap)
225 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
228 error = kern_readv(td, uap->fd, auio);
234 linux_writev(struct thread *td, struct linux_writev_args *uap)
239 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
242 error = kern_writev(td, uap->fd, auio);
247 struct l_ipc_kludge {
253 linux_ipc(struct thread *td, struct linux_ipc_args *args)
256 switch (args->what & 0xFFFF) {
258 struct linux_semop_args a;
260 a.semid = args->arg1;
261 a.tsops = PTRIN(args->ptr);
262 a.nsops = args->arg2;
263 return (linux_semop(td, &a));
266 struct linux_semget_args a;
269 a.nsems = args->arg2;
270 a.semflg = args->arg3;
271 return (linux_semget(td, &a));
274 struct linux_semctl_args a;
277 a.semid = args->arg1;
278 a.semnum = args->arg2;
280 error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg));
283 return (linux_semctl(td, &a));
286 struct linux_msgsnd_args a;
288 a.msqid = args->arg1;
289 a.msgp = PTRIN(args->ptr);
290 a.msgsz = args->arg2;
291 a.msgflg = args->arg3;
292 return (linux_msgsnd(td, &a));
295 struct linux_msgrcv_args a;
297 a.msqid = args->arg1;
298 a.msgsz = args->arg2;
299 a.msgflg = args->arg3;
300 if ((args->what >> 16) == 0) {
301 struct l_ipc_kludge tmp;
306 error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp));
309 a.msgp = PTRIN(tmp.msgp);
310 a.msgtyp = tmp.msgtyp;
312 a.msgp = PTRIN(args->ptr);
313 a.msgtyp = args->arg5;
315 return (linux_msgrcv(td, &a));
318 struct linux_msgget_args a;
321 a.msgflg = args->arg2;
322 return (linux_msgget(td, &a));
325 struct linux_msgctl_args a;
327 a.msqid = args->arg1;
329 a.buf = PTRIN(args->ptr);
330 return (linux_msgctl(td, &a));
333 struct linux_shmat_args a;
337 a.shmid = args->arg1;
338 a.shmaddr = PTRIN(args->ptr);
339 a.shmflg = args->arg2;
340 error = linux_shmat(td, &a);
343 addr = td->td_retval[0];
344 error = copyout(&addr, PTRIN(args->arg3), sizeof(addr));
345 td->td_retval[0] = 0;
349 struct linux_shmdt_args a;
351 a.shmaddr = PTRIN(args->ptr);
352 return (linux_shmdt(td, &a));
355 struct linux_shmget_args a;
359 a.shmflg = args->arg3;
360 return (linux_shmget(td, &a));
363 struct linux_shmctl_args a;
365 a.shmid = args->arg1;
367 a.buf = PTRIN(args->ptr);
368 return (linux_shmctl(td, &a));
378 linux_old_select(struct thread *td, struct linux_old_select_args *args)
380 struct l_old_select_argv linux_args;
381 struct linux_select_args newsel;
384 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
388 newsel.nfds = linux_args.nfds;
389 newsel.readfds = PTRIN(linux_args.readfds);
390 newsel.writefds = PTRIN(linux_args.writefds);
391 newsel.exceptfds = PTRIN(linux_args.exceptfds);
392 newsel.timeout = PTRIN(linux_args.timeout);
393 return (linux_select(td, &newsel));
397 linux_set_cloned_tls(struct thread *td, void *desc)
399 struct l_user_desc info;
403 error = copyin(desc, &info, sizeof(struct l_user_desc));
405 linux_msg(td, "set_cloned_tls copyin info failed!");
407 /* We might copy out the entry_number as GUGS32_SEL. */
408 info.entry_number = GUGS32_SEL;
409 error = copyout(&info, desc, sizeof(struct l_user_desc));
411 linux_msg(td, "set_cloned_tls copyout info failed!");
414 update_pcb_bases(pcb);
415 pcb->pcb_gsbase = (register_t)info.base_addr;
416 td->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL);
423 linux_set_upcall(struct thread *td, register_t stack)
427 td->td_frame->tf_rsp = stack;
430 * The newly created Linux thread returns
431 * to the user space by the same path that a parent do.
433 td->td_frame->tf_rax = 0;
438 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
441 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
442 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
447 linux_mmap(struct thread *td, struct linux_mmap_args *args)
450 struct l_mmap_argv linux_args;
452 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
456 return (linux_mmap_common(td, linux_args.addr, linux_args.len,
457 linux_args.prot, linux_args.flags, linux_args.fd,
458 (uint32_t)linux_args.pgoff));
462 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
465 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
469 linux_madvise(struct thread *td, struct linux_madvise_args *uap)
472 return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav));
476 linux_iopl(struct thread *td, struct linux_iopl_args *args)
480 if (args->level < 0 || args->level > 3)
482 if ((error = priv_check(td, PRIV_IO)) != 0)
484 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
486 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
487 (args->level * (PSL_IOPL / 3));
493 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
496 l_sigaction_t act, oact;
499 if (args->nsa != NULL) {
500 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
503 act.lsa_handler = osa.lsa_handler;
504 act.lsa_flags = osa.lsa_flags;
505 act.lsa_restorer = osa.lsa_restorer;
506 LINUX_SIGEMPTYSET(act.lsa_mask);
507 act.lsa_mask.__mask = osa.lsa_mask;
510 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
511 args->osa ? &oact : NULL);
513 if (args->osa != NULL && !error) {
514 osa.lsa_handler = oact.lsa_handler;
515 osa.lsa_flags = oact.lsa_flags;
516 osa.lsa_restorer = oact.lsa_restorer;
517 osa.lsa_mask = oact.lsa_mask.__mask;
518 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
525 * Linux has two extra args, restart and oldmask. We don't use these,
526 * but it seems that "restart" is actually a context pointer that
527 * enables the signal to happen with a different register set.
530 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
535 LINUX_SIGEMPTYSET(mask);
536 mask.__mask = args->mask;
537 linux_to_bsd_sigset(&mask, &sigmask);
538 return (kern_sigsuspend(td, sigmask));
542 linux_pause(struct thread *td, struct linux_pause_args *args)
544 struct proc *p = td->td_proc;
548 sigmask = td->td_sigmask;
550 return (kern_sigsuspend(td, sigmask));
554 linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
563 atv32.tv_sec = atv.tv_sec;
564 atv32.tv_usec = atv.tv_usec;
565 error = copyout(&atv32, uap->tp, sizeof(atv32));
567 if (error == 0 && uap->tzp != NULL) {
568 rtz.tz_minuteswest = 0;
570 error = copyout(&rtz, uap->tzp, sizeof(rtz));
576 linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap)
579 struct timeval atv, *tvp;
580 struct timezone atz, *tzp;
584 error = copyin(uap->tp, &atv32, sizeof(atv32));
587 atv.tv_sec = atv32.tv_sec;
588 atv.tv_usec = atv32.tv_usec;
593 error = copyin(uap->tzp, &atz, sizeof(atz));
599 return (kern_settimeofday(td, tvp, tzp));
603 linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
608 error = kern_getrusage(td, uap->who, &s);
611 if (uap->rusage != NULL)
612 error = linux_copyout_rusage(&s, uap->rusage);
617 linux_set_thread_area(struct thread *td,
618 struct linux_set_thread_area_args *args)
620 struct l_user_desc info;
624 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
629 * Semantics of Linux version: every thread in the system has array
630 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown.
631 * This syscall loads one of the selected TLS decriptors with a value
632 * and also loads GDT descriptors 6, 7 and 8 with the content of
633 * the per-thread descriptors.
635 * Semantics of FreeBSD version: I think we can ignore that Linux has
636 * three per-thread descriptors and use just the first one.
637 * The tls_array[] is used only in [gs]et_thread_area() syscalls and
638 * for loading the GDT descriptors. We use just one GDT descriptor
639 * for TLS, so we will load just one.
641 * XXX: This doesn't work when a user space process tries to use more
642 * than one TLS segment. Comment in the Linux source says wine might
647 * GLIBC reads current %gs and call set_thread_area() with it.
648 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because
649 * we use these segments.
651 switch (info.entry_number) {
656 info.entry_number = GUGS32_SEL;
663 * We have to copy out the GDT entry we use.
665 * XXX: What if a user space program does not check the return value
666 * and tries to use 6, 7 or 8?
668 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
673 update_pcb_bases(pcb);
674 pcb->pcb_gsbase = (register_t)info.base_addr;
675 update_gdt_gsbase(td, info.base_addr);
681 bsd_to_linux_regset32(const struct reg32 *b_reg,
682 struct linux_pt_regset32 *l_regset)
685 l_regset->ebx = b_reg->r_ebx;
686 l_regset->ecx = b_reg->r_ecx;
687 l_regset->edx = b_reg->r_edx;
688 l_regset->esi = b_reg->r_esi;
689 l_regset->edi = b_reg->r_edi;
690 l_regset->ebp = b_reg->r_ebp;
691 l_regset->eax = b_reg->r_eax;
692 l_regset->ds = b_reg->r_ds;
693 l_regset->es = b_reg->r_es;
694 l_regset->fs = b_reg->r_fs;
695 l_regset->gs = b_reg->r_gs;
696 l_regset->orig_eax = b_reg->r_eax;
697 l_regset->eip = b_reg->r_eip;
698 l_regset->cs = b_reg->r_cs;
699 l_regset->eflags = b_reg->r_eflags;
700 l_regset->esp = b_reg->r_esp;
701 l_regset->ss = b_reg->r_ss;
704 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
705 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval);
706 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *))
709 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
710 futex_xchgl_smap : futex_xchgl_nosmap);
713 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
714 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval);
715 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *))
718 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
719 futex_addl_smap : futex_addl_nosmap);
722 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
723 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval);
724 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *))
727 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
728 futex_orl_smap : futex_orl_nosmap);
731 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
732 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval);
733 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *))
736 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
737 futex_andl_smap : futex_andl_nosmap);
740 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
741 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval);
742 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *))
745 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
746 futex_xorl_smap : futex_xorl_nosmap);