1 /*
2  * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 
9 #include <arch_helpers.h>
10 #include <context.h>
11 #include <common/debug.h>
12 #include <lib/el3_runtime/context_mgmt.h>
13 #include <plat/common/platform.h>
14 
15 #include "../bl1_private.h"
16 
17 /* Following contains the cpu context pointers. */
18 static void *bl1_cpu_context_ptr[2];
19 
20 
cm_get_context(uint32_t security_state)21 void *cm_get_context(uint32_t security_state)
22 {
23 	assert(sec_state_is_valid(security_state));
24 	return bl1_cpu_context_ptr[security_state];
25 }
26 
cm_set_context(void * context,uint32_t security_state)27 void cm_set_context(void *context, uint32_t security_state)
28 {
29 	assert(sec_state_is_valid(security_state));
30 	bl1_cpu_context_ptr[security_state] = context;
31 }
32 
33 /*******************************************************************************
34  * This function prepares the context for Secure/Normal world images.
35  * Normal world images are transitioned to EL2(if supported) else EL1.
36  ******************************************************************************/
bl1_prepare_next_image(unsigned int image_id)37 void bl1_prepare_next_image(unsigned int image_id)
38 {
39 
40 	/*
41 	 * Following array will be used for context management.
42 	 * There are 2 instances, for the Secure and Non-Secure contexts.
43 	 */
44 	static cpu_context_t bl1_cpu_context[2];
45 
46 	unsigned int security_state, mode = MODE_EL1;
47 	image_desc_t *desc;
48 	entry_point_info_t *next_bl_ep;
49 
50 #if CTX_INCLUDE_AARCH32_REGS
51 	/*
52 	 * Ensure that the build flag to save AArch32 system registers in CPU
53 	 * context is not set for AArch64-only platforms.
54 	 */
55 	if (el_implemented(1) == EL_IMPL_A64ONLY) {
56 		ERROR("EL1 supports AArch64-only. Please set build flag "
57 				"CTX_INCLUDE_AARCH32_REGS = 0\n");
58 		panic();
59 	}
60 #endif
61 
62 	/* Get the image descriptor. */
63 	desc = bl1_plat_get_image_desc(image_id);
64 	assert(desc != NULL);
65 
66 	/* Get the entry point info. */
67 	next_bl_ep = &desc->ep_info;
68 
69 	/* Get the image security state. */
70 	security_state = GET_SECURITY_STATE(next_bl_ep->h.attr);
71 
72 	/* Setup the Secure/Non-Secure context if not done already. */
73 	if (cm_get_context(security_state) == NULL)
74 		cm_set_context(&bl1_cpu_context[security_state], security_state);
75 
76 	/* Prepare the SPSR for the next BL image. */
77 	if ((security_state != SECURE) && (el_implemented(2) != EL_IMPL_NONE)) {
78 		mode = MODE_EL2;
79 	}
80 
81 	next_bl_ep->spsr = (uint32_t)SPSR_64((uint64_t) mode,
82 		(uint64_t)MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
83 
84 	/* Allow platform to make change */
85 	bl1_plat_set_ep_info(image_id, next_bl_ep);
86 
87 	/* Prepare the context for the next BL image. */
88 	cm_init_my_context(next_bl_ep);
89 	cm_prepare_el3_exit(security_state);
90 
91 	/* Indicate that image is in execution state. */
92 	desc->state = IMAGE_STATE_EXECUTED;
93 
94 	print_entry_point_info(next_bl_ep);
95 }
96