From 94bc8353f7de07dd9e676346bf865424fb40f953 Mon Sep 17 00:00:00 2001 From: equation314 Date: Thu, 29 Nov 2018 00:39:44 +0800 Subject: [PATCH 1/6] user: create SFS image in Makefile --- crate/riscv | 1 - kernel/Makefile | 27 +++++++++++++++----- user/Makefile | 58 +++++++++++++++++++++++++++++++++++++++--- user/x86_64-ucore.json | 3 ++- 4 files changed, 76 insertions(+), 13 deletions(-) delete mode 160000 crate/riscv diff --git a/crate/riscv b/crate/riscv deleted file mode 160000 index a37a65f..0000000 --- a/crate/riscv +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a37a65fa13a00c5aa0068c3f2b5d55af6a37dd93 diff --git a/kernel/Makefile b/kernel/Makefile index 0e1a2e8..ef57cea 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -27,16 +27,20 @@ kernel := target/$(target)/$(mode)/ucore bin := target/$(target)/$(mode)/kernel.bin bootimage := target/$(target)/bootimage.bin -user_bin_path := ../user/target/$(arch)-ucore/debug +user_dir := ../user +user_bin_path := $(user_dir)/target/$(arch)-ucore/debug user_bins := $(patsubst $(user_bin_path)/%.d, $(user_bin_path)/%, $(wildcard $(user_bin_path)/*.d)) user_obj := build/$(arch)/user.o -SFSIMG := ../user/ucore32.img +sfsimg := $(user_dir)/build/user-$(arch).img + +export ARCH = $(arch) +export SFSIMG = $(sfsimg) ### qemu options ### ifeq ($(arch), x86_64) qemu_opts := \ -drive format=raw,file=$(bootimage) \ - -drive format=raw,file=$(SFSIMG),media=disk,cache=writeback \ + -drive format=raw,file=$(sfsimg),media=disk,cache=writeback \ -smp cores=$(smp) \ -serial mon:stdio \ -device isa-debug-exit \ @@ -99,7 +103,7 @@ cc := $(prefix)gcc as := $(prefix)as gdb := $(prefix)gdb -.PHONY: all clean run build asm doc justrun kernel +.PHONY: all clean run build asm doc justrun debug kernel install all: kernel @@ -112,10 +116,10 @@ doc: run: build justrun -justrun: +justrun: $(sfsimg) @qemu-system-$(arch) $(qemu_opts) || [ $$? -eq 11 ] # run qemu and assert it exit 11 -debug: $(kernel) $(bin) +debug: $(kernel) $(bin) $(sfsimg) @qemu-system-$(arch) $(qemu_opts) -s -S & @sleep 1 @$(gdb) $(kernel) -x ../tools/gdbinit @@ -156,16 +160,25 @@ else ifeq ($(arch), aarch64) @$(objcopy) $(kernel) --strip-all -O binary $@ endif -kernel: ifeq ($(arch), x86_64) +kernel: @bootimage build $(build_args) else +kernel: $(sfsimg) +ifeq ($(arch), riscv32) @-patch -p0 -N -b \ $(shell rustc --print sysroot)/lib/rustlib/src/rust/src/libcore/sync/atomic.rs \ src/arch/riscv32/atomic.patch +endif @CC=$(cc) cargo xbuild $(build_args) endif + + +### user programs ### +$(sfsimg): + @cd $(user_dir) && make sfsimg + # make user.o from binary files $(user_obj): $(user_bins) @cd $(user_bin_path) && \ diff --git a/user/Makefile b/user/Makefile index 185ca0f..9b0648b 100644 --- a/user/Makefile +++ b/user/Makefile @@ -1,5 +1,55 @@ -# arch = {riscv32, x86_64} -arch := riscv32 +# arch = {riscv32, x86_64, aarch64} +# mode = {debug, release} +arch ?= riscv32 +mode ?= debug -all: - cargo xbuild --target $(arch)-ucore.json \ No newline at end of file +rust_src_dir := src/bin +rust_bin_path := target/$(arch)-ucore/$(mode) +user_rust_bins := $(patsubst $(rust_src_dir)/%.rs, $(rust_bin_path)/%, $(wildcard $(rust_src_dir)/*.rs)) +c_src_dir := +c_bin_path := +user_c_bins := +user_bins := $(user_rust_bins) $(user_c_bins) + +build_path := build +sfsroot := $(build_path)/sfsroot-$(arch) +sfsimg := $(build_path)/user-$(arch).img + +mksfs := mksfs + +build_args := --target $(arch)-ucore.json +ifeq ($(mode), release) +build_args := $(build_args) --release +endif + +.PHONY: all clean build-rust build-c build mksfs sfsimg + +all: $(sfsimg) + +build-rust: + @echo Building user rust programs + @cargo xbuild $(build_args) + +build-c: + @echo Building user C programs + +build: build-rust build-c + +mksfs: +ifeq ($(shell which $(mksfs)),) + @cargo install --git https://github.com/wangrunji0408/SimpleFileSystem-Rust --features="std" +endif + +$(sfsroot): build + @mkdir -p $(sfsroot)/ + @cp $(user_bins) $(sfsroot)/ + +$(sfsimg): mksfs $(sfsroot) + @echo Creating SFS image at $@ + @$(mksfs) zip $(sfsroot) $@ + +sfsimg: $(sfsimg) + +clean: + @cargo clean + @rm -rf $(build_path) diff --git a/user/x86_64-ucore.json b/user/x86_64-ucore.json index c587146..6510efa 100644 --- a/user/x86_64-ucore.json +++ b/user/x86_64-ucore.json @@ -7,8 +7,9 @@ "target-c-int-width": "32", "os": "none", "executables": true, + "linker": "rust-lld", "linker-flavor": "ld.lld", "panic-strategy": "abort", "disable-redzone": true, "features": "-mmx,-sse,+soft-float" -} \ No newline at end of file +} From f882a085d68bab9bbd287b7cc01e7f69bba61866 Mon Sep 17 00:00:00 2001 From: equation314 Date: Thu, 29 Nov 2018 01:22:44 +0800 Subject: [PATCH 2/6] user: link created SFS image in kernel --- .gitignore | 9 +++-- crate/atags/Cargo.lock | 14 -------- kernel/Makefile | 6 ++-- kernel/build.rs | 75 +++++++++++++++++++++++++++++++++--------- kernel/src/fs.rs | 17 ++-------- kernel/src/shell.rs | 11 ++++--- 6 files changed, 76 insertions(+), 56 deletions(-) delete mode 100644 crate/atags/Cargo.lock diff --git a/.gitignore b/.gitignore index fe8b1fd..975d0c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,10 @@ build target /kernel/src/arch/x86_64/interrupt/vector.asm -/crate/bit-allocator/Cargo.lock -/crate/memory/Cargo.lock -/crate/bbl/Cargo.lock -/crate/sync/Cargo.lock -/crate/process/Cargo.lock + +Cargo.lock +!kernel/Cargo.lock +!user/Cargo.lock .DS_Store diff --git a/crate/atags/Cargo.lock b/crate/atags/Cargo.lock deleted file mode 100644 index 42d11eb..0000000 --- a/crate/atags/Cargo.lock +++ /dev/null @@ -1,14 +0,0 @@ -[[package]] -name = "atags" -version = "0.1.0" -dependencies = [ - "volatile 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "volatile" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[metadata] -"checksum volatile 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d9ca391c55768e479d5c2f8beb40c136df09257292a809ea514e82cfdfc15d00" diff --git a/kernel/Makefile b/kernel/Makefile index ef57cea..3f4f76b 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -12,6 +12,7 @@ # d = int | in_asm | ... QEMU debug info # mode = debug | release # LOG = off | error | warn | info | debug | trace +# SFSIMG = SFS image path of user programs # smp SMP core number # board = fpga Only available on riscv32, build without bbl, run on board # | raspi3 Only available on aarch64, run on Raspberry Pi 3 Model B/B+ @@ -31,6 +32,7 @@ user_dir := ../user user_bin_path := $(user_dir)/target/$(arch)-ucore/debug user_bins := $(patsubst $(user_bin_path)/%.d, $(user_bin_path)/%, $(wildcard $(user_bin_path)/*.d)) user_obj := build/$(arch)/user.o +sfsroot := $(user_dir)/build/sfsroot-$(arch) sfsimg := $(user_dir)/build/user-$(arch).img export ARCH = $(arch) @@ -40,7 +42,7 @@ export SFSIMG = $(sfsimg) ifeq ($(arch), x86_64) qemu_opts := \ -drive format=raw,file=$(bootimage) \ - -drive format=raw,file=$(sfsimg),media=disk,cache=writeback \ + -drive format=raw,file=$(SFSIMG),media=disk,cache=writeback \ -smp cores=$(smp) \ -serial mon:stdio \ -device isa-debug-exit \ @@ -176,7 +178,7 @@ endif ### user programs ### -$(sfsimg): +$(sfsimg): $(sfsroot) @cd $(user_dir) && make sfsimg # make user.o from binary files diff --git a/kernel/build.rs b/kernel/build.rs index ec94d4e..e85f6f3 100644 --- a/kernel/build.rs +++ b/kernel/build.rs @@ -1,23 +1,42 @@ extern crate cc; use std::fs::File; -use std::io::{Write, Result}; +use std::path::Path; +use std::io::{Result, Write}; fn main() { - if std::env::var("TARGET").unwrap().find("x86_64").is_some() { -// cc::Build::new() -// .file("src/arch/x86_64/driver/apic/lapic.c") -// .file("src/arch/x86_64/driver/keyboard/keyboard.c") -// .flag("-mcmodel=large") -// .compile("cobj"); - gen_vector_asm().unwrap(); - } - if std::env::var("TARGET").unwrap().find("riscv32").is_some() { - cc::Build::new() - .file("src/arch/riscv32/compiler_rt.c") - .flag("-march=rv32ia") - .flag("-mabi=ilp32") - .compile("atomic_rt"); + println!("cargo:rerun-if-env-changed=LOG"); + + let arch: String = std::env::var("ARCH").unwrap(); + match arch.as_str() { + "x86_64" => { + // cc::Build::new() + // .file("src/arch/x86_64/driver/apic/lapic.c") + // .file("src/arch/x86_64/driver/keyboard/keyboard.c") + // .flag("-mcmodel=large") + // .compile("cobj"); + gen_vector_asm().unwrap(); + } + "riscv32" => { + cc::Build::new() + .file("src/arch/riscv32/compiler_rt.c") + .flag("-march=rv32ia") + .flag("-mabi=ilp32") + .compile("atomic_rt"); + if let Ok(file_path) = gen_sfsimg_asm() { + cc::Build::new() + .file(&file_path) + .flag("-march=rv32ia") + .flag("-mabi=ilp32") + .compile("sfsimg"); + } + } + "aarch64" => { + if let Ok(file_path) = gen_sfsimg_asm() { + cc::Build::new().file(&file_path).compile("cobj"); + } + } + _ => panic!("Unknown arch {}", arch), } } @@ -43,4 +62,28 @@ fn gen_vector_asm() -> Result<()> { writeln!(f, "\t.quad vector{}", i)?; } Ok(()) -} \ No newline at end of file +} + +fn gen_sfsimg_asm() -> Result { + let out_dir = std::env::var("OUT_DIR").unwrap(); + let sfsimg = std::env::var("SFSIMG").unwrap(); + + let file_path = Path::new(&out_dir).join("sfsimg.S"); + let mut f = File::create(&file_path).unwrap(); + + write!(f, "# generated by build.rs - do not edit")?; + write!(f, r#" + .section .rodata + .align 12 + .global _user_img_start + .global _user_img_end +_user_img_start: + .incbin "{}" +_user_img_end: +"#, sfsimg)?; + + println!("cargo:rerun-if-changed={}", sfsimg); + println!("cargo:rerun-if-env-changed=SFSIMG"); + + Ok(file_path) +} diff --git a/kernel/src/fs.rs b/kernel/src/fs.rs index 9fea7d7..a1a52f2 100644 --- a/kernel/src/fs.rs +++ b/kernel/src/fs.rs @@ -10,32 +10,19 @@ use crate::arch::driver::ide; use crate::sync::Condvar; use crate::sync::SpinNoIrqLock as Mutex; -// Hard link user program -#[cfg(target_arch = "riscv32")] -global_asm!(r#" - .section .rodata - .align 12 - .global _user_img_start - .global _user_img_end -_user_img_start: - .incbin "../user/user-riscv.img" -_user_img_end: -"#); - lazy_static! { pub static ref ROOT_INODE: Arc = { - #[cfg(target_arch = "riscv32")] + #[cfg(any(target_arch = "riscv32", target_arch = "aarch64"))] let device = { extern { fn _user_img_start(); fn _user_img_end(); } + // Hard link user program Box::new(unsafe { MemBuf::new(_user_img_start, _user_img_end) }) }; #[cfg(target_arch = "x86_64")] let device = Box::new(ide::IDE::new(1)); - #[cfg(target_arch = "aarch64")] - let device = unimplemented!(); let sfs = SimpleFileSystem::open(device).expect("failed to open SFS"); sfs.root_inode() diff --git a/kernel/src/shell.rs b/kernel/src/shell.rs index 1dcd521..b8443f3 100644 --- a/kernel/src/shell.rs +++ b/kernel/src/shell.rs @@ -6,12 +6,15 @@ use crate::fs::{ROOT_INODE, INodeExt}; use crate::process::*; pub fn run_user_shell() { - let inode = ROOT_INODE.lookup("sh").unwrap(); - let data = inode.read_as_vec().unwrap(); - processor().manager().add(ContextImpl::new_user(data.as_slice(), "sh".split(' ')), 0); + if let Ok(inode) = ROOT_INODE.lookup("sh") { + let data = inode.read_as_vec().unwrap(); + processor().manager().add(ContextImpl::new_user(data.as_slice(), "sh".split(' ')), 0); + } else { + processor().manager().add(ContextImpl::new_kernel(shell, 0), 0); + } } -pub fn shell() { +pub extern fn shell(_arg: usize) -> ! { let files = ROOT_INODE.list().unwrap(); println!("Available programs: {:?}", files); From e86229cb71cffe92f007e6e279b2c8ac448425b8 Mon Sep 17 00:00:00 2001 From: equation314 Date: Sun, 2 Dec 2018 19:44:05 +0800 Subject: [PATCH 3/6] user: user rust programs are runnable --- kernel/build.rs | 2 +- kernel/src/process/context.rs | 4 +++ kernel/src/syscall.rs | 1 + user/aarch64-ucore.json | 5 +++ user/riscv32-ucore.json | 7 +++- user/src/arch/aarch64/user.ld | 46 ++++++++++++++++++++++++ user/src/arch/riscv32/user.ld | 46 ++++++++++++++++++++++++ user/src/arch/x86_64/user.ld | 46 ++++++++++++++++++++++++ user/src/bin/{hello.rs => hello_rust.rs} | 4 ++- user/ucore-ulib/src/lang_items.rs | 42 +++++++++++++++++++--- user/ucore-ulib/src/syscall.rs | 37 +++++++++++++++++-- user/x86_64-ucore.json | 5 +++ 12 files changed, 234 insertions(+), 11 deletions(-) create mode 100644 user/src/arch/aarch64/user.ld create mode 100644 user/src/arch/riscv32/user.ld create mode 100644 user/src/arch/x86_64/user.ld rename user/src/bin/{hello.rs => hello_rust.rs} (50%) diff --git a/kernel/build.rs b/kernel/build.rs index e85f6f3..73117d7 100644 --- a/kernel/build.rs +++ b/kernel/build.rs @@ -33,7 +33,7 @@ fn main() { } "aarch64" => { if let Ok(file_path) = gen_sfsimg_asm() { - cc::Build::new().file(&file_path).compile("cobj"); + cc::Build::new().file(&file_path).compile("sfsimg"); } } _ => panic!("Unknown arch {}", arch), diff --git a/kernel/src/process/context.rs b/kernel/src/process/context.rs index 14b78b7..e5b5829 100644 --- a/kernel/src/process/context.rs +++ b/kernel/src/process/context.rs @@ -110,6 +110,10 @@ impl ContextImpl { unsafe { memory_set.with(|| { for ph in elf.program_iter() { + if ph.get_type() != Ok(Type::Load) { + continue; + } + let virt_addr = ph.virtual_addr() as usize; let offset = ph.offset() as usize; let file_size = ph.file_size() as usize; diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 98a0f9e..4935c29 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -284,6 +284,7 @@ fn get_file(fd: usize) -> Result<&'static Arc>, SysError> { pub type SysResult = Result; #[repr(i32)] +#[derive(Debug)] pub enum SysError { VfsError, InvalidFile, diff --git a/user/aarch64-ucore.json b/user/aarch64-ucore.json index 357c2e3..7d9e167 100644 --- a/user/aarch64-ucore.json +++ b/user/aarch64-ucore.json @@ -13,6 +13,11 @@ "linker": "rust-lld", "linker-flavor": "ld.lld", "linker-is-gnu": true, + "pre-link-args": { + "ld.lld": [ + "-Tsrc/arch/aarch64/user.ld" + ] + }, "llvm-target": "aarch64-unknown-none", "no-compiler-rt": true, "features": "+a53,+strict-align,-neon", diff --git a/user/riscv32-ucore.json b/user/riscv32-ucore.json index 37dac6f..354279e 100644 --- a/user/riscv32-ucore.json +++ b/user/riscv32-ucore.json @@ -11,6 +11,11 @@ "max-atomic-width": "32", "linker": "rust-lld", "linker-flavor": "ld.lld", + "pre-link-args": { + "ld.lld": [ + "-Tsrc/arch/riscv32/user.ld" + ] + }, "executables": true, "panic-strategy": "abort", "relocation-model": "static", @@ -27,4 +32,4 @@ "msp430-interrupt", "x86-interrupt" ] -} \ No newline at end of file +} diff --git a/user/src/arch/aarch64/user.ld b/user/src/arch/aarch64/user.ld new file mode 100644 index 0000000..b989922 --- /dev/null +++ b/user/src/arch/aarch64/user.ld @@ -0,0 +1,46 @@ +/* Simple linker script for ucore user-level programs. + See the GNU ld 'info' manual ("info ld") to learn the syntax. */ + +OUTPUT_ARCH(aarch64) +ENTRY(_start) + +SECTIONS { + /* Load programs at this address: "." means the current address */ + . = 0xffff000000000000; + + .text : { + *(.text .stub .text.* .gnu.linkonce.t.*) + } + + PROVIDE(etext = .); /* Define the 'etext' symbol to this value */ + + .rodata : { + *(.rodata .rodata.* .gnu.linkonce.r.*) + } + + /* Adjust the address for the data segment to the next page */ + . = ALIGN(0x1000); + + /* The data segment */ + .data : { + *(.data) + *(.data.*) + } + + .sdata : { + *(.sdata) + *(.sdata.*) + } + + PROVIDE(edata = .); + + .bss : { + *(.bss) + } + + PROVIDE(end = .); + + /DISCARD/ : { + *(.eh_frame .note.GNU-stack .comment) + } +} diff --git a/user/src/arch/riscv32/user.ld b/user/src/arch/riscv32/user.ld new file mode 100644 index 0000000..80ac4e5 --- /dev/null +++ b/user/src/arch/riscv32/user.ld @@ -0,0 +1,46 @@ +/* Simple linker script for ucore user-level programs. + See the GNU ld 'info' manual ("info ld") to learn the syntax. */ + +OUTPUT_ARCH(riscv) +ENTRY(_start) + +SECTIONS { + /* Load programs at this address: "." means the current address */ + . = 0x800020; + + .text : { + *(.text .stub .text.* .gnu.linkonce.t.*) + } + + PROVIDE(etext = .); /* Define the 'etext' symbol to this value */ + + .rodata : { + *(.rodata .rodata.* .gnu.linkonce.r.*) + } + + /* Adjust the address for the data segment to the next page */ + . = ALIGN(0x1000); + + /* The data segment */ + .data : { + *(.data) + *(.data.*) + } + + .sdata : { + *(.sdata) + *(.sdata.*) + } + + PROVIDE(edata = .); + + .bss : { + *(.bss) + } + + PROVIDE(end = .); + + /DISCARD/ : { + *(.eh_frame .note.GNU-stack .comment) + } +} diff --git a/user/src/arch/x86_64/user.ld b/user/src/arch/x86_64/user.ld new file mode 100644 index 0000000..b135c9f --- /dev/null +++ b/user/src/arch/x86_64/user.ld @@ -0,0 +1,46 @@ +/* Simple linker script for ucore user-level programs. + See the GNU ld 'info' manual ("info ld") to learn the syntax. */ + +OUTPUT_ARCH(x86_64) +ENTRY(_start) + +SECTIONS { + /* Load programs at this address: "." means the current address */ + . = 0x800020; + + .text : { + *(.text .stub .text.* .gnu.linkonce.t.*) + } + + PROVIDE(etext = .); /* Define the 'etext' symbol to this value */ + + .rodata : { + *(.rodata .rodata.* .gnu.linkonce.r.*) + } + + /* Adjust the address for the data segment to the next page */ + . = ALIGN(0x1000); + + /* The data segment */ + .data : { + *(.data) + *(.data.*) + } + + .sdata : { + *(.sdata) + *(.sdata.*) + } + + PROVIDE(edata = .); + + .bss : { + *(.bss) + } + + PROVIDE(end = .); + + /DISCARD/ : { + *(.eh_frame .note.GNU-stack .comment) + } +} diff --git a/user/src/bin/hello.rs b/user/src/bin/hello_rust.rs similarity index 50% rename from user/src/bin/hello.rs rename to user/src/bin/hello_rust.rs index 5a29680..72e3040 100644 --- a/user/src/bin/hello.rs +++ b/user/src/bin/hello_rust.rs @@ -7,5 +7,7 @@ extern crate ucore_ulib; // IMPORTANT: Must define main() like this #[no_mangle] pub fn main() { - println!("Hello uCore!"); + println!("Hello Rust uCore!"); + println!("I am process {}.", ucore_ulib::syscall::sys_getpid()); + println!("hello pass."); } diff --git a/user/ucore-ulib/src/lang_items.rs b/user/ucore-ulib/src/lang_items.rs index 89e5334..29aff8c 100644 --- a/user/ucore-ulib/src/lang_items.rs +++ b/user/ucore-ulib/src/lang_items.rs @@ -1,15 +1,47 @@ -use syscall::sys_exit; +use syscall::{sys_close, sys_dup, sys_exit, sys_open}; +use syscall::{O_RDONLY, O_WRONLY}; use core::alloc::Layout; use core::panic::PanicInfo; +// used for panic +macro_rules! print { + ($($arg:tt)*) => ({ + $crate::syscall::print_putc(format_args!($($arg)*)); + }); +} + #[linkage = "weak"] #[no_mangle] fn main() { panic!("No main() linked"); } +fn initfd(fd2: usize, path: &str, open_flags: usize) -> i32 { + let fd1 = sys_open(path, open_flags); + if fd1 < 0 { + return fd1; + } + let mut ret = fd1; + let fd1 = fd1 as usize; + if fd1 != fd2 { + sys_close(fd2); + ret = sys_dup(fd1, fd2); + sys_close(fd1); + } + return ret; +} + #[no_mangle] -pub extern fn _start(_argc: isize, _argv: *const *const u8) -> ! { +pub extern "C" fn _start(_argc: isize, _argv: *const *const u8) -> ! { + let fd = initfd(0, "stdin:", O_RDONLY); + if fd < 0 { + panic!("open failed: {}.", fd); + } + let fd = initfd(1, "stdout:", O_WRONLY); + if fd < 0 { + panic!("open failed: {}.", fd); + } + main(); sys_exit(0) } @@ -31,12 +63,12 @@ fn oom(_: Layout) -> ! { } #[no_mangle] -pub extern fn abort() -> ! { +pub extern "C" fn abort() -> ! { sys_exit(2) } #[no_mangle] -pub extern fn __mulsi3(mut a: u32, mut b: u32) -> u32 { +pub extern "C" fn __mulsi3(mut a: u32, mut b: u32) -> u32 { let mut r: u32 = 0; while a > 0 { @@ -48,4 +80,4 @@ pub extern fn __mulsi3(mut a: u32, mut b: u32) -> u32 { } r -} \ No newline at end of file +} diff --git a/user/ucore-ulib/src/syscall.rs b/user/ucore-ulib/src/syscall.rs index f707142..1a75785 100644 --- a/user/ucore-ulib/src/syscall.rs +++ b/user/ucore-ulib/src/syscall.rs @@ -17,14 +17,29 @@ pub fn print(args: fmt::Arguments) { StdOut.write_fmt(args).unwrap(); } +pub fn print_putc(args: fmt::Arguments) { + SysPutc.write_fmt(args).unwrap(); +} + struct StdOut; +struct SysPutc; impl fmt::Write for StdOut { fn write_str(&mut self, s: &str) -> fmt::Result { - match sys_write(0, s.as_ptr(), s.len()) { - 0 => Ok(()), - _ => Err(fmt::Error::default()), + if sys_write(1, s.as_ptr(), s.len()) >= 0 { + Ok(()) + } else { + Err(fmt::Error::default()) + } + } +} + +impl fmt::Write for SysPutc { + fn write_str(&mut self, s: &str) -> fmt::Result { + for c in s.bytes() { + sys_putc(c as char); } + Ok(()) } } @@ -77,6 +92,10 @@ pub fn sys_close(fd: usize) -> i32 { sys_call(SYS_CLOSE, fd, 0, 0, 0, 0, 0) } +pub fn sys_dup(fd1: usize, fd2: usize) -> i32 { + sys_call(SYS_DUP, fd1, fd2, 0, 0, 0, 0) +} + /// Fork the current process. Return the child's PID. pub fn sys_fork() -> i32 { sys_call(SYS_FORK, 0, 0, 0, 0, 0, 0) @@ -144,3 +163,15 @@ const SYS_GETCWD: usize = 121; const SYS_GETDIRENTRY: usize = 128; const SYS_DUP: usize = 130; const SYS_LAB6_SET_PRIORITY: usize = 255; + +/* VFS flags */ +// TODO: use bitflags +// flags for open: choose one of these +pub const O_RDONLY: usize = 0; // open for reading only +pub const O_WRONLY: usize = 1; // open for writing only +pub const O_RDWR: usize = 2; // open for reading and writing +// then or in any of these: +pub const O_CREAT: usize = 0x00000004; // create file if it does not exist +pub const O_EXCL: usize = 0x00000008; // error if O_CREAT and the file exists +pub const O_TRUNC: usize = 0x00000010; // truncate file upon open +pub const O_APPEND: usize = 0x00000020; // append on each write diff --git a/user/x86_64-ucore.json b/user/x86_64-ucore.json index 6510efa..a8ace2c 100644 --- a/user/x86_64-ucore.json +++ b/user/x86_64-ucore.json @@ -9,6 +9,11 @@ "executables": true, "linker": "rust-lld", "linker-flavor": "ld.lld", + "pre-link-args": { + "ld.lld": [ + "-Tsrc/arch/x86_64/user.ld" + ] + }, "panic-strategy": "abort", "disable-redzone": true, "features": "-mmx,-sse,+soft-float" From 710b6d1a28f518fde70315be949be1ee239454ab Mon Sep 17 00:00:00 2001 From: equation314 Date: Sun, 2 Dec 2018 19:51:52 +0800 Subject: [PATCH 4/6] user: update Makefiles --- kernel/Makefile | 16 +++++++--------- user/Makefile | 12 ++++++++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/kernel/Makefile b/kernel/Makefile index 3f4f76b..7771d54 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -32,11 +32,9 @@ user_dir := ../user user_bin_path := $(user_dir)/target/$(arch)-ucore/debug user_bins := $(patsubst $(user_bin_path)/%.d, $(user_bin_path)/%, $(wildcard $(user_bin_path)/*.d)) user_obj := build/$(arch)/user.o -sfsroot := $(user_dir)/build/sfsroot-$(arch) -sfsimg := $(user_dir)/build/user-$(arch).img export ARCH = $(arch) -export SFSIMG = $(sfsimg) +export SFSIMG = $(user_dir)/build/user-$(arch).img ### qemu options ### ifeq ($(arch), x86_64) @@ -105,7 +103,7 @@ cc := $(prefix)gcc as := $(prefix)as gdb := $(prefix)gdb -.PHONY: all clean run build asm doc justrun debug kernel install +.PHONY: all clean run build asm doc justrun debug kernel sfsimg install all: kernel @@ -116,12 +114,12 @@ clean: doc: @cargo rustdoc -- --document-private-items -run: build justrun +run: build sfsimg justrun -justrun: $(sfsimg) +justrun: @qemu-system-$(arch) $(qemu_opts) || [ $$? -eq 11 ] # run qemu and assert it exit 11 -debug: $(kernel) $(bin) $(sfsimg) +debug: $(kernel) $(bin) @qemu-system-$(arch) $(qemu_opts) -s -S & @sleep 1 @$(gdb) $(kernel) -x ../tools/gdbinit @@ -166,7 +164,7 @@ ifeq ($(arch), x86_64) kernel: @bootimage build $(build_args) else -kernel: $(sfsimg) +kernel: sfsimg ifeq ($(arch), riscv32) @-patch -p0 -N -b \ $(shell rustc --print sysroot)/lib/rustlib/src/rust/src/libcore/sync/atomic.rs \ @@ -178,7 +176,7 @@ endif ### user programs ### -$(sfsimg): $(sfsroot) +sfsimg: @cd $(user_dir) && make sfsimg # make user.o from binary files diff --git a/user/Makefile b/user/Makefile index 9b0648b..6c5f860 100644 --- a/user/Makefile +++ b/user/Makefile @@ -35,16 +35,20 @@ build-c: build: build-rust build-c +$(user_rust_bins): build-rust + +$(user_c_bins): build-c + mksfs: ifeq ($(shell which $(mksfs)),) @cargo install --git https://github.com/wangrunji0408/SimpleFileSystem-Rust --features="std" endif -$(sfsroot): build - @mkdir -p $(sfsroot)/ - @cp $(user_bins) $(sfsroot)/ +$(sfsroot): $(user_bins) + @mkdir -p $@ + @cp $^ $@/ -$(sfsimg): mksfs $(sfsroot) +$(sfsimg): $(user_bins) $(sfsroot) | mksfs @echo Creating SFS image at $@ @$(mksfs) zip $(sfsroot) $@ From 66ac30d4014604bee395b37f94d12ae1335b1834 Mon Sep 17 00:00:00 2001 From: equation314 Date: Sun, 2 Dec 2018 19:56:29 +0800 Subject: [PATCH 5/6] user: update make clean in kernel/ --- kernel/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/Makefile b/kernel/Makefile index 7771d54..d8ed131 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -109,6 +109,7 @@ all: kernel clean: @cargo clean + @cd $(user_dir) && make clean @rm -rf ../riscv-pk/build doc: From d5a6eb2020658b3dd1c4825032397053f89295ea Mon Sep 17 00:00:00 2001 From: equation314 Date: Mon, 3 Dec 2018 12:30:03 +0800 Subject: [PATCH 6/6] user: fix build error --- kernel/Cargo.toml | 1 + kernel/Makefile | 2 +- user/ucore-ulib/src/lang_items.rs | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index e8ba981..aa429ce 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -63,6 +63,7 @@ minimum-image-size = 0 # The minimum output file size (in MiB) # (the "{}" will be replaced with the path to the bootable disk image) run-command = ["qemu-system-x86_64", "-drive", "format=raw,file={}", + # TODO: use SFSIMG environment variable "-drive", "format=raw,file=../user/img/ucore-i386-pic.img,media=disk,cache=writeback", "-serial", "mon:stdio", "-device", "isa-debug-exit", diff --git a/kernel/Makefile b/kernel/Makefile index beaee8a..87668a4 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -48,7 +48,7 @@ qemu_opts := \ ifeq ($(arch), x86_64) qemu_opts += \ -drive format=raw,file=$(bootimage) \ - -drive format=raw,file="../user/img/ucore-i386-pic.img",media=disk,cache=writeback \ + -drive format=raw,file=$(SFSIMG),media=disk,cache=writeback \ -serial mon:stdio \ -device isa-debug-exit diff --git a/user/ucore-ulib/src/lang_items.rs b/user/ucore-ulib/src/lang_items.rs index 4fd3ed2..e1e58d8 100644 --- a/user/ucore-ulib/src/lang_items.rs +++ b/user/ucore-ulib/src/lang_items.rs @@ -1,5 +1,5 @@ -use syscall::{sys_close, sys_dup, sys_exit, sys_open}; -use syscall::{O_RDONLY, O_WRONLY}; +use crate::syscall::{sys_close, sys_dup, sys_exit, sys_open}; +use crate::syscall::{O_RDONLY, O_WRONLY}; use core::alloc::Layout; use core::panic::PanicInfo;