Skip to content

Commit

Permalink
Merge #589
Browse files Browse the repository at this point in the history
589: Migrate to hermit-sync r=stlankes a=mkroening



Co-authored-by: Martin Kröning <mkroening@posteo.net>
  • Loading branch information
bors[bot] and mkroening authored Nov 24, 2022
2 parents fdad7df + 03e4bd1 commit c8d6708
Show file tree
Hide file tree
Showing 34 changed files with 169 additions and 316 deletions.
43 changes: 40 additions & 3 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,12 @@ bitflags = "1.3"
crossbeam-utils = { version = "0.8", default-features = false }
hashbrown = { version = "0.13", default-features = false }
hermit-entry = { version = "0.9", features = ["kernel"] }
hermit-sync = "0.1.0"
include-transformed = { version = "0.2", optional = true }
lock_api = "0.4"
log = { version = "0.4", default-features = false }
num = { version = "0.4", default-features = false }
num-derive = { version = "0.3", optional = true }
num-traits = { version = "0.2", default-features = false }
once_cell = { version = "1.16", default-features = false, features = ["alloc"] }
pci-ids = { version = "0.2", optional = true }
scopeguard = { version = "1.1", default-features = false }
shell-words = { version = "1.1", default-features = false }
Expand Down
37 changes: 0 additions & 37 deletions src/arch/aarch64/kernel/irq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,43 +43,6 @@ pub fn disable() {
}
}

/// Disable IRQs (nested)
///
/// Disable IRQs when unsure if IRQs were enabled at all.
/// This function together with nested_enable can be used
/// in situations when interrupts shouldn't be activated if they
/// were not activated before calling this function.
#[inline]
pub fn nested_disable() -> bool {
let flags: usize;
unsafe {
asm!(
"mrs {}, daif",
out(reg) flags,
options(nostack, nomem),
);
}

let mut was_enabled = true;
if flags & (IRQ_FLAG_A | IRQ_FLAG_I | IRQ_FLAG_F) > 0 {
was_enabled = false;
}

disable();
was_enabled
}

/// Enable IRQs (nested)
///
/// Can be used in conjunction with nested_disable() to only enable
/// interrupts again if they were enabled before.
#[inline]
pub fn nested_enable(was_enabled: bool) {
if was_enabled {
enable();
}
}

#[no_mangle]
pub extern "C" fn irq_install_handler(irq_number: u32, handler: usize) {
info!("Install handler for interrupt {}", irq_number);
Expand Down
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
8 changes: 4 additions & 4 deletions src/arch/x86_64/kernel/apic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use core::{cmp, fmt, mem, u32};
use arch::x86_64::kernel::percore::*;
use arch::x86_64::kernel::{idt, irq, processor};
use crossbeam_utils::CachePadded;
use hermit_sync::without_interrupts;
#[cfg(feature = "smp")]
use x86::controlregs::*;
use x86::msr::*;
Expand All @@ -23,7 +24,6 @@ use crate::arch::x86_64::mm::paging::{
BasePageSize, PageSize, PageTableEntryFlags, PageTableEntryFlagsExt,
};
use crate::arch::x86_64::mm::{paging, virtualmem, PhysAddr, VirtAddr};
use crate::collections::irqsave;
use crate::config::*;
use crate::scheduler::CoreId;
use crate::{arch, env, mm, scheduler};
Expand Down Expand Up @@ -719,7 +719,7 @@ fn __set_oneshot_timer(wakeup_time: Option<u64>) {
}

pub fn set_oneshot_timer(wakeup_time: Option<u64>) {
irqsave(|| {
without_interrupts(|| {
__set_oneshot_timer(wakeup_time);
});
}
Expand Down Expand Up @@ -889,7 +889,7 @@ pub fn ipi_tlb_flush() {
}

// Send an IPI with our TLB Flush interrupt number to all other CPUs.
irqsave(|| {
without_interrupts(|| {
for (core_id_to_interrupt, &apic_id) in apic_ids.iter().enumerate() {
if core_id_to_interrupt != core_id.try_into().unwrap() {
let destination = u64::from(apic_id) << 32;
Expand All @@ -910,7 +910,7 @@ pub fn ipi_tlb_flush() {
pub fn wakeup_core(core_id_to_wakeup: CoreId) {
#[cfg(feature = "smp")]
if core_id_to_wakeup != core_id() {
irqsave(|| {
without_interrupts(|| {
let apic_ids = unsafe { CPU_LOCAL_APIC_IDS.as_ref().unwrap() };
let local_apic_id = apic_ids[core_id_to_wakeup as usize];
let destination = u64::from(local_apic_id) << 32;
Expand Down
34 changes: 3 additions & 31 deletions src/arch/x86_64/kernel/irq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use alloc::string::{String, ToString};
use core::arch::asm;
use core::fmt;

use x86::bits64::rflags::{self, RFlags};
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 Expand Up @@ -81,34 +81,6 @@ pub fn disable() {
}
}

/// Disable IRQs (nested)
///
/// Disable IRQs when unsure if IRQs were enabled at all.
/// This function together with nested_enable can be used
/// in situations when interrupts shouldn't be activated if they
/// were not activated before calling this function.
#[inline]
pub fn nested_disable() -> bool {
cfg!(target_os = "none") && {
let enabled = rflags::read().contains(RFlags::FLAGS_IF);
if enabled {
disable();
}
enabled
}
}

/// Enable IRQs (nested)
///
/// Can be used in conjunction with nested_disable() to only enable
/// interrupts again if they were enabled before.
#[inline]
pub fn nested_enable(was_enabled: bool) {
if was_enabled {
enable();
}
}

pub fn install() {
// Set gates to the Interrupt Service Routines (ISRs) for all 32 CPU exceptions.
// All of them use a dedicated stack per task (IST1) to prevent clobbering the current task stack.
Expand Down
14 changes: 7 additions & 7 deletions src/arch/x86_64/kernel/mmio.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use alloc::vec::Vec;
use core::str;

use hermit_sync::{without_interrupts, InterruptTicketMutex};

use crate::arch::x86_64::mm::paging::{
BasePageSize, PageSize, PageTableEntryFlags, PageTableEntryFlagsExt,
};
use crate::arch::x86_64::mm::{paging, PhysAddr};
use crate::collections::irqsave;
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 @@ -21,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 @@ -121,20 +121,20 @@ 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()) }
}

pub fn init_drivers() {
// virtio: MMIO Device Discovery
irqsave(|| {
without_interrupts(|| {
if let Ok(mmio) = detect_network() {
warn!(
"Found MMIO device, but we guess the interrupt number {}!",
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
Loading

0 comments on commit c8d6708

Please sign in to comment.