blob: 2c7fe0705ffa0f7fc5c7d65cfb8285fef0673c75 [file] [log] [blame]
Yatharth Kochara65be2f2015-10-09 18:06:13 +01001/*
Jeenu Viswambharan2a9b8822017-02-21 14:40:44 +00002 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
Yatharth Kochara65be2f2015-10-09 18:06:13 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Yatharth Kochara65be2f2015-10-09 18:06:13 +01005 */
6
7#include <arch_helpers.h>
8#include <assert.h>
9#include <context.h>
10#include <context_mgmt.h>
Soby Mathewd75d2ba2016-05-17 14:01:32 +010011#include <debug.h>
Yatharth Kochara65be2f2015-10-09 18:06:13 +010012#include <platform.h>
13
14/*
15 * Following array will be used for context management.
16 * There are 2 instances, for the Secure and Non-Secure contexts.
17 */
18static cpu_context_t bl1_cpu_context[2];
19
20/* Following contains the cpu context pointers. */
21static void *bl1_cpu_context_ptr[2];
22
23
24void *cm_get_context(uint32_t security_state)
25{
26 assert(sec_state_is_valid(security_state));
27 return bl1_cpu_context_ptr[security_state];
28}
29
30void cm_set_context(void *context, uint32_t security_state)
31{
32 assert(sec_state_is_valid(security_state));
33 bl1_cpu_context_ptr[security_state] = context;
34}
35
36/*******************************************************************************
37 * This function prepares the context for Secure/Normal world images.
38 * Normal world images are transitioned to EL2(if supported) else EL1.
39 ******************************************************************************/
40void bl1_prepare_next_image(unsigned int image_id)
41{
42 unsigned int security_state;
43 image_desc_t *image_desc;
44 entry_point_info_t *next_bl_ep;
45
Soby Mathewd75d2ba2016-05-17 14:01:32 +010046#if CTX_INCLUDE_AARCH32_REGS
47 /*
48 * Ensure that the build flag to save AArch32 system registers in CPU
49 * context is not set for AArch64-only platforms.
50 */
Jeenu Viswambharan2a9b8822017-02-21 14:40:44 +000051 if (EL_IMPLEMENTED(1) == EL_IMPL_A64ONLY) {
Soby Mathewd75d2ba2016-05-17 14:01:32 +010052 ERROR("EL1 supports AArch64-only. Please set build flag "
53 "CTX_INCLUDE_AARCH32_REGS = 0");
54 panic();
55 }
56#endif
57
Yatharth Kochara65be2f2015-10-09 18:06:13 +010058 /* Get the image descriptor. */
59 image_desc = bl1_plat_get_image_desc(image_id);
60 assert(image_desc);
61
62 /* Get the entry point info. */
63 next_bl_ep = &image_desc->ep_info;
64
65 /* Get the image security state. */
Yatharth Kocharf11b29a2016-02-01 11:04:46 +000066 security_state = GET_SECURITY_STATE(next_bl_ep->h.attr);
Yatharth Kochara65be2f2015-10-09 18:06:13 +010067
68 /* Setup the Secure/Non-Secure context if not done already. */
69 if (!cm_get_context(security_state))
70 cm_set_context(&bl1_cpu_context[security_state], security_state);
71
72 /* Prepare the SPSR for the next BL image. */
73 if (security_state == SECURE) {
74 next_bl_ep->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
75 DISABLE_ALL_EXCEPTIONS);
76 } else {
Jeenu Viswambharan2a9b8822017-02-21 14:40:44 +000077 /* Use EL2 if supported; else use EL1. */
78 if (EL_IMPLEMENTED(2)) {
Yatharth Kochara65be2f2015-10-09 18:06:13 +010079 next_bl_ep->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX,
80 DISABLE_ALL_EXCEPTIONS);
81 } else {
82 next_bl_ep->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
83 DISABLE_ALL_EXCEPTIONS);
84 }
85 }
86
87 /* Allow platform to make change */
88 bl1_plat_set_ep_info(image_id, next_bl_ep);
89
90 /* Prepare the context for the next BL image. */
91 cm_init_my_context(next_bl_ep);
92 cm_prepare_el3_exit(security_state);
93
94 /* Indicate that image is in execution state. */
95 image_desc->state = IMAGE_STATE_EXECUTED;
96
97 print_entry_point_info(next_bl_ep);
98}