]> Git Repo - linux.git/blob - arch/arm64/kvm/hyp/tlb.c
mm/slub.c: add a naive detection of double free or corruption
[linux.git] / arch / arm64 / kvm / hyp / tlb.c
1 /*
2  * Copyright (C) 2015 - ARM Ltd
3  * Author: Marc Zyngier <[email protected]>
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 <asm/kvm_hyp.h>
19 #include <asm/tlbflush.h>
20
21 static void __hyp_text __tlb_switch_to_guest_vhe(struct kvm *kvm)
22 {
23         u64 val;
24
25         /*
26          * With VHE enabled, we have HCR_EL2.{E2H,TGE} = {1,1}, and
27          * most TLB operations target EL2/EL0. In order to affect the
28          * guest TLBs (EL1/EL0), we need to change one of these two
29          * bits. Changing E2H is impossible (goodbye TTBR1_EL2), so
30          * let's flip TGE before executing the TLB operation.
31          */
32         write_sysreg(kvm->arch.vttbr, vttbr_el2);
33         val = read_sysreg(hcr_el2);
34         val &= ~HCR_TGE;
35         write_sysreg(val, hcr_el2);
36         isb();
37 }
38
39 static void __hyp_text __tlb_switch_to_guest_nvhe(struct kvm *kvm)
40 {
41         write_sysreg(kvm->arch.vttbr, vttbr_el2);
42         isb();
43 }
44
45 static hyp_alternate_select(__tlb_switch_to_guest,
46                             __tlb_switch_to_guest_nvhe,
47                             __tlb_switch_to_guest_vhe,
48                             ARM64_HAS_VIRT_HOST_EXTN);
49
50 static void __hyp_text __tlb_switch_to_host_vhe(struct kvm *kvm)
51 {
52         /*
53          * We're done with the TLB operation, let's restore the host's
54          * view of HCR_EL2.
55          */
56         write_sysreg(0, vttbr_el2);
57         write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
58 }
59
60 static void __hyp_text __tlb_switch_to_host_nvhe(struct kvm *kvm)
61 {
62         write_sysreg(0, vttbr_el2);
63 }
64
65 static hyp_alternate_select(__tlb_switch_to_host,
66                             __tlb_switch_to_host_nvhe,
67                             __tlb_switch_to_host_vhe,
68                             ARM64_HAS_VIRT_HOST_EXTN);
69
70 void __hyp_text __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
71 {
72         dsb(ishst);
73
74         /* Switch to requested VMID */
75         kvm = kern_hyp_va(kvm);
76         __tlb_switch_to_guest()(kvm);
77
78         /*
79          * We could do so much better if we had the VA as well.
80          * Instead, we invalidate Stage-2 for this IPA, and the
81          * whole of Stage-1. Weep...
82          */
83         ipa >>= 12;
84         __tlbi(ipas2e1is, ipa);
85
86         /*
87          * We have to ensure completion of the invalidation at Stage-2,
88          * since a table walk on another CPU could refill a TLB with a
89          * complete (S1 + S2) walk based on the old Stage-2 mapping if
90          * the Stage-1 invalidation happened first.
91          */
92         dsb(ish);
93         __tlbi(vmalle1is);
94         dsb(ish);
95         isb();
96
97         /*
98          * If the host is running at EL1 and we have a VPIPT I-cache,
99          * then we must perform I-cache maintenance at EL2 in order for
100          * it to have an effect on the guest. Since the guest cannot hit
101          * I-cache lines allocated with a different VMID, we don't need
102          * to worry about junk out of guest reset (we nuke the I-cache on
103          * VMID rollover), but we do need to be careful when remapping
104          * executable pages for the same guest. This can happen when KSM
105          * takes a CoW fault on an executable page, copies the page into
106          * a page that was previously mapped in the guest and then needs
107          * to invalidate the guest view of the I-cache for that page
108          * from EL1. To solve this, we invalidate the entire I-cache when
109          * unmapping a page from a guest if we have a VPIPT I-cache but
110          * the host is running at EL1. As above, we could do better if
111          * we had the VA.
112          *
113          * The moral of this story is: if you have a VPIPT I-cache, then
114          * you should be running with VHE enabled.
115          */
116         if (!has_vhe() && icache_is_vpipt())
117                 __flush_icache_all();
118
119         __tlb_switch_to_host()(kvm);
120 }
121
122 void __hyp_text __kvm_tlb_flush_vmid(struct kvm *kvm)
123 {
124         dsb(ishst);
125
126         /* Switch to requested VMID */
127         kvm = kern_hyp_va(kvm);
128         __tlb_switch_to_guest()(kvm);
129
130         __tlbi(vmalls12e1is);
131         dsb(ish);
132         isb();
133
134         __tlb_switch_to_host()(kvm);
135 }
136
137 void __hyp_text __kvm_tlb_flush_local_vmid(struct kvm_vcpu *vcpu)
138 {
139         struct kvm *kvm = kern_hyp_va(kern_hyp_va(vcpu)->kvm);
140
141         /* Switch to requested VMID */
142         __tlb_switch_to_guest()(kvm);
143
144         __tlbi(vmalle1);
145         dsb(nsh);
146         isb();
147
148         __tlb_switch_to_host()(kvm);
149 }
150
151 void __hyp_text __kvm_flush_vm_context(void)
152 {
153         dsb(ishst);
154         __tlbi(alle1is);
155         asm volatile("ic ialluis" : : );
156         dsb(ish);
157 }
This page took 0.041191 seconds and 4 git commands to generate.