mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Compare commits
282 Commits
post-08
...
4dd9c6bce7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4dd9c6bce7 | ||
|
|
28c9558a7a | ||
|
|
f63e0dacca | ||
|
|
bb3beb22da | ||
|
|
3743997479 | ||
|
|
2870ac4e13 | ||
|
|
486975e4dc | ||
|
|
c250dd0a91 | ||
|
|
268b5b6856 | ||
|
|
2fdc7c3b5c | ||
|
|
d54e691261 | ||
|
|
be58db8907 | ||
|
|
1550e83094 | ||
|
|
b654651a85 | ||
|
|
cd244cc604 | ||
|
|
bf7cf4c65d | ||
|
|
6b51fa467e | ||
|
|
4343f9be6a | ||
|
|
92f197c2e5 | ||
|
|
af909cab1e | ||
|
|
b6437c5cc1 | ||
|
|
5fc6566d45 | ||
|
|
ee550a64e0 | ||
|
|
f0149947cf | ||
|
|
a249900aed | ||
|
|
09a3ac4f80 | ||
|
|
e56cd71689 | ||
|
|
506a1d1961 | ||
|
|
17e1ae2508 | ||
|
|
e41ff18ea0 | ||
|
|
f345162ee5 | ||
|
|
0af790efa6 | ||
|
|
8452f2617a | ||
|
|
01f42c65e8 | ||
|
|
71e6fb1c40 | ||
|
|
3c1db5e79f | ||
|
|
f2d93a4e27 | ||
|
|
0e1efb591c | ||
|
|
31d907a1f2 | ||
|
|
6c161ff3e0 | ||
|
|
62378b25df | ||
|
|
66d0d5d11c | ||
|
|
51caf33232 | ||
|
|
b5d4b9b212 | ||
|
|
7653ffcb40 | ||
|
|
3c2b79b1f0 | ||
|
|
20dce91c5b | ||
|
|
b19100a021 | ||
|
|
8f20817ea3 | ||
|
|
abb90be792 | ||
|
|
d21dbc7c58 | ||
|
|
98c9de82b5 | ||
|
|
3532a12264 | ||
|
|
ffd34a58f9 | ||
|
|
4bcfdf4b03 | ||
|
|
2e5107d6e8 | ||
|
|
c79bf064c8 | ||
|
|
9783bf99ca | ||
|
|
71a70a3b95 | ||
|
|
ccc507a243 | ||
|
|
722c5579de | ||
|
|
8ca3aa3846 | ||
|
|
31d3eef0da | ||
|
|
0404f437f9 | ||
|
|
ce20cc8661 | ||
|
|
162071d611 | ||
|
|
3a230d0624 | ||
|
|
76282b9981 | ||
|
|
93844bee41 | ||
|
|
c1133b0948 | ||
|
|
92238b15b0 | ||
|
|
6742a16d50 | ||
|
|
624596adf1 | ||
|
|
4e08f97310 | ||
|
|
ff72429a55 | ||
|
|
aa800ddc83 | ||
|
|
deb95c5ead | ||
|
|
d820839541 | ||
|
|
df3a464cec | ||
|
|
6ebeb28fdb | ||
|
|
c2ef594a78 | ||
|
|
f48eed487c | ||
|
|
d2e7a4a970 | ||
|
|
287fed94ef | ||
|
|
6bbaf9de16 | ||
|
|
9af48bae17 | ||
|
|
4cec056568 | ||
|
|
d215eb07c2 | ||
|
|
69c7df8a30 | ||
|
|
b7835f3976 | ||
|
|
472ddcc696 | ||
|
|
e68f7a1d31 | ||
|
|
832ab43bdd | ||
|
|
e7fd27b648 | ||
|
|
cecab1a310 | ||
|
|
9809ea57ae | ||
|
|
b567d06108 | ||
|
|
8b90c04c62 | ||
|
|
384902502c | ||
|
|
5e510e41ee | ||
|
|
0db15e5ee5 | ||
|
|
44ab90f92d | ||
|
|
7a5d91cb96 | ||
|
|
491889a237 | ||
|
|
a2f07828e4 | ||
|
|
1eab81f58d | ||
|
|
d94b9513e7 | ||
|
|
02c36bae29 | ||
|
|
70deb3168e | ||
|
|
7b0d8f05ba | ||
|
|
f230ee4258 | ||
|
|
a5d409b777 | ||
|
|
39f14b6d0b | ||
|
|
dd1daf2652 | ||
|
|
9a415c85b9 | ||
|
|
79a0172ace | ||
|
|
c7cc718ae3 | ||
|
|
8fc746555d | ||
|
|
5ed04baab0 | ||
|
|
882c83f9de | ||
|
|
869a69e531 | ||
|
|
817267e51c | ||
|
|
9e75078dab | ||
|
|
ed3af8e984 | ||
|
|
1d1075b35e | ||
|
|
41f38d92f2 | ||
|
|
8eb44dcbd5 | ||
|
|
3ac5318c94 | ||
|
|
e94a489a31 | ||
|
|
f392d8a7f5 | ||
|
|
c7bc26d8ee | ||
|
|
9da9ecb515 | ||
|
|
6c1594ea24 | ||
|
|
f226ba0c3f | ||
|
|
94aa2c4093 | ||
|
|
c1fc7171fb | ||
|
|
259742a5e3 | ||
|
|
cf527e9ce7 | ||
|
|
c891acb234 | ||
|
|
6876e82fc5 | ||
|
|
b9a0702bc0 | ||
|
|
84ddda6a3f | ||
|
|
2919cbdefc | ||
|
|
f85664fd6b | ||
|
|
059ea76848 | ||
|
|
a65573b061 | ||
|
|
2f085b7310 | ||
|
|
385004cab2 | ||
|
|
cc713e4570 | ||
|
|
9696612b2b | ||
|
|
b3066e9a78 | ||
|
|
d5d7db0de6 | ||
|
|
66c3a0de76 | ||
|
|
45375d1f1b | ||
|
|
96d9de76b0 | ||
|
|
f56e4d24e3 | ||
|
|
8d3d712cef | ||
|
|
4897e9c4d5 | ||
|
|
5cced71fb0 | ||
|
|
7ec727f69f | ||
|
|
5f66b437d5 | ||
|
|
8059c229c5 | ||
|
|
0694f29946 | ||
|
|
bc2099d31d | ||
|
|
211ec3898b | ||
|
|
889c0771d6 | ||
|
|
2227fa434f | ||
|
|
e74d9753f2 | ||
|
|
23d554548a | ||
|
|
9fc71547d7 | ||
|
|
caa9d8b0e4 | ||
|
|
091f7ef153 | ||
|
|
29512ddd31 | ||
|
|
38d606b4d1 | ||
|
|
a7943e7e55 | ||
|
|
ae75d8b209 | ||
|
|
bcdadec5b6 | ||
|
|
e0d5cdd625 | ||
|
|
17f8866264 | ||
|
|
3f95494ae8 | ||
|
|
a7ef4012d0 | ||
|
|
cabbbec72f | ||
|
|
6f07c2b666 | ||
|
|
1dd7f03a7a | ||
|
|
e89c77398d | ||
|
|
66c4eae9cf | ||
|
|
f3a7689f33 | ||
|
|
e721878e4b | ||
|
|
14d0e07b3e | ||
|
|
f2eb6cec9a | ||
|
|
9651eb7a3b | ||
|
|
bc4ddcef05 | ||
|
|
89e3e2b190 | ||
|
|
004282138c | ||
|
|
4580b85fe2 | ||
|
|
8c575388cf | ||
|
|
4792ec41b1 | ||
|
|
df75f7f4e8 | ||
|
|
5cf3884396 | ||
|
|
e5b6ba38ac | ||
|
|
f429a8ab03 | ||
|
|
d7484ab48b | ||
|
|
06fc63028a | ||
|
|
d4623419b0 | ||
|
|
417c44159e | ||
|
|
ebbc6d55d2 | ||
|
|
c0367074ac | ||
|
|
48e2175bac | ||
|
|
45e1e99390 | ||
|
|
b5793c34c6 | ||
|
|
67ee45090d | ||
|
|
64d1a587e7 | ||
|
|
fbe279831e | ||
|
|
375d4d0479 | ||
|
|
7796d4c14a | ||
|
|
78e4b22a2f | ||
|
|
a867450e3b | ||
|
|
b5ee44621c | ||
|
|
24a9e7abd2 | ||
|
|
180c77d1f4 | ||
|
|
247af45791 | ||
|
|
fd0646fecf | ||
|
|
c8821cb226 | ||
|
|
2e531850b8 | ||
|
|
65dbb5ac48 | ||
|
|
16cf7e8e42 | ||
|
|
19556d9e68 | ||
|
|
338833262c | ||
|
|
77800d9212 | ||
|
|
be618df7f5 | ||
|
|
ff85a2e502 | ||
|
|
74675346c9 | ||
|
|
e9344ae046 | ||
|
|
4acf12bb69 | ||
|
|
161d5fe7be | ||
|
|
9bf4ea7341 | ||
|
|
a1bf5651fc | ||
|
|
763228c859 | ||
|
|
770af27d75 | ||
|
|
3e59283c19 | ||
|
|
6146ccba2d | ||
|
|
b0e1527a95 | ||
|
|
cb4410c84e | ||
|
|
98b5976656 | ||
|
|
9335386928 | ||
|
|
7c30d62f33 | ||
|
|
61683bccda | ||
|
|
e1ec5159b8 | ||
|
|
7b7d19592f | ||
|
|
e387c0b6b8 | ||
|
|
d5abc119f3 | ||
|
|
59da6e5620 | ||
|
|
ef1cc0ed4f | ||
|
|
90f0caec1a | ||
|
|
5c0fb63f33 | ||
|
|
036a8e7608 | ||
|
|
10c4d0509d | ||
|
|
57998ea4f8 | ||
|
|
ef9a629ddc | ||
|
|
d974cf5200 | ||
|
|
36d6c6d0e9 | ||
|
|
ff49104764 | ||
|
|
03e43da9f9 | ||
|
|
e696d65b60 | ||
|
|
ebf626061f | ||
|
|
c3d023ad40 | ||
|
|
0a10b3e784 | ||
|
|
e0f66a8196 | ||
|
|
e5dfbd4b23 | ||
|
|
5ad2962389 | ||
|
|
a56e22b6fc | ||
|
|
67f536d7c6 | ||
|
|
741224411b | ||
|
|
818417d119 | ||
|
|
f272785861 | ||
|
|
5d807ee622 | ||
|
|
90c3cdf0f3 | ||
|
|
2bc233b2f6 | ||
|
|
051b23f577 | ||
|
|
f2bc2d33f0 | ||
|
|
38a121a887 | ||
|
|
8b380f0692 |
@@ -1,5 +1,5 @@
|
||||
[unstable]
|
||||
build-std = ["core", "compiler_builtins"]
|
||||
build-std = ["core", "compiler_builtins", "alloc"]
|
||||
build-std-features = ["compiler-builtins-mem"]
|
||||
|
||||
[build]
|
||||
|
||||
34
Cargo.lock
generated
34
Cargo.lock
generated
@@ -20,6 +20,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"bootloader",
|
||||
"lazy_static",
|
||||
"linked_list_allocator",
|
||||
"pc-keyboard",
|
||||
"pic8259",
|
||||
"spin",
|
||||
@@ -43,6 +44,24 @@ dependencies = [
|
||||
"spin",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linked_list_allocator"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0b725207570aa16096962d0b20c79f8a543df2280bd3c903022b9b0b4d7ea68"
|
||||
dependencies = [
|
||||
"spinning_top",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb"
|
||||
dependencies = [
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pc-keyboard"
|
||||
version = "0.7.0"
|
||||
@@ -58,12 +77,27 @@ dependencies = [
|
||||
"x86_64",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||
|
||||
[[package]]
|
||||
name = "spinning_top"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "75adad84ee84b521fb2cca2d4fd0f1dab1d8d026bda3c5bea4ca63b5f9f9293c"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "uart_16550"
|
||||
version = "0.2.14"
|
||||
|
||||
13
Cargo.toml
13
Cargo.toml
@@ -13,13 +13,14 @@ name = "stack_overflow"
|
||||
harness = false
|
||||
|
||||
[dependencies]
|
||||
bootloader = "0.9"
|
||||
bootloader = { version = "0.9", features = ["map_physical_memory"] }
|
||||
volatile = "0.2.6"
|
||||
spin = "0.5.2"
|
||||
x86_64 = "0.14.2"
|
||||
uart_16550 = "0.2.0"
|
||||
pic8259 = "0.10.1"
|
||||
pc-keyboard = "0.7.0"
|
||||
linked_list_allocator = "0.9.0"
|
||||
|
||||
[dependencies.lazy_static]
|
||||
version = "1.0"
|
||||
@@ -28,7 +29,11 @@ features = ["spin_no_std"]
|
||||
|
||||
[package.metadata.bootimage]
|
||||
test-args = [
|
||||
"-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio",
|
||||
"-display", "none"
|
||||
"-device",
|
||||
"isa-debug-exit,iobase=0xf4,iosize=0x04",
|
||||
"-serial",
|
||||
"stdio",
|
||||
"-display",
|
||||
"none",
|
||||
]
|
||||
test-success-exit-code = 33 # (0x10 << 1) | 1
|
||||
test-success-exit-code = 33 # (0x10 << 1) | 1
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
# Blog OS (Introduction to Paging)
|
||||
# Blog OS (Heap Allocation)
|
||||
|
||||
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-08)
|
||||
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-10)
|
||||
|
||||
This repository contains the source code for the [Introduction to Paging][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series.
|
||||
This repository contains the source code for the [Heap Allocation][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series.
|
||||
|
||||
[post]: https://os.phil-opp.com/paging-introduction/
|
||||
[post]: https://os.phil-opp.com/heap-allocation/
|
||||
|
||||
**Check out the [master branch](https://github.com/phil-opp/blog_os) for more information.**
|
||||
|
||||
|
||||
54
src/allocator.rs
Normal file
54
src/allocator.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use alloc::alloc::{GlobalAlloc, Layout};
|
||||
use core::ptr::null_mut;
|
||||
use linked_list_allocator::LockedHeap;
|
||||
use x86_64::{
|
||||
structures::paging::{
|
||||
mapper::MapToError, FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB,
|
||||
},
|
||||
VirtAddr,
|
||||
};
|
||||
|
||||
pub const HEAP_START: usize = 0x_4444_4444_0000;
|
||||
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB
|
||||
|
||||
#[global_allocator]
|
||||
static ALLOCATOR: LockedHeap = LockedHeap::empty();
|
||||
|
||||
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 - 1u64;
|
||||
let heap_start_page = Page::containing_address(heap_start);
|
||||
let heap_end_page = Page::containing_address(heap_end);
|
||||
Page::range_inclusive(heap_start_page, heap_end_page)
|
||||
};
|
||||
|
||||
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(())
|
||||
}
|
||||
|
||||
pub struct Dummy;
|
||||
|
||||
unsafe impl GlobalAlloc for Dummy {
|
||||
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
|
||||
null_mut()
|
||||
}
|
||||
|
||||
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
|
||||
panic!("dealloc should be never called")
|
||||
}
|
||||
}
|
||||
12
src/lib.rs
12
src/lib.rs
@@ -5,10 +5,13 @@
|
||||
#![test_runner(crate::test_runner)]
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
extern crate alloc;
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
pub mod allocator;
|
||||
pub mod gdt;
|
||||
pub mod interrupts;
|
||||
pub mod memory;
|
||||
pub mod serial;
|
||||
pub mod vga_buffer;
|
||||
|
||||
@@ -70,10 +73,15 @@ pub fn hlt_loop() -> ! {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
use bootloader::{entry_point, BootInfo};
|
||||
|
||||
#[cfg(test)]
|
||||
entry_point!(test_kernel_main);
|
||||
|
||||
/// Entry point for `cargo xtest`
|
||||
#[cfg(test)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
fn test_kernel_main(_boot_info: &'static BootInfo) -> ! {
|
||||
init();
|
||||
test_main();
|
||||
hlt_loop();
|
||||
|
||||
45
src/main.rs
45
src/main.rs
@@ -4,20 +4,51 @@
|
||||
#![test_runner(blog_os::test_runner)]
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::{boxed::Box, rc::Rc, vec, vec::Vec};
|
||||
use blog_os::println;
|
||||
use bootloader::{entry_point, BootInfo};
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
use x86_64::registers::control::Cr3;
|
||||
println!("Hello World{}", "!");
|
||||
entry_point!(kernel_main);
|
||||
|
||||
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
use blog_os::allocator;
|
||||
use blog_os::memory::{self, BootInfoFrameAllocator};
|
||||
use x86_64::VirtAddr;
|
||||
|
||||
println!("Hello World{}", "!");
|
||||
blog_os::init();
|
||||
|
||||
let (level_4_page_table, _) = Cr3::read();
|
||||
let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset);
|
||||
let mut mapper = unsafe { memory::init(phys_mem_offset) };
|
||||
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
|
||||
|
||||
allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed");
|
||||
|
||||
// allocate a number on the heap
|
||||
let heap_value = Box::new(41);
|
||||
println!("heap_value at {:p}", heap_value);
|
||||
|
||||
// create a dynamically sized vector
|
||||
let mut vec = Vec::new();
|
||||
for i in 0..500 {
|
||||
vec.push(i);
|
||||
}
|
||||
println!("vec at {:p}", vec.as_slice());
|
||||
|
||||
// create a reference counted vector -> will be freed when count reaches 0
|
||||
let reference_counted = Rc::new(vec![1, 2, 3]);
|
||||
let cloned_reference = reference_counted.clone();
|
||||
println!(
|
||||
"Level 4 page table at: {:?}",
|
||||
level_4_page_table.start_address()
|
||||
"current reference count is {}",
|
||||
Rc::strong_count(&cloned_reference)
|
||||
);
|
||||
core::mem::drop(reference_counted);
|
||||
println!(
|
||||
"reference count is {} now",
|
||||
Rc::strong_count(&cloned_reference)
|
||||
);
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
84
src/memory.rs
Normal file
84
src/memory.rs
Normal file
@@ -0,0 +1,84 @@
|
||||
use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
|
||||
use x86_64::{
|
||||
structures::paging::{FrameAllocator, OffsetPageTable, PageTable, PhysFrame, Size4KiB},
|
||||
PhysAddr, VirtAddr,
|
||||
};
|
||||
|
||||
/// Initialize a new OffsetPageTable.
|
||||
///
|
||||
/// This function is unsafe because the caller must guarantee that the
|
||||
/// complete physical memory is mapped to virtual memory at the passed
|
||||
/// `physical_memory_offset`. Also, this function must be only called once
|
||||
/// to avoid aliasing `&mut` references (which is undefined behavior).
|
||||
pub unsafe fn init(physical_memory_offset: VirtAddr) -> OffsetPageTable<'static> {
|
||||
let level_4_table = active_level_4_table(physical_memory_offset);
|
||||
OffsetPageTable::new(level_4_table, physical_memory_offset)
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the active level 4 table.
|
||||
///
|
||||
/// This function is unsafe because the caller must guarantee that the
|
||||
/// complete physical memory is mapped to virtual memory at the passed
|
||||
/// `physical_memory_offset`. Also, this function must be only called once
|
||||
/// to avoid aliasing `&mut` references (which is undefined behavior).
|
||||
unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut PageTable {
|
||||
use x86_64::registers::control::Cr3;
|
||||
|
||||
let (level_4_table_frame, _) = Cr3::read();
|
||||
|
||||
let phys = level_4_table_frame.start_address();
|
||||
let virt = physical_memory_offset + phys.as_u64();
|
||||
let page_table_ptr: *mut PageTable = virt.as_mut_ptr();
|
||||
|
||||
&mut *page_table_ptr // unsafe
|
||||
}
|
||||
|
||||
/// A FrameAllocator that always returns `None`.
|
||||
pub struct EmptyFrameAllocator;
|
||||
|
||||
unsafe impl FrameAllocator<Size4KiB> for EmptyFrameAllocator {
|
||||
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// A FrameAllocator that returns usable frames from the bootloader's memory map.
|
||||
pub struct BootInfoFrameAllocator {
|
||||
memory_map: &'static MemoryMap,
|
||||
next: usize,
|
||||
}
|
||||
|
||||
impl BootInfoFrameAllocator {
|
||||
/// Create a FrameAllocator from the passed memory map.
|
||||
///
|
||||
/// This function is unsafe because the caller must guarantee that the passed
|
||||
/// memory map is valid. The main requirement is that all frames that are marked
|
||||
/// as `USABLE` in it are really unused.
|
||||
pub unsafe fn init(memory_map: &'static MemoryMap) -> Self {
|
||||
BootInfoFrameAllocator {
|
||||
memory_map,
|
||||
next: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns an iterator over the usable frames specified in the memory map.
|
||||
fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> {
|
||||
// get usable regions from memory map
|
||||
let regions = self.memory_map.iter();
|
||||
let usable_regions = regions.filter(|r| r.region_type == MemoryRegionType::Usable);
|
||||
// map each region to its address range
|
||||
let addr_ranges = usable_regions.map(|r| r.range.start_addr()..r.range.end_addr());
|
||||
// transform to an iterator of frame start addresses
|
||||
let frame_addresses = addr_ranges.flat_map(|r| r.step_by(4096));
|
||||
// create `PhysFrame` types from the start addresses
|
||||
frame_addresses.map(|addr| PhysFrame::containing_address(PhysAddr::new(addr)))
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
|
||||
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
||||
let frame = self.usable_frames().nth(self.next);
|
||||
self.next += 1;
|
||||
frame
|
||||
}
|
||||
}
|
||||
60
tests/heap_allocation.rs
Normal file
60
tests/heap_allocation.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(custom_test_frameworks)]
|
||||
#![test_runner(blog_os::test_runner)]
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::{boxed::Box, vec::Vec};
|
||||
use blog_os::allocator::HEAP_SIZE;
|
||||
use bootloader::{entry_point, BootInfo};
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
entry_point!(main);
|
||||
|
||||
fn main(boot_info: &'static BootInfo) -> ! {
|
||||
use blog_os::allocator;
|
||||
use blog_os::memory::{self, BootInfoFrameAllocator};
|
||||
use x86_64::VirtAddr;
|
||||
|
||||
blog_os::init();
|
||||
let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset);
|
||||
let mut mapper = unsafe { memory::init(phys_mem_offset) };
|
||||
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
|
||||
allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed");
|
||||
|
||||
test_main();
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn simple_allocation() {
|
||||
let heap_value_1 = Box::new(41);
|
||||
let heap_value_2 = Box::new(13);
|
||||
assert_eq!(*heap_value_1, 41);
|
||||
assert_eq!(*heap_value_2, 13);
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn large_vec() {
|
||||
let n = 1000;
|
||||
let mut vec = Vec::new();
|
||||
for i in 0..n {
|
||||
vec.push(i);
|
||||
}
|
||||
assert_eq!(vec.iter().sum::<u64>(), (n - 1) * n / 2);
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn many_boxes() {
|
||||
for i in 0..HEAP_SIZE {
|
||||
let x = Box::new(i);
|
||||
assert_eq!(*x, i);
|
||||
}
|
||||
}
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
blog_os::test_panic_handler(info)
|
||||
}
|
||||
Reference in New Issue
Block a user