1 /* 2 * Copyright (c) 2022 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 38 /* DLME header */ 39 struct_dlme_data_header dlme_data_hdr_init; 40 41 /* Minimum data memory requirement */ 42 uint64_t dlme_data_min_size; 43 44 int drtm_setup(void) 45 { 46 bool rc; 47 const plat_drtm_tpm_features_t *plat_tpm_feat; 48 const plat_drtm_dma_prot_features_t *plat_dma_prot_feat; 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_impdef_region_size = 108 plat_drtm_get_imp_def_dlme_region_size(); 109 110 dlme_data_min_size += dlme_data_hdr_init.dlme_addr_map_size + 111 PLAT_DRTM_EVENT_LOG_MAX_SIZE + 112 dlme_data_hdr_init.dlme_tcb_hashes_table_size + 113 dlme_data_hdr_init.dlme_impdef_region_size; 114 115 dlme_data_min_size = page_align(dlme_data_min_size, UP)/PAGE_SIZE; 116 117 /* Fill out platform DRTM features structure */ 118 /* Only support default PCR schema (0x1) in this implementation. */ 119 ARM_DRTM_TPM_FEATURES_SET_PCR_SCHEMA(plat_drtm_features.tpm_features, 120 ARM_DRTM_TPM_FEATURES_PCR_SCHEMA_DEFAULT); 121 ARM_DRTM_TPM_FEATURES_SET_TPM_HASH(plat_drtm_features.tpm_features, 122 plat_tpm_feat->tpm_based_hash_support); 123 ARM_DRTM_TPM_FEATURES_SET_FW_HASH(plat_drtm_features.tpm_features, 124 plat_tpm_feat->firmware_hash_algorithm); 125 ARM_DRTM_MIN_MEM_REQ_SET_MIN_DLME_DATA_SIZE(plat_drtm_features.minimum_memory_requirement, 126 dlme_data_min_size); 127 ARM_DRTM_MIN_MEM_REQ_SET_DCE_SIZE(plat_drtm_features.minimum_memory_requirement, 128 plat_drtm_get_min_size_normal_world_dce()); 129 ARM_DRTM_DMA_PROT_FEATURES_SET_MAX_REGIONS(plat_drtm_features.dma_prot_features, 130 plat_dma_prot_feat->max_num_mem_prot_regions); 131 ARM_DRTM_DMA_PROT_FEATURES_SET_DMA_SUPPORT(plat_drtm_features.dma_prot_features, 132 plat_dma_prot_feat->dma_protection_support); 133 ARM_DRTM_TCB_HASH_FEATURES_SET_MAX_NUM_HASHES(plat_drtm_features.tcb_hash_features, 134 plat_drtm_get_tcb_hash_features()); 135 136 return 0; 137 } 138 139 static inline uint64_t drtm_features_tpm(void *ctx) 140 { 141 SMC_RET2(ctx, 1ULL, /* TPM feature is supported */ 142 plat_drtm_features.tpm_features); 143 } 144 145 static inline uint64_t drtm_features_mem_req(void *ctx) 146 { 147 SMC_RET2(ctx, 1ULL, /* memory req Feature is supported */ 148 plat_drtm_features.minimum_memory_requirement); 149 } 150 151 static inline uint64_t drtm_features_boot_pe_id(void *ctx) 152 { 153 SMC_RET2(ctx, 1ULL, /* Boot PE feature is supported */ 154 plat_drtm_features.boot_pe_id); 155 } 156 157 static inline uint64_t drtm_features_dma_prot(void *ctx) 158 { 159 SMC_RET2(ctx, 1ULL, /* DMA protection feature is supported */ 160 plat_drtm_features.dma_prot_features); 161 } 162 163 static inline uint64_t drtm_features_tcb_hashes(void *ctx) 164 { 165 SMC_RET2(ctx, 1ULL, /* TCB hash feature is supported */ 166 plat_drtm_features.tcb_hash_features); 167 } 168 169 static enum drtm_retc drtm_dl_check_caller_el(void *ctx) 170 { 171 uint64_t spsr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SPSR_EL3); 172 uint64_t dl_caller_el; 173 uint64_t dl_caller_aarch; 174 175 dl_caller_el = spsr_el3 >> MODE_EL_SHIFT & MODE_EL_MASK; 176 dl_caller_aarch = spsr_el3 >> MODE_RW_SHIFT & MODE_RW_MASK; 177 178 /* Caller's security state is checked from drtm_smc_handle function */ 179 180 /* Caller can be NS-EL2/EL1 */ 181 if (dl_caller_el == MODE_EL3) { 182 ERROR("DRTM: invalid launch from EL3\n"); 183 return DENIED; 184 } 185 186 if (dl_caller_aarch != MODE_RW_64) { 187 ERROR("DRTM: invalid launch from non-AArch64 execution state\n"); 188 return DENIED; 189 } 190 191 return SUCCESS; 192 } 193 194 static enum drtm_retc drtm_dl_check_cores(void) 195 { 196 bool running_on_single_core; 197 uint64_t this_pe_aff_value = read_mpidr_el1() & MPIDR_AFFINITY_MASK; 198 199 if (this_pe_aff_value != plat_drtm_features.boot_pe_id) { 200 ERROR("DRTM: invalid launch on a non-boot PE\n"); 201 return DENIED; 202 } 203 204 running_on_single_core = psci_is_last_on_cpu_safe(); 205 if (!running_on_single_core) { 206 ERROR("DRTM: invalid launch due to non-boot PE not being turned off\n"); 207 return DENIED; 208 } 209 210 return SUCCESS; 211 } 212 213 static enum drtm_retc drtm_dl_prepare_dlme_data(const struct_drtm_dl_args *args) 214 { 215 int rc; 216 uint64_t dlme_data_paddr; 217 size_t dlme_data_max_size; 218 uintptr_t dlme_data_mapping; 219 struct_dlme_data_header *dlme_data_hdr; 220 uint8_t *dlme_data_cursor; 221 size_t dlme_data_mapping_bytes; 222 size_t serialised_bytes_actual; 223 224 dlme_data_paddr = args->dlme_paddr + args->dlme_data_off; 225 dlme_data_max_size = args->dlme_size - args->dlme_data_off; 226 227 /* 228 * The capacity of the given DLME data region is checked when 229 * the other dynamic launch arguments are. 230 */ 231 if (dlme_data_max_size < dlme_data_min_size) { 232 ERROR("%s: assertion failed:" 233 " dlme_data_max_size (%ld) < dlme_data_total_bytes_req (%ld)\n", 234 __func__, dlme_data_max_size, dlme_data_min_size); 235 panic(); 236 } 237 238 /* Map the DLME data region as NS memory. */ 239 dlme_data_mapping_bytes = ALIGNED_UP(dlme_data_max_size, DRTM_PAGE_SIZE); 240 rc = mmap_add_dynamic_region_alloc_va(dlme_data_paddr, 241 &dlme_data_mapping, 242 dlme_data_mapping_bytes, 243 MT_RW_DATA | MT_NS | 244 MT_SHAREABILITY_ISH); 245 if (rc != 0) { 246 WARN("DRTM: %s: mmap_add_dynamic_region() failed rc=%d\n", 247 __func__, rc); 248 return INTERNAL_ERROR; 249 } 250 dlme_data_hdr = (struct_dlme_data_header *)dlme_data_mapping; 251 dlme_data_cursor = (uint8_t *)dlme_data_hdr + sizeof(*dlme_data_hdr); 252 253 memcpy(dlme_data_hdr, (const void *)&dlme_data_hdr_init, 254 sizeof(*dlme_data_hdr)); 255 256 /* Set the header version and size. */ 257 dlme_data_hdr->version = 1; 258 dlme_data_hdr->this_hdr_size = sizeof(*dlme_data_hdr); 259 260 /* Prepare DLME protected regions. */ 261 drtm_dma_prot_serialise_table(dlme_data_cursor, 262 &serialised_bytes_actual); 263 assert(serialised_bytes_actual == 264 dlme_data_hdr->dlme_prot_regions_size); 265 dlme_data_cursor += serialised_bytes_actual; 266 267 /* Prepare DLME address map. */ 268 if (plat_drtm_mem_map != NULL) { 269 memcpy(dlme_data_cursor, plat_drtm_mem_map, 270 dlme_data_hdr->dlme_addr_map_size); 271 } else { 272 WARN("DRTM: DLME address map is not in the cache\n"); 273 } 274 dlme_data_cursor += dlme_data_hdr->dlme_addr_map_size; 275 276 /* Prepare DRTM event log for DLME. */ 277 drtm_serialise_event_log(dlme_data_cursor, &serialised_bytes_actual); 278 assert(serialised_bytes_actual <= PLAT_DRTM_EVENT_LOG_MAX_SIZE); 279 dlme_data_hdr->dlme_tpm_log_size = serialised_bytes_actual; 280 dlme_data_cursor += serialised_bytes_actual; 281 282 /* 283 * TODO: Prepare the TCB hashes for DLME, currently its size 284 * 0 285 */ 286 dlme_data_cursor += dlme_data_hdr->dlme_tcb_hashes_table_size; 287 288 /* Implementation-specific region size is unused. */ 289 dlme_data_cursor += dlme_data_hdr->dlme_impdef_region_size; 290 291 /* 292 * Prepare DLME data size, includes all data region referenced above 293 * alongwith the DLME data header 294 */ 295 dlme_data_hdr->dlme_data_size = dlme_data_cursor - (uint8_t *)dlme_data_hdr; 296 297 /* Unmap the DLME data region. */ 298 rc = mmap_remove_dynamic_region(dlme_data_mapping, dlme_data_mapping_bytes); 299 if (rc != 0) { 300 ERROR("%s(): mmap_remove_dynamic_region() failed" 301 " unexpectedly rc=%d\n", __func__, rc); 302 panic(); 303 } 304 305 return SUCCESS; 306 } 307 308 /* 309 * Note: accesses to the dynamic launch args, and to the DLME data are 310 * little-endian as required, thanks to TF-A BL31 init requirements. 311 */ 312 static enum drtm_retc drtm_dl_check_args(uint64_t x1, 313 struct_drtm_dl_args *a_out) 314 { 315 uint64_t dlme_start, dlme_end; 316 uint64_t dlme_img_start, dlme_img_ep, dlme_img_end; 317 uint64_t dlme_data_start, dlme_data_end; 318 uintptr_t args_mapping; 319 size_t args_mapping_size; 320 struct_drtm_dl_args *a; 321 struct_drtm_dl_args args_buf; 322 int rc; 323 324 if (x1 % DRTM_PAGE_SIZE != 0) { 325 ERROR("DRTM: parameters structure is not " 326 DRTM_PAGE_SIZE_STR "-aligned\n"); 327 return INVALID_PARAMETERS; 328 } 329 330 args_mapping_size = ALIGNED_UP(sizeof(struct_drtm_dl_args), DRTM_PAGE_SIZE); 331 332 /* check DRTM parameters are within NS address region */ 333 rc = plat_drtm_validate_ns_region(x1, args_mapping_size); 334 if (rc != 0) { 335 ERROR("DRTM: parameters lies within secure memory\n"); 336 return INVALID_PARAMETERS; 337 } 338 339 rc = mmap_add_dynamic_region_alloc_va(x1, &args_mapping, args_mapping_size, 340 MT_MEMORY | MT_NS | MT_RO | 341 MT_SHAREABILITY_ISH); 342 if (rc != 0) { 343 WARN("DRTM: %s: mmap_add_dynamic_region() failed rc=%d\n", 344 __func__, rc); 345 return INTERNAL_ERROR; 346 } 347 a = (struct_drtm_dl_args *)args_mapping; 348 /* 349 * TODO: invalidate all data cache before reading the data passed by the 350 * DCE Preamble. This is required to avoid / defend against racing with 351 * cache evictions. 352 */ 353 args_buf = *a; 354 355 rc = mmap_remove_dynamic_region(args_mapping, args_mapping_size); 356 if (rc) { 357 ERROR("%s(): mmap_remove_dynamic_region() failed unexpectedly" 358 " rc=%d\n", __func__, rc); 359 panic(); 360 } 361 a = &args_buf; 362 363 if (a->version != 1) { 364 ERROR("DRTM: parameters structure incompatible with major version %d\n", 365 ARM_DRTM_VERSION_MAJOR); 366 return NOT_SUPPORTED; 367 } 368 369 if (!(a->dlme_img_off < a->dlme_size && 370 a->dlme_data_off < a->dlme_size)) { 371 ERROR("DRTM: argument offset is outside of the DLME region\n"); 372 return INVALID_PARAMETERS; 373 } 374 dlme_start = a->dlme_paddr; 375 dlme_end = a->dlme_paddr + a->dlme_size; 376 dlme_img_start = a->dlme_paddr + a->dlme_img_off; 377 dlme_img_ep = dlme_img_start + a->dlme_img_ep_off; 378 dlme_img_end = dlme_img_start + a->dlme_img_size; 379 dlme_data_start = a->dlme_paddr + a->dlme_data_off; 380 dlme_data_end = dlme_end; 381 382 /* Check the DLME regions arguments. */ 383 if ((dlme_start % DRTM_PAGE_SIZE) != 0) { 384 ERROR("DRTM: argument DLME region is not " 385 DRTM_PAGE_SIZE_STR "-aligned\n"); 386 return INVALID_PARAMETERS; 387 } 388 389 if (!(dlme_start < dlme_end && 390 dlme_start <= dlme_img_start && dlme_img_start < dlme_img_end && 391 dlme_start <= dlme_data_start && dlme_data_start < dlme_data_end)) { 392 ERROR("DRTM: argument DLME region is discontiguous\n"); 393 return INVALID_PARAMETERS; 394 } 395 396 if (dlme_img_start < dlme_data_end && dlme_data_start < dlme_img_end) { 397 ERROR("DRTM: argument DLME regions overlap\n"); 398 return INVALID_PARAMETERS; 399 } 400 401 /* Check the DLME image region arguments. */ 402 if ((dlme_img_start % DRTM_PAGE_SIZE) != 0) { 403 ERROR("DRTM: argument DLME image region is not " 404 DRTM_PAGE_SIZE_STR "-aligned\n"); 405 return INVALID_PARAMETERS; 406 } 407 408 if (!(dlme_img_start <= dlme_img_ep && dlme_img_ep < dlme_img_end)) { 409 ERROR("DRTM: DLME entry point is outside of the DLME image region\n"); 410 return INVALID_PARAMETERS; 411 } 412 413 if ((dlme_img_ep % 4) != 0) { 414 ERROR("DRTM: DLME image entry point is not 4-byte-aligned\n"); 415 return INVALID_PARAMETERS; 416 } 417 418 /* Check the DLME data region arguments. */ 419 if ((dlme_data_start % DRTM_PAGE_SIZE) != 0) { 420 ERROR("DRTM: argument DLME data region is not " 421 DRTM_PAGE_SIZE_STR "-aligned\n"); 422 return INVALID_PARAMETERS; 423 } 424 425 if (dlme_data_end - dlme_data_start < dlme_data_min_size) { 426 ERROR("DRTM: argument DLME data region is short of %lu bytes\n", 427 dlme_data_min_size - (size_t)(dlme_data_end - dlme_data_start)); 428 return INVALID_PARAMETERS; 429 } 430 431 /* check DLME region (paddr + size) is within a NS address region */ 432 rc = plat_drtm_validate_ns_region(dlme_start, (size_t)a->dlme_size); 433 if (rc != 0) { 434 ERROR("DRTM: DLME region lies within secure memory\n"); 435 return INVALID_PARAMETERS; 436 } 437 438 /* Check the Normal World DCE region arguments. */ 439 if (a->dce_nwd_paddr != 0) { 440 uint32_t dce_nwd_start = a->dce_nwd_paddr; 441 uint32_t dce_nwd_end = dce_nwd_start + a->dce_nwd_size; 442 443 if (!(dce_nwd_start < dce_nwd_end)) { 444 ERROR("DRTM: argument Normal World DCE region is dicontiguous\n"); 445 return INVALID_PARAMETERS; 446 } 447 448 if (dce_nwd_start < dlme_end && dlme_start < dce_nwd_end) { 449 ERROR("DRTM: argument Normal World DCE regions overlap\n"); 450 return INVALID_PARAMETERS; 451 } 452 } 453 454 *a_out = *a; 455 return SUCCESS; 456 } 457 458 static void drtm_dl_reset_dlme_el_state(enum drtm_dlme_el dlme_el) 459 { 460 uint64_t sctlr; 461 462 /* 463 * TODO: Set PE state according to the PSCI's specification of the initial 464 * state after CPU_ON, or to reset values if unspecified, where they exist, 465 * or define sensible values otherwise. 466 */ 467 468 switch (dlme_el) { 469 case DLME_AT_EL1: 470 sctlr = read_sctlr_el1(); 471 break; 472 473 case DLME_AT_EL2: 474 sctlr = read_sctlr_el2(); 475 break; 476 477 default: /* Not reached */ 478 ERROR("%s(): dlme_el has the unexpected value %d\n", 479 __func__, dlme_el); 480 panic(); 481 } 482 483 sctlr &= ~(/* Disable DLME's EL MMU, since the existing page-tables are untrusted. */ 484 SCTLR_M_BIT 485 | SCTLR_EE_BIT /* Little-endian data accesses. */ 486 ); 487 488 sctlr |= SCTLR_C_BIT | SCTLR_I_BIT; /* Allow instruction and data caching. */ 489 490 switch (dlme_el) { 491 case DLME_AT_EL1: 492 write_sctlr_el1(sctlr); 493 break; 494 495 case DLME_AT_EL2: 496 write_sctlr_el2(sctlr); 497 break; 498 } 499 } 500 501 static void drtm_dl_reset_dlme_context(enum drtm_dlme_el dlme_el) 502 { 503 void *ns_ctx = cm_get_context(NON_SECURE); 504 gp_regs_t *gpregs = get_gpregs_ctx(ns_ctx); 505 uint64_t spsr_el3 = read_ctx_reg(get_el3state_ctx(ns_ctx), CTX_SPSR_EL3); 506 507 /* Reset all gpregs, including SP_EL0. */ 508 memset(gpregs, 0, sizeof(*gpregs)); 509 510 /* Reset SP_ELx. */ 511 switch (dlme_el) { 512 case DLME_AT_EL1: 513 write_sp_el1(0); 514 break; 515 516 case DLME_AT_EL2: 517 write_sp_el2(0); 518 break; 519 } 520 521 /* 522 * DLME's async exceptions are masked to avoid a NWd attacker's timed 523 * interference with any state we established trust in or measured. 524 */ 525 spsr_el3 |= SPSR_DAIF_MASK << SPSR_DAIF_SHIFT; 526 527 write_ctx_reg(get_el3state_ctx(ns_ctx), CTX_SPSR_EL3, spsr_el3); 528 } 529 530 static void drtm_dl_prepare_eret_to_dlme(const struct_drtm_dl_args *args, enum drtm_dlme_el dlme_el) 531 { 532 void *ctx = cm_get_context(NON_SECURE); 533 uint64_t dlme_ep = DL_ARGS_GET_DLME_ENTRY_POINT(args); 534 uint64_t spsr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SPSR_EL3); 535 536 /* Next ERET is to the DLME's EL. */ 537 spsr_el3 &= ~(MODE_EL_MASK << MODE_EL_SHIFT); 538 switch (dlme_el) { 539 case DLME_AT_EL1: 540 spsr_el3 |= MODE_EL1 << MODE_EL_SHIFT; 541 break; 542 543 case DLME_AT_EL2: 544 spsr_el3 |= MODE_EL2 << MODE_EL_SHIFT; 545 break; 546 } 547 548 /* Next ERET is to the DLME entry point. */ 549 cm_set_elr_spsr_el3(NON_SECURE, dlme_ep, spsr_el3); 550 } 551 552 static uint64_t drtm_dynamic_launch(uint64_t x1, void *handle) 553 { 554 enum drtm_retc ret = SUCCESS; 555 enum drtm_retc dma_prot_ret; 556 struct_drtm_dl_args args; 557 /* DLME should be highest NS exception level */ 558 enum drtm_dlme_el dlme_el = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1; 559 560 /* Ensure that only boot PE is powered on */ 561 ret = drtm_dl_check_cores(); 562 if (ret != SUCCESS) { 563 SMC_RET1(handle, ret); 564 } 565 566 /* 567 * Ensure that execution state is AArch64 and the caller 568 * is highest non-secure exception level 569 */ 570 ret = drtm_dl_check_caller_el(handle); 571 if (ret != SUCCESS) { 572 SMC_RET1(handle, ret); 573 } 574 575 ret = drtm_dl_check_args(x1, &args); 576 if (ret != SUCCESS) { 577 SMC_RET1(handle, ret); 578 } 579 580 /* Ensure that there are no SDEI event registered */ 581 #if SDEI_SUPPORT 582 if (sdei_get_registered_event_count() != 0) { 583 SMC_RET1(handle, DENIED); 584 } 585 #endif /* SDEI_SUPPORT */ 586 587 /* 588 * Engage the DMA protections. The launch cannot proceed without the DMA 589 * protections due to potential TOC/TOU vulnerabilities w.r.t. the DLME 590 * region (and to the NWd DCE region). 591 */ 592 ret = drtm_dma_prot_engage(&args.dma_prot_args, 593 DL_ARGS_GET_DMA_PROT_TYPE(&args)); 594 if (ret != SUCCESS) { 595 SMC_RET1(handle, ret); 596 } 597 598 /* 599 * The DMA protection is now engaged. Note that any failure mode that 600 * returns an error to the DRTM-launch caller must now disengage DMA 601 * protections before returning to the caller. 602 */ 603 604 ret = drtm_take_measurements(&args); 605 if (ret != SUCCESS) { 606 goto err_undo_dma_prot; 607 } 608 609 ret = drtm_dl_prepare_dlme_data(&args); 610 if (ret != SUCCESS) { 611 goto err_undo_dma_prot; 612 } 613 614 /* 615 * Note that, at the time of writing, the DRTM spec allows a successful 616 * launch from NS-EL1 to return to a DLME in NS-EL2. The practical risk 617 * of a privilege escalation, e.g. due to a compromised hypervisor, is 618 * considered small enough not to warrant the specification of additional 619 * DRTM conduits that would be necessary to maintain OSs' abstraction from 620 * the presence of EL2 were the dynamic launch only be allowed from the 621 * highest NS EL. 622 */ 623 624 dlme_el = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1; 625 626 drtm_dl_reset_dlme_el_state(dlme_el); 627 drtm_dl_reset_dlme_context(dlme_el); 628 629 drtm_dl_prepare_eret_to_dlme(&args, dlme_el); 630 631 /* 632 * TODO: invalidate the instruction cache before jumping to the DLME. 633 * This is required to defend against potentially-malicious cache contents. 634 */ 635 636 /* Return the DLME region's address in x0, and the DLME data offset in x1.*/ 637 SMC_RET2(handle, args.dlme_paddr, args.dlme_data_off); 638 639 err_undo_dma_prot: 640 dma_prot_ret = drtm_dma_prot_disengage(); 641 if (dma_prot_ret != SUCCESS) { 642 ERROR("%s(): drtm_dma_prot_disengage() failed unexpectedly" 643 " rc=%d\n", __func__, ret); 644 panic(); 645 } 646 647 SMC_RET1(handle, ret); 648 } 649 650 uint64_t drtm_smc_handler(uint32_t smc_fid, 651 uint64_t x1, 652 uint64_t x2, 653 uint64_t x3, 654 uint64_t x4, 655 void *cookie, 656 void *handle, 657 uint64_t flags) 658 { 659 /* Check that the SMC call is from the Normal World. */ 660 if (!is_caller_non_secure(flags)) { 661 SMC_RET1(handle, NOT_SUPPORTED); 662 } 663 664 switch (smc_fid) { 665 case ARM_DRTM_SVC_VERSION: 666 INFO("DRTM service handler: version\n"); 667 /* Return the version of current implementation */ 668 SMC_RET1(handle, ARM_DRTM_VERSION); 669 break; /* not reached */ 670 671 case ARM_DRTM_SVC_FEATURES: 672 if (((x1 >> ARM_DRTM_FUNC_SHIFT) & ARM_DRTM_FUNC_MASK) == 673 ARM_DRTM_FUNC_ID) { 674 /* Dispatch function-based queries. */ 675 switch (x1 & FUNCID_MASK) { 676 case ARM_DRTM_SVC_VERSION: 677 SMC_RET1(handle, SUCCESS); 678 break; /* not reached */ 679 680 case ARM_DRTM_SVC_FEATURES: 681 SMC_RET1(handle, SUCCESS); 682 break; /* not reached */ 683 684 case ARM_DRTM_SVC_UNPROTECT_MEM: 685 SMC_RET1(handle, SUCCESS); 686 break; /* not reached */ 687 688 case ARM_DRTM_SVC_DYNAMIC_LAUNCH: 689 SMC_RET1(handle, SUCCESS); 690 break; /* not reached */ 691 692 case ARM_DRTM_SVC_CLOSE_LOCALITY: 693 WARN("ARM_DRTM_SVC_CLOSE_LOCALITY feature %s", 694 "is not supported\n"); 695 SMC_RET1(handle, NOT_SUPPORTED); 696 break; /* not reached */ 697 698 case ARM_DRTM_SVC_GET_ERROR: 699 SMC_RET1(handle, SUCCESS); 700 break; /* not reached */ 701 702 case ARM_DRTM_SVC_SET_ERROR: 703 SMC_RET1(handle, SUCCESS); 704 break; /* not reached */ 705 706 case ARM_DRTM_SVC_SET_TCB_HASH: 707 WARN("ARM_DRTM_SVC_TCB_HASH feature %s", 708 "is not supported\n"); 709 SMC_RET1(handle, NOT_SUPPORTED); 710 break; /* not reached */ 711 712 case ARM_DRTM_SVC_LOCK_TCB_HASH: 713 WARN("ARM_DRTM_SVC_LOCK_TCB_HASH feature %s", 714 "is not supported\n"); 715 SMC_RET1(handle, NOT_SUPPORTED); 716 break; /* not reached */ 717 718 default: 719 ERROR("Unknown DRTM service function\n"); 720 SMC_RET1(handle, NOT_SUPPORTED); 721 break; /* not reached */ 722 } 723 } else { 724 /* Dispatch feature-based queries. */ 725 switch (x1 & ARM_DRTM_FEAT_ID_MASK) { 726 case ARM_DRTM_FEATURES_TPM: 727 INFO("++ DRTM service handler: TPM features\n"); 728 return drtm_features_tpm(handle); 729 break; /* not reached */ 730 731 case ARM_DRTM_FEATURES_MEM_REQ: 732 INFO("++ DRTM service handler: Min. mem." 733 " requirement features\n"); 734 return drtm_features_mem_req(handle); 735 break; /* not reached */ 736 737 case ARM_DRTM_FEATURES_DMA_PROT: 738 INFO("++ DRTM service handler: " 739 "DMA protection features\n"); 740 return drtm_features_dma_prot(handle); 741 break; /* not reached */ 742 743 case ARM_DRTM_FEATURES_BOOT_PE_ID: 744 INFO("++ DRTM service handler: " 745 "Boot PE ID features\n"); 746 return drtm_features_boot_pe_id(handle); 747 break; /* not reached */ 748 749 case ARM_DRTM_FEATURES_TCB_HASHES: 750 INFO("++ DRTM service handler: " 751 "TCB-hashes features\n"); 752 return drtm_features_tcb_hashes(handle); 753 break; /* not reached */ 754 755 default: 756 ERROR("Unknown ARM DRTM service feature\n"); 757 SMC_RET1(handle, NOT_SUPPORTED); 758 break; /* not reached */ 759 } 760 } 761 762 case ARM_DRTM_SVC_UNPROTECT_MEM: 763 INFO("DRTM service handler: unprotect mem\n"); 764 return drtm_unprotect_mem(handle); 765 break; /* not reached */ 766 767 case ARM_DRTM_SVC_DYNAMIC_LAUNCH: 768 INFO("DRTM service handler: dynamic launch\n"); 769 return drtm_dynamic_launch(x1, handle); 770 break; /* not reached */ 771 772 case ARM_DRTM_SVC_CLOSE_LOCALITY: 773 WARN("DRTM service handler: close locality %s\n", 774 "is not supported"); 775 SMC_RET1(handle, NOT_SUPPORTED); 776 break; /* not reached */ 777 778 case ARM_DRTM_SVC_GET_ERROR: 779 INFO("DRTM service handler: get error\n"); 780 drtm_get_error(handle); 781 break; /* not reached */ 782 783 case ARM_DRTM_SVC_SET_ERROR: 784 INFO("DRTM service handler: set error\n"); 785 drtm_set_error(x1, handle); 786 break; /* not reached */ 787 788 case ARM_DRTM_SVC_SET_TCB_HASH: 789 WARN("DRTM service handler: set TCB hash %s\n", 790 "is not supported"); 791 SMC_RET1(handle, NOT_SUPPORTED); 792 break; /* not reached */ 793 794 case ARM_DRTM_SVC_LOCK_TCB_HASH: 795 WARN("DRTM service handler: lock TCB hash %s\n", 796 "is not supported"); 797 SMC_RET1(handle, NOT_SUPPORTED); 798 break; /* not reached */ 799 800 default: 801 ERROR("Unknown DRTM service function: 0x%x\n", smc_fid); 802 SMC_RET1(handle, SMC_UNK); 803 break; /* not reached */ 804 } 805 806 /* not reached */ 807 SMC_RET1(handle, SMC_UNK); 808 } 809