Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 1 | /* |
Marc Bonnici | 758bd24 | 2021-12-19 21:37:50 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2022, ARM Limited and Contributors. All rights reserved. |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 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> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | #include <lib/xlat_tables/xlat_tables_v2.h> |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 12 | #include <platform_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 13 | #include <plat/common/platform.h> |
Paul Beesley | 45f4028 | 2019-10-15 10:57:42 +0000 | [diff] [blame] | 14 | #include <services/spm_mm_partition.h> |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 15 | #include <services/spm_mm_svc.h> |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 16 | |
Paul Beesley | 15a96a9 | 2019-10-15 14:23:23 +0000 | [diff] [blame] | 17 | #include "spm_mm_private.h" |
Paul Beesley | a24ed07 | 2019-10-15 16:11:46 +0000 | [diff] [blame] | 18 | #include "spm_mm_shim_private.h" |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 19 | |
| 20 | /* Place translation tables by default along with the ones used by BL31. */ |
| 21 | #ifndef PLAT_SP_IMAGE_XLAT_SECTION_NAME |
| 22 | #define PLAT_SP_IMAGE_XLAT_SECTION_NAME "xlat_table" |
| 23 | #endif |
Masahisa Kojima | 813b50b | 2020-06-02 05:54:13 +0900 | [diff] [blame] | 24 | #ifndef PLAT_SP_IMAGE_BASE_XLAT_SECTION_NAME |
| 25 | #define PLAT_SP_IMAGE_BASE_XLAT_SECTION_NAME ".bss" |
| 26 | #endif |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 27 | |
| 28 | /* Allocate and initialise the translation context for the secure partitions. */ |
| 29 | REGISTER_XLAT_CONTEXT2(sp, |
| 30 | PLAT_SP_IMAGE_MMAP_REGIONS, |
| 31 | PLAT_SP_IMAGE_MAX_XLAT_TABLES, |
| 32 | PLAT_VIRT_ADDR_SPACE_SIZE, PLAT_PHY_ADDR_SPACE_SIZE, |
Masahisa Kojima | 813b50b | 2020-06-02 05:54:13 +0900 | [diff] [blame] | 33 | EL1_EL0_REGIME, PLAT_SP_IMAGE_XLAT_SECTION_NAME, |
| 34 | PLAT_SP_IMAGE_BASE_XLAT_SECTION_NAME); |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 35 | |
| 36 | /* Lock used for SP_MEMORY_ATTRIBUTES_GET and SP_MEMORY_ATTRIBUTES_SET */ |
| 37 | static spinlock_t mem_attr_smc_lock; |
| 38 | |
| 39 | /* Get handle of Secure Partition translation context */ |
| 40 | xlat_ctx_t *spm_get_sp_xlat_context(void) |
| 41 | { |
| 42 | return &sp_xlat_ctx; |
| 43 | }; |
| 44 | |
| 45 | /* |
| 46 | * Attributes are encoded using a different format in the SMC interface than in |
| 47 | * the Trusted Firmware, where the mmap_attr_t enum type is used. This function |
| 48 | * converts an attributes value from the SMC format to the mmap_attr_t format by |
| 49 | * setting MT_RW/MT_RO, MT_USER/MT_PRIVILEGED and MT_EXECUTE/MT_EXECUTE_NEVER. |
| 50 | * The other fields are left as 0 because they are ignored by the function |
| 51 | * xlat_change_mem_attributes_ctx(). |
| 52 | */ |
| 53 | static unsigned int smc_attr_to_mmap_attr(unsigned int attributes) |
| 54 | { |
| 55 | unsigned int tf_attr = 0U; |
| 56 | |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 57 | unsigned int access = (attributes & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK) |
| 58 | >> MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 59 | |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 60 | if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW) { |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 61 | tf_attr |= MT_RW | MT_USER; |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 62 | } else if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO) { |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 63 | tf_attr |= MT_RO | MT_USER; |
| 64 | } else { |
| 65 | /* Other values are reserved. */ |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 66 | assert(access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS); |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 67 | /* The only requirement is that there's no access from EL0 */ |
| 68 | tf_attr |= MT_RO | MT_PRIVILEGED; |
| 69 | } |
| 70 | |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 71 | if ((attributes & MM_SP_MEMORY_ATTRIBUTES_NON_EXEC) == 0) { |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 72 | tf_attr |= MT_EXECUTE; |
| 73 | } else { |
| 74 | tf_attr |= MT_EXECUTE_NEVER; |
| 75 | } |
| 76 | |
| 77 | return tf_attr; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * This function converts attributes from the Trusted Firmware format into the |
| 82 | * SMC interface format. |
| 83 | */ |
| 84 | static unsigned int smc_mmap_to_smc_attr(unsigned int attr) |
| 85 | { |
| 86 | unsigned int smc_attr = 0U; |
| 87 | |
| 88 | unsigned int data_access; |
| 89 | |
| 90 | if ((attr & MT_USER) == 0) { |
| 91 | /* No access from EL0. */ |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 92 | data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 93 | } else { |
| 94 | if ((attr & MT_RW) != 0) { |
| 95 | assert(MT_TYPE(attr) != MT_DEVICE); |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 96 | data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 97 | } else { |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 98 | data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 102 | smc_attr |= (data_access & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK) |
| 103 | << MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 104 | |
| 105 | if ((attr & MT_EXECUTE_NEVER) != 0U) { |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 106 | smc_attr |= MM_SP_MEMORY_ATTRIBUTES_NON_EXEC; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | return smc_attr; |
| 110 | } |
| 111 | |
| 112 | int32_t spm_memory_attributes_get_smc_handler(sp_context_t *sp_ctx, |
| 113 | uintptr_t base_va) |
| 114 | { |
| 115 | uint32_t attributes; |
| 116 | |
| 117 | spin_lock(&mem_attr_smc_lock); |
| 118 | |
| 119 | int rc = xlat_get_mem_attributes_ctx(sp_ctx->xlat_ctx_handle, |
| 120 | base_va, &attributes); |
| 121 | |
| 122 | spin_unlock(&mem_attr_smc_lock); |
| 123 | |
| 124 | /* Convert error codes of xlat_get_mem_attributes_ctx() into SPM. */ |
| 125 | assert((rc == 0) || (rc == -EINVAL)); |
| 126 | |
| 127 | if (rc == 0) { |
| 128 | return (int32_t) smc_mmap_to_smc_attr(attributes); |
| 129 | } else { |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 130 | return SPM_MM_INVALID_PARAMETER; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | int spm_memory_attributes_set_smc_handler(sp_context_t *sp_ctx, |
| 135 | u_register_t page_address, |
| 136 | u_register_t pages_count, |
| 137 | u_register_t smc_attributes) |
| 138 | { |
| 139 | uintptr_t base_va = (uintptr_t) page_address; |
| 140 | size_t size = (size_t) (pages_count * PAGE_SIZE); |
| 141 | uint32_t attributes = (uint32_t) smc_attributes; |
| 142 | |
| 143 | INFO(" Start address : 0x%lx\n", base_va); |
| 144 | INFO(" Number of pages: %i (%zi bytes)\n", (int) pages_count, size); |
| 145 | INFO(" Attributes : 0x%x\n", attributes); |
| 146 | |
| 147 | spin_lock(&mem_attr_smc_lock); |
| 148 | |
| 149 | int ret = xlat_change_mem_attributes_ctx(sp_ctx->xlat_ctx_handle, |
| 150 | base_va, size, |
| 151 | smc_attr_to_mmap_attr(attributes)); |
| 152 | |
| 153 | spin_unlock(&mem_attr_smc_lock); |
| 154 | |
| 155 | /* Convert error codes of xlat_change_mem_attributes_ctx() into SPM. */ |
| 156 | assert((ret == 0) || (ret == -EINVAL)); |
| 157 | |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 158 | return (ret == 0) ? SPM_MM_SUCCESS : SPM_MM_INVALID_PARAMETER; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 159 | } |