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