Soby Mathew | acc144b | 2016-05-05 12:53:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | acc144b | 2016-05-05 12:53:53 +0100 | [diff] [blame] | 5 | */ |
| 6 | #ifndef __SMCC_MACROS_S__ |
| 7 | #define __SMCC_MACROS_S__ |
| 8 | |
| 9 | #include <arch.h> |
| 10 | |
| 11 | /* |
Soby Mathew | f3e3a43 | 2017-03-30 14:42:54 +0100 | [diff] [blame] | 12 | * 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 Mathew | acc144b | 2016-05-05 12:53:53 +0100 | [diff] [blame] | 16 | */ |
| 17 | .macro smcc_save_gp_mode_regs |
Soby Mathew | f3e3a43 | 2017-03-30 14:42:54 +0100 | [diff] [blame] | 18 | /* 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 Mathew | acc144b | 2016-05-05 12:53:53 +0100 | [diff] [blame] | 22 | |
| 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 Mathew | f3e3a43 | 2017-03-30 14:42:54 +0100 | [diff] [blame] | 33 | stm r0!, {r4-r12} |
Soby Mathew | acc144b | 2016-05-05 12:53:53 +0100 | [diff] [blame] | 34 | |
| 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 Mathew | f3e3a43 | 2017-03-30 14:42:54 +0100 | [diff] [blame] | 44 | stm r0!, {r4-r12} |
Soby Mathew | acc144b | 2016-05-05 12:53:53 +0100 | [diff] [blame] | 45 | |
Soby Mathew | f3e3a43 | 2017-03-30 14:42:54 +0100 | [diff] [blame] | 46 | /* lr_mon is already saved by caller */ |
| 47 | ldcopr r4, SCR |
| 48 | str r4, [sp, #SMC_CTX_SCR] |
Soby Mathew | acc144b | 2016-05-05 12:53:53 +0100 | [diff] [blame] | 49 | .endm |
| 50 | |
| 51 | /* |
Soby Mathew | f3e3a43 | 2017-03-30 14:42:54 +0100 | [diff] [blame] | 52 | * 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 Mathew | acc144b | 2016-05-05 12:53:53 +0100 | [diff] [blame] | 55 | */ |
Soby Mathew | f3e3a43 | 2017-03-30 14:42:54 +0100 | [diff] [blame] | 56 | .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 Mathew | acc144b | 2016-05-05 12:53:53 +0100 | [diff] [blame] | 72 | |
Soby Mathew | f3e3a43 | 2017-03-30 14:42:54 +0100 | [diff] [blame] | 73 | /* Restore the banked registers including the current SPSR */ |
Soby Mathew | acc144b | 2016-05-05 12:53:53 +0100 | [diff] [blame] | 74 | 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 Mathew | f3e3a43 | 2017-03-30 14:42:54 +0100 | [diff] [blame] | 86 | ldm r1!, {r4-r12} |
Soby Mathew | acc144b | 2016-05-05 12:53:53 +0100 | [diff] [blame] | 87 | 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 Kochar | d0f5f9c | 2016-11-09 15:39:25 +0000 | [diff] [blame] | 95 | /* |
| 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 Mathew | acc144b | 2016-05-05 12:53:53 +0100 | [diff] [blame] | 102 | |
Soby Mathew | f3e3a43 | 2017-03-30 14:42:54 +0100 | [diff] [blame] | 103 | /* Restore the LR */ |
| 104 | ldr lr, [r0, #SMC_CTX_LR_MON] |
| 105 | |
Soby Mathew | acc144b | 2016-05-05 12:53:53 +0100 | [diff] [blame] | 106 | /* Restore the rest of the general purpose registers */ |
| 107 | ldm r0, {r0-r12} |
Soby Mathew | f3e3a43 | 2017-03-30 14:42:54 +0100 | [diff] [blame] | 108 | eret |
Soby Mathew | acc144b | 2016-05-05 12:53:53 +0100 | [diff] [blame] | 109 | .endm |
| 110 | |
| 111 | #endif /* __SMCC_MACROS_S__ */ |