tlb.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include <linux/irqflags.h>
  18. #include <asm/kvm_hyp.h>
  19. #include <asm/kvm_mmu.h>
  20. #include <asm/tlbflush.h>
  21. static void __hyp_text __tlb_switch_to_guest_vhe(struct kvm *kvm,
  22. unsigned long *flags)
  23. {
  24. u64 val;
  25. local_irq_save(*flags);
  26. /*
  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.
  32. */
  33. write_sysreg(kvm->arch.vttbr, vttbr_el2);
  34. val = read_sysreg(hcr_el2);
  35. val &= ~HCR_TGE;
  36. write_sysreg(val, hcr_el2);
  37. isb();
  38. }
  39. static void __hyp_text __tlb_switch_to_guest_nvhe(struct kvm *kvm,
  40. unsigned long *flags)
  41. {
  42. write_sysreg(kvm->arch.vttbr, vttbr_el2);
  43. isb();
  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. static void __hyp_text __tlb_switch_to_host_vhe(struct kvm *kvm,
  50. unsigned long flags)
  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. isb();
  59. local_irq_restore(flags);
  60. }
  61. static void __hyp_text __tlb_switch_to_host_nvhe(struct kvm *kvm,
  62. unsigned long flags)
  63. {
  64. write_sysreg(0, vttbr_el2);
  65. }
  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);
  70. void __hyp_text __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
  71. {
  72. unsigned long flags;
  73. dsb(ishst);
  74. /* Switch to requested VMID */
  75. kvm = kern_hyp_va(kvm);
  76. __tlb_switch_to_guest()(kvm, &flags);
  77. /*
  78. * We could do so much better if we had the VA as well.
  79. * Instead, we invalidate Stage-2 for this IPA, and the
  80. * whole of Stage-1. Weep...
  81. */
  82. ipa >>= 12;
  83. __tlbi(ipas2e1is, ipa);
  84. /*
  85. * We have to ensure completion of the invalidation at Stage-2,
  86. * since a table walk on another CPU could refill a TLB with a
  87. * complete (S1 + S2) walk based on the old Stage-2 mapping if
  88. * the Stage-1 invalidation happened first.
  89. */
  90. dsb(ish);
  91. __tlbi(vmalle1is);
  92. dsb(ish);
  93. isb();
  94. /*
  95. * If the host is running at EL1 and we have a VPIPT I-cache,
  96. * then we must perform I-cache maintenance at EL2 in order for
  97. * it to have an effect on the guest. Since the guest cannot hit
  98. * I-cache lines allocated with a different VMID, we don't need
  99. * to worry about junk out of guest reset (we nuke the I-cache on
  100. * VMID rollover), but we do need to be careful when remapping
  101. * executable pages for the same guest. This can happen when KSM
  102. * takes a CoW fault on an executable page, copies the page into
  103. * a page that was previously mapped in the guest and then needs
  104. * to invalidate the guest view of the I-cache for that page
  105. * from EL1. To solve this, we invalidate the entire I-cache when
  106. * unmapping a page from a guest if we have a VPIPT I-cache but
  107. * the host is running at EL1. As above, we could do better if
  108. * we had the VA.
  109. *
  110. * The moral of this story is: if you have a VPIPT I-cache, then
  111. * you should be running with VHE enabled.
  112. */
  113. if (!has_vhe() && icache_is_vpipt())
  114. __flush_icache_all();
  115. __tlb_switch_to_host()(kvm, flags);
  116. }
  117. void __hyp_text __kvm_tlb_flush_vmid(struct kvm *kvm)
  118. {
  119. unsigned long flags;
  120. dsb(ishst);
  121. /* Switch to requested VMID */
  122. kvm = kern_hyp_va(kvm);
  123. __tlb_switch_to_guest()(kvm, &flags);
  124. __tlbi(vmalls12e1is);
  125. dsb(ish);
  126. isb();
  127. __tlb_switch_to_host()(kvm, flags);
  128. }
  129. void __hyp_text __kvm_tlb_flush_local_vmid(struct kvm_vcpu *vcpu)
  130. {
  131. struct kvm *kvm = kern_hyp_va(kern_hyp_va(vcpu)->kvm);
  132. unsigned long flags;
  133. /* Switch to requested VMID */
  134. __tlb_switch_to_guest()(kvm, &flags);
  135. __tlbi(vmalle1);
  136. dsb(nsh);
  137. isb();
  138. __tlb_switch_to_host()(kvm, flags);
  139. }
  140. void __hyp_text __kvm_flush_vm_context(void)
  141. {
  142. dsb(ishst);
  143. __tlbi(alle1is);
  144. asm volatile("ic ialluis" : : );
  145. dsb(ish);
  146. }