diff --git a/kernel/spinlock.c b/kernel/spinlock.c index aaf1b09..51252cf 100644 --- a/kernel/spinlock.c +++ b/kernel/spinlock.c @@ -8,12 +8,6 @@ #include "proc.h" #include "defs.h" -void initlock(struct spinlock *lk, char *name) { - lk->name = name; - lk->locked = 0; - lk->cpu = 0; -} - // Acquire the lock. // Loops (spins) until the lock is acquired. void acquire(struct spinlock *lk) { diff --git a/theseus/src/spinlock.rs b/theseus/src/spinlock.rs index faeafd0..cfc6ac7 100644 --- a/theseus/src/spinlock.rs +++ b/theseus/src/spinlock.rs @@ -1,9 +1,18 @@ +use core::ptr::null_mut; + // Mutual exclusion lock. #[repr(C)] pub struct SpinLock { // Is the lock held? pub locked: u32, // C boolean // For debugging: - pub name: Option<*mut ()>, // Name of lock. - pub cpu: Option<*mut ()>, // The cpu holding the lock. + pub name: *const u8, // Name of lock. + pub cpu: *mut (), // The cpu holding the lock. +} + +#[no_mangle] +pub unsafe extern "C" fn initlock(lock: *mut SpinLock, name: *const u8) { + (*lock).name = name; + (*lock).locked = 0; + (*lock).cpu = null_mut(); } diff --git a/theseus/src/uart.rs b/theseus/src/uart.rs index 1957f08..1fcbf26 100644 --- a/theseus/src/uart.rs +++ b/theseus/src/uart.rs @@ -1,11 +1,13 @@ -use crate::{memlayout::UART0, spinlock::SpinLock}; -use core::ptr::addr_of_mut; +use crate::{ + memlayout::UART0, + spinlock::{initlock, SpinLock}, +}; +use core::ptr::{addr_of_mut, null, null_mut}; extern "C" { // spinlock.c fn push_off(); fn pop_off(); - fn initlock(spinlock: *mut SpinLock, name: *const u8); fn acquire(spinlock: *mut SpinLock); fn release(spinlock: *mut SpinLock); @@ -60,12 +62,12 @@ pub fn WriteReg(reg: u8, v: u8) { // the transmit output buffer. static mut uart_tx_lock: crate::spinlock::SpinLock = crate::spinlock::SpinLock { locked: 0, - cpu: None, - name: None, + cpu: null_mut(), + name: null(), }; // NOTE: string 'uart\0' for c-implmentation of spinlock. Stupid but works. -static UART_LOCK_NAME: [u8; 5] = [117, 97, 114, 116, 0]; +static UART_LOCK_NAME: [u8; 5] = [0x75, 0x61, 0x72, 0x74, 0]; const UART_TX_BUF_SIZE: usize = 32; static mut uart_tx_buf: [u8; UART_TX_BUF_SIZE] = [0; UART_TX_BUF_SIZE];