Compare commits

..

3 Commits

Author SHA1 Message Date
Robert Lucas
bd136dcba4 Merge f894fb15d5 into bc7335ce6c 2025-02-11 11:15:35 +00:00
Robert-M-Lucas
f894fb15d5 cargo fmt 2025-01-01 14:30:55 +00:00
Robert-M-Lucas
38aab28025 Correcting many_boxes_long_lived that BumpAllocator should fail, when it currently succeeds with cargo test -r 2025-01-01 14:23:57 +00:00
16 changed files with 62 additions and 106 deletions

85
Cargo.lock generated
View File

@@ -2,29 +2,17 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "autocfg"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
[[package]] [[package]]
name = "bit_field" name = "bit_field"
version = "0.10.2" version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61" checksum = "dcb6dd1c2376d2e096796e234a70e17e94cc2d5d54ff8ce42b28cef1d0d359a4"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "bitflags"
version = "2.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a65b545ab31d687cff52899d4890855fec459eb6afe0da6417b8a18da87aa29"
[[package]] [[package]]
name = "blog_os" name = "blog_os"
@@ -35,7 +23,7 @@ dependencies = [
"linked_list_allocator", "linked_list_allocator",
"pc-keyboard", "pc-keyboard",
"pic8259", "pic8259",
"spin 0.5.2", "spin",
"uart_16550", "uart_16550",
"volatile 0.2.7", "volatile 0.2.7",
"x86_64", "x86_64",
@@ -43,35 +31,34 @@ dependencies = [
[[package]] [[package]]
name = "bootloader" name = "bootloader"
version = "0.9.33" version = "0.9.30"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bdfddac270bbdd45903296bc1caf29a7fdce6b326aaf0bbab7f04c5f98b7447" checksum = "8a9c8b93781debeb5bc44a12adc4be812aa9feb659d60eeafcd7e9bedb549561"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.5.0" version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
dependencies = [ dependencies = [
"spin 0.9.8", "spin",
] ]
[[package]] [[package]]
name = "linked_list_allocator" name = "linked_list_allocator"
version = "0.9.1" version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "549ce1740e46b291953c4340adcd74c59bcf4308f4cac050fd33ba91b7168f4a" checksum = "d0b725207570aa16096962d0b20c79f8a543df2280bd3c903022b9b0b4d7ea68"
dependencies = [ dependencies = [
"spinning_top", "spinning_top",
] ]
[[package]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.4.13" version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765" checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb"
dependencies = [ dependencies = [
"autocfg",
"scopeguard", "scopeguard",
] ]
@@ -83,24 +70,18 @@ checksum = "ed089a1fbffe3337a1a345501c981f1eb1e47e69de5a40e852433e12953c3174"
[[package]] [[package]]
name = "pic8259" name = "pic8259"
version = "0.10.4" version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb844b5b01db1e0b17938685738f113bfc903846f18932b378bc0eabfa40e194" checksum = "08cc920d83ee33c0f9b73aa441e75468bf2d10c959a3eb6260cf720b05ac91a1"
dependencies = [ dependencies = [
"x86_64", "x86_64",
] ]
[[package]]
name = "rustversion"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.2.0" version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]] [[package]]
name = "spin" name = "spin"
@@ -108,29 +89,22 @@ 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 = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
[[package]] [[package]]
name = "spinning_top" name = "spinning_top"
version = "0.2.5" version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b9eb1a2f4c41445a3a0ff9abc5221c5fcd28e1f13cd7c0397706f9ac938ddb0" checksum = "75adad84ee84b521fb2cca2d4fd0f1dab1d8d026bda3c5bea4ca63b5f9f9293c"
dependencies = [ dependencies = [
"lock_api", "lock_api",
] ]
[[package]] [[package]]
name = "uart_16550" name = "uart_16550"
version = "0.2.19" version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "614ff2a87880d4bd4374722268598a970bbad05ced8bf630439417347254ab2e" checksum = "503a6c0e6d82daa87985e662d120c0176b09587c92a68db22781b28ae95405dd"
dependencies = [ dependencies = [
"bitflags 1.3.2", "bitflags",
"rustversion",
"x86_64", "x86_64",
] ]
@@ -142,18 +116,17 @@ checksum = "f6b06ad3ed06fef1713569d547cdbdb439eafed76341820fb0e0344f29a41945"
[[package]] [[package]]
name = "volatile" name = "volatile"
version = "0.4.6" version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793" checksum = "e4c2dbd44eb8b53973357e6e207e370f0c1059990df850aca1eca8947cf464f0"
[[package]] [[package]]
name = "x86_64" name = "x86_64"
version = "0.14.13" version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c101112411baafbb4bf8d33e4c4a80ab5b02d74d2612331c61e8192fc9710491" checksum = "fb611915c917c6296d11e23f71ff1ecfe49c5766daba92cd3df52df6b58285b6"
dependencies = [ dependencies = [
"bit_field", "bit_field",
"bitflags 2.9.2", "bitflags",
"rustversion", "volatile 0.4.4",
"volatile 0.4.6",
] ]

View File

@@ -2,7 +2,7 @@
name = "blog_os" name = "blog_os"
version = "0.1.0" version = "0.1.0"
authors = ["Philipp Oppermann <dev@phil-opp.com>"] authors = ["Philipp Oppermann <dev@phil-opp.com>"]
edition = "2024" edition = "2018"
[[test]] [[test]]
name = "should_panic" name = "should_panic"
@@ -26,11 +26,6 @@ linked_list_allocator = "0.9.0"
version = "1.0" version = "1.0"
features = ["spin_no_std"] features = ["spin_no_std"]
[[bin]]
name = "blog_os"
test = true
bench = false
[package.metadata.bootimage] [package.metadata.bootimage]
test-args = [ test-args = [

View File

@@ -2,10 +2,10 @@ use alloc::alloc::{GlobalAlloc, Layout};
use core::ptr::null_mut; use core::ptr::null_mut;
use fixed_size_block::FixedSizeBlockAllocator; use fixed_size_block::FixedSizeBlockAllocator;
use x86_64::{ use x86_64::{
VirtAddr,
structures::paging::{ structures::paging::{
FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB, mapper::MapToError, mapper::MapToError, FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB,
}, },
VirtAddr,
}; };
pub mod bump; pub mod bump;

View File

@@ -1,4 +1,4 @@
use super::{Locked, align_up}; use super::{align_up, Locked};
use alloc::alloc::{GlobalAlloc, Layout}; use alloc::alloc::{GlobalAlloc, Layout};
use core::ptr; use core::ptr;

View File

@@ -44,10 +44,8 @@ impl FixedSizeBlockAllocator {
/// heap bounds are valid and that the heap is unused. This method must be /// heap bounds are valid and that the heap is unused. This method must be
/// called only once. /// called only once.
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) { pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
unsafe {
self.fallback_allocator.init(heap_start, heap_size); self.fallback_allocator.init(heap_start, heap_size);
} }
}
/// Allocates using the fallback allocator. /// Allocates using the fallback allocator.
fn fallback_alloc(&mut self, layout: Layout) -> *mut u8 { fn fallback_alloc(&mut self, layout: Layout) -> *mut u8 {
@@ -93,17 +91,13 @@ unsafe impl GlobalAlloc for Locked<FixedSizeBlockAllocator> {
assert!(mem::size_of::<ListNode>() <= BLOCK_SIZES[index]); assert!(mem::size_of::<ListNode>() <= BLOCK_SIZES[index]);
assert!(mem::align_of::<ListNode>() <= BLOCK_SIZES[index]); assert!(mem::align_of::<ListNode>() <= BLOCK_SIZES[index]);
let new_node_ptr = ptr as *mut ListNode; let new_node_ptr = ptr as *mut ListNode;
unsafe {
new_node_ptr.write(new_node); new_node_ptr.write(new_node);
allocator.list_heads[index] = Some(&mut *new_node_ptr); allocator.list_heads[index] = Some(&mut *new_node_ptr);
} }
}
None => { None => {
let ptr = NonNull::new(ptr).unwrap(); let ptr = NonNull::new(ptr).unwrap();
unsafe {
allocator.fallback_allocator.deallocate(ptr, layout); allocator.fallback_allocator.deallocate(ptr, layout);
} }
} }
} }
} }
}

View File

@@ -1,4 +1,4 @@
use super::{Locked, align_up}; use super::{align_up, Locked};
use alloc::alloc::{GlobalAlloc, Layout}; use alloc::alloc::{GlobalAlloc, Layout};
use core::{mem, ptr}; use core::{mem, ptr};
@@ -39,10 +39,8 @@ impl LinkedListAllocator {
/// heap bounds are valid and that the heap is unused. This method must be /// heap bounds are valid and that the heap is unused. This method must be
/// called only once. /// called only once.
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) { pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
unsafe {
self.add_free_region(heap_start, heap_size); self.add_free_region(heap_start, heap_size);
} }
}
/// Adds the given memory region to the front of the list. /// Adds the given memory region to the front of the list.
unsafe fn add_free_region(&mut self, addr: usize, size: usize) { unsafe fn add_free_region(&mut self, addr: usize, size: usize) {
@@ -54,10 +52,8 @@ impl LinkedListAllocator {
let mut node = ListNode::new(size); let mut node = ListNode::new(size);
node.next = self.head.next.take(); node.next = self.head.next.take();
let node_ptr = addr as *mut ListNode; let node_ptr = addr as *mut ListNode;
unsafe {
node_ptr.write(node); node_ptr.write(node);
self.head.next = Some(&mut *node_ptr); self.head.next = Some(&mut *node_ptr)
}
} }
/// Looks for a free region with the given size and alignment and removes /// Looks for a free region with the given size and alignment and removes
@@ -132,10 +128,8 @@ unsafe impl GlobalAlloc for Locked<LinkedListAllocator> {
let alloc_end = alloc_start.checked_add(size).expect("overflow"); let alloc_end = alloc_start.checked_add(size).expect("overflow");
let excess_size = region.end_addr() - alloc_end; let excess_size = region.end_addr() - alloc_end;
if excess_size > 0 { if excess_size > 0 {
unsafe {
allocator.add_free_region(alloc_end, excess_size); allocator.add_free_region(alloc_end, excess_size);
} }
}
alloc_start as *mut u8 alloc_start as *mut u8
} else { } else {
ptr::null_mut() ptr::null_mut()
@@ -146,6 +140,6 @@ unsafe impl GlobalAlloc for Locked<LinkedListAllocator> {
// perform layout adjustments // perform layout adjustments
let (size, _) = LinkedListAllocator::size_align(layout); let (size, _) = LinkedListAllocator::size_align(layout);
unsafe { self.lock().add_free_region(ptr as usize, size) } self.lock().add_free_region(ptr as usize, size)
} }
} }

View File

@@ -1,7 +1,7 @@
use lazy_static::lazy_static; use lazy_static::lazy_static;
use x86_64::VirtAddr;
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector}; use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector};
use x86_64::structures::tss::TaskStateSegment; use x86_64::structures::tss::TaskStateSegment;
use x86_64::VirtAddr;
pub const DOUBLE_FAULT_IST_INDEX: u16 = 0; pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;
@@ -41,7 +41,7 @@ struct Selectors {
} }
pub fn init() { pub fn init() {
use x86_64::instructions::segmentation::{CS, Segment}; use x86_64::instructions::segmentation::{Segment, CS};
use x86_64::instructions::tables::load_tss; use x86_64::instructions::tables::load_tss;
GDT.0.load(); GDT.0.load();

View File

@@ -80,7 +80,7 @@ extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFr
} }
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStackFrame) { extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStackFrame) {
use pc_keyboard::{DecodedKey, HandleControl, Keyboard, ScancodeSet1, layouts}; use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
use spin::Mutex; use spin::Mutex;
use x86_64::instructions::port::Port; use x86_64::instructions::port::Port;

View File

@@ -74,7 +74,7 @@ pub fn hlt_loop() -> ! {
} }
#[cfg(test)] #[cfg(test)]
use bootloader::{BootInfo, entry_point}; use bootloader::{entry_point, BootInfo};
#[cfg(test)] #[cfg(test)]
entry_point!(test_kernel_main); entry_point!(test_kernel_main);

View File

@@ -8,7 +8,7 @@ extern crate alloc;
use alloc::{boxed::Box, rc::Rc, vec, vec::Vec}; use alloc::{boxed::Box, rc::Rc, vec, vec::Vec};
use blog_os::println; use blog_os::println;
use bootloader::{BootInfo, entry_point}; use bootloader::{entry_point, BootInfo};
use core::panic::PanicInfo; use core::panic::PanicInfo;
entry_point!(kernel_main); entry_point!(kernel_main);

View File

@@ -1,7 +1,7 @@
use bootloader::bootinfo::{MemoryMap, MemoryRegionType}; use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
use x86_64::{ use x86_64::{
PhysAddr, VirtAddr,
structures::paging::{FrameAllocator, OffsetPageTable, PageTable, PhysFrame, Size4KiB}, structures::paging::{FrameAllocator, OffsetPageTable, PageTable, PhysFrame, Size4KiB},
PhysAddr, VirtAddr,
}; };
/// Initialize a new OffsetPageTable. /// Initialize a new OffsetPageTable.
@@ -11,11 +11,9 @@ use x86_64::{
/// `physical_memory_offset`. Also, this function must be only called once /// `physical_memory_offset`. Also, this function must be only called once
/// to avoid aliasing `&mut` references (which is undefined behavior). /// to avoid aliasing `&mut` references (which is undefined behavior).
pub unsafe fn init(physical_memory_offset: VirtAddr) -> OffsetPageTable<'static> { pub unsafe fn init(physical_memory_offset: VirtAddr) -> OffsetPageTable<'static> {
unsafe {
let level_4_table = active_level_4_table(physical_memory_offset); let level_4_table = active_level_4_table(physical_memory_offset);
OffsetPageTable::new(level_4_table, physical_memory_offset) OffsetPageTable::new(level_4_table, physical_memory_offset)
} }
}
/// Returns a mutable reference to the active level 4 table. /// Returns a mutable reference to the active level 4 table.
/// ///
@@ -32,7 +30,7 @@ unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut
let virt = physical_memory_offset + phys.as_u64(); let virt = physical_memory_offset + phys.as_u64();
let page_table_ptr: *mut PageTable = virt.as_mut_ptr(); let page_table_ptr: *mut PageTable = virt.as_mut_ptr();
unsafe { &mut *page_table_ptr } &mut *page_table_ptr // unsafe
} }
/// A FrameAllocator that always returns `None`. /// A FrameAllocator that always returns `None`.

View File

@@ -7,7 +7,7 @@
use blog_os::println; use blog_os::println;
use core::panic::PanicInfo; use core::panic::PanicInfo;
#[unsafe(no_mangle)] // don't mangle the name of this function #[no_mangle] // don't mangle the name of this function
pub extern "C" fn _start() -> ! { pub extern "C" fn _start() -> ! {
test_main(); test_main();

View File

@@ -8,7 +8,8 @@ extern crate alloc;
use alloc::{boxed::Box, vec::Vec}; use alloc::{boxed::Box, vec::Vec};
use blog_os::allocator::HEAP_SIZE; use blog_os::allocator::HEAP_SIZE;
use bootloader::{BootInfo, entry_point}; use bootloader::{entry_point, BootInfo};
use core::hint::black_box;
use core::panic::PanicInfo; use core::panic::PanicInfo;
entry_point!(main); entry_point!(main);
@@ -61,6 +62,7 @@ fn many_boxes_long_lived() {
let x = Box::new(i); let x = Box::new(i);
assert_eq!(*x, i); assert_eq!(*x, i);
} }
black_box(&long_lived); // new - ensures long_lived isn't optimized away
assert_eq!(*long_lived, 1); // new assert_eq!(*long_lived, 1); // new
} }

View File

@@ -1,10 +1,10 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
use blog_os::{QemuExitCode, exit_qemu, serial_print, serial_println}; use blog_os::{exit_qemu, serial_print, serial_println, QemuExitCode};
use core::panic::PanicInfo; use core::panic::PanicInfo;
#[unsafe(no_mangle)] #[no_mangle]
pub extern "C" fn _start() -> ! { pub extern "C" fn _start() -> ! {
should_fail(); should_fail();
serial_println!("[test did not panic]"); serial_println!("[test did not panic]");

View File

@@ -2,12 +2,12 @@
#![no_main] #![no_main]
#![feature(abi_x86_interrupt)] #![feature(abi_x86_interrupt)]
use blog_os::{QemuExitCode, exit_qemu, serial_print, serial_println}; use blog_os::{exit_qemu, serial_print, serial_println, QemuExitCode};
use core::panic::PanicInfo; use core::panic::PanicInfo;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame}; use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
#[unsafe(no_mangle)] #[no_mangle]
pub extern "C" fn _start() -> ! { pub extern "C" fn _start() -> ! {
serial_print!("stack_overflow::stack_overflow...\t"); serial_print!("stack_overflow::stack_overflow...\t");

View File

@@ -3,8 +3,8 @@
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128", "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
"arch": "x86_64", "arch": "x86_64",
"target-endian": "little", "target-endian": "little",
"target-pointer-width": 64, "target-pointer-width": "64",
"target-c-int-width": 32, "target-c-int-width": "32",
"os": "none", "os": "none",
"executables": true, "executables": true,
"linker-flavor": "ld.lld", "linker-flavor": "ld.lld",