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.12 2003/11/15 03:52:33 daver 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);
123 error = exec_copyin_args(&exec_args, path, PATH_SYSSPACE,
124 args->argp, args->envp);
126 linux_free_path(&path);
130 error = kern_execve(&nd, &exec_args);
133 * The syscall result is returned in registers to the new program.
134 * Linux will register %edx as an atexit function and we must be
135 * sure to set it to 0. XXX
138 args->sysmsg_result64 = 0;
140 exec_free_args(&exec_args);
141 linux_free_path(&path);
145 struct l_ipc_kludge {
146 struct l_msgbuf *msgp;
151 linux_ipc(struct linux_ipc_args *args)
155 switch (args->what & 0xFFFF) {
157 struct linux_semop_args a;
159 a.semid = args->arg1;
161 a.nsops = args->arg2;
162 a.sysmsg_lresult = 0;
163 error = linux_semop(&a);
164 args->sysmsg_lresult = a.sysmsg_lresult;
168 struct linux_semget_args a;
171 a.nsems = args->arg2;
172 a.semflg = args->arg3;
173 a.sysmsg_lresult = 0;
174 error = linux_semget(&a);
175 args->sysmsg_lresult = a.sysmsg_lresult;
179 struct linux_semctl_args a;
182 a.semid = args->arg1;
183 a.semnum = args->arg2;
185 a.sysmsg_lresult = 0;
186 error = copyin((caddr_t)args->ptr, &a.arg, sizeof(a.arg));
189 error = linux_semctl(&a);
190 args->sysmsg_lresult = a.sysmsg_lresult;
194 struct linux_msgsnd_args a;
196 a.msqid = args->arg1;
198 a.msgsz = args->arg2;
199 a.msgflg = args->arg3;
200 a.sysmsg_lresult = 0;
201 error = linux_msgsnd(&a);
202 args->sysmsg_lresult = a.sysmsg_lresult;
206 struct linux_msgrcv_args a;
208 a.msqid = args->arg1;
209 a.msgsz = args->arg2;
210 a.msgflg = args->arg3;
211 a.sysmsg_lresult = 0;
212 if ((args->what >> 16) == 0) {
213 struct l_ipc_kludge tmp;
216 if (args->ptr == NULL)
218 error = copyin((caddr_t)args->ptr, &tmp, sizeof(tmp));
222 a.msgtyp = tmp.msgtyp;
225 a.msgtyp = args->arg5;
227 error = linux_msgrcv(&a);
228 args->sysmsg_lresult = a.sysmsg_lresult;
232 struct linux_msgget_args a;
235 a.msgflg = args->arg2;
236 a.sysmsg_lresult = 0;
237 error = linux_msgget(&a);
238 args->sysmsg_lresult = a.sysmsg_lresult;
242 struct linux_msgctl_args a;
244 a.msqid = args->arg1;
247 a.sysmsg_lresult = 0;
248 error = linux_msgctl(&a);
249 args->sysmsg_lresult = a.sysmsg_lresult;
253 struct linux_shmat_args a;
255 a.shmid = args->arg1;
256 a.shmaddr = args->ptr;
257 a.shmflg = args->arg2;
258 a.raddr = (l_ulong *)args->arg3;
259 a.sysmsg_lresult = 0;
260 error = linux_shmat(&a);
261 args->sysmsg_lresult = a.sysmsg_lresult;
265 struct linux_shmdt_args a;
267 a.shmaddr = args->ptr;
268 a.sysmsg_lresult = 0;
269 error = linux_shmdt(&a);
270 args->sysmsg_lresult = a.sysmsg_lresult;
274 struct linux_shmget_args a;
278 a.shmflg = args->arg3;
279 a.sysmsg_lresult = 0;
280 error = linux_shmget(&a);
281 args->sysmsg_lresult = a.sysmsg_lresult;
285 struct linux_shmctl_args a;
287 a.shmid = args->arg1;
290 a.sysmsg_lresult = 0;
291 error = linux_shmctl(&a);
292 args->sysmsg_lresult = a.sysmsg_lresult;
303 linux_old_select(struct linux_old_select_args *args)
305 struct l_old_select_argv linux_args;
306 struct linux_select_args newsel;
310 if (ldebug(old_select))
311 printf(ARGS(old_select, "%p"), args->ptr);
314 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
318 newsel.sysmsg_result = 0;
319 newsel.nfds = linux_args.nfds;
320 newsel.readfds = linux_args.readfds;
321 newsel.writefds = linux_args.writefds;
322 newsel.exceptfds = linux_args.exceptfds;
323 newsel.timeout = linux_args.timeout;
324 error = linux_select(&newsel);
325 args->sysmsg_result = newsel.sysmsg_result;
330 linux_fork(struct linux_fork_args *args)
336 printf(ARGS(fork, ""));
339 if ((error = fork((struct fork_args *)args)) != 0)
342 if (args->sysmsg_result == 1)
343 args->sysmsg_result = 0;
348 linux_vfork(struct linux_vfork_args *args)
354 printf(ARGS(vfork, ""));
357 if ((error = vfork((struct vfork_args *)args)) != 0)
359 /* Are we the child? */
360 if (args->sysmsg_result == 1)
361 args->sysmsg_result = 0;
365 #define CLONE_VM 0x100
366 #define CLONE_FS 0x200
367 #define CLONE_FILES 0x400
368 #define CLONE_SIGHAND 0x800
369 #define CLONE_PID 0x1000
372 linux_clone(struct linux_clone_args *args)
374 int error, ff = RFPROC;
378 struct rfork_args rf_args;
382 printf(ARGS(clone, "flags %x, stack %x"),
383 (unsigned int)args->flags, (unsigned int)args->stack);
384 if (args->flags & CLONE_PID)
385 printf(LMSG("CLONE_PID not yet supported"));
392 exit_signal = args->flags & 0x000000ff;
393 if (exit_signal >= LINUX_NSIG)
396 if (exit_signal <= LINUX_SIGTBLSZ)
397 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
399 /* RFTHREAD probably not necessary here, but it shouldn't hurt */
402 if (args->flags & CLONE_VM)
404 if (args->flags & CLONE_SIGHAND)
406 if (!(args->flags & CLONE_FILES))
413 rf_args.sysmsg_result = 0;
414 if ((error = rfork(&rf_args)) != 0)
416 args->sysmsg_result = rf_args.sysmsg_result;
418 p2 = pfind(rf_args.sysmsg_result);
422 p2->p_sigparent = exit_signal;
423 p2->p_md.md_regs->tf_esp = (unsigned int)args->stack;
427 printf(LMSG("clone: successful rfork to %ld"),
444 #define STACK_SIZE (2 * 1024 * 1024)
445 #define GUARD_SIZE (4 * PAGE_SIZE)
448 linux_mmap_common(caddr_t linux_addr, size_t linux_len, int linux_prot,
449 int linux_flags, int linux_fd, off_t pos, void **res)
451 struct thread *td = curthread;
452 struct proc *p = td->td_proc;
455 int error, flags, len, prot, fd;
458 if (linux_flags & LINUX_MAP_SHARED)
460 if (linux_flags & LINUX_MAP_PRIVATE)
461 flags |= MAP_PRIVATE;
462 if (linux_flags & LINUX_MAP_FIXED)
464 if (linux_flags & LINUX_MAP_ANON) {
469 if (linux_flags & LINUX_MAP_GROWSDOWN) {
471 /* The linux MAP_GROWSDOWN option does not limit auto
472 * growth of the region. Linux mmap with this option
473 * takes as addr the inital BOS, and as len, the initial
474 * region size. It can then grow down from addr without
475 * limit. However, linux threads has an implicit internal
476 * limit to stack size of STACK_SIZE. Its just not
477 * enforced explicitly in linux. But, here we impose
478 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
479 * region, since we can do this with our mmap.
481 * Our mmap with MAP_STACK takes addr as the maximum
482 * downsize limit on BOS, and as len the max size of
483 * the region. It them maps the top SGROWSIZ bytes,
484 * and autgrows the region down, up to the limit
487 * If we don't use the MAP_STACK option, the effect
488 * of this code is to allocate a stack region of a
489 * fixed size of (STACK_SIZE - GUARD_SIZE).
492 /* This gives us TOS */
493 addr = linux_addr + linux_len;
495 if (addr > p->p_vmspace->vm_maxsaddr) {
496 /* Some linux apps will attempt to mmap
497 * thread stacks near the top of their
498 * address space. If their TOS is greater
499 * than vm_maxsaddr, vm_map_growstack()
500 * will confuse the thread stack with the
501 * process stack and deliver a SEGV if they
502 * attempt to grow the thread stack past their
503 * current stacksize rlimit. To avoid this,
504 * adjust vm_maxsaddr upwards to reflect
505 * the current stacksize rlimit rather
506 * than the maximum possible stacksize.
507 * It would be better to adjust the
508 * mmap'ed region, but some apps do not check
509 * mmap's return value.
511 p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
512 p->p_rlimit[RLIMIT_STACK].rlim_cur;
515 /* This gives us our maximum stack size */
516 if (linux_len > STACK_SIZE - GUARD_SIZE) {
519 len = STACK_SIZE - GUARD_SIZE;
521 /* This gives us a new BOS. If we're using VM_STACK, then
522 * mmap will just map the top SGROWSIZ bytes, and let
523 * the stack grow down to the limit at BOS. If we're
524 * not using VM_STACK we map the full stack, since we
525 * don't have a way to autogrow it.
533 prot = linux_prot | PROT_READ;
534 if (linux_flags & LINUX_MAP_ANON) {
541 if (ldebug(mmap) || ldebug(mmap2))
542 printf("-> (%p, %d, %d, 0x%08x, %d, %lld)\n",
543 addr, len, prot, flags, fd, pos);
545 error = kern_mmap(addr, len, prot, flags, fd, pos, &new);
553 linux_mmap(struct linux_mmap_args *args)
555 struct l_mmap_argv linux_args;
558 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
564 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
565 (void *)linux_args.addr, linux_args.len, linux_args.prot,
566 linux_args.flags, linux_args.fd, linux_args.pos);
568 error = linux_mmap_common(linux_args.addr, linux_args.len,
569 linux_args.prot, linux_args.flags, linux_args.fd,
570 linux_args.pos, &args->sysmsg_resultp);
573 printf("-> %p\n", args->sysmsg_resultp);
579 linux_mmap2(struct linux_mmap2_args *args)
585 printf(ARGS(mmap2, "%p, %d, %d, 0x%08x, %d, %d"),
586 (void *)args->addr, args->len, args->prot, args->flags,
587 args->fd, args->pgoff);
589 error = linux_mmap_common((void *)args->addr, args->len, args->prot,
590 args->flags, args->fd, args->pgoff * PAGE_SIZE,
591 &args->sysmsg_resultp);
594 printf("-> %p\n", args->sysmsg_resultp);
600 linux_pipe(struct linux_pipe_args *args)
604 struct pipe_args bsd_args;
608 printf(ARGS(pipe, "*"));
611 reg_edx = args->sysmsg_fds[1];
612 error = pipe(&bsd_args);
614 args->sysmsg_fds[1] = reg_edx;
618 error = copyout(bsd_args.sysmsg_fds, args->pipefds, 2*sizeof(int));
620 args->sysmsg_fds[1] = reg_edx;
624 args->sysmsg_fds[1] = reg_edx;
625 args->sysmsg_fds[0] = 0;
630 linux_ioperm(struct linux_ioperm_args *args)
632 struct sysarch_args sa;
633 struct i386_ioperm_args *iia;
637 sg = stackgap_init();
638 iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
639 iia->start = args->start;
640 iia->length = args->length;
641 iia->enable = args->enable;
642 sa.sysmsg_resultp = NULL;
643 sa.op = I386_SET_IOPERM;
644 sa.parms = (char *)iia;
645 error = sysarch(&sa);
646 args->sysmsg_resultp = sa.sysmsg_resultp;
651 linux_iopl(struct linux_iopl_args *args)
653 struct thread *td = curthread;
654 struct proc *p = td->td_proc;
659 if (args->level < 0 || args->level > 3)
661 if ((error = suser(td)) != 0)
665 p->p_md.md_regs->tf_eflags = (p->p_md.md_regs->tf_eflags & ~PSL_IOPL) |
666 (args->level * (PSL_IOPL / 3));
671 linux_modify_ldt(struct linux_modify_ldt_args *uap)
675 struct sysarch_args args;
676 struct i386_ldt_args *ldt;
677 struct l_descriptor ld;
678 union descriptor *desc;
680 sg = stackgap_init();
682 if (uap->ptr == NULL)
686 case 0x00: /* read_ldt */
687 ldt = stackgap_alloc(&sg, sizeof(*ldt));
689 ldt->descs = uap->ptr;
690 ldt->num = uap->bytecount / sizeof(union descriptor);
691 args.op = I386_GET_LDT;
692 args.parms = (char*)ldt;
693 args.sysmsg_result = 0;
694 error = sysarch(&args);
695 uap->sysmsg_result = args.sysmsg_result *
696 sizeof(union descriptor);
698 case 0x01: /* write_ldt */
699 case 0x11: /* write_ldt */
700 if (uap->bytecount != sizeof(ld))
703 error = copyin(uap->ptr, &ld, sizeof(ld));
707 ldt = stackgap_alloc(&sg, sizeof(*ldt));
708 desc = stackgap_alloc(&sg, sizeof(*desc));
709 ldt->start = ld.entry_number;
712 desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
713 desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
714 desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
715 desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
716 desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
719 desc->sd.sd_p = (ld.seg_not_present ^ 1);
721 desc->sd.sd_def32 = ld.seg_32bit;
722 desc->sd.sd_gran = ld.limit_in_pages;
723 args.op = I386_SET_LDT;
724 args.parms = (char*)ldt;
725 args.sysmsg_result = 0;
726 error = sysarch(&args);
727 uap->sysmsg_result = args.sysmsg_result;
734 if (error == EOPNOTSUPP) {
735 printf("linux: modify_ldt needs kernel option USER_LDT\n");
743 linux_sigaction(struct linux_sigaction_args *args)
746 l_sigaction_t linux_act, linux_oact;
747 struct sigaction act, oact;
751 if (ldebug(sigaction))
752 printf(ARGS(sigaction, "%d, %p, %p"),
753 args->sig, (void *)args->nsa, (void *)args->osa);
757 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
760 linux_act.lsa_handler = osa.lsa_handler;
761 linux_act.lsa_flags = osa.lsa_flags;
762 linux_act.lsa_restorer = osa.lsa_restorer;
763 LINUX_SIGEMPTYSET(linux_act.lsa_mask);
764 linux_act.lsa_mask.__bits[0] = osa.lsa_mask;
765 linux_to_bsd_sigaction(&linux_act, &act);
768 error = kern_sigaction(args->sig, args->nsa ? &act : NULL,
769 args->osa ? &oact : NULL);
771 if (args->osa != NULL && !error) {
772 bsd_to_linux_sigaction(&oact, &linux_oact);
773 osa.lsa_handler = linux_oact.lsa_handler;
774 osa.lsa_flags = linux_oact.lsa_flags;
775 osa.lsa_restorer = linux_oact.lsa_restorer;
776 osa.lsa_mask = linux_oact.lsa_mask.__bits[0];
777 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
783 * Linux has two extra args, restart and oldmask. We dont use these,
784 * but it seems that "restart" is actually a context pointer that
785 * enables the signal to happen with a different register set.
788 linux_sigsuspend(struct linux_sigsuspend_args *args)
790 l_sigset_t linux_mask;
795 if (ldebug(sigsuspend))
796 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
799 LINUX_SIGEMPTYSET(mask);
800 mask.__bits[0] = args->mask;
801 linux_to_bsd_sigset(&linux_mask, &mask);
803 error = kern_sigsuspend(&mask);
809 linux_rt_sigsuspend(struct linux_rt_sigsuspend_args *uap)
811 l_sigset_t linux_mask;
816 if (ldebug(rt_sigsuspend))
817 printf(ARGS(rt_sigsuspend, "%p, %d"),
818 (void *)uap->newset, uap->sigsetsize);
821 if (uap->sigsetsize != sizeof(l_sigset_t))
824 error = copyin(uap->newset, &linux_mask, sizeof(l_sigset_t));
828 linux_to_bsd_sigset(&linux_mask, &mask);
830 error = kern_sigsuspend(&mask);
836 linux_pause(struct linux_pause_args *args)
838 struct thread *td = curthread;
839 struct proc *p = td->td_proc;
845 printf(ARGS(pause, ""));
850 error = kern_sigsuspend(&mask);
856 linux_sigaltstack(struct linux_sigaltstack_args *uap)
863 if (ldebug(sigaltstack))
864 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
868 error = copyin(uap->uss, &linux_ss, sizeof(l_stack_t));
872 ss.ss_sp = linux_ss.ss_sp;
873 ss.ss_size = linux_ss.ss_size;
874 ss.ss_flags = linux_to_bsd_sigaltstack(linux_ss.ss_flags);
877 error = kern_sigaltstack(uap->uss ? &ss : NULL,
878 uap->uoss ? &oss : NULL);
880 if (error == 0 && uap->uoss) {
881 linux_ss.ss_sp = oss.ss_sp;
882 linux_ss.ss_size = oss.ss_size;
883 linux_ss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
884 error = copyout(&linux_ss, uap->uoss, sizeof(l_stack_t));