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.
28 * $FreeBSD: src/sys/i386/linux/linux_machdep.c,v 1.6.2.4 2001/11/05 19:08:23 marcel Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/imgact.h>
34 #include <sys/kern_syscall.h>
37 #include <sys/nlookup.h>
40 #include <sys/resource.h>
41 #include <sys/resourcevar.h>
42 #include <sys/ptrace.h>
43 #include <sys/sysproto.h>
44 #include <sys/thread2.h>
45 #include <sys/unistd.h>
48 #include <machine/frame.h>
49 #include <machine/psl.h>
50 #include <machine/segments.h>
51 #include <machine/sysarch.h>
55 #include <vm/vm_map.h>
57 #include <sys/mplock2.h>
60 #include "linux_proto.h"
61 #include "../linux_ipc.h"
62 #include "../linux_signal.h"
63 #include "../linux_util.h"
64 #include "../linux_emuldata.h"
72 l_uint read_exec_only:1;
73 l_uint limit_in_pages:1;
74 l_uint seg_not_present:1;
78 struct l_old_select_argv {
83 struct l_timeval *timeout;
87 linux_to_bsd_sigaltstack(int lsa)
91 if (lsa & LINUX_SS_DISABLE)
93 if (lsa & LINUX_SS_ONSTACK)
99 bsd_to_linux_sigaltstack(int bsa)
103 if (bsa & SS_DISABLE)
104 lsa |= LINUX_SS_DISABLE;
105 if (bsa & SS_ONSTACK)
106 lsa |= LINUX_SS_ONSTACK;
114 sys_linux_execve(struct linux_execve_args *args)
116 struct nlookupdata nd;
117 struct image_args exec_args;
121 error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
126 kprintf(ARGS(execve, "%s"), path);
129 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
130 bzero(&exec_args, sizeof(exec_args));
132 error = exec_copyin_args(&exec_args, path, PATH_SYSSPACE,
133 args->argp, args->envp);
136 error = kern_execve(&nd, &exec_args);
140 * The syscall result is returned in registers to the new program.
141 * Linux will register %edx as an atexit function and we must be
142 * sure to set it to 0. XXX
145 args->sysmsg_result64 = 0;
146 if (curproc->p_sysent == &elf_linux_sysvec)
147 error = emuldata_init(curproc, NULL, 0);
150 exec_free_args(&exec_args);
151 linux_free_path(&path);
154 /* We hit a lethal error condition. Let's die now. */
155 exit1(W_EXITCODE(0, SIGABRT));
163 struct l_ipc_kludge {
164 struct l_msgbuf *msgp;
172 sys_linux_ipc(struct linux_ipc_args *args)
178 switch (args->what & 0xFFFF) {
180 struct linux_semop_args a;
182 a.semid = args->arg1;
184 a.nsops = args->arg2;
185 a.sysmsg_lresult = 0;
186 error = linux_semop(&a);
187 args->sysmsg_lresult = a.sysmsg_lresult;
191 struct linux_semget_args a;
194 a.nsems = args->arg2;
195 a.semflg = args->arg3;
196 a.sysmsg_lresult = 0;
197 error = linux_semget(&a);
198 args->sysmsg_lresult = a.sysmsg_lresult;
202 struct linux_semctl_args a;
205 a.semid = args->arg1;
206 a.semnum = args->arg2;
208 a.sysmsg_lresult = 0;
209 error = copyin((caddr_t)args->ptr, &a.arg, sizeof(a.arg));
212 error = linux_semctl(&a);
213 args->sysmsg_lresult = a.sysmsg_lresult;
217 struct linux_msgsnd_args a;
219 a.msqid = args->arg1;
221 a.msgsz = args->arg2;
222 a.msgflg = args->arg3;
223 a.sysmsg_lresult = 0;
224 error = linux_msgsnd(&a);
225 args->sysmsg_lresult = a.sysmsg_lresult;
229 struct linux_msgrcv_args a;
231 a.msqid = args->arg1;
232 a.msgsz = args->arg2;
237 a.msgflg = args->arg3;
238 a.sysmsg_lresult = 0;
239 if ((args->what >> 16) == 0) {
240 struct l_ipc_kludge tmp;
243 if (args->ptr == NULL) {
247 error = copyin((caddr_t)args->ptr, &tmp, sizeof(tmp));
251 a.msgtyp = tmp.msgtyp;
254 a.msgtyp = args->arg5;
256 error = linux_msgrcv(&a);
257 args->sysmsg_lresult = a.sysmsg_lresult;
261 struct linux_msgget_args a;
264 a.msgflg = args->arg2;
265 a.sysmsg_lresult = 0;
266 error = linux_msgget(&a);
267 args->sysmsg_lresult = a.sysmsg_lresult;
271 struct linux_msgctl_args a;
273 a.msqid = args->arg1;
276 a.sysmsg_lresult = 0;
277 error = linux_msgctl(&a);
278 args->sysmsg_lresult = a.sysmsg_lresult;
282 struct linux_shmat_args a;
284 a.shmid = args->arg1;
285 a.shmaddr = args->ptr;
286 a.shmflg = args->arg2;
287 a.raddr = (l_ulong *)args->arg3;
288 a.sysmsg_lresult = 0;
289 error = linux_shmat(&a);
290 args->sysmsg_lresult = a.sysmsg_lresult;
294 struct linux_shmdt_args a;
296 a.shmaddr = args->ptr;
297 a.sysmsg_lresult = 0;
298 error = linux_shmdt(&a);
299 args->sysmsg_lresult = a.sysmsg_lresult;
303 struct linux_shmget_args a;
307 a.shmflg = args->arg3;
308 a.sysmsg_lresult = 0;
309 error = linux_shmget(&a);
310 args->sysmsg_lresult = a.sysmsg_lresult;
314 struct linux_shmctl_args a;
316 a.shmid = args->arg1;
319 a.sysmsg_lresult = 0;
320 error = linux_shmctl(&a);
321 args->sysmsg_lresult = a.sysmsg_lresult;
336 sys_linux_old_select(struct linux_old_select_args *args)
338 struct l_old_select_argv linux_args;
339 struct linux_select_args newsel;
343 if (ldebug(old_select))
344 kprintf(ARGS(old_select, "%p"), args->ptr);
347 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
351 newsel.sysmsg_iresult = 0;
352 newsel.nfds = linux_args.nfds;
353 newsel.readfds = linux_args.readfds;
354 newsel.writefds = linux_args.writefds;
355 newsel.exceptfds = linux_args.exceptfds;
356 newsel.timeout = linux_args.timeout;
357 error = sys_linux_select(&newsel);
358 args->sysmsg_iresult = newsel.sysmsg_iresult;
366 sys_linux_fork(struct linux_fork_args *args)
368 struct lwp *lp = curthread->td_lwp;
373 error = fork1(lp, RFFDG | RFPROC | RFPGLOCK, &p2);
375 emuldata_init(curproc, p2, 0);
377 start_forked_proc(lp, p2);
378 args->sysmsg_fds[0] = p2->p_pid;
379 args->sysmsg_fds[1] = 0;
383 /* Are we the child? */
384 if (args->sysmsg_iresult == 1)
385 args->sysmsg_iresult = 0;
394 sys_linux_exit_group(struct linux_exit_group_args *args)
396 struct linux_emuldata *em, *e;
402 em = emuldata_get(curproc);
404 if (em->s->refs == 1) {
406 exit1(W_EXITCODE(rval, 0));
410 KKASSERT(em->proc == curproc);
411 em->flags |= EMUL_DIDKILL;
412 em->s->flags |= LINUX_LES_INEXITGROUP;
413 em->s->xstat = W_EXITCODE(rval, 0);
415 LIST_REMOVE(em, threads);
416 LIST_INSERT_HEAD(&em->s->threads, em, threads);
418 while ((e = LIST_NEXT(em, threads)) != NULL) {
419 LIST_REMOVE(em, threads);
420 LIST_INSERT_AFTER(e, em, threads);
421 if ((e->flags & EMUL_DIDKILL) == 0) {
422 e->flags |= EMUL_DIDKILL;
423 lwkt_gettoken(&proc_token);
424 KKASSERT(pfindn(e->proc->p_pid) == e->proc);
425 ksignal(e->proc, SIGKILL);
426 lwkt_reltoken(&proc_token);
431 exit1(W_EXITCODE(rval, 0));
441 sys_linux_vfork(struct linux_vfork_args *args)
443 struct lwp *lp = curthread->td_lwp;
448 error = fork1(lp, RFFDG | RFPROC | RFPPWAIT | RFMEM | RFPGLOCK, &p2);
450 emuldata_init(curproc, p2, 0);
452 start_forked_proc(lp, p2);
453 args->sysmsg_fds[0] = p2->p_pid;
454 args->sysmsg_fds[1] = 0;
458 if (args->sysmsg_iresult == 1)
459 args->sysmsg_iresult = 0;
468 sys_linux_clone(struct linux_clone_args *args)
470 struct segment_descriptor *desc;
471 struct l_user_desc info;
475 struct lwp *lp = curthread->td_lwp;
476 int error, ff = RFPROC;
477 struct proc *p2 = NULL;
480 exit_signal = args->flags & 0x000000ff;
481 if (exit_signal >= LINUX_NSIG)
483 if (exit_signal <= LINUX_SIGTBLSZ)
484 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
486 if (args->flags & LINUX_CLONE_VM)
488 if (args->flags & LINUX_CLONE_SIGHAND)
490 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
492 if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
494 if (args->flags & LINUX_CLONE_VFORK)
496 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
497 if (args->parent_tidptr == NULL)
504 error = fork1(lp, ff | RFPGLOCK, &p2);
510 args->sysmsg_fds[0] = p2 ? p2->p_pid : 0;
511 args->sysmsg_fds[1] = 0;
513 if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) {
514 lwkt_gettoken(&curproc->p_token);
515 while (p2->p_pptr != curproc->p_pptr)
516 proc_reparent(p2, curproc->p_pptr);
517 lwkt_reltoken(&curproc->p_token);
520 emuldata_init(curproc, p2, args->flags);
521 linux_proc_fork(p2, curproc, args->child_tidptr);
523 * XXX: this can't happen, p2 is never NULL, or else we'd have
524 * other problems, too (see p2->p_sigparent == ...,
525 * linux_proc_fork and emuldata_init.
530 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
531 error = copyout(&p2->p_pid, args->parent_tidptr, sizeof(p2->p_pid));
535 p2->p_sigparent = exit_signal;
537 ONLY_LWP_IN_PROC(p2)->lwp_md.md_regs->tf_esp =
538 (unsigned long)args->stack;
541 if (args->flags & LINUX_CLONE_SETTLS) {
542 error = copyin((void *)curthread->td_lwp->lwp_md.md_regs->tf_esi, &info, sizeof(struct l_user_desc));
544 kprintf("copyin of tf_esi to info failed\n");
546 idx = info.entry_number;
548 * We understand both our own entries such as the ones
549 * we provide on linux_set_thread_area, as well as the
550 * linux-type entries 6-8.
552 if ((idx < 6 || idx > 8) && (idx < GTLS_START)) {
553 kprintf("LINUX_CLONE_SETTLS, invalid idx requested: %d\n", idx);
556 if (idx < GTLS_START) {
560 idx -= (GTLS_START + mycpu->gd_cpuid * NGDT);
566 a[0] = LINUX_LDT_entry_a(&info);
567 a[1] = LINUX_LDT_entry_b(&info);
569 desc = &FIRST_LWP_IN_PROC(p2)->lwp_thread->td_tls.tls[idx];
570 memcpy(desc, &a, sizeof(a));
572 kprintf("linux_clone... we don't have a p2\n");
578 start_forked_proc(lp, p2);
583 kprintf(LMSG("clone: successful rfork to %ld"),
600 #define STACK_SIZE (2 * 1024 * 1024)
601 #define GUARD_SIZE (4 * PAGE_SIZE)
607 linux_mmap_common(caddr_t linux_addr, size_t linux_len, int linux_prot,
608 int linux_flags, int linux_fd, off_t pos, void **res)
610 struct thread *td = curthread;
611 struct proc *p = td->td_proc;
614 int error, flags, len, prot, fd;
617 if (linux_flags & LINUX_MAP_SHARED)
619 if (linux_flags & LINUX_MAP_PRIVATE)
620 flags |= MAP_PRIVATE;
621 if (linux_flags & LINUX_MAP_FIXED)
623 if (linux_flags & LINUX_MAP_ANON) {
629 lwkt_gettoken(&curproc->p_vmspace->vm_map.token);
631 if (linux_flags & LINUX_MAP_GROWSDOWN) {
633 /* The linux MAP_GROWSDOWN option does not limit auto
634 * growth of the region. Linux mmap with this option
635 * takes as addr the inital BOS, and as len, the initial
636 * region size. It can then grow down from addr without
637 * limit. However, linux threads has an implicit internal
638 * limit to stack size of STACK_SIZE. Its just not
639 * enforced explicitly in linux. But, here we impose
640 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
641 * region, since we can do this with our mmap.
643 * Our mmap with MAP_STACK takes addr as the maximum
644 * downsize limit on BOS, and as len the max size of
645 * the region. It them maps the top SGROWSIZ bytes,
646 * and autgrows the region down, up to the limit
649 * If we don't use the MAP_STACK option, the effect
650 * of this code is to allocate a stack region of a
651 * fixed size of (STACK_SIZE - GUARD_SIZE).
654 /* This gives us TOS */
655 addr = linux_addr + linux_len;
657 if (addr > p->p_vmspace->vm_maxsaddr) {
658 /* Some linux apps will attempt to mmap
659 * thread stacks near the top of their
660 * address space. If their TOS is greater
661 * than vm_maxsaddr, vm_map_growstack()
662 * will confuse the thread stack with the
663 * process stack and deliver a SEGV if they
664 * attempt to grow the thread stack past their
665 * current stacksize rlimit. To avoid this,
666 * adjust vm_maxsaddr upwards to reflect
667 * the current stacksize rlimit rather
668 * than the maximum possible stacksize.
669 * It would be better to adjust the
670 * mmap'ed region, but some apps do not check
671 * mmap's return value.
673 p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
674 p->p_rlimit[RLIMIT_STACK].rlim_cur;
677 /* This gives us our maximum stack size */
678 if (linux_len > STACK_SIZE - GUARD_SIZE) {
681 len = STACK_SIZE - GUARD_SIZE;
683 /* This gives us a new BOS. If we're using VM_STACK, then
684 * mmap will just map the top SGROWSIZ bytes, and let
685 * the stack grow down to the limit at BOS. If we're
686 * not using VM_STACK we map the full stack, since we
687 * don't have a way to autogrow it.
697 if (prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
698 prot |= PROT_READ | PROT_EXEC;
700 if (linux_flags & LINUX_MAP_ANON) {
707 if (ldebug(mmap) || ldebug(mmap2))
708 kprintf("-> (%p, %d, %d, 0x%08x, %d, %lld)\n",
709 addr, len, prot, flags, fd, pos);
711 error = kern_mmap(curproc->p_vmspace, addr, len,
712 prot, flags, fd, pos, &new);
714 lwkt_reltoken(&curproc->p_vmspace->vm_map.token);
725 sys_linux_mmap(struct linux_mmap_args *args)
727 struct l_mmap_argv linux_args;
730 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
736 kprintf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
737 (void *)linux_args.addr, linux_args.len, linux_args.prot,
738 linux_args.flags, linux_args.fd, linux_args.pos);
740 error = linux_mmap_common(linux_args.addr, linux_args.len,
741 linux_args.prot, linux_args.flags, linux_args.fd,
742 linux_args.pos, &args->sysmsg_resultp);
745 kprintf("-> %p\n", args->sysmsg_resultp);
754 sys_linux_mmap2(struct linux_mmap2_args *args)
760 kprintf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
761 (void *)args->addr, args->len, args->prot, args->flags,
762 args->fd, args->pgoff);
764 error = linux_mmap_common((void *)args->addr, args->len, args->prot,
765 args->flags, args->fd, args->pgoff * PAGE_SIZE,
766 &args->sysmsg_resultp);
769 kprintf("-> %p\n", args->sysmsg_resultp);
778 sys_linux_pipe(struct linux_pipe_args *args)
782 struct pipe_args bsd_args;
786 kprintf(ARGS(pipe, "*"));
789 reg_edx = args->sysmsg_fds[1];
790 error = sys_pipe(&bsd_args);
792 args->sysmsg_fds[1] = reg_edx;
796 error = copyout(bsd_args.sysmsg_fds, args->pipefds, 2*sizeof(int));
798 args->sysmsg_fds[1] = reg_edx;
802 args->sysmsg_fds[1] = reg_edx;
803 args->sysmsg_fds[0] = 0;
811 sys_linux_pipe2(struct linux_pipe2_args *args)
813 struct thread *td = curthread;
816 struct pipe_args bsd_args;
819 reg_edx = args->sysmsg_fds[1];
820 error = sys_pipe(&bsd_args);
822 args->sysmsg_fds[1] = reg_edx;
826 // if (args->flags & LINUX_O_CLOEXEC) {
829 if (args->flags & LINUX_O_NONBLOCK) {
830 dat.fc_flags = O_NONBLOCK;
831 kern_fcntl(bsd_args.sysmsg_fds[0], F_SETFL, &dat, td->td_ucred);
832 kern_fcntl(bsd_args.sysmsg_fds[1], F_SETFL, &dat, td->td_ucred);
835 error = copyout(bsd_args.sysmsg_fds, args->pipefds, 2*sizeof(int));
837 args->sysmsg_fds[1] = reg_edx;
841 args->sysmsg_fds[1] = reg_edx;
842 args->sysmsg_fds[0] = 0;
850 sys_linux_ioperm(struct linux_ioperm_args *args)
852 struct sysarch_args sa;
853 struct i386_ioperm_args *iia;
857 sg = stackgap_init();
858 iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
859 iia->start = args->start;
860 iia->length = args->length;
861 iia->enable = args->enable;
862 sa.sysmsg_resultp = NULL;
863 sa.op = I386_SET_IOPERM;
864 sa.parms = (char *)iia;
865 error = sys_sysarch(&sa);
866 args->sysmsg_resultp = sa.sysmsg_resultp;
874 sys_linux_iopl(struct linux_iopl_args *args)
876 struct thread *td = curthread;
877 struct lwp *lp = td->td_lwp;
880 if (args->level < 0 || args->level > 3)
882 if ((error = priv_check(td, PRIV_ROOT)) != 0)
886 lp->lwp_md.md_regs->tf_eflags =
887 (lp->lwp_md.md_regs->tf_eflags & ~PSL_IOPL) |
888 (args->level * (PSL_IOPL / 3));
896 sys_linux_modify_ldt(struct linux_modify_ldt_args *uap)
900 struct sysarch_args args;
901 struct i386_ldt_args *ldt;
902 struct l_descriptor ld;
903 union descriptor *desc;
906 sg = stackgap_init();
908 if (uap->ptr == NULL)
912 case 0x00: /* read_ldt */
913 ldt = stackgap_alloc(&sg, sizeof(*ldt));
915 ldt->descs = uap->ptr;
916 ldt->num = uap->bytecount / sizeof(union descriptor);
917 args.op = I386_GET_LDT;
918 args.parms = (char*)ldt;
919 args.sysmsg_iresult = 0;
920 error = sys_sysarch(&args);
921 uap->sysmsg_iresult = args.sysmsg_iresult *
922 sizeof(union descriptor);
924 case 0x02: /* read_default_ldt = 0 */
925 size = 5*sizeof(struct l_desc_struct);
926 if (size > uap->bytecount)
927 size = uap->bytecount;
928 for (written = error = 0; written < size && error == 0; written++)
929 error = subyte((char *)uap->ptr + written, 0);
930 uap->sysmsg_iresult = written;
932 case 0x01: /* write_ldt */
933 case 0x11: /* write_ldt */
934 if (uap->bytecount != sizeof(ld))
937 error = copyin(uap->ptr, &ld, sizeof(ld));
941 ldt = stackgap_alloc(&sg, sizeof(*ldt));
942 desc = stackgap_alloc(&sg, sizeof(*desc));
943 ldt->start = ld.entry_number;
946 desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
947 desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
948 desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
949 desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
950 desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
953 desc->sd.sd_p = (ld.seg_not_present ^ 1);
955 desc->sd.sd_def32 = ld.seg_32bit;
956 desc->sd.sd_gran = ld.limit_in_pages;
957 args.op = I386_SET_LDT;
958 args.parms = (char*)ldt;
959 args.sysmsg_iresult = 0;
960 error = sys_sysarch(&args);
961 uap->sysmsg_iresult = args.sysmsg_iresult;
975 sys_linux_sigaction(struct linux_sigaction_args *args)
978 l_sigaction_t linux_act, linux_oact;
979 struct sigaction act, oact;
983 if (ldebug(sigaction))
984 kprintf(ARGS(sigaction, "%d, %p, %p"),
985 args->sig, (void *)args->nsa, (void *)args->osa);
989 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
992 linux_act.lsa_handler = osa.lsa_handler;
993 linux_act.lsa_flags = osa.lsa_flags;
994 linux_act.lsa_restorer = osa.lsa_restorer;
995 LINUX_SIGEMPTYSET(linux_act.lsa_mask);
996 linux_act.lsa_mask.__bits[0] = osa.lsa_mask;
997 linux_to_bsd_sigaction(&linux_act, &act);
1000 if (args->sig <= LINUX_SIGTBLSZ)
1001 sig = linux_to_bsd_signal[_SIG_IDX(args->sig)];
1006 error = kern_sigaction(sig, args->nsa ? &act : NULL,
1007 args->osa ? &oact : NULL);
1010 if (args->osa != NULL && !error) {
1011 bsd_to_linux_sigaction(&oact, &linux_oact);
1012 osa.lsa_handler = linux_oact.lsa_handler;
1013 osa.lsa_flags = linux_oact.lsa_flags;
1014 osa.lsa_restorer = linux_oact.lsa_restorer;
1015 osa.lsa_mask = linux_oact.lsa_mask.__bits[0];
1016 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
1022 * Linux has two extra args, restart and oldmask. We dont use these,
1023 * but it seems that "restart" is actually a context pointer that
1024 * enables the signal to happen with a different register set.
1029 sys_linux_sigsuspend(struct linux_sigsuspend_args *args)
1031 l_sigset_t linux_mask;
1036 if (ldebug(sigsuspend))
1037 kprintf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
1040 LINUX_SIGEMPTYSET(mask);
1041 mask.__bits[0] = args->mask;
1042 linux_to_bsd_sigset(&linux_mask, &mask);
1045 error = kern_sigsuspend(&mask);
1055 sys_linux_rt_sigsuspend(struct linux_rt_sigsuspend_args *uap)
1057 l_sigset_t linux_mask;
1062 if (ldebug(rt_sigsuspend))
1063 kprintf(ARGS(rt_sigsuspend, "%p, %d"),
1064 (void *)uap->newset, uap->sigsetsize);
1067 if (uap->sigsetsize != sizeof(l_sigset_t))
1070 error = copyin(uap->newset, &linux_mask, sizeof(l_sigset_t));
1074 linux_to_bsd_sigset(&linux_mask, &mask);
1077 error = kern_sigsuspend(&mask);
1087 sys_linux_pause(struct linux_pause_args *args)
1089 struct thread *td = curthread;
1090 struct lwp *lp = td->td_lwp;
1096 kprintf(ARGS(pause, ""));
1099 mask = lp->lwp_sigmask;
1102 error = kern_sigsuspend(&mask);
1112 sys_linux_sigaltstack(struct linux_sigaltstack_args *uap)
1119 if (ldebug(sigaltstack))
1120 kprintf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
1124 error = copyin(uap->uss, &linux_ss, sizeof(l_stack_t));
1128 ss.ss_sp = linux_ss.ss_sp;
1129 ss.ss_size = linux_ss.ss_size;
1130 ss.ss_flags = linux_to_bsd_sigaltstack(linux_ss.ss_flags);
1134 error = kern_sigaltstack(uap->uss ? &ss : NULL,
1135 uap->uoss ? &oss : NULL);
1138 if (error == 0 && uap->uoss) {
1139 linux_ss.ss_sp = oss.ss_sp;
1140 linux_ss.ss_size = oss.ss_size;
1141 linux_ss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
1142 error = copyout(&linux_ss, uap->uoss, sizeof(l_stack_t));
1149 sys_linux_set_thread_area(struct linux_set_thread_area_args *args)
1151 struct segment_descriptor *desc;
1152 struct l_user_desc info;
1158 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1163 if (ldebug(set_thread_area))
1164 kprintf(ARGS(set_thread_area, "%i, %x, %x, %i, %i, %i, %i, %i, %i\n"),
1170 info.read_exec_only,
1171 info.limit_in_pages,
1172 info.seg_not_present,
1176 idx = info.entry_number;
1177 if (idx != -1 && (idx < 6 || idx > 8))
1181 /* -1 means finding the first free TLS entry */
1182 for (i = 0; i < NGTLS; i++) {
1184 * try to determine if the TLS entry is empty by looking
1185 * at the lolimit entry.
1187 if (curthread->td_tls.tls[idx].sd_lolimit == 0) {
1195 * By now we should have an index. If not, it means
1196 * that no entry is free, so return ESRCH.
1201 /* translate the index from Linux to ours */
1206 /* Tell the caller about the allocated entry number */
1208 info.entry_number = GTLS_START + mycpu->gd_cpuid * NGDT + idx;
1210 info.entry_number = GTLS_START + idx;
1213 error = copyout(&info, args->desc, sizeof(struct l_user_desc));
1217 if (LINUX_LDT_empty(&info)) {
1221 a[0] = LINUX_LDT_entry_a(&info);
1222 a[1] = LINUX_LDT_entry_b(&info);
1226 * Update the TLS and the TLS entries in the GDT, but hold a critical
1227 * section as required by set_user_TLS().
1230 desc = &curthread->td_tls.tls[idx];
1231 memcpy(desc, &a, sizeof(a));
1239 sys_linux_get_thread_area(struct linux_get_thread_area_args *args)
1241 struct segment_descriptor *sd;
1242 struct l_desc_struct desc;
1243 struct l_user_desc info;
1248 if (ldebug(get_thread_area))
1249 kprintf(ARGS(get_thread_area, "%p"), args->desc);
1252 error = copyin(args->desc, &info, sizeof(struct l_user_desc));
1256 idx = info.entry_number;
1257 if ((idx < 6 || idx > 8) && (idx < GTLS_START)) {
1258 kprintf("sys_linux_get_thread_area, invalid idx requested: %d\n", idx);
1262 memset(&info, 0, sizeof(info));
1264 /* translate the index from Linux to ours */
1265 info.entry_number = idx;
1266 if (idx < GTLS_START) {
1270 idx -= (GTLS_START + mycpu->gd_cpuid * NGDT);
1277 sd = &curthread->td_tls.tls[idx];
1278 memcpy(&desc, sd, sizeof(desc));
1279 info.base_addr = LINUX_GET_BASE(&desc);
1280 info.limit = LINUX_GET_LIMIT(&desc);
1281 info.seg_32bit = LINUX_GET_32BIT(&desc);
1282 info.contents = LINUX_GET_CONTENTS(&desc);
1283 info.read_exec_only = !LINUX_GET_WRITABLE(&desc);
1284 info.limit_in_pages = LINUX_GET_LIMIT_PAGES(&desc);
1285 info.seg_not_present = !LINUX_GET_PRESENT(&desc);
1286 info.useable = LINUX_GET_USEABLE(&desc);
1288 error = copyout(&info, args->desc, sizeof(struct l_user_desc));