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