1f3b4914bSYatharth Kochar /* 2f3b4914bSYatharth Kochar * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. 3f3b4914bSYatharth Kochar * 4*82cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5f3b4914bSYatharth Kochar */ 6f3b4914bSYatharth Kochar 7f3b4914bSYatharth Kochar #include <arch_helpers.h> 8f3b4914bSYatharth Kochar #include <assert.h> 9f3b4914bSYatharth Kochar #include <context.h> 10f3b4914bSYatharth Kochar #include <context_mgmt.h> 11f3b4914bSYatharth Kochar #include <debug.h> 12f3b4914bSYatharth Kochar #include <platform.h> 13f3b4914bSYatharth Kochar #include <smcc_helpers.h> 14f3b4914bSYatharth Kochar 15f3b4914bSYatharth Kochar /* 16f3b4914bSYatharth Kochar * Following arrays will be used for context management. 17f3b4914bSYatharth Kochar * There are 2 instances, for the Secure and Non-Secure contexts. 18f3b4914bSYatharth Kochar */ 19f3b4914bSYatharth Kochar static cpu_context_t bl1_cpu_context[2]; 20f3b4914bSYatharth Kochar static smc_ctx_t bl1_smc_context[2]; 21f3b4914bSYatharth Kochar 22f3b4914bSYatharth Kochar /* Following contains the next cpu context pointer. */ 23f3b4914bSYatharth Kochar static void *bl1_next_cpu_context_ptr; 24f3b4914bSYatharth Kochar 25f3b4914bSYatharth Kochar /* Following contains the next smc context pointer. */ 26f3b4914bSYatharth Kochar static void *bl1_next_smc_context_ptr; 27f3b4914bSYatharth Kochar 28f3b4914bSYatharth Kochar /* Following functions are used for SMC context handling */ 29f3b4914bSYatharth Kochar void *smc_get_ctx(int security_state) 30f3b4914bSYatharth Kochar { 31f3b4914bSYatharth Kochar assert(sec_state_is_valid(security_state)); 32f3b4914bSYatharth Kochar return &bl1_smc_context[security_state]; 33f3b4914bSYatharth Kochar } 34f3b4914bSYatharth Kochar 35f3b4914bSYatharth Kochar void smc_set_next_ctx(int security_state) 36f3b4914bSYatharth Kochar { 37f3b4914bSYatharth Kochar assert(sec_state_is_valid(security_state)); 38f3b4914bSYatharth Kochar bl1_next_smc_context_ptr = &bl1_smc_context[security_state]; 39f3b4914bSYatharth Kochar } 40f3b4914bSYatharth Kochar 41f3b4914bSYatharth Kochar void *smc_get_next_ctx(void) 42f3b4914bSYatharth Kochar { 43f3b4914bSYatharth Kochar return bl1_next_smc_context_ptr; 44f3b4914bSYatharth Kochar } 45f3b4914bSYatharth Kochar 46f3b4914bSYatharth Kochar /* Following functions are used for CPU context handling */ 47f3b4914bSYatharth Kochar void *cm_get_context(uint32_t security_state) 48f3b4914bSYatharth Kochar { 49f3b4914bSYatharth Kochar assert(sec_state_is_valid(security_state)); 50f3b4914bSYatharth Kochar return &bl1_cpu_context[security_state]; 51f3b4914bSYatharth Kochar } 52f3b4914bSYatharth Kochar 53f3b4914bSYatharth Kochar void cm_set_next_context(void *cpu_context) 54f3b4914bSYatharth Kochar { 55f3b4914bSYatharth Kochar assert(cpu_context); 56f3b4914bSYatharth Kochar bl1_next_cpu_context_ptr = cpu_context; 57f3b4914bSYatharth Kochar } 58f3b4914bSYatharth Kochar 59f3b4914bSYatharth Kochar void *cm_get_next_context(void) 60f3b4914bSYatharth Kochar { 61f3b4914bSYatharth Kochar return bl1_next_cpu_context_ptr; 62f3b4914bSYatharth Kochar } 63f3b4914bSYatharth Kochar 64f3b4914bSYatharth Kochar /******************************************************************************* 65f3b4914bSYatharth Kochar * Following function copies GP regs r0-r4, lr and spsr, 66f3b4914bSYatharth Kochar * from the CPU context to the SMC context structures. 67f3b4914bSYatharth Kochar ******************************************************************************/ 68f3b4914bSYatharth Kochar static void copy_cpu_ctx_to_smc_ctx(const regs_t *cpu_reg_ctx, 69f3b4914bSYatharth Kochar smc_ctx_t *next_smc_ctx) 70f3b4914bSYatharth Kochar { 71f3b4914bSYatharth Kochar next_smc_ctx->r0 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R0); 72f3b4914bSYatharth Kochar next_smc_ctx->r1 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R1); 73f3b4914bSYatharth Kochar next_smc_ctx->r2 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R2); 74f3b4914bSYatharth Kochar next_smc_ctx->r3 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R3); 75f3b4914bSYatharth Kochar next_smc_ctx->lr_mon = read_ctx_reg(cpu_reg_ctx, CTX_LR); 76f3b4914bSYatharth Kochar next_smc_ctx->spsr_mon = read_ctx_reg(cpu_reg_ctx, CTX_SPSR); 77f3b4914bSYatharth Kochar } 78f3b4914bSYatharth Kochar 79f3b4914bSYatharth Kochar /******************************************************************************* 80f3b4914bSYatharth Kochar * Following function flushes the SMC & CPU context pointer and its data. 81f3b4914bSYatharth Kochar ******************************************************************************/ 82f3b4914bSYatharth Kochar static void flush_smc_and_cpu_ctx(void) 83f3b4914bSYatharth Kochar { 84f3b4914bSYatharth Kochar flush_dcache_range((uintptr_t)&bl1_next_smc_context_ptr, 85f3b4914bSYatharth Kochar sizeof(bl1_next_smc_context_ptr)); 86f3b4914bSYatharth Kochar flush_dcache_range((uintptr_t)bl1_next_smc_context_ptr, 87f3b4914bSYatharth Kochar sizeof(smc_ctx_t)); 88f3b4914bSYatharth Kochar 89f3b4914bSYatharth Kochar flush_dcache_range((uintptr_t)&bl1_next_cpu_context_ptr, 90f3b4914bSYatharth Kochar sizeof(bl1_next_cpu_context_ptr)); 91f3b4914bSYatharth Kochar flush_dcache_range((uintptr_t)bl1_next_cpu_context_ptr, 92f3b4914bSYatharth Kochar sizeof(cpu_context_t)); 93f3b4914bSYatharth Kochar } 94f3b4914bSYatharth Kochar 95f3b4914bSYatharth Kochar /******************************************************************************* 96f3b4914bSYatharth Kochar * This function prepares the context for Secure/Normal world images. 97f3b4914bSYatharth Kochar * Normal world images are transitioned to HYP(if supported) else SVC. 98f3b4914bSYatharth Kochar ******************************************************************************/ 99f3b4914bSYatharth Kochar void bl1_prepare_next_image(unsigned int image_id) 100f3b4914bSYatharth Kochar { 101f3b4914bSYatharth Kochar unsigned int security_state; 102f3b4914bSYatharth Kochar image_desc_t *image_desc; 103f3b4914bSYatharth Kochar entry_point_info_t *next_bl_ep; 104f3b4914bSYatharth Kochar 105f3b4914bSYatharth Kochar /* Get the image descriptor. */ 106f3b4914bSYatharth Kochar image_desc = bl1_plat_get_image_desc(image_id); 107f3b4914bSYatharth Kochar assert(image_desc); 108f3b4914bSYatharth Kochar 109f3b4914bSYatharth Kochar /* Get the entry point info. */ 110f3b4914bSYatharth Kochar next_bl_ep = &image_desc->ep_info; 111f3b4914bSYatharth Kochar 112f3b4914bSYatharth Kochar /* Get the image security state. */ 113f3b4914bSYatharth Kochar security_state = GET_SECURITY_STATE(next_bl_ep->h.attr); 114f3b4914bSYatharth Kochar 115f3b4914bSYatharth Kochar /* Prepare the SPSR for the next BL image. */ 116f3b4914bSYatharth Kochar if (security_state == SECURE) { 117f3b4914bSYatharth Kochar next_bl_ep->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM, 118f3b4914bSYatharth Kochar SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS); 119f3b4914bSYatharth Kochar } else { 120f3b4914bSYatharth Kochar /* Use HYP mode if supported else use SVC. */ 121fabf3017SYatharth Kochar if (GET_VIRT_EXT(read_id_pfr1())) { 122f3b4914bSYatharth Kochar next_bl_ep->spsr = SPSR_MODE32(MODE32_hyp, SPSR_T_ARM, 123f3b4914bSYatharth Kochar SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS); 124f3b4914bSYatharth Kochar } else { 125f3b4914bSYatharth Kochar next_bl_ep->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM, 126f3b4914bSYatharth Kochar SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS); 127f3b4914bSYatharth Kochar } 128f3b4914bSYatharth Kochar } 129f3b4914bSYatharth Kochar 130f3b4914bSYatharth Kochar /* Allow platform to make change */ 131f3b4914bSYatharth Kochar bl1_plat_set_ep_info(image_id, next_bl_ep); 132f3b4914bSYatharth Kochar 133f3b4914bSYatharth Kochar /* Prepare the cpu context for the next BL image. */ 134f3b4914bSYatharth Kochar cm_init_my_context(next_bl_ep); 135f3b4914bSYatharth Kochar cm_prepare_el3_exit(security_state); 136f3b4914bSYatharth Kochar cm_set_next_context(cm_get_context(security_state)); 137f3b4914bSYatharth Kochar 138f3b4914bSYatharth Kochar /* Prepare the smc context for the next BL image. */ 139f3b4914bSYatharth Kochar smc_set_next_ctx(security_state); 140f3b4914bSYatharth Kochar copy_cpu_ctx_to_smc_ctx(get_regs_ctx(cm_get_next_context()), 141f3b4914bSYatharth Kochar smc_get_next_ctx()); 142f3b4914bSYatharth Kochar 143f3b4914bSYatharth Kochar /* 144f3b4914bSYatharth Kochar * Flush the SMC & CPU context and the (next)pointers, 145f3b4914bSYatharth Kochar * to access them after caches are disabled. 146f3b4914bSYatharth Kochar */ 147f3b4914bSYatharth Kochar flush_smc_and_cpu_ctx(); 148f3b4914bSYatharth Kochar 149f3b4914bSYatharth Kochar /* Indicate that image is in execution state. */ 150f3b4914bSYatharth Kochar image_desc->state = IMAGE_STATE_EXECUTED; 151f3b4914bSYatharth Kochar 152f3b4914bSYatharth Kochar print_entry_point_info(next_bl_ep); 153f3b4914bSYatharth Kochar } 154