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

Fix issues with process alloc #327

Merged
merged 7 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 4 additions & 5 deletions src/sys/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn alloc_pages(addr: u64, size: u64) {
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE | PageTableFlags::USER_ACCESSIBLE;
let pages = {
let start_page = Page::containing_address(VirtAddr::new(addr));
let end_page = Page::containing_address(VirtAddr::new(addr + size));
let end_page = Page::containing_address(VirtAddr::new(addr + size - 1));
Page::range_inclusive(start_page, end_page)
};
for page in pages {
Expand All @@ -71,7 +71,7 @@ pub fn free_pages(addr: u64, size: u64) {
let mut mapper = unsafe { sys::mem::mapper(VirtAddr::new(sys::mem::PHYS_MEM_OFFSET)) };
let pages: PageRangeInclusive<Size4KiB> = {
let start_page = Page::containing_address(VirtAddr::new(addr));
let end_page = Page::containing_address(VirtAddr::new(addr + size));
let end_page = Page::containing_address(VirtAddr::new(addr + size - 1));
Page::range_inclusive(start_page, end_page)
};
for page in pages {
Expand Down Expand Up @@ -109,9 +109,8 @@ impl PhysBuf {
}
}

fn phys_addr(ptr: &u8) -> u64 {
let rx_ptr = ptr as *const u8;
let virt_addr = VirtAddr::new(rx_ptr as u64);
pub fn phys_addr(ptr: *const u8) -> u64 {
let virt_addr = VirtAddr::new(ptr as u64);
let phys_addr = sys::mem::virt_to_phys(virt_addr).unwrap();
phys_addr.as_u64()
}
Expand Down
16 changes: 9 additions & 7 deletions src/sys/mem.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::sys;
use bootloader::bootinfo::{BootInfo, MemoryMap, MemoryRegionType};
use core::sync::atomic::{AtomicU64, Ordering};
use core::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use x86_64::instructions::interrupts;
use x86_64::structures::paging::{FrameAllocator, OffsetPageTable, PageTable, PhysFrame, Size4KiB, Translate};
use x86_64::{PhysAddr, VirtAddr};
Expand All @@ -10,14 +10,16 @@ pub static mut PHYS_MEM_OFFSET: u64 = 0;
pub static mut MEMORY_MAP: Option<&MemoryMap> = None;
pub static MEMORY_SIZE: AtomicU64 = AtomicU64::new(0);

static ALLOCATED_FRAMES: AtomicUsize = AtomicUsize::new(0);

pub fn init(boot_info: &'static BootInfo) {
interrupts::without_interrupts(|| {
let mut memory_size = 0;
for region in boot_info.memory_map.iter() {
let start_addr = region.range.start_addr();
let end_addr = region.range.end_addr();
memory_size += end_addr - start_addr;
log!("MEM [{:#016X}-{:#016X}] {:?}\n", start_addr, end_addr, region.region_type);
log!("MEM [{:#016X}-{:#016X}] {:?}\n", start_addr, end_addr - 1, region.region_type);
}
log!("MEM {} KB\n", memory_size >> 10);
MEMORY_SIZE.store(memory_size, Ordering::Relaxed);
Expand Down Expand Up @@ -63,13 +65,12 @@ unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut
}

pub struct BootInfoFrameAllocator {
memory_map: &'static MemoryMap,
next: usize,
memory_map: &'static MemoryMap
}

impl BootInfoFrameAllocator {
pub unsafe fn init(memory_map: &'static MemoryMap) -> Self {
BootInfoFrameAllocator { memory_map, next: 0 }
BootInfoFrameAllocator { memory_map }
}

fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> {
Expand All @@ -83,10 +84,11 @@ impl BootInfoFrameAllocator {

unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
fn allocate_frame(&mut self) -> Option<PhysFrame> {
let next = ALLOCATED_FRAMES.fetch_add(1, Ordering::SeqCst);

// FIXME: creating an iterator for each allocation is very slow if
// the heap is larger than a few megabytes.
let frame = self.usable_frames().nth(self.next);
self.next += 1;
let frame = self.usable_frames().nth(next);
frame
}
}
5 changes: 3 additions & 2 deletions src/sys/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ use crate::sys::gdt::GDT;
use core::sync::atomic::AtomicU64;
use x86_64::VirtAddr;

static CODE_ADDR: AtomicU64 = AtomicU64::new((sys::allocator::HEAP_START as u64) + (16 << 20)); // 16 MB
static CODE_ADDR: AtomicU64 = AtomicU64::new((sys::allocator::HEAP_START as u64) + (16 << 20));
const PAGE_SIZE: u64 = 4 * 1024;

#[repr(align(8), C)]
Expand Down Expand Up @@ -253,8 +253,9 @@ impl Process {
}

fn create(bin: &[u8]) -> Result<usize, ()> {
let code_size = 1024 * PAGE_SIZE;
let code_size = 1 * PAGE_SIZE;
let code_addr = CODE_ADDR.fetch_add(code_size, Ordering::SeqCst);

sys::allocator::alloc_pages(code_addr, code_size);

let mut entry_point = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/usr/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub fn exec(cmd: &str) -> ExitCode {

let res = match args[0] {
"" => ExitCode::CommandSuccessful,
"a" | "alias" => ExitCode::CommandUnknown,
"a" => ExitCode::CommandUnknown,
"b" => ExitCode::CommandUnknown,
"c" | "copy" => usr::copy::main(&args),
"d" | "del" | "delete" => usr::delete::main(&args),
Expand All @@ -222,8 +222,8 @@ pub fn exec(cmd: &str) -> ExitCode {
"g" | "go" | "goto" => change_dir(&args),
"h" | "help" => usr::help::main(&args),
"i" => ExitCode::CommandUnknown,
"j" | "jump" => ExitCode::CommandUnknown,
"k" | "kill" => ExitCode::CommandUnknown,
"j" => ExitCode::CommandUnknown,
"k" => ExitCode::CommandUnknown,
"l" | "list" => usr::list::main(&args),
"m" | "move" => usr::r#move::main(&args),
"n" => ExitCode::CommandUnknown,
Expand Down