Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging branches #27

Merged
merged 22 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5d5496c
Merge pull request #20 from orust-org/17-create-a-general-docs-folder
Blindspot22 Jul 30, 2024
c352117
Merge pull request #21 from orust-org/main
Blindspot22 Jul 30, 2024
2d3da49
Merge pull request #22 from orust-org/Paging
Blindspot22 Jul 30, 2024
fabff7c
Update README.md
Blindspot22 Aug 5, 2024
0b8d0f9
Update Installation Manual
Blindspot22 Aug 5, 2024
927f04c
bootinfo frame allocator - basic implementation
Blindspot22 Aug 5, 2024
6754286
adding a dummy allocator and testing the error handler
Blindspot22 Aug 5, 2024
822ec41
creating a kernel heap
Blindspot22 Aug 5, 2024
1650b93
added heap initialization
Blindspot22 Aug 5, 2024
b799247
using the linked list to test out our frame allocator
Blindspot22 Aug 5, 2024
b753ca5
test for implementation of heap allocator
Blindspot22 Aug 5, 2024
ce2d926
Merge pull request #23 from orust-org/heap-allocation
Blindspot22 Aug 5, 2024
2978245
Update Installation-Manual.md
Blindspot22 Aug 6, 2024
f6e7d2b
Update Installation-Manual.md
Blindspot22 Aug 6, 2024
5b45c57
Update Installation-Manual.md
Blindspot22 Aug 6, 2024
435b0c1
Update Installation-Manual.md
Blindspot22 Aug 6, 2024
73a53c9
Update Installation-Manual.md
Blindspot22 Aug 6, 2024
453bbc6
updated tests for heap allocation
Blindspot22 Aug 6, 2024
2f9b656
updated test skeleton and heap allocation
Blindspot22 Aug 6, 2024
7161a75
Update Installation-Manual.md
Blindspot22 Aug 6, 2024
76015da
updated test skeleton and heap allocation
Blindspot22 Aug 6, 2024
57be56e
Merge pull request #26 from orust-org/heap-allocation
Blindspot22 Aug 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
41 changes: 41 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
53 changes: 36 additions & 17 deletions Installation-Manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ 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.
1. Add llvm-tools
```sh
cargo install bootimage
rustup component add llvm-tools-preview
```
Add llvm-tools
2. Install the bootimage.
```sh
rustup component add llvm-tools-preview
cargo install bootimage
```
Build and Run orust:
#### Build and Run orust:

```sh
cargo build
Expand All @@ -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-system-x86_64
sudo apt install qemu qemu-kvm
```
#### Verify the installation:
```sh
Expand All @@ -87,15 +106,15 @@ qemu-system-x86_64 --version
#### Run orust
Navigate into the orust directory through your terminal or IDE:

Install the bootimage.
1. Add llvm-tools
```sh
cargo install bootimage
rustup component add llvm-tools-preview
```
Add llvm-tools
2. Install the bootimage.
```sh
rustup component add llvm-tools-preview
cargo install bootimage
```
Build and Run orust:
#### Build and Run orust:

```sh
cargo build
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
54 changes: 54 additions & 0 deletions src/allocator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use x86_64::{
structures::paging::{
mapper::MapToError, FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB,
},
VirtAddr,
};
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: LockedHeap = LockedHeap::empty();
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")
}
}

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(())
}
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) -> ();
Expand Down Expand Up @@ -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)
}
44 changes: 30 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,55 @@
#![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, vec, vec::Vec, rc::Rc};

pub trait Testable {
fn run(&self);
}

entry_point!(kernel_main);

fn kernel_main(boot_info: &'static BootInfo) -> ! {
use orust_os:: memory;
use x86_64::{structures::paging::Translate, VirtAddr};
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 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 {
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 {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();
Expand Down
Loading
Loading