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/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;
+	}
+}