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
use core::marker::PhantomData;
use core::mem::ManuallyDrop;
use core::ops::{Deref, DerefMut};

mod block;
mod page;
mod table;

#[derive(Debug)]
pub struct Descriptor<L, Ty = Unknown> {
    bits: u64,
    phantom: PhantomData<(L, Ty)>,
}

impl<L, Ty> Descriptor<L, Ty> {
    /// Bit representation of an invalid descriptor.
    pub const INVALID_BITS: u64 = 0;

    pub unsafe fn from_bits_unchecked(bits: u64) -> Self {
        Self {
            bits,
            phantom: PhantomData,
        }
    }

    pub fn into_inner(self) -> u64 {
        let bits = self.bits;

        core::mem::forget(self);

        bits
    }
}

impl<L> Descriptor<L> {
    pub fn from_bits(bits: u64) -> Option<Self> {
        let valid = (bits & 1) == 1;

        if valid {
            Some(unsafe { Self::from_bits_unchecked(bits) })
        } else {
            None
        }
    }
}

impl<L, Ty> Drop for Descriptor<L, Ty> {
    fn drop(&mut self) {
        todo!("drop for descriptor")
    }
}

pub struct DescriptorRefMut<'tt, L, Ty = Unknown> {
    inner: ManuallyDrop<Descriptor<L, Ty>>,
    phantom: PhantomData<&'tt mut ()>,
}

impl<L> DescriptorRefMut<'_, L> {
    pub fn from_bits(bits: u64) -> Option<Self> {
        Descriptor::from_bits(bits).map(|inner| Self {
            inner: ManuallyDrop::new(inner),
            phantom: PhantomData,
        })
    }
}

impl<L, Ty> DescriptorRefMut<'_, L, Ty> {
    pub unsafe fn from_bits_unchecked(bits: u64) -> Self {
        let inner = Descriptor::from_bits_unchecked(bits);

        Self {
            inner: ManuallyDrop::new(inner),
            phantom: PhantomData,
        }
    }
}

impl<L, Ty> Drop for DescriptorRefMut<'_, L, Ty> {
    fn drop(&mut self) {
        // do nothing
    }
}

impl<L, Ty> Deref for DescriptorRefMut<'_, L, Ty> {
    type Target = Descriptor<L, Ty>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<L, Ty> DerefMut for DescriptorRefMut<'_, L, Ty> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

pub struct DescriptorBuilder<L, Ty = Unknown> {
    bits: u64,
    phantom: PhantomData<(L, Ty)>,
}

impl<L> Default for DescriptorBuilder<L> {
    fn default() -> Self {
        Self {
            bits: 0,
            phantom: PhantomData,
        }
    }
}

macro_rules! define_descriptor {
    ($ty:ident) => {
        #[derive(Debug)]
        pub struct $ty;
    };
    ($ty:ident, $descriptor:ident, $builder:ident) => {
        #[derive(Debug)]
        pub struct $ty;

        type $descriptor<L> = Descriptor<L, $ty>;
        type $builder<L> = DescriptorBuilder<L, $ty>;

        impl<L> From<$descriptor<L>> for Descriptor<L> {
            fn from(value: $descriptor<L>) -> Self {
                unsafe { Descriptor::from_bits_unchecked(value.into_inner()) }
            }
        }
    };
}

define_descriptor!(Unknown);
define_descriptor!(Table, TableDescriptor, TableDescriptorBuilder);
define_descriptor!(Block, BlockDescriptor, BlockDescriptorBuilder);
define_descriptor!(Page, PageDescriptor, PageDescriptorBuilder);