aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMingyang Sun <mingyang.sun@arm.com>2019-07-16 17:57:52 +0800
committerKen Liu <ken.liu@arm.com>2019-12-09 06:29:06 +0000
commitc4b5d8d732104a98c0224562602c07c5f2c10ede (patch)
tree99d4f28f4bcfe1de857ae66f51a0b29b7deec800
parent3d1ee70227e255a57412fd4db93563974ab694da (diff)
downloadtrusted-firmware-m-c4b5d8d732104a98c0224562602c07c5f2c10ede.tar.gz
SPRTL: 'memory copy' and 'memory move' functions
- Create a directory for Secure Partition Runtime Library source files. - Implement tfm_sprt_c_memcpy() and tfm_sprt_c_memmove() functions. - Create CMakefile for Secure Partition Runtime Library. - Support byte, double byte, quad byte copy. Change-Id: I77151a1cae3aa43c7ce265d1c19e458ade4b51f3 Signed-off-by: Mingyang Sun <mingyang.sun@arm.com>
-rw-r--r--secure_fw/CMakeLists.txt10
-rw-r--r--secure_fw/lib/sprt/CMakeLists.inc33
-rw-r--r--secure_fw/lib/sprt/CMakeLists.txt36
-rw-r--r--secure_fw/lib/sprt/tfm_libsprt_c.h38
-rw-r--r--secure_fw/lib/sprt/tfm_libsprt_c_memcpy.c14
-rw-r--r--secure_fw/lib/sprt/tfm_libsprt_c_memmove.c116
6 files changed, 244 insertions, 3 deletions
diff --git a/secure_fw/CMakeLists.txt b/secure_fw/CMakeLists.txt
index d29325705d..c48426bcaf 100644
--- a/secure_fw/CMakeLists.txt
+++ b/secure_fw/CMakeLists.txt
@@ -165,6 +165,7 @@ function(set_up_secure_fw_build)
add_dependencies(${EXE_NAME} tfm_internal_trusted_storage)
add_dependencies(${EXE_NAME} tfm_secure_tests)
add_dependencies(${EXE_NAME} tfm_attest)
+ add_dependencies(${EXE_NAME} libtfmsprt)
if (TFM_PARTITION_AUDIT_LOG)
add_dependencies(${EXE_NAME} tfm_audit)
endif()
@@ -178,12 +179,12 @@ function(set_up_secure_fw_build)
if (REGRESSION OR CORE_TEST)
if (DEFINED TFM_PARTITION_TEST_SECURE_SERVICES AND TFM_PARTITION_TEST_SECURE_SERVICES)
- target_link_libraries(${EXE_NAME} tfm_attest tfm_secure_tests tfm_attest tfm_crypto tfm_storage tfm_internal_trusted_storage tfm_attest)
+ target_link_libraries(${EXE_NAME} tfm_attest tfm_secure_tests tfm_attest tfm_crypto tfm_storage tfm_internal_trusted_storage tfm_attest libtfmsprt)
else()
- target_link_libraries(${EXE_NAME} tfm_attest tfm_crypto tfm_storage tfm_internal_trusted_storage tfm_secure_tests tfm_attest)
+ target_link_libraries(${EXE_NAME} tfm_attest tfm_crypto tfm_storage tfm_internal_trusted_storage tfm_secure_tests tfm_attest libtfmsprt)
endif()
else()
- target_link_libraries(${EXE_NAME} tfm_attest tfm_crypto tfm_storage tfm_internal_trusted_storage tfm_attest)
+ target_link_libraries(${EXE_NAME} tfm_attest tfm_crypto tfm_storage tfm_internal_trusted_storage tfm_attest libtfmsprt)
endif()
if (TFM_PARTITION_AUDIT_LOG)
@@ -400,6 +401,9 @@ if (TFM_PARTITION_AUDIT_LOG)
add_subdirectory(${SECURE_FW_DIR}/services/audit_logging)
endif()
+#Add the secure runtime library target
+add_subdirectory(${SECURE_FW_DIR}/lib/sprt)
+
if (NOT DEFINED TFM_MULTI_CORE_TOPOLOGY OR NOT TFM_MULTI_CORE_TOPOLOGY)
#Broadcast veneer path in bundled building case
set(S_VENEER_PATH "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
diff --git a/secure_fw/lib/sprt/CMakeLists.inc b/secure_fw/lib/sprt/CMakeLists.inc
new file mode 100644
index 0000000000..20aa1d6d6f
--- /dev/null
+++ b/secure_fw/lib/sprt/CMakeLists.inc
@@ -0,0 +1,33 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2019, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+#Definations to compile the "libtfmsprt" module.
+#This file assumes it will be included from a project specific cmakefile, and
+#will not create a library or executable.
+#Inputs:
+# TFM_ROOT_DIR - root directory of the TF-M repository.
+#Outputs:
+# Will modify include directories to make the source compile.
+# ALL_SRC_C_S: C source files to be compiled will be added to this list. This shall be added to your add_executable or add_library command.
+# ALL_SRC_CXX_S: C++ source files to be compiled will be added to this list. This shaall be added to your add_executable or add_library command.
+# ALL_SRC_ASM_S: assembly source files to be compiled will be added to this list. This shall be added to your add_executable or add_library command.
+# Include directories will be modified by using the include_directories() commands as needed.
+
+#Get the current directory where this file is located.
+set(LIBSPRT_DIR ${CMAKE_CURRENT_LIST_DIR})
+
+if(NOT DEFINED TFM_ROOT_DIR)
+ message(FATAL_ERROR "Please set TFM_ROOT_DIR before including this file.")
+endif()
+
+set (LIBSPRT_C_SRC
+ "${LIBSPRT_DIR}/tfm_libsprt_c_memcpy.c"
+ "${LIBSPRT_DIR}/tfm_libsprt_c_memmove.c")
+
+#Append all our source files to global lists.
+list(APPEND ALL_SRC_C_S ${LIBSPRT_C_SRC})
+unset(LIBSPRT_C_SRC)
diff --git a/secure_fw/lib/sprt/CMakeLists.txt b/secure_fw/lib/sprt/CMakeLists.txt
new file mode 100644
index 0000000000..283b16de58
--- /dev/null
+++ b/secure_fw/lib/sprt/CMakeLists.txt
@@ -0,0 +1,36 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2019, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+cmake_minimum_required(VERSION 3.7)
+
+#Tell cmake where our modules can be found
+list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../../../cmake)
+
+#Some project global settings
+set (LIBSPRT_DIR "${CMAKE_CURRENT_LIST_DIR}")
+get_filename_component(TFM_ROOT_DIR "${LIBSPRT_DIR}/../../.." ABSOLUTE)
+
+#Include common stuff to control cmake.
+include("Common/BuildSys")
+
+#Start an embedded project.
+embedded_project_start(CONFIG "${TFM_ROOT_DIR}/configs/ConfigDefault.cmake")
+project(libtfmsprt LANGUAGES ASM C)
+embedded_project_fixup()
+
+#Get the definition of what files we need to build
+include(CMakeLists.inc)
+
+#Build secure partition runtime library as a static library
+add_library(libtfmsprt STATIC ${ALL_SRC_ASM_S} ${ALL_SRC_C_S})
+embedded_set_target_compile_defines(TARGET libtfmsprt LANGUAGE C DEFINES __ARM_FEATURE_CMSE=${ARM_FEATURE_CMSE} __thumb2__)
+
+#Set common compiler and linker flags
+config_setting_shared_compiler_flags(libtfmsprt)
+config_setting_shared_linker_flags(libtfmsprt)
+
+embedded_project_end(libtfmsprt)
diff --git a/secure_fw/lib/sprt/tfm_libsprt_c.h b/secure_fw/lib/sprt/tfm_libsprt_c.h
new file mode 100644
index 0000000000..7dd2259d8b
--- /dev/null
+++ b/secure_fw/lib/sprt/tfm_libsprt_c.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __TFM_LIBSPRT_C_H__
+#define __TFM_LIBSPRT_C_H__
+
+#include <stddef.h>
+
+/**
+ * \brief This function moves 'n' bytes from 'src' to 'dest'.
+ *
+ * \param[out] dest Destination address
+ * \param[in] src Source address
+ * \param[in] n Number of bytes to be moved
+ *
+ * \retval dest Destination address
+ * \note Memory overlap has been taken into consideration
+ * and processed properly in the function.
+ */
+void *tfm_sprt_c_memmove(void *dest, const void *src, size_t n);
+
+/**
+ * \brief This function copies 'n' bytes from 'src' to 'dest'.
+ *
+ * \param[out] dest Destination address
+ * \param[in] src Source address
+ * \param[in] n Number of bytes to be copied
+ *
+ * \retval dest Destination address
+ * \note It has the same effect as tfm_sprt_c_memmove().
+ */
+void *tfm_sprt_c_memcpy(void *dest, const void *src, size_t n);
+
+#endif /* __TFM_LIBSPRT_C_H__ */
diff --git a/secure_fw/lib/sprt/tfm_libsprt_c_memcpy.c b/secure_fw/lib/sprt/tfm_libsprt_c_memcpy.c
new file mode 100644
index 0000000000..919d051195
--- /dev/null
+++ b/secure_fw/lib/sprt/tfm_libsprt_c_memcpy.c
@@ -0,0 +1,14 @@
+/*
+ * Copyright (c) 2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include <stddef.h>
+#include "tfm_libsprt_c.h"
+
+void *tfm_sprt_c_memcpy(void *dest, const void *src, size_t n)
+{
+ return tfm_sprt_c_memmove(dest, src, n);
+}
diff --git a/secure_fw/lib/sprt/tfm_libsprt_c_memmove.c b/secure_fw/lib/sprt/tfm_libsprt_c_memmove.c
new file mode 100644
index 0000000000..e46747e0cc
--- /dev/null
+++ b/secure_fw/lib/sprt/tfm_libsprt_c_memmove.c
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include <stdint.h>
+#include <stddef.h>
+#include "tfm_libsprt_c.h"
+
+#define GET_MEM_ADDR_BIT0(x) ((x) & 0x1)
+#define GET_MEM_ADDR_BIT1(x) ((x) & 0x2)
+
+union tfm_mem_addr_t {
+ uintptr_t uint_addr;
+ uint8_t *p_byte;
+ uint16_t *p_dbyte;
+ uint32_t *p_qbyte;
+};
+
+/*
+ * Consider 3 conditions.
+ * 1) quad-byte copy (qbyte)
+ * 2) double-byte copy (dbyte)
+ * 3) byte copy
+ *
+ * And for overlapped memory area.
+ * 1) overlapped: use backward memory move.
+ * 2) non-overlapped: use forward memory move.
+ */
+
+static void *tfm_memmove_forward(void *dest, const void *src, size_t n)
+{
+ union tfm_mem_addr_t p_dest, p_src;
+
+ p_dest.uint_addr = (uintptr_t)dest;
+ p_src.uint_addr = (uintptr_t)src;
+
+ /* byte copy for unaligned address. check the last bit of address. */
+ while (n && (GET_MEM_ADDR_BIT0(p_dest.uint_addr) ||
+ GET_MEM_ADDR_BIT0(p_src.uint_addr))) {
+ *p_dest.p_byte++ = *p_src.p_byte++;
+ n--;
+ }
+
+ /* dbyte-copy for aligned address. check the 2nd last bit of address. */
+ while (n >= sizeof(uint16_t) && (GET_MEM_ADDR_BIT1(p_dest.uint_addr) ||
+ GET_MEM_ADDR_BIT1(p_src.uint_addr))) {
+ *(p_dest.p_dbyte)++ = *(p_src.p_dbyte)++;
+ n -= sizeof(uint16_t);
+ }
+
+ /* qbyte-copy for aligned address. */
+ while (n >= sizeof(uint32_t)) {
+ *(p_dest.p_qbyte)++ = *(p_src.p_qbyte)++;
+ n -= sizeof(uint32_t);
+ }
+
+ /* byte copy for the remaining bytes. */
+ while (n--) {
+ *p_dest.p_byte++ = *p_src.p_byte++;
+ }
+
+ return dest;
+}
+
+static void *tfm_memmove_backward(void *dest, const void *src, size_t n)
+{
+ union tfm_mem_addr_t p_dest, p_src;
+
+ p_dest.uint_addr = (uintptr_t)dest + n;
+ p_src.uint_addr = (uintptr_t)src + n;
+
+ /* byte copy for unaligned address. check the last bit of address. */
+ while (n && (GET_MEM_ADDR_BIT0(p_dest.uint_addr) ||
+ GET_MEM_ADDR_BIT0(p_src.uint_addr))) {
+ *(--p_dest.p_byte) = *(--p_src.p_byte);
+ n--;
+ }
+
+ /* dbyte-copy for aligned address. check the 2nd last bit of address. */
+ while (n >= sizeof(uint16_t) && (GET_MEM_ADDR_BIT1(p_dest.uint_addr) ||
+ GET_MEM_ADDR_BIT1(p_src.uint_addr))) {
+ *(--p_dest.p_dbyte) = *(--p_src.p_dbyte);
+ n -= sizeof(uint16_t);
+ }
+
+ /* qbyte-copy for aligned address. */
+ while (n >= sizeof(uint32_t)) {
+ *(--p_dest.p_qbyte) = *(--p_src.p_qbyte);
+ n -= sizeof(uint32_t);
+ }
+
+ /* byte copy for the remaining bytes. */
+ while (n--) {
+ *(--p_dest.p_byte) = *(--p_src.p_byte);
+ }
+
+ return dest;
+}
+
+void *tfm_sprt_c_memmove(void *dest, const void *src, size_t n)
+{
+ /*
+ * FixMe: Add a "assert (dest == NULL || src == NULL)" here
+ * after "assert()" for sprtl is implemented.
+ */
+ if (src < dest) {
+ tfm_memmove_backward(dest, src, n);
+ } else {
+ tfm_memmove_forward(dest, src, n);
+ }
+
+ return dest;
+}