2 * Copyright (C) 2015 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
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.
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.
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/>.
18 #include <linux/irqflags.h>
20 #include <asm/kvm_hyp.h>
21 #include <asm/kvm_mmu.h>
22 #include <asm/tlbflush.h>
24 static void __hyp_text __tlb_switch_to_guest_vhe(struct kvm *kvm,
29 local_irq_save(*flags);
32 * With VHE enabled, we have HCR_EL2.{E2H,TGE} = {1,1}, and
33 * most TLB operations target EL2/EL0. In order to affect the
34 * guest TLBs (EL1/EL0), we need to change one of these two
35 * bits. Changing E2H is impossible (goodbye TTBR1_EL2), so
36 * let's flip TGE before executing the TLB operation.
38 write_sysreg(kvm->arch.vttbr, vttbr_el2);
39 val = read_sysreg(hcr_el2);
41 write_sysreg(val, hcr_el2);
45 static void __hyp_text __tlb_switch_to_guest_nvhe(struct kvm *kvm,
48 write_sysreg(kvm->arch.vttbr, vttbr_el2);
52 static hyp_alternate_select(__tlb_switch_to_guest,
53 __tlb_switch_to_guest_nvhe,
54 __tlb_switch_to_guest_vhe,
55 ARM64_HAS_VIRT_HOST_EXTN);
57 static void __hyp_text __tlb_switch_to_host_vhe(struct kvm *kvm,
61 * We're done with the TLB operation, let's restore the host's
64 write_sysreg(0, vttbr_el2);
65 write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
67 local_irq_restore(flags);
70 static void __hyp_text __tlb_switch_to_host_nvhe(struct kvm *kvm,
73 write_sysreg(0, vttbr_el2);
76 static hyp_alternate_select(__tlb_switch_to_host,
77 __tlb_switch_to_host_nvhe,
78 __tlb_switch_to_host_vhe,
79 ARM64_HAS_VIRT_HOST_EXTN);
81 void __hyp_text __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
87 /* Switch to requested VMID */
88 kvm = kern_hyp_va(kvm);
89 __tlb_switch_to_guest()(kvm, &flags);
92 * We could do so much better if we had the VA as well.
93 * Instead, we invalidate Stage-2 for this IPA, and the
94 * whole of Stage-1. Weep...
97 __tlbi(ipas2e1is, ipa);
100 * We have to ensure completion of the invalidation at Stage-2,
101 * since a table walk on another CPU could refill a TLB with a
102 * complete (S1 + S2) walk based on the old Stage-2 mapping if
103 * the Stage-1 invalidation happened first.
111 * If the host is running at EL1 and we have a VPIPT I-cache,
112 * then we must perform I-cache maintenance at EL2 in order for
113 * it to have an effect on the guest. Since the guest cannot hit
114 * I-cache lines allocated with a different VMID, we don't need
115 * to worry about junk out of guest reset (we nuke the I-cache on
116 * VMID rollover), but we do need to be careful when remapping
117 * executable pages for the same guest. This can happen when KSM
118 * takes a CoW fault on an executable page, copies the page into
119 * a page that was previously mapped in the guest and then needs
120 * to invalidate the guest view of the I-cache for that page
121 * from EL1. To solve this, we invalidate the entire I-cache when
122 * unmapping a page from a guest if we have a VPIPT I-cache but
123 * the host is running at EL1. As above, we could do better if
126 * The moral of this story is: if you have a VPIPT I-cache, then
127 * you should be running with VHE enabled.
129 if (!has_vhe() && icache_is_vpipt())
130 __flush_icache_all();
132 __tlb_switch_to_host()(kvm, flags);
135 void __hyp_text __kvm_tlb_flush_vmid(struct kvm *kvm)
141 /* Switch to requested VMID */
142 kvm = kern_hyp_va(kvm);
143 __tlb_switch_to_guest()(kvm, &flags);
145 __tlbi(vmalls12e1is);
149 __tlb_switch_to_host()(kvm, flags);
152 void __hyp_text __kvm_tlb_flush_local_vmid(struct kvm_vcpu *vcpu)
154 struct kvm *kvm = kern_hyp_va(kern_hyp_va(vcpu)->kvm);
157 /* Switch to requested VMID */
158 __tlb_switch_to_guest()(kvm, &flags);
164 __tlb_switch_to_host()(kvm, flags);
167 void __hyp_text __kvm_flush_vm_context(void)
171 asm volatile("ic ialluis" : : );