mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-17 06:47:49 +00:00
Compare commits
107 Commits
post-09
...
f0149947cf
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f0149947cf | ||
|
|
09a3ac4f80 | ||
|
|
506a1d1961 | ||
|
|
e41ff18ea0 | ||
|
|
0af790efa6 | ||
|
|
8452f2617a | ||
|
|
71e6fb1c40 | ||
|
|
f2d93a4e27 | ||
|
|
31d907a1f2 | ||
|
|
62378b25df | ||
|
|
51caf33232 | ||
|
|
7653ffcb40 | ||
|
|
20dce91c5b | ||
|
|
b19100a021 | ||
|
|
abb90be792 | ||
|
|
98c9de82b5 | ||
|
|
ffd34a58f9 | ||
|
|
2e5107d6e8 | ||
|
|
9783bf99ca | ||
|
|
ccc507a243 | ||
|
|
8ca3aa3846 | ||
|
|
0404f437f9 | ||
|
|
162071d611 | ||
|
|
3a230d0624 | ||
|
|
76282b9981 | ||
|
|
c1133b0948 | ||
|
|
6742a16d50 | ||
|
|
4e08f97310 | ||
|
|
aa800ddc83 | ||
|
|
deb95c5ead | ||
|
|
d820839541 | ||
|
|
6ebeb28fdb | ||
|
|
f48eed487c | ||
|
|
287fed94ef | ||
|
|
9af48bae17 | ||
|
|
d215eb07c2 | ||
|
|
69c7df8a30 | ||
|
|
e68f7a1d31 | ||
|
|
e7fd27b648 | ||
|
|
9809ea57ae | ||
|
|
b567d06108 | ||
|
|
384902502c | ||
|
|
0db15e5ee5 | ||
|
|
44ab90f92d | ||
|
|
7a5d91cb96 | ||
|
|
1eab81f58d | ||
|
|
02c36bae29 | ||
|
|
7b0d8f05ba | ||
|
|
f230ee4258 | ||
|
|
39f14b6d0b | ||
|
|
dd1daf2652 | ||
|
|
9a415c85b9 | ||
|
|
c7cc718ae3 | ||
|
|
5ed04baab0 | ||
|
|
882c83f9de | ||
|
|
869a69e531 | ||
|
|
817267e51c | ||
|
|
9e75078dab | ||
|
|
41f38d92f2 | ||
|
|
3ac5318c94 | ||
|
|
e94a489a31 | ||
|
|
9da9ecb515 | ||
|
|
f226ba0c3f | ||
|
|
c1fc7171fb | ||
|
|
cf527e9ce7 | ||
|
|
c891acb234 | ||
|
|
b9a0702bc0 | ||
|
|
2919cbdefc | ||
|
|
059ea76848 | ||
|
|
2f085b7310 | ||
|
|
cc713e4570 | ||
|
|
b3066e9a78 | ||
|
|
66c3a0de76 | ||
|
|
96d9de76b0 | ||
|
|
f56e4d24e3 | ||
|
|
8d3d712cef | ||
|
|
5f66b437d5 | ||
|
|
0694f29946 | ||
|
|
889c0771d6 | ||
|
|
e74d9753f2 | ||
|
|
9fc71547d7 | ||
|
|
caa9d8b0e4 | ||
|
|
29512ddd31 | ||
|
|
a7943e7e55 | ||
|
|
bcdadec5b6 | ||
|
|
17f8866264 | ||
|
|
a7ef4012d0 | ||
|
|
6f07c2b666 | ||
|
|
e89c77398d | ||
|
|
f3a7689f33 | ||
|
|
14d0e07b3e | ||
|
|
9651eb7a3b | ||
|
|
89e3e2b190 | ||
|
|
004282138c | ||
|
|
8c575388cf | ||
|
|
4792ec41b1 | ||
|
|
df75f7f4e8 | ||
|
|
5cf3884396 | ||
|
|
e5b6ba38ac | ||
|
|
f429a8ab03 | ||
|
|
d7484ab48b | ||
|
|
06fc63028a | ||
|
|
d4623419b0 | ||
|
|
417c44159e | ||
|
|
ebbc6d55d2 | ||
|
|
c0367074ac | ||
|
|
48e2175bac |
@@ -1,5 +1,5 @@
|
|||||||
[unstable]
|
[unstable]
|
||||||
build-std = ["core", "compiler_builtins"]
|
build-std = ["core", "compiler_builtins", "alloc"]
|
||||||
build-std-features = ["compiler-builtins-mem"]
|
build-std-features = ["compiler-builtins-mem"]
|
||||||
|
|
||||||
[build]
|
[build]
|
||||||
|
|||||||
34
Cargo.lock
generated
34
Cargo.lock
generated
@@ -20,6 +20,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"bootloader",
|
"bootloader",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
|
"linked_list_allocator",
|
||||||
"pc-keyboard",
|
"pc-keyboard",
|
||||||
"pic8259",
|
"pic8259",
|
||||||
"spin",
|
"spin",
|
||||||
@@ -43,6 +44,24 @@ dependencies = [
|
|||||||
"spin",
|
"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]]
|
[[package]]
|
||||||
name = "pc-keyboard"
|
name = "pc-keyboard"
|
||||||
version = "0.5.1"
|
version = "0.5.1"
|
||||||
@@ -58,12 +77,27 @@ dependencies = [
|
|||||||
"x86_64",
|
"x86_64",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "scopeguard"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "spin"
|
name = "spin"
|
||||||
version = "0.5.2"
|
version = "0.5.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
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]]
|
[[package]]
|
||||||
name = "uart_16550"
|
name = "uart_16550"
|
||||||
version = "0.2.14"
|
version = "0.2.14"
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ x86_64 = "0.14.2"
|
|||||||
uart_16550 = "0.2.0"
|
uart_16550 = "0.2.0"
|
||||||
pic8259 = "0.10.1"
|
pic8259 = "0.10.1"
|
||||||
pc-keyboard = "0.5.0"
|
pc-keyboard = "0.5.0"
|
||||||
|
linked_list_allocator = "0.9.0"
|
||||||
|
|
||||||
[dependencies.lazy_static]
|
[dependencies.lazy_static]
|
||||||
version = "1.0"
|
version = "1.0"
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
# Blog OS (Paging Implementation)
|
# Blog OS (Heap Allocation)
|
||||||
|
|
||||||
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-09)
|
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-10)
|
||||||
|
|
||||||
This repository contains the source code for the [Paging Implementation][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-implementation/
|
[post]: https://os.phil-opp.com/heap-allocation/
|
||||||
|
|
||||||
**Check out the [master branch](https://github.com/phil-opp/blog_os) for more information.**
|
**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")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,11 +2,14 @@
|
|||||||
#![cfg_attr(test, no_main)]
|
#![cfg_attr(test, no_main)]
|
||||||
#![feature(custom_test_frameworks)]
|
#![feature(custom_test_frameworks)]
|
||||||
#![feature(abi_x86_interrupt)]
|
#![feature(abi_x86_interrupt)]
|
||||||
|
#![feature(alloc_error_handler)]
|
||||||
#![test_runner(crate::test_runner)]
|
#![test_runner(crate::test_runner)]
|
||||||
#![reexport_test_harness_main = "test_main"]
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
|
pub mod allocator;
|
||||||
pub mod gdt;
|
pub mod gdt;
|
||||||
pub mod interrupts;
|
pub mod interrupts;
|
||||||
pub mod memory;
|
pub mod memory;
|
||||||
@@ -90,3 +93,8 @@ fn test_kernel_main(_boot_info: &'static BootInfo) -> ! {
|
|||||||
fn panic(info: &PanicInfo) -> ! {
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
test_panic_handler(info)
|
test_panic_handler(info)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[alloc_error_handler]
|
||||||
|
fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! {
|
||||||
|
panic!("allocation error: {:?}", layout)
|
||||||
|
}
|
||||||
|
|||||||
36
src/main.rs
36
src/main.rs
@@ -4,6 +4,9 @@
|
|||||||
#![test_runner(blog_os::test_runner)]
|
#![test_runner(blog_os::test_runner)]
|
||||||
#![reexport_test_harness_main = "test_main"]
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
|
use alloc::{boxed::Box, rc::Rc, vec, vec::Vec};
|
||||||
use blog_os::println;
|
use blog_os::println;
|
||||||
use bootloader::{entry_point, BootInfo};
|
use bootloader::{entry_point, BootInfo};
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
@@ -11,8 +14,9 @@ use core::panic::PanicInfo;
|
|||||||
entry_point!(kernel_main);
|
entry_point!(kernel_main);
|
||||||
|
|
||||||
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||||
|
use blog_os::allocator;
|
||||||
use blog_os::memory::{self, BootInfoFrameAllocator};
|
use blog_os::memory::{self, BootInfoFrameAllocator};
|
||||||
use x86_64::{structures::paging::Page, VirtAddr};
|
use x86_64::VirtAddr;
|
||||||
|
|
||||||
println!("Hello World{}", "!");
|
println!("Hello World{}", "!");
|
||||||
blog_os::init();
|
blog_os::init();
|
||||||
@@ -21,13 +25,31 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
|||||||
let mut mapper = unsafe { memory::init(phys_mem_offset) };
|
let mut mapper = unsafe { memory::init(phys_mem_offset) };
|
||||||
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
|
let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
|
||||||
|
|
||||||
// map an unused page
|
allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed");
|
||||||
let page = Page::containing_address(VirtAddr::new(0xdeadbeaf000));
|
|
||||||
memory::create_example_mapping(page, &mut mapper, &mut frame_allocator);
|
|
||||||
|
|
||||||
// write the string `New!` to the screen through the new mapping
|
// allocate a number on the heap
|
||||||
let page_ptr: *mut u64 = page.start_address().as_mut_ptr();
|
let heap_value = Box::new(41);
|
||||||
unsafe { page_ptr.offset(400).write_volatile(0x_f021_f077_f065_f04e) };
|
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!(
|
||||||
|
"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)]
|
#[cfg(test)]
|
||||||
test_main();
|
test_main();
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
|
use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
|
||||||
use x86_64::{
|
use x86_64::{
|
||||||
structures::paging::{
|
structures::paging::{FrameAllocator, OffsetPageTable, PageTable, PhysFrame, Size4KiB},
|
||||||
FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB,
|
|
||||||
},
|
|
||||||
PhysAddr, VirtAddr,
|
PhysAddr, VirtAddr,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -35,24 +33,6 @@ unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut
|
|||||||
&mut *page_table_ptr // unsafe
|
&mut *page_table_ptr // unsafe
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an example mapping for the given page to frame `0xb8000`.
|
|
||||||
pub fn create_example_mapping(
|
|
||||||
page: Page,
|
|
||||||
mapper: &mut OffsetPageTable,
|
|
||||||
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
|
|
||||||
) {
|
|
||||||
use x86_64::structures::paging::PageTableFlags as Flags;
|
|
||||||
|
|
||||||
let frame = PhysFrame::containing_address(PhysAddr::new(0xb8000));
|
|
||||||
let flags = Flags::PRESENT | Flags::WRITABLE;
|
|
||||||
|
|
||||||
let map_to_result = unsafe {
|
|
||||||
// FIXME: this is not safe, we do it only for testing
|
|
||||||
mapper.map_to(page, frame, flags, frame_allocator)
|
|
||||||
};
|
|
||||||
map_to_result.expect("map_to failed").flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A FrameAllocator that always returns `None`.
|
/// A FrameAllocator that always returns `None`.
|
||||||
pub struct EmptyFrameAllocator;
|
pub struct EmptyFrameAllocator;
|
||||||
|
|
||||||
|
|||||||
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