2 * Copyright (c) 2004 Tim J. Robbins
3 * Copyright (c) 2002 Doug Rabson
4 * Copyright (c) 2000 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_compat.h"
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/capsicum.h>
41 #include <sys/fcntl.h>
42 #include <sys/clock.h>
43 #include <sys/imgact.h>
44 #include <sys/limits.h>
46 #include <sys/malloc.h>
48 #include <sys/mutex.h>
51 #include <sys/resource.h>
52 #include <sys/resourcevar.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/sysproto.h>
55 #include <sys/unistd.h>
58 #include <machine/frame.h>
59 #include <machine/pcb.h>
60 #include <machine/psl.h>
61 #include <machine/segments.h>
62 #include <machine/specialreg.h>
66 #include <vm/vm_map.h>
68 #include <compat/freebsd32/freebsd32_util.h>
69 #include <amd64/linux32/linux.h>
70 #include <amd64/linux32/linux32_proto.h>
71 #include <compat/linux/linux_ipc.h>
72 #include <compat/linux/linux_misc.h>
73 #include <compat/linux/linux_mmap.h>
74 #include <compat/linux/linux_signal.h>
75 #include <compat/linux/linux_util.h>
76 #include <compat/linux/linux_emul.h>
78 static void bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru);
80 struct l_old_select_argv {
84 l_uintptr_t exceptfds;
90 bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru)
93 lru->ru_utime.tv_sec = ru->ru_utime.tv_sec;
94 lru->ru_utime.tv_usec = ru->ru_utime.tv_usec;
95 lru->ru_stime.tv_sec = ru->ru_stime.tv_sec;
96 lru->ru_stime.tv_usec = ru->ru_stime.tv_usec;
97 lru->ru_maxrss = ru->ru_maxrss;
98 lru->ru_ixrss = ru->ru_ixrss;
99 lru->ru_idrss = ru->ru_idrss;
100 lru->ru_isrss = ru->ru_isrss;
101 lru->ru_minflt = ru->ru_minflt;
102 lru->ru_majflt = ru->ru_majflt;
103 lru->ru_nswap = ru->ru_nswap;
104 lru->ru_inblock = ru->ru_inblock;
105 lru->ru_oublock = ru->ru_oublock;
106 lru->ru_msgsnd = ru->ru_msgsnd;
107 lru->ru_msgrcv = ru->ru_msgrcv;
108 lru->ru_nsignals = ru->ru_nsignals;
109 lru->ru_nvcsw = ru->ru_nvcsw;
110 lru->ru_nivcsw = ru->ru_nivcsw;
114 linux_copyout_rusage(struct rusage *ru, void *uaddr)
118 bsd_to_linux_rusage(ru, &lru);
120 return (copyout(&lru, uaddr, sizeof(struct l_rusage)));
124 linux_execve(struct thread *td, struct linux_execve_args *args)
126 struct image_args eargs;
130 LCONVPATHEXIST(td, args->path, &path);
134 printf(ARGS(execve, "%s"), path);
137 error = freebsd32_exec_copyin_args(&eargs, path, UIO_SYSSPACE,
138 args->argp, args->envp);
141 error = linux_common_execve(td, &eargs);
145 CTASSERT(sizeof(struct l_iovec32) == 8);
148 linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt, struct uio **uiop)
150 struct l_iovec32 iov32;
157 if (iovcnt > UIO_MAXIOV)
159 iovlen = iovcnt * sizeof(struct iovec);
160 uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK);
161 iov = (struct iovec *)(uio + 1);
162 for (i = 0; i < iovcnt; i++) {
163 error = copyin(&iovp[i], &iov32, sizeof(struct l_iovec32));
168 iov[i].iov_base = PTRIN(iov32.iov_base);
169 iov[i].iov_len = iov32.iov_len;
172 uio->uio_iovcnt = iovcnt;
173 uio->uio_segflg = UIO_USERSPACE;
174 uio->uio_offset = -1;
176 for (i = 0; i < iovcnt; i++) {
177 if (iov->iov_len > INT_MAX - uio->uio_resid) {
181 uio->uio_resid += iov->iov_len;
189 linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt, struct iovec **iovp,
192 struct l_iovec32 iov32;
198 if (iovcnt > UIO_MAXIOV)
200 iovlen = iovcnt * sizeof(struct iovec);
201 iov = malloc(iovlen, M_IOV, M_WAITOK);
202 for (i = 0; i < iovcnt; i++) {
203 error = copyin(&iovp32[i], &iov32, sizeof(struct l_iovec32));
208 iov[i].iov_base = PTRIN(iov32.iov_base);
209 iov[i].iov_len = iov32.iov_len;
217 linux_readv(struct thread *td, struct linux_readv_args *uap)
222 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
225 error = kern_readv(td, uap->fd, auio);
231 linux_writev(struct thread *td, struct linux_writev_args *uap)
236 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
239 error = kern_writev(td, uap->fd, auio);
244 struct l_ipc_kludge {
250 linux_ipc(struct thread *td, struct linux_ipc_args *args)
253 switch (args->what & 0xFFFF) {
255 struct linux_semop_args a;
257 a.semid = args->arg1;
259 a.nsops = args->arg2;
260 return (linux_semop(td, &a));
263 struct linux_semget_args a;
266 a.nsems = args->arg2;
267 a.semflg = args->arg3;
268 return (linux_semget(td, &a));
271 struct linux_semctl_args a;
274 a.semid = args->arg1;
275 a.semnum = args->arg2;
277 error = copyin(args->ptr, &a.arg, sizeof(a.arg));
280 return (linux_semctl(td, &a));
283 struct linux_msgsnd_args a;
285 a.msqid = args->arg1;
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(args->ptr, &tmp, sizeof(tmp));
306 a.msgp = PTRIN(tmp.msgp);
307 a.msgtyp = tmp.msgtyp;
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;
327 return (linux_msgctl(td, &a));
330 struct linux_shmat_args a;
332 a.shmid = args->arg1;
333 a.shmaddr = args->ptr;
334 a.shmflg = args->arg2;
335 a.raddr = PTRIN((l_uint)args->arg3);
336 return (linux_shmat(td, &a));
339 struct linux_shmdt_args a;
341 a.shmaddr = args->ptr;
342 return (linux_shmdt(td, &a));
345 struct linux_shmget_args a;
349 a.shmflg = args->arg3;
350 return (linux_shmget(td, &a));
353 struct linux_shmctl_args a;
355 a.shmid = args->arg1;
358 return (linux_shmctl(td, &a));
368 linux_old_select(struct thread *td, struct linux_old_select_args *args)
370 struct l_old_select_argv linux_args;
371 struct linux_select_args newsel;
375 if (ldebug(old_select))
376 printf(ARGS(old_select, "%p"), args->ptr);
379 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
383 newsel.nfds = linux_args.nfds;
384 newsel.readfds = PTRIN(linux_args.readfds);
385 newsel.writefds = PTRIN(linux_args.writefds);
386 newsel.exceptfds = PTRIN(linux_args.exceptfds);
387 newsel.timeout = PTRIN(linux_args.timeout);
388 return (linux_select(td, &newsel));
392 linux_set_cloned_tls(struct thread *td, void *desc)
394 struct user_segment_descriptor sd;
395 struct l_user_desc info;
400 error = copyin(desc, &info, sizeof(struct l_user_desc));
402 printf(LMSG("copyin 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 printf(LMSG("copyout failed!"));
410 a[0] = LINUX_LDT_entry_a(&info);
411 a[1] = LINUX_LDT_entry_b(&info);
413 memcpy(&sd, &a, sizeof(a));
416 printf("Segment created in clone with "
417 "CLONE_SETTLS: lobase: %x, hibase: %x, "
418 "lolimit: %x, hilimit: %x, type: %i, "
419 "dpl: %i, p: %i, xx: %i, long: %i, "
420 "def32: %i, gran: %i\n", sd.sd_lobase,
421 sd.sd_hibase, sd.sd_lolimit, sd.sd_hilimit,
422 sd.sd_type, sd.sd_dpl, sd.sd_p, sd.sd_xx,
423 sd.sd_long, sd.sd_def32, sd.sd_gran);
426 pcb->pcb_gsbase = (register_t)info.base_addr;
427 td->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL);
428 set_pcb_flags(pcb, PCB_32BIT);
435 linux_set_upcall_kse(struct thread *td, register_t stack)
439 td->td_frame->tf_rsp = stack;
442 * The newly created Linux thread returns
443 * to the user space by the same path that a parent do.
445 td->td_frame->tf_rax = 0;
450 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
455 printf(ARGS(mmap2, "0x%08x, %d, %d, 0x%08x, %d, %d"),
456 args->addr, args->len, args->prot,
457 args->flags, args->fd, args->pgoff);
460 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
461 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
466 linux_mmap(struct thread *td, struct linux_mmap_args *args)
469 struct l_mmap_argv linux_args;
471 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
477 printf(ARGS(mmap, "0x%08x, %d, %d, 0x%08x, %d, %d"),
478 linux_args.addr, linux_args.len, linux_args.prot,
479 linux_args.flags, linux_args.fd, linux_args.pgoff);
482 return (linux_mmap_common(td, linux_args.addr, linux_args.len,
483 linux_args.prot, linux_args.flags, linux_args.fd,
484 (uint32_t)linux_args.pgoff));
488 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
491 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot));
495 linux_iopl(struct thread *td, struct linux_iopl_args *args)
499 if (args->level < 0 || args->level > 3)
501 if ((error = priv_check(td, PRIV_IO)) != 0)
503 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
505 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
506 (args->level * (PSL_IOPL / 3));
512 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
515 l_sigaction_t act, oact;
519 if (ldebug(sigaction))
520 printf(ARGS(sigaction, "%d, %p, %p"),
521 args->sig, (void *)args->nsa, (void *)args->osa);
524 if (args->nsa != NULL) {
525 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
528 act.lsa_handler = osa.lsa_handler;
529 act.lsa_flags = osa.lsa_flags;
530 act.lsa_restorer = osa.lsa_restorer;
531 LINUX_SIGEMPTYSET(act.lsa_mask);
532 act.lsa_mask.__mask = osa.lsa_mask;
535 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
536 args->osa ? &oact : NULL);
538 if (args->osa != NULL && !error) {
539 osa.lsa_handler = oact.lsa_handler;
540 osa.lsa_flags = oact.lsa_flags;
541 osa.lsa_restorer = oact.lsa_restorer;
542 osa.lsa_mask = oact.lsa_mask.__mask;
543 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
550 * Linux has two extra args, restart and oldmask. We don't use these,
551 * but it seems that "restart" is actually a context pointer that
552 * enables the signal to happen with a different register set.
555 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
561 if (ldebug(sigsuspend))
562 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
565 LINUX_SIGEMPTYSET(mask);
566 mask.__mask = args->mask;
567 linux_to_bsd_sigset(&mask, &sigmask);
568 return (kern_sigsuspend(td, sigmask));
572 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
579 if (ldebug(rt_sigsuspend))
580 printf(ARGS(rt_sigsuspend, "%p, %d"),
581 (void *)uap->newset, uap->sigsetsize);
584 if (uap->sigsetsize != sizeof(l_sigset_t))
587 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
591 linux_to_bsd_sigset(&lmask, &sigmask);
592 return (kern_sigsuspend(td, sigmask));
596 linux_pause(struct thread *td, struct linux_pause_args *args)
598 struct proc *p = td->td_proc;
603 printf(ARGS(pause, ""));
607 sigmask = td->td_sigmask;
609 return (kern_sigsuspend(td, sigmask));
613 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
620 if (ldebug(sigaltstack))
621 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
624 if (uap->uss != NULL) {
625 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
629 ss.ss_sp = PTRIN(lss.ss_sp);
630 ss.ss_size = lss.ss_size;
631 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
633 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
634 (uap->uoss != NULL) ? &oss : NULL);
635 if (!error && uap->uoss != NULL) {
636 lss.ss_sp = PTROUT(oss.ss_sp);
637 lss.ss_size = oss.ss_size;
638 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
639 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
646 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
650 if (ldebug(ftruncate64))
651 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
652 (intmax_t)args->length);
655 return (kern_ftruncate(td, args->fd, args->length));
659 linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
668 atv32.tv_sec = atv.tv_sec;
669 atv32.tv_usec = atv.tv_usec;
670 error = copyout(&atv32, uap->tp, sizeof(atv32));
672 if (error == 0 && uap->tzp != NULL) {
673 rtz.tz_minuteswest = tz_minuteswest;
674 rtz.tz_dsttime = tz_dsttime;
675 error = copyout(&rtz, uap->tzp, sizeof(rtz));
681 linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap)
684 struct timeval atv, *tvp;
685 struct timezone atz, *tzp;
689 error = copyin(uap->tp, &atv32, sizeof(atv32));
692 atv.tv_sec = atv32.tv_sec;
693 atv.tv_usec = atv32.tv_usec;
698 error = copyin(uap->tzp, &atz, sizeof(atz));
704 return (kern_settimeofday(td, tvp, tzp));
708 linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
713 error = kern_getrusage(td, uap->who, &s);
716 if (uap->rusage != NULL)
717 error = linux_copyout_rusage(&s, uap->rusage);
722 linux_set_thread_area(struct thread *td,
723 struct linux_set_thread_area_args *args)
725 struct l_user_desc info;
726 struct user_segment_descriptor sd;
731 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
736 if (ldebug(set_thread_area))
737 printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, "
738 "%i, %i, %i"), info.entry_number, info.base_addr,
739 info.limit, info.seg_32bit, info.contents,
740 info.read_exec_only, info.limit_in_pages,
741 info.seg_not_present, info.useable);
745 * Semantics of Linux version: every thread in the system has array
746 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown.
747 * This syscall loads one of the selected TLS decriptors with a value
748 * and also loads GDT descriptors 6, 7 and 8 with the content of
749 * the per-thread descriptors.
751 * Semantics of FreeBSD version: I think we can ignore that Linux has
752 * three per-thread descriptors and use just the first one.
753 * The tls_array[] is used only in [gs]et_thread_area() syscalls and
754 * for loading the GDT descriptors. We use just one GDT descriptor
755 * for TLS, so we will load just one.
757 * XXX: This doesn't work when a user space process tries to use more
758 * than one TLS segment. Comment in the Linux source says wine might
763 * GLIBC reads current %gs and call set_thread_area() with it.
764 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because
765 * we use these segments.
767 switch (info.entry_number) {
772 info.entry_number = GUGS32_SEL;
779 * We have to copy out the GDT entry we use.
781 * XXX: What if a user space program does not check the return value
782 * and tries to use 6, 7 or 8?
784 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
788 if (LINUX_LDT_empty(&info)) {
792 a[0] = LINUX_LDT_entry_a(&info);
793 a[1] = LINUX_LDT_entry_b(&info);
796 memcpy(&sd, &a, sizeof(a));
798 if (ldebug(set_thread_area))
799 printf("Segment created in set_thread_area: "
800 "lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, "
801 "type: %i, dpl: %i, p: %i, xx: %i, long: %i, "
802 "def32: %i, gran: %i\n",
817 pcb->pcb_gsbase = (register_t)info.base_addr;
818 set_pcb_flags(pcb, PCB_32BIT);
819 update_gdt_gsbase(td, info.base_addr);