1 /* 2 * Copyright (c) 2018-2023, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <arch.h> 8 #include <arch_helpers.h> 9 #include <assert.h> 10 #include <errno.h> 11 #include <lib/xlat_tables/xlat_tables_v2.h> 12 #include <platform_def.h> 13 #include <plat/common/platform.h> 14 #include <services/spm_mm_partition.h> 15 #include <services/spm_mm_svc.h> 16 17 #include "spm_mm_private.h" 18 #include "spm_shim_private.h" 19 20 /* Lock used for SP_MEMORY_ATTRIBUTES_GET and SP_MEMORY_ATTRIBUTES_SET */ 21 static spinlock_t mem_attr_smc_lock; 22 23 /* 24 * Attributes are encoded using a different format in the SMC interface than in 25 * the Trusted Firmware, where the mmap_attr_t enum type is used. This function 26 * converts an attributes value from the SMC format to the mmap_attr_t format by 27 * setting MT_RW/MT_RO, MT_USER/MT_PRIVILEGED and MT_EXECUTE/MT_EXECUTE_NEVER. 28 * The other fields are left as 0 because they are ignored by the function 29 * xlat_change_mem_attributes_ctx(). 30 */ 31 static unsigned int smc_attr_to_mmap_attr(unsigned int attributes) 32 { 33 unsigned int tf_attr = 0U; 34 35 unsigned int access = (attributes & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK) 36 >> MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT; 37 38 if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW) { 39 tf_attr |= MT_RW | MT_USER; 40 } else if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO) { 41 tf_attr |= MT_RO | MT_USER; 42 } else { 43 /* Other values are reserved. */ 44 assert(access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS); 45 /* The only requirement is that there's no access from EL0 */ 46 tf_attr |= MT_RO | MT_PRIVILEGED; 47 } 48 49 if ((attributes & MM_SP_MEMORY_ATTRIBUTES_NON_EXEC) == 0) { 50 tf_attr |= MT_EXECUTE; 51 } else { 52 tf_attr |= MT_EXECUTE_NEVER; 53 } 54 55 return tf_attr; 56 } 57 58 /* 59 * This function converts attributes from the Trusted Firmware format into the 60 * SMC interface format. 61 */ 62 static unsigned int smc_mmap_to_smc_attr(unsigned int attr) 63 { 64 unsigned int smc_attr = 0U; 65 66 unsigned int data_access; 67 68 if ((attr & MT_USER) == 0) { 69 /* No access from EL0. */ 70 data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS; 71 } else { 72 if ((attr & MT_RW) != 0) { 73 assert(MT_TYPE(attr) != MT_DEVICE); 74 data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW; 75 } else { 76 data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO; 77 } 78 } 79 80 smc_attr |= (data_access & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK) 81 << MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT; 82 83 if ((attr & MT_EXECUTE_NEVER) != 0U) { 84 smc_attr |= MM_SP_MEMORY_ATTRIBUTES_NON_EXEC; 85 } 86 87 return smc_attr; 88 } 89 90 int32_t spm_memory_attributes_get_smc_handler(sp_context_t *sp_ctx, 91 uintptr_t base_va) 92 { 93 uint32_t attributes; 94 95 spin_lock(&mem_attr_smc_lock); 96 97 int rc = xlat_get_mem_attributes_ctx(sp_ctx->xlat_ctx_handle, 98 base_va, &attributes); 99 100 spin_unlock(&mem_attr_smc_lock); 101 102 /* Convert error codes of xlat_get_mem_attributes_ctx() into SPM. */ 103 assert((rc == 0) || (rc == -EINVAL)); 104 105 if (rc == 0) { 106 return (int32_t) smc_mmap_to_smc_attr(attributes); 107 } else { 108 return SPM_MM_INVALID_PARAMETER; 109 } 110 } 111 112 int spm_memory_attributes_set_smc_handler(sp_context_t *sp_ctx, 113 u_register_t page_address, 114 u_register_t pages_count, 115 u_register_t smc_attributes) 116 { 117 uintptr_t base_va = (uintptr_t) page_address; 118 size_t size = (size_t) (pages_count * PAGE_SIZE); 119 uint32_t attributes = (uint32_t) smc_attributes; 120 121 INFO(" Start address : 0x%lx\n", base_va); 122 INFO(" Number of pages: %i (%zi bytes)\n", (int) pages_count, size); 123 INFO(" Attributes : 0x%x\n", attributes); 124 125 spin_lock(&mem_attr_smc_lock); 126 127 int ret = xlat_change_mem_attributes_ctx(sp_ctx->xlat_ctx_handle, 128 base_va, size, 129 smc_attr_to_mmap_attr(attributes)); 130 131 spin_unlock(&mem_attr_smc_lock); 132 133 /* Convert error codes of xlat_change_mem_attributes_ctx() into SPM. */ 134 assert((ret == 0) || (ret == -EINVAL)); 135 136 return (ret == 0) ? SPM_MM_SUCCESS : SPM_MM_INVALID_PARAMETER; 137 } 138