mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Compare commits
50 Commits
bc7335ce6c
...
121dc393f8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
121dc393f8 | ||
|
|
08a6e13df4 | ||
|
|
569232322e | ||
|
|
94149c93f9 | ||
|
|
34195ec8b7 | ||
|
|
27b56832c1 | ||
|
|
48dfc6d027 | ||
|
|
e9e75e759e | ||
|
|
194fa3c3fe | ||
|
|
9799487962 | ||
|
|
a7c5741ab5 | ||
|
|
65d8ef5478 | ||
|
|
c6c0e1a7f0 | ||
|
|
c407c26224 | ||
|
|
0da8a4bc6b | ||
|
|
d43aa8aebf | ||
|
|
2049f23abb | ||
|
|
0dab4b7ec2 | ||
|
|
3e814722d0 | ||
|
|
5b088bffeb | ||
|
|
e9ca6b83c4 | ||
|
|
62b2c8a0b5 | ||
|
|
1d0aa17531 | ||
|
|
6de3aeaac3 | ||
|
|
a26d6c1b54 | ||
|
|
dc03bde076 | ||
|
|
3a988748de | ||
|
|
aa4f62187e | ||
|
|
e8f6e7300d | ||
|
|
889ceba24a | ||
|
|
1489b0aa06 | ||
|
|
08831b4ba1 | ||
|
|
3d94346f43 | ||
|
|
f19372d58a | ||
|
|
e4b3ed6d42 | ||
|
|
b1aec7eb66 | ||
|
|
e2a3e76a32 | ||
|
|
16bbb8f972 | ||
|
|
430e2143f8 | ||
|
|
86ffa24e8e | ||
|
|
69fe1aed12 | ||
|
|
d849a7dea5 | ||
|
|
bb2c57e1b4 | ||
|
|
18fd90b7a7 | ||
|
|
d83ab74e53 | ||
|
|
caa829db01 | ||
|
|
168dc5c2be | ||
|
|
a6141e8d2e | ||
|
|
df0c0b5783 | ||
|
|
c27eb1da9c |
4
Cargo.lock
generated
4
Cargo.lock
generated
@@ -31,9 +31,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bootloader"
|
name = "bootloader"
|
||||||
version = "0.9.30"
|
version = "0.9.31"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8a9c8b93781debeb5bc44a12adc4be812aa9feb659d60eeafcd7e9bedb549561"
|
checksum = "974e79cf1b0b737839f01330fb5393095daf1124d52693696494e32523ae9ef5"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
|
|||||||
@@ -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 = "2018"
|
edition = "2024"
|
||||||
|
|
||||||
[[test]]
|
[[test]]
|
||||||
name = "should_panic"
|
name = "should_panic"
|
||||||
|
|||||||
@@ -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::{
|
||||||
structures::paging::{
|
|
||||||
mapper::MapToError, FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB,
|
|
||||||
},
|
|
||||||
VirtAddr,
|
VirtAddr,
|
||||||
|
structures::paging::{
|
||||||
|
FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB, mapper::MapToError,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub mod bump;
|
pub mod bump;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use super::{align_up, Locked};
|
use super::{Locked, align_up};
|
||||||
use alloc::alloc::{GlobalAlloc, Layout};
|
use alloc::alloc::{GlobalAlloc, Layout};
|
||||||
use core::ptr;
|
use core::ptr;
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,9 @@ 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) {
|
||||||
self.fallback_allocator.init(heap_start, heap_size);
|
unsafe {
|
||||||
|
self.fallback_allocator.init(heap_start, heap_size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Allocates using the fallback allocator.
|
/// Allocates using the fallback allocator.
|
||||||
@@ -91,12 +93,16 @@ 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;
|
||||||
new_node_ptr.write(new_node);
|
unsafe {
|
||||||
allocator.list_heads[index] = Some(&mut *new_node_ptr);
|
new_node_ptr.write(new_node);
|
||||||
|
allocator.list_heads[index] = Some(&mut *new_node_ptr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let ptr = NonNull::new(ptr).unwrap();
|
let ptr = NonNull::new(ptr).unwrap();
|
||||||
allocator.fallback_allocator.deallocate(ptr, layout);
|
unsafe {
|
||||||
|
allocator.fallback_allocator.deallocate(ptr, layout);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use super::{align_up, Locked};
|
use super::{Locked, align_up};
|
||||||
use alloc::alloc::{GlobalAlloc, Layout};
|
use alloc::alloc::{GlobalAlloc, Layout};
|
||||||
use core::{mem, ptr};
|
use core::{mem, ptr};
|
||||||
|
|
||||||
@@ -39,7 +39,9 @@ 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) {
|
||||||
self.add_free_region(heap_start, heap_size);
|
unsafe {
|
||||||
|
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.
|
||||||
@@ -52,8 +54,10 @@ 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;
|
||||||
node_ptr.write(node);
|
unsafe {
|
||||||
self.head.next = Some(&mut *node_ptr)
|
node_ptr.write(node);
|
||||||
|
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
|
||||||
@@ -128,7 +132,9 @@ 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 {
|
||||||
allocator.add_free_region(alloc_end, excess_size);
|
unsafe {
|
||||||
|
allocator.add_free_region(alloc_end, excess_size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
alloc_start as *mut u8
|
alloc_start as *mut u8
|
||||||
} else {
|
} else {
|
||||||
@@ -140,6 +146,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);
|
||||||
|
|
||||||
self.lock().add_free_region(ptr as usize, size)
|
unsafe { self.lock().add_free_region(ptr as usize, size) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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::{Segment, CS};
|
use x86_64::instructions::segmentation::{CS, Segment};
|
||||||
use x86_64::instructions::tables::load_tss;
|
use x86_64::instructions::tables::load_tss;
|
||||||
|
|
||||||
GDT.0.load();
|
GDT.0.load();
|
||||||
|
|||||||
@@ -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::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
|
use pc_keyboard::{DecodedKey, HandleControl, Keyboard, ScancodeSet1, layouts};
|
||||||
use spin::Mutex;
|
use spin::Mutex;
|
||||||
use x86_64::instructions::port::Port;
|
use x86_64::instructions::port::Port;
|
||||||
|
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ pub fn hlt_loop() -> ! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use bootloader::{entry_point, BootInfo};
|
use bootloader::{BootInfo, entry_point};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
entry_point!(test_kernel_main);
|
entry_point!(test_kernel_main);
|
||||||
|
|||||||
@@ -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::{entry_point, BootInfo};
|
use bootloader::{BootInfo, entry_point};
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
entry_point!(kernel_main);
|
entry_point!(kernel_main);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
|
use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
|
||||||
use x86_64::{
|
use x86_64::{
|
||||||
structures::paging::{FrameAllocator, OffsetPageTable, PageTable, PhysFrame, Size4KiB},
|
|
||||||
PhysAddr, VirtAddr,
|
PhysAddr, VirtAddr,
|
||||||
|
structures::paging::{FrameAllocator, OffsetPageTable, PageTable, PhysFrame, Size4KiB},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Initialize a new OffsetPageTable.
|
/// Initialize a new OffsetPageTable.
|
||||||
@@ -11,8 +11,10 @@ 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> {
|
||||||
let level_4_table = active_level_4_table(physical_memory_offset);
|
unsafe {
|
||||||
OffsetPageTable::new(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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a mutable reference to the active level 4 table.
|
/// Returns a mutable reference to the active level 4 table.
|
||||||
@@ -30,7 +32,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();
|
||||||
|
|
||||||
&mut *page_table_ptr // unsafe
|
unsafe { &mut *page_table_ptr }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A FrameAllocator that always returns `None`.
|
/// A FrameAllocator that always returns `None`.
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
use blog_os::println;
|
use blog_os::println;
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
#[no_mangle] // don't mangle the name of this function
|
#[unsafe(no_mangle)] // don't mangle the name of this function
|
||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
test_main();
|
test_main();
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ 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::{entry_point, BootInfo};
|
use bootloader::{BootInfo, entry_point};
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
entry_point!(main);
|
entry_point!(main);
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
use blog_os::{exit_qemu, serial_print, serial_println, QemuExitCode};
|
use blog_os::{QemuExitCode, exit_qemu, serial_print, serial_println};
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
#[no_mangle]
|
#[unsafe(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]");
|
||||||
|
|||||||
@@ -2,12 +2,12 @@
|
|||||||
#![no_main]
|
#![no_main]
|
||||||
#![feature(abi_x86_interrupt)]
|
#![feature(abi_x86_interrupt)]
|
||||||
|
|
||||||
use blog_os::{exit_qemu, serial_print, serial_println, QemuExitCode};
|
use blog_os::{QemuExitCode, exit_qemu, serial_print, serial_println};
|
||||||
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};
|
||||||
|
|
||||||
#[no_mangle]
|
#[unsafe(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");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user