Skip to content

Commit

Permalink
core: replace thread_std_smc_handler_ptr with weak function
Browse files Browse the repository at this point in the history
Removes registration of platform specific standard SMC entry function in
thread_std_smc_handler_ptr. Instead a __weak overridable tee_entry_std()
is provided. Platforms that need a special tee_entry_std() (currently on
some STM platform) provides their own tee_entry_std() instead which at
the end should call __tee_entry_std() which does the generic standard
call handling.

With this also std_smc is removed from struct thread_handlers and
consequently all platforms are updated to stop using that field.

Acked-by: Jerome Forissier <[email protected]>
Signed-off-by: Jens Wiklander <[email protected]>
  • Loading branch information
jenswi-linaro authored and jforissier committed Aug 26, 2019
1 parent 2786f14 commit 2dd2ca5
Show file tree
Hide file tree
Showing 30 changed files with 140 additions and 159 deletions.
6 changes: 1 addition & 5 deletions core/arch/arm/include/kernel/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,11 @@ typedef unsigned long (*thread_pm_handler_t)(unsigned long a0,
unsigned long a1);
struct thread_handlers {
/*
* stdcall and fastcall are called as regular functions and
* fastcall is called as a regular function and
* normal ARM Calling Convention applies. Return values are passed
* args->param{1-3} and forwarded into r0-r3 when returned to
* non-secure world.
*
* stdcall handles calls which can be preemted from non-secure
* world. This handler is executed with a large stack.
*
* fastcall handles fast calls which can't be preemted. This
* handler is executed with a limited stack. This handler must not
* cause any aborts or reenenable native interrupts which are
Expand All @@ -235,7 +232,6 @@ struct thread_handlers {
* on different stacks allowing native interrupts to be enabled during
* a fastcall.
*/
thread_smc_handler_t std_smc;
thread_smc_handler_t fast_smc;

/*
Expand Down
11 changes: 9 additions & 2 deletions core/arch/arm/include/tee/entry_std.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
#define TEE_ENTRY_STD_H

#include <kernel/thread.h>
#include <optee_msg.h>

/* Standard call entry */
void tee_entry_std(struct thread_smc_args *args);
/*
* Standard call entry, __weak, overridable. If overridden should call
* __tee_entry_std() at the end in order to handle the standard functions.
*
* These functions are called in a normal thread context.
*/
uint32_t tee_entry_std(struct optee_msg_arg *arg, uint32_t num_params);
uint32_t __tee_entry_std(struct optee_msg_arg *arg, uint32_t num_params);

/* Get list head for sessions opened from non-secure */
void nsec_sessions_list_head(struct tee_ta_session_head **open_sessions);
Expand Down
5 changes: 0 additions & 5 deletions core/arch/arm/kernel/link_dummies.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ tee_svc_handler(struct thread_svc_regs *regs __unused)
{
}

void __section(".text.dummy.tee_entry_std")
tee_entry_std(struct thread_smc_args *smc_args __unused)
{
}

TEE_Result __section(".text.dummy.init_teecore") init_teecore(void)
{
return TEE_SUCCESS;
Expand Down
2 changes: 0 additions & 2 deletions core/arch/arm/kernel/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ const uint32_t stack_tmp_stride = sizeof(stack_tmp[0]);
KEEP_PAGER(stack_tmp_export);
KEEP_PAGER(stack_tmp_stride);

thread_smc_handler_t thread_std_smc_handler_ptr __nex_bss;
thread_smc_handler_t thread_fast_smc_handler_ptr __nex_bss;
thread_nintr_handler_t thread_nintr_handler_ptr __nex_bss;
thread_pm_handler_t thread_cpu_on_handler_ptr __nex_bss;
Expand Down Expand Up @@ -764,7 +763,6 @@ int thread_get_id(void)

static void init_handlers(const struct thread_handlers *handlers)
{
thread_std_smc_handler_ptr = handlers->std_smc;
thread_fast_smc_handler_ptr = handlers->fast_smc;
thread_nintr_handler_ptr = handlers->nintr;
thread_cpu_on_handler_ptr = handlers->cpu_on;
Expand Down
102 changes: 101 additions & 1 deletion core/arch/arm/kernel/thread_optee_smc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <assert.h>
#include <compiler.h>
#include <io.h>
#include <kernel/misc.h>
#include <kernel/msg_param.h>
#include <kernel/thread.h>
Expand All @@ -15,6 +16,7 @@
#include <sm/optee_smc.h>
#include <sm/sm.h>
#include <string.h>
#include <tee/entry_std.h>
#include <tee/tee_cryp_utl.h>
#include <tee/tee_fs_rpc.h>

Expand Down Expand Up @@ -92,6 +94,104 @@ static void thread_rpc_free_arg(uint64_t cookie)
}
}

static struct mobj *get_cmd_buffer(paddr_t parg, uint32_t *num_params)
{
struct optee_msg_arg *arg;
size_t args_size;

arg = phys_to_virt(parg, MEM_AREA_NSEC_SHM);
if (!arg)
return NULL;

*num_params = READ_ONCE(arg->num_params);
if (*num_params > OPTEE_MSG_MAX_NUM_PARAMS)
return NULL;

args_size = OPTEE_MSG_GET_ARG_SIZE(*num_params);

return mobj_shm_alloc(parg, args_size, 0);
}

#ifdef CFG_CORE_DYN_SHM
static struct mobj *map_cmd_buffer(paddr_t parg, uint32_t *num_params)
{
struct mobj *mobj;
struct optee_msg_arg *arg;
size_t args_size;

assert(!(parg & SMALL_PAGE_MASK));
/* mobj_mapped_shm_alloc checks if parg resides in nonsec ddr */
mobj = mobj_mapped_shm_alloc(&parg, 1, 0, 0);
if (!mobj)
return NULL;

arg = mobj_get_va(mobj, 0);
if (!arg)
goto err;

*num_params = READ_ONCE(arg->num_params);
if (*num_params > OPTEE_MSG_MAX_NUM_PARAMS)
goto err;

args_size = OPTEE_MSG_GET_ARG_SIZE(*num_params);
if (args_size > SMALL_PAGE_SIZE) {
EMSG("Command buffer spans across page boundary");
goto err;
}

return mobj;
err:
mobj_free(mobj);
return NULL;
}
#else
static struct mobj *map_cmd_buffer(paddr_t pargi __unused,
uint32_t *num_params __unused)
{
return NULL;
}
#endif /*CFG_CORE_DYN_SHM*/

static void std_smc_entry(struct thread_smc_args *smc_args)
{
paddr_t parg = 0;
struct optee_msg_arg *arg = NULL;
uint32_t num_params = 0;
struct mobj *mobj = NULL;

if (smc_args->a0 != OPTEE_SMC_CALL_WITH_ARG) {
EMSG("Unknown SMC 0x%" PRIx64, (uint64_t)smc_args->a0);
DMSG("Expected 0x%x", OPTEE_SMC_CALL_WITH_ARG);
smc_args->a0 = OPTEE_SMC_RETURN_EBADCMD;
return;
}
parg = (uint64_t)smc_args->a1 << 32 | smc_args->a2;

/* Check if this region is in static shared space */
if (core_pbuf_is(CORE_MEM_NSEC_SHM, parg,
sizeof(struct optee_msg_arg))) {
mobj = get_cmd_buffer(parg, &num_params);
} else {
if (parg & SMALL_PAGE_MASK) {
smc_args->a0 = OPTEE_SMC_RETURN_EBADADDR;
return;
}
mobj = map_cmd_buffer(parg, &num_params);
}

if (!mobj || !ALIGNMENT_IS_OK(parg, struct optee_msg_arg)) {
EMSG("Bad arg address 0x%" PRIxPA, parg);
smc_args->a0 = OPTEE_SMC_RETURN_EBADADDR;
mobj_free(mobj);
return;
}

arg = mobj_get_va(mobj, 0);
assert(arg && mobj_is_nonsec(mobj));
smc_args->a0 = tee_entry_std(arg, num_params);
mobj_free(mobj);
}

/*
* Helper routine for the assembly function thread_std_smc_entry()
*
Expand All @@ -103,7 +203,7 @@ void __weak __thread_std_smc_entry(struct thread_smc_args *args)
#ifdef CFG_VIRTUALIZATION
virt_on_stdcall();
#endif
thread_std_smc_handler_ptr(args);
std_smc_entry(args);

if (args->a0 == OPTEE_SMC_RETURN_OK) {
struct thread_ctx *thr = threads + thread_get_id();
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/kernel/thread_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ struct thread_ctx {
extern const void *stack_tmp_export;
extern const uint32_t stack_tmp_stride;
extern struct thread_ctx threads[];
extern thread_smc_handler_t thread_std_smc_handler_ptr;
extern thread_smc_handler_t thread_fast_smc_handler_ptr;
extern thread_nintr_handler_t thread_nintr_handler_ptr;
extern thread_pm_handler_t thread_cpu_on_handler_ptr;
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-bcm/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
static void secure_intr_handler(void);

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = secure_intr_handler,
.cpu_on = cpu_on_handler,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-d02/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
static void main_fiq(void);

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = cpu_on_handler,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-hikey/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
static void main_fiq(void);

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = cpu_on_handler,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-hisilicon/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
static void main_fiq(void);

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = pm_panic,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-imx/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ static void main_fiq(void);
static struct gic_data gic_data;

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
#if defined(CFG_WITH_ARM_TRUSTED_FW)
Expand Down
2 changes: 0 additions & 2 deletions core/arch/arm/plat-k3/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ static void main_fiq(void)
}

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = cpu_on_handler,
.cpu_off = pm_do_nothing,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-ls/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
static void main_fiq(void);

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
#if defined(CFG_WITH_ARM_TRUSTED_FW)
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-marvell/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
static void main_fiq(void);

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = cpu_on_handler,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-mediatek/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ register_phys_mem_pgdir(MEM_AREA_IO_NSEC,
CONSOLE_UART_BASE, SERIAL8250_UART_REG_SIZE);

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = cpu_on_handler,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-poplar/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
static void main_fiq(void);

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = cpu_on_handler,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-rcar/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ register_dynamic_shm(NSEC_DDR_3_BASE, NSEC_DDR_3_SIZE);
static void main_fiq(void);

static const struct thread_handlers handlers __nex_data = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = cpu_on_handler,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-rockchip/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ static void main_fiq(void)
}

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = pm_do_nothing,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-rpi3/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ static void main_fiq(void)
}

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = cpu_on_handler,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-sam/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ static void main_fiq(void)
}

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = pm_panic,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-sprd/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ register_phys_mem_pgdir(MEM_AREA_IO_SEC,
static void main_fiq(void);

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = cpu_on_handler,
Expand Down
9 changes: 5 additions & 4 deletions core/arch/arm/plat-stm/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ static struct stih_asc_pd console_data;
static void main_fiq(void);

#if defined(PLATFORM_FLAVOR_b2260)
#define stm_tee_entry_std tee_entry_std
static bool ns_resources_ready(void)
{
return true;
Expand All @@ -51,15 +50,17 @@ static bool ns_resources_ready(void)
{
return !!boot_is_completed;
}
static void stm_tee_entry_std(struct thread_smc_args *smc_args)

/* Overriding the default __weak tee_entry_std() */
uint32_t tee_entry_std(struct optee_msg_arg *arg, uint32_t num_params)
{
boot_is_completed = 1;
tee_entry_std(smc_args);

return __tee_entry_std(arg, num_params);
}
#endif

static const struct thread_handlers handlers = {
.std_smc = stm_tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = pm_panic,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-stm32mp1/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ register_phys_mem_pgdir(MEM_AREA_IO_SEC, USART1_BASE, SMALL_PAGE_SIZE);
static void main_fiq(void);

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = pm_panic,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-sunxi/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ static void main_fiq(void)
}

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
#if defined(CFG_WITH_ARM_TRUSTED_FW)
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-synquacer/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
static void main_fiq(void);

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = cpu_on_handler,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-ti/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ static void main_fiq(void)
}

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
.cpu_on = pm_panic,
Expand Down
1 change: 0 additions & 1 deletion core/arch/arm/plat-vexpress/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
static void main_fiq(void);

static const struct thread_handlers handlers = {
.std_smc = tee_entry_std,
.fast_smc = tee_entry_fast,
.nintr = main_fiq,
#if defined(CFG_WITH_ARM_TRUSTED_FW)
Expand Down
Loading

0 comments on commit 2dd2ca5

Please sign in to comment.