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