1/*
2 * Copyright (c) 2021, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include "../rmmd_private.h"
8#include <asm_macros.S>
9
10	.global rmmd_rmm_enter
11	.global rmmd_rmm_exit
12
13	/* ---------------------------------------------------------------------
14	 * This function is called with SP_EL0 as stack. Here we stash our EL3
15	 * callee-saved registers on to the stack as a part of saving the C
16	 * runtime and enter the secure payload.
17	 * 'x0' contains a pointer to the memory where the address of the C
18	 *  runtime context is to be saved.
19	 * ---------------------------------------------------------------------
20	 */
21func rmmd_rmm_enter
22	/* Make space for the registers that we're going to save */
23	mov	x3, sp
24	str	x3, [x0, #0]
25	sub	sp, sp, #RMMD_C_RT_CTX_SIZE
26
27	/* Save callee-saved registers on to the stack */
28	stp	x19, x20, [sp, #RMMD_C_RT_CTX_X19]
29	stp	x21, x22, [sp, #RMMD_C_RT_CTX_X21]
30	stp	x23, x24, [sp, #RMMD_C_RT_CTX_X23]
31	stp	x25, x26, [sp, #RMMD_C_RT_CTX_X25]
32	stp	x27, x28, [sp, #RMMD_C_RT_CTX_X27]
33	stp	x29, x30, [sp, #RMMD_C_RT_CTX_X29]
34
35	/* ---------------------------------------------------------------------
36	 * Everything is setup now. el3_exit() will use the secure context to
37	 * restore to the general purpose and EL3 system registers to ERET
38	 * into the secure payload.
39	 * ---------------------------------------------------------------------
40	 */
41	b	el3_exit
42endfunc rmmd_rmm_enter
43
44	/* ---------------------------------------------------------------------
45	 * This function is called with 'x0' pointing to a C runtime context.
46	 * It restores the saved registers and jumps to that runtime with 'x0'
47	 * as the new SP register. This destroys the C runtime context that had
48	 * been built on the stack below the saved context by the caller. Later
49	 * the second parameter 'x1' is passed as a return value to the caller.
50	 * ---------------------------------------------------------------------
51	 */
52func rmmd_rmm_exit
53	/* Restore the previous stack */
54	mov	sp, x0
55
56	/* Restore callee-saved registers on to the stack */
57	ldp	x19, x20, [x0, #(RMMD_C_RT_CTX_X19 - RMMD_C_RT_CTX_SIZE)]
58	ldp	x21, x22, [x0, #(RMMD_C_RT_CTX_X21 - RMMD_C_RT_CTX_SIZE)]
59	ldp	x23, x24, [x0, #(RMMD_C_RT_CTX_X23 - RMMD_C_RT_CTX_SIZE)]
60	ldp	x25, x26, [x0, #(RMMD_C_RT_CTX_X25 - RMMD_C_RT_CTX_SIZE)]
61	ldp	x27, x28, [x0, #(RMMD_C_RT_CTX_X27 - RMMD_C_RT_CTX_SIZE)]
62	ldp	x29, x30, [x0, #(RMMD_C_RT_CTX_X29 - RMMD_C_RT_CTX_SIZE)]
63
64	/* ---------------------------------------------------------------------
65	 * This should take us back to the instruction after the call to the
66	 * last rmmd_rmm_enter().* Place the second parameter to x0
67	 * so that the caller will see it as a return value from the original
68	 * entry call.
69	 * ---------------------------------------------------------------------
70	 */
71	mov	x0, x1
72	ret
73endfunc rmmd_rmm_exit
74