GNU Linux-libre 4.9.288-gnu1
[releases.git] / arch / arm64 / kernel / hyp-stub.S
1 /*
2  * Hypervisor stub
3  *
4  * Copyright (C) 2012 ARM Ltd.
5  * Author:      Marc Zyngier <marc.zyngier@arm.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <linux/init.h>
21 #include <linux/linkage.h>
22 #include <linux/irqchip/arm-gic-v3.h>
23
24 #include <asm/assembler.h>
25 #include <asm/kvm_arm.h>
26 #include <asm/kvm_asm.h>
27 #include <asm/ptrace.h>
28 #include <asm/virt.h>
29
30         .text
31         .pushsection    .hyp.text, "ax"
32
33         .align 11
34
35 ENTRY(__hyp_stub_vectors)
36         ventry  el2_sync_invalid                // Synchronous EL2t
37         ventry  el2_irq_invalid                 // IRQ EL2t
38         ventry  el2_fiq_invalid                 // FIQ EL2t
39         ventry  el2_error_invalid               // Error EL2t
40
41         ventry  el2_sync_invalid                // Synchronous EL2h
42         ventry  el2_irq_invalid                 // IRQ EL2h
43         ventry  el2_fiq_invalid                 // FIQ EL2h
44         ventry  el2_error_invalid               // Error EL2h
45
46         ventry  el1_sync                        // Synchronous 64-bit EL1
47         ventry  el1_irq_invalid                 // IRQ 64-bit EL1
48         ventry  el1_fiq_invalid                 // FIQ 64-bit EL1
49         ventry  el1_error_invalid               // Error 64-bit EL1
50
51         ventry  el1_sync_invalid                // Synchronous 32-bit EL1
52         ventry  el1_irq_invalid                 // IRQ 32-bit EL1
53         ventry  el1_fiq_invalid                 // FIQ 32-bit EL1
54         ventry  el1_error_invalid               // Error 32-bit EL1
55 ENDPROC(__hyp_stub_vectors)
56
57         .align 11
58
59 el1_sync:
60         mrs     x30, esr_el2
61         lsr     x30, x30, #ESR_ELx_EC_SHIFT
62
63         cmp     x30, #ESR_ELx_EC_HVC64
64         b.ne    9f                              // Not an HVC trap
65
66         cmp     x0, #HVC_GET_VECTORS
67         b.ne    1f
68         mrs     x0, vbar_el2
69         b       9f
70
71 1:      cmp     x0, #HVC_SET_VECTORS
72         b.ne    2f
73         msr     vbar_el2, x1
74         b       9f
75
76 2:      cmp     x0, #HVC_SOFT_RESTART
77         b.ne    3f
78         mov     x0, x2
79         mov     x2, x4
80         mov     x4, x1
81         mov     x1, x3
82         br      x4                              // no return
83
84         /* Someone called kvm_call_hyp() against the hyp-stub... */
85 3:      mov     x0, #ARM_EXCEPTION_HYP_GONE
86
87 9:      eret
88 ENDPROC(el1_sync)
89
90 .macro invalid_vector   label
91 \label:
92         b \label
93 ENDPROC(\label)
94 .endm
95
96         invalid_vector  el2_sync_invalid
97         invalid_vector  el2_irq_invalid
98         invalid_vector  el2_fiq_invalid
99         invalid_vector  el2_error_invalid
100         invalid_vector  el1_sync_invalid
101         invalid_vector  el1_irq_invalid
102         invalid_vector  el1_fiq_invalid
103         invalid_vector  el1_error_invalid
104
105 /*
106  * __hyp_set_vectors: Call this after boot to set the initial hypervisor
107  * vectors as part of hypervisor installation.  On an SMP system, this should
108  * be called on each CPU.
109  *
110  * x0 must be the physical address of the new vector table, and must be
111  * 2KB aligned.
112  *
113  * Before calling this, you must check that the stub hypervisor is installed
114  * everywhere, by waiting for any secondary CPUs to be brought up and then
115  * checking that is_hyp_mode_available() is true.
116  *
117  * If not, there is a pre-existing hypervisor, some CPUs failed to boot, or
118  * something else went wrong... in such cases, trying to install a new
119  * hypervisor is unlikely to work as desired.
120  *
121  * When you call into your shiny new hypervisor, sp_el2 will contain junk,
122  * so you will need to set that to something sensible at the new hypervisor's
123  * initialisation entry point.
124  */
125
126 ENTRY(__hyp_get_vectors)
127         str     lr, [sp, #-16]!
128         mov     x0, #HVC_GET_VECTORS
129         hvc     #0
130         ldr     lr, [sp], #16
131         ret
132 ENDPROC(__hyp_get_vectors)
133
134 ENTRY(__hyp_set_vectors)
135         str     lr, [sp, #-16]!
136         mov     x1, x0
137         mov     x0, #HVC_SET_VECTORS
138         hvc     #0
139         ldr     lr, [sp], #16
140         ret
141 ENDPROC(__hyp_set_vectors)