Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | */ |
| 5 | |
| 6 | #include <attestation.h> |
Javier Almansa Sobrino | 2f717dd | 2024-02-12 20:49:46 +0000 | [diff] [blame] | 7 | #include <buffer.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 8 | #include <debug.h> |
| 9 | #include <granule.h> |
| 10 | #include <measurement.h> |
| 11 | #include <realm.h> |
AlexeiFedorov | 5b186ad | 2023-04-26 14:43:18 +0100 | [diff] [blame] | 12 | #include <rsi-handler.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 13 | #include <smc-rsi.h> |
| 14 | #include <smc.h> |
| 15 | #include <string.h> |
| 16 | #include <utils_def.h> |
| 17 | |
AlexeiFedorov | efe2aec | 2023-06-08 16:17:00 +0100 | [diff] [blame] | 18 | #define MAX_EXTENDED_SIZE (64U) |
AlexeiFedorov | 7b3c304 | 2023-06-28 15:41:11 +0100 | [diff] [blame] | 19 | #define MAX_MEASUREMENT_WORDS (MAX_MEASUREMENT_SIZE / sizeof(unsigned long)) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 20 | /* |
| 21 | * Return the Realm Personalization Value. |
| 22 | * |
| 23 | * Arguments: |
| 24 | * rd - The Realm descriptor. |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 25 | * claim_ptr - The start address of the Realm Personalization Value claim |
| 26 | * claim_len - The length of the Realm Personalization Value claim |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 27 | */ |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 28 | static void get_rpv(struct rd *rd, void **claim_ptr, size_t *claim_len) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 29 | { |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 30 | *claim_ptr = (uint8_t *)&(rd->rpv[0]); |
| 31 | *claim_len = RPV_SIZE; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 35 | * Function to continue with the token write operation |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 36 | */ |
| 37 | static void attest_token_continue_write_state(struct rec *rec, |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 38 | struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 39 | { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 40 | struct granule *gr; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 41 | uintptr_t realm_att_token; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 42 | unsigned long realm_att_token_ipa = rec->regs[1]; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 43 | unsigned long offset = rec->regs[2]; |
| 44 | unsigned long size = rec->regs[3]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 45 | enum s2_walk_status walk_status; |
| 46 | struct s2_walk_result walk_res = { 0UL }; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 47 | size_t attest_token_len, length; |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 48 | struct rec_attest_data *attest_data = rec->aux_data.attest_data; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 49 | uintptr_t cca_token_buf = rec->aux_data.cca_token_buf; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 50 | |
| 51 | /* |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 52 | * Translate realm granule IPA to PA. If returns with |
| 53 | * WALK_SUCCESS then the last level page table (llt), |
| 54 | * which holds the realm_att_token_buf mapping, is locked. |
| 55 | */ |
AlexeiFedorov | d2e1bbd | 2023-04-18 15:18:39 +0100 | [diff] [blame] | 56 | walk_status = realm_ipa_to_pa(rec, realm_att_token_ipa, &walk_res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 57 | |
| 58 | /* Walk parameter validity was checked by RSI_ATTESTATION_TOKEN_INIT */ |
| 59 | assert(walk_status != WALK_INVALID_PARAMS); |
| 60 | |
| 61 | if (walk_status == WALK_FAIL) { |
AlexeiFedorov | d2e1bbd | 2023-04-18 15:18:39 +0100 | [diff] [blame] | 62 | if (walk_res.ripas_val == RIPAS_EMPTY) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 63 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
| 64 | } else { |
| 65 | /* |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 66 | * Translation failed, IPA is not mapped. |
| 67 | * Return to NS host to fix the issue. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 68 | */ |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 69 | res->action = STAGE_2_TRANSLATION_FAULT; |
| 70 | res->rtt_level = walk_res.rtt_level; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 71 | } |
| 72 | return; |
| 73 | } |
| 74 | |
Soby Mathew | 19eb433 | 2023-11-20 14:03:23 +0000 | [diff] [blame] | 75 | /* If size of buffer is 0, then return early. */ |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 76 | if (size == 0UL) { |
Soby Mathew | 19eb433 | 2023-11-20 14:03:23 +0000 | [diff] [blame] | 77 | res->smc_res.x[0] = RSI_INCOMPLETE; |
| 78 | goto out_unlock; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 81 | /* Map realm data granule to RMM address space */ |
| 82 | gr = find_granule(walk_res.pa); |
Javier Almansa Sobrino | 2f717dd | 2024-02-12 20:49:46 +0000 | [diff] [blame] | 83 | realm_att_token = (uintptr_t)buffer_granule_map(gr, SLOT_RSI_CALL); |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 84 | assert(realm_att_token != 0UL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 85 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 86 | if (attest_data->token_sign_ctx.copied_len == 0UL) { |
| 87 | attest_token_len = attest_cca_token_create( |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 88 | &attest_data->token_sign_ctx, |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 89 | (void *)cca_token_buf, |
| 90 | REC_ATTEST_TOKEN_BUF_SIZE, |
| 91 | &attest_data->rmm_realm_token_buf, |
| 92 | attest_data->rmm_realm_token_len); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 93 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 94 | if (attest_token_len == 0UL) { |
| 95 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 96 | goto out_unmap; |
| 97 | } |
| 98 | |
| 99 | attest_data->token_sign_ctx.cca_token_len = attest_token_len; |
| 100 | } else { |
| 101 | attest_token_len = attest_data->token_sign_ctx.cca_token_len; |
| 102 | } |
| 103 | |
| 104 | length = (size < attest_token_len) ? size : attest_token_len; |
| 105 | |
| 106 | /* Copy attestation token */ |
| 107 | (void)memcpy((void *)(realm_att_token + offset), |
| 108 | (void *)(cca_token_buf + |
| 109 | attest_data->token_sign_ctx.copied_len), |
| 110 | length); |
| 111 | |
| 112 | attest_token_len -= length; |
| 113 | |
| 114 | if (attest_token_len != 0UL) { |
| 115 | attest_data->token_sign_ctx.cca_token_len = attest_token_len; |
| 116 | attest_data->token_sign_ctx.copied_len += length; |
| 117 | |
| 118 | res->smc_res.x[0] = RSI_INCOMPLETE; |
| 119 | } else { |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 120 | res->smc_res.x[0] = RSI_SUCCESS; |
Soby Mathew | da53d22 | 2024-08-29 16:37:26 +0100 | [diff] [blame^] | 121 | /* Token creation is complete, reset realm token length */ |
| 122 | attest_data->rmm_realm_token_len = 0; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | res->smc_res.x[1] = length; |
| 126 | |
| 127 | out_unmap: |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 128 | /* Unmap realm granule */ |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 129 | buffer_unmap((void *)realm_att_token); |
Soby Mathew | 19eb433 | 2023-11-20 14:03:23 +0000 | [diff] [blame] | 130 | out_unlock: |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 131 | /* Unlock last level page table (walk_res.g_llt) */ |
| 132 | granule_unlock(walk_res.llt); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 133 | } |
| 134 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 135 | void handle_rsi_attest_token_init(struct rec *rec, struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 136 | { |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 137 | struct rd *rd; |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 138 | struct rec_attest_data *attest_data; |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 139 | void *rpv_ptr; |
| 140 | size_t rpv_len; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 141 | int att_ret; |
| 142 | |
| 143 | assert(rec != NULL); |
| 144 | |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 145 | attest_data = rec->aux_data.attest_data; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 146 | res->action = UPDATE_REC_RETURN_TO_REALM; |
| 147 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 148 | /* |
| 149 | * Calling RSI_ATTESTATION_TOKEN_INIT any time aborts any ongoing |
| 150 | * operation. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 151 | */ |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 152 | att_ret = attest_token_ctx_init(&attest_data->token_sign_ctx, |
| 153 | rec->aux_data.attest_heap_buf, |
| 154 | REC_HEAP_SIZE); |
| 155 | if (att_ret != 0) { |
| 156 | /* There is no provision for this failure so panic */ |
| 157 | panic(); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 160 | /* Initialize the final token len */ |
| 161 | attest_data->rmm_realm_token_len = 0; |
AlexeiFedorov | 2dcd79f | 2023-10-17 10:04:11 +0100 | [diff] [blame] | 162 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 163 | /* |
| 164 | * rd lock is acquired so that measurement cannot be updated |
| 165 | * simultaneously by another rec |
| 166 | */ |
| 167 | granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD); |
Javier Almansa Sobrino | 2f717dd | 2024-02-12 20:49:46 +0000 | [diff] [blame] | 168 | rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD); |
AlexeiFedorov | 9a9062c | 2023-08-21 15:41:48 +0100 | [diff] [blame] | 169 | assert(rd != NULL); |
| 170 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 171 | /* Save challenge value in the context */ |
| 172 | (void)memcpy((void *)attest_data->token_sign_ctx.challenge, |
| 173 | (const void *)&rec->regs[1], |
| 174 | ATTEST_CHALLENGE_SIZE); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 175 | |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 176 | get_rpv(rd, &rpv_ptr, &rpv_len); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 177 | att_ret = attest_realm_token_create(rd->algorithm, rd->measurement, |
| 178 | MEASUREMENT_SLOT_NR, |
Mate Toth-Pal | 071aa56 | 2023-07-04 09:09:26 +0200 | [diff] [blame] | 179 | rpv_ptr, |
| 180 | rpv_len, |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 181 | &attest_data->token_sign_ctx, |
| 182 | attest_data->rmm_realm_token_buf, |
| 183 | sizeof(attest_data->rmm_realm_token_buf)); |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 184 | buffer_unmap(rd); |
| 185 | granule_unlock(rec->realm_info.g_rd); |
| 186 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 187 | if (att_ret != 0) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 188 | ERROR("FATAL_ERROR: Realm token creation failed\n"); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 189 | panic(); |
| 190 | } |
| 191 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 192 | res->smc_res.x[0] = RSI_SUCCESS; |
AlexeiFedorov | 755a70b | 2023-10-12 12:31:45 +0100 | [diff] [blame] | 193 | res->smc_res.x[1] = REC_ATTEST_TOKEN_BUF_SIZE; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Return 'false' if no IRQ is pending, |
| 198 | * return 'true' if there is an IRQ pending, and need to return to Host. |
| 199 | */ |
| 200 | static bool check_pending_irq(void) |
| 201 | { |
| 202 | return (read_isr_el1() != 0UL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 205 | void handle_rsi_attest_token_continue(struct rec *rec, |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 206 | struct rmi_rec_exit *rec_exit, |
| 207 | struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 208 | { |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 209 | struct rec_attest_data *attest_data; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 210 | unsigned long realm_buf_ipa, offset, size; |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 211 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 212 | assert(rec != NULL); |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 213 | assert(rec_exit != NULL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 214 | |
AlexeiFedorov | ec35c54 | 2023-04-27 17:52:02 +0100 | [diff] [blame] | 215 | attest_data = rec->aux_data.attest_data; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 216 | res->action = UPDATE_REC_RETURN_TO_REALM; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 217 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 218 | realm_buf_ipa = rec->regs[1]; |
| 219 | offset = rec->regs[2]; |
| 220 | size = rec->regs[3]; |
| 221 | |
| 222 | if (!GRANULE_ALIGNED(realm_buf_ipa) || |
| 223 | (offset >= GRANULE_SIZE) || |
| 224 | ((offset + size) > GRANULE_SIZE) || |
| 225 | ((offset + size) < offset)) { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 226 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
| 227 | return; |
| 228 | } |
| 229 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 230 | if (!addr_in_rec_par(rec, realm_buf_ipa)) { |
| 231 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
| 232 | return; |
| 233 | } |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 234 | |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 235 | /* Sign the token */ |
| 236 | while (attest_data->rmm_realm_token_len == 0U) { |
| 237 | enum attest_token_err_t ret; |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 238 | |
Soby Mathew | f362213 | 2024-07-19 07:31:40 +0100 | [diff] [blame] | 239 | ret = attest_realm_token_sign(&(attest_data->token_sign_ctx), |
| 240 | &(attest_data->rmm_realm_token_len)); |
| 241 | |
| 242 | if (ret == ATTEST_TOKEN_ERR_INVALID_STATE) { |
| 243 | /* |
| 244 | * Before this call the initial attestation token call |
| 245 | * (SMC_RSI_ATTEST_TOKEN_INIT) must have been executed |
| 246 | * successfully. |
| 247 | */ |
| 248 | res->smc_res.x[0] = RSI_ERROR_STATE; |
| 249 | return; |
| 250 | } else if ((ret != ATTEST_TOKEN_ERR_COSE_SIGN_IN_PROGRESS) && |
| 251 | (ret != ATTEST_TOKEN_ERR_SUCCESS)) { |
| 252 | /* Accessible only in case of failure during token signing */ |
| 253 | ERROR("FATAL_ERROR: Realm token sign failed\n"); |
| 254 | panic(); |
| 255 | } |
| 256 | |
| 257 | res->smc_res.x[0] = RSI_INCOMPLETE; |
| 258 | |
| 259 | /* |
| 260 | * Return to RSI handler function after each iteration |
| 261 | * to check is there anything else to do (pending IRQ) |
| 262 | * or next signing iteration can be executed. |
| 263 | */ |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 264 | if (check_pending_irq()) { |
| 265 | res->action = UPDATE_REC_EXIT_TO_HOST; |
| 266 | rec_exit->exit_reason = RMI_EXIT_IRQ; |
| 267 | return; |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 268 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 269 | } |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 270 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 271 | attest_token_continue_write_state(rec, res); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 272 | } |
| 273 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 274 | void handle_rsi_measurement_extend(struct rec *rec, struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 275 | { |
| 276 | struct granule *g_rd; |
| 277 | struct rd *rd; |
| 278 | unsigned long index; |
| 279 | unsigned long rd_addr; |
| 280 | size_t size; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 281 | void *extend_measurement; |
| 282 | unsigned char *current_measurement; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 283 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 284 | assert(rec != NULL); |
| 285 | |
| 286 | res->action = UPDATE_REC_RETURN_TO_REALM; |
| 287 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 288 | /* |
| 289 | * rd lock is acquired so that measurement cannot be updated |
| 290 | * simultaneously by another rec |
| 291 | */ |
| 292 | rd_addr = granule_addr(rec->realm_info.g_rd); |
| 293 | g_rd = find_lock_granule(rd_addr, GRANULE_STATE_RD); |
| 294 | |
| 295 | assert(g_rd != NULL); |
| 296 | |
Javier Almansa Sobrino | 2f717dd | 2024-02-12 20:49:46 +0000 | [diff] [blame] | 297 | rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD); |
AlexeiFedorov | 9a9062c | 2023-08-21 15:41:48 +0100 | [diff] [blame] | 298 | assert(rd != NULL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 299 | |
| 300 | /* |
| 301 | * X1: index |
| 302 | * X2: size |
| 303 | * X3-X10: measurement value |
| 304 | */ |
| 305 | index = rec->regs[1]; |
| 306 | |
| 307 | if ((index == RIM_MEASUREMENT_SLOT) || |
| 308 | (index >= MEASUREMENT_SLOT_NR)) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 309 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 310 | goto out_unmap_rd; |
| 311 | } |
| 312 | |
| 313 | size = rec->regs[2]; |
| 314 | |
| 315 | if (size > MAX_EXTENDED_SIZE) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 316 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 317 | goto out_unmap_rd; |
| 318 | } |
| 319 | |
| 320 | extend_measurement = &rec->regs[3]; |
| 321 | current_measurement = rd->measurement[index]; |
| 322 | |
| 323 | measurement_extend(rd->algorithm, |
| 324 | current_measurement, |
| 325 | extend_measurement, |
| 326 | size, |
| 327 | current_measurement); |
| 328 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 329 | res->smc_res.x[0] = RSI_SUCCESS; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 330 | |
| 331 | out_unmap_rd: |
| 332 | buffer_unmap(rd); |
| 333 | granule_unlock(g_rd); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 334 | } |
| 335 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 336 | void handle_rsi_measurement_read(struct rec *rec, struct rsi_result *res) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 337 | { |
| 338 | struct rd *rd; |
| 339 | unsigned long idx; |
AlexeiFedorov | efe2aec | 2023-06-08 16:17:00 +0100 | [diff] [blame] | 340 | unsigned int i, cnt; |
Mate Toth-Pal | 59b52d0 | 2023-08-18 14:14:19 +0200 | [diff] [blame] | 341 | unsigned long *measurement_value_part; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 342 | |
| 343 | assert(rec != NULL); |
| 344 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 345 | res->action = UPDATE_REC_RETURN_TO_REALM; |
| 346 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 347 | /* X1: Index */ |
| 348 | idx = rec->regs[1]; |
| 349 | |
| 350 | if (idx >= MEASUREMENT_SLOT_NR) { |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 351 | res->smc_res.x[0] = RSI_ERROR_INPUT; |
| 352 | return; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* |
| 356 | * rd lock is acquired so that measurement cannot be updated |
| 357 | * simultaneously by another rec |
| 358 | */ |
| 359 | granule_lock(rec->realm_info.g_rd, GRANULE_STATE_RD); |
Javier Almansa Sobrino | 2f717dd | 2024-02-12 20:49:46 +0000 | [diff] [blame] | 360 | rd = buffer_granule_map(rec->realm_info.g_rd, SLOT_RD); |
AlexeiFedorov | 9a9062c | 2023-08-21 15:41:48 +0100 | [diff] [blame] | 361 | assert(rd != NULL); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 362 | |
AlexeiFedorov | efe2aec | 2023-06-08 16:17:00 +0100 | [diff] [blame] | 363 | /* Number of 8-bytes words in measurement */ |
AlexeiFedorov | 4faab85 | 2023-08-30 15:06:49 +0100 | [diff] [blame] | 364 | cnt = (unsigned int)(measurement_get_size(rd->algorithm) / |
| 365 | sizeof(unsigned long)); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 366 | |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 367 | assert(cnt >= (SMC_RESULT_REGS - 1U)); |
Mate Toth-Pal | 59b52d0 | 2023-08-18 14:14:19 +0200 | [diff] [blame] | 368 | assert(cnt < ARRAY_LEN(rec->regs)); |
| 369 | |
AlexeiFedorov | efe2aec | 2023-06-08 16:17:00 +0100 | [diff] [blame] | 370 | /* Copy the part of the measurement to res->smc_res.x[] */ |
AlexeiFedorov | ea68b55 | 2023-10-03 11:11:47 +0100 | [diff] [blame] | 371 | for (i = 0U; i < (SMC_RESULT_REGS - 1U); i++) { |
Mate Toth-Pal | 59b52d0 | 2023-08-18 14:14:19 +0200 | [diff] [blame] | 372 | measurement_value_part = (unsigned long *) |
| 373 | &(rd->measurement[idx][i * sizeof(unsigned long)]); |
| 374 | res->smc_res.x[i + 1U] = *measurement_value_part; |
AlexeiFedorov | efe2aec | 2023-06-08 16:17:00 +0100 | [diff] [blame] | 375 | } |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 376 | |
AlexeiFedorov | efe2aec | 2023-06-08 16:17:00 +0100 | [diff] [blame] | 377 | /* Copy the rest of the measurement to the rec->regs[] */ |
| 378 | for (; i < cnt; i++) { |
Mate Toth-Pal | 59b52d0 | 2023-08-18 14:14:19 +0200 | [diff] [blame] | 379 | measurement_value_part = (unsigned long *) |
| 380 | &(rd->measurement[idx][i * sizeof(unsigned long)]); |
| 381 | rec->regs[i + 1U] = *measurement_value_part; |
AlexeiFedorov | efe2aec | 2023-06-08 16:17:00 +0100 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* Zero-initialize unused area */ |
| 385 | for (; i < MAX_MEASUREMENT_WORDS; i++) { |
| 386 | rec->regs[i + 1U] = 0UL; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | buffer_unmap(rd); |
| 390 | granule_unlock(rec->realm_info.g_rd); |
| 391 | |
AlexeiFedorov | 9784420 | 2023-04-27 15:17:35 +0100 | [diff] [blame] | 392 | res->smc_res.x[0] = RSI_SUCCESS; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 393 | } |