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/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;
+}