blob: 0c2399c4fb1d10f71ac8208dfaf3f2521cec22ec [file] [log] [blame]
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +00001/*
Paul Beesley45f40282019-10-15 10:57:42 +00002 * Copyright (c) 2018-2019, 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
17#include "spm_private.h"
18#include "spm_shim_private.h"
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
24
25/* Allocate and initialise the translation context for the secure partitions. */
26REGISTER_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);
31
32/* Lock used for SP_MEMORY_ATTRIBUTES_GET and SP_MEMORY_ATTRIBUTES_SET */
33static spinlock_t mem_attr_smc_lock;
34
35/* Get handle of Secure Partition translation context */
36xlat_ctx_t *spm_get_sp_xlat_context(void)
37{
38 return &sp_xlat_ctx;
39};
40
41/*
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().
48 */
49static unsigned int smc_attr_to_mmap_attr(unsigned int attributes)
50{
51 unsigned int tf_attr = 0U;
52
Paul Beesleye9754e62019-10-15 12:51:55 +000053 unsigned int access = (attributes & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK)
54 >> MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000055
Paul Beesleye9754e62019-10-15 12:51:55 +000056 if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW) {
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000057 tf_attr |= MT_RW | MT_USER;
Paul Beesleye9754e62019-10-15 12:51:55 +000058 } else if (access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO) {
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000059 tf_attr |= MT_RO | MT_USER;
60 } else {
61 /* Other values are reserved. */
Paul Beesleye9754e62019-10-15 12:51:55 +000062 assert(access == MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS);
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000063 /* The only requirement is that there's no access from EL0 */
64 tf_attr |= MT_RO | MT_PRIVILEGED;
65 }
66
Paul Beesleye9754e62019-10-15 12:51:55 +000067 if ((attributes & MM_SP_MEMORY_ATTRIBUTES_NON_EXEC) == 0) {
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000068 tf_attr |= MT_EXECUTE;
69 } else {
70 tf_attr |= MT_EXECUTE_NEVER;
71 }
72
73 return tf_attr;
74}
75
76/*
77 * This function converts attributes from the Trusted Firmware format into the
78 * SMC interface format.
79 */
80static unsigned int smc_mmap_to_smc_attr(unsigned int attr)
81{
82 unsigned int smc_attr = 0U;
83
84 unsigned int data_access;
85
86 if ((attr & MT_USER) == 0) {
87 /* No access from EL0. */
Paul Beesleye9754e62019-10-15 12:51:55 +000088 data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000089 } else {
90 if ((attr & MT_RW) != 0) {
91 assert(MT_TYPE(attr) != MT_DEVICE);
Paul Beesleye9754e62019-10-15 12:51:55 +000092 data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RW;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000093 } else {
Paul Beesleye9754e62019-10-15 12:51:55 +000094 data_access = MM_SP_MEMORY_ATTRIBUTES_ACCESS_RO;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +000095 }
96 }
97
Paul Beesleye9754e62019-10-15 12:51:55 +000098 smc_attr |= (data_access & MM_SP_MEMORY_ATTRIBUTES_ACCESS_MASK)
99 << MM_SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +0000100
101 if ((attr & MT_EXECUTE_NEVER) != 0U) {
Paul Beesleye9754e62019-10-15 12:51:55 +0000102 smc_attr |= MM_SP_MEMORY_ATTRIBUTES_NON_EXEC;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +0000103 }
104
105 return smc_attr;
106}
107
108int32_t spm_memory_attributes_get_smc_handler(sp_context_t *sp_ctx,
109 uintptr_t base_va)
110{
111 uint32_t attributes;
112
113 spin_lock(&mem_attr_smc_lock);
114
115 int rc = xlat_get_mem_attributes_ctx(sp_ctx->xlat_ctx_handle,
116 base_va, &attributes);
117
118 spin_unlock(&mem_attr_smc_lock);
119
120 /* Convert error codes of xlat_get_mem_attributes_ctx() into SPM. */
121 assert((rc == 0) || (rc == -EINVAL));
122
123 if (rc == 0) {
124 return (int32_t) smc_mmap_to_smc_attr(attributes);
125 } else {
Paul Beesleye9754e62019-10-15 12:51:55 +0000126 return SPM_MM_INVALID_PARAMETER;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +0000127 }
128}
129
130int 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)
134{
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;
138
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);
142
143 spin_lock(&mem_attr_smc_lock);
144
145 int ret = xlat_change_mem_attributes_ctx(sp_ctx->xlat_ctx_handle,
146 base_va, size,
147 smc_attr_to_mmap_attr(attributes));
148
149 spin_unlock(&mem_attr_smc_lock);
150
151 /* Convert error codes of xlat_change_mem_attributes_ctx() into SPM. */
152 assert((ret == 0) || (ret == -EINVAL));
153
Paul Beesleye9754e62019-10-15 12:51:55 +0000154 return (ret == 0) ? SPM_MM_SUCCESS : SPM_MM_INVALID_PARAMETER;
Antonio Nino Diaz8cd7ea32018-10-30 11:08:08 +0000155}