Permalink
Browse files

Improve spinlocks and add locking to pfa

  • Loading branch information...
block8437 committed Dec 3, 2017
1 parent f9f4111 commit ff8e51fecc196b4f5b6f7af7d0588f3cdb78f563
Showing with 21 additions and 9 deletions.
  1. +4 −4 src/arch/i386/interrupts/spinlock.asm
  2. +6 −3 src/include/kernel/spinlock.h
  3. +2 −2 src/include/mem/frame.h
  4. +9 −0 src/kernel/mem/frame.c
@@ -1,10 +1,10 @@
; Spinlock implementation
global spinlock_lock
global spinlock_unlock
global _spinlock_lock
global _spinlock_unlock
; Lock the spinlock
spinlock_lock:
_spinlock_lock:
push eax
push ebp
@@ -20,7 +20,7 @@ _spinlock_spin:
ret
; Unlock the spinlock
spinlock_unlock:
_spinlock_unlock:
push eax
push ebx
@@ -2,9 +2,12 @@
#define _SPINLOCK_H
#include <stdint.h>
typedef uint32_t* spinlock_t;
typedef uint32_t spinlock_t;
extern void spinlock_lock(spinlock_t lock);
extern void spinlock_unlock(spinlock_t lock);
extern void _spinlock_lock(spinlock_t* lock);
extern void _spinlock_unlock(spinlock_t* lock);
#define spinlock_lock(LOCK) _spinlock_lock(&(LOCK));
#define spinlock_unlock(LOCK) _spinlock_unlock(&(LOCK));
#endif
View
@@ -1,7 +1,6 @@
// Physical Frame Allocator
// Plan for allocator:
// Run length encoded linked list of free frames
#include <kernel/spinlock.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
@@ -18,6 +17,7 @@ struct frame_node {
};
typedef struct {
spinlock_t lock;
uintptr_t base;
frame_node_t* free_lists[MAX_FRAMES];
} frame_alloc_t;
View
@@ -2,6 +2,8 @@
#include <mem/kmalloc.h>
#include <mem/slab.h>
#include <kernel/spinlock.h>
#include <kprint.h>
#include <stdint.h>
#include <math.h>
@@ -101,6 +103,8 @@ void* frame_alloc(size_t want_count) {
return NULL;
}
spinlock_lock(buddy_alloc.lock);
// Find the smallest frame count with space
for ( int count = want_count; count <= MAX_FRAMES; count++ ) {
if ( buddy_alloc.free_lists[count - 1] != NULL ) {
@@ -117,11 +121,14 @@ void* frame_alloc(size_t want_count) {
frame_add_free_item(chunk_end, return_count, false);
}
spinlock_unlock(buddy_alloc.lock);
return (void*) address;
}
}
spinlock_unlock(buddy_alloc.lock);
// Didin't find anything :(
return NULL;
}
@@ -130,7 +137,9 @@ void* frame_alloc(size_t want_count) {
// address - starting address of the allocation
// count - amount of frames to return
void frame_dealloc(void* address, size_t count) {
spinlock_lock(buddy_alloc.lock);
frame_add_free_item((uintptr_t) address, count, false);
spinlock_unlock(buddy_alloc.lock);
}
// Prints out the current status of the allocator

0 comments on commit ff8e51f

Please sign in to comment.