tlb.c 4.3 KB

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