Fixed spinlock halting caused by data misalignment

feat/main
hheik 2024-05-06 02:25:45 +03:00
parent 7797ae7810
commit 623c44fcb3
3 changed files with 19 additions and 14 deletions

View File

@ -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) {

View File

@ -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();
}

View File

@ -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];