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 <asm/kvm_hyp.h>
19 #include <asm/kvm_mmu.h>
20 #include <asm/tlbflush.h>
22 static void __hyp_text __tlb_switch_to_guest_vhe(struct kvm *kvm)
27 * With VHE enabled, we have HCR_EL2.{E2H,TGE} = {1,1}, and
28 * most TLB operations target EL2/EL0. In order to affect the
29 * guest TLBs (EL1/EL0), we need to change one of these two
30 * bits. Changing E2H is impossible (goodbye TTBR1_EL2), so
31 * let's flip TGE before executing the TLB operation.
33 write_sysreg(kvm->arch.vttbr, vttbr_el2);
34 val = read_sysreg(hcr_el2);
36 write_sysreg(val, hcr_el2);
40 static void __hyp_text __tlb_switch_to_guest_nvhe(struct kvm *kvm)
42 write_sysreg(kvm->arch.vttbr, vttbr_el2);
46 static hyp_alternate_select(__tlb_switch_to_guest,
47 __tlb_switch_to_guest_nvhe,
48 __tlb_switch_to_guest_vhe,
49 ARM64_HAS_VIRT_HOST_EXTN);
51 static void __hyp_text __tlb_switch_to_host_vhe(struct kvm *kvm)
54 * We're done with the TLB operation, let's restore the host's
57 write_sysreg(0, vttbr_el2);
58 write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
61 static void __hyp_text __tlb_switch_to_host_nvhe(struct kvm *kvm)
63 write_sysreg(0, vttbr_el2);
66 static hyp_alternate_select(__tlb_switch_to_host,
67 __tlb_switch_to_host_nvhe,
68 __tlb_switch_to_host_vhe,
69 ARM64_HAS_VIRT_HOST_EXTN);
71 void __hyp_text __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
75 /* Switch to requested VMID */
76 kvm = kern_hyp_va(kvm);
77 __tlb_switch_to_guest()(kvm);
80 * We could do so much better if we had the VA as well.
81 * Instead, we invalidate Stage-2 for this IPA, and the
82 * whole of Stage-1. Weep...
85 __tlbi(ipas2e1is, ipa);
88 * We have to ensure completion of the invalidation at Stage-2,
89 * since a table walk on another CPU could refill a TLB with a
90 * complete (S1 + S2) walk based on the old Stage-2 mapping if
91 * the Stage-1 invalidation happened first.
99 * If the host is running at EL1 and we have a VPIPT I-cache,
100 * then we must perform I-cache maintenance at EL2 in order for
101 * it to have an effect on the guest. Since the guest cannot hit
102 * I-cache lines allocated with a different VMID, we don't need
103 * to worry about junk out of guest reset (we nuke the I-cache on
104 * VMID rollover), but we do need to be careful when remapping
105 * executable pages for the same guest. This can happen when KSM
106 * takes a CoW fault on an executable page, copies the page into
107 * a page that was previously mapped in the guest and then needs
108 * to invalidate the guest view of the I-cache for that page
109 * from EL1. To solve this, we invalidate the entire I-cache when
110 * unmapping a page from a guest if we have a VPIPT I-cache but
111 * the host is running at EL1. As above, we could do better if
114 * The moral of this story is: if you have a VPIPT I-cache, then
115 * you should be running with VHE enabled.
117 if (!has_vhe() && icache_is_vpipt())
118 __flush_icache_all();
120 __tlb_switch_to_host()(kvm);
123 void __hyp_text __kvm_tlb_flush_vmid(struct kvm *kvm)
127 /* Switch to requested VMID */
128 kvm = kern_hyp_va(kvm);
129 __tlb_switch_to_guest()(kvm);
131 __tlbi(vmalls12e1is);
135 __tlb_switch_to_host()(kvm);
138 void __hyp_text __kvm_tlb_flush_local_vmid(struct kvm_vcpu *vcpu)
140 struct kvm *kvm = kern_hyp_va(kern_hyp_va(vcpu)->kvm);
142 /* Switch to requested VMID */
143 __tlb_switch_to_guest()(kvm);
149 __tlb_switch_to_host()(kvm);
152 void __hyp_text __kvm_flush_vm_context(void)
156 asm volatile("ic ialluis" : : );