RustForge

rust-kernel

Rustbelt: a hobby x86_64 kernel written in Rust

rust-kernel / src / allocator.rs
3781 bytes ยท raw
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
//! The kernel heap.
//!
//! 1. Pick a virtual address range for the heap (HEAP_START..+HEAP_SIZE).
//! 2. Map every page in it to a fresh physical frame (`init_heap`).
//! 3. Hand the range to an allocator and register it with
//!    `#[global_allocator]`, so `Box`, `Vec`, `Rc`, `String`... just work.
//!
//! Three allocator designs, from the "Allocator Designs" chapter:
//!   * `bump::BumpAllocator`      -- simplest, can only free everything at once
//!   * `linked_list_allocator`    -- crate: free-list, first fit, reuses memory
//!   * `fixed_size_block::FixedSizeBlockAllocator` (ACTIVE) -- fast lists of
//!     fixed-size blocks, falls back to the linked-list allocator for big ones
//!
//! To try another one, swap the `ALLOCATOR` line below (examples in comments).

use alloc::alloc::{GlobalAlloc, Layout};
use core::ptr::null_mut;
use x86_64::{
    VirtAddr,
    structures::paging::{
        FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB, mapper::MapToError,
    },
};

pub mod bump;
pub mod fixed_size_block;

/// Where the heap lives in *virtual* memory. Any unused, easy-to-recognise
/// address works; this one stands out in a debugger.
pub const HEAP_START: usize = 0x_4444_4444_0000;
pub const HEAP_SIZE: usize = 256 * 1024; // 256 KiB (thread stacks live here too)

#[global_allocator]
static ALLOCATOR: Locked<fixed_size_block::FixedSizeBlockAllocator> =
    Locked::new(fixed_size_block::FixedSizeBlockAllocator::new());
// Alternatives:
// static ALLOCATOR: Locked<bump::BumpAllocator> = Locked::new(bump::BumpAllocator::new());
// static ALLOCATOR: linked_list_allocator::LockedHeap = linked_list_allocator::LockedHeap::empty();
//   (then in init_heap: `ALLOCATOR.lock().init(HEAP_START, HEAP_SIZE)` works for all three)

/// Map the heap's pages to physical frames, then initialise the allocator.
pub fn init_heap(
    mapper: &mut impl Mapper<Size4KiB>,
    frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) -> Result<(), MapToError<Size4KiB>> {
    let page_range = {
        let heap_start = VirtAddr::new(HEAP_START as u64);
        let heap_end = heap_start + HEAP_SIZE as u64 - 1u64;
        Page::range_inclusive(
            Page::containing_address(heap_start),
            Page::containing_address(heap_end),
        )
    };

    for page in page_range {
        let frame = frame_allocator
            .allocate_frame()
            .ok_or(MapToError::FrameAllocationFailed)?;
        let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
        unsafe { mapper.map_to(page, frame, flags, frame_allocator)?.flush() };
    }

    unsafe {
        ALLOCATOR.lock().init(HEAP_START, HEAP_SIZE);
    }
    Ok(())
}

/// A wrapper around `spin::Mutex`. `GlobalAlloc` methods take `&self`, so the
/// allocator needs interior mutability; and Rust only lets us implement a
/// foreign trait (`GlobalAlloc`) for a type defined in our own crate.
pub struct Locked<A> {
    inner: spin::Mutex<A>,
}

impl<A> Locked<A> {
    pub const fn new(inner: A) -> Self {
        Locked { inner: spin::Mutex::new(inner) }
    }
    pub fn lock(&self) -> spin::MutexGuard<'_, A> {
        self.inner.lock()
    }
}

/// Round `addr` up to a multiple of `align` (which must be a power of two).
/// Example: align_up(13, 8) == 16.
fn align_up(addr: usize, align: usize) -> usize {
    (addr + align - 1) & !(align - 1)
}

/// The "allocator" from the start of the Heap Allocation chapter: always
/// fails. Kept to show the smallest possible `GlobalAlloc`.
pub struct Dummy;

unsafe impl GlobalAlloc for Dummy {
    unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
        null_mut() // null = "out of memory"
    }
    unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
        panic!("dealloc should never be called")
    }
}