From fabff7c403701e2f245c03bb93ce04f2f4721251 Mon Sep 17 00:00:00 2001 From: Enow Scott <148756598+Blindspot22@users.noreply.github.com> Date: Mon, 5 Aug 2024 08:47:23 +0100 Subject: [PATCH 01/17] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 5ce2c41..abd21dc 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,4 @@ This project, with exception of the blog/content folder, is licensed under eithe at your option. -This repository contains some codes from the "Writing an OS in Rust" series of Philipp Oppermann. - This is my updated and ameliorated version of Philipp Oppermann's "BlogOS" implementation. From 0b8d0f99acbc43ccaf15c470401c1df44b1c47f8 Mon Sep 17 00:00:00 2001 From: Blindspot22 Date: Mon, 5 Aug 2024 09:16:02 +0100 Subject: [PATCH 02/17] Update Installation Manual --- Installation-Manual.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Installation-Manual.md b/Installation-Manual.md index 5538bab..099714f 100644 --- a/Installation-Manual.md +++ b/Installation-Manual.md @@ -78,7 +78,7 @@ Ensure to clone this repository first. #### Install QEMU: ```sh -apt install qemu-system-x86_64 +apt install qemu qemu-kvm ``` #### Verify the installation: ```sh From 927f04c046c809ca8642106e8bdbb7462d8e9398 Mon Sep 17 00:00:00 2001 From: Blindspot22 Date: Mon, 5 Aug 2024 10:46:41 +0100 Subject: [PATCH 03/17] bootinfo frame allocator - basic implementation --- src/main.rs | 19 +++++------------- src/memory.rs | 53 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/main.rs b/src/main.rs index fd7da45..81d6f83 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,25 +18,16 @@ entry_point!(kernel_main); fn kernel_main(boot_info: &'static BootInfo) -> ! { use orust_os:: memory; - use x86_64::{structures::paging::Translate, VirtAddr}; + use x86_64::VirtAddr; println!("Hello, Welcome to the ORUST Operating System{}", "!"); orust_os::init(); let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset); - let mapper = unsafe { memory::init(phys_mem_offset) }; - let addresses = [ - 0xb8000, - 0x201008, - 0x0100_0020_1a10, - boot_info.physical_memory_offset - ]; - - for &address in &addresses { - let virt = VirtAddr::new(address); - let phys = mapper.translate_addr(virt); - println!("{:?} -> {:?}", virt, phys); - } + let mut _mapper = unsafe { memory::init(phys_mem_offset) }; + let mut _frame_allocator = unsafe { + memory::BootInfoFrameAllocator::init(&boot_info.memory_map) + }; #[cfg(test)] test_main(); diff --git a/src/memory.rs b/src/memory.rs index 7bbc697..fa49fda 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -1,6 +1,7 @@ +use bootloader::bootinfo::{MemoryRegionType,MemoryMap}; use x86_64::{ structures::paging::{ - Page, PhysFrame, Mapper, Size4KiB, FrameAllocator, PageTable, OffsetPageTable + PhysFrame, Size4KiB, FrameAllocator, PageTable, OffsetPageTable }, PhysAddr, VirtAddr }; @@ -24,27 +25,41 @@ unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) &mut *page_table_ptr } -pub fn create_example_mapping( - page: Page, - mapper: &mut OffsetPageTable, - frame_allocator: &mut impl FrameAllocator, -) { - 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(); +// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Boot Info Allocator +// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +pub struct BootInfoFrameAllocator { + memory_map: &'static MemoryMap, + next: usize, } -pub struct EmptyFrameAllocator; +impl BootInfoFrameAllocator { + pub unsafe fn init(memory_map: &'static MemoryMap) -> Self { + BootInfoFrameAllocator { + memory_map, + next: 0, + } + } + fn usable_frames(&self) -> impl Iterator { + // 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 for EmptyFrameAllocator { +unsafe impl FrameAllocator for BootInfoFrameAllocator { fn allocate_frame(&mut self) -> Option { - None + let frame = self.usable_frames().nth(self.next); + self.next += 1; + frame } } \ No newline at end of file From 675428611d9ca5dbfde0b181cdd03a188296046a Mon Sep 17 00:00:00 2001 From: Blindspot22 Date: Mon, 5 Aug 2024 11:46:06 +0100 Subject: [PATCH 04/17] adding a dummy allocator and testing the error handler --- .cargo/config.toml | 2 +- src/allocator.rs | 16 ++++++++++++++++ src/lib.rs | 11 ++++++++++- src/main.rs | 8 +++++++- 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/allocator.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index 012dd79..69c88aa 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,6 +1,6 @@ [unstable] build-std-features = ["compiler-builtins-mem"] -build-std = ["core", "compiler_builtins"] +build-std = ["core", "compiler_builtins", "alloc"] [build] target = "x86_64-orust_os.json" diff --git a/src/allocator.rs b/src/allocator.rs new file mode 100644 index 0000000..7988dd3 --- /dev/null +++ b/src/allocator.rs @@ -0,0 +1,16 @@ +use alloc::alloc::{GlobalAlloc, Layout}; +use core::ptr::null_mut; + +#[global_allocator] +static ALLOCATOR: Dummy = Dummy; +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") + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 43bed83..8998104 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,17 +4,21 @@ #![test_runner(crate::test_runner)] #![reexport_test_harness_main = "test_main"] #![feature(abi_x86_interrupt)] +#![feature(alloc_error_handler)] #[cfg(test)] use bootloader::{entry_point, BootInfo}; +extern crate alloc; + pub mod serial; pub mod vga_buffer; pub mod interrupts; pub mod gdt; pub mod memory; +pub mod allocator; -use core::panic::PanicInfo; +use core::{alloc::Layout, panic::PanicInfo}; pub trait Testable { fn run(&self) -> (); @@ -90,4 +94,9 @@ pub fn hlt_loop() ->! { loop { x86_64::instructions::hlt(); } +} + +#[alloc_error_handler] +fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! { + panic!("allocation error: {:?}", layout) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 81d6f83..4dab87d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,12 +4,16 @@ #![test_runner(orust_os::test_runner)] #![reexport_test_harness_main = "test_main"] +extern crate alloc; + use bootloader::{BootInfo, entry_point}; use core::panic::PanicInfo; use orust_os::println; +use alloc::boxed::Box; + pub trait Testable { fn run(&self); } @@ -29,10 +33,12 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! { memory::BootInfoFrameAllocator::init(&boot_info.memory_map) }; + let x = Box::new(41); + #[cfg(test)] test_main(); - println!("It did not crash!"); + println!("It did not crash!{x}"); orust_os::hlt_loop(); } From 822ec41388805ddb9938e950a610c04101169cc2 Mon Sep 17 00:00:00 2001 From: Blindspot22 Date: Mon, 5 Aug 2024 12:16:13 +0100 Subject: [PATCH 05/17] creating a kernel heap --- src/allocator.rs | 34 ++++++++++++++++++++++++++++++++++ src/main.rs | 12 ++++++++---- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/allocator.rs b/src/allocator.rs index 7988dd3..ac03f89 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -1,6 +1,15 @@ +use x86_64::{ + structures::paging::{ + mapper::MapToError, FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB, + }, + VirtAddr, +}; use alloc::alloc::{GlobalAlloc, Layout}; use core::ptr::null_mut; +pub const HEAP_START: usize = 0x_4444_4444_0000; +pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB + #[global_allocator] static ALLOCATOR: Dummy = Dummy; pub struct Dummy; @@ -13,4 +22,29 @@ unsafe impl GlobalAlloc for Dummy { unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) { panic!("dealloc should be never called") } +} + +pub fn init_heap( + mapper: &mut impl Mapper, + frame_allocator: &mut impl FrameAllocator, +) -> Result<(), MapToError> { + 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() + }; + } + + Ok(()) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 4dab87d..7c58fc0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,18 +21,22 @@ pub trait Testable { entry_point!(kernel_main); fn kernel_main(boot_info: &'static BootInfo) -> ! { - use orust_os:: memory; + use orust_os::allocator; + use orust_os::memory::{self, BootInfoFrameAllocator}; use x86_64::VirtAddr; println!("Hello, Welcome to the ORUST Operating System{}", "!"); orust_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 { - memory::BootInfoFrameAllocator::init(&boot_info.memory_map) + 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"); + let x = Box::new(41); #[cfg(test)] From 1650b935ba0594eed6d1d8aef4535b2063c6a349 Mon Sep 17 00:00:00 2001 From: Blindspot22 Date: Mon, 5 Aug 2024 12:18:47 +0100 Subject: [PATCH 06/17] added heap initialization --- src/allocator.rs | 2 +- src/main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/allocator.rs b/src/allocator.rs index ac03f89..3c17244 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -45,6 +45,6 @@ pub fn init_heap( mapper.map_to(page, frame, flags, frame_allocator)?.flush() }; } - + Ok(()) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7c58fc0..30dfd3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! { #[cfg(test)] test_main(); - + println!("It did not crash!{x}"); orust_os::hlt_loop(); From b79924740f668cc362de655dd652fdf2fa1b50e1 Mon Sep 17 00:00:00 2001 From: Blindspot22 Date: Mon, 5 Aug 2024 12:54:54 +0100 Subject: [PATCH 07/17] using the linked list to test out our frame allocator --- Cargo.lock | 41 +++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/allocator.rs | 8 ++++++-- src/main.rs | 23 +++++++++++++++++++---- 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index acf2748..1d55531 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + [[package]] name = "bit_field" version = "0.10.2" @@ -35,12 +41,32 @@ dependencies = [ "spin", ] +[[package]] +name = "linked_list_allocator" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "549ce1740e46b291953c4340adcd74c59bcf4308f4cac050fd33ba91b7168f4a" +dependencies = [ + "spinning_top", +] + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "orust_os" version = "0.1.0" dependencies = [ "bootloader", "lazy_static", + "linked_list_allocator", "pc-keyboard", "pic8259", "spin", @@ -70,12 +96,27 @@ version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[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.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b9eb1a2f4c41445a3a0ff9abc5221c5fcd28e1f13cd7c0397706f9ac938ddb0" +dependencies = [ + "lock_api", +] + [[package]] name = "uart_16550" version = "0.2.19" diff --git a/Cargo.toml b/Cargo.toml index c2a5329..03877ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ x86_64 = "0.14.2" uart_16550 = "0.2.0" pic8259 = "0.10.1" pc-keyboard = "0.5.0" +linked_list_allocator = "0.9.0" [package.metadata.bootimage] test-args = [ diff --git a/src/allocator.rs b/src/allocator.rs index 3c17244..02bfc0a 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -6,12 +6,13 @@ use x86_64::{ }; use alloc::alloc::{GlobalAlloc, Layout}; use core::ptr::null_mut; +use linked_list_allocator::LockedHeap; pub const HEAP_START: usize = 0x_4444_4444_0000; pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB #[global_allocator] -static ALLOCATOR: Dummy = Dummy; +static ALLOCATOR: LockedHeap = LockedHeap::empty(); pub struct Dummy; unsafe impl GlobalAlloc for Dummy { @@ -45,6 +46,9 @@ pub fn init_heap( mapper.map_to(page, frame, flags, frame_allocator)?.flush() }; } - + unsafe { + ALLOCATOR.lock().init(HEAP_START, HEAP_SIZE); + } + Ok(()) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 30dfd3e..84b4152 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use core::panic::PanicInfo; use orust_os::println; -use alloc::boxed::Box; +use alloc::{boxed::Box, vec, vec::Vec, rc::Rc}; pub trait Testable { fn run(&self); @@ -37,12 +37,27 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! { allocator::init_heap(&mut mapper, &mut frame_allocator) .expect("heap initialization failed"); - let x = Box::new(41); + // allocate a number on the heap + let heap_value = Box::new(41); + println!("heap_value at {heap_value:p}"); + + let mut vec = Vec::new(); + for i in 0..500 { + vec.push(i); + } + println!("vec at {:p}", vec.as_slice()); + + 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)] test_main(); - - println!("It did not crash!{x}"); + + println!("It did not crash!"); orust_os::hlt_loop(); } From b753ca57332894d576895385aea7efdadf322cf5 Mon Sep 17 00:00:00 2001 From: Blindspot22 Date: Mon, 5 Aug 2024 18:09:58 +0100 Subject: [PATCH 08/17] test for implementation of heap allocator --- tests/heap_allocation.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/heap_allocation.rs diff --git a/tests/heap_allocation.rs b/tests/heap_allocation.rs new file mode 100644 index 0000000..450ee5a --- /dev/null +++ b/tests/heap_allocation.rs @@ -0,0 +1,17 @@ +#![no_std] +#![no_main] +#![feature(custom_test_frameworks)] +#![test_runner(orust_os::test_runner)] +#![reexport_test_harness_main = "test_main"] + +use core::panic::PanicInfo; + +#[no_mangle] +pub extern "C" fn _start() -> ! { + unimplemented!(); +} + +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + orust_os::test_panic_handler(info) +} From 2978245f29fd6f9aa9328592872d05591f0bd0cd Mon Sep 17 00:00:00 2001 From: Enow Scott <148756598+Blindspot22@users.noreply.github.com> Date: Tue, 6 Aug 2024 12:29:16 +0100 Subject: [PATCH 09/17] Update Installation-Manual.md --- Installation-Manual.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Installation-Manual.md b/Installation-Manual.md index 099714f..e5d539b 100644 --- a/Installation-Manual.md +++ b/Installation-Manual.md @@ -87,14 +87,14 @@ qemu-system-x86_64 --version #### Run orust Navigate into the orust directory through your terminal or IDE: -Install the bootimage. -```sh -cargo install bootimage -``` Add llvm-tools ```sh rustup component add llvm-tools-preview ``` +Install the bootimage. +```sh +cargo install bootimage +``` Build and Run orust: ```sh From f6e7d2b72b7e8f8ded3cf114e20bfa8fb83e316c Mon Sep 17 00:00:00 2001 From: Enow Scott <148756598+Blindspot22@users.noreply.github.com> Date: Tue, 6 Aug 2024 12:31:13 +0100 Subject: [PATCH 10/17] Update Installation-Manual.md --- Installation-Manual.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Installation-Manual.md b/Installation-Manual.md index e5d539b..ac90d6e 100644 --- a/Installation-Manual.md +++ b/Installation-Manual.md @@ -31,7 +31,7 @@ brew install qemu ```sh qemu-system-x86_64 --version ``` -#### Run orust +#### Run orust: Navigate into the orust directory through your terminal or IDE: Install the bootimage. @@ -87,15 +87,15 @@ qemu-system-x86_64 --version #### Run orust Navigate into the orust directory through your terminal or IDE: -Add llvm-tools +1. Add llvm-tools ```sh rustup component add llvm-tools-preview ``` -Install the bootimage. +2. Install the bootimage. ```sh cargo install bootimage ``` -Build and Run orust: +#### Build and Run orust: ```sh cargo build From 5b45c572347505a42539f0468dfb619bba03d880 Mon Sep 17 00:00:00 2001 From: Enow Scott <148756598+Blindspot22@users.noreply.github.com> Date: Tue, 6 Aug 2024 12:46:41 +0100 Subject: [PATCH 11/17] Update Installation-Manual.md --- Installation-Manual.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Installation-Manual.md b/Installation-Manual.md index ac90d6e..ceb1ce9 100644 --- a/Installation-Manual.md +++ b/Installation-Manual.md @@ -34,14 +34,14 @@ qemu-system-x86_64 --version #### Run orust: Navigate into the orust directory through your terminal or IDE: -Install the bootimage. -```sh -cargo install bootimage -``` Add llvm-tools ```sh rustup component add llvm-tools-preview ``` +Install the bootimage. +```sh +cargo install bootimage +``` Build and Run orust: ```sh From 435b0c190edc5e43819b71f24ef1596c9e9266d3 Mon Sep 17 00:00:00 2001 From: Enow Scott <148756598+Blindspot22@users.noreply.github.com> Date: Tue, 6 Aug 2024 12:48:34 +0100 Subject: [PATCH 12/17] Update Installation-Manual.md --- Installation-Manual.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Installation-Manual.md b/Installation-Manual.md index ceb1ce9..68f43b3 100644 --- a/Installation-Manual.md +++ b/Installation-Manual.md @@ -34,11 +34,11 @@ qemu-system-x86_64 --version #### Run orust: Navigate into the orust directory through your terminal or IDE: -Add llvm-tools +1. Add llvm-tools ```sh rustup component add llvm-tools-preview ``` -Install the bootimage. +2. Install the bootimage. ```sh cargo install bootimage ``` From 73a53c98b424a51674cf89ceac4ea2548a06be22 Mon Sep 17 00:00:00 2001 From: Enow Scott <148756598+Blindspot22@users.noreply.github.com> Date: Tue, 6 Aug 2024 12:53:27 +0100 Subject: [PATCH 13/17] Update Installation-Manual.md --- Installation-Manual.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Installation-Manual.md b/Installation-Manual.md index 68f43b3..9cad4dc 100644 --- a/Installation-Manual.md +++ b/Installation-Manual.md @@ -42,7 +42,7 @@ rustup component add llvm-tools-preview ```sh cargo install bootimage ``` -Build and Run orust: +#### Build and Run orust: ```sh cargo build From 453bbc60003c1006f7c79ec497da363a01930b7a Mon Sep 17 00:00:00 2001 From: Blindspot22 Date: Tue, 6 Aug 2024 15:14:00 +0100 Subject: [PATCH 14/17] updated tests for heap allocation --- tests/heap_allocation.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/heap_allocation.rs b/tests/heap_allocation.rs index 450ee5a..42ae3c2 100644 --- a/tests/heap_allocation.rs +++ b/tests/heap_allocation.rs @@ -4,10 +4,14 @@ #![test_runner(orust_os::test_runner)] #![reexport_test_harness_main = "test_main"] +use bootloader::{entry_point, BootInfo}; use core::panic::PanicInfo; +entry_point!(main); + #[no_mangle] -pub extern "C" fn _start() -> ! { + +pub extern "C" fn main(boot_info: &'static BootInfo) -> ! { unimplemented!(); } From 2f9b65641a00e8131f9f9e911ca3f6d91ef478e3 Mon Sep 17 00:00:00 2001 From: Blindspot22 Date: Tue, 6 Aug 2024 15:17:39 +0100 Subject: [PATCH 15/17] updated test skeleton and heap allocation --- tests/heap_allocation.rs | 5 +---- tests/test-skeleton.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/heap_allocation.rs b/tests/heap_allocation.rs index 42ae3c2..dfad163 100644 --- a/tests/heap_allocation.rs +++ b/tests/heap_allocation.rs @@ -8,10 +8,7 @@ use bootloader::{entry_point, BootInfo}; use core::panic::PanicInfo; entry_point!(main); - -#[no_mangle] - -pub extern "C" fn main(boot_info: &'static BootInfo) -> ! { +fn main(boot_info: &'static BootInfo) -> ! { unimplemented!(); } diff --git a/tests/test-skeleton.rs b/tests/test-skeleton.rs index a0e9179..dfad163 100644 --- a/tests/test-skeleton.rs +++ b/tests/test-skeleton.rs @@ -1,10 +1,14 @@ #![no_std] #![no_main] +#![feature(custom_test_frameworks)] +#![test_runner(orust_os::test_runner)] +#![reexport_test_harness_main = "test_main"] +use bootloader::{entry_point, BootInfo}; use core::panic::PanicInfo; -#[no_mangle] -pub extern "C" fn _start() -> ! { +entry_point!(main); +fn main(boot_info: &'static BootInfo) -> ! { unimplemented!(); } From 7161a75f48eba7e699d88a971274d67d9422ce04 Mon Sep 17 00:00:00 2001 From: Enow Scott <148756598+Blindspot22@users.noreply.github.com> Date: Tue, 6 Aug 2024 15:58:49 +0100 Subject: [PATCH 16/17] Update Installation-Manual.md --- Installation-Manual.md | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/Installation-Manual.md b/Installation-Manual.md index 9cad4dc..6862f77 100644 --- a/Installation-Manual.md +++ b/Installation-Manual.md @@ -59,26 +59,45 @@ Go to the [official QEMU website](https://www.qemu.org/download/#windows) and do Follow the instructions in the installation wizard to complete the setup. #### Add QEMU to your system PATH: - Right-click on "This PC" or "Computer" on the desktop or in File Explorer and select "Properties". + 1. Right-click on "This PC" or "Computer" on the desktop or in File Explorer and select "Properties". - Click on "Advanced system settings". + 2. Click on "Advanced system settings". - In the System Properties window, click on the "Environment Variables" button. + 3. In the System Properties window, click on the "Environment Variables" button. - In the Environment Variables window, under "System variables", find the "Path" variable and select it, then click "Edit". + 4. In the Environment Variables window, under "System variables", find the "Path" variable and select it, then click "Edit". - In the Edit Environment Variable window, click "New" and add the path to your QEMU installation (e.g., C:\Program Files\qemu). + 5. In the Edit Environment Variable window, click "New" and add the path to your QEMU installation (e.g., C:\Program Files\qemu). #### Verify the installation: Open a new Command Prompt or PowerShell and run: ```sh qemu-system-x86_64 --version ``` +#### Run orust +Navigate into the orust folder through your IDE: + +1. Add llvm-tools +```sh +rustup component add llvm-tools-preview +``` +2. Install the bootimage. +```sh +cargo install bootimage +``` +#### Build and Run orust: + +```sh +cargo build +``` +```sh +cargo run +``` # Linux(Ubuntu/Debian) Ensure to clone this repository first. #### Install QEMU: ```sh -apt install qemu qemu-kvm +sudo apt install qemu qemu-kvm ``` #### Verify the installation: ```sh From 76015dae31dfa9e14b5344bade46c99d2ff5eb26 Mon Sep 17 00:00:00 2001 From: Blindspot22 Date: Tue, 6 Aug 2024 16:13:04 +0100 Subject: [PATCH 17/17] updated test skeleton and heap allocation --- tests/heap_allocation.rs | 47 +++++++++++++++++++++++++++++++++++++++- tests/test-skeleton.rs | 2 +- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/tests/heap_allocation.rs b/tests/heap_allocation.rs index dfad163..8e8f6c7 100644 --- a/tests/heap_allocation.rs +++ b/tests/heap_allocation.rs @@ -4,15 +4,60 @@ #![test_runner(orust_os::test_runner)] #![reexport_test_harness_main = "test_main"] +extern crate alloc; use bootloader::{entry_point, BootInfo}; use core::panic::PanicInfo; +use alloc::boxed::Box; +use alloc::vec::Vec; +use orust_os::allocator::HEAP_SIZE; + entry_point!(main); fn main(boot_info: &'static BootInfo) -> ! { - unimplemented!(); + use orust_os::allocator; + use orust_os::memory::{self, BootInfoFrameAllocator}; + use x86_64::VirtAddr; + + orust_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 {} } #[panic_handler] fn panic(info: &PanicInfo) -> ! { orust_os::test_panic_handler(info) } + +#[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::(), (n - 1) * n / 2); +} + +#[test_case] +fn many_boxes() { + for i in 0..HEAP_SIZE { + let x = Box::new(i); + assert_eq!(*x, i); + } +} diff --git a/tests/test-skeleton.rs b/tests/test-skeleton.rs index dfad163..c4151c3 100644 --- a/tests/test-skeleton.rs +++ b/tests/test-skeleton.rs @@ -8,7 +8,7 @@ use bootloader::{entry_point, BootInfo}; use core::panic::PanicInfo; entry_point!(main); -fn main(boot_info: &'static BootInfo) -> ! { +fn main(_boot_info: &'static BootInfo) -> ! { unimplemented!(); }