Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Fix stuff for new rustc, work on abi
Browse files Browse the repository at this point in the history
Define a basic binary format which still needs a lot of work
  • Loading branch information
jrasky committed Jun 7, 2016
1 parent 6481cd4 commit 7fa59b5
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 26 deletions.
5 changes: 5 additions & 0 deletions kernel_std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ log = { path = "../log" }
memory = { path = "../memory" }
serial = { path = "../serial" }
constants = { path = "../constants" }

[features]
default = []

freestanding = []
17 changes: 9 additions & 8 deletions kernel_std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub use map::Map;

use include::*;

#[cfg(feature = "freestanding")]
pub mod cpu;

mod include;
Expand Down Expand Up @@ -105,19 +106,19 @@ pub fn early_setup() {
serial::setup_serial();

// set up the reserve logger
static RESERVE: &'static Fn(&log::Location, &Display, &Display) = &serial::reserve_log;
static RESERVE: &'static (Fn(&log::Location, &Display, &Display) + Send + Sync) = &serial::reserve_log;
log::set_reserve(Some(RESERVE));
}

#[cfg(and(not(test), feature = "freestanding"))]
#[cfg(all(not(test), feature = "freestanding"))]
#[cold]
#[inline(never)]
#[lang = "eh_personality"]
extern "C" fn eh_personality() {
unreachable!("C++ exception code called")
}

#[cfg(and(not(test), feature = "freestanding"))]
#[cfg(all(not(test), feature = "freestanding"))]
#[cold]
#[inline(never)]
#[lang = "panic_fmt"]
Expand Down Expand Up @@ -154,7 +155,7 @@ pub extern "C" fn kernel_panic(msg: fmt::Arguments, file: &'static str, line: u3
}
}

#[cfg(and(not(test), feature = "freestanding"))]
#[cfg(all(not(test), feature = "freestanding"))]
#[cold]
#[inline(never)]
fn panic_fmt(msg: fmt::Arguments, file: &'static str, line: u32) -> ! {
Expand All @@ -172,7 +173,7 @@ fn panic_fmt(msg: fmt::Arguments, file: &'static str, line: u32) -> ! {
panic_halt();
}

#[cfg(and(not(test), feature = "freestanding"))]
#[cfg(all(not(test), feature = "freestanding"))]
#[cold]
#[inline(never)]
fn double_panic(original: &PanicInfo, msg: fmt::Arguments, file: &'static str, line: u32) -> ! {
Expand All @@ -193,7 +194,7 @@ fn double_panic(original: &PanicInfo, msg: fmt::Arguments, file: &'static str, l
panic_halt();
}

#[cfg(and(not(test), feature = "freestanding"))]
#[cfg(all(not(test), feature = "freestanding"))]
#[cold]
#[inline(never)]
fn triple_panic(file: &'static str, line: u32) -> ! {
Expand All @@ -211,7 +212,7 @@ fn triple_panic(file: &'static str, line: u32) -> ! {
panic_halt();
}

#[cfg(and(not(test), feature = "freestanding"))]
#[cfg(all(not(test), feature = "freestanding"))]
#[cold]
#[inline(never)]
fn panic_halt() -> ! {
Expand All @@ -224,7 +225,7 @@ fn panic_halt() -> ! {
}
}

#[cfg(and(not(test), feature = "freestanding"))]
#[cfg(all(not(test), feature = "freestanding"))]
#[cold]
#[inline(never)]
#[no_mangle]
Expand Down
4 changes: 4 additions & 0 deletions log/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub struct Logger {
lost: usize
}

// The logger struct is an internal singleton by design
unsafe impl Send for Logger {}
unsafe impl Sync for Logger {}

impl Logger {
#[cfg(any(all(feature = "log_any", debug_assertions), all(feature = "release_log_any", not(debug_assertions))))]
pub const fn new() -> Logger {
Expand Down
4 changes: 2 additions & 2 deletions log_abi/Cargo.lock

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

1 change: 1 addition & 0 deletions log_abi/src/include.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub use std::fmt::Display;
pub use std::sync::atomic::{AtomicUsize, Ordering};
pub use std::raw::TraitObject;
pub use std::marker::{Sync, Send};

pub use std::mem;

Expand Down
11 changes: 8 additions & 3 deletions log_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ mod include;

use include::*;

static CALLBACK: Mutex<Option<TraitObject>> = Mutex::new(None);
static CALLBACK: Mutex<Option<TOWrapper>> = Mutex::new(None);

// sshh rustc, I know what I'm doing
struct TOWrapper(TraitObject);
unsafe impl Sync for TOWrapper {}
unsafe impl Send for TOWrapper {}

pub struct Location {
pub module_path: &'static str,
Expand Down Expand Up @@ -49,13 +54,13 @@ pub fn set_callback(func: &'static Fn(usize, &Location, &Display, &Display)) {
unsafe {
let trait_obj: TraitObject = mem::transmute(func);
let mut callback = CALLBACK.lock();
*callback = Some(trait_obj);
*callback = Some(TOWrapper(trait_obj));
}
}

unsafe fn get_callback() -> Option<&'static Fn(usize, &Location, &Display, &Display)> {
if let Some(ref trait_ref) = *CALLBACK.lock() {
Some(mem::transmute(trait_ref.clone()))
Some(mem::transmute(trait_ref.0.clone()))
} else {
None
}
Expand Down
4 changes: 4 additions & 0 deletions memory/src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ struct Manager {
free: *mut Block
}

// Manager is an internally-managed singleton
unsafe impl Sync for Manager {}
unsafe impl Send for Manager {}

impl Manager {
#[inline]
const fn new() -> Manager {
Expand Down
48 changes: 47 additions & 1 deletion stage2/Cargo.lock

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

4 changes: 3 additions & 1 deletion stage2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ log = { path = "../log" }
paging = { path = "../paging" }
constants = { path = "../constants" }
uuid = { version = "*", features = ["v4"] }
corepack = { git = "https://github.com/jrasky/corepack" }
corepack = { git = "https://github.com/jrasky/corepack", features = ["std"] }
serde = { version = "*", git = "https://github.com/serde-rs/serde" }
serde_macros = { version = "*", git = "https://github.com/serde-rs/serde" }
23 changes: 12 additions & 11 deletions stage2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern crate constants;
extern crate uuid;
extern crate corepack;

use std::io::{Read, Write};
use std::io::{Read, Write, Seek, SeekFrom};
use std::fmt::Display;
use std::fs::File;
use std::collections::HashMap;
Expand Down Expand Up @@ -42,7 +42,7 @@ struct ModuleWriter {

// ModuleHeader heads the modules loaded by grub.
// The actual data follows on the next page boundary.
#[derive(Debug, Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
struct ModuleHeader {
magic: [u8; 16], // 0af979b7-02c3-4ca6-b354-b709bec81199
id: [u8; 16], // unique ID for this module
Expand All @@ -53,32 +53,33 @@ struct ModuleHeader {

impl elfloader::ElfLoader for ModuleWriter {
fn allocate(&mut self, base: elfloader::VAddr, size: usize, flags: elfloader::elf::ProgFlag) {
let mut new_file = File::create(format!("kernel-{}.mod", Uuid::new_v4().urn()))
let mut new_file = File::create(format!("target/kernel-{}.mod", Uuid::new_v4().hyphenated()))
.expect("Failed to open file");

let mut module_flags = 0;

if flags.0 & PF_X.0 {
module_flags &= 0x2;
if flags.0 & PF_X.0 == PF_X.0 {
module_flags |= 0x2;
}

if flags.0 & PF_W.0 {
module_flags &= 0x1;
if flags.0 & PF_W.0 == PF_W.0 {
module_flags |= 0x1;
}

// create header structure
let header = ModuleHeader {
magic: Uuid::parse_str("0af979b7-02c3-4ca6-b354-b709bec81199").unwrap().as_bytes(),
id: Uuid::new_v4().as_bytes(),
magic: *Uuid::parse_str("0af979b7-02c3-4ca6-b354-b709bec81199").unwrap().as_bytes(),
id: *Uuid::new_v4().as_bytes(),
base: base as u64,
flags: module_flags
};

// encode and write it to a file
let mut bytes = corepack::to_bytes(header).expect("Failed to encode module header");
let pad_len = align(bytes.len() as u64, 0x1000); // pad to page-align
file.write_all(bytes).expect("Failed to write bytes to file");
file.set_len(pad_len).expect("Failed to pad out file to page");
new_file.write_all(bytes.as_slice()).expect("Failed to write bytes to file");
new_file.set_len(pad_len).expect("Failed to pad out file to page");
new_file.seek(SeekFrom::Start(pad_len)).expect("Failed to seek to end of file");

self.files.insert(base, new_file);
}
Expand Down

0 comments on commit 7fa59b5

Please sign in to comment.