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.6 2003/07/26 18:12:43 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
37 #include <sys/resource.h>
38 #include <sys/resourcevar.h>
39 #include <sys/sysproto.h>
40 #include <sys/unistd.h>
42 #include <machine/frame.h>
43 #include <machine/psl.h>
44 #include <machine/segments.h>
45 #include <machine/sysarch.h>
49 #include <vm/vm_map.h>
51 #include <i386/linux/linux.h>
52 #include <i386/linux/linux_proto.h>
53 #include <compat/linux/linux_ipc.h>
54 #include <compat/linux/linux_signal.h>
55 #include <compat/linux/linux_util.h>
63 l_uint read_exec_only:1;
64 l_uint limit_in_pages:1;
65 l_uint seg_not_present:1;
69 struct l_old_select_argv {
74 struct l_timeval *timeout;
78 linux_to_bsd_sigaltstack(int lsa)
82 if (lsa & LINUX_SS_DISABLE)
84 if (lsa & LINUX_SS_ONSTACK)
90 bsd_to_linux_sigaltstack(int bsa)
95 lsa |= LINUX_SS_DISABLE;
97 lsa |= LINUX_SS_ONSTACK;
102 linux_execve(struct linux_execve_args *args)
104 struct execve_args bsd;
108 sg = stackgap_init();
109 CHECKALTEXIST(&sg, args->path);
113 printf(ARGS(execve, "%s"), args->path);
116 bsd.lmsg.u.ms_result = 0;
117 bsd.fname = args->path;
118 bsd.argv = args->argp;
119 bsd.envv = args->envp;
120 error = execve(&bsd);
121 args->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
125 struct l_ipc_kludge {
126 struct l_msgbuf *msgp;
131 linux_ipc(struct linux_ipc_args *args)
135 switch (args->what & 0xFFFF) {
137 struct linux_semop_args a;
139 a.semid = args->arg1;
141 a.nsops = args->arg2;
142 a.lmsg.u.ms_lresult = 0;
143 error = linux_semop(&a);
144 args->lmsg.u.ms_lresult = a.lmsg.u.ms_lresult;
148 struct linux_semget_args a;
151 a.nsems = args->arg2;
152 a.semflg = args->arg3;
153 a.lmsg.u.ms_lresult = 0;
154 error = linux_semget(&a);
155 args->lmsg.u.ms_lresult = a.lmsg.u.ms_lresult;
159 struct linux_semctl_args a;
162 a.semid = args->arg1;
163 a.semnum = args->arg2;
165 a.lmsg.u.ms_lresult = 0;
166 error = copyin((caddr_t)args->ptr, &a.arg, sizeof(a.arg));
169 error = linux_semctl(&a);
170 args->lmsg.u.ms_lresult = a.lmsg.u.ms_lresult;
174 struct linux_msgsnd_args a;
176 a.msqid = args->arg1;
178 a.msgsz = args->arg2;
179 a.msgflg = args->arg3;
180 a.lmsg.u.ms_lresult = 0;
181 error = linux_msgsnd(&a);
182 args->lmsg.u.ms_lresult = a.lmsg.u.ms_lresult;
186 struct linux_msgrcv_args a;
188 a.msqid = args->arg1;
189 a.msgsz = args->arg2;
190 a.msgflg = args->arg3;
191 a.lmsg.u.ms_lresult = 0;
192 if ((args->what >> 16) == 0) {
193 struct l_ipc_kludge tmp;
196 if (args->ptr == NULL)
198 error = copyin((caddr_t)args->ptr, &tmp, sizeof(tmp));
202 a.msgtyp = tmp.msgtyp;
205 a.msgtyp = args->arg5;
207 error = linux_msgrcv(&a);
208 args->lmsg.u.ms_lresult = a.lmsg.u.ms_lresult;
212 struct linux_msgget_args a;
215 a.msgflg = args->arg2;
216 a.lmsg.u.ms_lresult = 0;
217 error = linux_msgget(&a);
218 args->lmsg.u.ms_lresult = a.lmsg.u.ms_lresult;
222 struct linux_msgctl_args a;
224 a.msqid = args->arg1;
227 a.lmsg.u.ms_lresult = 0;
228 error = linux_msgctl(&a);
229 args->lmsg.u.ms_lresult = a.lmsg.u.ms_lresult;
233 struct linux_shmat_args a;
235 a.shmid = args->arg1;
236 a.shmaddr = args->ptr;
237 a.shmflg = args->arg2;
238 a.raddr = (l_ulong *)args->arg3;
239 a.lmsg.u.ms_lresult = 0;
240 error = linux_shmat(&a);
241 args->lmsg.u.ms_lresult = a.lmsg.u.ms_lresult;
245 struct linux_shmdt_args a;
247 a.shmaddr = args->ptr;
248 a.lmsg.u.ms_lresult = 0;
249 error = linux_shmdt(&a);
250 args->lmsg.u.ms_lresult = a.lmsg.u.ms_lresult;
254 struct linux_shmget_args a;
258 a.shmflg = args->arg3;
259 a.lmsg.u.ms_lresult = 0;
260 error = linux_shmget(&a);
261 args->lmsg.u.ms_lresult = a.lmsg.u.ms_lresult;
265 struct linux_shmctl_args a;
267 a.shmid = args->arg1;
270 a.lmsg.u.ms_lresult = 0;
271 error = linux_shmctl(&a);
272 args->lmsg.u.ms_lresult = a.lmsg.u.ms_lresult;
283 linux_old_select(struct linux_old_select_args *args)
285 struct l_old_select_argv linux_args;
286 struct linux_select_args newsel;
290 if (ldebug(old_select))
291 printf(ARGS(old_select, "%p"), args->ptr);
294 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
298 newsel.lmsg.u.ms_result = 0;
299 newsel.nfds = linux_args.nfds;
300 newsel.readfds = linux_args.readfds;
301 newsel.writefds = linux_args.writefds;
302 newsel.exceptfds = linux_args.exceptfds;
303 newsel.timeout = linux_args.timeout;
304 error = linux_select(&newsel);
305 args->lmsg.u.ms_result = newsel.lmsg.u.ms_result;
310 linux_fork(struct linux_fork_args *args)
316 printf(ARGS(fork, ""));
319 if ((error = fork((struct fork_args *)args)) != 0)
322 if (args->lmsg.u.ms_result == 1)
323 args->lmsg.u.ms_result = 0;
328 linux_vfork(struct linux_vfork_args *args)
334 printf(ARGS(vfork, ""));
337 if ((error = vfork((struct vfork_args *)args)) != 0)
339 /* Are we the child? */
340 if (args->lmsg.u.ms_result == 1)
341 args->lmsg.u.ms_result = 0;
345 #define CLONE_VM 0x100
346 #define CLONE_FS 0x200
347 #define CLONE_FILES 0x400
348 #define CLONE_SIGHAND 0x800
349 #define CLONE_PID 0x1000
352 linux_clone(struct linux_clone_args *args)
354 int error, ff = RFPROC;
358 struct rfork_args rf_args;
362 printf(ARGS(clone, "flags %x, stack %x"),
363 (unsigned int)args->flags, (unsigned int)args->stack);
364 if (args->flags & CLONE_PID)
365 printf(LMSG("CLONE_PID not yet supported"));
372 exit_signal = args->flags & 0x000000ff;
373 if (exit_signal >= LINUX_NSIG)
376 if (exit_signal <= LINUX_SIGTBLSZ)
377 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
379 /* RFTHREAD probably not necessary here, but it shouldn't hurt */
382 if (args->flags & CLONE_VM)
384 if (args->flags & CLONE_SIGHAND)
386 if (!(args->flags & CLONE_FILES))
393 rf_args.lmsg.u.ms_result = 0;
394 if ((error = rfork(&rf_args)) != 0)
396 args->lmsg.u.ms_result = rf_args.lmsg.u.ms_result;
398 p2 = pfind(rf_args.lmsg.u.ms_result);
402 p2->p_sigparent = exit_signal;
403 p2->p_md.md_regs->tf_esp = (unsigned int)args->stack;
407 printf(LMSG("clone: successful rfork to %ld"),
424 #define STACK_SIZE (2 * 1024 * 1024)
425 #define GUARD_SIZE (4 * PAGE_SIZE)
428 linux_mmap(struct linux_mmap_args *args)
430 struct proc *p = curproc;
431 struct mmap_args bsd_args;
433 struct l_mmap_argv linux_args;
435 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
441 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
442 (void *)linux_args.addr, linux_args.len, linux_args.prot,
443 linux_args.flags, linux_args.fd, linux_args.pos);
447 bsd_args.lmsg.u.ms_resultp = NULL;
448 if (linux_args.flags & LINUX_MAP_SHARED)
449 bsd_args.flags |= MAP_SHARED;
450 if (linux_args.flags & LINUX_MAP_PRIVATE)
451 bsd_args.flags |= MAP_PRIVATE;
452 if (linux_args.flags & LINUX_MAP_FIXED)
453 bsd_args.flags |= MAP_FIXED;
454 if (linux_args.flags & LINUX_MAP_ANON)
455 bsd_args.flags |= MAP_ANON;
457 bsd_args.flags |= MAP_NOSYNC;
458 if (linux_args.flags & LINUX_MAP_GROWSDOWN) {
459 bsd_args.flags |= MAP_STACK;
461 /* The linux MAP_GROWSDOWN option does not limit auto
462 * growth of the region. Linux mmap with this option
463 * takes as addr the inital BOS, and as len, the initial
464 * region size. It can then grow down from addr without
465 * limit. However, linux threads has an implicit internal
466 * limit to stack size of STACK_SIZE. Its just not
467 * enforced explicitly in linux. But, here we impose
468 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
469 * region, since we can do this with our mmap.
471 * Our mmap with MAP_STACK takes addr as the maximum
472 * downsize limit on BOS, and as len the max size of
473 * the region. It them maps the top SGROWSIZ bytes,
474 * and autgrows the region down, up to the limit
477 * If we don't use the MAP_STACK option, the effect
478 * of this code is to allocate a stack region of a
479 * fixed size of (STACK_SIZE - GUARD_SIZE).
482 /* This gives us TOS */
483 bsd_args.addr = linux_args.addr + linux_args.len;
485 if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) {
486 /* Some linux apps will attempt to mmap
487 * thread stacks near the top of their
488 * address space. If their TOS is greater
489 * than vm_maxsaddr, vm_map_growstack()
490 * will confuse the thread stack with the
491 * process stack and deliver a SEGV if they
492 * attempt to grow the thread stack past their
493 * current stacksize rlimit. To avoid this,
494 * adjust vm_maxsaddr upwards to reflect
495 * the current stacksize rlimit rather
496 * than the maximum possible stacksize.
497 * It would be better to adjust the
498 * mmap'ed region, but some apps do not check
499 * mmap's return value.
501 p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
502 p->p_rlimit[RLIMIT_STACK].rlim_cur;
505 /* This gives us our maximum stack size */
506 if (linux_args.len > STACK_SIZE - GUARD_SIZE)
507 bsd_args.len = linux_args.len;
509 bsd_args.len = STACK_SIZE - GUARD_SIZE;
511 /* This gives us a new BOS. If we're using VM_STACK, then
512 * mmap will just map the top SGROWSIZ bytes, and let
513 * the stack grow down to the limit at BOS. If we're
514 * not using VM_STACK we map the full stack, since we
515 * don't have a way to autogrow it.
517 bsd_args.addr -= bsd_args.len;
519 bsd_args.addr = linux_args.addr;
520 bsd_args.len = linux_args.len;
523 bsd_args.prot = linux_args.prot | PROT_READ; /* always required */
524 if (linux_args.flags & LINUX_MAP_ANON)
527 bsd_args.fd = linux_args.fd;
528 bsd_args.pos = linux_args.pos;
533 printf("-> (%p, %d, %d, 0x%08x, %d, %d)\n",
534 (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
535 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
538 error = mmap(&bsd_args);
539 args->lmsg.u.ms_resultp = bsd_args.lmsg.u.ms_resultp;
544 linux_pipe(struct linux_pipe_args *args)
548 struct pipe_args bsd_args;
552 printf(ARGS(pipe, "*"));
555 reg_edx = args->lmsg.u.ms_fds[1];
556 error = pipe(&bsd_args);
558 args->lmsg.u.ms_fds[1] = reg_edx;
562 error = copyout(bsd_args.lmsg.u.ms_fds, args->pipefds, 2*sizeof(int));
564 args->lmsg.u.ms_fds[1] = reg_edx;
568 args->lmsg.u.ms_fds[1] = reg_edx;
569 args->lmsg.u.ms_fds[0] = 0;
574 linux_ioperm(struct linux_ioperm_args *args)
576 struct sysarch_args sa;
577 struct i386_ioperm_args *iia;
581 sg = stackgap_init();
582 iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
583 iia->start = args->start;
584 iia->length = args->length;
585 iia->enable = args->enable;
586 sa.lmsg.u.ms_resultp = NULL;
587 sa.op = I386_SET_IOPERM;
588 sa.parms = (char *)iia;
589 error = sysarch(&sa);
590 args->lmsg.u.ms_resultp = sa.lmsg.u.ms_resultp;
595 linux_iopl(struct linux_iopl_args *args)
597 struct thread *td = curthread;
598 struct proc *p = td->td_proc;
603 if (args->level < 0 || args->level > 3)
605 if ((error = suser(td)) != 0)
609 p->p_md.md_regs->tf_eflags = (p->p_md.md_regs->tf_eflags & ~PSL_IOPL) |
610 (args->level * (PSL_IOPL / 3));
615 linux_modify_ldt(struct linux_modify_ldt_args *uap)
619 struct sysarch_args args;
620 struct i386_ldt_args *ldt;
621 struct l_descriptor ld;
622 union descriptor *desc;
624 sg = stackgap_init();
626 if (uap->ptr == NULL)
630 case 0x00: /* read_ldt */
631 ldt = stackgap_alloc(&sg, sizeof(*ldt));
633 ldt->descs = uap->ptr;
634 ldt->num = uap->bytecount / sizeof(union descriptor);
635 args.op = I386_GET_LDT;
636 args.parms = (char*)ldt;
637 args.lmsg.u.ms_result = 0;
638 error = sysarch(&args);
639 uap->lmsg.u.ms_result = args.lmsg.u.ms_result *
640 sizeof(union descriptor);
642 case 0x01: /* write_ldt */
643 case 0x11: /* write_ldt */
644 if (uap->bytecount != sizeof(ld))
647 error = copyin(uap->ptr, &ld, sizeof(ld));
651 ldt = stackgap_alloc(&sg, sizeof(*ldt));
652 desc = stackgap_alloc(&sg, sizeof(*desc));
653 ldt->start = ld.entry_number;
656 desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
657 desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
658 desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
659 desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
660 desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
663 desc->sd.sd_p = (ld.seg_not_present ^ 1);
665 desc->sd.sd_def32 = ld.seg_32bit;
666 desc->sd.sd_gran = ld.limit_in_pages;
667 args.op = I386_SET_LDT;
668 args.parms = (char*)ldt;
669 args.lmsg.u.ms_result = 0;
670 error = sysarch(&args);
671 uap->lmsg.u.ms_result = args.lmsg.u.ms_result;
678 if (error == EOPNOTSUPP) {
679 printf("linux: modify_ldt needs kernel option USER_LDT\n");
687 linux_sigaction(struct linux_sigaction_args *args)
690 l_sigaction_t act, oact;
694 if (ldebug(sigaction))
695 printf(ARGS(sigaction, "%d, %p, %p"),
696 args->sig, (void *)args->nsa, (void *)args->osa);
699 if (args->nsa != NULL) {
700 error = copyin((caddr_t)args->nsa, &osa,
701 sizeof(l_osigaction_t));
704 act.lsa_handler = osa.lsa_handler;
705 act.lsa_flags = osa.lsa_flags;
706 act.lsa_restorer = osa.lsa_restorer;
707 LINUX_SIGEMPTYSET(act.lsa_mask);
708 act.lsa_mask.__bits[0] = osa.lsa_mask;
711 error = linux_do_sigaction(args->sig, args->nsa ? &act : NULL,
712 args->osa ? &oact : NULL, &args->lmsg.u.ms_result);
714 if (args->osa != NULL && !error) {
715 osa.lsa_handler = oact.lsa_handler;
716 osa.lsa_flags = oact.lsa_flags;
717 osa.lsa_restorer = oact.lsa_restorer;
718 osa.lsa_mask = oact.lsa_mask.__bits[0];
719 error = copyout(&osa, (caddr_t)args->osa,
720 sizeof(l_osigaction_t));
727 * Linux has two extra args, restart and oldmask. We dont use these,
728 * but it seems that "restart" is actually a context pointer that
729 * enables the signal to happen with a different register set.
732 linux_sigsuspend(struct linux_sigsuspend_args *args)
734 struct sigsuspend_args bsd;
737 caddr_t sg = stackgap_init();
741 if (ldebug(sigsuspend))
742 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
745 sigmask = stackgap_alloc(&sg, sizeof(sigset_t));
746 LINUX_SIGEMPTYSET(mask);
747 mask.__bits[0] = args->mask;
748 linux_to_bsd_sigset(&mask, sigmask);
749 bsd.sigmask = sigmask;
750 bsd.lmsg.u.ms_result = 0;
751 error = sigsuspend(&bsd);
752 args->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
757 linux_rt_sigsuspend(struct linux_rt_sigsuspend_args *uap)
761 struct sigsuspend_args bsd;
762 caddr_t sg = stackgap_init();
766 if (ldebug(rt_sigsuspend))
767 printf(ARGS(rt_sigsuspend, "%p, %d"),
768 (void *)uap->newset, uap->sigsetsize);
771 if (uap->sigsetsize != sizeof(l_sigset_t))
774 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
778 bmask = stackgap_alloc(&sg, sizeof(sigset_t));
779 linux_to_bsd_sigset(&lmask, bmask);
781 bsd.lmsg.u.ms_result = 0;
782 error = sigsuspend(&bsd);
783 uap->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
788 linux_pause(struct linux_pause_args *args)
790 struct proc *p = curproc;
791 struct sigsuspend_args bsd;
793 caddr_t sg = stackgap_init();
798 printf(ARGS(pause, ""));
801 sigmask = stackgap_alloc(&sg, sizeof(sigset_t));
802 *sigmask = p->p_sigmask;
803 bsd.sigmask = sigmask;
804 bsd.lmsg.u.ms_result = 0;
805 error = sigsuspend(&bsd);
806 args->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
811 linux_sigaltstack(struct linux_sigaltstack_args *uap)
813 struct sigaltstack_args bsd;
817 caddr_t sg = stackgap_init();
820 if (ldebug(sigaltstack))
821 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
824 if (uap->uss == NULL) {
827 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
831 ss = stackgap_alloc(&sg, sizeof(stack_t));
832 ss->ss_sp = lss.ss_sp;
833 ss->ss_size = lss.ss_size;
834 ss->ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
836 oss = (uap->uoss != NULL)
837 ? stackgap_alloc(&sg, sizeof(stack_t))
842 bsd.lmsg.u.ms_result = 0;
843 error = sigaltstack(&bsd);
844 uap->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
846 if (!error && oss != NULL) {
847 lss.ss_sp = oss->ss_sp;
848 lss.ss_size = oss->ss_size;
849 lss.ss_flags = bsd_to_linux_sigaltstack(oss->ss_flags);
850 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));