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.10 2003/10/24 14:10:45 daver Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kern_syscall.h>
38 #include <sys/resource.h>
39 #include <sys/resourcevar.h>
40 #include <sys/sysproto.h>
41 #include <sys/unistd.h>
43 #include <machine/frame.h>
44 #include <machine/psl.h>
45 #include <machine/segments.h>
46 #include <machine/sysarch.h>
50 #include <vm/vm_map.h>
53 #include "linux_proto.h"
54 #include "../linux_ipc.h"
55 #include "../linux_signal.h"
56 #include "../linux_util.h"
64 l_uint read_exec_only:1;
65 l_uint limit_in_pages:1;
66 l_uint seg_not_present:1;
70 struct l_old_select_argv {
75 struct l_timeval *timeout;
79 linux_to_bsd_sigaltstack(int lsa)
83 if (lsa & LINUX_SS_DISABLE)
85 if (lsa & LINUX_SS_ONSTACK)
91 bsd_to_linux_sigaltstack(int bsa)
96 lsa |= LINUX_SS_DISABLE;
98 lsa |= LINUX_SS_ONSTACK;
103 linux_execve(struct linux_execve_args *args)
105 struct execve_args bsd;
109 sg = stackgap_init();
110 CHECKALTEXIST(&sg, args->path);
114 printf(ARGS(execve, "%s"), args->path);
118 * Note: inherit and set the full 64 bit syscall return
119 * value so a successful execve() sets %edx to 0.
121 bsd.sysmsg_result64 = args->sysmsg_result64;
122 bsd.fname = args->path;
123 bsd.argv = args->argp;
124 bsd.envv = args->envp;
125 error = execve(&bsd);
126 args->sysmsg_result64 = bsd.sysmsg_result64;
130 struct l_ipc_kludge {
131 struct l_msgbuf *msgp;
136 linux_ipc(struct linux_ipc_args *args)
140 switch (args->what & 0xFFFF) {
142 struct linux_semop_args a;
144 a.semid = args->arg1;
146 a.nsops = args->arg2;
147 a.sysmsg_lresult = 0;
148 error = linux_semop(&a);
149 args->sysmsg_lresult = a.sysmsg_lresult;
153 struct linux_semget_args a;
156 a.nsems = args->arg2;
157 a.semflg = args->arg3;
158 a.sysmsg_lresult = 0;
159 error = linux_semget(&a);
160 args->sysmsg_lresult = a.sysmsg_lresult;
164 struct linux_semctl_args a;
167 a.semid = args->arg1;
168 a.semnum = args->arg2;
170 a.sysmsg_lresult = 0;
171 error = copyin((caddr_t)args->ptr, &a.arg, sizeof(a.arg));
174 error = linux_semctl(&a);
175 args->sysmsg_lresult = a.sysmsg_lresult;
179 struct linux_msgsnd_args a;
181 a.msqid = args->arg1;
183 a.msgsz = args->arg2;
184 a.msgflg = args->arg3;
185 a.sysmsg_lresult = 0;
186 error = linux_msgsnd(&a);
187 args->sysmsg_lresult = a.sysmsg_lresult;
191 struct linux_msgrcv_args a;
193 a.msqid = args->arg1;
194 a.msgsz = args->arg2;
195 a.msgflg = args->arg3;
196 a.sysmsg_lresult = 0;
197 if ((args->what >> 16) == 0) {
198 struct l_ipc_kludge tmp;
201 if (args->ptr == NULL)
203 error = copyin((caddr_t)args->ptr, &tmp, sizeof(tmp));
207 a.msgtyp = tmp.msgtyp;
210 a.msgtyp = args->arg5;
212 error = linux_msgrcv(&a);
213 args->sysmsg_lresult = a.sysmsg_lresult;
217 struct linux_msgget_args a;
220 a.msgflg = args->arg2;
221 a.sysmsg_lresult = 0;
222 error = linux_msgget(&a);
223 args->sysmsg_lresult = a.sysmsg_lresult;
227 struct linux_msgctl_args a;
229 a.msqid = args->arg1;
232 a.sysmsg_lresult = 0;
233 error = linux_msgctl(&a);
234 args->sysmsg_lresult = a.sysmsg_lresult;
238 struct linux_shmat_args a;
240 a.shmid = args->arg1;
241 a.shmaddr = args->ptr;
242 a.shmflg = args->arg2;
243 a.raddr = (l_ulong *)args->arg3;
244 a.sysmsg_lresult = 0;
245 error = linux_shmat(&a);
246 args->sysmsg_lresult = a.sysmsg_lresult;
250 struct linux_shmdt_args a;
252 a.shmaddr = args->ptr;
253 a.sysmsg_lresult = 0;
254 error = linux_shmdt(&a);
255 args->sysmsg_lresult = a.sysmsg_lresult;
259 struct linux_shmget_args a;
263 a.shmflg = args->arg3;
264 a.sysmsg_lresult = 0;
265 error = linux_shmget(&a);
266 args->sysmsg_lresult = a.sysmsg_lresult;
270 struct linux_shmctl_args a;
272 a.shmid = args->arg1;
275 a.sysmsg_lresult = 0;
276 error = linux_shmctl(&a);
277 args->sysmsg_lresult = a.sysmsg_lresult;
288 linux_old_select(struct linux_old_select_args *args)
290 struct l_old_select_argv linux_args;
291 struct linux_select_args newsel;
295 if (ldebug(old_select))
296 printf(ARGS(old_select, "%p"), args->ptr);
299 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
303 newsel.sysmsg_result = 0;
304 newsel.nfds = linux_args.nfds;
305 newsel.readfds = linux_args.readfds;
306 newsel.writefds = linux_args.writefds;
307 newsel.exceptfds = linux_args.exceptfds;
308 newsel.timeout = linux_args.timeout;
309 error = linux_select(&newsel);
310 args->sysmsg_result = newsel.sysmsg_result;
315 linux_fork(struct linux_fork_args *args)
321 printf(ARGS(fork, ""));
324 if ((error = fork((struct fork_args *)args)) != 0)
327 if (args->sysmsg_result == 1)
328 args->sysmsg_result = 0;
333 linux_vfork(struct linux_vfork_args *args)
339 printf(ARGS(vfork, ""));
342 if ((error = vfork((struct vfork_args *)args)) != 0)
344 /* Are we the child? */
345 if (args->sysmsg_result == 1)
346 args->sysmsg_result = 0;
350 #define CLONE_VM 0x100
351 #define CLONE_FS 0x200
352 #define CLONE_FILES 0x400
353 #define CLONE_SIGHAND 0x800
354 #define CLONE_PID 0x1000
357 linux_clone(struct linux_clone_args *args)
359 int error, ff = RFPROC;
363 struct rfork_args rf_args;
367 printf(ARGS(clone, "flags %x, stack %x"),
368 (unsigned int)args->flags, (unsigned int)args->stack);
369 if (args->flags & CLONE_PID)
370 printf(LMSG("CLONE_PID not yet supported"));
377 exit_signal = args->flags & 0x000000ff;
378 if (exit_signal >= LINUX_NSIG)
381 if (exit_signal <= LINUX_SIGTBLSZ)
382 exit_signal = linux_to_bsd_signal[_SIG_IDX(exit_signal)];
384 /* RFTHREAD probably not necessary here, but it shouldn't hurt */
387 if (args->flags & CLONE_VM)
389 if (args->flags & CLONE_SIGHAND)
391 if (!(args->flags & CLONE_FILES))
398 rf_args.sysmsg_result = 0;
399 if ((error = rfork(&rf_args)) != 0)
401 args->sysmsg_result = rf_args.sysmsg_result;
403 p2 = pfind(rf_args.sysmsg_result);
407 p2->p_sigparent = exit_signal;
408 p2->p_md.md_regs->tf_esp = (unsigned int)args->stack;
412 printf(LMSG("clone: successful rfork to %ld"),
429 #define STACK_SIZE (2 * 1024 * 1024)
430 #define GUARD_SIZE (4 * PAGE_SIZE)
433 linux_mmap(struct linux_mmap_args *args)
435 struct proc *p = curproc;
436 struct mmap_args bsd_args;
438 struct l_mmap_argv linux_args;
440 error = copyin((caddr_t)args->ptr, &linux_args, sizeof(linux_args));
446 printf(ARGS(mmap, "%p, %d, %d, 0x%08x, %d, %d"),
447 (void *)linux_args.addr, linux_args.len, linux_args.prot,
448 linux_args.flags, linux_args.fd, linux_args.pos);
452 bsd_args.sysmsg_resultp = NULL;
453 if (linux_args.flags & LINUX_MAP_SHARED)
454 bsd_args.flags |= MAP_SHARED;
455 if (linux_args.flags & LINUX_MAP_PRIVATE)
456 bsd_args.flags |= MAP_PRIVATE;
457 if (linux_args.flags & LINUX_MAP_FIXED)
458 bsd_args.flags |= MAP_FIXED;
459 if (linux_args.flags & LINUX_MAP_ANON)
460 bsd_args.flags |= MAP_ANON;
462 bsd_args.flags |= MAP_NOSYNC;
463 if (linux_args.flags & LINUX_MAP_GROWSDOWN) {
464 bsd_args.flags |= MAP_STACK;
466 /* The linux MAP_GROWSDOWN option does not limit auto
467 * growth of the region. Linux mmap with this option
468 * takes as addr the inital BOS, and as len, the initial
469 * region size. It can then grow down from addr without
470 * limit. However, linux threads has an implicit internal
471 * limit to stack size of STACK_SIZE. Its just not
472 * enforced explicitly in linux. But, here we impose
473 * a limit of (STACK_SIZE - GUARD_SIZE) on the stack
474 * region, since we can do this with our mmap.
476 * Our mmap with MAP_STACK takes addr as the maximum
477 * downsize limit on BOS, and as len the max size of
478 * the region. It them maps the top SGROWSIZ bytes,
479 * and autgrows the region down, up to the limit
482 * If we don't use the MAP_STACK option, the effect
483 * of this code is to allocate a stack region of a
484 * fixed size of (STACK_SIZE - GUARD_SIZE).
487 /* This gives us TOS */
488 bsd_args.addr = linux_args.addr + linux_args.len;
490 if (bsd_args.addr > p->p_vmspace->vm_maxsaddr) {
491 /* Some linux apps will attempt to mmap
492 * thread stacks near the top of their
493 * address space. If their TOS is greater
494 * than vm_maxsaddr, vm_map_growstack()
495 * will confuse the thread stack with the
496 * process stack and deliver a SEGV if they
497 * attempt to grow the thread stack past their
498 * current stacksize rlimit. To avoid this,
499 * adjust vm_maxsaddr upwards to reflect
500 * the current stacksize rlimit rather
501 * than the maximum possible stacksize.
502 * It would be better to adjust the
503 * mmap'ed region, but some apps do not check
504 * mmap's return value.
506 p->p_vmspace->vm_maxsaddr = (char *)USRSTACK -
507 p->p_rlimit[RLIMIT_STACK].rlim_cur;
510 /* This gives us our maximum stack size */
511 if (linux_args.len > STACK_SIZE - GUARD_SIZE)
512 bsd_args.len = linux_args.len;
514 bsd_args.len = STACK_SIZE - GUARD_SIZE;
516 /* This gives us a new BOS. If we're using VM_STACK, then
517 * mmap will just map the top SGROWSIZ bytes, and let
518 * the stack grow down to the limit at BOS. If we're
519 * not using VM_STACK we map the full stack, since we
520 * don't have a way to autogrow it.
522 bsd_args.addr -= bsd_args.len;
524 bsd_args.addr = linux_args.addr;
525 bsd_args.len = linux_args.len;
528 bsd_args.prot = linux_args.prot | PROT_READ; /* always required */
529 if (linux_args.flags & LINUX_MAP_ANON)
532 bsd_args.fd = linux_args.fd;
533 bsd_args.pos = linux_args.pos;
538 printf("-> (%p, %d, %d, 0x%08x, %d, %d)\n",
539 (void *)bsd_args.addr, bsd_args.len, bsd_args.prot,
540 bsd_args.flags, bsd_args.fd, (int)bsd_args.pos);
543 error = mmap(&bsd_args);
544 args->sysmsg_resultp = bsd_args.sysmsg_resultp;
549 linux_pipe(struct linux_pipe_args *args)
553 struct pipe_args bsd_args;
557 printf(ARGS(pipe, "*"));
560 reg_edx = args->sysmsg_fds[1];
561 error = pipe(&bsd_args);
563 args->sysmsg_fds[1] = reg_edx;
567 error = copyout(bsd_args.sysmsg_fds, args->pipefds, 2*sizeof(int));
569 args->sysmsg_fds[1] = reg_edx;
573 args->sysmsg_fds[1] = reg_edx;
574 args->sysmsg_fds[0] = 0;
579 linux_ioperm(struct linux_ioperm_args *args)
581 struct sysarch_args sa;
582 struct i386_ioperm_args *iia;
586 sg = stackgap_init();
587 iia = stackgap_alloc(&sg, sizeof(struct i386_ioperm_args));
588 iia->start = args->start;
589 iia->length = args->length;
590 iia->enable = args->enable;
591 sa.sysmsg_resultp = NULL;
592 sa.op = I386_SET_IOPERM;
593 sa.parms = (char *)iia;
594 error = sysarch(&sa);
595 args->sysmsg_resultp = sa.sysmsg_resultp;
600 linux_iopl(struct linux_iopl_args *args)
602 struct thread *td = curthread;
603 struct proc *p = td->td_proc;
608 if (args->level < 0 || args->level > 3)
610 if ((error = suser(td)) != 0)
614 p->p_md.md_regs->tf_eflags = (p->p_md.md_regs->tf_eflags & ~PSL_IOPL) |
615 (args->level * (PSL_IOPL / 3));
620 linux_modify_ldt(struct linux_modify_ldt_args *uap)
624 struct sysarch_args args;
625 struct i386_ldt_args *ldt;
626 struct l_descriptor ld;
627 union descriptor *desc;
629 sg = stackgap_init();
631 if (uap->ptr == NULL)
635 case 0x00: /* read_ldt */
636 ldt = stackgap_alloc(&sg, sizeof(*ldt));
638 ldt->descs = uap->ptr;
639 ldt->num = uap->bytecount / sizeof(union descriptor);
640 args.op = I386_GET_LDT;
641 args.parms = (char*)ldt;
642 args.sysmsg_result = 0;
643 error = sysarch(&args);
644 uap->sysmsg_result = args.sysmsg_result *
645 sizeof(union descriptor);
647 case 0x01: /* write_ldt */
648 case 0x11: /* write_ldt */
649 if (uap->bytecount != sizeof(ld))
652 error = copyin(uap->ptr, &ld, sizeof(ld));
656 ldt = stackgap_alloc(&sg, sizeof(*ldt));
657 desc = stackgap_alloc(&sg, sizeof(*desc));
658 ldt->start = ld.entry_number;
661 desc->sd.sd_lolimit = (ld.limit & 0x0000ffff);
662 desc->sd.sd_hilimit = (ld.limit & 0x000f0000) >> 16;
663 desc->sd.sd_lobase = (ld.base_addr & 0x00ffffff);
664 desc->sd.sd_hibase = (ld.base_addr & 0xff000000) >> 24;
665 desc->sd.sd_type = SDT_MEMRO | ((ld.read_exec_only ^ 1) << 1) |
668 desc->sd.sd_p = (ld.seg_not_present ^ 1);
670 desc->sd.sd_def32 = ld.seg_32bit;
671 desc->sd.sd_gran = ld.limit_in_pages;
672 args.op = I386_SET_LDT;
673 args.parms = (char*)ldt;
674 args.sysmsg_result = 0;
675 error = sysarch(&args);
676 uap->sysmsg_result = args.sysmsg_result;
683 if (error == EOPNOTSUPP) {
684 printf("linux: modify_ldt needs kernel option USER_LDT\n");
692 linux_sigaction(struct linux_sigaction_args *args)
695 l_sigaction_t linux_act, linux_oact;
696 struct sigaction act, oact;
700 if (ldebug(sigaction))
701 printf(ARGS(sigaction, "%d, %p, %p"),
702 args->sig, (void *)args->nsa, (void *)args->osa);
706 error = copyin(args->nsa, &osa, sizeof(l_osigaction_t));
709 linux_act.lsa_handler = osa.lsa_handler;
710 linux_act.lsa_flags = osa.lsa_flags;
711 linux_act.lsa_restorer = osa.lsa_restorer;
712 LINUX_SIGEMPTYSET(linux_act.lsa_mask);
713 linux_act.lsa_mask.__bits[0] = osa.lsa_mask;
714 linux_to_bsd_sigaction(&linux_act, &act);
717 error = kern_sigaction(args->sig, args->nsa ? &act : NULL,
718 args->osa ? &oact : NULL);
720 if (args->osa != NULL && !error) {
721 bsd_to_linux_sigaction(&oact, &linux_oact);
722 osa.lsa_handler = linux_oact.lsa_handler;
723 osa.lsa_flags = linux_oact.lsa_flags;
724 osa.lsa_restorer = linux_oact.lsa_restorer;
725 osa.lsa_mask = linux_oact.lsa_mask.__bits[0];
726 error = copyout(&osa, args->osa, sizeof(l_osigaction_t));
732 * Linux has two extra args, restart and oldmask. We dont use these,
733 * but it seems that "restart" is actually a context pointer that
734 * enables the signal to happen with a different register set.
737 linux_sigsuspend(struct linux_sigsuspend_args *args)
739 l_sigset_t linux_mask;
744 if (ldebug(sigsuspend))
745 printf(ARGS(sigsuspend, "%08lx"), (unsigned long)args->mask);
748 LINUX_SIGEMPTYSET(mask);
749 mask.__bits[0] = args->mask;
750 linux_to_bsd_sigset(&linux_mask, &mask);
752 error = kern_sigsuspend(&mask);
758 linux_rt_sigsuspend(struct linux_rt_sigsuspend_args *uap)
760 l_sigset_t linux_mask;
765 if (ldebug(rt_sigsuspend))
766 printf(ARGS(rt_sigsuspend, "%p, %d"),
767 (void *)uap->newset, uap->sigsetsize);
770 if (uap->sigsetsize != sizeof(l_sigset_t))
773 error = copyin(uap->newset, &linux_mask, sizeof(l_sigset_t));
777 linux_to_bsd_sigset(&linux_mask, &mask);
779 error = kern_sigsuspend(&mask);
785 linux_pause(struct linux_pause_args *args)
787 struct thread *td = curthread;
788 struct proc *p = td->td_proc;
794 printf(ARGS(pause, ""));
799 error = kern_sigsuspend(&mask);
805 linux_sigaltstack(struct linux_sigaltstack_args *uap)
812 if (ldebug(sigaltstack))
813 printf(ARGS(sigaltstack, "%p, %p"), uap->uss, uap->uoss);
817 error = copyin(uap->uss, &linux_ss, sizeof(l_stack_t));
821 ss.ss_sp = linux_ss.ss_sp;
822 ss.ss_size = linux_ss.ss_size;
823 ss.ss_flags = linux_to_bsd_sigaltstack(linux_ss.ss_flags);
826 error = kern_sigaltstack(uap->uss ? &ss : NULL,
827 uap->uoss ? &oss : NULL);
829 if (error == 0 && uap->uoss) {
830 linux_ss.ss_sp = oss.ss_sp;
831 linux_ss.ss_size = oss.ss_size;
832 linux_ss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags);
833 error = copyout(&linux_ss, uap->uoss, sizeof(l_stack_t));