Move spinlock to separate folder
This commit is contained in:
@ -19,6 +19,7 @@ SRCFILES := $(wildcard *.c) \
|
|||||||
$(wildcard bitmap/*.c) \
|
$(wildcard bitmap/*.c) \
|
||||||
$(wildcard pmm/*.c) \
|
$(wildcard pmm/*.c) \
|
||||||
$(wildcard bootinfo/*.c) \
|
$(wildcard bootinfo/*.c) \
|
||||||
|
$(wildcard spinlock/*.c) \
|
||||||
$(wildcard hal/*.c) \
|
$(wildcard hal/*.c) \
|
||||||
$(wildcard hal/$(ARCH)/*.c) \
|
$(wildcard hal/$(ARCH)/*.c) \
|
||||||
$(wildcard hal/$(ARCH)/*.S) \
|
$(wildcard hal/$(ARCH)/*.S) \
|
||||||
|
@ -13,8 +13,8 @@ void kmain(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bootinfo_init();
|
bootinfo_init();
|
||||||
pmm_init();
|
|
||||||
hal_init();
|
hal_init();
|
||||||
|
pmm_init();
|
||||||
|
|
||||||
kprintf(BANNER_TEXT "\n");
|
kprintf(BANNER_TEXT "\n");
|
||||||
|
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <limine.h>
|
#include <limine.h>
|
||||||
#include "pmm.h"
|
#include "pmm.h"
|
||||||
#include "spinlock.h"
|
|
||||||
#include "kprintf.h"
|
#include "kprintf.h"
|
||||||
#include "bitmap/bitmap.h"
|
#include "bitmap/bitmap.h"
|
||||||
#include "bootinfo/bootinfo.h"
|
#include "bootinfo/bootinfo.h"
|
||||||
|
#include "spinlock/spinlock.h"
|
||||||
|
|
||||||
#define _DIV_ROUNDUP(num, div) ((num + div - 1) / div)
|
#define _DIV_ROUNDUP(num, div) ((num + div - 1) / div)
|
||||||
|
|
||||||
PhysMem PHYS_MEM;
|
PhysMem PHYS_MEM;
|
||||||
|
|
||||||
void pmm_init(void) {
|
void pmm_init(void) {
|
||||||
PHYS_MEM.spinlock = SPINLOCK_INIT();
|
spinlock_init(&PHYS_MEM.spinlock);
|
||||||
|
|
||||||
BitMap *bm = &PHYS_MEM.self;
|
BitMap *bm = &PHYS_MEM.self;
|
||||||
bm->init = false;
|
bm->init = false;
|
||||||
@ -53,9 +53,9 @@ void pmm_init(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void *pmm_alloc(size_t pages) {
|
void *pmm_alloc(size_t pages) {
|
||||||
SPINLOCK_ACQUIRE(&PHYS_MEM.spinlock);
|
spinlock_acquire(&PHYS_MEM.spinlock);
|
||||||
uintptr_t phys = (uintptr_t)bitmap_alloc(&PHYS_MEM.self, pages);
|
uintptr_t phys = (uintptr_t)bitmap_alloc(&PHYS_MEM.self, pages);
|
||||||
SPINLOCK_RELEASE(&PHYS_MEM.spinlock);
|
spinlock_release(&PHYS_MEM.spinlock);
|
||||||
|
|
||||||
if (!phys) {
|
if (!phys) {
|
||||||
ERR("hal/pmm", "phys memory ran out\n");
|
ERR("hal/pmm", "phys memory ran out\n");
|
||||||
@ -65,7 +65,7 @@ void *pmm_alloc(size_t pages) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void pmm_free(uintptr_t ptr, size_t pages) {
|
void pmm_free(uintptr_t ptr, size_t pages) {
|
||||||
SPINLOCK_ACQUIRE(&PHYS_MEM.spinlock);
|
spinlock_acquire(&PHYS_MEM.spinlock);
|
||||||
bitmap_markregion(&PHYS_MEM.self, (void *)ptr, pages * BITMAP_BLOCK_SIZE, 0);
|
bitmap_markregion(&PHYS_MEM.self, (void *)ptr, pages * BITMAP_BLOCK_SIZE, 0);
|
||||||
SPINLOCK_RELEASE(&PHYS_MEM.spinlock);
|
spinlock_release(&PHYS_MEM.spinlock);
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "spinlock.h"
|
|
||||||
#include "bitmap/bitmap.h"
|
#include "bitmap/bitmap.h"
|
||||||
|
#include "spinlock/spinlock.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
SpinLock spinlock;
|
SpinLock spinlock;
|
||||||
|
@ -1,31 +0,0 @@
|
|||||||
#ifndef SPINLOCK_SPINLOCK_H_
|
|
||||||
#define SPINLOCK_SPINLOCK_H_
|
|
||||||
|
|
||||||
#include <stdatomic.h>
|
|
||||||
#include "hal/hal.h"
|
|
||||||
|
|
||||||
typedef atomic_bool SpinLock;
|
|
||||||
|
|
||||||
// Spin more efficiently - cpu dependant
|
|
||||||
#if defined(__x86_64__)
|
|
||||||
# define SPINLOCK_HINT() asm volatile("pause")
|
|
||||||
#else
|
|
||||||
# define SPINLOCK_HINT()
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define SPINLOCK_ACQUIRE(sl) \
|
|
||||||
do { \
|
|
||||||
bool __unlocked = false; \
|
|
||||||
while (!atomic_compare_exchange_weak((sl), &__unlocked, true)) { \
|
|
||||||
SPINLOCK_HINT(); \
|
|
||||||
} \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
#define SPINLOCK_RELEASE(sl) \
|
|
||||||
do { \
|
|
||||||
atomic_store((sl), false); \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
#define SPINLOCK_INIT() false
|
|
||||||
|
|
||||||
#endif // SPINLOCK_SPINLOCK_H_
|
|
20
kernel/spinlock/spinlock.c
Normal file
20
kernel/spinlock/spinlock.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#include <stdatomic.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "spinlock.h"
|
||||||
|
|
||||||
|
void spinlock_init(SpinLock *sl) {
|
||||||
|
atomic_store(&sl->lock, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void spinlock_acquire(SpinLock *sl) {
|
||||||
|
bool unlocked = false;
|
||||||
|
while (!atomic_compare_exchange_weak(&sl->lock, &unlocked, true)) {
|
||||||
|
unlocked = false;
|
||||||
|
SPINLOCK_HINT();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void spinlock_release(SpinLock *sl) {
|
||||||
|
atomic_store(&sl->lock, false);
|
||||||
|
}
|
||||||
|
|
21
kernel/spinlock/spinlock.h
Normal file
21
kernel/spinlock/spinlock.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#ifndef SPINLOCK_SPINLOCK_H_
|
||||||
|
#define SPINLOCK_SPINLOCK_H_
|
||||||
|
|
||||||
|
#include <stdatomic.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "hal/hal.h"
|
||||||
|
|
||||||
|
typedef struct { atomic_bool lock; } SpinLock;
|
||||||
|
|
||||||
|
// Spin more efficiently - cpu dependant
|
||||||
|
#if defined(__x86_64__)
|
||||||
|
# define SPINLOCK_HINT() asm volatile("pause")
|
||||||
|
#else
|
||||||
|
# define SPINLOCK_HINT()
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void spinlock_init(SpinLock *sl);
|
||||||
|
void spinlock_acquire(SpinLock *sl);
|
||||||
|
void spinlock_release(SpinLock *sl);
|
||||||
|
|
||||||
|
#endif // SPINLOCK_SPINLOCK_H_
|
Reference in New Issue
Block a user