blob: b1ca55ae3dd7dbfa6748b89343857ec22043629d [file] [log] [blame]
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +00001/*
Marc Bonnici758bd242021-12-19 21:37:50 +00002 * Copyright (c) 2018-2022, ARM Limited and Contributors. All rights reserved.
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +00003 *
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 Diaze0f90632018-12-14 00:18:21 +000011#include <lib/xlat_tables/xlat_tables_v2.h>
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000012#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013#include <plat/common/platform.h>
Paul Beesley45f40282019-10-15 10:57:42 +000014#include <services/spm_mm_partition.h>
Paul Beesleye9754e62019-10-15 12:51:55 +000015#include <services/spm_mm_svc.h>
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000016
Paul Beesley15a96a92019-10-15 14:23:23 +000017#include "spm_mm_private.h"
Paul Beesleya24ed072019-10-15 16:11:46 +000018#include "spm_mm_shim_private.h"
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000019
20/* Place translation tables by default along with the ones used by BL31. */
21#ifndef PLAT_SP_IMAGE_XLAT_SECTION_NAME
Chris Kay33bfc5e2023-02-14 11:30:04 +000022#define PLAT_SP_IMAGE_XLAT_SECTION_NAME ".xlat_table"
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000023#endif
Masahisa Kojima813b50b2020-06-02 05:54:13 +090024#ifndef PLAT_SP_IMAGE_BASE_XLAT_SECTION_NAME
25#define PLAT_SP_IMAGE_BASE_XLAT_SECTION_NAME ".bss"
26#endif
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000027
28/* Allocate and initialise the translation context for the secure partitions. */
29REGISTER_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 Kojima813b50b2020-06-02 05:54:13 +090033 EL1_EL0_REGIME, PLAT_SP_IMAGE_XLAT_SECTION_NAME,
34 PLAT_SP_IMAGE_BASE_XLAT_SECTION_NAME);
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000035
36/* Lock used for SP_MEMORY_ATTRIBUTES_GET and SP_MEMORY_ATTRIBUTES_SET */
37static spinlock_t mem_attr_smc_lock;
38
39/* Get handle of Secure Partition translation context */
40xlat_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 */
53static unsigned int smc_attr_to_mmap_attr(unsigned int attributes)
54{
55 unsigned int tf_attr = 0U;
56
Paul Beesleye9754e62019-10-15 12:51:55 +000057 unsigned int access = (attributes & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK)
58 >> MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000059
Paul Beesleye9754e62019-10-15 12:51:55 +000060 if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW) {
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000061 tf_attr |= MT_RW | MT_USER;
Paul Beesleye9754e62019-10-15 12:51:55 +000062 } else if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO) {
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000063 tf_attr |= MT_RO | MT_USER;
64 } else {
65 /* Other values are reserved. */
Paul Beesleye9754e62019-10-15 12:51:55 +000066 assert(access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS);
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000067 /* The only requirement is that there's no access from EL0 */
68 tf_attr |= MT_RO | MT_PRIVILEGED;
69 }
70
Paul Beesleye9754e62019-10-15 12:51:55 +000071 if ((attributes & MM_SP_MEMORY_ATTRIBUTES_NON_EXEC) == 0) {
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000072 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 */
84static 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 Beesleye9754e62019-10-15 12:51:55 +000092 data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000093 } else {
94 if ((attr & MT_RW) != 0) {
95 assert(MT_TYPE(attr) != MT_DEVICE);
Paul Beesleye9754e62019-10-15 12:51:55 +000096 data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000097 } else {
Paul Beesleye9754e62019-10-15 12:51:55 +000098 data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000099 }
100 }
101
Paul Beesleye9754e62019-10-15 12:51:55 +0000102 smc_attr |= (data_access & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK)
103 << MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +0000104
105 if ((attr & MT_EXECUTE_NEVER) != 0U) {
Paul Beesleye9754e62019-10-15 12:51:55 +0000106 smc_attr |= MM_SP_MEMORY_ATTRIBUTES_NON_EXEC;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +0000107 }
108
109 return smc_attr;
110}
111
112int32_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 Beesleye9754e62019-10-15 12:51:55 +0000130 return SPM_MM_INVALID_PARAMETER;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +0000131 }
132}
133
134int 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 Beesleye9754e62019-10-15 12:51:55 +0000158 return (ret == 0) ? SPM_MM_SUCCESS : SPM_MM_INVALID_PARAMETER;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +0000159}