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.9 2003/08/20 07:13:27 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>
52 #include "linux_proto.h"
53 #include "../linux_ipc.h"
54 #include "../linux_signal.h"
55 #include "../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);
117 * Note: inherit and set the full 64 bit syscall return
118 * value so a successful execve() sets %edx to 0.
120 bsd.sysmsg_result64 = args->sysmsg_result64;
121 bsd.fname = args->path;
122 bsd.argv = args->argp;
123 bsd.envv = args->envp;
124 error = execve(&bsd);
125 args->sysmsg_result64 = bsd.sysmsg_result64;
129 struct l_ipc_kludge {
130 struct l_msgbuf *msgp;
135 linux_ipc(struct linux_ipc_args *args)
139 switch (args->what & 0xFFFF) {
141 struct linux_semop_args a;
143 a.semid = args->arg1;
145 a.nsops = args->arg2;
146 a.sysmsg_lresult = 0;
147 error = linux_semop(&a);
148 args->sysmsg_lresult = a.sysmsg_lresult;
152 struct linux_semget_args a;
155 a.nsems = args->arg2;
156 a.semflg = args->arg3;
157 a.sysmsg_lresult = 0;
158 error = linux_semget(&a);
159 args->sysmsg_lresult = a.sysmsg_lresult;
163 struct linux_semctl_args a;
166 a.semid = args->arg1;
167 a.semnum = args->arg2;
169 a.sysmsg_lresult = 0;
170 error = copyin((caddr_t)args->ptr, &a.arg, sizeof(a.arg));
173 error = linux_semctl(&a);
174 args->sysmsg_lresult = a.sysmsg_lresult;
178 struct linux_msgsnd_args a;
180 a.msqid = args->arg1;
182 a.msgsz = args->arg2;
183 a.msgflg = args->arg3;
184 a.sysmsg_lresult = 0;
185 error = linux_msgsnd(&a);
186 args->sysmsg_lresult = a.sysmsg_lresult;
190 struct linux_msgrcv_args a;
192 a.msqid = args->arg1;
193 a.msgsz = args->arg2;
194 a.msgflg = args->arg3;
195 a.sysmsg_lresult = 0;
196 if ((args->what >> 16) == 0) {
197 struct l_ipc_kludge tmp;
200 if (args->ptr == NULL)
202 error = copyin((caddr_t)args->ptr, &tmp, sizeof(tmp));
206 a.msgtyp = tmp.msgtyp;
209 a.msgtyp = args->arg5;
211 error = linux_msgrcv(&a);
212 args->sysmsg_lresult = a.sysmsg_lresult;
216 struct linux_msgget_args a;
219 a.msgflg = args->arg2;
220 a.sysmsg_lresult = 0;
221 error = linux_msgget(&a);
222 args->sysmsg_lresult = a.sysmsg_lresult;
226 struct linux_msgctl_args a;
228 a.msqid = args->arg1;
231 a.sysmsg_lresult = 0;
232 error = linux_msgctl(&a);
233 args->sysmsg_lresult = a.sysmsg_lresult;
237 struct linux_shmat_args a;
239 a.shmid = args->arg1;
240 a.shmaddr = args->ptr;
241 a.shmflg = args->arg2;
242 a.raddr = (l_ulong *)args->arg3;
243 a.sysmsg_lresult = 0;
244 error = linux_shmat(&a);
245 args->sysmsg_lresult = a.sysmsg_lresult;
249 struct linux_shmdt_args a;
251 a.shmaddr = args->ptr;
252 a.sysmsg_lresult = 0;
253 error = linux_shmdt(&a);
254 args->sysmsg_lresult = a.sysmsg_lresult;
258 struct linux_shmget_args a;
262 a.shmflg = args->arg3;
263 a.sysmsg_lresult = 0;
264 error = linux_shmget(&a);
265 args->sysmsg_lresult = a.sysmsg_lresult;
269 struct linux_shmctl_args a;
271 a.shmid = args->arg1;
274 a.sysmsg_lresult = 0;
275 error = linux_shmctl(&a);
276 args->sysmsg_lresult = a.sysmsg_lresult;
287 linux_old_select(struct linux_old_select_args *args)
289 struct l_old_select_argv linux_args;
290 struct linux_select_args newsel;
294 if (ldebug(old_select))
295 printf(ARGS(old_select, "%p"), args->ptr);
298 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
302 newsel.sysmsg_result = 0;
303 newsel.nfds = linux_args.nfds;
304 newsel.readfds = linux_args.readfds;
305 newsel.writefds = linux_args.writefds;
306 newsel.exceptfds = linux_args.exceptfds;
307 newsel.timeout = linux_args.timeout;
308 error = linux_select(&newsel);
309 args->sysmsg_result = newsel.sysmsg_result;
314 linux_fork(struct linux_fork_args *args)
320 printf(ARGS(fork, ""));
323 if ((error = fork((struct fork_args *)args)) != 0)
326 if (args->sysmsg_result == 1)
327 args->sysmsg_result = 0;
332 linux_vfork(struct linux_vfork_args *args)
338 printf(ARGS(vfork, ""));
341 if ((error = vfork((struct vfork_args *)args)) != 0)
343 /* Are we the child? */
344 if (args->sysmsg_result == 1)
345 args->sysmsg_result = 0;
349 #define CLONE_VM 0x100
350 #define CLONE_FS 0x200
351 #define CLONE_FILES 0x400
352 #define CLONE_SIGHAND 0x800
353 #define CLONE_PID 0x1000
356 linux_clone(struct linux_clone_args *args)
358 int error, ff = RFPROC;
362 struct rfork_args rf_args;
366 printf(ARGS(clone, "flags %x, stack %x"),
367 (unsigned int)args->flags, (unsigned int)args->stack);
368 if (args->flags & CLONE_PID)
369 printf(LMSG("CLONE_PID not yet supported"));
376 exit_signal = args->flags & 0x000000ff;
377 if (exit_signal >= LINUX_NSIG)
380 if (exit_signal <= LINUX_SIGTBLSZ)
381 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
383 /* RFTHREAD probably not necessary here, but it shouldn't hurt */
386 if (args->flags & CLONE_VM)
388 if (args->flags & CLONE_SIGHAND)
390 if (!(args->flags & CLONE_FILES))
397 rf_args.sysmsg_result = 0;
398 if ((error = rfork(&rf_args)) != 0)
400 args->sysmsg_result = rf_args.sysmsg_result;
402 p2 = pfind(rf_args.sysmsg_result);
406 p2->p_sigparent = exit_signal;
407 p2->p_md.md_regs->tf_esp = (unsigned int)args->stack;
411 printf(LMSG("clone: successful rfork to %ld"),
428 #define STACK_SIZE (2 * 1024 * 1024)
429 #define GUARD_SIZE (4 * PAGE_SIZE)
432 linux_mmap(struct linux_mmap_args *args)
434 struct proc *p = curproc;
435 struct mmap_args bsd_args;
437 struct l_mmap_argv linux_args;
439 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
445 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
446 (void *)linux_args.addr, linux_args.len, linux_args.prot,
447 linux_args.flags, linux_args.fd, linux_args.pos);
451 bsd_args.sysmsg_resultp = NULL;
452 if (linux_args.flags & LINUX_MAP_SHARED)
453 bsd_args.flags |= MAP_SHARED;
454 if (linux_args.flags & LINUX_MAP_PRIVATE)
455 bsd_args.flags |= MAP_PRIVATE;
456 if (linux_args.flags & LINUX_MAP_FIXED)
457 bsd_args.flags |= MAP_FIXED;
458 if (linux_args.flags & LINUX_MAP_ANON)
459 bsd_args.flags |= MAP_ANON;
461 bsd_args.flags |= MAP_NOSYNC;
462 if (linux_args.flags & LINUX_MAP_GROWSDOWN) {
463 bsd_args.flags |= MAP_STACK;
465 /* The linux MAP_GROWSDOWN option does not limit auto
466 * growth of the region. Linux mmap with this option
467 * takes as addr the inital BOS, and as len, the initial
468 * region size. It can then grow down from addr without
469 * limit. However, linux threads has an implicit internal
470 * limit to stack size of STACK_SIZE. Its just not
471 * enforced explicitly in linux. But, here we impose
472 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
473 * region, since we can do this with our mmap.
475 * Our mmap with MAP_STACK takes addr as the maximum
476 * downsize limit on BOS, and as len the max size of
477 * the region. It them maps the top SGROWSIZ bytes,
478 * and autgrows the region down, up to the limit
481 * If we don't use the MAP_STACK option, the effect
482 * of this code is to allocate a stack region of a
483 * fixed size of (STACK_SIZE - GUARD_SIZE).
486 /* This gives us TOS */
487 bsd_args.addr = linux_args.addr + linux_args.len;
489 if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) {
490 /* Some linux apps will attempt to mmap
491 * thread stacks near the top of their
492 * address space. If their TOS is greater
493 * than vm_maxsaddr, vm_map_growstack()
494 * will confuse the thread stack with the
495 * process stack and deliver a SEGV if they
496 * attempt to grow the thread stack past their
497 * current stacksize rlimit. To avoid this,
498 * adjust vm_maxsaddr upwards to reflect
499 * the current stacksize rlimit rather
500 * than the maximum possible stacksize.
501 * It would be better to adjust the
502 * mmap'ed region, but some apps do not check
503 * mmap's return value.
505 p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
506 p->p_rlimit[RLIMIT_STACK].rlim_cur;
509 /* This gives us our maximum stack size */
510 if (linux_args.len > STACK_SIZE - GUARD_SIZE)
511 bsd_args.len = linux_args.len;
513 bsd_args.len = STACK_SIZE - GUARD_SIZE;
515 /* This gives us a new BOS. If we're using VM_STACK, then
516 * mmap will just map the top SGROWSIZ bytes, and let
517 * the stack grow down to the limit at BOS. If we're
518 * not using VM_STACK we map the full stack, since we
519 * don't have a way to autogrow it.
521 bsd_args.addr -= bsd_args.len;
523 bsd_args.addr = linux_args.addr;
524 bsd_args.len = linux_args.len;
527 bsd_args.prot = linux_args.prot | PROT_READ; /* always required */
528 if (linux_args.flags & LINUX_MAP_ANON)
531 bsd_args.fd = linux_args.fd;
532 bsd_args.pos = linux_args.pos;
537 printf("-> (%p, %d, %d, 0x%08x, %d, %d)\n",
538 (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
539 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
542 error = mmap(&bsd_args);
543 args->sysmsg_resultp = bsd_args.sysmsg_resultp;
548 linux_pipe(struct linux_pipe_args *args)
552 struct pipe_args bsd_args;
556 printf(ARGS(pipe, "*"));
559 reg_edx = args->sysmsg_fds[1];
560 error = pipe(&bsd_args);
562 args->sysmsg_fds[1] = reg_edx;
566 error = copyout(bsd_args.sysmsg_fds, args->pipefds, 2*sizeof(int));
568 args->sysmsg_fds[1] = reg_edx;
572 args->sysmsg_fds[1] = reg_edx;
573 args->sysmsg_fds[0] = 0;
578 linux_ioperm(struct linux_ioperm_args *args)
580 struct sysarch_args sa;
581 struct i386_ioperm_args *iia;
585 sg = stackgap_init();
586 iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
587 iia->start = args->start;
588 iia->length = args->length;
589 iia->enable = args->enable;
590 sa.sysmsg_resultp = NULL;
591 sa.op = I386_SET_IOPERM;
592 sa.parms = (char *)iia;
593 error = sysarch(&sa);
594 args->sysmsg_resultp = sa.sysmsg_resultp;
599 linux_iopl(struct linux_iopl_args *args)
601 struct thread *td = curthread;
602 struct proc *p = td->td_proc;
607 if (args->level < 0 || args->level > 3)
609 if ((error = suser(td)) != 0)
613 p->p_md.md_regs->tf_eflags = (p->p_md.md_regs->tf_eflags & ~PSL_IOPL) |
614 (args->level * (PSL_IOPL / 3));
619 linux_modify_ldt(struct linux_modify_ldt_args *uap)
623 struct sysarch_args args;
624 struct i386_ldt_args *ldt;
625 struct l_descriptor ld;
626 union descriptor *desc;
628 sg = stackgap_init();
630 if (uap->ptr == NULL)
634 case 0x00: /* read_ldt */
635 ldt = stackgap_alloc(&sg, sizeof(*ldt));
637 ldt->descs = uap->ptr;
638 ldt->num = uap->bytecount / sizeof(union descriptor);
639 args.op = I386_GET_LDT;
640 args.parms = (char*)ldt;
641 args.sysmsg_result = 0;
642 error = sysarch(&args);
643 uap->sysmsg_result = args.sysmsg_result *
644 sizeof(union descriptor);
646 case 0x01: /* write_ldt */
647 case 0x11: /* write_ldt */
648 if (uap->bytecount != sizeof(ld))
651 error = copyin(uap->ptr, &ld, sizeof(ld));
655 ldt = stackgap_alloc(&sg, sizeof(*ldt));
656 desc = stackgap_alloc(&sg, sizeof(*desc));
657 ldt->start = ld.entry_number;
660 desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
661 desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
662 desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
663 desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
664 desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
667 desc->sd.sd_p = (ld.seg_not_present ^ 1);
669 desc->sd.sd_def32 = ld.seg_32bit;
670 desc->sd.sd_gran = ld.limit_in_pages;
671 args.op = I386_SET_LDT;
672 args.parms = (char*)ldt;
673 args.sysmsg_result = 0;
674 error = sysarch(&args);
675 uap->sysmsg_result = args.sysmsg_result;
682 if (error == EOPNOTSUPP) {
683 printf("linux: modify_ldt needs kernel option USER_LDT\n");
691 linux_sigaction(struct linux_sigaction_args *args)
694 l_sigaction_t act, oact;
698 if (ldebug(sigaction))
699 printf(ARGS(sigaction, "%d, %p, %p"),
700 args->sig, (void *)args->nsa, (void *)args->osa);
703 if (args->nsa != NULL) {
704 error = copyin((caddr_t)args->nsa, &osa,
705 sizeof(l_osigaction_t));
708 act.lsa_handler = osa.lsa_handler;
709 act.lsa_flags = osa.lsa_flags;
710 act.lsa_restorer = osa.lsa_restorer;
711 LINUX_SIGEMPTYSET(act.lsa_mask);
712 act.lsa_mask.__bits[0] = osa.lsa_mask;
715 error = linux_do_sigaction(args->sig, args->nsa ? &act : NULL,
716 args->osa ? &oact : NULL, &args->sysmsg_result);
718 if (args->osa != NULL && !error) {
719 osa.lsa_handler = oact.lsa_handler;
720 osa.lsa_flags = oact.lsa_flags;
721 osa.lsa_restorer = oact.lsa_restorer;
722 osa.lsa_mask = oact.lsa_mask.__bits[0];
723 error = copyout(&osa, (caddr_t)args->osa,
724 sizeof(l_osigaction_t));
731 * Linux has two extra args, restart and oldmask. We dont use these,
732 * but it seems that "restart" is actually a context pointer that
733 * enables the signal to happen with a different register set.
736 linux_sigsuspend(struct linux_sigsuspend_args *args)
738 struct sigsuspend_args bsd;
741 caddr_t sg = stackgap_init();
745 if (ldebug(sigsuspend))
746 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
749 sigmask = stackgap_alloc(&sg, sizeof(sigset_t));
750 LINUX_SIGEMPTYSET(mask);
751 mask.__bits[0] = args->mask;
752 linux_to_bsd_sigset(&mask, sigmask);
753 bsd.sigmask = sigmask;
754 bsd.sysmsg_result = 0;
755 error = sigsuspend(&bsd);
756 args->sysmsg_result = bsd.sysmsg_result;
761 linux_rt_sigsuspend(struct linux_rt_sigsuspend_args *uap)
765 struct sigsuspend_args bsd;
766 caddr_t sg = stackgap_init();
770 if (ldebug(rt_sigsuspend))
771 printf(ARGS(rt_sigsuspend, "%p, %d"),
772 (void *)uap->newset, uap->sigsetsize);
775 if (uap->sigsetsize != sizeof(l_sigset_t))
778 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t));
782 bmask = stackgap_alloc(&sg, sizeof(sigset_t));
783 linux_to_bsd_sigset(&lmask, bmask);
785 bsd.sysmsg_result = 0;
786 error = sigsuspend(&bsd);
787 uap->sysmsg_result = bsd.sysmsg_result;
792 linux_pause(struct linux_pause_args *args)
794 struct proc *p = curproc;
795 struct sigsuspend_args bsd;
797 caddr_t sg = stackgap_init();
802 printf(ARGS(pause, ""));
805 sigmask = stackgap_alloc(&sg, sizeof(sigset_t));
806 *sigmask = p->p_sigmask;
807 bsd.sigmask = sigmask;
808 bsd.sysmsg_result = 0;
809 error = sigsuspend(&bsd);
810 args->sysmsg_result = bsd.sysmsg_result;
815 linux_sigaltstack(struct linux_sigaltstack_args *uap)
817 struct sigaltstack_args bsd;
821 caddr_t sg = stackgap_init();
824 if (ldebug(sigaltstack))
825 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
828 if (uap->uss == NULL) {
831 error = copyin(uap->uss, &lss, sizeof(l_stack_t));
835 ss = stackgap_alloc(&sg, sizeof(stack_t));
836 ss->ss_sp = lss.ss_sp;
837 ss->ss_size = lss.ss_size;
838 ss->ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags);
840 oss = (uap->uoss != NULL)
841 ? stackgap_alloc(&sg, sizeof(stack_t))
846 bsd.sysmsg_result = 0;
847 error = sigaltstack(&bsd);
848 uap->sysmsg_result = bsd.sysmsg_result;
850 if (!error && oss != NULL) {
851 lss.ss_sp = oss->ss_sp;
852 lss.ss_size = oss->ss_size;
853 lss.ss_flags = bsd_to_linux_sigaltstack(oss->ss_flags);
854 error = copyout(&lss, uap->uoss, sizeof(l_stack_t));