sparreal_kernel/mem/
mod.rs

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
mod addr;

pub mod dma;
#[cfg(feature = "mmu")]
pub mod mmu;

use core::{
    alloc::{GlobalAlloc, Layout},
    ptr::{null_mut, NonNull},
};

pub use addr::*;
use buddy_system_allocator::Heap;
use log::*;

use crate::{
    kernel::KernelConfig,
    sync::{RwLock, RwLockWriteGuard},
};

#[global_allocator]
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::new();

pub const BYTES_1K: usize = 1024;
pub const BYTES_1M: usize = 1024 * BYTES_1K;
pub const BYTES_1G: usize = 1024 * BYTES_1M;

pub unsafe fn init(kconfig: &KernelConfig) {
    #[cfg(feature = "mmu")]
    mmu::set_va_offset(kconfig.boot_info.va_offset);

    let stack_size = kconfig.boot_info.hart_stack_size * 1;
    let start =
        (kconfig.boot_info.main_memory.start + kconfig.boot_info.main_memory_heap_offset).to_virt();
    let size =
        kconfig.boot_info.main_memory.size - kconfig.boot_info.main_memory_heap_offset - stack_size;
    let stack_top = kconfig.stack_top.to_virt();

    debug!("Heap: [{}, {})", start, start + size);
    debug!("Stack: [{}, {})", stack_top - stack_size, stack_top);

    let mut heap = HEAP_ALLOCATOR.write();
    heap.init(start.as_usize(), size);

    debug!("Heap initialized.");

    #[cfg(feature = "mmu")]
    {
        let mut heap_mut = PageAllocatorRef::new(heap);
        if let Err(e) = mmu::init_table(kconfig, &mut heap_mut) {
            error!("Failed to initialize page table: {:?}", e);
        }
    }
}

struct LockedHeap(RwLock<Heap<32>>);

unsafe impl Sync for LockedHeap {}
unsafe impl Send for LockedHeap {}

impl LockedHeap {
    const fn new() -> Self {
        Self(RwLock::new(Heap::new()))
    }

    fn write(&self) -> RwLockWriteGuard<'_, Heap<32>> {
        self.0.write()
    }
}

unsafe impl GlobalAlloc for LockedHeap {
    unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
        match self.write().alloc(layout) {
            Ok(ptr) => ptr.as_ptr(),
            Err(_) => null_mut(),
        }
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: core::alloc::Layout) {
        self.write().dealloc(NonNull::new_unchecked(ptr), layout);
    }
}

pub(crate) trait PhysToVirt<T> {
    fn to_virt(self) -> Virt<T>;
}

impl<T> PhysToVirt<T> for Phys<T> {
    fn to_virt(self) -> Virt<T> {
        let a: usize = self.into();

        #[cfg(feature = "mmu")]
        {
            use mmu::va_offset;
            (a + va_offset()).into()
        }

        #[cfg(not(feature = "mmu"))]
        {
            a.into()
        }
    }
}

pub(crate) trait VirtToPhys<T> {
    fn to_phys(self) -> Phys<T>;
}

impl<T> VirtToPhys<T> for Virt<T> {
    fn to_phys(self) -> Phys<T> {
        #[cfg(feature = "mmu")]
        {
            use mmu::va_offset;
            self.convert_to_phys(va_offset())
        }

        #[cfg(not(feature = "mmu"))]
        {
            let a: usize = self.into();
            a.into()
        }
    }
}

#[allow(dead_code)]
pub struct PageAllocatorRef<'a> {
    inner: RwLockWriteGuard<'a, Heap<32>>,
}
impl<'a> PageAllocatorRef<'a> {
    pub fn new(inner: RwLockWriteGuard<'a, Heap<32>>) -> Self {
        Self { inner }
    }
}

#[cfg(feature = "mmu")]
impl page_table_generic::Access for PageAllocatorRef<'_> {
    fn va_offset(&self) -> usize {
        mmu::va_offset()
    }

    unsafe fn alloc(&mut self, layout: core::alloc::Layout) -> Option<NonNull<u8>> {
        self.inner.alloc(layout).ok()
    }

    unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: core::alloc::Layout) {
        self.inner.dealloc(ptr, layout);
    }
}

#[allow(unused)]
pub struct PageAllocator(Heap<32>);

impl PageAllocator {
    pub unsafe fn new(start: NonNull<u8>, size: usize) -> Self {
        let mut heap = Heap::new();
        heap.init(start.as_ptr() as usize, size);
        Self(heap)
    }
}

#[cfg(feature = "mmu")]
impl page_table_generic::Access for PageAllocator {
    fn va_offset(&self) -> usize {
        mmu::va_offset()
    }

    unsafe fn alloc(&mut self, layout: Layout) -> Option<NonNull<u8>> {
        self.0.alloc(layout).ok()
    }

    unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: core::alloc::Layout) {
        self.0.dealloc(ptr, layout);
    }
}