From f2083ce6d6a293a05ce0759944765dde2d5f33bf Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Thu, 13 Jan 2022 10:53:55 +0000 Subject: [PATCH] Support for arguments and environment variables, to be completed. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15353 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- .../main.c | 12 +++++--- .../startup/ARMCMx-SB/compilers/GCC/crt0.S | 11 ++++--- .../ARMCMx-SB/compilers/GCC/ld/ram_sandbox.ld | 2 +- .../ARMCMx-SB/compilers/GCC/ld/rules_code.ld | 2 +- os/sb/host/sbhost.c | 29 +++++++++++++++---- os/sb/host/sbhost.h | 10 +++++-- 6 files changed, 45 insertions(+), 21 deletions(-) diff --git a/demos/STM32/RT-STM32L4R9-DISCOVERY-SB_HOST_STATIC/main.c b/demos/STM32/RT-STM32L4R9-DISCOVERY-SB_HOST_STATIC/main.c index ece85b534..02a6c2e05 100644 --- a/demos/STM32/RT-STM32L4R9-DISCOVERY-SB_HOST_STATIC/main.c +++ b/demos/STM32/RT-STM32L4R9-DISCOVERY-SB_HOST_STATIC/main.c @@ -81,10 +81,12 @@ static const sb_config_t sb_config1 = { .regions = { [0] = { .area = {STARTUP_FLASH1_BASE, STARTUP_FLASH1_SIZE}, + .used = true, .writeable = false }, [1] = { .area = {STARTUP_RAM1_BASE, STARTUP_RAM1_SIZE}, + .used = true, .writeable = true } }, @@ -98,10 +100,12 @@ static const sb_config_t sb_config2 = { .regions = { [0] = { .area = {STARTUP_FLASH2_BASE, STARTUP_FLASH2_SIZE}, + .used = true, .writeable = false }, [1] = { .area = {STARTUP_RAM2_BASE, STARTUP_RAM2_SIZE}, + .used = true, .writeable = true } }, @@ -295,16 +299,16 @@ int main(void) { /* Starting sandboxed thread 1.*/ sb1tp = sbStartThread(&sbx1, "sbx1", - waUnprivileged1, sizeof (waUnprivileged1), - NORMALPRIO - 1); + waUnprivileged1, sizeof (waUnprivileged1), NORMALPRIO - 1, + 0, NULL, NULL); if (sb1tp == NULL) { chSysHalt("sbx1 failed"); } /* Starting sandboxed thread 2.*/ sb2tp = sbStartThread(&sbx2, "sbx2", - waUnprivileged2, sizeof (waUnprivileged2), - NORMALPRIO - 1); + waUnprivileged2, sizeof (waUnprivileged2), NORMALPRIO - 1, + 0, NULL, NULL); if (sb2tp == NULL) { chSysHalt("sbx2 failed"); } diff --git a/os/common/startup/ARMCMx-SB/compilers/GCC/crt0.S b/os/common/startup/ARMCMx-SB/compilers/GCC/crt0.S index 0bb5b8184..9ac20c7a2 100644 --- a/os/common/startup/ARMCMx-SB/compilers/GCC/crt0.S +++ b/os/common/startup/ARMCMx-SB/compilers/GCC/crt0.S @@ -94,12 +94,11 @@ .section .sandbox, "ax" .align 4 - .globl _sandbox -_sandbox: .long 0xFE9154C0 + .globl __sandbox +__sandbox: .long 0xFE9154C0 .long 0x0C4519EF .long 16 - .long 0 - b _crt0_entry + .long __crt0_entry .text /* @@ -107,8 +106,8 @@ _sandbox: .long 0xFE9154C0 */ .align 2 .thumb_func - .global _crt0_entry -_crt0_entry: + .global __crt0_entry +__crt0_entry: /* PSP stack pointers initialization.*/ ldr r0, =__user_psp_end__ diff --git a/os/common/startup/ARMCMx-SB/compilers/GCC/ld/ram_sandbox.ld b/os/common/startup/ARMCMx-SB/compilers/GCC/ld/ram_sandbox.ld index 55512c924..deb0c6659 100644 --- a/os/common/startup/ARMCMx-SB/compilers/GCC/ld/ram_sandbox.ld +++ b/os/common/startup/ARMCMx-SB/compilers/GCC/ld/ram_sandbox.ld @@ -18,7 +18,7 @@ * RAM sandbox memory setup. */ -ENTRY(_crt0_entry) +ENTRY(__crt0_entry) SECTIONS { diff --git a/os/common/startup/ARMCMx-SB/compilers/GCC/ld/rules_code.ld b/os/common/startup/ARMCMx-SB/compilers/GCC/ld/rules_code.ld index 656841029..3b2afa6c0 100644 --- a/os/common/startup/ARMCMx-SB/compilers/GCC/ld/rules_code.ld +++ b/os/common/startup/ARMCMx-SB/compilers/GCC/ld/rules_code.ld @@ -14,7 +14,7 @@ limitations under the License. */ -ENTRY(_crt0_entry) +ENTRY(__crt0_entry) SECTIONS { diff --git a/os/sb/host/sbhost.c b/os/sb/host/sbhost.c index b351dc8ec..d85c3e0e1 100644 --- a/os/sb/host/sbhost.c +++ b/os/sb/host/sbhost.c @@ -124,15 +124,19 @@ void sbObjectInit(sb_class_t *sbcp, const sb_config_t *config) { * @param[out] wsp pointer to a working area dedicated to the thread stack * @param[in] size size of the working area * @param[in] prio the priority level for the new thread + * @param[in] argc number of parameters for the sandbox + * @param[in] argv array of parameters for the sandbox + * @param[in] envp array of environment variables for the sandbox * @return The thread pointer. * @retval NULL if the sandbox thread creation failed. */ thread_t *sbStartThread(sb_class_t *sbcp, const char *name, - void *wsp, size_t size, - tprio_t prio) { + void *wsp, size_t size, tprio_t prio, + int argc, char *argv[], char *envp[]) { thread_t *utp; const sb_header_t *sbhp; const sb_config_t *config = sbcp->config; + uint32_t *sp; /* Header location.*/ sbhp = (const sb_header_t *)(void *)config->regions[config->code_region].area.base; @@ -147,15 +151,28 @@ thread_t *sbStartThread(sb_class_t *sbcp, const char *name, return NULL; } + /* Checking header entry point.*/ + if (!chMemIsSpaceWithinX(&config->regions[config->code_region].area, + (const void *)sbhp->hdr_entry, + (size_t)2)) { + return NULL; + } + + /* Setting up an initial stack for the sandbox.*/ + sp = (uint32_t *)(void *)(config->regions[config->data_region].area.base + + config->regions[config->data_region].area.size); + sp -= 3 * sizeof (uint32_t); + sp[0] = (uint32_t)argc; + sp[1] = (uint32_t)argv; + sp[2] = (uint32_t)envp; + unprivileged_thread_descriptor_t utd = { .name = name, .wbase = (stkalign_t *)wsp, .wend = (stkalign_t *)wsp + (size / sizeof (stkalign_t)), .prio = prio, - .u_pc = (uint32_t)(config->regions[config->code_region].area.base + - sizeof (sb_header_t)) | 1U, - .u_psp = (uint32_t)(config->regions[config->data_region].area.base + - config->regions[config->data_region].area.size), + .u_pc = sbhp->hdr_entry, + .u_psp = (uint32_t)sp, .arg = (void *)sbcp }; #if PORT_SWITCHED_REGIONS_NUMBER > 0 diff --git a/os/sb/host/sbhost.h b/os/sb/host/sbhost.h index af17fd36f..db9cf9e1c 100644 --- a/os/sb/host/sbhost.h +++ b/os/sb/host/sbhost.h @@ -68,10 +68,14 @@ typedef struct { * @brief Header size, inclusive of magic numbers. */ uint32_t hdr_size; + /** + * @brief Entry point address. + */ + uint32_t hdr_entry; /** * @brief Used-defined parameters, defaulted to zero. */ - uint32_t user; + uint32_t user[4]; } sb_header_t; /*===========================================================================*/ @@ -93,8 +97,8 @@ extern "C" { bool sb_is_valid_string_range(sb_class_t *sbcp, const char *s, size_t n); void sbObjectInit(sb_class_t *sbcp, const sb_config_t *config); thread_t *sbStartThread(sb_class_t *sbcp, const char *name, - void *wsp, size_t size, - tprio_t prio); + void *wsp, size_t size, tprio_t prio, + int argc, char *argv[], char *envp[]); bool sbIsThreadRunningX(sb_class_t *sbcp); #if CH_CFG_USE_WAITEXIT == TRUE msg_t sbWaitThread(sb_class_t *sbcp);