arm64: KVM: Optimize __guest_enter/exit() to save a few instructions
[cascardo/linux.git] / arch / arm64 / kvm / hyp / 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/alternative.h>
21 #include <asm/assembler.h>
22 #include <asm/cpufeature.h>
23 #include <asm/kvm_arm.h>
24 #include <asm/kvm_asm.h>
25 #include <asm/kvm_mmu.h>
26
27         .text
28         .pushsection    .hyp.text, "ax"
29
30 .macro do_el2_call
31         /*
32          * Shuffle the parameters before calling the function
33          * pointed to in x0. Assumes parameters in x[1,2,3].
34          */
35         mov     lr, x0
36         mov     x0, x1
37         mov     x1, x2
38         mov     x2, x3
39         blr     lr
40 .endm
41
42 ENTRY(__vhe_hyp_call)
43         str     lr, [sp, #-16]!
44         do_el2_call
45         ldr     lr, [sp], #16
46         /*
47          * We used to rely on having an exception return to get
48          * an implicit isb. In the E2H case, we don't have it anymore.
49          * rather than changing all the leaf functions, just do it here
50          * before returning to the rest of the kernel.
51          */
52         isb
53         ret
54 ENDPROC(__vhe_hyp_call)
55
56 /*
57  * Compute the idmap address of __kvm_hyp_reset based on the idmap
58  * start passed as a parameter, and jump there.
59  *
60  * x0: HYP phys_idmap_start
61  */
62 ENTRY(__kvm_hyp_teardown)
63         mov     x4, x0
64         adr_l   x3, __kvm_hyp_reset
65
66         /* insert __kvm_hyp_reset()s offset into phys_idmap_start */
67         bfi     x4, x3, #0, #PAGE_SHIFT
68         br      x4
69 ENDPROC(__kvm_hyp_teardown)
70         
71 el1_sync:                               // Guest trapped into EL2
72         stp     x0, x1, [sp, #-16]!
73
74 alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
75         mrs     x1, esr_el2
76 alternative_else
77         mrs     x1, esr_el1
78 alternative_endif
79         lsr     x0, x1, #ESR_ELx_EC_SHIFT
80
81         cmp     x0, #ESR_ELx_EC_HVC64
82         b.ne    el1_trap
83
84         mrs     x1, vttbr_el2           // If vttbr is valid, the 64bit guest
85         cbnz    x1, el1_trap            // called HVC
86
87         /* Here, we're pretty sure the host called HVC. */
88         ldp     x0, x1, [sp], #16
89
90         cmp     x0, #HVC_GET_VECTORS
91         b.ne    1f
92         mrs     x0, vbar_el2
93         b       2f
94
95 1:
96         /*
97          * Perform the EL2 call
98          */
99         kern_hyp_va     x0
100         do_el2_call
101
102 2:      eret
103
104 el1_trap:
105         /*
106          * x0: ESR_EC
107          */
108
109         /* Guest accessed VFP/SIMD registers, save host, restore Guest */
110         cmp     x0, #ESR_ELx_EC_FP_ASIMD
111         b.eq    __fpsimd_guest_restore
112
113         mrs     x1, tpidr_el2
114         mov     x0, #ARM_EXCEPTION_TRAP
115         b       __guest_exit
116
117 el1_irq:
118         stp     x0, x1, [sp, #-16]!
119         mrs     x1, tpidr_el2
120         mov     x0, #ARM_EXCEPTION_IRQ
121         b       __guest_exit
122
123 ENTRY(__hyp_do_panic)
124         mov     lr, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT |\
125                       PSR_MODE_EL1h)
126         msr     spsr_el2, lr
127         ldr     lr, =panic
128         msr     elr_el2, lr
129         eret
130 ENDPROC(__hyp_do_panic)
131
132 .macro invalid_vector   label, target = __hyp_panic
133         .align  2
134 \label:
135         b \target
136 ENDPROC(\label)
137 .endm
138
139         /* None of these should ever happen */
140         invalid_vector  el2t_sync_invalid
141         invalid_vector  el2t_irq_invalid
142         invalid_vector  el2t_fiq_invalid
143         invalid_vector  el2t_error_invalid
144         invalid_vector  el2h_sync_invalid
145         invalid_vector  el2h_irq_invalid
146         invalid_vector  el2h_fiq_invalid
147         invalid_vector  el2h_error_invalid
148         invalid_vector  el1_sync_invalid
149         invalid_vector  el1_irq_invalid
150         invalid_vector  el1_fiq_invalid
151         invalid_vector  el1_error_invalid
152
153         .ltorg
154
155         .align 11
156
157 ENTRY(__kvm_hyp_vector)
158         ventry  el2t_sync_invalid               // Synchronous EL2t
159         ventry  el2t_irq_invalid                // IRQ EL2t
160         ventry  el2t_fiq_invalid                // FIQ EL2t
161         ventry  el2t_error_invalid              // Error EL2t
162
163         ventry  el2h_sync_invalid               // Synchronous EL2h
164         ventry  el2h_irq_invalid                // IRQ EL2h
165         ventry  el2h_fiq_invalid                // FIQ EL2h
166         ventry  el2h_error_invalid              // Error EL2h
167
168         ventry  el1_sync                        // Synchronous 64-bit EL1
169         ventry  el1_irq                         // IRQ 64-bit EL1
170         ventry  el1_fiq_invalid                 // FIQ 64-bit EL1
171         ventry  el1_error_invalid               // Error 64-bit EL1
172
173         ventry  el1_sync                        // Synchronous 32-bit EL1
174         ventry  el1_irq                         // IRQ 32-bit EL1
175         ventry  el1_fiq_invalid                 // FIQ 32-bit EL1
176         ventry  el1_error_invalid               // Error 32-bit EL1
177 ENDPROC(__kvm_hyp_vector)