2006-08-15 12:20:59 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2006 Roman Divacky
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
* in this position and unchanged.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include "opt_compat.h"
|
2012-05-05 19:42:38 +00:00
|
|
|
#include "opt_kdtrace.h"
|
2006-08-15 12:20:59 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/imgact.h>
|
2007-02-02 08:58:16 +00:00
|
|
|
#include <sys/kernel.h>
|
2006-08-15 12:20:59 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mutex.h>
|
2012-05-05 19:42:38 +00:00
|
|
|
#include <sys/sdt.h>
|
2006-08-15 12:20:59 +00:00
|
|
|
#include <sys/sx.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/syscallsubr.h>
|
2010-11-22 20:23:18 +00:00
|
|
|
#include <sys/sysent.h>
|
2006-08-15 12:20:59 +00:00
|
|
|
#include <sys/sysproto.h>
|
|
|
|
#include <sys/unistd.h>
|
|
|
|
|
|
|
|
#ifdef COMPAT_LINUX32
|
|
|
|
#include <machine/../linux32/linux.h>
|
|
|
|
#include <machine/../linux32/linux32_proto.h>
|
|
|
|
#else
|
|
|
|
#include <machine/../linux/linux.h>
|
|
|
|
#include <machine/../linux/linux_proto.h>
|
|
|
|
#endif
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
#include <compat/linux/linux_dtrace.h>
|
2008-05-13 20:01:27 +00:00
|
|
|
#include <compat/linux/linux_emul.h>
|
|
|
|
#include <compat/linux/linux_futex.h>
|
2013-01-29 18:41:30 +00:00
|
|
|
#include <compat/linux/linux_misc.h>
|
2008-05-13 20:01:27 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
/**
|
|
|
|
* Special DTrace provider for the linuxulator.
|
|
|
|
*
|
|
|
|
* In this file we define the provider for the entire linuxulator. All
|
|
|
|
* modules (= files of the linuxulator) use it.
|
|
|
|
*
|
|
|
|
* We define a different name depending on the emulated bitsize, see
|
|
|
|
* ../../<ARCH>/linux{,32}/linux.h, e.g.:
|
|
|
|
* native bitsize = linuxulator
|
|
|
|
* amd64, 32bit emulation = linuxulator32
|
|
|
|
*/
|
|
|
|
LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Special DTrace module "locks", it covers some linuxulator internal
|
|
|
|
* locks.
|
|
|
|
*/
|
|
|
|
LIN_SDT_PROBE_DEFINE1(locks, emul_lock, locked, "struct mtx *");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(locks, emul_lock, unlock, "struct mtx *");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(locks, emul_shared_rlock, locked, "struct sx *");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(locks, emul_shared_rlock, unlock, "struct sx *");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(locks, emul_shared_wlock, locked, "struct sx *");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(locks, emul_shared_wlock, unlock, "struct sx *");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DTrace probes in this module.
|
|
|
|
*/
|
|
|
|
LIN_SDT_PROBE_DEFINE2(emul, em_find, entry, "struct proc *", "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(emul, em_find, return);
|
|
|
|
LIN_SDT_PROBE_DEFINE3(emul, proc_init, entry, "struct thread *", "pid_t",
|
|
|
|
"int");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(emul, proc_init, create_thread);
|
|
|
|
LIN_SDT_PROBE_DEFINE0(emul, proc_init, fork);
|
|
|
|
LIN_SDT_PROBE_DEFINE0(emul, proc_init, exec);
|
|
|
|
LIN_SDT_PROBE_DEFINE0(emul, proc_init, return);
|
|
|
|
LIN_SDT_PROBE_DEFINE1(emul, proc_exit, entry, "struct proc *");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(emul, proc_exit, futex_failed);
|
|
|
|
LIN_SDT_PROBE_DEFINE3(emul, proc_exit, reparent, "pid_t", "pid_t",
|
|
|
|
"struct proc *");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(emul, proc_exit, child_clear_tid_error, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(emul, proc_exit, return);
|
|
|
|
LIN_SDT_PROBE_DEFINE2(emul, proc_exec, entry, "struct proc *",
|
|
|
|
"struct image_params *");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(emul, proc_exec, return);
|
|
|
|
LIN_SDT_PROBE_DEFINE0(emul, linux_schedtail, entry);
|
|
|
|
LIN_SDT_PROBE_DEFINE1(emul, linux_schedtail, copyout_error, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(emul, linux_schedtail, return);
|
|
|
|
LIN_SDT_PROBE_DEFINE1(emul, linux_set_tid_address, entry, "int *");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(emul, linux_set_tid_address, return);
|
|
|
|
LIN_SDT_PROBE_DEFINE2(emul, linux_kill_threads, entry, "struct thread *",
|
|
|
|
"int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(emul, linux_kill_threads, kill, "pid_t");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(emul, linux_kill_threads, return);
|
|
|
|
|
2007-04-02 18:38:13 +00:00
|
|
|
struct sx emul_shared_lock;
|
|
|
|
struct mtx emul_lock;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
|
|
|
/* this returns locked reference to the emuldata entry (if found) */
|
|
|
|
struct linux_emuldata *
|
|
|
|
em_find(struct proc *p, int locked)
|
|
|
|
{
|
|
|
|
struct linux_emuldata *em;
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE2(emul, em_find, entry, p, locked);
|
|
|
|
|
2007-01-07 19:00:38 +00:00
|
|
|
if (locked == EMUL_DOLOCK)
|
2006-12-31 12:42:55 +00:00
|
|
|
EMUL_LOCK(&emul_lock);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2006-12-31 12:42:55 +00:00
|
|
|
em = p->p_emuldata;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2007-01-07 19:00:38 +00:00
|
|
|
if (em == NULL && locked == EMUL_DOLOCK)
|
2006-12-31 12:42:55 +00:00
|
|
|
EMUL_UNLOCK(&emul_lock);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(emul, em_find, return, em);
|
2006-08-15 12:20:59 +00:00
|
|
|
return (em);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
linux_proc_init(struct thread *td, pid_t child, int flags)
|
|
|
|
{
|
|
|
|
struct linux_emuldata *em, *p_em;
|
|
|
|
struct proc *p;
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(emul, proc_init, entry, td, child, flags);
|
|
|
|
|
2006-08-15 12:20:59 +00:00
|
|
|
if (child != 0) {
|
2012-05-05 19:42:38 +00:00
|
|
|
/* fork or create a thread */
|
2006-08-19 11:54:19 +00:00
|
|
|
em = malloc(sizeof *em, M_LINUX, M_WAITOK | M_ZERO);
|
2006-08-15 12:20:59 +00:00
|
|
|
em->pid = child;
|
2006-10-28 10:59:59 +00:00
|
|
|
em->pdeath_signal = 0;
|
2011-02-12 20:58:59 +00:00
|
|
|
em->flags = 0;
|
2008-05-13 20:01:27 +00:00
|
|
|
em->robust_futexes = NULL;
|
2007-03-02 00:08:47 +00:00
|
|
|
if (flags & LINUX_CLONE_THREAD) {
|
2006-12-31 12:42:55 +00:00
|
|
|
/* handled later in the code */
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE0(emul, proc_init, create_thread);
|
2006-08-15 12:20:59 +00:00
|
|
|
} else {
|
2006-12-31 12:42:55 +00:00
|
|
|
struct linux_emuldata_shared *s;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE0(emul, proc_init, fork);
|
|
|
|
|
2006-08-19 11:54:19 +00:00
|
|
|
s = malloc(sizeof *s, M_LINUX, M_WAITOK | M_ZERO);
|
2006-08-15 12:20:59 +00:00
|
|
|
s->refs = 1;
|
|
|
|
s->group_pid = child;
|
|
|
|
|
|
|
|
LIST_INIT(&s->threads);
|
2007-01-20 14:58:59 +00:00
|
|
|
em->shared = s;
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-05-05 19:42:38 +00:00
|
|
|
/* exec */
|
|
|
|
LIN_SDT_PROBE0(emul, proc_init, exec);
|
|
|
|
|
2006-08-15 12:20:59 +00:00
|
|
|
/* lookup the old one */
|
2007-01-07 19:00:38 +00:00
|
|
|
em = em_find(td->td_proc, EMUL_DOLOCK);
|
2006-08-15 12:20:59 +00:00
|
|
|
KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
em->child_clear_tid = NULL;
|
|
|
|
em->child_set_tid = NULL;
|
|
|
|
|
2006-12-31 12:42:55 +00:00
|
|
|
/*
|
|
|
|
* allocate the shared struct only in clone()/fork cases in the case
|
|
|
|
* of clone() td = calling proc and child = pid of the newly created
|
|
|
|
* proc
|
2006-08-15 12:20:59 +00:00
|
|
|
*/
|
|
|
|
if (child != 0) {
|
2007-03-02 00:08:47 +00:00
|
|
|
if (flags & LINUX_CLONE_THREAD) {
|
2006-12-31 12:42:55 +00:00
|
|
|
/* lookup the parent */
|
2007-02-01 13:29:27 +00:00
|
|
|
/*
|
|
|
|
* we dont have to lock the p_em because
|
|
|
|
* its waiting for us in linux_clone so
|
|
|
|
* there is no chance of it changing the
|
|
|
|
* p_em->shared address
|
|
|
|
*/
|
|
|
|
p_em = em_find(td->td_proc, EMUL_DONTLOCK);
|
2006-11-15 11:04:37 +00:00
|
|
|
KASSERT(p_em != NULL, ("proc_init: parent emuldata not found for CLONE_THREAD\n"));
|
2006-08-15 12:20:59 +00:00
|
|
|
em->shared = p_em->shared;
|
2007-02-01 13:29:27 +00:00
|
|
|
EMUL_SHARED_WLOCK(&emul_shared_lock);
|
2006-08-15 12:20:59 +00:00
|
|
|
em->shared->refs++;
|
2007-02-01 13:29:27 +00:00
|
|
|
EMUL_SHARED_WUNLOCK(&emul_shared_lock);
|
2006-08-15 12:20:59 +00:00
|
|
|
} else {
|
2006-12-31 12:42:55 +00:00
|
|
|
/*
|
|
|
|
* handled earlier to avoid malloc(M_WAITOK) with
|
|
|
|
* rwlock held
|
|
|
|
*/
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
2012-05-05 19:42:38 +00:00
|
|
|
|
2006-12-31 12:42:55 +00:00
|
|
|
EMUL_SHARED_WLOCK(&emul_shared_lock);
|
|
|
|
LIST_INSERT_HEAD(&em->shared->threads, em, threads);
|
|
|
|
EMUL_SHARED_WUNLOCK(&emul_shared_lock);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
|
|
|
p = pfind(child);
|
2007-01-20 14:58:59 +00:00
|
|
|
KASSERT(p != NULL, ("process not found in proc_init\n"));
|
|
|
|
p->p_emuldata = em;
|
2006-09-09 16:55:55 +00:00
|
|
|
PROC_UNLOCK(p);
|
2006-08-15 12:20:59 +00:00
|
|
|
} else
|
2006-12-31 12:42:55 +00:00
|
|
|
EMUL_UNLOCK(&emul_lock);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE0(emul, proc_init, return);
|
2006-12-31 12:42:55 +00:00
|
|
|
return (0);
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
linux_proc_exit(void *arg __unused, struct proc *p)
|
|
|
|
{
|
2006-12-31 12:42:55 +00:00
|
|
|
struct linux_emuldata *em;
|
2010-11-22 09:06:59 +00:00
|
|
|
int error, shared_flags, shared_xstat;
|
2006-08-15 12:20:59 +00:00
|
|
|
struct thread *td = FIRST_THREAD_IN_PROC(p);
|
|
|
|
int *child_clear_tid;
|
2006-10-28 10:59:59 +00:00
|
|
|
struct proc *q, *nq;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
|
|
|
if (__predict_true(p->p_sysent != &elf_linux_sysvec))
|
2006-12-31 12:42:55 +00:00
|
|
|
return;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(emul, proc_exit, entry, p);
|
|
|
|
|
2008-05-13 20:01:27 +00:00
|
|
|
release_futexes(p);
|
|
|
|
|
2006-08-15 12:20:59 +00:00
|
|
|
/* find the emuldata */
|
2007-01-07 19:00:38 +00:00
|
|
|
em = em_find(p, EMUL_DOLOCK);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
|
|
|
KASSERT(em != NULL, ("proc_exit: emuldata not found.\n"));
|
|
|
|
|
2007-02-01 13:29:27 +00:00
|
|
|
/* reparent all procs that are not a thread leader to initproc */
|
|
|
|
if (em->shared->group_pid != p->p_pid) {
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(emul, proc_exit, reparent,
|
|
|
|
em->shared->group_pid, p->p_pid, p);
|
|
|
|
|
2007-02-23 22:29:24 +00:00
|
|
|
child_clear_tid = em->child_clear_tid;
|
|
|
|
EMUL_UNLOCK(&emul_lock);
|
|
|
|
sx_xlock(&proctree_lock);
|
|
|
|
wakeup(initproc);
|
2007-02-01 13:29:27 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
proc_reparent(p, initproc);
|
|
|
|
p->p_sigparent = SIGCHLD;
|
|
|
|
PROC_UNLOCK(p);
|
2007-02-23 22:29:24 +00:00
|
|
|
sx_xunlock(&proctree_lock);
|
|
|
|
} else {
|
|
|
|
child_clear_tid = em->child_clear_tid;
|
|
|
|
EMUL_UNLOCK(&emul_lock);
|
2007-02-01 13:29:27 +00:00
|
|
|
}
|
|
|
|
|
2006-08-15 12:20:59 +00:00
|
|
|
EMUL_SHARED_WLOCK(&emul_shared_lock);
|
2010-11-22 09:06:59 +00:00
|
|
|
shared_flags = em->shared->flags;
|
|
|
|
shared_xstat = em->shared->xstat;
|
2006-08-15 12:20:59 +00:00
|
|
|
LIST_REMOVE(em, threads);
|
|
|
|
|
|
|
|
em->shared->refs--;
|
2007-02-23 22:39:26 +00:00
|
|
|
if (em->shared->refs == 0) {
|
|
|
|
EMUL_SHARED_WUNLOCK(&emul_shared_lock);
|
2006-08-19 11:54:19 +00:00
|
|
|
free(em->shared, M_LINUX);
|
2007-02-23 22:39:26 +00:00
|
|
|
} else
|
|
|
|
EMUL_SHARED_WUNLOCK(&emul_shared_lock);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2010-11-22 12:42:32 +00:00
|
|
|
if ((shared_flags & EMUL_SHARED_HASXSTAT) != 0)
|
2010-11-22 09:06:59 +00:00
|
|
|
p->p_xstat = shared_xstat;
|
|
|
|
|
2006-08-15 12:20:59 +00:00
|
|
|
if (child_clear_tid != NULL) {
|
2006-12-31 12:42:55 +00:00
|
|
|
struct linux_sys_futex_args cup;
|
2006-08-15 12:20:59 +00:00
|
|
|
int null = 0;
|
|
|
|
|
|
|
|
error = copyout(&null, child_clear_tid, sizeof(null));
|
2006-10-08 17:10:34 +00:00
|
|
|
if (error) {
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(emul, proc_exit,
|
|
|
|
child_clear_tid_error, error);
|
|
|
|
|
2006-12-31 12:42:55 +00:00
|
|
|
free(em, M_LINUX);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE0(emul, proc_exit, return);
|
2006-12-31 12:42:55 +00:00
|
|
|
return;
|
2006-10-08 17:10:34 +00:00
|
|
|
}
|
2006-08-15 12:20:59 +00:00
|
|
|
|
|
|
|
/* futexes stuff */
|
|
|
|
cup.uaddr = child_clear_tid;
|
|
|
|
cup.op = LINUX_FUTEX_WAKE;
|
2006-12-31 12:42:55 +00:00
|
|
|
cup.val = 0x7fffffff; /* Awake everyone */
|
2006-08-15 12:20:59 +00:00
|
|
|
cup.timeout = NULL;
|
|
|
|
cup.uaddr2 = NULL;
|
|
|
|
cup.val3 = 0;
|
|
|
|
error = linux_sys_futex(FIRST_THREAD_IN_PROC(p), &cup);
|
2006-12-31 12:42:55 +00:00
|
|
|
/*
|
|
|
|
* this cannot happen at the moment and if this happens it
|
2007-02-24 16:49:25 +00:00
|
|
|
* probably means there is a user space bug
|
2006-08-16 18:54:51 +00:00
|
|
|
*/
|
2012-05-05 19:42:38 +00:00
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE0(emul, proc_exit, futex_failed);
|
2006-12-31 12:42:55 +00:00
|
|
|
printf(LMSG("futex stuff in proc_exit failed.\n"));
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clean the stuff up */
|
2006-08-19 11:54:19 +00:00
|
|
|
free(em, M_LINUX);
|
2006-10-28 10:59:59 +00:00
|
|
|
|
|
|
|
/* this is a little weird but rewritten from exit1() */
|
|
|
|
sx_xlock(&proctree_lock);
|
|
|
|
q = LIST_FIRST(&p->p_children);
|
|
|
|
for (; q != NULL; q = nq) {
|
2006-12-31 12:42:55 +00:00
|
|
|
nq = LIST_NEXT(q, p_sibling);
|
|
|
|
if (q->p_flag & P_WEXIT)
|
|
|
|
continue;
|
2006-10-28 10:59:59 +00:00
|
|
|
if (__predict_false(q->p_sysent != &elf_linux_sysvec))
|
2006-12-31 12:42:55 +00:00
|
|
|
continue;
|
2007-01-07 19:00:38 +00:00
|
|
|
em = em_find(q, EMUL_DOLOCK);
|
2006-10-28 10:59:59 +00:00
|
|
|
KASSERT(em != NULL, ("linux_reparent: emuldata not found: %i\n", q->p_pid));
|
2008-10-31 10:38:30 +00:00
|
|
|
PROC_LOCK(q);
|
|
|
|
if ((q->p_flag & P_WEXIT) == 0 && em->pdeath_signal != 0) {
|
2011-09-16 13:58:51 +00:00
|
|
|
kern_psignal(q, em->pdeath_signal);
|
2006-10-28 10:59:59 +00:00
|
|
|
}
|
2008-10-31 10:38:30 +00:00
|
|
|
PROC_UNLOCK(q);
|
2006-10-28 10:59:59 +00:00
|
|
|
EMUL_UNLOCK(&emul_lock);
|
|
|
|
}
|
|
|
|
sx_xunlock(&proctree_lock);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE0(emul, proc_exit, return);
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
2006-12-31 12:42:55 +00:00
|
|
|
/*
|
2006-08-16 18:54:51 +00:00
|
|
|
* This is used in a case of transition from FreeBSD binary execing to linux binary
|
2006-08-15 12:20:59 +00:00
|
|
|
* in this case we create linux emuldata proc entry with the pid of the currently running
|
|
|
|
* process.
|
|
|
|
*/
|
2006-12-31 12:42:55 +00:00
|
|
|
void
|
|
|
|
linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
|
2006-08-15 12:20:59 +00:00
|
|
|
{
|
2012-05-05 19:42:38 +00:00
|
|
|
if (__predict_false(imgp->sysent == &elf_linux_sysvec)) {
|
|
|
|
LIN_SDT_PROBE2(emul, proc_exec, entry, p, imgp);
|
|
|
|
}
|
2006-12-31 12:42:55 +00:00
|
|
|
if (__predict_false(imgp->sysent == &elf_linux_sysvec
|
|
|
|
&& p->p_sysent != &elf_linux_sysvec))
|
|
|
|
linux_proc_init(FIRST_THREAD_IN_PROC(p), p->p_pid, 0);
|
2010-11-22 12:42:32 +00:00
|
|
|
if (__predict_false((p->p_sysent->sv_flags & SV_ABI_MASK) ==
|
|
|
|
SV_ABI_LINUX))
|
2010-11-22 09:06:59 +00:00
|
|
|
/* Kill threads regardless of imgp->sysent value */
|
|
|
|
linux_kill_threads(FIRST_THREAD_IN_PROC(p), SIGKILL);
|
2006-08-15 12:20:59 +00:00
|
|
|
if (__predict_false(imgp->sysent != &elf_linux_sysvec
|
2006-12-31 12:42:55 +00:00
|
|
|
&& p->p_sysent == &elf_linux_sysvec)) {
|
|
|
|
struct linux_emuldata *em;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2007-02-01 13:29:27 +00:00
|
|
|
/*
|
|
|
|
* XXX:There's a race because here we assign p->p_emuldata NULL
|
|
|
|
* but the process is still counted as linux one for a short
|
|
|
|
* time so some other process might reference it and try to
|
|
|
|
* access its p->p_emuldata and panicing on a NULL reference.
|
|
|
|
*/
|
|
|
|
em = em_find(p, EMUL_DONTLOCK);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
|
|
|
KASSERT(em != NULL, ("proc_exec: emuldata not found.\n"));
|
2006-12-31 12:42:55 +00:00
|
|
|
|
2006-08-15 12:20:59 +00:00
|
|
|
EMUL_SHARED_WLOCK(&emul_shared_lock);
|
|
|
|
LIST_REMOVE(em, threads);
|
|
|
|
|
|
|
|
PROC_LOCK(p);
|
|
|
|
p->p_emuldata = NULL;
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
|
|
|
|
em->shared->refs--;
|
2007-02-23 22:39:26 +00:00
|
|
|
if (em->shared->refs == 0) {
|
|
|
|
EMUL_SHARED_WUNLOCK(&emul_shared_lock);
|
2006-12-31 12:42:55 +00:00
|
|
|
free(em->shared, M_LINUX);
|
2007-02-23 22:39:26 +00:00
|
|
|
} else
|
|
|
|
EMUL_SHARED_WUNLOCK(&emul_shared_lock);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2006-08-28 13:52:27 +00:00
|
|
|
free(em, M_LINUX);
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
if (__predict_false(imgp->sysent == &elf_linux_sysvec)) {
|
|
|
|
LIN_SDT_PROBE0(emul, proc_exec, return);
|
|
|
|
}
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-03-08 19:01:45 +00:00
|
|
|
linux_schedtail(struct thread *td)
|
2006-08-15 12:20:59 +00:00
|
|
|
{
|
|
|
|
struct linux_emuldata *em;
|
2011-03-08 19:01:45 +00:00
|
|
|
struct proc *p;
|
2006-08-15 12:20:59 +00:00
|
|
|
int error = 0;
|
|
|
|
int *child_set_tid;
|
|
|
|
|
2011-03-08 19:01:45 +00:00
|
|
|
p = td->td_proc;
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(emul, linux_schedtail, entry, p);
|
|
|
|
|
2006-08-15 12:20:59 +00:00
|
|
|
/* find the emuldata */
|
2007-01-07 19:00:38 +00:00
|
|
|
em = em_find(p, EMUL_DOLOCK);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2007-02-01 13:29:27 +00:00
|
|
|
KASSERT(em != NULL, ("linux_schedtail: emuldata not found.\n"));
|
2006-08-15 12:20:59 +00:00
|
|
|
child_set_tid = em->child_set_tid;
|
|
|
|
EMUL_UNLOCK(&emul_lock);
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
if (child_set_tid != NULL) {
|
2006-12-31 12:42:55 +00:00
|
|
|
error = copyout(&p->p_pid, (int *)child_set_tid,
|
|
|
|
sizeof(p->p_pid));
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
if (error != 0) {
|
|
|
|
LIN_SDT_PROBE1(emul, linux_schedtail, copyout_error,
|
|
|
|
error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LIN_SDT_PROBE0(emul, linux_schedtail, return);
|
|
|
|
|
2006-08-15 12:20:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
|
|
|
|
{
|
2006-12-31 12:42:55 +00:00
|
|
|
struct linux_emuldata *em;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(emul, linux_set_tid_address, entry, args->tidptr);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2006-12-31 12:42:55 +00:00
|
|
|
/* find the emuldata */
|
2007-01-07 19:00:38 +00:00
|
|
|
em = em_find(td->td_proc, EMUL_DOLOCK);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
|
|
|
KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
|
|
|
|
|
|
|
|
em->child_clear_tid = args->tidptr;
|
|
|
|
td->td_retval[0] = td->td_proc->p_pid;
|
|
|
|
|
|
|
|
EMUL_UNLOCK(&emul_lock);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE0(emul, linux_set_tid_address, return);
|
2006-08-15 12:20:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-11-22 09:06:59 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
linux_kill_threads(struct thread *td, int sig)
|
|
|
|
{
|
|
|
|
struct linux_emuldata *em, *td_em, *tmp_em;
|
|
|
|
struct proc *sp;
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE2(emul, linux_kill_threads, entry, td, sig);
|
|
|
|
|
2010-11-22 09:06:59 +00:00
|
|
|
td_em = em_find(td->td_proc, EMUL_DONTLOCK);
|
|
|
|
|
|
|
|
KASSERT(td_em != NULL, ("linux_kill_threads: emuldata not found.\n"));
|
|
|
|
|
|
|
|
EMUL_SHARED_RLOCK(&emul_shared_lock);
|
|
|
|
LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) {
|
|
|
|
if (em->pid == td_em->pid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sp = pfind(em->pid);
|
|
|
|
if ((sp->p_flag & P_WEXIT) == 0)
|
2011-09-16 13:58:51 +00:00
|
|
|
kern_psignal(sp, sig);
|
2010-11-22 09:06:59 +00:00
|
|
|
PROC_UNLOCK(sp);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(emul, linux_kill_threads, kill, em->pid);
|
2010-11-22 09:06:59 +00:00
|
|
|
}
|
|
|
|
EMUL_SHARED_RUNLOCK(&emul_shared_lock);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE0(emul, linux_kill_threads, return);
|
2010-11-22 09:06:59 +00:00
|
|
|
}
|