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

12
kernel/Cargo.lock generated
View File

@@ -86,7 +86,6 @@ dependencies = [
"linked_list_allocator", "linked_list_allocator",
"pc-keyboard", "pc-keyboard",
"pic8259", "pic8259",
"spin 0.9.8",
"uart_16550", "uart_16550",
"x86_64", "x86_64",
] ]
@@ -97,7 +96,7 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
dependencies = [ dependencies = [
"spin 0.5.2", "spin",
] ]
[[package]] [[package]]
@@ -176,15 +175,6 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
dependencies = [
"lock_api",
]
[[package]] [[package]]
name = "spinning_top" name = "spinning_top"
version = "0.2.5" version = "0.2.5"

View File

@@ -10,7 +10,6 @@ bench = false
[dependencies] [dependencies]
embedded-graphics = "0.8.1" embedded-graphics = "0.8.1"
spin = "0.9.8"
pc-keyboard = "0.5.0" pc-keyboard = "0.5.0"
linked_list_allocator = "0.10.5" linked_list_allocator = "0.10.5"

View File

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

View File

@@ -1,6 +1,6 @@
use core::fmt::{self, Write}; use core::fmt::{self, Write};
use spin::Mutex; use crate::util::mutex::Mutex;
// --machine sifive_u // --machine sifive_u
// const UART_BASE: u32 = 0x10010000; // 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_1_OFFSET: u8 = 32;
pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8; pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
pub static PICS: spin::Mutex<ChainedPics> = pub static PICS: Mutex<ChainedPics> =
spin::Mutex::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) }); Mutex::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) });

View File

@@ -1,6 +1,5 @@
use core::fmt::Arguments; use core::fmt::Arguments;
use spin::Mutex;
use uart_16550::SerialPort; use uart_16550::SerialPort;
use x86_64::instructions::{interrupts, port::Port}; 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]; let mut test = alloc::vec![1, 2, 3];
test.push(3); test.push(3);
println!("{:?}", test); println!("{:?}", test);
unsafe {
let x = *(0x3000 as *const u8);
println!("{}", x);
}
// for _ in 0..40000000 {} // for _ in 0..40000000 {}
// let x = unsafe { *(0x10000000000 as *mut u8) }; // let x = unsafe { *(0x10000000000 as *mut u8) };
// println!("we got {x}"); // println!("we got {x}");

View File

@@ -1,25 +1,27 @@
use core::ops::{Deref, DerefMut};
use super::spin::SpinLock; use super::spin::SpinLock;
struct Mutex<T> { pub struct Mutex<T> {
lock: SpinLock, lock: SpinLock,
val: T, val: T,
} }
impl<T> Mutex<T> { impl<T> Mutex<T> {
pub fn new(val: T) -> Self { pub const fn new(val: T) -> Self {
Self { Self {
lock: SpinLock::new(), lock: SpinLock::new(),
val, val,
} }
} }
pub fn lock(&mut self) -> MutexGuard<T> { pub fn lock(&self) -> MutexGuard<T> {
self.lock.lock(); self.lock.lock();
MutexGuard { mutex: self } MutexGuard { mutex: self }
} }
} }
struct MutexGuard<'a, T> { pub struct MutexGuard<'a, T> {
mutex: &'a mut Mutex<T>, mutex: &'a Mutex<T>,
} }
impl<'a, T> Drop for MutexGuard<'a, T> { impl<'a, T> Drop for MutexGuard<'a, T> {
@@ -27,3 +29,16 @@ impl <'a, T> Drop for MutexGuard<'a, T> {
self.mutex.lock.unlock(); 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 { impl SpinLock {
pub fn new() -> Self { pub const fn new() -> Self {
Self { Self {
locked: AtomicBool::new(false), locked: AtomicBool::new(false),
} }
} }
pub fn lock(&mut self) { pub fn lock(&self) {
while self.locked.swap(true, Ordering::Acquire) {} while self.locked.swap(true, Ordering::Acquire) {}
} }
pub fn unlock(&mut self) { pub fn unlock(&self) {
self.locked.store(false, Ordering::Release) self.locked.store(false, Ordering::Release)
} }
} }