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>
52 #include <sys/resource.h>
53 #include <sys/resourcevar.h>
54 #include <sys/syscallsubr.h>
55 #include <sys/sysproto.h>
56 #include <sys/systm.h>
57 #include <sys/unistd.h>
60 #include <machine/frame.h>
61 #include <machine/md_var.h>
62 #include <machine/pcb.h>
63 #include <machine/psl.h>
64 #include <machine/segments.h>
65 #include <machine/specialreg.h>
66 #include <x86/ifunc.h>
70 #include <vm/vm_map.h>
72 #include <security/audit/audit.h>
74 #include <compat/freebsd32/freebsd32_util.h>
75 #include <amd64/linux32/linux.h>
76 #include <amd64/linux32/linux32_proto.h>
77 #include <compat/linux/linux_emul.h>
78 #include <compat/linux/linux_ipc.h>
79 #include <compat/linux/linux_misc.h>
80 #include <compat/linux/linux_mmap.h>
81 #include <compat/linux/linux_signal.h>
82 #include <compat/linux/linux_util.h>
84 static void bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru);
86 struct l_old_select_argv {
90 l_uintptr_t exceptfds;
96 bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru)
99 lru->ru_utime.tv_sec = ru->ru_utime.tv_sec;
100 lru->ru_utime.tv_usec = ru->ru_utime.tv_usec;
101 lru->ru_stime.tv_sec = ru->ru_stime.tv_sec;
102 lru->ru_stime.tv_usec = ru->ru_stime.tv_usec;
103 lru->ru_maxrss = ru->ru_maxrss;
104 lru->ru_ixrss = ru->ru_ixrss;
105 lru->ru_idrss = ru->ru_idrss;
106 lru->ru_isrss = ru->ru_isrss;
107 lru->ru_minflt = ru->ru_minflt;
108 lru->ru_majflt = ru->ru_majflt;
109 lru->ru_nswap = ru->ru_nswap;
110 lru->ru_inblock = ru->ru_inblock;
111 lru->ru_oublock = ru->ru_oublock;
112 lru->ru_msgsnd = ru->ru_msgsnd;
113 lru->ru_msgrcv = ru->ru_msgrcv;
114 lru->ru_nsignals = ru->ru_nsignals;
115 lru->ru_nvcsw = ru->ru_nvcsw;
116 lru->ru_nivcsw = ru->ru_nivcsw;
120 linux_copyout_rusage(struct rusage *ru, void *uaddr)
124 bsd_to_linux_rusage(ru, &lru);
126 return (copyout(&lru, uaddr, sizeof(struct l_rusage)));
130 linux_execve(struct thread *td, struct linux_execve_args *args)
132 struct image_args eargs;
136 LCONVPATHEXIST(td, args->path, &path);
140 printf(ARGS(execve, "%s"), path);
143 error = freebsd32_exec_copyin_args(&eargs, path, UIO_SYSSPACE,
144 args->argp, args->envp);
147 error = linux_common_execve(td, &eargs);
148 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
152 CTASSERT(sizeof(struct l_iovec32) == 8);
155 linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt, struct uio **uiop)
157 struct l_iovec32 iov32;
164 if (iovcnt > UIO_MAXIOV)
166 iovlen = iovcnt * sizeof(struct iovec);
167 uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK);
168 iov = (struct iovec *)(uio + 1);
169 for (i = 0; i < iovcnt; i++) {
170 error = copyin(&iovp[i], &iov32, sizeof(struct l_iovec32));
175 iov[i].iov_base = PTRIN(iov32.iov_base);
176 iov[i].iov_len = iov32.iov_len;
179 uio->uio_iovcnt = iovcnt;
180 uio->uio_segflg = UIO_USERSPACE;
181 uio->uio_offset = -1;
183 for (i = 0; i < iovcnt; i++) {
184 if (iov->iov_len > INT_MAX - uio->uio_resid) {
188 uio->uio_resid += iov->iov_len;
196 linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt, struct iovec **iovp,
199 struct l_iovec32 iov32;
205 if (iovcnt > UIO_MAXIOV)
207 iovlen = iovcnt * sizeof(struct iovec);
208 iov = malloc(iovlen, M_IOV, M_WAITOK);
209 for (i = 0; i < iovcnt; i++) {
210 error = copyin(&iovp32[i], &iov32, sizeof(struct l_iovec32));
215 iov[i].iov_base = PTRIN(iov32.iov_base);
216 iov[i].iov_len = iov32.iov_len;
224 linux_readv(struct thread *td, struct linux_readv_args *uap)
229 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
232 error = kern_readv(td, uap->fd, auio);
238 linux_writev(struct thread *td, struct linux_writev_args *uap)
243 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
246 error = kern_writev(td, uap->fd, auio);
251 struct l_ipc_kludge {
257 linux_ipc(struct thread *td, struct linux_ipc_args *args)
260 switch (args->what & 0xFFFF) {
262 struct linux_semop_args a;
264 a.semid = args->arg1;
265 a.tsops = PTRIN(args->ptr);
266 a.nsops = args->arg2;
267 return (linux_semop(td, &a));
270 struct linux_semget_args a;
273 a.nsems = args->arg2;
274 a.semflg = args->arg3;
275 return (linux_semget(td, &a));
278 struct linux_semctl_args a;
281 a.semid = args->arg1;
282 a.semnum = args->arg2;
284 error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg));
287 return (linux_semctl(td, &a));
290 struct linux_msgsnd_args a;
292 a.msqid = args->arg1;
293 a.msgp = PTRIN(args->ptr);
294 a.msgsz = args->arg2;
295 a.msgflg = args->arg3;
296 return (linux_msgsnd(td, &a));
299 struct linux_msgrcv_args a;
301 a.msqid = args->arg1;
302 a.msgsz = args->arg2;
303 a.msgflg = args->arg3;
304 if ((args->what >> 16) == 0) {
305 struct l_ipc_kludge tmp;
310 error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp));
313 a.msgp = PTRIN(tmp.msgp);
314 a.msgtyp = tmp.msgtyp;
316 a.msgp = PTRIN(args->ptr);
317 a.msgtyp = args->arg5;
319 return (linux_msgrcv(td, &a));
322 struct linux_msgget_args a;
325 a.msgflg = args->arg2;
326 return (linux_msgget(td, &a));
329 struct linux_msgctl_args a;
331 a.msqid = args->arg1;
333 a.buf = PTRIN(args->ptr);
334 return (linux_msgctl(td, &a));
337 struct linux_shmat_args a;
341 a.shmid = args->arg1;
342 a.shmaddr = PTRIN(args->ptr);
343 a.shmflg = args->arg2;
344 error = linux_shmat(td, &a);
347 addr = td->td_retval[0];
348 error = copyout(&addr, PTRIN(args->arg3), sizeof(addr));
349 td->td_retval[0] = 0;
353 struct linux_shmdt_args a;
355 a.shmaddr = PTRIN(args->ptr);
356 return (linux_shmdt(td, &a));
359 struct linux_shmget_args a;
363 a.shmflg = args->arg3;
364 return (linux_shmget(td, &a));
367 struct linux_shmctl_args a;
369 a.shmid = args->arg1;
371 a.buf = PTRIN(args->ptr);
372 return (linux_shmctl(td, &a));
382 linux_old_select(struct thread *td, struct linux_old_select_args *args)
384 struct l_old_select_argv linux_args;
385 struct linux_select_args newsel;
389 if (ldebug(old_select))
390 printf(ARGS(old_select, "%p"), args->ptr);
393 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
397 newsel.nfds = linux_args.nfds;
398 newsel.readfds = PTRIN(linux_args.readfds);
399 newsel.writefds = PTRIN(linux_args.writefds);
400 newsel.exceptfds = PTRIN(linux_args.exceptfds);
401 newsel.timeout = PTRIN(linux_args.timeout);
402 return (linux_select(td, &newsel));
406 linux_set_cloned_tls(struct thread *td, void *desc)
408 struct user_segment_descriptor sd;
409 struct l_user_desc info;
414 error = copyin(desc, &info, sizeof(struct l_user_desc));
416 printf(LMSG("copyin failed!"));
418 /* We might copy out the entry_number as GUGS32_SEL. */
419 info.entry_number = GUGS32_SEL;
420 error = copyout(&info, desc, sizeof(struct l_user_desc));
422 printf(LMSG("copyout failed!"));
424 a[0] = LINUX_LDT_entry_a(&info);
425 a[1] = LINUX_LDT_entry_b(&info);
427 memcpy(&sd, &a, sizeof(a));
430 printf("Segment created in clone with "
431 "CLONE_SETTLS: lobase: %x, hibase: %x, "
432 "lolimit: %x, hilimit: %x, type: %i, "
433 "dpl: %i, p: %i, xx: %i, long: %i, "
434 "def32: %i, gran: %i\n", sd.sd_lobase,
435 sd.sd_hibase, sd.sd_lolimit, sd.sd_hilimit,
436 sd.sd_type, sd.sd_dpl, sd.sd_p, sd.sd_xx,
437 sd.sd_long, sd.sd_def32, sd.sd_gran);
440 pcb->pcb_gsbase = (register_t)info.base_addr;
441 td->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL);
442 set_pcb_flags(pcb, PCB_32BIT);
449 linux_set_upcall_kse(struct thread *td, register_t stack)
453 td->td_frame->tf_rsp = stack;
456 * The newly created Linux thread returns
457 * to the user space by the same path that a parent do.
459 td->td_frame->tf_rax = 0;
464 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
469 printf(ARGS(mmap2, "0x%08x, %d, %d, 0x%08x, %d, %d"),
470 args->addr, args->len, args->prot,
471 args->flags, args->fd, args->pgoff);
474 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
475 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
480 linux_mmap(struct thread *td, struct linux_mmap_args *args)
483 struct l_mmap_argv linux_args;
485 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
491 printf(ARGS(mmap, "0x%08x, %d, %d, 0x%08x, %d, %d"),
492 linux_args.addr, linux_args.len, linux_args.prot,
493 linux_args.flags, linux_args.fd, linux_args.pgoff);
496 return (linux_mmap_common(td, linux_args.addr, linux_args.len,
497 linux_args.prot, linux_args.flags, linux_args.fd,
498 (uint32_t)linux_args.pgoff));
502 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
505 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
509 linux_iopl(struct thread *td, struct linux_iopl_args *args)
513 if (args->level < 0 || args->level > 3)
515 if ((error = priv_check(td, PRIV_IO)) != 0)
517 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
519 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
520 (args->level * (PSL_IOPL / 3));
526 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
529 l_sigaction_t act, oact;
533 if (ldebug(sigaction))
534 printf(ARGS(sigaction, "%d, %p, %p"),
535 args->sig, (void *)args->nsa, (void *)args->osa);
538 if (args->nsa != NULL) {
539 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
542 act.lsa_handler = osa.lsa_handler;
543 act.lsa_flags = osa.lsa_flags;
544 act.lsa_restorer = osa.lsa_restorer;
545 LINUX_SIGEMPTYSET(act.lsa_mask);
546 act.lsa_mask.__mask = osa.lsa_mask;
549 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
550 args->osa ? &oact : NULL);
552 if (args->osa != NULL && !error) {
553 osa.lsa_handler = oact.lsa_handler;
554 osa.lsa_flags = oact.lsa_flags;
555 osa.lsa_restorer = oact.lsa_restorer;
556 osa.lsa_mask = oact.lsa_mask.__mask;
557 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
564 * Linux has two extra args, restart and oldmask. We don't use these,
565 * but it seems that "restart" is actually a context pointer that
566 * enables the signal to happen with a different register set.
569 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
575 if (ldebug(sigsuspend))
576 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
579 LINUX_SIGEMPTYSET(mask);
580 mask.__mask = args->mask;
581 linux_to_bsd_sigset(&mask, &sigmask);
582 return (kern_sigsuspend(td, sigmask));
586 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
593 if (ldebug(rt_sigsuspend))
594 printf(ARGS(rt_sigsuspend, "%p, %d"),
595 (void *)uap->newset, uap->sigsetsize);
598 if (uap->sigsetsize != sizeof(l_sigset_t))
601 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
605 linux_to_bsd_sigset(&lmask, &sigmask);
606 return (kern_sigsuspend(td, sigmask));
610 linux_pause(struct thread *td, struct linux_pause_args *args)
612 struct proc *p = td->td_proc;
617 printf(ARGS(pause, ""));
621 sigmask = td->td_sigmask;
623 return (kern_sigsuspend(td, sigmask));
627 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
634 if (ldebug(sigaltstack))
635 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
638 if (uap->uss != NULL) {
639 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
643 ss.ss_sp = PTRIN(lss.ss_sp);
644 ss.ss_size = lss.ss_size;
645 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
647 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
648 (uap->uoss != NULL) ? &oss : NULL);
649 if (!error && uap->uoss != NULL) {
650 lss.ss_sp = PTROUT(oss.ss_sp);
651 lss.ss_size = oss.ss_size;
652 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
653 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
660 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
664 if (ldebug(ftruncate64))
665 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
666 (intmax_t)args->length);
669 return (kern_ftruncate(td, args->fd, args->length));
673 linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
682 atv32.tv_sec = atv.tv_sec;
683 atv32.tv_usec = atv.tv_usec;
684 error = copyout(&atv32, uap->tp, sizeof(atv32));
686 if (error == 0 && uap->tzp != NULL) {
687 rtz.tz_minuteswest = tz_minuteswest;
688 rtz.tz_dsttime = tz_dsttime;
689 error = copyout(&rtz, uap->tzp, sizeof(rtz));
695 linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap)
698 struct timeval atv, *tvp;
699 struct timezone atz, *tzp;
703 error = copyin(uap->tp, &atv32, sizeof(atv32));
706 atv.tv_sec = atv32.tv_sec;
707 atv.tv_usec = atv32.tv_usec;
712 error = copyin(uap->tzp, &atz, sizeof(atz));
718 return (kern_settimeofday(td, tvp, tzp));
722 linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
727 error = kern_getrusage(td, uap->who, &s);
730 if (uap->rusage != NULL)
731 error = linux_copyout_rusage(&s, uap->rusage);
736 linux_set_thread_area(struct thread *td,
737 struct linux_set_thread_area_args *args)
739 struct l_user_desc info;
740 struct user_segment_descriptor sd;
745 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
750 if (ldebug(set_thread_area))
751 printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, "
752 "%i, %i, %i"), info.entry_number, info.base_addr,
753 info.limit, info.seg_32bit, info.contents,
754 info.read_exec_only, info.limit_in_pages,
755 info.seg_not_present, info.useable);
759 * Semantics of Linux version: every thread in the system has array
760 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown.
761 * This syscall loads one of the selected TLS decriptors with a value
762 * and also loads GDT descriptors 6, 7 and 8 with the content of
763 * the per-thread descriptors.
765 * Semantics of FreeBSD version: I think we can ignore that Linux has
766 * three per-thread descriptors and use just the first one.
767 * The tls_array[] is used only in [gs]et_thread_area() syscalls and
768 * for loading the GDT descriptors. We use just one GDT descriptor
769 * for TLS, so we will load just one.
771 * XXX: This doesn't work when a user space process tries to use more
772 * than one TLS segment. Comment in the Linux source says wine might
777 * GLIBC reads current %gs and call set_thread_area() with it.
778 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because
779 * we use these segments.
781 switch (info.entry_number) {
786 info.entry_number = GUGS32_SEL;
793 * We have to copy out the GDT entry we use.
795 * XXX: What if a user space program does not check the return value
796 * and tries to use 6, 7 or 8?
798 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
802 if (LINUX_LDT_empty(&info)) {
806 a[0] = LINUX_LDT_entry_a(&info);
807 a[1] = LINUX_LDT_entry_b(&info);
810 memcpy(&sd, &a, sizeof(a));
812 if (ldebug(set_thread_area))
813 printf("Segment created in set_thread_area: "
814 "lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, "
815 "type: %i, dpl: %i, p: %i, xx: %i, long: %i, "
816 "def32: %i, gran: %i\n",
831 pcb->pcb_gsbase = (register_t)info.base_addr;
832 set_pcb_flags(pcb, PCB_32BIT);
833 update_gdt_gsbase(td, info.base_addr);
838 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
839 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval);
840 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *), static)
843 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
844 futex_xchgl_smap : futex_xchgl_nosmap);
847 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
848 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval);
849 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *), static)
852 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
853 futex_addl_smap : futex_addl_nosmap);
856 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
857 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval);
858 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *), static)
861 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
862 futex_orl_smap : futex_orl_nosmap);
865 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
866 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval);
867 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *), static)
870 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
871 futex_andl_smap : futex_andl_nosmap);
874 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
875 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval);
876 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *), static)
879 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
880 futex_xorl_smap : futex_xorl_nosmap);