Skip to main content

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

1/// Virtual-address manipulation specs and lemmas for `CursorOwner`.
2///
3/// This module contains:
4/// - Spec functions for zeroing VA indices below the cursor's level
5///   (`zero_below_level`).
6/// - Lemmas about how zeroing preserves fields other than VA.
7/// - Spec functions for the cursor's current VA and VA range
8///   (`cur_va`, `cur_va_range`).
9/// - Lemmas relating the concrete VA to the page table view range.
10/// - Specifications and proofs for repositioning the cursor inside its current node.
11use vstd::prelude::*;
12use vstd_extra::{arithmetic::nat_align_down, ghost_tree::*, ownership::*};
13
14use crate::specs::{
15    arch::*,
16    mm::page_table::{
17        cursor::{
18            owners::{CursorContinuation, CursorOwner},
19            page_size_lemmas::*,
20        },
21        owners::*,
22        *,
23    },
24};
25
26use crate::mm::{Paddr, PagingConstsTrait, PagingLevel, Vaddr, page_size, page_table::*};
27
28use core::ops::Range;
29
30verus! {
31
32broadcast use group_ghost_tree_lemmas;
33
34impl<'rcu, C: PageTableConfig> CursorOwner<'rcu, C> {
35    pub open spec fn zero_below_level(self) -> Self
36        recommends
37            1 <= self.level <= C::NR_LEVELS,
38    {
39        Self {
40            va: nat_align_down(self.va as nat, page_size::<C>(self.level) as nat) as Vaddr,
41            ..self
42        }
43    }
44
45    pub open spec fn cur_va(self) -> Vaddr {
46        self.va
47    }
48
49    pub open spec fn prefix_vaddr(self) -> Vaddr {
50        self.prefix
51    }
52
53    pub open spec fn cur_va_range(self) -> Range<Vaddr> {
54        let size = page_size::<C>(self.level);
55        let start = nat_align_down(self.cur_va() as nat, size as nat) as Vaddr;
56        Range { start, end: (start + size) as Vaddr }
57    }
58
59    pub open spec fn set_va_in_node(self, new_va: Vaddr) -> Self {
60        let old_cont = self.continuations[self.level - 1];
61        Self {
62            va: new_va,
63            continuations: self.continuations.insert(
64                self.level - 1,
65                CursorContinuation { idx: pte_index_spec::<C>(new_va, self.level), ..old_cont },
66            ),
67            // Repositioning to a concrete in-range VA clears the
68            // transient `popped_too_high` state.
69            popped_too_high: false,
70            ..self
71        }
72    }
73
74    /// Incrementing the selected nonterminal entry advances by one slot without overflow.
75    pub proof fn lemma_inc_index_va(self)
76        requires
77            self.inv(),
78            self.in_barrier_va(),
79            self.index() + 1 < NR_ENTRIES,
80        ensures
81            self.inc_index().va == self.va + page_size::<C>(self.level),
82    {
83        C::lemma_paging_consts_properties();
84        C::lemma_page_table_config_constant_properties();
85        self.lemma_cur_pte_index();
86        lemma_inc_slot_indices::<C>(self.va, self.level);
87        reveal(vaddr_replace_pte_index_spec);
88        vstd::arithmetic::mul::lemma_mul_basics(page_size::<C>(self.level) as int);
89    }
90
91    /// Incrementing a nonterminal index and aligning down reaches the next slot boundary.
92    pub proof fn inc_and_zero_increases_va(self)
93        requires
94            self.inv(),
95            self.in_barrier_va(),
96            self.index() + 1 < NR_ENTRIES,
97        ensures
98            self.inc_index().zero_below_level().va > self.va,
99            self.inc_index().zero_below_level().va == self@.align_up_spec(
100                page_size::<C>(self.level),
101            ),
102    {
103        C::lemma_paging_consts_properties();
104        C::lemma_page_table_config_constant_properties();
105        self.lemma_inc_index_va();
106        lemma_page_size_ge_base::<C>(self.level);
107        let ps = page_size::<C>(self.level) as nat;
108        let va = self.va as nat;
109        vstd_extra::arithmetic::lemma_nat_align_down_sound(va, ps);
110        vstd::arithmetic::div_mod::lemma_mod_add_multiples_vanish(va as int, ps as int);
111        vstd::arithmetic::div_mod::lemma_fundamental_div_mod(va as int, ps as int);
112    }
113
114    /// The current subtree starts at the cursor's aligned slot boundary and contains its VA.
115    pub proof fn cur_va_in_subtree_range(self)
116        requires
117            self.inv(),
118            self.in_barrier_va(),
119        ensures
120            vaddr_of::<C>(self.cur_subtree().value().path) <= self.cur_va() < vaddr_of::<C>(
121                self.cur_subtree().value().path,
122            ) + page_size::<C>(self.level),
123            vaddr_of::<C>(self.cur_subtree().value().path) == nat_align_down(
124                self.cur_va() as nat,
125                page_size::<C>(self.level) as nat,
126            ),
127    {
128        C::lemma_paging_consts_properties();
129        C::lemma_page_table_config_constant_properties();
130        self.cur_va_in_cont_child_range(self.level - 1);
131    }
132
133    /// Addresses in the same guard node share indices strictly above its level.
134    pub proof fn lemma_locked_range_vaddr_prefix_match(self, new_va: Vaddr)
135        requires
136            self.inv(),
137            new_va % C::BASE_PAGE_SIZE == 0,
138            vaddr_leading_part_spec::<C>(new_va) == vaddr_leading_part_spec::<C>(
139                self.prefix_vaddr(),
140            ),
141            self.locked_range().start <= new_va < self.locked_range().end,
142        ensures
143            forall|i: int|
144                self.guard_level <= i < C::NR_LEVELS ==> (#[trigger] pte_index_spec::<C>(
145                    new_va,
146                    (i + 1) as PagingLevel,
147                )) == pte_index_spec::<C>(self.prefix, (i + 1) as PagingLevel),
148    {
149        C::lemma_paging_consts_properties();
150        C::lemma_page_table_config_constant_properties();
151        self.lemma_locked_range_span();
152        if self.guard_level < C::NR_LEVELS {
153            lemma_same_node_pte_indices_match::<C>(
154                new_va,
155                self.prefix,
156                self.prefix,
157                self.guard_level,
158            );
159        }
160    }
161
162    /// The entry selected by the cursor's concrete VA is the entry owned by its current
163    /// continuation. This is the architecture-parameterized replacement for exposing the
164    /// decomposed address stored in `CursorOwner`.
165    pub proof fn lemma_cur_pte_index(self)
166        requires
167            self.inv(),
168            self.in_barrier_va(),
169        ensures
170            1 <= self.level <= C::NR_LEVELS,
171            pte_index_spec::<C>(self.cur_va(), self.level) == self.continuations[self.level
172                - 1].idx,
173            pte_index_spec::<C>(self.cur_va(), self.level) < NR_ENTRIES,
174    {
175        C::lemma_paging_consts_properties();
176        C::lemma_page_table_config_constant_properties();
177        lemma_pte_index_bound::<C>(self.cur_va(), self.level);
178    }
179
180    /// Architecture-parameterized entry point for repositioning a cursor inside its current
181    /// page-table node.
182    pub proof fn tracked_set_vaddr_in_node(tracked &mut self, new_va: Vaddr)
183        requires
184            old(self).inv(),
185            new_va % C::BASE_PAGE_SIZE == 0,
186            vaddr_leading_part_spec::<C>(new_va) == vaddr_leading_part_spec::<C>(old(self).prefix),
187            forall|i: int|
188                old(self).level <= i < C::NR_LEVELS ==> (#[trigger] pte_index_spec::<C>(
189                    new_va,
190                    (i + 1) as PagingLevel,
191                )) == old(self).continuations[i].idx,
192            old(self).locked_range().start <= new_va < old(self).locked_range().end,
193            old(self).set_va_in_node(new_va).in_barrier_va(),
194            old(self).level <= old(self).guard_level,
195        ensures
196            *final(self) == old(self).set_va_in_node(new_va),
197            final(self).inv(),
198    {
199        let ghost old_self = *self;
200        C::lemma_paging_consts_properties();
201        C::lemma_page_table_config_constant_properties();
202        lemma_pte_index_bound::<C>(new_va, old_self.level);
203        let tracked mut cont = self.continuations.tracked_remove(self.level - 1);
204        cont.idx = pte_index_spec::<C>(new_va, old_self.level);
205        self.continuations.tracked_insert(self.level - 1, cont);
206        self.va = new_va;
207        self.popped_too_high = false;
208        assert(self.continuations == old_self.continuations.insert(old_self.level - 1, cont));
209        old_self.lemma_barrier_va_bounds(new_va);
210        old_self.lemma_locked_range_vaddr_prefix_match(new_va);
211        assert(self.continuations[self.level - 1].inv());
212        assert forall|lv: PagingLevel| self.level <= lv <= C::NR_LEVELS implies (
213        #[trigger] pte_index_spec::<C>(new_va, lv)) == self.continuations[lv - 1].idx by {
214            if self.level < lv {
215                let i = lv - 1;
216                assert(old(self).level <= i < C::NR_LEVELS);
217                assert(pte_index_spec::<C>(new_va, (i + 1) as PagingLevel) == old(
218                    self,
219                ).continuations[i].idx);
220            }
221        };
222        assert(self.inv());
223    }
224}
225
226impl<'rcu, C: PageTableConfig> Cursor<'rcu, C> {
227    /// Connects the executable cursor address to the architecture-parameterized index owned by
228    /// its ghost continuation, without exposing the owner's decomposed address representation.
229    pub proof fn lemma_pte_index_matches_owner(self, owner: CursorOwner<'rcu, C>)
230        requires
231            self.wf(owner),
232            owner.inv(),
233            owner.in_barrier_va(),
234        ensures
235            1 <= self.level <= C::NR_LEVELS,
236            pte_index_spec::<C>(self.va, self.level) == owner.continuations[owner.level - 1].idx,
237            pte_index_spec::<C>(self.va, self.level) < NR_ENTRIES,
238    {
239        owner.lemma_cur_pte_index();
240    }
241}
242
243} // verus!