Skip to content

Commit

Permalink
merged memory management into master
Browse files Browse the repository at this point in the history
  • Loading branch information
kanetkarster committed Feb 12, 2016
2 parents ba477e6 + d9d359b commit f969155
Show file tree
Hide file tree
Showing 26 changed files with 774 additions and 157 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ target

# OS junk
**/.DS_Store

# Ctags file
tags
6 changes: 0 additions & 6 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ version = "0.1.0"
authors = ["Carl Patenaude-Poulin <carl.patenaudepoulin@mail.mcgill.ca>", "Scott Cooper <scott.cooper2@mail.mcgill.ca"]

[dependencies]
rlibc = "0.1"
rlibc = "*"

[dependencies.mm]
path = "./mm"

[dependencies.alloc]
git = "https://github.com/phil-opp/nightly-liballoc.git"

6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

SHELL = /bin/bash

PROFILE ?= debug
ARCH ?= x86_64
TARGET_TRIPLE ?= x86_64-unknown-xen
Expand All @@ -19,7 +21,7 @@ clean:

# Modules (include is order-sensitive)
include $(BUILD)/malloc.mk
include $(BUILD)/Rust.mk
include $(BUILD)/Boot.mk
include $(BUILD)/libcore.mk
include $(BUILD)/Runtime.mk
include $(BUILD)/Rust.mk
include $(BUILD)/Debug.mk
2 changes: 1 addition & 1 deletion build/Boot.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $(DEPS)/$(BIN)/boot.o.d: $(ASM_FILES)
$(ECHO) "$(BIN)/boot.o: $(OBJ_FILES)" > $@
-include $(DEPS)/$(BIN)/boot.o.d

$(BIN)/boot.o:
$(BIN)/boot.o: $(OBJ_FILES)
$(MKDIR) $(@D)
$(ECHO) Bundling $(OBJ_FILES)...
$(LD) --relocatable -o $@ $(OBJ_FILES)
29 changes: 9 additions & 20 deletions build/Debug.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,27 @@ include build/Utils.mk

# I tried using libvirt instead of xl but I'm getting "error: libxenlight state driver is not active"
XL = sudo xl
JQ = jq --raw-output --compact-output

_DOM = $(XL) list -l | $(JQ) 'map(select(.config.c_info.name == "$(DOMAIN_NAME)")) | .[0] // empty'
DOM = $(shell $(_DOM))

_DOM_ID = $(_DOM) | $(JQ) '.domid'
DOM_ID = $(shell $(_DOM_ID))

DOM_RUNNING = $(DOM)
DOM_ID = $$($(XL) domid $(DOMAIN_NAME) 2> /dev/null)

.PHONY: dom_create
dom_create: $(TARGET)/crust crust.cfg var_DOMAIN_NAME
$(if $(DOM_RUNNING),,$(XL) create -p crust.cfg 'name="$(DOMAIN_NAME)"' 'kernel="$(TARGET)/crust"')
dom_create: $(TARGET)/crust crust.cfg
$(XL) create -p crust.cfg 'name="$(DOMAIN_NAME)"' 'kernel="$(TARGET)/crust"'

.PHONY: dom_destroy
dom_destroy: var_DOMAIN_NAME
$(if $(DOM_RUNNING),$(XL) destroy $(DOM_ID))
$(XL) destroy $(DOM_ID)

.PHONY: dom_start
dom_start: dom_create
$(XL) unpause $(DOM_ID)

.PHONY: dom_console
dom_console: dom_start
@echo Starting console - use C-] to exit
$(XL) console $(DOM_ID)

.PHONY: dom_%
dom_%: var_DOMAIN_NAME
$(XL) % $(DOM_ID)
dom_console: dom_create
(sleep 0.1; $(XL) unpause $(DOM_ID)) &
echo -e '\e[32mStarting console - use C-] to exit\e[0m'; $(XL) console $(DOM_ID)

clean: $(if $(DOMAIN_NAME),dom_destroy)
clean: $(if $(shell $(DOM_ID)),dom_destroy)

GDBSX_PROC = $(shell pgrep --list-full 'gdbsx' | grep "gdbsx -a $(DOM_ID)")
GDBSX_PID = $(firstword $(GDBSX_PROC))
Expand All @@ -51,4 +40,4 @@ gdbsx_stop:
gdb: gdbsx_start
gdb -ex "target remote localhost:$(GDBSX_PROC_PORT)"

clean: $(if $(DOM_ID),gdbsx_stop)
clean: $(if $(GDBSX_PROC),gdbsx_stop)
28 changes: 28 additions & 0 deletions build/Runtime.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Patch and recompile libcore to remove float support

RUSTLIB_TMP = $(TMP)/rustlib
RUSTLIB ?= $(HOME)/.multirust/toolchains/nightly/lib/rustlib/$(TARGET_TRIPLE)/lib
LIBS = libcore liballoc librustc_unicode libcollections

$(patsubst %, $(RUSTLIB_TMP)/%, $(LIBS)): $(RUSTLIB_TMP)/%:
$(MKDIR) $(@D)
git clone https://github.com/phil-opp/nightly-$(@F) $@

$(patsubst %, $(RUSTLIB_TMP)/%/$(notdir $(TARGET_FILE)), $(LIBS)): $(RUSTLIB_TMP)/%/$(notdir $(TARGET_FILE)): $(RUSTLIB_TMP)/% $(TARGET_FILE)
cp $(TARGET_FILE) $@

$(patsubst %, $(RUSTLIB)/%.rlib, $(filter-out libcore, $(LIBS))): $(RUSTLIB)/%.rlib: $(RUSTLIB_TMP)/% $(RUSTLIB_TMP)/%/$(notdir $(TARGET_FILE))
cargo rustc --manifest-path $</Cargo.toml --target $(TARGET_TRIPLE) --release -- -Z no-landing-pads
cp $</target/$(TARGET_TRIPLE)/release/$(@F) $@

$(RUSTLIB)/libcore.rlib: $(RUSTLIB_TMP)/libcore $(RUSTLIB_TMP)/libcore/$(notdir $(TARGET_FILE))
cargo rustc --manifest-path $</Cargo.toml --target $(TARGET_TRIPLE) --release --features disable_float -- -Z no-landing-pads
cp $</target/$(TARGET_TRIPLE)/release/$(@F) $@

$(RUSTLIB)/liballoc.rlib: $(RUSTLIB)/libcore.rlib
$(RUSTLIB)/librustc_unicode.rlib: $(RUSTLIB)/libcore.rlib
$(RUSTLIB)/libcollections.rlib: $(RUSTLIB)/liballoc.rlib $(RUSTLIB)/librustc_unicode.rlib

$(TMP)/runtime: $(patsubst %, $(RUSTLIB)/%.rlib, $(LIBS))
$(MKDIR) $(@D)
@touch $@
9 changes: 4 additions & 5 deletions build/Rust.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
CARGO = cargo
CARGO_ARGS += --target $(TARGET_TRIPLE)
RUSTC_ARGS += -Z no-landing-pads
CARGO_DEPS += $(TARGET_TRIPLE).json
CARGO_DEPS += $(TARGET_FILE) crust.lds

# Caching libcrust.a dependencies; see build/Utils.mk
RUST_FILES = $(shell find $(SRC) -name "*.rs")
$(DEPS)/$(TARGET)/crust.d: $(RUST_FILES)
-include $(DEPS)/$(TARGET)/crust.d

$(TARGET)/crust: $(CARGO_DEPS) $(BIN)/boot.o
$(TARGET)/crust: $(CARGO_DEPS) $(BIN)/boot.o $(TMP)/runtime
$(MKDIR) $(@D)
$(warning if the following fails with "error: can't find crate for `core`" or "the crate `core` has been compiled with ...", you need to `make lib/libcore.rlib` and put it in your rust toolchain directory under lib/rustlib/$(TARGET_TRIPLE)/lib.)
$(warning if the following fails with "error: can't find crate for `core`" or "the crate `core` has been compiled with ...", you need to `multirust update` and `make runtime`.)
$(CARGO) rustc $(CARGO_ARGS) -- $(RUSTC_ARGS)
# Cargo doesn't always update timestamp
[ -e $@ ] && touch $@
@[ -e $@ ] && touch $@ # Cargo doesn't always update timestamp
4 changes: 1 addition & 3 deletions build/Utils.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ OBJ = obj
DIRTY += $(OBJ)
BIN = bin
DIRTY += $(BIN)
LIB = lib
DIRTY += $(LIB)
TMP = tmp
DIRTY += $(TMP)
DEPS = deps
DIRTY += $(DEPS)
# Cleaned by Cargo
TARGET = target/$(TARGET_TRIPLE)/$(PROFILE)
TARGET_FILE = $(TARGET_TRIPLE).json

# Commands
RM = rm -rf
Expand All @@ -29,7 +28,6 @@ ECHO = @echo
# Macros
lowercase = $(shell echo ${$1,,})


# When writing a target declaration with wildcard dependencies, caching the dependencies in a .d file means that deleting a dependency will result in rebuilding. (Wildcard dependencies usually do not pick up on deletions)
$(DEPS)/%.d:
$(MKDIR) $(@D)
Expand Down
22 changes: 0 additions & 22 deletions build/libcore.mk

This file was deleted.

3 changes: 2 additions & 1 deletion crust.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@

#----------------------------------------------------------------------------
# Initial memory allocation (in megabytes) for the new domain.
memory = 64
memory = 128
serial='tty'
vif=[ 'bridge=virbr0' ]
76 changes: 47 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(lang_items)]
#![feature(collections)]
#![feature(asm)]
#![feature(stmt_expr_attributes)]
#![feature(type_macros)]
Expand All @@ -7,19 +8,25 @@
#![feature(alloc)]
#![feature(braced_empty_structs)] // XXX: For now
#![feature(start)]
#![feature(reflect_marker)]
#![feature(const_fn)]
//#![feature(core_str_ext)]
//#![feature(ptr_as_ref)]
#![no_std]
#![allow(dead_code)] // XXX: For now, because a lot of unused structs
extern crate rlibc;
extern crate mm;
extern crate alloc;
extern crate collections;

#[macro_use]
mod std;
mod xen;

pub use xen::poweroff;
pub use xen::console_io::STDOUT;
pub use xen::mem::sbrk;
pub use xen::emergency_console::EMERGENCY_CONSOLE as DEBUG;
use xen::start_info::start_info_page;
use core::fmt::Write;
use alloc::boxed::Box;
Expand All @@ -31,55 +38,66 @@ const LEN : usize = 2000;

#[lang = "panic_fmt"]
#[no_mangle]
pub extern fn rust_begin_unwind(_fmt: core::fmt::Arguments, _file_line: &(&'static str, u32)) -> ! {
xen::emergency_console::print(b"panic_fmt!\n\0");
pub extern fn rust_begin_unwind(args: core::fmt::Arguments, file: &'static str, line: u32) -> ! {
writeln!(STDOUT, "In file {} at line {} (XXX LINE COUNT IS CURRENTLY BROKEN)", file, line).unwrap();
writeln!(STDOUT, "{:?}", args).unwrap();
xen::crash();
}

fn print_init_info(){
let _ = writeln!(STDOUT, "Magic: {}", core::str::from_utf8(&start_info_page.magic).unwrap_or("ERROR"));
let _ = writeln!(STDOUT, "nr_pages: {:#X}", start_info_page.nr_pages);
let _ = writeln!(STDOUT, "shared_info: {:#X}", start_info_page.shared_info);
writeln!(STDOUT, "Magic: {}", core::str::from_utf8(&start_info_page.magic).unwrap_or("ERROR")).unwrap();
writeln!(STDOUT, "nr_pages: {:#X}", start_info_page.nr_pages).unwrap();
writeln!(STDOUT, "shared_info: {:#X}", start_info_page.shared_info).unwrap();
}


#[no_mangle]
pub extern fn prologue() {
use core::ptr;
let null: *const u8 = ptr::null();
let argv: *const *const u8 = &null;
let result = main(0, argv);
()
unsafe {
writeln!(DEBUG, "prologue!").unwrap();
use core::ptr;
let null: *const u8 = ptr::null();
let argv: *const *const u8 = &null;
writeln!(DEBUG, "mm::setup").unwrap();
writeln!(DEBUG, "xen::console_io::initialize").unwrap();
xen::console_io::initialize();
writeln!(DEBUG, "xen::xenstore::initialize").unwrap();
xen::xenstore::initialize();
writeln!(DEBUG, "end of prologue!").unwrap();
let _result = main(0, argv);
}
}

#[start]
pub fn main(_argc: isize, _argv: *const *const u8) -> isize {
xen::emergency_console::print(b"main!\n\0");
let _ = writeln!(STDOUT, "Hello world!");

let x = mm::__rust_allocate(1, 16);
let y = mm::__rust_allocate(1, 16);

unsafe {
*x = 100;
*y = 2 * *x;
if *x == 100 && *y == 200 {
xen::emergency_console::print(b"Assigned Properly!\n\0");
let _ = writeln!(STDOUT, "Assigned Properly!");
}
else {
xen::emergency_console::print(b"Error Assigning!\n\0");
let _ = writeln!(STDOUT, "Error Assigning");
writeln!(DEBUG, "main!").unwrap();

let mut s = collections::String::new();
writeln!(STDOUT, "Growing sequences of numbers to test allocation...");
for i in 0 .. 1 {
for j in 0 .. 10 {
s.push(('0' as u8 + j) as char);
writeln!(STDOUT, "{}, {}, {}", &s, s.len(), s.as_ptr() as usize);
}
}
unsafe {
let vm_name = xen::xenstore::XENSTORE.write().as_mut().unwrap().read("name").unwrap();
writeln!(STDOUT, "Hello world {}!", vm_name).unwrap();
let key = "examplekey";
let value = "examplevalue";
xen::xenstore::XENSTORE.write().as_mut().unwrap().write(key, value).unwrap();
writeln!(STDOUT, "Wrote!").unwrap();
// let read = xen::xenstore::XENSTORE.write().as_mut().unwrap().read(key).unwrap();
// writeln!(STDOUT, "wrote {}, read {}", value, read).unwrap();
}

let x = Box::new(12);

if *x == 12 {
xen::emergency_console::print(b"Box Worked!\n\0");
writeln!(STDOUT, "Box Worked").unwrap();
}
else {
xen::emergency_console::print(b"Box Failed!\n\0");
writeln!(STDOUT, "Box Failed").unwrap();
}

let mut a = Box::new([0; LEN]);
Expand All @@ -89,7 +107,7 @@ pub fn main(_argc: isize, _argv: *const *const u8) -> isize {

for i in 1..LEN {
if a[i] != i {
xen::emergency_console::print(b"Memory Error\n\0");
writeln!(STDOUT, "Error in Memory").unwrap();
}
}

Expand Down
Loading

0 comments on commit f969155

Please sign in to comment.