Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 1 | /* |
Nishant Sharma | 09aebce | 2022-03-25 23:03:31 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2023, 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" |
Nishant Sharma | 09aebce | 2022-03-25 23:03:31 +0000 | [diff] [blame] | 18 | #include "spm_shim_private.h" |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 19 | |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 20 | /* Lock used for SP_MEMORY_ATTRIBUTES_GET and SP_MEMORY_ATTRIBUTES_SET */ |
| 21 | static spinlock_t mem_attr_smc_lock; |
| 22 | |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 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 | |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 35 | unsigned int access = (attributes & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK) |
| 36 | >> MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 37 | |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 38 | if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW) { |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 39 | tf_attr |= MT_RW | MT_USER; |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 40 | } else if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO) { |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 41 | tf_attr |= MT_RO | MT_USER; |
| 42 | } else { |
| 43 | /* Other values are reserved. */ |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 44 | assert(access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS); |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 45 | /* The only requirement is that there's no access from EL0 */ |
| 46 | tf_attr |= MT_RO | MT_PRIVILEGED; |
| 47 | } |
| 48 | |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 49 | if ((attributes & MM_SP_MEMORY_ATTRIBUTES_NON_EXEC) == 0) { |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 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. */ |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 70 | data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 71 | } else { |
| 72 | if ((attr & MT_RW) != 0) { |
| 73 | assert(MT_TYPE(attr) != MT_DEVICE); |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 74 | data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 75 | } else { |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 76 | data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 80 | smc_attr |= (data_access & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK) |
| 81 | << MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 82 | |
| 83 | if ((attr & MT_EXECUTE_NEVER) != 0U) { |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 84 | smc_attr |= MM_SP_MEMORY_ATTRIBUTES_NON_EXEC; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 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 { |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 108 | return SPM_MM_INVALID_PARAMETER; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 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 | |
Paul Beesley | e9754e6 | 2019-10-15 12:51:55 +0000 | [diff] [blame] | 136 | return (ret == 0) ? SPM_MM_SUCCESS : SPM_MM_INVALID_PARAMETER; |
Antonio Nino Diaz | 8cd7ea3 | 2018-10-30 11:08:08 +0000 | [diff] [blame] | 137 | } |