// SPDX-License-Identifier: GPL-2.01#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt23#include "mmu_internal.h"4#include "tdp_iter.h"5#include "spte.h"67/*8* Recalculates the pointer to the SPTE for the current GFN and level and9* reread the SPTE.10*/11static void tdp_iter_refresh_sptep(struct tdp_iter *iter)12{13iter->sptep = iter->pt_path[iter->level - 1] +14SPTE_INDEX((iter->gfn | iter->gfn_bits) << PAGE_SHIFT, iter->level);15iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);16}1718/*19* Return the TDP iterator to the root PT and allow it to continue its20* traversal over the paging structure from there.21*/22void tdp_iter_restart(struct tdp_iter *iter)23{24iter->yielded = false;25iter->yielded_gfn = iter->next_last_level_gfn;26iter->level = iter->root_level;2728iter->gfn = gfn_round_for_level(iter->next_last_level_gfn, iter->level);29tdp_iter_refresh_sptep(iter);3031iter->valid = true;32}3334/*35* Sets a TDP iterator to walk a pre-order traversal of the paging structure36* rooted at root_pt, starting with the walk to translate next_last_level_gfn.37*/38void tdp_iter_start(struct tdp_iter *iter, struct kvm_mmu_page *root,39int min_level, gfn_t next_last_level_gfn, gfn_t gfn_bits)40{41if (WARN_ON_ONCE(!root || (root->role.level < 1) ||42(root->role.level > PT64_ROOT_MAX_LEVEL) ||43(gfn_bits && next_last_level_gfn >= gfn_bits))) {44iter->valid = false;45return;46}4748iter->next_last_level_gfn = next_last_level_gfn;49iter->gfn_bits = gfn_bits;50iter->root_level = root->role.level;51iter->min_level = min_level;52iter->pt_path[iter->root_level - 1] = (tdp_ptep_t)root->spt;53iter->as_id = kvm_mmu_page_as_id(root);5455tdp_iter_restart(iter);56}5758/*59* Given an SPTE and its level, returns a pointer containing the host virtual60* address of the child page table referenced by the SPTE. Returns null if61* there is no such entry.62*/63tdp_ptep_t spte_to_child_pt(u64 spte, int level)64{65/*66* There's no child entry if this entry isn't present or is a67* last-level entry.68*/69if (!is_shadow_present_pte(spte) || is_last_spte(spte, level))70return NULL;7172return (tdp_ptep_t)__va(spte_to_pfn(spte) << PAGE_SHIFT);73}7475/*76* Steps down one level in the paging structure towards the goal GFN. Returns77* true if the iterator was able to step down a level, false otherwise.78*/79static bool try_step_down(struct tdp_iter *iter)80{81tdp_ptep_t child_pt;8283if (iter->level == iter->min_level)84return false;8586/*87* Reread the SPTE before stepping down to avoid traversing into page88* tables that are no longer linked from this entry.89*/90iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);9192child_pt = spte_to_child_pt(iter->old_spte, iter->level);93if (!child_pt)94return false;9596iter->level--;97iter->pt_path[iter->level - 1] = child_pt;98iter->gfn = gfn_round_for_level(iter->next_last_level_gfn, iter->level);99tdp_iter_refresh_sptep(iter);100101return true;102}103104/*105* Steps to the next entry in the current page table, at the current page table106* level. The next entry could point to a page backing guest memory or another107* page table, or it could be non-present. Returns true if the iterator was108* able to step to the next entry in the page table, false if the iterator was109* already at the end of the current page table.110*/111static bool try_step_side(struct tdp_iter *iter)112{113/*114* Check if the iterator is already at the end of the current page115* table.116*/117if (SPTE_INDEX((iter->gfn | iter->gfn_bits) << PAGE_SHIFT, iter->level) ==118(SPTE_ENT_PER_PAGE - 1))119return false;120121iter->gfn += KVM_PAGES_PER_HPAGE(iter->level);122iter->next_last_level_gfn = iter->gfn;123iter->sptep++;124iter->old_spte = kvm_tdp_mmu_read_spte(iter->sptep);125126return true;127}128129/*130* Tries to traverse back up a level in the paging structure so that the walk131* can continue from the next entry in the parent page table. Returns true on a132* successful step up, false if already in the root page.133*/134static bool try_step_up(struct tdp_iter *iter)135{136if (iter->level == iter->root_level)137return false;138139iter->level++;140iter->gfn = gfn_round_for_level(iter->gfn, iter->level);141tdp_iter_refresh_sptep(iter);142143return true;144}145146/*147* Step to the next SPTE in a pre-order traversal of the paging structure.148* To get to the next SPTE, the iterator either steps down towards the goal149* GFN, if at a present, non-last-level SPTE, or over to a SPTE mapping a150* higher GFN.151*152* The basic algorithm is as follows:153* 1. If the current SPTE is a non-last-level SPTE, step down into the page154* table it points to.155* 2. If the iterator cannot step down, it will try to step to the next SPTE156* in the current page of the paging structure.157* 3. If the iterator cannot step to the next entry in the current page, it will158* try to step up to the parent paging structure page. In this case, that159* SPTE will have already been visited, and so the iterator must also step160* to the side again.161*/162void tdp_iter_next(struct tdp_iter *iter)163{164if (iter->yielded) {165tdp_iter_restart(iter);166return;167}168169if (try_step_down(iter))170return;171172do {173if (try_step_side(iter))174return;175} while (try_step_up(iter));176iter->valid = false;177}178179180181