refactor(lib/realm): remove 'lib/realm' library

This patch removes the lib/realm library by refactoring its code and
moving its components to related submodules. It also introduces two
new libraries, 'lib/granule' and 'lib/slot_buf' with sources from
'lib/realm'

Change-Id: Id41eb2da20172d8da9a5f066b3e78ec4f1a6d35d
Signed-off-by: Javier Almansa Sobrino <javier.almansasobrino@arm.com>
Signed-off-by: Soby Mathew <soby.mathew@arm.com>
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 5fb591a..974c95f 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -13,12 +13,12 @@
               rmm-lib-console
               rmm-lib-debug
               rmm-lib-gic
+              rmm-lib-granule
               rmm-lib-measurement
-              rmm-lib-realm
               rmm-lib-rmm_el3_ifc
               rmm-lib-smc
               rmm-lib-s2tt
-              rmm-lib-timers
+              rmm-lib-slot_buf
               rmm-lib-xlat
               t_cose)
 
@@ -29,13 +29,13 @@
 add_subdirectory("console")
 add_subdirectory("debug")
 add_subdirectory("gic")
+add_subdirectory("granule")
 add_subdirectory("mbedtls")
 add_subdirectory("measurement")
 add_subdirectory("qcbor")
-add_subdirectory("realm")
 add_subdirectory("rmm_el3_ifc")
 add_subdirectory("smc")
 add_subdirectory("s2tt")
+add_subdirectory("slot_buf")
 add_subdirectory("t_cose")
-add_subdirectory("timers")
 add_subdirectory("xlat")
diff --git a/lib/arch/CMakeLists.txt b/lib/arch/CMakeLists.txt
index 89980be..76a8231 100644
--- a/lib/arch/CMakeLists.txt
+++ b/lib/arch/CMakeLists.txt
@@ -18,6 +18,10 @@
     PRIVATE "src/${RMM_ARCH}"
             "src/include")
 
+# Include files needed for aarch64, fake_host and cbmc builds
+target_sources(rmm-lib-arch
+        PRIVATE "src/vmid.c")
+
 if(HOST_VARIANT STREQUAL "host_cbmc")
   target_sources(rmm-lib-arch
         PRIVATE "src/fake_host/cbmc_arch_features.c"
diff --git a/lib/realm/include/vmid.h b/lib/arch/include/vmid.h
similarity index 100%
rename from lib/realm/include/vmid.h
rename to lib/arch/include/vmid.h
diff --git a/runtime/core/vmid.c b/lib/arch/src/vmid.c
similarity index 100%
rename from runtime/core/vmid.c
rename to lib/arch/src/vmid.c
diff --git a/lib/realm/include/status.h b/lib/common/include/status.h
similarity index 98%
rename from lib/realm/include/status.h
rename to lib/common/include/status.h
index 81462b6..f22255d 100644
--- a/lib/realm/include/status.h
+++ b/lib/common/include/status.h
@@ -8,7 +8,6 @@
 
 #include <assert.h>
 #include <smc-rmi.h>
-#include <stdbool.h>
 
 /*
  * Logical representation of return code returned by RMM commands.
diff --git a/lib/granule/CMakeLists.txt b/lib/granule/CMakeLists.txt
new file mode 100644
index 0000000..723e240
--- /dev/null
+++ b/lib/granule/CMakeLists.txt
@@ -0,0 +1,19 @@
+#
+# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
+#
+
+add_library(rmm-lib-granule)
+
+target_link_libraries(rmm-lib-granule
+    PUBLIC rmm-lib-arch
+           rmm-lib-common
+    PRIVATE rmm-lib-debug)
+
+target_include_directories(rmm-lib-granule
+    PUBLIC "include")
+
+target_sources(rmm-lib-granule
+    PRIVATE "src/granule.c")
+
+include (tests/CMakeLists.txt)
diff --git a/lib/realm/include/granule.h b/lib/granule/include/granule.h
similarity index 95%
rename from lib/realm/include/granule.h
rename to lib/granule/include/granule.h
index 8114449..04c045d 100644
--- a/lib/realm/include/granule.h
+++ b/lib/granule/include/granule.h
@@ -8,12 +8,12 @@
 
 #include <assert.h>
 #include <atomics.h>
-#include <buffer.h>
 #include <errno.h>
 #include <granule_types.h>
 #include <memory.h>
 #include <spinlock.h>
-#include <status.h>
+#include <stdbool.h>
+#include <utils_def.h>
 
 static inline unsigned short granule_refcount_read_relaxed(struct granule *g)
 {
@@ -150,13 +150,8 @@
 			    unsigned char expected_state2,
 			    struct granule **g2);
 
-void granule_memzero(struct granule *g, enum buffer_slot slot);
-
 void granule_memzero_mapped(void *buf);
 
-void *aux_granules_map(struct granule *rec_aux_pages[], unsigned int num_aux);
-void aux_granules_unmap(void *rec_aux, unsigned int num_aux);
-
 /* Must be called with g->lock held */
 static inline void __granule_get(struct granule *g)
 {
diff --git a/lib/realm/include/granule_types.h b/lib/granule/include/granule_types.h
similarity index 100%
rename from lib/realm/include/granule_types.h
rename to lib/granule/include/granule_types.h
diff --git a/lib/realm/src/granule.c b/lib/granule/src/granule.c
similarity index 84%
rename from lib/realm/src/granule.c
rename to lib/granule/src/granule.c
index bba6129..3ff6ed6 100644
--- a/lib/realm/src/granule.c
+++ b/lib/granule/src/granule.c
@@ -5,13 +5,10 @@
 
 #include <arch_helpers.h>
 #include <assert.h>
-#include <buffer.h>
 #include <debug.h>
 #include <granule.h>
 #include <mmio.h>
 #include <platform_api.h>
-#include <smc.h>
-#include <status.h>
 #include <stddef.h>
 /* According to the C standard, the memset function used in this file is declared in string.h */
 /* coverity[unnecessary_header: SUPPRESS] */
@@ -239,19 +236,6 @@
 	return find_lock_granules(gs, ARRAY_SIZE(gs));
 }
 
-void granule_memzero(struct granule *g, enum buffer_slot slot)
-{
-	unsigned long *buf;
-
-	assert(g != NULL);
-
-	buf = granule_map(g, slot);
-	assert(buf != NULL);
-
-	granule_memzero_mapped(buf);
-	buffer_unmap(buf);
-}
-
 void granule_memzero_mapped(void *buf)
 {
 	unsigned long dczid_el0 = read_dczid_el0();
@@ -281,39 +265,3 @@
 	dsb(ish);
 }
 
-/*
- * The parent REC granules lock is expected to be acquired before functions
- * aux_granules_map() and aux_granules_unmap() are called.
- */
-void *aux_granules_map(struct granule *rec_aux_pages[], unsigned int num_aux)
-{
-	void *rec_aux = NULL;
-
-	assert(rec_aux_pages != NULL);
-	assert(num_aux <= MAX_REC_AUX_GRANULES);
-
-	for (unsigned int i = 0U; i < num_aux; i++) {
-		void *aux = granule_map(rec_aux_pages[i],
-					(enum buffer_slot)((unsigned int)
-							   SLOT_REC_AUX0 + i));
-
-		assert(aux != NULL);
-
-		if (i == 0UL) {
-			rec_aux = aux;
-		}
-	}
-	return rec_aux;
-}
-
-void aux_granules_unmap(void *rec_aux, unsigned int num_aux)
-{
-	unsigned char *rec_aux_vaddr = (unsigned char *)rec_aux;
-
-	assert(rec_aux != NULL);
-	assert(num_aux <= MAX_REC_AUX_GRANULES);
-
-	for (unsigned int i = 0U; i < num_aux; i++) {
-		buffer_unmap((void *)((uintptr_t)rec_aux_vaddr + (i * GRANULE_SIZE)));
-	}
-}
diff --git a/lib/granule/tests/CMakeLists.txt b/lib/granule/tests/CMakeLists.txt
new file mode 100644
index 0000000..e8f539e
--- /dev/null
+++ b/lib/granule/tests/CMakeLists.txt
@@ -0,0 +1,19 @@
+#
+# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
+#
+
+if(RMM_UNITTESTS)
+    target_include_directories(rmm-lib-granule
+        PRIVATE "tests")
+endif()
+
+# Add test functionality
+rmm_build_unittest(NAME granule
+                   TARGET rmm-lib-granule
+                   LIBRARIES rmm-lib-slot_buf
+                             rmm-platform
+                             rmm-lib-xlat
+                   SOURCES "tests/granule.cpp"
+                   ITERATIONS 10)
+
diff --git a/lib/realm/tests/granule.cpp b/lib/granule/tests/granule.cpp
similarity index 95%
rename from lib/realm/tests/granule.cpp
rename to lib/granule/tests/granule.cpp
index bd42283..a016ad0 100644
--- a/lib/realm/tests/granule.cpp
+++ b/lib/granule/tests/granule.cpp
@@ -8,12 +8,10 @@
 
 extern "C" {
 #include <buffer.h>
-#include <buffer_private.h>
 #include <cpuid.h>
 #include <granule.h>	/* Interface to exercise */
 #include <host_harness.h>
 #include <host_utils.h>
-#include <realm_test_utils.h>
 #include <status.h>
 #include <stdlib.h>
 #include <string.h>
@@ -1754,86 +1752,3 @@
 		CHECK_TRUE(granule == NULL);
 	}
 }
-
-TEST(granule, granule_memzero_TC1)
-{
-	unsigned long addrs[3] = {host_util_get_granule_base(),
-				  (get_rand_granule_idx() * GRANULE_SIZE) +
-					host_util_get_granule_base(),
-				  ((test_helpers_get_nr_granules() - 1) *
-								GRANULE_SIZE) +
-					host_util_get_granule_base()};
-	struct granule *granule;
-	int *val;
-	union test_harness_cbs cb;
-
-	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
-	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
-	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
-
-	/***************************************************************
-	 * TEST CASE 1:
-	 *
-	 * Map a granule to every possible slot type and memzero
-	 * it. Verify then that the whole slot buffer is all 0.
-	 * Test the first and the last valid granules as well as random
-	 * granules in between.
-	 * Repeat the operation on all possible CPUs.
-	 *
-	 * NOTE: granule_memzero() will fail with SLOT_NS, so skip that
-	 *	 testcase.
-	 ***************************************************************/
-
-	for (unsigned int i = 0U; i < 3U; i++) {
-		granule = addr_to_granule(addrs[i]);
-		val = (int *)addrs[i];
-
-		for (unsigned int j = 0U; j < MAX_CPUS; j++) {
-			/* Configure the cpu id */
-			host_util_set_cpuid(j);
-
-			for (unsigned int k = 0; k < NR_CPU_SLOTS; k++) {
-				if (k == SLOT_NS) {
-					/* Not supported by granule_memzero */
-					continue;
-				}
-
-				/* Initialize the granule with random data */
-				memset((void *)addrs[i],
-					(int)test_helpers_get_rand_in_range(1UL, INT_MAX),
-					GRANULE_SIZE);
-				granule_memzero(granule, (enum buffer_slot)k);
-
-				for (unsigned int l = 0;
-				     l < (GRANULE_SIZE / sizeof(int)); l++) {
-					if (*(val + l) != 0) {
-						FAIL_TEST("Memory not properly zeroed");
-					}
-				} /* GRANULE_SIZE */
-			} /* NR_CPU_SLOTS */
-		} /* MAX_CPUS */
-	} /* Number of granules to test */
-}
-
-ASSERT_TEST(granule, granule_memzero_TC2)
-{
-	/***************************************************************
-	 * TEST CASE 2:
-	 *
-	 * Verify that granule_memzero() asserts if granule is NULL
-	 ***************************************************************/
-
-	test_helpers_expect_assert_fail(true);
-	granule_memzero(NULL, SLOT_DELEGATED);
-	test_helpers_fail_if_no_assert_failed();
-}
-
-TEST(granule, granule_memzero_mapped_TC1)
-{
-	/*
-	 * Current implementation for granule_memzero_mapped()
-	 * is a wrapper for memset, so skip this test for now.
-	 */
-}
diff --git a/lib/realm/CMakeLists.txt b/lib/realm/CMakeLists.txt
deleted file mode 100644
index ef4ddc0..0000000
--- a/lib/realm/CMakeLists.txt
+++ /dev/null
@@ -1,50 +0,0 @@
-#
-# SPDX-License-Identifier: BSD-3-Clause
-# SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
-#
-
-add_library(rmm-lib-realm)
-
-arm_config_option(
-    NAME VIRT_ADDR_SPACE_WIDTH
-    HELP "Size in bits of the virtual address space."
-    DEFAULT 38
-    TYPE STRING
-    ADVANCED)
-
-if(VIRT_ADDR_SPACE_WIDTH EQUAL 0x0)
-    message(FATAL_ERROR "VIRT_ADDR_SPACE_WIDTH is not initialized")
-endif()
-
-target_compile_definitions(rmm-lib-realm
-    PUBLIC "VIRT_ADDR_SPACE_SIZE=(1ULL << ULL(${VIRT_ADDR_SPACE_WIDTH}))")
-
-target_link_libraries(rmm-lib-realm
-    PRIVATE rmm-lib-arch
-            rmm-lib-common
-            rmm-lib-debug
-            rmm-lib-xlat)
-
-# Needed to link with the platform callbacks for buffer map/unmap
-if(RMM_ARCH STREQUAL fake_host)
-    target_link_libraries(rmm-lib-realm
-        PRIVATE rmm-platform)
-endif()
-
-target_link_libraries(rmm-lib-realm
-    PUBLIC rmm-lib-attestation
-           rmm-lib-gic
-           rmm-lib-measurement
-           rmm-lib-s2tt
-           rmm-lib-smc)
-
-target_include_directories(rmm-lib-realm
-    PUBLIC "include"
-    PRIVATE "src/include/${RMM_ARCH}"
-    PRIVATE "src/include")
-
-target_sources(rmm-lib-realm
-    PRIVATE "src/buffer.c"
-            "src/granule.c")
-
-include (tests/CMakeLists.txt)
diff --git a/lib/realm/tests/CMakeLists.txt b/lib/realm/tests/CMakeLists.txt
deleted file mode 100644
index bc7eb04..0000000
--- a/lib/realm/tests/CMakeLists.txt
+++ /dev/null
@@ -1,22 +0,0 @@
-#
-# SPDX-License-Identifier: BSD-3-Clause
-# SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
-#
-
-target_include_directories(rmm-lib-realm
-    PRIVATE "tests")
-
-# Add test functionality
-rmm_build_unittest(NAME granule
-                   TARGET rmm-lib-realm
-                   SOURCES "tests/granule.cpp"
-                           "tests/realm_test_utils.c"
-                           "tests/test_harness.c"
-                   ITERATIONS 10)
-
-rmm_build_unittest(NAME slot_buffer
-                   TARGET rmm-lib-realm
-                   SOURCES "tests/buffer.cpp"
-                           "tests/realm_test_utils.c"
-                           "tests/test_harness.c"
-                   ITERATIONS 10)
diff --git a/lib/realm/tests/realm_test_utils.c b/lib/realm/tests/realm_test_utils.c
deleted file mode 100644
index ea17f69..0000000
--- a/lib/realm/tests/realm_test_utils.c
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * SPDX-License-Identifier: BSD-3-Clause
- * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
- */
-
-#include <buffer.h>
-#include <buffer_private.h>
-#include <granule.h>
-#include <host_utils.h>
-#include <xlat_tables.h>
-
-/*
- * Return the PA mapped to a given slot.
- *
- * NOTE:	This API assumes a 4KB granularity and that the architecture
- *		has a VA space of 48 bits.
- */
-uintptr_t realm_test_util_slot_to_pa(enum buffer_slot slot)
-{
-	struct xlat_llt_info *entry = get_cached_llt_info();
-	uintptr_t va = slot_to_va(slot);
-	uint64_t *desc_ptr = xlat_get_tte_ptr(entry, va);
-	uint64_t descriptor = xlat_read_tte(desc_ptr);
-
-	return (uintptr_t)xlat_get_oa_from_tte(descriptor);
-}
-
-/*
- * Helper function to find the slot VA to which a PA is mapped to.
- * This function is used to validate that the slot buffer library
- * mapped the given PA to the VA that would be expected by the
- * aarch64 VMSA.
- */
-uintptr_t realm_test_util_slot_va_from_pa(uintptr_t pa)
-{
-	for (unsigned int i = 0U; i < (unsigned int)NR_CPU_SLOTS; i++) {
-		if (pa == realm_test_util_slot_to_pa((enum buffer_slot)i)) {
-			/*
-			 * Found a slot returning the same address, get
-			 * the VA for that slot (the one that would be
-			 * used by the aarch64 VMSA).
-			 */
-			return slot_to_va((enum buffer_slot)i);
-		}
-	}
-
-	/* No buffer slot found */
-	return (uintptr_t)NULL;
-}
-
diff --git a/lib/realm/tests/realm_test_utils.h b/lib/realm/tests/realm_test_utils.h
deleted file mode 100644
index 9dcc56c..0000000
--- a/lib/realm/tests/realm_test_utils.h
+++ /dev/null
@@ -1,14 +0,0 @@
-/*
- * SPDX-License-Identifier: BSD-3-Clause
- * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
- */
-
-#ifndef REALM_TEST_UTILS_H
-#define REALM_TEST_UTILS_H
-
-#include <buffer.h>
-
-uintptr_t realm_test_util_slot_to_pa(enum buffer_slot slot);
-uintptr_t realm_test_util_slot_va_from_pa(uintptr_t pa);
-
-#endif /* REALM_TEST_UTILS_H */
diff --git a/lib/realm/tests/test_harness.c b/lib/realm/tests/test_harness.c
deleted file mode 100644
index fcd6631..0000000
--- a/lib/realm/tests/test_harness.c
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * SPDX-License-Identifier: BSD-3-Clause
- * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
- */
-
-#include <buffer.h>
-#include <realm_test_utils.h>
-#include <stddef.h>
-
-/*
- * Maps addr to the requested slot buffer and returns a pointer which can be
- * accessed for read or write by the tests. The callback maps the `addr` as
- * per aarch64 VMSA and walks the xlat tables to retrieve the original
- * `addr` thus verifying that the `addr` was mapped correctly in the tables.
- */
-void *test_buffer_map_access(unsigned int slot, unsigned long addr)
-{
-	void *va = buffer_map_internal((enum buffer_slot)slot, addr);
-
-	if (va == NULL) {
-		return NULL;
-	}
-
-	/*
-	 * Perform a table walk to get the PA mapped to `slot`.
-	 * If everything went well it should return the same address as `addr`.
-	 */
-	return (void *)realm_test_util_slot_to_pa((enum buffer_slot)slot);
-}
-
-/*
- * Receives an accessible `buf` address corresponding to a mapped
- * slot buffer and unmaps the granule mapped to it.
- */
-void test_buffer_unmap_access(void *buf)
-{
-	void *slot_va =
-		(void *)realm_test_util_slot_va_from_pa((uintptr_t)buf);
-
-	assert(slot_va != NULL);
-
-	buffer_unmap_internal(slot_va);
-}
-
-/*
- * Maps addr to the requested slot buffer and returns a mapped VA
- * corresponding to the slot buffer as per aarch64 VMSA.
- */
-void *test_buffer_map_aarch64_vmsa(unsigned int slot, unsigned long addr)
-{
-	return buffer_map_internal((enum buffer_slot)slot, addr);
-}
-
-/*
- * Receives an aarch64 VMSA `buf` address corresponding to a mapped
- * slot buffer and unmaps the granule mapped to it.
- */
-void test_buffer_unmap_aarch64_vmsa(void *buf)
-{
-	buffer_unmap_internal(buf);
-}
diff --git a/lib/realm/tests/test_harness.h b/lib/realm/tests/test_harness.h
deleted file mode 100644
index d489074..0000000
--- a/lib/realm/tests/test_harness.h
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * SPDX-License-Identifier: BSD-3-Clause
- * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
- */
-
-#ifndef TEST_HARNESS_H
-#define TEST_HARNESS_H
-
-/*
- * Test specific host_harness functions which are dynamically
- * overridden during test execution.
- */
-
-void *test_buffer_map_access(unsigned int slot, unsigned long addr);
-void test_buffer_unmap_access(void *buf);
-
-void *test_buffer_map_aarch64_vmsa(unsigned int slot, unsigned long addr);
-void test_buffer_unmap_aarch64_vmsa(void *buf);
-
-#endif /* TEST_HARNESS_H */
diff --git a/lib/s2tt/CMakeLists.txt b/lib/s2tt/CMakeLists.txt
index 8c04844..5be6596 100644
--- a/lib/s2tt/CMakeLists.txt
+++ b/lib/s2tt/CMakeLists.txt
@@ -9,7 +9,9 @@
     PRIVATE rmm-lib-arch
             rmm-lib-common
             rmm-lib-debug
-            rmm-lib-realm)
+            rmm-lib-slot_buf
+            rmm-lib-smc
+    PUBLIC  rmm-lib-granule)
 
 target_include_directories(rmm-lib-s2tt
     PUBLIC "include"
diff --git a/lib/s2tt/src/s2tt.c b/lib/s2tt/src/s2tt.c
index b603640..61e6d6a 100644
--- a/lib/s2tt/src/s2tt.c
+++ b/lib/s2tt/src/s2tt.c
@@ -6,6 +6,7 @@
 #include <arch_helpers.h>
 #include <assert.h>
 #include <bitmap.h>
+#include <buffer.h>
 #include <granule.h>
 #include <ripas.h>
 #include <s2tt.h>
@@ -238,7 +239,7 @@
 
 	(void)s2_ctx;
 
-	table = granule_map(g_tbl, SLOT_RTT);
+	table = buffer_granule_map(g_tbl, SLOT_RTT);
 	assert(table != NULL);
 
 	entry = s2tte_read(&table[idx]);
diff --git a/lib/slot_buf/CMakeLists.txt b/lib/slot_buf/CMakeLists.txt
new file mode 100644
index 0000000..b45c3c2
--- /dev/null
+++ b/lib/slot_buf/CMakeLists.txt
@@ -0,0 +1,50 @@
+#
+# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
+#
+
+add_library(rmm-lib-slot_buf)
+
+arm_config_option(
+    NAME VIRT_ADDR_SPACE_WIDTH
+    HELP "Size in bits of the virtual address space."
+    DEFAULT 38
+    TYPE STRING
+    ADVANCED)
+
+if(VIRT_ADDR_SPACE_WIDTH EQUAL 0x0)
+    message(FATAL_ERROR "VIRT_ADDR_SPACE_WIDTH is not initialized")
+endif()
+
+target_compile_definitions(rmm-lib-slot_buf
+    PUBLIC "VIRT_ADDR_SPACE_SIZE=(1ULL << ULL(${VIRT_ADDR_SPACE_WIDTH}))")
+
+target_link_libraries(rmm-lib-slot_buf
+    PRIVATE rmm-lib-arch
+            rmm-lib-common
+            rmm-lib-debug
+            rmm-lib-xlat)
+
+target_link_libraries(rmm-lib-slot_buf
+    PUBLIC rmm-lib-granule
+           rmm-lib-smc)
+
+target_include_directories(rmm-lib-slot_buf
+    PUBLIC "include"
+    PRIVATE "src/include"
+            "src/include/${RMM_ARCH}")
+
+if(RMM_ARCH STREQUAL fake_host)
+    # Needed to access the platform callbacks.
+    target_link_libraries(rmm-lib-slot_buf
+        PRIVATE rmm-platform)
+else()
+    target_sources(rmm-lib-slot_buf
+        PRIVATE "src/aarch64/ns_access.S")
+endif()
+
+target_sources(rmm-lib-slot_buf
+    PRIVATE "src/buffer.c")
+
+include (tests/CMakeLists.txt)
+
diff --git a/lib/realm/include/buffer.h b/lib/slot_buf/include/buffer.h
similarity index 78%
rename from lib/realm/include/buffer.h
rename to lib/slot_buf/include/buffer.h
index 20ea2db..a9754db 100644
--- a/lib/realm/include/buffer.h
+++ b/lib/slot_buf/include/buffer.h
@@ -6,9 +6,8 @@
 #ifndef BUFFER_H
 #define BUFFER_H
 
-#include <assert.h>
+#include <granule.h>
 #include <smc-rmi.h>
-#include <stdbool.h>
 #include <stddef.h>
 #include <utils_def.h>
 
@@ -37,10 +36,8 @@
 	NR_CPU_SLOTS
 };
 
-struct granule;
-
 bool check_cpu_slots_empty(void);
-void *granule_map(struct granule *g, enum buffer_slot slot);
+void *buffer_granule_map(struct granule *g, enum buffer_slot slot);
 void buffer_unmap(void *buf);
 
 bool ns_buffer_read(enum buffer_slot slot,
@@ -62,6 +59,22 @@
  */
 void slot_buf_finish_warmboot_init(void);
 
+/*
+ * Maps the `num_aux` SLOT_REC_AUX granules.
+ */
+void *buffer_aux_granules_map(struct granule *g_rec_aux[], unsigned int num_aux);
+
+/*
+ * Unmaps the `num_aux` SLOT_REC_AUX buffers starting with the one
+ * passed at the beginning of `rec_aux`.
+ */
+void buffer_aux_unmap(void *rec_aux, unsigned int num_aux);
+
+/*
+ * Map the granule 'g' to 'slot', zeroes its content and unmaps it.
+ */
+void buffer_granule_memzero(struct granule *g, enum buffer_slot slot);
+
 /******************************************************************************
  * Internal APIs not meant to be invoked by generic RMM code.
  * These are exposed to facilitate testing.
diff --git a/runtime/core/aarch64/ns_access.S b/lib/slot_buf/src/aarch64/ns_access.S
similarity index 100%
rename from runtime/core/aarch64/ns_access.S
rename to lib/slot_buf/src/aarch64/ns_access.S
diff --git a/lib/realm/src/buffer.c b/lib/slot_buf/src/buffer.c
similarity index 81%
rename from lib/realm/src/buffer.c
rename to lib/slot_buf/src/buffer.c
index ebc26e2..f15fda9 100644
--- a/lib/realm/src/buffer.c
+++ b/lib/slot_buf/src/buffer.c
@@ -15,6 +15,8 @@
 #include <slot_buf_arch.h>
 #include <stdbool.h>
 #include <stdint.h>
+/* coverity[unnecessary_header: SUPPRESS] */
+#include <string.h>
 #include <utils_def.h>
 #include <xlat_contexts.h>
 #include <xlat_high_va.h>
@@ -102,7 +104,7 @@
 	return (slot != SLOT_NS) && (slot < NR_CPU_SLOTS);
 }
 
-static void *ns_granule_map(enum buffer_slot slot, struct granule *granule)
+static void *ns_buffer_granule_map(enum buffer_slot slot, struct granule *granule)
 {
 	unsigned long addr = granule_addr(granule);
 
@@ -121,7 +123,7 @@
  *
  * The caller must either hold @g::lock or hold a reference.
  */
-void *granule_map(struct granule *g, enum buffer_slot slot)
+void *buffer_granule_map(struct granule *g, enum buffer_slot slot)
 {
 	unsigned long addr = granule_addr(g);
 
@@ -168,7 +170,7 @@
 	offset &= (unsigned int)(~GRANULE_MASK);
 	assert((offset + size) <= GRANULE_SIZE);
 
-	src = (uintptr_t)ns_granule_map(slot, ns_gr);
+	src = (uintptr_t)ns_buffer_granule_map(slot, ns_gr);
 	retval = memcpy_ns_read(dest, (void *)(src + offset), size);
 	ns_buffer_unmap((void *)src);
 
@@ -208,13 +210,64 @@
 	offset &= (unsigned int)(~GRANULE_MASK);
 	assert((offset + size) <= GRANULE_SIZE);
 
-	dest = (uintptr_t)ns_granule_map(slot, ns_gr);
+	dest = (uintptr_t)ns_buffer_granule_map(slot, ns_gr);
 	retval = memcpy_ns_write((void *)(dest + offset), src, size);
 	ns_buffer_unmap((void *)dest);
 
 	return retval;
 }
 
+/*
+ * The parent REC granules lock is expected to be acquired before functions
+ * buffer_aux_granules_map() and buffer_aux_unmap() are called.
+ */
+void *buffer_aux_granules_map(struct granule *g_rec_aux[], unsigned int num_aux)
+{
+	void *rec_aux = NULL;
+
+	assert(g_rec_aux != NULL);
+	assert(num_aux <= MAX_REC_AUX_GRANULES);
+
+	for (unsigned int i = 0U; i < num_aux; i++) {
+		void *aux = buffer_granule_map(g_rec_aux[i],
+					(enum buffer_slot)((unsigned int)
+							   SLOT_REC_AUX0 + i));
+
+		assert(aux != NULL);
+
+		if (i == 0UL) {
+			rec_aux = aux;
+		}
+	}
+	return rec_aux;
+}
+
+void buffer_aux_unmap(void *rec_aux, unsigned int num_aux)
+{
+	unsigned char *rec_aux_vaddr = (unsigned char *)rec_aux;
+
+	assert(rec_aux != NULL);
+	assert(num_aux <= MAX_REC_AUX_GRANULES);
+
+	for (unsigned int i = 0U; i < num_aux; i++) {
+		buffer_unmap((void *)((uintptr_t)rec_aux_vaddr +
+							(i * GRANULE_SIZE)));
+	}
+}
+
+void buffer_granule_memzero(struct granule *g, enum buffer_slot slot)
+{
+	unsigned long *buf;
+
+	assert(g != NULL);
+
+	buf = buffer_granule_map(g, slot);
+	assert(buf != NULL);
+
+	(void)memset(buf, 0, GRANULE_SIZE);
+	buffer_unmap(buf);
+}
+
 /******************************************************************************
  * Internal helpers
  ******************************************************************************/
diff --git a/lib/realm/src/include/aarch64/slot_buf_arch.h b/lib/slot_buf/src/include/aarch64/slot_buf_arch.h
similarity index 100%
rename from lib/realm/src/include/aarch64/slot_buf_arch.h
rename to lib/slot_buf/src/include/aarch64/slot_buf_arch.h
diff --git a/lib/realm/src/include/buffer_private.h b/lib/slot_buf/src/include/buffer_private.h
similarity index 100%
rename from lib/realm/src/include/buffer_private.h
rename to lib/slot_buf/src/include/buffer_private.h
diff --git a/lib/realm/src/include/fake_host/slot_buf_arch.h b/lib/slot_buf/src/include/fake_host/slot_buf_arch.h
similarity index 100%
rename from lib/realm/src/include/fake_host/slot_buf_arch.h
rename to lib/slot_buf/src/include/fake_host/slot_buf_arch.h
diff --git a/lib/slot_buf/tests/CMakeLists.txt b/lib/slot_buf/tests/CMakeLists.txt
new file mode 100644
index 0000000..62035fb
--- /dev/null
+++ b/lib/slot_buf/tests/CMakeLists.txt
@@ -0,0 +1,16 @@
+#
+# SPDX-License-Identifier: BSD-3-Clause
+# SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
+#
+
+if(RMM_UNITTESTS)
+    target_include_directories(rmm-lib-slot_buf
+        PRIVATE "tests")
+endif()
+
+# Add test functionality
+rmm_build_unittest(NAME slot_buffer
+                   TARGET rmm-lib-slot_buf
+                   SOURCES "tests/buffer.cpp"
+                           "tests/buffer_test_helpers.c"
+                   ITERATIONS 10)
diff --git a/lib/realm/tests/buffer.cpp b/lib/slot_buf/tests/buffer.cpp
similarity index 85%
rename from lib/realm/tests/buffer.cpp
rename to lib/slot_buf/tests/buffer.cpp
index 74a4d7c..fc3a1a2 100644
--- a/lib/realm/tests/buffer.cpp
+++ b/lib/slot_buf/tests/buffer.cpp
@@ -9,12 +9,12 @@
 extern "C" {
 #include <buffer.h>	/* Interface to exercise */
 #include <buffer_private.h>
+#include <buffer_test_helpers.h>
 #include <cpuid.h>
 #include <granule.h>
 #include <host_defs.h>
 #include <host_harness.h>
 #include <host_utils.h>
-#include <realm_test_utils.h>
 #include <stdlib.h>
 #include <string.h>
 #include <test_harness.h>
@@ -29,6 +29,13 @@
 #define GRANULE_BLOCK_SIZE		(GRANULE_SIZE >> 2U)
 #define GRANULE_BLOCKS			(GRANULE_SIZE/GRANULE_BLOCK_SIZE)
 
+/* Function to get a random granule index in the range [1, NR_GRANULES - 2] */
+static inline unsigned int get_rand_granule_idx(void)
+{
+	return (unsigned int)test_helpers_get_rand_in_range(1UL,
+					test_helpers_get_nr_granules() - 2U);
+}
+
 /*
  * Function to get a random granule address within the valid address range.
  */
@@ -104,7 +111,7 @@
 	}
 };
 
-TEST(slot_buffer, granule_map_buffer_unmap_TC1)
+TEST(slot_buffer, buffer_granule_map_buffer_unmap_TC1)
 {
 	uintptr_t slot_va, expected_va, granule_addr;
 	struct granule *test_granule;
@@ -118,9 +125,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_aarch64_vmsa;
+	cb.buffer_map = buffer_test_cb_map_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
+	cb.buffer_unmap = buffer_test_cb_unmap_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	granule_addr = get_rand_granule_addr();
@@ -130,14 +137,14 @@
 		host_util_set_cpuid(i);
 		for (unsigned int j = 0U; j < NR_CPU_SLOTS; j++) {
 			if (j == SLOT_NS) {
-				/* Not supported. granule_map() would assert */
+				/* Not supported. buffer_granule_map() would assert */
 				continue;
 			}
-			slot_va = (uintptr_t)granule_map(test_granule,
+			slot_va = (uintptr_t)buffer_granule_map(test_granule,
 							 (enum buffer_slot)j);
 			expected_va = slot_to_va((enum buffer_slot)j);
 
-			/* Test the return value from granule_map() */
+			/* Test the return value from buffer_granule_map() */
 			POINTERS_EQUAL(slot_va, expected_va);
 
 			/*
@@ -146,24 +153,24 @@
 			 * aarch64 VMSA.
 			 */
 			POINTERS_EQUAL(expected_va,
-				realm_test_util_slot_va_from_pa(granule_addr));
+				buffer_test_helpers_slot_va_from_pa(granule_addr));
 
 			/* Unmap the buffer */
 			buffer_unmap((void *)slot_va);
 
 			/*
-			 * realm_test_util_slot_va_from_pa() return NULL
+			 * buffer_test_helpers_slot_va_from_pa() return NULL
 			 * if the address passed to it is not mapped to any
 			 * slot buffer.
 			 */
 			POINTERS_EQUAL(NULL,
-				realm_test_util_slot_va_from_pa(granule_addr));
+				buffer_test_helpers_slot_va_from_pa(granule_addr));
 
 		} /* For each slot type */
 	} /* For each CPU */
 }
 
-TEST(slot_buffer, granule_map_buffer_unmap_TC2)
+TEST(slot_buffer, buffer_granule_map_buffer_unmap_TC2)
 {
 	uintptr_t mapped_pa;
 	struct granule *test_granule;
@@ -180,15 +187,15 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_aarch64_vmsa;
+	cb.buffer_map = buffer_test_cb_map_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
+	cb.buffer_unmap = buffer_test_cb_unmap_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	get_rand_granule_array(granules_per_cpu, MAX_CPUS);
 	for (unsigned int i = 0U; i < NR_CPU_SLOTS; i++) {
 		if (i == SLOT_NS) {
-			/* Not supported. granule_map() would assert */
+			/* Not supported. buffer_granule_map() would assert */
 			continue;
 		}
 
@@ -196,7 +203,7 @@
 		for (unsigned int j = 0U; j < MAX_CPUS; j++) {
 			host_util_set_cpuid(j);
 			test_granule = addr_to_granule(granules_per_cpu[j]);
-			slot_va[j] = granule_map(test_granule,
+			slot_va[j] = buffer_granule_map(test_granule,
 						 (enum buffer_slot)i);
 		}
 
@@ -209,7 +216,7 @@
 			 * Get the PA mapped to the slot 'i' for CPU 'j'
 			 */
 			host_util_set_cpuid(j);
-			mapped_pa = realm_test_util_slot_to_pa(
+			mapped_pa = buffer_test_helpers_slot_to_pa(
 						(enum buffer_slot)i);
 
 			/*
@@ -237,7 +244,7 @@
 	} /* NR_CPU_SLOTS */
 };
 
-TEST(slot_buffer, granule_map_buffer_unmap_TC3)
+TEST(slot_buffer, buffer_granule_map_buffer_unmap_TC3)
 {
 	/******************************************************************
 	 * TEST CASE 3:
@@ -250,7 +257,7 @@
 	TEST_EXIT;
 }
 
-TEST(slot_buffer, granule_map_buffer_unmap_TC4)
+TEST(slot_buffer, buffer_granule_map_buffer_unmap_TC4)
 {
 	/******************************************************************
 	 * TEST CASE 4:
@@ -263,7 +270,7 @@
 	TEST_EXIT;
 }
 
-ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC5)
+ASSERT_TEST(slot_buffer, buffer_granule_map_buffer_unmap_TC5)
 {
 	uintptr_t granule_addr;
 	struct granule *test_granule;
@@ -278,9 +285,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_aarch64_vmsa;
+	cb.buffer_map = buffer_test_cb_map_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
+	cb.buffer_unmap = buffer_test_cb_unmap_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	granule_addr = get_rand_granule_addr();
@@ -290,11 +297,11 @@
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
-	(void)granule_map(test_granule, SLOT_NS);
+	(void)buffer_granule_map(test_granule, SLOT_NS);
 	test_helpers_fail_if_no_assert_failed();
 }
 
-ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC6)
+ASSERT_TEST(slot_buffer, buffer_granule_map_buffer_unmap_TC6)
 {
 	union test_harness_cbs cb;
 	unsigned int cpuid;
@@ -309,9 +316,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_aarch64_vmsa;
+	cb.buffer_map = buffer_test_cb_map_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
+	cb.buffer_unmap = buffer_test_cb_unmap_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	slot = (enum buffer_slot)test_helpers_get_rand_in_range(
@@ -322,11 +329,11 @@
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
-	(void)granule_map((struct granule *)NULL, slot);
+	(void)buffer_granule_map((struct granule *)NULL, slot);
 	test_helpers_fail_if_no_assert_failed();
 }
 
-ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC7)
+ASSERT_TEST(slot_buffer, buffer_granule_map_buffer_unmap_TC7)
 {
 	union test_harness_cbs cb;
 	unsigned int cpuid;
@@ -343,9 +350,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_aarch64_vmsa;
+	cb.buffer_map = buffer_test_cb_map_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
+	cb.buffer_unmap = buffer_test_cb_unmap_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	test_granule = test_helpers_granule_struct_base() - 1U;
@@ -357,11 +364,11 @@
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
-	(void)granule_map(test_granule, slot);
+	(void)buffer_granule_map(test_granule, slot);
 	test_helpers_fail_if_no_assert_failed();
 }
 
-ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC8)
+ASSERT_TEST(slot_buffer, buffer_granule_map_buffer_unmap_TC8)
 {
 	union test_harness_cbs cb;
 	unsigned int cpuid;
@@ -377,9 +384,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_aarch64_vmsa;
+	cb.buffer_map = buffer_test_cb_map_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
+	cb.buffer_unmap = buffer_test_cb_unmap_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	test_granule = test_helpers_granule_struct_base() +
@@ -392,11 +399,11 @@
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
-	(void)granule_map(test_granule, slot);
+	(void)buffer_granule_map(test_granule, slot);
 	test_helpers_fail_if_no_assert_failed();
 }
 
-ASSERT_TEST(slot_buffer, granule_map_buffer_unmap_TC9)
+ASSERT_TEST(slot_buffer, buffer_granule_map_buffer_unmap_TC9)
 {
 	uintptr_t granule_addr;
 	uintptr_t test_granule;
@@ -413,9 +420,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_aarch64_vmsa;
+	cb.buffer_map = buffer_test_cb_map_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_aarch64_vmsa;
+	cb.buffer_unmap = buffer_test_cb_unmap_aarch64_vmsa;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	granule_addr = get_rand_granule_addr();
@@ -431,7 +438,7 @@
 	host_util_set_cpuid(cpuid);
 
 	test_helpers_expect_assert_fail(true);
-	(void)granule_map((struct granule *)test_granule, slot);
+	(void)buffer_granule_map((struct granule *)test_granule, slot);
 	test_helpers_fail_if_no_assert_failed();
 }
 
@@ -452,9 +459,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/*
@@ -527,9 +534,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/*
@@ -582,9 +589,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/* Get two random granules, one for each CPU to test. */
@@ -642,9 +649,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/* Get two random granules, one for destination and one for source. */
@@ -680,9 +687,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	granule_addr = get_rand_granule_addr();
@@ -712,9 +719,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	granule_addr = get_rand_granule_addr();
@@ -745,9 +752,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/* Get two random granules, one for destination and one for source. */
@@ -782,9 +789,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/* Get two random granules, one for destination and one for source. */
@@ -818,9 +825,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/* Get two random granules, one for destination and one for source. */
@@ -861,9 +868,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/* Get two random granules, one for destination and one for source. */
@@ -901,9 +908,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/*
@@ -976,9 +983,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/*
@@ -1032,9 +1039,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/* Get a random granule for each CPU to use. */
@@ -1090,9 +1097,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/* Get two random granules, one for destination and one for source. */
@@ -1128,9 +1135,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	granule_addr = get_rand_granule_addr();
@@ -1160,9 +1167,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	granule_addr = get_rand_granule_addr();
@@ -1193,9 +1200,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/* Get two random granules, one for destination and one for source. */
@@ -1230,9 +1237,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/* Get two random granules, one for destination and one for source. */
@@ -1266,9 +1273,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/* Get two random granules, one for destination and one for source. */
@@ -1308,9 +1315,9 @@
 	 ******************************************************************/
 
 	/* Register harness callbacks to use by this test */
-	cb.buffer_map = test_buffer_map_access;
+	cb.buffer_map = buffer_test_cb_map_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
-	cb.buffer_unmap = test_buffer_unmap_access;
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
 	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
 
 	/* Get two random granules, one for destination and one for source. */
@@ -1333,7 +1340,82 @@
 	test_helpers_fail_if_no_assert_failed();
 }
 
-TEST(slot_buffer, slot_buf_finish_warmboot_init_TC1)
+TEST(slot_buffer, buffer_granule_memzero_TC1)
+{
+	unsigned long addrs[3] = {host_util_get_granule_base(),
+				  (get_rand_granule_idx() * GRANULE_SIZE) +
+					host_util_get_granule_base(),
+				  ((test_helpers_get_nr_granules() - 1) *
+								GRANULE_SIZE) +
+					host_util_get_granule_base()};
+	struct granule *granule;
+	int *val;
+	union test_harness_cbs cb;
+
+	/* Register harness callbacks to use by this test */
+	cb.buffer_map = buffer_test_cb_map_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_MAP);
+	cb.buffer_unmap = buffer_test_cb_unmap_access;
+	(void)test_helpers_register_cb(cb, CB_BUFFER_UNMAP);
+
+	/***************************************************************
+	 * TEST CASE 1:
+	 *
+	 * Map a granule to every possible slot type and memzero
+	 * it. Verify then that the whole slot buffer is all 0.
+	 * Test the first and the last valid granules as well as random
+	 * granules in between.
+	 * Repeat the operation on all possible CPUs.
+	 *
+	 * NOTE: buffer_granule_memzero() will fail with SLOT_NS, so skip that
+	 *	 testcase.
+	 ***************************************************************/
+
+	for (unsigned int i = 0U; i < 3U; i++) {
+		granule = addr_to_granule(addrs[i]);
+		val = (int *)addrs[i];
+
+		for (unsigned int j = 0U; j < MAX_CPUS; j++) {
+			/* Configure the cpu id */
+			host_util_set_cpuid(j);
+
+			for (unsigned int k = 0; k < NR_CPU_SLOTS; k++) {
+				if (k == SLOT_NS) {
+					/* Not supported by buffer_granule_memzero */
+					continue;
+				}
+
+				/* Initialize the granule with random data */
+				memset((void *)addrs[i],
+					(int)test_helpers_get_rand_in_range(1UL, INT_MAX),
+					GRANULE_SIZE);
+				buffer_granule_memzero(granule, (enum buffer_slot)k);
+
+				for (unsigned int l = 0;
+				     l < (GRANULE_SIZE / sizeof(int)); l++) {
+					if (*(val + l) != 0) {
+						FAIL_TEST("Memory not properly zeroed");
+					}
+				} /* GRANULE_SIZE */
+			} /* NR_CPU_SLOTS */
+		} /* MAX_CPUS */
+	} /* Number of granules to test */
+}
+
+ASSERT_TEST(slot_buffer, buffer_granule_memzero_TC2)
+{
+	/***************************************************************
+	 * TEST CASE 2:
+	 *
+	 * Verify that buffer_granule_memzero() asserts if granule is NULL
+	 ***************************************************************/
+
+	test_helpers_expect_assert_fail(true);
+	buffer_granule_memzero(NULL, SLOT_DELEGATED);
+	test_helpers_fail_if_no_assert_failed();
+}
+
+IGNORE_TEST(slot_buffer, slot_buf_finish_warmboot_init_TC1)
 {
 	/*
 	 * slot_buf_finish_warmboot_init() has already been used during
diff --git a/lib/slot_buf/tests/buffer_test_helpers.c b/lib/slot_buf/tests/buffer_test_helpers.c
new file mode 100644
index 0000000..73a3a52
--- /dev/null
+++ b/lib/slot_buf/tests/buffer_test_helpers.c
@@ -0,0 +1,103 @@
+/*
+ * SPDX-License-Identifier: BSD-3-Clause
+ * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
+ */
+
+#include <buffer.h>
+#include <buffer_private.h>
+#include <stddef.h>
+#include <test_helpers.h>
+#include <xlat_tables.h>
+
+uintptr_t buffer_test_helpers_slot_to_pa(enum buffer_slot slot)
+{
+	struct xlat_llt_info *entry = get_cached_llt_info();
+	uintptr_t va = slot_to_va(slot);
+	uint64_t *desc_ptr = xlat_get_tte_ptr(entry, va);
+	uint64_t descriptor = xlat_read_tte(desc_ptr);
+
+	return (uintptr_t)xlat_get_oa_from_tte(descriptor);
+}
+
+/*
+ * Helper function to find the slot VA to which a PA is mapped to.
+ * This function is used to validate that the slot buffer library
+ * mapped the given PA to the VA that would be expected by the
+ * aarch64 VMSA.
+ */
+uintptr_t buffer_test_helpers_slot_va_from_pa(uintptr_t pa)
+{
+	for (unsigned int i = 0U; i < (unsigned int)NR_CPU_SLOTS; i++) {
+		if (pa == buffer_test_helpers_slot_to_pa((enum buffer_slot)i)) {
+			/*
+			 * Found a slot returning the same address, get
+			 * the VA for that slot (the one that would be
+			 * used by the aarch64 VMSA).
+			 */
+			return slot_to_va((enum buffer_slot)i);
+		}
+	}
+
+	/* No buffer slot found */
+	return (uintptr_t)NULL;
+}
+
+/*
+ * Callback to mock aarch64 based slot buffer mapping to the buffer tests.
+ * This function maps `addr` to the requested slot in slot buffer and returns
+ * a pointer which can be read or written to by the tests on fake_host
+ * architecture.
+ * Note that the function maps the addr in the Stage 1 xlat tables as per
+ * aarch64 VMSA and it walks the table to retrieve the PA and returns this
+ * back to the caller.
+ */
+void *buffer_test_cb_map_access(unsigned int slot, unsigned long addr)
+{
+	void *va = buffer_map_internal((enum buffer_slot)slot, addr);
+
+	if (va == NULL) {
+		return NULL;
+	}
+
+	/*
+	 * Perform a table walk to get the PA mapped to `slot`.
+	 * If everything went well it should return the same address as `addr`.
+	 */
+	return (void *)buffer_test_helpers_slot_to_pa((enum buffer_slot)slot);
+}
+
+/*
+ * Callback to mock aarch64 based slot buffer ummapping to the buffer tests.
+ * The function receives a `buf` pointer mapped using
+ * buffer_test_cb_map_access(). It needs to find the VA as per aarch64
+ * VMSA based slot buffer and then it uses this va to unmap from Stage 1
+ * xlat tables.
+ */
+void buffer_test_cb_unmap_access(void *buf)
+{
+	void *slot_va =
+		(void *)buffer_test_helpers_slot_va_from_pa((uintptr_t)buf);
+
+	assert(slot_va != NULL);
+
+	buffer_unmap_internal(slot_va);
+}
+
+/*
+ * Callback to map an addr to a slot as per aarch64 VMSA. Note that the address
+ * to the buffer returned by this function cannot be read/written to from
+ * the tests.
+ */
+void *buffer_test_cb_map_aarch64_vmsa(unsigned int slot, unsigned long addr)
+{
+	return buffer_map_internal((enum buffer_slot)slot, addr);
+}
+
+/*
+ * Callback to unmap a buf mapped to a slot as per aarch64 vmsa via
+ * buffer_test_cb_map_aarch64_vmsa() callback.
+ */
+void buffer_test_cb_unmap_aarch64_vmsa(void *buf)
+{
+	buffer_unmap_internal(buf);
+}
diff --git a/lib/slot_buf/tests/buffer_test_helpers.h b/lib/slot_buf/tests/buffer_test_helpers.h
new file mode 100644
index 0000000..6834ce5
--- /dev/null
+++ b/lib/slot_buf/tests/buffer_test_helpers.h
@@ -0,0 +1,34 @@
+/*
+ * SPDX-License-Identifier: BSD-3-Clause
+ * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
+ */
+
+#ifndef BUFFER_TEST_HELPERS_H
+#define BUFFER_TEST_HELPERS_H
+
+
+/*
+ * Return the PA mapped to a given slot.
+ *
+ * NOTE:	This API assumes a 4KB granularity and that the host
+ *		architecture has a VA space of 48 bits.
+ */
+uintptr_t buffer_test_helpers_slot_to_pa(enum buffer_slot slot);
+
+/*
+ * Helper function to find the slot VA to which a PA is mapped to.
+ */
+uintptr_t buffer_test_helpers_slot_va_from_pa(uintptr_t pa);
+
+/*
+ * Test specific test_harness_cbs functions which are dynamically
+ * overridden during test execution.
+ */
+
+void *buffer_test_cb_map_access(unsigned int slot, unsigned long addr);
+void buffer_test_cb_unmap_access(void *buf);
+
+void *buffer_test_cb_map_aarch64_vmsa(unsigned int slot, unsigned long addr);
+void buffer_test_cb_unmap_aarch64_vmsa(void *buf);
+
+#endif /* BUFFER_TEST_HELPERS_H */
diff --git a/plat/host/host_test/src/test_helpers.c b/plat/host/host_test/src/test_helpers.c
index b66e77b..59979c0 100644
--- a/plat/host/host_test/src/test_helpers.c
+++ b/plat/host/host_test/src/test_helpers.c
@@ -4,6 +4,7 @@
  */
 
 #include <arch_helpers.h>
+#include <buffer.h>
 #include <debug.h>
 #include <errno.h>
 #include <granule.h>
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt
index b85c0d1..df6b0d3 100644
--- a/runtime/CMakeLists.txt
+++ b/runtime/CMakeLists.txt
@@ -61,7 +61,6 @@
         PRIVATE "core/aarch64/entry.S"
                 "core/aarch64/head.S"
                 "core/aarch64/helpers.S"
-                "core/aarch64/ns_access.S"
                 "core/aarch64/run-asm.S"
                 "core/aarch64/vectors.S")
 else()
@@ -75,7 +74,7 @@
             "core/init.c"
             "core/inject_exp.c"
             "core/sysregs.c"
-            "core/vmid.c")
+            "core/timers.c")
 
 target_sources(rmm-runtime
     PRIVATE "rmi/feature.c"
diff --git a/runtime/core/run.c b/runtime/core/run.c
index 88a0eb3..d189399 100644
--- a/runtime/core/run.c
+++ b/runtime/core/run.c
@@ -274,7 +274,7 @@
 	rec->ns = ns_state;
 
 	/* Map auxiliary granules */
-	rec_aux = aux_granules_map(rec->g_aux, rec->num_rec_aux);
+	rec_aux = buffer_aux_granules_map(rec->g_aux, rec->num_rec_aux);
 
 	/*
 	 * Associate the attest heap with the current CPU. This heap will be
@@ -377,5 +377,5 @@
 	assert(ret == 0);
 
 	/* Unmap auxiliary granules */
-	aux_granules_unmap(rec_aux, rec->num_rec_aux);
+	buffer_aux_unmap(rec_aux, rec->num_rec_aux);
 }
diff --git a/lib/timers/src/timers.c b/runtime/core/timers.c
similarity index 100%
rename from lib/timers/src/timers.c
rename to runtime/core/timers.c
diff --git a/lib/realm/include/realm.h b/runtime/include/realm.h
similarity index 100%
rename from lib/realm/include/realm.h
rename to runtime/include/realm.h
diff --git a/lib/realm/include/rec.h b/runtime/include/rec.h
similarity index 98%
rename from lib/realm/include/rec.h
rename to runtime/include/rec.h
index 477daa8..d65f723 100644
--- a/lib/realm/include/rec.h
+++ b/runtime/include/rec.h
@@ -113,7 +113,7 @@
 	unsigned long sp_el0;
 	unsigned long icc_sre_el2;
 	struct pmu_state *pmu;
-} __attribute__((aligned(CACHE_WRITEBACK_GRANULE)));
+} __aligned(CACHE_WRITEBACK_GRANULE);
 
 /*
  * Data used when handling attestation requests
diff --git a/lib/timers/include/timers.h b/runtime/include/timers.h
similarity index 100%
rename from lib/timers/include/timers.h
rename to runtime/include/timers.h
diff --git a/runtime/rmi/granule.c b/runtime/rmi/granule.c
index c115fd6..68a0ab2 100644
--- a/runtime/rmi/granule.c
+++ b/runtime/rmi/granule.c
@@ -3,6 +3,7 @@
  * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
  */
 
+#include <buffer.h>
 #include <debug.h>
 #include <granule.h>
 #include <rmm_el3_ifc.h>
@@ -34,7 +35,7 @@
 	}
 
 	granule_set_state(g, GRANULE_STATE_DELEGATED);
-	granule_memzero(g, SLOT_DELEGATED);
+	buffer_granule_memzero(g, SLOT_DELEGATED);
 
 	granule_unlock(g);
 	return RMI_SUCCESS;
diff --git a/runtime/rmi/realm.c b/runtime/rmi/realm.c
index 5019b20..64a2aa5 100644
--- a/runtime/rmi/realm.c
+++ b/runtime/rmi/realm.c
@@ -33,7 +33,7 @@
 		return RMI_ERROR_INPUT;
 	}
 
-	rd = granule_map(g_rd, SLOT_RD);
+	rd = buffer_granule_map(g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	if (get_rd_state_locked(rd) == REALM_NEW) {
@@ -199,7 +199,7 @@
 
 	num_root_rtts = rd->s2_ctx.num_root_rtts;
 	for (unsigned int rtt = 0U; rtt < num_root_rtts; rtt++) {
-		unsigned long *s2tt = granule_map(g_rtt, SLOT_RTT);
+		unsigned long *s2tt = buffer_granule_map(g_rtt, SLOT_RTT);
 
 		assert(s2tt != NULL);
 
@@ -326,7 +326,7 @@
 						(i * sizeof(struct granule)));
 
 		granule_lock(g, GRANULE_STATE_RTT);
-		granule_memzero(g, SLOT_RTT);
+		buffer_granule_memzero(g, SLOT_RTT);
 		granule_unlock_transition(g, GRANULE_STATE_DELEGATED);
 	}
 }
@@ -424,7 +424,7 @@
 		return RMI_ERROR_INPUT;
 	}
 
-	rd = granule_map(g_rd, SLOT_RD);
+	rd = buffer_granule_map(g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	set_rd_state(rd, REALM_NEW);
@@ -513,7 +513,7 @@
 		}
 	}
 
-	rd = granule_map(g_rd, SLOT_RD);
+	rd = buffer_granule_map(g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	g_rtt = rd->s2_ctx.g_rtt;
@@ -538,7 +538,7 @@
 	free_sl_rtts(g_rtt, num_rtts);
 
 	/* This implicitly destroys the measurement */
-	granule_memzero(g_rd, SLOT_RD);
+	buffer_granule_memzero(g_rd, SLOT_RD);
 	granule_unlock_transition(g_rd, GRANULE_STATE_DELEGATED);
 
 	return RMI_SUCCESS;
diff --git a/runtime/rmi/rec.c b/runtime/rmi/rec.c
index 3775670..e396322 100644
--- a/runtime/rmi/rec.c
+++ b/runtime/rmi/rec.c
@@ -163,7 +163,7 @@
 
 		granule_lock(g_rec_aux, GRANULE_STATE_REC_AUX);
 		if (scrub) {
-			granule_memzero(g_rec_aux,
+			buffer_granule_memzero(g_rec_aux,
 			   (enum buffer_slot)((unsigned int)SLOT_REC_AUX0 + i));
 		}
 		granule_unlock_transition(g_rec_aux, GRANULE_STATE_DELEGATED);
@@ -209,7 +209,7 @@
 	struct rec_aux_data *aux_data;
 
 	/* Map auxiliary granules */
-	rec_aux = aux_granules_map(r->g_aux, r->num_rec_aux);
+	rec_aux = buffer_aux_granules_map(r->g_aux, r->num_rec_aux);
 	assert(rec_aux != NULL);
 
 	/*
@@ -241,7 +241,7 @@
 	rec_simd_state_init(r);
 
 	/* Unmap auxiliary granules */
-	aux_granules_unmap(rec_aux, r->num_rec_aux);
+	buffer_aux_unmap(rec_aux, r->num_rec_aux);
 }
 
 unsigned long smc_rec_create(unsigned long rd_addr,
@@ -302,10 +302,10 @@
 		goto out_free_aux;
 	}
 
-	rec = granule_map(g_rec, SLOT_REC);
+	rec = buffer_granule_map(g_rec, SLOT_REC);
 	assert(rec != NULL);
 
-	rd = granule_map(g_rd, SLOT_RD);
+	rd = buffer_granule_map(g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	if (get_rd_state_locked(rd) != REALM_NEW) {
@@ -404,7 +404,7 @@
 		}
 	}
 
-	rec = granule_map(g_rec, SLOT_REC);
+	rec = buffer_granule_map(g_rec, SLOT_REC);
 	assert(rec != NULL);
 
 	g_rd = rec->realm_info.g_rd;
@@ -442,7 +442,7 @@
 		return;
 	}
 
-	rd = granule_map(g_rd, SLOT_RD);
+	rd = buffer_granule_map(g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	num_rec_aux = rd->num_rec_aux;
@@ -493,10 +493,10 @@
 		goto out_unlock;
 	}
 
-	calling_rec = granule_map(g_calling_rec, SLOT_REC);
+	calling_rec = buffer_granule_map(g_calling_rec, SLOT_REC);
 	assert(calling_rec != NULL);
 
-	target_rec = granule_map(g_target_rec, SLOT_REC2);
+	target_rec = buffer_granule_map(g_target_rec, SLOT_REC2);
 	assert(target_rec != NULL);
 
 	ret = psci_complete_request(calling_rec, target_rec, status);
diff --git a/runtime/rmi/rtt.c b/runtime/rmi/rtt.c
index 84e64bc..c21095a 100644
--- a/runtime/rmi/rtt.c
+++ b/runtime/rmi/rtt.c
@@ -15,6 +15,7 @@
 #include <smc-handler.h>
 #include <smc-rmi.h>
 #include <smc.h>
+#include <status.h>
 #include <stddef.h>
 #include <string.h>
 
@@ -97,7 +98,7 @@
 		return RMI_ERROR_INPUT;
 	}
 
-	rd = granule_map(g_rd, SLOT_RD);
+	rd = buffer_granule_map(g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	if (!validate_rtt_structure_cmds(map_addr, level, rd)) {
@@ -126,11 +127,11 @@
 		goto out_unlock_llt;
 	}
 
-	parent_s2tt = granule_map(wi.g_llt, SLOT_RTT);
+	parent_s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
 	assert(parent_s2tt != NULL);
 
 	parent_s2tte = s2tte_read(&parent_s2tt[wi.index]);
-	s2tt = granule_map(g_tbl, SLOT_DELEGATED);
+	s2tt = buffer_granule_map(g_tbl, SLOT_DELEGATED);
 	assert(s2tt != NULL);
 
 	if (s2tte_is_unassigned_empty(&s2_ctx, parent_s2tte)) {
@@ -293,7 +294,7 @@
 		return;
 	}
 
-	rd = granule_map(g_rd, SLOT_RD);
+	rd = buffer_granule_map(g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	if (!validate_rtt_structure_cmds(map_addr, level, rd)) {
@@ -315,7 +316,7 @@
 		goto out_unlock_parent_table;
 	}
 
-	parent_s2tt = granule_map(wi.g_llt, SLOT_RTT);
+	parent_s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
 	assert(parent_s2tt != NULL);
 
 	parent_s2tte = s2tte_read(&parent_s2tt[wi.index]);
@@ -333,7 +334,7 @@
 	 */
 	assert(g_tbl != NULL);
 
-	table = granule_map(g_tbl, SLOT_RTT2);
+	table = buffer_granule_map(g_tbl, SLOT_RTT2);
 	assert(table != NULL);
 
 	/*
@@ -489,7 +490,7 @@
 		return;
 	}
 
-	rd = granule_map(g_rd, SLOT_RD);
+	rd = buffer_granule_map(g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	if (!validate_rtt_structure_cmds(map_addr, level, rd)) {
@@ -508,7 +509,7 @@
 
 	s2tt_walk_lock_unlock(&s2_ctx, map_addr, level - 1L, &wi);
 
-	parent_s2tt = granule_map(wi.g_llt, SLOT_RTT);
+	parent_s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
 	assert(parent_s2tt != NULL);
 
 	parent_s2tte = s2tte_read(&parent_s2tt[wi.index]);
@@ -547,7 +548,7 @@
 	res->x[1] = rtt_addr;
 	skip_non_live = true;
 
-	table = granule_map(g_tbl, SLOT_RTT2);
+	table = buffer_granule_map(g_tbl, SLOT_RTT2);
 	assert(table != NULL);
 
 	if (in_par) {
@@ -624,7 +625,7 @@
 		return;
 	}
 
-	rd = granule_map(g_rd, SLOT_RD);
+	rd = buffer_granule_map(g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	s2_ctx = rd->s2_ctx;
@@ -670,7 +671,7 @@
 		goto out_unlock_llt;
 	}
 
-	s2tt = granule_map(wi.g_llt, SLOT_RTT);
+	s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
 	assert(s2tt != NULL);
 
 	s2tte = s2tte_read(&s2tt[wi.index]);
@@ -771,7 +772,7 @@
 		return;
 	}
 
-	rd = granule_map(g_rd, SLOT_RD);
+	rd = buffer_granule_map(g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	if (!validate_rtt_entry_cmds(map_addr, level, rd)) {
@@ -788,7 +789,7 @@
 	granule_unlock(g_rd);
 
 	s2tt_walk_lock_unlock(&s2_ctx, map_addr, level, &wi);
-	s2tt = granule_map(wi.g_llt, SLOT_RTT);
+	s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
 	assert(s2tt != NULL);
 
 	s2tte = s2tte_read(&s2tt[wi.index]);
@@ -894,7 +895,7 @@
 		return RMI_ERROR_INPUT;
 	}
 
-	rd = granule_map(g_rd, SLOT_RD);
+	rd = buffer_granule_map(g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	ret = (g_src != NULL) ?
@@ -915,7 +916,7 @@
 		goto out_unlock_ll_table;
 	}
 
-	s2tt = granule_map(wi.g_llt, SLOT_RTT);
+	s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
 	assert(s2tt != NULL);
 
 	s2tte = s2tte_read(&s2tt[wi.index]);
@@ -927,7 +928,7 @@
 
 	if (g_src != NULL) {
 		bool ns_access_ok;
-		void *data = granule_map(g_data, SLOT_DELEGATED);
+		void *data = buffer_granule_map(g_data, SLOT_DELEGATED);
 
 		assert(data != NULL);
 
@@ -1024,7 +1025,7 @@
 		return;
 	}
 
-	rd = granule_map(g_rd, SLOT_RD);
+	rd = buffer_granule_map(g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	if (!addr_in_par(rd, map_addr) ||
@@ -1043,7 +1044,7 @@
 	granule_unlock(g_rd);
 
 	s2tt_walk_lock_unlock(&s2_ctx, map_addr, S2TT_PAGE_LEVEL, &wi);
-	s2tt = granule_map(wi.g_llt, SLOT_RTT);
+	s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
 	assert(s2tt != NULL);
 
 	if (wi.last_level != S2TT_PAGE_LEVEL) {
@@ -1084,7 +1085,7 @@
 	 */
 	g_data = find_lock_granule(data_addr, GRANULE_STATE_DATA);
 	assert(g_data != NULL);
-	granule_memzero(g_data, SLOT_DELEGATED);
+	buffer_granule_memzero(g_data, SLOT_DELEGATED);
 	granule_unlock_transition(g_data, GRANULE_STATE_DELEGATED);
 
 	res->x[0] = RMI_SUCCESS;
@@ -1203,7 +1204,7 @@
 		return;
 	}
 
-	rd = granule_map(g_rd, SLOT_RD);
+	rd = buffer_granule_map(g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	if (!validate_map_addr(base, S2TT_PAGE_LEVEL, rd) ||
@@ -1227,7 +1228,7 @@
 
 	s2tt_walk_lock_unlock(s2_ctx, base, S2TT_PAGE_LEVEL, &wi);
 	level = wi.last_level;
-	s2tt = granule_map(wi.g_llt, SLOT_RTT);
+	s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
 	assert(s2tt != NULL);
 
 	map_size = s2tte_map_size(level);
@@ -1381,7 +1382,7 @@
 		goto out_unlock_rec_rd;
 	}
 
-	rec = granule_map(g_rec, SLOT_REC);
+	rec = buffer_granule_map(g_rec, SLOT_REC);
 	assert(rec != NULL);
 
 	if (g_rd != rec->realm_info.g_rd) {
@@ -1402,7 +1403,7 @@
 		goto out_unmap_rec;
 	}
 
-	rd = granule_map(g_rd, SLOT_RD);
+	rd = buffer_granule_map(g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	/*
@@ -1427,7 +1428,7 @@
 		goto out_unlock_llt;
 	}
 
-	s2tt = granule_map(wi.g_llt, SLOT_RTT);
+	s2tt = buffer_granule_map(wi.g_llt, SLOT_RTT);
 	assert(s2tt != NULL);
 
 	rtt_set_ripas_range(s2_ctx, s2tt, base, top, &wi,
diff --git a/runtime/rmi/run.c b/runtime/rmi/run.c
index 2ba56a8..85a1d84 100644
--- a/runtime/rmi/run.c
+++ b/runtime/rmi/run.c
@@ -4,6 +4,7 @@
  */
 
 #include <arch.h>
+#include <buffer.h>
 #include <debug.h>
 #include <esr.h>
 #include <gic.h>
@@ -213,10 +214,10 @@
 		return RMI_ERROR_INPUT;
 	}
 
-	rec = granule_map(g_rec, SLOT_REC);
+	rec = buffer_granule_map(g_rec, SLOT_REC);
 	assert(rec != NULL);
 
-	rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
+	rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	realm_state = get_rd_state_unlocked(rd);
diff --git a/runtime/rsi/config.c b/runtime/rsi/config.c
index 28a5c71..65bc42b 100644
--- a/runtime/rsi/config.c
+++ b/runtime/rsi/config.c
@@ -3,6 +3,7 @@
  * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
  */
 
+#include <buffer.h>
 #include <granule.h>
 #include <realm.h>
 #include <rsi-handler.h>
@@ -42,7 +43,7 @@
 
 	/* Map Realm data granule to RMM address space */
 	gr = find_granule(walk_res.pa);
-	config = (struct rsi_realm_config *)granule_map(gr, SLOT_RSI_CALL);
+	config = (struct rsi_realm_config *)buffer_granule_map(gr, SLOT_RSI_CALL);
 	assert(config != NULL);
 
 	/* Populate config structure */
diff --git a/runtime/rsi/host_call.c b/runtime/rsi/host_call.c
index 097a8d9..bb704aa 100644
--- a/runtime/rsi/host_call.c
+++ b/runtime/rsi/host_call.c
@@ -67,7 +67,7 @@
 
 	/* Map Realm data granule to RMM address space */
 	gr = find_granule(walk_result.pa);
-	data = (uintptr_t)granule_map(gr, SLOT_RSI_CALL);
+	data = (uintptr_t)buffer_granule_map(gr, SLOT_RSI_CALL);
 	assert(data != 0UL);
 
 	host_call = (struct rsi_host_call *)(data + ipa - page_ipa);
diff --git a/runtime/rsi/psci.c b/runtime/rsi/psci.c
index fa2401a..8c8da19 100644
--- a/runtime/rsi/psci.c
+++ b/runtime/rsi/psci.c
@@ -3,6 +3,7 @@
  * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
  */
 
+#include <buffer.h>
 #include <granule.h>
 #include <psci.h>
 #include <realm.h>
@@ -110,7 +111,7 @@
 static unsigned long rd_map_read_rec_count(struct granule *g_rd)
 {
 	unsigned long rec_count;
-	struct rd *rd = granule_map(g_rd, SLOT_RD);
+	struct rd *rd = buffer_granule_map(g_rd, SLOT_RD);
 
 	assert(rd != NULL);
 
@@ -228,7 +229,7 @@
 	 * the rd lock here before we set the Realm's new state.
 	 */
 	granule_lock(g_rd, GRANULE_STATE_RD);
-	rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
+	rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	set_rd_state(rd, REALM_SYSTEM_OFF);
diff --git a/runtime/rsi/realm_attest.c b/runtime/rsi/realm_attest.c
index 2396849..29df074 100644
--- a/runtime/rsi/realm_attest.c
+++ b/runtime/rsi/realm_attest.c
@@ -4,6 +4,7 @@
  */
 
 #include <attestation.h>
+#include <buffer.h>
 #include <debug.h>
 #include <granule.h>
 #include <measurement.h>
@@ -113,7 +114,7 @@
 
 	/* Map realm data granule to RMM address space */
 	gr = find_granule(walk_res.pa);
-	realm_att_token = (uintptr_t)granule_map(gr, SLOT_RSI_CALL);
+	realm_att_token = (uintptr_t)buffer_granule_map(gr, SLOT_RSI_CALL);
 	assert(realm_att_token != 0UL);
 
 	if (attest_data->token_sign_ctx.copied_len == 0UL) {
@@ -210,7 +211,7 @@
 	 * simultaneously by another rec
 	 */
 	granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD);
-	rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
+	rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	/* Save challenge value in the context */
@@ -328,7 +329,7 @@
 
 	assert(g_rd != NULL);
 
-	rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
+	rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	/*
@@ -391,7 +392,7 @@
 	 * simultaneously by another rec
 	 */
 	granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD);
-	rd = granule_map(rec->realm_info.g_rd, SLOT_RD);
+	rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD);
 	assert(rd != NULL);
 
 	/* Number of 8-bytes words in measurement */
diff --git a/runtime/rsi/realm_ipa_helper.c b/runtime/rsi/realm_ipa_helper.c
index 4a7b3bd..a1395ba 100644
--- a/runtime/rsi/realm_ipa_helper.c
+++ b/runtime/rsi/realm_ipa_helper.c
@@ -54,7 +54,7 @@
 
 	s2tt_walk_lock_unlock(s2_ctx, ipa, S2TT_PAGE_LEVEL, &wi);
 
-	ll_table = granule_map(wi.g_llt, SLOT_RTT);
+	ll_table = buffer_granule_map(wi.g_llt, SLOT_RTT);
 	assert(ll_table != NULL);
 
 	s2tte = s2tte_read(&ll_table[wi.index]);
@@ -122,7 +122,7 @@
 
 	s2tt_walk_lock_unlock(s2_ctx, ipa, S2TT_PAGE_LEVEL, &wi);
 
-	ll_table = granule_map(wi.g_llt, SLOT_RTT);
+	ll_table = buffer_granule_map(wi.g_llt, SLOT_RTT);
 	assert(ll_table != NULL);
 
 	s2tte = s2tte_read(&ll_table[wi.index]);