tdp_iter.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "mmu_internal.h"
  3. #include "tdp_iter.h"
  4. #include "spte.h"
  5. /*
  6. * Recalculates the pointer to the SPTE for the current GFN and level and
  7. * reread the SPTE.
  8. */
  9. static void tdp_iter_refresh_sptep(struct tdp_iter *iter)
  10. {
  11. iter->sptep = iter->pt_path[iter->level - 1] +
  12. SPTE_INDEX(iter->gfn << PAGE_SHIFT, iter->level);
  13. iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);
  14. }
  15. static gfn_t round_gfn_for_level(gfn_t gfn, int level)
  16. {
  17. return gfn & -KVM_PAGES_PER_HPAGE(level);
  18. }
  19. /*
  20. * Return the TDP iterator to the root PT and allow it to continue its
  21. * traversal over the paging structure from there.
  22. */
  23. void tdp_iter_restart(struct tdp_iter *iter)
  24. {
  25. iter->yielded = false;
  26. iter->yielded_gfn = iter->next_last_level_gfn;
  27. iter->level = iter->root_level;
  28. iter->gfn = round_gfn_for_level(iter->next_last_level_gfn, iter->level);
  29. tdp_iter_refresh_sptep(iter);
  30. iter->valid = true;
  31. }
  32. /*
  33. * Sets a TDP iterator to walk a pre-order traversal of the paging structure
  34. * rooted at root_pt, starting with the walk to translate next_last_level_gfn.
  35. */
  36. void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root,
  37. int min_level, gfn_t next_last_level_gfn)
  38. {
  39. int root_level = root->role.level;
  40. WARN_ON(root_level < 1);
  41. WARN_ON(root_level > PT64_ROOT_MAX_LEVEL);
  42. iter->next_last_level_gfn = next_last_level_gfn;
  43. iter->root_level = root_level;
  44. iter->min_level = min_level;
  45. iter->pt_path[iter->root_level - 1] = (tdp_ptep_t)root->spt;
  46. iter->as_id = kvm_mmu_page_as_id(root);
  47. tdp_iter_restart(iter);
  48. }
  49. /*
  50. * Given an SPTE and its level, returns a pointer containing the host virtual
  51. * address of the child page table referenced by the SPTE. Returns null if
  52. * there is no such entry.
  53. */
  54. tdp_ptep_t spte_to_child_pt(u64 spte, int level)
  55. {
  56. /*
  57. * There's no child entry if this entry isn't present or is a
  58. * last-level entry.
  59. */
  60. if (!is_shadow_present_pte(spte) || is_last_spte(spte, level))
  61. return NULL;
  62. return (tdp_ptep_t)__va(spte_to_pfn(spte) << PAGE_SHIFT);
  63. }
  64. /*
  65. * Steps down one level in the paging structure towards the goal GFN. Returns
  66. * true if the iterator was able to step down a level, false otherwise.
  67. */
  68. static bool try_step_down(struct tdp_iter *iter)
  69. {
  70. tdp_ptep_t child_pt;
  71. if (iter->level == iter->min_level)
  72. return false;
  73. /*
  74. * Reread the SPTE before stepping down to avoid traversing into page
  75. * tables that are no longer linked from this entry.
  76. */
  77. iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);
  78. child_pt = spte_to_child_pt(iter->old_spte, iter->level);
  79. if (!child_pt)
  80. return false;
  81. iter->level--;
  82. iter->pt_path[iter->level - 1] = child_pt;
  83. iter->gfn = round_gfn_for_level(iter->next_last_level_gfn, iter->level);
  84. tdp_iter_refresh_sptep(iter);
  85. return true;
  86. }
  87. /*
  88. * Steps to the next entry in the current page table, at the current page table
  89. * level. The next entry could point to a page backing guest memory or another
  90. * page table, or it could be non-present. Returns true if the iterator was
  91. * able to step to the next entry in the page table, false if the iterator was
  92. * already at the end of the current page table.
  93. */
  94. static bool try_step_side(struct tdp_iter *iter)
  95. {
  96. /*
  97. * Check if the iterator is already at the end of the current page
  98. * table.
  99. */
  100. if (SPTE_INDEX(iter->gfn << PAGE_SHIFT, iter->level) ==
  101. (SPTE_ENT_PER_PAGE - 1))
  102. return false;
  103. iter->gfn += KVM_PAGES_PER_HPAGE(iter->level);
  104. iter->next_last_level_gfn = iter->gfn;
  105. iter->sptep++;
  106. iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);
  107. return true;
  108. }
  109. /*
  110. * Tries to traverse back up a level in the paging structure so that the walk
  111. * can continue from the next entry in the parent page table. Returns true on a
  112. * successful step up, false if already in the root page.
  113. */
  114. static bool try_step_up(struct tdp_iter *iter)
  115. {
  116. if (iter->level == iter->root_level)
  117. return false;
  118. iter->level++;
  119. iter->gfn = round_gfn_for_level(iter->gfn, iter->level);
  120. tdp_iter_refresh_sptep(iter);
  121. return true;
  122. }
  123. /*
  124. * Step to the next SPTE in a pre-order traversal of the paging structure.
  125. * To get to the next SPTE, the iterator either steps down towards the goal
  126. * GFN, if at a present, non-last-level SPTE, or over to a SPTE mapping a
  127. * highter GFN.
  128. *
  129. * The basic algorithm is as follows:
  130. * 1. If the current SPTE is a non-last-level SPTE, step down into the page
  131. * table it points to.
  132. * 2. If the iterator cannot step down, it will try to step to the next SPTE
  133. * in the current page of the paging structure.
  134. * 3. If the iterator cannot step to the next entry in the current page, it will
  135. * try to step up to the parent paging structure page. In this case, that
  136. * SPTE will have already been visited, and so the iterator must also step
  137. * to the side again.
  138. */
  139. void tdp_iter_next(struct tdp_iter *iter)
  140. {
  141. if (iter->yielded) {
  142. tdp_iter_restart(iter);
  143. return;
  144. }
  145. if (try_step_down(iter))
  146. return;
  147. do {
  148. if (try_step_side(iter))
  149. return;
  150. } while (try_step_up(iter));
  151. iter->valid = false;
  152. }