garbage self made raw spin lock 😎

This commit is contained in:
Bryan McShea
2024-02-10 22:04:12 -05:00
parent 4cf2f5b29e
commit 97f3c5bb93
9 changed files with 34 additions and 25 deletions

View File

@@ -13,6 +13,8 @@ static mut DT_ADDR: *mut FDT = null_mut();
#[naked]
unsafe extern "C" fn _start() -> ! {
core::arch::asm!(
// disable interrupts
"csrw mie, zero",
// set up gp & sp
".option push",
".option norelax",

View File

@@ -1,6 +1,6 @@
use core::fmt::{self, Write};
use spin::Mutex;
use crate::util::mutex::Mutex;
// --machine sifive_u
// const UART_BASE: u32 = 0x10010000;

View File

@@ -104,5 +104,5 @@ extern "x86-interrupt" fn keyboard(_stack_frame: InterruptStackFrame) {
pub const PIC_1_OFFSET: u8 = 32;
pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
pub static PICS: spin::Mutex<ChainedPics> =
spin::Mutex::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) });
pub static PICS: Mutex<ChainedPics> =
Mutex::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) });

View File

@@ -1,6 +1,5 @@
use core::fmt::Arguments;
use spin::Mutex;
use uart_16550::SerialPort;
use x86_64::instructions::{interrupts, port::Port};

View File

@@ -21,6 +21,10 @@ pub fn main(dt_addr: *mut FDT) -> ! {
let mut test = alloc::vec![1, 2, 3];
test.push(3);
println!("{:?}", test);
unsafe {
let x = *(0x3000 as *const u8);
println!("{}", x);
}
// for _ in 0..40000000 {}
// let x = unsafe { *(0x10000000000 as *mut u8) };
// println!("we got {x}");

View File

@@ -1,29 +1,44 @@
use core::ops::{Deref, DerefMut};
use super::spin::SpinLock;
struct Mutex<T> {
pub struct Mutex<T> {
lock: SpinLock,
val: T,
}
impl<T> Mutex<T> {
pub fn new(val: T) -> Self {
pub const fn new(val: T) -> Self {
Self {
lock: SpinLock::new(),
val,
}
}
pub fn lock(&mut self) -> MutexGuard<T> {
pub fn lock(&self) -> MutexGuard<T> {
self.lock.lock();
MutexGuard { mutex: self }
}
}
struct MutexGuard<'a, T> {
mutex: &'a mut Mutex<T>,
pub struct MutexGuard<'a, T> {
mutex: &'a Mutex<T>,
}
impl <'a, T> Drop for MutexGuard<'a, T> {
impl<'a, T> Drop for MutexGuard<'a, T> {
fn drop(&mut self) {
self.mutex.lock.unlock();
}
}
impl<'a, T> Deref for MutexGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.mutex.val
}
}
impl<'a, T> DerefMut for MutexGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe {&mut (*(self.mutex as *const Mutex<T> as *mut Mutex<T>)).val}
}
}

View File

@@ -5,15 +5,15 @@ pub struct SpinLock {
}
impl SpinLock {
pub fn new() -> Self {
pub const fn new() -> Self {
Self {
locked: AtomicBool::new(false),
}
}
pub fn lock(&mut self) {
pub fn lock(&self) {
while self.locked.swap(true, Ordering::Acquire) {}
}
pub fn unlock(&mut self) {
pub fn unlock(&self) {
self.locked.store(false, Ordering::Release)
}
}