From c0193e69e66924b645ccf93073d886085231b8e6 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Tue, 3 Jul 2018 22:27:55 +0800 Subject: [PATCH] Clear all code, make it compile for RISCV in docker. --- Cargo.toml | 2 +- Makefile | 10 ++++++++-- Xargo.toml | 2 +- riscv-env/Dockerfile | 30 ++++++++++++++++++++++++++++++ src/lang.rs | 10 +++++++++- src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 riscv-env/Dockerfile diff --git a/Cargo.toml b/Cargo.toml index c59b65d..8f11e94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ opt-level = 1 [profile.release] debug = true -[dependencies] +[target.x86_64-blog_os.dependencies] bit_field = "0.9.0" rlibc = "1.0" volatile = "0.1.0" diff --git a/Makefile b/Makefile index 836f35f..283cb01 100644 --- a/Makefile +++ b/Makefile @@ -8,10 +8,13 @@ # make asm Open the deassemble file of the last build # make clean Clean -arch ?= x86_64 +arch ?= riscv kernel := build/kernel-$(arch).bin iso := build/os-$(arch).iso target ?= $(arch)-blog_os +ifeq ($(arch), riscv) +target := riscv32i-unknown-none +endif mode ?= debug rust_os := target/$(target)/$(mode)/librust_ucore.a @@ -152,4 +155,7 @@ docker_interactive: @docker run -it --rm $(docker_args) $(docker_image):$(tag) docker_clean: - @docker volume rm $(docker_clean_args) \ No newline at end of file + @docker volume rm $(docker_clean_args) + +docker_riscv: + @docker run -it --rm $(docker_args) riscv-rust \ No newline at end of file diff --git a/Xargo.toml b/Xargo.toml index 73f2a1d..de8cec3 100644 --- a/Xargo.toml +++ b/Xargo.toml @@ -1,2 +1,2 @@ -[target.x86_64-blog_os.dependencies] +[dependencies] alloc = {} diff --git a/riscv-env/Dockerfile b/riscv-env/Dockerfile new file mode 100644 index 0000000..2293abf --- /dev/null +++ b/riscv-env/Dockerfile @@ -0,0 +1,30 @@ +FROM ubuntu:latest + +WORKDIR /rust + +# Rust toolchain bins +# https://github.com/riscv-rust/rust/releases/download/riscv-rust-1.26.0-1-dev/rust-1.26.0-dev-x86_64-unknown-linux-gnu.tar.xz +ADD rust-1.26.0-dev-x86_64-unknown-linux-gnu.tar.xz . + +# Rust src +# https://github.com/riscv-rust/rust/archive/riscv-rust-1.26.0-1-dev.zip +# with submodule: libcompiler_builtins, stdsimd +ADD rust-riscv-rust-1.26.0-1-dev.tar.gz . + +# RISCV32 toolchain +# From tencent cloud for OS2018: ssh 2015011279@140.143.187.14 +ADD rv32-toolchains-prebuild.tar.bz2 . + +# Dependencies +RUN apt-get update && apt-get install -q -y --no-install-recommends libssl1.0.0 curl libssh2-1 gcc make git libc6-dev +# Rust bins need this +RUN mkdir -p /gnu/store/n6acaivs0jwiwpidjr551dhdni5kgpcr-glibc-2.26.105-g0890d5379c/lib \ + && ln -s /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 /gnu/store/n6acaivs0jwiwpidjr551dhdni5kgpcr-glibc-2.26.105-g0890d5379c/lib/ld-linux-x86-64.so.2 +# Install Rust toolchains +RUN ./rust-1.26.0-dev-x86_64-unknown-linux-gnu/install.sh && rm -rf ./rust-1.26.0-dev-x86_64-unknown-linux-gnu +# Install xargo +RUN cargo install xargo +# Env +ENV PATH=~/.cargo/bin:/rust/install-rv32/bin:$PATH +ENV XARGO_RUST_SRC=/rust/rust-riscv-rust-1.26.0-1-dev/src +RUN ln -s ~/.cargo/bin/xargo /usr/local/bin/xargo \ No newline at end of file diff --git a/src/lang.rs b/src/lang.rs index 93d316b..3d0b9e2 100644 --- a/src/lang.rs +++ b/src/lang.rs @@ -1,12 +1,12 @@ // Rust language features implementions use core::panic::PanicInfo; -use arch::cpu; #[lang = "eh_personality"] extern fn eh_personality() { } +#[cfg(target_arch = "x86_64")] #[panic_implementation] #[no_mangle] pub fn panic(info: &PanicInfo) -> ! { @@ -14,12 +14,20 @@ pub fn panic(info: &PanicInfo) -> ! { let message = info.message().unwrap(); error!("\n\nPANIC in {} at line {}\n {}", location.file(), location.line(), message); if cfg!(feature = "qemu_auto_exit") { + use arch::cpu; unsafe{ cpu::exit_in_qemu(3) } } else { loop { } } } +#[cfg(target_arch = "riscv")] +#[lang = "panic_fmt"] +unsafe extern "C" fn panic_fmt(_fmt: ::core::fmt::Arguments, _file: &'static str, _line: u32, _col: u32) -> ! { + loop {} +} + +#[cfg(target_arch = "x86_64")] #[lang = "oom"] #[no_mangle] fn oom() -> ! { diff --git a/src/lib.rs b/src/lib.rs index bb9e735..9c22321 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,49 +14,79 @@ #![feature(optin_builtin_traits)] #![feature(panic_implementation)] #![feature(panic_info_message)] +#![feature(universal_impl_trait)] #![no_std] #[macro_use] +#[cfg(target_arch = "x86_64")] extern crate alloc; +#[cfg(target_arch = "x86_64")] extern crate bit_allocator; +#[cfg(target_arch = "x86_64")] extern crate bit_field; #[macro_use] +#[cfg(target_arch = "x86_64")] extern crate bitflags; #[macro_use] +#[cfg(target_arch = "x86_64")] extern crate lazy_static; +#[cfg(target_arch = "x86_64")] extern crate linked_list_allocator; #[macro_use] +#[cfg(target_arch = "x86_64")] extern crate log; +#[cfg(target_arch = "x86_64")] extern crate multiboot2; #[macro_use] +#[cfg(target_arch = "x86_64")] extern crate once; +#[cfg(target_arch = "x86_64")] extern crate rlibc; +#[cfg(target_arch = "x86_64")] extern crate simple_filesystem; +#[cfg(target_arch = "x86_64")] extern crate spin; +#[cfg(target_arch = "x86_64")] extern crate syscall as redox_syscall; +#[cfg(target_arch = "x86_64")] extern crate uart_16550; +#[cfg(target_arch = "x86_64")] extern crate ucore_memory; +#[cfg(target_arch = "x86_64")] extern crate volatile; #[macro_use] +#[cfg(target_arch = "x86_64")] extern crate x86_64; +#[cfg(target_arch = "x86_64")] extern crate xmas_elf; +#[cfg(target_arch = "x86_64")] pub use arch::interrupt::{rust_trap, set_return_rsp}; +#[cfg(target_arch = "x86_64")] use linked_list_allocator::LockedHeap; #[macro_use] // print! +#[cfg(target_arch = "x86_64")] mod io; +#[cfg(target_arch = "x86_64")] mod memory; mod lang; +#[cfg(target_arch = "x86_64")] mod util; #[macro_use] mod macros; +#[cfg(target_arch = "x86_64")] mod consts; +#[cfg(target_arch = "x86_64")] mod process; +#[cfg(target_arch = "x86_64")] mod syscall; +#[cfg(target_arch = "x86_64")] mod fs; +#[cfg(target_arch = "x86_64")] mod thread; +#[cfg(target_arch = "x86_64")] mod sync; #[allow(dead_code)] @@ -66,6 +96,7 @@ mod arch; /// The entry point of Rust kernel #[no_mangle] +#[cfg(target_arch = "x86_64")] pub extern "C" fn rust_main(multiboot_information_address: usize) -> ! { arch::idt::init(); io::init(); @@ -124,6 +155,7 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) -> ! { /// The entry point for another processors #[no_mangle] +#[cfg(target_arch = "x86_64")] pub extern "C" fn other_main() -> ! { arch::gdt::init(); arch::idt::init(); @@ -142,8 +174,10 @@ pub extern "C" fn other_main() -> ! { /// /// It should be defined in memory mod, but in Rust `global_allocator` must be in root mod. #[global_allocator] +#[cfg(target_arch = "x86_64")] static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty(); +#[cfg(target_arch = "x86_64")] mod test { pub fn global_allocator() { for i in 0..10000 {