Skip to main content

ostd/specs/arch/
model.rs

1//! Architecture contracts for modeled frames and linear address mappings.
2//!
3//! # Verified Properties
4//!
5//! These erased contracts require each architecture instance to prove that its
6//! modeled physical-address bound is page-aligned and that its linear mapping
7//! has room for those addresses without overflow. The bound limits tracked
8//! memory; it does not describe the processor's physical-address width.
9//! Within each architecture's linear mapping window, the address conversions
10//! preserve bounds and are mutual inverses. Physical-to-virtual conversion also
11//! preserves base-page alignment.
12//! `CurrentArch` selects the instance for this target. Paging configurations must
13//! prove the layout requirements declared by `PagingConstsTrait`.
14use vstd::prelude::*;
15use vstd_extra::arithmetic::lemma_mod_0_add;
16
17use crate::mm::{Paddr, PagingConstsTrait, Vaddr};
18
19verus! {
20
21/// The architecture contract for modeled physical frames and their linear mapping.
22///
23/// The associated paging constants are still supplied by the existing
24/// `PagingConstsTrait`; this trait supplies the modeled physical-address bound
25/// and the kernel's linear mapping, together with their compatibility proofs.
26pub trait ArchAddressSpaceModel {
27    type C: PagingConstsTrait;
28
29    /// The exclusive upper bound for modeled physical frame addresses.
30    spec fn max_paddr_spec() -> Paddr;
31
32    /// The base of the kernel's physical-to-virtual linear mapping.
33    spec fn linear_mapping_base_vaddr_spec() -> Vaddr;
34
35    /// The first virtual address reserved for vmalloc mappings.
36    spec fn vmalloc_base_vaddr_spec() -> Vaddr;
37
38    /// Proves that the physical range and linear mapping satisfy the model requirements.
39    proof fn lemma_model_requirements()
40        ensures
41            0 < Self::max_paddr_spec(),
42            Self::C::BASE_PAGE_SIZE <= Self::max_paddr_spec(),
43            Self::max_paddr_spec() % Self::C::BASE_PAGE_SIZE == 0,
44            Self::linear_mapping_base_vaddr_spec() % Self::C::BASE_PAGE_SIZE == 0,
45            Self::max_paddr_spec() + Self::linear_mapping_base_vaddr_spec()
46                < Self::vmalloc_base_vaddr_spec(),
47    ;
48}
49
50/// A physical address that can identify a base-page frame for architecture `A`.
51pub open spec fn valid_frame_paddr_for<A: ArchAddressSpaceModel>(pa: Paddr) -> bool {
52    pa % A::C::BASE_PAGE_SIZE == 0 && pa < A::max_paddr_spec()
53}
54
55/// Convert a physical address through architecture `A`'s linear mapping.
56pub open spec fn paddr_to_vaddr_for<A: ArchAddressSpaceModel>(pa: Paddr) -> Vaddr {
57    (pa + A::linear_mapping_base_vaddr_spec()) as usize
58}
59
60/// Convert a linear-mapped virtual address back to a physical address.
61pub open spec fn vaddr_to_paddr_for<A: ArchAddressSpaceModel>(va: Vaddr) -> Paddr {
62    (va - A::linear_mapping_base_vaddr_spec()) as usize
63}
64
65/// Maps a physical offset into the linear mapping window and back without loss.
66pub proof fn lemma_paddr_to_vaddr_properties_for<A: ArchAddressSpaceModel>(pa: Paddr)
67    requires
68        pa < A::vmalloc_base_vaddr_spec() - A::linear_mapping_base_vaddr_spec(),
69    ensures
70        A::linear_mapping_base_vaddr_spec() <= paddr_to_vaddr_for::<A>(pa)
71            < A::vmalloc_base_vaddr_spec(),
72        vaddr_to_paddr_for::<A>(paddr_to_vaddr_for::<A>(pa)) == pa,
73{
74}
75
76/// Maps a linear-mapped virtual address to a physical offset and back without loss.
77pub proof fn lemma_vaddr_to_paddr_properties_for<A: ArchAddressSpaceModel>(va: Vaddr)
78    requires
79        A::linear_mapping_base_vaddr_spec() <= va < A::vmalloc_base_vaddr_spec(),
80    ensures
81        vaddr_to_paddr_for::<A>(va) < A::vmalloc_base_vaddr_spec()
82            - A::linear_mapping_base_vaddr_spec(),
83        paddr_to_vaddr_for::<A>(vaddr_to_paddr_for::<A>(va)) == va,
84{
85}
86
87/// Preserves base-page alignment when mapping a physical offset into the linear window.
88pub proof fn lemma_paddr_to_vaddr_aligned_for<A: ArchAddressSpaceModel>(pa: Paddr)
89    requires
90        pa < A::vmalloc_base_vaddr_spec() - A::linear_mapping_base_vaddr_spec(),
91        pa % A::C::BASE_PAGE_SIZE == 0,
92    ensures
93        paddr_to_vaddr_for::<A>(pa) % A::C::BASE_PAGE_SIZE == 0,
94{
95    A::C::lemma_paging_consts_requirements();
96    A::lemma_model_requirements();
97    lemma_mod_0_add(
98        pa as int,
99        A::linear_mapping_base_vaddr_spec() as int,
100        A::C::BASE_PAGE_SIZE as int,
101    );
102}
103
104} // verus!