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.14 2003/12/20 05:52:21 dillon 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/namei.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 linux_execve(struct linux_execve_args *args)
107 struct thread *td = curthread;
109 struct image_args exec_args;
113 error = linux_copyin_path(args->path, &path, LINUX_PATH_EXISTS);
118 printf(ARGS(execve, "%s"), path);
120 NDINIT(&nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_FOLLOW | CNP_SAVENAME,
121 UIO_SYSSPACE, path, td);
122 error = exec_copyin_args(&exec_args, path, PATH_SYSSPACE,
123 args->argp, args->envp);
125 error = kern_execve(&nd, &exec_args);
128 * The syscall result is returned in registers to the new program.
129 * Linux will register %edx as an atexit function and we must be
130 * sure to set it to 0. XXX
133 args->sysmsg_result64 = 0;
135 exec_free_args(&exec_args);
136 linux_free_path(&path);
140 struct l_ipc_kludge {
141 struct l_msgbuf *msgp;
146 linux_ipc(struct linux_ipc_args *args)
150 switch (args->what & 0xFFFF) {
152 struct linux_semop_args a;
154 a.semid = args->arg1;
156 a.nsops = args->arg2;
157 a.sysmsg_lresult = 0;
158 error = linux_semop(&a);
159 args->sysmsg_lresult = a.sysmsg_lresult;
163 struct linux_semget_args a;
166 a.nsems = args->arg2;
167 a.semflg = args->arg3;
168 a.sysmsg_lresult = 0;
169 error = linux_semget(&a);
170 args->sysmsg_lresult = a.sysmsg_lresult;
174 struct linux_semctl_args a;
177 a.semid = args->arg1;
178 a.semnum = args->arg2;
180 a.sysmsg_lresult = 0;
181 error = copyin((caddr_t)args->ptr, &a.arg, sizeof(a.arg));
184 error = linux_semctl(&a);
185 args->sysmsg_lresult = a.sysmsg_lresult;
189 struct linux_msgsnd_args a;
191 a.msqid = args->arg1;
193 a.msgsz = args->arg2;
194 a.msgflg = args->arg3;
195 a.sysmsg_lresult = 0;
196 error = linux_msgsnd(&a);
197 args->sysmsg_lresult = a.sysmsg_lresult;
201 struct linux_msgrcv_args a;
203 a.msqid = args->arg1;
204 a.msgsz = args->arg2;
205 a.msgflg = args->arg3;
206 a.sysmsg_lresult = 0;
207 if ((args->what >> 16) == 0) {
208 struct l_ipc_kludge tmp;
211 if (args->ptr == NULL)
213 error = copyin((caddr_t)args->ptr, &tmp, sizeof(tmp));
217 a.msgtyp = tmp.msgtyp;
220 a.msgtyp = args->arg5;
222 error = linux_msgrcv(&a);
223 args->sysmsg_lresult = a.sysmsg_lresult;
227 struct linux_msgget_args a;
230 a.msgflg = args->arg2;
231 a.sysmsg_lresult = 0;
232 error = linux_msgget(&a);
233 args->sysmsg_lresult = a.sysmsg_lresult;
237 struct linux_msgctl_args a;
239 a.msqid = args->arg1;
242 a.sysmsg_lresult = 0;
243 error = linux_msgctl(&a);
244 args->sysmsg_lresult = a.sysmsg_lresult;
248 struct linux_shmat_args a;
250 a.shmid = args->arg1;
251 a.shmaddr = args->ptr;
252 a.shmflg = args->arg2;
253 a.raddr = (l_ulong *)args->arg3;
254 a.sysmsg_lresult = 0;
255 error = linux_shmat(&a);
256 args->sysmsg_lresult = a.sysmsg_lresult;
260 struct linux_shmdt_args a;
262 a.shmaddr = args->ptr;
263 a.sysmsg_lresult = 0;
264 error = linux_shmdt(&a);
265 args->sysmsg_lresult = a.sysmsg_lresult;
269 struct linux_shmget_args a;
273 a.shmflg = args->arg3;
274 a.sysmsg_lresult = 0;
275 error = linux_shmget(&a);
276 args->sysmsg_lresult = a.sysmsg_lresult;
280 struct linux_shmctl_args a;
282 a.shmid = args->arg1;
285 a.sysmsg_lresult = 0;
286 error = linux_shmctl(&a);
287 args->sysmsg_lresult = a.sysmsg_lresult;
298 linux_old_select(struct linux_old_select_args *args)
300 struct l_old_select_argv linux_args;
301 struct linux_select_args newsel;
305 if (ldebug(old_select))
306 printf(ARGS(old_select, "%p"), args->ptr);
309 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
313 newsel.sysmsg_result = 0;
314 newsel.nfds = linux_args.nfds;
315 newsel.readfds = linux_args.readfds;
316 newsel.writefds = linux_args.writefds;
317 newsel.exceptfds = linux_args.exceptfds;
318 newsel.timeout = linux_args.timeout;
319 error = linux_select(&newsel);
320 args->sysmsg_result = newsel.sysmsg_result;
325 linux_fork(struct linux_fork_args *args)
331 printf(ARGS(fork, ""));
334 if ((error = fork((struct fork_args *)args)) != 0)
337 if (args->sysmsg_result == 1)
338 args->sysmsg_result = 0;
343 linux_vfork(struct linux_vfork_args *args)
349 printf(ARGS(vfork, ""));
352 if ((error = vfork((struct vfork_args *)args)) != 0)
354 /* Are we the child? */
355 if (args->sysmsg_result == 1)
356 args->sysmsg_result = 0;
360 #define CLONE_VM 0x100
361 #define CLONE_FS 0x200
362 #define CLONE_FILES 0x400
363 #define CLONE_SIGHAND 0x800
364 #define CLONE_PID 0x1000
367 linux_clone(struct linux_clone_args *args)
369 int error, ff = RFPROC;
373 struct rfork_args rf_args;
377 printf(ARGS(clone, "flags %x, stack %x"),
378 (unsigned int)args->flags, (unsigned int)args->stack);
379 if (args->flags & CLONE_PID)
380 printf(LMSG("CLONE_PID not yet supported"));
387 exit_signal = args->flags & 0x000000ff;
388 if (exit_signal >= LINUX_NSIG)
391 if (exit_signal <= LINUX_SIGTBLSZ)
392 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
394 /* RFTHREAD probably not necessary here, but it shouldn't hurt */
397 if (args->flags & CLONE_VM)
399 if (args->flags & CLONE_SIGHAND)
401 if (!(args->flags & CLONE_FILES))
408 rf_args.sysmsg_result = 0;
409 if ((error = rfork(&rf_args)) != 0)
411 args->sysmsg_result = rf_args.sysmsg_result;
413 p2 = pfind(rf_args.sysmsg_result);
417 p2->p_sigparent = exit_signal;
418 p2->p_md.md_regs->tf_esp = (unsigned int)args->stack;
422 printf(LMSG("clone: successful rfork to %ld"),
439 #define STACK_SIZE (2 * 1024 * 1024)
440 #define GUARD_SIZE (4 * PAGE_SIZE)
443 linux_mmap_common(caddr_t linux_addr, size_t linux_len, int linux_prot,
444 int linux_flags, int linux_fd, off_t pos, void **res)
446 struct thread *td = curthread;
447 struct proc *p = td->td_proc;
450 int error, flags, len, prot, fd;
453 if (linux_flags & LINUX_MAP_SHARED)
455 if (linux_flags & LINUX_MAP_PRIVATE)
456 flags |= MAP_PRIVATE;
457 if (linux_flags & LINUX_MAP_FIXED)
459 if (linux_flags & LINUX_MAP_ANON) {
464 if (linux_flags & LINUX_MAP_GROWSDOWN) {
466 /* The linux MAP_GROWSDOWN option does not limit auto
467 * growth of the region. Linux mmap with this option
468 * takes as addr the inital BOS, and as len, the initial
469 * region size. It can then grow down from addr without
470 * limit. However, linux threads has an implicit internal
471 * limit to stack size of STACK_SIZE. Its just not
472 * enforced explicitly in linux. But, here we impose
473 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
474 * region, since we can do this with our mmap.
476 * Our mmap with MAP_STACK takes addr as the maximum
477 * downsize limit on BOS, and as len the max size of
478 * the region. It them maps the top SGROWSIZ bytes,
479 * and autgrows the region down, up to the limit
482 * If we don't use the MAP_STACK option, the effect
483 * of this code is to allocate a stack region of a
484 * fixed size of (STACK_SIZE - GUARD_SIZE).
487 /* This gives us TOS */
488 addr = linux_addr + linux_len;
490 if (addr > p->p_vmspace->vm_maxsaddr) {
491 /* Some linux apps will attempt to mmap
492 * thread stacks near the top of their
493 * address space. If their TOS is greater
494 * than vm_maxsaddr, vm_map_growstack()
495 * will confuse the thread stack with the
496 * process stack and deliver a SEGV if they
497 * attempt to grow the thread stack past their
498 * current stacksize rlimit. To avoid this,
499 * adjust vm_maxsaddr upwards to reflect
500 * the current stacksize rlimit rather
501 * than the maximum possible stacksize.
502 * It would be better to adjust the
503 * mmap'ed region, but some apps do not check
504 * mmap's return value.
506 p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
507 p->p_rlimit[RLIMIT_STACK].rlim_cur;
510 /* This gives us our maximum stack size */
511 if (linux_len > STACK_SIZE - GUARD_SIZE) {
514 len = STACK_SIZE - GUARD_SIZE;
516 /* This gives us a new BOS. If we're using VM_STACK, then
517 * mmap will just map the top SGROWSIZ bytes, and let
518 * the stack grow down to the limit at BOS. If we're
519 * not using VM_STACK we map the full stack, since we
520 * don't have a way to autogrow it.
528 prot = linux_prot | PROT_READ;
529 if (linux_flags & LINUX_MAP_ANON) {
536 if (ldebug(mmap) || ldebug(mmap2))
537 printf("-> (%p, %d, %d, 0x%08x, %d, %lld)\n",
538 addr, len, prot, flags, fd, pos);
540 error = kern_mmap(addr, len, prot, flags, fd, pos, &new);
548 linux_mmap(struct linux_mmap_args *args)
550 struct l_mmap_argv linux_args;
553 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
559 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
560 (void *)linux_args.addr, linux_args.len, linux_args.prot,
561 linux_args.flags, linux_args.fd, linux_args.pos);
563 error = linux_mmap_common(linux_args.addr, linux_args.len,
564 linux_args.prot, linux_args.flags, linux_args.fd,
565 linux_args.pos, &args->sysmsg_resultp);
568 printf("-> %p\n", args->sysmsg_resultp);
574 linux_mmap2(struct linux_mmap2_args *args)
580 printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
581 (void *)args->addr, args->len, args->prot, args->flags,
582 args->fd, args->pgoff);
584 error = linux_mmap_common((void *)args->addr, args->len, args->prot,
585 args->flags, args->fd, args->pgoff * PAGE_SIZE,
586 &args->sysmsg_resultp);
589 printf("-> %p\n", args->sysmsg_resultp);
595 linux_pipe(struct linux_pipe_args *args)
599 struct pipe_args bsd_args;
603 printf(ARGS(pipe, "*"));
606 reg_edx = args->sysmsg_fds[1];
607 error = pipe(&bsd_args);
609 args->sysmsg_fds[1] = reg_edx;
613 error = copyout(bsd_args.sysmsg_fds, args->pipefds, 2*sizeof(int));
615 args->sysmsg_fds[1] = reg_edx;
619 args->sysmsg_fds[1] = reg_edx;
620 args->sysmsg_fds[0] = 0;
625 linux_ioperm(struct linux_ioperm_args *args)
627 struct sysarch_args sa;
628 struct i386_ioperm_args *iia;
632 sg = stackgap_init();
633 iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
634 iia->start = args->start;
635 iia->length = args->length;
636 iia->enable = args->enable;
637 sa.sysmsg_resultp = NULL;
638 sa.op = I386_SET_IOPERM;
639 sa.parms = (char *)iia;
640 error = sysarch(&sa);
641 args->sysmsg_resultp = sa.sysmsg_resultp;
646 linux_iopl(struct linux_iopl_args *args)
648 struct thread *td = curthread;
649 struct proc *p = td->td_proc;
654 if (args->level < 0 || args->level > 3)
656 if ((error = suser(td)) != 0)
660 p->p_md.md_regs->tf_eflags = (p->p_md.md_regs->tf_eflags & ~PSL_IOPL) |
661 (args->level * (PSL_IOPL / 3));
666 linux_modify_ldt(struct linux_modify_ldt_args *uap)
670 struct sysarch_args args;
671 struct i386_ldt_args *ldt;
672 struct l_descriptor ld;
673 union descriptor *desc;
675 sg = stackgap_init();
677 if (uap->ptr == NULL)
681 case 0x00: /* read_ldt */
682 ldt = stackgap_alloc(&sg, sizeof(*ldt));
684 ldt->descs = uap->ptr;
685 ldt->num = uap->bytecount / sizeof(union descriptor);
686 args.op = I386_GET_LDT;
687 args.parms = (char*)ldt;
688 args.sysmsg_result = 0;
689 error = sysarch(&args);
690 uap->sysmsg_result = args.sysmsg_result *
691 sizeof(union descriptor);
693 case 0x01: /* write_ldt */
694 case 0x11: /* write_ldt */
695 if (uap->bytecount != sizeof(ld))
698 error = copyin(uap->ptr, &ld, sizeof(ld));
702 ldt = stackgap_alloc(&sg, sizeof(*ldt));
703 desc = stackgap_alloc(&sg, sizeof(*desc));
704 ldt->start = ld.entry_number;
707 desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
708 desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
709 desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
710 desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
711 desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
714 desc->sd.sd_p = (ld.seg_not_present ^ 1);
716 desc->sd.sd_def32 = ld.seg_32bit;
717 desc->sd.sd_gran = ld.limit_in_pages;
718 args.op = I386_SET_LDT;
719 args.parms = (char*)ldt;
720 args.sysmsg_result = 0;
721 error = sysarch(&args);
722 uap->sysmsg_result = args.sysmsg_result;
733 linux_sigaction(struct linux_sigaction_args *args)
736 l_sigaction_t linux_act, linux_oact;
737 struct sigaction act, oact;
741 if (ldebug(sigaction))
742 printf(ARGS(sigaction, "%d, %p, %p"),
743 args->sig, (void *)args->nsa, (void *)args->osa);
747 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
750 linux_act.lsa_handler = osa.lsa_handler;
751 linux_act.lsa_flags = osa.lsa_flags;
752 linux_act.lsa_restorer = osa.lsa_restorer;
753 LINUX_SIGEMPTYSET(linux_act.lsa_mask);
754 linux_act.lsa_mask.__bits[0] = osa.lsa_mask;
755 linux_to_bsd_sigaction(&linux_act, &act);
758 error = kern_sigaction(args->sig, args->nsa ? &act : NULL,
759 args->osa ? &oact : NULL);
761 if (args->osa != NULL && !error) {
762 bsd_to_linux_sigaction(&oact, &linux_oact);
763 osa.lsa_handler = linux_oact.lsa_handler;
764 osa.lsa_flags = linux_oact.lsa_flags;
765 osa.lsa_restorer = linux_oact.lsa_restorer;
766 osa.lsa_mask = linux_oact.lsa_mask.__bits[0];
767 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
773 * Linux has two extra args, restart and oldmask. We dont use these,
774 * but it seems that "restart" is actually a context pointer that
775 * enables the signal to happen with a different register set.
778 linux_sigsuspend(struct linux_sigsuspend_args *args)
780 l_sigset_t linux_mask;
785 if (ldebug(sigsuspend))
786 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
789 LINUX_SIGEMPTYSET(mask);
790 mask.__bits[0] = args->mask;
791 linux_to_bsd_sigset(&linux_mask, &mask);
793 error = kern_sigsuspend(&mask);
799 linux_rt_sigsuspend(struct linux_rt_sigsuspend_args *uap)
801 l_sigset_t linux_mask;
806 if (ldebug(rt_sigsuspend))
807 printf(ARGS(rt_sigsuspend, "%p, %d"),
808 (void *)uap->newset, uap->sigsetsize);
811 if (uap->sigsetsize != sizeof(l_sigset_t))
814 error = copyin(uap->newset, &linux_mask, sizeof(l_sigset_t));
818 linux_to_bsd_sigset(&linux_mask, &mask);
820 error = kern_sigsuspend(&mask);
826 linux_pause(struct linux_pause_args *args)
828 struct thread *td = curthread;
829 struct proc *p = td->td_proc;
835 printf(ARGS(pause, ""));
840 error = kern_sigsuspend(&mask);
846 linux_sigaltstack(struct linux_sigaltstack_args *uap)
853 if (ldebug(sigaltstack))
854 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
858 error = copyin(uap->uss, &linux_ss, sizeof(l_stack_t));
862 ss.ss_sp = linux_ss.ss_sp;
863 ss.ss_size = linux_ss.ss_size;
864 ss.ss_flags = linux_to_bsd_sigaltstack(linux_ss.ss_flags);
867 error = kern_sigaltstack(uap->uss ? &ss : NULL,
868 uap->uoss ? &oss : NULL);
870 if (error == 0 && uap->uoss) {
871 linux_ss.ss_sp = oss.ss_sp;
872 linux_ss.ss_size = oss.ss_size;
873 linux_ss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
874 error = copyout(&linux_ss, uap->uoss, sizeof(l_stack_t));