Kernel: Implement basic SpinLock

This commit is contained in:
Bananymous 2023-02-01 01:51:25 +02:00
parent 1945b716ad
commit 337569b0ca
8 changed files with 109 additions and 1 deletions

View File

@ -45,6 +45,7 @@ kernel/PIT.o \
kernel/RTC.o \
kernel/Serial.o \
kernel/Shell.o \
kernel/SpinLock.o \
kernel/SSP.o \
kernel/TTY.o \
kernel/VesaTerminalDriver.o \

View File

@ -0,0 +1,17 @@
.global spinlock_lock_asm
spinlock_lock_asm:
lock; btsl $0, (%edi)
jnc .done
.retry:
pause
testl $1, (%edi)
jne .retry
lock; btsl $0, (%edi)
jc .retry
.done:
ret
.global spinlock_unlock_asm
spinlock_unlock_asm:
movl $0, (%edi)
ret

View File

@ -7,4 +7,5 @@ KERNEL_ARCH_OBJS= \
$(ARCHDIR)/boot.o \
$(ARCHDIR)/IDT.o \
$(ARCHDIR)/MMU.o \
$(ARCHDIR)/SpinLock.o \

View File

@ -0,0 +1,17 @@
.global spinlock_lock_asm
spinlock_lock_asm:
lock; btsq $0, (%rdi)
jnc .done
.retry:
pause
testq $1, (%rdi)
jne .retry
lock; btsq $0, (%rdi)
jc .retry
.done:
ret
.global spinlock_unlock_asm
spinlock_unlock_asm:
movq $0, (%rdi)
ret

View File

@ -6,6 +6,7 @@ KERNEL_ARCH_LIBS=
KERNEL_ARCH_OBJS= \
$(ARCHDIR)/boot.o \
$(ARCHDIR)/IDT.o \
$(ARCHDIR)/MMU.o \
$(ARCHDIR)/interrupts.o \
$(ARCHDIR)/MMU.o \
$(ARCHDIR)/SpinLock.o \

View File

@ -0,0 +1,30 @@
#pragma once
#include <BAN/NoCopyMove.h>
namespace Kernel
{
template<typename Lock>
class LockGuard
{
BAN_NON_COPYABLE(LockGuard<Lock>);
BAN_NON_MOVABLE(LockGuard<Lock>);
public:
LockGuard(Lock& lock)
: m_lock(lock)
{
m_lock.lock();
}
~LockGuard()
{
m_lock.unlock();
}
private:
Lock& m_lock;
};
}

View File

@ -0,0 +1,22 @@
#pragma once
#include <BAN/NoCopyMove.h>
namespace Kernel
{
class SpinLock
{
BAN_NON_COPYABLE(SpinLock);
BAN_NON_MOVABLE(SpinLock);
public:
SpinLock() = default;
void lock();
void unlock();
private:
int m_lock = 0;
};
}

View File

@ -0,0 +1,19 @@
#include <kernel/SpinLock.h>
namespace Kernel
{
extern "C" void spinlock_lock_asm(int*);
extern "C" void spinlock_unlock_asm(int*);
void SpinLock::lock()
{
spinlock_lock_asm(&m_lock);
}
void SpinLock::unlock()
{
spinlock_unlock_asm(&m_lock);
}
}