2006-08-15 12:20:59 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2006 Roman Divacky
|
2015-05-24 14:53:16 +00:00
|
|
|
* Copyright (c) 2013 Dmitry Chagin
|
2006-08-15 12:20:59 +00:00
|
|
|
* 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 <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/imgact.h>
|
2007-02-02 08:58:16 +00:00
|
|
|
#include <sys/kernel.h>
|
2015-05-24 14:53:16 +00:00
|
|
|
#include <sys/ktr.h>
|
2006-08-15 12:20:59 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#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
|
|
|
|
2008-05-13 20:01:27 +00:00
|
|
|
#include <compat/linux/linux_emul.h>
|
2013-01-29 18:41:30 +00:00
|
|
|
#include <compat/linux/linux_misc.h>
|
2015-05-24 14:53:16 +00:00
|
|
|
#include <compat/linux/linux_util.h>
|
2008-05-13 20:01:27 +00:00
|
|
|
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
/*
|
2015-05-24 15:54:58 +00:00
|
|
|
* This returns reference to the thread emuldata entry (if found)
|
2015-05-24 14:53:16 +00:00
|
|
|
*
|
|
|
|
* Hold PROC_LOCK when referencing emuldata from other threads.
|
|
|
|
*/
|
2006-08-15 12:20:59 +00:00
|
|
|
struct linux_emuldata *
|
2015-05-24 14:53:16 +00:00
|
|
|
em_find(struct thread *td)
|
2006-08-15 12:20:59 +00:00
|
|
|
{
|
|
|
|
struct linux_emuldata *em;
|
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
em = td->td_emuldata;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
|
|
|
return (em);
|
|
|
|
}
|
|
|
|
|
2015-05-24 15:54:58 +00:00
|
|
|
/*
|
|
|
|
* This returns reference to the proc pemuldata entry (if found)
|
|
|
|
*
|
|
|
|
* Hold PROC_LOCK when referencing proc pemuldata from other threads.
|
|
|
|
* Hold LINUX_PEM_LOCK wher referencing pemuldata members.
|
|
|
|
*/
|
|
|
|
struct linux_pemuldata *
|
|
|
|
pem_find(struct proc *p)
|
|
|
|
{
|
|
|
|
struct linux_pemuldata *pem;
|
|
|
|
|
|
|
|
pem = p->p_emuldata;
|
|
|
|
|
|
|
|
return (pem);
|
|
|
|
}
|
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
void
|
|
|
|
linux_proc_init(struct thread *td, struct thread *newtd, int flags)
|
2006-08-15 12:20:59 +00:00
|
|
|
{
|
2015-05-24 14:53:16 +00:00
|
|
|
struct linux_emuldata *em;
|
2015-05-24 15:54:58 +00:00
|
|
|
struct linux_pemuldata *pem;
|
2015-05-24 16:41:39 +00:00
|
|
|
struct epoll_emuldata *emd;
|
2015-05-24 17:08:25 +00:00
|
|
|
struct proc *p;
|
2012-05-05 19:42:38 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
if (newtd != NULL) {
|
2015-05-24 17:08:25 +00:00
|
|
|
p = newtd->td_proc;
|
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
/* non-exec call */
|
|
|
|
em = malloc(sizeof(*em), M_TEMP, M_WAITOK | M_ZERO);
|
2007-03-02 00:08:47 +00:00
|
|
|
if (flags & LINUX_CLONE_THREAD) {
|
2015-05-24 16:41:39 +00:00
|
|
|
LINUX_CTR1(proc_init, "thread newtd(%d)",
|
|
|
|
newtd->td_tid);
|
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
em->em_tid = newtd->td_tid;
|
|
|
|
} else {
|
2015-05-24 17:08:25 +00:00
|
|
|
LINUX_CTR1(proc_init, "fork newtd(%d)", p->p_pid);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
2015-05-24 17:08:25 +00:00
|
|
|
em->em_tid = p->p_pid;
|
2015-05-24 15:54:58 +00:00
|
|
|
|
2015-05-24 16:14:41 +00:00
|
|
|
pem = malloc(sizeof(*pem), M_LINUX, M_WAITOK | M_ZERO);
|
2015-05-24 15:54:58 +00:00
|
|
|
sx_init(&pem->pem_sx, "lpemlk");
|
2015-05-24 17:08:25 +00:00
|
|
|
p->p_emuldata = pem;
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
2015-05-24 14:53:16 +00:00
|
|
|
newtd->td_emuldata = em;
|
2006-08-15 12:20:59 +00:00
|
|
|
} else {
|
2015-05-24 17:08:25 +00:00
|
|
|
p = td->td_proc;
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
/* exec */
|
2015-05-24 17:08:25 +00:00
|
|
|
LINUX_CTR1(proc_init, "exec newtd(%d)", p->p_pid);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
2006-08-15 12:20:59 +00:00
|
|
|
/* lookup the old one */
|
2015-05-24 14:53:16 +00:00
|
|
|
em = em_find(td);
|
2006-08-15 12:20:59 +00:00
|
|
|
KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n"));
|
2015-05-24 14:53:16 +00:00
|
|
|
|
2015-05-24 17:08:25 +00:00
|
|
|
em->em_tid = p->p_pid;
|
2015-05-24 17:07:10 +00:00
|
|
|
em->flags = 0;
|
|
|
|
em->pdeath_signal = 0;
|
|
|
|
em->robust_futexes = NULL;
|
|
|
|
em->child_clear_tid = NULL;
|
|
|
|
em->child_set_tid = NULL;
|
2015-05-24 16:41:39 +00:00
|
|
|
|
|
|
|
/* epoll should be destroyed in a case of exec. */
|
2015-05-24 17:08:25 +00:00
|
|
|
pem = pem_find(p);
|
2015-05-24 16:41:39 +00:00
|
|
|
KASSERT(pem != NULL, ("proc_exit: proc emuldata not found.\n"));
|
|
|
|
|
|
|
|
if (pem->epoll != NULL) {
|
|
|
|
emd = pem->epoll;
|
|
|
|
pem->epoll = NULL;
|
|
|
|
free(emd, M_EPOLL);
|
|
|
|
}
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
linux_proc_exit(void *arg __unused, struct proc *p)
|
|
|
|
{
|
2015-05-24 15:54:58 +00:00
|
|
|
struct linux_pemuldata *pem;
|
2015-05-24 16:41:39 +00:00
|
|
|
struct epoll_emuldata *emd;
|
2015-05-24 14:53:16 +00:00
|
|
|
struct thread *td = curthread;
|
|
|
|
|
2015-05-24 15:54:58 +00:00
|
|
|
if (__predict_false(SV_CURPROC_ABI() != SV_ABI_LINUX))
|
|
|
|
return;
|
|
|
|
|
2015-05-24 17:09:07 +00:00
|
|
|
LINUX_CTR3(proc_exit, "thread(%d) proc(%d) p %p",
|
|
|
|
td->td_tid, p->p_pid, p);
|
|
|
|
|
2015-05-24 15:54:58 +00:00
|
|
|
pem = pem_find(p);
|
|
|
|
if (pem == NULL)
|
|
|
|
return;
|
|
|
|
(p->p_sysent->sv_thread_detach)(td);
|
|
|
|
|
|
|
|
p->p_emuldata = NULL;
|
|
|
|
|
2015-05-24 16:41:39 +00:00
|
|
|
if (pem->epoll != NULL) {
|
|
|
|
emd = pem->epoll;
|
|
|
|
pem->epoll = NULL;
|
|
|
|
free(emd, M_EPOLL);
|
|
|
|
}
|
|
|
|
|
2015-05-24 15:54:58 +00:00
|
|
|
sx_destroy(&pem->pem_sx);
|
2015-05-24 16:14:41 +00:00
|
|
|
free(pem, M_LINUX);
|
2015-05-24 14:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
linux_common_execve(struct thread *td, struct image_args *eargs)
|
|
|
|
{
|
2015-05-24 15:54:58 +00:00
|
|
|
struct linux_pemuldata *pem;
|
2015-05-24 16:41:39 +00:00
|
|
|
struct epoll_emuldata *emd;
|
2015-05-25 20:44:46 +00:00
|
|
|
struct vmspace *oldvmspace;
|
2015-05-24 14:53:16 +00:00
|
|
|
struct linux_emuldata *em;
|
|
|
|
struct proc *p;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
p = td->td_proc;
|
|
|
|
|
2015-05-25 20:44:46 +00:00
|
|
|
error = pre_execve(td, &oldvmspace);
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
error = kern_execve(td, eargs, NULL);
|
2015-05-25 20:44:46 +00:00
|
|
|
post_execve(td, error, oldvmspace);
|
2015-05-24 14:53:16 +00:00
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
/*
|
|
|
|
* In a case of transition from Linux binary execing to
|
2015-05-24 15:54:58 +00:00
|
|
|
* FreeBSD binary we destroy linux emuldata thread & proc entries.
|
2015-05-24 14:53:16 +00:00
|
|
|
*/
|
|
|
|
if (SV_CURPROC_ABI() != SV_ABI_LINUX) {
|
|
|
|
PROC_LOCK(p);
|
|
|
|
em = em_find(td);
|
2015-05-24 15:54:58 +00:00
|
|
|
KASSERT(em != NULL, ("proc_exec: thread emuldata not found.\n"));
|
2015-05-24 14:53:16 +00:00
|
|
|
td->td_emuldata = NULL;
|
2015-05-24 15:54:58 +00:00
|
|
|
|
|
|
|
pem = pem_find(p);
|
|
|
|
KASSERT(pem != NULL, ("proc_exec: proc pemuldata not found.\n"));
|
|
|
|
p->p_emuldata = NULL;
|
2006-09-09 16:55:55 +00:00
|
|
|
PROC_UNLOCK(p);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2015-05-24 16:41:39 +00:00
|
|
|
if (pem->epoll != NULL) {
|
|
|
|
emd = pem->epoll;
|
|
|
|
pem->epoll = NULL;
|
|
|
|
free(emd, M_EPOLL);
|
|
|
|
}
|
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
free(em, M_TEMP);
|
2015-05-24 16:14:41 +00:00
|
|
|
free(pem, M_LINUX);
|
2015-05-24 14:53:16 +00:00
|
|
|
}
|
2006-12-31 12:42:55 +00:00
|
|
|
return (0);
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
void
|
|
|
|
linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
|
2006-08-15 12:20:59 +00:00
|
|
|
{
|
2015-05-24 14:53:16 +00:00
|
|
|
struct thread *td = curthread;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
/*
|
|
|
|
* In a case of execing to linux binary we create linux
|
|
|
|
* emuldata thread entry.
|
|
|
|
*/
|
|
|
|
if (__predict_false((imgp->sysent->sv_flags & SV_ABI_MASK) ==
|
|
|
|
SV_ABI_LINUX)) {
|
2015-05-24 16:41:39 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
if (SV_PROC_ABI(p) == SV_ABI_LINUX)
|
|
|
|
linux_proc_init(td, NULL, 0);
|
|
|
|
else
|
|
|
|
linux_proc_init(td, td, 0);
|
2006-10-28 10:59:59 +00:00
|
|
|
}
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
void
|
|
|
|
linux_thread_dtor(void *arg __unused, struct thread *td)
|
2006-08-15 12:20:59 +00:00
|
|
|
{
|
2015-05-24 14:53:16 +00:00
|
|
|
struct linux_emuldata *em;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
em = em_find(td);
|
|
|
|
if (em == NULL)
|
|
|
|
return;
|
|
|
|
td->td_emuldata = NULL;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2015-05-24 17:09:07 +00:00
|
|
|
LINUX_CTR1(thread_dtor, "thread(%d)", em->em_tid);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
free(em, M_TEMP);
|
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;
|
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
p = td->td_proc;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2015-05-24 14:53:16 +00:00
|
|
|
em = em_find(td);
|
2015-05-24 15:54:58 +00:00
|
|
|
KASSERT(em != NULL, ("linux_schedtail: thread emuldata not found.\n"));
|
2006-08-15 12:20:59 +00:00
|
|
|
child_set_tid = em->child_set_tid;
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
if (child_set_tid != NULL) {
|
2015-05-24 17:05:59 +00:00
|
|
|
error = copyout(&em->em_tid, child_set_tid,
|
2015-05-24 14:53:16 +00:00
|
|
|
sizeof(em->em_tid));
|
2015-05-24 17:09:07 +00:00
|
|
|
LINUX_CTR4(schedtail, "thread(%d) %p stored %d error %d",
|
2015-05-24 14:53:16 +00:00
|
|
|
td->td_tid, child_set_tid, em->em_tid, error);
|
|
|
|
} else
|
2015-05-24 17:09:07 +00:00
|
|
|
LINUX_CTR1(schedtail, "thread(%d)", em->em_tid);
|
2010-11-22 09:06:59 +00:00
|
|
|
}
|