2 * Copyright (c) 2000 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact.h>
38 #include <sys/malloc.h>
40 #include <sys/mutex.h>
44 #include <sys/queue.h>
45 #include <sys/resource.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signalvar.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/sysproto.h>
50 #include <sys/unistd.h>
52 #include <sys/sched.h>
54 #include <machine/frame.h>
55 #include <machine/psl.h>
56 #include <machine/segments.h>
57 #include <machine/sysarch.h>
61 #include <vm/vm_map.h>
63 #include <i386/linux/linux.h>
64 #include <i386/linux/linux_proto.h>
65 #include <compat/linux/linux_ipc.h>
66 #include <compat/linux/linux_signal.h>
67 #include <compat/linux/linux_util.h>
68 #include <compat/linux/linux_emul.h>
70 #include <i386/include/pcb.h> /* needed for pcb definition in linux_set_thread_area */
72 #include "opt_posix.h"
74 extern struct sysentvec elf32_freebsd_sysvec; /* defined in i386/i386/elf_machdep.c */
82 l_uint read_exec_only:1;
83 l_uint limit_in_pages:1;
84 l_uint seg_not_present:1;
88 struct l_old_select_argv {
93 struct l_timeval *timeout;
96 static int linux_mmap_common(struct thread *td, l_uintptr_t addr,
97 l_size_t len, l_int prot, l_int flags, l_int fd,
101 linux_to_bsd_sigaltstack(int lsa)
105 if (lsa & LINUX_SS_DISABLE)
107 if (lsa & LINUX_SS_ONSTACK)
113 bsd_to_linux_sigaltstack(int bsa)
117 if (bsa & SS_DISABLE)
118 lsa |= LINUX_SS_DISABLE;
119 if (bsa & SS_ONSTACK)
120 lsa |= LINUX_SS_ONSTACK;
125 linux_execve(struct thread *td, struct linux_execve_args *args)
129 struct image_args eargs;
131 LCONVPATHEXIST(td, args->path, &newpath);
135 printf(ARGS(execve, "%s"), newpath);
138 error = exec_copyin_args(&eargs, newpath, UIO_SYSSPACE,
139 args->argp, args->envp);
140 free(newpath, M_TEMP);
142 error = kern_execve(td, &eargs, NULL);
144 /* linux process can exec fbsd one, dont attempt
145 * to create emuldata for such process using
146 * linux_proc_init, this leads to a panic on KASSERT
147 * because such process has p->p_emuldata == NULL
149 if (td->td_proc->p_sysent == &elf_linux_sysvec)
150 error = linux_proc_init(td, 0, 0);
154 struct l_ipc_kludge {
155 struct l_msgbuf *msgp;
160 linux_ipc(struct thread *td, struct linux_ipc_args *args)
163 switch (args->what & 0xFFFF) {
165 struct linux_semop_args a;
167 a.semid = args->arg1;
169 a.nsops = args->arg2;
170 return (linux_semop(td, &a));
173 struct linux_semget_args a;
176 a.nsems = args->arg2;
177 a.semflg = args->arg3;
178 return (linux_semget(td, &a));
181 struct linux_semctl_args a;
184 a.semid = args->arg1;
185 a.semnum = args->arg2;
187 error = copyin(args->ptr, &a.arg, sizeof(a.arg));
190 return (linux_semctl(td, &a));
193 struct linux_msgsnd_args a;
195 a.msqid = args->arg1;
197 a.msgsz = args->arg2;
198 a.msgflg = args->arg3;
199 return (linux_msgsnd(td, &a));
202 struct linux_msgrcv_args a;
204 a.msqid = args->arg1;
205 a.msgsz = args->arg2;
206 a.msgflg = args->arg3;
207 if ((args->what >> 16) == 0) {
208 struct l_ipc_kludge tmp;
211 if (args->ptr == NULL)
213 error = copyin(args->ptr, &tmp, sizeof(tmp));
217 a.msgtyp = tmp.msgtyp;
220 a.msgtyp = args->arg5;
222 return (linux_msgrcv(td, &a));
225 struct linux_msgget_args a;
228 a.msgflg = args->arg2;
229 return (linux_msgget(td, &a));
232 struct linux_msgctl_args a;
234 a.msqid = args->arg1;
237 return (linux_msgctl(td, &a));
240 struct linux_shmat_args a;
242 a.shmid = args->arg1;
243 a.shmaddr = args->ptr;
244 a.shmflg = args->arg2;
245 a.raddr = (l_ulong *)args->arg3;
246 return (linux_shmat(td, &a));
249 struct linux_shmdt_args a;
251 a.shmaddr = args->ptr;
252 return (linux_shmdt(td, &a));
255 struct linux_shmget_args a;
259 a.shmflg = args->arg3;
260 return (linux_shmget(td, &a));
263 struct linux_shmctl_args a;
265 a.shmid = args->arg1;
268 return (linux_shmctl(td, &a));
278 linux_old_select(struct thread *td, struct linux_old_select_args *args)
280 struct l_old_select_argv linux_args;
281 struct linux_select_args newsel;
285 if (ldebug(old_select))
286 printf(ARGS(old_select, "%p"), args->ptr);
289 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
293 newsel.nfds = linux_args.nfds;
294 newsel.readfds = linux_args.readfds;
295 newsel.writefds = linux_args.writefds;
296 newsel.exceptfds = linux_args.exceptfds;
297 newsel.timeout = linux_args.timeout;
298 return (linux_select(td, &newsel));
302 linux_fork(struct thread *td, struct linux_fork_args *args)
310 printf(ARGS(fork, ""));
313 if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2)) != 0)
317 td->td_retval[0] = p2->p_pid;
318 td->td_retval[1] = 0;
321 if (td->td_retval[1] == 1)
322 td->td_retval[0] = 0;
323 error = linux_proc_init(td, td->td_retval[0], 0);
327 td2 = FIRST_THREAD_IN_PROC(p2);
330 * Make this runnable after we are finished with it.
334 sched_add(td2, SRQ_BORING);
341 linux_vfork(struct thread *td, struct linux_vfork_args *args)
349 printf(ARGS(vfork, ""));
352 /* exclude RFPPWAIT */
353 if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2)) != 0)
356 td->td_retval[0] = p2->p_pid;
357 td->td_retval[1] = 0;
359 /* Are we the child? */
360 if (td->td_retval[1] == 1)
361 td->td_retval[0] = 0;
362 error = linux_proc_init(td, td->td_retval[0], 0);
367 p2->p_flag |= P_PPWAIT;
370 td2 = FIRST_THREAD_IN_PROC(p2);
373 * Make this runnable after we are finished with it.
377 sched_add(td2, SRQ_BORING);
380 /* wait for the children to exit, ie. emulate vfork */
382 while (p2->p_flag & P_PPWAIT)
383 cv_wait(&p2->p_pwait, &p2->p_mtx);
390 linux_clone(struct thread *td, struct linux_clone_args *args)
392 int error, ff = RFPROC | RFSTOPPED;
396 struct linux_emuldata *em;
400 printf(ARGS(clone, "flags %x, stack %x, parent tid: %x, child tid: %x"),
401 (unsigned int)args->flags, (unsigned int)args->stack,
402 (unsigned int)args->parent_tidptr, (unsigned int)args->child_tidptr);
406 exit_signal = args->flags & 0x000000ff;
407 if (LINUX_SIG_VALID(exit_signal)) {
408 if (exit_signal <= LINUX_SIGTBLSZ)
410 linux_to_bsd_signal[_SIG_IDX(exit_signal)];
411 } else if (exit_signal != 0)
414 if (args->flags & LINUX_CLONE_VM)
416 if (args->flags & LINUX_CLONE_SIGHAND)
419 * XXX: in linux sharing of fs info (chroot/cwd/umask)
420 * and open files is independant. in fbsd its in one
421 * structure but in reality it doesn't cause any problems
422 * because both of these flags are usually set together.
424 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
428 * Attempt to detect when linux_clone(2) is used for creating
429 * kernel threads. Unfortunately despite the existence of the
430 * CLONE_THREAD flag, version of linuxthreads package used in
431 * most popular distros as of beginning of 2005 doesn't make
432 * any use of it. Therefore, this detection relies on
433 * empirical observation that linuxthreads sets certain
434 * combination of flags, so that we can make more or less
435 * precise detection and notify the FreeBSD kernel that several
436 * processes are in fact part of the same threading group, so
437 * that special treatment is necessary for signal delivery
438 * between those processes and fd locking.
440 if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
443 if (args->flags & LINUX_CLONE_PARENT_SETTID)
444 if (args->parent_tidptr == NULL)
447 error = fork1(td, ff, 0, &p2);
451 if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) {
452 sx_xlock(&proctree_lock);
454 proc_reparent(p2, td->td_proc->p_pptr);
456 sx_xunlock(&proctree_lock);
459 /* create the emuldata */
460 error = linux_proc_init(td, p2->p_pid, args->flags);
461 /* reference it - no need to check this */
462 em = em_find(p2, EMUL_DOLOCK);
463 KASSERT(em != NULL, ("clone: emuldata not found.\n"));
466 if (args->flags & LINUX_CLONE_THREAD) {
467 /* XXX: linux mangles pgrp and pptr somehow
468 * I think it might be this but I am not sure.
472 p2->p_pgrp = td->td_proc->p_pgrp;
478 if (args->flags & LINUX_CLONE_CHILD_SETTID)
479 em->child_set_tid = args->child_tidptr;
481 em->child_set_tid = NULL;
483 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
484 em->child_clear_tid = args->child_tidptr;
486 em->child_clear_tid = NULL;
488 EMUL_UNLOCK(&emul_lock);
490 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
491 error = copyout(&p2->p_pid, args->parent_tidptr, sizeof(p2->p_pid));
493 printf(LMSG("copyout failed!"));
497 p2->p_sigparent = exit_signal;
499 td2 = FIRST_THREAD_IN_PROC(p2);
501 * in a case of stack = NULL we are supposed to COW calling process stack
502 * this is what normal fork() does so we just keep the tf_esp arg intact
505 td2->td_frame->tf_esp = (unsigned int)args->stack;
507 if (args->flags & LINUX_CLONE_SETTLS) {
508 struct l_user_desc info;
511 struct segment_descriptor sd;
513 error = copyin((void *)td->td_frame->tf_esi, &info, sizeof(struct l_user_desc));
515 printf(LMSG("copyin failed!"));
518 idx = info.entry_number;
521 * looks like we're getting the idx we returned
522 * in the set_thread_area() syscall
524 if (idx != 6 && idx != 3) {
525 printf(LMSG("resetting idx!"));
529 /* this doesnt happen in practice */
531 /* we might copy out the entry_number as 3 */
532 info.entry_number = 3;
533 error = copyout(&info, (void *) td->td_frame->tf_esi, sizeof(struct l_user_desc));
535 printf(LMSG("copyout failed!"));
538 a[0] = LINUX_LDT_entry_a(&info);
539 a[1] = LINUX_LDT_entry_b(&info);
541 memcpy(&sd, &a, sizeof(a));
544 printf("Segment created in clone with CLONE_SETTLS: lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, def32: %i, gran: %i\n", sd.sd_lobase,
557 td2->td_pcb->pcb_gsd = sd;
558 td2->td_pcb->pcb_gs = GSEL(GUGS_SEL, SEL_UPL);
564 printf(LMSG("clone: successful rfork to %ld, stack %p sig = %d"),
565 (long)p2->p_pid, args->stack, exit_signal);
567 if (args->flags & LINUX_CLONE_VFORK) {
569 p2->p_flag |= P_PPWAIT;
574 * Make this runnable after we are finished with it.
578 sched_add(td2, SRQ_BORING);
581 td->td_retval[0] = p2->p_pid;
582 td->td_retval[1] = 0;
584 if (args->flags & LINUX_CLONE_VFORK) {
585 /* wait for the children to exit, ie. emulate vfork */
587 while (p2->p_flag & P_PPWAIT)
588 cv_wait(&p2->p_pwait, &p2->p_mtx);
595 #define STACK_SIZE (2 * 1024 * 1024)
596 #define GUARD_SIZE (4 * PAGE_SIZE)
599 linux_mmap2(struct thread *td, struct linux_mmap2_args *args)
604 printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
605 (void *)args->addr, args->len, args->prot,
606 args->flags, args->fd, args->pgoff);
609 return (linux_mmap_common(td, args->addr, args->len, args->prot,
610 args->flags, args->fd, (uint64_t)(uint32_t)args->pgoff *
615 linux_mmap(struct thread *td, struct linux_mmap_args *args)
618 struct l_mmap_argv linux_args;
620 error = copyin(args->ptr, &linux_args, sizeof(linux_args));
626 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
627 (void *)linux_args.addr, linux_args.len, linux_args.prot,
628 linux_args.flags, linux_args.fd, linux_args.pgoff);
631 return (linux_mmap_common(td, linux_args.addr, linux_args.len,
632 linux_args.prot, linux_args.flags, linux_args.fd,
633 (uint32_t)linux_args.pgoff));
637 linux_mmap_common(struct thread *td, l_uintptr_t addr, l_size_t len, l_int prot,
638 l_int flags, l_int fd, l_loff_t pos)
640 struct proc *p = td->td_proc;
641 struct mmap_args /* {
659 * You must specify exactly one of MAP_SHARED and MAP_PRIVATE
661 if (!((flags & LINUX_MAP_SHARED) ^ (flags & LINUX_MAP_PRIVATE)))
664 if (flags & LINUX_MAP_SHARED)
665 bsd_args.flags |= MAP_SHARED;
666 if (flags & LINUX_MAP_PRIVATE)
667 bsd_args.flags |= MAP_PRIVATE;
668 if (flags & LINUX_MAP_FIXED)
669 bsd_args.flags |= MAP_FIXED;
670 if (flags & LINUX_MAP_ANON) {
671 /* Enforce pos to be on page boundary, then ignore. */
672 if ((pos & PAGE_MASK) != 0)
675 bsd_args.flags |= MAP_ANON;
677 bsd_args.flags |= MAP_NOSYNC;
678 if (flags & LINUX_MAP_GROWSDOWN)
679 bsd_args.flags |= MAP_STACK;
682 * PROT_READ, PROT_WRITE, or PROT_EXEC implies PROT_READ and PROT_EXEC
683 * on Linux/i386. We do this to ensure maximum compatibility.
684 * Linux/ia64 does the same in i386 emulation mode.
686 bsd_args.prot = prot;
687 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
688 bsd_args.prot |= PROT_READ | PROT_EXEC;
690 /* Linux does not check file descriptor when MAP_ANONYMOUS is set. */
691 bsd_args.fd = (bsd_args.flags & MAP_ANON) ? -1 : fd;
692 if (bsd_args.fd != -1) {
694 * Linux follows Solaris mmap(2) description:
695 * The file descriptor fildes is opened with
696 * read permission, regardless of the
697 * protection options specified.
700 if ((error = fget(td, bsd_args.fd, &fp)) != 0)
702 if (fp->f_type != DTYPE_VNODE) {
707 /* Linux mmap() just fails for O_WRONLY files */
708 if (!(fp->f_flag & FREAD)) {
716 if (flags & LINUX_MAP_GROWSDOWN) {
718 * The Linux MAP_GROWSDOWN option does not limit auto
719 * growth of the region. Linux mmap with this option
720 * takes as addr the inital BOS, and as len, the initial
721 * region size. It can then grow down from addr without
722 * limit. However, linux threads has an implicit internal
723 * limit to stack size of STACK_SIZE. Its just not
724 * enforced explicitly in linux. But, here we impose
725 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
726 * region, since we can do this with our mmap.
728 * Our mmap with MAP_STACK takes addr as the maximum
729 * downsize limit on BOS, and as len the max size of
730 * the region. It them maps the top SGROWSIZ bytes,
731 * and auto grows the region down, up to the limit
734 * If we don't use the MAP_STACK option, the effect
735 * of this code is to allocate a stack region of a
736 * fixed size of (STACK_SIZE - GUARD_SIZE).
739 if ((caddr_t)PTRIN(addr) + len > p->p_vmspace->vm_maxsaddr) {
741 * Some linux apps will attempt to mmap
742 * thread stacks near the top of their
743 * address space. If their TOS is greater
744 * than vm_maxsaddr, vm_map_growstack()
745 * will confuse the thread stack with the
746 * process stack and deliver a SEGV if they
747 * attempt to grow the thread stack past their
748 * current stacksize rlimit. To avoid this,
749 * adjust vm_maxsaddr upwards to reflect
750 * the current stacksize rlimit rather
751 * than the maximum possible stacksize.
752 * It would be better to adjust the
753 * mmap'ed region, but some apps do not check
754 * mmap's return value.
757 p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
758 lim_cur(p, RLIMIT_STACK);
763 * This gives us our maximum stack size and a new BOS.
764 * If we're using VM_STACK, then mmap will just map
765 * the top SGROWSIZ bytes, and let the stack grow down
766 * to the limit at BOS. If we're not using VM_STACK
767 * we map the full stack, since we don't have a way
770 if (len > STACK_SIZE - GUARD_SIZE) {
771 bsd_args.addr = (caddr_t)PTRIN(addr);
774 bsd_args.addr = (caddr_t)PTRIN(addr) -
775 (STACK_SIZE - GUARD_SIZE - len);
776 bsd_args.len = STACK_SIZE - GUARD_SIZE;
779 bsd_args.addr = (caddr_t)PTRIN(addr);
786 printf("-> %s(%p, %d, %d, 0x%08x, %d, 0x%x)\n",
788 (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
789 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
791 error = mmap(td, &bsd_args);
794 printf("-> %s() return: 0x%x (0x%08x)\n",
795 __func__, error, (u_int)td->td_retval[0]);
801 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap)
803 struct mprotect_args bsd_args;
805 bsd_args.addr = uap->addr;
806 bsd_args.len = uap->len;
807 bsd_args.prot = uap->prot;
808 if (bsd_args.prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
809 bsd_args.prot |= PROT_READ | PROT_EXEC;
810 return (mprotect(td, &bsd_args));
814 linux_pipe(struct thread *td, struct linux_pipe_args *args)
821 printf(ARGS(pipe, "*"));
824 error = kern_pipe(td, fildes);
828 /* XXX: Close descriptors on error. */
829 return (copyout(fildes, args->pipefds, sizeof fildes));
833 linux_ioperm(struct thread *td, struct linux_ioperm_args *args)
836 struct i386_ioperm_args iia;
838 iia.start = args->start;
839 iia.length = args->length;
840 iia.enable = args->enable;
841 error = i386_set_ioperm(td, &iia);
846 linux_iopl(struct thread *td, struct linux_iopl_args *args)
850 if (args->level < 0 || args->level > 3)
852 if ((error = priv_check(td, PRIV_IO)) != 0)
854 if ((error = securelevel_gt(td->td_ucred, 0)) != 0)
856 td->td_frame->tf_eflags = (td->td_frame->tf_eflags & ~PSL_IOPL) |
857 (args->level * (PSL_IOPL / 3));
862 linux_modify_ldt(struct thread *td, struct linux_modify_ldt_args *uap)
865 struct i386_ldt_args ldt;
866 struct l_descriptor ld;
867 union descriptor desc;
871 case 0x00: /* read_ldt */
873 ldt.descs = uap->ptr;
874 ldt.num = uap->bytecount / sizeof(union descriptor);
875 error = i386_get_ldt(td, &ldt);
876 td->td_retval[0] *= sizeof(union descriptor);
878 case 0x02: /* read_default_ldt = 0 */
879 size = 5*sizeof(struct l_desc_struct);
880 if (size > uap->bytecount)
881 size = uap->bytecount;
882 for (written = error = 0; written < size && error == 0; written++)
883 error = subyte((char *)uap->ptr + written, 0);
884 td->td_retval[0] = written;
886 case 0x01: /* write_ldt */
887 case 0x11: /* write_ldt */
888 if (uap->bytecount != sizeof(ld))
891 error = copyin(uap->ptr, &ld, sizeof(ld));
895 ldt.start = ld.entry_number;
898 desc.sd.sd_lolimit = (ld.limit & 0x0000ffff);
899 desc.sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
900 desc.sd.sd_lobase = (ld.base_addr & 0x00ffffff);
901 desc.sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
902 desc.sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
905 desc.sd.sd_p = (ld.seg_not_present ^ 1);
907 desc.sd.sd_def32 = ld.seg_32bit;
908 desc.sd.sd_gran = ld.limit_in_pages;
909 error = i386_set_ldt(td, &ldt, &desc);
916 if (error == EOPNOTSUPP) {
917 printf("linux: modify_ldt needs kernel option USER_LDT\n");
925 linux_sigaction(struct thread *td, struct linux_sigaction_args *args)
928 l_sigaction_t act, oact;
932 if (ldebug(sigaction))
933 printf(ARGS(sigaction, "%d, %p, %p"),
934 args->sig, (void *)args->nsa, (void *)args->osa);
937 if (args->nsa != NULL) {
938 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
941 act.lsa_handler = osa.lsa_handler;
942 act.lsa_flags = osa.lsa_flags;
943 act.lsa_restorer = osa.lsa_restorer;
944 LINUX_SIGEMPTYSET(act.lsa_mask);
945 act.lsa_mask.__bits[0] = osa.lsa_mask;
948 error = linux_do_sigaction(td, args->sig, args->nsa ? &act : NULL,
949 args->osa ? &oact : NULL);
951 if (args->osa != NULL && !error) {
952 osa.lsa_handler = oact.lsa_handler;
953 osa.lsa_flags = oact.lsa_flags;
954 osa.lsa_restorer = oact.lsa_restorer;
955 osa.lsa_mask = oact.lsa_mask.__bits[0];
956 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
963 * Linux has two extra args, restart and oldmask. We dont use these,
964 * but it seems that "restart" is actually a context pointer that
965 * enables the signal to happen with a different register set.
968 linux_sigsuspend(struct thread *td, struct linux_sigsuspend_args *args)
974 if (ldebug(sigsuspend))
975 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
978 LINUX_SIGEMPTYSET(mask);
979 mask.__bits[0] = args->mask;
980 linux_to_bsd_sigset(&mask, &sigmask);
981 return (kern_sigsuspend(td, sigmask));
985 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap)
992 if (ldebug(rt_sigsuspend))
993 printf(ARGS(rt_sigsuspend, "%p, %d"),
994 (void *)uap->newset, uap->sigsetsize);
997 if (uap->sigsetsize != sizeof(l_sigset_t))
1000 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
1004 linux_to_bsd_sigset(&lmask, &sigmask);
1005 return (kern_sigsuspend(td, sigmask));
1009 linux_pause(struct thread *td, struct linux_pause_args *args)
1011 struct proc *p = td->td_proc;
1016 printf(ARGS(pause, ""));
1020 sigmask = td->td_sigmask;
1022 return (kern_sigsuspend(td, sigmask));
1026 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap)
1033 if (ldebug(sigaltstack))
1034 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
1037 if (uap->uss != NULL) {
1038 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
1042 ss.ss_sp = lss.ss_sp;
1043 ss.ss_size = lss.ss_size;
1044 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
1046 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL,
1047 (uap->uoss != NULL) ? &oss : NULL);
1048 if (!error && uap->uoss != NULL) {
1049 lss.ss_sp = oss.ss_sp;
1050 lss.ss_size = oss.ss_size;
1051 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
1052 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));
1059 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
1061 struct ftruncate_args sa;
1064 if (ldebug(ftruncate64))
1065 printf(ARGS(ftruncate64, "%u, %jd"), args->fd,
1066 (intmax_t)args->length);
1070 sa.length = args->length;
1071 return ftruncate(td, &sa);
1075 linux_set_thread_area(struct thread *td, struct linux_set_thread_area_args *args)
1077 struct l_user_desc info;
1081 struct segment_descriptor sd;
1083 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1088 if (ldebug(set_thread_area))
1089 printf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
1095 info.read_exec_only,
1096 info.limit_in_pages,
1097 info.seg_not_present,
1101 idx = info.entry_number;
1103 * Semantics of linux version: every thread in the system has array of
1104 * 3 tls descriptors. 1st is GLIBC TLS, 2nd is WINE, 3rd unknown. This
1105 * syscall loads one of the selected tls decriptors with a value and
1106 * also loads GDT descriptors 6, 7 and 8 with the content of the
1107 * per-thread descriptors.
1109 * Semantics of fbsd version: I think we can ignore that linux has 3
1110 * per-thread descriptors and use just the 1st one. The tls_array[]
1111 * is used only in set/get-thread_area() syscalls and for loading the
1112 * GDT descriptors. In fbsd we use just one GDT descriptor for TLS so
1113 * we will load just one.
1115 * XXX: this doesn't work when a user space process tries to use more
1116 * than 1 TLS segment. Comment in the linux sources says wine might do
1121 * we support just GLIBC TLS now
1122 * we should let 3 proceed as well because we use this segment so
1123 * if code does two subsequent calls it should succeed
1125 if (idx != 6 && idx != -1 && idx != 3)
1129 * we have to copy out the GDT entry we use
1130 * FreeBSD uses GDT entry #3 for storing %gs so load that
1132 * XXX: what if a user space program doesn't check this value and tries
1135 idx = info.entry_number = 3;
1136 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1140 if (LINUX_LDT_empty(&info)) {
1144 a[0] = LINUX_LDT_entry_a(&info);
1145 a[1] = LINUX_LDT_entry_b(&info);
1148 memcpy(&sd, &a, sizeof(a));
1150 if (ldebug(set_thread_area))
1151 printf("Segment created in set_thread_area: lobase: %x, hibase: %x, lolimit: %x, hilimit: %x, type: %i, dpl: %i, p: %i, xx: %i, def32: %i, gran: %i\n", sd.sd_lobase,
1163 /* this is taken from i386 version of cpu_set_user_tls() */
1166 td->td_pcb->pcb_gsd = sd;
1167 PCPU_GET(fsgs_gdt)[1] = sd;
1168 load_gs(GSEL(GUGS_SEL, SEL_UPL));
1175 linux_get_thread_area(struct thread *td, struct linux_get_thread_area_args *args)
1178 struct l_user_desc info;
1181 struct l_desc_struct desc;
1182 struct segment_descriptor sd;
1185 if (ldebug(get_thread_area))
1186 printf(ARGS(get_thread_area, "%p"), args->desc);
1189 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1193 idx = info.entry_number;
1194 /* XXX: I am not sure if we want 3 to be allowed too. */
1195 if (idx != 6 && idx != 3)
1200 memset(&info, 0, sizeof(info));
1202 sd = PCPU_GET(fsgs_gdt)[1];
1204 memcpy(&desc, &sd, sizeof(desc));
1206 info.entry_number = idx;
1207 info.base_addr = LINUX_GET_BASE(&desc);
1208 info.limit = LINUX_GET_LIMIT(&desc);
1209 info.seg_32bit = LINUX_GET_32BIT(&desc);
1210 info.contents = LINUX_GET_CONTENTS(&desc);
1211 info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
1212 info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
1213 info.seg_not_present = !LINUX_GET_PRESENT(&desc);
1214 info.useable = LINUX_GET_USEABLE(&desc);
1216 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1223 /* copied from kern/kern_time.c */
1225 linux_timer_create(struct thread *td, struct linux_timer_create_args *args)
1227 return ktimer_create(td, (struct ktimer_create_args *) args);
1231 linux_timer_settime(struct thread *td, struct linux_timer_settime_args *args)
1233 return ktimer_settime(td, (struct ktimer_settime_args *) args);
1237 linux_timer_gettime(struct thread *td, struct linux_timer_gettime_args *args)
1239 return ktimer_gettime(td, (struct ktimer_gettime_args *) args);
1243 linux_timer_getoverrun(struct thread *td, struct linux_timer_getoverrun_args *args)
1245 return ktimer_getoverrun(td, (struct ktimer_getoverrun_args *) args);
1249 linux_timer_delete(struct thread *td, struct linux_timer_delete_args *args)
1251 return ktimer_delete(td, (struct ktimer_delete_args *) args);
1254 /* XXX: this wont work with module - convert it */
1256 linux_mq_open(struct thread *td, struct linux_mq_open_args *args)
1258 #ifdef P1003_1B_MQUEUE
1259 return kmq_open(td, (struct kmq_open_args *) args);
1266 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args)
1268 #ifdef P1003_1B_MQUEUE
1269 return kmq_unlink(td, (struct kmq_unlink_args *) args);
1276 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args)
1278 #ifdef P1003_1B_MQUEUE
1279 return kmq_timedsend(td, (struct kmq_timedsend_args *) args);
1286 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args)
1288 #ifdef P1003_1B_MQUEUE
1289 return kmq_timedreceive(td, (struct kmq_timedreceive_args *) args);
1296 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args)
1298 #ifdef P1003_1B_MQUEUE
1299 return kmq_notify(td, (struct kmq_notify_args *) args);
1306 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args)
1308 #ifdef P1003_1B_MQUEUE
1309 return kmq_setattr(td, (struct kmq_setattr_args *) args);