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