From 1bde1ad9fe68dcbf747d99e78d62fc30e8955c30 Mon Sep 17 00:00:00 2001 From: hky1999 <976929993@qq.com> Date: Mon, 3 Jul 2023 22:40:44 +0800 Subject: [PATCH 1/2] feat: add unilib unlink support --- src/kernel/hvc.rs | 6 ++++-- src/lib/unilib.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/kernel/hvc.rs b/src/kernel/hvc.rs index 9bd2505..d339c48 100644 --- a/src/kernel/hvc.rs +++ b/src/kernel/hvc.rs @@ -108,8 +108,9 @@ pub const HVC_UNILIB_FS_READ: usize = 3; pub const HVC_UNILIB_FS_WRITE: usize = 4; pub const HVC_UNILIB_FS_LSEEK: usize = 5; pub const HVC_UNILIB_FS_STAT: usize = 6; -pub const HVC_UNILIB_FS_APPEND: usize = 7; -pub const HVC_UNILIB_FS_FINISHED: usize = 8; +pub const HVC_UNILIB_FS_UNLINK: usize = 7; +pub const HVC_UNILIB_FS_APPEND: usize = 0x10; +pub const HVC_UNILIB_FS_FINISHED: usize = 0x11; // hvc_config_event pub const HVC_CONFIG_ADD_VM: usize = 0; @@ -436,6 +437,7 @@ fn hvc_unilib_handler(event: usize, x0: usize, x1: usize, x2: usize) -> Result unilib_fs_write(x0, x1, x2), HVC_UNILIB_FS_LSEEK => unilib_fs_lseek(x0, x1, x2), HVC_UNILIB_FS_STAT => unilib_fs_stat(), + HVC_UNILIB_FS_UNLINK => unilib_fs_unlink(x0, x1), HVC_UNILIB_FS_APPEND => unilib_fs_append(x0), HVC_UNILIB_FS_FINISHED => unilib_fs_finished(x0), _ => { diff --git a/src/lib/unilib.rs b/src/lib/unilib.rs index 359973a..c8af431 100644 --- a/src/lib/unilib.rs +++ b/src/lib/unilib.rs @@ -21,7 +21,7 @@ use alloc::collections::BTreeMap; use spin::Mutex; use crate::lib::{memcpy_safe, sleep}; -use crate::kernel::{vm_ipa2pa, active_vm, HVC_UNILIB_FS_INIT, HVC_UNILIB_FS_LSEEK}; +use crate::kernel::{vm_ipa2pa, active_vm, HVC_UNILIB_FS_INIT, HVC_UNILIB_FS_LSEEK, HVC_UNILIB_FS_UNLINK}; use crate::kernel::{HvcGuestMsg, HvcUniLibMsg, hvc_send_msg_to_vm}; use crate::kernel::HVC_UNILIB; use crate::kernel::{HVC_UNILIB_FS_OPEN, HVC_UNILIB_FS_CLOSE, HVC_UNILIB_FS_READ, HVC_UNILIB_FS_WRITE}; @@ -119,7 +119,7 @@ impl UnilibFS { fn loop_for_response(&self) -> usize { loop { if self.flag() != 0 { - println!( + trace!( "unilib operation finished, flag {}, value {}", self.flag(), self.value() @@ -465,3 +465,51 @@ pub fn unilib_fs_lseek(fd: usize, offset: usize, whence: usize) -> Result Result { unimplemented!("stat is unimplemented"); } + +/// **Unlink** API for unilib fs. +/// HVC_UNILIB | HVC_UNILIB_FS_UNLINK +/// Currently unsupported. +pub fn unilib_fs_unlink(path_start_ipa: usize, path_length: usize) -> Result { + let vm = active_vm().unwrap(); + let vm_id = vm.id(); + // println!( + // "VM[{}] unilib fs unlink path_ipa: {:x}, path_length {}", + // vm_id, path_start_ipa, path_length + // ); + // Get fs_cfg struct according to vm_id. + let fs_list_lock = UNILIB_FS_LIST.lock(); + let fs_cfg = match fs_list_lock.get(&vm_id) { + Some(cfg) => cfg, + None => { + println!("VM[{}] doesn't register a unilib fs, return", vm_id); + return Err(()); + } + }; + + // Copy path to unilib_fs buf, see UnilibFSCfg. + let path_pa = vm_ipa2pa(active_vm().unwrap(), path_start_ipa); + memcpy_safe(fs_cfg.get_buf(), path_pa as *mut u8, path_length); + // Add end '\0' for path buf. + unsafe { + *((fs_cfg.get_buf() as usize + path_length) as *mut u8) = 0u8; + } + + fs_cfg.prepare_for_request(); + + // Notify MVM to operate the fs operation. + let unilib_msg = HvcUniLibMsg { + fid: HVC_UNILIB, + event: HVC_UNILIB_FS_UNLINK, + vm_id: vm.id(), + arg_1: path_length, + arg_2: 0, + arg_3: 0, + }; + if !hvc_send_msg_to_vm(0, &HvcGuestMsg::UniLib(unilib_msg)) { + println!("unilib fs unlink: failed to notify VM 0"); + return Err(()); + } + + // Still, we need to enter a loop, wait for VM to complete operation. + Ok(fs_cfg.loop_for_response()) +} -- Gitee From bb8a824f5007cbb161f5ec33f38ccaace7ff1ecf Mon Sep 17 00:00:00 2001 From: moce0627 <491745115@qq.com> Date: Fri, 7 Jul 2023 16:04:57 +0800 Subject: [PATCH 2/2] toolchain: compiler configuration, linker script and makefile --- Makefile | 64 +++++++++---------- README.ch.md | 2 +- README.md | 2 +- aarch64-qemu.json | 27 -------- aarch64-tx2-update.json | 25 -------- aarch64-tx2.json | 26 -------- aarch64-pi4.json => aarch64.json | 5 -- build.rs | 19 ++++++ .../aarch64-tx2.ld => linkers/aarch64.ld | 2 +- src/linkers/aarch64-pi4.ld | 42 ------------ src/linkers/aarch64-qemu.ld | 41 ------------ src/linkers/aarch64-tx2-update.ld | 41 ------------ 12 files changed, 52 insertions(+), 244 deletions(-) delete mode 100644 aarch64-qemu.json delete mode 100644 aarch64-tx2-update.json delete mode 100644 aarch64-tx2.json rename aarch64-pi4.json => aarch64.json (86%) rename src/linkers/aarch64-tx2.ld => linkers/aarch64.ld (96%) delete mode 100644 src/linkers/aarch64-pi4.ld delete mode 100644 src/linkers/aarch64-qemu.ld delete mode 100644 src/linkers/aarch64-tx2-update.ld diff --git a/Makefile b/Makefile index 6d2401d..a73529d 100644 --- a/Makefile +++ b/Makefile @@ -4,54 +4,58 @@ DISK = vm0.img # Compile ARCH ?= aarch64 -BUILD_STD = core,alloc +PROFILE ?= release +BOARD ?= tx2 +# features, seperate with comma `,` +FEATURES = # Toolchain TOOLCHAIN=aarch64-none-elf QEMU = qemu-system-aarch64 GDB = ${TOOLCHAIN}-gdb OBJDUMP = ${TOOLCHAIN}-objdump +OBJCOPY = ${TOOLCHAIN}-objcopy IMAGE=rust_shyper -qemu_debug: - cargo build -Z build-std=${BUILD_STD} --target aarch64-qemu.json --features qemu - ${TOOLCHAIN}-objcopy target/aarch64-qemu/debug/${IMAGE} -O binary target/aarch64-qemu/debug/${IMAGE}.bin - ${OBJDUMP} --demangle -d target/aarch64-qemu/debug/${IMAGE} > target/aarch64-qemu/debug/t.txt +TARGET_DIR=target/${ARCH}/${PROFILE} -qemu_release: - cargo build -Z build-std=${BUILD_STD} --target aarch64-qemu.json --features qemu --release - ${TOOLCHAIN}-objcopy target/aarch64-qemu/release/${IMAGE} -O binary target/aarch64-qemu/release/${IMAGE}.bin - ${OBJDUMP} --demangle -d target/aarch64-qemu/release/${IMAGE} > target/aarch64-qemu/release/t.txt +# Cargo flags. +CARGO_FLAGS ?= -Z build-std=core,alloc --target ${ARCH}.json --no-default-features --features ${BOARD},${FEATURES} +ifeq (${PROFILE}, release) +CARGO_FLAGS := ${CARGO_FLAGS} --release +endif -tx2: - cargo build -Z build-std=${BUILD_STD} --target aarch64-tx2.json --features tx2 - # bash upload - ${OBJDUMP} --demangle -d target/aarch64-tx2/debug/${IMAGE} > target/aarch64-tx2/debug/t.txt +.PHONY: build qemu tx2 pi4 tx2_update tx2_ramdisk gdb clean + +build: + cargo build ${CARGO_FLAGS} + ${OBJDUMP} --demangle -d ${TARGET_DIR}/${IMAGE} > ${TARGET_DIR}/t.txt -tx2_release: - cargo build -Z build-std=${BUILD_STD} --target aarch64-tx2.json --features tx2 --release +qemu: + $(MAKE) build BOARD=qemu + ${OBJCOPY} ${TARGET_DIR}/${IMAGE} -O binary ${TARGET_DIR}/${IMAGE}.bin + +tx2: + $(MAKE) build BOARD=tx2 # bash upload_release - ${OBJDUMP} --demangle -d target/aarch64-tx2/release/${IMAGE} > target/aarch64-tx2/release/t.txt tx2_ramdisk: - cargo build -Z build-std=${BUILD_STD} --target aarch64-tx2.json --features "tx2 ramdisk" --release + $(MAKE) build BOARD=tx2 FEATURES=ramdisk # bash upload_release - ${OBJDUMP} --demangle -d target/aarch64-tx2/release/${IMAGE} > target/aarch64-tx2/release/t.txt tx2_update: - cargo build -Z build-std=${BUILD_STD} --target aarch64-tx2-update.json --features "tx2 update" --release + $(MAKE) build BOARD=tx2 FEATURES=update # bash upload_update - ${OBJDUMP} --demangle -d target/aarch64-tx2-update/release/${IMAGE} > target/aarch64-tx2-update/release/update.txt -pi4_release: - cargo build -Z build-std=${BUILD_STD} --target aarch64-pi4.json --features pi4 --release +pi4: + $(MAKE) build BOARD=pi4 # bash pi4_upload_release - ${OBJDUMP} --demangle -d target/aarch64-pi4/release/${IMAGE} > target/aarch64-pi4/release/t.txt QEMU_COMMON_OPTIONS = -machine virt,virtualization=on,gic-version=2\ - -m 8g -cpu cortex-a57 -smp 4 -display none -global virtio-mmio.force-legacy=false + -m 8g -cpu cortex-a57 -smp 4 -display none -global virtio-mmio.force-legacy=false\ + -kernel ${TARGET_DIR}/${IMAGE}.bin QEMU_SERIAL_OPTIONS = -serial mon:stdio #\ -serial telnet:localhost:12345,server @@ -60,21 +64,13 @@ QEMU_NETWORK_OPTIONS = -netdev user,id=n0,hostfwd=tcp::5555-:22 -device virtio-n QEMU_DISK_OPTIONS = -drive file=${DISK},if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.25 -run: qemu_debug +run: qemu ${QEMU} ${QEMU_COMMON_OPTIONS} ${QEMU_SERIAL_OPTIONS} ${QEMU_NETWORK_OPTIONS} ${QEMU_DISK_OPTIONS} \ - -kernel target/aarch64-qemu/debug/${IMAGE}.bin -run_release: qemu_release +debug: qemu ${QEMU} ${QEMU_COMMON_OPTIONS} ${QEMU_SERIAL_OPTIONS} ${QEMU_NETWORK_OPTIONS} ${QEMU_DISK_OPTIONS} \ - -kernel target/aarch64-qemu/release/${IMAGE}.bin - -debug: qemu_debug - ${QEMU} ${QEMU_COMMON_OPTIONS} ${QEMU_SERIAL_OPTIONS} ${QEMU_NETWORK_OPTIONS} ${QEMU_DISK_OPTIONS} \ - -kernel target/aarch64-qemu/debug/${IMAGE}.bin \ -s -S -.PHONY: gdb clean - gdb: ${GDB} -x gdb/aarch64.gdb diff --git a/README.ch.md b/README.ch.md index a09039b..7f17aa4 100644 --- a/README.ch.md +++ b/README.ch.md @@ -34,7 +34,7 @@ Rust-Shyper是由北航计算机学院操作系统研究团队,在华为技术 make ``` -例如, `make tx2_release` 是编译包含优化Rust-Shyper的TX2版本。具体可查看Makefile文件。 +例如, `make tx2` 是编译Rust-Shyper的TX2版本。具体可查看Makefile文件。 主要注意的是,请在编译前,根据需求编辑管理虚拟机(MVM)的配置文件。该文件的路径是 src/config/\_def.rs. diff --git a/README.md b/README.md index c505f90..94d1f8d 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Simply run `make` make ``` -For example, `make tx2_release` is to build Rust-Shyper for TX2 with opimization. +For example, `make tx2` is to build Rust-Shyper for TX2. Note that please edit the MVM profile in src/config/\_def.rs according to your requirements. diff --git a/aarch64-qemu.json b/aarch64-qemu.json deleted file mode 100644 index 0763cc9..0000000 --- a/aarch64-qemu.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "arch": "aarch64", - "data-layout": "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", - "disable-redzone": true, - "env": "", - "executables": true, - "features": "+v8a,+strict-align,-neon,-fp-armv8", - "is-builtin": false, - "linker": "rust-lld", - "linker-flavor": "ld.lld", - "linker-is-gnu": true, - "pre-link-args": { - "ld.lld": [ - "-Tsrc/linkers/aarch64-qemu.ld" - ] - }, - "llvm-target": "aarch64-unknown-none", - "max-atomic-width": 128, - "os": "none", - "panic-strategy": "abort", - "relocation-model": "static", - "code-model": "large", - "target-c-int-width": "32", - "target-endian": "little", - "target-pointer-width": "64", - "vendor": "" -} \ No newline at end of file diff --git a/aarch64-tx2-update.json b/aarch64-tx2-update.json deleted file mode 100644 index a12ee6b..0000000 --- a/aarch64-tx2-update.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "arch": "aarch64", - "data-layout": "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", - "disable-redzone": true, - "executables": true, - "features": "+v8a,+strict-align,-neon,-fp-armv8", - "is-builtin": false, - "linker": "rust-lld", - "linker-flavor": "ld.lld", - "linker-is-gnu": true, - "pre-link-args": { - "ld.lld": [ - "-Tsrc/linkers/aarch64-tx2-update.ld" - ] - }, - "llvm-target": "aarch64-unknown-none", - "max-atomic-width": 128, - "os": "none", - "panic-strategy": "abort", - "relocation-model": "static", - "code-model": "large", - "target-c-int-width": "32", - "target-endian": "little", - "target-pointer-width": "64" -} \ No newline at end of file diff --git a/aarch64-tx2.json b/aarch64-tx2.json deleted file mode 100644 index b05790a..0000000 --- a/aarch64-tx2.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "arch": "aarch64", - "cpu": "cortex-a57", - "data-layout": "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", - "disable-redzone": true, - "executables": true, - "features": "+v8a,+strict-align,-neon,-fp-armv8", - "is-builtin": false, - "linker": "rust-lld", - "linker-flavor": "ld.lld", - "linker-is-gnu": true, - "pre-link-args": { - "ld.lld": [ - "-Tsrc/linkers/aarch64-tx2.ld" - ] - }, - "llvm-target": "aarch64-unknown-none", - "max-atomic-width": 128, - "os": "none", - "panic-strategy": "abort", - "relocation-model": "static", - "code-model": "large", - "target-c-int-width": "32", - "target-endian": "little", - "target-pointer-width": "64" -} \ No newline at end of file diff --git a/aarch64-pi4.json b/aarch64.json similarity index 86% rename from aarch64-pi4.json rename to aarch64.json index 85aebdf..a0c5a75 100644 --- a/aarch64-pi4.json +++ b/aarch64.json @@ -8,11 +8,6 @@ "linker": "rust-lld", "linker-flavor": "ld.lld", "linker-is-gnu": true, - "pre-link-args": { - "ld.lld": [ - "-Tsrc/linkers/aarch64-pi4.ld" - ] - }, "llvm-target": "aarch64-unknown-none", "max-atomic-width": 128, "os": "none", diff --git a/build.rs b/build.rs index 351d51e..09ed177 100644 --- a/build.rs +++ b/build.rs @@ -10,6 +10,7 @@ use std::process::Command; use std::fs; +use std::env::var; fn main() { let files = fs::read_dir("libfdt-binding").unwrap().into_iter().filter_map(|f| { @@ -26,6 +27,24 @@ fn main() { .files(files) .flag("-w") .compile("fdt-binding"); + + let arch = var("CARGO_CFG_TARGET_ARCH").unwrap(); + let text_start = if cfg!(feature = "tx2") { + if cfg!(feature = "update") { + 0x8a000000_u64 + } else { + 0x83000000_u64 + } + } else if cfg!(feature = "pi4") { + 0xf0080000_u64 + } else if cfg!(feature = "qemu") { + 0x40080000_u64 + } else { + panic!("Unsupported platform!"); + }; + println!("cargo:rustc-link-arg=-Tlinkers/{arch}.ld"); + println!("cargo:rustc-link-arg=--defsym=TEXT_START={text_start}"); + // note: add error checking yourself. let output = Command::new("date").arg("+\"%Y-%m-%d %H:%M:%S %Z\"").output().unwrap(); let build_time = String::from_utf8(output.stdout).unwrap(); diff --git a/src/linkers/aarch64-tx2.ld b/linkers/aarch64.ld similarity index 96% rename from src/linkers/aarch64-tx2.ld rename to linkers/aarch64.ld index 767ee53..e9dfe88 100644 --- a/src/linkers/aarch64-tx2.ld +++ b/linkers/aarch64.ld @@ -1,7 +1,7 @@ ENTRY(_start) SECTIONS { - . = 0x83000000; + . = TEXT_START; _image_start = ABSOLUTE(.); diff --git a/src/linkers/aarch64-pi4.ld b/src/linkers/aarch64-pi4.ld deleted file mode 100644 index 26096ec..0000000 --- a/src/linkers/aarch64-pi4.ld +++ /dev/null @@ -1,42 +0,0 @@ -ENTRY(_start) - -SECTIONS -{ - . = 0xf0080000; - - _image_start = ABSOLUTE(.); - - .boot : { - *(.text.boot) - *(.data.boot) - } - - .text : { - *(.text*) - } - - .rodata : { - *(.rodata*) - } - - - .data : { - *(.data*) - } - - . = ALIGN(4096); - _bss_begin = .; - .bss (NOLOAD) : ALIGN(4096) { - *(.bss*) - } - . = ALIGN(4096); - _bss_end = .; - - _image_end = ABSOLUTE(.); - - . = 0x400000000; - - .cpu_private (NOLOAD) : ALIGN(4096) { - *(.cpu_private) - } -} diff --git a/src/linkers/aarch64-qemu.ld b/src/linkers/aarch64-qemu.ld deleted file mode 100644 index b2108f3..0000000 --- a/src/linkers/aarch64-qemu.ld +++ /dev/null @@ -1,41 +0,0 @@ -ENTRY(_start) -SECTIONS -{ - . = 0x40080000; - - _image_start = ABSOLUTE(.); - - .boot : { - *(.text.boot) - *(.data.boot) - } - - .text : { - *(.text*) - } - - .rodata : { - *(.rodata*) - } - - - .data : { - *(.data*) - } - - . = ALIGN(4096); - _bss_begin = .; - .bss (NOLOAD) : ALIGN(4096) { - *(.bss*) - } - . = ALIGN(4096); - _bss_end = .; - - _image_end = ABSOLUTE(.); - - . = 0x400000000; - - .cpu_private (NOLOAD) : ALIGN(4096) { - *(.cpu_private) - } -} diff --git a/src/linkers/aarch64-tx2-update.ld b/src/linkers/aarch64-tx2-update.ld deleted file mode 100644 index 002389b..0000000 --- a/src/linkers/aarch64-tx2-update.ld +++ /dev/null @@ -1,41 +0,0 @@ -ENTRY(_start) -SECTIONS -{ - . = 0x8a000000; - - _image_start = ABSOLUTE(.); - - .boot : { - *(.text.boot) - *(.data.boot) - } - - .text : { - *(.text*) - } - - .rodata : { - *(.rodata*) - } - - - .data : { - *(.data*) - } - - . = ALIGN(4096); - _bss_begin = .; - .bss (NOLOAD) : ALIGN(4096) { - *(.bss*) - } - . = ALIGN(4096); - _bss_end = .; - - _image_end = ABSOLUTE(.); - - . = 0x400000000; - - .cpu_private (NOLOAD) : ALIGN(4096) { - *(.cpu_private) - } -} -- Gitee