Spinlock save cpu flags

This commit is contained in:
2026-03-12 22:48:34 +01:00
parent 19793e9126
commit 4760818118
50 changed files with 704 additions and 461 deletions

View File

@@ -12,13 +12,13 @@
spin_lock_t _liballoc_lock = SPIN_LOCK_INIT;
int liballoc_lock (void) {
spin_lock (&_liballoc_lock);
int liballoc_lock (uint64_t* flags) {
spin_lock (&_liballoc_lock, flags);
return 0;
}
int liballoc_unlock (void) {
spin_unlock (&_liballoc_lock);
int liballoc_unlock (uint64_t flags) {
spin_unlock (&_liballoc_lock, flags);
return 0;
}
@@ -242,12 +242,13 @@ static struct boundary_tag* allocate_new_tag (unsigned int size) {
void* malloc (size_t size) {
size = align_up (size, 16);
uint64_t fla;
int index;
void* ptr;
struct boundary_tag* tag = NULL;
liballoc_lock ();
liballoc_lock (&fla);
if (l_initialized == 0) {
for (index = 0; index < MAXEXP; index++) {
@@ -275,7 +276,7 @@ void* malloc (size_t size) {
// No page found. Make one.
if (tag == NULL) {
if ((tag = allocate_new_tag (size)) == NULL) {
liballoc_unlock ();
liballoc_unlock (fla);
return NULL;
}
@@ -308,23 +309,24 @@ void* malloc (size_t size) {
ptr = (void*)((uintptr_t)tag + sizeof (struct boundary_tag));
liballoc_unlock ();
liballoc_unlock (fla);
return ptr;
}
void free (void* ptr) {
int index;
struct boundary_tag* tag;
uint64_t flf;
if (ptr == NULL)
return;
liballoc_lock ();
liballoc_lock (&flf);
tag = (struct boundary_tag*)((uintptr_t)ptr - sizeof (struct boundary_tag));
if (tag->magic != LIBALLOC_MAGIC) {
liballoc_unlock (); // release the lock
liballoc_unlock (flf); // release the lock
return;
}
@@ -357,7 +359,7 @@ void free (void* ptr) {
liballoc_free (tag, pages);
liballoc_unlock ();
liballoc_unlock (flf);
return;
}
@@ -368,7 +370,7 @@ void free (void* ptr) {
insert_tag (tag, index);
liballoc_unlock ();
liballoc_unlock (flf);
}
void* calloc (size_t nobj, size_t size) {
@@ -388,6 +390,7 @@ void* realloc (void* p, size_t size) {
void* ptr;
struct boundary_tag* tag;
int real_size;
uint64_t flr;
if (size == 0) {
free (p);
@@ -397,11 +400,11 @@ void* realloc (void* p, size_t size) {
return malloc (size);
if (&liballoc_lock != NULL)
liballoc_lock (); // lockit
liballoc_lock (&flr); // lockit
tag = (struct boundary_tag*)((uintptr_t)p - sizeof (struct boundary_tag));
real_size = tag->size;
if (&liballoc_unlock != NULL)
liballoc_unlock ();
liballoc_unlock (flr);
if ((size_t)real_size > size)
real_size = size;

View File

@@ -48,7 +48,7 @@ struct boundary_tag {
* \return 0 if the lock was acquired successfully. Anything else is
* failure.
*/
extern int liballoc_lock (void);
extern int liballoc_lock (uint64_t* flags);
/** This function unlocks what was previously locked by the liballoc_lock
* function. If it disabled interrupts, it enables interrupts. If it
@@ -56,7 +56,7 @@ extern int liballoc_lock (void);
*
* \return 0 if the lock was successfully released.
*/
extern int liballoc_unlock (void);
extern int liballoc_unlock (uint64_t flags);
/** This is the hook into the local system which allocates pages. It
* accepts an integer parameter which is the number of pages

View File

@@ -100,6 +100,8 @@ static size_t pmm_find_free_space (struct pmm_region* pmm_region, size_t nblks)
}
physaddr_t pmm_alloc (size_t nblks) {
uint64_t fpr;
for (size_t region = 0; region < PMM_REGIONS_MAX; region++) {
struct pmm_region* pmm_region = &pmm.regions[region];
@@ -107,7 +109,7 @@ physaddr_t pmm_alloc (size_t nblks) {
if (!(pmm_region->flags & PMM_REGION_ACTIVE))
continue;
spin_lock (&pmm_region->lock);
spin_lock (&pmm_region->lock, &fpr);
/* Find starting bit of the free bit range */
size_t bit = pmm_find_free_space (pmm_region, nblks);
@@ -116,18 +118,20 @@ physaddr_t pmm_alloc (size_t nblks) {
if (bit != (size_t)-1) {
/* Mark it */
bm_set_region (&pmm_region->bm, bit, nblks);
spin_unlock (&pmm_region->lock);
spin_unlock (&pmm_region->lock, fpr);
return pmm_region->membase + bit * PAGE_SIZE;
}
spin_unlock (&pmm_region->lock);
spin_unlock (&pmm_region->lock, fpr);
}
return PMM_ALLOC_ERR;
}
void pmm_free (physaddr_t p_addr, size_t nblks) {
uint64_t fpr;
/* Round down to nearest page boundary */
physaddr_t aligned_p_addr = align_down (p_addr, PAGE_SIZE);
@@ -145,11 +149,11 @@ void pmm_free (physaddr_t p_addr, size_t nblks) {
size_t bit = div_align_up (addr, PAGE_SIZE);
spin_lock (&pmm_region->lock);
spin_lock (&pmm_region->lock, &fpr);
bm_clear_region (&pmm_region->bm, bit, nblks);
spin_unlock (&pmm_region->lock);
spin_unlock (&pmm_region->lock, fpr);
break;
}