Skip to content

Commit

Permalink
sync: move rest of kernel to use oro-sync instead of spin
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- committed Oct 22, 2024
1 parent 1618725 commit f617965
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 105 deletions.
41 changes: 5 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ oro-sync.path = "oro-sync"
limine = "0.2.0"
uart_16550 = "0.3.0"
volatile-register = "0.2.2"
spin = { version = "0.9.8", features = ["ticket_mutex", "fair_mutex"] }

bindgen = { git = "https://github.com/oro-os/dep.rust-bindgen.git" }
syn = { version = "2.0.60", features = ["full", "printing"] }
Expand Down
3 changes: 1 addition & 2 deletions oro-arch-aarch64/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ oro-debug.workspace = true
oro-dtb.workspace = true
oro-type.workspace = true
oro-dbgutil.workspace = true

spin.workspace = true
oro-sync.workspace = true

[lints]
workspace = true
1 change: 1 addition & 0 deletions oro-arch-aarch64/src/boot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod secondary;
use oro_debug::dbg;
#[cfg(debug_assertions)]
use oro_mem::phys::{Phys, PhysAddr};
use oro_sync::Lock;

/// The number of pages to allocate for the secondary core stacks.
// TODO(qix-): Make this configurable.
Expand Down
4 changes: 2 additions & 2 deletions oro-arch-aarch64/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use core::mem::MaybeUninit;

use oro_kernel::KernelState;
use spin::mutex::fair::FairMutex;
use oro_sync::TicketMutex;

/// The global kernel state. Initialized once during boot
/// and re-used across all cores.
Expand Down Expand Up @@ -34,7 +34,7 @@ pub unsafe fn initialize_primary(pfa: crate::Pfa) {

// SAFETY(qix-): We know what we're doing here.
#[expect(static_mut_refs)]
KernelState::init(&mut KERNEL_STATE, FairMutex::new(pfa))
KernelState::init(&mut KERNEL_STATE, TicketMutex::new(pfa))
.expect("failed to create global kernel state");
}

Expand Down
3 changes: 1 addition & 2 deletions oro-arch-x86_64/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ oro-debug.workspace = true
oro-dbgutil.workspace = true
oro-acpi.workspace = true
oro-id.workspace = true

spin.workspace = true
oro-sync.workspace = true

[lints]
workspace = true
1 change: 1 addition & 0 deletions oro-arch-x86_64/src/boot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use oro_mem::{
mapper::AddressSpace,
phys::{Phys, PhysAddr},
};
use oro_sync::Lock;

use crate::mem::address_space::AddressSpaceLayout;

Expand Down
8 changes: 4 additions & 4 deletions oro-arch-x86_64/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use oro_mem::{
pfa::alloc::Alloc,
phys::{Phys, PhysAddr},
};
use spin::mutex::fair::FairMutex;
use oro_sync::{Lock, TicketMutex};

use crate::{
gdt::{Gdt, SysEntry},
Expand Down Expand Up @@ -52,7 +52,7 @@ pub unsafe fn initialize_primary(pfa: crate::Pfa) {

// SAFETY(qix-): We know what we're doing here.
#[expect(static_mut_refs)]
KernelState::init(&mut KERNEL_STATE, FairMutex::new(pfa))
KernelState::init(&mut KERNEL_STATE, TicketMutex::new(pfa))
.expect("failed to create global kernel state");

#[expect(static_mut_refs)]
Expand Down Expand Up @@ -107,7 +107,7 @@ pub unsafe fn initialize_primary(pfa: crate::Pfa) {
.expect("failed to create root ring module");

let entry_point = {
let module_lock = module_handle.try_lock().expect("failed to lock module");
let module_lock = module_handle.lock();

let mapper = module_lock.mapper();

Expand Down Expand Up @@ -149,7 +149,7 @@ pub unsafe fn initialize_primary(pfa: crate::Pfa) {
segment.target_size()
);

let mut pfa = state.pfa().try_lock().expect("failed to lock pfa");
let mut pfa = state.pfa().lock();

// NOTE(qix-): This will almost definitely be improved in the future.
// NOTE(qix-): At the very least, hugepages will change this.
Expand Down
1 change: 1 addition & 0 deletions oro-arch-x86_64/src/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use core::arch::{asm, naked_asm};

use oro_mem::mapper::AddressSegment;
use oro_sync::Lock;

use crate::{
isr_store_user_task_and_jmp,
Expand Down
5 changes: 5 additions & 0 deletions oro-arch-x86_64/src/lapic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ pub struct Lapic {
base: *mut u8,
}

// SAFETY: The pointer is valid across all cores and is thus sendable.
// SAFETY: We can guarantee that the register blocks are mapped into all
// SAFETY: cores and reside at the same location across each.
unsafe impl Send for Lapic {}

impl Lapic {
/// Creates a new LAPIC controller.
///
Expand Down
7 changes: 7 additions & 0 deletions oro-arch-x86_64/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ pub(crate) struct CoreState {
pub kernel_irq_stack: UnsafeCell<u64>,
}

// XXX(qix-): This is temporary. The core state is not currently used
// XXX(qix-): across core boundaries, so we can manually mark it as `Sync`.
// XXX(qix-): I want to fix up how the kernel guards this value so as to drop
// XXX(qix-): the `Sync` requirement altogether, but for now this is sufficient,
// XXX(qix-): if not a little fragile.
unsafe impl Sync for CoreState {}

/// x86_64-specific thread state.
pub(crate) struct ThreadState {
/// The thread's interrupt stack pointer.
Expand Down
3 changes: 2 additions & 1 deletion oro-debug-pl011/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ repository = "https://github.com/oro-os/kernel"
license = "MPL-2.0"

[dependencies]
oro-sync.workspace = true

volatile-register.workspace = true
spin.workspace = true

[lints]
workspace = true
4 changes: 2 additions & 2 deletions oro-debug-pl011/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ mod driver;

use core::fmt::{self, Write};

use spin::mutex::fair::FairMutex;
use oro_sync::{Lock, TicketMutex};

/// The shared serial port for the system.
// NOTE(qix-): This is a temporary solution until pre-boot module loading
// NOTE(qix-): is implemented.
static SERIAL: FairMutex<Option<driver::PL011>> = FairMutex::new(None);
static SERIAL: TicketMutex<Option<driver::PL011>> = TicketMutex::new(None);

/// Initializes the PL011.
pub fn init(offset: usize) {
Expand Down
3 changes: 2 additions & 1 deletion oro-debug-uart16550/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ repository = "https://github.com/oro-os/kernel"
license = "MPL-2.0"

[dependencies]
oro-sync.workspace = true

uart_16550.workspace = true
spin.workspace = true

[lints]
workspace = true
4 changes: 2 additions & 2 deletions oro-debug-uart16550/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use core::fmt::{self, Write};

use spin::mutex::fair::FairMutex;
use oro_sync::{Lock, TicketMutex};
use uart_16550::SerialPort;

/// The shared serial port for the system.
static SERIAL: FairMutex<SerialPort> = FairMutex::new(unsafe { SerialPort::new(0x3F8) });
static SERIAL: TicketMutex<SerialPort> = TicketMutex::new(unsafe { SerialPort::new(0x3F8) });

/// Initializes the UART.
pub fn init() {
Expand Down
3 changes: 1 addition & 2 deletions oro-kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ oro-mem.workspace = true
oro-macro.workspace = true
oro-id.workspace = true
oro-debug.workspace = true

spin.workspace = true
oro-sync.workspace = true

[lints]
workspace = true
20 changes: 10 additions & 10 deletions oro-kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use oro_mem::{
mapper::{AddressSegment, AddressSpace, MapError, UnmapError},
pfa::alloc::Alloc,
};
use spin::mutex::fair::FairMutex;
use oro_sync::{Lock, TicketMutex};

use self::{
registry::{Handle, List, ListRegistry, Registry},
Expand All @@ -54,7 +54,7 @@ pub struct Kernel<A: Arch> {
/// The kernel scheduler.
///
/// Guaranteed valid after a successful call to `initialize_for_core`.
scheduler: MaybeUninit<FairMutex<Scheduler<A>>>,
scheduler: MaybeUninit<TicketMutex<Scheduler<A>>>,
/// Cached mapper handle for the kernel.
mapper: SupervisorHandle<A>,
}
Expand Down Expand Up @@ -109,7 +109,7 @@ impl<A: Arch> Kernel<A> {

(*kernel_ptr)
.scheduler
.write(FairMutex::new(Scheduler::new(&*kernel_ptr)));
.write(TicketMutex::new(Scheduler::new(&*kernel_ptr)));

Ok(&*kernel_ptr)
}
Expand Down Expand Up @@ -163,7 +163,7 @@ impl<A: Arch> Kernel<A> {
/// interrupts are disabled; the spinlock is _not_ a critical
/// spinlock and thus does not disable interrupts.
#[must_use]
pub unsafe fn scheduler(&self) -> &FairMutex<Scheduler<A>> {
pub unsafe fn scheduler(&self) -> &TicketMutex<Scheduler<A>> {
self.scheduler.assume_init_ref()
}
}
Expand All @@ -173,7 +173,7 @@ impl<A: Arch> Kernel<A> {
pub struct KernelState<A: Arch> {
/// The shared, spinlocked page frame allocator (PFA) for the
/// entire system.
pfa: FairMutex<A::Pfa>,
pfa: TicketMutex<A::Pfa>,

/// The base userspace address space mapper.
///
Expand Down Expand Up @@ -250,7 +250,7 @@ impl<A: Arch> KernelState<A> {
#[allow(clippy::missing_panics_doc)]
pub unsafe fn init(
this: &'static mut MaybeUninit<Self>,
pfa: FairMutex<A::Pfa>,
pfa: TicketMutex<A::Pfa>,
) -> Result<(), MapError> {
#[expect(clippy::missing_docs_in_private_items)]
macro_rules! init_registries {
Expand Down Expand Up @@ -348,7 +348,7 @@ impl<A: Arch> KernelState<A> {
}

/// Returns the underlying PFA belonging to the kernel state.
pub fn pfa(&'static self) -> &'static FairMutex<A::Pfa> {
pub fn pfa(&'static self) -> &'static TicketMutex<A::Pfa> {
&self.pfa
}

Expand Down Expand Up @@ -614,14 +614,14 @@ impl<A: Arch> KernelState<A> {
pub trait Arch: 'static {
/// The type of page frame allocator (PFA) the architecture
/// uses.
type Pfa: Alloc;
type Pfa: Alloc + Send;
/// The address space layout the architecture uses.
type AddrSpace: AddressSpace;
/// Architecture-specific thread state to be stored alongside
/// each thread.
type ThreadState: Sized = ();
type ThreadState: Sized + Send = ();
/// The core-local state type.
type CoreState: Sized + 'static = ();
type CoreState: Sized + Send + Sync + 'static = ();

/// Allows the architecture to further initialize an instance
/// thread's mappings when threads are created.
Expand Down
Loading

0 comments on commit f617965

Please sign in to comment.