blob: d401f8cc2b6b2e1147c1e83ad3a931c813fb33a4 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Dan Handleye83b0ca2014-01-14 18:17:09 +00002 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <stdio.h>
32#include <string.h>
33#include <errno.h>
34#include <assert.h>
35#include <arch_helpers.h>
36#include <console.h>
37#include <platform.h>
38#include <semihosting.h>
39#include <bl_common.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010040
41/***********************************************************
42 * Memory for sharing data while changing exception levels.
43 * Only used by the primary core.
44 **********************************************************/
45unsigned char bl2_el_change_mem_ptr[EL_CHANGE_MEM_SIZE];
46
47unsigned long *get_el_change_mem_ptr(void)
48{
49 return (unsigned long *) bl2_el_change_mem_ptr;
50}
51
52unsigned long page_align(unsigned long value, unsigned dir)
53{
54 unsigned long page_size = 1 << FOUR_KB_SHIFT;
55
56 /* Round up the limit to the next page boundary */
57 if (value & (page_size - 1)) {
58 value &= ~(page_size - 1);
59 if (dir == UP)
60 value += page_size;
61 }
62
63 return value;
64}
65
66static inline unsigned int is_page_aligned (unsigned long addr) {
67 const unsigned long page_size = 1 << FOUR_KB_SHIFT;
68
69 return (addr & (page_size - 1)) == 0;
70}
71
72void change_security_state(unsigned int target_security_state)
73{
74 unsigned long scr = read_scr();
75
76 if (target_security_state == SECURE)
77 scr &= ~SCR_NS_BIT;
78 else if (target_security_state == NON_SECURE)
79 scr |= SCR_NS_BIT;
80 else
81 assert(0);
82
83 write_scr(scr);
84}
85
86int drop_el(aapcs64_params *args,
87 unsigned long spsr,
88 unsigned long entrypoint)
89{
90 write_spsr(spsr);
91 write_elr(entrypoint);
92 eret(args->arg0,
93 args->arg1,
94 args->arg2,
95 args->arg3,
96 args->arg4,
97 args->arg5,
98 args->arg6,
99 args->arg7);
100 return -EINVAL;
101}
102
103long raise_el(aapcs64_params *args)
104{
105 return smc(args->arg0,
106 args->arg1,
107 args->arg2,
108 args->arg3,
109 args->arg4,
110 args->arg5,
111 args->arg6,
112 args->arg7);
113}
114
115/*
116 * TODO: If we are not EL3 then currently we only issue an SMC.
117 * Add support for dropping into EL0 etc. Consider adding support
118 * for switching from S-EL1 to S-EL0/1 etc.
119 */
120long change_el(el_change_info *info)
121{
122 unsigned long current_el = read_current_el();
123
124 if (GET_EL(current_el) == MODE_EL3) {
125 /*
126 * We can go anywhere from EL3. So find where.
127 * TODO: Lots to do if we are going non-secure.
128 * Flip the NS bit. Restore NS registers etc.
129 * Just doing the bare minimal for now.
130 */
131
132 if (info->security_state == NON_SECURE)
133 change_security_state(info->security_state);
134
135 return drop_el(&info->args, info->spsr, info->entrypoint);
136 } else
137 return raise_el(&info->args);
138}
139
140/* TODO: add a parameter for DAIF. not needed right now */
141unsigned long make_spsr(unsigned long target_el,
142 unsigned long target_sp,
143 unsigned long target_rw)
144{
145 unsigned long spsr;
146
147 /* Disable all exceptions & setup the EL */
148 spsr = (DAIF_FIQ_BIT | DAIF_IRQ_BIT | DAIF_ABT_BIT | DAIF_DBG_BIT)
149 << PSR_DAIF_SHIFT;
150 spsr |= PSR_MODE(target_rw, target_el, target_sp);
151
152 return spsr;
153}
154
155/*******************************************************************************
156 * The next two functions are the weak definitions. Platform specific
157 * code can override them if it wishes to.
158 ******************************************************************************/
159
160/*******************************************************************************
161 * Function that takes a memory layout into which BL31 has been either top or
162 * bottom loaded. Using this information, it populates bl31_mem_layout to tell
163 * BL31 how much memory it has access to and how much is available for use. It
164 * does not need the address where BL31 has been loaded as BL31 will reclaim
165 * all the memory used by BL2.
166 * TODO: Revisit if this and init_bl2_mem_layout can be replaced by a single
167 * routine.
168 ******************************************************************************/
169void init_bl31_mem_layout(const meminfo *bl2_mem_layout,
170 meminfo *bl31_mem_layout,
171 unsigned int load_type)
172{
173 if (load_type == BOT_LOAD) {
174 /*
175 * ------------ ^
176 * | BL2 | |
177 * |----------| ^ | BL2
178 * | | | BL2 free | total
179 * | | | size | size
180 * |----------| BL2 free base v |
181 * | BL31 | |
182 * ------------ BL2 total base v
183 */
184 unsigned long bl31_size;
185
186 bl31_mem_layout->free_base = bl2_mem_layout->free_base;
187
188 bl31_size = bl2_mem_layout->free_base - bl2_mem_layout->total_base;
189 bl31_mem_layout->free_size = bl2_mem_layout->total_size - bl31_size;
190 } else {
191 /*
192 * ------------ ^
193 * | BL31 | |
194 * |----------| ^ | BL2
195 * | | | BL2 free | total
196 * | | | size | size
197 * |----------| BL2 free base v |
198 * | BL2 | |
199 * ------------ BL2 total base v
200 */
201 unsigned long bl2_size;
202
203 bl31_mem_layout->free_base = bl2_mem_layout->total_base;
204
205 bl2_size = bl2_mem_layout->free_base - bl2_mem_layout->total_base;
206 bl31_mem_layout->free_size = bl2_mem_layout->free_size + bl2_size;
207 }
208
209 bl31_mem_layout->total_base = bl2_mem_layout->total_base;
210 bl31_mem_layout->total_size = bl2_mem_layout->total_size;
211 bl31_mem_layout->attr = load_type;
212
213 flush_dcache_range((unsigned long) bl31_mem_layout, sizeof(meminfo));
214 return;
215}
216
217/*******************************************************************************
218 * Function that takes a memory layout into which BL2 has been either top or
219 * bottom loaded along with the address where BL2 has been loaded in it. Using
220 * this information, it populates bl2_mem_layout to tell BL2 how much memory
221 * it has access to and how much is available for use.
222 ******************************************************************************/
223void init_bl2_mem_layout(meminfo *bl1_mem_layout,
224 meminfo *bl2_mem_layout,
225 unsigned int load_type,
226 unsigned long bl2_base)
227{
228 unsigned tmp;
229
230 if (load_type == BOT_LOAD) {
231 bl2_mem_layout->total_base = bl2_base;
232 tmp = bl1_mem_layout->free_base - bl2_base;
233 bl2_mem_layout->total_size = bl1_mem_layout->free_size + tmp;
234
235 } else {
236 bl2_mem_layout->total_base = bl1_mem_layout->free_base;
237 tmp = bl1_mem_layout->total_base + bl1_mem_layout->total_size;
238 bl2_mem_layout->total_size = tmp - bl1_mem_layout->free_base;
239 }
240
241 bl2_mem_layout->free_base = bl1_mem_layout->free_base;
242 bl2_mem_layout->free_size = bl1_mem_layout->free_size;
243 bl2_mem_layout->attr = load_type;
244
245 flush_dcache_range((unsigned long) bl2_mem_layout, sizeof(meminfo));
246 return;
247}
248
249static void dump_load_info(unsigned long image_load_addr,
250 unsigned long image_size,
251 const meminfo *mem_layout)
252{
253#if DEBUG
254 printf("Trying to load image at address 0x%lx, size = 0x%lx\r\n",
255 image_load_addr, image_size);
256 printf("Current memory layout:\r\n");
257 printf(" total region = [0x%lx, 0x%lx]\r\n", mem_layout->total_base,
258 mem_layout->total_base + mem_layout->total_size);
259 printf(" free region = [0x%lx, 0x%lx]\r\n", mem_layout->free_base,
260 mem_layout->free_base + mem_layout->free_size);
261#endif
262}
263
264/*******************************************************************************
265 * Generic function to load an image into the trusted RAM using semihosting
266 * given a name, extents of free memory & whether the image should be loaded at
267 * the bottom or top of the free memory. It updates the memory layout if the
268 * load is successful.
269 ******************************************************************************/
270unsigned long load_image(meminfo *mem_layout,
271 const char *image_name,
272 unsigned int load_type,
273 unsigned long fixed_addr)
274{
James Morrissey40a6f642014-02-10 14:24:36 +0000275 unsigned long temp_image_base = 0;
276 unsigned long image_base = 0;
277 long offset = 0;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100278 int image_flen;
279
280 /* Find the size of the image */
281 image_flen = semihosting_get_flen(image_name);
282 if (image_flen < 0) {
283 printf("ERROR: Cannot access '%s' file (%i).\r\n",
284 image_name, image_flen);
285 return 0;
286 }
287
288 /* See if we have enough space */
289 if (image_flen > mem_layout->free_size) {
290 printf("ERROR: Cannot load '%s' file: Not enough space.\r\n",
291 image_name);
292 dump_load_info(0, image_flen, mem_layout);
293 return 0;
294 }
295
296 switch (load_type) {
297
298 case TOP_LOAD:
299
300 /* Load the image in the top of free memory */
301 temp_image_base = mem_layout->free_base + mem_layout->free_size;
302 temp_image_base -= image_flen;
303
304 /* Page align base address and check whether the image still fits */
305 image_base = page_align(temp_image_base, DOWN);
306 assert(image_base <= temp_image_base);
307
308 if (image_base < mem_layout->free_base) {
309 printf("ERROR: Cannot load '%s' file: Not enough space.\r\n",
310 image_name);
311 dump_load_info(image_base, image_flen, mem_layout);
312 return 0;
313 }
314
315 /* Calculate the amount of extra memory used due to alignment */
316 offset = temp_image_base - image_base;
317
318 break;
319
320 case BOT_LOAD:
321
322 /* Load the BL2 image in the bottom of free memory */
323 temp_image_base = mem_layout->free_base;
324 image_base = page_align(temp_image_base, UP);
325 assert(image_base >= temp_image_base);
326
327 /* Page align base address and check whether the image still fits */
328 if (image_base + image_flen >
329 mem_layout->free_base + mem_layout->free_size) {
330 printf("ERROR: Cannot load '%s' file: Not enough space.\r\n",
331 image_name);
332 dump_load_info(image_base, image_flen, mem_layout);
333 return 0;
334 }
335
336 /* Calculate the amount of extra memory used due to alignment */
337 offset = image_base - temp_image_base;
338
339 break;
340
341 default:
342 assert(0);
343
344 }
345
346 /*
347 * Some images must be loaded at a fixed address, not a dynamic one.
348 *
349 * This has been implemented as a hack on top of the existing dynamic
350 * loading mechanism, for the time being. If the 'fixed_addr' function
351 * argument is different from zero, then it will force the load address.
352 * So we still have this principle of top/bottom loading but the code
353 * determining the load address is bypassed and the load address is
354 * forced to the fixed one.
355 *
356 * This can result in quite a lot of wasted space because we still use
357 * 1 sole meminfo structure to represent the extents of free memory,
358 * where we should use some sort of linked list.
359 *
360 * E.g. we want to load BL2 at address 0x04020000, the resulting memory
361 * layout should look as follows:
362 * ------------ 0x04040000
363 * | | <- Free space (1)
364 * |----------|
365 * | BL2 |
366 * |----------| 0x04020000
367 * | | <- Free space (2)
368 * |----------|
369 * | BL1 |
370 * ------------ 0x04000000
371 *
372 * But in the current hacky implementation, we'll need to specify
373 * whether BL2 is loaded at the top or bottom of the free memory.
374 * E.g. if BL2 is considered as top-loaded, the meminfo structure
375 * will give the following view of the memory, hiding the chunk of
376 * free memory above BL2:
377 * ------------ 0x04040000
378 * | |
379 * | |
380 * | BL2 |
381 * |----------| 0x04020000
382 * | | <- Free space (2)
383 * |----------|
384 * | BL1 |
385 * ------------ 0x04000000
386 */
387 if (fixed_addr != 0) {
388 /* Load the image at the given address. */
389 image_base = fixed_addr;
390
391 /* Check whether the image fits. */
392 if ((image_base < mem_layout->free_base) ||
393 (image_base + image_flen >
394 mem_layout->free_base + mem_layout->free_size)) {
395 printf("ERROR: Cannot load '%s' file: Not enough space.\r\n",
396 image_name);
397 dump_load_info(image_base, image_flen, mem_layout);
398 return 0;
399 }
400
401 /* Check whether the fixed load address is page-aligned. */
402 if (!is_page_aligned(image_base)) {
403 printf("ERROR: Cannot load '%s' file at unaligned address 0x%lx.\r\n",
404 image_name, fixed_addr);
405 return 0;
406 }
407
408 /*
409 * Calculate the amount of extra memory used due to fixed
410 * loading.
411 */
412 if (load_type == TOP_LOAD) {
413 unsigned long max_addr, space_used;
414 /*
415 * ------------ max_addr
416 * | /wasted/ | | offset
417 * |..........|..............................
418 * | image | | image_flen
419 * |----------| fixed_addr
420 * | |
421 * | |
422 * ------------ total_base
423 */
424 max_addr = mem_layout->total_base + mem_layout->total_size;
425 /*
426 * Compute the amount of memory used by the image.
427 * Corresponds to all space above the image load
428 * address.
429 */
430 space_used = max_addr - fixed_addr;
431 /*
432 * Calculate the amount of wasted memory within the
433 * amount of memory used by the image.
434 */
435 offset = space_used - image_flen;
436 } else /* BOT_LOAD */
437 /*
438 * ------------
439 * | |
440 * | |
441 * |----------|
442 * | image |
443 * |..........| fixed_addr
444 * | /wasted/ | | offset
445 * ------------ total_base
446 */
447 offset = fixed_addr - mem_layout->total_base;
448 }
449
450 /* We have enough space so load the image now */
451 image_flen = semihosting_download_file(image_name,
452 image_flen,
453 (void *) image_base);
454 if (image_flen <= 0) {
455 printf("ERROR: Failed to load '%s' file from semihosting (%i).\r\n",
456 image_name, image_flen);
457 return 0;
458 }
459
460 /*
461 * File has been successfully loaded. Update the free memory
462 * data structure & flush the contents of the TZRAM so that
463 * the next EL can see it.
464 */
465 /* Update the memory contents */
466 flush_dcache_range(image_base, image_flen);
467
468 mem_layout->free_size -= image_flen + offset;
469
470 /* Update the base of free memory since its moved up */
471 if (load_type == BOT_LOAD)
472 mem_layout->free_base += offset + image_flen;
473
474 return image_base;
475}
476
477/*******************************************************************************
478 * Run a loaded image from the given entry point. This could result in either
479 * dropping into a lower exception level or jumping to a higher exception level.
480 * The only way of doing the latter is through an SMC. In either case, setup the
481 * parameters for the EL change request correctly.
482 ******************************************************************************/
483int run_image(unsigned long entrypoint,
484 unsigned long spsr,
485 unsigned long target_security_state,
486 meminfo *mem_layout,
487 void *data)
488{
489 el_change_info run_image_info;
490 unsigned long current_el = read_current_el();
491
492 /* Tell next EL what we want done */
493 run_image_info.args.arg0 = RUN_IMAGE;
494 run_image_info.entrypoint = entrypoint;
495 run_image_info.spsr = spsr;
496 run_image_info.security_state = target_security_state;
497 run_image_info.next = 0;
498
499 /*
500 * If we are EL3 then only an eret can take us to the desired
501 * exception level. Else for the time being assume that we have
502 * to jump to a higher EL and issue an SMC. Contents of argY
503 * will go into the general purpose register xY e.g. arg0->x0
504 */
505 if (GET_EL(current_el) == MODE_EL3) {
506 run_image_info.args.arg1 = (unsigned long) mem_layout;
507 run_image_info.args.arg2 = (unsigned long) data;
508 } else {
509 run_image_info.args.arg1 = entrypoint;
510 run_image_info.args.arg2 = spsr;
511 run_image_info.args.arg3 = (unsigned long) mem_layout;
512 run_image_info.args.arg4 = (unsigned long) data;
513 }
514
515 return change_el(&run_image_info);
516}