blob: cbdd1986f6cd58d7a7920408aee4d1a510c0d5c3 [file] [log] [blame]
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001/*
2 * Copyright (c) 2015, Linaro Limited
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
Jens Wiklander67a65412016-04-05 22:38:26 +020028#include <atomic.h>
Jens Wiklanderac27ec12015-07-15 15:23:14 +020029#include <tee_ta_api.h>
30#include <tee_api.h>
31#include <ta_concurrent.h>
32#include <trace.h>
33#include <utee_defines.h>
34
Jens Wiklanderac27ec12015-07-15 15:23:14 +020035TEE_Result TA_CreateEntryPoint(void)
36{
37 return TEE_SUCCESS;
38}
39
40void TA_DestroyEntryPoint(void)
41{
42}
43
44TEE_Result TA_OpenSessionEntryPoint(uint32_t param_types,
45 TEE_Param params[4],
46 void **session_ctx)
47{
48 (void)param_types;
49 (void)params;
50 (void)session_ctx;
51 return TEE_SUCCESS;
52}
53
54void TA_CloseSessionEntryPoint(void *session_ctx)
55{
56 (void)session_ctx;
57}
58
59static uint32_t inc_active_count(struct ta_concurrent_shm *shm)
60{
Jens Wiklander67a65412016-04-05 22:38:26 +020061 return atomic_inc32(&shm->active_count);
Jens Wiklanderac27ec12015-07-15 15:23:14 +020062}
63
64static uint32_t dec_active_count(struct ta_concurrent_shm *shm)
65{
Jens Wiklander67a65412016-04-05 22:38:26 +020066 return atomic_dec32(&shm->active_count);
Jens Wiklanderac27ec12015-07-15 15:23:14 +020067}
68
Jens Wiklanderac27ec12015-07-15 15:23:14 +020069static TEE_Result ta_entry_busy_loop(uint32_t param_types, TEE_Param params[4])
70{
Etienne Carriere102092e2019-03-28 15:24:22 +010071 size_t num_rounds = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +020072 uint32_t req_param_types =
73 TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
74 TEE_PARAM_TYPE_VALUE_INOUT,
75 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE);
76
77 if (param_types != req_param_types) {
78 EMSG("got param_types 0x%x, expected 0x%x",
79 param_types, req_param_types);
80 return TEE_ERROR_BAD_PARAMETERS;
81 }
82
83 if (params[0].memref.size < sizeof(struct ta_concurrent_shm))
84 return TEE_ERROR_BAD_PARAMETERS;
85
86 params[1].value.b = inc_active_count(params[0].memref.buffer);
87
88 num_rounds = params[1].value.a;
89 while (num_rounds) {
90 volatile size_t n = 1000;
91
92 while (n)
93 n--;
94
95 num_rounds--;
96 }
97
98 dec_active_count(params[0].memref.buffer);
99 return TEE_SUCCESS;
100}
101
102static TEE_Result ta_entry_sha256(uint32_t param_types, TEE_Param params[4])
103{
Etienne Carriere102092e2019-03-28 15:24:22 +0100104 TEE_Result res = TEE_ERROR_GENERIC;
Jens Wiklanderac27ec12015-07-15 15:23:14 +0200105 TEE_OperationHandle op = TEE_HANDLE_NULL;
Etienne Carriere102092e2019-03-28 15:24:22 +0100106 void *out = NULL;
107 uint32_t out_len = 0;
108 size_t num_rounds = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +0200109 uint32_t req_param_types =
110 TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
111 TEE_PARAM_TYPE_VALUE_INOUT,
112 TEE_PARAM_TYPE_MEMREF_INPUT,
113 TEE_PARAM_TYPE_MEMREF_OUTPUT);
114
115 if (param_types != req_param_types) {
116 EMSG("got param_types 0x%x, expected 0x%x",
117 param_types, req_param_types);
118 return TEE_ERROR_BAD_PARAMETERS;
119 }
120
121 if (params[0].memref.size < sizeof(struct ta_concurrent_shm))
122 return TEE_ERROR_BAD_PARAMETERS;
123 if (params[3].memref.size < TEE_SHA256_HASH_SIZE)
124 return TEE_ERROR_BAD_PARAMETERS;
125
126 params[1].value.b = inc_active_count(params[0].memref.buffer);
127
128 out_len = params[3].memref.size;
129 out = TEE_Malloc(out_len, 0);
130 if (!out) {
131 res = TEE_ERROR_OUT_OF_MEMORY;
132 goto out;
133 }
134
135 res = TEE_AllocateOperation(&op, TEE_ALG_SHA256, TEE_MODE_DIGEST, 0);
136 if (res != TEE_SUCCESS)
137 goto out;
138
139
140 num_rounds = params[1].value.a;
141 while (num_rounds) {
142 TEE_ResetOperation(op);
143 res = TEE_DigestDoFinal(op, params[2].memref.buffer,
144 params[2].memref.size, out, &out_len);
145 num_rounds--;
146 }
147
148 TEE_MemMove(params[3].memref.buffer, out, out_len);
149 params[3].memref.size = out_len;
150
151out:
152 if (out)
153 TEE_Free(out);
154 if (op)
155 TEE_FreeOperation(op);
156 dec_active_count(params[0].memref.buffer);
157 return res;
158}
159
160TEE_Result TA_InvokeCommandEntryPoint(void *session_ctx,
161 uint32_t cmd_id, uint32_t param_types,
162 TEE_Param params[4])
163{
164 (void)session_ctx;
165
166 switch (cmd_id) {
167 case TA_CONCURRENT_CMD_BUSY_LOOP:
168 return ta_entry_busy_loop(param_types, params);
169 case TA_CONCURRENT_CMD_SHA256:
170 return ta_entry_sha256(param_types, params);
171 default:
172 return TEE_ERROR_BAD_PARAMETERS;
173 }
174}