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/platform/ext/accelerator/cc312/otp_cc312.c b/platform/ext/accelerator/cc312/otp_cc312.c
index c3d2344..c893d9f 100644
--- a/platform/ext/accelerator/cc312/otp_cc312.c
+++ b/platform/ext/accelerator/cc312/otp_cc312.c
@@ -13,6 +13,7 @@
 #include "uart_stdout.h"
 #include "region_defs.h"
 #include "cmsis.h"
+#include <string.h>
 
 /* Define some offsets from the CC312 base address, to access particular
  * registers and memory regions
diff --git a/platform/ext/common/exception_info.c b/platform/ext/common/exception_info.c
index 68acc19..d383fa4 100644
--- a/platform/ext/common/exception_info.c
+++ b/platform/ext/common/exception_info.c
@@ -8,7 +8,6 @@
 #include "tfm_arch.h"
 #include "exception_info.h"
 #include "tfm_spm_log.h"
-#include "tfm_core_utils.h"
 
 struct exception_info_t {
     uint32_t EXC_RETURN;        /* EXC_RETURN value in LR. */
diff --git a/platform/ext/target/arm/corstone1000/openamp/tfm_spe_dual_core_psa_client_secure_lib.c b/platform/ext/target/arm/corstone1000/openamp/tfm_spe_dual_core_psa_client_secure_lib.c
index 0dde10c..2c7acbd 100644
--- a/platform/ext/target/arm/corstone1000/openamp/tfm_spe_dual_core_psa_client_secure_lib.c
+++ b/platform/ext/target/arm/corstone1000/openamp/tfm_spe_dual_core_psa_client_secure_lib.c
@@ -16,7 +16,6 @@
 #include "psa/error.h"
 #include "utilities.h"
 #include "thread.h"
-#include "tfm_core_utils.h"
 
 /**
  * In linux environment and for psa_call type client api,
diff --git a/platform/ext/target/arm/corstone1000/openamp/tfm_spe_psa_client_lib_unordered_map.c b/platform/ext/target/arm/corstone1000/openamp/tfm_spe_psa_client_lib_unordered_map.c
index 7ffc9eb..96ff46e 100644
--- a/platform/ext/target/arm/corstone1000/openamp/tfm_spe_psa_client_lib_unordered_map.c
+++ b/platform/ext/target/arm/corstone1000/openamp/tfm_spe_psa_client_lib_unordered_map.c
@@ -6,7 +6,7 @@
  */
 
 #include "tfm_spe_psa_client_lib_unordered_map.h"
-#include "tfm_core_utils.h"
+#include "utilities.h"
 #include "tfm_spe_openamp_interface.h"
 #include "tfm_spe_shm_openamp.h"
 #include <stdbool.h>
diff --git a/platform/ext/target/arm/mps2/an519/tfm_hal_isolation.c b/platform/ext/target/arm/mps2/an519/tfm_hal_isolation.c
index 4164411..0ca133f 100644
--- a/platform/ext/target/arm/mps2/an519/tfm_hal_isolation.c
+++ b/platform/ext/target/arm/mps2/an519/tfm_hal_isolation.c
@@ -14,7 +14,6 @@
 #include "target_cfg.h"
 #include "tfm_hal_isolation.h"
 #include "tfm_peripherals_def.h"
-#include "tfm_core_utils.h"
 #ifdef TFM_PSA_API
 #include "load/partition_defs.h"
 #include "load/asset_defs.h"
diff --git a/platform/ext/target/arm/mps2/an521/tfm_hal_isolation.c b/platform/ext/target/arm/mps2/an521/tfm_hal_isolation.c
index 1cfa416..2feb553 100644
--- a/platform/ext/target/arm/mps2/an521/tfm_hal_isolation.c
+++ b/platform/ext/target/arm/mps2/an521/tfm_hal_isolation.c
@@ -14,7 +14,6 @@
 #include "target_cfg.h"
 #include "tfm_hal_isolation.h"
 #include "tfm_peripherals_def.h"
-#include "tfm_core_utils.h"
 #ifdef TFM_PSA_API
 #include "load/partition_defs.h"
 #include "load/asset_defs.h"
diff --git a/platform/ext/target/arm/mps3/an524/tfm_hal_isolation.c b/platform/ext/target/arm/mps3/an524/tfm_hal_isolation.c
index 5964374..c7abc7a 100644
--- a/platform/ext/target/arm/mps3/an524/tfm_hal_isolation.c
+++ b/platform/ext/target/arm/mps3/an524/tfm_hal_isolation.c
@@ -14,7 +14,6 @@
 #include "target_cfg.h"
 #include "tfm_hal_isolation.h"
 #include "tfm_peripherals_def.h"
-#include "tfm_core_utils.h"
 #ifdef TFM_PSA_API
 #include "load/partition_defs.h"
 #include "load/asset_defs.h"
diff --git a/platform/ext/target/arm/mps3/an547/tfm_hal_isolation.c b/platform/ext/target/arm/mps3/an547/tfm_hal_isolation.c
index a56afd2..7bcbcfd 100644
--- a/platform/ext/target/arm/mps3/an547/tfm_hal_isolation.c
+++ b/platform/ext/target/arm/mps3/an547/tfm_hal_isolation.c
@@ -15,7 +15,6 @@
 #include "tfm_hal_isolation.h"
 
 #include "tfm_peripherals_def.h"
-#include "tfm_core_utils.h"
 #ifdef TFM_PSA_API
 #include "load/partition_defs.h"
 #include "load/asset_defs.h"
diff --git a/platform/ext/target/arm/mps3/an552/tfm_hal_isolation.c b/platform/ext/target/arm/mps3/an552/tfm_hal_isolation.c
index 53b85f8..bb272a3 100644
--- a/platform/ext/target/arm/mps3/an552/tfm_hal_isolation.c
+++ b/platform/ext/target/arm/mps3/an552/tfm_hal_isolation.c
@@ -15,7 +15,6 @@
 #include "tfm_hal_isolation.h"
 
 #include "tfm_peripherals_def.h"
-#include "tfm_core_utils.h"
 #ifdef TFM_PSA_API
 #include "load/partition_defs.h"
 #include "load/asset_defs.h"
diff --git a/platform/ext/target/arm/mps3/corstone_polaris/tfm_hal_isolation.c b/platform/ext/target/arm/mps3/corstone_polaris/tfm_hal_isolation.c
index a938f1b..4b12b0c 100644
--- a/platform/ext/target/arm/mps3/corstone_polaris/tfm_hal_isolation.c
+++ b/platform/ext/target/arm/mps3/corstone_polaris/tfm_hal_isolation.c
@@ -15,7 +15,6 @@
 #include "tfm_hal_isolation.h"
 
 #include "tfm_peripherals_def.h"
-#include "tfm_core_utils.h"
 #if TFM_PSA_API
 #include "load/partition_defs.h"
 #include "load/asset_defs.h"
diff --git a/platform/ext/target/arm/musca_b1/sse_200/tfm_hal_isolation.c b/platform/ext/target/arm/musca_b1/sse_200/tfm_hal_isolation.c
index a8893b8..d017a2a 100644
--- a/platform/ext/target/arm/musca_b1/sse_200/tfm_hal_isolation.c
+++ b/platform/ext/target/arm/musca_b1/sse_200/tfm_hal_isolation.c
@@ -14,8 +14,7 @@
 #include "target_cfg.h"
 #include "tfm_hal_isolation.h"
 #include "tfm_peripherals_def.h"
-#include "tfm_core_utils.h"
-#ifdef TFM_PSA_API
+#if TFM_PSA_API
 #include "load/partition_defs.h"
 #include "load/asset_defs.h"
 #include "load/spm_load_api.h"
diff --git a/platform/ext/target/arm/musca_s1/tfm_hal_isolation.c b/platform/ext/target/arm/musca_s1/tfm_hal_isolation.c
index 09bef85..8ce1fa6 100644
--- a/platform/ext/target/arm/musca_s1/tfm_hal_isolation.c
+++ b/platform/ext/target/arm/musca_s1/tfm_hal_isolation.c
@@ -14,8 +14,7 @@
 #include "target_cfg.h"
 #include "tfm_hal_isolation.h"
 #include "tfm_peripherals_def.h"
-#include "tfm_core_utils.h"
-#ifdef TFM_PSA_API
+#if TFM_PSA_API
 #include "load/partition_defs.h"
 #include "load/asset_defs.h"
 #include "load/spm_load_api.h"
diff --git a/platform/ext/target/nuvoton/common/tfm_hal_isolation.c b/platform/ext/target/nuvoton/common/tfm_hal_isolation.c
index 1cfa416..2feb553 100644
--- a/platform/ext/target/nuvoton/common/tfm_hal_isolation.c
+++ b/platform/ext/target/nuvoton/common/tfm_hal_isolation.c
@@ -14,7 +14,6 @@
 #include "target_cfg.h"
 #include "tfm_hal_isolation.h"
 #include "tfm_peripherals_def.h"
-#include "tfm_core_utils.h"
 #ifdef TFM_PSA_API
 #include "load/partition_defs.h"
 #include "load/asset_defs.h"
diff --git a/platform/ext/target/nxp/common/tfm_hal_isolation.c b/platform/ext/target/nxp/common/tfm_hal_isolation.c
index c704a9d..a270019 100644
--- a/platform/ext/target/nxp/common/tfm_hal_isolation.c
+++ b/platform/ext/target/nxp/common/tfm_hal_isolation.c
@@ -15,9 +15,7 @@
 #include "tfm_hal_isolation.h"
 #include "region_defs.h" //NXP
 #include "tfm_peripherals_def.h"
-#include "tfm_core_utils.h"
-#include "tfm_spm_log.h"
-#ifdef TFM_PSA_API
+#if TFM_PSA_API
 #include "load/partition_defs.h"
 #include "load/asset_defs.h"
 #include "load/spm_load_api.h"
diff --git a/platform/ext/target/stm/common/stm32l5xx/secure/tfm_hal_isolation.c b/platform/ext/target/stm/common/stm32l5xx/secure/tfm_hal_isolation.c
index 0a567ac..bf6460e 100644
--- a/platform/ext/target/stm/common/stm32l5xx/secure/tfm_hal_isolation.c
+++ b/platform/ext/target/stm/common/stm32l5xx/secure/tfm_hal_isolation.c
@@ -15,9 +15,7 @@
 #include "tfm_hal_isolation.h"
 #include "tfm_plat_defs.h"
 #include "tfm_peripherals_def.h"
-#include "tfm_core_utils.h"
-#include "low_level_rng.h"
-#ifdef TFM_PSA_API
+#if TFM_PSA_API
 #include "load/partition_defs.h"
 #include "load/asset_defs.h"
 #include "load/spm_load_api.h"
diff --git a/platform/ext/target/stm/common/stm32u5xx/secure/tfm_hal_isolation.c b/platform/ext/target/stm/common/stm32u5xx/secure/tfm_hal_isolation.c
index 20e3314..b82763b 100644
--- a/platform/ext/target/stm/common/stm32u5xx/secure/tfm_hal_isolation.c
+++ b/platform/ext/target/stm/common/stm32u5xx/secure/tfm_hal_isolation.c
@@ -16,10 +16,7 @@
 #include "tfm_plat_defs.h"
 #include "region_defs.h"
 #include "tfm_peripherals_def.h"
-#include "tfm_core_utils.h"
-#include "low_level_rng.h"
-
-#ifdef TFM_PSA_API
+#if TFM_PSA_API
 #include "load/partition_defs.h"
 #include "load/asset_defs.h"
 #include "load/spm_load_api.h"
diff --git a/secure_fw/include/crt_impl_private.h b/secure_fw/include/crt_impl_private.h
new file mode 100644
index 0000000..3727f09
--- /dev/null
+++ b/secure_fw/include/crt_impl_private.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2020, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __CRT_IMPL_PRIVATE_H__
+#define __CRT_IMPL_PRIVATE_H__
+
+#include <stddef.h>
+#include <stdint.h>
+
+#define ADDR_WORD_UNALIGNED(x)        ((x) & 0x3)
+
+union composite_addr_t {
+    uintptr_t uint_addr;        /* Address as integer value  */
+    uint8_t   *p_byte;          /* Address in BYTE pointer   */
+    uint32_t  *p_word;          /* Address in WORD pointer   */
+};
+
+#endif /* __CRT_IMPL_PRIVATE_H__ */
diff --git a/secure_fw/partitions/lib/sprt/CMakeLists.txt b/secure_fw/partitions/lib/sprt/CMakeLists.txt
index 1e7a19b..fede342 100644
--- a/secure_fw/partitions/lib/sprt/CMakeLists.txt
+++ b/secure_fw/partitions/lib/sprt/CMakeLists.txt
@@ -18,11 +18,11 @@
 target_sources(tfm_sprt
     PRIVATE
         ./crt_memcmp.c
-        ./crt_memcpy.c
         ./crt_memmove.c
-        ./crt_memset.c
         ./crt_strnlen.c
         ./service_api.c
+        ${CMAKE_SOURCE_DIR}/secure_fw/shared/crt_memcpy.c
+        ${CMAKE_SOURCE_DIR}/secure_fw/shared/crt_memset.c
         $<$<BOOL:${CONFIG_TFM_PARTITION_META}>:./sprt_partition_metadata_indicator.c>
         $<$<BOOL:${CONFIG_TFM_PARTITION_META}>:./sprt_main.c>
         $<$<BOOL:${CONFIG_TFM_SPM_BACKEND_IPC}>:./sfn_common_thread.c>
diff --git a/secure_fw/partitions/lib/sprt/crt_impl_private.h b/secure_fw/partitions/lib/sprt/crt_impl_private.h
deleted file mode 100644
index b427f0c..0000000
--- a/secure_fw/partitions/lib/sprt/crt_impl_private.h
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright (c) 2020, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#ifndef __CRT_IMPL_PRIVATE_H__
-#define __CRT_IMPL_PRIVATE_H__
-
-#include <stdint.h>
-#include <stddef.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   */
-};
-
-#endif /* __CRT_IMPL_PRIVATE_H__ */
diff --git a/secure_fw/partitions/lib/sprt/crt_memcpy.c b/secure_fw/partitions/lib/sprt/crt_memcpy.c
deleted file mode 100644
index 21a6301..0000000
--- a/secure_fw/partitions/lib/sprt/crt_memcpy.c
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#include "crt_impl_private.h"
-
-void *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;
-}
diff --git a/secure_fw/partitions/lib/sprt/crt_memmove.c b/secure_fw/partitions/lib/sprt/crt_memmove.c
index b24a39d..a4dbf7b 100644
--- a/secure_fw/partitions/lib/sprt/crt_memmove.c
+++ b/secure_fw/partitions/lib/sprt/crt_memmove.c
@@ -10,41 +10,35 @@
 
 static void *memcpy_r(void *dest, const void *src, size_t n)
 {
-    union tfm_mem_addr_t p_dest, p_src;
+    union composite_addr_t p_dst, p_src;
 
-    p_dest.uint_addr = (uintptr_t)dest + n;
-    p_src.uint_addr = (uintptr_t)src + n;
+    p_dst.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);
+    while (n && (ADDR_WORD_UNALIGNED(p_dst.uint_addr) ||
+                 ADDR_WORD_UNALIGNED(p_src.uint_addr))) {
+        *(--p_dst.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);
+        *(--p_dst.p_word) = *(--p_src.p_word);
         n -= sizeof(uint32_t);
     }
 
     /* Byte copy for the remaining bytes. */
     while (n--) {
-        *(--p_dest.p_byte) = *(--p_src.p_byte);
+        *(--p_dst.p_byte) = *(--p_src.p_byte);
     }
 
     return dest;
 }
 
+/* Mind the direction for copying in memcpy! */
+#define memcpy_f memcpy
+
 /*
  * For overlapped memory area:
  * 1) overlapped: use reverse memory move.
@@ -52,15 +46,9 @@
  */
 void *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) {
-        memcpy(dest, src, n);
+        return memcpy_f(dest, src, n);
     } else {
-        memcpy_r(dest, src, n);
+        return memcpy_r(dest, src, n);
     }
-
-    return dest;
 }
diff --git a/secure_fw/shared/crt_memcpy.c b/secure_fw/shared/crt_memcpy.c
new file mode 100644
index 0000000..0675c2a
--- /dev/null
+++ b/secure_fw/shared/crt_memcpy.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2019-2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "crt_impl_private.h"
+
+void *memcpy(void *dest, const void *src, size_t n)
+{
+    union composite_addr_t p_dst, p_src;
+
+    p_dst.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 && (ADDR_WORD_UNALIGNED(p_dst.uint_addr) ||
+                 ADDR_WORD_UNALIGNED(p_src.uint_addr))) {
+        *p_dst.p_byte++ = *p_src.p_byte++;
+        n--;
+    }
+
+    /* Quad byte copy for aligned address. */
+    while (n >= sizeof(uint32_t)) {
+        *(p_dst.p_word)++ = *(p_src.p_word)++;
+        n -= sizeof(uint32_t);
+    }
+
+    /* Byte copy for the remaining bytes. */
+    while (n--) {
+        *p_dst.p_byte++ = *p_src.p_byte++;
+    }
+
+    return dest;
+}
diff --git a/secure_fw/partitions/lib/sprt/crt_memset.c b/secure_fw/shared/crt_memset.c
similarity index 60%
rename from secure_fw/partitions/lib/sprt/crt_memset.c
rename to secure_fw/shared/crt_memset.c
index 92dd5a1..6d88d2f 100644
--- a/secure_fw/partitions/lib/sprt/crt_memset.c
+++ b/secure_fw/shared/crt_memset.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2020-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -9,20 +9,20 @@
 
 void *memset(void *s, int c, size_t n)
 {
-    union tfm_mem_addr_t p_mem;
-    uint32_t quad_pattern;
+    union composite_addr_t p_mem;
+    uint32_t pattern_word;
 
     p_mem.p_byte = (uint8_t *)s;
-    quad_pattern = (((uint8_t)c) << 24) | (((uint8_t)c) << 16) |
+    pattern_word = (((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))) {
+    while (n && ADDR_WORD_UNALIGNED(p_mem.uint_addr)) {
         *p_mem.p_byte++ = (uint8_t)c;
         n--;
     }
 
     while (n >= sizeof(uint32_t)) {
-        *p_mem.p_qbyte++ = quad_pattern;
+        *p_mem.p_word++ = pattern_word;
         n -= sizeof(uint32_t);
     }
 
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__ */