# page.rs - source
Source of the Rust file `/home/runner/.cargo/git/checkouts/riscv-ab2abd16c438337b/11d43cf/src/addr/page.rs`.
Keywords: rust, rustlang, rust-lang

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174

    pub use super::*;
    pub use bit_field::BitField;

    pub trait PageWithL4 {
        fn p4_index(&self) -> usize;
        fn p3_index(&self) -> usize;
        fn p2_index(&self) -> usize;
        fn p1_index(&self) -> usize;
        fn from_page_table_indices(
            p4_index: usize,
            p3_index: usize,
            p2_index: usize,
            p1_index: usize,
        ) -> Self;
    }

    pub trait PageWithL3 {
        fn p3_index(&self) -> usize;
        fn p2_index(&self) -> usize;
        fn p1_index(&self) -> usize;
        fn from_page_table_indices(p3_index: usize, p2_index: usize, p1_index: usize) -> Self;
    }

    pub trait PageWithL2 {
        fn p2_index(&self) -> usize;
        fn p1_index(&self) -> usize;
        fn from_page_table_indices(p2_index: usize, p1_index: usize) -> Self;
    }

    #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
    pub struct PageWith<T: VirtualAddress>(T);

    impl<T: AddressL4 + VirtualAddress> PageWithL4 for PageWith<T> {
        fn p4_index(&self) -> usize {
            self.0.p4_index()
        }
        fn p3_index(&self) -> usize {
            self.0.p3_index()
        }
        fn p2_index(&self) -> usize {
            self.0.p2_index()
        }
        fn p1_index(&self) -> usize {
            self.0.p1_index()
        }
        fn from_page_table_indices(
            p4_index: usize,
            p3_index: usize,
            p2_index: usize,
            p1_index: usize,
        ) -> Self {
            PageWith::of_addr(T::from_page_table_indices(
                p4_index, p3_index, p2_index, p1_index, 0,
            ))
        }
    }
    impl<T: AddressL3 + VirtualAddress> PageWithL3 for PageWith<T> {
        fn p3_index(&self) -> usize {
            self.0.p3_index()
        }
        fn p2_index(&self) -> usize {
            self.0.p2_index()
        }
        fn p1_index(&self) -> usize {
            self.0.p1_index()
        }
        fn from_page_table_indices(p3_index: usize, p2_index: usize, p1_index: usize) -> Self {
            PageWith::of_addr(T::from_page_table_indices(p3_index, p2_index, p1_index, 0))
        }
    }
    impl<T: AddressL2 + VirtualAddress> PageWithL2 for PageWith<T> {
        fn p2_index(&self) -> usize {
            self.0.p2_index()
        }
        fn p1_index(&self) -> usize {
            self.0.p1_index()
        }
        fn from_page_table_indices(p2_index: usize, p1_index: usize) -> Self {
            PageWith::of_addr(T::from_page_table_indices(p2_index, p1_index, 0))
        }
    }
    impl<T: VirtualAddress> PageWith<T> {
        pub fn of_addr(addr: T) -> Self {
            PageWith(addr.to_4k_aligned())
        }

        pub fn of_vpn(vpn: usize) -> Self {
            PageWith(T::new(vpn << 12))
        }

        pub fn start_address(&self) -> T {
            self.0.clone()
        }

        pub fn number(&self) -> usize {
            self.0.page_number()
        }
    }

    #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
    pub struct FrameWith<T: PhysicalAddress>(T);

    impl<T: AddressL4 + PhysicalAddress> PageWithL4 for FrameWith<T> {
        fn p4_index(&self) -> usize {
            self.0.p4_index()
        }
        fn p3_index(&self) -> usize {
            self.0.p3_index()
        }
        fn p2_index(&self) -> usize {
            self.0.p2_index()
        }
        fn p1_index(&self) -> usize {
            self.0.p1_index()
        }
        fn from_page_table_indices(
            p4_index: usize,
            p3_index: usize,
            p2_index: usize,
            p1_index: usize,
        ) -> Self {
            FrameWith::of_addr(T::from_page_table_indices(
                p4_index, p3_index, p2_index, p1_index, 0,
            ))
        }
    }
    impl<T: AddressL3 + PhysicalAddress> PageWithL3 for FrameWith<T> {
        fn p3_index(&self) -> usize {
            self.0.p3_index()
        }
        fn p2_index(&self) -> usize {
            self.0.p2_index()
        }
        fn p1_index(&self) -> usize {
            self.0.p1_index()
        }
        fn from_page_table_indices(p3_index: usize, p2_index: usize, p1_index: usize) -> Self {
            FrameWith::of_addr(T::from_page_table_indices(p3_index, p2_index, p1_index, 0))
        }
    }
    impl<T: AddressL2 + PhysicalAddress> PageWithL2 for FrameWith<T> {
        fn p2_index(&self) -> usize {
            self.0.p2_index()
        }
        fn p1_index(&self) -> usize {
            self.0.p1_index()
        }
        fn from_page_table_indices(p2_index: usize, p1_index: usize) -> Self {
            FrameWith::of_addr(T::from_page_table_indices(p2_index, p1_index, 0))
        }
    }

    impl<T: PhysicalAddress> FrameWith<T> {
        pub fn of_addr(addr: T) -> Self {
            FrameWith(addr.to_4k_aligned())
        }

        #[inline(always)]
        pub fn of_ppn(ppn: usize) -> Self {
            FrameWith(T::new_u64((ppn as u64) << 12))
        }

        pub fn start_address(&self) -> T {
            self.0.clone()
        }

        pub fn number(&self) -> usize {
            self.0.page_number()
        }

        pub unsafe fn as_kernel_mut<'a, 'b, U>(&'a self, linear_offset: u64) -> &'b mut U {
            &mut *(((self.0).as_u64() + linear_offset) as *mut U)
        }
    }