Skip to content

Commit

Permalink
Replace sync::spinlock with direct use of hermit-sync
Browse files Browse the repository at this point in the history
  • Loading branch information
mkroening committed Nov 23, 2022
1 parent dadde60 commit 03e4bd1
Show file tree
Hide file tree
Showing 26 changed files with 98 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/arch/aarch64/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use core::arch::{asm, global_asm};
use core::ptr;

use hermit_entry::boot_info::{BootInfo, PlatformInfo, RawBootInfo};
use hermit_sync::TicketMutex;

use crate::arch::aarch64::kernel::percore::*;
use crate::arch::aarch64::kernel::serial::SerialPort;
Expand All @@ -21,12 +22,11 @@ pub use crate::arch::aarch64::kernel::systemtime::get_boot_time;
use crate::arch::aarch64::mm::{PhysAddr, VirtAddr};
use crate::config::*;
use crate::env;
use crate::synch::spinlock::Spinlock;

const SERIAL_PORT_BAUDRATE: u32 = 115200;

static mut COM1: SerialPort = SerialPort::new(0x800);
static CPU_ONLINE: Spinlock<u32> = Spinlock::new(0);
static CPU_ONLINE: TicketMutex<u32> = TicketMutex::new(0);

global_asm!(include_str!("start.s"));

Expand Down
4 changes: 2 additions & 2 deletions src/arch/aarch64/kernel/pci.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::rc::Rc;
use core::cell::RefCell;

use crate::synch::spinlock::SpinlockIrqSave;
use hermit_sync::InterruptTicketMutex;

// Currently, onbly a dummy implementation
pub struct VirtioNetDriver;
Expand Down Expand Up @@ -40,6 +40,6 @@ impl VirtioNetDriver {
pub fn rx_buffer_consumed(&mut self) {}
}

pub fn get_network_driver() -> Option<&'static SpinlockIrqSave<VirtioNetDriver>> {
pub fn get_network_driver() -> Option<&'static InterruptTicketMutex<VirtioNetDriver>> {
None
}
6 changes: 4 additions & 2 deletions src/arch/aarch64/mm/physicalmem.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use core::alloc::AllocError;
use core::sync::atomic::{AtomicUsize, Ordering};

use hermit_sync::InterruptTicketMutex;

use crate::arch::aarch64::kernel::{get_boot_info_address, get_limit, get_ram_address};
use crate::arch::aarch64::mm::paging::{BasePageSize, PageSize};
use crate::arch::aarch64::mm::{PhysAddr, VirtAddr};
use crate::env::is_uhyve;
use crate::mm;
use crate::mm::freelist::{FreeList, FreeListEntry};
use crate::synch::spinlock::SpinlockIrqSave;

static PHYSICAL_FREE_LIST: SpinlockIrqSave<FreeList> = SpinlockIrqSave::new(FreeList::new());
static PHYSICAL_FREE_LIST: InterruptTicketMutex<FreeList> =
InterruptTicketMutex::new(FreeList::new());
static TOTAL_MEMORY: AtomicUsize = AtomicUsize::new(0);

fn detect_from_limits() -> Result<(), ()> {
Expand Down
6 changes: 4 additions & 2 deletions src/arch/aarch64/mm/virtualmem.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use core::alloc::AllocError;

use hermit_sync::InterruptTicketMutex;

use crate::arch::aarch64::kernel::get_ram_address;
use crate::arch::aarch64::mm::paging::{BasePageSize, PageSize};
use crate::arch::aarch64::mm::{PhysAddr, VirtAddr};
use crate::mm;
use crate::mm::freelist::{FreeList, FreeListEntry};
use crate::synch::spinlock::SpinlockIrqSave;

static KERNEL_FREE_LIST: SpinlockIrqSave<FreeList> = SpinlockIrqSave::new(FreeList::new());
static KERNEL_FREE_LIST: InterruptTicketMutex<FreeList> =
InterruptTicketMutex::new(FreeList::new());

/// End of the virtual memory address space reserved for kernel memory (4 GiB).
/// This also marks the start of the virtual memory address space reserved for the task heap.
Expand Down
5 changes: 3 additions & 2 deletions src/arch/x86_64/kernel/irq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ use alloc::string::{String, ToString};
use core::arch::asm;
use core::fmt;

use hermit_sync::InterruptTicketMutex;
use x86::irq::PageFaultError;
use x86::{controlregs, irq};

use crate::arch::x86_64::kernel::percore::*;
use crate::arch::x86_64::kernel::{apic, idt, processor};
use crate::scheduler;
use crate::synch::spinlock::SpinlockIrqSave;

static IRQ_NAMES: SpinlockIrqSave<BTreeMap<u32, String>> = SpinlockIrqSave::new(BTreeMap::new());
static IRQ_NAMES: InterruptTicketMutex<BTreeMap<u32, String>> =
InterruptTicketMutex::new(BTreeMap::new());

// Derived from Philipp Oppermann's blog
// => https://github.com/phil-opp/blog_os/blob/master/src/interrupts/mod.rs
Expand Down
11 changes: 5 additions & 6 deletions src/arch/x86_64/kernel/mmio.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::vec::Vec;
use core::str;

use hermit_sync::without_interrupts;
use hermit_sync::{without_interrupts, InterruptTicketMutex};

use crate::arch::x86_64::mm::paging::{
BasePageSize, PageSize, PageTableEntryFlags, PageTableEntryFlagsExt,
Expand All @@ -11,7 +11,6 @@ use crate::drivers::net::virtio_net::VirtioNetDriver;
use crate::drivers::net::NetworkInterface;
use crate::drivers::virtio::transport::mmio as mmio_virtio;
use crate::drivers::virtio::transport::mmio::{DevId, MmioRegisterLayout, VirtioDriver};
use crate::synch::spinlock::SpinlockIrqSave;

pub const MAGIC_VALUE: u32 = 0x74726976;

Expand All @@ -22,12 +21,12 @@ const IRQ_NUMBER: u32 = 12;
static mut MMIO_DRIVERS: Vec<MmioDriver> = Vec::new();

pub enum MmioDriver {
VirtioNet(SpinlockIrqSave<VirtioNetDriver>),
VirtioNet(InterruptTicketMutex<VirtioNetDriver>),
}

impl MmioDriver {
#[allow(unreachable_patterns)]
fn get_network_driver(&self) -> Option<&SpinlockIrqSave<dyn NetworkInterface>> {
fn get_network_driver(&self) -> Option<&InterruptTicketMutex<dyn NetworkInterface>> {
match self {
Self::VirtioNet(drv) => Some(drv),
_ => None,
Expand Down Expand Up @@ -122,7 +121,7 @@ pub fn register_driver(drv: MmioDriver) {
}
}

pub fn get_network_driver() -> Option<&'static SpinlockIrqSave<dyn NetworkInterface>> {
pub fn get_network_driver() -> Option<&'static InterruptTicketMutex<dyn NetworkInterface>> {
unsafe { MMIO_DRIVERS.iter().find_map(|drv| drv.get_network_driver()) }
}

Expand All @@ -135,7 +134,7 @@ pub fn init_drivers() {
IRQ_NUMBER
);
if let Ok(VirtioDriver::Network(drv)) = mmio_virtio::init_device(mmio, IRQ_NUMBER) {
register_driver(MmioDriver::VirtioNet(SpinlockIrqSave::new(drv)))
register_driver(MmioDriver::VirtioNet(InterruptTicketMutex::new(drv)))
}
} else {
warn!("Unable to find mmio device");
Expand Down
21 changes: 10 additions & 11 deletions src/arch/x86_64/kernel/pci.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::vec::Vec;
use core::{fmt, u32, u8};

use hermit_sync::without_interrupts;
use hermit_sync::{without_interrupts, InterruptTicketMutex};
use num_derive::{FromPrimitive, ToPrimitive};
use x86::io::*;

Expand All @@ -12,7 +12,6 @@ use crate::drivers::net::NetworkInterface;
use crate::drivers::virtio::depr::virtio_fs::VirtioFsDriver;
use crate::drivers::virtio::transport::pci as pci_virtio;
use crate::drivers::virtio::transport::pci::VirtioDriver;
use crate::synch::spinlock::SpinlockIrqSave;

// TODO: should these be pub? currently needed since used in virtio.rs maybe use getter methods to be more flexible.
pub const PCI_MAX_BUS_NUMBER: u8 = 32;
Expand Down Expand Up @@ -120,21 +119,21 @@ pub struct MemoryBar {

#[allow(clippy::large_enum_variant)]
pub enum PciDriver<'a> {
VirtioFs(SpinlockIrqSave<VirtioFsDriver<'a>>),
VirtioNet(SpinlockIrqSave<VirtioNetDriver>),
RTL8139Net(SpinlockIrqSave<RTL8139Driver>),
VirtioFs(InterruptTicketMutex<VirtioFsDriver<'a>>),
VirtioNet(InterruptTicketMutex<VirtioNetDriver>),
RTL8139Net(InterruptTicketMutex<RTL8139Driver>),
}

impl<'a> PciDriver<'a> {
fn get_network_driver(&self) -> Option<&SpinlockIrqSave<dyn NetworkInterface>> {
fn get_network_driver(&self) -> Option<&InterruptTicketMutex<dyn NetworkInterface>> {
match self {
Self::VirtioNet(drv) => Some(drv),
Self::RTL8139Net(drv) => Some(drv),
_ => None,
}
}

fn get_filesystem_driver(&self) -> Option<&SpinlockIrqSave<VirtioFsDriver<'a>>> {
fn get_filesystem_driver(&self) -> Option<&InterruptTicketMutex<VirtioFsDriver<'a>>> {
match self {
Self::VirtioFs(drv) => Some(drv),
_ => None,
Expand All @@ -148,11 +147,11 @@ pub fn register_driver(drv: PciDriver<'static>) {
}
}

pub fn get_network_driver() -> Option<&'static SpinlockIrqSave<dyn NetworkInterface>> {
pub fn get_network_driver() -> Option<&'static InterruptTicketMutex<dyn NetworkInterface>> {
unsafe { PCI_DRIVERS.iter().find_map(|drv| drv.get_network_driver()) }
}

pub fn get_filesystem_driver() -> Option<&'static SpinlockIrqSave<VirtioFsDriver<'static>>> {
pub fn get_filesystem_driver() -> Option<&'static InterruptTicketMutex<VirtioFsDriver<'static>>> {
unsafe {
PCI_DRIVERS
.iter()
Expand Down Expand Up @@ -490,7 +489,7 @@ pub fn init_drivers() {

if let Ok(VirtioDriver::Network(drv)) = pci_virtio::init_device(adapter) {
nic_available = true;
register_driver(PciDriver::VirtioNet(SpinlockIrqSave::new(drv)))
register_driver(PciDriver::VirtioNet(InterruptTicketMutex::new(drv)))
}
}

Expand All @@ -508,7 +507,7 @@ pub fn init_drivers() {
);

if let Ok(drv) = rtl8139::init_device(adapter) {
register_driver(PciDriver::RTL8139Net(SpinlockIrqSave::new(drv)))
register_driver(PciDriver::RTL8139Net(InterruptTicketMutex::new(drv)))
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/arch/x86_64/mm/physicalmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ use core::alloc::AllocError;
use core::sync::atomic::{AtomicUsize, Ordering};

use ::x86_64::structures::paging::{FrameAllocator, PhysFrame};
use hermit_sync::InterruptTicketMutex;
use multiboot::information::{MemoryType, Multiboot};

use crate::arch::x86_64::kernel::{get_limit, get_mbinfo};
use crate::arch::x86_64::mm::paging::{BasePageSize, PageSize};
use crate::arch::x86_64::mm::{PhysAddr, VirtAddr, MEM};
use crate::mm;
use crate::mm::freelist::{FreeList, FreeListEntry};
use crate::synch::spinlock::*;

static PHYSICAL_FREE_LIST: SpinlockIrqSave<FreeList> = SpinlockIrqSave::new(FreeList::new());
static PHYSICAL_FREE_LIST: InterruptTicketMutex<FreeList> =
InterruptTicketMutex::new(FreeList::new());
static TOTAL_MEMORY: AtomicUsize = AtomicUsize::new(0);

fn detect_from_multiboot_info() -> Result<(), ()> {
Expand Down
6 changes: 4 additions & 2 deletions src/arch/x86_64/mm/virtualmem.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use core::alloc::AllocError;

use hermit_sync::InterruptTicketMutex;

use crate::arch::x86_64::mm::paging::{BasePageSize, PageSize};
use crate::arch::x86_64::mm::VirtAddr;
use crate::mm;
use crate::mm::freelist::{FreeList, FreeListEntry};
use crate::synch::spinlock::*;

static KERNEL_FREE_LIST: SpinlockIrqSave<FreeList> = SpinlockIrqSave::new(FreeList::new());
static KERNEL_FREE_LIST: InterruptTicketMutex<FreeList> =
InterruptTicketMutex::new(FreeList::new());

pub fn init() {
let entry = FreeListEntry::new(
Expand Down
5 changes: 3 additions & 2 deletions src/console.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use core::fmt;

use hermit_sync::InterruptTicketMutex;

use crate::arch;
use crate::synch::spinlock::SpinlockIrqSave;

pub struct Console(());

Expand All @@ -27,7 +28,7 @@ impl Console {
}
}

pub static CONSOLE: SpinlockIrqSave<Console> = SpinlockIrqSave::new(Console(()));
pub static CONSOLE: InterruptTicketMutex<Console> = InterruptTicketMutex::new(Console(()));

#[cfg(not(target_os = "none"))]
#[test]
Expand Down
5 changes: 3 additions & 2 deletions src/drivers/virtio/transport/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use alloc::vec::Vec;
use core::mem;
use core::result::Result;

use hermit_sync::InterruptTicketMutex;

use crate::arch::kernel::pci as kernel_pci;
use crate::arch::kernel::pci::error::PciError;
use crate::arch::kernel::pci::{PciAdapter, PciDriver};
Expand All @@ -19,7 +21,6 @@ use crate::drivers::virtio::depr::virtio_fs;
use crate::drivers::virtio::env::memory::{MemLen, MemOff, VirtMemAddr};
use crate::drivers::virtio::error::VirtioError;
use crate::drivers::virtio::{device, env};
use crate::synch::spinlock::SpinlockIrqSave;

/// Virtio device ID's
/// See Virtio specification v1.1. - 5
Expand Down Expand Up @@ -1258,7 +1259,7 @@ pub fn init_device(adapter: &PciAdapter) -> Result<VirtioDriver, DriverError> {
// TODO: proper error handling on driver creation fail
match virtio_fs::create_virtiofs_driver(adapter) {
Some(virt_fs_drv) => {
kernel_pci::register_driver(PciDriver::VirtioFs(SpinlockIrqSave::new(
kernel_pci::register_driver(PciDriver::VirtioFs(InterruptTicketMutex::new(
virt_fs_drv,
)));
// initialize file system
Expand Down
13 changes: 7 additions & 6 deletions src/mm/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ use core::ops::Deref;
use core::ptr::NonNull;
use core::{cmp, mem, ptr};

use hermit_sync::InterruptTicketMutex;

use crate::mm::hole::{Hole, HoleList};
use crate::mm::kernel_end_address;
use crate::synch::spinlock::*;
use crate::HW_DESTRUCTIVE_INTERFERENCE_SIZE;

/// Size of the preallocated space for the Bootstrap Allocator.
Expand Down Expand Up @@ -175,20 +176,20 @@ impl Heap {
}
}

pub struct LockedHeap(SpinlockIrqSave<Heap>);
pub struct LockedHeap(InterruptTicketMutex<Heap>);

impl LockedHeap {
/// Creates an empty heap. All allocate calls will return `None`.
pub const fn empty() -> LockedHeap {
LockedHeap(SpinlockIrqSave::new(Heap::empty()))
LockedHeap(InterruptTicketMutex::new(Heap::empty()))
}

/// Creates a new heap with the given `bottom` and `size`. The bottom address must be valid
/// and the memory in the `[heap_bottom, heap_bottom + heap_size)` range must not be used for
/// anything else. This function is unsafe because it can cause undefined behavior if the
/// given address is invalid.
pub unsafe fn new(heap_bottom: usize, heap_size: usize) -> LockedHeap {
LockedHeap(SpinlockIrqSave::new(Heap {
LockedHeap(InterruptTicketMutex::new(Heap {
first_block: [0xCC; BOOTSTRAP_HEAP_SIZE],
index: 0,
bottom: heap_bottom,
Expand All @@ -199,9 +200,9 @@ impl LockedHeap {
}

impl Deref for LockedHeap {
type Target = SpinlockIrqSave<Heap>;
type Target = InterruptTicketMutex<Heap>;

fn deref(&self) -> &SpinlockIrqSave<Heap> {
fn deref(&self) -> &InterruptTicketMutex<Heap> {
&self.0
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/net/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use core::task::{Context, Poll};

use async_task::{Runnable, Task};
use futures_lite::pin;
use hermit_sync::{InterruptTicketMutex, TicketMutex};
use smoltcp::time::{Duration, Instant};

use crate::core_scheduler;
use crate::scheduler::task::TaskHandle;
use crate::synch::spinlock::{Spinlock, SpinlockIrqSave};

static QUEUE: Spinlock<Vec<Runnable>> = Spinlock::new(Vec::new());
static QUEUE: TicketMutex<Vec<Runnable>> = TicketMutex::new(Vec::new());

#[inline]
fn network_delay(timestamp: Instant) -> Option<Duration> {
Expand Down Expand Up @@ -150,7 +150,7 @@ where

/// set driver in polling mode and threads will not be blocked
fn set_polling_mode(value: bool) {
static IN_POLLING_MODE: SpinlockIrqSave<usize> = SpinlockIrqSave::new(0);
static IN_POLLING_MODE: InterruptTicketMutex<usize> = InterruptTicketMutex::new(0);

let mut guard = IN_POLLING_MODE.lock();

Expand Down
Loading

0 comments on commit 03e4bd1

Please sign in to comment.