arm64: KVM: Optimize __guest_enter/exit() to save a few instructions
[cascardo/linux.git] / arch / arm64 / kvm / hyp / entry.S
1 /*
2  * Copyright (C) 2015 - ARM Ltd
3  * Author: Marc Zyngier <marc.zyngier@arm.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <linux/linkage.h>
19
20 #include <asm/asm-offsets.h>
21 #include <asm/assembler.h>
22 #include <asm/fpsimdmacros.h>
23 #include <asm/kvm.h>
24 #include <asm/kvm_arm.h>
25 #include <asm/kvm_asm.h>
26 #include <asm/kvm_mmu.h>
27
28 #define CPU_GP_REG_OFFSET(x)    (CPU_GP_REGS + x)
29 #define CPU_XREG_OFFSET(x)      CPU_GP_REG_OFFSET(CPU_USER_PT_REGS + 8*x)
30
31         .text
32         .pushsection    .hyp.text, "ax"
33
34 .macro save_callee_saved_regs ctxt
35         stp     x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)]
36         stp     x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)]
37         stp     x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)]
38         stp     x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)]
39         stp     x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)]
40         stp     x29, lr,  [\ctxt, #CPU_XREG_OFFSET(29)]
41 .endm
42
43 .macro restore_callee_saved_regs ctxt
44         ldp     x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)]
45         ldp     x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)]
46         ldp     x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)]
47         ldp     x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)]
48         ldp     x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)]
49         ldp     x29, lr,  [\ctxt, #CPU_XREG_OFFSET(29)]
50 .endm
51
52 /*
53  * u64 __guest_enter(struct kvm_vcpu *vcpu,
54  *                   struct kvm_cpu_context *host_ctxt);
55  */
56 ENTRY(__guest_enter)
57         // x0: vcpu
58         // x1: host context
59         // x2-x17: clobbered by macros
60         // x18: guest context
61
62         // Store the host regs
63         save_callee_saved_regs x1
64
65         // Store the host_ctxt for use at exit time
66         str     x1, [sp, #-16]!
67
68         add     x18, x0, #VCPU_CONTEXT
69
70         // Restore guest regs x0-x17
71         ldp     x0, x1,   [x18, #CPU_XREG_OFFSET(0)]
72         ldp     x2, x3,   [x18, #CPU_XREG_OFFSET(2)]
73         ldp     x4, x5,   [x18, #CPU_XREG_OFFSET(4)]
74         ldp     x6, x7,   [x18, #CPU_XREG_OFFSET(6)]
75         ldp     x8, x9,   [x18, #CPU_XREG_OFFSET(8)]
76         ldp     x10, x11, [x18, #CPU_XREG_OFFSET(10)]
77         ldp     x12, x13, [x18, #CPU_XREG_OFFSET(12)]
78         ldp     x14, x15, [x18, #CPU_XREG_OFFSET(14)]
79         ldp     x16, x17, [x18, #CPU_XREG_OFFSET(16)]
80
81         // Restore guest regs x19-x29, lr
82         restore_callee_saved_regs x18
83
84         // Restore guest reg x18
85         ldr     x18,      [x18, #CPU_XREG_OFFSET(18)]
86
87         // Do not touch any register after this!
88         eret
89 ENDPROC(__guest_enter)
90
91 ENTRY(__guest_exit)
92         // x0: return code
93         // x1: vcpu
94         // x2-x29,lr: vcpu regs
95         // vcpu x0-x1 on the stack
96
97         add     x1, x1, #VCPU_CONTEXT
98
99         // Store the guest regs x2 and x3
100         stp     x2, x3,   [x1, #CPU_XREG_OFFSET(2)]
101
102         // Retrieve the guest regs x0-x1 from the stack
103         ldp     x2, x3, [sp], #16       // x0, x1
104
105         // Store the guest regs x0-x1 and x4-x18
106         stp     x2, x3,   [x1, #CPU_XREG_OFFSET(0)]
107         stp     x4, x5,   [x1, #CPU_XREG_OFFSET(4)]
108         stp     x6, x7,   [x1, #CPU_XREG_OFFSET(6)]
109         stp     x8, x9,   [x1, #CPU_XREG_OFFSET(8)]
110         stp     x10, x11, [x1, #CPU_XREG_OFFSET(10)]
111         stp     x12, x13, [x1, #CPU_XREG_OFFSET(12)]
112         stp     x14, x15, [x1, #CPU_XREG_OFFSET(14)]
113         stp     x16, x17, [x1, #CPU_XREG_OFFSET(16)]
114         str     x18,      [x1, #CPU_XREG_OFFSET(18)]
115
116         // Store the guest regs x19-x29, lr
117         save_callee_saved_regs x1
118
119         // Restore the host_ctxt from the stack
120         ldr     x2, [sp], #16
121
122         // Now restore the host regs
123         restore_callee_saved_regs x2
124
125         ret
126 ENDPROC(__guest_exit)
127
128 ENTRY(__fpsimd_guest_restore)
129         stp     x2, x3, [sp, #-16]!
130         stp     x4, lr, [sp, #-16]!
131
132 alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
133         mrs     x2, cptr_el2
134         bic     x2, x2, #CPTR_EL2_TFP
135         msr     cptr_el2, x2
136 alternative_else
137         mrs     x2, cpacr_el1
138         orr     x2, x2, #CPACR_EL1_FPEN
139         msr     cpacr_el1, x2
140 alternative_endif
141         isb
142
143         mrs     x3, tpidr_el2
144
145         ldr     x0, [x3, #VCPU_HOST_CONTEXT]
146         kern_hyp_va x0
147         add     x0, x0, #CPU_GP_REG_OFFSET(CPU_FP_REGS)
148         bl      __fpsimd_save_state
149
150         add     x2, x3, #VCPU_CONTEXT
151         add     x0, x2, #CPU_GP_REG_OFFSET(CPU_FP_REGS)
152         bl      __fpsimd_restore_state
153
154         // Skip restoring fpexc32 for AArch64 guests
155         mrs     x1, hcr_el2
156         tbnz    x1, #HCR_RW_SHIFT, 1f
157         ldr     x4, [x3, #VCPU_FPEXC32_EL2]
158         msr     fpexc32_el2, x4
159 1:
160         ldp     x4, lr, [sp], #16
161         ldp     x2, x3, [sp], #16
162         ldp     x0, x1, [sp], #16
163
164         eret
165 ENDPROC(__fpsimd_guest_restore)