2 * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
4 * SPDX-License-Identifier: BSD-3-Clause
8 #include <arch_helpers.h>
11 #include <platform_def.h>
13 #include <secure_partition.h>
15 #include <xlat_tables_v2.h>
17 #include "spm_private.h"
18 #include "spm_shim_private.h"
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"
25 /* Allocate and initialise the translation context for the secure partitions. */
26 REGISTER_XLAT_CONTEXT2(sp
,
27 PLAT_SP_IMAGE_MMAP_REGIONS
,
28 PLAT_SP_IMAGE_MAX_XLAT_TABLES
,
29 PLAT_VIRT_ADDR_SPACE_SIZE
, PLAT_PHY_ADDR_SPACE_SIZE
,
30 EL1_EL0_REGIME
, PLAT_SP_IMAGE_XLAT_SECTION_NAME
);
32 /* Lock used for SP_MEMORY_ATTRIBUTES_GET and SP_MEMORY_ATTRIBUTES_SET */
33 static spinlock_t mem_attr_smc_lock
;
35 /* Get handle of Secure Partition translation context */
36 xlat_ctx_t
*spm_get_sp_xlat_context(void)
42 * Attributes are encoded using a different format in the SMC interface than in
43 * the Trusted Firmware, where the mmap_attr_t enum type is used. This function
44 * converts an attributes value from the SMC format to the mmap_attr_t format by
45 * setting MT_RW/MT_RO, MT_USER/MT_PRIVILEGED and MT_EXECUTE/MT_EXECUTE_NEVER.
46 * The other fields are left as 0 because they are ignored by the function
47 * xlat_change_mem_attributes_ctx().
49 static unsigned int smc_attr_to_mmap_attr(unsigned int attributes
)
51 unsigned int tf_attr
= 0U;
53 unsigned int access
= (attributes
& SP_MEMORY_ATTRIBUTES_ACCESS_MASK
)
54 >> SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT
;
56 if (access
== SP_MEMORY_ATTRIBUTES_ACCESS_RW
) {
57 tf_attr
|= MT_RW
| MT_USER
;
58 } else if (access
== SP_MEMORY_ATTRIBUTES_ACCESS_RO
) {
59 tf_attr
|= MT_RO
| MT_USER
;
61 /* Other values are reserved. */
62 assert(access
== SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS
);
63 /* The only requirement is that there's no access from EL0 */
64 tf_attr
|= MT_RO
| MT_PRIVILEGED
;
67 if ((attributes
& SP_MEMORY_ATTRIBUTES_NON_EXEC
) == 0) {
68 tf_attr
|= MT_EXECUTE
;
70 tf_attr
|= MT_EXECUTE_NEVER
;
77 * This function converts attributes from the Trusted Firmware format into the
78 * SMC interface format.
80 static unsigned int smc_mmap_to_smc_attr(unsigned int attr
)
82 unsigned int smc_attr
= 0U;
84 unsigned int data_access
;
86 if ((attr
& MT_USER
) == 0) {
87 /* No access from EL0. */
88 data_access
= SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS
;
90 if ((attr
& MT_RW
) != 0) {
91 assert(MT_TYPE(attr
) != MT_DEVICE
);
92 data_access
= SP_MEMORY_ATTRIBUTES_ACCESS_RW
;
94 data_access
= SP_MEMORY_ATTRIBUTES_ACCESS_RO
;
98 smc_attr
|= (data_access
& SP_MEMORY_ATTRIBUTES_ACCESS_MASK
)
99 << SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT
;
101 if ((attr
& MT_EXECUTE_NEVER
) != 0U) {
102 smc_attr
|= SP_MEMORY_ATTRIBUTES_NON_EXEC
;
108 int32_t spm_memory_attributes_get_smc_handler(sp_context_t
*sp_ctx
,
113 spin_lock(&mem_attr_smc_lock
);
115 int rc
= xlat_get_mem_attributes_ctx(sp_ctx
->xlat_ctx_handle
,
116 base_va
, &attributes
);
118 spin_unlock(&mem_attr_smc_lock
);
120 /* Convert error codes of xlat_get_mem_attributes_ctx() into SPM. */
121 assert((rc
== 0) || (rc
== -EINVAL
));
124 return (int32_t) smc_mmap_to_smc_attr(attributes
);
126 return SPM_INVALID_PARAMETER
;
130 int spm_memory_attributes_set_smc_handler(sp_context_t
*sp_ctx
,
131 u_register_t page_address
,
132 u_register_t pages_count
,
133 u_register_t smc_attributes
)
135 uintptr_t base_va
= (uintptr_t) page_address
;
136 size_t size
= (size_t) (pages_count
* PAGE_SIZE
);
137 uint32_t attributes
= (uint32_t) smc_attributes
;
139 INFO(" Start address : 0x%lx\n", base_va
);
140 INFO(" Number of pages: %i (%zi bytes)\n", (int) pages_count
, size
);
141 INFO(" Attributes : 0x%x\n", attributes
);
143 spin_lock(&mem_attr_smc_lock
);
145 int ret
= xlat_change_mem_attributes_ctx(sp_ctx
->xlat_ctx_handle
,
147 smc_attr_to_mmap_attr(attributes
));
149 spin_unlock(&mem_attr_smc_lock
);
151 /* Convert error codes of xlat_change_mem_attributes_ctx() into SPM. */
152 assert((ret
== 0) || (ret
== -EINVAL
));
154 return (ret
== 0) ? SPM_SUCCESS
: SPM_INVALID_PARAMETER
;