Add concurrency test 1013

Adds ta_concurrent and test case 1013 to test concurrent execution.

Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org>
Tested-by: Jerome Forissier <jerome.forissier@linaro.org> (HiKey)
Tested-by: Jens Wiklander <jens.wiklander@linaro.org> (qemu, FVP)
diff --git a/host/xtest/Makefile b/host/xtest/Makefile
index 1ea5a88..bcb89c7 100644
--- a/host/xtest/Makefile
+++ b/host/xtest/Makefile
@@ -71,6 +71,7 @@
 CFLAGS += -I../../ta/rpc_test/include
 CFLAGS += -I../../ta/sims/include
 CFLAGS += -I../../ta/storage/include
+CFLAGS += -I../../ta/concurrent/include
 ifdef CFG_GP_PACKAGE_PATH
 CFLAGS += -I../../ta/GP_TTA_Arithmetical
 CFLAGS += -I../../ta/GP_TTA_Crypto
diff --git a/host/xtest/xtest_1000.c b/host/xtest/xtest_1000.c
index bbd3c39..0b22e7a 100644
--- a/host/xtest/xtest_1000.c
+++ b/host/xtest/xtest_1000.c
@@ -29,6 +29,7 @@
 #include <ta_create_fail_test.h>
 #include <ta_rpc_test.h>
 #include <ta_sims_test.h>
+#include <ta_concurrent.h>
 
 static void xtest_tee_test_1001(ADBG_Case_t *Case_p);
 static void xtest_tee_test_1004(ADBG_Case_t *Case_p);
@@ -40,6 +41,7 @@
 static void xtest_tee_test_1010(ADBG_Case_t *Case_p);
 static void xtest_tee_test_1011(ADBG_Case_t *Case_p);
 static void xtest_tee_test_1012(ADBG_Case_t *Case_p);
+static void xtest_tee_test_1013(ADBG_Case_t *Case_p);
 
 ADBG_CASE_DEFINE(XTEST_TEE_1001, xtest_tee_test_1001,
 		/* Title */
@@ -151,6 +153,17 @@
 		"Description of how to implement ..."
 		 );
 
+ADBG_CASE_DEFINE(XTEST_TEE_1013, xtest_tee_test_1013,
+		/* Title */
+		"Test concurency with concurrent TA",
+		/* Short description */
+		"Short description ...",
+		/* Requirement IDs */
+		"TEE-??",
+		/* How to implement */
+		"Description of how to implement ..."
+		 );
+
 struct xtest_crypto_session {
 	ADBG_Case_t *c;
 	TEEC_Session *session;
@@ -1017,3 +1030,157 @@
 		TEEC_CloseSession(&session1);
 	}
 }
+
+struct test_1013_thread_arg {
+	uint32_t cmd;
+	uint32_t repeat;
+	TEEC_SharedMemory *shm;
+	uint32_t error_orig;
+	TEEC_Result res;
+	uint32_t max_concurrency;
+	const uint8_t *in;
+	size_t in_len;
+	uint8_t *out;
+	size_t out_len;
+};
+
+static void *test_1013_thread(void *arg)
+{
+	struct test_1013_thread_arg *a = arg;
+	TEEC_Session session = { 0 };
+	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
+	uint8_t p2 = TEEC_NONE;
+	uint8_t p3 = TEEC_NONE;
+
+	a->res = xtest_teec_open_session(&session, &concurrent_ta_uuid, NULL,
+					 &a->error_orig);
+	if (a->res != TEEC_SUCCESS)
+		return NULL;
+
+	op.params[0].memref.parent = a->shm;
+	op.params[0].memref.size = a->shm->size;
+	op.params[0].memref.offset = 0;
+	op.params[1].value.a = a->repeat;
+	op.params[1].value.b = 0;
+	op.params[2].tmpref.buffer = (void *)a->in;
+	op.params[2].tmpref.size = a->in_len;
+	op.params[3].tmpref.buffer = a->out;
+	op.params[3].tmpref.size = a->out_len;
+
+	if (a->in_len)
+		p2 = TEEC_MEMREF_TEMP_INPUT;
+	if (a->out_len)
+		p3 = TEEC_MEMREF_TEMP_OUTPUT;
+
+	op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
+					 TEEC_VALUE_INOUT, p2, p3);
+
+	a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
+	a->max_concurrency = op.params[1].value.b;
+	a->out_len = op.params[3].tmpref.size;
+	TEEC_CloseSession(&session);
+	return NULL;
+}
+
+static void xtest_tee_test_1013(ADBG_Case_t *c)
+{
+	size_t num_threads = 3;
+	size_t nt;
+	size_t n;
+	pthread_t thr[num_threads];
+	TEEC_SharedMemory shm;
+	size_t max_concurrency;
+	struct test_1013_thread_arg arg[num_threads];
+	static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
+	static const uint8_t sha256_out[] = {
+		0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
+		0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
+		0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
+		0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
+	};
+	uint8_t out[32] = { 0 };
+
+
+	memset(&shm, 0, sizeof(shm));
+	shm.size = sizeof(struct ta_concurrent_shm);
+	shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
+	if (!ADBG_EXPECT_TEEC_SUCCESS(c,
+		TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
+		return;
+
+	Do_ADBG_BeginSubCase(c, "Busy loop with %zu parallel threads",
+			     num_threads);
+
+	memset(shm.buffer, 0, shm.size);
+	memset(arg, 0, sizeof(arg));
+	max_concurrency = 0;
+	nt = num_threads;
+
+	for (n = 0; n < nt; n++) {
+		arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
+		arg[n].repeat = 10000;
+		arg[n].shm = &shm;
+		if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
+						test_1013_thread, arg + n)))
+			nt = n; /* break loop and start cleanup */
+	}
+
+	for (n = 0; n < nt; n++) {
+		ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
+		ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
+		if (arg[n].max_concurrency > max_concurrency)
+			max_concurrency = arg[n].max_concurrency;
+	}
+
+	Do_ADBG_Log("Max concurrency %zu", max_concurrency);
+
+	/*
+	 * Concurrency can be limited by several factors, for instance in a
+	 * single CPU system it's dependent on the Preemtion Model used by
+	 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
+	 * best result there).
+	 */
+	(void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
+	(void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=, num_threads);
+
+	Do_ADBG_EndSubCase(c, "Busy loop with %zu parallel threads",
+			   num_threads);
+
+
+	Do_ADBG_BeginSubCase(c, "Hashing with %zu parallel threads",
+			     num_threads);
+
+	memset(shm.buffer, 0, shm.size);
+	memset(arg, 0, sizeof(arg));
+	max_concurrency = 0;
+	nt = num_threads;
+
+	for (n = 0; n < nt; n++) {
+		arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
+		arg[n].repeat = 1000;
+		arg[n].shm = &shm;
+		arg[n].in = sha256_in;
+		arg[n].in_len = sizeof(sha256_in);
+		arg[n].out = out;
+		arg[n].out_len = sizeof(out);
+		if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
+						test_1013_thread, arg + n)))
+			nt = n; /* break loop and start cleanup */
+	}
+
+	for (n = 0; n < nt; n++) {
+		if (ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)) &&
+		    ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
+			ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
+					   arg[n].out, arg[n].out_len);
+		if (arg[n].max_concurrency > max_concurrency)
+			max_concurrency = arg[n].max_concurrency;
+	}
+
+	Do_ADBG_Log("Max concurrency %zu", max_concurrency);
+
+	Do_ADBG_EndSubCase(c, "Hashing with %zu parallel threads",
+			   num_threads);
+
+	TEEC_ReleaseSharedMemory(&shm);
+}
diff --git a/host/xtest/xtest_main.c b/host/xtest/xtest_main.c
index 1a0a030..098dc53 100644
--- a/host/xtest/xtest_main.c
+++ b/host/xtest/xtest_main.c
@@ -37,6 +37,7 @@
 ADBG_SUITE_ENTRY(XTEST_TEE_1010, NULL)
 ADBG_SUITE_ENTRY(XTEST_TEE_1011, NULL)
 ADBG_SUITE_ENTRY(XTEST_TEE_1012, NULL)
+ADBG_SUITE_ENTRY(XTEST_TEE_1013, NULL)
 ADBG_SUITE_ENTRY(XTEST_TEE_4001, NULL)
 ADBG_SUITE_ENTRY(XTEST_TEE_4002, NULL)
 ADBG_SUITE_ENTRY(XTEST_TEE_4003_NO_XTS, NULL)
diff --git a/host/xtest/xtest_test.c b/host/xtest/xtest_test.c
index 728c63c..f66f879 100644
--- a/host/xtest/xtest_test.c
+++ b/host/xtest/xtest_test.c
@@ -19,6 +19,7 @@
 #include <ta_rpc_test.h>
 #include <ta_sims_test.h>
 #include <ta_storage.h>
+#include <ta_concurrent.h>
 #include <enc_fs_key_manager_test.h>
 #include <tee_api_defines.h>
 #ifdef WITH_GP_TESTS
@@ -78,6 +79,7 @@
 const TEEC_UUID sims_test_ta_uuid = TA_SIMS_TEST_UUID;
 const TEEC_UUID storage_ta_uuid = TA_STORAGE_UUID;
 const TEEC_UUID enc_fs_key_manager_test_ta_uuid = ENC_FS_KEY_MANAGER_TEST_UUID;
+const TEEC_UUID concurrent_ta_uuid = TA_CONCURRENT_UUID;
 #ifdef WITH_GP_TESTS
 const TEEC_UUID gp_tta_ds_uuid = TA_TTA_DS_UUID;
 #endif
diff --git a/host/xtest/xtest_test.h b/host/xtest/xtest_test.h
index 6ce060f..12623e9 100644
--- a/host/xtest/xtest_test.h
+++ b/host/xtest/xtest_test.h
@@ -27,6 +27,7 @@
 ADBG_CASE_DECLARE(XTEST_TEE_1010);
 ADBG_CASE_DECLARE(XTEST_TEE_1011);
 ADBG_CASE_DECLARE(XTEST_TEE_1012);
+ADBG_CASE_DECLARE(XTEST_TEE_1013);
 
 ADBG_CASE_DECLARE(XTEST_TEE_2001);
 ADBG_CASE_DECLARE(XTEST_TEE_2002);
@@ -199,6 +200,7 @@
 extern const TEEC_UUID enc_fs_key_manager_test_ta_uuid;
 extern const TEEC_UUID ecc_test_ta_uuid;
 extern const TEEC_UUID gp_tta_time_uuid;
+extern const TEEC_UUID concurrent_ta_uuid;
 extern char *_device;
 
 #endif /*XTEST_TEST_H*/
diff --git a/ta/Makefile b/ta/Makefile
index 4d68dc4..546d4cb 100644
--- a/ta/Makefile
+++ b/ta/Makefile
@@ -10,7 +10,8 @@
 	   os_test \
 	   rpc_test \
 	   sims \
-	   storage
+	   storage \
+	   concurrent
 
 ifdef CFG_GP_PACKAGE_PATH
 TA_DIRS += GP_TTA_Arithmetical \
diff --git a/ta/concurrent/Makefile b/ta/concurrent/Makefile
new file mode 100644
index 0000000..6145b59
--- /dev/null
+++ b/ta/concurrent/Makefile
@@ -0,0 +1,2 @@
+BINARY =  e13010e0-2ae1-11e5-896a0002a5d5c51b
+include ../ta_common.mk
diff --git a/ta/concurrent/atomic_a32.S b/ta/concurrent/atomic_a32.S
new file mode 100644
index 0000000..f109adf
--- /dev/null
+++ b/ta/concurrent/atomic_a32.S
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/* uint32_t atomic_inc(uint32_t *v); */
+.global atomic_inc
+.func atomic_inc
+atomic_inc:
+	ldrex	r1, [r0]
+	add	r1, r1, #1
+	strex	r2, r1, [r0]
+	cmp	r2, #0
+	bne	atomic_inc
+	mov	r0, r1
+	bx	lr
+.endfunc
+
+/* uint32_t atomic_dec(uint32_t *v); */
+.global atomic_dec
+.func atomic_dec
+atomic_dec:
+	ldrex	r1, [r0]
+	sub	r1, r1, #1
+	strex	r2, r1, [r0]
+	cmp	r2, #0
+	bne	atomic_dec
+	mov	r0, r1
+	bx	lr
+.endfunc
diff --git a/ta/concurrent/include/ta_concurrent.h b/ta/concurrent/include/ta_concurrent.h
new file mode 100644
index 0000000..c742ba6
--- /dev/null
+++ b/ta/concurrent/include/ta_concurrent.h
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef TA_CONCURRENT_H
+#define TA_CONCURRENT_H
+
+/* This UUID is generated with the ITU-T UUID generator at
+   http://www.itu.int/ITU-T/asn1/uuid.html */
+#define TA_CONCURRENT_UUID { 0xe13010e0, 0x2ae1, 0x11e5, \
+	{ 0x89, 0x6a, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }
+
+struct ta_concurrent_shm {
+	uint32_t active_count;
+};
+
+/*
+ * Busy loops and updates max concurrency.  params[0].memref should contain
+ * a struct ta_concurent_shm which can be used to tell how many instances
+ * of this function is running in parallel.
+ *
+ * in/out	params[0].memref
+ * in/out	params[1].value.a	(input) number times to calcule the hash
+ * in/out	params[1].value.b	(output) max concurency
+ */
+
+#define TA_CONCURRENT_CMD_BUSY_LOOP	0
+
+/*
+ * Calculates a sha-256 hash over param[2].memref and stores the result in
+ * params[3].memref. params[0].memref should contain a struct
+ * ta_concurent_shm which can be used to tell how many instances of this
+ * function is running in parallel.
+ *
+ * in/out	params[0].memref
+ * in/out	params[1].value.a	(input) number times to calcule the hash
+ * in/out	params[1].value.b	(output) max concurency
+ * in		params[2].memref
+ * out		params[3].memref
+ */
+#define TA_CONCURRENT_CMD_SHA256	1
+
+#endif /*TA_OS_TEST_H */
diff --git a/ta/concurrent/include/user_ta_header_defines.h b/ta/concurrent/include/user_ta_header_defines.h
new file mode 100644
index 0000000..1e734c1
--- /dev/null
+++ b/ta/concurrent/include/user_ta_header_defines.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef USER_TA_HEADER_DEFINES_H
+#define USER_TA_HEADER_DEFINES_H
+
+#include <ta_concurrent.h>
+#include <user_ta_header.h>
+
+#define TA_UUID TA_CONCURRENT_UUID
+
+#define TA_FLAGS ( TA_FLAG_EXEC_DDR | TA_FLAG_MULTI_SESSION)
+#define TA_STACK_SIZE (2 * 1024)
+#define TA_DATA_SIZE (32 * 1024)
+
+#endif /*USER_TA_HEADER_DEFINES_H*/
diff --git a/ta/concurrent/sub.mk b/ta/concurrent/sub.mk
new file mode 100644
index 0000000..b632aa8
--- /dev/null
+++ b/ta/concurrent/sub.mk
@@ -0,0 +1,3 @@
+global-incdirs-y += include
+srcs-y += ta_entry.c
+srcs-y += atomic_a32.S
diff --git a/ta/concurrent/ta_entry.c b/ta/concurrent/ta_entry.c
new file mode 100644
index 0000000..f94a5b4
--- /dev/null
+++ b/ta/concurrent/ta_entry.c
@@ -0,0 +1,177 @@
+/*
+ * Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <tee_ta_api.h>
+#include <tee_api.h>
+#include <ta_concurrent.h>
+#include <trace.h>
+#include <utee_defines.h>
+
+uint32_t atomic_inc(uint32_t *v);
+uint32_t atomic_dec(uint32_t *v);
+
+TEE_Result TA_CreateEntryPoint(void)
+{
+	return TEE_SUCCESS;
+}
+
+void TA_DestroyEntryPoint(void)
+{
+}
+
+TEE_Result TA_OpenSessionEntryPoint(uint32_t param_types,
+				    TEE_Param params[4],
+				    void **session_ctx)
+{
+	(void)param_types;
+	(void)params;
+	(void)session_ctx;
+	return TEE_SUCCESS;
+}
+
+void TA_CloseSessionEntryPoint(void *session_ctx)
+{
+	(void)session_ctx;
+}
+
+static uint32_t inc_active_count(struct ta_concurrent_shm *shm)
+{
+	return atomic_inc(&shm->active_count);
+}
+
+static uint32_t dec_active_count(struct ta_concurrent_shm *shm)
+{
+	return atomic_dec(&shm->active_count);
+}
+
+
+static TEE_Result ta_entry_busy_loop(uint32_t param_types, TEE_Param params[4])
+{
+	size_t num_rounds;
+	uint32_t req_param_types =
+		TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
+				TEE_PARAM_TYPE_VALUE_INOUT,
+				TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE);
+
+	if (param_types != req_param_types) {
+		EMSG("got param_types 0x%x, expected 0x%x",
+			param_types, req_param_types);
+		return TEE_ERROR_BAD_PARAMETERS;
+	}
+
+	if (params[0].memref.size < sizeof(struct ta_concurrent_shm))
+		return TEE_ERROR_BAD_PARAMETERS;
+
+	params[1].value.b = inc_active_count(params[0].memref.buffer);
+
+	num_rounds = params[1].value.a;
+	while (num_rounds) {
+		volatile size_t n = 1000;
+
+		while (n)
+			n--;
+
+		num_rounds--;
+	}
+
+	dec_active_count(params[0].memref.buffer);
+	return TEE_SUCCESS;
+}
+
+static TEE_Result ta_entry_sha256(uint32_t param_types, TEE_Param params[4])
+{
+	TEE_Result res;
+	TEE_OperationHandle op = TEE_HANDLE_NULL;
+	void *out;
+	uint32_t out_len;
+	size_t num_rounds;
+	uint32_t req_param_types =
+		TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
+				TEE_PARAM_TYPE_VALUE_INOUT,
+				TEE_PARAM_TYPE_MEMREF_INPUT,
+				TEE_PARAM_TYPE_MEMREF_OUTPUT);
+
+	if (param_types != req_param_types) {
+		EMSG("got param_types 0x%x, expected 0x%x",
+			param_types, req_param_types);
+		return TEE_ERROR_BAD_PARAMETERS;
+	}
+
+	if (params[0].memref.size < sizeof(struct ta_concurrent_shm))
+		return TEE_ERROR_BAD_PARAMETERS;
+	if (params[3].memref.size < TEE_SHA256_HASH_SIZE)
+		return TEE_ERROR_BAD_PARAMETERS;
+
+	params[1].value.b = inc_active_count(params[0].memref.buffer);
+
+	out_len = params[3].memref.size;
+	out = TEE_Malloc(out_len, 0);
+	if (!out) {
+		res = TEE_ERROR_OUT_OF_MEMORY;
+		goto out;
+	}
+
+	res = TEE_AllocateOperation(&op, TEE_ALG_SHA256, TEE_MODE_DIGEST, 0);
+	if (res != TEE_SUCCESS)
+		goto out;
+
+
+	num_rounds = params[1].value.a;
+	while (num_rounds) {
+		TEE_ResetOperation(op);
+		res = TEE_DigestDoFinal(op, params[2].memref.buffer,
+					params[2].memref.size, out, &out_len);
+		num_rounds--;
+	}
+
+	TEE_MemMove(params[3].memref.buffer, out, out_len);
+	params[3].memref.size = out_len;
+
+out:
+	if (out)
+		TEE_Free(out);
+	if (op)
+		TEE_FreeOperation(op);
+	dec_active_count(params[0].memref.buffer);
+	return res;
+}
+
+TEE_Result TA_InvokeCommandEntryPoint(void *session_ctx,
+				      uint32_t cmd_id, uint32_t param_types,
+				      TEE_Param params[4])
+{
+	(void)session_ctx;
+
+	switch (cmd_id) {
+	case TA_CONCURRENT_CMD_BUSY_LOOP:
+		return ta_entry_busy_loop(param_types, params);
+	case TA_CONCURRENT_CMD_SHA256:
+		return ta_entry_sha256(param_types, params);
+	default:
+		return TEE_ERROR_BAD_PARAMETERS;
+	}
+}