-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pmm: Added NBBS definitions and init() function. WIP.
Signed-off-by: TunaCici <[email protected]>
- Loading branch information
Showing
4 changed files
with
186 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters