blob: 2aa2fa135763a51a4178a7a098e2c3e8a3fe7555 [file] [log] [blame]
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +01001/*
2 * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
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>
11#include <platform_def.h>
12#include <platform.h>
13#include <secure_partition.h>
14#include <spm_svc.h>
15#include <xlat_tables_v2.h>
16
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 * change_mem_attributes().
48 */
49static unsigned int smc_attr_to_mmap_attr(unsigned int attributes)
50{
51 unsigned int tf_attr = 0U;
52
53 unsigned int access = (attributes & SP_MEMORY_ATTRIBUTES_ACCESS_MASK)
54 >> SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT;
55
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;
60 } else {
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;
65 }
66
67 if ((attributes & SP_MEMORY_ATTRIBUTES_NON_EXEC) == 0) {
68 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. */
88 data_access = SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS;
89 } else {
90 if ((attr & MT_RW) != 0) {
91 assert(MT_TYPE(attr) != MT_DEVICE);
92 data_access = SP_MEMORY_ATTRIBUTES_ACCESS_RW;
93 } else {
94 data_access = SP_MEMORY_ATTRIBUTES_ACCESS_RO;
95 }
96 }
97
98 smc_attr |= (data_access & SP_MEMORY_ATTRIBUTES_ACCESS_MASK)
99 << SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT;
100
101 if ((attr & MT_EXECUTE_NEVER) != 0U) {
102 smc_attr |= SP_MEMORY_ATTRIBUTES_NON_EXEC;
103 }
104
105 return smc_attr;
106}
107
Antonio Nino Diaz28759312018-05-22 16:26:48 +0100108int32_t spm_memory_attributes_get_smc_handler(sp_context_t *sp_ctx,
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +0100109 uintptr_t base_va)
110{
111 uint32_t attributes;
112
113 spin_lock(&mem_attr_smc_lock);
114
115 int rc = get_mem_attributes(sp_ctx->xlat_ctx_handle,
116 base_va, &attributes);
117
118 spin_unlock(&mem_attr_smc_lock);
119
120 /* Convert error codes of get_mem_attributes() into SPM ones. */
121 assert((rc == 0) || (rc == -EINVAL));
122
123 if (rc == 0) {
124 return (int32_t) smc_mmap_to_smc_attr(attributes);
125 } else {
126 return SPM_INVALID_PARAMETER;
127 }
128}
129
Antonio Nino Diaz28759312018-05-22 16:26:48 +0100130int spm_memory_attributes_set_smc_handler(sp_context_t *sp_ctx,
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +0100131 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 = change_mem_attributes(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 change_mem_attributes() into SPM ones. */
152 assert((ret == 0) || (ret == -EINVAL));
153
154 return (ret == 0) ? SPM_SUCCESS : SPM_INVALID_PARAMETER;
155}