rust-kernel
Rustbelt: a hobby x86_64 kernel written in Rust
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 ;
use null_mut;
use ;
/// 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)
static ALLOCATOR: =
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.
/// 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.
/// Round `addr` up to a multiple of `align` (which must be a power of two).
/// Example: align_up(13, 8) == 16.
/// The "allocator" from the start of the Heap Allocation chapter: always
/// fails. Kept to show the smallest possible `GlobalAlloc`.
;
unsafe
|