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 <sys/param.h>
37 #include <sys/fcntl.h>
38 #include <sys/imgact.h>
39 #include <sys/limits.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
46 #include <sys/syscallsubr.h>
48 #include <machine/frame.h>
49 #include <machine/md_var.h>
50 #include <machine/pcb.h>
51 #include <machine/psl.h>
52 #include <machine/segments.h>
53 #include <machine/specialreg.h>
54 #include <x86/ifunc.h>
58 #include <vm/vm_map.h>
60 #include <security/audit/audit.h>
62 #include <compat/freebsd32/freebsd32_util.h>
63 #include <amd64/linux32/linux.h>
64 #include <amd64/linux32/linux32_proto.h>
65 #include <compat/linux/linux_emul.h>
66 #include <compat/linux/linux_fork.h>
67 #include <compat/linux/linux_ipc.h>
68 #include <compat/linux/linux_misc.h>
69 #include <compat/linux/linux_mmap.h>
70 #include <compat/linux/linux_signal.h>
71 #include <compat/linux/linux_util.h>
73 static void bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru);
75 struct l_old_select_argv {
79 l_uintptr_t exceptfds;
84 bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru)
87 lru->ru_utime.tv_sec = ru->ru_utime.tv_sec;
88 lru->ru_utime.tv_usec = ru->ru_utime.tv_usec;
89 lru->ru_stime.tv_sec = ru->ru_stime.tv_sec;
90 lru->ru_stime.tv_usec = ru->ru_stime.tv_usec;
91 lru->ru_maxrss = ru->ru_maxrss;
92 lru->ru_ixrss = ru->ru_ixrss;
93 lru->ru_idrss = ru->ru_idrss;
94 lru->ru_isrss = ru->ru_isrss;
95 lru->ru_minflt = ru->ru_minflt;
96 lru->ru_majflt = ru->ru_majflt;
97 lru->ru_nswap = ru->ru_nswap;
98 lru->ru_inblock = ru->ru_inblock;
99 lru->ru_oublock = ru->ru_oublock;
100 lru->ru_msgsnd = ru->ru_msgsnd;
101 lru->ru_msgrcv = ru->ru_msgrcv;
102 lru->ru_nsignals = ru->ru_nsignals;
103 lru->ru_nvcsw = ru->ru_nvcsw;
104 lru->ru_nivcsw = ru->ru_nivcsw;
108 linux_copyout_rusage(struct rusage *ru, void *uaddr)
112 bsd_to_linux_rusage(ru, &lru);
114 return (copyout(&lru, uaddr, sizeof(struct l_rusage)));
118 linux_execve(struct thread *td, struct linux_execve_args *args)
120 struct image_args eargs;
124 if (!LUSECONVPATH(td)) {
125 error = freebsd32_exec_copyin_args(&eargs, args->path, UIO_USERSPACE,
126 args->argp, args->envp);
128 LCONVPATHEXIST(args->path, &path);
129 error = freebsd32_exec_copyin_args(&eargs, path, UIO_SYSSPACE,
130 args->argp, args->envp);
134 error = linux_common_execve(td, &eargs);
135 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td);
139 CTASSERT(sizeof(struct l_iovec32) == 8);
142 linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt, struct uio **uiop)
144 struct l_iovec32 iov32;
151 if (iovcnt > UIO_MAXIOV)
153 iovlen = iovcnt * sizeof(struct iovec);
154 uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK);
155 iov = (struct iovec *)(uio + 1);
156 for (i = 0; i < iovcnt; i++) {
157 error = copyin(&iovp[i], &iov32, sizeof(struct l_iovec32));
162 iov[i].iov_base = PTRIN(iov32.iov_base);
163 iov[i].iov_len = iov32.iov_len;
166 uio->uio_iovcnt = iovcnt;
167 uio->uio_segflg = UIO_USERSPACE;
168 uio->uio_offset = -1;
170 for (i = 0; i < iovcnt; i++) {
171 if (iov->iov_len > INT_MAX - uio->uio_resid) {
175 uio->uio_resid += iov->iov_len;
183 linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt, struct iovec **iovp,
186 struct l_iovec32 iov32;
192 if (iovcnt > UIO_MAXIOV)
194 iovlen = iovcnt * sizeof(struct iovec);
195 iov = malloc(iovlen, M_IOV, M_WAITOK);
196 for (i = 0; i < iovcnt; i++) {
197 error = copyin(&iovp32[i], &iov32, sizeof(struct l_iovec32));
202 iov[i].iov_base = PTRIN(iov32.iov_base);
203 iov[i].iov_len = iov32.iov_len;
211 linux_readv(struct thread *td, struct linux_readv_args *uap)
216 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
219 error = kern_readv(td, uap->fd, auio);
225 linux_writev(struct thread *td, struct linux_writev_args *uap)
230 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
233 error = kern_writev(td, uap->fd, auio);
238 struct l_ipc_kludge {
244 linux_ipc(struct thread *td, struct linux_ipc_args *args)
247 switch (args->what & 0xFFFF) {
250 return (kern_semop(td, args->arg1, PTRIN(args->ptr),
254 struct linux_semget_args a;
257 a.nsems = args->arg2;
258 a.semflg = args->arg3;
259 return (linux_semget(td, &a));
262 struct linux_semctl_args a;
265 a.semid = args->arg1;
266 a.semnum = args->arg2;
268 error = copyin(PTRIN(args->ptr), &a.arg, sizeof(a.arg));
271 return (linux_semctl(td, &a));
273 case LINUX_SEMTIMEDOP: {
274 struct linux_semtimedop_args a;
276 a.semid = args->arg1;
277 a.tsops = PTRIN(args->ptr);
278 a.nsops = args->arg2;
279 a.timeout = PTRIN(args->arg5);
280 return (linux_semtimedop(td, &a));
283 struct linux_msgsnd_args a;
285 a.msqid = args->arg1;
286 a.msgp = PTRIN(args->ptr);
287 a.msgsz = args->arg2;
288 a.msgflg = args->arg3;
289 return (linux_msgsnd(td, &a));
292 struct linux_msgrcv_args a;
294 a.msqid = args->arg1;
295 a.msgsz = args->arg2;
296 a.msgflg = args->arg3;
297 if ((args->what >> 16) == 0) {
298 struct l_ipc_kludge tmp;
303 error = copyin(PTRIN(args->ptr), &tmp, sizeof(tmp));
306 a.msgp = PTRIN(tmp.msgp);
307 a.msgtyp = tmp.msgtyp;
309 a.msgp = PTRIN(args->ptr);
310 a.msgtyp = args->arg5;
312 return (linux_msgrcv(td, &a));
315 struct linux_msgget_args a;
318 a.msgflg = args->arg2;
319 return (linux_msgget(td, &a));
322 struct linux_msgctl_args a;
324 a.msqid = args->arg1;
326 a.buf = PTRIN(args->ptr);
327 return (linux_msgctl(td, &a));
330 struct linux_shmat_args a;
334 a.shmid = args->arg1;
335 a.shmaddr = PTRIN(args->ptr);
336 a.shmflg = args->arg2;
337 error = linux_shmat(td, &a);
340 addr = td->td_retval[0];
341 error = copyout(&addr, PTRIN(args->arg3), sizeof(addr));
342 td->td_retval[0] = 0;
346 struct linux_shmdt_args a;
348 a.shmaddr = PTRIN(args->ptr);
349 return (linux_shmdt(td, &a));
352 struct linux_shmget_args a;
356 a.shmflg = args->arg3;
357 return (linux_shmget(td, &a));
360 struct linux_shmctl_args a;
362 a.shmid = args->arg1;
364 a.buf = PTRIN(args->ptr);
365 return (linux_shmctl(td, &a));
375 linux_old_select(struct thread *td, struct linux_old_select_args *args)
377 struct l_old_select_argv linux_args;
378 struct linux_select_args newsel;
381 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
385 newsel.nfds = linux_args.nfds;
386 newsel.readfds = PTRIN(linux_args.readfds);
387 newsel.writefds = PTRIN(linux_args.writefds);
388 newsel.exceptfds = PTRIN(linux_args.exceptfds);
389 newsel.timeout = PTRIN(linux_args.timeout);
390 return (linux_select(td, &newsel));
394 linux_set_cloned_tls(struct thread *td, void *desc)
396 struct l_user_desc info;
400 error = copyin(desc, &info, sizeof(struct l_user_desc));
402 linux_msg(td, "set_cloned_tls copyin info failed!");
404 /* We might copy out the entry_number as GUGS32_SEL. */
405 info.entry_number = GUGS32_SEL;
406 error = copyout(&info, desc, sizeof(struct l_user_desc));
408 linux_msg(td, "set_cloned_tls copyout info failed!");
411 update_pcb_bases(pcb);
412 pcb->pcb_gsbase = (register_t)info.base_addr;
413 td->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL);
420 linux_set_upcall(struct thread *td, register_t stack)
424 td->td_frame->tf_rsp = stack;
427 * The newly created Linux thread returns
428 * to the user space by the same path that a parent do.
430 td->td_frame->tf_rax = 0;
435 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
438 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
439 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
444 linux_mmap(struct thread *td, struct linux_mmap_args *args)
447 struct l_mmap_argv linux_args;
449 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
453 return (linux_mmap_common(td, linux_args.addr, linux_args.len,
454 linux_args.prot, linux_args.flags, linux_args.fd,
455 (uint32_t)linux_args.pgoff));
459 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
462 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
466 linux_madvise(struct thread *td, struct linux_madvise_args *uap)
469 return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav));
473 linux_iopl(struct thread *td, struct linux_iopl_args *args)
477 if (args->level < 0 || args->level > 3)
479 if ((error = priv_check(td, PRIV_IO)) != 0)
481 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
483 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
484 (args->level * (PSL_IOPL / 3));
490 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
493 l_sigaction_t act, oact;
496 if (args->nsa != NULL) {
497 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
500 act.lsa_handler = osa.lsa_handler;
501 act.lsa_flags = osa.lsa_flags;
502 act.lsa_restorer = osa.lsa_restorer;
503 LINUX_SIGEMPTYSET(act.lsa_mask);
504 act.lsa_mask.__mask = osa.lsa_mask;
507 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
508 args->osa ? &oact : NULL);
510 if (args->osa != NULL && !error) {
511 osa.lsa_handler = oact.lsa_handler;
512 osa.lsa_flags = oact.lsa_flags;
513 osa.lsa_restorer = oact.lsa_restorer;
514 osa.lsa_mask = oact.lsa_mask.__mask;
515 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
522 * Linux has two extra args, restart and oldmask. We don't use these,
523 * but it seems that "restart" is actually a context pointer that
524 * enables the signal to happen with a different register set.
527 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
532 LINUX_SIGEMPTYSET(mask);
533 mask.__mask = args->mask;
534 linux_to_bsd_sigset(&mask, &sigmask);
535 return (kern_sigsuspend(td, sigmask));
539 linux_pause(struct thread *td, struct linux_pause_args *args)
541 struct proc *p = td->td_proc;
545 sigmask = td->td_sigmask;
547 return (kern_sigsuspend(td, sigmask));
551 linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
560 atv32.tv_sec = atv.tv_sec;
561 atv32.tv_usec = atv.tv_usec;
562 error = copyout(&atv32, uap->tp, sizeof(atv32));
564 if (error == 0 && uap->tzp != NULL) {
565 rtz.tz_minuteswest = 0;
567 error = copyout(&rtz, uap->tzp, sizeof(rtz));
573 linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap)
576 struct timeval atv, *tvp;
577 struct timezone atz, *tzp;
581 error = copyin(uap->tp, &atv32, sizeof(atv32));
584 atv.tv_sec = atv32.tv_sec;
585 atv.tv_usec = atv32.tv_usec;
590 error = copyin(uap->tzp, &atz, sizeof(atz));
596 return (kern_settimeofday(td, tvp, tzp));
600 linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
605 error = kern_getrusage(td, uap->who, &s);
608 if (uap->rusage != NULL)
609 error = linux_copyout_rusage(&s, uap->rusage);
614 linux_set_thread_area(struct thread *td,
615 struct linux_set_thread_area_args *args)
617 struct l_user_desc info;
621 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
626 * Semantics of Linux version: every thread in the system has array
627 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown.
628 * This syscall loads one of the selected TLS decriptors with a value
629 * and also loads GDT descriptors 6, 7 and 8 with the content of
630 * the per-thread descriptors.
632 * Semantics of FreeBSD version: I think we can ignore that Linux has
633 * three per-thread descriptors and use just the first one.
634 * The tls_array[] is used only in [gs]et_thread_area() syscalls and
635 * for loading the GDT descriptors. We use just one GDT descriptor
636 * for TLS, so we will load just one.
638 * XXX: This doesn't work when a user space process tries to use more
639 * than one TLS segment. Comment in the Linux source says wine might
644 * GLIBC reads current %gs and call set_thread_area() with it.
645 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because
646 * we use these segments.
648 switch (info.entry_number) {
653 info.entry_number = GUGS32_SEL;
660 * We have to copy out the GDT entry we use.
662 * XXX: What if a user space program does not check the return value
663 * and tries to use 6, 7 or 8?
665 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
670 update_pcb_bases(pcb);
671 pcb->pcb_gsbase = (register_t)info.base_addr;
672 update_gdt_gsbase(td, info.base_addr);
678 bsd_to_linux_regset32(const struct reg32 *b_reg,
679 struct linux_pt_regset32 *l_regset)
682 l_regset->ebx = b_reg->r_ebx;
683 l_regset->ecx = b_reg->r_ecx;
684 l_regset->edx = b_reg->r_edx;
685 l_regset->esi = b_reg->r_esi;
686 l_regset->edi = b_reg->r_edi;
687 l_regset->ebp = b_reg->r_ebp;
688 l_regset->eax = b_reg->r_eax;
689 l_regset->ds = b_reg->r_ds;
690 l_regset->es = b_reg->r_es;
691 l_regset->fs = b_reg->r_fs;
692 l_regset->gs = b_reg->r_gs;
693 l_regset->orig_eax = b_reg->r_eax;
694 l_regset->eip = b_reg->r_eip;
695 l_regset->cs = b_reg->r_cs;
696 l_regset->eflags = b_reg->r_eflags;
697 l_regset->esp = b_reg->r_esp;
698 l_regset->ss = b_reg->r_ss;
701 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
702 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval);
703 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *))
706 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
707 futex_xchgl_smap : futex_xchgl_nosmap);
710 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
711 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval);
712 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *))
715 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
716 futex_addl_smap : futex_addl_nosmap);
719 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
720 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval);
721 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *))
724 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
725 futex_orl_smap : futex_orl_nosmap);
728 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
729 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval);
730 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *))
733 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
734 futex_andl_smap : futex_andl_nosmap);
737 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval);
738 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval);
739 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *))
742 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ?
743 futex_xorl_smap : futex_xorl_nosmap);