Skip to main content

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

1//! Tree-predicate lifting, tree entry level constraints, and tree membership
2//! lemmas for `CursorContinuation` and `CursorOwner`.
3use vstd::{
4    arithmetic::div_mod::{lemma_small_mod, lemma_sub_mod_noop},
5    prelude::*,
6};
7use vstd_extra::{arithmetic::lemma_nat_align_down_sound, ghost_tree::*, ownership::*};
8
9use crate::specs::{
10    arch::*,
11    mm::page_table::{
12        cursor::owners::{CursorContinuation, CursorOwner},
13        node::entry_owners::EntryOwner,
14        owners::*,
15    },
16};
17
18use crate::mm::{
19    Paddr, PagingConstsTrait, PagingLevel, Vaddr, page_prop::PageProperty, page_size, page_table::*,
20};
21use core::ops::Range;
22
23verus! {
24
25// ─── Tree predicate lifting (CursorContinuation) ───────────────────
26impl<'rcu, C: PageTableConfig> CursorContinuation<'rcu, C> {
27    /// Lift map_children(f) to map_children(g) when implies(f, g).
28    pub proof fn map_children_lift(
29        self,
30        f: spec_fn(EntryOwner<C>, TreePath<NR_ENTRIES>) -> bool,
31        g: spec_fn(EntryOwner<C>, TreePath<NR_ENTRIES>) -> bool,
32    )
33        requires
34            self.inv(),
35            self.map_children(f),
36            OwnerSubtree::implies(f, g),
37        ensures
38            self.map_children(g),
39    {
40        reveal(CursorContinuation::map_children);
41        assert forall|j: int|
42            #![auto]
43            0 <= j < self.children.len()
44                && self.children[j] is Some implies self.children[j].unwrap().subtree_satisfies(
45            self.path().push_tail(j),
46            g,
47        ) by {
48            self.lemma_inv_children_unroll(j);
49            OwnerSubtree::lemma_subtree_satisfies_implies(
50                self.children[j].unwrap(),
51                self.path().push_tail(j),
52                f,
53                g,
54            );
55        };
56    }
57
58    /// Lift map_children from f to g when siblings (j != idx) come from
59    /// cont0 which had map_children(f), and the child at idx (if present)
60    /// already satisfies g.
61    pub proof fn lemma_map_children_lift_skip_idx(
62        self,
63        cont0: Self,
64        idx: int,
65        f: spec_fn(EntryOwner<C>, TreePath<NR_ENTRIES>) -> bool,
66        g: spec_fn(EntryOwner<C>, TreePath<NR_ENTRIES>) -> bool,
67    )
68        requires
69            0 <= idx < NR_ENTRIES,
70            OwnerSubtree::implies(f, g),
71            cont0.inv(),
72            cont0.map_children(f),
73            self.path() == cont0.path(),
74            self.children.len() == cont0.children.len(),
75            forall|j: int|
76                #![auto]
77                0 <= j < NR_ENTRIES && j != idx ==> self.children[j] == cont0.children[j],
78            self.children[idx] is Some ==> self.children[idx]->0.subtree_satisfies(
79                self.path().push_tail(idx as int),
80                g,
81            ),
82        ensures
83            self.map_children(g),
84    {
85        reveal(CursorContinuation::map_children);
86        assert forall|j: int|
87            #![auto]
88            0 <= j < self.children.len()
89                && self.children[j] is Some implies self.children[j].unwrap().subtree_satisfies(
90            self.path().push_tail(j),
91            g,
92        ) by {
93            if j != idx {
94                cont0.lemma_inv_children_unroll(j);
95                OwnerSubtree::lemma_subtree_satisfies_implies(
96                    cont0.children[j].unwrap(),
97                    cont0.path().push_tail(j),
98                    f,
99                    g,
100                );
101            }
102        };
103    }
104
105    pub proof fn as_subtree_restore(self, child: Self)
106        requires
107            self.inv(),
108            child.inv(),
109            self.all_but_index_some(),
110            child.all_some(),
111        ensures
112            self.restore(child).0.as_subtree() == self.put_child(child.as_subtree()).as_subtree(),
113    {
114        assert(self.put_child(child.as_subtree()).children == self.children.update(
115            self.idx as int,
116            Some(child.as_subtree()),
117        ));
118    }
119}
120
121// ─── Tree predicate lifting (CursorOwner) ──────────────────────────
122impl<'rcu, C: PageTableConfig> CursorOwner<'rcu, C> {
123    pub proof fn lemma_map_children_implies(
124        self,
125        f: spec_fn(EntryOwner<C>, TreePath<NR_ENTRIES>) -> bool,
126        g: spec_fn(EntryOwner<C>, TreePath<NR_ENTRIES>) -> bool,
127    )
128        requires
129            self.inv(),
130            OwnerSubtree::implies(f, g),
131            forall|i: int|
132                #![trigger self.continuations[i]]
133                self.level - 1 <= i < C::NR_LEVELS ==> self.continuations[i].map_children(f),
134        ensures
135            forall|i: int|
136                #![trigger self.continuations[i]]
137                self.level - 1 <= i < C::NR_LEVELS ==> self.continuations[i].map_children(g),
138    {
139        C::lemma_paging_consts_properties();
140        C::lemma_page_table_config_constant_properties();
141        assert forall|i: int|
142            #![trigger self.continuations[i]]
143            self.level - 1 <= i < C::NR_LEVELS implies self.continuations[i].map_children(g) by {
144            self.lemma_inv_continuation(i);
145            self.continuations[i].map_children_lift(f, g);
146        }
147    }
148
149    // ─── Tree entry level constraints ──────────────────────────
150    pub proof fn lemma_cur_entry_node_implies_level_gt_1(self)
151        requires
152            self.inv(),
153            self.cur_entry_owner().is_node(),
154        ensures
155            self.level > 1,
156    {
157        self.lemma_cur_subtree_inv();
158    }
159
160    /// A frame entry at the cursor's current level that doesn't fit the aligned range
161    /// `[cur_va, end)` must be at level > 1.
162    /// Justification: At level 1, `page_size(1) == BASE_PAGE_SIZE`. Since the cursor VA
163    /// and `end` are BASE_PAGE_SIZE-aligned and `cur_va < end`, we have
164    /// `cur_va + page_size(1) <= end`, so a level-1 frame always fits. Therefore
165    /// `!cur_entry_fits_range` implies `level > 1`.
166    #[verifier::spinoff_prover]
167    pub proof fn frame_not_fits_implies_level_gt_1(
168        self,
169        cur_entry_fits_range: bool,
170        cur_va: Vaddr,
171        end: Vaddr,
172    )
173        requires
174            self.inv(),
175            self.cur_entry_owner().is_frame(),
176            !cur_entry_fits_range,
177            cur_va < end,
178            // cur_va is the cursor's current VA (from the call site: cur_va == self.va).
179            cur_va == self.cur_va(),
180            // Definition: cur_entry_fits_range iff cur_va is at start of entry AND entry end <= end.
181            cur_entry_fits_range == (cur_va == self.cur_va_range().start && self.cur_va_range().end
182                <= end),
183            // cur_va and end are PAGE_SIZE-aligned
184            cur_va as nat % PAGE_SIZE as nat == 0,
185            end as nat % PAGE_SIZE as nat == 0,
186        ensures
187            self.level > 1,
188    {
189        C::lemma_paging_consts_properties();
190        C::lemma_page_table_config_constant_properties();
191        if self.level == 1 {
192            crate::specs::mm::page_table::cursor::page_size_lemmas::lemma_page_size_base::<C>();
193            lemma_nat_align_down_sound(cur_va as nat, PAGE_SIZE as nat);
194            lemma_sub_mod_noop(end as int, cur_va as int, PAGE_SIZE as int);
195            if end - cur_va < PAGE_SIZE {
196                lemma_small_mod((end - cur_va) as nat, PAGE_SIZE as nat);
197                assert(false);
198            }
199        }
200    }
201}
202
203} // verus!