blob: 57ad742453a2f7493977f377ecfb607c30ccd4fa [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>
Antonio Nino Diazbb7d1cd2018-10-30 11:34:23 +000011#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012
13#include <platform_def.h>
14
15#include <lib/object_pool.h>
16#include <lib/utils.h>
17#include <lib/utils_def.h>
18#include <lib/xlat_tables/xlat_tables_v2.h>
19#include <plat/common/platform.h>
20#include <services/sp_res_desc.h>
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +010021
22#include "spm_private.h"
23#include "spm_shim_private.h"
24
Antonio Nino Diazbb7d1cd2018-10-30 11:34:23 +000025/*******************************************************************************
26 * Instantiation of translation table context
27 ******************************************************************************/
28
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +010029/* Place translation tables by default along with the ones used by BL31. */
30#ifndef PLAT_SP_IMAGE_XLAT_SECTION_NAME
31#define PLAT_SP_IMAGE_XLAT_SECTION_NAME "xlat_table"
32#endif
33
Antonio Nino Diaz675d1552018-10-30 11:36:47 +000034/*
35 * Allocate elements of the translation contexts for the Secure Partitions.
36 */
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +010037
Antonio Nino Diaz675d1552018-10-30 11:36:47 +000038/* Allocate an array of mmap_region per partition. */
39static struct mmap_region sp_mmap_regions[PLAT_SP_IMAGE_MMAP_REGIONS + 1]
40 [PLAT_SPM_MAX_PARTITIONS];
41static OBJECT_POOL(sp_mmap_regions_pool, sp_mmap_regions,
42 sizeof(mmap_region_t) * (PLAT_SP_IMAGE_MMAP_REGIONS + 1),
43 PLAT_SPM_MAX_PARTITIONS);
44
45/* Allocate individual translation tables. */
46static uint64_t sp_xlat_tables[XLAT_TABLE_ENTRIES]
47 [(PLAT_SP_IMAGE_MAX_XLAT_TABLES + 1) * PLAT_SPM_MAX_PARTITIONS]
48 __aligned(XLAT_TABLE_SIZE) __section(PLAT_SP_IMAGE_XLAT_SECTION_NAME);
49static OBJECT_POOL(sp_xlat_tables_pool, sp_xlat_tables,
50 XLAT_TABLE_ENTRIES * sizeof(uint64_t),
51 (PLAT_SP_IMAGE_MAX_XLAT_TABLES + 1) * PLAT_SPM_MAX_PARTITIONS);
52
53/* Allocate base translation tables. */
54static uint64_t sp_xlat_base_tables
55 [GET_NUM_BASE_LEVEL_ENTRIES(PLAT_VIRT_ADDR_SPACE_SIZE)]
56 [PLAT_SPM_MAX_PARTITIONS]
57 __aligned(GET_NUM_BASE_LEVEL_ENTRIES(PLAT_VIRT_ADDR_SPACE_SIZE)
58 * sizeof(uint64_t))
59 __section(PLAT_SP_IMAGE_XLAT_SECTION_NAME);
60static OBJECT_POOL(sp_xlat_base_tables_pool, sp_xlat_base_tables,
61 GET_NUM_BASE_LEVEL_ENTRIES(PLAT_VIRT_ADDR_SPACE_SIZE) * sizeof(uint64_t),
62 PLAT_SPM_MAX_PARTITIONS);
63
64/* Allocate arrays. */
65static int sp_xlat_mapped_regions[PLAT_SP_IMAGE_MAX_XLAT_TABLES]
66 [PLAT_SPM_MAX_PARTITIONS];
67static OBJECT_POOL(sp_xlat_mapped_regions_pool, sp_xlat_mapped_regions,
68 sizeof(int) * PLAT_SP_IMAGE_MAX_XLAT_TABLES, PLAT_SPM_MAX_PARTITIONS);
69
70/* Allocate individual contexts. */
71static xlat_ctx_t sp_xlat_ctx[PLAT_SPM_MAX_PARTITIONS];
72static OBJECT_POOL(sp_xlat_ctx_pool, sp_xlat_ctx, sizeof(xlat_ctx_t),
73 PLAT_SPM_MAX_PARTITIONS);
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +010074
75/* Get handle of Secure Partition translation context */
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +000076xlat_ctx_t *spm_sp_xlat_context_alloc(void)
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +010077{
Antonio Nino Diaz675d1552018-10-30 11:36:47 +000078 xlat_ctx_t *ctx = pool_alloc(&sp_xlat_ctx_pool);
79
80 struct mmap_region *mmap = pool_alloc(&sp_mmap_regions_pool);
81
82 uint64_t *base_table = pool_alloc(&sp_xlat_base_tables_pool);
83 uint64_t **tables = pool_alloc_n(&sp_xlat_tables_pool,
84 PLAT_SP_IMAGE_MAX_XLAT_TABLES);
85
86 int *mapped_regions = pool_alloc(&sp_xlat_mapped_regions_pool);
87
88 xlat_setup_dynamic_ctx(ctx, PLAT_PHY_ADDR_SPACE_SIZE - 1,
89 PLAT_VIRT_ADDR_SPACE_SIZE - 1, mmap,
90 PLAT_SP_IMAGE_MMAP_REGIONS, tables,
91 PLAT_SP_IMAGE_MAX_XLAT_TABLES, base_table,
92 EL1_EL0_REGIME, mapped_regions);
93
94 return ctx;
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +010095};
96
Antonio Nino Diazbb7d1cd2018-10-30 11:34:23 +000097/*******************************************************************************
98 * Functions to allocate memory for regions.
99 ******************************************************************************/
100
101/*
102 * The region with base PLAT_SPM_HEAP_BASE and size PLAT_SPM_HEAP_SIZE is
103 * reserved for SPM to use as heap to allocate memory regions of Secure
104 * Partitions. This is only done at boot.
105 */
106static OBJECT_POOL(spm_heap_mem, (void *)PLAT_SPM_HEAP_BASE, 1U,
107 PLAT_SPM_HEAP_SIZE);
108
109static uintptr_t spm_alloc_heap(size_t size)
110{
111 return (uintptr_t)pool_alloc_n(&spm_heap_mem, size);
112}
113
114/*******************************************************************************
115 * Functions to map memory regions described in the resource description.
116 ******************************************************************************/
117static unsigned int rdmem_attr_to_mmap_attr(uint32_t attr)
118{
119 unsigned int index = attr & RD_MEM_MASK;
120
121 const unsigned int mmap_attr_arr[8] = {
122 MT_DEVICE | MT_RW | MT_SECURE, /* RD_MEM_DEVICE */
123 MT_CODE | MT_SECURE, /* RD_MEM_NORMAL_CODE */
124 MT_MEMORY | MT_RW | MT_SECURE, /* RD_MEM_NORMAL_DATA */
125 MT_MEMORY | MT_RW | MT_SECURE, /* RD_MEM_NORMAL_BSS */
126 MT_RO_DATA | MT_SECURE, /* RD_MEM_NORMAL_RODATA */
127 MT_MEMORY | MT_RW | MT_SECURE, /* RD_MEM_NORMAL_SPM_SP_SHARED_MEM */
128 MT_MEMORY | MT_RW | MT_SECURE, /* RD_MEM_NORMAL_CLIENT_SHARED_MEM */
129 MT_MEMORY | MT_RW | MT_SECURE /* RD_MEM_NORMAL_MISCELLANEOUS */
130 };
131
132 if (index >= ARRAY_SIZE(mmap_attr_arr)) {
133 ERROR("Unsupported RD memory attributes 0x%x\n", attr);
134 panic();
135 }
136
137 return mmap_attr_arr[index];
138}
139
140/*
141 * The data provided in the resource description structure is not directly
142 * compatible with a mmap_region structure. This function handles the conversion
143 * and maps it.
144 */
145static void map_rdmem(sp_context_t *sp_ctx, struct sp_rd_sect_mem_region *rdmem)
146{
147 int rc;
148 mmap_region_t mmap;
149
150 /* Location of the SP image */
151 uintptr_t sp_size = sp_ctx->image_size;
152 uintptr_t sp_base_va = sp_ctx->rd.attribute.load_address;
153 unsigned long long sp_base_pa = sp_ctx->image_base;
154
155 /* Location of the memory region to map */
156 size_t rd_size = rdmem->size;
157 uintptr_t rd_base_va = rdmem->base;
158 unsigned long long rd_base_pa;
159
160 unsigned int memtype = rdmem->attr & RD_MEM_MASK;
161
162 VERBOSE("Adding memory region '%s'\n", rdmem->name);
163
164 mmap.granularity = REGION_DEFAULT_GRANULARITY;
165
166 /* Check if the RD region is inside of the SP image or not */
167 int is_outside = (rd_base_va + rd_size <= sp_base_va) ||
168 (sp_base_va + sp_size <= rd_base_va);
169
170 /* Set to 1 if it is needed to zero this region */
171 int zero_region = 0;
172
173 switch (memtype) {
174 case RD_MEM_DEVICE:
175 /* Device regions are mapped 1:1 */
176 rd_base_pa = rd_base_va;
177 break;
178
179 case RD_MEM_NORMAL_CODE:
180 case RD_MEM_NORMAL_RODATA:
181 {
182 if (is_outside == 1) {
183 ERROR("Code and rodata sections must be fully contained in the image.");
184 panic();
185 }
186
187 /* Get offset into the image */
188 rd_base_pa = sp_base_pa + rd_base_va - sp_base_va;
189 break;
190 }
191 case RD_MEM_NORMAL_DATA:
192 {
193 if (is_outside == 1) {
194 ERROR("Data sections must be fully contained in the image.");
195 panic();
196 }
197
198 rd_base_pa = spm_alloc_heap(rd_size);
199
200 /* Get offset into the image */
201 void *img_pa = (void *)(sp_base_pa + rd_base_va - sp_base_va);
202
203 VERBOSE(" Copying data from %p to 0x%llx\n", img_pa, rd_base_pa);
204
205 /* Map destination */
206 rc = mmap_add_dynamic_region(rd_base_pa, rd_base_pa,
207 rd_size, MT_MEMORY | MT_RW | MT_SECURE);
208 if (rc != 0) {
209 ERROR("Unable to map data region at EL3: %d\n", rc);
210 panic();
211 }
212
213 /* Copy original data to destination */
214 memcpy((void *)rd_base_pa, img_pa, rd_size);
215
216 /* Unmap destination region */
217 rc = mmap_remove_dynamic_region(rd_base_pa, rd_size);
218 if (rc != 0) {
219 ERROR("Unable to remove data region at EL3: %d\n", rc);
220 panic();
221 }
222
223 break;
224 }
225 case RD_MEM_NORMAL_MISCELLANEOUS:
226 /* Allow SPM to change the attributes of the region. */
227 mmap.granularity = PAGE_SIZE;
228 rd_base_pa = spm_alloc_heap(rd_size);
229 zero_region = 1;
230 break;
231
232 case RD_MEM_NORMAL_SPM_SP_SHARED_MEM:
233 if ((sp_ctx->spm_sp_buffer_base != 0) ||
234 (sp_ctx->spm_sp_buffer_size != 0)) {
235 ERROR("A partition must have only one SPM<->SP buffer.\n");
236 panic();
237 }
238 rd_base_pa = spm_alloc_heap(rd_size);
239 zero_region = 1;
240 /* Save location of this buffer, it is needed by SPM */
241 sp_ctx->spm_sp_buffer_base = rd_base_pa;
242 sp_ctx->spm_sp_buffer_size = rd_size;
243 break;
244
245 case RD_MEM_NORMAL_CLIENT_SHARED_MEM:
246 /* Fallthrough */
247 case RD_MEM_NORMAL_BSS:
248 rd_base_pa = spm_alloc_heap(rd_size);
249 zero_region = 1;
250 break;
251
252 default:
253 panic();
254 }
255
256 mmap.base_pa = rd_base_pa;
257 mmap.base_va = rd_base_va;
258 mmap.size = rd_size;
259
260 /* Only S-EL0 mappings supported for now */
261 mmap.attr = rdmem_attr_to_mmap_attr(rdmem->attr) | MT_USER;
262
263 VERBOSE(" VA: 0x%lx PA: 0x%llx (0x%lx, attr: 0x%x)\n",
264 mmap.base_va, mmap.base_pa, mmap.size, mmap.attr);
265
266 /* Map region in the context of the Secure Partition */
267 mmap_add_region_ctx(sp_ctx->xlat_ctx_handle, &mmap);
268
269 if (zero_region == 1) {
270 VERBOSE(" Zeroing region...\n");
271
272 rc = mmap_add_dynamic_region(mmap.base_pa, mmap.base_pa,
273 mmap.size, MT_MEMORY | MT_RW | MT_SECURE);
274 if (rc != 0) {
275 ERROR("Unable to map memory at EL3 to zero: %d\n",
276 rc);
277 panic();
278 }
279
280 zeromem((void *)mmap.base_pa, mmap.size);
281
282 /*
283 * Unmap destination region unless it is the SPM<->SP buffer,
284 * which must be used by SPM.
285 */
286 if (memtype != RD_MEM_NORMAL_SPM_SP_SHARED_MEM) {
287 rc = mmap_remove_dynamic_region(rd_base_pa, rd_size);
288 if (rc != 0) {
289 ERROR("Unable to remove region at EL3: %d\n", rc);
290 panic();
291 }
292 }
293 }
294}
295
296void sp_map_memory_regions(sp_context_t *sp_ctx)
297{
298 /* This region contains the exception vectors used at S-EL1. */
299 const mmap_region_t sel1_exception_vectors =
300 MAP_REGION_FLAT(SPM_SHIM_EXCEPTIONS_START,
301 SPM_SHIM_EXCEPTIONS_SIZE,
302 MT_CODE | MT_SECURE | MT_PRIVILEGED);
303
304 mmap_add_region_ctx(sp_ctx->xlat_ctx_handle,
305 &sel1_exception_vectors);
306
307 struct sp_rd_sect_mem_region *rdmem;
308
309 for (rdmem = sp_ctx->rd.mem_region; rdmem != NULL; rdmem = rdmem->next) {
310 map_rdmem(sp_ctx, rdmem);
311 }
312
313 init_xlat_tables_ctx(sp_ctx->xlat_ctx_handle);
314}