tlb.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. static void __hyp_text __tlb_switch_to_guest_vhe(struct kvm *kvm)
  19. {
  20. u64 val;
  21. /*
  22. * With VHE enabled, we have HCR_EL2.{E2H,TGE} = {1,1}, and
  23. * most TLB operations target EL2/EL0. In order to affect the
  24. * guest TLBs (EL1/EL0), we need to change one of these two
  25. * bits. Changing E2H is impossible (goodbye TTBR1_EL2), so
  26. * let's flip TGE before executing the TLB operation.
  27. */
  28. write_sysreg(kvm->arch.vttbr, vttbr_el2);
  29. val = read_sysreg(hcr_el2);
  30. val &= ~HCR_TGE;
  31. write_sysreg(val, hcr_el2);
  32. isb();
  33. }
  34. static void __hyp_text __tlb_switch_to_guest_nvhe(struct kvm *kvm)
  35. {
  36. write_sysreg(kvm->arch.vttbr, vttbr_el2);
  37. isb();
  38. }
  39. static hyp_alternate_select(__tlb_switch_to_guest,
  40. __tlb_switch_to_guest_nvhe,
  41. __tlb_switch_to_guest_vhe,
  42. ARM64_HAS_VIRT_HOST_EXTN);
  43. static void __hyp_text __tlb_switch_to_host_vhe(struct kvm *kvm)
  44. {
  45. /*
  46. * We're done with the TLB operation, let's restore the host's
  47. * view of HCR_EL2.
  48. */
  49. write_sysreg(0, vttbr_el2);
  50. write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
  51. }
  52. static void __hyp_text __tlb_switch_to_host_nvhe(struct kvm *kvm)
  53. {
  54. write_sysreg(0, vttbr_el2);
  55. }
  56. static hyp_alternate_select(__tlb_switch_to_host,
  57. __tlb_switch_to_host_nvhe,
  58. __tlb_switch_to_host_vhe,
  59. ARM64_HAS_VIRT_HOST_EXTN);
  60. void __hyp_text __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa)
  61. {
  62. dsb(ishst);
  63. /* Switch to requested VMID */
  64. kvm = kern_hyp_va(kvm);
  65. __tlb_switch_to_guest()(kvm);
  66. /*
  67. * We could do so much better if we had the VA as well.
  68. * Instead, we invalidate Stage-2 for this IPA, and the
  69. * whole of Stage-1. Weep...
  70. */
  71. ipa >>= 12;
  72. asm volatile("tlbi ipas2e1is, %0" : : "r" (ipa));
  73. /*
  74. * We have to ensure completion of the invalidation at Stage-2,
  75. * since a table walk on another CPU could refill a TLB with a
  76. * complete (S1 + S2) walk based on the old Stage-2 mapping if
  77. * the Stage-1 invalidation happened first.
  78. */
  79. dsb(ish);
  80. asm volatile("tlbi vmalle1is" : : );
  81. dsb(ish);
  82. isb();
  83. __tlb_switch_to_host()(kvm);
  84. }
  85. void __hyp_text __kvm_tlb_flush_vmid(struct kvm *kvm)
  86. {
  87. dsb(ishst);
  88. /* Switch to requested VMID */
  89. kvm = kern_hyp_va(kvm);
  90. __tlb_switch_to_guest()(kvm);
  91. asm volatile("tlbi vmalls12e1is" : : );
  92. dsb(ish);
  93. isb();
  94. __tlb_switch_to_host()(kvm);
  95. }
  96. void __hyp_text __kvm_tlb_flush_local_vmid(struct kvm_vcpu *vcpu)
  97. {
  98. struct kvm *kvm = kern_hyp_va(kern_hyp_va(vcpu)->kvm);
  99. /* Switch to requested VMID */
  100. __tlb_switch_to_guest()(kvm);
  101. asm volatile("tlbi vmalle1" : : );
  102. dsb(nsh);
  103. isb();
  104. __tlb_switch_to_host()(kvm);
  105. }
  106. void __hyp_text __kvm_flush_vm_context(void)
  107. {
  108. dsb(ishst);
  109. asm volatile("tlbi alle1is \n"
  110. "ic ialluis ": : );
  111. dsb(ish);
  112. }