KernelVM: Make g_used_vm atomic and g_kernelvm_bitmap a LockedValue
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2022-12-17 10:54:09 +01:00
parent 132963070b
commit df9a13cbfb
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -1,10 +1,11 @@
#include "memory/KernelVM.h"
#include "arch/MMU.h"
#include "thread/Spinlock.h"
#include <luna/Bitmap.h>
static const u64 KERNEL_VM_RANGE_START = 0xfffffffff0000000;
static Bitmap g_kernelvm_bitmap;
static LockedValue<Bitmap> g_kernelvm_bitmap;
static u8 bitmap_memory[4096];
@ -14,22 +15,26 @@ static const u64 KERNEL_VM_RANGE_END = KERNEL_VM_RANGE_SIZE + KERNEL_VM_RANGE_ST
static_assert(KERNEL_VM_RANGE_END == 0xfffffffff8000000);
static usize g_used_vm;
static Atomic<usize> g_used_vm;
namespace KernelVM
{
void init()
{
g_kernelvm_bitmap.initialize(bitmap_memory, sizeof(bitmap_memory));
g_kernelvm_bitmap.clear(false);
g_kernelvm_bitmap.init();
auto kernelvm_bitmap = g_kernelvm_bitmap.lock();
kernelvm_bitmap->initialize(bitmap_memory, sizeof(bitmap_memory));
kernelvm_bitmap->clear(false);
}
Result<u64> alloc_one_page()
{
for (u64 index = 0; index < g_kernelvm_bitmap.size(); index++)
auto kernelvm_bitmap = g_kernelvm_bitmap.lock();
for (u64 index = 0; index < kernelvm_bitmap->size(); index++)
{
if (g_kernelvm_bitmap.get(index)) continue;
g_kernelvm_bitmap.set(index, true);
if (kernelvm_bitmap->get(index)) continue;
kernelvm_bitmap->set(index, true);
g_used_vm += ARCH_PAGE_SIZE;
return KERNEL_VM_RANGE_START + (index * ARCH_PAGE_SIZE);
}
@ -41,9 +46,10 @@ namespace KernelVM
{
u64 first_free_index = 0;
u64 free_contiguous_pages = 0;
for (u64 index = 0; index < g_kernelvm_bitmap.size(); index++)
auto kernelvm_bitmap = g_kernelvm_bitmap.lock();
for (u64 index = 0; index < kernelvm_bitmap->size(); index++)
{
if (g_kernelvm_bitmap.get(index))
if (kernelvm_bitmap->get(index))
{
free_contiguous_pages = 0;
continue;
@ -70,7 +76,7 @@ namespace KernelVM
u64 start_index;
if (find_several_pages_impl(count, start_index))
{
g_kernelvm_bitmap.clear_region(start_index, count, true);
g_kernelvm_bitmap.lock()->clear_region(start_index, count, true);
g_used_vm += ARCH_PAGE_SIZE * count;
return KERNEL_VM_RANGE_START + (start_index * ARCH_PAGE_SIZE);
}
@ -84,9 +90,11 @@ namespace KernelVM
u64 index = (address - KERNEL_VM_RANGE_START) / ARCH_PAGE_SIZE;
if (index >= g_kernelvm_bitmap.size()) return err(EFAULT);
auto kernelvm_bitmap = g_kernelvm_bitmap.lock();
g_kernelvm_bitmap.set(index, false);
if (index >= kernelvm_bitmap->size()) return err(EFAULT);
kernelvm_bitmap->set(index, false);
g_used_vm -= ARCH_PAGE_SIZE;
return {};
@ -99,7 +107,7 @@ namespace KernelVM
u64 index = (address - KERNEL_VM_RANGE_START) / ARCH_PAGE_SIZE;
g_kernelvm_bitmap.clear_region(index, count, false);
g_kernelvm_bitmap.lock()->clear_region(index, count, false);
g_used_vm -= ARCH_PAGE_SIZE * count;
return {};