blob: bbe3d792973734877ba4b712b4ee87fb022d8fd9 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4 */
5
6#include <attestation.h>
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +00007#include <buffer.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +00008#include <debug.h>
9#include <granule.h>
10#include <measurement.h>
11#include <realm.h>
AlexeiFedorov5b186ad2023-04-26 14:43:18 +010012#include <rsi-handler.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000013#include <smc-rsi.h>
14#include <smc.h>
15#include <string.h>
16#include <utils_def.h>
17
AlexeiFedorovefe2aec2023-06-08 16:17:00 +010018#define MAX_EXTENDED_SIZE (64U)
AlexeiFedorov7b3c3042023-06-28 15:41:11 +010019#define MAX_MEASUREMENT_WORDS (MAX_MEASUREMENT_SIZE / sizeof(unsigned long))
Soby Mathewb4c6df42022-11-09 11:13:29 +000020/*
21 * Return the Realm Personalization Value.
22 *
23 * Arguments:
24 * rd - The Realm descriptor.
Mate Toth-Pal071aa562023-07-04 09:09:26 +020025 * claim_ptr - The start address of the Realm Personalization Value claim
26 * claim_len - The length of the Realm Personalization Value claim
Soby Mathewb4c6df42022-11-09 11:13:29 +000027 */
Mate Toth-Pal071aa562023-07-04 09:09:26 +020028static void get_rpv(struct rd *rd, void **claim_ptr, size_t *claim_len)
Soby Mathewb4c6df42022-11-09 11:13:29 +000029{
Mate Toth-Pal071aa562023-07-04 09:09:26 +020030 *claim_ptr = (uint8_t *)&(rd->rpv[0]);
31 *claim_len = RPV_SIZE;
Soby Mathewb4c6df42022-11-09 11:13:29 +000032}
33
34/*
AlexeiFedorov97844202023-04-27 15:17:35 +010035 * Function to continue with the token write operation
Soby Mathewb4c6df42022-11-09 11:13:29 +000036 */
37static void attest_token_continue_write_state(struct rec *rec,
AlexeiFedorov97844202023-04-27 15:17:35 +010038 struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +000039{
Soby Mathewb4c6df42022-11-09 11:13:29 +000040 struct granule *gr;
AlexeiFedorovea68b552023-10-03 11:11:47 +010041 uintptr_t realm_att_token;
Soby Mathewb4c6df42022-11-09 11:13:29 +000042 unsigned long realm_att_token_ipa = rec->regs[1];
AlexeiFedorovea68b552023-10-03 11:11:47 +010043 unsigned long offset = rec->regs[2];
44 unsigned long size = rec->regs[3];
Soby Mathewb4c6df42022-11-09 11:13:29 +000045 enum s2_walk_status walk_status;
46 struct s2_walk_result walk_res = { 0UL };
AlexeiFedorovea68b552023-10-03 11:11:47 +010047 size_t attest_token_len, length;
AlexeiFedorovec35c542023-04-27 17:52:02 +010048 struct rec_attest_data *attest_data = rec->aux_data.attest_data;
AlexeiFedorovea68b552023-10-03 11:11:47 +010049 uintptr_t cca_token_buf = rec->aux_data.cca_token_buf;
Juan Pablo Conde79ca2562024-08-12 17:05:41 -050050 enum attest_token_err_t ret;
Soby Mathewb4c6df42022-11-09 11:13:29 +000051
52 /*
Soby Mathewb4c6df42022-11-09 11:13:29 +000053 * Translate realm granule IPA to PA. If returns with
54 * WALK_SUCCESS then the last level page table (llt),
55 * which holds the realm_att_token_buf mapping, is locked.
56 */
AlexeiFedorovd2e1bbd2023-04-18 15:18:39 +010057 walk_status = realm_ipa_to_pa(rec, realm_att_token_ipa, &walk_res);
Soby Mathewb4c6df42022-11-09 11:13:29 +000058
59 /* Walk parameter validity was checked by RSI_ATTESTATION_TOKEN_INIT */
60 assert(walk_status != WALK_INVALID_PARAMS);
61
62 if (walk_status == WALK_FAIL) {
AlexeiFedorovd2e1bbd2023-04-18 15:18:39 +010063 if (walk_res.ripas_val == RIPAS_EMPTY) {
Soby Mathewb4c6df42022-11-09 11:13:29 +000064 res->smc_res.x[0] = RSI_ERROR_INPUT;
65 } else {
66 /*
AlexeiFedorov97844202023-04-27 15:17:35 +010067 * Translation failed, IPA is not mapped.
68 * Return to NS host to fix the issue.
Soby Mathewb4c6df42022-11-09 11:13:29 +000069 */
AlexeiFedorov97844202023-04-27 15:17:35 +010070 res->action = STAGE_2_TRANSLATION_FAULT;
71 res->rtt_level = walk_res.rtt_level;
Soby Mathewb4c6df42022-11-09 11:13:29 +000072 }
73 return;
74 }
75
Soby Mathew19eb4332023-11-20 14:03:23 +000076 /* If size of buffer is 0, then return early. */
AlexeiFedorovea68b552023-10-03 11:11:47 +010077 if (size == 0UL) {
Soby Mathew19eb4332023-11-20 14:03:23 +000078 res->smc_res.x[0] = RSI_INCOMPLETE;
79 goto out_unlock;
AlexeiFedorovea68b552023-10-03 11:11:47 +010080 }
81
Soby Mathewb4c6df42022-11-09 11:13:29 +000082 /* Map realm data granule to RMM address space */
83 gr = find_granule(walk_res.pa);
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +000084 realm_att_token = (uintptr_t)buffer_granule_map(gr, SLOT_RSI_CALL);
AlexeiFedorovea68b552023-10-03 11:11:47 +010085 assert(realm_att_token != 0UL);
Soby Mathewb4c6df42022-11-09 11:13:29 +000086
Mate Toth-Pal4feff402024-08-30 10:53:32 +020087 if (attest_data->rmm_cca_token_copied_len == 0UL) {
Juan Pablo Conde79ca2562024-08-12 17:05:41 -050088 ret = attest_cca_token_create(
89 &attest_data->token_sign_ctx,
90 (void *)cca_token_buf,
91 REC_ATTEST_TOKEN_BUF_SIZE,
92 &attest_data->rmm_realm_token_buf,
93 attest_data->rmm_realm_token_len,
94 &attest_token_len);
Soby Mathewb4c6df42022-11-09 11:13:29 +000095
Juan Pablo Conde79ca2562024-08-12 17:05:41 -050096 if (ret != ATTEST_TOKEN_ERR_SUCCESS) {
AlexeiFedorovea68b552023-10-03 11:11:47 +010097 res->smc_res.x[0] = RSI_ERROR_INPUT;
AlexeiFedorovea68b552023-10-03 11:11:47 +010098 goto out_unmap;
99 }
100
Mate Toth-Pal4feff402024-08-30 10:53:32 +0200101 attest_data->rmm_cca_token_len = attest_token_len;
AlexeiFedorovea68b552023-10-03 11:11:47 +0100102 } else {
Mate Toth-Pal4feff402024-08-30 10:53:32 +0200103 attest_token_len = attest_data->rmm_cca_token_len;
AlexeiFedorovea68b552023-10-03 11:11:47 +0100104 }
105
106 length = (size < attest_token_len) ? size : attest_token_len;
107
108 /* Copy attestation token */
109 (void)memcpy((void *)(realm_att_token + offset),
110 (void *)(cca_token_buf +
Mate Toth-Pal4feff402024-08-30 10:53:32 +0200111 attest_data->rmm_cca_token_copied_len),
AlexeiFedorovea68b552023-10-03 11:11:47 +0100112 length);
113
114 attest_token_len -= length;
115
116 if (attest_token_len != 0UL) {
Mate Toth-Pal4feff402024-08-30 10:53:32 +0200117 attest_data->rmm_cca_token_len = attest_token_len;
118 attest_data->rmm_cca_token_copied_len += length;
AlexeiFedorovea68b552023-10-03 11:11:47 +0100119
120 res->smc_res.x[0] = RSI_INCOMPLETE;
121 } else {
AlexeiFedorovea68b552023-10-03 11:11:47 +0100122 res->smc_res.x[0] = RSI_SUCCESS;
Soby Mathewda53d222024-08-29 16:37:26 +0100123 /* Token creation is complete, reset realm token length */
124 attest_data->rmm_realm_token_len = 0;
AlexeiFedorovea68b552023-10-03 11:11:47 +0100125 }
126
127 res->smc_res.x[1] = length;
128
129out_unmap:
Soby Mathewb4c6df42022-11-09 11:13:29 +0000130 /* Unmap realm granule */
AlexeiFedorovea68b552023-10-03 11:11:47 +0100131 buffer_unmap((void *)realm_att_token);
Soby Mathew19eb4332023-11-20 14:03:23 +0000132out_unlock:
Soby Mathewb4c6df42022-11-09 11:13:29 +0000133 /* Unlock last level page table (walk_res.g_llt) */
134 granule_unlock(walk_res.llt);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000135}
136
AlexeiFedorov97844202023-04-27 15:17:35 +0100137void handle_rsi_attest_token_init(struct rec *rec, struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000138{
AlexeiFedorovea68b552023-10-03 11:11:47 +0100139 struct rd *rd;
AlexeiFedorovec35c542023-04-27 17:52:02 +0100140 struct rec_attest_data *attest_data;
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200141 void *rpv_ptr;
142 size_t rpv_len;
Soby Mathewfdbc4b42024-11-07 06:54:42 +0000143 enum attest_token_err_t ret;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000144 int att_ret;
145
146 assert(rec != NULL);
147
AlexeiFedorovec35c542023-04-27 17:52:02 +0100148 attest_data = rec->aux_data.attest_data;
AlexeiFedorov97844202023-04-27 15:17:35 +0100149 res->action = UPDATE_REC_RETURN_TO_REALM;
150
Soby Mathew376ffa42024-10-04 14:01:59 +0100151 /* Initialize length fields in attestation data */
152 attest_data->rmm_realm_token_len = 0;
153 attest_data->rmm_cca_token_copied_len = 0;
154 attest_data->rmm_cca_token_len = 0;
155
Soby Mathewb4c6df42022-11-09 11:13:29 +0000156 /*
157 * Calling RSI_ATTESTATION_TOKEN_INIT any time aborts any ongoing
158 * operation.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000159 */
Soby Mathewfdbc4b42024-11-07 06:54:42 +0000160 ret = attest_token_ctx_init(&attest_data->token_sign_ctx,
Soby Mathewf3622132024-07-19 07:31:40 +0100161 rec->aux_data.attest_heap_buf,
Soby Mathew9d2bd2b2024-10-03 12:29:10 +0100162 REC_HEAP_SIZE,
163 granule_addr(rec->g_rec));
Soby Mathewfdbc4b42024-11-07 06:54:42 +0000164 if (ret != ATTEST_TOKEN_ERR_SUCCESS) {
Juan Pablo Condead871f92024-09-17 15:44:45 -0500165 ERROR("Failed to initialize attestation token context.\n");
166 res->smc_res.x[0] = RSI_ERROR_UNKNOWN;
167 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000168 }
169
Soby Mathewb4c6df42022-11-09 11:13:29 +0000170 /*
171 * rd lock is acquired so that measurement cannot be updated
172 * simultaneously by another rec
173 */
174 granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD);
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000175 rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100176 assert(rd != NULL);
177
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200178 get_rpv(rd, &rpv_ptr, &rpv_len);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000179 att_ret = attest_realm_token_create(rd->algorithm, rd->measurement,
180 MEASUREMENT_SLOT_NR,
Mate Toth-Pal071aa562023-07-04 09:09:26 +0200181 rpv_ptr,
182 rpv_len,
Mate Toth-Pal4feff402024-08-30 10:53:32 +0200183 (const void *)&rec->regs[1],
184 ATTEST_CHALLENGE_SIZE,
AlexeiFedorovec35c542023-04-27 17:52:02 +0100185 &attest_data->token_sign_ctx,
186 attest_data->rmm_realm_token_buf,
187 sizeof(attest_data->rmm_realm_token_buf));
AlexeiFedorovea68b552023-10-03 11:11:47 +0100188 buffer_unmap(rd);
189 granule_unlock(rec->realm_info.g_rd);
190
Soby Mathewb4c6df42022-11-09 11:13:29 +0000191 if (att_ret != 0) {
Juan Pablo Condead871f92024-09-17 15:44:45 -0500192 ERROR("Realm token creation failed.\n");
193 res->smc_res.x[0] = RSI_ERROR_UNKNOWN;
194 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000195 }
196
AlexeiFedorovea68b552023-10-03 11:11:47 +0100197 res->smc_res.x[0] = RSI_SUCCESS;
AlexeiFedorov755a70b2023-10-12 12:31:45 +0100198 res->smc_res.x[1] = REC_ATTEST_TOKEN_BUF_SIZE;
AlexeiFedorov97844202023-04-27 15:17:35 +0100199}
200
201/*
202 * Return 'false' if no IRQ is pending,
203 * return 'true' if there is an IRQ pending, and need to return to Host.
204 */
205static bool check_pending_irq(void)
206{
207 return (read_isr_el1() != 0UL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000208}
209
Soby Mathew9d2bd2b2024-10-03 12:29:10 +0100210static __unused int write_response_to_rec(struct rec *curr_rec,
211 uintptr_t resp_granule)
212{
213 struct granule *rec_granule = NULL;
214 struct rec *rec = NULL;
215 void *rec_aux = NULL;
216 struct token_sign_cntxt *ctx = NULL;
217 struct rec_attest_data *attest_data = NULL;
218 bool unmap_unlock_needed = false;
219 int ret = 0;
220
221 /*
222 * Check if the granule is the same as the current REC. If it is, the current
223 * code path is guaranteed to have a reference on the REC and the REC
224 * cannot be deleted. It also means that the REC is mapped at the usual
225 * SLOT_REC, so we can avoid locking and mapping the REC and the AUX
226 * granules.
227 */
228 if (resp_granule != granule_addr(curr_rec->g_rec)) {
229
230 rec_granule = find_lock_granule(
231 resp_granule, GRANULE_STATE_REC);
AlexeiFedorovc1795042024-10-07 11:54:39 +0100232 if (rec_granule == NULL) {
Soby Mathew9d2bd2b2024-10-03 12:29:10 +0100233 /*
234 * REC must have been destroyed, drop the response.
235 */
236 VERBOSE("REC granule %lx not found\n", resp_granule);
237 return 0;
238 }
239
240 rec = buffer_granule_map(rec_granule, SLOT_EL3_TOKEN_SIGN_REC);
241 assert(rec != NULL);
242
243 /*
244 * Map auxiliary granules. Note that the aux granules are mapped at a
245 * different high VA than during realm creation since this function
246 * may be executing with another rec mapped at the same high VA.
247 * Any accesses to aux granules via initialized pointers such as
248 * attest_data, need to be recaluclated at the new high VA.
249 */
Arunachalam Ganapathy78417802024-05-17 15:01:50 +0100250 rec_aux = buffer_rec_aux_granules_map_el3_token_sign_slot(
251 rec->g_aux,
252 rec->num_rec_aux);
Soby Mathew9d2bd2b2024-10-03 12:29:10 +0100253 assert(rec_aux != NULL);
254
255 unmap_unlock_needed = true;
256 attest_data = (struct rec_attest_data *)((uintptr_t)rec_aux + REC_HEAP_SIZE +
257 REC_PMU_SIZE + REC_SIMD_SIZE);
258 } else {
259 rec = curr_rec;
260 attest_data = rec->aux_data.attest_data;
261 }
262 ctx = &attest_data->token_sign_ctx;
263
264 if (attest_el3_token_write_response_to_ctx(ctx, resp_granule) != 0) {
265 ret = -EPERM;
266 }
267
268 if (unmap_unlock_needed) {
Arunachalam Ganapathy78417802024-05-17 15:01:50 +0100269 buffer_rec_aux_unmap(rec_aux, rec->num_rec_aux);
Soby Mathew9d2bd2b2024-10-03 12:29:10 +0100270 buffer_unmap(rec);
271 granule_unlock(rec_granule);
272 }
273
274 return ret;
275}
276
Soby Mathewb4c6df42022-11-09 11:13:29 +0000277void handle_rsi_attest_token_continue(struct rec *rec,
AlexeiFedorov97844202023-04-27 15:17:35 +0100278 struct rmi_rec_exit *rec_exit,
279 struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000280{
AlexeiFedorovec35c542023-04-27 17:52:02 +0100281 struct rec_attest_data *attest_data;
AlexeiFedorovea68b552023-10-03 11:11:47 +0100282 unsigned long realm_buf_ipa, offset, size;
AlexeiFedorovec35c542023-04-27 17:52:02 +0100283
Soby Mathewb4c6df42022-11-09 11:13:29 +0000284 assert(rec != NULL);
AlexeiFedorov97844202023-04-27 15:17:35 +0100285 assert(rec_exit != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000286
AlexeiFedorovec35c542023-04-27 17:52:02 +0100287 attest_data = rec->aux_data.attest_data;
AlexeiFedorov97844202023-04-27 15:17:35 +0100288 res->action = UPDATE_REC_RETURN_TO_REALM;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000289
AlexeiFedorovea68b552023-10-03 11:11:47 +0100290 realm_buf_ipa = rec->regs[1];
291 offset = rec->regs[2];
292 size = rec->regs[3];
293
294 if (!GRANULE_ALIGNED(realm_buf_ipa) ||
295 (offset >= GRANULE_SIZE) ||
296 ((offset + size) > GRANULE_SIZE) ||
297 ((offset + size) < offset)) {
Soby Mathewb4c6df42022-11-09 11:13:29 +0000298 res->smc_res.x[0] = RSI_ERROR_INPUT;
299 return;
300 }
301
AlexeiFedorovea68b552023-10-03 11:11:47 +0100302 if (!addr_in_rec_par(rec, realm_buf_ipa)) {
303 res->smc_res.x[0] = RSI_ERROR_INPUT;
304 return;
305 }
AlexeiFedorov97844202023-04-27 15:17:35 +0100306
Soby Mathewf3622132024-07-19 07:31:40 +0100307 /* Sign the token */
308 while (attest_data->rmm_realm_token_len == 0U) {
309 enum attest_token_err_t ret;
AlexeiFedorovea68b552023-10-03 11:11:47 +0100310
Soby Mathewf3622132024-07-19 07:31:40 +0100311 ret = attest_realm_token_sign(&(attest_data->token_sign_ctx),
312 &(attest_data->rmm_realm_token_len));
313
314 if (ret == ATTEST_TOKEN_ERR_INVALID_STATE) {
315 /*
316 * Before this call the initial attestation token call
317 * (SMC_RSI_ATTEST_TOKEN_INIT) must have been executed
318 * successfully.
319 */
320 res->smc_res.x[0] = RSI_ERROR_STATE;
321 return;
322 } else if ((ret != ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS) &&
323 (ret != ATTEST_TOKEN_ERR_SUCCESS)) {
324 /* Accessible only in case of failure during token signing */
Juan Pablo Condead871f92024-09-17 15:44:45 -0500325 ERROR("Realm token signing failed.\n");
326 res->smc_res.x[0] = RSI_ERROR_UNKNOWN;
327 return;
Soby Mathewf3622132024-07-19 07:31:40 +0100328 }
329
330 res->smc_res.x[0] = RSI_INCOMPLETE;
331
332 /*
333 * Return to RSI handler function after each iteration
334 * to check is there anything else to do (pending IRQ)
335 * or next signing iteration can be executed.
336 */
AlexeiFedorovea68b552023-10-03 11:11:47 +0100337 if (check_pending_irq()) {
338 res->action = UPDATE_REC_EXIT_TO_HOST;
339 rec_exit->exit_reason = RMI_EXIT_IRQ;
340 return;
AlexeiFedorov97844202023-04-27 15:17:35 +0100341 }
Soby Mathew9d2bd2b2024-10-03 12:29:10 +0100342
343#if ATTEST_EL3_TOKEN_SIGN
344 /*
345 * Pull response from EL3, find the corresponding rec granule
346 * and attestation context, and have the attestation library
347 * write the response to the context.
348 */
349 uintptr_t granule = 0UL;
350 int el3_token_sign_ret;
351
352 el3_token_sign_ret = attest_el3_token_sign_pull_response_from_el3(&granule);
353 if (el3_token_sign_ret == -EAGAIN) {
354 continue;
355 }
356
357 if (el3_token_sign_ret != 0) {
358 ERROR("Failed to pull response from EL3: %d\n", el3_token_sign_ret);
359 res->smc_res.x[0] = RSI_ERROR_UNKNOWN;
360 return;
361 }
362
363 el3_token_sign_ret = write_response_to_rec(rec, granule);
364 if (el3_token_sign_ret != 0) {
365 ERROR("Failed to write response to REC: %d\n", el3_token_sign_ret);
366 res->smc_res.x[0] = RSI_ERROR_UNKNOWN;
367 return;
368 }
369#endif
370 /*
371 * If there are no interrupts pending, then continue to pull
372 * requests from EL3 until this RECs request is done.
373 */
Soby Mathewb4c6df42022-11-09 11:13:29 +0000374 }
AlexeiFedorovea68b552023-10-03 11:11:47 +0100375
AlexeiFedorovea68b552023-10-03 11:11:47 +0100376 attest_token_continue_write_state(rec, res);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000377}
378
AlexeiFedorov97844202023-04-27 15:17:35 +0100379void handle_rsi_measurement_extend(struct rec *rec, struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000380{
381 struct granule *g_rd;
382 struct rd *rd;
383 unsigned long index;
384 unsigned long rd_addr;
385 size_t size;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000386 void *extend_measurement;
387 unsigned char *current_measurement;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000388
AlexeiFedorov97844202023-04-27 15:17:35 +0100389 assert(rec != NULL);
390
391 res->action = UPDATE_REC_RETURN_TO_REALM;
392
Soby Mathewb4c6df42022-11-09 11:13:29 +0000393 /*
394 * rd lock is acquired so that measurement cannot be updated
395 * simultaneously by another rec
396 */
397 rd_addr = granule_addr(rec->realm_info.g_rd);
398 g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD);
399
400 assert(g_rd != NULL);
401
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000402 rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100403 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000404
405 /*
406 * X1: index
407 * X2: size
408 * X3-X10: measurement value
409 */
410 index = rec->regs[1];
411
412 if ((index == RIM_MEASUREMENT_SLOT) ||
413 (index >= MEASUREMENT_SLOT_NR)) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100414 res->smc_res.x[0] = RSI_ERROR_INPUT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000415 goto out_unmap_rd;
416 }
417
418 size = rec->regs[2];
419
420 if (size > MAX_EXTENDED_SIZE) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100421 res->smc_res.x[0] = RSI_ERROR_INPUT;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000422 goto out_unmap_rd;
423 }
424
425 extend_measurement = &rec->regs[3];
426 current_measurement = rd->measurement[index];
427
428 measurement_extend(rd->algorithm,
429 current_measurement,
430 extend_measurement,
431 size,
Juan Pablo Conded3084982024-09-18 17:05:49 -0500432 current_measurement,
433 MAX_MEASUREMENT_SIZE);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000434
AlexeiFedorov97844202023-04-27 15:17:35 +0100435 res->smc_res.x[0] = RSI_SUCCESS;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000436
437out_unmap_rd:
438 buffer_unmap(rd);
439 granule_unlock(g_rd);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000440}
441
AlexeiFedorov97844202023-04-27 15:17:35 +0100442void handle_rsi_measurement_read(struct rec *rec, struct rsi_result *res)
Soby Mathewb4c6df42022-11-09 11:13:29 +0000443{
444 struct rd *rd;
445 unsigned long idx;
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100446 unsigned int i, cnt;
Mate Toth-Pal59b52d02023-08-18 14:14:19 +0200447 unsigned long *measurement_value_part;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000448
449 assert(rec != NULL);
450
AlexeiFedorov97844202023-04-27 15:17:35 +0100451 res->action = UPDATE_REC_RETURN_TO_REALM;
452
Soby Mathewb4c6df42022-11-09 11:13:29 +0000453 /* X1: Index */
454 idx = rec->regs[1];
455
456 if (idx >= MEASUREMENT_SLOT_NR) {
AlexeiFedorov97844202023-04-27 15:17:35 +0100457 res->smc_res.x[0] = RSI_ERROR_INPUT;
458 return;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000459 }
460
461 /*
462 * rd lock is acquired so that measurement cannot be updated
463 * simultaneously by another rec
464 */
465 granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD);
Javier Almansa Sobrino2f717dd2024-02-12 20:49:46 +0000466 rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD);
AlexeiFedorov9a9062c2023-08-21 15:41:48 +0100467 assert(rd != NULL);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000468
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100469 /* Number of 8-bytes words in measurement */
AlexeiFedorov4faab852023-08-30 15:06:49 +0100470 cnt = (unsigned int)(measurement_get_size(rd->algorithm) /
471 sizeof(unsigned long));
Soby Mathewb4c6df42022-11-09 11:13:29 +0000472
AlexeiFedorovea68b552023-10-03 11:11:47 +0100473 assert(cnt >= (SMC_RESULT_REGS - 1U));
Soby Mathew1ceb0082024-10-10 17:22:38 +0100474 assert(cnt < ARRAY_SIZE(rec->regs));
Mate Toth-Pal59b52d02023-08-18 14:14:19 +0200475
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100476 /* Copy the part of the measurement to res->smc_res.x[] */
AlexeiFedorovea68b552023-10-03 11:11:47 +0100477 for (i = 0U; i < (SMC_RESULT_REGS - 1U); i++) {
Mate Toth-Pal59b52d02023-08-18 14:14:19 +0200478 measurement_value_part = (unsigned long *)
479 &(rd->measurement[idx][i * sizeof(unsigned long)]);
480 res->smc_res.x[i + 1U] = *measurement_value_part;
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100481 }
Soby Mathewb4c6df42022-11-09 11:13:29 +0000482
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100483 /* Copy the rest of the measurement to the rec->regs[] */
484 for (; i < cnt; i++) {
Mate Toth-Pal59b52d02023-08-18 14:14:19 +0200485 measurement_value_part = (unsigned long *)
486 &(rd->measurement[idx][i * sizeof(unsigned long)]);
487 rec->regs[i + 1U] = *measurement_value_part;
AlexeiFedorovefe2aec2023-06-08 16:17:00 +0100488 }
489
490 /* Zero-initialize unused area */
491 for (; i < MAX_MEASUREMENT_WORDS; i++) {
492 rec->regs[i + 1U] = 0UL;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000493 }
494
495 buffer_unmap(rd);
496 granule_unlock(rec->realm_info.g_rd);
497
AlexeiFedorov97844202023-04-27 15:17:35 +0100498 res->smc_res.x[0] = RSI_SUCCESS;
Soby Mathewb4c6df42022-11-09 11:13:29 +0000499}