serial print macro
parent
988d3d601d
commit
e2478d0f3a
@ -1,6 +1,6 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
const LINKER_SCRIPT_PATH: &str = "link.ld";
|
const LINKER_SCRIPT_PATH: &str = "link.ld";
|
||||||
|
|
||||||
println!("cargo::rerun-if-changed={}", LINKER_SCRIPT_PATH);
|
println!("cargo::rerun-if-changed={}", LINKER_SCRIPT_PATH);
|
||||||
println!("cargo::rustc-link-arg=-T{}", LINKER_SCRIPT_PATH);
|
println!("cargo::rustc-link-arg=-T{}", LINKER_SCRIPT_PATH);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,23 +1,23 @@
|
|||||||
use core::arch::asm;
|
use core::arch::asm;
|
||||||
|
|
||||||
pub unsafe fn inb(port: u16) -> u8 {
|
pub unsafe fn inb(port: u16) -> u8 {
|
||||||
let x: u8;
|
let x: u8;
|
||||||
unsafe {
|
unsafe {
|
||||||
asm!(
|
asm!(
|
||||||
"in al, dx",
|
"in al, dx",
|
||||||
in("dx") port,
|
in("dx") port,
|
||||||
lateout("al") x
|
lateout("al") x
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
x
|
x
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn outb(port: u16, x: u8) {
|
pub unsafe fn outb(port: u16, x: u8) {
|
||||||
unsafe {
|
unsafe {
|
||||||
asm!(
|
asm!(
|
||||||
"out dx, al",
|
"out dx, al",
|
||||||
in("al") x,
|
in("al") x,
|
||||||
in("dx") port,
|
in("dx") port,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,101 +1,104 @@
|
|||||||
use core::{cell::UnsafeCell, hint, ops::{Deref, DerefMut}, sync::atomic::{AtomicBool, Ordering}};
|
use core::{
|
||||||
|
cell::UnsafeCell,
|
||||||
|
hint,
|
||||||
|
ops::{Deref, DerefMut},
|
||||||
|
sync::atomic::{AtomicBool, Ordering},
|
||||||
|
};
|
||||||
|
|
||||||
pub struct Spinlock<T> {
|
pub struct Spinlock<T> {
|
||||||
data: UnsafeCell<T>,
|
data: UnsafeCell<T>,
|
||||||
locked: AtomicBool,
|
locked: AtomicBool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Spinlock<T> {
|
impl<T> Spinlock<T> {
|
||||||
pub const fn new(data: T) -> Self {
|
pub const fn new(data: T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
data: UnsafeCell::new(data),
|
data: UnsafeCell::new(data),
|
||||||
locked: AtomicBool::new(false),
|
locked: AtomicBool::new(false),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn lock(&self) -> SpinlockGuard<'_, T> {
|
pub fn lock(&self) -> SpinlockGuard<'_, T> {
|
||||||
// If we observe `locked` was `false`, then:
|
// If we observe `locked` was `false`, then:
|
||||||
// - Use acquire ordering, so nothing inside the critical section gets reordered before we
|
// - Use acquire ordering, so nothing inside the critical section gets reordered before we
|
||||||
// observed the `false` value.
|
// observed the `false` value.
|
||||||
// - Store `true`, so nothing else can enter the critical section until we exit it.
|
// - Store `true`, so nothing else can enter the critical section until we exit it.
|
||||||
// Otherwise, spin until we observe a `false` value.
|
// Otherwise, spin until we observe a `false` value.
|
||||||
while self.locked
|
while self
|
||||||
.compare_exchange_weak(false, true, Ordering::Acquire, Ordering::Relaxed)
|
.locked
|
||||||
.is_err()
|
.compare_exchange_weak(false, true, Ordering::Acquire, Ordering::Relaxed)
|
||||||
{
|
.is_err()
|
||||||
hint::spin_loop();
|
{
|
||||||
}
|
hint::spin_loop();
|
||||||
|
}
|
||||||
|
|
||||||
SpinlockGuard { lock: self }
|
SpinlockGuard { lock: self }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// There must be no "active" `SpinlockGuards` for this lock, i.e. a `SpinlockGuard` which can be
|
/// There must be no "active" `SpinlockGuards` for this lock, i.e. a `SpinlockGuard` which can be
|
||||||
/// used to obtain a reference to the spinlock-protected data.
|
/// used to obtain a reference to the spinlock-protected data.
|
||||||
unsafe fn unlock(&self) {
|
unsafe fn unlock(&self) {
|
||||||
// Unset `locked` with release ordering so that nothing inside the critical section gets
|
// Unset `locked` with release ordering so that nothing inside the critical section gets
|
||||||
// reordered to after we stored `false`.
|
// reordered to after we stored `false`.
|
||||||
self.locked.store(false, Ordering::Release);
|
self.locked.store(false, Ordering::Release);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// No mutable references to the spinlock-protected data may exist.
|
/// No mutable references to the spinlock-protected data may exist.
|
||||||
unsafe fn get<'s, 'a>(&'s self) -> &'a T
|
unsafe fn get<'s, 'a>(&'s self) -> &'a T
|
||||||
where
|
where
|
||||||
's: 'a,
|
's: 'a,
|
||||||
{
|
{
|
||||||
unsafe { &*self.data.get() }
|
unsafe { &*self.data.get() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// No references to the spinlock-protected data may exist.
|
/// No references to the spinlock-protected data may exist.
|
||||||
unsafe fn get_mut<'s, 'a>(&'s self) -> &'a mut T
|
unsafe fn get_mut<'s, 'a>(&'s self) -> &'a mut T
|
||||||
where
|
where
|
||||||
's: 'a,
|
's: 'a,
|
||||||
{
|
{
|
||||||
unsafe { &mut *self.data.get() }
|
unsafe { &mut *self.data.get() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<T> Sync for Spinlock<T>
|
unsafe impl<T> Sync for Spinlock<T> where T: Send {}
|
||||||
where
|
|
||||||
T: Send,
|
|
||||||
{}
|
|
||||||
|
|
||||||
pub struct SpinlockGuard<'a, T> {
|
pub struct SpinlockGuard<'a, T> {
|
||||||
lock: &'a Spinlock<T>,
|
lock: &'a Spinlock<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> Deref for SpinlockGuard<'a, T> {
|
impl<'a, T> Deref for SpinlockGuard<'a, T> {
|
||||||
type Target = T;
|
type Target = T;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
// For the entire lifetime of the `SpinlockGuard`, `locked` remains `true`, so we have
|
// For the entire lifetime of the `SpinlockGuard`, `locked` remains `true`, so we have
|
||||||
// exclusive access to `data`, so no mutable references to `data` can exist.
|
// exclusive access to `data`, so no mutable references to `data` can exist.
|
||||||
unsafe { self.lock.get() }
|
unsafe { self.lock.get() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> DerefMut for SpinlockGuard<'a, T> {
|
impl<'a, T> DerefMut for SpinlockGuard<'a, T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
// For the entire lifetime of the `SpinlockGuard`, `locked` remains `true`, so we have
|
// For the entire lifetime of the `SpinlockGuard`, `locked` remains `true`, so we have
|
||||||
// exclusive access to `data`, so no other references to `data` can exist.
|
// exclusive access to `data`, so no other references to `data` can exist.
|
||||||
unsafe { self.lock.get_mut() }
|
unsafe { self.lock.get_mut() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> Drop for SpinlockGuard<'a, T> {
|
impl<'a, T> Drop for SpinlockGuard<'a, T> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
// Only one `SpinlockGuard` can exist at a time for a particular lock, since we set `locked`
|
// Only one `SpinlockGuard` can exist at a time for a particular lock, since we set `locked`
|
||||||
// to true before creating a guard and refuse to create any new ones until it is `false` again.
|
// to true before creating a guard and refuse to create any new ones until it is `false` again.
|
||||||
// Therefore, we are the only `SpinlockGuard` for the lock. Since this is the destructor, and
|
// Therefore, we are the only `SpinlockGuard` for the lock. Since this is the destructor, and
|
||||||
// we don't access the spinlock-protected data here, there are therefore no "active"
|
// we don't access the spinlock-protected data here, there are therefore no "active"
|
||||||
// `SpinlockGuard`s remaining for the lock.
|
// `SpinlockGuard`s remaining for the lock.
|
||||||
unsafe { self.lock.unlock() }
|
unsafe { self.lock.unlock() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue