Skip to content

Commit

Permalink
Kernel: Atomic frame allocator
Browse files Browse the repository at this point in the history
Make the frame allocator atomic. This allows allocating frames from
multiple CPU cores without having to go through a global lock.
  • Loading branch information
roblabla committed Feb 20, 2019
1 parent 9ebd44f commit cb06523
Show file tree
Hide file tree
Showing 13 changed files with 1,001 additions and 754 deletions.
2 changes: 1 addition & 1 deletion kernel/src/arch/i386/gdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::arch::i386::instructions::segmentation::*;

use crate::paging::PAGE_SIZE;
use crate::paging::{MappingAccessRights, kernel_memory::get_kernel_memory};
use crate::frame_allocator::{FrameAllocator, FrameAllocatorTrait};
use crate::frame_allocator::FrameAllocator;
use crate::mem::VirtualAddress;
use alloc::vec::Vec;
use crate::utils::align_up;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/arch/i386/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use ::core::mem::size_of;
use crate::paging::lands::{VirtualSpaceLand, UserLand, KernelLand};
use crate::paging::{PAGE_SIZE, process_memory::QueryMemory, MappingAccessRights, PageState, kernel_memory::get_kernel_memory};
use crate::frame_allocator::{FrameAllocator, FrameAllocatorTrait};
use crate::frame_allocator::FrameAllocator;
use crate::mem::VirtualAddress;
use crate::error::KernelError;
use xmas_elf::ElfFile;
Expand Down
711 changes: 0 additions & 711 deletions kernel/src/frame_allocator/i386.rs

This file was deleted.

737 changes: 707 additions & 30 deletions kernel/src/frame_allocator/mod.rs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions kernel/src/frame_allocator/physical_mem_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! A [PhysicalMemRegion] is a span of consecutive physical frames.

use super::{FrameAllocator, FrameAllocatorTraitPrivate};
use super::FrameAllocator;
use crate::paging::PAGE_SIZE;
use crate::mem::PhysicalAddress;
use crate::utils::{align_down, div_ceil, check_aligned, Splittable};
Expand Down Expand Up @@ -209,11 +209,12 @@ impl Splittable for Vec<PhysicalMemRegion> {

#[cfg(test)]
mod test {
use super::super::{FrameAllocator, FrameAllocatorTrait};
use super::super::{FrameAllocator, FRAME_ALLOCATOR};
use super::{PhysicalMemRegion, PhysicalMemRegionIter};
use crate::utils::Splittable;
use crate::mem::PhysicalAddress;
use crate::paging::PAGE_SIZE;
use core::sync::atomic::Ordering;

#[test]
#[should_panic]
Expand All @@ -226,8 +227,7 @@ mod test {
fn on_fixed_mmio_rounds_unaligned() {
let _f = crate::frame_allocator::init();
// reserve them so we don't panic
crate::frame_allocator::mark_frame_bootstrap_allocated(PhysicalAddress(0));
crate::frame_allocator::mark_frame_bootstrap_allocated(PhysicalAddress(PAGE_SIZE));
FRAME_ALLOCATOR.mark_area_reserved(0, 0x1FFF);

let region = unsafe { PhysicalMemRegion::on_fixed_mmio(PhysicalAddress(0x00000007), PAGE_SIZE + 1) };
assert_eq!(region.start_addr, 0);
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/heap_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::ops::Deref;
use core::ptr::NonNull;
use linked_list_allocator::{Heap, align_up};
use crate::paging::{PAGE_SIZE, MappingAccessRights, kernel_memory::get_kernel_memory};
use crate::frame_allocator::{FrameAllocator, FrameAllocatorTrait};
use crate::frame_allocator::FrameAllocator;
use crate::mem::VirtualAddress;

/// Simple wrapper around linked_list_allocator, growing heap by allocating pages
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! Currently doesn't do much, besides booting and printing Hello World on the
//! screen. But hey, that's a start.

#![feature(lang_items, start, asm, global_asm, compiler_builtins_lib, naked_functions, core_intrinsics, const_fn, abi_x86_interrupt, allocator_api, alloc, box_syntax, no_more_cas, const_vec_new, range_contains, step_trait, thread_local, nll)]
#![feature(lang_items, start, asm, global_asm, compiler_builtins_lib, naked_functions, core_intrinsics, const_fn, abi_x86_interrupt, allocator_api, alloc, box_syntax, no_more_cas, const_vec_new, range_contains, step_trait, thread_local, nll, untagged_unions, maybe_uninit, const_fn_union)]
#![no_std]
#![cfg_attr(target_os = "none", no_main)]
#![recursion_limit = "1024"]
Expand Down
6 changes: 6 additions & 0 deletions kernel/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ impl VirtualAddress {

/// Rounds up PAGE_SIZE.
pub fn ceil(self) -> VirtualAddress { VirtualAddress(round_to_page_upper(self.0)) }

/// Wrapping (modular) addition. Computes self + rhs, wrapping around at the boundary of the type.
pub fn wrapping_add(self, rhs: usize) -> VirtualAddress { VirtualAddress(self.0.wrapping_add(rhs)) }

/// Wrapping (modular) substraction. Computes self - rhs, wrapping around at the boundary of the type.
pub fn wrapping_sub(self, rhs: usize) -> VirtualAddress { VirtualAddress(self.0.wrapping_sub(rhs)) }
}

impl core::iter::Step for PhysicalAddress {
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/paging/arch/i386/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::super::super::lands::{KernelLand, UserLand, VirtualSpaceLand};
use super::super::super::kernel_memory::get_kernel_memory;
use super::super::super::MappingAccessRights;
use crate::mem::{VirtualAddress, PhysicalAddress};
use crate::frame_allocator::{PhysicalMemRegion, FrameAllocator, FrameAllocatorTrait};
use crate::frame_allocator::{PhysicalMemRegion, FrameAllocator};
use core::fmt::{Debug, Formatter, Error};

/// When paging is on, accessing this address loops back to the directory itself thanks to
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/paging/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ mod test {
use super::MappingType;
use crate::mem::{VirtualAddress, PhysicalAddress};
use crate::paging::PAGE_SIZE;
use crate::frame_allocator::{PhysicalMemRegion, FrameAllocator, FrameAllocatorTrait};
use crate::frame_allocator::{PhysicalMemRegion, FrameAllocator};
use std::sync::Arc;
use std::vec::Vec;
use crate::utils::Splittable;
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/paging/process_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use super::cross_process::CrossProcessMapping;
use super::error::MmError;
use super::MappingAccessRights;
use crate::mem::{VirtualAddress, PhysicalAddress};
use crate::frame_allocator::{FrameAllocator, FrameAllocatorTrait, PhysicalMemRegion};
use crate::frame_allocator::{FrameAllocator, PhysicalMemRegion};
use crate::paging::arch::Entry;
use crate::error::KernelError;
use crate::utils::{check_aligned, check_nonzero_length};
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::mem::{VirtualAddress, PhysicalAddress};
use crate::mem::{UserSpacePtr, UserSpacePtrMut};
use crate::paging::{MappingAccessRights, mapping::MappingType};
use crate::frame_allocator::{PhysicalMemRegion, FrameAllocator, FrameAllocatorTrait};
use crate::frame_allocator::{PhysicalMemRegion, FrameAllocator};
use crate::process::{Handle, ThreadStruct, ProcessStruct};
use crate::event::{self, Waitable};
use crate::scheduler::{self, get_current_thread, get_current_process};
Expand Down
Loading

0 comments on commit cb06523

Please sign in to comment.