blob: 881d97f3503917eaa2049812ca7b1177598397dd [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>
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +010013#include <spm_svc.h>
14#include <xlat_tables_v2.h>
15
16#include "spm_private.h"
17#include "spm_shim_private.h"
18
19/* Place translation tables by default along with the ones used by BL31. */
20#ifndef PLAT_SP_IMAGE_XLAT_SECTION_NAME
21#define PLAT_SP_IMAGE_XLAT_SECTION_NAME "xlat_table"
22#endif
23
24/* Allocate and initialise the translation context for the secure partitions. */
25REGISTER_XLAT_CONTEXT2(sp,
26 PLAT_SP_IMAGE_MMAP_REGIONS,
27 PLAT_SP_IMAGE_MAX_XLAT_TABLES,
28 PLAT_VIRT_ADDR_SPACE_SIZE, PLAT_PHY_ADDR_SPACE_SIZE,
29 EL1_EL0_REGIME, PLAT_SP_IMAGE_XLAT_SECTION_NAME);
30
31/* Lock used for SP_MEMORY_ATTRIBUTES_GET and SP_MEMORY_ATTRIBUTES_SET */
32static spinlock_t mem_attr_smc_lock;
33
34/* Get handle of Secure Partition translation context */
35xlat_ctx_t *spm_get_sp_xlat_context(void)
36{
37 return &sp_xlat_ctx;
38};
39
40/*
41 * Attributes are encoded using a different format in the SMC interface than in
42 * the Trusted Firmware, where the mmap_attr_t enum type is used. This function
43 * converts an attributes value from the SMC format to the mmap_attr_t format by
44 * setting MT_RW/MT_RO, MT_USER/MT_PRIVILEGED and MT_EXECUTE/MT_EXECUTE_NEVER.
45 * The other fields are left as 0 because they are ignored by the function
Antonio Nino Diaz6c4c9ee2018-08-05 15:34:10 +010046 * xlat_change_mem_attributes_ctx().
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +010047 */
48static unsigned int smc_attr_to_mmap_attr(unsigned int attributes)
49{
50 unsigned int tf_attr = 0U;
51
52 unsigned int access = (attributes & SP_MEMORY_ATTRIBUTES_ACCESS_MASK)
53 >> SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT;
54
55 if (access == SP_MEMORY_ATTRIBUTES_ACCESS_RW) {
56 tf_attr |= MT_RW | MT_USER;
57 } else if (access == SP_MEMORY_ATTRIBUTES_ACCESS_RO) {
58 tf_attr |= MT_RO | MT_USER;
59 } else {
60 /* Other values are reserved. */
61 assert(access == SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS);
62 /* The only requirement is that there's no access from EL0 */
63 tf_attr |= MT_RO | MT_PRIVILEGED;
64 }
65
66 if ((attributes & SP_MEMORY_ATTRIBUTES_NON_EXEC) == 0) {
67 tf_attr |= MT_EXECUTE;
68 } else {
69 tf_attr |= MT_EXECUTE_NEVER;
70 }
71
72 return tf_attr;
73}
74
75/*
76 * This function converts attributes from the Trusted Firmware format into the
77 * SMC interface format.
78 */
79static unsigned int smc_mmap_to_smc_attr(unsigned int attr)
80{
81 unsigned int smc_attr = 0U;
82
83 unsigned int data_access;
84
85 if ((attr & MT_USER) == 0) {
86 /* No access from EL0. */
87 data_access = SP_MEMORY_ATTRIBUTES_ACCESS_NOACCESS;
88 } else {
89 if ((attr & MT_RW) != 0) {
90 assert(MT_TYPE(attr) != MT_DEVICE);
91 data_access = SP_MEMORY_ATTRIBUTES_ACCESS_RW;
92 } else {
93 data_access = SP_MEMORY_ATTRIBUTES_ACCESS_RO;
94 }
95 }
96
97 smc_attr |= (data_access & SP_MEMORY_ATTRIBUTES_ACCESS_MASK)
98 << SP_MEMORY_ATTRIBUTES_ACCESS_SHIFT;
99
100 if ((attr & MT_EXECUTE_NEVER) != 0U) {
101 smc_attr |= SP_MEMORY_ATTRIBUTES_NON_EXEC;
102 }
103
104 return smc_attr;
105}
106
Antonio Nino Diaz28759312018-05-22 16:26:48 +0100107int32_t spm_memory_attributes_get_smc_handler(sp_context_t *sp_ctx,
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +0100108 uintptr_t base_va)
109{
110 uint32_t attributes;
111
112 spin_lock(&mem_attr_smc_lock);
113
Antonio Nino Diaz6c4c9ee2018-08-05 15:34:10 +0100114 int rc = xlat_get_mem_attributes_ctx(sp_ctx->xlat_ctx_handle,
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +0100115 base_va, &attributes);
116
117 spin_unlock(&mem_attr_smc_lock);
118
Antonio Nino Diaz6c4c9ee2018-08-05 15:34:10 +0100119 /* Convert error codes of xlat_get_mem_attributes_ctx() into SPM. */
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +0100120 assert((rc == 0) || (rc == -EINVAL));
121
122 if (rc == 0) {
123 return (int32_t) smc_mmap_to_smc_attr(attributes);
124 } else {
125 return SPM_INVALID_PARAMETER;
126 }
127}
128
Antonio Nino Diaz28759312018-05-22 16:26:48 +0100129int spm_memory_attributes_set_smc_handler(sp_context_t *sp_ctx,
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +0100130 u_register_t page_address,
131 u_register_t pages_count,
132 u_register_t smc_attributes)
133{
134 uintptr_t base_va = (uintptr_t) page_address;
135 size_t size = (size_t) (pages_count * PAGE_SIZE);
136 uint32_t attributes = (uint32_t) smc_attributes;
137
138 INFO(" Start address : 0x%lx\n", base_va);
139 INFO(" Number of pages: %i (%zi bytes)\n", (int) pages_count, size);
140 INFO(" Attributes : 0x%x\n", attributes);
141
142 spin_lock(&mem_attr_smc_lock);
143
Antonio Nino Diaz6c4c9ee2018-08-05 15:34:10 +0100144 int ret = xlat_change_mem_attributes_ctx(sp_ctx->xlat_ctx_handle,
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +0100145 base_va, size,
146 smc_attr_to_mmap_attr(attributes));
147
148 spin_unlock(&mem_attr_smc_lock);
149
Antonio Nino Diaz6c4c9ee2018-08-05 15:34:10 +0100150 /* Convert error codes of xlat_change_mem_attributes_ctx() into SPM. */
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +0100151 assert((ret == 0) || (ret == -EINVAL));
152
153 return (ret == 0) ? SPM_SUCCESS : SPM_INVALID_PARAMETER;
154}