summaryrefslogtreecommitdiffstats
path: root/sys/amd64/linux32/linux32_machdep.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/amd64/linux32/linux32_machdep.c')
-rw-r--r--sys/amd64/linux32/linux32_machdep.c380
1 files changed, 100 insertions, 280 deletions
diff --git a/sys/amd64/linux32/linux32_machdep.c b/sys/amd64/linux32/linux32_machdep.c
index be0acb3..26041a3 100644
--- a/sys/amd64/linux32/linux32_machdep.c
+++ b/sys/amd64/linux32/linux32_machdep.c
@@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$");
#include <sys/syscallsubr.h>
#include <sys/sysproto.h>
#include <sys/unistd.h>
+#include <sys/wait.h>
#include <machine/frame.h>
#include <machine/pcb.h>
@@ -66,6 +67,7 @@ __FBSDID("$FreeBSD$");
#include <amd64/linux32/linux.h>
#include <amd64/linux32/linux32_proto.h>
#include <compat/linux/linux_ipc.h>
+#include <compat/linux/linux_misc.h>
#include <compat/linux/linux_signal.h>
#include <compat/linux/linux_util.h>
#include <compat/linux/linux_emul.h>
@@ -106,6 +108,30 @@ bsd_to_linux_sigaltstack(int bsa)
return (lsa);
}
+static void
+bsd_to_linux_rusage(struct rusage *ru, struct l_rusage *lru)
+{
+
+ lru->ru_utime.tv_sec = ru->ru_utime.tv_sec;
+ lru->ru_utime.tv_usec = ru->ru_utime.tv_usec;
+ lru->ru_stime.tv_sec = ru->ru_stime.tv_sec;
+ lru->ru_stime.tv_usec = ru->ru_stime.tv_usec;
+ lru->ru_maxrss = ru->ru_maxrss;
+ lru->ru_ixrss = ru->ru_ixrss;
+ lru->ru_idrss = ru->ru_idrss;
+ lru->ru_isrss = ru->ru_isrss;
+ lru->ru_minflt = ru->ru_minflt;
+ lru->ru_majflt = ru->ru_majflt;
+ lru->ru_nswap = ru->ru_nswap;
+ lru->ru_inblock = ru->ru_inblock;
+ lru->ru_oublock = ru->ru_oublock;
+ lru->ru_msgsnd = ru->ru_msgsnd;
+ lru->ru_msgrcv = ru->ru_msgrcv;
+ lru->ru_nsignals = ru->ru_nsignals;
+ lru->ru_nvcsw = ru->ru_nvcsw;
+ lru->ru_nivcsw = ru->ru_nivcsw;
+}
+
int
linux_execve(struct thread *td, struct linux_execve_args *args)
{
@@ -131,7 +157,7 @@ linux_execve(struct thread *td, struct linux_execve_args *args)
* linux_proc_init, this leads to a panic on KASSERT
* because such process has p->p_emuldata == NULL.
*/
- if (td->td_proc->p_sysent == &elf_linux_sysvec)
+ if (SV_PROC_ABI(td->td_proc) == SV_ABI_LINUX)
error = linux_proc_init(td, 0, 0);
return (error);
}
@@ -383,279 +409,54 @@ linux_old_select(struct thread *td, struct linux_old_select_args *args)
}
int
-linux_fork(struct thread *td, struct linux_fork_args *args)
+linux_set_cloned_tls(struct thread *td, void *desc)
{
+ struct user_segment_descriptor sd;
+ struct l_user_desc info;
+ struct pcb *pcb;
int error;
- struct proc *p2;
- struct thread *td2;
-
-#ifdef DEBUG
- if (ldebug(fork))
- printf(ARGS(fork, ""));
-#endif
-
- if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2)) != 0)
- return (error);
-
- if (error == 0) {
- td->td_retval[0] = p2->p_pid;
- td->td_retval[1] = 0;
- }
-
- if (td->td_retval[1] == 1)
- td->td_retval[0] = 0;
- error = linux_proc_init(td, td->td_retval[0], 0);
- if (error)
- return (error);
-
- td2 = FIRST_THREAD_IN_PROC(p2);
-
- /*
- * Make this runnable after we are finished with it.
- */
- thread_lock(td2);
- TD_SET_CAN_RUN(td2);
- sched_add(td2, SRQ_BORING);
- thread_unlock(td2);
+ int a[2];
- return (0);
-}
+ error = copyin(desc, &info, sizeof(struct l_user_desc));
+ if (error) {
+ printf(LMSG("copyin failed!"));
+ } else {
+ /* We might copy out the entry_number as GUGS32_SEL. */
+ info.entry_number = GUGS32_SEL;
+ error = copyout(&info, desc, sizeof(struct l_user_desc));
+ if (error)
+ printf(LMSG("copyout failed!"));
-int
-linux_vfork(struct thread *td, struct linux_vfork_args *args)
-{
- int error;
- struct proc *p2;
- struct thread *td2;
+ a[0] = LINUX_LDT_entry_a(&info);
+ a[1] = LINUX_LDT_entry_b(&info);
+ memcpy(&sd, &a, sizeof(a));
#ifdef DEBUG
- if (ldebug(vfork))
- printf(ARGS(vfork, ""));
+ if (ldebug(clone))
+ printf("Segment created in clone with "
+ "CLONE_SETTLS: lobase: %x, hibase: %x, "
+ "lolimit: %x, hilimit: %x, type: %i, "
+ "dpl: %i, p: %i, xx: %i, long: %i, "
+ "def32: %i, gran: %i\n", sd.sd_lobase,
+ sd.sd_hibase, sd.sd_lolimit, sd.sd_hilimit,
+ sd.sd_type, sd.sd_dpl, sd.sd_p, sd.sd_xx,
+ sd.sd_long, sd.sd_def32, sd.sd_gran);
#endif
-
- /* Exclude RFPPWAIT */
- if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2)) != 0)
- return (error);
- if (error == 0) {
- td->td_retval[0] = p2->p_pid;
- td->td_retval[1] = 0;
+ pcb = td->td_pcb;
+ pcb->pcb_gsbase = (register_t)info.base_addr;
+/* XXXKIB pcb->pcb_gs32sd = sd; */
+ td->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL);
+ set_pcb_flags(pcb, PCB_GS32BIT | PCB_32BIT);
}
- /* Are we the child? */
- if (td->td_retval[1] == 1)
- td->td_retval[0] = 0;
- error = linux_proc_init(td, td->td_retval[0], 0);
- if (error)
- return (error);
-
- PROC_LOCK(p2);
- p2->p_flag |= P_PPWAIT;
- PROC_UNLOCK(p2);
-
- td2 = FIRST_THREAD_IN_PROC(p2);
-
- /*
- * Make this runnable after we are finished with it.
- */
- thread_lock(td2);
- TD_SET_CAN_RUN(td2);
- sched_add(td2, SRQ_BORING);
- thread_unlock(td2);
-
- /* wait for the children to exit, ie. emulate vfork */
- PROC_LOCK(p2);
- while (p2->p_flag & P_PPWAIT)
- cv_wait(&p2->p_pwait, &p2->p_mtx);
- PROC_UNLOCK(p2);
- return (0);
+ return (error);
}
int
-linux_clone(struct thread *td, struct linux_clone_args *args)
+linux_set_upcall_kse(struct thread *td, register_t stack)
{
- int error, ff = RFPROC | RFSTOPPED;
- struct proc *p2;
- struct thread *td2;
- int exit_signal;
- struct linux_emuldata *em;
-
-#ifdef DEBUG
- if (ldebug(clone)) {
- printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, "
- "child tid: %p"), (unsigned)args->flags,
- args->stack, args->parent_tidptr, args->child_tidptr);
- }
-#endif
-
- exit_signal = args->flags & 0x000000ff;
- if (LINUX_SIG_VALID(exit_signal)) {
- if (exit_signal <= LINUX_SIGTBLSZ)
- exit_signal =
- linux_to_bsd_signal[_SIG_IDX(exit_signal)];
- } else if (exit_signal != 0)
- return (EINVAL);
- if (args->flags & LINUX_CLONE_VM)
- ff |= RFMEM;
- if (args->flags & LINUX_CLONE_SIGHAND)
- ff |= RFSIGSHARE;
- /*
- * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
- * and open files is independant. In FreeBSD, its in one
- * structure but in reality it does not cause any problems
- * because both of these flags are usually set together.
- */
- if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
- ff |= RFFDG;
-
- /*
- * Attempt to detect when linux_clone(2) is used for creating
- * kernel threads. Unfortunately despite the existence of the
- * CLONE_THREAD flag, version of linuxthreads package used in
- * most popular distros as of beginning of 2005 doesn't make
- * any use of it. Therefore, this detection relies on
- * empirical observation that linuxthreads sets certain
- * combination of flags, so that we can make more or less
- * precise detection and notify the FreeBSD kernel that several
- * processes are in fact part of the same threading group, so
- * that special treatment is necessary for signal delivery
- * between those processes and fd locking.
- */
- if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
- ff |= RFTHREAD;
-
- if (args->flags & LINUX_CLONE_PARENT_SETTID)
- if (args->parent_tidptr == NULL)
- return (EINVAL);
-
- error = fork1(td, ff, 0, &p2);
- if (error)
- return (error);
-
- if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) {
- sx_xlock(&proctree_lock);
- PROC_LOCK(p2);
- proc_reparent(p2, td->td_proc->p_pptr);
- PROC_UNLOCK(p2);
- sx_xunlock(&proctree_lock);
- }
-
- /* create the emuldata */
- error = linux_proc_init(td, p2->p_pid, args->flags);
- /* reference it - no need to check this */
- em = em_find(p2, EMUL_DOLOCK);
- KASSERT(em != NULL, ("clone: emuldata not found.\n"));
- /* and adjust it */
-
- if (args->flags & LINUX_CLONE_THREAD) {
-#ifdef notyet
- PROC_LOCK(p2);
- p2->p_pgrp = td->td_proc->p_pgrp;
- PROC_UNLOCK(p2);
-#endif
- exit_signal = 0;
- }
-
- if (args->flags & LINUX_CLONE_CHILD_SETTID)
- em->child_set_tid = args->child_tidptr;
- else
- em->child_set_tid = NULL;
-
- if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
- em->child_clear_tid = args->child_tidptr;
- else
- em->child_clear_tid = NULL;
-
- EMUL_UNLOCK(&emul_lock);
-
- if (args->flags & LINUX_CLONE_PARENT_SETTID) {
- error = copyout(&p2->p_pid, args->parent_tidptr,
- sizeof(p2->p_pid));
- if (error)
- printf(LMSG("copyout failed!"));
- }
-
- PROC_LOCK(p2);
- p2->p_sigparent = exit_signal;
- PROC_UNLOCK(p2);
- td2 = FIRST_THREAD_IN_PROC(p2);
- /*
- * In a case of stack = NULL, we are supposed to COW calling process
- * stack. This is what normal fork() does, so we just keep tf_rsp arg
- * intact.
- */
- if (args->stack)
- td2->td_frame->tf_rsp = PTROUT(args->stack);
-
- if (args->flags & LINUX_CLONE_SETTLS) {
- struct user_segment_descriptor sd;
- struct l_user_desc info;
- int a[2];
-
- error = copyin((void *)td->td_frame->tf_rsi, &info,
- sizeof(struct l_user_desc));
- if (error) {
- printf(LMSG("copyin failed!"));
- } else {
- /* We might copy out the entry_number as GUGS32_SEL. */
- info.entry_number = GUGS32_SEL;
- error = copyout(&info, (void *)td->td_frame->tf_rsi,
- sizeof(struct l_user_desc));
- if (error)
- printf(LMSG("copyout failed!"));
-
- a[0] = LINUX_LDT_entry_a(&info);
- a[1] = LINUX_LDT_entry_b(&info);
-
- memcpy(&sd, &a, sizeof(a));
-#ifdef DEBUG
- if (ldebug(clone))
- printf("Segment created in clone with "
- "CLONE_SETTLS: lobase: %x, hibase: %x, "
- "lolimit: %x, hilimit: %x, type: %i, "
- "dpl: %i, p: %i, xx: %i, long: %i, "
- "def32: %i, gran: %i\n", sd.sd_lobase,
- sd.sd_hibase, sd.sd_lolimit, sd.sd_hilimit,
- sd.sd_type, sd.sd_dpl, sd.sd_p, sd.sd_xx,
- sd.sd_long, sd.sd_def32, sd.sd_gran);
-#endif
- td2->td_pcb->pcb_gsbase = (register_t)info.base_addr;
-/* XXXKIB td2->td_pcb->pcb_gs32sd = sd; */
- td2->td_frame->tf_gs = GSEL(GUGS32_SEL, SEL_UPL);
- td2->td_pcb->pcb_flags |= PCB_GS32BIT | PCB_32BIT;
- }
- }
-
-#ifdef DEBUG
- if (ldebug(clone))
- printf(LMSG("clone: successful rfork to %d, "
- "stack %p sig = %d"), (int)p2->p_pid, args->stack,
- exit_signal);
-#endif
- if (args->flags & LINUX_CLONE_VFORK) {
- PROC_LOCK(p2);
- p2->p_flag |= P_PPWAIT;
- PROC_UNLOCK(p2);
- }
-
- /*
- * Make this runnable after we are finished with it.
- */
- thread_lock(td2);
- TD_SET_CAN_RUN(td2);
- sched_add(td2, SRQ_BORING);
- thread_unlock(td2);
-
- td->td_retval[0] = p2->p_pid;
- td->td_retval[1] = 0;
-
- if (args->flags & LINUX_CLONE_VFORK) {
- /* wait for the children to exit, ie. emulate vfork */
- PROC_LOCK(p2);
- while (p2->p_flag & P_PPWAIT)
- cv_wait(&p2->p_pwait, &p2->p_mtx);
- PROC_UNLOCK(p2);
- }
+ td->td_frame->tf_rsp = stack;
return (0);
}
@@ -1124,24 +925,7 @@ linux_getrusage(struct thread *td, struct linux_getrusage_args *uap)
if (error != 0)
return (error);
if (uap->rusage != NULL) {
- s32.ru_utime.tv_sec = s.ru_utime.tv_sec;
- s32.ru_utime.tv_usec = s.ru_utime.tv_usec;
- s32.ru_stime.tv_sec = s.ru_stime.tv_sec;
- s32.ru_stime.tv_usec = s.ru_stime.tv_usec;
- s32.ru_maxrss = s.ru_maxrss;
- s32.ru_ixrss = s.ru_ixrss;
- s32.ru_idrss = s.ru_idrss;
- s32.ru_isrss = s.ru_isrss;
- s32.ru_minflt = s.ru_minflt;
- s32.ru_majflt = s.ru_majflt;
- s32.ru_nswap = s.ru_nswap;
- s32.ru_inblock = s.ru_inblock;
- s32.ru_oublock = s.ru_oublock;
- s32.ru_msgsnd = s.ru_msgsnd;
- s32.ru_msgrcv = s.ru_msgrcv;
- s32.ru_nsignals = s.ru_nsignals;
- s32.ru_nvcsw = s.ru_nvcsw;
- s32.ru_nivcsw = s.ru_nivcsw;
+ bsd_to_linux_rusage(&s, &s32);
error = copyout(&s32, uap->rusage, sizeof(s32));
}
return (error);
@@ -1169,6 +953,7 @@ linux_set_thread_area(struct thread *td,
{
struct l_user_desc info;
struct user_segment_descriptor sd;
+ struct pcb *pcb;
int a[2];
int error;
@@ -1257,9 +1042,44 @@ linux_set_thread_area(struct thread *td,
sd.sd_gran);
#endif
- td->td_pcb->pcb_gsbase = (register_t)info.base_addr;
- td->td_pcb->pcb_flags |= PCB_32BIT | PCB_GS32BIT;
+ pcb = td->td_pcb;
+ pcb->pcb_gsbase = (register_t)info.base_addr;
+ set_pcb_flags(pcb, PCB_32BIT | PCB_GS32BIT);
update_gdt_gsbase(td, info.base_addr);
return (0);
}
+
+int
+linux_wait4(struct thread *td, struct linux_wait4_args *args)
+{
+ int error, options;
+ struct rusage ru, *rup;
+ struct l_rusage lru;
+
+#ifdef DEBUG
+ if (ldebug(wait4))
+ printf(ARGS(wait4, "%d, %p, %d, %p"),
+ args->pid, (void *)args->status, args->options,
+ (void *)args->rusage);
+#endif
+
+ options = (args->options & (WNOHANG | WUNTRACED));
+ /* WLINUXCLONE should be equal to __WCLONE, but we make sure */
+ if (args->options & __WCLONE)
+ options |= WLINUXCLONE;
+
+ if (args->rusage != NULL)
+ rup = &ru;
+ else
+ rup = NULL;
+ error = linux_common_wait(td, args->pid, args->status, options, rup);
+ if (error)
+ return (error);
+ if (args->rusage != NULL) {
+ bsd_to_linux_rusage(rup, &lru);
+ error = copyout(&lru, args->rusage, sizeof(lru));
+ }
+
+ return (error);
+}
OpenPOWER on IntegriCloud