Interface: Interface provided to NS side

These files provide a reference interface mplementation for integration
with OS running on the NS side. This has been tested to work with
RTX scheduler.
Modifications may be required while integrating other OS.

Change-Id: I4845584465c5df0bc574de31564a0789154c0dd5
Signed-off-by: Ashutosh Singh <ashutosh.singh@arm.com>
Co-Authored-By: Marc Moreno Berengue <marc.morenoberengue@arm.com>
Co-Authored-By: Antonio de Angelis <antonio.deangelis@arm.com>
diff --git a/interface/src/tfm_id_mngr_dummy.c b/interface/src/tfm_id_mngr_dummy.c
new file mode 100644
index 0000000..c882c8a
--- /dev/null
+++ b/interface/src/tfm_id_mngr_dummy.c
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2017, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+/* FIXME: this TFM ID manager is only a stub implementation. It is system
+ * integrators responsibility to define a way of identifying the app id and
+ * based on their non secure side of the threat model. The secure side only
+ * checks if this is an ID belonging to NS side entities. The secure side
+ * doesn't make any attempt to challenge the app id value, this is left for NS
+ * side privileged code to implement.
+ */
+
+#include "tfm_id_mngr.h"
+
+#include <string.h>
+#include "cmsis_os2.h"
+
+#define INVALID_APP_ID  0
+
+/* 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 app IDs */
+struct thread_sst_appid_pair {
+    const char* t_name;  /*!< Task/Thread name */
+    uint32_t    app_id;  /*!< Application ID used in assets definition */
+};
+
+static struct thread_sst_appid_pair sst_ns_policy_table[] =
+{
+    {"Thread_A", 9},
+    {"Thread_B", 10},
+    {"Thread_C", 11},
+};
+
+static const char* get_active_task_name(void)
+{
+    const char* thread_name;
+
+    thread_name = svcRtxThreadGetName(svcRtxThreadGetId());
+
+    return thread_name;
+}
+
+uint32_t tfm_sst_get_cur_id(void)
+{
+    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) {
+                return sst_ns_policy_table[i].app_id;
+        }
+    }
+
+    return INVALID_APP_ID;
+}
diff --git a/interface/src/tfm_ns_lock_rtx.c b/interface/src/tfm_ns_lock_rtx.c
new file mode 100644
index 0000000..cf5fd6d
--- /dev/null
+++ b/interface/src/tfm_ns_lock_rtx.c
@@ -0,0 +1,158 @@
+/*
+ * Copyright (c) 2017, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "cmsis.h"
+#include "cmsis_os2.h"
+
+#include "tfm_api.h"
+#include "tfm_ns_svc.h"
+
+/**
+ * \brief struct ns_lock_state type
+ */
+struct ns_lock_state
+{
+    bool        init;
+    osMutexId_t id;
+};
+
+/**
+ * \brief ns_lock status
+ */
+static struct ns_lock_state ns_lock = {.init=false, .id=NULL};
+
+/**
+ * \brief Mutex properties, NS lock
+ */
+static const osMutexAttr_t ns_lock_attrib = {
+    .name = "ns_lock",
+    .attr_bits = osMutexPrioInherit
+};
+
+/**
+ * \def NUM_SVC_DISPATCHERS
+ *
+ */
+#define NUM_SVC_DISPATCHERS (6)
+
+/**
+ * \brief Naked functions associated to each
+ *        SVC needed
+ */
+__attribute__((naked))
+static uint32_t tfm_svc_dispatch_SST_GET_HANDLE(uint32_t arg0, uint32_t arg1,
+                                                uint32_t arg2, uint32_t arg3)
+{
+    SVC(SVC_TFM_SST_GET_HANDLE);
+    __ASM("BX LR");
+}
+
+__attribute__((naked))
+static uint32_t tfm_svc_dispatch_SST_CREATE(uint32_t arg0, uint32_t arg1,
+                                                uint32_t arg2, uint32_t arg3)
+{
+    SVC(SVC_TFM_SST_CREATE);
+    __ASM("BX LR");
+}
+
+__attribute__((naked))
+static uint32_t tfm_svc_dispatch_SST_GET_ATTRIBUTES(uint32_t arg0,uint32_t arg1,
+                                                    uint32_t arg2,uint32_t arg3)
+{
+    SVC(SVC_TFM_SST_GET_ATTRIBUTES);
+    __ASM("BX LR");
+}
+
+__attribute__((naked))
+static uint32_t tfm_svc_dispatch_SST_READ(uint32_t arg0, uint32_t arg1,
+                                              uint32_t arg2, uint32_t arg3)
+{
+    SVC(SVC_TFM_SST_READ);
+    __ASM("BX LR");
+}
+
+__attribute__((naked))
+static uint32_t tfm_svc_dispatch_SST_WRITE(uint32_t arg0, uint32_t arg1,
+                                               uint32_t arg2, uint32_t arg3)
+{
+    SVC(SVC_TFM_SST_WRITE);
+    __ASM("BX LR");
+}
+
+__attribute__((naked))
+static uint32_t tfm_svc_dispatch_SST_DELETE(uint32_t arg0, uint32_t arg1,
+                                                uint32_t arg2, uint32_t arg3)
+{
+    SVC(SVC_TFM_SST_DELETE);
+    __ASM("BX LR");
+}
+
+/**
+ * \brief Array with function pointers to the
+ *        naked functions. Entry 0 is treated
+*         as invalid
+ */
+static void *tfm_svc_dispatch_functions[NUM_SVC_DISPATCHERS+1] = {
+    (void *) NULL, /* SVC_INVALID */
+    (void *) tfm_svc_dispatch_SST_GET_HANDLE,
+    (void *) tfm_svc_dispatch_SST_CREATE,
+    (void *) tfm_svc_dispatch_SST_GET_ATTRIBUTES,
+    (void *) tfm_svc_dispatch_SST_READ,
+    (void *) tfm_svc_dispatch_SST_WRITE,
+    (void *) tfm_svc_dispatch_SST_DELETE
+};
+
+/**
+ * \brief NS world, NS lock based dispatcher
+ */
+uint32_t tfm_ns_lock_svc_dispatch(enum tfm_svc_num svc_num,
+                                  uint32_t arg0,
+                                  uint32_t arg1,
+                                  uint32_t arg2,
+                                  uint32_t arg3)
+{
+    uint32_t result;
+    uint32_t (*tfm_svc_dispatch_function_p)(uint32_t, uint32_t,
+                                                uint32_t, uint32_t);
+
+    /* Check the NS lock has been initialized */
+    if (ns_lock.init == false) {
+        return TFM_ERROR_GENERIC;
+    }
+
+    /* Validate the SVC number requested */
+    if ((svc_num > SVC_INVALID) && (svc_num < (NUM_SVC_DISPATCHERS+1))) {
+        tfm_svc_dispatch_function_p = tfm_svc_dispatch_functions[svc_num];
+
+        /* TFM request protected by NS lock */
+        osMutexAcquire(ns_lock.id,osWaitForever);
+        result = (*tfm_svc_dispatch_function_p)(arg0, arg1, arg2, arg3);
+        osMutexRelease(ns_lock.id);
+
+        return result;
+    }
+    else {
+        return TFM_ERROR_GENERIC;
+    }
+}
+
+/**
+ * \brief NS world, Init NS lock
+ */
+uint32_t tfm_ns_lock_init()
+{
+    if (ns_lock.init == false) {
+        ns_lock.id = osMutexNew(&ns_lock_attrib);
+        ns_lock.init = true;
+        return TFM_SUCCESS;
+    }
+    else {
+        return TFM_ERROR_GENERIC;
+    }
+}
diff --git a/interface/src/tfm_sst_api.c b/interface/src/tfm_sst_api.c
new file mode 100644
index 0000000..fb13ed0
--- /dev/null
+++ b/interface/src/tfm_sst_api.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2017, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "tfm_sst_defs.h"
+#include "tfm_ns_lock.h"
+
+enum tfm_sst_err_t tfm_sst_get_handle(uint16_t asset_uuid, uint32_t* hdl)
+{
+    return tfm_ns_lock_svc_dispatch(SVC_TFM_SST_GET_HANDLE,
+                                    (uint32_t)asset_uuid,
+                                    (uint32_t)hdl,
+                                    0,
+                                    0);
+}
+
+enum tfm_sst_err_t tfm_sst_create(uint16_t asset_uuid)
+{
+    return tfm_ns_lock_svc_dispatch(SVC_TFM_SST_CREATE,
+                                    (uint32_t) asset_uuid,
+                                    0,
+                                    0,
+                                    0);
+}
+
+enum tfm_sst_err_t tfm_sst_get_attributes(uint32_t asset_handle,
+                                        struct tfm_sst_attribs_t* attrib_struct)
+{
+    return tfm_ns_lock_svc_dispatch(SVC_TFM_SST_GET_ATTRIBUTES,
+                                    (uint32_t)asset_handle,
+                                    (uint32_t)attrib_struct,
+                                    0,
+                                    0);
+}
+
+enum tfm_sst_err_t tfm_sst_read(uint32_t asset_handle, struct tfm_sst_buf_t* data)
+{
+    return tfm_ns_lock_svc_dispatch(SVC_TFM_SST_READ,
+                                    (uint32_t)asset_handle,
+                                    (uint32_t)data,
+                                    0,
+                                    0);
+}
+
+enum tfm_sst_err_t tfm_sst_write(uint32_t asset_handle, struct tfm_sst_buf_t* data)
+{
+    return tfm_ns_lock_svc_dispatch(SVC_TFM_SST_WRITE,
+                                    (uint32_t)asset_handle,
+                                    (uint32_t)data,
+                                    0,
+                                    0);
+}
+
+enum tfm_sst_err_t tfm_sst_delete(uint32_t asset_handle)
+{
+    return tfm_ns_lock_svc_dispatch(SVC_TFM_SST_DELETE,
+                                    (uint32_t)asset_handle,
+                                    0,
+                                    0,
+                                    0);
+}
diff --git a/interface/src/tfm_sst_svc_handler.c b/interface/src/tfm_sst_svc_handler.c
new file mode 100644
index 0000000..c10432e
--- /dev/null
+++ b/interface/src/tfm_sst_svc_handler.c
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2017, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include <string.h>
+#include "tfm_ns_svc.h"
+#include "tfm_sst_veneers.h"
+#include "tfm_id_mngr.h"
+
+/* SVC function implementations */
+enum tfm_sst_err_t tfm_sst_svc_get_handle(uint16_t asset_uuid,
+                                          uint32_t* hdl)
+{
+    uint32_t app_id;
+
+    app_id = tfm_sst_get_cur_id();
+
+    return tfm_sst_veneer_get_handle(app_id, asset_uuid, hdl);
+}
+
+enum tfm_sst_err_t tfm_sst_svc_create(uint16_t asset_uuid)
+{
+    uint32_t app_id;
+
+    app_id = tfm_sst_get_cur_id();
+
+    return tfm_sst_veneer_create(app_id, asset_uuid);
+}
+
+enum tfm_sst_err_t tfm_sst_svc_get_attributes(uint32_t asset_handle,
+                                        struct tfm_sst_attribs_t* attrib_struct)
+{
+    uint32_t app_id;
+
+    app_id = tfm_sst_get_cur_id();
+
+    return tfm_sst_veneer_get_attributes(app_id, asset_handle, attrib_struct);
+}
+
+enum tfm_sst_err_t tfm_sst_svc_read(uint32_t asset_handle,
+                                    struct tfm_sst_buf_t* data)
+{
+    uint32_t app_id;
+
+    app_id = tfm_sst_get_cur_id();
+
+    return tfm_sst_veneer_read(app_id, asset_handle, data);
+}
+
+enum tfm_sst_err_t tfm_sst_svc_write(uint32_t asset_handle,
+                                     struct tfm_sst_buf_t* data)
+{
+    uint32_t app_id;
+
+    app_id = tfm_sst_get_cur_id();
+
+    return tfm_sst_veneer_write(app_id, asset_handle, data);
+}
+
+enum tfm_sst_err_t tfm_sst_svc_delete(uint32_t asset_handle)
+{
+    uint32_t app_id;
+
+    app_id = tfm_sst_get_cur_id();
+
+    return tfm_sst_veneer_delete(app_id, asset_handle);
+}