Skip to content

Commit

Permalink
pmm: Implement tryalloc() for NBBS.
Browse files Browse the repository at this point in the history
Signed-off-by: TunaCici <[email protected]>
  • Loading branch information
TunaCici committed May 11, 2024
1 parent 02f91ad commit 3c86119
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 21 deletions.
22 changes: 13 additions & 9 deletions Kernel/Include/Memory/NBBS.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@
* +---------+----------+------------+------------+----------+----------+
*/

#define OCC_RIGHT 0x1
#define OCC_LEFT 0x2
#define COAL_RIGHT 0x4
#define COAL_LEFT 0x8
#define OCC 0x10
#define OCC_RIGHT ((uint8_t) 0x1)
#define OCC_LEFT ((uint8_t) 0x2)
#define COAL_RIGHT ((uint8_t) 0x4)
#define COAL_LEFT ((uint8_t) 0x8)
#define OCC ((uint8_t) 0x10)
#define BUSY (OCC | OCC_LEFT | OCC_RIGHT)

#define EXP2(n) (0x1 << (n))
#define LOG2_LOWER(n) (64 - __builtin_clzll(n) - 1) // 64 bit
#define CAS(addr, cmp, val) __atomic_compare_exchange (addr, cmp, val, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
#define EXP2(n) (0x1ULL << (n))
#define LOG2_LOWER(n) (64ULL - __builtin_clzll(n) - 1ULL) // 64 bit
#define CAS(addr, cmp, val) __atomic_compare_exchange_n(addr, cmp, val, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)

#define LEVEL(n) LOG2_LOWER(n)

/*
* Public APIs
Expand All @@ -42,7 +44,7 @@

int nb_init(uint64_t base_addr, uint64_t size);
void* nb_alloc(uint64_t size);
void* nb_free(void *addr);
void nb_free(void *addr);

/*
* Private APIs
Expand All @@ -51,6 +53,8 @@ void* nb_free(void *addr);
*/

uint32_t try_alloc(uint32_t node);
void freenode(uint32_t node, uint32_t level);


/*
* Helpers
Expand Down
17 changes: 11 additions & 6 deletions Kernel/Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,27 @@ void kmain(boot_sysinfo* boot_params)
/* 2. Init NBBS */
klog("[kmain] Initializing NBBS...\n");

if (nb_init(mem_start, 32 * 1024)) {
if (nb_init(mem_start + 0x1000000, 32 * 1024)) {
klog("[kmain] Failed to initialize NBBS ;(\n");
} else {
klog("[kmain] Initialized NBBS (%u KiB)!\n", 64u);
}

klog("[kmain] nb_alloc\n");

uint64_t *bruh = nb_alloc(4096);
for (int i = 0; i < 10; i++) {
uint64_t *bruh = nb_alloc(4096);

if (bruh) {
klog("[kmain] nb_alloc ok\n");
} else {
klog("[kmain] nb_alloc fail\n");
if (bruh) {
klog("[kmain] nb_alloc ok (0x%p)\n", bruh);
} else {
klog("[kmain] nb_alloc fail\n");
}

ksleep(1000);
}


/* 2. Init PMM */
// klog("[kmain] Initializing physical memory manager...\n");
//
Expand Down
45 changes: 40 additions & 5 deletions Kernel/Memory/NBBS.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,37 @@ int nb_init(uint64_t base, uint64_t size)

uint32_t try_alloc(uint32_t node)
{
return 1;
/* Try to mark as BUSY */
uint8_t free = 0;
if (!CAS(&tree[node], &free, BUSY)) {
return node;
}

uint32_t current = node;
uint32_t child = 0;

/* Propagate info about the occupancy up to the ancestor node(s) */
while (max_level < LEVEL(current)) {
child = current;
current = current >> 1;

uint8_t curr_val = 0;
uint8_t new_val = 0;

do {
curr_val = tree[current];

if (curr_val & OCC) {
freenode(node, LEVEL(child));
return current;
}

new_val = clean_coal(curr_val, child);
new_val = mark(new_val, child);
} while (!CAS(&tree[current], &curr_val, new_val));
}

return 0;
}

void* nb_alloc(uint64_t size)
Expand Down Expand Up @@ -115,8 +145,8 @@ void* nb_alloc(uint64_t size)
return (void*) (base_address + leaf * min_size);
} else {
/* Skip the entire subtree [of failed] */
uint32_t curr_level = LOG2_LOWER(i);
uint32_t fail_level = LOG2_LOWER(failed_at);
uint32_t curr_level = LEVEL(i);
uint32_t fail_level = LEVEL(failed_at);

uint32_t d = EXP2(curr_level - fail_level);
i = (failed_at + 1) * d;
Expand All @@ -128,9 +158,14 @@ void* nb_alloc(uint64_t size)

}

void* nb_free(void *addr)
void freenode(uint32_t node, uint32_t level)
{


}

void nb_free(void *addr)
{

}


Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ HOST_AR = ar
INCLUDES = \
-I Kernel/Include -I Kernel/Arch \
-I Tests/googletest/googletest/include
CCFLAGS = ${INCLUDES} -march=armv8-a -mtune=cortex-a72 -g \
CCFLAGS = ${INCLUDES} -march=armv8-a -mtune=cortex-a72 -mno-outline-atomics -g \
-Wall -Wextra -ffreestanding -nostdlib -std=gnu99 -DDEBUG
CXXFLAGS = ${INCLUDES} -march=armv8-a -mtune=cortex-a72 -g \
-Wall -Wextra -ffreestanding -nostdlib -std=c++20 -DDEBUG
Expand Down

0 comments on commit 3c86119

Please sign in to comment.