SPM: Share 'memcpy' and 'memset' with partitions
TF-M applies isolation rule I3, which allows code-sharing between
cross-domain components. This makes separating fundamental APIs
such as memset/memcpy less significant. This patch shares these
two fundamental APIs memset/memcpy between SPM and partitions.
A slight optimization is made on the implementation - removes
unnecessary half-word copying.
Change-Id: I7d4c931aefd94d56468806ab768048da156e4656
Signed-off-by: Ken Liu <Ken.Liu@arm.com>
diff --git a/secure_fw/spm/CMakeLists.txt b/secure_fw/spm/CMakeLists.txt
index 084d69a..1e05dbe 100755
--- a/secure_fw/spm/CMakeLists.txt
+++ b/secure_fw/spm/CMakeLists.txt
@@ -40,7 +40,6 @@
target_sources(tfm_spm
PRIVATE
ffm/tfm_boot_data.c
- ffm/tfm_core_utils.c
ffm/utilities.c
$<$<NOT:$<STREQUAL:${TFM_SPM_LOG_LEVEL},TFM_SPM_LOG_LEVEL_SILENCE>>:ffm/spm_log.c>
$<$<BOOL:${TFM_MULTI_CORE_TOPOLOGY}>:cmsis_psa/tfm_multi_core_mem_check.c>
diff --git a/secure_fw/spm/cmsis_func/arch.h b/secure_fw/spm/cmsis_func/arch.h
index c483d7c..70114ab 100644
--- a/secure_fw/spm/cmsis_func/arch.h
+++ b/secure_fw/spm/cmsis_func/arch.h
@@ -47,6 +47,7 @@
*
* \return Does not return
*/
-void tfm_sfn_completion(enum tfm_status_e res, uint32_t exc_return, uintptr_t msp);
+void tfm_sfn_completion(enum tfm_status_e res, uint32_t exc_return,
+ uintptr_t msp);
#endif /* __ARCH_H__ */
diff --git a/secure_fw/spm/cmsis_psa/arch/tfm_arch.c b/secure_fw/spm/cmsis_psa/arch/tfm_arch.c
index c1d131a..e340181 100644
--- a/secure_fw/spm/cmsis_psa/arch/tfm_arch.c
+++ b/secure_fw/spm/cmsis_psa/arch/tfm_arch.c
@@ -8,7 +8,6 @@
#include "compiler_ext_defs.h"
#include "security_defs.h"
#include "tfm_arch.h"
-#include "tfm_core_utils.h"
#include "utilities.h"
__naked void tfm_arch_free_msp_and_exc_ret(uint32_t msp_base,
@@ -46,7 +45,7 @@
}
/* Caution: Keep 'uint32_t' always for collecting thread return values! */
-__attribute__((naked)) uint32_t tfm_arch_trigger_pendsv(void)
+__naked uint32_t tfm_arch_trigger_pendsv(void)
{
__ASM volatile(
#ifndef __ICCARM__
diff --git a/secure_fw/spm/cmsis_psa/arch/tfm_arch_v6m_v7m.c b/secure_fw/spm/cmsis_psa/arch/tfm_arch_v6m_v7m.c
index 25feb21..2b844ab 100644
--- a/secure_fw/spm/cmsis_psa/arch/tfm_arch_v6m_v7m.c
+++ b/secure_fw/spm/cmsis_psa/arch/tfm_arch_v6m_v7m.c
@@ -9,7 +9,6 @@
#include "compiler_ext_defs.h"
#include "spm_ipc.h"
#include "tfm_arch.h"
-#include "tfm_core_utils.h"
#include "tfm_hal_device_header.h"
#include "tfm_svcalls.h"
#include "svc_num.h"
diff --git a/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_base.c b/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_base.c
index 983abc1..15fc098 100644
--- a/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_base.c
+++ b/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_base.c
@@ -11,7 +11,6 @@
#include "svc_num.h"
#include "tfm_hal_device_header.h"
#include "tfm_arch.h"
-#include "tfm_core_utils.h"
#include "tfm_secure_api.h"
#include "tfm_svcalls.h"
#include "utilities.h"
diff --git a/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_main.c b/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_main.c
index 764fb1d..2398a81 100644
--- a/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_main.c
+++ b/secure_fw/spm/cmsis_psa/arch/tfm_arch_v8m_main.c
@@ -11,7 +11,6 @@
#include "spm_ipc.h"
#include "svc_num.h"
#include "tfm_arch.h"
-#include "tfm_core_utils.h"
#include "tfm_hal_device_header.h"
#include "tfm_memory_utils.h"
#include "tfm_secure_api.h"
diff --git a/secure_fw/spm/cmsis_psa/spm_ipc.c b/secure_fw/spm/cmsis_psa/spm_ipc.c
index 821003e..f3f20b9 100755
--- a/secure_fw/spm/cmsis_psa/spm_ipc.c
+++ b/secure_fw/spm/cmsis_psa/spm_ipc.c
@@ -29,7 +29,6 @@
#include "tfm_hal_isolation.h"
#include "spm_ipc.h"
#include "tfm_peripherals_def.h"
-#include "tfm_core_utils.h"
#include "tfm_nspm.h"
#include "tfm_rpc.h"
#include "tfm_core_trustzone.h"
diff --git a/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c b/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c
index e61a45e..cbcc65e 100644
--- a/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c
+++ b/secure_fw/spm/cmsis_psa/tfm_core_svcalls_ipc.c
@@ -12,7 +12,6 @@
#include "tfm_api.h"
#include "tfm_arch.h"
#include "tfm_core_trustzone.h"
-#include "tfm_core_utils.h"
#include "tfm_svcalls.h"
#include "utilities.h"
#include "load/spm_load_api.h"
diff --git a/secure_fw/spm/cmsis_psa/tfm_pools.c b/secure_fw/spm/cmsis_psa/tfm_pools.c
index 2758237..c83ce8a 100644
--- a/secure_fw/spm/cmsis_psa/tfm_pools.c
+++ b/secure_fw/spm/cmsis_psa/tfm_pools.c
@@ -16,7 +16,6 @@
#include "lists.h"
#include "tfm_pools.h"
#include "tfm_memory_utils.h"
-#include "tfm_core_utils.h"
psa_status_t tfm_pool_init(struct tfm_pool_instance_t *pool, size_t poolsz,
size_t chunksz, size_t num)
diff --git a/secure_fw/spm/cmsis_psa/tfm_spe_mailbox.c b/secure_fw/spm/cmsis_psa/tfm_spe_mailbox.c
index ef19c80..a698b9e 100644
--- a/secure_fw/spm/cmsis_psa/tfm_spe_mailbox.c
+++ b/secure_fw/spm/cmsis_psa/tfm_spe_mailbox.c
@@ -9,7 +9,6 @@
#include "cmsis_compiler.h"
#include "psa/error.h"
-#include "tfm_core_utils.h"
#include "utilities.h"
#include "tfm_arch.h"
#include "thread.h"
diff --git a/secure_fw/spm/ffm/psa_api.c b/secure_fw/spm/ffm/psa_api.c
index 5299081..3f8c840 100644
--- a/secure_fw/spm/ffm/psa_api.c
+++ b/secure_fw/spm/ffm/psa_api.c
@@ -14,7 +14,6 @@
#include "interrupt.h"
#include "spm_ipc.h"
#include "tfm_arch.h"
-#include "tfm_core_utils.h"
#include "load/partition_defs.h"
#include "load/service_defs.h"
#include "load/interrupt_defs.h"
diff --git a/secure_fw/spm/ffm/tfm_boot_data.c b/secure_fw/spm/ffm/tfm_boot_data.c
index 015a0f0..7e3a36f 100644
--- a/secure_fw/spm/ffm/tfm_boot_data.c
+++ b/secure_fw/spm/ffm/tfm_boot_data.c
@@ -11,7 +11,6 @@
#include "region_defs.h"
#include "tfm_memory_utils.h"
#include "tfm_api.h"
-#include "tfm_core_utils.h"
#include "psa_manifest/pid.h"
#ifdef TFM_PSA_API
#include "internal_errors.h"
diff --git a/secure_fw/spm/ffm/tfm_core_utils.c b/secure_fw/spm/ffm/tfm_core_utils.c
deleted file mode 100644
index 070b32e..0000000
--- a/secure_fw/spm/ffm/tfm_core_utils.c
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#include <stdint.h>
-#include "utilities.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; /* Address */
- uint8_t *p_byte; /* Byte copy */
- uint16_t *p_dbyte; /* Double byte copy */
- uint32_t *p_qbyte; /* Quad byte copy */
-};
-
-void *spm_memcpy(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--;
- }
-
- /*
- * Double byte 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);
- }
-
- /* Quad byte 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 *spm_memset(void *s, int c, size_t n)
-{
- union tfm_mem_addr_t p_mem;
- uint32_t quad_pattern;
-
- p_mem.p_byte = (uint8_t *)s;
- quad_pattern = (((uint8_t)c) << 24) | (((uint8_t)c) << 16) |
- (((uint8_t)c) << 8) | ((uint8_t)c);
-
- while (n && (p_mem.uint_addr & (sizeof(uint32_t) - 1))) {
- *p_mem.p_byte++ = (uint8_t)c;
- n--;
- }
-
- while (n >= sizeof(uint32_t)) {
- *p_mem.p_qbyte++ = quad_pattern;
- n -= sizeof(uint32_t);
- }
-
- while (n--) {
- *p_mem.p_byte++ = (uint8_t)c;
- }
-
- return s;
-}
diff --git a/secure_fw/spm/include/tfm_core_utils.h b/secure_fw/spm/include/tfm_core_utils.h
deleted file mode 100644
index e916842..0000000
--- a/secure_fw/spm/include/tfm_core_utils.h
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#ifndef __TFM_CORE_UTILS_H__
-#define __TFM_CORE_UTILS_H__
-
-#include <stddef.h>
-#include <stdint.h>
-
-/**
- * \brief Memory copy function for TF-M core
- *
- * \param[out] dest Destination address of memory
- * \param[in] src Source address of memory
- * \param[in] n Number of bytes to copy
- *
- * \retval Destination address of memory
- * \note The function is used for copying same-sized object
- * only.
- */
-void *spm_memcpy(void *dest, const void *src, size_t n);
-
-/**
- * \brief Memory set function for TF-M core
- *
- * \param[out] s Destination address of memory
- * \param[in] c Value to be written to memory
- * \param[in] n Number of bytes to be wirtten
- *
- * \retval Destination address of memory
- */
-void *spm_memset(void *s, int c, size_t n);
-
-#endif /* __TFM_CORE_UTILS_H__ */
diff --git a/secure_fw/spm/include/utilities.h b/secure_fw/spm/include/utilities.h
index 2f27487..71180c5 100644
--- a/secure_fw/spm/include/utilities.h
+++ b/secure_fw/spm/include/utilities.h
@@ -8,7 +8,7 @@
#define __TFM_UTILS_H__
#include <stdbool.h>
-#include <stdio.h>
+#include <string.h>
#include "tfm_spm_log.h"
/*
@@ -41,4 +41,8 @@
#define STRINGIFY_EXPAND(x) #x
#define M2S(m) STRINGIFY_EXPAND(m)
+/* Runtime memory operations forwarding */
+#define spm_memcpy memcpy
+#define spm_memset memset
+
#endif /* __TFM_UTILS_H__ */