Skip to main content

ostd/specs/mm/page_table/cursor/
cursor_steps.rs

1use vstd::{arithmetic::div_mod::lemma_mod_add_multiples_vanish, prelude::*};
2use vstd_extra::{
3    arithmetic::{lemma_nat_align_down_sound, lemma_nat_align_down_within_block, nat_align_down},
4    ghost_tree::*,
5    ownership::*,
6};
7
8use crate::specs::{
9    arch::NR_ENTRIES,
10    mm::{
11        frame::mapping::meta_to_index,
12        page_table::{
13            cursor::{
14                owners::*,
15                page_size_lemmas::{lemma_page_size_ge_base, lemma_page_size_monotone},
16            },
17            node::EntryOwner,
18            owners::*,
19            vaddr_range_proofs::lemma_page_table_managed_index_bounds,
20            *,
21        },
22        *,
23    },
24};
25
26use crate::mm::{Paddr, PagingConstsTrait, PagingLevel, Vaddr, page_size, page_size_spec, page_table::*};
27use core::ops::Range;
28
29verus! {
30
31broadcast use group_ghost_tree_lemmas;
32
33/// Upgrade `node_unlocked_except` to `node_unlocked` on a subtree where the excepted
34/// entry cannot appear. The precondition `path == subtree.value.path` ties structural
35/// positions to entry paths. `excepted_path` must differ from all descendant paths,
36/// which is guaranteed when `excepted_path != path` and `excepted_path` is not an
37/// extension of `path` (all descendants have paths extending `path`).
38pub proof fn subtree_unlock_upgrade<'rcu, C: PageTableConfig>(
39    subtree: OwnerSubtree<C>,
40    path: TreePath<NR_ENTRIES>,
41    guards: Guards,
42    regions: MetaRegionOwners,
43    excepted_addr: usize,
44    excepted_path: TreePath<NR_ENTRIES>,
45)
46    requires
47        subtree.inv(),
48        PageTableOwner::<C>(subtree).pt_inv(),
49        subtree.subtree_satisfies(path, PageTableOwner::<C>::metaregion_sound_pred(regions)),
50        subtree.subtree_satisfies(
51            path,
52            CursorOwner::<'rcu, C>::node_unlocked_except(guards, excepted_addr),
53        ),
54        regions.slot_owners[meta_to_index(excepted_addr)].paths_in_pt == set![excepted_path],
55        // Structural path == value path
56        path == subtree.value().path,
57        path.inv(),
58        // excepted_path does not match this subtree root
59        path != excepted_path,
60        // excepted_path is not a descendant (all descendants extend path, so if
61        // excepted_path.len() <= path.len() it can't be a descendant; otherwise
62        // it must diverge at some index below path.len())
63        excepted_path.len() <= path.len() || (exists|k: int|
64            0 <= k < path.len() && #[trigger] excepted_path[k] != path[k]),
65    ensures
66        subtree.subtree_satisfies(path, CursorOwner::<'rcu, C>::node_unlocked(guards)),
67    decreases INC_LEVELS - subtree.level(),
68{
69    C::lemma_paging_consts_properties();
70    C::lemma_page_table_config_constant_properties();
71    reveal(PageTableOwner::pt_inv_at_depth);
72    let f = PageTableOwner::<C>::metaregion_sound_pred(regions);
73    let g = CursorOwner::<'rcu, C>::node_unlocked_except(guards, excepted_addr);
74    let h = CursorOwner::<'rcu, C>::node_unlocked(guards);
75
76    if subtree.value().is_node() {
77        if subtree.value().node().slot_vaddr() == excepted_addr {
78            // addr == excepted_addr contradicts path != excepted_path
79            // via metaregion_sound's singleton paths_in_pt.
80            let idx = meta_to_index(excepted_addr);
81
82            assert(set![subtree.value().path].contains(excepted_path));
83            assert(false);
84        }
85    }
86    if subtree.level() < INC_LEVELS - 1 && subtree.value().is_node() {
87        assert forall|i: int|
88            #![trigger subtree.children()[i]]
89            0 <= i < subtree.children().len() && subtree.has_child(i) implies subtree.child(
90            i,
91        ).subtree_satisfies(path.push_tail(i), h) by {
92            let child = subtree.child(i);
93
94            let child_path = path.push_tail(i);
95
96            subtree_unlock_upgrade(
97                child,
98                child_path,
99                guards,
100                regions,
101                excepted_addr,
102                excepted_path,
103            );
104        };
105    } else if subtree.level() < INC_LEVELS - 1 && !subtree.value().is_node() {
106        // Non-node: pt_inv gives children[i] is None, so subtree_satisfies
107        // has no children to recurse into.
108    }
109}
110
111impl<'rcu, C: PageTableConfig> CursorOwner<'rcu, C> {
112    /// The number of steps it will take to walk through every node of a full
113    /// page table at level `level`
114    pub open spec fn max_steps_subtree(level: usize) -> nat
115        decreases level,
116    {
117        if level <= 1 {
118            NR_ENTRIES as nat
119        } else {
120            (NR_ENTRIES as nat) * (Self::max_steps_subtree((level - 1) as usize) + 1)
121        }
122    }
123
124    /// Per-level "above-current" contribution: count `NR_ENTRIES - cont.idx - 1`
125    /// at every level (the entry at `cont.idx` is being descended into; its
126    /// work is captured at lower levels in the recursion). `max_steps()`
127    /// adds back one `subtree(self.level)` to count the current level's
128    /// in-progress entry.
129    ///
130    /// The base case is `level > C::NR_LEVELS` (not `== C::NR_LEVELS`) so that
131    /// `level == C::NR_LEVELS` itself contributes a non-zero term. This avoids
132    /// degenerate behavior at the root: without it, `max_steps` collapses
133    /// to 0 at the root and `push_level` from the root cannot decrease
134    /// (and the popped_too_high `q` at C::NR_LEVELS would dominate `self`).
135    pub open spec fn max_steps_partial(self, level: usize) -> nat
136        decreases C::NR_LEVELS + 1 - level,
137        when level <= C::NR_LEVELS + 1
138    {
139        if level > C::NR_LEVELS {
140            0
141        } else {
142            let cont = self.continuations[level - 1];
143            let count: nat = (NR_ENTRIES - cont.idx - 1) as nat;
144            let steps = Self::max_steps_subtree(level) * count;
145            let remaining_steps = self.max_steps_partial((level + 1) as usize);
146            steps + remaining_steps
147        }
148    }
149
150    pub open spec fn max_steps(self) -> nat {
151        if self.at_or_past_barrier_end() {
152            0
153        } else {
154            (self.max_steps_partial(self.level as usize) + Self::max_steps_subtree(
155                self.level as usize,
156            )) as nat
157        }
158    }
159
160    /// Two owners with the same idx values from `start` upward have the same max_steps_partial.
161    pub proof fn lemma_max_steps_partial_eq(self, other: Self, start: usize)
162        requires
163            1 <= start <= C::NR_LEVELS + 1,
164            forall|k: int|
165                start - 1 <= k < C::NR_LEVELS ==> #[trigger] self.continuations[k].idx
166                    == other.continuations[k].idx,
167        ensures
168            self.max_steps_partial(start) == other.max_steps_partial(start),
169        decreases C::NR_LEVELS + 1 - start,
170    {
171        C::lemma_paging_consts_properties();
172        C::lemma_page_table_config_constant_properties();
173        if start <= C::NR_LEVELS {
174            self.lemma_max_steps_partial_eq(other, (start + 1) as usize);
175        }
176    }
177
178    pub open spec fn push_level_owner(self, guard: PageTableGuard<'rcu, C>) -> Self {
179        let cont = self.continuations[self.level - 1];
180        let (child, cont) = cont.make_cont(
181            pte_index_spec::<C>(self.cur_va(), (self.level - 1) as PagingLevel),
182            guard,
183        );
184        let new_continuations = self.continuations.insert(self.level - 1, cont).insert(
185            self.level - 2,
186            child,
187        );
188
189        let new_level = (self.level - 1) as u8;
190        Self { continuations: new_continuations, level: new_level, popped_too_high: false, ..self }
191    }
192
193    pub proof fn push_level_owner_decreases_steps(self, guard: PageTableGuard<'rcu, C>)
194        requires
195            self.inv(),
196            self.in_barrier_va(),
197            self.level > 1,
198        ensures
199            self.push_level_owner(guard).max_steps() < self.max_steps(),
200    {
201        C::lemma_paging_consts_properties();
202        C::lemma_page_table_config_constant_properties();
203        lemma_pte_index_bound::<C>(self.cur_va(), (self.level - 1) as PagingLevel);
204        let new_self = self.push_level_owner(guard);
205        let l = self.level as usize;
206        let lm1 = (self.level - 1) as usize;
207        // Continuations agree at indices [l-1, C::NR_LEVELS): only [l-2] changed.
208        new_self.lemma_max_steps_partial_eq(self, l);
209        let new_child = new_self.continuations[lm1 - 1];
210
211        // subtree(l) == NR * (subtree(lm1) + 1) (from def of max_steps_subtree, l > 1).
212        // subtree(lm1) * (NR - new_child.idx) <= subtree(lm1) * NR < subtree(l).
213        vstd::arithmetic::mul::lemma_mul_inequality(
214            (NR_ENTRIES - new_child.idx) as int,
215            NR_ENTRIES as int,
216            Self::max_steps_subtree(lm1) as int,
217        );
218        vstd::arithmetic::mul::lemma_mul_is_distributive_add(
219            Self::max_steps_subtree(lm1) as int,
220            NR_ENTRIES - new_child.idx - 1,
221            1,
222        );
223        vstd::arithmetic::mul::lemma_mul_is_commutative(
224            (NR_ENTRIES - new_child.idx) as int,
225            Self::max_steps_subtree(lm1) as int,
226        );
227
228    }
229
230    pub proof fn push_level_owner_preserves_mappings(self, guard: PageTableGuard<'rcu, C>)
231        requires
232            self.inv(),
233            self.level > 1,
234            self.cur_entry_owner().is_node(),
235        ensures
236            self.push_level_owner(guard)@.mappings == self@.mappings,
237    {
238        hide(rec_vaddr);
239        C::lemma_paging_consts_properties();
240        C::lemma_page_table_config_constant_properties();
241        broadcast use {
242            CursorContinuation::group_lemmas,
243            CursorOwner::group_lemmas,
244            PageTableOwner::group_lemmas,
245        };
246
247        let new_owner = self.push_level_owner(guard);
248        let old_cont = self.continuations[self.level - 1];
249        let (_, modified_cont) = old_cont.make_cont(
250            pte_index_spec::<C>(self.cur_va(), (self.level - 1) as PagingLevel),
251            guard,
252        );
253
254        old_cont.view_mappings_take_child();
255
256        let taken = old_cont.take_child().1;
257
258        assert(modified_cont.children == taken.children) by {};
259
260        assert forall|m: Mapping|
261            self.view_mappings().contains(m) implies new_owner.view_mappings().contains(m) by {
262            let i = choose|i: int|
263                self.level - 1 <= i < C::NR_LEVELS && (
264                #[trigger] self.continuations[i]).view_mappings().contains(m);
265            if i == self.level - 1 {
266                if old_cont.view_mappings_take_child_spec().contains(m) {
267                    assert(new_owner.continuations[self.level - 2].view_mappings().contains(m));
268                } else {
269                    assert(new_owner.continuations[self.level - 1].view_mappings().contains(m));
270                }
271            } else {
272                assert(new_owner.continuations[i] == self.continuations[i]);
273            }
274        };
275
276    }
277
278    pub proof fn push_level_owner_preserves_inv(self, guard: PageTableGuard<'rcu, C>)
279        requires
280            self.inv(),
281            self.level > 1,
282            !self.popped_too_high,
283            self.level <= self.guard_level,
284            self.in_barrier_va(),
285            // The current entry is a node (we're descending into it)
286            self.cur_entry_owner().is_node(),
287            // The child node's guard relates to the new guard
288            self.cur_entry_owner().node().relate_guard(guard),
289            // Guard distinctness: the new guard points to a different node than all existing continuations
290            forall|i: int|
291                #![trigger self.continuations[i]]
292                self.level - 1 <= i < C::NR_LEVELS ==> self.continuations[i].guard.inner.slot_vaddr()
293                    != guard.inner.slot_vaddr(),
294        ensures
295            self.push_level_owner(guard).inv(),
296    {
297        C::lemma_paging_consts_properties();
298        C::lemma_page_table_config_constant_properties();
299        lemma_pte_index_bound::<C>(self.cur_va(), (self.level - 1) as PagingLevel);
300        let new_owner = self.push_level_owner(guard);
301
302        let old_cont = self.continuations[self.level - 1];
303
304        let child_node = old_cont.children[old_cont.idx as int].unwrap();
305        let (child, _) = old_cont.make_cont(
306            pte_index_spec::<C>(self.cur_va(), (self.level - 1) as PagingLevel),
307            guard,
308        );
309
310        assert(child.inv_children_rel()) by {
311            assert forall|j: int|
312                0 <= j < NR_ENTRIES && #[trigger] child.children[j] is Some implies {
313                &&& child.children[j]->0.value().is_node()
314                    ==> child.children[j]->0.value().node_permission is Some
315                &&& child.children[j].unwrap().value().parent_level == child.level()
316                &&& child.children[j].unwrap().level() == child.tree_level + 1
317                &&& child.children[j].unwrap().value().path.len()
318                    == child.entry_own.node().tree_level + 1
319                &&& child.children[j].unwrap().value().match_pte(
320                    child.entry_own.node().children_perm.value()[j],
321                    child.entry_own.node().level(),
322                )
323                &&& <EntryOwner<C> as TreeNodeValue<{ crate::mm::MAX_NR_LEVELS }>>::rel_children(
324                    child.entry_own,
325                    j,
326                    Some(child.children[j].unwrap().value()),
327                )
328                &&& child.children[j].unwrap().value().path == child.path().push_tail(j)
329            } by {
330                let gc = child.children[j].unwrap();
331                PageTableOwner(child_node).pt_inv_unroll(j);
332
333            };
334        };
335        assert(child.pt_inv_children()) by {
336            assert forall|j: int|
337                0 <= j < child.children.len()
338                    && #[trigger] child.children[j] is Some implies PageTableOwner(
339                child.children[j].unwrap(),
340            ).pt_inv() by {
341                PageTableOwner(child_node).pt_inv_unroll(j);
342
343            };
344        };
345
346        assert(new_owner.continuations[new_owner.level - 1].all_some()) by {
347            assert forall|j: int| 0 <= j < NR_ENTRIES implies child.children[j] is Some by {
348                PageTableOwner(child_node).pt_inv_unroll(j);
349
350            };
351        };
352
353    }
354
355    #[verifier::spinoff_prover]
356    #[verifier::rlimit(20)]
357    pub proof fn push_level_owner_preserves_invs(
358        self,
359        guard: PageTableGuard<'rcu, C>,
360        regions: MetaRegionOwners,
361        guards: Guards,
362    )
363        requires
364            self.inv(),
365            self.level > 1,
366            !self.popped_too_high,
367            self.level <= self.guard_level,
368            self.in_barrier_va(),
369            self.only_current_locked(guards),
370            self.nodes_locked(guards),
371            self.metaregion_sound(regions),
372            // The current entry is a node (we're descending into it)
373            self.cur_entry_owner().is_node(),
374            // The child node's guard relates to the new guard
375            self.cur_entry_owner().node().relate_guard(guard),
376            // The new guard must be locked in guards
377            guards.lock_held(guard.inner.slot_vaddr()),
378        ensures
379            self.push_level_owner(guard).inv(),
380            self.push_level_owner(guard).children_not_locked(guards),
381            self.push_level_owner(guard).nodes_locked(guards),
382            self.push_level_owner(guard).metaregion_sound(regions),
383    {
384        hide(CursorContinuation::view_mappings);
385        hide(CursorOwner::view_mappings);
386        C::lemma_paging_consts_properties();
387        C::lemma_page_table_config_constant_properties();
388        reveal(CursorContinuation::map_children);
389        let new_owner = self.push_level_owner(guard);
390        let old_cont = self.continuations[self.level - 1];
391
392        let (child_cont, _) = old_cont.make_cont(
393            pte_index_spec::<C>(self.cur_va(), (self.level - 1) as PagingLevel),
394            guard,
395        );
396
397        let cur_entry_path = old_cont.path().push_tail(old_cont.idx as int);
398
399        assert forall|i: int|
400            #![trigger self.continuations[i]]
401            self.level - 1 <= i
402                < C::NR_LEVELS implies self.continuations[i].guard.inner.slot_vaddr()
403            != guard.inner.slot_vaddr() by {
404            reveal(CursorOwner::path_metaregion_sound);
405            let cont_i = self.continuations[i];
406
407            if cont_i.guard.inner.slot_vaddr() == guard.inner.slot_vaddr() {
408                assert(set![cont_i.path()].contains(cur_entry_path));
409
410                assert(false);
411            }
412        };
413
414        assert(new_owner.inv()) by {
415            self.push_level_owner_preserves_inv(guard);
416        }
417
418        self.lemma_push_level_children_not_locked(guard, regions, guards);
419
420        let f = PageTableOwner::<C>::metaregion_sound_pred(regions);
421        let child_subtree = child_cont.as_subtree();
422
423        assert(child_cont.map_children(f)) by {
424            assert forall|j: int|
425                0 <= j < child_cont.children.len()
426                    && #[trigger] child_cont.children[j] is Some implies child_cont.children[j].unwrap().subtree_satisfies(
427            child_cont.path().push_tail(j), f) by {
428                child_subtree.lemma_subtree_satisfies_unroll_once(child_cont.path(), f, j);
429
430            };
431        };
432        reveal(CursorOwner::path_metaregion_sound);
433    }
434
435    pub proof fn tracked_push_level_owner(tracked &mut self, guard: PageTableGuard<'rcu, C>)
436        requires
437            old(self).inv(),
438            old(self).level > 1,
439        ensures
440            *final(self) == old(self).push_level_owner(guard),
441    {
442        C::lemma_paging_consts_properties();
443        C::lemma_page_table_config_constant_properties();
444        lemma_pte_index_bound::<C>(self.cur_va(), (self.level - 1) as PagingLevel);
445
446        let ghost self0 = *self;
447        let tracked mut cont = self.continuations.tracked_remove(self.level - 1);
448        let tracked child = cont.tracked_make_cont(
449            pte_index_spec::<C>(self.cur_va(), (self.level - 1) as PagingLevel),
450            guard,
451        );
452
453        self.continuations.tracked_insert(self.level - 1, cont);
454        self.continuations.tracked_insert(self.level - 2, child);
455
456        assert(self.continuations == self0.continuations.insert(self.level - 1, cont).insert(
457            self.level - 2,
458            child,
459        ));
460
461        self.popped_too_high = false;
462
463        self.level = (self.level - 1) as u8;
464    }
465
466    pub open spec fn pop_level_owner(self) -> (Self, PageTableGuard<'rcu, C>) {
467        let child = self.continuations[self.level - 1];
468        let cont = self.continuations[self.level as int];
469        let (new_cont, guard) = cont.restore(child);
470        let new_continuations = self.continuations.insert(self.level as int, new_cont).remove(
471            self.level - 1,
472        );
473        let new_level = (self.level + 1) as u8;
474        let popped_too_high = if new_level > self.guard_level {
475            true
476        } else {
477            false
478        };
479        (
480            Self {
481                continuations: new_continuations,
482                level: new_level,
483                popped_too_high: popped_too_high,
484                ..self
485            },
486            guard,
487        )
488    }
489
490    #[verifier::spinoff_prover]
491    #[verifier::rlimit(200)]
492    pub proof fn pop_level_owner_preserves_invs(self, guards: Guards, regions: MetaRegionOwners)
493        requires
494            self.inv(),
495            self.level < C::NR_LEVELS,
496            self.children_not_locked(guards),
497            self.nodes_locked(guards),
498            self.metaregion_sound(regions),
499        ensures
500            self.pop_level_owner().0.inv(),
501            self.pop_level_owner().0.only_current_locked(guards),
502            self.pop_level_owner().0.nodes_locked(guards),
503            self.pop_level_owner().0.metaregion_sound(regions),
504    {
505        hide(rec_vaddr);
506        hide(page_size_spec);
507        hide(CursorContinuation::pt_inv_children);
508        hide(CursorContinuation::view_mappings);
509        hide(CursorOwner::view_mappings);
510        C::lemma_paging_consts_properties();
511        C::lemma_page_table_config_constant_properties();
512        let child = self.continuations[self.level - 1];
513        let child_addr = child.entry_own.node().slot_vaddr();
514        child.as_page_table_owner_pt_inv();
515        let cont = self.continuations[self.level as int];
516        assert(cont.restore(child).0.pt_inv_children()) by {
517            reveal(CursorContinuation::pt_inv_children);
518            reveal(PageTableOwner::pt_inv_at_depth);
519        };
520
521
522        self.lemma_map_children_implies(
523            CursorOwner::<'rcu, C>::node_unlocked(guards),
524            CursorOwner::<'rcu, C>::node_unlocked_except(guards, child_addr),
525        );
526        assert({
527            &&& self.pop_level_owner().0.only_current_locked(guards)
528            &&& self.pop_level_owner().0.nodes_locked(guards)
529            &&& self.pop_level_owner().0.metaregion_sound(regions)
530        }) by {
531            reveal(CursorContinuation::map_children);
532            reveal(CursorOwner::path_metaregion_sound);
533        };
534    }
535
536    pub open spec fn move_forward_owner_spec(self) -> Self
537        decreases C::NR_LEVELS - self.level,
538        when self.level <= C::NR_LEVELS
539    {
540        if self.index() + 1 == NR_ENTRIES && self.level == self.guard_level {
541            Self {
542                va: (nat_align_down(self.va as nat, page_size::<C>(self.level) as nat)
543                    + page_size::<C>(self.level)) as Vaddr,
544                popped_too_high: false,
545                ..self
546            }
547        } else if self.index() + 1 < NR_ENTRIES {
548            // Advance to the next aligned slot boundary. At the end of the managed
549            // range, the resulting VA is exactly the exclusive end sentinel.
550            self.inc_index().zero_below_level()
551        } else if self.level < C::NR_LEVELS {
552            self.pop_level_owner().0.move_forward_owner_spec()
553        } else {
554            // self.level == C::NR_LEVELS && self.index() + 1 == NR_ENTRIES.
555            // Preserve lower address bits and the former address-word wrapping semantics.
556            Self {
557                va: (self.va + page_size::<C>(C::NR_LEVELS)) as Vaddr,
558                popped_too_high: false,
559                ..self
560            }
561        }
562    }
563
564    pub proof fn lemma_move_forward_increases_va(self)
565        requires
566            self.inv(),
567            self.level <= C::NR_LEVELS,
568            self.in_barrier_va(),
569            !self.popped_too_high,
570        ensures
571            self.move_forward_owner_spec().va > self.va,
572    {
573        C::lemma_paging_consts_properties();
574        C::lemma_page_table_config_constant_properties();
575        self.lemma_move_forward_va_is_align_up();
576        self.lemma_va_plus_page_size_no_overflow(self.level);
577        lemma_page_size_ge_base::<C>(self.level);
578        lemma_nat_align_down_sound(self.va as nat, page_size::<C>(self.level) as nat);
579    }
580
581    pub proof fn lemma_move_forward_not_popped_too_high(self)
582        requires
583            self.inv(),
584            self.level <= C::NR_LEVELS,
585            self.in_barrier_va(),
586        ensures
587            !self.move_forward_owner_spec().popped_too_high,
588        decreases C::NR_LEVELS - self.level,
589    {
590        C::lemma_paging_consts_properties();
591        C::lemma_page_table_config_constant_properties();
592        reveal(PageTableOwner::pt_inv_at_depth);
593        if self.level == self.guard_level && self.index() + 1 == NR_ENTRIES {
594            return;
595        }
596        if self.index() + 1 >= NR_ENTRIES && self.level < C::NR_LEVELS {
597            self.pop_level_owner().0.lemma_move_forward_not_popped_too_high();
598        }
599    }
600
601    proof fn lemma_move_forward_owner_popped_too_high_decreases(self)
602        requires
603            self.inv(),
604            self.level <= C::NR_LEVELS,
605            self.in_barrier_va(),
606            self.popped_too_high,
607            self.continuations[C::NR_LEVELS - 1].idx + 1 < NR_ENTRIES,
608        ensures
609            self.move_forward_owner_spec().max_steps() + Self::max_steps_subtree(
610                self.level as usize,
611            ) <= self.max_steps(),
612        decreases C::NR_LEVELS - self.level,
613    {
614        C::lemma_paging_consts_properties();
615        C::lemma_page_table_config_constant_properties();
616        reveal(PageTableOwner::pt_inv_at_depth);
617        let l = self.level as usize;
618        let st_l = Self::max_steps_subtree(l) as int;
619
620        if self.level == self.guard_level && self.index() + 1 == NR_ENTRIES {
621            self.lemma_move_forward_guard_end();
622            return;
623        }
624        if self.index() + 1 < NR_ENTRIES {
625            // Case A: advance via inc_index().zero_below_level().
626            // (Mirror of subcase A in the main lemma's case 2b.)
627            let inc = self.inc_index();
628
629            let new_state = inc.zero_below_level();
630
631            new_state.lemma_max_steps_partial_eq(self, (self.level + 1) as usize);
632            let self_idx = self.continuations[self.level - 1].idx as int;
633            vstd::arithmetic::mul::lemma_mul_is_distributive_add(
634                st_l,
635                NR_ENTRIES - self_idx - 2,
636                1,
637            );
638
639        } else if self.level < C::NR_LEVELS {
640            let popped2 = self.pop_level_owner().0;
641            let lp1 = (self.level + 1) as usize;
642            popped2.lemma_max_steps_partial_eq(self, lp1);
643
644            assert(Self::max_steps_subtree(l) * 0nat == 0) by (nonlinear_arith);
645            popped2.lemma_move_forward_owner_popped_too_high_decreases();
646
647        } else {
648            assert(false);
649        }
650    }
651
652    pub proof fn lemma_move_forward_owner_decreases_steps(self)
653        requires
654            self.inv(),
655            self.level <= C::NR_LEVELS,
656            self.in_barrier_va(),
657            !self.popped_too_high,
658        ensures
659            self.move_forward_owner_spec().max_steps() + Self::max_steps_subtree(
660                self.level as usize,
661            ) <= self.max_steps(),
662            self.move_forward_owner_spec().max_steps() < self.max_steps(),
663        decreases C::NR_LEVELS - self.level,
664    {
665        hide(rec_vaddr);
666        C::lemma_paging_consts_properties();
667        C::lemma_page_table_config_constant_properties();
668        reveal(PageTableOwner::pt_inv_at_depth);
669        self.lemma_in_barrier_va_level_le_guard_level();
670        let l = self.level as usize;
671        let st_l = Self::max_steps_subtree(l) as int;
672
673        if self.level == self.guard_level && self.index() + 1 == NR_ENTRIES {
674            self.lemma_move_forward_guard_end();
675            return;
676        }
677        if self.index() + 1 < NR_ENTRIES {
678            let inc = self.inc_index();
679
680            let new_state = inc.zero_below_level();
681
682            new_state.lemma_max_steps_partial_eq(self, (self.level + 1) as usize);
683            let self_idx = self.continuations[self.level - 1].idx as int;
684            let tail = self.max_steps_partial((self.level + 1) as usize) as int;
685            vstd::arithmetic::mul::lemma_mul_is_distributive_add(
686                st_l,
687                NR_ENTRIES - self_idx - 2,
688                1,
689            );
690
691        } else if self.level < C::NR_LEVELS {
692            let popped = self.pop_level_owner().0;
693            let lp1 = (self.level + 1) as usize;
694            popped.lemma_max_steps_partial_eq(self, lp1);
695
696            assert(Self::max_steps_subtree(l) * 0nat == 0) by (nonlinear_arith);
697
698            if !popped.popped_too_high {
699                popped.lemma_move_forward_owner_decreases_steps();
700
701            } else {
702                popped.lemma_move_forward_owner_popped_too_high_decreases();
703            }
704        } else {
705            assert(false);
706        }
707    }
708
709    /// Advancing the cursor reaches the next slot boundary, including carries to a parent.
710    #[verifier::spinoff_prover]
711    pub proof fn lemma_move_forward_va_is_align_up(self)
712        requires
713            self.inv(),
714            self.level <= C::NR_LEVELS,
715            self.in_barrier_va(),
716            !self.popped_too_high,
717        ensures
718            self.move_forward_owner_spec().va == self@.align_up_spec(
719                page_size::<C>(self.level),
720            ),
721        decreases C::NR_LEVELS - self.level,
722    {
723        C::lemma_paging_consts_properties();
724        C::lemma_page_table_config_constant_properties();
725        reveal(PageTableOwner::pt_inv_at_depth);
726        if self.level == self.guard_level && self.index() + 1 == NR_ENTRIES {
727            self.lemma_move_forward_guard_end();
728            return;
729        }
730        if self.index() + 1 < NR_ENTRIES {
731            self.inc_and_zero_increases_va();
732            return;
733        }
734        assert(self.level < C::NR_LEVELS);
735        let popped = self.pop_level_owner().0;
736        assert(self.move_forward_owner_spec().va == popped.move_forward_owner_spec().va);
737        if !popped.popped_too_high {
738            popped.lemma_move_forward_va_is_align_up();
739            popped.lemma_va_plus_page_size_no_overflow(popped.level);
740        } else {
741            popped.inc_and_zero_increases_va();
742            popped.lemma_inc_index_va();
743        }
744
745        self.lemma_va_plus_page_size_no_overflow(self.level);
746        lemma_page_size_next::<C>(self.level);
747        lemma_page_size_ge_base::<C>(self.level);
748        lemma_nat_align_down_sound(self.va as nat, page_size::<C>(self.level) as nat);
749        let next = self@.align_up_spec(page_size::<C>(self.level));
750        lemma_mod_add_multiples_vanish(
751            nat_align_down(self.va as nat, page_size::<C>(self.level) as nat) as int,
752            page_size::<C>(self.level) as int,
753        );
754        lemma_next_slot_pte_index::<C>(self.va, next, self.level);
755        assert(pte_index_spec::<C>(next, self.level) == 0);
756        lemma_next_slot_pte_index::<C>(popped.va, next, popped.level);
757        assert(self@.align_up_spec(page_size::<C>(self.level)) == popped@.align_up_spec(
758            page_size::<C>(popped.level),
759        ));
760    }
761
762    /// After popping a level, the total view_mappings is preserved.
763    /// The restored parent at index self.level absorbs the child's mappings,
764    /// and both are within the view_mappings range [self.level, C::NR_LEVELS).
765    pub proof fn lemma_pop_level_owner_preserves_mappings(self)
766        requires
767            self.inv(),
768            self.level < C::NR_LEVELS,
769            self.in_barrier_va(),
770        ensures
771            self.pop_level_owner().0@.mappings == self@.mappings,
772    {
773        C::lemma_paging_consts_properties();
774        C::lemma_page_table_config_constant_properties();
775        broadcast use {CursorContinuation::group_lemmas, CursorOwner::group_lemmas};
776
777        let child = self.continuations[self.level - 1];
778        let parent = self.continuations[self.level as int];
779        let (restored_parent, _) = parent.restore(child);
780        let popped = self.pop_level_owner().0;
781        let child_subtree = child.as_subtree();
782
783        let r = restored_parent;
784        let p = parent.put_child(child_subtree);
785        assert forall|j: int| 0 <= j < r.children.len() implies r.children[j]
786            == p.children[j] by {};
787
788        assert(restored_parent.view_mappings() == parent.put_child(child_subtree).view_mappings())
789            by {};
790
791        parent.view_mappings_put_child(child_subtree);
792        child.as_page_table_owner_preserves_view_mappings();
793
794        assert(popped.view_mappings() == self.view_mappings()) by {
795            assert forall|m: Mapping|
796                self.view_mappings().contains(m) implies popped.view_mappings().contains(m) by {
797                let i = choose|i: int|
798                    self.level - 1 <= i < C::NR_LEVELS && (
799                    #[trigger] self.continuations[i]).view_mappings().contains(m);
800                if i == self.level - 1 {
801                    assert(popped.continuations[self.level as int].view_mappings().contains(m));
802                } else if i == self.level {
803                    assert(popped.continuations[self.level as int].view_mappings().contains(m));
804                } else {
805                    assert(popped.continuations[i] == self.continuations[i]);
806                }
807            };
808
809        };
810    }
811
812    pub proof fn lemma_move_forward_owner_preserves_mappings(self)
813        requires
814            self.inv(),
815            self.in_barrier_va(),
816        ensures
817            self.move_forward_owner_spec()@.mappings == self@.mappings,
818        decreases C::NR_LEVELS - self.level,
819    {
820        C::lemma_paging_consts_properties();
821        C::lemma_page_table_config_constant_properties();
822        reveal(PageTableOwner::pt_inv_at_depth);
823        broadcast use {CursorContinuation::group_lemmas, CursorOwner::group_lemmas};
824
825        if self.level == self.guard_level && self.index() + 1 == NR_ENTRIES {
826            return;
827        }
828        if self.index() + 1 < NR_ENTRIES {
829            let inc = self.inc_index();
830            let result = inc.zero_below_level();
831
832            let old_cont = self.continuations[self.level - 1];
833            let new_cont = old_cont.inc_index();
834
835            assert(result.view_mappings() == self.view_mappings()) by {
836                assert forall|m: Mapping|
837                    self.view_mappings().contains(m) implies result.view_mappings().contains(m) by {
838                    let i = choose|i: int|
839                        self.level - 1 <= i < C::NR_LEVELS && (
840                        #[trigger] self.continuations[i]).view_mappings().contains(m);
841                    if i == self.level - 1 {
842                        assert(result.continuations[i].view_mappings().contains(m));
843                    } else {
844                        assert(result.continuations[i] == self.continuations[i]);
845                    }
846                };
847
848            };
849
850        } else if self.level < C::NR_LEVELS {
851            let popped = self.pop_level_owner().0;
852
853            self.lemma_pop_level_owner_preserves_mappings();
854            popped.lemma_move_forward_owner_preserves_mappings();
855        }
856    }
857
858    /// Advancing the guard's final entry retains its continuation at the node-end sentinel.
859    pub proof fn lemma_move_forward_guard_end(self)
860        requires
861            self.inv(),
862            self.in_barrier_va(),
863            self.level == self.guard_level,
864            self.index() + 1 == NR_ENTRIES,
865        ensures
866            self.move_forward_owner_spec().va == self.locked_range().end,
867            self.move_forward_owner_spec().at_or_past_barrier_end(),
868            self.move_forward_owner_spec().inv(),
869            self.move_forward_owner_spec().max_steps() == 0,
870            self.move_forward_owner_spec().va == self@.align_up_spec(
871                page_size::<C>(self.level),
872            ),
873    {
874        use vstd::arithmetic::div_mod::*;
875        C::lemma_paging_consts_properties();
876        C::lemma_page_table_config_constant_properties();
877        self.lemma_cur_pte_index();
878        self.lemma_locked_range_span();
879        self.lemma_va_plus_page_size_no_overflow(self.level);
880        lemma_page_size_next::<C>(self.level);
881        lemma_pte_index_spec_is_div_mod::<C>(self.va, self.level);
882        let ps = page_size::<C>(self.level) as int;
883        let node_ps = page_size::<C>((self.level + 1) as PagingLevel) as int;
884        lemma_fundamental_div_mod(self.prefix as int, node_ps);
885        lemma_fundamental_div_mod_converse(
886            self.va as int,
887            node_ps,
888            self.prefix as int / node_ps,
889            self.va - self.prefix,
890        );
891        lemma_mod_breakdown(self.va as int, ps, NR_ENTRIES as int);
892        lemma_nat_align_down_sound(self.va as nat, ps as nat);
893        assert(nat_align_down(self.va as nat, ps as nat) + ps == self.prefix + node_ps)
894            by (nonlinear_arith)
895            requires
896                self.va as int % node_ps == self.va - self.prefix,
897                self.va as int % node_ps == ps * (self.va as int / ps % NR_ENTRIES as int)
898                    + self.va as int % ps,
899                self.va as int / ps % NR_ENTRIES as int == NR_ENTRIES - 1,
900                node_ps == ps * NR_ENTRIES,
901                nat_align_down(self.va as nat, ps as nat) == self.va - self.va as int % ps,
902        ;
903        let next = self.move_forward_owner_spec().va;
904        self.lemma_locked_range_page_aligned();
905        lemma_page_table_managed_index_bounds::<C>(self.va);
906        lemma_page_size_divides::<C>(self.level, C::NR_LEVELS);
907        lemma_nat_align_down_within_block(
908            self.va as nat,
909            page_size::<C>(self.level) as nat,
910            page_size::<C>(C::NR_LEVELS) as nat,
911        );
912        lemma_page_size_next::<C>(C::NR_LEVELS);
913        lemma_vaddr_leading_part_is_align_down::<C>(self.va);
914        let root_base = vaddr_leading_part_spec::<C>(self.va);
915        let root_size = page_size::<C>((C::NR_LEVELS + 1) as PagingLevel);
916        lemma_nat_align_down_sound(self.va as nat, root_size as nat);
917        lemma_same_node_vaddr_leading_part_match::<C>(next, self.va, root_base, C::NR_LEVELS);
918        assert(self.move_forward_owner_spec().inv()) by {
919            reveal(<CursorOwner as Inv>::inv);
920        };
921    }
922
923    /// Descending preserves the unlocked status of child subtrees.
924    proof fn lemma_push_level_children_not_locked(
925        self,
926        guard: PageTableGuard<'rcu, C>,
927        regions: MetaRegionOwners,
928        guards: Guards,
929    )
930        requires
931            self.inv(),
932            self.level > 1,
933            !self.popped_too_high,
934            self.level <= self.guard_level,
935            self.in_barrier_va(),
936            self.only_current_locked(guards),
937            self.nodes_locked(guards),
938            self.metaregion_sound(regions),
939            self.cur_entry_owner().is_node(),
940            self.cur_entry_owner().node().relate_guard(guard),
941            guards.lock_held(guard.inner.slot_vaddr()),
942            self.push_level_owner(guard).inv(),
943        ensures
944            self.push_level_owner(guard).children_not_locked(guards),
945    {
946        hide(CursorContinuation::view_mappings);
947        hide(CursorOwner::view_mappings);
948        C::lemma_paging_consts_properties();
949        C::lemma_page_table_config_constant_properties();
950        reveal(CursorContinuation::map_children);
951        let new_owner = self.push_level_owner(guard);
952        let old_cont = self.continuations[self.level - 1];
953
954        let (child_cont, modified_cont) = old_cont.make_cont(
955            pte_index_spec::<C>(self.cur_va(), (self.level - 1) as PagingLevel),
956            guard,
957        );
958
959        let cur_entry = self.cur_entry_owner();
960        let cur_entry_addr = cur_entry.node().slot_vaddr();
961        let cur_entry_path = old_cont.path().push_tail(old_cont.idx as int);
962
963        let f = PageTableOwner::<C>::metaregion_sound_pred(regions);
964        let g_except = CursorOwner::<'rcu, C>::node_unlocked_except(guards, cur_entry_addr);
965        let h = CursorOwner::<'rcu, C>::node_unlocked(guards);
966
967        assert forall|i: int|
968            #![trigger new_owner.continuations[i]]
969            new_owner.level - 1 <= i
970                < C::NR_LEVELS implies new_owner.continuations[i].map_children(h) by {
971            if i == self.level - 2 {
972                assert forall|j: int|
973                    #![trigger child_cont.children[j]]
974                    0 <= j < child_cont.children.len()
975                        && child_cont.children[j] is Some implies child_cont.children[j].unwrap().subtree_satisfies(
976                child_cont.path().push_tail(j), h) by {
977                    let gc = child_cont.children[j].unwrap();
978                    let gc_path = child_cont.path().push_tail(j);
979
980                    let child_subtree = old_cont.children[old_cont.idx as int].unwrap();
981                    child_subtree.lemma_subtree_satisfies_unroll_once(cur_entry_path, f, j);
982                    child_subtree.lemma_subtree_satisfies_unroll_once(cur_entry_path, g_except, j);
983
984                    subtree_unlock_upgrade(
985                        gc,
986                        gc_path,
987                        guards,
988                        regions,
989                        cur_entry_addr,
990                        cur_entry_path,
991                    );
992                };
993            } else if i == self.level - 1 {
994                assert forall|j: int|
995                    #![trigger modified_cont.children[j]]
996                    0 <= j < modified_cont.children.len()
997                        && modified_cont.children[j] is Some implies modified_cont.children[j].unwrap().subtree_satisfies(
998                modified_cont.path().push_tail(j), h) by {
999                    let sibling = old_cont.children[j].unwrap();
1000                    let sibling_path = old_cont.path().push_tail(j);
1001
1002                    subtree_unlock_upgrade(
1003                        sibling,
1004                        sibling_path,
1005                        guards,
1006                        regions,
1007                        cur_entry_addr,
1008                        cur_entry_path,
1009                    );
1010                };
1011            } else {
1012                let cont_i = self.continuations[i];
1013
1014                assert(cur_entry_path[cont_i.tree_level as int] == cont_i.idx);
1015
1016                assert forall|j: int|
1017                    #![trigger cont_i.children[j]]
1018                    0 <= j < cont_i.children.len()
1019                        && cont_i.children[j] is Some implies cont_i.children[j].unwrap().subtree_satisfies(
1020                cont_i.path().push_tail(j), h) by {
1021                    let child_sub = cont_i.children[j].unwrap();
1022                    let child_path = cont_i.path().push_tail(j);
1023
1024                    subtree_unlock_upgrade(
1025                        child_sub,
1026                        child_path,
1027                        guards,
1028                        regions,
1029                        cur_entry_addr,
1030                        cur_entry_path,
1031                    );
1032                };
1033            }
1034        };
1035    }
1036}
1037
1038} // verus!