2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/binfmt_execmodule.c
|
|
|
|
*
|
2020-03-26 17:40:10 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2009-06-17 22:25:27 +02:00
|
|
|
*
|
2020-03-26 17:40:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-06-17 22:25:27 +02:00
|
|
|
*
|
2020-03-26 17:40:10 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2009-06-17 22:25:27 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-08-11 14:09:21 +02:00
|
|
|
#include <sys/stat.h>
|
2009-12-15 18:06:59 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
2009-06-17 22:25:27 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2023-01-27 12:45:03 +01:00
|
|
|
#include <nuttx/addrenv.h>
|
2009-06-17 22:25:27 +02:00
|
|
|
#include <nuttx/arch.h>
|
2013-01-16 15:14:14 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2020-03-26 17:40:10 +01:00
|
|
|
#include <nuttx/sched.h>
|
2023-04-26 05:44:08 +02:00
|
|
|
#include <nuttx/spawn.h>
|
2012-10-24 22:19:44 +02:00
|
|
|
#include <nuttx/binfmt/binfmt.h>
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2015-11-14 14:29:47 +01:00
|
|
|
#include "binfmt.h"
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2009-06-17 23:15:31 +02:00
|
|
|
#ifndef CONFIG_BINFMT_DISABLE
|
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2020-01-07 16:54:35 +01:00
|
|
|
|
2013-01-27 16:52:58 +01:00
|
|
|
/* If C++ constructors are used, then CONFIG_SCHED_STARTHOOK must also be
|
|
|
|
* selected be the start hook is used to schedule execution of the
|
|
|
|
* constructors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(CONFIG_BINFMT_CONSTRUCTORS) && !defined(CONFIG_SCHED_STARTHOOK)
|
2019-09-20 02:19:18 +02:00
|
|
|
# error "CONFIG_SCHED_STARTHOOK must be defined to use constructors"
|
2013-01-27 16:52:58 +01:00
|
|
|
#endif
|
2009-06-17 22:25:27 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: exec_ctors
|
|
|
|
*
|
|
|
|
* Description:
|
2013-01-27 16:52:58 +01:00
|
|
|
* Execute C++ static constructors. This function is registered as a
|
|
|
|
* start hook and runs on the thread of the newly created task before
|
|
|
|
* the new task's main function is called.
|
2012-10-29 20:32:05 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-08-05 16:09:54 +02:00
|
|
|
* arg - Argument is instance of load state info structure cast to void *.
|
2012-10-29 20:32:05 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
2013-01-27 16:52:58 +01:00
|
|
|
static void exec_ctors(FAR void *arg)
|
2012-10-29 20:32:05 +01:00
|
|
|
{
|
2013-01-27 16:52:58 +01:00
|
|
|
FAR const struct binary_s *binp = (FAR const struct binary_s *)arg;
|
2012-10-29 21:43:35 +01:00
|
|
|
binfmt_ctor_t *ctor = binp->ctors;
|
2012-10-29 20:32:05 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Execute each constructor */
|
|
|
|
|
|
|
|
for (i = 0; i < binp->nctors; i++)
|
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
binfo("Calling ctor %d at %p\n", i, ctor);
|
2012-10-29 20:32:05 +01:00
|
|
|
|
|
|
|
(*ctor)();
|
|
|
|
ctor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: exec_module
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Execute a module that has been loaded into memory by load_module().
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-10-02 23:30:55 +02:00
|
|
|
* This is a NuttX internal function so it follows the convention that
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
2009-06-17 22:25:27 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-01-27 12:45:03 +01:00
|
|
|
int exec_module(FAR struct binary_s *binp,
|
2022-04-04 12:11:06 +02:00
|
|
|
FAR const char *filename, FAR char * const *argv,
|
2023-04-26 05:44:08 +02:00
|
|
|
FAR char * const *envp,
|
|
|
|
FAR const posix_spawn_file_actions_t *actions)
|
2009-06-17 22:25:27 +02:00
|
|
|
{
|
2013-02-04 22:24:00 +01:00
|
|
|
FAR struct task_tcb_s *tcb;
|
2014-09-11 18:31:12 +02:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
2023-04-13 10:48:06 +02:00
|
|
|
FAR struct arch_addrenv_s *addrenv = &binp->addrenv->addrenv;
|
2022-03-17 12:55:50 +01:00
|
|
|
FAR void *vheap;
|
2014-09-10 01:32:32 +02:00
|
|
|
#endif
|
2022-10-19 21:27:21 +02:00
|
|
|
FAR void *stackaddr = NULL;
|
2013-02-04 19:46:28 +01:00
|
|
|
pid_t pid;
|
|
|
|
int ret;
|
2009-06-17 22:25:27 +02:00
|
|
|
|
|
|
|
/* Sanity checking */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-10-29 20:32:05 +01:00
|
|
|
if (!binp || !binp->entrypt || binp->stacksize <= 0)
|
2009-06-17 22:25:27 +02:00
|
|
|
{
|
2017-10-02 23:30:55 +02:00
|
|
|
return -EINVAL;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-05-24 11:12:06 +02:00
|
|
|
binfo("Executing %s\n", filename);
|
2009-06-17 22:25:27 +02:00
|
|
|
|
|
|
|
/* Allocate a TCB for the new task. */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
tcb = kmm_zalloc(sizeof(struct task_tcb_s));
|
2009-06-17 22:25:27 +02:00
|
|
|
if (!tcb)
|
|
|
|
{
|
2017-10-02 23:30:55 +02:00
|
|
|
return -ENOMEM;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
|
|
|
|
2022-10-17 17:24:34 +02:00
|
|
|
ret = binfmt_copyargv(&argv, argv);
|
|
|
|
if (ret < 0)
|
2021-05-24 09:06:29 +02:00
|
|
|
{
|
2022-10-17 17:24:34 +02:00
|
|
|
goto errout_with_tcb;
|
2021-05-24 09:06:29 +02:00
|
|
|
}
|
|
|
|
|
2022-04-04 12:11:06 +02:00
|
|
|
/* Make a copy of the environment here */
|
|
|
|
|
2022-10-17 17:24:34 +02:00
|
|
|
if (envp == NULL)
|
|
|
|
{
|
|
|
|
envp = environ;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = binfmt_copyenv(&envp, envp);
|
|
|
|
if (ret < 0)
|
2022-04-04 12:11:06 +02:00
|
|
|
{
|
2022-10-17 17:24:34 +02:00
|
|
|
goto errout_with_args;
|
2022-04-04 12:11:06 +02:00
|
|
|
}
|
|
|
|
|
2014-09-14 20:40:09 +02:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
2014-09-10 23:55:36 +02:00
|
|
|
/* Instantiate the address environment containing the user heap */
|
|
|
|
|
2023-04-19 14:29:00 +02:00
|
|
|
ret = addrenv_select(binp->addrenv, &binp->oldenv);
|
2014-09-10 01:32:32 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-01-27 12:45:03 +01:00
|
|
|
berr("ERROR: addrenv_select() failed: %d\n", ret);
|
2022-04-04 12:11:06 +02:00
|
|
|
goto errout_with_envp;
|
2014-09-10 01:32:32 +02:00
|
|
|
}
|
2022-02-11 07:45:09 +01:00
|
|
|
|
2023-01-27 12:45:03 +01:00
|
|
|
ret = up_addrenv_vheap(addrenv, &vheap);
|
2022-03-17 12:55:50 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: up_addrenv_vheap() failed: %d\n", ret);
|
|
|
|
goto errout_with_addrenv;
|
|
|
|
}
|
|
|
|
|
2022-05-02 08:12:46 +02:00
|
|
|
binfo("Initialize the user heap (heapsize=%zu)\n",
|
2023-01-27 12:45:03 +01:00
|
|
|
up_addrenv_heapsize(addrenv));
|
|
|
|
umm_initialize(vheap, up_addrenv_heapsize(addrenv));
|
2014-09-10 01:32:32 +02:00
|
|
|
#endif
|
|
|
|
|
2023-05-31 13:39:09 +02:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_ARCH_KERNEL_STACK)
|
|
|
|
/* Allocate the kernel stack */
|
|
|
|
|
|
|
|
ret = up_addrenv_kstackalloc(&tcb->cmn);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: up_addrenv_kstackalloc() failed: %d\n", ret);
|
|
|
|
goto errout_with_addrenv;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-29 10:26:48 +02:00
|
|
|
/* Note that tcb->flags are not modified. 0=normal task */
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2020-06-29 10:26:48 +02:00
|
|
|
/* tcb->flags |= TCB_FLAG_TTYPE_TASK; */
|
2009-06-17 22:25:27 +02:00
|
|
|
|
|
|
|
/* Initialize the task */
|
|
|
|
|
2022-10-19 21:27:21 +02:00
|
|
|
#ifndef CONFIG_BUILD_KERNEL
|
|
|
|
stackaddr = binp->stackaddr;
|
|
|
|
#endif
|
|
|
|
|
2021-06-15 19:38:07 +02:00
|
|
|
if (argv && argv[0])
|
|
|
|
{
|
2022-10-19 21:27:21 +02:00
|
|
|
ret = nxtask_init(tcb, argv[0], binp->priority, stackaddr,
|
2022-04-04 12:11:06 +02:00
|
|
|
binp->stacksize, binp->entrypt, &argv[1], envp);
|
2021-06-15 19:38:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-19 21:27:21 +02:00
|
|
|
ret = nxtask_init(tcb, filename, binp->priority, stackaddr,
|
2022-04-04 12:11:06 +02:00
|
|
|
binp->stacksize, binp->entrypt, argv, envp);
|
2021-06-15 19:38:07 +02:00
|
|
|
}
|
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-05-25 23:30:40 +02:00
|
|
|
berr("nxtask_init() failed: %d\n", ret);
|
2014-09-14 22:10:23 +02:00
|
|
|
goto errout_with_addrenv;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
|
|
|
|
2022-04-04 12:11:06 +02:00
|
|
|
/* The copied argv and envp can now be released */
|
|
|
|
|
2022-04-08 10:10:01 +02:00
|
|
|
binfmt_freeargv(argv);
|
2022-04-04 12:11:06 +02:00
|
|
|
binfmt_freeenv(envp);
|
2022-04-08 10:10:01 +02:00
|
|
|
|
2023-04-26 05:44:08 +02:00
|
|
|
/* Perform file actions */
|
|
|
|
|
|
|
|
if (actions != NULL)
|
|
|
|
{
|
|
|
|
ret = spawn_file_actions(&tcb->cmn, actions);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_tcbinit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 20:16:05 +02:00
|
|
|
#ifdef CONFIG_PIC
|
2012-10-29 20:32:05 +01:00
|
|
|
/* Add the D-Space address as the PIC base address. By convention, this
|
|
|
|
* must be the first allocated address space.
|
|
|
|
*/
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
tcb->cmn.dspace = binp->alloc[0];
|
2009-06-17 22:25:27 +02:00
|
|
|
|
|
|
|
/* Re-initialize the task's initial state to account for the new PIC base */
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
up_initial_state(&tcb->cmn);
|
2009-06-17 23:15:31 +02:00
|
|
|
#endif
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2014-08-26 20:16:05 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
2023-01-27 09:28:19 +01:00
|
|
|
/* Attach the address environment to the new task */
|
2012-12-19 18:54:26 +01:00
|
|
|
|
2023-04-13 10:48:06 +02:00
|
|
|
ret = addrenv_attach((FAR struct tcb_s *)tcb, binp->addrenv);
|
2012-12-19 18:54:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-01-27 09:28:19 +01:00
|
|
|
berr("ERROR: addrenv_attach() failed: %d\n", ret);
|
2014-09-14 19:19:34 +02:00
|
|
|
goto errout_with_tcbinit;
|
2012-12-19 18:54:26 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-08-26 20:16:05 +02:00
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
2013-01-27 16:52:58 +01:00
|
|
|
/* Setup a start hook that will execute all of the C++ static constructors
|
|
|
|
* on the newly created thread. The struct binary_s must persist at least
|
|
|
|
* until the new task has been started.
|
|
|
|
*/
|
2012-10-29 20:32:05 +01:00
|
|
|
|
2017-10-02 22:04:25 +02:00
|
|
|
if (binp->nctors > 0)
|
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
nxtask_starthook(tcb, exec_ctors, binp);
|
2017-10-02 22:04:25 +02:00
|
|
|
}
|
2012-10-29 20:32:05 +01:00
|
|
|
#endif
|
|
|
|
|
2013-01-27 16:52:58 +01:00
|
|
|
/* Get the assigned pid before we start the task */
|
|
|
|
|
2013-02-04 22:24:00 +01:00
|
|
|
pid = tcb->cmn.pid;
|
2013-01-27 16:52:58 +01:00
|
|
|
|
2023-08-11 14:09:21 +02:00
|
|
|
#ifdef CONFIG_SCHED_USER_IDENTITY
|
|
|
|
if (binp->mode & S_ISUID)
|
|
|
|
{
|
|
|
|
tcb->cmn.group->tg_euid = binp->uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (binp->mode & S_ISGID)
|
|
|
|
{
|
|
|
|
tcb->cmn.group->tg_egid = binp->gid;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-06-17 22:25:27 +02:00
|
|
|
/* Then activate the task at the provided priority */
|
|
|
|
|
2020-06-04 14:19:40 +02:00
|
|
|
nxtask_activate((FAR struct tcb_s *)tcb);
|
2012-10-29 20:32:05 +01:00
|
|
|
|
2014-09-11 18:31:12 +02:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
2014-09-10 23:55:36 +02:00
|
|
|
/* Restore the address environment of the caller */
|
|
|
|
|
2023-04-19 14:29:00 +02:00
|
|
|
ret = addrenv_restore(binp->oldenv);
|
2014-09-10 23:55:36 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-01-27 12:45:03 +01:00
|
|
|
berr("ERROR: addrenv_restore() failed: %d\n", ret);
|
2014-09-14 19:19:34 +02:00
|
|
|
goto errout_with_tcbinit;
|
2014-09-10 23:55:36 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-06-18 22:20:04 +02:00
|
|
|
return pid;
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2014-09-14 19:19:34 +02:00
|
|
|
errout_with_tcbinit:
|
2023-04-26 05:18:54 +02:00
|
|
|
#ifndef CONFIG_BUILD_KERNEL
|
|
|
|
if (binp->stackaddr != NULL)
|
|
|
|
{
|
|
|
|
tcb->cmn.stack_alloc_ptr = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nxtask_uninit(tcb);
|
2017-10-02 23:30:55 +02:00
|
|
|
return ret;
|
2009-06-17 22:25:27 +02:00
|
|
|
|
2014-09-10 16:41:01 +02:00
|
|
|
errout_with_addrenv:
|
2014-09-11 18:31:12 +02:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
|
2023-04-19 14:29:00 +02:00
|
|
|
addrenv_restore(binp->oldenv);
|
2022-04-04 12:11:06 +02:00
|
|
|
errout_with_envp:
|
2014-09-11 18:31:12 +02:00
|
|
|
#endif
|
2022-04-04 12:11:06 +02:00
|
|
|
binfmt_freeenv(envp);
|
2022-04-08 10:10:01 +02:00
|
|
|
errout_with_args:
|
|
|
|
binfmt_freeargv(argv);
|
2021-05-24 09:06:29 +02:00
|
|
|
errout_with_tcb:
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(tcb);
|
2017-10-02 23:30:55 +02:00
|
|
|
return ret;
|
2009-06-17 22:25:27 +02:00
|
|
|
}
|
|
|
|
|
2009-06-17 23:15:31 +02:00
|
|
|
#endif /* CONFIG_BINFMT_DISABLE */
|