Skip to content

Commit

Permalink
Merge pull request #103 from hawkw/paging-dev
Browse files Browse the repository at this point in the history
feat(x86_64::paging): Implement paging & kernel remap
  • Loading branch information
hawkw authored May 28, 2017
2 parents f934519 + 4bd062f commit 929dd83
Show file tree
Hide file tree
Showing 63 changed files with 4,488 additions and 1,364 deletions.
11 changes: 4 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,21 @@ trace = []

[dependencies]
rlibc = "0.1.4"
spin = "0.3.5"
spin = "0.4.5"
once = "0.3.2"
bitflags = "0.7"
cpu = { path = "cpu" }
memory = { path = "memory" }
util = { path = "util" }
elf = { path = "elf" }
paging = { path = "paging" }
params = { path = "params" }

[dependencies.log]
version = "0.3.6"
default-features = false
features = ["release_max_level_info"]

# [dependencies.custom_derive]
# git = "https://github.com/DanielKeep/rust-custom-derive.git"
# version = "0.1.6"
# default-features = false

[dependencies.lazy_static]
version = "0.2.*"
features = ["spin_no_std"]
Expand All @@ -66,7 +63,7 @@ features = ["kinfo", "system_term"]

[dependencies.alloc]
path = "alloc"
features = ["buddy_as_system"]
features = ["buddy", "system", "borrow", "buddy_as_system"]

[dependencies.clippy]
version = "0.0.60"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ HELP_FUN = \
.PHONY: all clean kernel run iso cargo help gdb test doc release-iso release-run release-kernel

exception: $(iso) ##@build Run the kernel, dumping the state from QEMU if an exception occurs
@qemu-system-x86_64 -s -hda $(iso) -d int -no-reboot
@qemu-system-x86_64 -s -hda $(iso) -d int -no-reboot -serial file:$(CURDIR)/target/$(target)/serial-$(TIMESTAMP).log

doc: ##@utilities Make RustDoc documentation
@xargo doc
Expand Down
26 changes: 21 additions & 5 deletions alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ version = "0.1.0"
authors = ["Eliza Weisman <[email protected]>"]

[features]
default = ["buddy"]
default = ["buddy", "bump_ptr", "borrow"]
buddy = ["sos_intrusive"]
as_system = []
buddy_as_system = ["spin", "as_system"]
buddy_as_system = ["buddy", "once"]
system = []
bump_ptr = []
placement_in = ["system"]
borrow = []
first_fit = ["arrayvec"]
bench = []

[dependencies.log]
version = "0.3.6"
Expand All @@ -18,9 +23,20 @@ features = ["release_max_level_info"]
path = "../memory"

[dependencies.spin]
version = "0.3.5"
optional = true
version = "^0.4.5"

[dependencies.sos_intrusive]
path = "../sos_intrusive"
optional = true

[dependencies.arrayvec]
version = "0.3.16"
default-features = false
optional = true

[dependencies.once]
version = "^0.3.2"
optional = true

[dependencies.params]
path = "../params"
121 changes: 121 additions & 0 deletions alloc/src/borrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use super::{Address, Allocator, Layout};
use ptr::Unique;
use ops::{Deref, DerefMut};
use spin::Mutex;

pub trait Lender {
type Borrowed;
fn borrow(&self) -> Self::Borrowed;
}

/// A borrowed handle on a heap allocation with a specified lifetime.
///
/// This automatically deallocates the allocated object when the borrow's
/// lifetime ends. It also ensures that the borrow only lives as long as the
/// allocator that provided it, and that the borrow is dropped if the allocator
/// is dropped.
// TODO: can we replace this with one generic implementation that works for
// borrowed pointers, objects, and arrays? can that code be reused for
// BorrowedFrame/BorrowedFrameRange as well?
// - eliza, 02/21/2017
pub struct BorrowedPtr<'alloc, A>
where A: Allocator
, A: 'alloc {
ptr: Unique<u8>
, layout: Layout
, allocator: &'alloc Mutex<A>
}

impl<'alloc, A> BorrowedPtr<'alloc, A>
where A: Allocator
, A: 'alloc {

#[inline]
pub fn new( ptr: Address
, layout: Layout
, allocator: &'alloc Mutex<A>)
-> Self {
BorrowedPtr { ptr: unsafe { Unique::new(ptr) }
, layout: layout
, allocator: allocator
}

}
}

impl<'alloc, A> Deref for BorrowedPtr<'alloc, A>
where A: Allocator
, A: 'alloc {
type Target = Unique<u8>;
fn deref(&self) -> &Self::Target { &self.ptr }
}

impl<'alloc, A> Drop for BorrowedPtr<'alloc, A>
where A: Allocator
, A: 'alloc {
fn drop(&mut self) {
unsafe {
self.allocator.lock().dealloc(self.ptr.as_ptr(), self.layout.clone())
}
}
}

/// A borrowed handle on a heap allocation with a specified lifetime.
///
/// This automatically deallocates the allocated object when the borrow's
/// lifetime ends. It also ensures that the borrow only lives as long as the
/// allocator that provided it, and that the borrow is dropped if the allocator
/// is dropped.
pub struct Borrowed<'alloc, A, T>
where A: Allocator
, A: 'alloc {
value: Unique<T>
, allocator: &'alloc Mutex<A>
}

impl<'alloc, A, T> Borrowed<'alloc, A, T>
where A: Allocator
, A: 'alloc {

#[inline]
pub fn new( value: Unique<T>, allocator: &'alloc Mutex<A>)
-> Self {
Borrowed { value: value
, allocator: allocator
}

}
}

impl<'alloc, A, T> Deref for Borrowed<'alloc, A, T>
where A: Allocator
, A: 'alloc {
type Target = T;
fn deref(&self) -> &Self::Target { unsafe { self.value.as_ref() } }
}

impl<'alloc, A, T> DerefMut for Borrowed<'alloc, A, T>
where A: Allocator
, A: 'alloc {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.value.as_mut() }
}
}

impl<'alloc, A, T> Drop for Borrowed<'alloc, A, T>
where A: Allocator
, A: 'alloc {
fn drop(&mut self) {
use mem::drop;
let address = self.value.as_ptr() as Address;
// ensure we drop the object _before_ deallocating it, so that
// the object's destructor gets run first
// i hope this is correct...
drop(self.value.as_ptr());
unsafe {
self.allocator.lock()
.dealloc( address
, Layout::for_value(self.value.as_ref()))
}
}
}
50 changes: 50 additions & 0 deletions alloc/src/buddy/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ impl PowersOf2 for usize {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "bench")]
use test::{self, Bencher};

#[test]
fn test_next_pow2() {
Expand Down Expand Up @@ -118,4 +120,52 @@ mod tests {
assert_eq!(5, 32.log2());
assert_eq!(10, 1024.log2());
}

#[cfg(feature = "bench")]
#[bench]
fn our_next_pow2(b: &mut Bencher) {
use collections::Vec;
b.iter(|| {
let n = test::black_box(100_000);

(0..n).map(|x: usize| x.next_pow2())
.collect::<Vec<_>>() // force the map to return so it doesn't get optimised away
})
}

#[cfg(feature = "bench")]
#[bench]
fn std_next_power_of_two(b: &mut Bencher) {
use collections::Vec;
b.iter(|| {
let n = test::black_box(100_000);

(0..n).map(|x: usize| x.next_power_of_two())
.collect::<Vec<_>>() // force the map to return so it doesn't get optimised away
})
}

#[cfg(feature = "bench")]
#[bench]
fn our_is_pow2(b: &mut Bencher) {
use collections::Vec;
b.iter(|| {
let n = test::black_box(100_000);

(0..n).map(|ref x: usize| x.is_pow2())
.collect::<Vec<_>>() // force the map to return so it doesn't get optimised away
})
}

#[cfg(feature = "bench")]
#[bench]
fn std_is_power_of_two(b: &mut Bencher) {
use collections::Vec;
b.iter(|| {
let n = test::black_box(100_000);

(0..n).map(|x: usize| x.next_power_of_two())
.collect::<Vec<_>>() // force the map to return so it doesn't get optimised away
})
}
}
Loading

0 comments on commit 929dd83

Please sign in to comment.