From 4c056d7b2c48e00e25f691c09943be2b75905396 Mon Sep 17 00:00:00 2001 From: ch3lt Date: Thu, 3 Apr 2025 23:18:23 +0200 Subject: [PATCH] WIP: codebase ref: remove kernel spec. from qlib --- qkernel/src/lib.rs | 1 + qkernel/src/qkernel.rs | 15 +++ qkernel/src/qkernel/arch.rs | 16 +++ .../src/qkernel}/x86_64/arch_x86.rs | 9 +- .../src/qkernel}/x86_64/context.rs | 26 ++-- .../src/qkernel}/x86_64/entry.asm | 0 .../src/qkernel}/x86_64/entry.s | 0 .../src/qkernel}/x86_64/linker.ld | 0 .../src/qkernel}/x86_64/mod.rs | 1 - .../src/qkernel}/x86_64/syscall_entry.s | 0 qlib/kernel/arch/mod.rs | 7 +- qlib/kernel/arch/x86_64/signal.rs | 122 ------------------ 12 files changed, 59 insertions(+), 138 deletions(-) create mode 100644 qkernel/src/qkernel.rs create mode 100644 qkernel/src/qkernel/arch.rs rename {qlib/kernel/arch => qkernel/src/qkernel}/x86_64/arch_x86.rs (98%) rename {qlib/kernel/arch => qkernel/src/qkernel}/x86_64/context.rs (95%) rename {qlib/kernel/arch => qkernel/src/qkernel}/x86_64/entry.asm (100%) rename {qlib/kernel/arch => qkernel/src/qkernel}/x86_64/entry.s (100%) rename {qlib/kernel/arch => qkernel/src/qkernel}/x86_64/linker.ld (100%) rename {qlib/kernel/arch => qkernel/src/qkernel}/x86_64/mod.rs (97%) rename {qlib/kernel/arch => qkernel/src/qkernel}/x86_64/syscall_entry.s (100%) delete mode 100644 qlib/kernel/arch/x86_64/signal.rs diff --git a/qkernel/src/lib.rs b/qkernel/src/lib.rs index 79ece28a2..b05096b87 100644 --- a/qkernel/src/lib.rs +++ b/qkernel/src/lib.rs @@ -124,6 +124,7 @@ mod interrupt; pub mod kernel_def; pub mod rdma_def; mod syscalls; +pub mod qkernel; #[global_allocator] pub static VCPU_ALLOCATOR: GlobalVcpuAllocator = GlobalVcpuAllocator::New(); diff --git a/qkernel/src/qkernel.rs b/qkernel/src/qkernel.rs new file mode 100644 index 000000000..9132045a5 --- /dev/null +++ b/qkernel/src/qkernel.rs @@ -0,0 +1,15 @@ +// Copyright (c) 2021 Quark Container Authors / 2018 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub mod arch; diff --git a/qkernel/src/qkernel/arch.rs b/qkernel/src/qkernel/arch.rs new file mode 100644 index 000000000..3712cbe33 --- /dev/null +++ b/qkernel/src/qkernel/arch.rs @@ -0,0 +1,16 @@ +// Copyright (c) 2021 Quark Container Authors / 2018 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#[cfg(target_arch = "x86_64")] +#[path = "./x86_64/mod.rs"] +pub mod __arch; diff --git a/qlib/kernel/arch/x86_64/arch_x86.rs b/qkernel/src/qkernel/x86_64/arch_x86.rs similarity index 98% rename from qlib/kernel/arch/x86_64/arch_x86.rs rename to qkernel/src/qkernel/x86_64/arch_x86.rs index 247487b5d..3eff072dc 100644 --- a/qlib/kernel/arch/x86_64/arch_x86.rs +++ b/qkernel/src/qkernel/x86_64/arch_x86.rs @@ -12,16 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. +use crate::qlib::kernel::SignalDef; use crate::qlib::mutex::*; use alloc::sync::Arc; use core::sync::atomic::AtomicUsize; use core::sync::atomic::AtomicU64; use core::sync::atomic::Ordering; -use super::super::super::super::cpuid::*; -use super::super::super::asm::*; -use super::super::super::SignalDef::*; -use super::super::super::FP_STATE; +use crate::qlib::cpuid::*; +use crate::asm::*; +use SignalDef::*; +use FP_STATE; // System-related constants for x86. diff --git a/qlib/kernel/arch/x86_64/context.rs b/qkernel/src/qkernel/x86_64/context.rs similarity index 95% rename from qlib/kernel/arch/x86_64/context.rs rename to qkernel/src/qkernel/x86_64/context.rs index 3286bf297..a2449dabb 100644 --- a/qlib/kernel/arch/x86_64/context.rs +++ b/qkernel/src/qkernel/x86_64/context.rs @@ -12,14 +12,24 @@ // See the License for the specific language governing permissions and // limitations under the License. -use super::super::super::super::addr::*; -use super::super::super::super::common::*; -use super::super::super::super::limits::*; -use super::super::super::super::linux_def::*; -use super::super::super::kernel_util::*; -use super::super::super::memmgr::arch::*; -use super::super::super::SignalDef::*; -use super::arch_def::*; +use addr::*; +use crate::kernel_util; +use crate::qlib::addr; +use crate::qlib::common; +use crate::qlib::kernel::memmgr; +use crate::qlib::kernel::SignalDef; +use crate::qlib::limits; +use crate::qlib::linux_def; + +use common::*; +use limits::*; +use linux_def::*; +use kernel_util::*; +use memmgr::arch::*; +use SignalDef::*; +use arch_def::*; + +use super::arch_def; // These constants come directly from Linux. diff --git a/qlib/kernel/arch/x86_64/entry.asm b/qkernel/src/qkernel/x86_64/entry.asm similarity index 100% rename from qlib/kernel/arch/x86_64/entry.asm rename to qkernel/src/qkernel/x86_64/entry.asm diff --git a/qlib/kernel/arch/x86_64/entry.s b/qkernel/src/qkernel/x86_64/entry.s similarity index 100% rename from qlib/kernel/arch/x86_64/entry.s rename to qkernel/src/qkernel/x86_64/entry.s diff --git a/qlib/kernel/arch/x86_64/linker.ld b/qkernel/src/qkernel/x86_64/linker.ld similarity index 100% rename from qlib/kernel/arch/x86_64/linker.ld rename to qkernel/src/qkernel/x86_64/linker.ld diff --git a/qlib/kernel/arch/x86_64/mod.rs b/qkernel/src/qkernel/x86_64/mod.rs similarity index 97% rename from qlib/kernel/arch/x86_64/mod.rs rename to qkernel/src/qkernel/x86_64/mod.rs index 278021f35..a76071b6b 100644 --- a/qlib/kernel/arch/x86_64/mod.rs +++ b/qkernel/src/qkernel/x86_64/mod.rs @@ -15,4 +15,3 @@ pub mod arch_def; pub mod context; -pub mod signal; diff --git a/qlib/kernel/arch/x86_64/syscall_entry.s b/qkernel/src/qkernel/x86_64/syscall_entry.s similarity index 100% rename from qlib/kernel/arch/x86_64/syscall_entry.s rename to qkernel/src/qkernel/x86_64/syscall_entry.s diff --git a/qlib/kernel/arch/mod.rs b/qlib/kernel/arch/mod.rs index e32b69ba3..6aa28eb94 100644 --- a/qlib/kernel/arch/mod.rs +++ b/qlib/kernel/arch/mod.rs @@ -11,9 +11,10 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -#[cfg(target_arch = "x86_64")] -#[path = "./x86_64/mod.rs"] -pub mod __arch; + +// +// TODO - Arm next +// #[cfg(target_arch = "aarch64")] #[path = "./aarch64/mod.rs"] pub mod __arch; diff --git a/qlib/kernel/arch/x86_64/signal.rs b/qlib/kernel/arch/x86_64/signal.rs deleted file mode 100644 index 9758eee40..000000000 --- a/qlib/kernel/arch/x86_64/signal.rs +++ /dev/null @@ -1,122 +0,0 @@ -// Copyright (c) 2021 Quark Container Authors / 2018 The gVisor Authors. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//use core::mem; -//use alloc::sync::Arc; -//use crate::qlib::mutex::*; - -//use super::super::super::SignalDef::*; -//use super::super::super::stack::*; -//use super::super::super::super::common::*; -//use super::super::super::super::linux_def::*; -use super::context::*; -//use super::arch_x86::*; - -const FP_XSTATE_MAGIC2_SIZE: usize = 4; - -impl Context64 { - /*fn fpuFrameSize(&self) -> (u64, bool) { - let mut size = self.state.x86FPState.lock().size; - let mut useXsave = false; - if size > 512 { - // Make room for the magic cookie at the end of the xsave frame. - size += FP_XSTATE_MAGIC2_SIZE; - useXsave = true; - } - - return (size as u64, useXsave) - }*/ - - // SignalSetup implements Context.SignalSetup. (Compare to Linux's - // arch/x86/kernel/signal.c:__setup_rt_frame().) - /*pub fn SignalSetup(&mut self, st: &mut Stack, act: &SigAct, info: &mut SignalInfo, alt: &SignalStack, sigset: SignalSet) -> Result<()> { - let mut sp = st.sp; - - // "The 128-byte area beyond the location pointed to by %rsp is considered - // to be reserved and shall not be modified by signal or interrupt - // handlers. ... leaf functions may use this area for their entire stack - // frame, rather than adjusting the stack pointer in the prologue and - // epilogue." - AMD64 ABI - // - // (But this doesn't apply if we're starting at the top of the signal - // stack, in which case there is no following stack frame.) - if !(alt.IsEnable() && sp == alt.Top()) { - sp -= 128; - } - - let (fpSize, _) = self.fpuFrameSize(); - sp = (sp - fpSize) & !(64 - 1); - - let regs = &self.state.Regs; - // Construct the UContext64 now since we need its size. - let mut uc = UContext { - // No _UC_FP_XSTATE: see Fpstate above. - // No _UC_STRICT_RESTORE_SS: we don't allow SS changes. - Flags: UC_SIGCONTEXT_SS, - Link: 0, - Stack: *alt, - MContext: SigContext::New(regs, sigset.0, 0, 0), - Sigset: sigset.0, - }; - - // based on the fault that caused the signal. For now, leave Err and - // Trapno unset and assume CR2 == info.Addr() for SIGSEGVs and - // SIGBUSes. - if info.Signo == Signal::SIGSEGV || info.Signo == Signal::SIGBUS { - uc.MContext.cr2 = info.SigFault().addr; - } - - // "... the value (%rsp+8) is always a multiple of 16 (...) when - // control is transferred to the function entry point." - AMD64 ABI - let ucSize = mem::size_of::(); - - let frameSize = 8 + ucSize + 128; - let frameBottom = (sp - frameSize as u64) & !15 - 8; - sp = frameBottom + frameSize as u64; - st.sp = sp; - - // Prior to proceeding, figure out if the frame will exhaust the range - // for the signal stack. This is not allowed, and should immediately - // force signal delivery (reverting to the default handler). - if act.flags.IsOnStack() && alt.IsEnable() && !alt.Contains(frameBottom) { - return Err(Error::SysError(SysErr::EFAULT)) - } - - // Adjust the code. - info.FixSignalCodeForUser(); - - let infoAddr = st.PushType::(info); - let ucAddr = st.PushType::(&uc); - - if act.flags.HasRestorer() { - st.PushU64(act.restorer); - } else { - return Err(Error::SysError(SysErr::EFAULT)) - } - - self.state.Regs.rip = act.handler; - self.state.Regs.rsp = st.sp; - self.state.Regs.rdi = info.Signo as u64; - self.state.Regs.rsi = infoAddr; - self.state.Regs.rdx = ucAddr; - self.state.Regs.rax = 0; - - // Save the thread's floating point state. - self.sigFPState.push(self.state.x86FPState.clone()); - - self.state.x86FPState = Arc::new(QMutex::new(X86fpstate::NewX86FPState())); - - return Ok(()) - }*/ -}