diff --git a/.github/workflows/cargo_check.yml b/.github/workflows/cargo_check.yml deleted file mode 100644 index a84f90b1e..000000000 --- a/.github/workflows/cargo_check.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: Cargo check - -on: - pull_request: - push: - branches: - - main - -jobs: - test: - runs-on: ubuntu-latest - timeout-minutes: 10 - container: asterinas/asterinas:0.3.1 - steps: - - run: echo "Running in asterinas/asterinas:0.3.1" - - - uses: actions/checkout@v3 - - - run: make install_osdk - - - name: Check - id: check - run: make check diff --git a/.github/workflows/unit_test.yml b/.github/workflows/unit_test.yml deleted file mode 100644 index 2ccf0d354..000000000 --- a/.github/workflows/unit_test.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Unit test - -on: - pull_request: - push: - branches: - - main - -jobs: - test: - runs-on: ubuntu-latest - timeout-minutes: 15 - container: asterinas/asterinas:0.3.1 - steps: - - run: echo "Running in asterinas/asterinas:0.3.1" - - - uses: actions/checkout@v3 - - - run: make install_osdk - - - name: Usermode Unit test - id: usermode_unit_test - run: make test - - - name: Ktest Unit Test - id: ktest_unit_test - run: make update_initramfs && make ktest - - # TODO: add component check. diff --git a/Cargo.toml b/Cargo.toml index be341832f..cd7d650f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] - +resolver = "2" members = [ "framework/aster-frame", "framework/libs/align_ext", @@ -28,7 +28,6 @@ members = [ "kernel/libs/typeflags", "kernel/libs/typeflags-util", ] - exclude = [ "osdk", "target/osdk/base", diff --git a/Makefile b/Makefile index 093a4c691..84b0cb776 100644 --- a/Makefile +++ b/Makefile @@ -125,10 +125,10 @@ docs: @cd docs && mdbook build # Build mdBook format: - ./tools/format_all.sh + @./tools/format_all.sh check: - ./tools/format_all.sh --check # Check Rust format issues + @./tools/format_all.sh --check # Check Rust format issues @cargo osdk clippy clean: diff --git a/framework/aster-frame/Cargo.toml b/framework/aster-frame/Cargo.toml index d1caa1f19..5c33d6fb5 100644 --- a/framework/aster-frame/Cargo.toml +++ b/framework/aster-frame/Cargo.toml @@ -29,7 +29,7 @@ trapframe = { git = "https://github.com/asterinas/trapframe-rs", rev = "2f37590" unwinding = { version = "0.2.1", default-features = false, features = ["fde-static", "hide-trace", "panic", "personality", "unwinder"] } volatile = { version = "0.4.5", features = ["unstable"] } -[target.x86_64-custom.dependencies] +[target.x86_64-unknown-none.dependencies] x86_64 = "0.14.2" x86 = "0.52.0" acpi = "4.1.1" diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 7a5e81638..6f2d3ab14 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -13,17 +13,5 @@ x86_64 = "0.14.2" aster-time = { path = "comps/time" } aster-framebuffer = { path = "comps/framebuffer" } -[profile.dev] -opt-level = 0 -debug = true -lto = false -panic = "unwind" - -[profile.release] -opt-level = 3 -debug = false -lto = false -panic = "unwind" - [features] intel_tdx = ["aster-frame/intel_tdx", "aster-nix/intel_tdx"] diff --git a/kernel/libs/comp-sys/controlled/src/lib.rs b/kernel/libs/comp-sys/controlled/src/lib.rs index d4f313dce..951cdf521 100644 --- a/kernel/libs/comp-sys/controlled/src/lib.rs +++ b/kernel/libs/comp-sys/controlled/src/lib.rs @@ -28,7 +28,7 @@ pub fn controlled( item: proc_macro::TokenStream, ) -> proc_macro::TokenStream { let attr = attr.to_string(); - if attr.len() != 0 { + if !attr.is_empty() { panic!("controlled cannot accept inner tokens.") } let mut tokens: proc_macro::TokenStream = quote!( @@ -45,7 +45,7 @@ pub fn uncontrolled( item: proc_macro::TokenStream, ) -> proc_macro::TokenStream { let attr = attr.to_string(); - if attr.len() != 0 { + if !attr.is_empty() { panic!("uncontrolled cannot accept inner tokens.") } let mut tokens: proc_macro::TokenStream = quote!( diff --git a/osdk/Cargo.lock b/osdk/Cargo.lock index 0db13c92e..4478918e6 100644 --- a/osdk/Cargo.lock +++ b/osdk/Cargo.lock @@ -128,7 +128,7 @@ dependencies = [ [[package]] name = "cargo-osdk" -version = "0.2.0" +version = "0.4.0" dependencies = [ "assert_cmd", "clap", diff --git a/osdk/src/base_crate/mod.rs b/osdk/src/base_crate/mod.rs index d9bb61aa2..ec08d52e1 100644 --- a/osdk/src/base_crate/mod.rs +++ b/osdk/src/base_crate/mod.rs @@ -4,13 +4,20 @@ //! It will depend on the kernel crate. //! -use std::{fs, path::Path, str::FromStr}; +use std::{fs, path::{Path, PathBuf}, str::FromStr}; + +use crate::util::get_cargo_metadata; pub fn new_base_crate( base_crate_path: impl AsRef, dep_crate_name: &str, dep_crate_path: impl AsRef, ) { + let workspace_root = { + let meta = get_cargo_metadata(None::<&str>, None::<&[&str]>).unwrap(); + PathBuf::from(meta.get("workspace_root").unwrap().as_str().unwrap()) + }; + if base_crate_path.as_ref().exists() { std::fs::remove_dir_all(&base_crate_path).unwrap(); } @@ -47,12 +54,8 @@ pub fn new_base_crate( std::env::set_current_dir(&base_crate_path).unwrap(); // Add linker.ld file - let linker_ld = include_str!("x86_64-custom.ld.template"); - fs::write("x86_64-custom.ld", linker_ld).unwrap(); - - // Add target json file - let target_json = include_str!("x86_64-custom.json.template"); - fs::write("x86_64-custom.json", target_json).unwrap(); + let linker_ld = include_str!("x86_64.ld.template"); + fs::write("x86_64.ld", linker_ld).unwrap(); // Overrite the main.rs file let main_rs = include_str!("main.rs.template"); @@ -64,7 +67,7 @@ pub fn new_base_crate( add_manifest_dependency(dep_crate_name, dep_crate_path); // Copy the manifest configurations from the target crate to the base crate - copy_manifest_configurations(base_crate_path); + copy_profile_configurations(workspace_root); // Get back to the original directory std::env::set_current_dir(original_dir).unwrap(); @@ -100,8 +103,8 @@ fn add_manifest_dependency(crate_name: &str, crate_path: impl AsRef) { fs::write(mainfest_path, content).unwrap(); } -fn copy_manifest_configurations(target_crate_path: impl AsRef) { - let target_manifest_path = target_crate_path.as_ref().join("Cargo.toml"); +fn copy_profile_configurations(workspace_root: impl AsRef) { + let target_manifest_path = workspace_root.as_ref().join("Cargo.toml"); let manifest_path = "Cargo.toml"; let target_manifest: toml::Table = { diff --git a/osdk/src/base_crate/x86_64-custom.json.template b/osdk/src/base_crate/x86_64-custom.json.template deleted file mode 100644 index a7d797f8d..000000000 --- a/osdk/src/base_crate/x86_64-custom.json.template +++ /dev/null @@ -1,21 +0,0 @@ -{ - "llvm-target": "x86_64-unknown-none", - "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128", - "code-model": "kernel", - "cpu": "x86-64", - "arch": "x86_64", - "target-endian": "little", - "target-pointer-width": "64", - "target-c-int-width": "32", - "os": "none", - "executables": true, - "linker-flavor": "ld.lld", - "linker": "rust-lld", - "pre-link-args": { - "ld.lld": [ - "--script=x86_64-custom.ld" - ] - }, - "disable-redzone": true, - "features": "-mmx,-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-3dnow,-3dnowa,-avx,-avx2,+soft-float" -} \ No newline at end of file diff --git a/osdk/src/base_crate/x86_64-custom.ld.template b/osdk/src/base_crate/x86_64.ld.template similarity index 100% rename from osdk/src/base_crate/x86_64-custom.ld.template rename to osdk/src/base_crate/x86_64.ld.template diff --git a/osdk/src/commands/build/mod.rs b/osdk/src/commands/build/mod.rs index e6bdd97df..bd19f2d1b 100644 --- a/osdk/src/commands/build/mod.rs +++ b/osdk/src/commands/build/mod.rs @@ -3,11 +3,7 @@ mod bin; mod grub; -use std::{ - path::{Path, PathBuf}, - process, - str::FromStr, -}; +use std::{path::Path, process}; use bin::strip_elf_for_qemu; @@ -40,6 +36,7 @@ pub fn execute_build_command(config: &BuildConfig) { &osdk_target_directory, &ws_target_directory, config, + &[], ); } @@ -48,6 +45,7 @@ pub fn create_base_and_build( osdk_target_directory: impl AsRef, cargo_target_directory: impl AsRef, config: &BuildConfig, + rustflags: &[&str], ) -> Bundle { let base_crate_path = osdk_target_directory.as_ref().join("base"); new_base_crate( @@ -62,6 +60,7 @@ pub fn create_base_and_build( &osdk_target_directory, &cargo_target_directory, config, + rustflags, ); std::env::set_current_dir(original_dir).unwrap(); bundle @@ -72,6 +71,7 @@ pub fn do_build( osdk_target_directory: impl AsRef, cargo_target_directory: impl AsRef, config: &BuildConfig, + rustflags: &[&str], ) -> Bundle { if bundle_path.as_ref().exists() { std::fs::remove_dir_all(&bundle_path).unwrap(); @@ -93,7 +93,7 @@ pub fn do_build( }; info!("Building kernel ELF"); - let aster_elf = build_kernel_elf(&config.cargo_args, &cargo_target_directory); + let aster_elf = build_kernel_elf(&config.cargo_args, &cargo_target_directory, rustflags); if matches!(config.manifest.qemu.machine, QemuMachine::Microvm) { let stripped_elf = strip_elf_for_qemu(&osdk_target_directory, &aster_elf); @@ -117,13 +117,25 @@ pub fn do_build( bundle } -fn build_kernel_elf(args: &CargoArgs, cargo_target_directory: impl AsRef) -> AsterBin { - let target_json_path = PathBuf::from_str("x86_64-custom.json").unwrap(); +fn build_kernel_elf(args: &CargoArgs, cargo_target_directory: impl AsRef, rustflags: &[&str]) -> AsterBin { + let target = "x86_64-unknown-none"; + + let env_rustflags = std::env::var("RUSTFLAGS").unwrap_or_default(); + let mut rustflags = Vec::from(rustflags); + // We disable RELRO and PIC here because they cause link failures + rustflags.extend(vec![ + &env_rustflags, + "-C link-arg=-Tx86_64.ld", + "-C code-model=kernel", + "-C relocation-model=static", + "-Z relro-level=off", + ]); let mut command = cargo(); command.env_remove("RUSTUP_TOOLCHAIN"); + command.env("RUSTFLAGS", rustflags.join(" ")); command.arg("build"); - command.arg("--target").arg(&target_json_path); + command.arg("--target").arg(target); command .arg("--target-dir") .arg(cargo_target_directory.as_ref()); @@ -135,9 +147,7 @@ fn build_kernel_elf(args: &CargoArgs, cargo_target_directory: impl AsRef) process::exit(Errno::ExecuteCommand as _); } - let aster_bin_path = cargo_target_directory - .as_ref() - .join(target_json_path.file_stem().unwrap().to_str().unwrap()); + let aster_bin_path = cargo_target_directory.as_ref().join(target); let aster_bin_path = if args.profile == "dev" { aster_bin_path.join("debug") } else { diff --git a/osdk/src/commands/check.rs b/osdk/src/commands/check.rs index 5091f6a54..c81f9d9e9 100644 --- a/osdk/src/commands/check.rs +++ b/osdk/src/commands/check.rs @@ -3,13 +3,14 @@ use std::process; use super::util::{cargo, COMMON_CARGO_ARGS}; -use crate::{commands::util::create_target_json, error::Errno, error_msg}; +use crate::{error::Errno, error_msg}; pub fn execute_check_command() { - let target_json_path = create_target_json(); - let mut command = cargo(); - command.arg("check").arg("--target").arg(target_json_path); + command + .arg("check") + .arg("--target") + .arg("x86_64-unkown-none"); command.args(COMMON_CARGO_ARGS); let status = command.status().unwrap(); if !status.success() { diff --git a/osdk/src/commands/clippy.rs b/osdk/src/commands/clippy.rs index 393767848..f873c191d 100644 --- a/osdk/src/commands/clippy.rs +++ b/osdk/src/commands/clippy.rs @@ -3,13 +3,16 @@ use std::process; use super::util::{cargo, COMMON_CARGO_ARGS}; -use crate::{commands::util::create_target_json, error::Errno, error_msg}; +use crate::{error::Errno, error_msg}; pub fn execute_clippy_command() { - let target_json_path = create_target_json(); - let mut command = cargo(); - command.arg("clippy").arg("-h"); + command + .arg("clippy") + .arg("-h") + .arg("--target") + .arg("x86_64-unknown-none") + .args(COMMON_CARGO_ARGS); info!("Running `cargo clippy -h`"); let output = command.output().unwrap(); if !output.status.success() { @@ -20,8 +23,11 @@ pub fn execute_clippy_command() { } let mut command = cargo(); - command.arg("clippy").arg("--target").arg(target_json_path); - command.args(COMMON_CARGO_ARGS); + command + .arg("clippy") + .arg("--target") + .arg("x86_64-unknown-none") + .args(COMMON_CARGO_ARGS); // TODO: Add support for custom clippy args using OSDK commandline rather than hardcode it. command.args(["--", "-D", "warnings"]); let status = command.status().unwrap(); diff --git a/osdk/src/commands/run.rs b/osdk/src/commands/run.rs index c53882051..7973469f5 100644 --- a/osdk/src/commands/run.rs +++ b/osdk/src/commands/run.rs @@ -50,6 +50,7 @@ pub fn execute_run_command(config: &RunConfig) { &osdk_target_directory, &ws_target_directory, &required_build_config, + &[], ); bundle.run(config); diff --git a/osdk/src/commands/test.rs b/osdk/src/commands/test.rs index 636d7a756..299883ab9 100644 --- a/osdk/src/commands/test.rs +++ b/osdk/src/commands/test.rs @@ -52,13 +52,12 @@ pub static KTEST_CRATE_WHITELIST: Option<&[&str]> = Some(&{:#?}); }; let original_dir = std::env::current_dir().unwrap(); std::env::set_current_dir(&target_crate_dir).unwrap(); - // Add `--cfg ktest` to RUSTFLAGS - std::env::set_var("RUSTFLAGS", "--cfg ktest"); let bundle = do_build( default_bundle_directory, &osdk_target_directory, &ws_target_directory, &required_build_config, + &["--cfg ktest", "-C panic=unwind"], ); std::env::remove_var("RUSTFLAGS"); std::env::set_current_dir(original_dir).unwrap(); diff --git a/osdk/src/commands/util.rs b/osdk/src/commands/util.rs index 4c8b99b09..19538ba13 100644 --- a/osdk/src/commands/util.rs +++ b/osdk/src/commands/util.rs @@ -1,8 +1,6 @@ // SPDX-License-Identifier: MPL-2.0 -use std::{fs, path::PathBuf, process::Command}; - -use crate::util::get_target_directory; +use std::process::Command; pub const COMMON_CARGO_ARGS: &[&str] = &[ "-Zbuild-std=core,alloc,compiler_builtins", @@ -14,18 +12,3 @@ pub const DEFAULT_TARGET_RELPATH: &str = "osdk"; pub fn cargo() -> Command { Command::new("cargo") } - -pub fn create_target_json() -> PathBuf { - let target_osdk_dir = get_target_directory().join(DEFAULT_TARGET_RELPATH); - fs::create_dir_all(&target_osdk_dir).unwrap(); - - let target_json_path = target_osdk_dir.join("x86_64-custom.json"); - if target_json_path.is_file() { - return target_json_path; - } - - let contents = include_str!("../base_crate/x86_64-custom.json.template"); - fs::write(&target_json_path, contents).unwrap(); - - target_json_path -} diff --git a/tools/docker/Dockerfile.ubuntu22.04 b/tools/docker/Dockerfile.ubuntu22.04 index bea55eb33..50c56d20e 100644 --- a/tools/docker/Dockerfile.ubuntu22.04 +++ b/tools/docker/Dockerfile.ubuntu22.04 @@ -221,6 +221,7 @@ FROM rust RUN apt update && apt-get install -y --no-install-recommends \ cpio \ cpuid \ + exfatprogs \ file \ gdb \ grub-efi-amd64 \ @@ -237,8 +238,7 @@ RUN apt update && apt-get install -y --no-install-recommends \ unzip \ vim \ xorriso \ - zip \ - exfatprogs + zip # Clean apt cache RUN apt clean && rm -rf /var/lib/apt/lists/*