Skip to main content

ostd/specs/mm/page_table/
vaddr_range_proofs.rs

1// SPDX-License-Identifier: MPL-2.0
2//! Bounded-arithmetic proofs for `vaddr_range`'s body.
3//!
4//! Factored into a leaf module because the `nonlinear_arith` tactic
5//! triggers a Verus internal panic when used in `page_table/mod.rs`
6//! alongside `largest_pages` (which has an `impl Iterator` return type).
7//! Same workaround pattern as the older `vaddr_range_bv_lemmas`.
8use vstd::{
9    arithmetic::{
10        div_mod::{lemma_mod_bound, lemma_mod_breakdown, lemma_mod_decreases},
11        power2::{lemma_pow2_adds, lemma_pow2_pos, lemma_pow2_strictly_increases, pow2},
12    },
13    prelude::*,
14};
15use vstd_extra::{arithmetic::nat_align_down, external::ilog2::lemma_usize_ilog2_to32};
16
17use crate::specs::mm::page_table::*;
18
19use crate::mm::{
20    PagingConstsTrait, PagingLevel, Vaddr, nr_subpage_per_huge, page_size,
21    page_table::PageTableConfig,
22};
23
24verus! {
25
26/// Derives the sign-extension mask and its width from the paging constants.
27pub proof fn lemma_leading_bits_properties<C: PageTableConfig>()
28    ensures
29        page_table_coverage_start_spec::<C>() == leading_bits_spec::<C>() * pow2(
30            nr_page_table_coverage_bits_spec::<C>() as nat,
31        ),
32        page_table_coverage_start_spec::<C>() + pow2(nr_page_table_coverage_bits_spec::<C>() as nat)
33            <= pow2(usize::BITS as nat),
34        C::VA_SIGN_EXT && (C::TOP_LEVEL_INDEX_RANGE.start * pow2(
35            pte_index_bit_offset_spec::<C>(C::NR_LEVELS) as nat,
36        ) / pow2((C::ADDRESS_WIDTH - 1) as nat) as int) % 2 == 1
37            ==> page_table_coverage_start_spec::<C>() == pow2(usize::BITS as nat) - pow2(
38            C::ADDRESS_WIDTH as nat,
39        ),
40        leading_bits_spec::<C>() < pow2(
41            (usize::BITS - nr_page_table_coverage_bits_spec::<C>()) as nat,
42        ),
43{
44    reveal(leading_bits_spec);
45    C::lemma_paging_consts_properties();
46    let bits = nr_page_table_coverage_bits_spec::<C>() as nat;
47    let width = C::ADDRESS_WIDTH as nat;
48    lemma_pow2_adds((usize::BITS - bits) as nat, bits);
49    lemma_pow2_adds((width - bits) as nat, bits);
50    lemma_pow2_pos((width - bits) as nat);
51    vstd::arithmetic::power2::lemma_pow2_strictly_increases(
52        (width - bits) as nat,
53        (usize::BITS - bits) as nat,
54    );
55    vstd::layout::unsigned_int_max_values();
56    if bits < usize::BITS {
57        if bits > 0 {
58            vstd::arithmetic::power2::lemma_pow2_strictly_increases(
59                (usize::BITS - bits) as nat,
60                usize::BITS as nat,
61            );
62        }
63    }
64    assert((pow2((usize::BITS - bits) as nat) - pow2((width - bits) as nat)) * pow2(bits) == pow2(
65        usize::BITS as nat,
66    ) - pow2(width)) by (nonlinear_arith)
67        requires
68            pow2((usize::BITS - bits) as nat) * pow2(bits) == pow2(usize::BITS as nat),
69            pow2((width - bits) as nat) * pow2(bits) == pow2(width),
70    ;
71    assert(leading_bits_spec::<C>() * pow2(bits) + pow2(bits) <= pow2(usize::BITS as nat))
72        by (nonlinear_arith)
73        requires
74            leading_bits_spec::<C>() < pow2((usize::BITS - bits) as nat),
75            pow2(bits) > 0,
76            pow2((usize::BITS - bits) as nat) * pow2(bits) == pow2(usize::BITS as nat),
77    ;
78}
79
80/// Managed bounds preserve their mathematical values and match the inclusive range model.
81pub proof fn lemma_page_table_managed_bounds<C: PageTableConfig>()
82    ensures
83        page_table_managed_start_spec::<C>() == page_table_coverage_start_spec::<C>()
84            + C::TOP_LEVEL_INDEX_RANGE.start * page_size::<C>(C::NR_LEVELS),
85        page_table_managed_start_spec::<C>() < page_table_managed_end_spec::<C>() <= usize::MAX + 1,
86        vaddr_range_spec::<C>().end + 1 == page_table_managed_end_spec::<C>(),
87{
88    reveal(leading_bits_spec);
89    C::lemma_paging_consts_properties();
90    C::lemma_page_table_config_constant_properties();
91    lemma_leading_bits_properties::<C>();
92    lemma_page_size_is_pow2_pte_index_bit_offset::<C>(C::NR_LEVELS);
93    let off = pte_index_bit_offset_spec::<C>(C::NR_LEVELS) as nat;
94    let aw = C::ADDRESS_WIDTH as nat;
95    lemma_pow2_adds((aw - off) as nat, off);
96    lemma_pow2_strictly_increases(aw, usize::BITS as nat);
97    vstd::layout::unsigned_int_max_values();
98    let base = page_table_coverage_start_spec::<C>() as int;
99    if !(C::VA_SIGN_EXT && (C::TOP_LEVEL_INDEX_RANGE.start * pow2(
100        pte_index_bit_offset_spec::<C>(C::NR_LEVELS) as nat,
101    ) / pow2((C::ADDRESS_WIDTH - 1) as nat) as int) % 2 == 1) {
102        assert(base == 0) by (nonlinear_arith)
103            requires
104                leading_bits_spec::<C>() == 0,
105                base == leading_bits_spec::<C>() * pow2(
106                    nr_page_table_coverage_bits_spec::<C>() as nat,
107                ),
108        ;
109    }
110    let size = page_size::<C>(C::NR_LEVELS) as int;
111    let start = base + C::TOP_LEVEL_INDEX_RANGE.start * size;
112    let end = page_table_managed_end_spec::<C>();
113    assert(0 <= start < end <= usize::MAX + 1) by (nonlinear_arith)
114        requires
115            0 <= base,
116            base == 0 || base == pow2(usize::BITS as nat) - pow2(aw),
117            pow2(aw) < pow2(usize::BITS as nat),
118            pow2(usize::BITS as nat) == usize::MAX + 1,
119            0 < size,
120            size == pow2(off),
121            pow2((aw - off) as nat) * pow2(off) == pow2(aw),
122            0 <= C::TOP_LEVEL_INDEX_RANGE.start < C::TOP_LEVEL_INDEX_RANGE.end <= pow2(
123                (aw - off) as nat,
124            ),
125            start == base + C::TOP_LEVEL_INDEX_RANGE.start * size,
126            end == base + C::TOP_LEVEL_INDEX_RANGE.end * size,
127    ;
128}
129
130/// Managed address bounds are equivalent to root-index bounds when the upper address bits match.
131/// The current root slot ends within the managed range whenever the address is inside it.
132pub proof fn lemma_page_table_managed_index_bounds<C: PageTableConfig>(va: Vaddr)
133    requires
134        vaddr_leading_part_spec::<C>(va) == page_table_coverage_start_spec::<C>(),
135    ensures
136        (page_table_managed_start_spec::<C>() <= va) == (C::TOP_LEVEL_INDEX_RANGE.start
137            <= pte_index_spec::<C>(va, C::NR_LEVELS)),
138        (va < page_table_managed_end_spec::<C>()) == (pte_index_spec::<C>(va, C::NR_LEVELS)
139            < C::TOP_LEVEL_INDEX_RANGE.end),
140        va < page_table_managed_end_spec::<C>() ==> nat_align_down(
141            va as nat,
142            page_size::<C>(C::NR_LEVELS) as nat,
143        ) + page_size::<C>(C::NR_LEVELS) <= page_table_managed_end_spec::<C>(),
144{
145    C::lemma_paging_consts_properties();
146    lemma_page_table_managed_bounds::<C>();
147    let top = C::NR_LEVELS;
148    lemma_page_size_next::<C>(top);
149    lemma_vaddr_leading_part_is_align_down::<C>(va);
150    lemma_pte_index_spec_is_div_mod::<C>(va, top);
151    let size = page_size::<C>(top) as int;
152    let body = page_size::<C>((top + 1) as PagingLevel) as int;
153    let fanout = nr_subpage_per_huge::<C>() as int;
154    lemma_mod_decreases(va as nat, body as nat);
155    lemma_mod_breakdown(va as int, size, fanout);
156    let base = page_table_coverage_start_spec::<C>() as int;
157    let index = pte_index_spec::<C>(va, top) as int;
158    assert({
159        &&& base + index * size == va - va as int % size
160        &&& base + index * size <= va < base + (index + 1) * size
161    }) by (nonlinear_arith)
162        requires
163            base == va - va as int % body,
164            va as int % body == size * index + va as int % size,
165            0 <= va as int % size < size,
166    ;
167    assert({
168        &&& (page_table_managed_start_spec::<C>() <= va) == (C::TOP_LEVEL_INDEX_RANGE.start
169            <= index)
170        &&& (va < page_table_managed_end_spec::<C>()) == (index < C::TOP_LEVEL_INDEX_RANGE.end)
171        &&& (va < page_table_managed_end_spec::<C>() ==> base + index * size + size
172            <= page_table_managed_end_spec::<C>())
173    }) by (nonlinear_arith)
174        requires
175            size > 0,
176            base + index * size <= va < base + (index + 1) * size,
177            page_table_managed_start_spec::<C>() == base + C::TOP_LEVEL_INDEX_RANGE.start * size,
178            page_table_managed_end_spec::<C>() == base + C::TOP_LEVEL_INDEX_RANGE.end * size,
179    ;
180}
181
182/// Facts needed to turn `idx.start << offset` into
183/// `idx.start * 2^offset` for `pt_va_range_start`.
184pub proof fn lemma_pt_va_range_start_shift_facts<C: PageTableConfig>(
185    idx_start: usize,
186    offset: usize,
187)
188    requires
189        idx_start == C::TOP_LEVEL_INDEX_RANGE.start,
190        offset == pte_index_bit_offset_spec::<C>(C::NR_LEVELS),
191    ensures
192        offset < usize::BITS,
193        idx_start * pow2(offset as nat) <= usize::MAX,
194        offset == pte_index_bit_offset_spec::<C>(C::NR_LEVELS),
195{
196    C::lemma_paging_consts_properties();
197    C::lemma_page_table_config_constant_properties();
198    vstd::layout::unsigned_int_max_values();
199
200    let off = pte_index_bit_offset_spec::<C>(C::NR_LEVELS) as nat;
201    let aw = C::ADDRESS_WIDTH as nat;
202    let top_w = (aw - off) as nat;
203    lemma_pow2_adds(top_w, off);
204    lemma_pow2_pos(off);
205
206    let i_start = C::TOP_LEVEL_INDEX_RANGE.start as int;
207    let p_off = pow2(off) as int;
208    let p_top = pow2(top_w) as int;
209
210    assert(i_start * p_off < p_top * p_off) by (nonlinear_arith)
211        requires
212            i_start < p_top,
213            p_off > 0,
214    ;
215
216    if aw < 64 {
217        vstd::arithmetic::power2::lemma_pow2_strictly_increases(aw, 64);
218    }
219}
220
221/// Facts needed to turn `idx.end << offset` into
222/// `idx.end * 2^offset` for `pt_va_range_end`.
223pub proof fn lemma_pt_va_range_end_shift_facts<C: PageTableConfig>(idx_end: usize, offset: usize)
224    requires
225        idx_end == C::TOP_LEVEL_INDEX_RANGE.end,
226        offset == pte_index_bit_offset_spec::<C>(C::NR_LEVELS),
227    ensures
228        offset < usize::BITS,
229        idx_end * pow2(offset as nat) <= usize::MAX,
230        0 < idx_end * pow2(offset as nat),
231        offset == pte_index_bit_offset_spec::<C>(C::NR_LEVELS),
232{
233    C::lemma_paging_consts_properties();
234    C::lemma_page_table_config_constant_properties();
235    lemma_pow2_pos(offset as nat);
236
237    let i_end = C::TOP_LEVEL_INDEX_RANGE.end as int;
238    let p_off = pow2(offset as nat) as int;
239    assert(i_end * p_off > 0) by (nonlinear_arith)
240        requires
241            i_end > 0,
242            p_off > 0,
243    ;
244}
245
246/// Two-in-one: `start = idx.start * 2^off < 2^ADDRESS_WIDTH` and
247/// `end = (idx.end * 2^off - 1) % 2^64 < 2^ADDRESS_WIDTH`.
248///
249/// The first follows from `idx.start < 2^(ADDRESS_WIDTH - off)`. The
250/// second from `idx.end <= 2^(ADDRESS_WIDTH - off)` plus the no-wrap
251/// condition: `idx.end * 2^off <= 2^ADDRESS_WIDTH ≤ 2^64`, so the `% 2^64`
252/// is a no-op when the value is positive.
253pub proof fn lemma_idx_times_pow2_bound<C: PageTableConfig>(start: Vaddr, end: Vaddr)
254    requires
255        start == C::TOP_LEVEL_INDEX_RANGE.start * (pow2(
256            pte_index_bit_offset_spec::<C>(C::NR_LEVELS) as nat,
257        ) as int),
258        end == (C::TOP_LEVEL_INDEX_RANGE.end * (pow2(
259            pte_index_bit_offset_spec::<C>(C::NR_LEVELS) as nat,
260        ) as int) - 1) % 0x1_0000_0000_0000_0000int,
261    ensures
262        start < pow2(C::ADDRESS_WIDTH as nat),
263        end < pow2(C::ADDRESS_WIDTH as nat),
264        // For the end-of-range arithmetic ensures of `vaddr_range`:
265        end == C::TOP_LEVEL_INDEX_RANGE.end * pow2(
266            pte_index_bit_offset_spec::<C>(C::NR_LEVELS) as nat,
267        ) - 1,
268{
269    C::lemma_paging_consts_properties();
270    C::lemma_page_table_config_constant_properties();
271    let off = pte_index_bit_offset_spec::<C>(C::NR_LEVELS) as nat;
272    let aw = C::ADDRESS_WIDTH as nat;
273    let top_w = (aw - off) as nat;
274    lemma_pow2_adds(top_w, off);
275    lemma_pow2_pos(off);
276    // Constants for clarity.
277    let i_start = C::TOP_LEVEL_INDEX_RANGE.start as int;
278    let i_end = C::TOP_LEVEL_INDEX_RANGE.end as int;
279    let p_off = pow2(off) as int;
280    let p_top = pow2(top_w) as int;
281    let p_aw = pow2(aw) as int;
282    // start < p_top * p_off = p_aw.
283    assert(start < (p_top * p_off)) by (nonlinear_arith)
284        requires
285            start == i_start * p_off,
286            i_start < p_top,
287            p_off > 0,
288    ;
289    // end pre-wrap value.
290    let e_pre = i_end * p_off;
291    assert(e_pre <= p_top * p_off) by (nonlinear_arith)
292        requires
293            e_pre == i_end * p_off,
294            i_end <= p_top,
295            p_off > 0,
296    ;
297    // i_end > 0 — from `lemma_page_table_config_constant_properties`'s
298    // `idx.start < idx.end` plus `idx.start >= 0` (usize).
299    assert(e_pre > 0) by (nonlinear_arith)
300        requires
301            e_pre == i_end * p_off,
302            i_end > 0,
303            p_off > 0,
304    ;
305}
306
307/// Derives the coverage width from the required base-page and PTE layout.
308pub(super) proof fn lemma_paging_layout_bits<C: PageTableConfig>()
309    ensures
310        C::BASE_PAGE_SIZE.ilog2() == 12,
311        nr_pte_index_bits_spec::<C>() == 9,
312        nr_page_table_coverage_bits_spec::<C>() == 12 + 9 * C::NR_LEVELS,
313{
314    C::lemma_paging_consts_properties();
315    lemma_usize_ilog2_to32();
316    assert(C::BASE_PAGE_SIZE.ilog2() == 12);
317    assert(nr_pte_index_bits_spec::<C>() == 9);
318    assert(nr_page_table_coverage_bits_spec::<C>() == 12 + 9 * C::NR_LEVELS) by (nonlinear_arith)
319        requires
320            C::BASE_PAGE_SIZE.ilog2() == 12,
321            nr_pte_index_bits_spec::<C>() == 9,
322            1 <= C::NR_LEVELS <= crate::mm::MAX_NR_LEVELS,
323    ;
324}
325
326} // verus!