Skip to content

Commit

Permalink
fix compile error in the latest nightly 1.33.0. (#1)
Browse files Browse the repository at this point in the history
* fix compile error in the latest nightly 1.33.0.

* review: Add eh_personality.
  • Loading branch information
tomoyuki-nakabayashi authored and souvik1997 committed Apr 12, 2019
1 parent 67b57cf commit 26ecfcd
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 31 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ crate-type = ["staticlib"]

[dependencies]
lazy_static = { version = "1.0.0", features = ["spin_no_std"] }
spin = "0.4.7"
spin = "0.4.9"
rand = { version = "0.4.2", default-features = false }
2 changes: 1 addition & 1 deletion kbuild.mk
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ $(obj)/$(RUST_TARGET): $(RUST_FILES) $(LLVM_TARGET_SPEC) $(BASE_DIR)/$(KBUILD)
# directory but subsequent invokations of cargo/xargo/rustc might change their working directory. Setting
# RUST_TARGET_PATH ensures that the compiler can find the LLVM target specification.
# We also have to `cd` into $(BASE_DIR) since we are currently in the kernel headers directory.
(cd $(BASE_DIR); env RUST_TARGET_PATH=$(BASE_DIR) $(XARGO) rustc $(if $(RELEASE),--release) $(if $(VERBOSE),--verbose) --target $(UTS_MACHINE)-unknown-none-gnu -- -C code-model=kernel -C relocation-model=static -C panic=abort)
(cd $(BASE_DIR); env RUST_TARGET_PATH=$(BASE_DIR) $(CARGO) xbuild $(if $(RELEASE),--release) $(if $(VERBOSE),--verbose) --target $(UTS_MACHINE)-unknown-none-gnu)
# After the archive is compiled, copy it to the build directory
cp "$(CARGO_BUILD_DIR)/$(RUST_TARGET)" $(obj)

Expand Down
1 change: 1 addition & 0 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloc::*;
use alloc::vec::Vec;
use core::fmt;
use spin::Mutex;

Expand Down
36 changes: 19 additions & 17 deletions src/lang.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
use core;
use core::panic::PanicInfo;

// These functions and traits are used by the compiler, but not
// for a bare-bones hello world. These are normally
// provided by libstd.
#[lang = "eh_personality"]
#[no_mangle]
pub extern "C" fn eh_personality() {}

#[lang = "eh_unwind_resume"]
#[no_mangle]
pub extern "C" fn eh_unwind_resume(_a: *const u8) {}

#[allow(dead_code)]
extern "C" {
fn abort();
fn abort() -> !;
fn panic_c();
}

#[lang = "panic_fmt"]
#[panic_handler]
#[no_mangle]
pub extern "C" fn rust_begin_panic(msg: core::fmt::Arguments, file: &'static str, line: u32) -> ! {
use super::io;
pub fn rust_begin_panic(info: &PanicInfo) -> ! {
// Print the file and line number
println!("Rust panic @ {}:{}", file, line);
if let Some(location) = info.location() {
println!("Rust panic @ {}:{}",
location.file(), location.line());
}

// Print the message and a newline
io::print(msg);
println!("");
if let Some(message) = info.message() {
println!("{}", message);
}

unsafe {
// In a real kernel module, we should use abort() instead of panic()
abort(); // replace with panic_c() if you want
abort() // replace with panic_c() if you want
}
loop {}
}

use core::alloc::Layout;
#[cfg(not(test))]
#[alloc_error_handler]
pub fn alloc_error(_: Layout) -> ! {
unsafe { abort() }
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
// Used for dynamic allocation
#![feature(alloc)]
#![feature(allocator_api)]
#![feature(global_allocator)]
#![feature(alloc_error_handler)]
#![feature(const_fn)]
#![feature(panic_info_message)]
extern crate alloc;
// Memory-related functions are in the `mem` module
mod mem;
Expand Down
12 changes: 6 additions & 6 deletions src/mem/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::heap::{Alloc, AllocErr, Layout};
use core::alloc::{GlobalAlloc, Layout};

#[derive(Default)]
pub struct KernelAllocator;
Expand All @@ -17,8 +17,8 @@ extern "C" {
fn krealloc_c(ptr: *mut u8, size: usize) -> *mut u8;
}

unsafe impl<'a> Alloc for &'a KernelAllocator {
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
unsafe impl GlobalAlloc for KernelAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
// A side effect of the buddy allocator is that allocations are aligned to
// the power-of-two that is larger than the allocation size. So if the
// request needs to be aligned to something larger than the allocation size,
Expand All @@ -27,13 +27,13 @@ unsafe impl<'a> Alloc for &'a KernelAllocator {
use core::cmp::max;
let p = kmalloc_c(max(layout.size(), layout.align()));
if p.is_null() {
Err(AllocErr::Exhausted { request: layout })
0 as *mut u8
} else {
Ok(p)
p
}
}

unsafe fn dealloc(&mut self, ptr: *mut u8, _layout: Layout) {
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
kfree_c(ptr);
}
}
2 changes: 1 addition & 1 deletion x86_64-unknown-none-gnu.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"pre-link-args": ["-m64"],
"pre-link-args": { "ld": ["-m64"]},
"data-layout": "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128",
"llvm-target": "x86_64-unknown-none-gnu",
"target-endian": "little",
Expand Down

0 comments on commit 26ecfcd

Please sign in to comment.