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_map.h>
65 #include <compat/freebsd32/freebsd32_util.h>
66 #include <amd64/linux32/linux.h>
67 #include <amd64/linux32/linux32_proto.h>
68 #include <compat/linux/linux_ipc.h>
69 #include <compat/linux/linux_signal.h>
70 #include <compat/linux/linux_util.h>
71 #include <compat/linux/linux_emul.h>
73 struct l_old_select_argv {
77 l_uintptr_t exceptfds;
82 linux_to_bsd_sigaltstack(int lsa)
86 if (lsa & LINUX_SS_DISABLE)
88 if (lsa & LINUX_SS_ONSTACK)
93 static int linux_mmap_common(struct thread *td, l_uintptr_t addr,
94 l_size_t len, l_int prot, l_int flags, l_int fd,
98 bsd_to_linux_sigaltstack(int bsa)
102 if (bsa & SS_DISABLE)
103 lsa |= LINUX_SS_DISABLE;
104 if (bsa & SS_ONSTACK)
105 lsa |= LINUX_SS_ONSTACK;
110 linux_execve(struct thread *td, struct linux_execve_args *args)
112 struct image_args eargs;
116 LCONVPATHEXIST(td, args->path, &path);
120 printf(ARGS(execve, "%s"), path);
123 error = freebsd32_exec_copyin_args(&eargs, path, UIO_SYSSPACE,
124 args->argp, args->envp);
127 error = kern_execve(td, &eargs, NULL);
129 /* Linux process can execute FreeBSD one, do not attempt
130 * to create emuldata for such process using
131 * linux_proc_init, this leads to a panic on KASSERT
132 * because such process has p->p_emuldata == NULL.
134 if (td->td_proc->p_sysent == &elf_linux_sysvec)
135 error = linux_proc_init(td, 0, 0);
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) {
249 struct linux_semop_args a;
251 a.semid = args->arg1;
253 a.nsops = args->arg2;
254 return (linux_semop(td, &a));
257 struct linux_semget_args a;
260 a.nsems = args->arg2;
261 a.semflg = args->arg3;
262 return (linux_semget(td, &a));
265 struct linux_semctl_args a;
268 a.semid = args->arg1;
269 a.semnum = args->arg2;
271 error = copyin(args->ptr, &a.arg, sizeof(a.arg));
274 return (linux_semctl(td, &a));
277 struct linux_msgsnd_args a;
279 a.msqid = args->arg1;
281 a.msgsz = args->arg2;
282 a.msgflg = args->arg3;
283 return (linux_msgsnd(td, &a));
286 struct linux_msgrcv_args a;
288 a.msqid = args->arg1;
289 a.msgsz = args->arg2;
290 a.msgflg = args->arg3;
291 if ((args->what >> 16) == 0) {
292 struct l_ipc_kludge tmp;
297 error = copyin(args->ptr, &tmp, sizeof(tmp));
300 a.msgp = PTRIN(tmp.msgp);
301 a.msgtyp = tmp.msgtyp;
304 a.msgtyp = args->arg5;
306 return (linux_msgrcv(td, &a));
309 struct linux_msgget_args a;
312 a.msgflg = args->arg2;
313 return (linux_msgget(td, &a));
316 struct linux_msgctl_args a;
318 a.msqid = args->arg1;
321 return (linux_msgctl(td, &a));
324 struct linux_shmat_args a;
326 a.shmid = args->arg1;
327 a.shmaddr = args->ptr;
328 a.shmflg = args->arg2;
329 a.raddr = PTRIN((l_uint)args->arg3);
330 return (linux_shmat(td, &a));
333 struct linux_shmdt_args a;
335 a.shmaddr = args->ptr;
336 return (linux_shmdt(td, &a));
339 struct linux_shmget_args a;
343 a.shmflg = args->arg3;
344 return (linux_shmget(td, &a));
347 struct linux_shmctl_args a;
349 a.shmid = args->arg1;
352 return (linux_shmctl(td, &a));
362 linux_old_select(struct thread *td, struct linux_old_select_args *args)
364 struct l_old_select_argv linux_args;
365 struct linux_select_args newsel;
369 if (ldebug(old_select))
370 printf(ARGS(old_select, "%p"), args->ptr);
373 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
377 newsel.nfds = linux_args.nfds;
378 newsel.readfds = PTRIN(linux_args.readfds);
379 newsel.writefds = PTRIN(linux_args.writefds);
380 newsel.exceptfds = PTRIN(linux_args.exceptfds);
381 newsel.timeout = PTRIN(linux_args.timeout);
382 return (linux_select(td, &newsel));
386 linux_fork(struct thread *td, struct linux_fork_args *args)
394 printf(ARGS(fork, ""));
397 if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2)) != 0)
401 td->td_retval[0] = p2->p_pid;
402 td->td_retval[1] = 0;
405 if (td->td_retval[1] == 1)
406 td->td_retval[0] = 0;
407 error = linux_proc_init(td, td->td_retval[0], 0);
411 td2 = FIRST_THREAD_IN_PROC(p2);
414 * Make this runnable after we are finished with it.
418 sched_add(td2, SRQ_BORING);
425 linux_vfork(struct thread *td, struct linux_vfork_args *args)
433 printf(ARGS(vfork, ""));
436 /* Exclude RFPPWAIT */
437 if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2)) != 0)
440 td->td_retval[0] = p2->p_pid;
441 td->td_retval[1] = 0;
443 /* Are we the child? */
444 if (td->td_retval[1] == 1)
445 td->td_retval[0] = 0;
446 error = linux_proc_init(td, td->td_retval[0], 0);
451 p2->p_flag |= P_PPWAIT;
454 td2 = FIRST_THREAD_IN_PROC(p2);
457 * Make this runnable after we are finished with it.
461 sched_add(td2, SRQ_BORING);
464 /* wait for the children to exit, ie. emulate vfork */
466 while (p2->p_flag & P_PPWAIT)
467 cv_wait(&p2->p_pwait, &p2->p_mtx);
474 linux_clone(struct thread *td, struct linux_clone_args *args)
476 int error, ff = RFPROC | RFSTOPPED;
480 struct linux_emuldata *em;
484 printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, "
485 "child tid: %p"), (unsigned)args->flags,
486 args->stack, args->parent_tidptr, args->child_tidptr);
490 exit_signal = args->flags & 0x000000ff;
491 if (LINUX_SIG_VALID(exit_signal)) {
492 if (exit_signal <= LINUX_SIGTBLSZ)
494 linux_to_bsd_signal[_SIG_IDX(exit_signal)];
495 } else if (exit_signal != 0)
498 if (args->flags & LINUX_CLONE_VM)
500 if (args->flags & LINUX_CLONE_SIGHAND)
503 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
504 * and open files is independant. In FreeBSD, its in one
505 * structure but in reality it does not cause any problems
506 * because both of these flags are usually set together.
508 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
512 * Attempt to detect when linux_clone(2) is used for creating
513 * kernel threads. Unfortunately despite the existence of the
514 * CLONE_THREAD flag, version of linuxthreads package used in
515 * most popular distros as of beginning of 2005 doesn't make
516 * any use of it. Therefore, this detection relies on
517 * empirical observation that linuxthreads sets certain
518 * combination of flags, so that we can make more or less
519 * precise detection and notify the FreeBSD kernel that several
520 * processes are in fact part of the same threading group, so
521 * that special treatment is necessary for signal delivery
522 * between those processes and fd locking.
524 if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
527 if (args->flags & LINUX_CLONE_PARENT_SETTID)
528 if (args->parent_tidptr == NULL)
531 error = fork1(td, ff, 0, &p2);
535 if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) {
536 sx_xlock(&proctree_lock);
538 proc_reparent(p2, td->td_proc->p_pptr);
540 sx_xunlock(&proctree_lock);
543 /* create the emuldata */
544 error = linux_proc_init(td, p2->p_pid, args->flags);
545 /* reference it - no need to check this */
546 em = em_find(p2, EMUL_DOLOCK);
547 KASSERT(em != NULL, ("clone: emuldata not found.\n"));
550 if (args->flags & LINUX_CLONE_THREAD) {
553 p2->p_pgrp = td->td_proc->p_pgrp;
559 if (args->flags & LINUX_CLONE_CHILD_SETTID)
560 em->child_set_tid = args->child_tidptr;
562 em->child_set_tid = NULL;
564 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
565 em->child_clear_tid = args->child_tidptr;
567 em->child_clear_tid = NULL;
569 EMUL_UNLOCK(&emul_lock);
571 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
572 error = copyout(&p2->p_pid, args->parent_tidptr,
575 printf(LMSG("copyout failed!"));
579 p2->p_sigparent = exit_signal;
581 td2 = FIRST_THREAD_IN_PROC(p2);
583 * In a case of stack = NULL, we are supposed to COW calling process
584 * stack. This is what normal fork() does, so we just keep tf_rsp arg
588 td2->td_frame->tf_rsp = PTROUT(args->stack);
590 if (args->flags & LINUX_CLONE_SETTLS) {
591 struct user_segment_descriptor sd;
592 struct l_user_desc info;
595 error = copyin((void *)td->td_frame->tf_rsi, &info,
596 sizeof(struct l_user_desc));
598 printf(LMSG("copyin failed!"));
600 /* We might copy out the entry_number as GUGS32_SEL. */
601 info.entry_number = GUGS32_SEL;
602 error = copyout(&info, (void *)td->td_frame->tf_rsi,
603 sizeof(struct l_user_desc));
605 printf(LMSG("copyout failed!"));
607 a[0] = LINUX_LDT_entry_a(&info);
608 a[1] = LINUX_LDT_entry_b(&info);
610 memcpy(&sd, &a, sizeof(a));
613 printf("Segment created in clone with "
614 "CLONE_SETTLS: lobase: %x, hibase: %x, "
615 "lolimit: %x, hilimit: %x, type: %i, "
616 "dpl: %i, p: %i, xx: %i, long: %i, "
617 "def32: %i, gran: %i\n", sd.sd_lobase,
618 sd.sd_hibase, sd.sd_lolimit, sd.sd_hilimit,
619 sd.sd_type, sd.sd_dpl, sd.sd_p, sd.sd_xx,
620 sd.sd_long, sd.sd_def32, sd.sd_gran);
622 td2->td_pcb->pcb_gsbase = (register_t)info.base_addr;
623 /* XXXKIB td2->td_pcb->pcb_gs32sd = sd; */
624 td2->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL);
625 td2->td_pcb->pcb_flags |= PCB_GS32BIT | PCB_32BIT;
631 printf(LMSG("clone: successful rfork to %d, "
632 "stack %p sig = %d"), (int)p2->p_pid, args->stack,
635 if (args->flags & LINUX_CLONE_VFORK) {
637 p2->p_flag |= P_PPWAIT;
642 * Make this runnable after we are finished with it.
646 sched_add(td2, SRQ_BORING);
649 td->td_retval[0] = p2->p_pid;
650 td->td_retval[1] = 0;
652 if (args->flags & LINUX_CLONE_VFORK) {
653 /* wait for the children to exit, ie. emulate vfork */
655 while (p2->p_flag & P_PPWAIT)
656 cv_wait(&p2->p_pwait, &p2->p_mtx);
663 #define STACK_SIZE (2 * 1024 * 1024)
664 #define GUARD_SIZE (4 * PAGE_SIZE)
667 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
672 printf(ARGS(mmap2, "0x%08x, %d, %d, 0x%08x, %d, %d"),
673 args->addr, args->len, args->prot,
674 args->flags, args->fd, args->pgoff);
677 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot,
678 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
683 linux_mmap(struct thread *td, struct linux_mmap_args *args)
686 struct l_mmap_argv linux_args;
688 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
694 printf(ARGS(mmap, "0x%08x, %d, %d, 0x%08x, %d, %d"),
695 linux_args.addr, linux_args.len, linux_args.prot,
696 linux_args.flags, linux_args.fd, linux_args.pgoff);
699 return (linux_mmap_common(td, linux_args.addr, linux_args.len,
700 linux_args.prot, linux_args.flags, linux_args.fd,
701 (uint32_t)linux_args.pgoff));
705 linux_mmap_common(struct thread *td, l_uintptr_t addr, l_size_t len, l_int prot,
706 l_int flags, l_int fd, l_loff_t pos)
708 struct proc *p = td->td_proc;
709 struct mmap_args /* {
727 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
729 if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE)))
732 if (flags & LINUX_MAP_SHARED)
733 bsd_args.flags |= MAP_SHARED;
734 if (flags & LINUX_MAP_PRIVATE)
735 bsd_args.flags |= MAP_PRIVATE;
736 if (flags & LINUX_MAP_FIXED)
737 bsd_args.flags |= MAP_FIXED;
738 if (flags & LINUX_MAP_ANON) {
739 /* Enforce pos to be on page boundary, then ignore. */
740 if ((pos & PAGE_MASK) != 0)
743 bsd_args.flags |= MAP_ANON;
745 bsd_args.flags |= MAP_NOSYNC;
746 if (flags & LINUX_MAP_GROWSDOWN)
747 bsd_args.flags |= MAP_STACK;
750 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
751 * on Linux/i386. We do this to ensure maximum compatibility.
752 * Linux/ia64 does the same in i386 emulation mode.
754 bsd_args.prot = prot;
755 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
756 bsd_args.prot |= PROT_READ | PROT_EXEC;
758 /* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
759 bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : fd;
760 if (bsd_args.fd != -1) {
762 * Linux follows Solaris mmap(2) description:
763 * The file descriptor fildes is opened with
764 * read permission, regardless of the
765 * protection options specified.
768 if ((error = fget(td, bsd_args.fd, &fp)) != 0)
770 if (fp->f_type != DTYPE_VNODE) {
775 /* Linux mmap() just fails for O_WRONLY files */
776 if (!(fp->f_flag & FREAD)) {
784 if (flags & LINUX_MAP_GROWSDOWN) {
786 * The Linux MAP_GROWSDOWN option does not limit auto
787 * growth of the region. Linux mmap with this option
788 * takes as addr the inital BOS, and as len, the initial
789 * region size. It can then grow down from addr without
790 * limit. However, Linux threads has an implicit internal
791 * limit to stack size of STACK_SIZE. Its just not
792 * enforced explicitly in Linux. But, here we impose
793 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
794 * region, since we can do this with our mmap.
796 * Our mmap with MAP_STACK takes addr as the maximum
797 * downsize limit on BOS, and as len the max size of
798 * the region. It then maps the top SGROWSIZ bytes,
799 * and auto grows the region down, up to the limit
802 * If we don't use the MAP_STACK option, the effect
803 * of this code is to allocate a stack region of a
804 * fixed size of (STACK_SIZE - GUARD_SIZE).
807 if ((caddr_t)PTRIN(addr) + len > p->p_vmspace->vm_maxsaddr) {
809 * Some Linux apps will attempt to mmap
810 * thread stacks near the top of their
811 * address space. If their TOS is greater
812 * than vm_maxsaddr, vm_map_growstack()
813 * will confuse the thread stack with the
814 * process stack and deliver a SEGV if they
815 * attempt to grow the thread stack past their
816 * current stacksize rlimit. To avoid this,
817 * adjust vm_maxsaddr upwards to reflect
818 * the current stacksize rlimit rather
819 * than the maximum possible stacksize.
820 * It would be better to adjust the
821 * mmap'ed region, but some apps do not check
822 * mmap's return value.
825 p->p_vmspace->vm_maxsaddr = (char *)LINUX32_USRSTACK -
826 lim_cur(p, RLIMIT_STACK);
831 * This gives us our maximum stack size and a new BOS.
832 * If we're using VM_STACK, then mmap will just map
833 * the top SGROWSIZ bytes, and let the stack grow down
834 * to the limit at BOS. If we're not using VM_STACK
835 * we map the full stack, since we don't have a way
838 if (len > STACK_SIZE - GUARD_SIZE) {
839 bsd_args.addr = (caddr_t)PTRIN(addr);
842 bsd_args.addr = (caddr_t)PTRIN(addr) -
843 (STACK_SIZE - GUARD_SIZE - len);
844 bsd_args.len = STACK_SIZE - GUARD_SIZE;
847 bsd_args.addr = (caddr_t)PTRIN(addr);
854 printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
856 (void *)bsd_args.addr, (int)bsd_args.len, bsd_args.prot,
857 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
859 error = mmap(td, &bsd_args);
862 printf("-> %s() return: 0x%x (0x%08x)\n",
863 __func__, error, (u_int)td->td_retval[0]);
869 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
871 struct mprotect_args bsd_args;
873 bsd_args.addr = uap->addr;
874 bsd_args.len = uap->len;
875 bsd_args.prot = uap->prot;
876 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
877 bsd_args.prot |= PROT_READ | PROT_EXEC;
878 return (mprotect(td, &bsd_args));
882 linux_iopl(struct thread *td, struct linux_iopl_args *args)
886 if (args->level < 0 || args->level > 3)
888 if ((error = priv_check(td, PRIV_IO)) != 0)
890 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
892 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) |
893 (args->level * (PSL_IOPL / 3));
899 linux_pipe(struct thread *td, struct linux_pipe_args *args)
906 printf(ARGS(pipe, "*"));
909 error = kern_pipe(td, fildes);
913 /* XXX: Close descriptors on error. */
914 return (copyout(fildes, args->pipefds, sizeof fildes));
918 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
921 l_sigaction_t act, oact;
925 if (ldebug(sigaction))
926 printf(ARGS(sigaction, "%d, %p, %p"),
927 args->sig, (void *)args->nsa, (void *)args->osa);
930 if (args->nsa != NULL) {
931 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
934 act.lsa_handler = osa.lsa_handler;
935 act.lsa_flags = osa.lsa_flags;
936 act.lsa_restorer = osa.lsa_restorer;
937 LINUX_SIGEMPTYSET(act.lsa_mask);
938 act.lsa_mask.__bits[0] = osa.lsa_mask;
941 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
942 args->osa ? &oact : NULL);
944 if (args->osa != NULL && !error) {
945 osa.lsa_handler = oact.lsa_handler;
946 osa.lsa_flags = oact.lsa_flags;
947 osa.lsa_restorer = oact.lsa_restorer;
948 osa.lsa_mask = oact.lsa_mask.__bits[0];
949 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
956 * Linux has two extra args, restart and oldmask. We don't use these,
957 * but it seems that "restart" is actually a context pointer that
958 * enables the signal to happen with a different register set.
961 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
967 if (ldebug(sigsuspend))
968 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
971 LINUX_SIGEMPTYSET(mask);
972 mask.__bits[0] = args->mask;
973 linux_to_bsd_sigset(&mask, &sigmask);
974 return (kern_sigsuspend(td, sigmask));
978 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
985 if (ldebug(rt_sigsuspend))
986 printf(ARGS(rt_sigsuspend, "%p, %d"),
987 (void *)uap->newset, uap->sigsetsize);
990 if (uap->sigsetsize != sizeof(l_sigset_t))
993 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
997 linux_to_bsd_sigset(&lmask, &sigmask);
998 return (kern_sigsuspend(td, sigmask));
1002 linux_pause(struct thread *td, struct linux_pause_args *args)
1004 struct proc *p = td->td_proc;
1009 printf(ARGS(pause, ""));
1013 sigmask = td->td_sigmask;
1015 return (kern_sigsuspend(td, sigmask));
1019 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
1026 if (ldebug(sigaltstack))
1027 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
1030 if (uap->uss != NULL) {
1031 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
1035 ss.ss_sp = PTRIN(lss.ss_sp);
1036 ss.ss_size = lss.ss_size;
1037 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
1039 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
1040 (uap->uoss != NULL) ? &oss : NULL);
1041 if (!error && uap->uoss != NULL) {
1042 lss.ss_sp = PTROUT(oss.ss_sp);
1043 lss.ss_size = oss.ss_size;
1044 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
1045 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
1052 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
1054 struct ftruncate_args sa;
1057 if (ldebug(ftruncate64))
1058 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
1059 (intmax_t)args->length);
1063 sa.length = args->length;
1064 return ftruncate(td, &sa);
1068 linux_gettimeofday(struct thread *td, struct linux_gettimeofday_args *uap)
1072 struct timezone rtz;
1077 atv32.tv_sec = atv.tv_sec;
1078 atv32.tv_usec = atv.tv_usec;
1079 error = copyout(&atv32, uap->tp, sizeof(atv32));
1081 if (error == 0 && uap->tzp != NULL) {
1082 rtz.tz_minuteswest = tz_minuteswest;
1083 rtz.tz_dsttime = tz_dsttime;
1084 error = copyout(&rtz, uap->tzp, sizeof(rtz));
1090 linux_settimeofday(struct thread *td, struct linux_settimeofday_args *uap)
1093 struct timeval atv, *tvp;
1094 struct timezone atz, *tzp;
1098 error = copyin(uap->tp, &atv32, sizeof(atv32));
1101 atv.tv_sec = atv32.tv_sec;
1102 atv.tv_usec = atv32.tv_usec;
1107 error = copyin(uap->tzp, &atz, sizeof(atz));
1113 return (kern_settimeofday(td, tvp, tzp));
1117 linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
1119 struct l_rusage s32;
1123 error = kern_getrusage(td, uap->who, &s);
1126 if (uap->rusage != NULL) {
1127 s32.ru_utime.tv_sec = s.ru_utime.tv_sec;
1128 s32.ru_utime.tv_usec = s.ru_utime.tv_usec;
1129 s32.ru_stime.tv_sec = s.ru_stime.tv_sec;
1130 s32.ru_stime.tv_usec = s.ru_stime.tv_usec;
1131 s32.ru_maxrss = s.ru_maxrss;
1132 s32.ru_ixrss = s.ru_ixrss;
1133 s32.ru_idrss = s.ru_idrss;
1134 s32.ru_isrss = s.ru_isrss;
1135 s32.ru_minflt = s.ru_minflt;
1136 s32.ru_majflt = s.ru_majflt;
1137 s32.ru_nswap = s.ru_nswap;
1138 s32.ru_inblock = s.ru_inblock;
1139 s32.ru_oublock = s.ru_oublock;
1140 s32.ru_msgsnd = s.ru_msgsnd;
1141 s32.ru_msgrcv = s.ru_msgrcv;
1142 s32.ru_nsignals = s.ru_nsignals;
1143 s32.ru_nvcsw = s.ru_nvcsw;
1144 s32.ru_nivcsw = s.ru_nivcsw;
1145 error = copyout(&s32, uap->rusage, sizeof(s32));
1151 linux_sched_rr_get_interval(struct thread *td,
1152 struct linux_sched_rr_get_interval_args *uap)
1155 struct l_timespec ts32;
1158 error = kern_sched_rr_get_interval(td, uap->pid, &ts);
1161 ts32.tv_sec = ts.tv_sec;
1162 ts32.tv_nsec = ts.tv_nsec;
1163 return (copyout(&ts32, uap->interval, sizeof(ts32)));
1167 linux_set_thread_area(struct thread *td,
1168 struct linux_set_thread_area_args *args)
1170 struct l_user_desc info;
1171 struct user_segment_descriptor sd;
1175 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1180 if (ldebug(set_thread_area))
1181 printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, "
1182 "%i, %i, %i"), info.entry_number, info.base_addr,
1183 info.limit, info.seg_32bit, info.contents,
1184 info.read_exec_only, info.limit_in_pages,
1185 info.seg_not_present, info.useable);
1189 * Semantics of Linux version: every thread in the system has array
1190 * of three TLS descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown.
1191 * This syscall loads one of the selected TLS decriptors with a value
1192 * and also loads GDT descriptors 6, 7 and 8 with the content of
1193 * the per-thread descriptors.
1195 * Semantics of FreeBSD version: I think we can ignore that Linux has
1196 * three per-thread descriptors and use just the first one.
1197 * The tls_array[] is used only in [gs]et_thread_area() syscalls and
1198 * for loading the GDT descriptors. We use just one GDT descriptor
1199 * for TLS, so we will load just one.
1201 * XXX: This doesn't work when a user space process tries to use more
1202 * than one TLS segment. Comment in the Linux source says wine might
1207 * GLIBC reads current %gs and call set_thread_area() with it.
1208 * We should let GUDATA_SEL and GUGS32_SEL proceed as well because
1209 * we use these segments.
1211 switch (info.entry_number) {
1216 info.entry_number = GUGS32_SEL;
1223 * We have to copy out the GDT entry we use.
1225 * XXX: What if a user space program does not check the return value
1226 * and tries to use 6, 7 or 8?
1228 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1232 if (LINUX_LDT_empty(&info)) {
1236 a[0] = LINUX_LDT_entry_a(&info);
1237 a[1] = LINUX_LDT_entry_b(&info);
1240 memcpy(&sd, &a, sizeof(a));
1242 if (ldebug(set_thread_area))
1243 printf("Segment created in set_thread_area: "
1244 "lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, "
1245 "type: %i, dpl: %i, p: %i, xx: %i, long: %i, "
1246 "def32: %i, gran: %i\n",
1260 td->td_pcb->pcb_gsbase = (register_t)info.base_addr;
1261 td->td_pcb->pcb_flags |= PCB_32BIT | PCB_GS32BIT;
1262 update_gdt_gsbase(td, info.base_addr);