smcc_macros.S 2.72 KB
Newer Older
Soby Mathew's avatar
Soby Mathew committed
1
/*
2
 * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
Soby Mathew's avatar
Soby Mathew committed
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathew's avatar
Soby Mathew committed
5
6
7
8
9
10
11
 */
#ifndef __SMCC_MACROS_S__
#define __SMCC_MACROS_S__

#include <arch.h>

/*
12
13
14
15
 * Macro to save the General purpose registers (r0 - r12), the banked
 * spsr, lr, sp registers and the `scr` register to the SMC context on entry
 * due a SMC call. The `lr` of the current mode (monitor) is expected to be
 * already saved. The `sp` must point to the `smc_ctx_t` to save to.
16
17
 * Additionally, also save the 'pmcr' register as this is updated whilst
 * executing in the secure world.
Soby Mathew's avatar
Soby Mathew committed
18
19
 */
	.macro smcc_save_gp_mode_regs
20
21
22
23
	/* Save r0 - r12 in the SMC context */
	stm	sp, {r0-r12}
	mov	r0, sp
	add	r0, r0, #SMC_CTX_SP_USR
Soby Mathew's avatar
Soby Mathew committed
24
25
26
27
28
29
30
31
32
33
34

	/* Save the banked registers including the current SPSR and LR */
	mrs	r4, sp_usr
	mrs	r5, lr_usr
	mrs	r6, spsr_irq
	mrs	r7, sp_irq
	mrs	r8, lr_irq
	mrs	r9, spsr_fiq
	mrs	r10, sp_fiq
	mrs	r11, lr_fiq
	mrs	r12, spsr_svc
35
	stm	r0!, {r4-r12}
Soby Mathew's avatar
Soby Mathew committed
36
37
38
39
40
41
42
43
44
45

	mrs	r4, sp_svc
	mrs	r5, lr_svc
	mrs	r6, spsr_abt
	mrs	r7, sp_abt
	mrs	r8, lr_abt
	mrs	r9, spsr_und
	mrs	r10, sp_und
	mrs	r11, lr_und
	mrs	r12, spsr
46
	stm	r0!, {r4-r12}
Soby Mathew's avatar
Soby Mathew committed
47

48
49
50
	/* lr_mon is already saved by caller */
	ldcopr	r4, SCR
	str	r4, [sp, #SMC_CTX_SCR]
51
52
	ldcopr	r4, PMCR
	str	r4, [sp, #SMC_CTX_PMCR]
Soby Mathew's avatar
Soby Mathew committed
53
54
55
	.endm

/*
56
57
58
 * Macro to restore the `smc_ctx_t`, which includes the General purpose
 * registers and banked mode registers, and exit from the monitor mode.
 * r0 must point to the `smc_ctx_t` to restore from.
Soby Mathew's avatar
Soby Mathew committed
59
 */
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
	.macro monitor_exit
	/*
	 * Save the current sp and restore the smc context
	 * pointer to sp which will be used for handling the
	 * next SMC.
	 */
	str	sp, [r0, #SMC_CTX_SP_MON]
	mov	sp, r0

	/*
	 * Restore SCR first so that we access the right banked register
	 * when the other mode registers are restored.
	 */
	ldr	r1, [r0, #SMC_CTX_SCR]
	stcopr	r1, SCR
	isb
Soby Mathew's avatar
Soby Mathew committed
76

77
78
79
80
81
82
	/*
	 * Restore the PMCR register.
	 */
	ldr	r1, [r0, #SMC_CTX_PMCR]
	stcopr	r1, PMCR

83
	/* Restore the banked registers including the current SPSR */
Soby Mathew's avatar
Soby Mathew committed
84
85
86
87
88
89
90
91
92
93
94
95
	add	r1, r0, #SMC_CTX_SP_USR
	ldm	r1!, {r4-r12}
	msr	sp_usr, r4
	msr	lr_usr, r5
	msr	spsr_irq, r6
	msr	sp_irq, r7
	msr	lr_irq, r8
	msr	spsr_fiq, r9
	msr	sp_fiq, r10
	msr	lr_fiq, r11
	msr	spsr_svc, r12

96
	ldm	r1!, {r4-r12}
Soby Mathew's avatar
Soby Mathew committed
97
98
99
100
101
102
103
104
	msr	sp_svc, r4
	msr	lr_svc, r5
	msr	spsr_abt, r6
	msr	sp_abt, r7
	msr	lr_abt, r8
	msr	spsr_und, r9
	msr	sp_und, r10
	msr	lr_und, r11
105
106
107
108
109
110
111
	/*
	 * Use the `_fsxc` suffix explicitly to instruct the assembler
	 * to update all the 32 bits of SPSR. Else, by default, the
	 * assembler assumes `_fc` suffix which only modifies
	 * f->[31:24] and c->[7:0] bits of SPSR.
	 */
	msr	spsr_fsxc, r12
Soby Mathew's avatar
Soby Mathew committed
112

113
114
115
	/* Restore the LR */
	ldr	lr, [r0, #SMC_CTX_LR_MON]

Soby Mathew's avatar
Soby Mathew committed
116
117
	/* Restore the rest of the general purpose registers */
	ldm	r0, {r0-r12}
118
	eret
Soby Mathew's avatar
Soby Mathew committed
119
120
121
	.endm

#endif /* __SMCC_MACROS_S__ */