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 <compat/freebsd32/freebsd32_util.h>
73 #include <amd64/linux32/linux.h>
74 #include <amd64/linux32/linux32_proto.h>
75 #include <compat/linux/linux_emul.h>
76 #include <compat/linux/linux_ipc.h>
77 #include <compat/linux/linux_misc.h>
78 #include <compat/linux/linux_mmap.h>
79 #include <compat/linux/linux_signal.h>
80 #include <compat/linux/linux_util.h>
82 static void bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru);
84 struct l_old_select_argv {
88 l_uintptr_t exceptfds;
94 bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru)
97 lru->ru_utime.tv_sec = ru->ru_utime.tv_sec;
98 lru->ru_utime.tv_usec = ru->ru_utime.tv_usec;
99 lru->ru_stime.tv_sec = ru->ru_stime.tv_sec;
100 lru->ru_stime.tv_usec = ru->ru_stime.tv_usec;
101 lru->ru_maxrss = ru->ru_maxrss;
102 lru->ru_ixrss = ru->ru_ixrss;
103 lru->ru_idrss = ru->ru_idrss;
104 lru->ru_isrss = ru->ru_isrss;
105 lru->ru_minflt = ru->ru_minflt;
106 lru->ru_majflt = ru->ru_majflt;
107 lru->ru_nswap = ru->ru_nswap;
108 lru->ru_inblock = ru->ru_inblock;
109 lru->ru_oublock = ru->ru_oublock;
110 lru->ru_msgsnd = ru->ru_msgsnd;
111 lru->ru_msgrcv = ru->ru_msgrcv;
112 lru->ru_nsignals = ru->ru_nsignals;
113 lru->ru_nvcsw = ru->ru_nvcsw;
114 lru->ru_nivcsw = ru->ru_nivcsw;
118 linux_copyout_rusage(struct rusage *ru, void *uaddr)
122 bsd_to_linux_rusage(ru, &lru);
124 return (copyout(&lru, uaddr, sizeof(struct l_rusage)));
128 linux_execve(struct thread *td, struct linux_execve_args *args)
130 struct image_args eargs;
134 LCONVPATHEXIST(td, args->path, &path);
138 printf(ARGS(execve, "%s"), path);
141 error = freebsd32_exec_copyin_args(&eargs, path, UIO_SYSSPACE,
142 args->argp, args->envp);
145 error = linux_common_execve(td, &eargs);
149 CTASSERT(sizeof(struct l_iovec32) == 8);
152 linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt, struct uio **uiop)
154 struct l_iovec32 iov32;
161 if (iovcnt > UIO_MAXIOV)
163 iovlen = iovcnt * sizeof(struct iovec);
164 uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK);
165 iov = (struct iovec *)(uio + 1);
166 for (i = 0; i < iovcnt; i++) {
167 error = copyin(&iovp[i], &iov32, sizeof(struct l_iovec32));
172 iov[i].iov_base = PTRIN(iov32.iov_base);
173 iov[i].iov_len = iov32.iov_len;
176 uio->uio_iovcnt = iovcnt;
177 uio->uio_segflg = UIO_USERSPACE;
178 uio->uio_offset = -1;
180 for (i = 0; i < iovcnt; i++) {
181 if (iov->iov_len > INT_MAX - uio->uio_resid) {
185 uio->uio_resid += iov->iov_len;
193 linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt, struct iovec **iovp,
196 struct l_iovec32 iov32;
202 if (iovcnt > UIO_MAXIOV)
204 iovlen = iovcnt * sizeof(struct iovec);
205 iov = malloc(iovlen, M_IOV, M_WAITOK);
206 for (i = 0; i < iovcnt; i++) {
207 error = copyin(&iovp32[i], &iov32, sizeof(struct l_iovec32));
212 iov[i].iov_base = PTRIN(iov32.iov_base);
213 iov[i].iov_len = iov32.iov_len;
221 linux_readv(struct thread *td, struct linux_readv_args *uap)
226 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
229 error = kern_readv(td, uap->fd, auio);
235 linux_writev(struct thread *td, struct linux_writev_args *uap)
240 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
243 error = kern_writev(td, uap->fd, auio);
248 struct l_ipc_kludge {
254 linux_ipc(struct thread *td, struct linux_ipc_args *args)
257 switch (args->what & 0xFFFF) {
259 struct linux_semop_args a;
261 a.semid = args->arg1;
262 a.tsops = PTRIN(args->ptr);
263 a.nsops = args->arg2;
264 return (linux_semop(td, &a));
267 struct linux_semget_args a;
270 a.nsems = args->arg2;
271 a.semflg = args->arg3;
272 return (linux_semget(td, &a));
275 struct linux_semctl_args a;
278 a.semid = args->arg1;
279 a.semnum = args->arg2;
281 error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg));
284 return (linux_semctl(td, &a));
287 struct linux_msgsnd_args a;
289 a.msqid = args->arg1;
290 a.msgp = PTRIN(args->ptr);
291 a.msgsz = args->arg2;
292 a.msgflg = args->arg3;
293 return (linux_msgsnd(td, &a));
296 struct linux_msgrcv_args a;
298 a.msqid = args->arg1;
299 a.msgsz = args->arg2;
300 a.msgflg = args->arg3;
301 if ((args->what >> 16) == 0) {
302 struct l_ipc_kludge tmp;
307 error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp));
310 a.msgp = PTRIN(tmp.msgp);
311 a.msgtyp = tmp.msgtyp;
313 a.msgp = PTRIN(args->ptr);
314 a.msgtyp = args->arg5;
316 return (linux_msgrcv(td, &a));
319 struct linux_msgget_args a;
322 a.msgflg = args->arg2;
323 return (linux_msgget(td, &a));
326 struct linux_msgctl_args a;
328 a.msqid = args->arg1;
330 a.buf = PTRIN(args->ptr);
331 return (linux_msgctl(td, &a));
334 struct linux_shmat_args a;
338 a.shmid = args->arg1;
339 a.shmaddr = PTRIN(args->ptr);
340 a.shmflg = args->arg2;
341 error = linux_shmat(td, &a);
344 addr = td->td_retval[0];
345 error = copyout(&addr, PTRIN(args->arg3), sizeof(addr));
346 td->td_retval[0] = 0;
350 struct linux_shmdt_args a;
352 a.shmaddr = PTRIN(args->ptr);
353 return (linux_shmdt(td, &a));
356 struct linux_shmget_args a;
360 a.shmflg = args->arg3;
361 return (linux_shmget(td, &a));
364 struct linux_shmctl_args a;
366 a.shmid = args->arg1;
368 a.buf = PTRIN(args->ptr);
369 return (linux_shmctl(td, &a));
379 linux_old_select(struct thread *td, struct linux_old_select_args *args)
381 struct l_old_select_argv linux_args;
382 struct linux_select_args newsel;
386 if (ldebug(old_select))
387 printf(ARGS(old_select, "%p"), args->ptr);
390 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
394 newsel.nfds = linux_args.nfds;
395 newsel.readfds = PTRIN(linux_args.readfds);
396 newsel.writefds = PTRIN(linux_args.writefds);
397 newsel.exceptfds = PTRIN(linux_args.exceptfds);
398 newsel.timeout = PTRIN(linux_args.timeout);
399 return (linux_select(td, &newsel));
403 linux_set_cloned_tls(struct thread *td, void *desc)
405 struct user_segment_descriptor sd;
406 struct l_user_desc info;
411 error = copyin(desc, &info, sizeof(struct l_user_desc));
413 printf(LMSG("copyin failed!"));
415 /* We might copy out the entry_number as GUGS32_SEL. */
416 info.entry_number = GUGS32_SEL;
417 error = copyout(&info, desc, sizeof(struct l_user_desc));
419 printf(LMSG("copyout failed!"));
421 a[0] = LINUX_LDT_entry_a(&info);
422 a[1] = LINUX_LDT_entry_b(&info);
424 memcpy(&sd, &a, sizeof(a));
427 printf("Segment created in clone with "
428 "CLONE_SETTLS: lobase: %x, hibase: %x, "
429 "lolimit: %x, hilimit: %x, type: %i, "
430 "dpl: %i, p: %i, xx: %i, long: %i, "
431 "def32: %i, gran: %i\n", sd.sd_lobase,
432 sd.sd_hibase, sd.sd_lolimit, sd.sd_hilimit,
433 sd.sd_type, sd.sd_dpl, sd.sd_p, sd.sd_xx,
434 sd.sd_long, sd.sd_def32, sd.sd_gran);
437 pcb->pcb_gsbase = (register_t)info.base_addr;
438 td->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL);
439 set_pcb_flags(pcb, PCB_32BIT);
446 linux_set_upcall_kse(struct thread *td, register_t stack)
450 td->td_frame->tf_rsp = stack;
453 * The newly created Linux thread returns
454 * to the user space by the same path that a parent do.
456 td->td_frame->tf_rax = 0;
461 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
466 printf(ARGS(mmap2, "0x%08x, %d, %d, 0x%08x, %d, %d"),
467 args->addr, args->len, args->prot,
468 args->flags, args->fd, args->pgoff);
471 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
472 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
477 linux_mmap(struct thread *td, struct linux_mmap_args *args)
480 struct l_mmap_argv linux_args;
482 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
488 printf(ARGS(mmap, "0x%08x, %d, %d, 0x%08x, %d, %d"),
489 linux_args.addr, linux_args.len, linux_args.prot,
490 linux_args.flags, linux_args.fd, linux_args.pgoff);
493 return (linux_mmap_common(td, linux_args.addr, linux_args.len,
494 linux_args.prot, linux_args.flags, linux_args.fd,
495 (uint32_t)linux_args.pgoff));
499 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
502 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
506 linux_iopl(struct thread *td, struct linux_iopl_args *args)
510 if (args->level < 0 || args->level > 3)
512 if ((error = priv_check(td, PRIV_IO)) != 0)
514 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
516 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
517 (args->level * (PSL_IOPL / 3));
523 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
526 l_sigaction_t act, oact;
530 if (ldebug(sigaction))
531 printf(ARGS(sigaction, "%d, %p, %p"),
532 args->sig, (void *)args->nsa, (void *)args->osa);
535 if (args->nsa != NULL) {
536 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
539 act.lsa_handler = osa.lsa_handler;
540 act.lsa_flags = osa.lsa_flags;
541 act.lsa_restorer = osa.lsa_restorer;
542 LINUX_SIGEMPTYSET(act.lsa_mask);
543 act.lsa_mask.__mask = osa.lsa_mask;
546 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
547 args->osa ? &oact : NULL);
549 if (args->osa != NULL && !error) {
550 osa.lsa_handler = oact.lsa_handler;
551 osa.lsa_flags = oact.lsa_flags;
552 osa.lsa_restorer = oact.lsa_restorer;
553 osa.lsa_mask = oact.lsa_mask.__mask;
554 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
561 * Linux has two extra args, restart and oldmask. We don't use these,
562 * but it seems that "restart" is actually a context pointer that
563 * enables the signal to happen with a different register set.
566 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
572 if (ldebug(sigsuspend))
573 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
576 LINUX_SIGEMPTYSET(mask);
577 mask.__mask = args->mask;
578 linux_to_bsd_sigset(&mask, &sigmask);
579 return (kern_sigsuspend(td, sigmask));
583 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
590 if (ldebug(rt_sigsuspend))
591 printf(ARGS(rt_sigsuspend, "%p, %d"),
592 (void *)uap->newset, uap->sigsetsize);
595 if (uap->sigsetsize != sizeof(l_sigset_t))
598 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
602 linux_to_bsd_sigset(&lmask, &sigmask);
603 return (kern_sigsuspend(td, sigmask));
607 linux_pause(struct thread *td, struct linux_pause_args *args)
609 struct proc *p = td->td_proc;
614 printf(ARGS(pause, ""));
618 sigmask = td->td_sigmask;
620 return (kern_sigsuspend(td, sigmask));
624 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
631 if (ldebug(sigaltstack))
632 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
635 if (uap->uss != NULL) {
636 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
640 ss.ss_sp = PTRIN(lss.ss_sp);
641 ss.ss_size = lss.ss_size;
642 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
644 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
645 (uap->uoss != NULL) ? &oss : NULL);
646 if (!error && uap->uoss != NULL) {
647 lss.ss_sp = PTROUT(oss.ss_sp);
648 lss.ss_size = oss.ss_size;
649 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
650 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
657 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
661 if (ldebug(ftruncate64))
662 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
663 (intmax_t)args->length);
666 return (kern_ftruncate(td, args->fd, args->length));
670 linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
679 atv32.tv_sec = atv.tv_sec;
680 atv32.tv_usec = atv.tv_usec;
681 error = copyout(&atv32, uap->tp, sizeof(atv32));
683 if (error == 0 && uap->tzp != NULL) {
684 rtz.tz_minuteswest = tz_minuteswest;
685 rtz.tz_dsttime = tz_dsttime;
686 error = copyout(&rtz, uap->tzp, sizeof(rtz));
692 linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap)
695 struct timeval atv, *tvp;
696 struct timezone atz, *tzp;
700 error = copyin(uap->tp, &atv32, sizeof(atv32));
703 atv.tv_sec = atv32.tv_sec;
704 atv.tv_usec = atv32.tv_usec;
709 error = copyin(uap->tzp, &atz, sizeof(atz));
715 return (kern_settimeofday(td, tvp, tzp));
719 linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
724 error = kern_getrusage(td, uap->who, &s);
727 if (uap->rusage != NULL)
728 error = linux_copyout_rusage(&s, uap->rusage);
733 linux_set_thread_area(struct thread *td,
734 struct linux_set_thread_area_args *args)
736 struct l_user_desc info;
737 struct user_segment_descriptor sd;
742 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
747 if (ldebug(set_thread_area))
748 printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, "
749 "%i, %i, %i"), info.entry_number, info.base_addr,
750 info.limit, info.seg_32bit, info.contents,
751 info.read_exec_only, info.limit_in_pages,
752 info.seg_not_present, info.useable);
756 * Semantics of Linux version: every thread in the system has array
757 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown.
758 * This syscall loads one of the selected TLS decriptors with a value
759 * and also loads GDT descriptors 6, 7 and 8 with the content of
760 * the per-thread descriptors.
762 * Semantics of FreeBSD version: I think we can ignore that Linux has
763 * three per-thread descriptors and use just the first one.
764 * The tls_array[] is used only in [gs]et_thread_area() syscalls and
765 * for loading the GDT descriptors. We use just one GDT descriptor
766 * for TLS, so we will load just one.
768 * XXX: This doesn't work when a user space process tries to use more
769 * than one TLS segment. Comment in the Linux source says wine might
774 * GLIBC reads current %gs and call set_thread_area() with it.
775 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because
776 * we use these segments.
778 switch (info.entry_number) {
783 info.entry_number = GUGS32_SEL;
790 * We have to copy out the GDT entry we use.
792 * XXX: What if a user space program does not check the return value
793 * and tries to use 6, 7 or 8?
795 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
799 if (LINUX_LDT_empty(&info)) {
803 a[0] = LINUX_LDT_entry_a(&info);
804 a[1] = LINUX_LDT_entry_b(&info);
807 memcpy(&sd, &a, sizeof(a));
809 if (ldebug(set_thread_area))
810 printf("Segment created in set_thread_area: "
811 "lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, "
812 "type: %i, dpl: %i, p: %i, xx: %i, long: %i, "
813 "def32: %i, gran: %i\n",
828 pcb->pcb_gsbase = (register_t)info.base_addr;
829 set_pcb_flags(pcb, PCB_32BIT);
830 update_gdt_gsbase(td, info.base_addr);
835 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
836 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval);
837 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *), static)
840 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
841 futex_xchgl_smap : futex_xchgl_nosmap);
844 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
845 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval);
846 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *), static)
849 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
850 futex_addl_smap : futex_addl_nosmap);
853 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
854 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval);
855 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *), static)
858 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
859 futex_orl_smap : futex_orl_nosmap);
862 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
863 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval);
864 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *), static)
867 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
868 futex_andl_smap : futex_andl_nosmap);
871 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
872 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval);
873 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *), static)
876 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
877 futex_xorl_smap : futex_xorl_nosmap);