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