diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 9a8325c..c953d28 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -8,11 +8,10 @@ jobs: build_and_deploy: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions-rs/toolchain@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + - uses: dtolnay/rust-toolchain@nightly with: - toolchain: nightly-2022-01-20 - profile: minimal components: rust-src - name: Build docs run: | @@ -22,9 +21,7 @@ jobs: aarch64-unknown-linux-gnu \ aarch64-unknown-none-softfloat \ riscv32imac-unknown-none-elf \ - riscv64imac-unknown-none-elf \ - riscv64imac-unknown-none-elf \ - mipsel-unknown-linux-gnu + riscv64imac-unknown-none-elf do rustup target add ${target} cargo doc --target ${target} @@ -34,7 +31,7 @@ jobs: do rustup target add ${target} cargo doc --target ${target} - cargo doc --target ${target} -Z build-std=core,alloc + cargo +nightly doc --target ${target} -Z build-std=core,alloc mv target/${target}/doc doc/${target} done - name: Deploy to Github Pages diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b3d4021..ac96323 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -4,24 +4,16 @@ on: [push, pull_request] jobs: check: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - run: rm rust-toolchain - - uses: actions-rs/toolchain@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable with: - profile: minimal - toolchain: nightly-2022-01-20 - override: true components: rustfmt, clippy - - name: Check code format - uses: actions-rs/cargo@v1 - with: - command: fmt - args: --all -- --check + - run: cargo fmt --all -- --check build: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest strategy: matrix: target: [ @@ -31,84 +23,47 @@ jobs: aarch64-unknown-none-softfloat, riscv32imac-unknown-none-elf, riscv64imac-unknown-none-elf, - mipsel-unknown-linux-gnu, ] steps: - - uses: actions/checkout@v2 - - uses: actions-rs/toolchain@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable with: - profile: minimal - toolchain: nightly-2022-01-20 - target: ${{ matrix.target }} + targets: ${{ matrix.target }} components: clippy - - name: Build - uses: actions-rs/cargo@v1 - with: - command: build - args: --all-features --target ${{ matrix.target }} - - name: Clippy - uses: actions-rs/cargo@v1 - with: - command: clippy - args: --target ${{ matrix.target }} - - name: Docs - uses: actions-rs/cargo@v1 - with: - command: doc - args: --target ${{ matrix.target }} + - run: cargo build --all-features --target ${{ matrix.target }} + - run: cargo clippy --target ${{ matrix.target }} + - run: cargo doc --target ${{ matrix.target }} build-x86_64-none: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions-rs/toolchain@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@nightly with: - profile: minimal - toolchain: nightly-2022-01-20 components: rust-src - - name: Build - uses: actions-rs/cargo@v1 - with: - command: build - args: --all-features --target x86_64-unknown-linux-gnu -Z build-std=core,alloc - - name: Docs - uses: actions-rs/cargo@v1 - with: - command: doc - args: --all-features --target x86_64-unknown-linux-gnu -Z build-std=core,alloc + - run: cargo +nightly build --all-features --target x86_64-unknown-linux-gnu -Z build-std=core,alloc + - run: cargo +nightly doc --all-features --target x86_64-unknown-linux-gnu -Z build-std=core,alloc test-x86_64: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-20.04, macos-latest] + os: [ubuntu-latest, macos-latest] steps: - - uses: actions/checkout@v2 - - run: rm rust-toolchain - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: nightly-2022-01-20 - override: true - - name: Test - uses: actions-rs/cargo@v1 - with: - command: test - args: --all-features + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + - run: cargo test --all-features test-aarch64: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - run: rm rust-toolchain - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: nightly-2022-01-20 - target: aarch64-unknown-linux-gnu - override: true - - uses: actions-rs/cargo@v1 + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable with: - use-cross: true - command: test - args: --target aarch64-unknown-linux-gnu + targets: aarch64-unknown-linux-gnu + - run: sudo apt-get update && sudo apt-get install -y qemu-user gcc-aarch64-linux-gnu + - run: cargo test --target aarch64-unknown-linux-gnu + env: + CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUNNER: qemu-aarch64 + CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: aarch64-linux-gnu-gcc + QEMU_LD_PREFIX: /usr/aarch64-linux-gnu diff --git a/Cargo.toml b/Cargo.toml index 00940b6..205f9d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ exclude = ["docs", ".idea"] [dependencies] [target.'cfg(target_arch = "x86_64")'.dependencies] -x86_64 = "0.15" +x86_64 = { version = "0.15", default-features = false, features = ["instructions"] } raw-cpuid = "11.0" [features] diff --git a/examples/riscv/.cargo/config b/examples/riscv/.cargo/config.toml similarity index 100% rename from examples/riscv/.cargo/config rename to examples/riscv/.cargo/config.toml diff --git a/examples/riscv/Cargo.toml b/examples/riscv/Cargo.toml index ad3a3fb..c30395b 100644 --- a/examples/riscv/Cargo.toml +++ b/examples/riscv/Cargo.toml @@ -9,5 +9,5 @@ edition = "2018" [dependencies] log = "0.4" riscv = "0.7" -opensbi-rt = { git = "https://github.com/rcore-os/opensbi-rt.git", rev = "abdfeb7" } +opensbi-rt = { git = "https://github.com/rcore-os/opensbi-rt.git", rev = "6d44fced33f6116bfea68d52b0e027bea6414e85" } trapframe = { path = "../.." } diff --git a/examples/riscv/Makefile b/examples/riscv/Makefile index a3f8554..bba7539 100644 --- a/examples/riscv/Makefile +++ b/examples/riscv/Makefile @@ -2,7 +2,6 @@ arch ?= riscv64 target := $(arch)imac-unknown-none-elf mode := debug kernel := target/$(target)/$(mode)/riscv -bin := target/$(target)/$(mode)/kernel.bin sysroot := $(shell rustc --print sysroot) objdump := $(shell find $(sysroot) -name llvm-objdump) --arch-name=$(arch) @@ -21,17 +20,14 @@ endif .PHONY: kernel build clean qemu run env -build: $(bin) +build: kernel env: - rustup component add llvm-tools-preview rustfmt - rustup target add $(target) + rustup component add llvm-tools-preview rustfmt --toolchain nightly + rustup target add $(target) --toolchain nightly kernel: - cargo build $(BUILD_ARGS) - -$(bin): kernel - $(objcopy) $(kernel) --strip-all -O binary $@ + cargo +nightly build $(BUILD_ARGS) asm: $(objdump) -d $(kernel) | less @@ -45,11 +41,11 @@ header: clean: cargo clean -qemu: $(bin) +qemu: kernel qemu-system-$(arch) \ -machine virt \ -nographic \ -bios default \ - -device loader,file=$(bin),addr=$(START_ADDR) + -kernel $(kernel) run: build qemu diff --git a/examples/riscv/src/main.rs b/examples/riscv/src/main.rs index 79b95e2..273ae33 100644 --- a/examples/riscv/src/main.rs +++ b/examples/riscv/src/main.rs @@ -52,7 +52,7 @@ extern "C" fn main() { t6: 31, }, sstatus: 0xdead_beaf, - sepc: user_entry as usize, + sepc: user_entry as *const () as usize, }; println!("Go to user: {:#x?}", regs); regs.run(); diff --git a/examples/uefi/.cargo/config b/examples/uefi/.cargo/config deleted file mode 100644 index 704aee0..0000000 --- a/examples/uefi/.cargo/config +++ /dev/null @@ -1,6 +0,0 @@ -[build] -target = "x86_64-unknown-uefi" - -[unstable] -build-std = ["core", "alloc"] -build-std-features = ["compiler-builtins-mem"] diff --git a/examples/uefi/.cargo/config.toml b/examples/uefi/.cargo/config.toml new file mode 100644 index 0000000..85d49dc --- /dev/null +++ b/examples/uefi/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +target = "x86_64-unknown-uefi" diff --git a/examples/uefi/Cargo.toml b/examples/uefi/Cargo.toml index 1d2f962..0c733b7 100644 --- a/examples/uefi/Cargo.toml +++ b/examples/uefi/Cargo.toml @@ -2,13 +2,18 @@ name = "uefi" version = "0.1.0" authors = ["Runji Wang "] -edition = "2018" +edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[profile.dev] +panic = "abort" + +[profile.release] +panic = "abort" + [dependencies] log = "0.4" -x86_64 = "0.14" -uefi = "0.14" -uefi-services = "0.11" +x86_64 = { version = "0.15", default-features = false, features = ["instructions"] } +uefi = { version = "0.38", features = ["global_allocator", "logger", "panic_handler"] } trapframe = { path = "../.." } diff --git a/examples/uefi/Makefile b/examples/uefi/Makefile index 98078ad..92af3b4 100644 --- a/examples/uefi/Makefile +++ b/examples/uefi/Makefile @@ -3,7 +3,7 @@ TARGET := target OVMF := OVMF.fd EFI := $(TARGET)/x86_64-unknown-uefi/$(MODE)/uefi.efi ESP := $(TARGET)/x86_64-unknown-uefi/$(MODE)/esp -QEMU_ARGS := -net none -nographic +QEMU_ARGS := -net none -nographic -cpu max OBJDUMP := rust-objdump ifeq (${MODE}, release) diff --git a/examples/uefi/src/main.rs b/examples/uefi/src/main.rs index 597b543..44b2311 100644 --- a/examples/uefi/src/main.rs +++ b/examples/uefi/src/main.rs @@ -1,25 +1,34 @@ #![no_std] #![no_main] -#![feature(abi_efiapi)] -#![feature(core_intrinsics)] -#![feature(naked_functions)] #![deny(warnings)] extern crate alloc; use core::arch::asm; -use core::intrinsics::breakpoint; use log::*; use trapframe::{GeneralRegs, TrapFrame, UserContext}; use uefi::prelude::*; use x86_64::registers::control::*; use x86_64::structures::paging::{PageTable, PageTableFlags}; +core::arch::global_asm!( + r#" +.global user_entry +user_entry: + syscall + int3 +"# +); + +extern "C" { + fn user_entry(); +} + #[entry] -fn efi_main(_image: Handle, mut st: SystemTable) -> uefi::Status { - uefi_services::init(&mut st).expect_success("Failed to initialize utilities"); +fn efi_main() -> Status { + uefi::helpers::init().expect("Failed to initialize utilities"); check_and_set_cpu_features(); - allow_user_access(user_entry as usize); + allow_user_access(user_entry as *const () as usize); unsafe { trapframe::init(); } @@ -42,7 +51,7 @@ fn efi_main(_image: Handle, mut st: SystemTable) -> uefi::Status { r13: 13, r14: 14, r15: 15, - rip: user_entry as usize, + rip: user_entry as *const () as usize, rflags: 0x202, fsbase: 18, gsbase: 19, @@ -62,7 +71,7 @@ fn efi_main(_image: Handle, mut st: SystemTable) -> uefi::Status { // trap from kernel unsafe { - breakpoint(); + asm!("int3", options(nomem, nostack)); } unimplemented!() } @@ -78,11 +87,6 @@ extern "sysv64" fn trap_handler(tf: &mut TrapFrame) { } } -#[naked] -unsafe extern "C" fn user_entry() { - asm!("syscall", "int3", options(noreturn)); -} - /// Set user bit for 4-level PDEs of the `page`. /// This is a workaround since `x86_64` crate does not set user bit for PDEs. fn allow_user_access(vaddr: usize) { diff --git a/rust-toolchain b/rust-toolchain index 12ae469..870bbe4 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2022-01-20 \ No newline at end of file +stable \ No newline at end of file diff --git a/src/arch/aarch64/fncall.rs b/src/arch/aarch64/fncall.rs index bb9d4ad..b7cc16d 100644 --- a/src/arch/aarch64/fncall.rs +++ b/src/arch/aarch64/fncall.rs @@ -148,7 +148,7 @@ elr_location: let mut cx = UserContext { general, sp: stack.as_mut_ptr() as usize + 0x1000, - elr: dump_registers as usize, + elr: dump_registers as *const () as usize, ..Default::default() }; cx.run_fncall(); @@ -157,7 +157,7 @@ elr_location: assert_eq!( general_dump, GeneralRegs { - x30: dump_registers as usize, + x30: dump_registers as *const () as usize, ..general } ); @@ -195,10 +195,10 @@ elr_location: x27: 100 + 27, x28: 100 + 28, x29: 100 + 29, - x30: elr_location as usize, + x30: elr_location as *const () as usize, ..cx.general } ); - assert_eq!(cx.elr, elr_location as usize); + assert_eq!(cx.elr, elr_location as *const () as usize); } } diff --git a/src/arch/aarch64/trap.rs b/src/arch/aarch64/trap.rs index b0031b0..17e0e5c 100644 --- a/src/arch/aarch64/trap.rs +++ b/src/arch/aarch64/trap.rs @@ -13,7 +13,7 @@ global_asm!(include_str!("trap.S")); /// You **MUST NOT** modify these registers later. pub unsafe fn init() { // Set the exception vector address - asm!("msr VBAR_EL1, {}", in(reg) __vectors as usize); + asm!("msr VBAR_EL1, {}", in(reg) __vectors as *const () as usize); } /// Trap frame of kernel interrupt diff --git a/src/arch/mipsel/trap.rs b/src/arch/mipsel/trap.rs index 217be89..6992765 100644 --- a/src/arch/mipsel/trap.rs +++ b/src/arch/mipsel/trap.rs @@ -19,12 +19,6 @@ pub unsafe fn init() { ); } -#[no_mangle] -#[linkage = "weak"] -extern "C" fn trap_handler(tf: &mut TrapFrame) { - unimplemented!("TRAP: tf={:#x?}", tf); -} - /// Trap frame of kernel interrupt /// /// # Trap handler diff --git a/src/arch/riscv/trap.S b/src/arch/riscv/trap.S index fa0dc54..a3f15e7 100644 --- a/src/arch/riscv/trap.S +++ b/src/arch/riscv/trap.S @@ -62,7 +62,7 @@ trap_from_user: end_trap_from_kernel: mv a0, sp # first arg is TrapFrame la ra, trap_return # set return address - j trap_handler + tail trap_handler end_trap_from_user: # load callee-saved registers diff --git a/src/arch/riscv/trap.rs b/src/arch/riscv/trap.rs index 5a5fc68..c6f1a52 100644 --- a/src/arch/riscv/trap.rs +++ b/src/arch/riscv/trap.rs @@ -41,13 +41,7 @@ pub unsafe fn init() { // presently executing in the kernel asm!("csrw sscratch, zero"); // Set the exception vector address - asm!("csrw stvec, {}", in(reg) trap_entry as usize); -} - -#[no_mangle] -#[linkage = "weak"] -extern "C" fn trap_handler(tf: &mut TrapFrame) { - unimplemented!("TRAP: tf={:#x?}", tf); + asm!("csrw stvec, {}", in(reg) trap_entry as *const () as usize); } /// Trap frame of kernel interrupt diff --git a/src/arch/x86_64/fncall.rs b/src/arch/x86_64/fncall.rs index 3c6b6d3..ff3ea55 100644 --- a/src/arch/x86_64/fncall.rs +++ b/src/arch/x86_64/fncall.rs @@ -301,7 +301,7 @@ dump_registers: r13: 13, r14: 14, r15: 15, - rip: dump_registers as usize, + rip: dump_registers as *const () as usize, rflags: 0, fsbase: 0, // don't set to non-zero garbage value gsbase: 0, diff --git a/src/arch/x86_64/idt.rs b/src/arch/x86_64/idt.rs index ad474e4..c373405 100644 --- a/src/arch/x86_64/idt.rs +++ b/src/arch/x86_64/idt.rs @@ -11,11 +11,12 @@ pub fn init() { } let idt = Box::leak(Box::new(InterruptDescriptorTable::new())); - // let idt = sidt().base; - let entries: &'static mut [Entry; 256] = - unsafe { core::mem::transmute_copy(&idt) }; + let entries = idt as *mut InterruptDescriptorTable as *mut Entry<()>; for i in 0..256 { - let opt = entries[i].set_handler_fn(unsafe { core::mem::transmute(VECTORS[i]) }); + let opt = unsafe { + (*entries.add(i)) + .set_handler_addr(VirtAddr::new(VECTORS[i] as *const () as usize as u64)) + }; // Enable user space `int3` and `into` if i == 3 || i == 4 { opt.set_privilege_level(PrivilegeLevel::Ring3); diff --git a/src/arch/x86_64/syscall.rs b/src/arch/x86_64/syscall.rs index 51d5044..2aa139b 100644 --- a/src/arch/x86_64/syscall.rs +++ b/src/arch/x86_64/syscall.rs @@ -30,7 +30,7 @@ pub fn init() { // TF|DF|IF|IOPL|AC|NT const RFLAGS_MASK: u64 = 0x47700; - LStar::write(VirtAddr::new(syscall_entry as usize as u64)); + LStar::write(VirtAddr::new(syscall_entry as *const () as usize as u64)); SFMask::write(RFlags::from_bits(RFLAGS_MASK).unwrap()); } } diff --git a/src/lib.rs b/src/lib.rs index 13389fe..bbfd56f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,5 @@ #![no_std] -#![feature(linkage)] #![deny(warnings)] -#![cfg_attr(target_arch = "mips", feature(asm_experimental_arch))] extern crate alloc;