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 if (!LUSECONVPATH(td)) {
138 error = freebsd32_exec_copyin_args(&eargs, args->path, UIO_USERSPACE,
139 args->argp, args->envp);
141 LCONVPATHEXIST(args->path, &path);
142 error = freebsd32_exec_copyin_args(&eargs, path, UIO_SYSSPACE,
143 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) {
263 return (kern_semop(td, args->arg1, PTRIN(args->ptr),
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));
286 case LINUX_SEMTIMEDOP: {
287 struct linux_semtimedop_args a;
289 a.semid = args->arg1;
290 a.tsops = PTRIN(args->ptr);
291 a.nsops = args->arg2;
292 a.timeout = PTRIN(args->arg5);
293 return (linux_semtimedop(td, &a));
296 struct linux_msgsnd_args a;
298 a.msqid = args->arg1;
299 a.msgp = PTRIN(args->ptr);
300 a.msgsz = args->arg2;
301 a.msgflg = args->arg3;
302 return (linux_msgsnd(td, &a));
305 struct linux_msgrcv_args a;
307 a.msqid = args->arg1;
308 a.msgsz = args->arg2;
309 a.msgflg = args->arg3;
310 if ((args->what >> 16) == 0) {
311 struct l_ipc_kludge tmp;
316 error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp));
319 a.msgp = PTRIN(tmp.msgp);
320 a.msgtyp = tmp.msgtyp;
322 a.msgp = PTRIN(args->ptr);
323 a.msgtyp = args->arg5;
325 return (linux_msgrcv(td, &a));
328 struct linux_msgget_args a;
331 a.msgflg = args->arg2;
332 return (linux_msgget(td, &a));
335 struct linux_msgctl_args a;
337 a.msqid = args->arg1;
339 a.buf = PTRIN(args->ptr);
340 return (linux_msgctl(td, &a));
343 struct linux_shmat_args a;
347 a.shmid = args->arg1;
348 a.shmaddr = PTRIN(args->ptr);
349 a.shmflg = args->arg2;
350 error = linux_shmat(td, &a);
353 addr = td->td_retval[0];
354 error = copyout(&addr, PTRIN(args->arg3), sizeof(addr));
355 td->td_retval[0] = 0;
359 struct linux_shmdt_args a;
361 a.shmaddr = PTRIN(args->ptr);
362 return (linux_shmdt(td, &a));
365 struct linux_shmget_args a;
369 a.shmflg = args->arg3;
370 return (linux_shmget(td, &a));
373 struct linux_shmctl_args a;
375 a.shmid = args->arg1;
377 a.buf = PTRIN(args->ptr);
378 return (linux_shmctl(td, &a));
388 linux_old_select(struct thread *td, struct linux_old_select_args *args)
390 struct l_old_select_argv linux_args;
391 struct linux_select_args newsel;
394 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
398 newsel.nfds = linux_args.nfds;
399 newsel.readfds = PTRIN(linux_args.readfds);
400 newsel.writefds = PTRIN(linux_args.writefds);
401 newsel.exceptfds = PTRIN(linux_args.exceptfds);
402 newsel.timeout = PTRIN(linux_args.timeout);
403 return (linux_select(td, &newsel));
407 linux_set_cloned_tls(struct thread *td, void *desc)
409 struct l_user_desc info;
413 error = copyin(desc, &info, sizeof(struct l_user_desc));
415 linux_msg(td, "set_cloned_tls copyin info failed!");
417 /* We might copy out the entry_number as GUGS32_SEL. */
418 info.entry_number = GUGS32_SEL;
419 error = copyout(&info, desc, sizeof(struct l_user_desc));
421 linux_msg(td, "set_cloned_tls copyout info failed!");
424 update_pcb_bases(pcb);
425 pcb->pcb_gsbase = (register_t)info.base_addr;
426 td->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL);
433 linux_set_upcall(struct thread *td, register_t stack)
437 td->td_frame->tf_rsp = stack;
440 * The newly created Linux thread returns
441 * to the user space by the same path that a parent do.
443 td->td_frame->tf_rax = 0;
448 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
451 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
452 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
457 linux_mmap(struct thread *td, struct linux_mmap_args *args)
460 struct l_mmap_argv linux_args;
462 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
466 return (linux_mmap_common(td, linux_args.addr, linux_args.len,
467 linux_args.prot, linux_args.flags, linux_args.fd,
468 (uint32_t)linux_args.pgoff));
472 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
475 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
479 linux_madvise(struct thread *td, struct linux_madvise_args *uap)
482 return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav));
486 linux_iopl(struct thread *td, struct linux_iopl_args *args)
490 if (args->level < 0 || args->level > 3)
492 if ((error = priv_check(td, PRIV_IO)) != 0)
494 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
496 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
497 (args->level * (PSL_IOPL / 3));
503 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
506 l_sigaction_t act, oact;
509 if (args->nsa != NULL) {
510 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
513 act.lsa_handler = osa.lsa_handler;
514 act.lsa_flags = osa.lsa_flags;
515 act.lsa_restorer = osa.lsa_restorer;
516 LINUX_SIGEMPTYSET(act.lsa_mask);
517 act.lsa_mask.__mask = osa.lsa_mask;
520 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
521 args->osa ? &oact : NULL);
523 if (args->osa != NULL && !error) {
524 osa.lsa_handler = oact.lsa_handler;
525 osa.lsa_flags = oact.lsa_flags;
526 osa.lsa_restorer = oact.lsa_restorer;
527 osa.lsa_mask = oact.lsa_mask.__mask;
528 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
535 * Linux has two extra args, restart and oldmask. We don't use these,
536 * but it seems that "restart" is actually a context pointer that
537 * enables the signal to happen with a different register set.
540 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
545 LINUX_SIGEMPTYSET(mask);
546 mask.__mask = args->mask;
547 linux_to_bsd_sigset(&mask, &sigmask);
548 return (kern_sigsuspend(td, sigmask));
552 linux_pause(struct thread *td, struct linux_pause_args *args)
554 struct proc *p = td->td_proc;
558 sigmask = td->td_sigmask;
560 return (kern_sigsuspend(td, sigmask));
564 linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
573 atv32.tv_sec = atv.tv_sec;
574 atv32.tv_usec = atv.tv_usec;
575 error = copyout(&atv32, uap->tp, sizeof(atv32));
577 if (error == 0 && uap->tzp != NULL) {
578 rtz.tz_minuteswest = 0;
580 error = copyout(&rtz, uap->tzp, sizeof(rtz));
586 linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap)
589 struct timeval atv, *tvp;
590 struct timezone atz, *tzp;
594 error = copyin(uap->tp, &atv32, sizeof(atv32));
597 atv.tv_sec = atv32.tv_sec;
598 atv.tv_usec = atv32.tv_usec;
603 error = copyin(uap->tzp, &atz, sizeof(atz));
609 return (kern_settimeofday(td, tvp, tzp));
613 linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
618 error = kern_getrusage(td, uap->who, &s);
621 if (uap->rusage != NULL)
622 error = linux_copyout_rusage(&s, uap->rusage);
627 linux_set_thread_area(struct thread *td,
628 struct linux_set_thread_area_args *args)
630 struct l_user_desc info;
634 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
639 * Semantics of Linux version: every thread in the system has array
640 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown.
641 * This syscall loads one of the selected TLS decriptors with a value
642 * and also loads GDT descriptors 6, 7 and 8 with the content of
643 * the per-thread descriptors.
645 * Semantics of FreeBSD version: I think we can ignore that Linux has
646 * three per-thread descriptors and use just the first one.
647 * The tls_array[] is used only in [gs]et_thread_area() syscalls and
648 * for loading the GDT descriptors. We use just one GDT descriptor
649 * for TLS, so we will load just one.
651 * XXX: This doesn't work when a user space process tries to use more
652 * than one TLS segment. Comment in the Linux source says wine might
657 * GLIBC reads current %gs and call set_thread_area() with it.
658 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because
659 * we use these segments.
661 switch (info.entry_number) {
666 info.entry_number = GUGS32_SEL;
673 * We have to copy out the GDT entry we use.
675 * XXX: What if a user space program does not check the return value
676 * and tries to use 6, 7 or 8?
678 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
683 update_pcb_bases(pcb);
684 pcb->pcb_gsbase = (register_t)info.base_addr;
685 update_gdt_gsbase(td, info.base_addr);
691 bsd_to_linux_regset32(const struct reg32 *b_reg,
692 struct linux_pt_regset32 *l_regset)
695 l_regset->ebx = b_reg->r_ebx;
696 l_regset->ecx = b_reg->r_ecx;
697 l_regset->edx = b_reg->r_edx;
698 l_regset->esi = b_reg->r_esi;
699 l_regset->edi = b_reg->r_edi;
700 l_regset->ebp = b_reg->r_ebp;
701 l_regset->eax = b_reg->r_eax;
702 l_regset->ds = b_reg->r_ds;
703 l_regset->es = b_reg->r_es;
704 l_regset->fs = b_reg->r_fs;
705 l_regset->gs = b_reg->r_gs;
706 l_regset->orig_eax = b_reg->r_eax;
707 l_regset->eip = b_reg->r_eip;
708 l_regset->cs = b_reg->r_cs;
709 l_regset->eflags = b_reg->r_eflags;
710 l_regset->esp = b_reg->r_esp;
711 l_regset->ss = b_reg->r_ss;
714 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
715 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval);
716 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *))
719 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
720 futex_xchgl_smap : futex_xchgl_nosmap);
723 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
724 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval);
725 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *))
728 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
729 futex_addl_smap : futex_addl_nosmap);
732 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
733 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval);
734 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *))
737 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
738 futex_orl_smap : futex_orl_nosmap);
741 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
742 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval);
743 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *))
746 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
747 futex_andl_smap : futex_andl_nosmap);
750 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
751 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval);
752 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *))
755 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
756 futex_xorl_smap : futex_xorl_nosmap);