Skip to content

Commit

Permalink
pmm: Added NBBS definitions and init() function. WIP.
Browse files Browse the repository at this point in the history
Signed-off-by: TunaCici <[email protected]>
  • Loading branch information
TunaCici committed May 8, 2024
1 parent 863b029 commit 44dbdc8
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 10 deletions.
82 changes: 82 additions & 0 deletions Kernel/Include/Memory/NBBS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Non-Blocking Buddy System - To be used to the new physical memory mngr.
*
* As described in:
* "A Non-blocking Buddy System for Scalable Memory Allocation on Multi-core
* Machines by" R. Marotta, M. Ianni, A. Scarselli, A. Pellegrini and F. Quaglia
*
* Reference:
* https://github.com/HPDCS/NBBS
*
* Author: Tuna CICI
*/

#include <stdint.h>

/*
* Tree node status bits:
*
* 7 5 4 3 2 1 0
* +---------+----------+------------+------------+----------+----------+
* | ignored | occupied | left | rigth | left | right |
* | | | coalescent | coalescent | occupied | occupied |
* +---------+----------+------------+------------+----------+----------+
*/

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

/*
* Public APIs
*
* TODO: Explain.
*/

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

/*
* Helpers
*
* TODO:? Explain.
*/

static inline uint8_t clean_coal(uint8_t val, uint32_t child)
{
return ((uint8_t) (val & !(COAL_LEFT >> (child % 2))));
}

static inline uint8_t mark(uint8_t val, uint32_t child)
{
return ((uint8_t) (val | (OCC_LEFT >> (child % 2))));
}

static inline uint8_t unmark(uint8_t val, uint32_t child)
{
return ((uint8_t) (val & !((COAL_LEFT | OCC_LEFT) >> (child % 2))));
}

static inline uint8_t is_coal(uint8_t val, uint32_t child)
{
return ((uint8_t) (val & (COAL_LEFT >> (child % 2))));
}

static inline uint8_t is_occ_buddy(uint8_t val, uint32_t child)
{
return ((uint8_t) (val & (OCC_RIGHT << (child % 2))));
}

static inline uint8_t is_coal_buddy(uint8_t val, uint32_t child)
{
return ((uint8_t) (val & (COAL_RIGHT << (child % 2))));
}

static inline uint8_t is_free(uint8_t val)
{
return !(val & BUSY);
}
28 changes: 19 additions & 9 deletions Kernel/Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "Memory/PageDef.h"
#include "Memory/BootMem.h"
#include "Memory/Physical.h"
#include "Memory/NBBS.h"
#include "Memory/Virtual.h"

/*
Expand Down Expand Up @@ -77,17 +78,26 @@ void kmain(boot_sysinfo* boot_params)
pageCount, (pageCount * PAGE_SIZE) / 1024
);

/* 2. Init PMM */
klog("[kmain] Initializing physical memory manager...\n");
/* 2. Init NBBS */
klog("[kmain] Initializing NBBS...\n");

uint64_t blockCount = init_allocator(
(const void *) boot_params->k_phy_base + boot_params->k_size + pageCount * PAGE_SIZE,
(const void *) mem_end
);
if (nb_init(mem_start, mem_end - mem_start)) {
klog("[kmain] Failed to initialize NBBS ;(\n");
} else {
klog("[kmain] Initialized NBBS!\n");
}

klog("[kmain] 2 MiB blocks available: %lu (%lu MiB) in pmm\n",
blockCount, blockCount * 2
);
/* 2. Init PMM */
// klog("[kmain] Initializing physical memory manager...\n");
//
// uint64_t blockCount = init_allocator(
// (const void *) boot_params->k_phy_base + boot_params->k_size + pageCount * PAGE_SIZE,
// (const void *) mem_end
// );

// klog("[kmain] 2 MiB blocks available: %lu (%lu MiB) in pmm\n",
// blockCount, blockCount * 2
// );

/* 3. Init Kernel Page Tables & Enable MMU */

Expand Down
83 changes: 83 additions & 0 deletions Kernel/Memory/NBBS.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* This file (NBSS.c) implements the Non-Blocking Buddy System
*/

#ifndef NBBS_H
#define NBBS_H

#include <stdint.h>

#include "LibKern/String.h"
#include "Libkern/String.h"

#include "Memory/PageDef.h"
#include "Memory/BootMem.h"
#include "Memory/NBBS.h"

static volatile uint8_t *tree = 0;
static volatile uint32_t *index = 0;

static volatile uint64_t tree_size = 0; /* bytes */
static volatile uint64_t index_size = 0; /* bytes */

/*
* Maximum allowed alloc size is: 2^(max_level - 1) * min_size
* Naturally max_allowed <= total_memory
*/
static volatile uint32_t max_level = 10;

static volatile uint64_t min_size = PAGE_SIZE;
static volatile uint64_t max_size = 0;

int nb_init(uint64_t base_addr, uint64_t size)
{
if (base_addr == 0 | size == 0) {
return 1;
}

/* Calculate required tree size */
uint32_t total_nodes = 1; // we start at idx 1
for (uint32_t i = 0; i < max_level; i++) {
total_nodes += (size / ((0x1 << i) * min_size));
}

/* Calculate required index size */
uint32_t total_elems = (size / min_size);

tree_size = total_nodes * 1; // each node is 1 byte
index_size = total_elems * 4; // each elem is 4 byte

/* Allocate */
uint32_t req_pages = (tree_size + PAGE_SIZE - 1) / PAGE_SIZE;
tree = (uint8_t*) bootmem_alloc(req_pages);

if (!tree) {
return 1;
}

req_pages = (index_size + PAGE_SIZE - 1) / PAGE_SIZE;
index = (uint32_t*) bootmem_alloc(req_pages);

if (!index) {
return 1;
}

/* Initialize */
memset((void*) tree, 0x0, tree_size);
memset((void*) index, 0x0, index_size);

return 0;
}

void* nb_alloc(uint64_t size)
{

}

void* nb_free(void *addr)
{

}


#endif /* NBBS_H */
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ SRCS = \
Kernel/Library/LibKern/Time.c \
Kernel/Memory/BootMem.c \
Kernel/Memory/Physical.c \
Kernel/Memory/NBBS.c \
Kernel/Memory/Virtual.c
OBJS = ${SRCS:.c=.o}

Expand Down Expand Up @@ -165,7 +166,7 @@ debug:

compiledb:
@echo "COMPILEDB -n make all"
@python3 -m compiledb -n make all
@compiledb -n make all
@echo "COMPILEDB -n make all ${GREEN}ok${NC}"

all:
Expand Down

0 comments on commit 44dbdc8

Please sign in to comment.