diff --git a/kernel/src/arch/x86/cpu.rs b/kernel/src/arch/x86/cpu.rs index 8963dd8da..79fe9cbc3 100644 --- a/kernel/src/arch/x86/cpu.rs +++ b/kernel/src/arch/x86/cpu.rs @@ -7,7 +7,7 @@ use alloc::{ }; use ostd::{ - cpu::{cpuid, CpuException, CpuExceptionInfo, RawGeneralRegs, UserContext}, + cpu::context::{cpuid, CpuException, CpuExceptionInfo, RawGeneralRegs, UserContext}, Pod, }; diff --git a/kernel/src/arch/x86/signal.rs b/kernel/src/arch/x86/signal.rs index aac24ffda..a7ec448dc 100644 --- a/kernel/src/arch/x86/signal.rs +++ b/kernel/src/arch/x86/signal.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MPL-2.0 -use ostd::cpu::{CpuException, CpuExceptionInfo, UserContext}; +use ostd::cpu::context::{CpuException, CpuExceptionInfo, UserContext}; use crate::process::signal::{ constants::*, sig_num::SigNum, signals::fault::FaultSignal, SignalContext, diff --git a/kernel/src/process/clone.rs b/kernel/src/process/clone.rs index a58c102cb..189d35491 100644 --- a/kernel/src/process/clone.rs +++ b/kernel/src/process/clone.rs @@ -2,7 +2,7 @@ use core::{num::NonZeroU64, sync::atomic::Ordering}; -use ostd::{cpu::UserContext, sync::RwArc, task::Task, user::UserContextApi}; +use ostd::{cpu::context::UserContext, sync::RwArc, task::Task, user::UserContextApi}; use super::{ posix_thread::{AsPosixThread, PosixThreadBuilder, ThreadName}, diff --git a/kernel/src/process/posix_thread/builder.rs b/kernel/src/process/posix_thread/builder.rs index f0f3f5d19..729e6334f 100644 --- a/kernel/src/process/posix_thread/builder.rs +++ b/kernel/src/process/posix_thread/builder.rs @@ -3,7 +3,7 @@ #![expect(dead_code)] use ostd::{ - cpu::{CpuSet, UserContext}, + cpu::{context::UserContext, CpuSet}, sync::RwArc, task::Task, }; diff --git a/kernel/src/process/posix_thread/posix_thread_ext.rs b/kernel/src/process/posix_thread/posix_thread_ext.rs index ed11225f4..cca24beb3 100644 --- a/kernel/src/process/posix_thread/posix_thread_ext.rs +++ b/kernel/src/process/posix_thread/posix_thread_ext.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MPL-2.0 -use ostd::{cpu::UserContext, task::Task, user::UserContextApi}; +use ostd::{cpu::context::UserContext, task::Task, user::UserContextApi}; use super::{builder::PosixThreadBuilder, name::ThreadName, PosixThread}; use crate::{ diff --git a/kernel/src/process/signal/mod.rs b/kernel/src/process/signal/mod.rs index 47fb9801b..8a267cc8a 100644 --- a/kernel/src/process/signal/mod.rs +++ b/kernel/src/process/signal/mod.rs @@ -19,7 +19,7 @@ use align_ext::AlignExt; use c_types::{siginfo_t, ucontext_t}; use constants::SIGSEGV; pub use events::{SigEvents, SigEventsFilter}; -use ostd::{cpu::UserContext, user::UserContextApi}; +use ostd::{cpu::context::UserContext, user::UserContextApi}; pub use pause::{with_signal_blocked, Pause}; pub use poll::{PollAdaptor, PollHandle, Pollable, Pollee, Poller}; use sig_action::{SigAction, SigActionFlags, SigDefaultAction}; diff --git a/kernel/src/syscall/arch_prctl.rs b/kernel/src/syscall/arch_prctl.rs index 041de4874..86a452363 100644 --- a/kernel/src/syscall/arch_prctl.rs +++ b/kernel/src/syscall/arch_prctl.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MPL-2.0 -use ostd::cpu::UserContext; +use ostd::cpu::context::UserContext; use super::SyscallReturn; use crate::prelude::*; diff --git a/kernel/src/syscall/clone.rs b/kernel/src/syscall/clone.rs index b3c90f2c9..524ca91e0 100644 --- a/kernel/src/syscall/clone.rs +++ b/kernel/src/syscall/clone.rs @@ -2,7 +2,7 @@ use core::num::NonZeroU64; -use ostd::cpu::UserContext; +use ostd::cpu::context::UserContext; use super::SyscallReturn; use crate::{ diff --git a/kernel/src/syscall/execve.rs b/kernel/src/syscall/execve.rs index 25a4b9405..4175ee2c9 100644 --- a/kernel/src/syscall/execve.rs +++ b/kernel/src/syscall/execve.rs @@ -2,7 +2,7 @@ use aster_rights::WriteOp; use ostd::{ - cpu::{FpuState, RawGeneralRegs, UserContext}, + cpu::context::{FpuState, RawGeneralRegs, UserContext}, user::UserContextApi, }; diff --git a/kernel/src/syscall/fork.rs b/kernel/src/syscall/fork.rs index 801f9365e..65b0cb8ab 100644 --- a/kernel/src/syscall/fork.rs +++ b/kernel/src/syscall/fork.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MPL-2.0 -use ostd::cpu::UserContext; +use ostd::cpu::context::UserContext; use super::SyscallReturn; use crate::{ diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index 2cb006243..28d89ca57 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -3,7 +3,7 @@ //! Read the Cpu ctx content then dispatch syscall to corresponding handler //! The each sub module contains functions that handle real syscall logic. pub use clock_gettime::ClockId; -use ostd::cpu::UserContext; +use ostd::cpu::context::UserContext; use crate::{context::Context, cpu::LinuxAbi, prelude::*}; @@ -281,7 +281,7 @@ macro_rules! impl_syscall_nums_and_dispatch_fn { syscall_number: u64, args: [u64; 6], ctx: &crate::context::Context, - user_ctx: &mut ostd::cpu::UserContext, + user_ctx: &mut ostd::cpu::context::UserContext, ) -> $crate::prelude::Result<$crate::syscall::SyscallReturn> { match syscall_number { $( diff --git a/kernel/src/syscall/rt_sigreturn.rs b/kernel/src/syscall/rt_sigreturn.rs index 0ba202d9e..f9c2ec75c 100644 --- a/kernel/src/syscall/rt_sigreturn.rs +++ b/kernel/src/syscall/rt_sigreturn.rs @@ -2,7 +2,7 @@ use core::sync::atomic::Ordering; -use ostd::{cpu::UserContext, user::UserContextApi}; +use ostd::{cpu::context::UserContext, user::UserContextApi}; use super::SyscallReturn; use crate::{prelude::*, process::signal::c_types::ucontext_t}; diff --git a/kernel/src/thread/exception.rs b/kernel/src/thread/exception.rs index 72cd167d3..f38087726 100644 --- a/kernel/src/thread/exception.rs +++ b/kernel/src/thread/exception.rs @@ -3,7 +3,7 @@ #![expect(unused_variables)] use aster_rights::Full; -use ostd::cpu::*; +use ostd::cpu::context::{CpuExceptionInfo, UserContext}; use crate::{ current_userspace, diff --git a/kernel/src/thread/task.rs b/kernel/src/thread/task.rs index c40b073fe..8864ce23a 100644 --- a/kernel/src/thread/task.rs +++ b/kernel/src/thread/task.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MPL-2.0 use ostd::{ - cpu::UserContext, + cpu::context::UserContext, task::{Task, TaskOptions}, user::{ReturnReason, UserContextApi, UserMode}, }; diff --git a/osdk/tests/examples_in_book/write_a_kernel_in_100_lines_templates/lib.rs b/osdk/tests/examples_in_book/write_a_kernel_in_100_lines_templates/lib.rs index 4783698a1..f02335745 100644 --- a/osdk/tests/examples_in_book/write_a_kernel_in_100_lines_templates/lib.rs +++ b/osdk/tests/examples_in_book/write_a_kernel_in_100_lines_templates/lib.rs @@ -12,7 +12,7 @@ use alloc::sync::Arc; use alloc::vec; use ostd::arch::qemu::{exit_qemu, QemuExitCode}; -use ostd::cpu::UserContext; +use ostd::cpu::context::UserContext; use ostd::mm::{ CachePolicy, FallibleVmRead, FrameAllocOptions, PageFlags, PageProperty, Vaddr, VmIo, VmSpace, VmWriter, PAGE_SIZE, diff --git a/ostd/src/arch/riscv/cpu/context.rs b/ostd/src/arch/riscv/cpu/context.rs index 76ebc3bec..21f09cb8c 100644 --- a/ostd/src/arch/riscv/cpu/context.rs +++ b/ostd/src/arch/riscv/cpu/context.rs @@ -1,16 +1,16 @@ // SPDX-License-Identifier: MPL-2.0 -//! CPU - -pub mod local; +//! CPU execution context control. use core::fmt::Debug; use riscv::register::scause::{Exception, Trap}; -pub use super::trap::GeneralRegs as RawGeneralRegs; -use super::trap::{TrapFrame, UserContext as RawUserContext}; -use crate::user::{ReturnReason, UserContextApi, UserContextApiInternal}; +pub use crate::arch::riscv::trap::GeneralRegs as RawGeneralRegs; +use crate::{ + arch::riscv::trap::{TrapFrame, UserContext as RawUserContext}, + user::{ReturnReason, UserContextApi, UserContextApiInternal}, +}; /// Cpu context, including both general-purpose registers and FPU state. #[derive(Clone, Copy, Debug)] diff --git a/ostd/src/arch/riscv/cpu/mod.rs b/ostd/src/arch/riscv/cpu/mod.rs new file mode 100644 index 000000000..bf160e756 --- /dev/null +++ b/ostd/src/arch/riscv/cpu/mod.rs @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: MPL-2.0 + +//! CPU context & state control and CPU local memory. + +pub mod context; +pub mod local; diff --git a/ostd/src/arch/x86/cpu/context/mod.rs b/ostd/src/arch/x86/cpu/context/mod.rs index a1a2f40da..63a2630c8 100644 --- a/ostd/src/arch/x86/cpu/context/mod.rs +++ b/ostd/src/arch/x86/cpu/context/mod.rs @@ -1,8 +1,6 @@ // SPDX-License-Identifier: MPL-2.0 -//! CPU. - -pub mod local; +//! CPU execution context control. use alloc::boxed::Box; use core::{ @@ -18,19 +16,14 @@ use num_derive::FromPrimitive; use num_traits::FromPrimitive; use spin::Once; use x86::bits64::segmentation::wrfsbase; -pub use x86::cpuid; use x86_64::registers::{ control::{Cr0, Cr0Flags}, rflags::RFlags, xcontrol::XCr0, }; -pub use super::trap::GeneralRegs as RawGeneralRegs; -use super::{ - trap::{TrapFrame, UserContext as RawUserContext}, - CPU_FEATURES, -}; use crate::{ + arch::x86::CPU_FEATURES, task::scheduler, trap::call_irq_callback_functions, user::{ReturnReason, UserContextApi, UserContextApiInternal}, @@ -44,6 +37,12 @@ cfg_if! { } } +pub use x86::cpuid; + +pub use crate::arch::trap::{ + GeneralRegs as RawGeneralRegs, TrapFrame, UserContext as RawUserContext, +}; + /// Cpu context, including both general-purpose registers and FPU state. #[derive(Clone, Default, Debug)] #[repr(C)] @@ -588,7 +587,7 @@ static XSAVE_AREA_SIZE: Once = Once::new(); /// The max size in bytes of the XSAVE area. const MAX_XSAVE_AREA_SIZE: usize = 4096; -pub(super) fn enable_essential_features() { +pub(in crate::arch::x86) fn enable_essential_features() { XSTATE_MAX_FEATURES.call_once(|| { const XSTATE_CPUID: u32 = 0x0000000d; diff --git a/ostd/src/arch/x86/cpu/context/tdx.rs b/ostd/src/arch/x86/cpu/context/tdx.rs index 697428a22..24654fe1b 100644 --- a/ostd/src/arch/x86/cpu/context/tdx.rs +++ b/ostd/src/arch/x86/cpu/context/tdx.rs @@ -4,7 +4,7 @@ use tdx_guest::{ handle_virtual_exception as do_handle_virtual_exception, tdcall, TdgVeInfo, TdxTrapFrame, }; -use crate::cpu::{RawGeneralRegs, UserContext}; +use crate::cpu::context::{RawGeneralRegs, UserContext}; pub(crate) struct VirtualizationExceptionHandler { ve_info: TdgVeInfo, diff --git a/ostd/src/arch/x86/cpu/mod.rs b/ostd/src/arch/x86/cpu/mod.rs new file mode 100644 index 000000000..bf160e756 --- /dev/null +++ b/ostd/src/arch/x86/cpu/mod.rs @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: MPL-2.0 + +//! CPU context & state control and CPU local memory. + +pub mod context; +pub mod local; diff --git a/ostd/src/arch/x86/mod.rs b/ostd/src/arch/x86/mod.rs index 12943ed87..80adb8a59 100644 --- a/ostd/src/arch/x86/mod.rs +++ b/ostd/src/arch/x86/mod.rs @@ -119,7 +119,7 @@ pub(crate) unsafe fn init_on_ap() { } pub(crate) fn interrupts_ack(irq_number: usize) { - if !cpu::CpuException::is_cpu_exception(irq_number as u16) { + if !cpu::context::CpuException::is_cpu_exception(irq_number as u16) { kernel::apic::with_borrow(|apic| { apic.eoi(); }); @@ -178,7 +178,7 @@ pub(crate) fn enable_cpu_features() { cpuid.get_feature_info().unwrap() }); - cpu::enable_essential_features(); + cpu::context::enable_essential_features(); let mut cr4 = x86_64::registers::control::Cr4::read(); cr4 |= Cr4Flags::FSGSBASE diff --git a/ostd/src/arch/x86/trap/mod.rs b/ostd/src/arch/x86/trap/mod.rs index 69a74071e..dc0739f69 100644 --- a/ostd/src/arch/x86/trap/mod.rs +++ b/ostd/src/arch/x86/trap/mod.rs @@ -28,7 +28,7 @@ use spin::Once; use super::ex_table::ExTable; use crate::{ arch::irq::{disable_local, enable_local}, - cpu::{CpuException, CpuExceptionInfo, PageFaultErrorCode}, + cpu::context::{CpuException, CpuExceptionInfo, PageFaultErrorCode}, cpu_local_cell, if_tdx_enabled, mm::{ kspace::{KERNEL_PAGE_TABLE, LINEAR_MAPPING_BASE_VADDR, LINEAR_MAPPING_VADDR_RANGE}, diff --git a/ostd/src/task/mod.rs b/ostd/src/task/mod.rs index f59252494..420fecb1d 100644 --- a/ostd/src/task/mod.rs +++ b/ostd/src/task/mod.rs @@ -27,7 +27,7 @@ pub use self::{ scheduler::info::{AtomicCpuId, TaskScheduleInfo}, }; pub(crate) use crate::arch::task::{context_switch, TaskContext}; -use crate::{cpu::UserContext, prelude::*, trap::in_interrupt_context}; +use crate::{cpu::context::UserContext, prelude::*, trap::in_interrupt_context}; static POST_SCHEDULE_HANDLER: Once = Once::new(); diff --git a/ostd/src/user.rs b/ostd/src/user.rs index 7fc90f7ee..f71475591 100644 --- a/ostd/src/user.rs +++ b/ostd/src/user.rs @@ -2,7 +2,7 @@ //! User mode. -use crate::{cpu::UserContext, trap::TrapFrame}; +use crate::{cpu::context::UserContext, trap::TrapFrame}; /// Specific architectures need to implement this trait. This should only used in [`UserMode`] ///