Skip to main content

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

1use vstd::prelude::*;
2use vstd_extra::{
3    arithmetic::{
4        lemma_nat_align_down_monotone, lemma_nat_align_down_sound,
5        lemma_nat_align_down_within_block, nat_align_down,
6    },
7    ghost_tree::*,
8    ownership::*,
9};
10
11use crate::specs::{
12    arch::NR_ENTRIES,
13    mm::page_table::{
14        cursor::{
15            owners::*,
16            page_size_lemmas::{lemma_page_size_divides, lemma_page_size_ge_base},
17        },
18        owners::*,
19        *,
20    },
21};
22
23use crate::mm::{PagingConstsTrait, PagingLevel, Vaddr, page_size, page_size_spec, page_table::*};
24
25verus! {
26
27broadcast use group_ghost_tree_lemmas;
28// ─── CursorContinuation mapping lemmas ───────────────────────────────────────
29
30impl<'rcu, C: PageTableConfig> CursorContinuation<'rcu, C> {
31    pub proof fn as_page_table_owner_preserves_view_mappings(self)
32        requires
33            self.inv(),
34            self.all_some(),
35        ensures
36            self.as_page_table_owner().view_rec(self.path()) == self.view_mappings(),
37            self.as_subtree().inv(),
38            PageTableOwner(self.as_subtree()).pt_inv(),
39    {
40        C::lemma_paging_consts_properties();
41        C::lemma_page_table_config_constant_properties();
42        broadcast use {CursorContinuation::group_lemmas, PageTableOwner::group_lemmas};
43
44        self.as_page_table_owner_pt_inv();
45    }
46
47    pub proof fn view_mappings_take_child(self)
48        requires
49            self.inv(),
50            self.all_some(),
51        ensures
52            self.take_child().1.view_mappings() == self.view_mappings()
53                - self.view_mappings_take_child_spec(),
54    {
55        hide(PageTableOwner::view_rec);
56        C::lemma_paging_consts_properties();
57        C::lemma_page_table_config_constant_properties();
58        broadcast use CursorContinuation::group_lemmas;
59
60        let def = self.take_child().1.view_mappings();
61        let diff = self.view_mappings() - self.view_mappings_take_child_spec();
62        assert forall|m: Mapping| diff.contains(m) implies def.contains(m) by {
63            let i = choose|i: int|
64                0 <= i < self.children.len() && #[trigger] self.children[i] is Some
65                    && PageTableOwner(self.children[i].unwrap()).view_rec(
66                    self.path().push_tail(i),
67                ).contains(m);
68            assert(self.take_child().1.children[i] is Some);
69        };
70        assert forall|m: Mapping| #![trigger def.contains(m)] def.contains(m) implies diff.contains(
71            m,
72        ) by {
73            let left = self.take_child().1;
74            if self.view_mappings_take_child_spec().contains(m) {
75                let i = choose|i: int|
76                    0 <= i < left.children.len() && #[trigger] left.children[i] is Some
77                        && PageTableOwner(left.children[i].unwrap()).view_rec(
78                        left.path().push_tail(i),
79                    ).contains(m);
80
81                PageTableOwner(self.children[self.idx as int].unwrap()).lemma_view_rec_vaddr_range(
82                    self.path().push_tail(self.idx as int),
83                    m,
84                );
85                PageTableOwner(left.children[i as int].unwrap()).lemma_view_rec_vaddr_range(
86                    left.path().push_tail(i),
87                    m,
88                );
89
90                let size = page_size::<C>((C::NR_LEVELS - self.path().len()) as PagingLevel);
91                // Positional disjointness; shift both sides by LEADING_BITS * 2^48.
92                sibling_paths_disjoint::<C>(self.path(), self.idx as int, i, size);
93                lemma_vaddr_of_eq_int::<C>(self.path().push_tail(self.idx as int));
94                lemma_vaddr_of_eq_int::<C>(self.path().push_tail(i));
95            }
96        };
97    }
98
99    pub proof fn view_mappings_put_child(self, child: OwnerSubtree<C>)
100        requires
101            self.inv(),
102            child.inv(),
103            self.all_but_index_some(),
104        ensures
105            self.put_child(child).view_mappings() == self.view_mappings() + PageTableOwner(
106                child,
107            ).view_rec(self.path().push_tail(self.idx as int)),
108    {
109        broadcast use CursorContinuation::group_lemmas;
110
111        let def = self.put_child(child).view_mappings();
112        let sum = self.view_mappings() + PageTableOwner(child).view_rec(
113            self.path().push_tail(self.idx as int),
114        );
115        assert forall|m: Mapping| sum.contains(m) implies def.contains(m) by {
116            if self.view_mappings().contains(m) {
117                let i = choose|i: int|
118                    0 <= i < self.children.len() && #[trigger] self.children[i] is Some
119                        && PageTableOwner(self.children[i].unwrap()).view_rec(
120                        self.path().push_tail(i),
121                    ).contains(m);
122                assert(self.put_child(child).children[i] == self.children[i]);
123            } else {
124                self.put_child(child).lemma_view_mappings_intro(m, self.idx as int);
125            }
126        };
127    }
128}
129
130impl<'rcu, C: PageTableConfig> CursorContinuation<'rcu, C> {
131    /// When a continuation has all_some and inv, its as_subtree() also has `TreeNode::inv()`.
132    proof fn as_subtree_inv(self)
133        requires
134            self.inv(),
135            self.all_some(),
136        ensures
137            self.as_subtree().inv(),
138    {
139        C::lemma_paging_consts_properties();
140        C::lemma_page_table_config_constant_properties();
141    }
142
143    pub(super) proof fn as_page_table_owner_pt_inv(self)
144        requires
145            self.inv(),
146            self.all_some(),
147        ensures
148            PageTableOwner(self.as_subtree()).pt_inv(),
149    {
150        C::lemma_paging_consts_properties();
151        C::lemma_page_table_config_constant_properties();
152        reveal(PageTableOwner::pt_inv_at_depth);
153    }
154}
155
156// ─── CursorOwner mapping lemmas ──────────────────────────────────────────────
157impl<'rcu, C: PageTableConfig> CursorOwner<'rcu, C> {
158    /// The current subtree's mappings equal the filter over [subtree_va, subtree_va + page_size(level))
159    /// where subtree_va = vaddr::<C>(cur_subtree path).
160    pub proof fn lemma_cur_subtree_eq_filtered_mappings_path(self)
161        requires
162            self.inv(),
163            self.in_barrier_va(),
164        ensures
165            ({
166                let subtree_va = vaddr_of::<C>(self.cur_subtree().value().path) as int;
167                let size = page_size::<C>(self.level) as int;
168                PageTableOwner(self.cur_subtree())@.mappings == self@.mappings.filter(
169                    |m: Mapping| subtree_va <= m.va_range.start < subtree_va + size,
170                )
171            }),
172    {
173        hide(PageTableOwner::view_rec);
174        C::lemma_paging_consts_properties();
175        broadcast use {CursorContinuation::group_lemmas, CursorOwner::group_lemmas};
176
177        let cur_subtree = self.cur_subtree();
178        let cur_path = cur_subtree.value().path;
179        let subtree_va = vaddr_of::<C>(cur_path) as int;
180        let size = page_size::<C>(self.level) as int;
181
182        let subtree_mappings = PageTableOwner(cur_subtree)@.mappings;
183        let filtered = self@.mappings.filter(
184            |m: Mapping| subtree_va <= m.va_range.start < subtree_va + size,
185        );
186
187        let cont = self.continuations[self.level - 1];
188        // cur_subtree.value.path == cont.path().push_tail(self.index())
189
190        // Forward: subtree mappings are in the filtered set
191        assert forall|m: Mapping| subtree_mappings.contains(m) implies filtered.contains(m) by {
192            // m is in the current subtree's view_rec => in cont[level-1].view_mappings() => in self.view_mappings()
193            // lemma_view_rec_vaddr_range gives m.va_range.start in
194            // [vaddr_of(cur_path), vaddr_of(cur_path) + page_size(level)),
195            // which is exactly our filter range.
196            PageTableOwner(cur_subtree).lemma_view_rec_vaddr_range(cur_path, m);
197        };
198
199        // Reverse: filtered mappings are in the subtree
200        assert forall|m: Mapping| filtered.contains(m) implies subtree_mappings.contains(m) by {
201            // m is in self.view_mappings() and subtree_va <= m.va_range.start < subtree_va + size
202            let i = choose|i: int|
203                self.level - 1 <= i < C::NR_LEVELS
204                    && #[trigger] self.continuations[i].view_mappings().contains(m);
205
206            let cont_i = self.continuations[i];
207            let j = choose|j: int|
208                #![auto]
209                0 <= j < NR_ENTRIES && cont_i.children[j] is Some && PageTableOwner(
210                    cont_i.children[j].unwrap(),
211                ).view_rec(cont_i.path().push_tail(j)).contains(m);
212
213            PageTableOwner(cont_i.children[j].unwrap()).lemma_view_rec_vaddr_range(
214                cont_i.path().push_tail(j),
215                m,
216            );
217
218            if i == self.level - 1 {
219                // j == index: cont_i.children[j] is exactly cur_subtree; m is already
220                // in subtree_mappings via view_rec at the same path.
221                if j as usize != self.index() {
222                    // Disjointness: sibling j's VA range doesn't overlap [subtree_va, subtree_va + page_size(level))
223                    let sib_size = page_size::<C>(
224                        (C::NR_LEVELS - cont.path().len()) as PagingLevel,
225                    );
226                    sibling_paths_disjoint::<C>(cont.path(), self.index() as int, j, sib_size);
227                    // Lift positional disjointness to canonical by adding
228                    // the same leading_bits * 2^48 to both sides.
229                    lemma_vaddr_of_eq_int::<C>(cont.path().push_tail(self.index() as int));
230                    lemma_vaddr_of_eq_int::<C>(cont.path().push_tail(j));
231                    assert(false);  // contradiction from disjointness + filter
232                }
233            } else {
234                if j as usize != cont_i.idx as int {
235                    // Subtree VA range is contained in ancestor child range
236                    self.subtree_va_in_ancestor_range(i);
237
238                    // Sibling j is disjoint from cont_i.idx child
239                    let sib_size = page_size::<C>(
240                        (C::NR_LEVELS - cont_i.path().len()) as PagingLevel,
241                    );
242                    sibling_paths_disjoint::<C>(cont_i.path(), cont_i.idx as int, j, sib_size);
243                    lemma_vaddr_of_eq_int::<C>(cont_i.path().push_tail(j));
244                    lemma_vaddr_of_eq_int::<C>(cur_path);
245                    assert(false);  // contradiction
246                } else {
247                    assert(false);  // cont_i.children[j] is Some contradicts
248                }
249            }
250        };
251
252    }
253
254    /// Version using nat_align_down(cur_va, page_size(level)) in the filter.
255    /// Uses the shared canonical subtree boundary rather than reconstructing the VA path.
256    pub proof fn lemma_cur_subtree_eq_filtered_mappings(self)
257        requires
258            self.inv(),
259            self.in_barrier_va(),
260        ensures
261            ({
262                let start = nat_align_down(
263                    self@.cur_va as nat,
264                    page_size::<C>(self.level) as nat,
265                ) as Vaddr;
266                let size = page_size::<C>(self.level);
267                PageTableOwner(self.cur_subtree())@.mappings == self@.mappings.filter(
268                    |m: Mapping| start <= m.va_range.start < start + size,
269                )
270            }),
271    {
272        C::lemma_paging_consts_properties();
273        C::lemma_page_table_config_constant_properties();
274        self.lemma_cur_subtree_eq_filtered_mappings_path();
275        self.cur_va_in_subtree_range();
276    }
277
278    /// The cursor's VA falls within the canonical VA range of any ancestor
279    /// continuation's child that the cursor descended through. Canonical
280    /// form: positional `vaddr::<C>(path)` plus the architecture-parameterized upper-address base.
281    pub(super) proof fn cur_va_in_cont_child_range(self, lvl: int)
282        requires
283            self.inv(),
284            self.in_barrier_va(),
285            self.level - 1 <= lvl < C::NR_LEVELS,
286        ensures
287            vaddr::<C>(self.continuations[lvl].path().push_tail(self.continuations[lvl].idx as int))
288                + vaddr_leading_part_spec::<C>(self.cur_va()) <= self.cur_va(),
289            self.cur_va() < vaddr::<C>(
290                self.continuations[lvl].path().push_tail(self.continuations[lvl].idx as int),
291            ) + vaddr_leading_part_spec::<C>(self.cur_va()) + page_size::<C>(
292                (lvl + 1) as PagingLevel,
293            ),
294            vaddr::<C>(self.continuations[lvl].path().push_tail(self.continuations[lvl].idx as int))
295                + vaddr_leading_part_spec::<C>(self.cur_va()) == nat_align_down(
296                self.cur_va() as nat,
297                page_size::<C>((lvl + 1) as PagingLevel) as nat,
298            ),
299            vaddr_of::<C>(
300                self.continuations[lvl].path().push_tail(self.continuations[lvl].idx as int),
301            ) == nat_align_down(
302                self.cur_va() as nat,
303                page_size::<C>((lvl + 1) as PagingLevel) as nat,
304            ),
305    {
306        C::lemma_paging_consts_properties();
307        C::lemma_page_table_config_constant_properties();
308        lemma_vaddr_leading_part_is_align_down::<C>(self.cur_va());
309        let cont = self.continuations[lvl];
310        let child_path = cont.path().push_tail(cont.idx as int);
311        assert(child_path.len() == C::NR_LEVELS - lvl);
312        assert forall|k: int| 0 <= k < child_path.len() implies #[trigger] child_path[k]
313            == pte_index_spec::<C>(self.cur_va(), (C::NR_LEVELS - k) as PagingLevel) by {
314            lemma_pte_index_bound::<C>(self.cur_va(), (C::NR_LEVELS - k) as PagingLevel);
315        };
316        lemma_vaddr_path_aligned::<C>(child_path, self.cur_va());
317        lemma_vaddr_of_eq_int::<C>(child_path);
318        lemma_page_size_ge_base::<C>((lvl + 1) as PagingLevel);
319        lemma_nat_align_down_sound(
320            self.cur_va() as nat,
321            page_size::<C>((lvl + 1) as PagingLevel) as nat,
322        );
323    }
324
325    /// The current subtree's VA range [subtree_va, subtree_va + page_size(level)) is contained
326    /// within the VA range of any ancestor continuation's child that the cursor descended through.
327    proof fn subtree_va_in_ancestor_range(self, lvl: int)
328        requires
329            self.inv(),
330            self.in_barrier_va(),
331            self.level - 1 < lvl < C::NR_LEVELS,
332        ensures
333            ({
334                let subtree_va = vaddr::<C>(self.cur_subtree().value().path);
335                let idx_path_va = vaddr::<C>(
336                    self.continuations[lvl].path().push_tail(self.continuations[lvl].idx as int),
337                );
338                &&& idx_path_va <= subtree_va
339                &&& subtree_va + page_size::<C>(self.level) <= idx_path_va + page_size::<C>(
340                    (lvl + 1) as PagingLevel,
341                )
342            }),
343    {
344        C::lemma_paging_consts_properties();
345        C::lemma_page_table_config_constant_properties();
346        self.cur_va_in_cont_child_range(self.level - 1);
347        self.cur_va_in_cont_child_range(lvl);
348
349        let x = self.cur_va() as nat;
350        let fine = page_size::<C>(self.level as PagingLevel) as nat;
351        let coarse = page_size::<C>((lvl + 1) as PagingLevel) as nat;
352
353        lemma_page_size_divides::<C>(self.level as PagingLevel, (lvl + 1) as PagingLevel);
354        lemma_nat_align_down_monotone(x, fine, coarse);
355        lemma_nat_align_down_within_block(x, fine, coarse);
356    }
357
358    /// Subtrees at different indices have disjoint VA ranges.
359    pub proof fn subtree_va_ranges_disjoint(self, j: int)
360        requires
361            self.inv(),
362            self.in_barrier_va(),
363            0 <= j < NR_ENTRIES,
364            j != self.index(),
365            self.continuations[self.level - 1].children[j] is Some,
366        ensures
367            vaddr::<C>(self.continuations[self.level - 1].path().push_tail(j))
368                + vaddr_leading_part_spec::<C>(self.cur_va()) + page_size::<C>(
369                self.level as PagingLevel,
370            ) <= self.cur_va() || self.cur_va() < vaddr::<C>(
371                self.continuations[self.level - 1].path().push_tail(j),
372            ) + vaddr_leading_part_spec::<C>(self.cur_va()),
373    {
374        C::lemma_paging_consts_properties();
375        C::lemma_page_table_config_constant_properties();
376        let cont = self.continuations[self.level - 1];
377        let idx = self.index();
378
379        // Establish cont.level() == self.level via case split
380        // cur_va is within the child at cont[level-1].idx
381        self.cur_va_in_cont_child_range(self.level - 1);
382
383        // Sibling paths are separated by `page_size(self.level)` (child page size).
384        let size = page_size::<C>((C::NR_LEVELS - cont.path().len()) as PagingLevel);
385        sibling_paths_disjoint::<C>(cont.path(), idx as int, j, size);
386    }
387
388    /// Children of higher-level continuations have VA ranges that don't include cur_va,
389    /// because cur_va's indices at those levels match the path to the current position.
390    pub proof fn higher_level_children_disjoint(self, i: int, j: int)
391        requires
392            self.inv(),
393            self.in_barrier_va(),
394            self.level - 1 < i < C::NR_LEVELS,
395            0 <= j < NR_ENTRIES,
396            j != self.continuations[i].idx,
397            self.continuations[i].children[j] is Some,
398        ensures
399            vaddr::<C>(self.continuations[i].path().push_tail(j)) + vaddr_leading_part_spec::<C>(
400                self.cur_va(),
401            ) + page_size::<C>((i + 1) as PagingLevel) <= self.cur_va() || self.cur_va() < vaddr::<
402                C,
403            >(self.continuations[i].path().push_tail(j)) + vaddr_leading_part_spec::<C>(
404                self.cur_va(),
405            ),
406    {
407        C::lemma_paging_consts_properties();
408        C::lemma_page_table_config_constant_properties();
409        let cont = self.continuations[i];
410
411        // Establish cont.level() == i + 1 via case split
412        // cur_va is within the child at cont[i].idx
413        self.cur_va_in_cont_child_range(i);
414
415        // Siblings at this depth are separated by `page_size(i+1)` (child page size).
416        let size = page_size::<C>((C::NR_LEVELS - cont.path().len()) as PagingLevel);
417        sibling_paths_disjoint::<C>(cont.path(), cont.idx as int, j, size);
418    }
419
420    /// Any mapping that covers cur_va must come from the current subtree.
421    /// This follows from the disjointness of VA ranges and the fact that
422    /// cur_va falls within the current subtree's VA range.
423    pub proof fn mapping_covering_cur_va_from_cur_subtree(self, m: Mapping)
424        requires
425            self.inv(),
426            self.in_barrier_va(),
427            self.view_mappings().contains(m),
428            m.va_range.start <= self.cur_va() < m.va_range.end,
429        ensures
430            PageTableOwner(self.cur_subtree()).view_rec(self.cur_subtree().value().path).contains(
431                m,
432            ),
433    {
434        C::lemma_paging_consts_properties();
435        C::lemma_page_table_config_constant_properties();
436        broadcast use {CursorContinuation::group_lemmas, CursorOwner::group_lemmas};
437
438        lemma_vaddr_leading_part_is_align_down::<C>(self.cur_va());
439        // m comes from some continuation level i
440
441        let i = choose|i: int|
442            self.level - 1 <= i < C::NR_LEVELS
443                && #[trigger] self.continuations[i].view_mappings().contains(m);
444
445        let cont_i = self.continuations[i];
446        let j = choose|j: int|
447            #![auto]
448            0 <= j < NR_ENTRIES && cont_i.children[j] is Some && PageTableOwner(
449                cont_i.children[j].unwrap(),
450            ).view_rec(cont_i.path().push_tail(j)).contains(m);
451
452        let child_j = cont_i.children[j].unwrap();
453        let path_j = cont_i.path().push_tail(j);
454        PageTableOwner(child_j).lemma_view_rec_vaddr_range(path_j, m);
455        // Bridge lemma_view_rec_vaddr_range's canonical bounds to the
456        // disjointness lemmas (also canonical after the refactor).
457        lemma_vaddr_of_eq_int::<C>(path_j);
458
459        if i == self.level - 1 {
460            if j as usize != self.index() {
461                self.subtree_va_ranges_disjoint(j);
462            }
463        } else {
464            if j as usize != cont_i.idx as int {
465                self.higher_level_children_disjoint(i, j);
466            } else {
467                assert(false);
468            }
469        }
470    }
471
472    /// Combined replace: swap the lowest continuation, relating old and new mapping sets.
473    /// Avoids the Map::remove phantom key issue by requiring both old and new to have inv().
474    #[verifier::spinoff_prover]
475    #[verifier::rlimit(60)]
476    pub proof fn view_mappings_replace_lowest(
477        old_self: Self,
478        new_self: Self,
479        old_cont: CursorContinuation<'rcu, C>,
480        new_cont: CursorContinuation<'rcu, C>,
481    )
482        requires
483            old_self.inv(),
484            old_self.in_barrier_va(),
485            new_self.inv(),
486            old_self.level == new_self.level,
487            old_self.continuations[old_self.level - 1] == old_cont,
488            new_self.continuations[new_self.level - 1] == new_cont,
489            forall|i: int|
490                old_self.level <= i < C::NR_LEVELS ==> old_self.continuations[i]
491                    == new_self.continuations[i],
492        ensures
493            new_self.view_mappings() == (old_self.view_mappings() - old_cont.view_mappings()).union(
494                new_cont.view_mappings(),
495            ),
496    {
497        hide(PageTableOwner::view_rec);
498        hide(CursorContinuation::inv_children_rel);
499        hide(CursorContinuation::inv_children);
500        hide(CursorContinuation::pt_inv_children);
501        C::lemma_paging_consts_properties();
502        C::lemma_page_table_config_constant_properties();
503        broadcast use {CursorContinuation::group_lemmas, CursorOwner::group_lemmas};
504
505        let level = old_self.level;
506
507        assert forall|m: Mapping| new_self.view_mappings().contains(m) implies ((
508        old_self.view_mappings().contains(m) && !old_cont.view_mappings().contains(m))
509            || new_cont.view_mappings().contains(m)) by {
510            let i = choose|i: int|
511                level - 1 <= i < C::NR_LEVELS
512                    && #[trigger] new_self.continuations[i].view_mappings().contains(m);
513            // i == level - 1: new_self.continuations[level-1] == new_cont, so the
514            // existential is witnessed by new_cont.view_mappings().contains(m).
515            if i != level - 1 {
516                assert(old_self.continuations[i] == new_self.continuations[i]);
517
518                if old_cont.view_mappings().contains(m) {
519                    old_self.lemma_higher_mapping_disjoint_from_lowest(i, m);
520                }
521            }
522        };
523
524    }
525
526    /// Mappings carried by the active continuations up to `level`.
527    closed spec fn view_mappings_through(self, level: PagingLevel) -> Set<Mapping>
528        decreases level,
529    {
530        if level <= self.level {
531            self.continuations[self.level - 1].view_mappings()
532        } else {
533            self.view_mappings_through((level - 1) as PagingLevel).union(
534                self.continuations[level - 1].view_mappings(),
535            )
536        }
537    }
538
539    pub proof fn as_page_table_owner_preserves_view_mappings(self)
540        requires
541            self.inv(),
542        ensures
543            self.as_page_table_owner().view_rec(self.continuations[C::NR_LEVELS - 1].path())
544                == self.view_mappings(),
545            self.as_page_table_owner().0.inv(),
546            self.as_page_table_owner().0.level() == self.continuations[C::NR_LEVELS - 1].tree_level,
547            self.as_page_table_owner().pt_inv(),
548    {
549        C::lemma_paging_consts_properties();
550        self.lemma_restore_to_level(C::NR_LEVELS);
551        let root = self.restore_to_level(C::NR_LEVELS);
552        root.as_subtree_inv();
553        root.as_page_table_owner_pt_inv();
554        root.as_page_table_owner_preserves_view_mappings();
555        broadcast use CursorOwner::group_lemmas;
556
557        assert forall|m: Mapping|
558            self.view_mappings_through(C::NR_LEVELS).contains(m)
559                == #[trigger] self.view_mappings().contains(m) by {
560            self.lemma_view_mappings_through_contains(C::NR_LEVELS, m);
561            if self.view_mappings_through(C::NR_LEVELS).contains(m) {
562                let i = choose|i: int|
563                    self.level - 1 <= i < C::NR_LEVELS
564                        && #[trigger] self.continuations[i].view_mappings().contains(m);
565                self.lemma_inv_continuation(i);
566                self.lemma_view_mappings_intro(m, i);
567            }
568        };
569        assert(self.view_mappings_through(C::NR_LEVELS) =~= self.view_mappings());
570    }
571
572    /// Every mapping in the cursor view satisfies `Mapping::inv()`.
573    pub proof fn lemma_view_mapping_inv(self)
574        requires
575            self.inv(),
576        ensures
577            forall|m: Mapping| self.view_mappings().contains(m) ==> #[trigger] m.inv(),
578    {
579        C::lemma_paging_consts_properties();
580        C::lemma_page_table_config_constant_properties();
581        self.as_page_table_owner_preserves_view_mappings();
582        let pto = self.as_page_table_owner();
583        let root_path = self.continuations[C::NR_LEVELS - 1].path();
584        pto.lemma_view_rec_mapping_inv(root_path);
585    }
586
587    /// Non-overlapping mappings in the cursor view.
588    ///
589    /// Collapses the union-over-continuations `view_mappings` into a single
590    /// `view_rec` rooted at the reconstructed root page table, then applies
591    /// `lemma_view_rec_disjoint_vaddrs` on that single subtree.
592    /// Follows from tree structure alone.
593    pub proof fn as_page_table_owner_view_non_overlapping(self)
594        requires
595            self.inv(),
596        ensures
597            self@.non_overlapping(),
598    {
599        C::lemma_paging_consts_properties();
600        C::lemma_page_table_config_constant_properties();
601        self.as_page_table_owner_preserves_view_mappings();
602        let pto = self.as_page_table_owner();
603        let root_path = self.continuations[C::NR_LEVELS - 1].path();
604
605        assert forall|m: Mapping, n: Mapping| #[trigger]
606            self@.mappings.contains(m) && #[trigger] self@.mappings.contains(n) && m
607                != n implies m.va_range.end <= n.va_range.start || n.va_range.end
608            <= m.va_range.start by {
609            pto.lemma_view_rec_disjoint_vaddrs(root_path, m, n);
610        };
611    }
612
613    proof fn lemma_view_mappings_through_contains(self, level: PagingLevel, m: Mapping)
614        requires
615            self.inv(),
616            self.level <= level <= C::NR_LEVELS,
617        ensures
618            self.view_mappings_through(level).contains(m) == (exists|i: int|
619                self.level - 1 <= i < level
620                    && #[trigger] self.continuations[i].view_mappings().contains(m)),
621        decreases level,
622    {
623        C::lemma_paging_consts_properties();
624        if level > self.level {
625            self.lemma_view_mappings_through_contains((level - 1) as PagingLevel, m);
626        }
627    }
628
629    /// Reassembly preserves ownership and the union of mappings at every actual level.
630    proof fn lemma_restore_to_level(self, level: PagingLevel)
631        requires
632            self.inv(),
633            self.level <= level <= C::NR_LEVELS,
634        ensures
635            self.restore_to_level(level).inv(),
636            self.restore_to_level(level).all_some(),
637            self.restore_to_level(level).entry_own == self.continuations[level - 1].entry_own,
638            self.restore_to_level(level).tree_level == self.continuations[level - 1].tree_level,
639            self.restore_to_level(level).guard == self.continuations[level - 1].guard,
640            self.restore_to_level(level).path() == self.continuations[level - 1].path(),
641            self.restore_to_level(level).idx == self.continuations[level - 1].idx,
642            self.restore_to_level(level).view_mappings() == self.view_mappings_through(level),
643        decreases level,
644    {
645        hide(PageTableOwner::view_rec);
646        C::lemma_page_table_config_constant_properties();
647        C::lemma_paging_consts_properties();
648        broadcast use vstd::map_lib::group_map_properties;
649
650        self.lemma_inv_continuation(level - 1);
651        let cont = self.continuations[level - 1];
652        if level == self.level {
653            assert(self.view_mappings_through(level) =~= cont.view_mappings());
654        } else {
655            self.lemma_restore_to_level((level - 1) as PagingLevel);
656            let child = self.restore_to_level((level - 1) as PagingLevel);
657            child.as_subtree_inv();
658            child.as_page_table_owner_pt_inv();
659            child.lemma_inv_children_unroll_all();
660            cont.lemma_inv_children_unroll_all();
661            cont.view_mappings_put_child(child.as_subtree());
662            cont.as_subtree_restore(child);
663            let restored = cont.restore(child).0;
664            assert(restored.inv());
665            restored.as_page_table_owner_preserves_view_mappings();
666            child.as_page_table_owner_preserves_view_mappings();
667            assert(self.view_mappings_through(level) =~= self.view_mappings_through(
668                (level - 1) as PagingLevel,
669            ).union(cont.view_mappings()));
670        }
671    }
672
673    // Isolate address-range geometry from the caller's mapping-set algebra.
674    #[verifier::spinoff_prover]
675    proof fn lemma_higher_mapping_disjoint_from_lowest(self, i: int, m: Mapping)
676        requires
677            self.inv(),
678            self.in_barrier_va(),
679            self.level <= i < C::NR_LEVELS,
680            self.continuations[i].view_mappings().contains(m),
681        ensures
682            !self.continuations[self.level - 1].view_mappings().contains(m),
683    {
684        hide(page_size_spec);
685        hide(PageTableOwner::view_rec);
686        hide(CursorContinuation::inv_children_rel);
687        hide(CursorContinuation::inv_children);
688        hide(CursorContinuation::pt_inv_children);
689        C::lemma_paging_consts_properties();
690        C::lemma_page_table_config_constant_properties();
691        broadcast use {CursorContinuation::group_lemmas, CursorOwner::group_lemmas};
692
693        let old_cont = self.continuations[self.level - 1];
694        if old_cont.view_mappings().contains(m) {
695            self.lemma_inv_continuation(i);
696            self.lemma_inv_continuation(self.level - 1);
697            let cont_i = self.continuations[i];
698            let j = choose|j: int|
699                #![auto]
700                0 <= j < NR_ENTRIES && cont_i.children[j] is Some && PageTableOwner(
701                    cont_i.children[j].unwrap(),
702                ).view_rec(cont_i.path().push_tail(j)).contains(m);
703            cont_i.lemma_inv_children_unroll(j);
704            cont_i.lemma_pt_inv_children_unroll(j);
705            cont_i.lemma_inv_children_rel_unroll(j);
706            PageTableOwner(cont_i.children[j].unwrap()).lemma_view_rec_vaddr_range(
707                cont_i.path().push_tail(j),
708                m,
709            );
710
711            let k = choose|k: int|
712                #![auto]
713                0 <= k < NR_ENTRIES && old_cont.children[k] is Some && PageTableOwner(
714                    old_cont.children[k].unwrap(),
715                ).view_rec(old_cont.path().push_tail(k)).contains(m);
716            old_cont.lemma_inv_children_unroll(k);
717            old_cont.lemma_pt_inv_children_unroll(k);
718            old_cont.lemma_inv_children_rel_unroll(k);
719            PageTableOwner(old_cont.children[k].unwrap()).lemma_view_rec_vaddr_range(
720                old_cont.path().push_tail(k),
721                m,
722            );
723
724            if j as usize != cont_i.idx as int {
725                self.cur_va_in_cont_child_range(self.level as int);
726                self.cur_va_in_cont_child_range(i);
727
728                let x = self.cur_va() as nat;
729                let ps_node = page_size::<C>((self.level + 1) as PagingLevel) as nat;
730                let ps_anc = page_size::<C>((i + 1) as PagingLevel) as nat;
731
732                lemma_page_size_ge_base::<C>((i + 1) as PagingLevel);
733                lemma_page_size_divides::<C>(
734                    (self.level + 1) as PagingLevel,
735                    (i + 1) as PagingLevel,
736                );
737
738                lemma_nat_align_down_monotone(x, ps_node, ps_anc);
739                lemma_nat_align_down_within_block(x, ps_node, ps_anc);
740
741                let sib_size = page_size::<C>((C::NR_LEVELS - cont_i.path().len()) as PagingLevel);
742                sibling_paths_disjoint::<C>(cont_i.path(), cont_i.idx as int, j, sib_size);
743                // Lift positional disjointness to canonical.
744                lemma_vaddr_of_eq_int::<C>(cont_i.path().push_tail(cont_i.idx as int));
745                lemma_vaddr_of_eq_int::<C>(cont_i.path().push_tail(j));
746
747                old_cont.as_subtree_inv();
748                old_cont.as_page_table_owner_preserves_view_mappings();
749                PageTableOwner(old_cont.as_subtree()).lemma_view_rec_vaddr_range(
750                    old_cont.path(),
751                    m,
752                );
753            } else {
754                assert(cont_i.children[cont_i.idx as int] is None);
755            }
756        }
757    }
758}
759
760} // verus!