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",
"pc-keyboard",
"pic8259",
"spin 0.9.8",
"uart_16550",
"x86_64",
]
@@ -97,7 +96,7 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
dependencies = [
"spin 0.5.2",
"spin",
]
[[package]]
@@ -176,15 +175,6 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "spinning_top"
version = "0.2.5"

View File

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

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)
}
}