drtm_main.c 24 KB

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