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 $
29 * $DragonFly: src/sys/emulation/linux/i386/linux_machdep.c,v 1.18 2006/10/06 13:40:40 joerg Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/imgact.h>
35 #include <sys/kern_syscall.h>
38 #include <sys/nlookup.h>
40 #include <sys/resource.h>
41 #include <sys/resourcevar.h>
42 #include <sys/sysproto.h>
43 #include <sys/unistd.h>
45 #include <machine/frame.h>
46 #include <machine/psl.h>
47 #include <machine/segments.h>
48 #include <machine/sysarch.h>
52 #include <vm/vm_map.h>
55 #include "linux_proto.h"
56 #include "../linux_ipc.h"
57 #include "../linux_signal.h"
58 #include "../linux_util.h"
66 l_uint read_exec_only:1;
67 l_uint limit_in_pages:1;
68 l_uint seg_not_present:1;
72 struct l_old_select_argv {
77 struct l_timeval *timeout;
81 linux_to_bsd_sigaltstack(int lsa)
85 if (lsa & LINUX_SS_DISABLE)
87 if (lsa & LINUX_SS_ONSTACK)
93 bsd_to_linux_sigaltstack(int bsa)
98 lsa |= LINUX_SS_DISABLE;
100 lsa |= LINUX_SS_ONSTACK;
105 sys_linux_execve(struct linux_execve_args *args)
107 struct nlookupdata nd;
108 struct image_args exec_args;
112 error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
117 printf(ARGS(execve, "%s"), path);
119 error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW);
121 error = exec_copyin_args(&exec_args, path, PATH_SYSSPACE,
122 args->argp, args->envp);
125 error = kern_execve(&nd, &exec_args);
129 * The syscall result is returned in registers to the new program.
130 * Linux will register %edx as an atexit function and we must be
131 * sure to set it to 0. XXX
134 args->sysmsg_result64 = 0;
136 exec_free_args(&exec_args);
137 linux_free_path(&path);
141 struct l_ipc_kludge {
142 struct l_msgbuf *msgp;
147 sys_linux_ipc(struct linux_ipc_args *args)
151 switch (args->what & 0xFFFF) {
153 struct linux_semop_args a;
155 a.semid = args->arg1;
157 a.nsops = args->arg2;
158 a.sysmsg_lresult = 0;
159 error = linux_semop(&a);
160 args->sysmsg_lresult = a.sysmsg_lresult;
164 struct linux_semget_args a;
167 a.nsems = args->arg2;
168 a.semflg = args->arg3;
169 a.sysmsg_lresult = 0;
170 error = linux_semget(&a);
171 args->sysmsg_lresult = a.sysmsg_lresult;
175 struct linux_semctl_args a;
178 a.semid = args->arg1;
179 a.semnum = args->arg2;
181 a.sysmsg_lresult = 0;
182 error = copyin((caddr_t)args->ptr, &a.arg, sizeof(a.arg));
185 error = linux_semctl(&a);
186 args->sysmsg_lresult = a.sysmsg_lresult;
190 struct linux_msgsnd_args a;
192 a.msqid = args->arg1;
194 a.msgsz = args->arg2;
195 a.msgflg = args->arg3;
196 a.sysmsg_lresult = 0;
197 error = linux_msgsnd(&a);
198 args->sysmsg_lresult = a.sysmsg_lresult;
202 struct linux_msgrcv_args a;
204 a.msqid = args->arg1;
205 a.msgsz = args->arg2;
206 a.msgflg = args->arg3;
207 a.sysmsg_lresult = 0;
208 if ((args->what >> 16) == 0) {
209 struct l_ipc_kludge tmp;
212 if (args->ptr == NULL)
214 error = copyin((caddr_t)args->ptr, &tmp, sizeof(tmp));
218 a.msgtyp = tmp.msgtyp;
221 a.msgtyp = args->arg5;
223 error = linux_msgrcv(&a);
224 args->sysmsg_lresult = a.sysmsg_lresult;
228 struct linux_msgget_args a;
231 a.msgflg = args->arg2;
232 a.sysmsg_lresult = 0;
233 error = linux_msgget(&a);
234 args->sysmsg_lresult = a.sysmsg_lresult;
238 struct linux_msgctl_args a;
240 a.msqid = args->arg1;
243 a.sysmsg_lresult = 0;
244 error = linux_msgctl(&a);
245 args->sysmsg_lresult = a.sysmsg_lresult;
249 struct linux_shmat_args a;
251 a.shmid = args->arg1;
252 a.shmaddr = args->ptr;
253 a.shmflg = args->arg2;
254 a.raddr = (l_ulong *)args->arg3;
255 a.sysmsg_lresult = 0;
256 error = linux_shmat(&a);
257 args->sysmsg_lresult = a.sysmsg_lresult;
261 struct linux_shmdt_args a;
263 a.shmaddr = args->ptr;
264 a.sysmsg_lresult = 0;
265 error = linux_shmdt(&a);
266 args->sysmsg_lresult = a.sysmsg_lresult;
270 struct linux_shmget_args a;
274 a.shmflg = args->arg3;
275 a.sysmsg_lresult = 0;
276 error = linux_shmget(&a);
277 args->sysmsg_lresult = a.sysmsg_lresult;
281 struct linux_shmctl_args a;
283 a.shmid = args->arg1;
286 a.sysmsg_lresult = 0;
287 error = linux_shmctl(&a);
288 args->sysmsg_lresult = a.sysmsg_lresult;
299 sys_linux_old_select(struct linux_old_select_args *args)
301 struct l_old_select_argv linux_args;
302 struct linux_select_args newsel;
306 if (ldebug(old_select))
307 printf(ARGS(old_select, "%p"), args->ptr);
310 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
314 newsel.sysmsg_result = 0;
315 newsel.nfds = linux_args.nfds;
316 newsel.readfds = linux_args.readfds;
317 newsel.writefds = linux_args.writefds;
318 newsel.exceptfds = linux_args.exceptfds;
319 newsel.timeout = linux_args.timeout;
320 error = sys_linux_select(&newsel);
321 args->sysmsg_result = newsel.sysmsg_result;
326 sys_linux_fork(struct linux_fork_args *args)
332 printf(ARGS(fork, ""));
335 if ((error = sys_fork((struct fork_args *)args)) != 0)
338 if (args->sysmsg_result == 1)
339 args->sysmsg_result = 0;
344 sys_linux_exit_group(struct linux_exit_group_args *args)
346 struct exit_args newargs;
349 newargs.sysmsg_result = 0;
350 newargs.rval = args->rval;
351 error = sys_exit(&newargs);
352 args->sysmsg_result = newargs.sysmsg_result;
357 sys_linux_vfork(struct linux_vfork_args *args)
363 printf(ARGS(vfork, ""));
366 if ((error = sys_vfork((struct vfork_args *)args)) != 0)
368 /* Are we the child? */
369 if (args->sysmsg_result == 1)
370 args->sysmsg_result = 0;
374 #define CLONE_VM 0x100
375 #define CLONE_FS 0x200
376 #define CLONE_FILES 0x400
377 #define CLONE_SIGHAND 0x800
378 #define CLONE_PID 0x1000
381 sys_linux_clone(struct linux_clone_args *args)
383 int error, ff = RFPROC;
387 struct rfork_args rf_args;
391 printf(ARGS(clone, "flags %x, stack %x"),
392 (unsigned int)args->flags, (unsigned int)args->stack);
393 if (args->flags & CLONE_PID)
394 printf(LMSG("CLONE_PID not yet supported"));
401 exit_signal = args->flags & 0x000000ff;
402 if (exit_signal >= LINUX_NSIG)
405 if (exit_signal <= LINUX_SIGTBLSZ)
406 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
408 /* RFTHREAD probably not necessary here, but it shouldn't hurt */
411 if (args->flags & CLONE_VM)
413 if (args->flags & CLONE_SIGHAND)
415 if (!(args->flags & CLONE_FILES))
422 rf_args.sysmsg_result = 0;
423 if ((error = sys_rfork(&rf_args)) != 0)
425 args->sysmsg_result = rf_args.sysmsg_result;
427 p2 = pfind(rf_args.sysmsg_result);
431 p2->p_sigparent = exit_signal;
432 p2->p_md.md_regs->tf_esp = (unsigned int)args->stack;
436 printf(LMSG("clone: successful rfork to %ld"),
453 #define STACK_SIZE (2 * 1024 * 1024)
454 #define GUARD_SIZE (4 * PAGE_SIZE)
457 linux_mmap_common(caddr_t linux_addr, size_t linux_len, int linux_prot,
458 int linux_flags, int linux_fd, off_t pos, void **res)
460 struct thread *td = curthread;
461 struct proc *p = td->td_proc;
464 int error, flags, len, prot, fd;
467 if (linux_flags & LINUX_MAP_SHARED)
469 if (linux_flags & LINUX_MAP_PRIVATE)
470 flags |= MAP_PRIVATE;
471 if (linux_flags & LINUX_MAP_FIXED)
473 if (linux_flags & LINUX_MAP_ANON) {
478 if (linux_flags & LINUX_MAP_GROWSDOWN) {
480 /* The linux MAP_GROWSDOWN option does not limit auto
481 * growth of the region. Linux mmap with this option
482 * takes as addr the inital BOS, and as len, the initial
483 * region size. It can then grow down from addr without
484 * limit. However, linux threads has an implicit internal
485 * limit to stack size of STACK_SIZE. Its just not
486 * enforced explicitly in linux. But, here we impose
487 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
488 * region, since we can do this with our mmap.
490 * Our mmap with MAP_STACK takes addr as the maximum
491 * downsize limit on BOS, and as len the max size of
492 * the region. It them maps the top SGROWSIZ bytes,
493 * and autgrows the region down, up to the limit
496 * If we don't use the MAP_STACK option, the effect
497 * of this code is to allocate a stack region of a
498 * fixed size of (STACK_SIZE - GUARD_SIZE).
501 /* This gives us TOS */
502 addr = linux_addr + linux_len;
504 if (addr > p->p_vmspace->vm_maxsaddr) {
505 /* Some linux apps will attempt to mmap
506 * thread stacks near the top of their
507 * address space. If their TOS is greater
508 * than vm_maxsaddr, vm_map_growstack()
509 * will confuse the thread stack with the
510 * process stack and deliver a SEGV if they
511 * attempt to grow the thread stack past their
512 * current stacksize rlimit. To avoid this,
513 * adjust vm_maxsaddr upwards to reflect
514 * the current stacksize rlimit rather
515 * than the maximum possible stacksize.
516 * It would be better to adjust the
517 * mmap'ed region, but some apps do not check
518 * mmap's return value.
520 p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
521 p->p_rlimit[RLIMIT_STACK].rlim_cur;
524 /* This gives us our maximum stack size */
525 if (linux_len > STACK_SIZE - GUARD_SIZE) {
528 len = STACK_SIZE - GUARD_SIZE;
530 /* This gives us a new BOS. If we're using VM_STACK, then
531 * mmap will just map the top SGROWSIZ bytes, and let
532 * the stack grow down to the limit at BOS. If we're
533 * not using VM_STACK we map the full stack, since we
534 * don't have a way to autogrow it.
542 prot = linux_prot | PROT_READ;
543 if (linux_flags & LINUX_MAP_ANON) {
550 if (ldebug(mmap) || ldebug(mmap2))
551 printf("-> (%p, %d, %d, 0x%08x, %d, %lld)\n",
552 addr, len, prot, flags, fd, pos);
554 error = kern_mmap(curproc->p_vmspace, addr, len,
555 prot, flags, fd, pos, &new);
563 sys_linux_mmap(struct linux_mmap_args *args)
565 struct l_mmap_argv linux_args;
568 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
574 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
575 (void *)linux_args.addr, linux_args.len, linux_args.prot,
576 linux_args.flags, linux_args.fd, linux_args.pos);
578 error = linux_mmap_common(linux_args.addr, linux_args.len,
579 linux_args.prot, linux_args.flags, linux_args.fd,
580 linux_args.pos, &args->sysmsg_resultp);
583 printf("-> %p\n", args->sysmsg_resultp);
589 sys_linux_mmap2(struct linux_mmap2_args *args)
595 printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
596 (void *)args->addr, args->len, args->prot, args->flags,
597 args->fd, args->pgoff);
599 error = linux_mmap_common((void *)args->addr, args->len, args->prot,
600 args->flags, args->fd, args->pgoff * PAGE_SIZE,
601 &args->sysmsg_resultp);
604 printf("-> %p\n", args->sysmsg_resultp);
610 sys_linux_pipe(struct linux_pipe_args *args)
614 struct pipe_args bsd_args;
618 printf(ARGS(pipe, "*"));
621 reg_edx = args->sysmsg_fds[1];
622 error = sys_pipe(&bsd_args);
624 args->sysmsg_fds[1] = reg_edx;
628 error = copyout(bsd_args.sysmsg_fds, args->pipefds, 2*sizeof(int));
630 args->sysmsg_fds[1] = reg_edx;
634 args->sysmsg_fds[1] = reg_edx;
635 args->sysmsg_fds[0] = 0;
640 sys_linux_ioperm(struct linux_ioperm_args *args)
642 struct sysarch_args sa;
643 struct i386_ioperm_args *iia;
647 sg = stackgap_init();
648 iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
649 iia->start = args->start;
650 iia->length = args->length;
651 iia->enable = args->enable;
652 sa.sysmsg_resultp = NULL;
653 sa.op = I386_SET_IOPERM;
654 sa.parms = (char *)iia;
655 error = sys_sysarch(&sa);
656 args->sysmsg_resultp = sa.sysmsg_resultp;
661 sys_linux_iopl(struct linux_iopl_args *args)
663 struct thread *td = curthread;
664 struct proc *p = td->td_proc;
669 if (args->level < 0 || args->level > 3)
671 if ((error = suser(td)) != 0)
675 p->p_md.md_regs->tf_eflags = (p->p_md.md_regs->tf_eflags & ~PSL_IOPL) |
676 (args->level * (PSL_IOPL / 3));
681 sys_linux_modify_ldt(struct linux_modify_ldt_args *uap)
685 struct sysarch_args args;
686 struct i386_ldt_args *ldt;
687 struct l_descriptor ld;
688 union descriptor *desc;
690 sg = stackgap_init();
692 if (uap->ptr == NULL)
696 case 0x00: /* read_ldt */
697 ldt = stackgap_alloc(&sg, sizeof(*ldt));
699 ldt->descs = uap->ptr;
700 ldt->num = uap->bytecount / sizeof(union descriptor);
701 args.op = I386_GET_LDT;
702 args.parms = (char*)ldt;
703 args.sysmsg_result = 0;
704 error = sys_sysarch(&args);
705 uap->sysmsg_result = args.sysmsg_result *
706 sizeof(union descriptor);
708 case 0x01: /* write_ldt */
709 case 0x11: /* write_ldt */
710 if (uap->bytecount != sizeof(ld))
713 error = copyin(uap->ptr, &ld, sizeof(ld));
717 ldt = stackgap_alloc(&sg, sizeof(*ldt));
718 desc = stackgap_alloc(&sg, sizeof(*desc));
719 ldt->start = ld.entry_number;
722 desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
723 desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
724 desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
725 desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
726 desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
729 desc->sd.sd_p = (ld.seg_not_present ^ 1);
731 desc->sd.sd_def32 = ld.seg_32bit;
732 desc->sd.sd_gran = ld.limit_in_pages;
733 args.op = I386_SET_LDT;
734 args.parms = (char*)ldt;
735 args.sysmsg_result = 0;
736 error = sys_sysarch(&args);
737 uap->sysmsg_result = args.sysmsg_result;
748 sys_linux_sigaction(struct linux_sigaction_args *args)
751 l_sigaction_t linux_act, linux_oact;
752 struct sigaction act, oact;
756 if (ldebug(sigaction))
757 printf(ARGS(sigaction, "%d, %p, %p"),
758 args->sig, (void *)args->nsa, (void *)args->osa);
762 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
765 linux_act.lsa_handler = osa.lsa_handler;
766 linux_act.lsa_flags = osa.lsa_flags;
767 linux_act.lsa_restorer = osa.lsa_restorer;
768 LINUX_SIGEMPTYSET(linux_act.lsa_mask);
769 linux_act.lsa_mask.__bits[0] = osa.lsa_mask;
770 linux_to_bsd_sigaction(&linux_act, &act);
773 error = kern_sigaction(args->sig, args->nsa ? &act : NULL,
774 args->osa ? &oact : NULL);
776 if (args->osa != NULL && !error) {
777 bsd_to_linux_sigaction(&oact, &linux_oact);
778 osa.lsa_handler = linux_oact.lsa_handler;
779 osa.lsa_flags = linux_oact.lsa_flags;
780 osa.lsa_restorer = linux_oact.lsa_restorer;
781 osa.lsa_mask = linux_oact.lsa_mask.__bits[0];
782 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
788 * Linux has two extra args, restart and oldmask. We dont use these,
789 * but it seems that "restart" is actually a context pointer that
790 * enables the signal to happen with a different register set.
793 sys_linux_sigsuspend(struct linux_sigsuspend_args *args)
795 l_sigset_t linux_mask;
800 if (ldebug(sigsuspend))
801 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
804 LINUX_SIGEMPTYSET(mask);
805 mask.__bits[0] = args->mask;
806 linux_to_bsd_sigset(&linux_mask, &mask);
808 error = kern_sigsuspend(&mask);
814 sys_linux_rt_sigsuspend(struct linux_rt_sigsuspend_args *uap)
816 l_sigset_t linux_mask;
821 if (ldebug(rt_sigsuspend))
822 printf(ARGS(rt_sigsuspend, "%p, %d"),
823 (void *)uap->newset, uap->sigsetsize);
826 if (uap->sigsetsize != sizeof(l_sigset_t))
829 error = copyin(uap->newset, &linux_mask, sizeof(l_sigset_t));
833 linux_to_bsd_sigset(&linux_mask, &mask);
835 error = kern_sigsuspend(&mask);
841 sys_linux_pause(struct linux_pause_args *args)
843 struct thread *td = curthread;
844 struct proc *p = td->td_proc;
850 printf(ARGS(pause, ""));
855 error = kern_sigsuspend(&mask);
861 sys_linux_sigaltstack(struct linux_sigaltstack_args *uap)
868 if (ldebug(sigaltstack))
869 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
873 error = copyin(uap->uss, &linux_ss, sizeof(l_stack_t));
877 ss.ss_sp = linux_ss.ss_sp;
878 ss.ss_size = linux_ss.ss_size;
879 ss.ss_flags = linux_to_bsd_sigaltstack(linux_ss.ss_flags);
882 error = kern_sigaltstack(uap->uss ? &ss : NULL,
883 uap->uoss ? &oss : NULL);
885 if (error == 0 && uap->uoss) {
886 linux_ss.ss_sp = oss.ss_sp;
887 linux_ss.ss_size = oss.ss_size;
888 linux_ss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
889 error = copyout(&linux_ss, uap->uoss, sizeof(l_stack_t));