Skip to main content

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

1//! Cursor function-specific lemmas for `CursorOwner`.
2use vstd::{arithmetic::power2::pow2, prelude::*};
3use vstd_extra::{ghost_tree::*, ownership::*};
4
5use crate::specs::{
6    arch::*,
7    mm::{
8        frame::meta_region_owners::MetaRegionOwners,
9        page_table::{
10            cursor::owners::{CursorContinuation, CursorOwner},
11            cursor::page_size_lemmas::lemma_page_size_next,
12            owners::*,
13            *,
14        },
15    },
16};
17
18use crate::mm::{PagingConstsTrait, PagingLevel, Vaddr, page_size, page_table::*};
19use core::ops::Range;
20
21verus! {
22
23broadcast use group_ghost_tree_lemmas;
24
25impl<'rcu, C: PageTableConfig> CursorOwner<'rcu, C> {
26    /// Restore a tree predicate after replacing only the current child subtree.
27    pub proof fn lemma_replaced_child_preserves_map_full_tree(
28        self,
29        old: 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            old.inv(),
36            self.level == old.level,
37            old.map_full_tree(f),
38            OwnerSubtree::implies(f, g),
39            forall|i: int|
40                self.level <= i < C::NR_LEVELS ==> #[trigger] self.continuations[i]
41                    == old.continuations[i],
42            self.continuations[self.level - 1].path() == old.continuations[old.level - 1].path(),
43            self.index() == old.index(),
44            forall|j: int|
45                0 <= j < NR_ENTRIES && j != old.index() ==> #[trigger] self.continuations[self.level
46                    - 1].children[j] == old.continuations[old.level - 1].children[j],
47            self.cur_subtree().subtree_satisfies(self.cur_entry_owner().path, g),
48        ensures
49            self.map_full_tree(g),
50    {
51        C::lemma_paging_consts_properties();
52        C::lemma_page_table_config_constant_properties();
53        assert forall|i: int|
54            self.level - 1 <= i
55                < C::NR_LEVELS implies #[trigger] self.continuations[i].map_children(g) by {
56            if i == self.level - 1 {
57                self.continuations[i].lemma_map_children_lift_skip_idx(
58                    old.continuations[i],
59                    self.index() as int,
60                    f,
61                    g,
62                );
63            } else {
64                old.continuations[i].map_children_lift(f, g);
65            }
66        };
67    }
68
69    /// Replace the huge current mapping with the proven view of its split subtree.
70    pub proof fn lemma_split_current_mappings(self, old: Self)
71        requires
72            self.inv(),
73            old.inv(),
74            self.in_barrier_va(),
75            old.in_barrier_va(),
76            self.level == old.level,
77            self.va == old.va,
78            old.cur_entry_owner().is_frame(),
79            self.cur_entry_owner().is_node(),
80            self.index() == old.index(),
81            self.continuations[self.level - 1].path() == old.continuations[old.level - 1].path(),
82            forall|i: int|
83                self.level <= i < C::NR_LEVELS ==> #[trigger] self.continuations[i]
84                    == old.continuations[i],
85            forall|j: int|
86                0 <= j < NR_ENTRIES && j != old.index() ==> #[trigger] self.continuations[self.level
87                    - 1].children[j] == old.continuations[old.level - 1].children[j],
88            1 < old.level <= C::NR_LEVELS,
89            PageTableOwner(self.cur_subtree()).view_rec(self.cur_entry_owner().path) == Set::<
90                int,
91            >::range(0, NR_ENTRIES as int).map(
92                |i: int|
93                    CursorView::<C>::split_index(
94                        PageTableOwner(old.cur_subtree()).view_rec(
95                            old.cur_entry_owner().path,
96                        ).choose(),
97                        page_size::<C>((old.level - 1) as PagingLevel),
98                        i as usize,
99                    ),
100            ),
101        ensures
102            self@ == old@.split_if_mapped_huge_spec(page_size::<C>((old.level - 1) as PagingLevel)),
103            old@.inv(),
104            old@.present(),
105            old@.query_mapping().page_size == page_size::<C>(old.level),
106    {
107        hide(CursorContinuation::inv_children_rel);
108        hide(CursorContinuation::inv_children);
109        hide(CursorContinuation::pt_inv_children);
110        hide(CursorOwner::view_mappings);
111        hide(CursorContinuation::view_mappings);
112        C::lemma_paging_consts_properties();
113        C::lemma_page_table_config_constant_properties();
114        broadcast use {CursorContinuation::group_lemmas, CursorOwner::group_lemmas};
115
116        old.lemma_inv_continuation(old.level - 1);
117        let old_cont = old.continuations[old.level - 1];
118        old_cont.lemma_inv_children_unroll(old_cont.idx as int);
119        old_cont.lemma_inv_children_rel_unroll(old_cont.idx as int);
120        old_cont.lemma_pt_inv_children_unroll(old_cont.idx as int);
121        self.lemma_cur_subtree_inv();
122        old.view_preserves_inv();
123        old.lemma_cur_subtree_inv();
124        old.cur_va_in_subtree_range();
125        old.lemma_cur_entry_frame_present();
126        let old_subtree = old.cur_subtree();
127        let path = old.cur_entry_owner().path;
128        let m = old@.query_mapping();
129        old.query_mapping_from_subtree(m);
130        assert(PageTableOwner(old_subtree).view_rec(path) == set![m]);
131        vstd::set::lemma_set_choose_len(set![m]);
132        let size = page_size::<C>((old.level - 1) as PagingLevel);
133        lemma_page_size_next::<C>((old.level - 1) as PagingLevel);
134        C::lemma_paging_consts_properties();
135        C::lemma_page_table_config_constant_properties();
136        assert(m.page_size / size == NR_ENTRIES) by (nonlinear_arith)
137            requires
138                size > 0,
139                m.page_size == size * NR_ENTRIES,
140        ;
141        let cont0 = old.continuations[old.level - 1];
142        let cont1 = self.continuations[self.level - 1];
143        cont1.lemma_inv_children_rel_unroll(cont1.idx as int);
144        cont0.view_mappings_take_child();
145        cont1.view_mappings_take_child();
146        cont0.take_child_preserves_inv();
147        cont1.take_child_preserves_inv();
148        let siblings0 = cont0.take_child().1;
149        let siblings1 = cont1.take_child().1;
150        assert(siblings0.view_mappings() == siblings1.view_mappings()) by {
151            assert forall|j: int| 0 <= j < NR_ENTRIES implies (#[trigger] siblings0.children[j])
152                == siblings1.children[j] by {};
153            assert forall|e: Mapping|
154                siblings0.view_mappings().contains(e) implies siblings1.view_mappings().contains(
155                e,
156            ) by {
157                let j = choose|j: int|
158                    0 <= j < NR_ENTRIES && (#[trigger] siblings0.children[j]) is Some
159                        && PageTableOwner(siblings0.children[j]->0).view_rec(
160                        siblings0.path().push_tail(j),
161                    ).contains(e);
162                siblings1.lemma_view_mappings_intro(e, j);
163            };
164            assert forall|e: Mapping|
165                siblings1.view_mappings().contains(e) implies siblings0.view_mappings().contains(
166                e,
167            ) by {
168                let j = choose|j: int|
169                    0 <= j < NR_ENTRIES && (#[trigger] siblings1.children[j]) is Some
170                        && PageTableOwner(siblings1.children[j]->0).view_rec(
171                        siblings1.path().push_tail(j),
172                    ).contains(e);
173                siblings0.lemma_view_mappings_intro(e, j);
174            };
175        };
176        let new_subtree = self.cur_subtree();
177        siblings1.view_mappings_put_child(new_subtree);
178        assert(siblings1.put_child(new_subtree).view_mappings() == cont1.view_mappings());
179        Self::view_mappings_replace_lowest(old, self, cont0, cont1);
180        assert(cont0.view_mappings().subset_of(old.view_mappings())) by {
181            assert forall|e: Mapping|
182                cont0.view_mappings().contains(e) implies old.view_mappings().contains(e) by {
183                old.lemma_view_mappings_intro(e, old.level - 1);
184            };
185        };
186        assert(self.view_mappings() =~= old.view_mappings() - set![m] + PageTableOwner(
187            new_subtree,
188        ).view_rec(self.cur_entry_owner().path));
189    }
190
191    /// Restoring a changed child and parent PTE preserves cursor and metadata invariants.
192    pub proof fn lemma_replace_preserves_cursor_inv_metaregion(
193        self,
194        other: Self,
195        regions: MetaRegionOwners,
196    )
197        requires
198            self.inv(),
199            self.in_barrier_va(),
200            !self.popped_too_high,
201            self.metaregion_sound(regions),
202            // cursor level and structural fields unchanged
203            self.level == other.level,
204            self.guard_level == other.guard_level,
205            self.va == other.va,
206            self.barrier_va == other.barrier_va,
207            self.prefix == other.prefix,
208            self.popped_too_high == other.popped_too_high,
209            // higher-level continuations unchanged
210            forall|i: int|
211                self.level <= i < C::NR_LEVELS ==> #[trigger] self.continuations[i]
212                    == other.continuations[i],
213            // bottom continuation well-formed after protect
214            other.continuations[self.level - 1].inv(),
215            other.continuations[self.level - 1].all_some(),
216            other.continuations[self.level - 1].idx == self.continuations[self.level - 1].idx,
217            other.continuations[self.level - 1].entry_own.parent_level
218                == self.continuations[self.level - 1].entry_own.parent_level,
219            other.continuations[self.level - 1].guard.inner.slot_vaddr()
220                == self.continuations[self.level - 1].guard.inner.slot_vaddr(),
221            other.continuations[self.level - 1].path() == self.continuations[self.level - 1].path(),
222            other.continuations.dom() =~= self.continuations.dom(),
223            forall|j: int|
224                0 <= j < NR_ENTRIES && j != self.continuations[self.level - 1].idx
225                    ==> #[trigger] other.continuations[self.level - 1].children[j]
226                    == self.continuations[self.level - 1].children[j],
227            ({
228                let new_child = other.continuations[self.level
229                    - 1].children[other.continuations[self.level - 1].idx as int]->0;
230                let new_path = other.continuations[self.level - 1].path().push_tail(
231                    other.continuations[self.level - 1].idx as int,
232                );
233                new_child.subtree_satisfies(
234                    new_path,
235                    PageTableOwner::<C>::metaregion_sound_pred(regions),
236                )
237            }),
238            other.continuations[self.level - 1].entry_own.metaregion_sound(regions),
239        ensures
240            other.inv(),
241            other.metaregion_sound(regions),
242    {
243        C::lemma_paging_consts_properties();
244        C::lemma_page_table_config_constant_properties();
245        self.lemma_cur_pte_index();
246        other.lemma_map_branch_none_inv_holds(self);
247
248        let f = PageTableOwner::<C>::metaregion_sound_pred(regions);
249        let L = self.level as int;
250        let idx = self.continuations[L - 1].idx as int;
251
252        assert forall|i: int|
253            #![trigger other.continuations[i]]
254            other.level - 1 <= i < C::NR_LEVELS implies other.continuations[i].map_children(f) by {
255            reveal(CursorContinuation::map_children);
256            if i > L - 1 {
257                assert(other.continuations[i] == self.continuations[i]);
258                assert(self.continuations[i].map_children(f));
259            } else {
260                assert(i == L - 1);
261                let o_cont = other.continuations[L - 1];
262                let s_cont = self.continuations[L - 1];
263                reveal(CursorContinuation::inv_children);
264                assert forall|j: int|
265                    #![trigger o_cont.children[j]]
266                    0 <= j < o_cont.children.len()
267                        && o_cont.children[j] is Some implies o_cont.children[j].unwrap().subtree_satisfies(
268                o_cont.path().push_tail(j), f) by {
269                    if j != idx {
270                        assert(o_cont.children[j] == s_cont.children[j]);
271                        s_cont.lemma_inv_children_unroll(j);
272                    }
273                };
274            }
275        };
276
277        reveal(CursorOwner::path_metaregion_sound);
278        assert forall|i: int|
279            #![trigger other.continuations[i]]
280            other.level - 1 <= i
281                < C::NR_LEVELS implies other.continuations[i].entry_own.metaregion_sound(
282            regions,
283        ) by {
284            if i > L - 1 {
285                assert(other.continuations[i] == self.continuations[i]);
286                self.lemma_inv_continuation(i);
287            }
288        };
289    }
290
291    pub proof fn lemma_map_branch_none_inv_holds(self, owner0: Self)
292        requires
293            owner0.inv(),
294            // The map happens in the locked range and changes only the current
295            // continuation's slot at `idx` (a real, in-range slot). With this +
296            // "higher continuations unchanged", the root continuation's
297            // isolation clauses are preserved.
298            self.in_barrier_va(),
299            !self.popped_too_high,
300            forall|j: int|
301                0 <= j < NR_ENTRIES && j != owner0.continuations[owner0.level - 1].idx ==> (
302                #[trigger] self.continuations[self.level - 1].children[j])
303                    == owner0.continuations[owner0.level - 1].children[j],
304            self.level == owner0.level,
305            self.va == owner0.va,
306            self.barrier_va == owner0.barrier_va,
307            self.guard_level == owner0.guard_level,
308            self.prefix == owner0.prefix,
309            self.popped_too_high == owner0.popped_too_high,
310            // Higher-level continuations unchanged
311            forall|i: int|
312                self.level <= i < C::NR_LEVELS ==> #[trigger] self.continuations[i]
313                    == owner0.continuations[i],
314            // Bottom continuation is well-formed
315            self.continuations[self.level - 1].inv(),
316            self.continuations[self.level - 1].all_some(),
317            self.continuations[self.level - 1].idx == owner0.continuations[owner0.level - 1].idx,
318            self.continuations[self.level - 1].entry_own.parent_level
319                == owner0.continuations[owner0.level - 1].entry_own.parent_level,
320            // Guard address preserved (from parent_perms_preserved).
321            self.continuations[self.level - 1].guard.inner.slot_vaddr()
322                == owner0.continuations[owner0.level - 1].guard.inner.slot_vaddr(),
323            self.continuations[self.level - 1].path() == owner0.continuations[owner0.level
324                - 1].path(),
325            pte_index_spec::<C>(self.cur_va(), self.level as PagingLevel)
326                == self.continuations[self.level - 1].idx,
327            // Domain preserved: same keys as owner0.
328            self.continuations.dom() =~= owner0.continuations.dom(),
329        ensures
330            self.inv(),
331    {
332        let L = self.level as int;
333        C::lemma_paging_consts_properties();
334        C::lemma_page_table_config_constant_properties();
335        lemma_pte_index_bound::<C>(self.cur_va(), self.level);
336        assert(pte_index_spec::<C>(self.va, self.level as PagingLevel)
337            == self.continuations[self.level - 1].idx);
338        assert(self.continuations[L - 1].level() == self.level);
339        assert(self.continuations.contains_key(L - 1));
340        if self.level < C::NR_LEVELS {
341            assert(self.continuations[C::NR_LEVELS - 1] == owner0.continuations[C::NR_LEVELS - 1]);
342        } else {
343            owner0.lemma_in_barrier_va_top_index_bounds();
344            assert(self.continuations[C::NR_LEVELS - 1].idx == pte_index_spec::<C>(
345                self.va,
346                C::NR_LEVELS as PagingLevel,
347            ));
348            assert(self.continuations[C::NR_LEVELS - 1].idx == owner0.continuations[owner0.level
349                - 1].idx);
350            assert(C::TOP_LEVEL_INDEX_RANGE.start <= owner0.continuations[owner0.level - 1].idx
351                < C::TOP_LEVEL_INDEX_RANGE.end);
352            assert(forall|j: int|
353                0 <= j < NR_ENTRIES && !(C::TOP_LEVEL_INDEX_RANGE.start <= j
354                    < C::TOP_LEVEL_INDEX_RANGE.end) ==> (#[trigger] self.continuations[C::NR_LEVELS
355                    - 1].children[j]) is Some ==> (self.continuations[C::NR_LEVELS
356                    - 1].children[j].unwrap().value().is_borrowed()
357                    || self.continuations[C::NR_LEVELS
358                    - 1].children[j].unwrap().value().is_absent()));
359        }
360    }
361
362    /// After alloc_if_none (absent->node), `view_mappings` is unchanged (both contribute zero mappings).
363    pub proof fn map_branch_none_no_new_mappings(self, owner0: Self)
364        requires
365            owner0.inv(),
366            owner0.in_barrier_va(),
367            self.inv(),
368            self.in_barrier_va(),
369            self.level == owner0.level,
370            self.va == owner0.va,
371            forall|i: int|
372                self.level <= i < C::NR_LEVELS ==> #[trigger] self.continuations[i]
373                    == owner0.continuations[i],
374            // child at idx changed from absent to empty node
375            owner0.continuations[owner0.level - 1].children[owner0.continuations[owner0.level
376                - 1].idx as int] is Some,
377            owner0.continuations[owner0.level - 1].children[owner0.continuations[owner0.level
378                - 1].idx as int]->0.value().is_absent(),
379            self.continuations[self.level - 1].children[self.continuations[self.level
380                - 1].idx as int] is Some,
381            self.continuations[self.level - 1].children[self.continuations[self.level
382                - 1].idx as int]->0.value().is_node(),
383            // Non-idx children and path preserved
384            self.continuations[self.level - 1].path() == owner0.continuations[owner0.level
385                - 1].path(),
386            forall|j: int|
387                0 <= j < NR_ENTRIES && j != owner0.continuations[owner0.level - 1].idx as int
388                    ==> #[trigger] self.continuations[self.level - 1].children[j]
389                    == owner0.continuations[owner0.level - 1].children[j],
390            // The new node's subtree has empty view_rec (from alloc_if_none postcondition)
391            PageTableOwner(
392                self.continuations[self.level - 1].children[self.continuations[self.level
393                    - 1].idx as int]->0,
394            ).view_rec(
395                self.continuations[self.level - 1].path().push_tail(
396                    self.continuations[self.level - 1].idx as int,
397                ),
398            ) =~= Set::<Mapping>::empty(),
399        ensures
400            self.view_mappings() == owner0.view_mappings(),
401    {
402        C::lemma_paging_consts_properties();
403        C::lemma_page_table_config_constant_properties();
404        broadcast use {CursorContinuation::group_lemmas, CursorOwner::group_lemmas};
405
406        let L = self.level as int;
407        let cont = self.continuations[L - 1];
408        let cont0 = owner0.continuations[L - 1];
409        let idx = cont0.idx as int;
410
411        assert(cont.view_mappings() == cont0.view_mappings()) by {
412            cont0.lemma_inv_children_unroll(idx);
413            PageTableOwner(cont0.children[idx].unwrap()).lemma_view_rec_absent_empty(
414                cont0.path().push_tail(idx as int),
415            );
416            assert forall|m: Mapping|
417                cont.view_mappings().contains(m) implies cont0.view_mappings().contains(m) by {
418                let j = choose|j: int|
419                    0 <= j < cont.children.len() && #[trigger] cont.children[j] is Some
420                        && PageTableOwner(cont.children[j].unwrap()).view_rec(
421                        cont.path().push_tail(j),
422                    ).contains(m);
423                if j == idx {
424                    // cont.children[idx]'s view_rec == empty (from precondition)
425                    assert(false);
426                } else {
427                    assert(cont.children[j] == cont0.children[j]);
428                }
429            };
430            assert forall|m: Mapping|
431                cont0.view_mappings().contains(m) implies cont.view_mappings().contains(m) by {
432                let j = choose|j: int|
433                    0 <= j < cont0.children.len() && #[trigger] cont0.children[j] is Some
434                        && PageTableOwner(cont0.children[j].unwrap()).view_rec(
435                        cont0.path().push_tail(j),
436                    ).contains(m);
437                if j == idx {
438                    // cont0.children[idx] is absent, view_rec is empty
439                    assert(false);
440                } else {
441                    assert(cont0.children[j] == cont.children[j]);
442                }
443            };
444        };
445        // Lift cont == cont0 to self.view_mappings() == owner0.view_mappings()
446        assert(self.view_mappings() == owner0.view_mappings()) by {
447            assert forall|m: Mapping|
448                self.view_mappings().contains(m) implies owner0.view_mappings().contains(m) by {
449                let i = choose|i: int|
450                    self.level - 1 <= i < C::NR_LEVELS
451                        && #[trigger] self.continuations[i].view_mappings().contains(m);
452                if i == L - 1 {
453                    assert(cont0.view_mappings().contains(m));
454                } else {
455                    assert(owner0.continuations[i] == self.continuations[i]);
456                }
457            };
458            assert forall|m: Mapping|
459                owner0.view_mappings().contains(m) implies self.view_mappings().contains(m) by {
460                let i = choose|i: int|
461                    owner0.level - 1 <= i < C::NR_LEVELS
462                        && #[trigger] owner0.continuations[i].view_mappings().contains(m);
463                if i == L - 1 {
464                    assert(cont.view_mappings().contains(m));
465                } else {
466                    assert(self.continuations[i] == owner0.continuations[i]);
467                }
468            };
469        };
470    }
471
472    /// Repositioning within the same indexed slot preserves the owner invariant.
473    #[verifier::spinoff_prover]
474    pub(crate) proof fn lemma_set_va_same_indices_preserves_inv(self, new_va: Vaddr)
475        requires
476            self.inv(),
477            self.in_barrier_va(),
478            !self.popped_too_high,
479            self.va <= new_va <= self.barrier_va.end,
480            new_va % C::BASE_PAGE_SIZE == 0,
481            vaddr_leading_part_spec::<C>(new_va) == vaddr_leading_part_spec::<C>(self.va),
482            forall|i: PagingLevel|
483                self.level <= i <= C::NR_LEVELS ==> #[trigger] pte_index_spec::<C>(new_va, i)
484                    == pte_index_spec::<C>(self.va, i),
485        ensures
486            (Self { va: new_va, ..self }).inv(),
487    {
488        C::lemma_paging_consts_properties();
489        C::lemma_page_table_config_constant_properties();
490        self.lemma_cur_pte_index();
491        assert(self.set_va_in_node(new_va).continuations =~= self.continuations);
492        assert(self.set_va_in_node(new_va) == (Self { va: new_va, ..self }));
493    }
494}
495
496} // verus!