xref: /rk3399_ARM-atf/services/spd/opteed/opteed_helpers.S (revision aa5da46138e1583990086b76b56e0a9186cb7b7d)
1*aa5da461SJens Wiklander/*
2*aa5da461SJens Wiklander * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
3*aa5da461SJens Wiklander *
4*aa5da461SJens Wiklander * Redistribution and use in source and binary forms, with or without
5*aa5da461SJens Wiklander * modification, are permitted provided that the following conditions are met:
6*aa5da461SJens Wiklander *
7*aa5da461SJens Wiklander * Redistributions of source code must retain the above copyright notice, this
8*aa5da461SJens Wiklander * list of conditions and the following disclaimer.
9*aa5da461SJens Wiklander *
10*aa5da461SJens Wiklander * Redistributions in binary form must reproduce the above copyright notice,
11*aa5da461SJens Wiklander * this list of conditions and the following disclaimer in the documentation
12*aa5da461SJens Wiklander * and/or other materials provided with the distribution.
13*aa5da461SJens Wiklander *
14*aa5da461SJens Wiklander * Neither the name of ARM nor the names of its contributors may be used
15*aa5da461SJens Wiklander * to endorse or promote products derived from this software without specific
16*aa5da461SJens Wiklander * prior written permission.
17*aa5da461SJens Wiklander *
18*aa5da461SJens Wiklander * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*aa5da461SJens Wiklander * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*aa5da461SJens Wiklander * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*aa5da461SJens Wiklander * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*aa5da461SJens Wiklander * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*aa5da461SJens Wiklander * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*aa5da461SJens Wiklander * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*aa5da461SJens Wiklander * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*aa5da461SJens Wiklander * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*aa5da461SJens Wiklander * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*aa5da461SJens Wiklander * POSSIBILITY OF SUCH DAMAGE.
29*aa5da461SJens Wiklander */
30*aa5da461SJens Wiklander
31*aa5da461SJens Wiklander#include <asm_macros.S>
32*aa5da461SJens Wiklander#include "opteed_private.h"
33*aa5da461SJens Wiklander
34*aa5da461SJens Wiklander	.global	opteed_enter_sp
35*aa5da461SJens Wiklander	/* ---------------------------------------------
36*aa5da461SJens Wiklander	 * This function is called with SP_EL0 as stack.
37*aa5da461SJens Wiklander	 * Here we stash our EL3 callee-saved registers
38*aa5da461SJens Wiklander	 * on to the stack as a part of saving the C
39*aa5da461SJens Wiklander	 * runtime and enter the secure payload.
40*aa5da461SJens Wiklander	 * 'x0' contains a pointer to the memory where
41*aa5da461SJens Wiklander	 * the address of the C runtime context is to be
42*aa5da461SJens Wiklander	 * saved.
43*aa5da461SJens Wiklander	 * ---------------------------------------------
44*aa5da461SJens Wiklander	 */
45*aa5da461SJens Wiklanderfunc opteed_enter_sp
46*aa5da461SJens Wiklander	/* Make space for the registers that we're going to save */
47*aa5da461SJens Wiklander	mov	x3, sp
48*aa5da461SJens Wiklander	str	x3, [x0, #0]
49*aa5da461SJens Wiklander	sub	sp, sp, #OPTEED_C_RT_CTX_SIZE
50*aa5da461SJens Wiklander
51*aa5da461SJens Wiklander	/* Save callee-saved registers on to the stack */
52*aa5da461SJens Wiklander	stp	x19, x20, [sp, #OPTEED_C_RT_CTX_X19]
53*aa5da461SJens Wiklander	stp	x21, x22, [sp, #OPTEED_C_RT_CTX_X21]
54*aa5da461SJens Wiklander	stp	x23, x24, [sp, #OPTEED_C_RT_CTX_X23]
55*aa5da461SJens Wiklander	stp	x25, x26, [sp, #OPTEED_C_RT_CTX_X25]
56*aa5da461SJens Wiklander	stp	x27, x28, [sp, #OPTEED_C_RT_CTX_X27]
57*aa5da461SJens Wiklander	stp	x29, x30, [sp, #OPTEED_C_RT_CTX_X29]
58*aa5da461SJens Wiklander
59*aa5da461SJens Wiklander	/* ---------------------------------------------
60*aa5da461SJens Wiklander	 * Everything is setup now. el3_exit() will
61*aa5da461SJens Wiklander	 * use the secure context to restore to the
62*aa5da461SJens Wiklander	 * general purpose and EL3 system registers to
63*aa5da461SJens Wiklander	 * ERET into OPTEE.
64*aa5da461SJens Wiklander	 * ---------------------------------------------
65*aa5da461SJens Wiklander	 */
66*aa5da461SJens Wiklander	b	el3_exit
67*aa5da461SJens Wiklander
68*aa5da461SJens Wiklander	/* ---------------------------------------------
69*aa5da461SJens Wiklander	 * This function is called 'x0' pointing to a C
70*aa5da461SJens Wiklander	 * runtime context saved in opteed_enter_sp().  It
71*aa5da461SJens Wiklander	 * restores the saved registers and jumps to
72*aa5da461SJens Wiklander	 * that runtime with 'x0' as the new sp. This
73*aa5da461SJens Wiklander	 * destroys the C runtime context that had been
74*aa5da461SJens Wiklander	 * built on the stack below the saved context by
75*aa5da461SJens Wiklander	 * the caller. Later the second parameter 'x1'
76*aa5da461SJens Wiklander	 * is passed as return value to the caller
77*aa5da461SJens Wiklander	 * ---------------------------------------------
78*aa5da461SJens Wiklander	 */
79*aa5da461SJens Wiklander	.global opteed_exit_sp
80*aa5da461SJens Wiklanderfunc opteed_exit_sp
81*aa5da461SJens Wiklander	/* Restore the previous stack */
82*aa5da461SJens Wiklander	mov	sp, x0
83*aa5da461SJens Wiklander
84*aa5da461SJens Wiklander	/* Restore callee-saved registers on to the stack */
85*aa5da461SJens Wiklander	ldp	x19, x20, [x0, #(OPTEED_C_RT_CTX_X19 - OPTEED_C_RT_CTX_SIZE)]
86*aa5da461SJens Wiklander	ldp	x21, x22, [x0, #(OPTEED_C_RT_CTX_X21 - OPTEED_C_RT_CTX_SIZE)]
87*aa5da461SJens Wiklander	ldp	x23, x24, [x0, #(OPTEED_C_RT_CTX_X23 - OPTEED_C_RT_CTX_SIZE)]
88*aa5da461SJens Wiklander	ldp	x25, x26, [x0, #(OPTEED_C_RT_CTX_X25 - OPTEED_C_RT_CTX_SIZE)]
89*aa5da461SJens Wiklander	ldp	x27, x28, [x0, #(OPTEED_C_RT_CTX_X27 - OPTEED_C_RT_CTX_SIZE)]
90*aa5da461SJens Wiklander	ldp	x29, x30, [x0, #(OPTEED_C_RT_CTX_X29 - OPTEED_C_RT_CTX_SIZE)]
91*aa5da461SJens Wiklander
92*aa5da461SJens Wiklander	/* ---------------------------------------------
93*aa5da461SJens Wiklander	 * This should take us back to the instruction
94*aa5da461SJens Wiklander	 * after the call to the last opteed_enter_sp().
95*aa5da461SJens Wiklander	 * Place the second parameter to x0 so that the
96*aa5da461SJens Wiklander	 * caller will see it as a return value from the
97*aa5da461SJens Wiklander	 * original entry call
98*aa5da461SJens Wiklander	 * ---------------------------------------------
99*aa5da461SJens Wiklander	 */
100*aa5da461SJens Wiklander	mov	x0, x1
101*aa5da461SJens Wiklander	ret
102