diff --git a/.cargo/config.toml b/.cargo/config.toml index e13558992..74e3b891e 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -3,11 +3,12 @@ runner = "cargo run --package jinux-build --" [alias] -kcheck = "check --target x86_64-custom.json -Zbuild-std=core,alloc,compiler_builtins -Zbuild-std-features=compiler-builtins-mem" -kbuild = "build --target x86_64-custom.json -Zbuild-std=core,alloc,compiler_builtins -Zbuild-std-features=compiler-builtins-mem" -krun = "run --target x86_64-custom.json -Zbuild-std=core,alloc,compiler_builtins -Zbuild-std-features=compiler-builtins-mem" -ktest = "test --target x86_64-custom.json -Zbuild-std=core,alloc,compiler_builtins -Zbuild-std-features=compiler-builtins-mem" -component-check = "component check --target x86_64-custom.json -Zbuild-std=core,alloc,compiler_builtins -Zbuild-std-features=compiler-builtins-mem" +kcheck = "check --target x86_64-custom.json -Zbuild-std=core,alloc,compiler_builtins -Zbuild-std-features=compiler-builtins-mem" +kbuild = "build --target x86_64-custom.json -Zbuild-std=core,alloc,compiler_builtins -Zbuild-std-features=compiler-builtins-mem" +krun = "run --target x86_64-custom.json -Zbuild-std=core,alloc,compiler_builtins -Zbuild-std-features=compiler-builtins-mem" +keval = "run --target x86_64-custom.json -Zbuild-std=core,alloc,compiler_builtins -Zbuild-std-features=compiler-builtins-mem -- --eval" +ktest = "test --target x86_64-custom.json -Zbuild-std=core,alloc,compiler_builtins -Zbuild-std-features=compiler-builtins-mem" +component-check = "component check --target x86_64-custom.json -Zbuild-std=core,alloc,compiler_builtins -Zbuild-std-features=compiler-builtins-mem" [install] root = "./target" diff --git a/Cargo.lock b/Cargo.lock index 6f44ae0fb..36174c21c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,6 +47,55 @@ dependencies = [ "spinning_top", ] +[[package]] +name = "anstream" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is-terminal", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a30da5c5f2d5e72842e00bcb57657162cdabef0931f40e2deb9b4140440cecd" + +[[package]] +name = "anstyle-parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "anyhow" version = "1.0.32" @@ -131,12 +180,65 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fd304a20bff958a57f04c4e96a2e7594cc4490a0e809cbd48bb6437edaa452d" +dependencies = [ + "clap_builder", + "clap_derive", + "once_cell", +] + +[[package]] +name = "clap_builder" +version = "4.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01c6a3f08f1fe5662a35cfe393aec09c4df95f60ee93b7556505260f75eee9e1" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.15", +] + +[[package]] +name = "clap_lex" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "component" version = "0.1.0" @@ -303,6 +405,27 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" +[[package]] +name = "errno" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" +dependencies = [ + "errno-dragonfly", + "libc", + "windows-sys", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "ext-trait" version = "1.0.1" @@ -409,6 +532,18 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] +name = "hermit-abi" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b" + [[package]] name = "ident_case" version = "1.0.1" @@ -470,6 +605,17 @@ dependencies = [ "ghost", ] +[[package]] +name = "is-terminal" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys", +] + [[package]] name = "itertools" version = "0.10.5" @@ -511,8 +657,7 @@ name = "jinux-build" version = "0.1.0" dependencies = [ "anyhow", - "cfg-if", - "runner-utils", + "clap", ] [[package]] @@ -761,6 +906,12 @@ dependencies = [ "rle-decode-fast", ] +[[package]] +name = "linux-raw-sys" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09fc20d2ca12cb9f044c93e3bd6d32d523e6e2ec3db4f7b2939cd99026ecd3f0" + [[package]] name = "lock_api" version = "0.4.9" @@ -1003,16 +1154,6 @@ dependencies = [ "log", ] -[[package]] -name = "runner-utils" -version = "0.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9dc6848b056990cd51e72aa5556bdbea4a96013e8b18635d183c84159c2988f" -dependencies = [ - "thiserror", - "wait-timeout", -] - [[package]] name = "rustc_version" version = "0.4.0" @@ -1022,6 +1163,19 @@ dependencies = [ "semver", ] +[[package]] +name = "rustix" +version = "0.38.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac5ffa1efe7548069688cd7028f32591853cd7b5b756d41bcffd2353e4fc75b4" +dependencies = [ + "bitflags 2.3.3", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "rustversion" version = "1.0.9" @@ -1254,9 +1408,9 @@ checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" [[package]] name = "utf8parse" -version = "0.2.0" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" [[package]] name = "version_check" @@ -1297,21 +1451,78 @@ dependencies = [ "quote", ] -[[package]] -name = "wait-timeout" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" -dependencies = [ - "libc", -] - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" + [[package]] name = "winnow" version = "0.3.3" diff --git a/Makefile b/Makefile index 34311466b..97753c49e 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all build clean docs fmt run setup test tools +.PHONY: all build clean docs fmt run setup test tools eval all: build @@ -18,6 +18,11 @@ tools: run: build @cargo krun +# Test Jinux in a QEMU guest VM and run a series of evaluations. +eval: build + @cargo keval + +# The usermode cargo test of Jinux frame and Jinux standard library. test: build @cargo ktest diff --git a/build/Cargo.toml b/build/Cargo.toml index 310190d2a..96acfe5f9 100644 --- a/build/Cargo.toml +++ b/build/Cargo.toml @@ -5,10 +5,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -runner-utils = "0.0.2" anyhow = "1.0.32" -cfg-if = "1.0" - -[features] -default = [] -iommu = [] +clap = { version = "4.3.19", features = ["derive"] } diff --git a/build/src/main.rs b/build/src/main.rs index cbc400b3a..fa7578968 100644 --- a/build/src/main.rs +++ b/build/src/main.rs @@ -1,21 +1,41 @@ -use anyhow::anyhow; +//! This is the Jinux runner to ease the pain of +//! running and testing Jinux inside a QEMU VM. + use std::{ fs::OpenOptions, - ops::Add, path::{Path, PathBuf}, - process::{Command, ExitStatus}, - time::Duration, + process::Command, }; +use clap::Parser; + +/// The CLI of this runner. +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Args { + // Positional arguments. + /// The Jinux binary path. + path: PathBuf, + + // Options. + /// Automatically run integration tests and exit. + #[arg(short, long, default_value_t = false)] + eval: bool, + + /// Emulate Intel IOMMU by QEMU. + #[arg(short, long, default_value_t = false)] + iommu: bool, +} + const COMMON_ARGS: &[&str] = &[ "--no-reboot", "-machine", "q35,kernel-irqchip=split", - "-enable-kvm", "-cpu", "Icelake-Server,+x2apic", "-m", "2G", + "-enable-kvm", "-nographic", // TODO: figure out why grub can't shown up without it "-monitor", "vc", @@ -23,148 +43,103 @@ const COMMON_ARGS: &[&str] = &[ "mon:stdio", "-display", "none", -]; - -cfg_if::cfg_if!( - if #[cfg(feature="iommu")] { - macro_rules! virtio_device_args { - ($($args:tt),*) => { - concat!($($args,)*"disable-legacy=on,disable-modern=off,iommu_platform=on,ats=on",) - }; - } - const OPTION_ARGS: &[&str] = &[ - "-device", - "intel-iommu,intremap=on,device-iotlb=on", - "-device", - "ioh3420,id=pcie.0,chassis=1", - ]; - } else { - macro_rules! virtio_device_args { - ($($args:tt),*) => { - concat!($($args,)*"disable-legacy=on,disable-modern=off",) - }; - } - const OPTION_ARGS: &[&str] = &[]; - } -); - -const DEVICE_ARGS: &[&str] = &[ "-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", - "-device", - virtio_device_args!("virtio-blk-pci,bus=pcie.0,addr=0x6,drive=x0,"), - "-device", - virtio_device_args!("virtio-keyboard-pci,"), - "-device", - virtio_device_args!("virtio-net-pci,netdev=net01,"), "-netdev", "user,id=net01,hostfwd=tcp::30022-:22,hostfwd=tcp::30080-:8080", "-object", "filter-dump,id=filter0,netdev=net01,file=virtio-net.pcap", ]; -const RUN_ARGS: &[&str] = &[]; -const TEST_ARGS: &[&str] = &[]; -const TEST_TIMEOUT_SECS: u64 = 30; +const COMMON_DEVICE_ARGS: &[&str] = &[ + "-device", + "virtio-blk-pci,bus=pcie.0,addr=0x6,drive=x0,disable-legacy=on,disable-modern=off", + "-device", + "virtio-keyboard-pci,disable-legacy=on,disable-modern=off", + "-device", + "virtio-net-pci,netdev=net01,disable-legacy=on,disable-modern=off", +]; -fn main() -> anyhow::Result<()> { - let mut args = std::env::args().skip(1); - let kernel_binary_path = { - let path = PathBuf::from(args.next().unwrap()); - path.canonicalize().unwrap() - }; +const IOMMU_DEVICE_ARGS: &[&str] = &[ + "-device", + "virtio-blk-pci,bus=pcie.0,addr=0x6,drive=x0,disable-legacy=on,disable-modern=off,iommu_platform=on,ats=on", + "-device", + "virtio-keyboard-pci,disable-legacy=on,disable-modern=off,iommu_platform=on,ats=on", + "-device", + "virtio-net-pci,netdev=net01,disable-legacy=on,disable-modern=off,iommu_platform=on,ats=on", + "-device", + "intel-iommu,intremap=on,device-iotlb=on", + "-device", + "ioh3420,id=pcie.0,chassis=1", +]; - call_bootloader_build_script( - &PathBuf::from("build/grub/scripts/build-grub-image.sh"), - &kernel_binary_path, - ) - .unwrap(); +fn main() { + let args = Args::parse(); - let kernel_iso_path = { - let a = kernel_binary_path.parent().unwrap(); - let str = kernel_binary_path - .file_name() - .unwrap() - .to_str() - .unwrap() - .to_string(); - a.join(str.add(".iso")) - }; + if args.eval { + panic!("No eval yet."); + } let mut qemu_cmd = Command::new("qemu-system-x86_64"); - let binary_kind = runner_utils::binary_kind(&kernel_binary_path); let mut qemu_args = COMMON_ARGS.clone().to_vec(); - - qemu_args.extend(DEVICE_ARGS.clone().to_vec().iter()); - qemu_args.extend(OPTION_ARGS.clone().to_vec().iter()); - qemu_args.push("-drive"); - let binding = create_fs_image(kernel_binary_path.as_path())?; - qemu_args.push(binding.as_str()); - qemu_cmd.arg("-cdrom"); - qemu_cmd.arg(kernel_iso_path.to_str().unwrap()); - if binary_kind.is_test() { - qemu_args.append(&mut TEST_ARGS.to_vec()); - qemu_cmd.args(qemu_args); - qemu_cmd.args(args); - println!("testing:{:?}", qemu_cmd); - - let exit_status = run_test_command(qemu_cmd)?; - match exit_status.code() { - Some(33) => {} // success - other => return Err(anyhow!("Test failed (exit code: {:?})", other)), - } + if args.iommu { + qemu_args.extend(IOMMU_DEVICE_ARGS.clone().to_vec().iter()); } else { - qemu_args.append(&mut RUN_ARGS.to_vec()); - qemu_cmd.args(qemu_args); - qemu_cmd.args(args); - println!("running:{:?}", qemu_cmd); - - let exit_status = qemu_cmd.status()?; - if !exit_status.success() { - std::process::exit(exit_status.code().unwrap_or(1)); - } + qemu_args.extend(COMMON_DEVICE_ARGS.clone().to_vec().iter()); } - Ok(()) -} -fn call_bootloader_build_script( - script_path: &PathBuf, - kernel_path: &PathBuf, -) -> anyhow::Result<()> { - let mut cmd = Command::new(script_path.to_str().unwrap()); - cmd.arg(kernel_path.to_str().unwrap()); - let exit_status = cmd.status()?; + let fs_image = create_fs_image(args.path.as_path()); + qemu_args.push("-drive"); + qemu_args.push(fs_image.as_str()); + + let bootdev_image = create_bootdev_image(args.path); + qemu_cmd.arg("-cdrom"); + qemu_cmd.arg(bootdev_image.as_str()); + + qemu_cmd.args(qemu_args); + + println!("running:{:?}", qemu_cmd); + + let exit_status = qemu_cmd.status().unwrap(); if !exit_status.success() { std::process::exit(exit_status.code().unwrap_or(1)); } - Ok(()) } -fn create_fs_image(path: &Path) -> anyhow::Result { +fn call_bootloader_build_script(script_path: &PathBuf, kernel_path: &PathBuf) { + let mut cmd = Command::new(script_path.to_str().unwrap()); + cmd.arg(kernel_path.to_str().unwrap()); + let exit_status = cmd.status().unwrap(); + if !exit_status.success() { + std::process::exit(exit_status.code().unwrap_or(1)); + } +} + +fn create_bootdev_image(path: PathBuf) -> String { + call_bootloader_build_script( + &PathBuf::from("build/grub/scripts/build-grub-image.sh"), + &path, + ); + let dir = path.parent().unwrap(); + let name = path.file_name().unwrap().to_str().unwrap().to_string(); + dir.join(name + ".iso").to_str().unwrap().to_string() +} + +fn create_fs_image(path: &Path) -> String { let mut fs_img_path = path.parent().unwrap().to_str().unwrap().to_string(); fs_img_path.push_str("/fs.img"); let path = Path::new(fs_img_path.as_str()); if path.exists() { - return Ok(format!( - "file={},if=none,format=raw,id=x0", - fs_img_path.as_str() - )); + return format!("file={},if=none,format=raw,id=x0", fs_img_path.as_str()); } let f = OpenOptions::new() .read(true) .write(true) .create(true) - .open(fs_img_path.as_str())?; + .open(fs_img_path.as_str()) + .unwrap(); // 32MiB f.set_len(64 * 1024 * 1024).unwrap(); - Ok(format!( - "file={},if=none,format=raw,id=x0", - fs_img_path.as_str() - )) -} - -fn run_test_command(mut cmd: Command) -> anyhow::Result { - let status = runner_utils::run_with_timeout(&mut cmd, Duration::from_secs(TEST_TIMEOUT_SECS))?; - Ok(status) + format!("file={},if=none,format=raw,id=x0", fs_img_path.as_str()) } diff --git a/services/libs/jinux-std/src/lib.rs b/services/libs/jinux-std/src/lib.rs index 91489c42d..19b7f6ff3 100644 --- a/services/libs/jinux-std/src/lib.rs +++ b/services/libs/jinux-std/src/lib.rs @@ -83,7 +83,8 @@ fn init_thread() { karg.get_initproc_path().unwrap(), karg.get_initproc_argv().unwrap().to_vec(), karg.get_initproc_envp().unwrap().to_vec(), - ).expect("Run init process failed."); + ) + .expect("Run init process failed."); loop { // If initproc becomes zombie, then exit qemu.