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