From c0138c4c35295a59cc8c1691a0511c60c6a5eb12 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Sat, 14 Jul 2018 01:11:47 +0800 Subject: [PATCH] Fix linking user program binaries --- kernel/Makefile | 17 +++++++++++------ kernel/src/fs.rs | 19 +++++++++++++++---- kernel/src/lib.rs | 19 +++++++++++++++++++ 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/kernel/Makefile b/kernel/Makefile index b71a39d..a19570e 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -22,8 +22,9 @@ grub_cfg := $(boot_src)/grub.cfg assembly_source_files := $(wildcard $(boot_src)/*.asm) assembly_object_files := $(patsubst $(boot_src)/%.asm, \ build/arch/$(arch)/boot/%.o, $(assembly_source_files)) -user_image_files := $(wildcard ../user/*.img) -user_object_files := $(patsubst ../user/%.img, build/user/%.o, $(user_image_files)) +user_bin_path := ../user/target/$(arch)-ucore/debug +user_obj_path := build/arch/$(arch)/user +user_object_files := $(patsubst $(user_bin_path)/%.d, $(user_obj_path)/%.o, $(wildcard $(user_bin_path)/*.d)) SFSIMG := ../user/ucore32.img qemu_opts := -cdrom $(iso) -smp 4 -serial mon:stdio -drive file=$(SFSIMG),media=disk,cache=writeback ifeq ($(arch), riscv32) @@ -103,6 +104,9 @@ build: iso asm: @$(objdump) -dS $(kernel) | less +elf-h: + @$(objdump) -h $(kernel) + build/os-x86_64.iso: $(kernel) $(grub_cfg) @mkdir -p build/isofiles/boot/grub @cp $(kernel) build/isofiles/boot/kernel.bin @@ -139,10 +143,11 @@ build/arch/riscv32/boot/%.o: $(boot_src)/%.asm @mkdir -p $(shell dirname $@) @$(as) -march=rv32i $< -o $@ -# make .o from .img file -build/user/%.o: ../user/%.img - @mkdir -p $(shell dirname $@) - @$(ld) -r -b binary $< -o $@ +# make .o from binary files +$(user_obj_path)/%.o: $(user_bin_path)/% + @mkdir -p $(user_obj_path) + @cd $(user_bin_path) && \ + $(ld) -b binary $(notdir $<) -o $(abspath $@) # patch Rust core for RISCV32I atomic patch-core: diff --git a/kernel/src/fs.rs b/kernel/src/fs.rs index 27d897f..8f9a1f7 100644 --- a/kernel/src/fs.rs +++ b/kernel/src/fs.rs @@ -4,6 +4,7 @@ use arch::driver::ide; use spin::Mutex; use process; +#[cfg(not(feature = "link_user_program"))] pub fn load_sfs() { // let slice = unsafe { MemBuf::new(_binary_user_ucore32_img_start, _binary_user_ucore32_img_end) }; let sfs = SimpleFileSystem::open(Box::new(&ide::DISK0)).unwrap(); @@ -22,12 +23,22 @@ pub fn load_sfs() { process::print(); } +#[cfg(feature = "link_user_program")] +pub fn load_sfs() { + let slice = unsafe { + slice::from_raw_parts(_binary_hello_start as *const u8, + _binary_hello_size as usize) + }; + + process::add_user_process("hello", slice); + process::print(); +} + + #[cfg(feature = "link_user_program")] extern { - fn _binary_user_ucore32_img_start(); - fn _binary_user_ucore32_img_end(); - fn _binary_user_xv6_64_img_start(); - fn _binary_user_xv6_64_img_end(); + fn _binary_hello_start(); + fn _binary_hello_size(); } struct MemBuf(&'static [u8]); diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index bbf123f..d864718 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -82,6 +82,25 @@ pub extern fn rust_main() -> ! { arch::init(); process::init(); info!("RISCV init end"); + + #[cfg(feature = "link_user_program")] + { + use core::slice; + let slice = unsafe { + slice::from_raw_parts(_binary_hello_start as *const u8, + _binary_hello_size as usize) + }; + + process::add_user_process("hello", slice); + process::print(); + + + extern { + fn _binary_hello_start(); + fn _binary_hello_size(); + } + } + unsafe { arch::interrupt::enable(); } loop {} }