From 6d1e436d29671e677fc13b938dc315bd365ac5d4 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Fri, 9 Feb 2024 10:09:15 +0100 Subject: [PATCH] Remove some dead code. --- src/context/arch/aarch64.rs | 66 ------------------------------------- src/context/arch/x86.rs | 55 ------------------------------- src/context/arch/x86_64.rs | 59 +-------------------------------- src/context/context.rs | 4 +-- src/context/list.rs | 2 +- src/context/switch.rs | 19 +++++++---- src/scheme/proc.rs | 2 +- src/syscall/mod.rs | 2 +- src/syscall/usercopy.rs | 6 ---- 9 files changed, 18 insertions(+), 197 deletions(-) diff --git a/src/context/arch/aarch64.rs b/src/context/arch/aarch64.rs index 86137259d5..68cd2e9ecd 100644 --- a/src/context/arch/aarch64.rs +++ b/src/context/arch/aarch64.rs @@ -10,9 +10,7 @@ use spin::Once; use crate::{ device::cpu::registers::{control_regs, tlb}, - interrupt::handler::ScratchRegisters, paging::{RmmA, RmmArch, TableKind}, - pop_scratch, push_scratch, syscall::FloatRegisters, percpu::PercpuBlock, }; @@ -97,33 +95,6 @@ impl Context { self.tpidrro_el0 } - pub unsafe fn signal_stack(&mut self, handler: extern "C" fn(usize), sig: u8) { - let lr = self.lr.clone(); - self.push_pair((sig as usize, lr)); - self.push_pair((0 as usize, handler as usize)); - self.set_lr(signal_handler_wrapper as usize); - } - - // It isn't possible to implement "just push one value to stack" on aarch64 - // https://community.arm.com/arm-community-blogs/b/architectures-and-processors-blog/posts/using-the-stack-in-aarch32-and-aarch64 - // Looks like QEMU TCG doesn't care about it, so it works in it, but not on bare metal or kvm - // Reverese to be compatible with ldp and stp instructions - // TODO: optimize? - pub unsafe fn push_pair(&mut self, pair: (usize, usize)) { - self.sp -= 1 * mem::size_of::(); - *(self.sp as *mut usize) = pair.1; - self.sp -= 1 * mem::size_of::(); - *(self.sp as *mut usize) = pair.0; - } - - pub unsafe fn pop_pair(&mut self) -> (usize, usize) { - let a = *(self.sp as *const usize); - self.sp += 1 * mem::size_of::(); - let b = *(self.sp as *const usize); - self.sp += 1 * mem::size_of::(); - (a, b) - } - pub fn dump(&self) { println!("elr_el1: 0x{:016x}", self.elr_el1); println!("sp_el0: 0x{:016x}", self.sp_el0); @@ -336,40 +307,3 @@ unsafe extern "C" fn switch_to_inner(_prev: &mut Context, _next: &mut Context) { options(noreturn), ); } - -#[allow(dead_code)] -#[repr(packed)] -pub struct SignalHandlerStack { - scratch: ScratchRegisters, - padding: usize, - handler: extern "C" fn(usize), - sig: usize, - lr: usize, -} - -#[naked] -unsafe extern "C" fn signal_handler_wrapper() { - #[inline(never)] - unsafe extern "C" fn inner(stack: &SignalHandlerStack) { - (stack.handler)(stack.sig); - } - - // Push scratch registers - core::arch::asm!( - concat!( - push_scratch!(), - " - mov x0, sp - bl {inner} - ", - pop_scratch!(), - " - add sp, sp, 32 - ldr x30, [sp, #-8] - ret - " - ), - inner = sym inner, - options(noreturn), - ); -} diff --git a/src/context/arch/x86.rs b/src/context/arch/x86.rs index 4257c3d5ba..5046686396 100644 --- a/src/context/arch/x86.rs +++ b/src/context/arch/x86.rs @@ -4,10 +4,8 @@ use alloc::sync::Arc; use crate::{ gdt::{pcr, GDT_USER_FS, GDT_USER_GS}, - interrupt::handler::ScratchRegisters, paging::{RmmA, RmmArch, TableKind}, percpu::PercpuBlock, - pop_scratch, push_scratch, syscall::FloatRegisters, }; @@ -68,23 +66,6 @@ impl Context { pub fn set_stack(&mut self, address: usize) { self.esp = address; } - - pub unsafe fn signal_stack(&mut self, handler: extern "C" fn(usize), sig: u8) { - self.push_stack(sig as usize); - self.push_stack(handler as usize); - self.push_stack(signal_handler_wrapper as usize); - } - - pub unsafe fn push_stack(&mut self, value: usize) { - self.esp -= mem::size_of::(); - *(self.esp as *mut usize) = value; - } - - pub unsafe fn pop_stack(&mut self) -> usize { - let value = *(self.esp as *const usize); - self.esp += mem::size_of::(); - value - } } impl super::Context { pub fn get_fx_regs(&self) -> FloatRegisters { @@ -221,39 +202,3 @@ unsafe extern "cdecl" fn switch_to_inner() { options(noreturn), ); } -#[allow(dead_code)] -#[repr(packed)] -pub struct SignalHandlerStack { - scratch: ScratchRegisters, - handler: extern "C" fn(usize), - sig: usize, - eip: usize, -} - -#[naked] -unsafe extern "C" fn signal_handler_wrapper() { - #[inline(never)] - unsafe extern "C" fn inner(stack: &SignalHandlerStack) { - (stack.handler)(stack.sig); - } - - // Push scratch registers - core::arch::asm!( - concat!( - "push eax", - push_scratch!(), - " - push esp - call {inner} - pop esp - ", - pop_scratch!(), - " - add esp, 8 - ret - ", - ), - inner = sym inner, - options(noreturn), - ); -} diff --git a/src/context/arch/x86_64.rs b/src/context/arch/x86_64.rs index 5b12fda3ab..baa5e4b886 100644 --- a/src/context/arch/x86_64.rs +++ b/src/context/arch/x86_64.rs @@ -1,14 +1,9 @@ use core::{ - mem, ptr::{addr_of, addr_of_mut}, sync::atomic::AtomicBool, }; -use crate::{ - interrupt::handler::ScratchRegisters, - pop_scratch, push_scratch, - syscall::FloatRegisters, percpu::PercpuBlock, -}; +use crate::{syscall::FloatRegisters, percpu::PercpuBlock}; use core::mem::offset_of; use spin::Once; @@ -84,23 +79,6 @@ impl Context { pub fn set_stack(&mut self, address: usize) { self.rsp = address; } - - pub unsafe fn signal_stack(&mut self, handler: extern "C" fn(usize), sig: u8) { - self.push_stack(sig as usize); - self.push_stack(handler as usize); - self.push_stack(signal_handler_wrapper as usize); - } - - pub unsafe fn push_stack(&mut self, value: usize) { - self.rsp -= mem::size_of::(); - *(self.rsp as *mut usize) = value; - } - - pub unsafe fn pop_stack(&mut self) -> usize { - let value = *(self.rsp as *const usize); - self.rsp += mem::size_of::(); - value - } } impl super::Context { pub fn get_fx_regs(&self) -> FloatRegisters { @@ -299,38 +277,3 @@ unsafe extern "sysv64" fn switch_to_inner(_prev: &mut Context, _next: &mut Conte options(noreturn), ); } -#[allow(dead_code)] -#[repr(packed)] -pub struct SignalHandlerStack { - scratch: ScratchRegisters, - handler: extern "C" fn(usize), - sig: usize, - rip: usize, -} - -#[naked] -unsafe extern "C" fn signal_handler_wrapper() { - #[inline(never)] - unsafe extern "C" fn inner(stack: &SignalHandlerStack) { - (stack.handler)(stack.sig); - } - - // Push scratch registers - core::arch::asm!( - concat!( - "push rax", - push_scratch!(), - " - mov rdi, rsp - call {inner} - ", - pop_scratch!(), - " - add rsp, 16 - ret - " - ), - inner = sym inner, - options(noreturn) - ); -} diff --git a/src/context/context.rs b/src/context/context.rs index 0d797ef474..5e0260e399 100644 --- a/src/context/context.rs +++ b/src/context/context.rs @@ -1,10 +1,10 @@ -use alloc::{borrow::Cow, boxed::Box, collections::VecDeque, sync::Arc, vec::Vec}; +use alloc::{borrow::Cow, sync::Arc, vec::Vec}; use core::{cmp::Ordering, mem, num::NonZeroUsize}; use spin::RwLock; use crate::{ arch::{interrupt::InterruptStack, paging::PAGE_SIZE}, - common::{aligned_box::AlignedBox, unique::Unique}, + common::aligned_box::AlignedBox, context::{self, arch, file::FileDescriptor, memory::AddrSpace}, cpu_set::{LogicalCpuId, LogicalCpuSet}, ipi::{ipi, IpiKind, IpiTarget}, diff --git a/src/context/list.rs b/src/context/list.rs index f40f058abf..86d43f888c 100644 --- a/src/context/list.rs +++ b/src/context/list.rs @@ -1,5 +1,5 @@ use alloc::{collections::BTreeMap, sync::Arc}; -use core::{iter, mem}; +use core::iter; use spinning_top::RwSpinlock; diff --git a/src/context/switch.rs b/src/context/switch.rs index 8f7bd71bc3..d783e3f73d 100644 --- a/src/context/switch.rs +++ b/src/context/switch.rs @@ -3,11 +3,11 @@ use core::{cell::Cell, mem, ops::Bound, sync::atomic::Ordering}; use spinning_top::guard::ArcRwSpinlockWriteGuard; use crate::{ - context::{arch, contexts, signal::signal_handler, Context}, + context::{self, arch, contexts, Context}, cpu_set::LogicalCpuId, interrupt, percpu::PercpuBlock, - ptrace, time, + time, }; use super::{ContextId, Status}; @@ -24,6 +24,7 @@ unsafe fn update_runnable(context: &mut Context, cpu_id: LogicalCpuId) -> Update } // Ignore contexts stopped by ptrace + // TODO: ContextStatus::HardBlocked? if context.ptrace_stop { return UpdateResult::Skip; } @@ -198,13 +199,17 @@ pub unsafe fn switch() -> bool { arch::switch_to(prev_context, next_context); - if percpu.switch_internals.switch_signal.replace(false) { - crate::context::signal::signal_handler(); - } - // NOTE: After switch_to is called, the return address can even be different from the // current return address, meaning that we cannot use local variables here, and that we - // need to use the `switch_finish_hook` to be able to release the locks. + // need to use the `switch_finish_hook` to be able to release the locks. Newly created + // contexts will return directly to the function pointer passed to context::spawn, and not + // reach this code until the next context switch back. + + // We can't reuse the `percpu` variable, since it's from the stack before the last + // context::switch, and can thus point to another CPU's percpu block. + if PercpuBlock::current().switch_internals.switch_signal.replace(false) { + context::signal::signal_handler(); + } true } else { diff --git a/src/scheme/proc.rs b/src/scheme/proc.rs index 68bfa8f331..5afef310c2 100644 --- a/src/scheme/proc.rs +++ b/src/scheme/proc.rs @@ -4,7 +4,7 @@ use crate::{ self, file::FileDescriptor, memory::{handle_notify_files, Grant, PageSpan, AddrSpaceWrapper}, - Context, ContextId, Status, context::{SignalState, HardBlockedReason, Altstack, SignalHandler}, + Context, ContextId, Status, context::{HardBlockedReason, Altstack, SignalHandler}, }, memory::PAGE_SIZE, ptrace, diff --git a/src/syscall/mod.rs b/src/syscall/mod.rs index e0cbf4c81a..8b250fca5a 100644 --- a/src/syscall/mod.rs +++ b/src/syscall/mod.rs @@ -14,7 +14,7 @@ pub use self::{ use self::{ data::{Map, SigAction, TimeSpec}, - error::{Error, Result, EINTR, EOVERFLOW, ENOSYS}, + error::{Error, Result, EOVERFLOW, ENOSYS}, flag::{EventFlags, MapFlags, WaitFlags}, number::*, }; diff --git a/src/syscall/usercopy.rs b/src/syscall/usercopy.rs index 09db806e78..d4e1c4f3bd 100644 --- a/src/syscall/usercopy.rs +++ b/src/syscall/usercopy.rs @@ -14,7 +14,6 @@ pub struct UserSlice { } pub type UserSliceRo = UserSlice; pub type UserSliceWo = UserSlice; -pub type UserSliceRw = UserSlice; impl UserSlice { pub fn empty() -> Self { @@ -211,11 +210,6 @@ impl UserSliceWo { Self::new(base, size) } } -impl UserSliceRw { - pub fn rw(base: usize, size: usize) -> Result { - Self::new(base, size) - } -} fn is_kernel_mem(slice: &[u8]) -> bool { (slice.as_ptr() as usize) >= crate::USER_END_OFFSET