Lucian Paul-Trifu | 3519afe | 2022-03-08 15:02:31 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | * DRTM service |
| 7 | * |
| 8 | * Authors: |
| 9 | * Lucian Paul-Trifu <lucian.paultrifu@gmail.com> |
| 10 | * Brian Nezvadovitz |
| 11 | */ |
| 12 | |
| 13 | #include <stdint.h> |
| 14 | |
| 15 | #include <common/debug.h> |
| 16 | #include <common/runtime_svc.h> |
| 17 | #include <lib/xlat_tables/xlat_tables_v2.h> |
| 18 | #include <lib/el3_runtime/context_mgmt.h> |
| 19 | #include <plat/arm/common/plat_arm.h> |
| 20 | #include <plat/common/platform.h> |
| 21 | #include <services/drtm_svc.h> |
| 22 | #include <services/drtm_cache.h> |
| 23 | #include <tools_share/uuid.h> |
| 24 | |
| 25 | #include "drtm_dma_prot.h" |
| 26 | #include "drtm_main.h" |
| 27 | #include "drtm_measurements.h" |
| 28 | #include "drtm_remediation.h" |
| 29 | #include "drtm_res_tcb_hashes.h" |
| 30 | |
| 31 | #define XLAT_PAGE_SIZE PAGE_SIZE |
| 32 | #if XLAT_PAGE_SIZE != DRTM_PAGE_SIZE |
| 33 | #warning "xlat library page size differs from DRTM page size;"\ |
| 34 | " mmap_add_dynamic_region() calls to the xlat library might fail" |
| 35 | #endif |
| 36 | |
| 37 | |
| 38 | enum drtm_dlme_el { |
| 39 | DLME_AT_EL1, |
| 40 | DLME_AT_EL2 |
| 41 | }; |
| 42 | static enum drtm_dlme_el drtm_dlme_el(unsigned int el) |
| 43 | { |
| 44 | return (enum drtm_dlme_el)el - 1; |
| 45 | } |
| 46 | |
| 47 | struct __packed dlme_data_header_v1 { |
| 48 | uint16_t version; /* Must be 1. */ |
| 49 | uint16_t this_hdr_size; |
| 50 | uint8_t __res[4]; |
| 51 | uint64_t dlme_data_size; |
| 52 | uint64_t dlme_prot_regions_size; |
| 53 | uint64_t dlme_addr_map_size; |
| 54 | uint64_t dlme_tpm_log_size; |
| 55 | uint64_t dlme_tcb_hashes_table_size; |
| 56 | uint64_t dlme_impdef_region_size; |
| 57 | } __aligned(__alignof(uint16_t /* First member's type, `uint16_t version'. */)); |
| 58 | |
| 59 | typedef struct dlme_data_header_v1 struct_dlme_data_header; |
| 60 | |
| 61 | |
| 62 | static uint64_t boot_pe_aff_value; |
| 63 | static int locality2, locality3; |
| 64 | |
| 65 | |
| 66 | static unsigned int get_highest_ns_el_implemented(void) |
| 67 | { |
| 68 | return nonsecure_el_implemented(2) != EL_IMPL_NONE ? 2 : 1; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | int drtm_setup(void) |
| 73 | { |
| 74 | int rc; |
| 75 | |
| 76 | INFO("++ DRTM service setup\n"); |
| 77 | |
| 78 | boot_pe_aff_value = read_mpidr_el1() & MPIDR_AFFINITY_MASK; |
| 79 | |
| 80 | if ((rc = drtm_dma_prot_init())) { |
| 81 | return rc; |
| 82 | } |
| 83 | |
| 84 | if ((rc = drtm_tcb_hashes_init())) { |
| 85 | return rc; |
| 86 | } |
| 87 | |
| 88 | drtm_cache_init(); |
| 89 | |
| 90 | if ((rc = drtm_measurements_init())) { |
| 91 | return rc; |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | static enum drtm_retc drtm_dl_check_caller_el(void *ctx) |
| 99 | { |
| 100 | uint64_t spsr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SPSR_EL3); |
| 101 | uint64_t dl_caller_el; |
| 102 | uint64_t dl_caller_aarch; |
| 103 | |
| 104 | dl_caller_el = spsr_el3 >> MODE_EL_SHIFT & MODE_EL_MASK; |
| 105 | dl_caller_aarch = spsr_el3 >> MODE_RW_SHIFT & MODE_RW_MASK; |
| 106 | |
| 107 | if (dl_caller_el == MODE_EL3) { |
| 108 | ERROR("DRTM: invalid launch from EL3\n"); |
| 109 | return DENIED; |
| 110 | } |
| 111 | |
| 112 | if (dl_caller_aarch != MODE_RW_64) { |
| 113 | ERROR("DRTM: invalid launch from non-AArch64 execution state\n"); |
| 114 | return DENIED; |
| 115 | } |
| 116 | |
| 117 | return SUCCESS; |
| 118 | } |
| 119 | |
| 120 | static enum drtm_retc drtm_dl_check_cores(void) |
| 121 | { |
| 122 | unsigned int core_not_off; |
| 123 | uint64_t this_pe_aff_value = read_mpidr_el1() & MPIDR_AFFINITY_MASK; |
| 124 | |
| 125 | if (this_pe_aff_value != boot_pe_aff_value) { |
| 126 | ERROR("DRTM: invalid launch on a non-boot PE\n"); |
| 127 | return DENIED; |
| 128 | } |
| 129 | |
| 130 | core_not_off = psci_is_last_on_core_safe(); |
| 131 | if (core_not_off < PLATFORM_CORE_COUNT) { |
| 132 | ERROR("DRTM: invalid launch due to non-boot PE not being turned off\n"); |
| 133 | return DENIED; |
| 134 | } |
| 135 | |
| 136 | return SUCCESS; |
| 137 | } |
| 138 | |
| 139 | static enum drtm_retc drtm_dl_prepare_dlme_data(const struct_drtm_dl_args *args, |
| 140 | const drtm_event_log_t *ev_log, |
| 141 | size_t *dlme_data_size_out); |
| 142 | |
| 143 | /* |
| 144 | * Note: accesses to the dynamic launch args, and to the DLME data are |
| 145 | * little-endian as required, thanks to TF-A BL31 init requirements. |
| 146 | */ |
| 147 | static enum drtm_retc drtm_dl_check_args(uint64_t x1, |
| 148 | struct_drtm_dl_args *a_out) |
| 149 | { |
| 150 | uint64_t dlme_start, dlme_end; |
| 151 | uint64_t dlme_img_start, dlme_img_ep, dlme_img_end; |
| 152 | uint64_t dlme_data_start, dlme_data_end; |
| 153 | uintptr_t args_mapping; |
| 154 | size_t args_mapping_size; |
| 155 | struct_drtm_dl_args *a; |
| 156 | struct_drtm_dl_args args_buf; |
| 157 | size_t dlme_data_size_req; |
| 158 | struct __protected_regions protected_regions; |
| 159 | int rc; |
| 160 | enum drtm_retc ret; |
| 161 | |
| 162 | if (x1 % DRTM_PAGE_SIZE != 0) { |
| 163 | ERROR("DRTM: parameters structure is not " |
| 164 | DRTM_PAGE_SIZE_STR "-aligned\n"); |
| 165 | return INVALID_PARAMETERS; |
| 166 | } |
| 167 | |
| 168 | args_mapping_size = ALIGNED_UP(sizeof(struct_drtm_dl_args), DRTM_PAGE_SIZE); |
| 169 | rc = mmap_add_dynamic_region_alloc_va(x1, &args_mapping, args_mapping_size, |
| 170 | MT_MEMORY | MT_NS | MT_RO | MT_SHAREABILITY_ISH); |
| 171 | if (rc) { |
| 172 | WARN("DRTM: %s: mmap_add_dynamic_region() failed rc=%d\n", |
| 173 | __func__, rc); |
| 174 | return INTERNAL_ERROR; |
| 175 | } |
| 176 | a = (struct_drtm_dl_args *)args_mapping; |
| 177 | /* |
| 178 | * TODO: invalidate all data cache before reading the data passed by the |
| 179 | * DCE Preamble. This is required to avoid / defend against racing with |
| 180 | * cache evictions. |
| 181 | */ |
| 182 | args_buf = *a; |
| 183 | |
| 184 | rc = mmap_remove_dynamic_region(args_mapping, args_mapping_size); |
| 185 | if (rc) { |
| 186 | ERROR("%s(): mmap_remove_dynamic_region() failed unexpectedly" |
| 187 | " rc=%d\n", __func__, rc); |
| 188 | panic(); |
| 189 | } |
| 190 | a = &args_buf; |
| 191 | |
| 192 | if (a->version != 1) { |
| 193 | ERROR("DRTM: parameters structure incompatible with major version %d\n", |
| 194 | ARM_DRTM_VERSION_MAJOR); |
| 195 | return NOT_SUPPORTED; |
| 196 | } |
| 197 | |
| 198 | if (!(a->dlme_img_off < a->dlme_size && |
| 199 | a->dlme_data_off < a->dlme_size)) { |
| 200 | ERROR("DRTM: argument offset is outside of the DLME region\n"); |
| 201 | return INVALID_PARAMETERS; |
| 202 | } |
| 203 | dlme_start = a->dlme_paddr; |
| 204 | dlme_end = a->dlme_paddr + a->dlme_size; |
| 205 | dlme_img_start = a->dlme_paddr + a->dlme_img_off; |
| 206 | dlme_img_ep = DL_ARGS_GET_DLME_ENTRY_POINT(a); |
| 207 | dlme_img_end = dlme_img_start + a->dlme_img_size; |
| 208 | dlme_data_start = a->dlme_paddr + a->dlme_data_off; |
| 209 | dlme_data_end = dlme_end; |
| 210 | |
| 211 | /* |
| 212 | * TODO: validate that the DLME physical address range is all NS memory, |
| 213 | * return INVALID_PARAMETERS if it is not. |
| 214 | * Note that this check relies on platform-specific information. For |
| 215 | * examples, see psci_plat_pm_ops->validate_ns_entrypoint() or |
| 216 | * arm_validate_ns_entrypoint(). |
| 217 | */ |
| 218 | |
| 219 | /* Check the DLME regions arguments. */ |
| 220 | if (dlme_start % DRTM_PAGE_SIZE) { |
| 221 | ERROR("DRTM: argument DLME region is not " |
| 222 | DRTM_PAGE_SIZE_STR "-aligned\n"); |
| 223 | return INVALID_PARAMETERS; |
| 224 | } |
| 225 | |
| 226 | if (!(dlme_start < dlme_end && |
| 227 | dlme_start <= dlme_img_start && dlme_img_start < dlme_img_end && |
| 228 | dlme_start <= dlme_data_start && dlme_data_start < dlme_data_end)) { |
| 229 | ERROR("DRTM: argument DLME region is discontiguous\n"); |
| 230 | return INVALID_PARAMETERS; |
| 231 | } |
| 232 | |
| 233 | if (dlme_img_start < dlme_data_end && dlme_data_start < dlme_img_end) { |
| 234 | ERROR("DRTM: argument DLME regions overlap\n"); |
| 235 | return INVALID_PARAMETERS; |
| 236 | } |
| 237 | |
| 238 | /* Check the DLME image region arguments. */ |
| 239 | if (dlme_img_start % DRTM_PAGE_SIZE) { |
| 240 | ERROR("DRTM: argument DLME image region is not " |
| 241 | DRTM_PAGE_SIZE_STR "-aligned\n"); |
| 242 | return INVALID_PARAMETERS; |
| 243 | } |
| 244 | |
| 245 | if (!(dlme_img_start <= dlme_img_ep && dlme_img_ep < dlme_img_end)) { |
| 246 | ERROR("DRTM: DLME entry point is outside of the DLME image region\n"); |
| 247 | return INVALID_PARAMETERS; |
| 248 | } |
| 249 | |
| 250 | if (dlme_img_ep % 4) { |
| 251 | ERROR("DRTM: DLME image entry point is not 4-byte-aligned\n"); |
| 252 | return INVALID_PARAMETERS; |
| 253 | } |
| 254 | |
| 255 | /* Check the DLME data region arguments. */ |
| 256 | if (dlme_data_start % DRTM_PAGE_SIZE) { |
| 257 | ERROR("DRTM: argument DLME data region is not " |
| 258 | DRTM_PAGE_SIZE_STR "-aligned\n"); |
| 259 | return INVALID_PARAMETERS; |
| 260 | } |
| 261 | |
| 262 | rc = drtm_dl_prepare_dlme_data(NULL, NULL, &dlme_data_size_req); |
| 263 | if (rc) { |
| 264 | ERROR("%s: drtm_dl_prepare_dlme_data() failed unexpectedly rc=%d\n", |
| 265 | __func__, rc); |
| 266 | panic(); |
| 267 | } |
| 268 | if (dlme_data_end - dlme_data_start < dlme_data_size_req) { |
| 269 | ERROR("DRTM: argument DLME data region is short of %lu bytes\n", |
| 270 | dlme_data_size_req - (size_t)(dlme_data_end - dlme_data_start)); |
| 271 | return INVALID_PARAMETERS; |
| 272 | } |
| 273 | |
| 274 | /* Check the Normal World DCE region arguments. */ |
| 275 | if (a->dce_nwd_paddr != 0) { |
| 276 | uint32_t dce_nwd_start = a->dce_nwd_paddr; |
| 277 | uint32_t dce_nwd_end = dce_nwd_start + a->dce_nwd_size; |
| 278 | |
| 279 | if (!(dce_nwd_start < dce_nwd_end)) { |
| 280 | ERROR("DRTM: argument Normal World DCE region is dicontiguous\n"); |
| 281 | return INVALID_PARAMETERS; |
| 282 | } |
| 283 | |
| 284 | if (dce_nwd_start < dlme_end && dlme_start < dce_nwd_end) { |
| 285 | ERROR("DRTM: argument Normal World DCE regions overlap\n"); |
| 286 | return INVALID_PARAMETERS; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | protected_regions = (struct __protected_regions) { |
| 291 | .dlme_region = { a->dlme_paddr, a->dlme_size }, |
| 292 | .dce_nwd_region = { a->dce_nwd_paddr, a->dce_nwd_size }, |
| 293 | }; |
| 294 | if ((ret = drtm_dma_prot_check_args(&a->dma_prot_args, |
| 295 | DL_ARGS_GET_DMA_PROT_TYPE(a), |
| 296 | protected_regions))){ |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | *a_out = *a; |
| 301 | return SUCCESS; |
| 302 | } |
| 303 | |
| 304 | static enum drtm_retc drtm_dl_prepare_dlme_data(const struct_drtm_dl_args *args, |
| 305 | const drtm_event_log_t *drtm_event_log, |
| 306 | size_t *dlme_data_size_out) |
| 307 | { |
| 308 | int rc; |
| 309 | size_t dlme_data_total_bytes_req = 0; |
| 310 | uint64_t dlme_data_paddr; |
| 311 | size_t dlme_data_max_size; |
| 312 | uintptr_t dlme_data_mapping; |
| 313 | size_t dlme_data_mapping_bytes; |
| 314 | struct_dlme_data_header *dlme_data_hdr; |
| 315 | char *dlme_data_cursor; |
| 316 | size_t dlme_prot_tables_bytes; |
| 317 | const char *dlme_addr_map; |
| 318 | size_t dlme_addr_map_bytes; |
| 319 | size_t drtm_event_log_bytes; |
| 320 | size_t drtm_tcb_hashes_bytes; |
| 321 | size_t serialised_bytes_actual; |
| 322 | |
| 323 | /* Size the DLME protected regions. */ |
| 324 | drtm_dma_prot_serialise_table(NULL, &dlme_prot_tables_bytes); |
| 325 | dlme_data_total_bytes_req += dlme_prot_tables_bytes; |
| 326 | |
| 327 | /* Size the DLME address map. */ |
| 328 | drtm_cache_get_resource("address-map", |
| 329 | &dlme_addr_map, &dlme_addr_map_bytes); |
| 330 | dlme_data_total_bytes_req += dlme_addr_map_bytes; |
| 331 | |
| 332 | /* Size the DRTM event log. */ |
| 333 | drtm_serialise_event_log(NULL, drtm_event_log, &drtm_event_log_bytes); |
| 334 | dlme_data_total_bytes_req += drtm_event_log_bytes; |
| 335 | |
| 336 | /* Size the TCB hashes table. */ |
| 337 | drtm_serialise_tcb_hashes_table(NULL, &drtm_tcb_hashes_bytes); |
| 338 | dlme_data_total_bytes_req += drtm_tcb_hashes_bytes; |
| 339 | |
| 340 | /* Size the implementation-specific DLME region. */ |
| 341 | |
| 342 | if (args == NULL) { |
| 343 | if (dlme_data_size_out) { |
| 344 | *dlme_data_size_out = dlme_data_total_bytes_req; |
| 345 | } |
| 346 | return SUCCESS; |
| 347 | } |
| 348 | |
| 349 | dlme_data_paddr = args->dlme_paddr + args->dlme_data_off; |
| 350 | dlme_data_max_size = args->dlme_size - args->dlme_data_off; |
| 351 | |
| 352 | /* |
| 353 | * The capacity of the given DLME data region is checked when |
| 354 | * the other dynamic launch arguments are. |
| 355 | */ |
| 356 | if (dlme_data_max_size < dlme_data_total_bytes_req) { |
| 357 | ERROR("%s: assertion failed:" |
| 358 | " dlme_data_max_size (%ld) < dlme_data_total_bytes_req (%ld)\n", |
| 359 | __func__, dlme_data_max_size, dlme_data_total_bytes_req); |
| 360 | panic(); |
| 361 | } |
| 362 | |
| 363 | /* Map the DLME data region as NS memory. */ |
| 364 | dlme_data_mapping_bytes = ALIGNED_UP(dlme_data_max_size, DRTM_PAGE_SIZE); |
| 365 | rc = mmap_add_dynamic_region_alloc_va(dlme_data_paddr, &dlme_data_mapping, |
| 366 | dlme_data_mapping_bytes, MT_RW_DATA | MT_NS | MT_SHAREABILITY_ISH); |
| 367 | if (rc) { |
| 368 | WARN("DRTM: %s: mmap_add_dynamic_region() failed rc=%d\n", __func__, rc); |
| 369 | return INTERNAL_ERROR; |
| 370 | } |
| 371 | dlme_data_hdr = (struct_dlme_data_header *)dlme_data_mapping; |
| 372 | dlme_data_cursor = (char *)dlme_data_hdr + sizeof(*dlme_data_hdr); |
| 373 | |
| 374 | /* Set the header version and size. */ |
| 375 | dlme_data_hdr->version = 1; |
| 376 | dlme_data_hdr->this_hdr_size = sizeof(*dlme_data_hdr); |
| 377 | |
| 378 | /* Prepare DLME protected regions. */ |
| 379 | drtm_dma_prot_serialise_table(dlme_data_cursor, &serialised_bytes_actual); |
| 380 | assert(serialised_bytes_actual == dlme_prot_tables_bytes); |
| 381 | dlme_data_hdr->dlme_prot_regions_size = dlme_prot_tables_bytes; |
| 382 | dlme_data_cursor += dlme_prot_tables_bytes; |
| 383 | |
| 384 | /* Prepare DLME address map. */ |
| 385 | if (dlme_addr_map) { |
| 386 | memcpy(dlme_data_cursor, dlme_addr_map, dlme_addr_map_bytes); |
| 387 | } else { |
| 388 | WARN("DRTM: DLME address map is not in the cache\n"); |
| 389 | } |
| 390 | dlme_data_hdr->dlme_addr_map_size = dlme_addr_map_bytes; |
| 391 | dlme_data_cursor += dlme_addr_map_bytes; |
| 392 | |
| 393 | /* Prepare DRTM event log for DLME. */ |
| 394 | drtm_serialise_event_log(dlme_data_cursor, drtm_event_log, |
| 395 | &serialised_bytes_actual); |
| 396 | assert(serialised_bytes_actual <= drtm_event_log_bytes); |
| 397 | dlme_data_hdr->dlme_tpm_log_size = serialised_bytes_actual; |
| 398 | dlme_data_cursor += serialised_bytes_actual; |
| 399 | |
| 400 | /* Prepare the TCB hashes for DLME. */ |
| 401 | drtm_serialise_tcb_hashes_table(dlme_data_cursor, &serialised_bytes_actual); |
| 402 | assert(serialised_bytes_actual == drtm_tcb_hashes_bytes); |
| 403 | dlme_data_hdr->dlme_tcb_hashes_table_size = drtm_tcb_hashes_bytes; |
| 404 | dlme_data_cursor += drtm_tcb_hashes_bytes; |
| 405 | |
| 406 | /* Implementation-specific region size is unused. */ |
| 407 | dlme_data_hdr->dlme_impdef_region_size = 0; |
| 408 | dlme_data_cursor += 0; |
| 409 | |
| 410 | /* Prepare DLME data size. */ |
| 411 | dlme_data_hdr->dlme_data_size = dlme_data_cursor - (char *)dlme_data_hdr; |
| 412 | |
| 413 | /* Unmap the DLME data region. */ |
| 414 | rc = mmap_remove_dynamic_region(dlme_data_mapping, dlme_data_mapping_bytes); |
| 415 | if (rc) { |
| 416 | ERROR("%s(): mmap_remove_dynamic_region() failed" |
| 417 | " unexpectedly rc=%d\n", __func__, rc); |
| 418 | panic(); |
| 419 | } |
| 420 | |
| 421 | if (dlme_data_size_out) { |
| 422 | *dlme_data_size_out = dlme_data_total_bytes_req; |
| 423 | } |
| 424 | return SUCCESS; |
| 425 | } |
| 426 | |
| 427 | static void drtm_dl_reset_dlme_el_state(enum drtm_dlme_el dlme_el) |
| 428 | { |
| 429 | uint64_t sctlr; |
| 430 | |
| 431 | /* |
| 432 | * TODO: Set PE state according to the PSCI's specification of the initial |
| 433 | * state after CPU_ON, or to reset values if unspecified, where they exist, |
| 434 | * or define sensible values otherwise. |
| 435 | */ |
| 436 | |
| 437 | switch (dlme_el) { |
| 438 | case DLME_AT_EL1: |
| 439 | sctlr = read_sctlr_el1(); |
| 440 | break; |
| 441 | |
| 442 | case DLME_AT_EL2: |
| 443 | sctlr = read_sctlr_el2(); |
| 444 | break; |
| 445 | |
| 446 | default: /* Not reached */ |
| 447 | ERROR("%s(): dlme_el has the unexpected value %d\n", |
| 448 | __func__, dlme_el); |
| 449 | panic(); |
| 450 | } |
| 451 | |
| 452 | sctlr &= ~( |
| 453 | /* Disable DLME's EL MMU, since the existing page-tables are untrusted. */ |
| 454 | SCTLR_M_BIT |
| 455 | | SCTLR_EE_BIT /* Little-endian data accesses. */ |
| 456 | ); |
| 457 | |
| 458 | sctlr |= |
| 459 | SCTLR_C_BIT | SCTLR_I_BIT /* Allow instruction and data caching. */ |
| 460 | ; |
| 461 | |
| 462 | switch (dlme_el) { |
| 463 | case DLME_AT_EL1: |
| 464 | write_sctlr_el1(sctlr); |
| 465 | break; |
| 466 | |
| 467 | case DLME_AT_EL2: |
| 468 | write_sctlr_el2(sctlr); |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | static void drtm_dl_reset_dlme_context(enum drtm_dlme_el dlme_el) |
| 474 | { |
| 475 | void *ns_ctx = cm_get_context(NON_SECURE); |
| 476 | gp_regs_t *gpregs = get_gpregs_ctx(ns_ctx); |
| 477 | uint64_t spsr_el3 = read_ctx_reg(get_el3state_ctx(ns_ctx), CTX_SPSR_EL3); |
| 478 | |
| 479 | /* Reset all gpregs, including SP_EL0. */ |
| 480 | memset(gpregs, 0, sizeof(*gpregs)); |
| 481 | |
| 482 | /* Reset SP_ELx. */ |
| 483 | switch (dlme_el) { |
| 484 | case DLME_AT_EL1: |
| 485 | write_sp_el1(0); |
| 486 | break; |
| 487 | |
| 488 | case DLME_AT_EL2: |
| 489 | write_sp_el2(0); |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * DLME's async exceptions are masked to avoid a NWd attacker's timed |
| 495 | * interference with any state we established trust in or measured. |
| 496 | */ |
| 497 | spsr_el3 |= SPSR_DAIF_MASK << SPSR_DAIF_SHIFT; |
| 498 | |
| 499 | write_ctx_reg(get_el3state_ctx(ns_ctx), CTX_SPSR_EL3, spsr_el3); |
| 500 | } |
| 501 | |
| 502 | static void drtm_dl_prepare_eret_to_dlme(const struct_drtm_dl_args *args, |
| 503 | enum drtm_dlme_el dlme_el) |
| 504 | { |
| 505 | void *ctx = cm_get_context(NON_SECURE); |
| 506 | uint64_t dlme_ep = DL_ARGS_GET_DLME_ENTRY_POINT(args); |
| 507 | uint64_t spsr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SPSR_EL3); |
| 508 | |
| 509 | /* Next ERET is to the DLME's EL. */ |
| 510 | spsr_el3 &= ~(MODE_EL_MASK << MODE_EL_SHIFT); |
| 511 | switch (dlme_el) { |
| 512 | case DLME_AT_EL1: |
| 513 | spsr_el3 |= MODE_EL1 << MODE_EL_SHIFT; |
| 514 | break; |
| 515 | |
| 516 | case DLME_AT_EL2: |
| 517 | spsr_el3 |= MODE_EL2 << MODE_EL_SHIFT; |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | /* Next ERET is to the DLME entry point. */ |
| 522 | cm_set_elr_spsr_el3(NON_SECURE, dlme_ep, spsr_el3); |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * TODO: |
| 527 | * - Close locality 3; |
| 528 | * - See section 4.4 and section 4.5 for other requirements; |
| 529 | */ |
| 530 | static uint64_t drtm_dynamic_launch(uint64_t x1, void *handle) |
| 531 | { |
| 532 | enum drtm_retc ret; |
| 533 | struct_drtm_dl_args args; |
| 534 | enum drtm_dlme_el dlme_el; |
| 535 | drtm_event_log_t event_log; |
| 536 | |
| 537 | /* |
| 538 | * Non-secure interrupts are masked to avoid a NWd attacker's timed |
| 539 | * interference with any state we are establishing trust in or measuring. |
| 540 | * Note that in this particular implementation, both Non-secure and Secure |
| 541 | * interrupts are automatically masked consequence of the SMC call. |
| 542 | */ |
| 543 | |
| 544 | if ((ret = drtm_dl_check_caller_el(handle))) { |
| 545 | SMC_RET1(handle, ret); |
| 546 | } |
| 547 | |
| 548 | if ((ret = drtm_dl_check_cores())) { |
| 549 | SMC_RET1(handle, ret); |
| 550 | } |
| 551 | |
| 552 | if ((ret = drtm_dl_check_args(x1, &args))) { |
| 553 | SMC_RET1(handle, ret); |
| 554 | } |
| 555 | |
| 556 | drtm_dl_ensure_tcb_hashes_are_final(); |
| 557 | |
| 558 | /* |
| 559 | * Engage the DMA protections. The launch cannot proceed without the DMA |
| 560 | * protections due to potential TOC/TOU vulnerabilities w.r.t. the DLME |
| 561 | * region (and to the NWd DCE region). |
| 562 | */ |
| 563 | if ((ret = drtm_dma_prot_engage(&args.dma_prot_args, |
| 564 | DL_ARGS_GET_DMA_PROT_TYPE(&args)))) { |
| 565 | SMC_RET1(handle, ret); |
| 566 | } |
| 567 | |
| 568 | /* |
| 569 | * The DMA protection is now engaged. Note that any failure mode that |
| 570 | * returns an error to the DRTM-launch caller must now disengage DMA |
| 571 | * protections before returning to the caller. |
| 572 | */ |
| 573 | |
| 574 | if ((ret = drtm_take_measurements(&args, &event_log))) { |
| 575 | goto err_undo_dma_prot; |
| 576 | } |
| 577 | |
| 578 | if ((ret = drtm_dl_prepare_dlme_data(&args, &event_log, NULL))) { |
| 579 | goto err_undo_dma_prot; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Note that, at the time of writing, the DRTM spec allows a successful |
| 584 | * launch from NS-EL1 to return to a DLME in NS-EL2. The practical risk |
| 585 | * of a privilege escalation, e.g. due to a compromised hypervisor, is |
| 586 | * considered small enough not to warrant the specification of additional |
| 587 | * DRTM conduits that would be necessary to maintain OSs' abstraction from |
| 588 | * the presence of EL2 were the dynamic launch only be allowed from the |
| 589 | * highest NS EL. |
| 590 | */ |
| 591 | dlme_el = drtm_dlme_el(get_highest_ns_el_implemented()); |
| 592 | |
| 593 | drtm_dl_reset_dlme_el_state(dlme_el); |
| 594 | drtm_dl_reset_dlme_context(dlme_el); |
| 595 | |
| 596 | /* |
| 597 | * TODO: Reset all SDEI event handlers, since they are untrusted. Both |
| 598 | * private and shared events for all cores must be unregistered. |
| 599 | * Note that simply calling SDEI ABIs would not be adequate for this, since |
| 600 | * there is currently no SDEI operation that clears private data for all PEs. |
| 601 | */ |
| 602 | |
| 603 | drtm_dl_prepare_eret_to_dlme(&args, dlme_el); |
| 604 | |
| 605 | /* |
| 606 | * TODO: invalidate the instruction cache before jumping to the DLME. |
| 607 | * This is required to defend against potentially-malicious cache contents. |
| 608 | */ |
| 609 | |
| 610 | /* Return the DLME region's address in x0, and the DLME data offset in x1.*/ |
| 611 | SMC_RET2(handle, args.dlme_paddr, args.dlme_data_off); |
| 612 | |
| 613 | err_undo_dma_prot: |
| 614 | ; |
| 615 | int rc; |
| 616 | |
| 617 | if ((rc = drtm_dma_prot_disengage())) { |
| 618 | ERROR("%s(): drtm_dma_prot_disengage() failed unexpectedly" |
| 619 | " rc=%d\n", __func__, rc); |
| 620 | panic(); |
| 621 | } |
| 622 | SMC_RET1(handle, ret); |
| 623 | } |
| 624 | |
| 625 | |
| 626 | static uint64_t drtm_features_tpm(void *ctx) |
| 627 | { |
| 628 | SMC_RET2(ctx, 1ULL, /* TPM feature is supported */ |
| 629 | 1ULL << 33 /* Default PCR usage schema */ |
| 630 | | 0ULL << 32 /* Firmware-based hashing */ |
| 631 | /* The firmware hashing algorithm */ |
| 632 | | (uint32_t)DRTM_TPM_HASH_ALG << 0 |
| 633 | ); |
| 634 | } |
| 635 | |
| 636 | static uint64_t drtm_features_mem_req(void *ctx) |
| 637 | { |
| 638 | int rc; |
| 639 | size_t dlme_data_bytes_req; |
| 640 | uint64_t dlme_data_pages_req; |
| 641 | |
| 642 | rc = drtm_dl_prepare_dlme_data(NULL, NULL, &dlme_data_bytes_req); |
| 643 | if (rc) { |
| 644 | ERROR("%s(): drtm_dl_prepare_dlme_data() failed unexpectedly" |
| 645 | " rc=%d\n", __func__, rc); |
| 646 | panic(); |
| 647 | } |
| 648 | |
| 649 | dlme_data_pages_req = ALIGNED_UP(dlme_data_bytes_req, DRTM_PAGE_SIZE) |
| 650 | / DRTM_PAGE_SIZE; |
| 651 | if (dlme_data_pages_req > UINT32_MAX) { |
| 652 | ERROR("%s(): dlme_data_pages_req is unexpectedly large" |
| 653 | " (does not fit in the bit-field)\n", __func__); |
| 654 | panic(); |
| 655 | } |
| 656 | |
| 657 | SMC_RET2(ctx, 1ULL, /* Feature is supported */ |
| 658 | 0ULL << 32 /* Not using a Normal World DCE */ |
| 659 | /* Minimum amount of space needed for the DLME data */ |
| 660 | | (dlme_data_pages_req & 0xffffffffULL) |
| 661 | ); |
| 662 | } |
| 663 | |
| 664 | static uint64_t drtm_features_boot_pe_id(void *ctx) |
| 665 | { |
| 666 | SMC_RET2(ctx, 1ULL, /* Boot PE feature is supported */ |
| 667 | boot_pe_aff_value /* Boot PE identification */ |
| 668 | ); |
| 669 | } |
| 670 | |
| 671 | |
| 672 | uint64_t drtm_smc_handler(uint32_t smc_fid, |
| 673 | uint64_t x1, |
| 674 | uint64_t x2, |
| 675 | uint64_t x3, |
| 676 | uint64_t x4, |
| 677 | void *cookie, |
| 678 | void *handle, |
| 679 | uint64_t flags) |
| 680 | { |
| 681 | /* Check that the SMC call is from the Normal World. */ |
| 682 | if (is_caller_secure(flags)) { |
| 683 | SMC_RET1(handle, NOT_SUPPORTED); |
| 684 | } |
| 685 | |
| 686 | switch (smc_fid) { |
| 687 | case ARM_DRTM_SVC_VERSION: |
| 688 | INFO("++ DRTM service handler: version\n"); |
| 689 | /* Return the version of current implementation */ |
| 690 | SMC_RET1(handle, ARM_DRTM_VERSION); |
| 691 | |
| 692 | case ARM_DRTM_SVC_FEATURES: |
| 693 | if ((x1 >> 63 & 0x1U) == 0) { |
| 694 | uint32_t func_id = x1; |
| 695 | |
| 696 | /* Dispatch function-based queries. */ |
| 697 | switch (func_id) { |
| 698 | case ARM_DRTM_SVC_VERSION: |
| 699 | INFO("++ DRTM service handler: DRTM_VERSION feature\n"); |
| 700 | SMC_RET1(handle, SUCCESS); |
| 701 | |
| 702 | case ARM_DRTM_SVC_FEATURES: |
| 703 | INFO("++ DRTM service handler: DRTM_FEATURES feature\n"); |
| 704 | SMC_RET1(handle, SUCCESS); |
| 705 | |
| 706 | case ARM_DRTM_SVC_UNPROTECT_MEM: |
| 707 | INFO("++ DRTM service handler: DRTM_UNPROTECT_MEMORY feature\n"); |
| 708 | SMC_RET1(handle, SUCCESS); |
| 709 | |
| 710 | case ARM_DRTM_SVC_DYNAMIC_LAUNCH: |
| 711 | INFO("++ DRTM service handler: DRTM_DYNAMIC_LAUNCH feature\n"); |
| 712 | SMC_RET1(handle, SUCCESS); |
| 713 | |
| 714 | case ARM_DRTM_SVC_CLOSE_LOCALITY: |
| 715 | INFO("++ DRTM service handler: DRTM_CLOSE_LOCALITY feature\n"); |
| 716 | SMC_RET1(handle, NOT_SUPPORTED); |
| 717 | |
| 718 | case ARM_DRTM_SVC_GET_ERROR: |
| 719 | INFO("++ DRTM service handler: DRTM_GET_ERROR feature\n"); |
| 720 | SMC_RET1(handle, NOT_SUPPORTED); |
| 721 | |
| 722 | case ARM_DRTM_SVC_SET_ERROR: |
| 723 | INFO("++ DRTM service handler: DRTM_SET_ERROR feature\n"); |
| 724 | SMC_RET1(handle, NOT_SUPPORTED); |
| 725 | |
| 726 | case ARM_DRTM_SVC_SET_TCB_HASH: |
| 727 | INFO("++ DRTM service handler: DRTM_SET_TCB_HASH feature\n"); |
| 728 | SMC_RET1(handle, NOT_SUPPORTED); |
| 729 | |
| 730 | case ARM_DRTM_SVC_LOCK_TCB_HASHES: |
| 731 | INFO("++ DRTM service handler: DRTM_LOCK_TCB_HASHES feature\n"); |
| 732 | SMC_RET1(handle, NOT_SUPPORTED); |
| 733 | |
| 734 | default: |
| 735 | ERROR("Unknown ARM DRTM service function feature\n"); |
| 736 | SMC_RET1(handle, NOT_SUPPORTED); |
| 737 | } |
| 738 | } else { |
| 739 | uint8_t feat_id = x1; |
| 740 | |
| 741 | /* Dispatch feature-based queries. */ |
| 742 | switch (feat_id) { |
| 743 | case ARM_DRTM_FEATURES_TPM: |
| 744 | INFO("++ DRTM service handler: TPM features\n"); |
| 745 | return drtm_features_tpm(handle); |
| 746 | |
| 747 | case ARM_DRTM_FEATURES_MEM_REQ: |
| 748 | INFO("++ DRTM service handler: Min. mem." |
| 749 | " requirement features\n"); |
| 750 | return drtm_features_mem_req(handle); |
| 751 | |
| 752 | case ARM_DRTM_FEATURES_DMA_PROT: |
| 753 | INFO("++ DRTM service handler: DMA protection features\n"); |
| 754 | return drtm_features_dma_prot(handle); |
| 755 | |
| 756 | case ARM_DRTM_FEATURES_BOOT_PE_ID: |
| 757 | INFO("++ DRTM service handler: Boot PE ID features\n"); |
| 758 | return drtm_features_boot_pe_id(handle); |
| 759 | |
| 760 | case ARM_DRTM_FEATURES_TCB_HASHES: |
| 761 | INFO("++ DRTM service handler: TCB-hashes features\n"); |
| 762 | return drtm_features_tcb_hashes(handle); |
| 763 | |
| 764 | default: |
| 765 | ERROR("Unknown ARM DRTM service feature\n"); |
| 766 | SMC_RET1(handle, NOT_SUPPORTED); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | case ARM_DRTM_SVC_UNPROTECT_MEM: |
| 771 | INFO("++ DRTM service handler: unprotect mem\n"); |
| 772 | return drtm_unprotect_mem(handle); |
| 773 | |
| 774 | case ARM_DRTM_SVC_DYNAMIC_LAUNCH: |
| 775 | INFO("++ DRTM service handler: dynamic launch\n"); |
| 776 | //locality2 = 1; |
| 777 | //locality3 = 1; |
| 778 | return drtm_dynamic_launch(x1, handle); |
| 779 | |
| 780 | case ARM_DRTM_SVC_CLOSE_LOCALITY: |
| 781 | INFO("++ DRTM service handler: close locality\n"); |
| 782 | if (x1 == 2) { |
| 783 | if (locality2 == 1) { |
| 784 | locality2 = 0; |
| 785 | SMC_RET1(handle, SMC_OK); |
| 786 | } |
| 787 | SMC_RET1(handle, DENIED); |
| 788 | } |
| 789 | if (x1 == 3) { |
| 790 | if (locality3 == 1) { |
| 791 | locality3 = 0; |
| 792 | SMC_RET1(handle, SMC_OK); |
| 793 | } |
| 794 | SMC_RET1(handle, DENIED); |
| 795 | } |
| 796 | SMC_RET1(handle, INVALID_PARAMETERS); |
| 797 | |
| 798 | case ARM_DRTM_SVC_GET_ERROR: |
| 799 | INFO("++ DRTM service handler: get error\n"); |
| 800 | return drtm_get_error(handle); |
| 801 | |
| 802 | case ARM_DRTM_SVC_SET_ERROR: |
| 803 | INFO("++ DRTM service handler: set error\n"); |
| 804 | return drtm_set_error(x1, handle); |
| 805 | |
| 806 | default: |
| 807 | ERROR("Unknown ARM DRTM service call: 0x%x \n", smc_fid); |
| 808 | SMC_RET1(handle, SMC_UNK); |
| 809 | } |
| 810 | } |