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>
36 #include <sys/resource.h>
37 #include <sys/resourcevar.h>
38 #include <sys/sysproto.h>
39 #include <sys/unistd.h>
41 #include <machine/frame.h>
42 #include <machine/psl.h>
43 #include <machine/segments.h>
44 #include <machine/sysarch.h>
48 #include <vm/vm_map.h>
50 #include <i386/linux/linux.h>
51 #include <i386/linux/linux_proto.h>
52 #include <compat/linux/linux_ipc.h>
53 #include <compat/linux/linux_signal.h>
54 #include <compat/linux/linux_util.h>
62 l_uint read_exec_only:1;
63 l_uint limit_in_pages:1;
64 l_uint seg_not_present:1;
68 struct l_old_select_argv {
73 struct l_timeval *timeout;
77 linux_to_bsd_sigaltstack(int lsa)
81 if (lsa & LINUX_SS_DISABLE)
83 if (lsa & LINUX_SS_ONSTACK)
89 bsd_to_linux_sigaltstack(int bsa)
94 lsa |= LINUX_SS_DISABLE;
96 lsa |= LINUX_SS_ONSTACK;
101 linux_execve(struct proc *p, struct linux_execve_args *args)
103 struct execve_args bsd;
106 sg = stackgap_init();
107 CHECKALTEXIST(p, &sg, args->path);
111 printf(ARGS(execve, "%s"), args->path);
114 bsd.fname = args->path;
115 bsd.argv = args->argp;
116 bsd.envv = args->envp;
117 return (execve(p, &bsd));
120 struct l_ipc_kludge {
121 struct l_msgbuf *msgp;
126 linux_ipc(struct proc *p, struct linux_ipc_args *args)
129 switch (args->what & 0xFFFF) {
131 struct linux_semop_args a;
133 a.semid = args->arg1;
135 a.nsops = args->arg2;
136 return (linux_semop(p, &a));
139 struct linux_semget_args a;
142 a.nsems = args->arg2;
143 a.semflg = args->arg3;
144 return (linux_semget(p, &a));
147 struct linux_semctl_args a;
150 a.semid = args->arg1;
151 a.semnum = args->arg2;
153 error = copyin((caddr_t)args->ptr, &a.arg, sizeof(a.arg));
156 return (linux_semctl(p, &a));
159 struct linux_msgsnd_args a;
161 a.msqid = args->arg1;
163 a.msgsz = args->arg2;
164 a.msgflg = args->arg3;
165 return (linux_msgsnd(p, &a));
168 struct linux_msgrcv_args a;
170 a.msqid = args->arg1;
171 a.msgsz = args->arg2;
172 a.msgflg = args->arg3;
173 if ((args->what >> 16) == 0) {
174 struct l_ipc_kludge tmp;
177 if (args->ptr == NULL)
179 error = copyin((caddr_t)args->ptr, &tmp, sizeof(tmp));
183 a.msgtyp = tmp.msgtyp;
186 a.msgtyp = args->arg5;
188 return (linux_msgrcv(p, &a));
191 struct linux_msgget_args a;
194 a.msgflg = args->arg2;
195 return (linux_msgget(p, &a));
198 struct linux_msgctl_args a;
200 a.msqid = args->arg1;
203 return (linux_msgctl(p, &a));
206 struct linux_shmat_args a;
208 a.shmid = args->arg1;
209 a.shmaddr = args->ptr;
210 a.shmflg = args->arg2;
211 a.raddr = (l_ulong *)args->arg3;
212 return (linux_shmat(p, &a));
215 struct linux_shmdt_args a;
217 a.shmaddr = args->ptr;
218 return (linux_shmdt(p, &a));
221 struct linux_shmget_args a;
225 a.shmflg = args->arg3;
226 return (linux_shmget(p, &a));
229 struct linux_shmctl_args a;
231 a.shmid = args->arg1;
234 return (linux_shmctl(p, &a));
244 linux_old_select(struct proc *p, struct linux_old_select_args *args)
246 struct l_old_select_argv linux_args;
247 struct linux_select_args newsel;
251 if (ldebug(old_select))
252 printf(ARGS(old_select, "%x"), args->ptr);
255 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
259 newsel.nfds = linux_args.nfds;
260 newsel.readfds = linux_args.readfds;
261 newsel.writefds = linux_args.writefds;
262 newsel.exceptfds = linux_args.exceptfds;
263 newsel.timeout = linux_args.timeout;
264 return (linux_select(p, &newsel));
268 linux_fork(struct proc *p, struct linux_fork_args *args)
274 printf(ARGS(fork, ""));
277 if ((error = fork(p, (struct fork_args *)args)) != 0)
280 if (p->p_retval[1] == 1)
286 linux_vfork(struct proc *p, struct linux_vfork_args *args)
292 printf(ARGS(vfork, ""));
295 if ((error = vfork(p, (struct vfork_args *)args)) != 0)
297 /* Are we the child? */
298 if (p->p_retval[1] == 1)
303 #define CLONE_VM 0x100
304 #define CLONE_FS 0x200
305 #define CLONE_FILES 0x400
306 #define CLONE_SIGHAND 0x800
307 #define CLONE_PID 0x1000
310 linux_clone(struct proc *p, struct linux_clone_args *args)
312 int error, ff = RFPROC;
316 struct rfork_args rf_args;
320 printf(ARGS(clone, "flags %x, stack %x"),
321 (unsigned int)args->flags, (unsigned int)args->stack);
322 if (args->flags & CLONE_PID)
323 printf(LMSG("CLONE_PID not yet supported"));
330 exit_signal = args->flags & 0x000000ff;
331 if (exit_signal >= LINUX_NSIG)
334 if (exit_signal <= LINUX_SIGTBLSZ)
335 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
337 /* RFTHREAD probably not necessary here, but it shouldn't hurt */
340 if (args->flags & CLONE_VM)
342 if (args->flags & CLONE_SIGHAND)
344 if (!(args->flags & CLONE_FILES))
351 if ((error = rfork(p, &rf_args)) != 0)
354 p2 = pfind(p->p_retval[0]);
358 p2->p_sigparent = exit_signal;
359 p2->p_md.md_regs->tf_esp = (unsigned int)args->stack;
363 printf(LMSG("clone: successful rfork to %ld"),
380 #define STACK_SIZE (2 * 1024 * 1024)
381 #define GUARD_SIZE (4 * PAGE_SIZE)
384 linux_mmap(struct proc *p, struct linux_mmap_args *args)
386 struct mmap_args /* {
396 struct l_mmap_argv linux_args;
398 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
404 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
405 (void *)linux_args.addr, linux_args.len, linux_args.prot,
406 linux_args.flags, linux_args.fd, linux_args.pos);
410 if (linux_args.flags & LINUX_MAP_SHARED)
411 bsd_args.flags |= MAP_SHARED;
412 if (linux_args.flags & LINUX_MAP_PRIVATE)
413 bsd_args.flags |= MAP_PRIVATE;
414 if (linux_args.flags & LINUX_MAP_FIXED)
415 bsd_args.flags |= MAP_FIXED;
416 if (linux_args.flags & LINUX_MAP_ANON)
417 bsd_args.flags |= MAP_ANON;
419 bsd_args.flags |= MAP_NOSYNC;
420 if (linux_args.flags & LINUX_MAP_GROWSDOWN) {
421 bsd_args.flags |= MAP_STACK;
423 /* The linux MAP_GROWSDOWN option does not limit auto
424 * growth of the region. Linux mmap with this option
425 * takes as addr the inital BOS, and as len, the initial
426 * region size. It can then grow down from addr without
427 * limit. However, linux threads has an implicit internal
428 * limit to stack size of STACK_SIZE. Its just not
429 * enforced explicitly in linux. But, here we impose
430 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
431 * region, since we can do this with our mmap.
433 * Our mmap with MAP_STACK takes addr as the maximum
434 * downsize limit on BOS, and as len the max size of
435 * the region. It them maps the top SGROWSIZ bytes,
436 * and autgrows the region down, up to the limit
439 * If we don't use the MAP_STACK option, the effect
440 * of this code is to allocate a stack region of a
441 * fixed size of (STACK_SIZE - GUARD_SIZE).
444 /* This gives us TOS */
445 bsd_args.addr = linux_args.addr + linux_args.len;
447 if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) {
448 /* Some linux apps will attempt to mmap
449 * thread stacks near the top of their
450 * address space. If their TOS is greater
451 * than vm_maxsaddr, vm_map_growstack()
452 * will confuse the thread stack with the
453 * process stack and deliver a SEGV if they
454 * attempt to grow the thread stack past their
455 * current stacksize rlimit. To avoid this,
456 * adjust vm_maxsaddr upwards to reflect
457 * the current stacksize rlimit rather
458 * than the maximum possible stacksize.
459 * It would be better to adjust the
460 * mmap'ed region, but some apps do not check
461 * mmap's return value.
463 p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
464 p->p_rlimit[RLIMIT_STACK].rlim_cur;
467 /* This gives us our maximum stack size */
468 if (linux_args.len > STACK_SIZE - GUARD_SIZE)
469 bsd_args.len = linux_args.len;
471 bsd_args.len = STACK_SIZE - GUARD_SIZE;
473 /* This gives us a new BOS. If we're using VM_STACK, then
474 * mmap will just map the top SGROWSIZ bytes, and let
475 * the stack grow down to the limit at BOS. If we're
476 * not using VM_STACK we map the full stack, since we
477 * don't have a way to autogrow it.
479 bsd_args.addr -= bsd_args.len;
481 bsd_args.addr = linux_args.addr;
482 bsd_args.len = linux_args.len;
485 bsd_args.prot = linux_args.prot | PROT_READ; /* always required */
486 if (linux_args.flags & LINUX_MAP_ANON)
489 bsd_args.fd = linux_args.fd;
490 bsd_args.pos = linux_args.pos;
495 printf("-> (%p, %d, %d, 0x%08x, %d, %d)\n",
496 (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
497 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
500 return (mmap(p, &bsd_args));
504 linux_pipe(struct proc *p, struct linux_pipe_args *args)
511 printf(ARGS(pipe, "*"));
514 reg_edx = p->p_retval[1];
517 p->p_retval[1] = reg_edx;
521 error = copyout(p->p_retval, args->pipefds, 2*sizeof(int));
523 p->p_retval[1] = reg_edx;
527 p->p_retval[1] = reg_edx;
533 linux_ioperm(struct proc *p, struct linux_ioperm_args *args)
535 struct sysarch_args sa;
536 struct i386_ioperm_args *iia;
539 sg = stackgap_init();
540 iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
541 iia->start = args->start;
542 iia->length = args->length;
543 iia->enable = args->enable;
544 sa.op = I386_SET_IOPERM;
545 sa.parms = (char *)iia;
546 return (sysarch(p, &sa));
550 linux_iopl(struct proc *p, struct linux_iopl_args *args)
554 if (args->level < 0 || args->level > 3)
556 if ((error = suser(p)) != 0)
560 p->p_md.md_regs->tf_eflags = (p->p_md.md_regs->tf_eflags & ~PSL_IOPL) |
561 (args->level * (PSL_IOPL / 3));
566 linux_modify_ldt(p, uap)
568 struct linux_modify_ldt_args *uap;
572 struct sysarch_args args;
573 struct i386_ldt_args *ldt;
574 struct l_descriptor ld;
575 union descriptor *desc;
577 sg = stackgap_init();
579 if (uap->ptr == NULL)
583 case 0x00: /* read_ldt */
584 ldt = stackgap_alloc(&sg, sizeof(*ldt));
586 ldt->descs = uap->ptr;
587 ldt->num = uap->bytecount / sizeof(union descriptor);
588 args.op = I386_GET_LDT;
589 args.parms = (char*)ldt;
590 error = sysarch(p, &args);
591 p->p_retval[0] *= sizeof(union descriptor);
593 case 0x01: /* write_ldt */
594 case 0x11: /* write_ldt */
595 if (uap->bytecount != sizeof(ld))
598 error = copyin(uap->ptr, &ld, sizeof(ld));
602 ldt = stackgap_alloc(&sg, sizeof(*ldt));
603 desc = stackgap_alloc(&sg, sizeof(*desc));
604 ldt->start = ld.entry_number;
607 desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
608 desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
609 desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
610 desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
611 desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
614 desc->sd.sd_p = (ld.seg_not_present ^ 1);
616 desc->sd.sd_def32 = ld.seg_32bit;
617 desc->sd.sd_gran = ld.limit_in_pages;
618 args.op = I386_SET_LDT;
619 args.parms = (char*)ldt;
620 error = sysarch(p, &args);
627 if (error == EOPNOTSUPP) {
628 printf("linux: modify_ldt needs kernel option USER_LDT\n");
636 linux_sigaction(struct proc *p, struct linux_sigaction_args *args)
639 l_sigaction_t act, oact;
643 if (ldebug(sigaction))
644 printf(ARGS(sigaction, "%d, %p, %p"),
645 args->sig, (void *)args->nsa, (void *)args->osa);
648 if (args->nsa != NULL) {
649 error = copyin((caddr_t)args->nsa, &osa,
650 sizeof(l_osigaction_t));
653 act.lsa_handler = osa.lsa_handler;
654 act.lsa_flags = osa.lsa_flags;
655 act.lsa_restorer = osa.lsa_restorer;
656 LINUX_SIGEMPTYSET(act.lsa_mask);
657 act.lsa_mask.__bits[0] = osa.lsa_mask;
660 error = linux_do_sigaction(p, args->sig, args->nsa ? &act : NULL,
661 args->osa ? &oact : NULL);
663 if (args->osa != NULL && !error) {
664 osa.lsa_handler = oact.lsa_handler;
665 osa.lsa_flags = oact.lsa_flags;
666 osa.lsa_restorer = oact.lsa_restorer;
667 osa.lsa_mask = oact.lsa_mask.__bits[0];
668 error = copyout(&osa, (caddr_t)args->osa,
669 sizeof(l_osigaction_t));
676 * Linux has two extra args, restart and oldmask. We dont use these,
677 * but it seems that "restart" is actually a context pointer that
678 * enables the signal to happen with a different register set.
681 linux_sigsuspend(struct proc *p, struct linux_sigsuspend_args *args)
683 struct sigsuspend_args bsd;
686 caddr_t sg = stackgap_init();
689 if (ldebug(sigsuspend))
690 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
693 sigmask = stackgap_alloc(&sg, sizeof(sigset_t));
694 LINUX_SIGEMPTYSET(mask);
695 mask.__bits[0] = args->mask;
696 linux_to_bsd_sigset(&mask, sigmask);
697 bsd.sigmask = sigmask;
698 return (sigsuspend(p, &bsd));
702 linux_rt_sigsuspend(p, uap)
704 struct linux_rt_sigsuspend_args *uap;
708 struct sigsuspend_args bsd;
709 caddr_t sg = stackgap_init();
713 if (ldebug(rt_sigsuspend))
714 printf(ARGS(rt_sigsuspend, "%p, %d"),
715 (void *)uap->newset, uap->sigsetsize);
718 if (uap->sigsetsize != sizeof(l_sigset_t))
721 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
725 bmask = stackgap_alloc(&sg, sizeof(sigset_t));
726 linux_to_bsd_sigset(&lmask, bmask);
728 return (sigsuspend(p, &bsd));
732 linux_pause(struct proc *p, struct linux_pause_args *args)
734 struct sigsuspend_args bsd;
736 caddr_t sg = stackgap_init();
740 printf(ARGS(pause, ""));
743 sigmask = stackgap_alloc(&sg, sizeof(sigset_t));
744 *sigmask = p->p_sigmask;
745 bsd.sigmask = sigmask;
746 return (sigsuspend(p, &bsd));
750 linux_sigaltstack(struct proc *p, struct linux_sigaltstack_args *uap)
752 struct sigaltstack_args bsd;
756 caddr_t sg = stackgap_init();
759 if (ldebug(sigaltstack))
760 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
763 if (uap->uss == NULL) {
766 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
770 ss = stackgap_alloc(&sg, sizeof(stack_t));
771 ss->ss_sp = lss.ss_sp;
772 ss->ss_size = lss.ss_size;
773 ss->ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
775 oss = (uap->uoss != NULL)
776 ? stackgap_alloc(&sg, sizeof(stack_t))
781 error = sigaltstack(p, &bsd);
783 if (!error && oss != NULL) {
784 lss.ss_sp = oss->ss_sp;
785 lss.ss_size = oss->ss_size;
786 lss.ss_flags = bsd_to_linux_sigaltstack(oss->ss_flags);
787 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));