From dc5f1fe055b6456f2db4646f9dc100fe41ccdf88 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Fri, 19 Mar 2021 10:33:26 +0100 Subject: [PATCH 1/7] Update toolchain. --- .cargo/config | 4 ++++ .../{x86_64-unknown-none.json => x86_64-unknown-kernel.json} | 0 2 files changed, 4 insertions(+) rename targets/{x86_64-unknown-none.json => x86_64-unknown-kernel.json} (100%) diff --git a/.cargo/config b/.cargo/config index fc260d9..20ee1ec 100644 --- a/.cargo/config +++ b/.cargo/config @@ -3,3 +3,7 @@ rustflags = [ # Kernel should preserve floating-point registers "-Csoft-float", ] + +[unstable] +build-std = ["core", "alloc", "compiler_builtins"] +build-std-features = ["panic-unwind"] diff --git a/targets/x86_64-unknown-none.json b/targets/x86_64-unknown-kernel.json similarity index 100% rename from targets/x86_64-unknown-none.json rename to targets/x86_64-unknown-kernel.json From cc6f792a03eacf0abac993964f68444713159430 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Fri, 19 Mar 2021 11:06:56 +0100 Subject: [PATCH 2/7] Use options(noreturn) in all naked functions. --- src/context/arch/x86_64.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/context/arch/x86_64.rs b/src/context/arch/x86_64.rs index a333a69..ceffbb0 100644 --- a/src/context/arch/x86_64.rs +++ b/src/context/arch/x86_64.rs @@ -216,12 +216,14 @@ pub unsafe extern "C" fn switch_to(_prev: &mut Context, _next: &mut Context) { // the calling function, `context::switch`. Thus, we have to execute this Rust hook by // ourselves, which will unlock the contexts before the later switch. - call {switch_hook} + // Note that switch_finish_hook will be responsible for executing `ret`. + jmp {switch_hook} ", true = const(AbiCompatBool::True as u8), switch_hook = sym crate::context::switch_finish_hook, + options(noreturn), ); } @@ -275,8 +277,10 @@ unsafe extern fn signal_handler_wrapper() { pop rcx pop rax add rsp, 16 + ret ", inner = sym inner, + options(noreturn), ); } From 7d4defa5e52a035e49c78f4958392cdb4d480c8b Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Fri, 19 Mar 2021 11:07:41 +0100 Subject: [PATCH 3/7] Use weak CAS and use abort() in context::switch. Previously context::switch used compare_and_swap for acquiring the global context switch lock, but given its deprecation in more recent Rust versions, it has been replaced with compare_exchange_weak (which can be further optimized on some architectures). It also replaces panic!() with abort() in switch_finish_hook, because unwinding from assembly is not that fun. --- src/context/switch.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/context/switch.rs b/src/context/switch.rs index 2c37430..a823854 100644 --- a/src/context/switch.rs +++ b/src/context/switch.rs @@ -78,7 +78,8 @@ pub unsafe extern "C" fn switch_finish_hook() { prev_lock.force_write_unlock(); next_lock.force_write_unlock(); } else { - panic!("SWITCH_RESULT was not set"); + // TODO: unreachable_unchecked()? + core::intrinsics::abort(); } arch::CONTEXT_SWITCH_LOCK.store(false, Ordering::SeqCst); } @@ -97,11 +98,12 @@ unsafe fn runnable(context: &Context, cpu_id: usize) -> bool { /// /// Do not call this while holding locks! pub unsafe fn switch() -> bool { + // TODO: Better memory orderings? //set PIT Interrupt counter to 0, giving each process same amount of PIT ticks let ticks = PIT_TICKS.swap(0, Ordering::SeqCst); // Set the global lock to avoid the unsafe operations below from causing issues - while arch::CONTEXT_SWITCH_LOCK.compare_and_swap(false, true, Ordering::SeqCst) { + while arch::CONTEXT_SWITCH_LOCK.compare_exchange_weak(false, true, Ordering::SeqCst, Ordering::Relaxed).is_err() { interrupt::pause(); } From 7594dd60d2cf4e23fd8a76ffb30e981387080774 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Fri, 19 Mar 2021 11:09:49 +0100 Subject: [PATCH 4/7] Remove compare_and_swap from int_like!. --- src/common/int_like.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/common/int_like.rs b/src/common/int_like.rs index 103e509..925d9b7 100644 --- a/src/common/int_like.rs +++ b/src/common/int_like.rs @@ -73,10 +73,6 @@ macro_rules! int_like { $new_type_name::from(self.container.swap(val.into(), order)) } #[allow(dead_code)] - pub fn compare_and_swap(&self, current: $new_type_name, new: $new_type_name, order: ::core::sync::atomic::Ordering) -> $new_type_name { - $new_type_name::from(self.container.compare_and_swap(current.into(), new.into(), order)) - } - #[allow(dead_code)] pub fn compare_exchange(&self, current: $new_type_name, new: $new_type_name, success: ::core::sync::atomic::Ordering, failure: ::core::sync::atomic::Ordering) -> ::core::result::Result<$new_type_name, $new_type_name> { match self.container.compare_exchange(current.into(), new.into(), success, failure) { Ok(result) => Ok($new_type_name::from(result)), From 2dc899dc3b5c95366d4da6d6349a5da4fa5d332b Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Fri, 19 Mar 2021 11:10:09 +0100 Subject: [PATCH 5/7] Update paste and align interrupt handlers. --- Cargo.lock | 27 ++++----------------------- Cargo.toml | 5 +---- src/arch/x86_64/interrupt/handler.rs | 28 ++++++++++++++-------------- 3 files changed, 19 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1e2bffd..75d8620 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,9 +26,9 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "cc" -version = "1.0.67" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd" +checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787" [[package]] name = "cfg-if" @@ -115,22 +115,9 @@ dependencies = [ [[package]] name = "paste" -version = "0.1.18" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45ca20c77d80be666aef2b45486da86238fabe33e38306bd3118fe4af33fa880" -dependencies = [ - "paste-impl", - "proc-macro-hack", -] - -[[package]] -name = "paste-impl" -version = "0.1.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d95a7db200b97ef370c8e6de0088252f7e0dfff7d047a28528e47456c0fc98b6" -dependencies = [ - "proc-macro-hack", -] +checksum = "acbf547ad0c65e31259204bd90935776d1c693cec2f4ff7abb7a1bbbd40dfe58" [[package]] name = "plain" @@ -138,12 +125,6 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" -[[package]] -name = "proc-macro-hack" -version = "0.5.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" - [[package]] name = "raw-cpuid" version = "7.0.4" diff --git a/Cargo.toml b/Cargo.toml index 2c38985..c7fb591 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ log = { version = "0.4" } redox_syscall = { path = "syscall" } slab_allocator = { path = "slab_allocator", optional = true } spin = "0.5.2" -paste = "0.1.18" +paste = "1" rmm = { path = "rmm", default-features = false } [dependencies.goblin] @@ -59,8 +59,5 @@ slab = ["slab_allocator"] # Kernel doesn't yet work great with debug mode :( opt-level = 3 -# LTO fixes some duplicate symbols of memcpy/memmove/etc -lto = true - [profile.release] lto = true diff --git a/src/arch/x86_64/interrupt/handler.rs b/src/arch/x86_64/interrupt/handler.rs index d5a93be..cde79a1 100644 --- a/src/arch/x86_64/interrupt/handler.rs +++ b/src/arch/x86_64/interrupt/handler.rs @@ -215,6 +215,8 @@ macro_rules! function { ".global ", stringify!($name), "\n", ".type ", stringify!($name), ", @function\n", ".section .text.", stringify!($name), ", \"ax\", @progbits\n", + // Align the function to a 16-byte boundary, padding with multi-byte NOPs. + ".p2align 4,,15\n", stringify!($name), ":\n", $($body),+, ".size ", stringify!($name), ", . - ", stringify!($name), "\n", @@ -325,15 +327,15 @@ macro_rules! interrupt_stack { ($name:ident, super_atomic: $is_super_atomic:ident!, |$stack:ident| $code:block) => { paste::item! { #[no_mangle] - unsafe extern "C" fn [<__interrupt_ $name>](stack: *mut $crate::arch::x86_64::interrupt::InterruptStack) { - // This inner function is needed because macros are buggy: - // https://github.com/dtolnay/paste/issues/7 - #[inline(always)] - unsafe fn inner($stack: &mut $crate::arch::x86_64::interrupt::InterruptStack) { + unsafe extern "C" fn [<__interrupt_ $name>]($stack: &mut $crate::arch::x86_64::interrupt::InterruptStack) { + let _guard = $crate::ptrace::set_process_regs($stack); + + // TODO: Force the declarations to specify unsafe? + + #[allow(unused_unsafe)] + unsafe { $code } - let _guard = $crate::ptrace::set_process_regs(stack); - inner(&mut *stack); } function!($name => { @@ -404,15 +406,13 @@ macro_rules! interrupt_error { ($name:ident, |$stack:ident| $code:block) => { paste::item! { #[no_mangle] - unsafe extern "C" fn [<__interrupt_ $name>](stack: *mut $crate::arch::x86_64::interrupt::handler::InterruptErrorStack) { - // This inner function is needed because macros are buggy: - // https://github.com/dtolnay/paste/issues/7 - #[inline(always)] - unsafe fn inner($stack: &mut $crate::arch::x86_64::interrupt::handler::InterruptErrorStack) { + unsafe extern "C" fn [<__interrupt_ $name>]($stack: &mut $crate::arch::x86_64::interrupt::handler::InterruptErrorStack) { + let _guard = $crate::ptrace::set_process_regs(&mut $stack.inner); + + #[allow(unused_unsafe)] + unsafe { $code } - let _guard = $crate::ptrace::set_process_regs(&mut (*stack).inner); - inner(&mut *stack); } function!($name => { From 1cf5f5ea22e86171476bc95e853db43610865a73 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Thu, 17 Jun 2021 18:42:16 +0200 Subject: [PATCH 6/7] Update dependencies to work with latest nightly. --- Cargo.lock | 29 +++++++++++++++++++---------- Cargo.toml | 6 +++--- rmm | 2 +- src/lib.rs | 1 - src/scheme/acpi.rs | 10 +++++----- syscall | 2 +- 6 files changed, 29 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 75d8620..d62066c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,7 +64,7 @@ dependencies = [ "cc", "fdt", "goblin", - "linked_list_allocator 0.8.11", + "linked_list_allocator 0.9.0", "log", "paste", "raw-cpuid 8.1.2", @@ -73,7 +73,7 @@ dependencies = [ "rustc-cfg", "rustc-demangle", "slab_allocator", - "spin 0.5.2", + "spin 0.9.0", "x86", ] @@ -88,18 +88,18 @@ dependencies = [ [[package]] name = "linked_list_allocator" -version = "0.8.11" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "822add9edb1860698b79522510da17bef885171f75aa395cff099d770c609c24" +checksum = "d0b725207570aa16096962d0b20c79f8a543df2280bd3c903022b9b0b4d7ea68" dependencies = [ "spinning_top", ] [[package]] name = "lock_api" -version = "0.3.4" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75" +checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb" dependencies = [ "scopeguard", ] @@ -149,7 +149,7 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.2.8" +version = "0.2.9" dependencies = [ "bitflags", ] @@ -227,10 +227,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" [[package]] -name = "spinning_top" -version = "0.1.1" +name = "spin" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "047031d6df5f5ae0092c97aa4f6bb04cfc9c081b4cd4cb9cdb38657994279a00" +checksum = "b87bbf98cb81332a56c1ee8929845836f85e8ddd693157c30d76660196014478" +dependencies = [ + "lock_api", +] + +[[package]] +name = "spinning_top" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75adad84ee84b521fb2cca2d4fd0f1dab1d8d026bda3c5bea4ca63b5f9f9293c" dependencies = [ "lock_api", ] diff --git a/Cargo.toml b/Cargo.toml index c7fb591..def826d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,13 +14,13 @@ cc = "1.0.3" rustc-cfg = "0.3.0" [dependencies] -bitfield = "0.13.1" bitflags = "1.2.1" -linked_list_allocator = "0.8.4" +bitfield = "0.13.2" +linked_list_allocator = "0.9.0" log = { version = "0.4" } redox_syscall = { path = "syscall" } slab_allocator = { path = "slab_allocator", optional = true } -spin = "0.5.2" +spin = "0.9" paste = "1" rmm = { path = "rmm", default-features = false } diff --git a/rmm b/rmm index c81c4de..c66956c 160000 --- a/rmm +++ b/rmm @@ -1 +1 @@ -Subproject commit c81c4de223583b23d2f91f0978e378ef1dc3676f +Subproject commit c66956ca2ac1b4df3904afbafe53321a4b6c50af diff --git a/src/lib.rs b/src/lib.rs index 9f141dd..d0cc4fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,7 +47,6 @@ #![cfg_attr(target_arch = "aarch64", feature(llvm_asm))] // TODO: Rewrite using asm! #![feature(concat_idents)] #![feature(const_btree_new)] -#![feature(const_fn)] #![feature(core_intrinsics)] #![feature(global_asm)] #![feature(integer_atomics)] diff --git a/src/scheme/acpi.rs b/src/scheme/acpi.rs index c56a581..41e731b 100644 --- a/src/scheme/acpi.rs +++ b/src/scheme/acpi.rs @@ -54,7 +54,7 @@ pub fn register_kstop() -> bool { *KSTOP_FLAG.lock() = true; let mut waiters_awoken = KSTOP_WAITCOND.notify(); - if let Some(&acpi_scheme) = SCHEME_ID.r#try() { + if let Some(&acpi_scheme) = SCHEME_ID.get() { let handles = HANDLES.read(); for (&fd, _) in handles.iter().filter(|(_, handle)| handle.kind == HandleKind::ShutdownPipe) { @@ -88,7 +88,7 @@ impl AcpiScheme { data_init = true; let rsdt_or_xsdt = RXSDT_ENUM - .r#try() + .get() .expect("expected RXSDT_ENUM to be initialized before AcpiScheme"); let table = match rsdt_or_xsdt { @@ -168,7 +168,7 @@ impl Scheme for AcpiScheme { match handle.kind { HandleKind::Rxsdt => { - let data = DATA.r#try().ok_or(Error::new(EBADFD))?; + let data = DATA.get().ok_or(Error::new(EBADFD))?; stat.st_mode = MODE_FILE; stat.st_size = data.len().try_into().unwrap_or(u64::max_value()); @@ -194,7 +194,7 @@ impl Scheme for AcpiScheme { } let file_len = match handle.kind { - HandleKind::Rxsdt => DATA.r#try().ok_or(Error::new(EBADFD))?.len(), + HandleKind::Rxsdt => DATA.get().ok_or(Error::new(EBADFD))?.len(), HandleKind::ShutdownPipe => 1, HandleKind::TopLevel => TOPLEVEL_CONTENTS.len(), }; @@ -251,7 +251,7 @@ impl Scheme for AcpiScheme { handle.offset = 1; return Ok(1); } - HandleKind::Rxsdt => DATA.r#try().ok_or(Error::new(EBADFD))?, + HandleKind::Rxsdt => DATA.get().ok_or(Error::new(EBADFD))?, HandleKind::TopLevel => TOPLEVEL_CONTENTS, }; diff --git a/syscall b/syscall index 0b51bdd..52fcd23 160000 --- a/syscall +++ b/syscall @@ -1 +1 @@ -Subproject commit 0b51bddd288dac97c9c7fc04bca9f16ad3b93bd2 +Subproject commit 52fcd238db87354b6556eac9b05bb8ab2658426a From ecfcedb9bf50130e87ed22a8581ab5f70b20eab3 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Fri, 18 Jun 2021 09:20:13 +0200 Subject: [PATCH 7/7] Remove explicit .intel_syntax directive. It is now default, and the new compiler generated a lot of warnings now that Intel syntax has (luckily) become the default. --- src/arch/x86_64/interrupt/handler.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/arch/x86_64/interrupt/handler.rs b/src/arch/x86_64/interrupt/handler.rs index cde79a1..ac67cc8 100644 --- a/src/arch/x86_64/interrupt/handler.rs +++ b/src/arch/x86_64/interrupt/handler.rs @@ -202,9 +202,7 @@ impl InterruptErrorStack { macro_rules! intel_asm { ($($strings:expr,)+) => { global_asm!(concat!( - ".intel_syntax noprefix\n", $($strings),+, - ".att_syntax prefix\n", )); }; }