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 <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
38 #include <sys/fcntl.h>
39 #include <sys/clock.h>
40 #include <sys/imgact.h>
41 #include <sys/limits.h>
43 #include <sys/malloc.h>
45 #include <sys/mutex.h>
48 #include <sys/resource.h>
49 #include <sys/resourcevar.h>
50 #include <sys/sched.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/sysproto.h>
53 #include <sys/unistd.h>
55 #include <machine/frame.h>
56 #include <machine/pcb.h>
57 #include <machine/psl.h>
58 #include <machine/segments.h>
59 #include <machine/specialreg.h>
63 #include <vm/vm_extern.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_map.h>
67 #include <amd64/linux32/linux.h>
68 #include <amd64/linux32/linux32_proto.h>
69 #include <compat/linux/linux_ipc.h>
70 #include <compat/linux/linux_signal.h>
71 #include <compat/linux/linux_util.h>
72 #include <compat/linux/linux_emul.h>
74 struct l_old_select_argv {
78 l_uintptr_t exceptfds;
83 linux_to_bsd_sigaltstack(int lsa)
87 if (lsa & LINUX_SS_DISABLE)
89 if (lsa & LINUX_SS_ONSTACK)
95 bsd_to_linux_sigaltstack(int bsa)
100 lsa |= LINUX_SS_DISABLE;
101 if (bsa & SS_ONSTACK)
102 lsa |= LINUX_SS_ONSTACK;
107 * Custom version of exec_copyin_args() so that we can translate
111 linux_exec_copyin_args(struct image_args *args, char *fname,
112 enum uio_seg segflg, char **argv, char **envv)
119 bzero(args, sizeof(*args));
124 * Allocate temporary demand zeroed space for argument and
125 * environment strings
127 args->buf = (char *)kmem_alloc_wait(exec_map,
128 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
129 if (args->buf == NULL)
131 args->begin_argv = args->buf;
132 args->endp = args->begin_argv;
133 args->stringspace = ARG_MAX;
135 args->fname = args->buf + ARG_MAX;
138 * Copy the file name.
140 error = (segflg == UIO_SYSSPACE) ?
141 copystr(fname, args->fname, PATH_MAX, &length) :
142 copyinstr(fname, args->fname, PATH_MAX, &length);
147 * extract arguments first
149 p32 = (u_int32_t *)argv;
151 error = copyin(p32++, &arg, sizeof(arg));
157 error = copyinstr(argp, args->endp, args->stringspace, &length);
159 if (error == ENAMETOOLONG)
164 args->stringspace -= length;
165 args->endp += length;
169 args->begin_envv = args->endp;
172 * extract environment strings
175 p32 = (u_int32_t *)envv;
177 error = copyin(p32++, &arg, sizeof(arg));
183 error = copyinstr(envp, args->endp, args->stringspace,
186 if (error == ENAMETOOLONG)
190 args->stringspace -= length;
191 args->endp += length;
199 kmem_free_wakeup(exec_map, (vm_offset_t)args->buf,
200 PATH_MAX + ARG_MAX + MAXSHELLCMDLEN);
206 linux_execve(struct thread *td, struct linux_execve_args *args)
208 struct image_args eargs;
212 LCONVPATHEXIST(td, args->path, &path);
216 printf(ARGS(execve, "%s"), path);
219 error = linux_exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp,
223 error = kern_execve(td, &eargs, NULL);
225 /* Linux process can execute FreeBSD one, do not attempt
226 * to create emuldata for such process using
227 * linux_proc_init, this leads to a panic on KASSERT
228 * because such process has p->p_emuldata == NULL.
230 if (td->td_proc->p_sysent == &elf_linux_sysvec)
231 error = linux_proc_init(td, 0, 0);
235 CTASSERT(sizeof(struct l_iovec32) == 8);
238 linux32_copyinuio(struct l_iovec32 *iovp, l_ulong iovcnt, struct uio **uiop)
240 struct l_iovec32 iov32;
247 if (iovcnt > UIO_MAXIOV)
249 iovlen = iovcnt * sizeof(struct iovec);
250 uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK);
251 iov = (struct iovec *)(uio + 1);
252 for (i = 0; i < iovcnt; i++) {
253 error = copyin(&iovp[i], &iov32, sizeof(struct l_iovec32));
258 iov[i].iov_base = PTRIN(iov32.iov_base);
259 iov[i].iov_len = iov32.iov_len;
262 uio->uio_iovcnt = iovcnt;
263 uio->uio_segflg = UIO_USERSPACE;
264 uio->uio_offset = -1;
266 for (i = 0; i < iovcnt; i++) {
267 if (iov->iov_len > INT_MAX - uio->uio_resid) {
271 uio->uio_resid += iov->iov_len;
279 linux32_copyiniov(struct l_iovec32 *iovp32, l_ulong iovcnt, struct iovec **iovp,
282 struct l_iovec32 iov32;
288 if (iovcnt > UIO_MAXIOV)
290 iovlen = iovcnt * sizeof(struct iovec);
291 iov = malloc(iovlen, M_IOV, M_WAITOK);
292 for (i = 0; i < iovcnt; i++) {
293 error = copyin(&iovp32[i], &iov32, sizeof(struct l_iovec32));
298 iov[i].iov_base = PTRIN(iov32.iov_base);
299 iov[i].iov_len = iov32.iov_len;
307 linux_readv(struct thread *td, struct linux_readv_args *uap)
312 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
315 error = kern_readv(td, uap->fd, auio);
321 linux_writev(struct thread *td, struct linux_writev_args *uap)
326 error = linux32_copyinuio(uap->iovp, uap->iovcnt, &auio);
329 error = kern_writev(td, uap->fd, auio);
334 struct l_ipc_kludge {
340 linux_ipc(struct thread *td, struct linux_ipc_args *args)
343 switch (args->what & 0xFFFF) {
345 struct linux_semop_args a;
347 a.semid = args->arg1;
349 a.nsops = args->arg2;
350 return (linux_semop(td, &a));
353 struct linux_semget_args a;
356 a.nsems = args->arg2;
357 a.semflg = args->arg3;
358 return (linux_semget(td, &a));
361 struct linux_semctl_args a;
364 a.semid = args->arg1;
365 a.semnum = args->arg2;
367 error = copyin(args->ptr, &a.arg, sizeof(a.arg));
370 return (linux_semctl(td, &a));
373 struct linux_msgsnd_args a;
375 a.msqid = args->arg1;
377 a.msgsz = args->arg2;
378 a.msgflg = args->arg3;
379 return (linux_msgsnd(td, &a));
382 struct linux_msgrcv_args a;
384 a.msqid = args->arg1;
385 a.msgsz = args->arg2;
386 a.msgflg = args->arg3;
387 if ((args->what >> 16) == 0) {
388 struct l_ipc_kludge tmp;
393 error = copyin(args->ptr, &tmp, sizeof(tmp));
396 a.msgp = PTRIN(tmp.msgp);
397 a.msgtyp = tmp.msgtyp;
400 a.msgtyp = args->arg5;
402 return (linux_msgrcv(td, &a));
405 struct linux_msgget_args a;
408 a.msgflg = args->arg2;
409 return (linux_msgget(td, &a));
412 struct linux_msgctl_args a;
414 a.msqid = args->arg1;
417 return (linux_msgctl(td, &a));
420 struct linux_shmat_args a;
422 a.shmid = args->arg1;
423 a.shmaddr = args->ptr;
424 a.shmflg = args->arg2;
425 a.raddr = PTRIN((l_uint)args->arg3);
426 return (linux_shmat(td, &a));
429 struct linux_shmdt_args a;
431 a.shmaddr = args->ptr;
432 return (linux_shmdt(td, &a));
435 struct linux_shmget_args a;
439 a.shmflg = args->arg3;
440 return (linux_shmget(td, &a));
443 struct linux_shmctl_args a;
445 a.shmid = args->arg1;
448 return (linux_shmctl(td, &a));
458 linux_old_select(struct thread *td, struct linux_old_select_args *args)
460 struct l_old_select_argv linux_args;
461 struct linux_select_args newsel;
465 if (ldebug(old_select))
466 printf(ARGS(old_select, "%p"), args->ptr);
469 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
473 newsel.nfds = linux_args.nfds;
474 newsel.readfds = PTRIN(linux_args.readfds);
475 newsel.writefds = PTRIN(linux_args.writefds);
476 newsel.exceptfds = PTRIN(linux_args.exceptfds);
477 newsel.timeout = PTRIN(linux_args.timeout);
478 return (linux_select(td, &newsel));
482 linux_fork(struct thread *td, struct linux_fork_args *args)
490 printf(ARGS(fork, ""));
493 if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2)) != 0)
497 td->td_retval[0] = p2->p_pid;
498 td->td_retval[1] = 0;
501 if (td->td_retval[1] == 1)
502 td->td_retval[0] = 0;
503 error = linux_proc_init(td, td->td_retval[0], 0);
507 td2 = FIRST_THREAD_IN_PROC(p2);
510 * Make this runnable after we are finished with it.
514 sched_add(td2, SRQ_BORING);
521 linux_vfork(struct thread *td, struct linux_vfork_args *args)
529 printf(ARGS(vfork, ""));
532 /* Exclude RFPPWAIT */
533 if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2)) != 0)
536 td->td_retval[0] = p2->p_pid;
537 td->td_retval[1] = 0;
539 /* Are we the child? */
540 if (td->td_retval[1] == 1)
541 td->td_retval[0] = 0;
542 error = linux_proc_init(td, td->td_retval[0], 0);
547 p2->p_flag |= P_PPWAIT;
550 td2 = FIRST_THREAD_IN_PROC(p2);
553 * Make this runnable after we are finished with it.
557 sched_add(td2, SRQ_BORING);
560 /* wait for the children to exit, ie. emulate vfork */
562 while (p2->p_flag & P_PPWAIT)
563 cv_wait(&p2->p_pwait, &p2->p_mtx);
570 linux_clone(struct thread *td, struct linux_clone_args *args)
572 int error, ff = RFPROC | RFSTOPPED;
576 struct linux_emuldata *em;
580 printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, "
581 "child tid: %p"), (unsigned)args->flags,
582 args->stack, args->parent_tidptr, args->child_tidptr);
586 exit_signal = args->flags & 0x000000ff;
587 if (LINUX_SIG_VALID(exit_signal)) {
588 if (exit_signal <= LINUX_SIGTBLSZ)
590 linux_to_bsd_signal[_SIG_IDX(exit_signal)];
591 } else if (exit_signal != 0)
594 if (args->flags & LINUX_CLONE_VM)
596 if (args->flags & LINUX_CLONE_SIGHAND)
599 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
600 * and open files is independant. In FreeBSD, its in one
601 * structure but in reality it does not cause any problems
602 * because both of these flags are usually set together.
604 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
608 * Attempt to detect when linux_clone(2) is used for creating
609 * kernel threads. Unfortunately despite the existence of the
610 * CLONE_THREAD flag, version of linuxthreads package used in
611 * most popular distros as of beginning of 2005 doesn't make
612 * any use of it. Therefore, this detection relies on
613 * empirical observation that linuxthreads sets certain
614 * combination of flags, so that we can make more or less
615 * precise detection and notify the FreeBSD kernel that several
616 * processes are in fact part of the same threading group, so
617 * that special treatment is necessary for signal delivery
618 * between those processes and fd locking.
620 if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
623 if (args->flags & LINUX_CLONE_PARENT_SETTID)
624 if (args->parent_tidptr == NULL)
627 error = fork1(td, ff, 0, &p2);
631 if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) {
632 sx_xlock(&proctree_lock);
634 proc_reparent(p2, td->td_proc->p_pptr);
636 sx_xunlock(&proctree_lock);
639 /* create the emuldata */
640 error = linux_proc_init(td, p2->p_pid, args->flags);
641 /* reference it - no need to check this */
642 em = em_find(p2, EMUL_DOLOCK);
643 KASSERT(em != NULL, ("clone: emuldata not found.\n"));
646 if (args->flags & LINUX_CLONE_THREAD) {
649 p2->p_pgrp = td->td_proc->p_pgrp;
655 if (args->flags & LINUX_CLONE_CHILD_SETTID)
656 em->child_set_tid = args->child_tidptr;
658 em->child_set_tid = NULL;
660 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
661 em->child_clear_tid = args->child_tidptr;
663 em->child_clear_tid = NULL;
665 EMUL_UNLOCK(&emul_lock);
667 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
668 error = copyout(&p2->p_pid, args->parent_tidptr,
671 printf(LMSG("copyout failed!"));
675 p2->p_sigparent = exit_signal;
677 td2 = FIRST_THREAD_IN_PROC(p2);
679 * In a case of stack = NULL, we are supposed to COW calling process
680 * stack. This is what normal fork() does, so we just keep tf_rsp arg
684 td2->td_frame->tf_rsp = PTROUT(args->stack);
686 if (args->flags & LINUX_CLONE_SETTLS) {
687 struct user_segment_descriptor sd;
688 struct l_user_desc info;
691 error = copyin((void *)td->td_frame->tf_rsi, &info,
692 sizeof(struct l_user_desc));
694 printf(LMSG("copyin failed!"));
696 /* We might copy out the entry_number as GUGS32_SEL. */
697 info.entry_number = GUGS32_SEL;
698 error = copyout(&info, (void *)td->td_frame->tf_rsi,
699 sizeof(struct l_user_desc));
701 printf(LMSG("copyout failed!"));
703 a[0] = LINUX_LDT_entry_a(&info);
704 a[1] = LINUX_LDT_entry_b(&info);
706 memcpy(&sd, &a, sizeof(a));
709 printf("Segment created in clone with "
710 "CLONE_SETTLS: lobase: %x, hibase: %x, "
711 "lolimit: %x, hilimit: %x, type: %i, "
712 "dpl: %i, p: %i, xx: %i, long: %i, "
713 "def32: %i, gran: %i\n", sd.sd_lobase,
714 sd.sd_hibase, sd.sd_lolimit, sd.sd_hilimit,
715 sd.sd_type, sd.sd_dpl, sd.sd_p, sd.sd_xx,
716 sd.sd_long, sd.sd_def32, sd.sd_gran);
718 td2->td_pcb->pcb_gsbase = (register_t)info.base_addr;
719 /* XXXKIB td2->td_pcb->pcb_gs32sd = sd; */
720 td2->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL);
721 td2->td_pcb->pcb_flags |= PCB_GS32BIT | PCB_32BIT;
727 printf(LMSG("clone: successful rfork to %d, "
728 "stack %p sig = %d"), (int)p2->p_pid, args->stack,
731 if (args->flags & LINUX_CLONE_VFORK) {
733 p2->p_flag |= P_PPWAIT;
738 * Make this runnable after we are finished with it.
742 sched_add(td2, SRQ_BORING);
745 td->td_retval[0] = p2->p_pid;
746 td->td_retval[1] = 0;
748 if (args->flags & LINUX_CLONE_VFORK) {
749 /* wait for the children to exit, ie. emulate vfork */
751 while (p2->p_flag & P_PPWAIT)
752 cv_wait(&p2->p_pwait, &p2->p_mtx);
759 #define STACK_SIZE (2 * 1024 * 1024)
760 #define GUARD_SIZE (4 * PAGE_SIZE)
762 static int linux_mmap_common(struct thread *, struct l_mmap_argv *);
765 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
767 struct l_mmap_argv linux_args;
771 printf(ARGS(mmap2, "0x%08x, %d, %d, 0x%08x, %d, %d"),
772 args->addr, args->len, args->prot,
773 args->flags, args->fd, args->pgoff);
776 linux_args.addr = PTROUT(args->addr);
777 linux_args.len = args->len;
778 linux_args.prot = args->prot;
779 linux_args.flags = args->flags;
780 linux_args.fd = args->fd;
781 linux_args.pgoff = args->pgoff;
783 return (linux_mmap_common(td, &linux_args));
787 linux_mmap(struct thread *td, struct linux_mmap_args *args)
790 struct l_mmap_argv linux_args;
792 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
798 printf(ARGS(mmap, "0x%08x, %d, %d, 0x%08x, %d, %d"),
799 linux_args.addr, linux_args.len, linux_args.prot,
800 linux_args.flags, linux_args.fd, linux_args.pgoff);
802 if ((linux_args.pgoff % PAGE_SIZE) != 0)
804 linux_args.pgoff /= PAGE_SIZE;
806 return (linux_mmap_common(td, &linux_args));
810 linux_mmap_common(struct thread *td, struct l_mmap_argv *linux_args)
812 struct proc *p = td->td_proc;
813 struct mmap_args /* {
831 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
833 if (! ((linux_args->flags & LINUX_MAP_SHARED) ^
834 (linux_args->flags & LINUX_MAP_PRIVATE)))
837 if (linux_args->flags & LINUX_MAP_SHARED)
838 bsd_args.flags |= MAP_SHARED;
839 if (linux_args->flags & LINUX_MAP_PRIVATE)
840 bsd_args.flags |= MAP_PRIVATE;
841 if (linux_args->flags & LINUX_MAP_FIXED)
842 bsd_args.flags |= MAP_FIXED;
843 if (linux_args->flags & LINUX_MAP_ANON)
844 bsd_args.flags |= MAP_ANON;
846 bsd_args.flags |= MAP_NOSYNC;
847 if (linux_args->flags & LINUX_MAP_GROWSDOWN)
848 bsd_args.flags |= MAP_STACK;
851 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
852 * on Linux/i386. We do this to ensure maximum compatibility.
853 * Linux/ia64 does the same in i386 emulation mode.
855 bsd_args.prot = linux_args->prot;
856 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
857 bsd_args.prot |= PROT_READ | PROT_EXEC;
859 /* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
860 bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : linux_args->fd;
861 if (bsd_args.fd != -1) {
863 * Linux follows Solaris mmap(2) description:
864 * The file descriptor fildes is opened with
865 * read permission, regardless of the
866 * protection options specified.
869 if ((error = fget(td, bsd_args.fd, &fp)) != 0)
871 if (fp->f_type != DTYPE_VNODE) {
876 /* Linux mmap() just fails for O_WRONLY files */
877 if (!(fp->f_flag & FREAD)) {
885 if (linux_args->flags & LINUX_MAP_GROWSDOWN) {
887 * The Linux MAP_GROWSDOWN option does not limit auto
888 * growth of the region. Linux mmap with this option
889 * takes as addr the inital BOS, and as len, the initial
890 * region size. It can then grow down from addr without
891 * limit. However, Linux threads has an implicit internal
892 * limit to stack size of STACK_SIZE. Its just not
893 * enforced explicitly in Linux. But, here we impose
894 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
895 * region, since we can do this with our mmap.
897 * Our mmap with MAP_STACK takes addr as the maximum
898 * downsize limit on BOS, and as len the max size of
899 * the region. It then maps the top SGROWSIZ bytes,
900 * and auto grows the region down, up to the limit
903 * If we don't use the MAP_STACK option, the effect
904 * of this code is to allocate a stack region of a
905 * fixed size of (STACK_SIZE - GUARD_SIZE).
908 if ((caddr_t)PTRIN(linux_args->addr) + linux_args->len >
909 p->p_vmspace->vm_maxsaddr) {
911 * Some Linux apps will attempt to mmap
912 * thread stacks near the top of their
913 * address space. If their TOS is greater
914 * than vm_maxsaddr, vm_map_growstack()
915 * will confuse the thread stack with the
916 * process stack and deliver a SEGV if they
917 * attempt to grow the thread stack past their
918 * current stacksize rlimit. To avoid this,
919 * adjust vm_maxsaddr upwards to reflect
920 * the current stacksize rlimit rather
921 * than the maximum possible stacksize.
922 * It would be better to adjust the
923 * mmap'ed region, but some apps do not check
924 * mmap's return value.
927 p->p_vmspace->vm_maxsaddr = (char *)LINUX32_USRSTACK -
928 lim_cur(p, RLIMIT_STACK);
933 * This gives us our maximum stack size and a new BOS.
934 * If we're using VM_STACK, then mmap will just map
935 * the top SGROWSIZ bytes, and let the stack grow down
936 * to the limit at BOS. If we're not using VM_STACK
937 * we map the full stack, since we don't have a way
940 if (linux_args->len > STACK_SIZE - GUARD_SIZE) {
941 bsd_args.addr = (caddr_t)PTRIN(linux_args->addr);
942 bsd_args.len = linux_args->len;
944 bsd_args.addr = (caddr_t)PTRIN(linux_args->addr) -
945 (STACK_SIZE - GUARD_SIZE - linux_args->len);
946 bsd_args.len = STACK_SIZE - GUARD_SIZE;
949 bsd_args.addr = (caddr_t)PTRIN(linux_args->addr);
950 bsd_args.len = linux_args->len;
952 bsd_args.pos = (off_t)linux_args->pgoff * PAGE_SIZE;
956 printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
958 (void *)bsd_args.addr, (int)bsd_args.len, bsd_args.prot,
959 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
961 error = mmap(td, &bsd_args);
964 printf("-> %s() return: 0x%x (0x%08x)\n",
965 __func__, error, (u_int)td->td_retval[0]);
971 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
973 struct mprotect_args bsd_args;
975 bsd_args.addr = uap->addr;
976 bsd_args.len = uap->len;
977 bsd_args.prot = uap->prot;
978 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
979 bsd_args.prot |= PROT_READ | PROT_EXEC;
980 return (mprotect(td, &bsd_args));
984 linux_iopl(struct thread *td, struct linux_iopl_args *args)
988 if (args->level < 0 || args->level > 3)
990 if ((error = priv_check(td, PRIV_IO)) != 0)
992 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
994 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
995 (args->level * (PSL_IOPL / 3));
1001 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1008 printf(ARGS(pipe, "*"));
1011 error = kern_pipe(td, fildes);
1015 /* XXX: Close descriptors on error. */
1016 return (copyout(fildes, args->pipefds, sizeof fildes));
1020 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
1023 l_sigaction_t act, oact;
1027 if (ldebug(sigaction))
1028 printf(ARGS(sigaction, "%d, %p, %p"),
1029 args->sig, (void *)args->nsa, (void *)args->osa);
1032 if (args->nsa != NULL) {
1033 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
1036 act.lsa_handler = osa.lsa_handler;
1037 act.lsa_flags = osa.lsa_flags;
1038 act.lsa_restorer = osa.lsa_restorer;
1039 LINUX_SIGEMPTYSET(act.lsa_mask);
1040 act.lsa_mask.__bits[0] = osa.lsa_mask;
1043 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
1044 args->osa ? &oact : NULL);
1046 if (args->osa != NULL && !error) {
1047 osa.lsa_handler = oact.lsa_handler;
1048 osa.lsa_flags = oact.lsa_flags;
1049 osa.lsa_restorer = oact.lsa_restorer;
1050 osa.lsa_mask = oact.lsa_mask.__bits[0];
1051 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
1058 * Linux has two extra args, restart and oldmask. We don't use these,
1059 * but it seems that "restart" is actually a context pointer that
1060 * enables the signal to happen with a different register set.
1063 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
1069 if (ldebug(sigsuspend))
1070 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
1073 LINUX_SIGEMPTYSET(mask);
1074 mask.__bits[0] = args->mask;
1075 linux_to_bsd_sigset(&mask, &sigmask);
1076 return (kern_sigsuspend(td, sigmask));
1080 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
1087 if (ldebug(rt_sigsuspend))
1088 printf(ARGS(rt_sigsuspend, "%p, %d"),
1089 (void *)uap->newset, uap->sigsetsize);
1092 if (uap->sigsetsize != sizeof(l_sigset_t))
1095 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
1099 linux_to_bsd_sigset(&lmask, &sigmask);
1100 return (kern_sigsuspend(td, sigmask));
1104 linux_pause(struct thread *td, struct linux_pause_args *args)
1106 struct proc *p = td->td_proc;
1111 printf(ARGS(pause, ""));
1115 sigmask = td->td_sigmask;
1117 return (kern_sigsuspend(td, sigmask));
1121 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
1128 if (ldebug(sigaltstack))
1129 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
1132 if (uap->uss != NULL) {
1133 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
1137 ss.ss_sp = PTRIN(lss.ss_sp);
1138 ss.ss_size = lss.ss_size;
1139 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
1141 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
1142 (uap->uoss != NULL) ? &oss : NULL);
1143 if (!error && uap->uoss != NULL) {
1144 lss.ss_sp = PTROUT(oss.ss_sp);
1145 lss.ss_size = oss.ss_size;
1146 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
1147 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
1154 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
1156 struct ftruncate_args sa;
1159 if (ldebug(ftruncate64))
1160 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
1161 (intmax_t)args->length);
1165 sa.length = args->length;
1166 return ftruncate(td, &sa);
1170 linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
1174 struct timezone rtz;
1179 atv32.tv_sec = atv.tv_sec;
1180 atv32.tv_usec = atv.tv_usec;
1181 error = copyout(&atv32, uap->tp, sizeof(atv32));
1183 if (error == 0 && uap->tzp != NULL) {
1184 rtz.tz_minuteswest = tz_minuteswest;
1185 rtz.tz_dsttime = tz_dsttime;
1186 error = copyout(&rtz, uap->tzp, sizeof(rtz));
1192 linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap)
1195 struct timeval atv, *tvp;
1196 struct timezone atz, *tzp;
1200 error = copyin(uap->tp, &atv32, sizeof(atv32));
1203 atv.tv_sec = atv32.tv_sec;
1204 atv.tv_usec = atv32.tv_usec;
1209 error = copyin(uap->tzp, &atz, sizeof(atz));
1215 return (kern_settimeofday(td, tvp, tzp));
1219 linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
1221 struct l_rusage s32;
1225 error = kern_getrusage(td, uap->who, &s);
1228 if (uap->rusage != NULL) {
1229 s32.ru_utime.tv_sec = s.ru_utime.tv_sec;
1230 s32.ru_utime.tv_usec = s.ru_utime.tv_usec;
1231 s32.ru_stime.tv_sec = s.ru_stime.tv_sec;
1232 s32.ru_stime.tv_usec = s.ru_stime.tv_usec;
1233 s32.ru_maxrss = s.ru_maxrss;
1234 s32.ru_ixrss = s.ru_ixrss;
1235 s32.ru_idrss = s.ru_idrss;
1236 s32.ru_isrss = s.ru_isrss;
1237 s32.ru_minflt = s.ru_minflt;
1238 s32.ru_majflt = s.ru_majflt;
1239 s32.ru_nswap = s.ru_nswap;
1240 s32.ru_inblock = s.ru_inblock;
1241 s32.ru_oublock = s.ru_oublock;
1242 s32.ru_msgsnd = s.ru_msgsnd;
1243 s32.ru_msgrcv = s.ru_msgrcv;
1244 s32.ru_nsignals = s.ru_nsignals;
1245 s32.ru_nvcsw = s.ru_nvcsw;
1246 s32.ru_nivcsw = s.ru_nivcsw;
1247 error = copyout(&s32, uap->rusage, sizeof(s32));
1253 linux_sched_rr_get_interval(struct thread *td,
1254 struct linux_sched_rr_get_interval_args *uap)
1257 struct l_timespec ts32;
1260 error = kern_sched_rr_get_interval(td, uap->pid, &ts);
1263 ts32.tv_sec = ts.tv_sec;
1264 ts32.tv_nsec = ts.tv_nsec;
1265 return (copyout(&ts32, uap->interval, sizeof(ts32)));
1269 linux_set_thread_area(struct thread *td,
1270 struct linux_set_thread_area_args *args)
1272 struct l_user_desc info;
1273 struct user_segment_descriptor sd;
1277 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1282 if (ldebug(set_thread_area))
1283 printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, "
1284 "%i, %i, %i"), info.entry_number, info.base_addr,
1285 info.limit, info.seg_32bit, info.contents,
1286 info.read_exec_only, info.limit_in_pages,
1287 info.seg_not_present, info.useable);
1291 * Semantics of Linux version: every thread in the system has array
1292 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown.
1293 * This syscall loads one of the selected TLS decriptors with a value
1294 * and also loads GDT descriptors 6, 7 and 8 with the content of
1295 * the per-thread descriptors.
1297 * Semantics of FreeBSD version: I think we can ignore that Linux has
1298 * three per-thread descriptors and use just the first one.
1299 * The tls_array[] is used only in [gs]et_thread_area() syscalls and
1300 * for loading the GDT descriptors. We use just one GDT descriptor
1301 * for TLS, so we will load just one.
1303 * XXX: This doesn't work when a user space process tries to use more
1304 * than one TLS segment. Comment in the Linux source says wine might
1309 * GLIBC reads current %gs and call set_thread_area() with it.
1310 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because
1311 * we use these segments.
1313 switch (info.entry_number) {
1318 info.entry_number = GUGS32_SEL;
1325 * We have to copy out the GDT entry we use.
1327 * XXX: What if a user space program does not check the return value
1328 * and tries to use 6, 7 or 8?
1330 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1334 if (LINUX_LDT_empty(&info)) {
1338 a[0] = LINUX_LDT_entry_a(&info);
1339 a[1] = LINUX_LDT_entry_b(&info);
1342 memcpy(&sd, &a, sizeof(a));
1344 if (ldebug(set_thread_area))
1345 printf("Segment created in set_thread_area: "
1346 "lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, "
1347 "type: %i, dpl: %i, p: %i, xx: %i, long: %i, "
1348 "def32: %i, gran: %i\n",
1362 td->td_pcb->pcb_gsbase = (register_t)info.base_addr;
1363 td->td_pcb->pcb_flags |= PCB_32BIT | PCB_GS32BIT;
1364 update_gdt_gsbase(td, info.base_addr);