1 /* 2 * Copyright (c) 2022-2025 Arm Limited. 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 <brinez@microsoft.com> 2021-02-01 11 */ 12 13 #include <stdint.h> 14 15 #include <arch.h> 16 #include <arch_helpers.h> 17 #include <common/bl_common.h> 18 #include <common/debug.h> 19 #include <common/runtime_svc.h> 20 #include <drivers/auth/crypto_mod.h> 21 #include "drtm_main.h" 22 #include "drtm_measurements.h" 23 #include "drtm_remediation.h" 24 #include <lib/el3_runtime/context_mgmt.h> 25 #include <lib/psci/psci_lib.h> 26 #include <lib/xlat_tables/xlat_tables_v2.h> 27 #include <plat/common/platform.h> 28 #include <services/drtm_svc.h> 29 #include <services/sdei.h> 30 #include <platform_def.h> 31 32 /* Structure to store DRTM features specific to the platform. */ 33 static drtm_features_t plat_drtm_features; 34 35 /* DRTM-formatted memory map. */ 36 static drtm_memory_region_descriptor_table_t *plat_drtm_mem_map; 37 static const plat_drtm_dma_prot_features_t *plat_dma_prot_feat; 38 static const plat_drtm_tpm_features_t *plat_tpm_feat; 39 40 /* DLME header */ 41 struct_dlme_data_header dlme_data_hdr_init; 42 43 /* Minimum data memory requirement */ 44 uint64_t dlme_data_min_size; 45 46 int drtm_setup(void) 47 { 48 bool rc; 49 50 INFO("DRTM service setup\n"); 51 52 /* Read boot PE ID from MPIDR */ 53 plat_drtm_features.boot_pe_id = read_mpidr_el1() & MPIDR_AFFINITY_MASK; 54 55 rc = drtm_dma_prot_init(); 56 if (rc) { 57 return INTERNAL_ERROR; 58 } 59 60 /* 61 * initialise the platform supported crypto module that will 62 * be used by the DRTM-service to calculate hash of DRTM- 63 * implementation specific components 64 */ 65 crypto_mod_init(); 66 67 /* Build DRTM-compatible address map. */ 68 plat_drtm_mem_map = drtm_build_address_map(); 69 if (plat_drtm_mem_map == NULL) { 70 return INTERNAL_ERROR; 71 } 72 73 /* Get DRTM features from platform hooks. */ 74 plat_tpm_feat = plat_drtm_get_tpm_features(); 75 if (plat_tpm_feat == NULL) { 76 return INTERNAL_ERROR; 77 } 78 79 plat_dma_prot_feat = plat_drtm_get_dma_prot_features(); 80 if (plat_dma_prot_feat == NULL) { 81 return INTERNAL_ERROR; 82 } 83 84 /* 85 * Add up minimum DLME data memory. 86 * 87 * For systems with complete DMA protection there is only one entry in 88 * the protected regions table. 89 */ 90 if (plat_dma_prot_feat->dma_protection_support == 91 ARM_DRTM_DMA_PROT_FEATURES_DMA_SUPPORT_COMPLETE) { 92 dlme_data_min_size = 93 sizeof(drtm_memory_region_descriptor_table_t) + 94 sizeof(drtm_mem_region_t); 95 dlme_data_hdr_init.dlme_prot_regions_size = dlme_data_min_size; 96 } else { 97 /* 98 * TODO set protected regions table size based on platform DMA 99 * protection configuration 100 */ 101 panic(); 102 } 103 104 dlme_data_hdr_init.dlme_addr_map_size = drtm_get_address_map_size(); 105 dlme_data_hdr_init.dlme_tcb_hashes_table_size = 106 plat_drtm_get_tcb_hash_table_size(); 107 dlme_data_hdr_init.dlme_acpi_tables_region_size = 108 plat_drtm_get_acpi_tables_region_size(); 109 dlme_data_hdr_init.dlme_impdef_region_size = 110 plat_drtm_get_imp_def_dlme_region_size(); 111 112 dlme_data_min_size += sizeof(struct_dlme_data_header) + 113 dlme_data_hdr_init.dlme_addr_map_size + 114 ARM_DRTM_MIN_EVENT_LOG_SIZE + 115 dlme_data_hdr_init.dlme_tcb_hashes_table_size + 116 dlme_data_hdr_init.dlme_acpi_tables_region_size + 117 dlme_data_hdr_init.dlme_impdef_region_size; 118 119 /* Fill out platform DRTM features structure */ 120 /* Only support default PCR schema (0x1) in this implementation. */ 121 ARM_DRTM_TPM_FEATURES_SET_PCR_SCHEMA(plat_drtm_features.tpm_features, 122 ARM_DRTM_TPM_FEATURES_PCR_SCHEMA_DEFAULT); 123 ARM_DRTM_TPM_FEATURES_SET_TPM_HASH(plat_drtm_features.tpm_features, 124 plat_tpm_feat->tpm_based_hash_support); 125 ARM_DRTM_TPM_FEATURES_SET_FW_HASH(plat_drtm_features.tpm_features, 126 plat_tpm_feat->firmware_hash_algorithm); 127 ARM_DRTM_MIN_MEM_REQ_SET_MIN_DLME_DATA_SIZE(plat_drtm_features.minimum_memory_requirement, 128 page_align(dlme_data_min_size, UP)/PAGE_SIZE); 129 ARM_DRTM_MIN_MEM_REQ_SET_DCE_SIZE(plat_drtm_features.minimum_memory_requirement, 130 plat_drtm_get_min_size_normal_world_dce()); 131 ARM_DRTM_DMA_PROT_FEATURES_SET_MAX_REGIONS(plat_drtm_features.dma_prot_features, 132 plat_dma_prot_feat->max_num_mem_prot_regions); 133 ARM_DRTM_DMA_PROT_FEATURES_SET_DMA_SUPPORT(plat_drtm_features.dma_prot_features, 134 plat_dma_prot_feat->dma_protection_support); 135 ARM_DRTM_TCB_HASH_FEATURES_SET_MAX_NUM_HASHES(plat_drtm_features.tcb_hash_features, 136 plat_drtm_get_tcb_hash_features()); 137 ARM_DRTM_DLME_IMG_AUTH_SUPPORT(plat_drtm_features.dlme_image_auth_features, 138 plat_drtm_get_dlme_img_auth_features()); 139 140 return 0; 141 } 142 143 static inline void invalidate_icache_all(void) 144 { 145 __asm__ volatile("ic ialluis"); 146 dsb(); 147 isb(); 148 } 149 150 static inline uint64_t drtm_features_tpm(void *ctx) 151 { 152 SMC_RET2(ctx, 1ULL, /* TPM feature is supported */ 153 plat_drtm_features.tpm_features); 154 } 155 156 static inline uint64_t drtm_features_mem_req(void *ctx) 157 { 158 SMC_RET2(ctx, 1ULL, /* memory req Feature is supported */ 159 plat_drtm_features.minimum_memory_requirement); 160 } 161 162 static inline uint64_t drtm_features_boot_pe_id(void *ctx) 163 { 164 SMC_RET2(ctx, 1ULL, /* Boot PE feature is supported */ 165 plat_drtm_features.boot_pe_id); 166 } 167 168 static inline uint64_t drtm_features_dma_prot(void *ctx) 169 { 170 SMC_RET2(ctx, 1ULL, /* DMA protection feature is supported */ 171 plat_drtm_features.dma_prot_features); 172 } 173 174 static inline uint64_t drtm_features_tcb_hashes(void *ctx) 175 { 176 SMC_RET2(ctx, 1ULL, /* TCB hash feature is supported */ 177 plat_drtm_features.tcb_hash_features); 178 } 179 180 static inline uint64_t drtm_features_dlme_img_auth_features(void *ctx) 181 { 182 SMC_RET2(ctx, 1ULL, /* DLME Image auth is supported */ 183 plat_drtm_features.dlme_image_auth_features); 184 } 185 186 static enum drtm_retc drtm_dl_check_caller_el(void *ctx) 187 { 188 uint64_t spsr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SPSR_EL3); 189 uint64_t dl_caller_el; 190 uint64_t dl_caller_aarch; 191 192 dl_caller_el = spsr_el3 >> MODE_EL_SHIFT & MODE_EL_MASK; 193 dl_caller_aarch = spsr_el3 >> MODE_RW_SHIFT & MODE_RW_MASK; 194 195 /* Caller's security state is checked from drtm_smc_handle function */ 196 197 /* Caller can be NS-EL2/EL1 */ 198 if (dl_caller_el == MODE_EL3) { 199 ERROR("DRTM: invalid launch from EL3\n"); 200 return DENIED; 201 } 202 203 if (dl_caller_aarch != MODE_RW_64) { 204 ERROR("DRTM: invalid launch from non-AArch64 execution state\n"); 205 return DENIED; 206 } 207 208 return SUCCESS; 209 } 210 211 static enum drtm_retc drtm_dl_check_cores(void) 212 { 213 bool running_on_single_core; 214 uint64_t this_pe_aff_value = read_mpidr_el1() & MPIDR_AFFINITY_MASK; 215 216 if (this_pe_aff_value != plat_drtm_features.boot_pe_id) { 217 ERROR("DRTM: invalid launch on a non-boot PE\n"); 218 return DENIED; 219 } 220 221 running_on_single_core = psci_is_last_on_cpu_safe(plat_my_core_pos()); 222 if (!running_on_single_core) { 223 ERROR("DRTM: invalid launch due to non-boot PE not being turned off\n"); 224 return SECONDARY_PE_NOT_OFF; 225 } 226 227 return SUCCESS; 228 } 229 230 static enum drtm_retc drtm_dl_prepare_dlme_data(const struct_drtm_dl_args *args) 231 { 232 int rc; 233 uint64_t dlme_data_paddr; 234 size_t dlme_data_max_size; 235 uintptr_t dlme_data_mapping; 236 struct_dlme_data_header *dlme_data_hdr; 237 uint8_t *dlme_data_cursor; 238 size_t dlme_data_mapping_bytes; 239 size_t serialised_bytes_actual; 240 241 dlme_data_paddr = args->dlme_paddr + args->dlme_data_off; 242 dlme_data_max_size = args->dlme_size - args->dlme_data_off; 243 244 /* 245 * The capacity of the given DLME data region is checked when 246 * the other dynamic launch arguments are. 247 */ 248 if (dlme_data_max_size < dlme_data_min_size) { 249 ERROR("%s: assertion failed:" 250 " dlme_data_max_size (%ld) < dlme_data_min_size (%ld)\n", 251 __func__, dlme_data_max_size, dlme_data_min_size); 252 panic(); 253 } 254 255 /* Map the DLME data region as NS memory. */ 256 dlme_data_mapping_bytes = ALIGNED_UP(dlme_data_max_size, DRTM_PAGE_SIZE); 257 rc = mmap_add_dynamic_region_alloc_va(dlme_data_paddr, 258 &dlme_data_mapping, 259 dlme_data_mapping_bytes, 260 MT_RW_DATA | MT_NS | 261 MT_SHAREABILITY_ISH); 262 if (rc != 0) { 263 WARN("DRTM: %s: mmap_add_dynamic_region() failed rc=%d\n", 264 __func__, rc); 265 return INTERNAL_ERROR; 266 } 267 dlme_data_hdr = (struct_dlme_data_header *)dlme_data_mapping; 268 dlme_data_cursor = (uint8_t *)dlme_data_hdr + sizeof(*dlme_data_hdr); 269 270 memcpy(dlme_data_hdr, (const void *)&dlme_data_hdr_init, 271 sizeof(*dlme_data_hdr)); 272 273 /* Set the header version and size. */ 274 dlme_data_hdr->version = 1; 275 dlme_data_hdr->this_hdr_size = sizeof(*dlme_data_hdr); 276 277 /* Prepare DLME protected regions. */ 278 drtm_dma_prot_serialise_table(dlme_data_cursor, 279 &serialised_bytes_actual); 280 assert(serialised_bytes_actual == 281 dlme_data_hdr->dlme_prot_regions_size); 282 dlme_data_cursor += serialised_bytes_actual; 283 284 /* Prepare DLME address map. */ 285 if (plat_drtm_mem_map != NULL) { 286 memcpy(dlme_data_cursor, plat_drtm_mem_map, 287 dlme_data_hdr->dlme_addr_map_size); 288 } else { 289 WARN("DRTM: DLME address map is not in the cache\n"); 290 } 291 dlme_data_cursor += dlme_data_hdr->dlme_addr_map_size; 292 293 /* Prepare DRTM event log for DLME. */ 294 drtm_serialise_event_log(dlme_data_cursor, &serialised_bytes_actual); 295 assert(serialised_bytes_actual <= ARM_DRTM_MIN_EVENT_LOG_SIZE); 296 dlme_data_hdr->dlme_tpm_log_size = serialised_bytes_actual; 297 dlme_data_cursor += serialised_bytes_actual; 298 299 /* 300 * TODO: Prepare the TCB hashes for DLME, currently its size 301 * 0 302 */ 303 dlme_data_cursor += dlme_data_hdr->dlme_tcb_hashes_table_size; 304 305 /* Implementation-specific region size is unused. */ 306 dlme_data_cursor += dlme_data_hdr->dlme_impdef_region_size; 307 308 /* 309 * Prepare DLME data size, includes all data region referenced above 310 * alongwith the DLME data header 311 */ 312 dlme_data_hdr->dlme_data_size = dlme_data_cursor - (uint8_t *)dlme_data_hdr; 313 314 /* Unmap the DLME data region. */ 315 rc = mmap_remove_dynamic_region(dlme_data_mapping, dlme_data_mapping_bytes); 316 if (rc != 0) { 317 ERROR("%s(): mmap_remove_dynamic_region() failed" 318 " unexpectedly rc=%d\n", __func__, rc); 319 panic(); 320 } 321 322 return SUCCESS; 323 } 324 325 /* Function to check if the value is valid for each bit field */ 326 static int drtm_dl_check_features_sanity(uint32_t val) 327 { 328 /** 329 * Ensure that if DLME Authorities Schema (Bits [2:1]) is set, then 330 * DLME image authentication (Bit[6]) must also be set 331 */ 332 if ((EXTRACT_FIELD(val, DRTM_LAUNCH_FEAT_PCR_USAGE_SCHEMA_MASK, 333 DRTM_LAUNCH_FEAT_PCR_USAGE_SCHEMA_SHIFT) == DLME_AUTH_SCHEMA) && 334 (EXTRACT_FIELD(val, DRTM_LAUNCH_FEAT_DLME_IMG_AUTH_MASK, 335 DRTM_LAUNCH_FEAT_DLME_IMG_AUTH_SHIFT) != DLME_IMG_AUTH)) { 336 return INVALID_PARAMETERS; 337 } 338 339 /** 340 * Check if Bits [5:3] (Memory protection type) matches with platform's 341 * memory protection type 342 */ 343 if (EXTRACT_FIELD(val, DRTM_LAUNCH_FEAT_MEM_PROTECTION_TYPE_MASK, 344 DRTM_LAUNCH_FEAT_MEM_PROTECTION_TYPE_SHIFT) != 345 __builtin_ctz(plat_dma_prot_feat->dma_protection_support)) { 346 return INVALID_PARAMETERS; 347 } 348 349 /** 350 * Check if Bits [0] (Type of hashing) matches with platform's 351 * supported hash type. 352 */ 353 if (EXTRACT_FIELD(val, DRTM_LAUNCH_FEAT_HASHING_TYPE_MASK, 354 DRTM_LAUNCH_FEAT_HASHING_TYPE_SHIFT) != 355 plat_tpm_feat->tpm_based_hash_support) { 356 return INVALID_PARAMETERS; 357 } 358 359 return 0; 360 } 361 362 /* 363 * Note: accesses to the dynamic launch args, and to the DLME data are 364 * little-endian as required, thanks to TF-A BL31 init requirements. 365 */ 366 static enum drtm_retc drtm_dl_check_args(uint64_t x1, 367 struct_drtm_dl_args *a_out) 368 { 369 uint64_t dlme_start, dlme_end; 370 uint64_t dlme_img_start, dlme_img_ep, dlme_img_end; 371 uint64_t dlme_data_start, dlme_data_end; 372 uintptr_t va_mapping; 373 size_t va_mapping_size; 374 struct_drtm_dl_args *a; 375 struct_drtm_dl_args args_buf; 376 int rc; 377 378 if (x1 % DRTM_PAGE_SIZE != 0) { 379 ERROR("DRTM: parameters structure is not " 380 DRTM_PAGE_SIZE_STR "-aligned\n"); 381 return INVALID_PARAMETERS; 382 } 383 384 va_mapping_size = ALIGNED_UP(sizeof(struct_drtm_dl_args), DRTM_PAGE_SIZE); 385 386 /* check DRTM parameters are within NS address region */ 387 rc = plat_drtm_validate_ns_region(x1, va_mapping_size); 388 if (rc != 0) { 389 ERROR("DRTM: parameters lies within secure memory\n"); 390 return INVALID_PARAMETERS; 391 } 392 393 rc = mmap_add_dynamic_region_alloc_va(x1, &va_mapping, va_mapping_size, 394 MT_MEMORY | MT_NS | MT_RO | 395 MT_SHAREABILITY_ISH); 396 if (rc != 0) { 397 WARN("DRTM: %s: mmap_add_dynamic_region() failed rc=%d\n", 398 __func__, rc); 399 return INTERNAL_ERROR; 400 } 401 a = (struct_drtm_dl_args *)va_mapping; 402 403 /* Sanitize cache of data passed in args by the DCE Preamble. */ 404 flush_dcache_range(va_mapping, va_mapping_size); 405 406 args_buf = *a; 407 408 rc = mmap_remove_dynamic_region(va_mapping, va_mapping_size); 409 if (rc != 0) { 410 ERROR("%s(): mmap_remove_dynamic_region() failed unexpectedly" 411 " rc=%d\n", __func__, rc); 412 panic(); 413 } 414 a = &args_buf; 415 416 if (!((a->version >= ARM_DRTM_PARAMS_MIN_VERSION) && 417 (a->version <= ARM_DRTM_PARAMS_MAX_VERSION))) { 418 ERROR("DRTM: parameters structure version %u is unsupported\n", 419 a->version); 420 return NOT_SUPPORTED; 421 } 422 423 rc = drtm_dl_check_features_sanity(a->features); 424 if (rc != 0) { 425 ERROR("%s(): drtm_dl_check_features_sanity() failed.\n" 426 " rc=%d\n", __func__, rc); 427 return rc; 428 } 429 430 if (!(a->dlme_img_off < a->dlme_size && 431 a->dlme_data_off < a->dlme_size)) { 432 ERROR("DRTM: argument offset is outside of the DLME region\n"); 433 return INVALID_PARAMETERS; 434 } 435 dlme_start = a->dlme_paddr; 436 dlme_end = a->dlme_paddr + a->dlme_size; 437 dlme_img_start = a->dlme_paddr + a->dlme_img_off; 438 dlme_img_ep = dlme_img_start + a->dlme_img_ep_off; 439 dlme_img_end = dlme_img_start + a->dlme_img_size; 440 dlme_data_start = a->dlme_paddr + a->dlme_data_off; 441 dlme_data_end = dlme_end; 442 443 /* Check the DLME regions arguments. */ 444 if ((dlme_start % DRTM_PAGE_SIZE) != 0) { 445 ERROR("DRTM: argument DLME region is not " 446 DRTM_PAGE_SIZE_STR "-aligned\n"); 447 return INVALID_PARAMETERS; 448 } 449 450 if (!(dlme_start < dlme_end && 451 dlme_start <= dlme_img_start && dlme_img_start < dlme_img_end && 452 dlme_start <= dlme_data_start && dlme_data_start < dlme_data_end)) { 453 ERROR("DRTM: argument DLME region is discontiguous\n"); 454 return INVALID_PARAMETERS; 455 } 456 457 if (dlme_img_start < dlme_data_end && dlme_data_start < dlme_img_end) { 458 ERROR("DRTM: argument DLME regions overlap\n"); 459 return INVALID_PARAMETERS; 460 } 461 462 /* Check the DLME image region arguments. */ 463 if ((dlme_img_start % DRTM_PAGE_SIZE) != 0) { 464 ERROR("DRTM: argument DLME image region is not " 465 DRTM_PAGE_SIZE_STR "-aligned\n"); 466 return INVALID_PARAMETERS; 467 } 468 469 if (!(dlme_img_start <= dlme_img_ep && dlme_img_ep < dlme_img_end)) { 470 ERROR("DRTM: DLME entry point is outside of the DLME image region\n"); 471 return INVALID_PARAMETERS; 472 } 473 474 if ((dlme_img_ep % 4) != 0) { 475 ERROR("DRTM: DLME image entry point is not 4-byte-aligned\n"); 476 return INVALID_PARAMETERS; 477 } 478 479 /* Check the DLME data region arguments. */ 480 if ((dlme_data_start % DRTM_PAGE_SIZE) != 0) { 481 ERROR("DRTM: argument DLME data region is not " 482 DRTM_PAGE_SIZE_STR "-aligned\n"); 483 return INVALID_PARAMETERS; 484 } 485 486 if (dlme_data_end - dlme_data_start < dlme_data_min_size) { 487 ERROR("DRTM: argument DLME data region is short of %lu bytes\n", 488 dlme_data_min_size - (size_t)(dlme_data_end - dlme_data_start)); 489 return INVALID_PARAMETERS; 490 } 491 492 /* check DLME region (paddr + size) is within a NS address region */ 493 rc = plat_drtm_validate_ns_region(dlme_start, (size_t)a->dlme_size); 494 if (rc != 0) { 495 ERROR("DRTM: DLME region lies within secure memory\n"); 496 return INVALID_PARAMETERS; 497 } 498 499 /* Check the Normal World DCE region arguments. */ 500 if (a->dce_nwd_paddr != 0) { 501 uint32_t dce_nwd_start = a->dce_nwd_paddr; 502 uint32_t dce_nwd_end = dce_nwd_start + a->dce_nwd_size; 503 504 if (!(dce_nwd_start < dce_nwd_end)) { 505 ERROR("DRTM: argument Normal World DCE region is dicontiguous\n"); 506 return INVALID_PARAMETERS; 507 } 508 509 if (dce_nwd_start < dlme_end && dlme_start < dce_nwd_end) { 510 ERROR("DRTM: argument Normal World DCE regions overlap\n"); 511 return INVALID_PARAMETERS; 512 } 513 } 514 515 /* 516 * Map and sanitize the cache of data range passed by DCE Preamble. This 517 * is required to avoid / defend against racing with cache evictions 518 */ 519 va_mapping_size = ALIGNED_UP((dlme_end - dlme_start), DRTM_PAGE_SIZE); 520 rc = mmap_add_dynamic_region_alloc_va(dlme_start, &va_mapping, va_mapping_size, 521 MT_MEMORY | MT_NS | MT_RO | 522 MT_SHAREABILITY_ISH); 523 if (rc != 0) { 524 ERROR("DRTM: %s: mmap_add_dynamic_region_alloc_va() failed rc=%d\n", 525 __func__, rc); 526 return INTERNAL_ERROR; 527 } 528 flush_dcache_range(va_mapping, va_mapping_size); 529 530 rc = mmap_remove_dynamic_region(va_mapping, va_mapping_size); 531 if (rc) { 532 ERROR("%s(): mmap_remove_dynamic_region() failed unexpectedly" 533 " rc=%d\n", __func__, rc); 534 panic(); 535 } 536 537 *a_out = *a; 538 return SUCCESS; 539 } 540 541 static void drtm_dl_reset_dlme_el_state(enum drtm_dlme_el dlme_el) 542 { 543 uint64_t sctlr; 544 545 /* 546 * TODO: Set PE state according to the PSCI's specification of the initial 547 * state after CPU_ON, or to reset values if unspecified, where they exist, 548 * or define sensible values otherwise. 549 */ 550 551 switch (dlme_el) { 552 case DLME_AT_EL1: 553 sctlr = read_sctlr_el1(); 554 break; 555 556 case DLME_AT_EL2: 557 sctlr = read_sctlr_el2(); 558 break; 559 560 default: /* Not reached */ 561 ERROR("%s(): dlme_el has the unexpected value %d\n", 562 __func__, dlme_el); 563 panic(); 564 } 565 566 sctlr &= ~(/* Disable DLME's EL MMU, since the existing page-tables are untrusted. */ 567 SCTLR_M_BIT 568 | SCTLR_EE_BIT /* Little-endian data accesses. */ 569 | SCTLR_C_BIT /* disable data caching */ 570 | SCTLR_I_BIT /* disable instruction caching */ 571 ); 572 573 switch (dlme_el) { 574 case DLME_AT_EL1: 575 write_sctlr_el1(sctlr); 576 break; 577 578 case DLME_AT_EL2: 579 write_sctlr_el2(sctlr); 580 break; 581 } 582 } 583 584 static void drtm_dl_reset_dlme_context(enum drtm_dlme_el dlme_el) 585 { 586 void *ns_ctx = cm_get_context(NON_SECURE); 587 gp_regs_t *gpregs = get_gpregs_ctx(ns_ctx); 588 uint64_t spsr_el3 = read_ctx_reg(get_el3state_ctx(ns_ctx), CTX_SPSR_EL3); 589 590 /* Reset all gpregs, including SP_EL0. */ 591 memset(gpregs, 0, sizeof(*gpregs)); 592 593 /* Reset SP_ELx. */ 594 switch (dlme_el) { 595 case DLME_AT_EL1: 596 write_sp_el1(0); 597 break; 598 599 case DLME_AT_EL2: 600 write_sp_el2(0); 601 break; 602 } 603 604 /* 605 * DLME's async exceptions are masked to avoid a NWd attacker's timed 606 * interference with any state we established trust in or measured. 607 */ 608 spsr_el3 |= SPSR_DAIF_MASK << SPSR_DAIF_SHIFT; 609 610 write_ctx_reg(get_el3state_ctx(ns_ctx), CTX_SPSR_EL3, spsr_el3); 611 } 612 613 static void drtm_dl_prepare_eret_to_dlme(const struct_drtm_dl_args *args, enum drtm_dlme_el dlme_el) 614 { 615 void *ctx = cm_get_context(NON_SECURE); 616 uint64_t dlme_ep = DL_ARGS_GET_DLME_ENTRY_POINT(args); 617 uint64_t spsr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SPSR_EL3); 618 619 /* Next ERET is to the DLME's EL. */ 620 spsr_el3 &= ~(MODE_EL_MASK << MODE_EL_SHIFT); 621 switch (dlme_el) { 622 case DLME_AT_EL1: 623 spsr_el3 |= MODE_EL1 << MODE_EL_SHIFT; 624 break; 625 626 case DLME_AT_EL2: 627 spsr_el3 |= MODE_EL2 << MODE_EL_SHIFT; 628 break; 629 } 630 631 /* Next ERET is to the DLME entry point. */ 632 cm_set_elr_spsr_el3(NON_SECURE, dlme_ep, spsr_el3); 633 } 634 635 static uint64_t drtm_dynamic_launch(uint64_t x1, void *handle) 636 { 637 enum drtm_retc ret = SUCCESS; 638 enum drtm_retc dma_prot_ret; 639 struct_drtm_dl_args args; 640 /* DLME should be highest NS exception level */ 641 enum drtm_dlme_el dlme_el = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1; 642 643 /* Ensure that only boot PE is powered on */ 644 ret = drtm_dl_check_cores(); 645 if (ret != SUCCESS) { 646 SMC_RET1(handle, ret); 647 } 648 649 /* 650 * Ensure that execution state is AArch64 and the caller 651 * is highest non-secure exception level 652 */ 653 ret = drtm_dl_check_caller_el(handle); 654 if (ret != SUCCESS) { 655 SMC_RET1(handle, ret); 656 } 657 658 ret = drtm_dl_check_args(x1, &args); 659 if (ret != SUCCESS) { 660 SMC_RET1(handle, ret); 661 } 662 663 /* Ensure that there are no SDEI event registered */ 664 #if SDEI_SUPPORT 665 if (sdei_get_registered_event_count() != 0) { 666 SMC_RET1(handle, DENIED); 667 } 668 #endif /* SDEI_SUPPORT */ 669 670 /* 671 * Engage the DMA protections. The launch cannot proceed without the DMA 672 * protections due to potential TOC/TOU vulnerabilities w.r.t. the DLME 673 * region (and to the NWd DCE region). 674 */ 675 ret = drtm_dma_prot_engage(&args.dma_prot_args, 676 DL_ARGS_GET_DMA_PROT_TYPE(&args)); 677 if (ret != SUCCESS) { 678 SMC_RET1(handle, ret); 679 } 680 681 /* 682 * The DMA protection is now engaged. Note that any failure mode that 683 * returns an error to the DRTM-launch caller must now disengage DMA 684 * protections before returning to the caller. 685 */ 686 687 ret = drtm_take_measurements(&args); 688 if (ret != SUCCESS) { 689 goto err_undo_dma_prot; 690 } 691 692 ret = drtm_dl_prepare_dlme_data(&args); 693 if (ret != SUCCESS) { 694 goto err_undo_dma_prot; 695 } 696 697 /* 698 * Note that, at the time of writing, the DRTM spec allows a successful 699 * launch from NS-EL1 to return to a DLME in NS-EL2. The practical risk 700 * of a privilege escalation, e.g. due to a compromised hypervisor, is 701 * considered small enough not to warrant the specification of additional 702 * DRTM conduits that would be necessary to maintain OSs' abstraction from 703 * the presence of EL2 were the dynamic launch only be allowed from the 704 * highest NS EL. 705 */ 706 707 dlme_el = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1; 708 709 drtm_dl_reset_dlme_el_state(dlme_el); 710 drtm_dl_reset_dlme_context(dlme_el); 711 712 /* 713 * Setting the Generic Timer frequency is required before launching 714 * DLME and is already done for running CPU during PSCI setup. 715 */ 716 drtm_dl_prepare_eret_to_dlme(&args, dlme_el); 717 718 /* 719 * As per DRTM 1.0 spec table #30 invalidate the instruction cache 720 * before jumping to the DLME. This is required to defend against 721 * potentially-malicious cache contents. 722 */ 723 invalidate_icache_all(); 724 725 /* Return the DLME region's address in x0, and the DLME data offset in x1.*/ 726 SMC_RET2(handle, args.dlme_paddr, args.dlme_data_off); 727 728 err_undo_dma_prot: 729 dma_prot_ret = drtm_dma_prot_disengage(); 730 if (dma_prot_ret != SUCCESS) { 731 ERROR("%s(): drtm_dma_prot_disengage() failed unexpectedly" 732 " rc=%d\n", __func__, ret); 733 panic(); 734 } 735 736 SMC_RET1(handle, ret); 737 } 738 739 uint64_t drtm_smc_handler(uint32_t smc_fid, 740 uint64_t x1, 741 uint64_t x2, 742 uint64_t x3, 743 uint64_t x4, 744 void *cookie, 745 void *handle, 746 uint64_t flags) 747 { 748 /* Check that the SMC call is from the Normal World. */ 749 if (!is_caller_non_secure(flags)) { 750 SMC_RET1(handle, NOT_SUPPORTED); 751 } 752 753 switch (smc_fid) { 754 case ARM_DRTM_SVC_VERSION: 755 INFO("DRTM service handler: version\n"); 756 /* Return the version of current implementation */ 757 SMC_RET1(handle, ARM_DRTM_VERSION); 758 break; /* not reached */ 759 760 case ARM_DRTM_SVC_FEATURES: 761 if (((x1 >> ARM_DRTM_FUNC_SHIFT) & ARM_DRTM_FUNC_MASK) == 762 ARM_DRTM_FUNC_ID) { 763 /* Dispatch function-based queries. */ 764 switch (x1 & FUNCID_MASK) { 765 case ARM_DRTM_SVC_VERSION: 766 SMC_RET1(handle, SUCCESS); 767 break; /* not reached */ 768 769 case ARM_DRTM_SVC_FEATURES: 770 SMC_RET1(handle, SUCCESS); 771 break; /* not reached */ 772 773 case ARM_DRTM_SVC_UNPROTECT_MEM: 774 SMC_RET1(handle, SUCCESS); 775 break; /* not reached */ 776 777 case ARM_DRTM_SVC_DYNAMIC_LAUNCH: 778 SMC_RET1(handle, SUCCESS); 779 break; /* not reached */ 780 781 case ARM_DRTM_SVC_CLOSE_LOCALITY: 782 WARN("ARM_DRTM_SVC_CLOSE_LOCALITY feature %s", 783 "is not supported\n"); 784 SMC_RET1(handle, NOT_SUPPORTED); 785 break; /* not reached */ 786 787 case ARM_DRTM_SVC_GET_ERROR: 788 SMC_RET1(handle, SUCCESS); 789 break; /* not reached */ 790 791 case ARM_DRTM_SVC_SET_ERROR: 792 SMC_RET1(handle, SUCCESS); 793 break; /* not reached */ 794 795 case ARM_DRTM_SVC_SET_TCB_HASH: 796 WARN("ARM_DRTM_SVC_TCB_HASH feature %s", 797 "is not supported\n"); 798 SMC_RET1(handle, NOT_SUPPORTED); 799 break; /* not reached */ 800 801 case ARM_DRTM_SVC_LOCK_TCB_HASH: 802 WARN("ARM_DRTM_SVC_LOCK_TCB_HASH feature %s", 803 "is not supported\n"); 804 SMC_RET1(handle, NOT_SUPPORTED); 805 break; /* not reached */ 806 807 default: 808 ERROR("Unknown DRTM service function\n"); 809 SMC_RET1(handle, NOT_SUPPORTED); 810 break; /* not reached */ 811 } 812 } else { 813 /* Dispatch feature-based queries. */ 814 switch (x1 & ARM_DRTM_FEAT_ID_MASK) { 815 case ARM_DRTM_FEATURES_TPM: 816 INFO("++ DRTM service handler: TPM features\n"); 817 return drtm_features_tpm(handle); 818 break; /* not reached */ 819 820 case ARM_DRTM_FEATURES_MEM_REQ: 821 INFO("++ DRTM service handler: Min. mem." 822 " requirement features\n"); 823 return drtm_features_mem_req(handle); 824 break; /* not reached */ 825 826 case ARM_DRTM_FEATURES_DMA_PROT: 827 INFO("++ DRTM service handler: " 828 "DMA protection features\n"); 829 return drtm_features_dma_prot(handle); 830 break; /* not reached */ 831 832 case ARM_DRTM_FEATURES_BOOT_PE_ID: 833 INFO("++ DRTM service handler: " 834 "Boot PE ID features\n"); 835 return drtm_features_boot_pe_id(handle); 836 break; /* not reached */ 837 838 case ARM_DRTM_FEATURES_TCB_HASHES: 839 INFO("++ DRTM service handler: " 840 "TCB-hashes features\n"); 841 return drtm_features_tcb_hashes(handle); 842 break; /* not reached */ 843 844 case ARM_DRTM_FEATURES_DLME_IMG_AUTH: 845 INFO("++ DRTM service handler: " 846 "DLME Image authentication features\n"); 847 return drtm_features_dlme_img_auth_features(handle); 848 break; /* not reached */ 849 850 default: 851 ERROR("Unknown ARM DRTM service feature\n"); 852 SMC_RET1(handle, NOT_SUPPORTED); 853 break; /* not reached */ 854 } 855 } 856 857 case ARM_DRTM_SVC_UNPROTECT_MEM: 858 INFO("DRTM service handler: unprotect mem\n"); 859 return drtm_unprotect_mem(handle); 860 break; /* not reached */ 861 862 case ARM_DRTM_SVC_DYNAMIC_LAUNCH: 863 INFO("DRTM service handler: dynamic launch\n"); 864 return drtm_dynamic_launch(x1, handle); 865 break; /* not reached */ 866 867 case ARM_DRTM_SVC_CLOSE_LOCALITY: 868 WARN("DRTM service handler: close locality %s\n", 869 "is not supported"); 870 SMC_RET1(handle, NOT_SUPPORTED); 871 break; /* not reached */ 872 873 case ARM_DRTM_SVC_GET_ERROR: 874 INFO("DRTM service handler: get error\n"); 875 return drtm_get_error(handle); 876 break; /* not reached */ 877 878 case ARM_DRTM_SVC_SET_ERROR: 879 INFO("DRTM service handler: set error\n"); 880 return drtm_set_error(x1, handle); 881 break; /* not reached */ 882 883 case ARM_DRTM_SVC_SET_TCB_HASH: 884 WARN("DRTM service handler: set TCB hash %s\n", 885 "is not supported"); 886 SMC_RET1(handle, NOT_SUPPORTED); 887 break; /* not reached */ 888 889 case ARM_DRTM_SVC_LOCK_TCB_HASH: 890 WARN("DRTM service handler: lock TCB hash %s\n", 891 "is not supported"); 892 SMC_RET1(handle, NOT_SUPPORTED); 893 break; /* not reached */ 894 895 default: 896 ERROR("Unknown DRTM service function: 0x%x\n", smc_fid); 897 SMC_RET1(handle, SMC_UNK); 898 break; /* not reached */ 899 } 900 901 /* not reached */ 902 SMC_RET1(handle, SMC_UNK); 903 } 904