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.2 2003/06/17 04:28:38 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 proc *p, struct linux_execve_args *args)
104 struct execve_args bsd;
107 sg = stackgap_init();
108 CHECKALTEXIST(p, &sg, args->path);
112 printf(ARGS(execve, "%s"), args->path);
115 bsd.fname = args->path;
116 bsd.argv = args->argp;
117 bsd.envv = args->envp;
118 return (execve(p, &bsd));
121 struct l_ipc_kludge {
122 struct l_msgbuf *msgp;
127 linux_ipc(struct proc *p, struct linux_ipc_args *args)
130 switch (args->what & 0xFFFF) {
132 struct linux_semop_args a;
134 a.semid = args->arg1;
136 a.nsops = args->arg2;
137 return (linux_semop(p, &a));
140 struct linux_semget_args a;
143 a.nsems = args->arg2;
144 a.semflg = args->arg3;
145 return (linux_semget(p, &a));
148 struct linux_semctl_args a;
151 a.semid = args->arg1;
152 a.semnum = args->arg2;
154 error = copyin((caddr_t)args->ptr, &a.arg, sizeof(a.arg));
157 return (linux_semctl(p, &a));
160 struct linux_msgsnd_args a;
162 a.msqid = args->arg1;
164 a.msgsz = args->arg2;
165 a.msgflg = args->arg3;
166 return (linux_msgsnd(p, &a));
169 struct linux_msgrcv_args a;
171 a.msqid = args->arg1;
172 a.msgsz = args->arg2;
173 a.msgflg = args->arg3;
174 if ((args->what >> 16) == 0) {
175 struct l_ipc_kludge tmp;
178 if (args->ptr == NULL)
180 error = copyin((caddr_t)args->ptr, &tmp, sizeof(tmp));
184 a.msgtyp = tmp.msgtyp;
187 a.msgtyp = args->arg5;
189 return (linux_msgrcv(p, &a));
192 struct linux_msgget_args a;
195 a.msgflg = args->arg2;
196 return (linux_msgget(p, &a));
199 struct linux_msgctl_args a;
201 a.msqid = args->arg1;
204 return (linux_msgctl(p, &a));
207 struct linux_shmat_args a;
209 a.shmid = args->arg1;
210 a.shmaddr = args->ptr;
211 a.shmflg = args->arg2;
212 a.raddr = (l_ulong *)args->arg3;
213 return (linux_shmat(p, &a));
216 struct linux_shmdt_args a;
218 a.shmaddr = args->ptr;
219 return (linux_shmdt(p, &a));
222 struct linux_shmget_args a;
226 a.shmflg = args->arg3;
227 return (linux_shmget(p, &a));
230 struct linux_shmctl_args a;
232 a.shmid = args->arg1;
235 return (linux_shmctl(p, &a));
245 linux_old_select(struct proc *p, struct linux_old_select_args *args)
247 struct l_old_select_argv linux_args;
248 struct linux_select_args newsel;
252 if (ldebug(old_select))
253 printf(ARGS(old_select, "%x"), args->ptr);
256 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
260 newsel.nfds = linux_args.nfds;
261 newsel.readfds = linux_args.readfds;
262 newsel.writefds = linux_args.writefds;
263 newsel.exceptfds = linux_args.exceptfds;
264 newsel.timeout = linux_args.timeout;
265 return (linux_select(p, &newsel));
269 linux_fork(struct proc *p, struct linux_fork_args *args)
275 printf(ARGS(fork, ""));
278 if ((error = fork(p, (struct fork_args *)args)) != 0)
281 if (p->p_retval[1] == 1)
287 linux_vfork(struct proc *p, struct linux_vfork_args *args)
293 printf(ARGS(vfork, ""));
296 if ((error = vfork(p, (struct vfork_args *)args)) != 0)
298 /* Are we the child? */
299 if (p->p_retval[1] == 1)
304 #define CLONE_VM 0x100
305 #define CLONE_FS 0x200
306 #define CLONE_FILES 0x400
307 #define CLONE_SIGHAND 0x800
308 #define CLONE_PID 0x1000
311 linux_clone(struct proc *p, struct linux_clone_args *args)
313 int error, ff = RFPROC;
317 struct rfork_args rf_args;
321 printf(ARGS(clone, "flags %x, stack %x"),
322 (unsigned int)args->flags, (unsigned int)args->stack);
323 if (args->flags & CLONE_PID)
324 printf(LMSG("CLONE_PID not yet supported"));
331 exit_signal = args->flags & 0x000000ff;
332 if (exit_signal >= LINUX_NSIG)
335 if (exit_signal <= LINUX_SIGTBLSZ)
336 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
338 /* RFTHREAD probably not necessary here, but it shouldn't hurt */
341 if (args->flags & CLONE_VM)
343 if (args->flags & CLONE_SIGHAND)
345 if (!(args->flags & CLONE_FILES))
352 if ((error = rfork(p, &rf_args)) != 0)
355 p2 = pfind(p->p_retval[0]);
359 p2->p_sigparent = exit_signal;
360 p2->p_md.md_regs->tf_esp = (unsigned int)args->stack;
364 printf(LMSG("clone: successful rfork to %ld"),
381 #define STACK_SIZE (2 * 1024 * 1024)
382 #define GUARD_SIZE (4 * PAGE_SIZE)
385 linux_mmap(struct proc *p, struct linux_mmap_args *args)
387 struct mmap_args /* {
397 struct l_mmap_argv linux_args;
399 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
405 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
406 (void *)linux_args.addr, linux_args.len, linux_args.prot,
407 linux_args.flags, linux_args.fd, linux_args.pos);
411 if (linux_args.flags & LINUX_MAP_SHARED)
412 bsd_args.flags |= MAP_SHARED;
413 if (linux_args.flags & LINUX_MAP_PRIVATE)
414 bsd_args.flags |= MAP_PRIVATE;
415 if (linux_args.flags & LINUX_MAP_FIXED)
416 bsd_args.flags |= MAP_FIXED;
417 if (linux_args.flags & LINUX_MAP_ANON)
418 bsd_args.flags |= MAP_ANON;
420 bsd_args.flags |= MAP_NOSYNC;
421 if (linux_args.flags & LINUX_MAP_GROWSDOWN) {
422 bsd_args.flags |= MAP_STACK;
424 /* The linux MAP_GROWSDOWN option does not limit auto
425 * growth of the region. Linux mmap with this option
426 * takes as addr the inital BOS, and as len, the initial
427 * region size. It can then grow down from addr without
428 * limit. However, linux threads has an implicit internal
429 * limit to stack size of STACK_SIZE. Its just not
430 * enforced explicitly in linux. But, here we impose
431 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
432 * region, since we can do this with our mmap.
434 * Our mmap with MAP_STACK takes addr as the maximum
435 * downsize limit on BOS, and as len the max size of
436 * the region. It them maps the top SGROWSIZ bytes,
437 * and autgrows the region down, up to the limit
440 * If we don't use the MAP_STACK option, the effect
441 * of this code is to allocate a stack region of a
442 * fixed size of (STACK_SIZE - GUARD_SIZE).
445 /* This gives us TOS */
446 bsd_args.addr = linux_args.addr + linux_args.len;
448 if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) {
449 /* Some linux apps will attempt to mmap
450 * thread stacks near the top of their
451 * address space. If their TOS is greater
452 * than vm_maxsaddr, vm_map_growstack()
453 * will confuse the thread stack with the
454 * process stack and deliver a SEGV if they
455 * attempt to grow the thread stack past their
456 * current stacksize rlimit. To avoid this,
457 * adjust vm_maxsaddr upwards to reflect
458 * the current stacksize rlimit rather
459 * than the maximum possible stacksize.
460 * It would be better to adjust the
461 * mmap'ed region, but some apps do not check
462 * mmap's return value.
464 p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
465 p->p_rlimit[RLIMIT_STACK].rlim_cur;
468 /* This gives us our maximum stack size */
469 if (linux_args.len > STACK_SIZE - GUARD_SIZE)
470 bsd_args.len = linux_args.len;
472 bsd_args.len = STACK_SIZE - GUARD_SIZE;
474 /* This gives us a new BOS. If we're using VM_STACK, then
475 * mmap will just map the top SGROWSIZ bytes, and let
476 * the stack grow down to the limit at BOS. If we're
477 * not using VM_STACK we map the full stack, since we
478 * don't have a way to autogrow it.
480 bsd_args.addr -= bsd_args.len;
482 bsd_args.addr = linux_args.addr;
483 bsd_args.len = linux_args.len;
486 bsd_args.prot = linux_args.prot | PROT_READ; /* always required */
487 if (linux_args.flags & LINUX_MAP_ANON)
490 bsd_args.fd = linux_args.fd;
491 bsd_args.pos = linux_args.pos;
496 printf("-> (%p, %d, %d, 0x%08x, %d, %d)\n",
497 (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
498 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
501 return (mmap(p, &bsd_args));
505 linux_pipe(struct proc *p, struct linux_pipe_args *args)
512 printf(ARGS(pipe, "*"));
515 reg_edx = p->p_retval[1];
518 p->p_retval[1] = reg_edx;
522 error = copyout(p->p_retval, args->pipefds, 2*sizeof(int));
524 p->p_retval[1] = reg_edx;
528 p->p_retval[1] = reg_edx;
534 linux_ioperm(struct proc *p, struct linux_ioperm_args *args)
536 struct sysarch_args sa;
537 struct i386_ioperm_args *iia;
540 sg = stackgap_init();
541 iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
542 iia->start = args->start;
543 iia->length = args->length;
544 iia->enable = args->enable;
545 sa.op = I386_SET_IOPERM;
546 sa.parms = (char *)iia;
547 return (sysarch(p, &sa));
551 linux_iopl(struct proc *p, struct linux_iopl_args *args)
555 if (args->level < 0 || args->level > 3)
557 if ((error = suser(p)) != 0)
561 p->p_md.md_regs->tf_eflags = (p->p_md.md_regs->tf_eflags & ~PSL_IOPL) |
562 (args->level * (PSL_IOPL / 3));
567 linux_modify_ldt(p, uap)
569 struct linux_modify_ldt_args *uap;
573 struct sysarch_args args;
574 struct i386_ldt_args *ldt;
575 struct l_descriptor ld;
576 union descriptor *desc;
578 sg = stackgap_init();
580 if (uap->ptr == NULL)
584 case 0x00: /* read_ldt */
585 ldt = stackgap_alloc(&sg, sizeof(*ldt));
587 ldt->descs = uap->ptr;
588 ldt->num = uap->bytecount / sizeof(union descriptor);
589 args.op = I386_GET_LDT;
590 args.parms = (char*)ldt;
591 error = sysarch(p, &args);
592 p->p_retval[0] *= sizeof(union descriptor);
594 case 0x01: /* write_ldt */
595 case 0x11: /* write_ldt */
596 if (uap->bytecount != sizeof(ld))
599 error = copyin(uap->ptr, &ld, sizeof(ld));
603 ldt = stackgap_alloc(&sg, sizeof(*ldt));
604 desc = stackgap_alloc(&sg, sizeof(*desc));
605 ldt->start = ld.entry_number;
608 desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
609 desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
610 desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
611 desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
612 desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
615 desc->sd.sd_p = (ld.seg_not_present ^ 1);
617 desc->sd.sd_def32 = ld.seg_32bit;
618 desc->sd.sd_gran = ld.limit_in_pages;
619 args.op = I386_SET_LDT;
620 args.parms = (char*)ldt;
621 error = sysarch(p, &args);
628 if (error == EOPNOTSUPP) {
629 printf("linux: modify_ldt needs kernel option USER_LDT\n");
637 linux_sigaction(struct proc *p, struct linux_sigaction_args *args)
640 l_sigaction_t act, oact;
644 if (ldebug(sigaction))
645 printf(ARGS(sigaction, "%d, %p, %p"),
646 args->sig, (void *)args->nsa, (void *)args->osa);
649 if (args->nsa != NULL) {
650 error = copyin((caddr_t)args->nsa, &osa,
651 sizeof(l_osigaction_t));
654 act.lsa_handler = osa.lsa_handler;
655 act.lsa_flags = osa.lsa_flags;
656 act.lsa_restorer = osa.lsa_restorer;
657 LINUX_SIGEMPTYSET(act.lsa_mask);
658 act.lsa_mask.__bits[0] = osa.lsa_mask;
661 error = linux_do_sigaction(p, args->sig, args->nsa ? &act : NULL,
662 args->osa ? &oact : NULL);
664 if (args->osa != NULL && !error) {
665 osa.lsa_handler = oact.lsa_handler;
666 osa.lsa_flags = oact.lsa_flags;
667 osa.lsa_restorer = oact.lsa_restorer;
668 osa.lsa_mask = oact.lsa_mask.__bits[0];
669 error = copyout(&osa, (caddr_t)args->osa,
670 sizeof(l_osigaction_t));
677 * Linux has two extra args, restart and oldmask. We dont use these,
678 * but it seems that "restart" is actually a context pointer that
679 * enables the signal to happen with a different register set.
682 linux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args)
684 struct sigsuspend_args bsd;
687 caddr_t sg = stackgap_init();
690 if (ldebug(sigsuspend))
691 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
694 sigmask = stackgap_alloc(&sg, sizeof(sigset_t));
695 LINUX_SIGEMPTYSET(mask);
696 mask.__bits[0] = args->mask;
697 linux_to_bsd_sigset(&mask, sigmask);
698 bsd.sigmask = sigmask;
699 return (sigsuspend(p, &bsd));
703 linux_rt_sigsuspend(p, uap)
705 struct linux_rt_sigsuspend_args *uap;
709 struct sigsuspend_args bsd;
710 caddr_t sg = stackgap_init();
714 if (ldebug(rt_sigsuspend))
715 printf(ARGS(rt_sigsuspend, "%p, %d"),
716 (void *)uap->newset, uap->sigsetsize);
719 if (uap->sigsetsize != sizeof(l_sigset_t))
722 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
726 bmask = stackgap_alloc(&sg, sizeof(sigset_t));
727 linux_to_bsd_sigset(&lmask, bmask);
729 return (sigsuspend(p, &bsd));
733 linux_pause(struct proc *p, struct linux_pause_args *args)
735 struct sigsuspend_args bsd;
737 caddr_t sg = stackgap_init();
741 printf(ARGS(pause, ""));
744 sigmask = stackgap_alloc(&sg, sizeof(sigset_t));
745 *sigmask = p->p_sigmask;
746 bsd.sigmask = sigmask;
747 return (sigsuspend(p, &bsd));
751 linux_sigaltstack(struct proc *p, struct linux_sigaltstack_args *uap)
753 struct sigaltstack_args bsd;
757 caddr_t sg = stackgap_init();
760 if (ldebug(sigaltstack))
761 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
764 if (uap->uss == NULL) {
767 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
771 ss = stackgap_alloc(&sg, sizeof(stack_t));
772 ss->ss_sp = lss.ss_sp;
773 ss->ss_size = lss.ss_size;
774 ss->ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
776 oss = (uap->uoss != NULL)
777 ? stackgap_alloc(&sg, sizeof(stack_t))
782 error = sigaltstack(p, &bsd);
784 if (!error && oss != NULL) {
785 lss.ss_sp = oss->ss_sp;
786 lss.ss_size = oss->ss_size;
787 lss.ss_flags = bsd_to_linux_sigaltstack(oss->ss_flags);
788 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));