SPM: Test open and close handle

The tests request valid and invalid handles and close them.

Change-Id: Ie421507d8dd4793e635e82f74c206529d9ba59d0
Signed-off-by: Antonio Nino Diaz <antonio.ninodiaz@arm.com>
diff --git a/include/common/test_helpers.h b/include/common/test_helpers.h
index 942e9f8..983aed3 100644
--- a/include/common/test_helpers.h
+++ b/include/common/test_helpers.h
@@ -9,6 +9,7 @@
 
 #include <plat_topology.h>
 #include <psci.h>
+#include <spci_svc.h>
 #include <tftf_lib.h>
 #include <trusted_os.h>
 #include <tsp.h>
@@ -113,6 +114,27 @@
 			version & MM_VERSION_MINOR_MASK);			\
 	} while (0)
 
+#define SKIP_TEST_IF_SPCI_VERSION_LESS_THAN(major, minor)			\
+	do {									\
+		smc_args version_smc = { SPCI_VERSION };			\
+		smc_ret_values smc_ret = tftf_smc(&version_smc);		\
+		uint32_t version = smc_ret.ret0;				\
+										\
+		if (version == SMC_UNKNOWN) {					\
+			tftf_testcase_printf("SPM not detected.\n");		\
+			return TEST_RESULT_SKIPPED;				\
+		}								\
+										\
+		if (version < SPCI_VERSION_FORM(major, minor)) {		\
+			tftf_testcase_printf("SPCI_VERSION returned %d.%d\n"	\
+					     "The required version is %d.%d\n",	\
+					     version >> SPCI_VERSION_MAJOR_SHIFT,\
+					     version & SPCI_VERSION_MINOR_MASK,	\
+					     major, minor);			\
+			return TEST_RESULT_SKIPPED;				\
+		}								\
+	} while (0)
+
 /* Helper macro to verify if system suspend API is supported */
 #define is_psci_sys_susp_supported()	\
 		(tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND)	\
diff --git a/include/runtime_services/spci_helpers.h b/include/runtime_services/spci_helpers.h
new file mode 100644
index 0000000..fa7eeae
--- /dev/null
+++ b/include/runtime_services/spci_helpers.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef SPCI_HELPERS_H
+#define SPCI_HELPERS_H
+
+#include <utils_def.h>
+
+/* This error code must be different to the ones used by SPCI */
+#define SPCI_TFTF_ERROR		-42
+
+/* Client ID used for SPCI calls */
+#define TFTF_SPCI_CLIENT_ID	U(0x00007F7F)
+
+#ifndef __ASSEMBLY__
+
+#include <stdint.h>
+#include <types.h>
+
+int spci_service_handle_open(uint16_t client_id, uint16_t *handle,
+			     uint32_t uuid1, uint32_t uuid2,
+			     uint32_t uuid3, uint32_t uuid4);
+int spci_service_handle_close(uint16_t client_id, uint16_t handle);
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* SPCI_HELPERS_H */
diff --git a/spm/cactus/cactus_service_loop.c b/spm/cactus/cactus_service_loop.c
index a6b576d..4b202ad 100644
--- a/spm/cactus/cactus_service_loop.c
+++ b/spm/cactus/cactus_service_loop.c
@@ -6,7 +6,6 @@
 
 #include <assert.h>
 #include <debug.h>
-#include <mm_svc.h>
 #include <sp_helpers.h>
 #include <sprt_svc.h>
 #include <spm_svc.h>
@@ -23,10 +22,10 @@
 	 * secure service requests.
 	 */
 	NOTICE("Cactus: Signal end of init to SPM\n");
-	event_status_code = SPM_SUCCESS;
+	event_status_code = SPRT_SUCCESS;
 
 	while (1) {
-		svc_values.arg0 = SP_EVENT_COMPLETE_AARCH64;
+		svc_values.arg0 = SPRT_REQUEST_COMPLETE_BLOCKING_AARCH64;
 		svc_values.arg1 = event_status_code;
 		int32_t event_id = sp_svc(&svc_values);
 
@@ -34,7 +33,7 @@
 
 		default:
 			NOTICE("Unhandled Service ID 0x%x\n", event_id);
-			event_status_code = SPM_NOT_SUPPORTED;
+			event_status_code = SPRT_NOT_SUPPORTED;
 			break;
 		}
 	}
diff --git a/tftf/framework/framework.mk b/tftf/framework/framework.mk
index 9fdbb7c..be0ab6b 100644
--- a/tftf/framework/framework.mk
+++ b/tftf/framework/framework.mk
@@ -27,7 +27,8 @@
 	-Iinclude/plat/common				\
 	-Iinclude/runtime_services			\
 	-Iinclude/runtime_services/secure_el0_payloads	\
-	-Iinclude/runtime_services/secure_el1_payloads
+	-Iinclude/runtime_services/secure_el1_payloads	\
+	-Ispm/cactus
 
 # Standard C library source files
 STD_LIB_SOURCES		:=	lib/stdlib/abort.c			\
diff --git a/tftf/tests/runtime_services/secure_service/spci_helpers.c b/tftf/tests/runtime_services/secure_service/spci_helpers.c
new file mode 100644
index 0000000..7ffb417
--- /dev/null
+++ b/tftf/tests/runtime_services/secure_service/spci_helpers.c
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <debug.h>
+#include <smccc.h>
+#include <spci_helpers.h>
+#include <spci_svc.h>
+#include <tftf_lib.h>
+
+/* Returns a SPCI error code. On success, it also returns a 16 bit handle. */
+int spci_service_handle_open(uint16_t client_id, uint16_t *handle,
+			     uint32_t uuid1, uint32_t uuid2,
+			     uint32_t uuid3, uint32_t uuid4)
+{
+	int32_t ret;
+
+	smc_args get_handle_smc_args = {
+		SPCI_SERVICE_HANDLE_OPEN,
+		uuid1, uuid2, uuid3, uuid4,
+		0, 0, /* Reserved - MBZ */
+		client_id
+	};
+
+	smc_ret_values smc_ret = tftf_smc(&get_handle_smc_args);
+
+	ret = smc_ret.ret0;
+	if (ret != SPCI_SUCCESS)
+		return ret;
+
+	uint32_t x1 = smc_ret.ret1;
+
+	if ((x1 & 0x0000FFFF) != 0) {
+		tftf_testcase_printf("SPCI_SERVICE_HANDLE_OPEN returned x1 = 0x%08x\n", x1);
+		return SPCI_TFTF_ERROR;
+	}
+
+	/* The handle is returned in the top 16 bits */
+	*handle = (x1 >> 16) & 0xFFFF;
+
+	return SPCI_SUCCESS;
+}
+
+/* Invokes SPCI_SERVICE_HANDLE_CLOSE. Returns a SPCI error code. */
+int spci_service_handle_close(uint16_t client_id, uint16_t handle)
+{
+	smc_args close_handle_smc_args = {
+		SPCI_SERVICE_HANDLE_CLOSE,
+		client_id | (handle << 16)
+	};
+
+	smc_ret_values smc_ret = tftf_smc(&close_handle_smc_args);
+
+	return (int32_t)(uint32_t)smc_ret.ret0;
+}
diff --git a/tftf/tests/runtime_services/secure_service/test_spci_handle_open.c b/tftf/tests/runtime_services/secure_service/test_spci_handle_open.c
new file mode 100644
index 0000000..522465e
--- /dev/null
+++ b/tftf/tests/runtime_services/secure_service/test_spci_handle_open.c
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <arch_helpers.h>
+#include <cactus_def.h>
+#include <debug.h>
+#include <events.h>
+#include <plat_topology.h>
+#include <platform.h>
+#include <smccc.h>
+#include <spci_helpers.h>
+#include <spci_svc.h>
+#include <test_helpers.h>
+#include <tftf_lib.h>
+
+/*
+ * @Test_Aim@ This tests that we can get the handle of a Secure Service and
+ * close it correctly.
+ */
+test_result_t test_spci_handle_open(void)
+{
+	int ret;
+	uint16_t handle1, handle2;
+
+	/**********************************************************************
+	 * Verify that SPCI is there and that it has the correct version.
+	 **********************************************************************/
+
+	SKIP_TEST_IF_SPCI_VERSION_LESS_THAN(0, 1);
+
+	/**********************************************************************
+	 * Try to get handle of an invalid Secure Service.
+	 **********************************************************************/
+
+	ret = spci_service_handle_open(TFTF_SPCI_CLIENT_ID, &handle1,
+				       CACTUS_INVALID_UUID);
+
+	if (ret != SPCI_NOT_PRESENT) {
+		tftf_testcase_printf("%d: SPM should have returned SPCI_NOT_PRESENT. Returned: %d\n",
+				     __LINE__, ret);
+		return TEST_RESULT_FAIL;
+	}
+
+	/**********************************************************************
+	 * Get handle of valid Secure Services.
+	 **********************************************************************/
+
+	ret = spci_service_handle_open(TFTF_SPCI_CLIENT_ID, &handle1,
+				       CACTUS_SERVICE1_UUID);
+
+	if (ret != SPCI_SUCCESS) {
+		tftf_testcase_printf("%d: SPM failed to return a valid handle. Returned: %d\n",
+				     __LINE__, ret);
+		return TEST_RESULT_FAIL;
+	}
+
+	ret = spci_service_handle_open(TFTF_SPCI_CLIENT_ID, &handle2,
+				       CACTUS_SERVICE2_UUID);
+
+	if (ret != SPCI_SUCCESS) {
+		tftf_testcase_printf("%d: SPM failed to return a valid handle. Returned: %d\n",
+				     __LINE__, ret);
+		return TEST_RESULT_FAIL;
+	}
+
+	/**********************************************************************
+	 * Close invalid handle.
+	 **********************************************************************/
+
+	ret = spci_service_handle_close(TFTF_SPCI_CLIENT_ID, ~handle1);
+
+	if (ret != SPCI_INVALID_PARAMETER) {
+		tftf_testcase_printf("%d: SPM didn't fail to close the handle. Returned: %d\n",
+				     __LINE__, ret);
+		return TEST_RESULT_FAIL;
+	}
+
+	/**********************************************************************
+	 * Close valid handles.
+	 **********************************************************************/
+
+	/* Close in the reverse order to test that it can be done. */
+
+	ret = spci_service_handle_close(TFTF_SPCI_CLIENT_ID, handle2);
+
+	if (ret != SPCI_SUCCESS) {
+		tftf_testcase_printf("%d: SPM failed to close the handle. Returned: %d\n",
+				     __LINE__, ret);
+		return TEST_RESULT_FAIL;
+	}
+
+	ret = spci_service_handle_close(TFTF_SPCI_CLIENT_ID, handle1);
+
+	if (ret != SPCI_SUCCESS) {
+		tftf_testcase_printf("%d: SPM failed to close the handle. Returned: %d\n",
+				     __LINE__, ret);
+		return TEST_RESULT_FAIL;
+	}
+
+	/**********************************************************************
+	 * All tests passed.
+	 **********************************************************************/
+
+	return TEST_RESULT_SUCCESS;
+}
diff --git a/tftf/tests/tests-spm.mk b/tftf/tests/tests-spm.mk
index 69a8e49..b1404b6 100644
--- a/tftf/tests/tests-spm.mk
+++ b/tftf/tests/tests-spm.mk
@@ -6,4 +6,6 @@
 
 TESTS_SOURCES	+=							\
 	$(addprefix tftf/tests/runtime_services/secure_service/,	\
+		spci_helpers.c						\
+		test_spci_handle_open.c					\
 	)
diff --git a/tftf/tests/tests-spm.xml b/tftf/tests/tests-spm.xml
index 69636c1..1fa123c 100644
--- a/tftf/tests/tests-spm.xml
+++ b/tftf/tests/tests-spm.xml
@@ -11,6 +11,9 @@
   <testsuite name="Secure Partition Manager"
              description="Test SPM APIs">
 
+     <testcase name="SPCI handle open and close"
+               function="test_spci_handle_open" />
+
   </testsuite>
 
 </testsuites>