blob: 7ec9f899c8f67e4a8cfd3b72a51c8864d732974a [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
69
70static TEE_Result ta_entry_busy_loop(uint32_t param_types, TEE_Param params[4])
71{
72 size_t num_rounds;
73 uint32_t req_param_types =
74 TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
75 TEE_PARAM_TYPE_VALUE_INOUT,
76 TEE_PARAM_TYPE_NONE, TEE_PARAM_TYPE_NONE);
77
78 if (param_types != req_param_types) {
79 EMSG("got param_types 0x%x, expected 0x%x",
80 param_types, req_param_types);
81 return TEE_ERROR_BAD_PARAMETERS;
82 }
83
84 if (params[0].memref.size < sizeof(struct ta_concurrent_shm))
85 return TEE_ERROR_BAD_PARAMETERS;
86
87 params[1].value.b = inc_active_count(params[0].memref.buffer);
88
89 num_rounds = params[1].value.a;
90 while (num_rounds) {
91 volatile size_t n = 1000;
92
93 while (n)
94 n--;
95
96 num_rounds--;
97 }
98
99 dec_active_count(params[0].memref.buffer);
100 return TEE_SUCCESS;
101}
102
103static TEE_Result ta_entry_sha256(uint32_t param_types, TEE_Param params[4])
104{
105 TEE_Result res;
106 TEE_OperationHandle op = TEE_HANDLE_NULL;
107 void *out;
108 uint32_t out_len;
109 size_t num_rounds;
110 uint32_t req_param_types =
111 TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INOUT,
112 TEE_PARAM_TYPE_VALUE_INOUT,
113 TEE_PARAM_TYPE_MEMREF_INPUT,
114 TEE_PARAM_TYPE_MEMREF_OUTPUT);
115
116 if (param_types != req_param_types) {
117 EMSG("got param_types 0x%x, expected 0x%x",
118 param_types, req_param_types);
119 return TEE_ERROR_BAD_PARAMETERS;
120 }
121
122 if (params[0].memref.size < sizeof(struct ta_concurrent_shm))
123 return TEE_ERROR_BAD_PARAMETERS;
124 if (params[3].memref.size < TEE_SHA256_HASH_SIZE)
125 return TEE_ERROR_BAD_PARAMETERS;
126
127 params[1].value.b = inc_active_count(params[0].memref.buffer);
128
129 out_len = params[3].memref.size;
130 out = TEE_Malloc(out_len, 0);
131 if (!out) {
132 res = TEE_ERROR_OUT_OF_MEMORY;
133 goto out;
134 }
135
136 res = TEE_AllocateOperation(&op, TEE_ALG_SHA256, TEE_MODE_DIGEST, 0);
137 if (res != TEE_SUCCESS)
138 goto out;
139
140
141 num_rounds = params[1].value.a;
142 while (num_rounds) {
143 TEE_ResetOperation(op);
144 res = TEE_DigestDoFinal(op, params[2].memref.buffer,
145 params[2].memref.size, out, &out_len);
146 num_rounds--;
147 }
148
149 TEE_MemMove(params[3].memref.buffer, out, out_len);
150 params[3].memref.size = out_len;
151
152out:
153 if (out)
154 TEE_Free(out);
155 if (op)
156 TEE_FreeOperation(op);
157 dec_active_count(params[0].memref.buffer);
158 return res;
159}
160
161TEE_Result TA_InvokeCommandEntryPoint(void *session_ctx,
162 uint32_t cmd_id, uint32_t param_types,
163 TEE_Param params[4])
164{
165 (void)session_ctx;
166
167 switch (cmd_id) {
168 case TA_CONCURRENT_CMD_BUSY_LOOP:
169 return ta_entry_busy_loop(param_types, params);
170 case TA_CONCURRENT_CMD_SHA256:
171 return ta_entry_sha256(param_types, params);
172 default:
173 return TEE_ERROR_BAD_PARAMETERS;
174 }
175}