From e20135575c6392dfbf43e117cafee03e00ae8f0c Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 9 Jan 2017 20:35:54 -0700 Subject: [PATCH] Refactor to move alloc_kernel in tree, and move io into syscall --- alloc_kernel/Cargo.toml | 8 +++++ alloc_kernel/src/lib.rs | 62 ++++++++++++++++++++++++++++++++ arch/arm/Cargo.toml | 2 +- arch/arm/src/lib.rs | 2 +- arch/x86_64/Cargo.toml | 5 ++- arch/x86_64/src/device/rtc.rs | 2 +- arch/x86_64/src/device/serial.rs | 2 +- arch/x86_64/src/lib.rs | 4 +-- arch/x86_64/src/stop.rs | 2 +- 9 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 alloc_kernel/Cargo.toml create mode 100644 alloc_kernel/src/lib.rs diff --git a/alloc_kernel/Cargo.toml b/alloc_kernel/Cargo.toml new file mode 100644 index 0000000..c3fa74d --- /dev/null +++ b/alloc_kernel/Cargo.toml @@ -0,0 +1,8 @@ +[package] +authors = ["Philipp Oppermann "] +name = "alloc_kernel" +version = "0.1.0" + +[dependencies] +linked_list_allocator = { git = "https://github.com/phil-opp/linked-list-allocator.git" } +spin = "*" diff --git a/alloc_kernel/src/lib.rs b/alloc_kernel/src/lib.rs new file mode 100644 index 0000000..df90843 --- /dev/null +++ b/alloc_kernel/src/lib.rs @@ -0,0 +1,62 @@ +#![feature(allocator)] +#![feature(const_fn)] + +#![allocator] +#![no_std] + +use spin::Mutex; +use linked_list_allocator::Heap; + +extern crate spin; +extern crate linked_list_allocator; + +static HEAP: Mutex> = Mutex::new(None); + +pub unsafe fn init(offset: usize, size: usize) { + *HEAP.lock() = Some(Heap::new(offset, size)); +} + +#[no_mangle] +pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 { + if let Some(ref mut heap) = *HEAP.lock() { + heap.allocate_first_fit(size, align).expect("out of memory") + } else { + panic!("__rust_allocate: heap not initialized"); + } +} + +#[no_mangle] +pub extern fn __rust_deallocate(ptr: *mut u8, size: usize, align: usize) { + if let Some(ref mut heap) = *HEAP.lock() { + unsafe { heap.deallocate(ptr, size, align) }; + } else { + panic!("__rust_deallocate: heap not initialized"); + } +} + +#[no_mangle] +pub extern fn __rust_usable_size(size: usize, _align: usize) -> usize { + size +} + +#[no_mangle] +pub extern fn __rust_reallocate_inplace(_ptr: *mut u8, size: usize, + _new_size: usize, _align: usize) -> usize +{ + size +} + +#[no_mangle] +pub extern fn __rust_reallocate(ptr: *mut u8, size: usize, new_size: usize, + align: usize) -> *mut u8 { + use core::{ptr, cmp}; + + // from: https://github.com/rust-lang/rust/blob/ + // c66d2380a810c9a2b3dbb4f93a830b101ee49cc2/ + // src/liballoc_system/lib.rs#L98-L101 + + let new_ptr = __rust_allocate(new_size, align); + unsafe { ptr::copy(ptr, new_ptr, cmp::min(size, new_size)) }; + __rust_deallocate(ptr, size, align); + new_ptr +} diff --git a/arch/arm/Cargo.toml b/arch/arm/Cargo.toml index d477d18..8a7e5cb 100644 --- a/arch/arm/Cargo.toml +++ b/arch/arm/Cargo.toml @@ -3,6 +3,6 @@ name = "arch_arm" version = "0.1.0" [dependencies] +alloc_kernel = { path = "../../alloc_kernel" } bitflags = "*" -hole_list_allocator = { path = "../../../crates/hole_list_allocator"} spin = "*" diff --git a/arch/arm/src/lib.rs b/arch/arm/src/lib.rs index 77b0e9c..60a1471 100644 --- a/arch/arm/src/lib.rs +++ b/arch/arm/src/lib.rs @@ -5,7 +5,7 @@ #![feature(naked_functions)] #![no_std] -extern crate hole_list_allocator as allocator; +extern crate alloc_kernel as allocator; #[macro_use] extern crate bitflags; extern crate spin; diff --git a/arch/x86_64/Cargo.toml b/arch/x86_64/Cargo.toml index a255e4d..bda357c 100644 --- a/arch/x86_64/Cargo.toml +++ b/arch/x86_64/Cargo.toml @@ -3,12 +3,11 @@ name = "arch_x86_64" version = "0.1.0" [dependencies] +alloc_kernel = { path = "../../alloc_kernel/" } bitflags = "*" -hole_list_allocator = { path = "../../../crates/hole_list_allocator/" } -io = { path = "../../../crates/io/" } raw-cpuid = { git = "https://github.com/gz/rust-cpuid" } spin = "*" -redox_syscall = { path = "../../../syscall/" } +redox_syscall = { git = "https://github.com/redox-os/syscall" } [dependencies.x86] version = "0.7" diff --git a/arch/x86_64/src/device/rtc.rs b/arch/x86_64/src/device/rtc.rs index ef6de05..bfef1fc 100644 --- a/arch/x86_64/src/device/rtc.rs +++ b/arch/x86_64/src/device/rtc.rs @@ -1,4 +1,4 @@ -use io::{Io, Pio}; +use syscall::io::{Io, Pio}; use time; pub fn init() { diff --git a/arch/x86_64/src/device/serial.rs b/arch/x86_64/src/device/serial.rs index 521c04e..02037da 100644 --- a/arch/x86_64/src/device/serial.rs +++ b/arch/x86_64/src/device/serial.rs @@ -1,7 +1,7 @@ use core::fmt::{self, Write}; use spin::Mutex; -use io::{Io, Pio, ReadOnly}; +use syscall::io::{Io, Pio, ReadOnly}; pub static COM1: Mutex = Mutex::new(SerialPort::new(0x3F8)); pub static COM2: Mutex = Mutex::new(SerialPort::new(0x2F8)); diff --git a/arch/x86_64/src/lib.rs b/arch/x86_64/src/lib.rs index 954535f..1b0847e 100644 --- a/arch/x86_64/src/lib.rs +++ b/arch/x86_64/src/lib.rs @@ -12,11 +12,9 @@ #![feature(unique)] #![no_std] -extern crate hole_list_allocator as allocator; - +extern crate alloc_kernel as allocator; #[macro_use] extern crate bitflags; -extern crate io; extern crate spin; extern crate syscall; pub extern crate x86; diff --git a/arch/x86_64/src/stop.rs b/arch/x86_64/src/stop.rs index 5af92ae..241bdf9 100644 --- a/arch/x86_64/src/stop.rs +++ b/arch/x86_64/src/stop.rs @@ -1,4 +1,4 @@ -use io::{Io, Pio}; +use syscall::io::{Io, Pio}; #[no_mangle] pub unsafe extern fn kstop() -> ! {