blob: 7edf41061eca2f9d9cc16df8dfb7196d7ffc7796 [file] [log] [blame]
Soby Mathewacc144b2016-05-05 12:53:53 +01001/*
2 * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathewacc144b2016-05-05 12:53:53 +01005 */
6#ifndef __SMCC_MACROS_S__
7#define __SMCC_MACROS_S__
8
9#include <arch.h>
10
11/*
Soby Mathewf3e3a432017-03-30 14:42:54 +010012 * Macro to save the General purpose registers (r0 - r12), the banked
13 * spsr, lr, sp registers and the `scr` register to the SMC context on entry
14 * due a SMC call. The `lr` of the current mode (monitor) is expected to be
15 * already saved. The `sp` must point to the `smc_ctx_t` to save to.
Soby Mathewacc144b2016-05-05 12:53:53 +010016 */
17 .macro smcc_save_gp_mode_regs
Soby Mathewf3e3a432017-03-30 14:42:54 +010018 /* Save r0 - r12 in the SMC context */
19 stm sp, {r0-r12}
20 mov r0, sp
21 add r0, r0, #SMC_CTX_SP_USR
Soby Mathewacc144b2016-05-05 12:53:53 +010022
23 /* Save the banked registers including the current SPSR and LR */
24 mrs r4, sp_usr
25 mrs r5, lr_usr
26 mrs r6, spsr_irq
27 mrs r7, sp_irq
28 mrs r8, lr_irq
29 mrs r9, spsr_fiq
30 mrs r10, sp_fiq
31 mrs r11, lr_fiq
32 mrs r12, spsr_svc
Soby Mathewf3e3a432017-03-30 14:42:54 +010033 stm r0!, {r4-r12}
Soby Mathewacc144b2016-05-05 12:53:53 +010034
35 mrs r4, sp_svc
36 mrs r5, lr_svc
37 mrs r6, spsr_abt
38 mrs r7, sp_abt
39 mrs r8, lr_abt
40 mrs r9, spsr_und
41 mrs r10, sp_und
42 mrs r11, lr_und
43 mrs r12, spsr
Soby Mathewf3e3a432017-03-30 14:42:54 +010044 stm r0!, {r4-r12}
Soby Mathewacc144b2016-05-05 12:53:53 +010045
Soby Mathewf3e3a432017-03-30 14:42:54 +010046 /* lr_mon is already saved by caller */
47 ldcopr r4, SCR
48 str r4, [sp, #SMC_CTX_SCR]
Soby Mathewacc144b2016-05-05 12:53:53 +010049 .endm
50
51/*
Soby Mathewf3e3a432017-03-30 14:42:54 +010052 * Macro to restore the `smc_ctx_t`, which includes the General purpose
53 * registers and banked mode registers, and exit from the monitor mode.
54 * r0 must point to the `smc_ctx_t` to restore from.
Soby Mathewacc144b2016-05-05 12:53:53 +010055 */
Soby Mathewf3e3a432017-03-30 14:42:54 +010056 .macro monitor_exit
57 /*
58 * Save the current sp and restore the smc context
59 * pointer to sp which will be used for handling the
60 * next SMC.
61 */
62 str sp, [r0, #SMC_CTX_SP_MON]
63 mov sp, r0
64
65 /*
66 * Restore SCR first so that we access the right banked register
67 * when the other mode registers are restored.
68 */
69 ldr r1, [r0, #SMC_CTX_SCR]
70 stcopr r1, SCR
71 isb
Soby Mathewacc144b2016-05-05 12:53:53 +010072
Soby Mathewf3e3a432017-03-30 14:42:54 +010073 /* Restore the banked registers including the current SPSR */
Soby Mathewacc144b2016-05-05 12:53:53 +010074 add r1, r0, #SMC_CTX_SP_USR
75 ldm r1!, {r4-r12}
76 msr sp_usr, r4
77 msr lr_usr, r5
78 msr spsr_irq, r6
79 msr sp_irq, r7
80 msr lr_irq, r8
81 msr spsr_fiq, r9
82 msr sp_fiq, r10
83 msr lr_fiq, r11
84 msr spsr_svc, r12
85
Soby Mathewf3e3a432017-03-30 14:42:54 +010086 ldm r1!, {r4-r12}
Soby Mathewacc144b2016-05-05 12:53:53 +010087 msr sp_svc, r4
88 msr lr_svc, r5
89 msr spsr_abt, r6
90 msr sp_abt, r7
91 msr lr_abt, r8
92 msr spsr_und, r9
93 msr sp_und, r10
94 msr lr_und, r11
Yatharth Kochard0f5f9c2016-11-09 15:39:25 +000095 /*
96 * Use the `_fsxc` suffix explicitly to instruct the assembler
97 * to update all the 32 bits of SPSR. Else, by default, the
98 * assembler assumes `_fc` suffix which only modifies
99 * f->[31:24] and c->[7:0] bits of SPSR.
100 */
101 msr spsr_fsxc, r12
Soby Mathewacc144b2016-05-05 12:53:53 +0100102
Soby Mathewf3e3a432017-03-30 14:42:54 +0100103 /* Restore the LR */
104 ldr lr, [r0, #SMC_CTX_LR_MON]
105
Soby Mathewacc144b2016-05-05 12:53:53 +0100106 /* Restore the rest of the general purpose registers */
107 ldm r0, {r0-r12}
Soby Mathewf3e3a432017-03-30 14:42:54 +0100108 eret
Soby Mathewacc144b2016-05-05 12:53:53 +0100109 .endm
110
111#endif /* __SMCC_MACROS_S__ */