1f3b4914bSYatharth Kochar /* 2*f4c8aa90SJeenu Viswambharan * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. 3f3b4914bSYatharth Kochar * 4f3b4914bSYatharth Kochar * Redistribution and use in source and binary forms, with or without 5f3b4914bSYatharth Kochar * modification, are permitted provided that the following conditions are met: 6f3b4914bSYatharth Kochar * 7f3b4914bSYatharth Kochar * Redistributions of source code must retain the above copyright notice, this 8f3b4914bSYatharth Kochar * list of conditions and the following disclaimer. 9f3b4914bSYatharth Kochar * 10f3b4914bSYatharth Kochar * Redistributions in binary form must reproduce the above copyright notice, 11f3b4914bSYatharth Kochar * this list of conditions and the following disclaimer in the documentation 12f3b4914bSYatharth Kochar * and/or other materials provided with the distribution. 13f3b4914bSYatharth Kochar * 14f3b4914bSYatharth Kochar * Neither the name of ARM nor the names of its contributors may be used 15f3b4914bSYatharth Kochar * to endorse or promote products derived from this software without specific 16f3b4914bSYatharth Kochar * prior written permission. 17f3b4914bSYatharth Kochar * 18f3b4914bSYatharth Kochar * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19f3b4914bSYatharth Kochar * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20f3b4914bSYatharth Kochar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21f3b4914bSYatharth Kochar * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22f3b4914bSYatharth Kochar * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23f3b4914bSYatharth Kochar * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24f3b4914bSYatharth Kochar * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25f3b4914bSYatharth Kochar * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26f3b4914bSYatharth Kochar * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27f3b4914bSYatharth Kochar * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28f3b4914bSYatharth Kochar * POSSIBILITY OF SUCH DAMAGE. 29f3b4914bSYatharth Kochar */ 30f3b4914bSYatharth Kochar 31f3b4914bSYatharth Kochar #include <arch_helpers.h> 32f3b4914bSYatharth Kochar #include <assert.h> 33f3b4914bSYatharth Kochar #include <context.h> 34f3b4914bSYatharth Kochar #include <context_mgmt.h> 35f3b4914bSYatharth Kochar #include <debug.h> 36f3b4914bSYatharth Kochar #include <platform.h> 37f3b4914bSYatharth Kochar 38f3b4914bSYatharth Kochar /* 39f3b4914bSYatharth Kochar * Following array will be used for context management. 40f3b4914bSYatharth Kochar * There are 2 instances, for the Secure and Non-Secure contexts. 41f3b4914bSYatharth Kochar */ 42f3b4914bSYatharth Kochar static cpu_context_t bl1_cpu_context[2]; 43f3b4914bSYatharth Kochar 44f3b4914bSYatharth Kochar /* Following contains the cpu context pointers. */ 45f3b4914bSYatharth Kochar static void *bl1_cpu_context_ptr[2]; 46f3b4914bSYatharth Kochar 47f3b4914bSYatharth Kochar 48f3b4914bSYatharth Kochar void *cm_get_context(uint32_t security_state) 49f3b4914bSYatharth Kochar { 50f3b4914bSYatharth Kochar assert(sec_state_is_valid(security_state)); 51f3b4914bSYatharth Kochar return bl1_cpu_context_ptr[security_state]; 52f3b4914bSYatharth Kochar } 53f3b4914bSYatharth Kochar 54f3b4914bSYatharth Kochar void cm_set_context(void *context, uint32_t security_state) 55f3b4914bSYatharth Kochar { 56f3b4914bSYatharth Kochar assert(sec_state_is_valid(security_state)); 57f3b4914bSYatharth Kochar bl1_cpu_context_ptr[security_state] = context; 58f3b4914bSYatharth Kochar } 59f3b4914bSYatharth Kochar 60f3b4914bSYatharth Kochar /******************************************************************************* 61f3b4914bSYatharth Kochar * This function prepares the context for Secure/Normal world images. 62f3b4914bSYatharth Kochar * Normal world images are transitioned to EL2(if supported) else EL1. 63f3b4914bSYatharth Kochar ******************************************************************************/ 64f3b4914bSYatharth Kochar void bl1_prepare_next_image(unsigned int image_id) 65f3b4914bSYatharth Kochar { 66f3b4914bSYatharth Kochar unsigned int security_state; 67f3b4914bSYatharth Kochar image_desc_t *image_desc; 68f3b4914bSYatharth Kochar entry_point_info_t *next_bl_ep; 69f3b4914bSYatharth Kochar 70f3b4914bSYatharth Kochar #if CTX_INCLUDE_AARCH32_REGS 71f3b4914bSYatharth Kochar /* 72f3b4914bSYatharth Kochar * Ensure that the build flag to save AArch32 system registers in CPU 73f3b4914bSYatharth Kochar * context is not set for AArch64-only platforms. 74f3b4914bSYatharth Kochar */ 75*f4c8aa90SJeenu Viswambharan if (EL_IMPLEMENTED(1) == EL_IMPL_A64ONLY) { 76f3b4914bSYatharth Kochar ERROR("EL1 supports AArch64-only. Please set build flag " 77f3b4914bSYatharth Kochar "CTX_INCLUDE_AARCH32_REGS = 0"); 78f3b4914bSYatharth Kochar panic(); 79f3b4914bSYatharth Kochar } 80f3b4914bSYatharth Kochar #endif 81f3b4914bSYatharth Kochar 82f3b4914bSYatharth Kochar /* Get the image descriptor. */ 83f3b4914bSYatharth Kochar image_desc = bl1_plat_get_image_desc(image_id); 84f3b4914bSYatharth Kochar assert(image_desc); 85f3b4914bSYatharth Kochar 86f3b4914bSYatharth Kochar /* Get the entry point info. */ 87f3b4914bSYatharth Kochar next_bl_ep = &image_desc->ep_info; 88f3b4914bSYatharth Kochar 89f3b4914bSYatharth Kochar /* Get the image security state. */ 90f3b4914bSYatharth Kochar security_state = GET_SECURITY_STATE(next_bl_ep->h.attr); 91f3b4914bSYatharth Kochar 92f3b4914bSYatharth Kochar /* Setup the Secure/Non-Secure context if not done already. */ 93f3b4914bSYatharth Kochar if (!cm_get_context(security_state)) 94f3b4914bSYatharth Kochar cm_set_context(&bl1_cpu_context[security_state], security_state); 95f3b4914bSYatharth Kochar 96f3b4914bSYatharth Kochar /* Prepare the SPSR for the next BL image. */ 97f3b4914bSYatharth Kochar if (security_state == SECURE) { 98f3b4914bSYatharth Kochar next_bl_ep->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, 99f3b4914bSYatharth Kochar DISABLE_ALL_EXCEPTIONS); 100f3b4914bSYatharth Kochar } else { 101*f4c8aa90SJeenu Viswambharan /* Use EL2 if supported; else use EL1. */ 102*f4c8aa90SJeenu Viswambharan if (EL_IMPLEMENTED(2)) { 103f3b4914bSYatharth Kochar next_bl_ep->spsr = SPSR_64(MODE_EL2, MODE_SP_ELX, 104f3b4914bSYatharth Kochar DISABLE_ALL_EXCEPTIONS); 105f3b4914bSYatharth Kochar } else { 106f3b4914bSYatharth Kochar next_bl_ep->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, 107f3b4914bSYatharth Kochar DISABLE_ALL_EXCEPTIONS); 108f3b4914bSYatharth Kochar } 109f3b4914bSYatharth Kochar } 110f3b4914bSYatharth Kochar 111f3b4914bSYatharth Kochar /* Allow platform to make change */ 112f3b4914bSYatharth Kochar bl1_plat_set_ep_info(image_id, next_bl_ep); 113f3b4914bSYatharth Kochar 114f3b4914bSYatharth Kochar /* Prepare the context for the next BL image. */ 115f3b4914bSYatharth Kochar cm_init_my_context(next_bl_ep); 116f3b4914bSYatharth Kochar cm_prepare_el3_exit(security_state); 117f3b4914bSYatharth Kochar 118f3b4914bSYatharth Kochar /* Indicate that image is in execution state. */ 119f3b4914bSYatharth Kochar image_desc->state = IMAGE_STATE_EXECUTED; 120f3b4914bSYatharth Kochar 121f3b4914bSYatharth Kochar print_entry_point_info(next_bl_ep); 122f3b4914bSYatharth Kochar } 123