From 3e49ec75d3a876a386457460c60acee920b085bd Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 15 Jan 2019 23:16:36 +0100 Subject: [PATCH] it compiles, update for rust 2018 --- kernel-rs/Cargo.toml | 15 +++++------ kernel-rs/Makefile | 10 +++---- kernel-rs/README.md | 19 ++++++++++---- kernel-rs/mk/grub.mk | 4 +-- kernel-rs/mk/qemu.mk | 2 +- kernel-rs/src/allocator/mod.rs | 13 ++++++---- kernel-rs/src/allocator/slab.rs | 43 ------------------------------- kernel-rs/src/arch/x86/mod.rs | 2 +- kernel-rs/src/lib.rs | 22 +++++----------- kernel-rs/src/memory/recycle.rs | 2 +- kernel-rs/src/scheduling/fifo.rs | 2 +- kernel-rs/src/scheduling/sleep.rs | 2 +- 12 files changed, 46 insertions(+), 90 deletions(-) delete mode 100644 kernel-rs/src/allocator/slab.rs diff --git a/kernel-rs/Cargo.toml b/kernel-rs/Cargo.toml index 824d95b2..293bdd48 100644 --- a/kernel-rs/Cargo.toml +++ b/kernel-rs/Cargo.toml @@ -10,22 +10,19 @@ crate-type = ["staticlib"] rlibc = "1.0" bitflags = "1.0.1" spin = "0.4" -slab_allocator = "0.3.1" -multiboot2 = { path = "multiboot2-elf64" } # added rsdp tag +slab_allocator = "0.3.5" +x86 = { path = "x86" } # forked for IA-32 compat +multiboot2 = { path = "multiboot2-elf64" } # forked to add rsdp tag [dependencies.raw-cpuid] # need to use github/master because of features not yet on crates.io git = "https://github.com/gz/rust-cpuid" features = ["nightly"] -[dependencies.x86] -# forked for IA32 compat -path = "x86" - [dependencies.lazy_static] version = "1.0.0" features = ["spin_no_std"] -[dependencies.compiler_builtins] -#I'm waiting for somebody to port i386/udivdi3.S ... :((( -git = "https://github.com/rust-lang-nursery/compiler-builtins" +#[dependencies.compiler_builtins] +##I'm waiting for somebody to port i386/udivdi3.S ... :((( +#git = "https://github.com/rust-lang-nursery/compiler-builtins" diff --git a/kernel-rs/Makefile b/kernel-rs/Makefile index 7bc433c7..cf398440 100644 --- a/kernel-rs/Makefile +++ b/kernel-rs/Makefile @@ -1,7 +1,7 @@ SHELL := /bin/bash ARCH := x86 -OS := bluesnow +OS := bluesnow TARGET ?= $(ARCH)-$(OS) all: @@ -23,10 +23,10 @@ $(rust_os): $(TARGET).json Makefile TERM=xterm RUST_TARGET_PATH="$(shell pwd)" xargo build --target $(TARGET) ## LINKAGE -kernel := build/$(OS)-$(ARCH).bin +KERNEL := build/$(OS)-$(ARCH).bin linker_script := src/arch/$(ARCH)/linker.ld LD := /usr/bin/ld -m elf_i386 -L ./ -n --gc-sections -$(kernel): $(rust_os) $(asm_object) $(linker_script) Makefile +$(KERNEL): $(rust_os) $(asm_object) $(linker_script) Makefile $(LD) -o $@ -T $(linker_script) $(asm_object) $(rust_os) clean: @@ -36,11 +36,11 @@ clean: .PHONY: clean kernel iso $(rust_os) # Bootloader recipes -ISO := $(kernel:.bin=.iso) +ISO := $(KERNEL:.bin=.iso) iso: $(ISO) include mk/grub.mk # Emulation recipes include mk/qemu.mk -kernel: $(kernel) +kernel: $(KERNEL) diff --git a/kernel-rs/README.md b/kernel-rs/README.md index 47f4d9ee..c27a0f95 100644 --- a/kernel-rs/README.md +++ b/kernel-rs/README.md @@ -4,31 +4,40 @@ Kernel from scratch (KFS) series of projects at Ecole 42 ! # building +`git submodule udpate --init` + - `nasm` compiles the bootcode - `ld` links the bootcode and rust binary - - `grub-mkrescue` builds the iso + - `grub-mkrescue` builds the iso (need xorriso and mtools) - `xargo` builds rust code See `.travis.yml` to get an ubuntu environment ready -on archlinux `pacman -S rustup make grub xorriso mtools binutils gcc qemu` +on archlinux `pacman -S rustup make grub xorriso mtools binutils gcc qemu` +on voidlinux `xbps-install -S rustup make grub xorriso mtools binutils gcc qemu nasm` + #### rust setup +We build on nightly channel because of some cool features not yet in stable. +We need the rust sources to build with xargo for cross-compiling to custom platform. + ``` -rustup override add nightly rustup component add rust-src +rustup override add nightly +rustup default nightly cargo install xargo ``` # running - `make iso` builds a bootable iso with grub - - `make qemu` runs the iso, `make qemu-reload` after a re-build + - `make qemu` runs the iso, + - `make qemu-reload` reloads the CD # todo - remove assembly for a pure rust entry point - - replace grub with something lighter + - replace grub with something lighter (no bootloader at all with `qemu -kernel` ?) # inspiration diff --git a/kernel-rs/mk/grub.mk b/kernel-rs/mk/grub.mk index 2cd521b8..78240426 100644 --- a/kernel-rs/mk/grub.mk +++ b/kernel-rs/mk/grub.mk @@ -1,8 +1,8 @@ grub-cfg := src/arch/$(ARCH)/grub.cfg isodir := build/isofiles -$(ISO): $(kernel) $(grub-cfg) Makefile +$(ISO): $(KERNEL) $(grub-cfg) Makefile @mkdir -p $(isodir)/boot/grub @cp $(grub-cfg) $(isodir)/boot/grub - @cp $(kernel) $(isodir)/boot/$(OS) + @cp $(KERNEL) $(isodir)/boot/$(OS) grub-mkrescue -o $@ $(isodir) 2>/dev/null diff --git a/kernel-rs/mk/qemu.mk b/kernel-rs/mk/qemu.mk index 7dd71894..e5f89e2d 100644 --- a/kernel-rs/mk/qemu.mk +++ b/kernel-rs/mk/qemu.mk @@ -13,7 +13,7 @@ qemu: qemu-gdb: gdb -q\ - -symbols "$(kernel)" \ + -symbols "$(KERNEL)" \ -ex "target remote :$(QEMU_GDB_PORT)"\ -ex "set arch i386" diff --git a/kernel-rs/src/allocator/mod.rs b/kernel-rs/src/allocator/mod.rs index 1aa4c172..baa5135f 100644 --- a/kernel-rs/src/allocator/mod.rs +++ b/kernel-rs/src/allocator/mod.rs @@ -1,12 +1,10 @@ -pub use self::slab::Allocator; -mod slab; +// pub use self::ALLOCATOR; -use x86::*; use x86::structures::paging::*; use arch::x86::paging::*; fn map_heap(active_table: &mut ActivePageTable) { - //zone for heap is predefines in `consts.rs` + //zone for heap is predefined in `consts.rs` for page in ::KERNEL_HEAP_RANGE { active_table.map(page, PageTableFlags::WRITABLE); } @@ -20,5 +18,10 @@ pub unsafe fn init(active_table: &mut ActivePageTable) { map_heap(active_table); //slab allocator - Allocator::init(offset.as_u32() as usize, size as usize); + super::ALLOCATOR.init(offset.as_u32() as usize, size as usize); +} + +#[alloc_error_handler] +fn foo(_: core::alloc::Layout) -> ! { + panic!(); } diff --git a/kernel-rs/src/allocator/slab.rs b/kernel-rs/src/allocator/slab.rs deleted file mode 100644 index ddc50501..00000000 --- a/kernel-rs/src/allocator/slab.rs +++ /dev/null @@ -1,43 +0,0 @@ -use alloc::heap::{Alloc, AllocErr, Layout}; -use spin::Mutex; -use slab_allocator::Heap; - -static HEAP: Mutex> = Mutex::new(None); - -pub struct Allocator; - -impl Allocator { - pub unsafe fn init(offset: usize, size: usize) { - *HEAP.lock() = Some(Heap::new(offset, size)); - } -} - -unsafe impl<'a> Alloc for &'a Allocator { - unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { - if let Some(ref mut heap) = *HEAP.lock() { - heap.allocate(layout) - } else { - panic!("__rust_allocate: heap not initialized"); - } - } - - unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) { - if let Some(ref mut heap) = *HEAP.lock() { - heap.deallocate(ptr, layout) - } else { - panic!("__rust_deallocate: heap not initialized"); - } - } - - fn oom(&mut self, error: AllocErr) -> ! { - panic!("Out of memory: {:?}", error); - } - - fn usable_size(&self, layout: &Layout) -> (usize, usize) { - if let Some(ref mut heap) = *HEAP.lock() { - heap.usable_size(layout) - } else { - panic!("__rust_usable_size: heap not initialized"); - } - } -} diff --git a/kernel-rs/src/arch/x86/mod.rs b/kernel-rs/src/arch/x86/mod.rs index 59b7f073..73ae5e38 100644 --- a/kernel-rs/src/arch/x86/mod.rs +++ b/kernel-rs/src/arch/x86/mod.rs @@ -54,7 +54,7 @@ pub unsafe extern "C" fn x86_rust_start(multiboot_info_addr: usize) { // set up pic, pit devices::init(); - // primary CPU entry point + // primary CPU entry point, architutecture independant now. ::kmain(); } diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index 4d4b3fbe..0156eff5 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -10,8 +10,8 @@ #![feature(thread_local)] // home made heap #![feature(alloc)] +#![feature(alloc_error_handler)] #![feature(allocator_api)] -#![feature(global_allocator)] // x86 specific #![feature(abi_x86_interrupt)] @@ -27,25 +27,16 @@ extern crate spin; // used by arch/x86, need conditional compilation here extern crate x86; -/// 80x25 terminal driver #[macro_use] pub mod vga; -/// PS/2 detection and processing pub mod keyboard; -/// simplisitc kernel commands pub mod console; -/// ACPI self contained module pub mod acpi; -/// Heap allocators pub mod allocator; -/// Memory management pub mod memory; -/// arch specific entry points pub mod arch; pub use arch::x86::consts::*; -/// concurrency management pub mod scheduling; -/// uptime counting pub mod time; /// kernel entry point. arch module is responsible for @@ -66,15 +57,14 @@ pub fn kmain() -> ! { #[no_mangle] pub extern "C" fn eh_personality() {} -#[lang = "panic_fmt"] +#[panic_handler] #[no_mangle] -pub extern "C" fn panic_fmt(fmt: core::fmt::Arguments, file: &'static str, line: u32) -> ! { - println!("PANIC: {}", fmt); - println!("FILE: {}", file); - println!("LINE: {}", line); +pub extern "C" fn panic_fmt(info: &core::panic::PanicInfo) -> ! { + println!("{}", info); flush!(); loop {} } #[global_allocator] -static HEAP_ALLOCATOR: allocator::Allocator = allocator::Allocator; +// pub static ALLOCATOR: slab_allocator::LockedHeap = allocator::ALLOCATOR; +pub static ALLOCATOR: slab_allocator::LockedHeap = slab_allocator::LockedHeap::empty(); diff --git a/kernel-rs/src/memory/recycle.rs b/kernel-rs/src/memory/recycle.rs index 69373d5c..4772addb 100644 --- a/kernel-rs/src/memory/recycle.rs +++ b/kernel-rs/src/memory/recycle.rs @@ -1,7 +1,7 @@ //! Recycle allocator //! Uses freed frames if possible, then uses inner allocator -use alloc::Vec; +use alloc::vec::Vec; use x86::*; use x86::structures::paging::*; use super::*; diff --git a/kernel-rs/src/scheduling/fifo.rs b/kernel-rs/src/scheduling/fifo.rs index 5f64228f..aea1e4bb 100644 --- a/kernel-rs/src/scheduling/fifo.rs +++ b/kernel-rs/src/scheduling/fifo.rs @@ -4,7 +4,7 @@ //! processes... //! however it's stupid simple to implement! -use alloc::VecDeque; +use alloc::collections::vec_deque::VecDeque; use super::*; // use super::process::*; diff --git a/kernel-rs/src/scheduling/sleep.rs b/kernel-rs/src/scheduling/sleep.rs index ff4c75d7..af69d146 100644 --- a/kernel-rs/src/scheduling/sleep.rs +++ b/kernel-rs/src/scheduling/sleep.rs @@ -5,7 +5,7 @@ //! //! inspired from https://wiki.osdev.org/Blocking_Process -use alloc::VecDeque; +use alloc::collections::vec_deque::VecDeque; use super::*; struct Sleeper {