App: Add SVC call for registering client ID
Add an SVC call that calls the NS client ID registering veneer.
Also call that veneer at the beginning of the app threads' main
function.
Change-Id: I06c46529967ed66ef4f63ee6ba96513c0815b64d
Signed-off-by: Mate Toth-Pal <mate.toth-pal@arm.com>
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index 86641ac..2be76fd 100755
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -45,7 +45,9 @@
"${APP_DIR}/os_wrapper_rtx.c"
"${INTERFACE_DIR}/src/tfm_sst_api.c"
"${INTERFACE_DIR}/src/tfm_audit_api.c"
+ "${INTERFACE_DIR}/src/tfm_nspm_svc_handler.c"
"${INTERFACE_DIR}/src/tfm_id_mngr_dummy.c"
+ "${INTERFACE_DIR}/src/tfm_nspm_api.c"
"${INTERFACE_DIR}/src/tfm_ns_lock_rtx.c"
)
diff --git a/app/main_ns.c b/app/main_ns.c
index f1ffc19..c3ba83b 100644
--- a/app/main_ns.c
+++ b/app/main_ns.c
@@ -13,6 +13,7 @@
#include "tfm_api.h"
#include "cmsis_os2.h"
#include "tfm_integ_test.h"
+#include "tfm_ns_svc.h"
#include "tfm_ns_lock.h"
#ifdef TEST_FRAMEWORK_NS
#include "test/framework/test_framework_integ_test.h"
@@ -24,6 +25,29 @@
/* For UART the CMSIS driver is used */
extern ARM_DRIVER_USART NS_DRIVER_STDIO;
+/**
+ * \brief Modified table template for user defined SVC functions
+ *
+ * \details RTX has a weak definition of osRtxUserSVC, which
+ * is overridden here
+ */
+extern void * const osRtxUserSVC[1+USER_SVC_COUNT];
+ void * const osRtxUserSVC[1+USER_SVC_COUNT] = {
+ (void *)USER_SVC_COUNT,
+
+#define X(SVC_ENUM, SVC_HANDLER) (void*)SVC_HANDLER,
+
+ /* SVC API for Services */
+ LIST_SVC_NSPM
+
+#undef X
+
+/*
+ * (void *)user_function1,
+ * ...
+ */
+};
+
/* Struct FILE is implemented in stdio.h. Used to redirect printf to
* NS_DRIVER_STDIO
*/
diff --git a/app/tfm_integ_test.c b/app/tfm_integ_test.c
index e7b518a..0707737 100644
--- a/app/tfm_integ_test.c
+++ b/app/tfm_integ_test.c
@@ -13,6 +13,7 @@
#include "cmsis.h"
#include "tfm_api.h"
#include "cmsis_os2.h"
+#include "tfm_nspm_api.h"
#include "tfm_integ_test.h"
#include "test/framework/test_framework_integ_test.h"
@@ -218,6 +219,8 @@
osThreadState_t thread_pri_state;
uint32_t idx;
+ tfm_nspm_register_client_id();
+
thread_id_pri = *((osThreadId_t *)argument);
/* go to sleep */
@@ -249,6 +252,8 @@
__attribute__((noreturn))
static void pri_task(void *argument)
{
+ tfm_nspm_register_client_id();
+
/* go to sleep */
osDelay(100U);
@@ -275,6 +280,8 @@
struct tfm_ns_lock_options ns_lock_opt_pri =
{.use_ns_lock=true, .timeout=osWaitForever};
+ tfm_nspm_register_client_id();
+
test_type = *((enum test_type *)argument);
if (test_type == TEST_TYPE_1) {
diff --git a/interface/include/tfm_ns_svc.h b/interface/include/tfm_ns_svc.h
new file mode 100644
index 0000000..794cc5c
--- /dev/null
+++ b/interface/include/tfm_ns_svc.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2017-2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include <stdio.h>
+#include <cmsis_compiler.h>
+
+#ifndef __TFM_NS_SVC_H__
+#define __TFM_NS_SVC_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief Include all the SVC handler headers
+ */
+#include "tfm_nspm_svc_handler.h"
+
+/**
+ * \brief Macro to encode an svc instruction
+ *
+ */
+#define SVC(code) __ASM("svc %0" : : "I" (code))
+
+/**
+ * \def LIST_SVC_NSPM
+ *
+ * \brief This is an X macro which lists
+ * the SVC interface exposed by TF-M
+ * for the NS OS.
+ *
+ */
+#define LIST_SVC_NSPM \
+ X(SVC_TFM_NSPM_REGISTER_CLIENT_ID, tfm_nspm_svc_register_client_id) \
+
+/**
+ * \brief Numbers associated to each SVC available
+ *
+ * \details Start from 1 as 0 is reserved by RTX
+ */
+enum tfm_svc_num {
+ SVC_INVALID = 0,
+
+#define X(SVC_ENUM, SVC_HANDLER) SVC_ENUM,
+
+ /* SVC API for Services */
+ LIST_SVC_NSPM
+
+#undef X
+
+ /* add all the new entries above this line */
+ SVC_TFM_MAX,
+};
+
+/* number of user SVC functions */
+#define USER_SVC_COUNT (SVC_TFM_MAX - 1)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __TFM_NS_SVC_H__ */
diff --git a/interface/include/tfm_nspm_api.h b/interface/include/tfm_nspm_api.h
new file mode 100644
index 0000000..5f03845
--- /dev/null
+++ b/interface/include/tfm_nspm_api.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __TFM_NSPM_API_H__
+#define __TFM_NSPM_API_H__
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief Reports the client ID of this task to TF-M
+ *
+ * \return Returns 1 if the client ID was successfully reported 0 otherwise
+ */
+uint32_t tfm_nspm_register_client_id(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __TFM_NSPM_API_H__ */
diff --git a/interface/include/tfm_nspm_svc_handler.h b/interface/include/tfm_nspm_svc_handler.h
new file mode 100644
index 0000000..785fda6
--- /dev/null
+++ b/interface/include/tfm_nspm_svc_handler.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __TFM_NSPM_SVC_HANDLER_H__
+#define __TFM_NSPM_SVC_HANDLER_H__
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief Reports the client ID of this task to TF-M (SVC function)
+ *
+ * \return Returns 1 if the client ID was successfully reported 0 otherwise
+ */
+uint32_t tfm_nspm_svc_register_client_id(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __TFM_NSPM_SVC_HANDLER_H__ */
diff --git a/interface/src/tfm_nspm_api.c b/interface/src/tfm_nspm_api.c
new file mode 100644
index 0000000..c95ecae
--- /dev/null
+++ b/interface/src/tfm_nspm_api.c
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include <stdint.h>
+
+#include "tfm_ns_svc.h"
+
+__attribute__ ((naked)) uint32_t tfm_nspm_register_client_id(void)
+{
+ SVC(SVC_TFM_NSPM_REGISTER_CLIENT_ID);
+ __ASM("BX LR");
+}
+
+
diff --git a/interface/src/tfm_nspm_svc_handler.c b/interface/src/tfm_nspm_svc_handler.c
new file mode 100644
index 0000000..7578308
--- /dev/null
+++ b/interface/src/tfm_nspm_svc_handler.c
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+#include <string.h>
+
+#include "tfm_nspm_svc_handler.h"
+#include "cmsis_os2.h"
+#include "tfm_api.h"
+#include "tfm_ns_svc.h"
+
+/* FIXME: following two functions are meant to be internally
+ * available to RTX. The header file containing prototype of
+ * these functions has complex header inclusion which leads
+ * to compiler specific paths in CMSIS, which currently doesn't have
+ * clang variant. To simplify this, following functions are directly
+ * declared here (as opposed to header inclusion). After clear
+ * separation of S and NS builds this will require to be revisited
+ */
+extern osThreadId_t svcRtxThreadGetId(void);
+extern const char *svcRtxThreadGetName(osThreadId_t thread_id);
+
+/* Translation table pair between OS threads and SST client IDs */
+struct thread_sst_clientid_pair {
+ const char* t_name; /*!< Task/Thread name */
+ int32_t client_id; /*!< Client ID used in assets definition */
+};
+
+static struct thread_sst_clientid_pair sst_ns_policy_table[] =
+{
+ {"Thread_A", -9},
+ {"Thread_B", -10},
+ {"Thread_C", -11},
+ {"Thread_D", -12},
+ {"seq_task", -13},
+ {"mid_task", -14},
+ {"pri_task", -15},
+};
+
+static const char* get_active_task_name(void)
+{
+ const char* thread_name;
+
+ thread_name = svcRtxThreadGetName(svcRtxThreadGetId());
+
+ return thread_name;
+}
+
+/* SVC function implementations */
+uint32_t tfm_nspm_svc_register_client_id()
+{
+ int32_t client_id_ns;
+ uint32_t i;
+ static uint32_t sst_table_size = (sizeof(sst_ns_policy_table) /
+ sizeof(sst_ns_policy_table[0]));
+ const char* p_thread_name;
+
+ p_thread_name = get_active_task_name();
+
+ for (i = 0; i < sst_table_size; i++) {
+ if (strcmp(sst_ns_policy_table[i].t_name, p_thread_name) == 0) {
+ client_id_ns = sst_ns_policy_table[i].client_id;
+ if (tfm_register_client_id(client_id_ns) == TFM_SUCCESS) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+ }
+ return 0;
+}
diff --git a/test/suites/sst/non_secure/ns_test_helpers.c b/test/suites/sst/non_secure/ns_test_helpers.c
index 2721954..a07a356 100644
--- a/test/suites/sst/non_secure/ns_test_helpers.c
+++ b/test/suites/sst/non_secure/ns_test_helpers.c
@@ -9,6 +9,8 @@
#include "os_wrapper.h"
+#include "tfm_nspm_api.h"
+
#define SST_TEST_TASK_STACK_SIZE 2048
struct test_task_t {
@@ -28,6 +30,8 @@
{
struct test_task_t *test = arg;
+ tfm_nspm_register_client_id();
+
/* Call the test function */
test->func(test->ret);