Skip to content

Commit

Permalink
Merge pull request #1421 from hermit-os/static_mut_refs
Browse files Browse the repository at this point in the history
fix: `static_mut_refs`
  • Loading branch information
mkroening authored Oct 22, 2024
2 parents 4a32dde + cd4e02e commit 9f90219
Show file tree
Hide file tree
Showing 18 changed files with 151 additions and 85 deletions.
8 changes: 3 additions & 5 deletions src/arch/aarch64/kernel/interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ahash::RandomState;
use arm_gic::gicv3::{GicV3, IntId, Trigger};
use hashbrown::HashMap;
use hermit_dtb::Dtb;
use hermit_sync::{InterruptSpinMutex, InterruptTicketMutex, OnceCell};
use hermit_sync::{InterruptSpinMutex, InterruptTicketMutex, OnceCell, SpinMutex};

use crate::arch::aarch64::kernel::core_local::increment_irq_counter;
use crate::arch::aarch64::kernel::scheduler::State;
Expand Down Expand Up @@ -37,7 +37,7 @@ static mut TIMER_INTERRUPT: u32 = 0;
static INTERRUPT_HANDLERS: OnceCell<HashMap<u8, InterruptHandlerQueue, RandomState>> =
OnceCell::new();
/// Driver for the Arm Generic Interrupt Controller version 3 (or 4).
pub(crate) static mut GIC: OnceCell<GicV3> = OnceCell::new();
pub(crate) static GIC: SpinMutex<Option<GicV3>> = SpinMutex::new(None);

/// Enable all interrupts
#[inline]
Expand Down Expand Up @@ -337,9 +337,7 @@ pub(crate) fn init() {
gic.enable_interrupt(reschedid, true);
IRQ_NAMES.lock().insert(SGI_RESCHED, "Reschedule");

unsafe {
GIC.set(gic).unwrap();
}
*GIC.lock() = Some(gic);
}

static IRQ_NAMES: InterruptTicketMutex<HashMap<u8, &'static str, RandomState>> =
Expand Down
30 changes: 14 additions & 16 deletions src/arch/aarch64/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ use core::arch::global_asm;
use core::str;
use core::sync::atomic::{AtomicU32, AtomicU64, Ordering};

use hermit_sync::SpinMutex;

use crate::arch::aarch64::kernel::core_local::*;
use crate::arch::aarch64::kernel::serial::SerialPort;
use crate::arch::aarch64::mm::{PhysAddr, VirtAddr};
use crate::env;

const SERIAL_PORT_BAUDRATE: u32 = 115200;

static mut COM1: SerialPort = SerialPort::new(0x800);
static COM1: SpinMutex<SerialPort> = SpinMutex::new(SerialPort::new(0x800));

/// `CPU_ONLINE` is the count of CPUs that finished initialization.
///
Expand Down Expand Up @@ -79,28 +81,24 @@ pub fn args() -> Option<&'static str> {
pub fn message_output_init() {
CoreLocal::install();

unsafe {
COM1.port_address = env::boot_info()
.hardware_info
.serial_port_base
.map(|uartport| uartport.get())
.unwrap_or_default()
.try_into()
.unwrap();
}
let mut com1 = COM1.lock();

com1.port_address = env::boot_info()
.hardware_info
.serial_port_base
.map(|uartport| uartport.get())
.unwrap_or_default()
.try_into()
.unwrap();

// We can only initialize the serial port here, because VGA requires processor
// configuration first.
unsafe {
COM1.init(SERIAL_PORT_BAUDRATE);
}
com1.init(SERIAL_PORT_BAUDRATE);
}

pub fn output_message_byte(byte: u8) {
// Output messages to the serial port.
unsafe {
COM1.write_byte(byte);
}
COM1.lock().write_byte(byte);
}

pub fn output_message_buf(buf: &[u8]) {
Expand Down
7 changes: 3 additions & 4 deletions src/arch/aarch64/kernel/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ fn detect_interrupt(
if irq_type == 0 {
// enable interrupt
let irq_id = IntId::spi(irq_number);
let gic = unsafe { GIC.get_mut().unwrap() };
let mut gic = GIC.lock();
let gic = gic.as_mut().unwrap();
gic.set_interrupt_priority(irq_id, 0x10);
if irq_flags == 4 {
gic.set_trigger(irq_id, Trigger::Level);
Expand Down Expand Up @@ -339,9 +340,7 @@ pub fn init() {
dev.set_irq(pin, line);
}

unsafe {
PCI_DEVICES.push(dev);
}
PCI_DEVICES.with(|pci_devices| pci_devices.unwrap().push(dev));
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/arch/riscv64/kernel/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use hermit_sync::InterruptSpinMutex;
use crate::drivers::net::gem::GEMDriver;
#[cfg(not(feature = "gem-net"))]
use crate::drivers::net::virtio::VirtioNetDriver;
use crate::init_cell::InitCell;

static mut MMIO_DRIVERS: Vec<MmioDriver> = Vec::new();
static MMIO_DRIVERS: InitCell<Vec<MmioDriver>> = InitCell::new(Vec::new());

pub(crate) enum MmioDriver {
#[cfg(feature = "gem-net")]
Expand All @@ -32,17 +33,21 @@ impl MmioDriver {
}
}
pub(crate) fn register_driver(drv: MmioDriver) {
unsafe {
MMIO_DRIVERS.push(drv);
}
MMIO_DRIVERS.with(|mmio_drivers| mmio_drivers.unwrap().push(drv));
}

#[cfg(feature = "gem-net")]
pub(crate) fn get_network_driver() -> Option<&'static InterruptSpinMutex<GEMDriver>> {
unsafe { MMIO_DRIVERS.iter().find_map(|drv| drv.get_network_driver()) }
MMIO_DRIVERS
.get()?
.iter()
.find_map(|drv| drv.get_network_driver())
}

#[cfg(not(feature = "gem-net"))]
pub(crate) fn get_network_driver() -> Option<&'static InterruptSpinMutex<VirtioNetDriver>> {
unsafe { MMIO_DRIVERS.iter().find_map(|drv| drv.get_network_driver()) }
MMIO_DRIVERS
.get()?
.iter()
.find_map(|drv| drv.get_network_driver())
}
14 changes: 5 additions & 9 deletions src/arch/riscv64/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ use crate::arch::riscv64::kernel::processor::lsb;
use crate::arch::riscv64::mm::{physicalmem, PhysAddr, VirtAddr};
use crate::config::KERNEL_STACK_SIZE;
use crate::env;
use crate::init_cell::InitCell;

// Used to store information about available harts. The index of the hart in the vector
// represents its CpuId and does not need to match its hart_id
pub(crate) static mut HARTS_AVAILABLE: Vec<usize> = Vec::new();
pub(crate) static HARTS_AVAILABLE: InitCell<Vec<usize>> = InitCell::new(Vec::new());

/// Kernel header to announce machine features
static CPU_ONLINE: AtomicU32 = AtomicU32::new(0);
Expand Down Expand Up @@ -148,14 +149,9 @@ fn finish_processor_init() {

let current_hart_id = get_current_boot_id() as usize;

unsafe {
// Add hart to HARTS_AVAILABLE, the hart id is stored in current_boot_id
HARTS_AVAILABLE.push(current_hart_id);
info!(
"Initialized CPU with hart_id {}",
HARTS_AVAILABLE[core_local::core_id() as usize]
);
}
// Add hart to HARTS_AVAILABLE, the hart id is stored in current_boot_id
HARTS_AVAILABLE.with(|harts_available| harts_available.unwrap().push(current_hart_id));
info!("Initialized CPU with hart_id {current_hart_id}");

crate::scheduler::add_current_core();

Expand Down
2 changes: 1 addition & 1 deletion src/arch/riscv64/kernel/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ pub fn set_oneshot_timer(wakeup_time: Option<u64>) {
}

pub fn wakeup_core(core_to_wakeup: CoreId) {
let hart_id = unsafe { HARTS_AVAILABLE[core_to_wakeup as usize] };
let hart_id = HARTS_AVAILABLE.finalize()[core_to_wakeup as usize];
debug!("Wakeup core: {} , hart_id: {}", core_to_wakeup, hart_id);
sbi_rt::send_ipi(sbi_rt::HartMask::from_mask_base(0b1, hart_id));
}
14 changes: 9 additions & 5 deletions src/arch/x86_64/kernel/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ use crate::drivers::net::virtio::VirtioNetDriver;
use crate::drivers::virtio::transport::mmio as mmio_virtio;
use crate::drivers::virtio::transport::mmio::VirtioDriver;
use crate::env;
use crate::init_cell::InitCell;

pub const MAGIC_VALUE: u32 = 0x74726976;

pub const MMIO_START: usize = 0x00000000feb00000;
pub const MMIO_END: usize = 0x00000000feb0ffff;
const IRQ_NUMBER: u8 = 44 - 32;

static mut MMIO_DRIVERS: Vec<MmioDriver> = Vec::new();
static MMIO_DRIVERS: InitCell<Vec<MmioDriver>> = InitCell::new(Vec::new());

pub(crate) enum MmioDriver {
VirtioNet(InterruptTicketMutex<VirtioNetDriver>),
Expand Down Expand Up @@ -189,13 +190,14 @@ fn detect_network() -> Result<(VolatileRef<'static, DeviceRegisters>, u8), &'sta
}

pub(crate) fn register_driver(drv: MmioDriver) {
unsafe {
MMIO_DRIVERS.push(drv);
}
MMIO_DRIVERS.with(|mmio_drivers| mmio_drivers.unwrap().push(drv));
}

pub(crate) fn get_network_driver() -> Option<&'static InterruptTicketMutex<VirtioNetDriver>> {
unsafe { MMIO_DRIVERS.iter().find_map(|drv| drv.get_network_driver()) }
MMIO_DRIVERS
.get()?
.iter()
.find_map(|drv| drv.get_network_driver())
}

pub(crate) fn init_drivers() {
Expand All @@ -215,5 +217,7 @@ pub(crate) fn init_drivers() {
} else {
warn!("Unable to find mmio device");
}

MMIO_DRIVERS.finalize();
});
}
5 changes: 2 additions & 3 deletions src/arch/x86_64/kernel/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ pub(crate) fn init() {

let (device_id, vendor_id) = header.id(pci_config);
if device_id != u16::MAX && vendor_id != u16::MAX {
unsafe {
PCI_DEVICES.push(PciDevice::new(pci_address, pci_config));
}
let device = PciDevice::new(pci_address, pci_config);
PCI_DEVICES.with(|pci_devices| pci_devices.unwrap().push(device));
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/arch/x86_64/kernel/vga.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use hermit_sync::SpinMutex;
use x86::io::*;

use crate::arch::x86_64::mm::paging::{BasePageSize, PageTableEntryFlags, PageTableEntryFlagsExt};
Expand All @@ -14,7 +15,7 @@ const COLS: usize = 80;
const ROWS: usize = 25;
const VGA_BUFFER_ADDRESS: u64 = 0xB8000;

static mut VGA_SCREEN: VgaScreen = VgaScreen::new();
static VGA_SCREEN: SpinMutex<VgaScreen> = SpinMutex::new(VgaScreen::new());

#[derive(Clone, Copy)]
#[repr(C, packed)]
Expand All @@ -39,6 +40,9 @@ struct VgaScreen {
is_initialized: bool,
}

// FIXME: make `buffer` implement `Send` instead
unsafe impl Send for VgaScreen {}

impl VgaScreen {
const fn new() -> Self {
Self {
Expand Down Expand Up @@ -124,11 +128,9 @@ impl VgaScreen {
}

pub fn init() {
unsafe { VGA_SCREEN.init() };
VGA_SCREEN.lock().init();
}

pub fn write_byte(byte: u8) {
unsafe {
VGA_SCREEN.write_byte(byte);
}
VGA_SCREEN.lock().write_byte(byte);
}
6 changes: 5 additions & 1 deletion src/drivers/fs/virtio_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ impl FuseInterface for VirtioFsDriver {
&mut self,
cmd: fuse::Cmd<O>,
rsp_payload_len: u32,
) -> Result<fuse::Rsp<O>, VirtqError> {
) -> Result<fuse::Rsp<O>, VirtqError>
where
<O as fuse::ops::Op>::InStruct: Send,
<O as fuse::ops::Op>::OutStruct: Send,
{
let fuse::Cmd {
headers: cmd_headers,
payload: cmd_payload_opt,
Expand Down
3 changes: 3 additions & 0 deletions src/drivers/net/gem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ pub struct GEMDriver {
txbuffer_list: VirtAddr,
}

// FIXME: make `gem` implement `Send` instead
unsafe impl Send for GEMDriver {}

impl NetworkDriver for GEMDriver {
/// Returns the MAC address of the network interface
fn get_mac_address(&self) -> [u8; 6] {
Expand Down
Loading

0 comments on commit 9f90219

Please sign in to comment.