Various cleaning

This commit is contained in:
Mathieu Maret 2021-01-25 20:05:38 +01:00
parent ede53ae4f9
commit 5ab68d8197
5 changed files with 92 additions and 35 deletions

View File

@ -14,9 +14,12 @@
// Slab will contains object from sizeof(void *) to PAGE_SIZE/2 by pow2 // Slab will contains object from sizeof(void *) to PAGE_SIZE/2 by pow2
static struct slabDesc *slub; static struct slabDesc *slub;
int allocSlab(struct slabDesc **desc, size_t sizeEl, size_t sizeSlab, int self_containing); static int allocSlab(struct slabDesc **desc, size_t sizeEl, size_t sizeSlab,
int allocSlabEntry(struct slabEntry **desc, size_t sizeEl, size_t sizeSlab, int selfContained); int self_containing);
static int allocSlabEntry(struct slabEntry **desc, size_t sizeEl, size_t sizeSlab,
int selfContained);
static int formatPage(struct slabEntry *desc, size_t size, size_t sizeSlab, int selfContained); static int formatPage(struct slabEntry *desc, size_t size, size_t sizeSlab, int selfContained);
static int freeFromSlab(void *ptr, struct slabEntry *slab);
static struct { static struct {
size_t elementSize; size_t elementSize;
@ -44,26 +47,30 @@ int allocSetup(void)
for (uint i = 0; initSlab[i].elementSize != 0; i++) { for (uint i = 0; initSlab[i].elementSize != 0; i++) {
int ret; int ret;
if ((ret = allocBookSlab(initSlab[i].elementSize, initSlab[i].slabSize, if ((ret = allocBookSlab(initSlab[i].elementSize, initSlab[i].slabSize,
initSlab[i].isSelf))) { initSlab[i].isSelf))) {
if (ret == -EEXIST) if (ret == -EEXIST)
continue; continue;
pr_devel("Fail to allocBookSlab %d for %d \n", ret, (1U << i)); pr_devel("Fail to allocBookSlab %d for %d \n", ret, (1U << i));
return ret; return ret;
} }
} }
return 0; return 0;
} }
int allocBookSlab(size_t sizeEl, size_t sizeSlab, int selfContained) int allocBookSlab(size_t sizeEl, size_t sizeSlab, int selfContained)
{ {
pr_devel("%s for element of size %d is self %d\n", __func__, sizeEl, selfContained);
struct slabDesc *slab = NULL; struct slabDesc *slab = NULL;
struct slabDesc *newSlab = NULL; struct slabDesc *newSlab = NULL;
int slabIdx; int slabIdx;
int ret; int ret;
int flags; int flags;
pr_devel("%s for element of size %d is self %d\n", __func__, sizeEl, selfContained);
disable_IRQs(flags); disable_IRQs(flags);
list_foreach(slub, slab, slabIdx) list_foreach(slub, slab, slabIdx)
{ {
@ -88,14 +95,16 @@ int allocBookSlab(size_t sizeEl, size_t sizeSlab, int selfContained)
} }
restore_IRQs(flags); restore_IRQs(flags);
return 0; return 0;
} }
int allocSlab(struct slabDesc **desc, size_t size, size_t sizeSlab, int selfContained) static int allocSlab(struct slabDesc **desc, size_t size, size_t sizeSlab, int selfContained)
{ {
uint nbPage, i; uint nbPage, i;
pr_devel("%s for size %d is self %d\n", __func__, size, selfContained); pr_devel("%s for size %d is self %d\n", __func__, size, selfContained);
sizeSlab = MAX(sizeSlab, PAGE_SIZE); sizeSlab = MAX(sizeSlab, PAGE_SIZE);
if (size > sizeSlab) { if (size > sizeSlab) {
pr_devel("size of element %d are bigger than slab size %d\n", size, sizeSlab); pr_devel("size of element %d are bigger than slab size %d\n", size, sizeSlab);
@ -106,6 +115,7 @@ int allocSlab(struct slabDesc **desc, size_t size, size_t sizeSlab, int selfCont
paddr_t alloc = allocPhyPage(nbPage); paddr_t alloc = allocPhyPage(nbPage);
if (alloc == (paddr_t)NULL) if (alloc == (paddr_t)NULL)
return -ENOMEM; return -ENOMEM;
for (i = 0; i < nbPage; i++) { for (i = 0; i < nbPage; i++) {
if (pageMap((vaddr_t)alloc + i * PAGE_SIZE, alloc + i * PAGE_SIZE, PAGING_MEM_WRITE)) if (pageMap((vaddr_t)alloc + i * PAGE_SIZE, alloc + i * PAGE_SIZE, PAGING_MEM_WRITE))
goto free_page; goto free_page;
@ -118,27 +128,31 @@ int allocSlab(struct slabDesc **desc, size_t size, size_t sizeSlab, int selfCont
*desc = malloc(sizeof(struct slabDesc)); *desc = malloc(sizeof(struct slabDesc));
(*desc)->slab.freeEl = (void *)alloc; (*desc)->slab.freeEl = (void *)alloc;
} }
struct slabEntry *slab = &(*desc)->slab; struct slabEntry *slab = &(*desc)->slab;
list_singleton(slab, slab); list_singleton(slab, slab);
slab->page = (vaddr_t)alloc; slab->page = (vaddr_t)alloc;
slab->full = 0; slab->full = 0;
slab->size = sizeSlab; slab->size = sizeSlab;
(*desc)->size = size; (*desc)->size = size;
// pr_devel("got page %d for size %d first %d", alloc, size, (*desc)->slab.freeEl);
return formatPage(&(*desc)->slab, size, sizeSlab, selfContained); return formatPage(&(*desc)->slab, size, sizeSlab, selfContained);
free_page: free_page:
for (uint j = 0; j < i; j++) { for (uint j = 0; j < i; j++) {
pageUnmap((vaddr_t)alloc + i * PAGE_SIZE); pageUnmap((vaddr_t)alloc + i * PAGE_SIZE);
} }
return -ENOMEM; return -ENOMEM;
} }
int allocSlabEntry(struct slabEntry **desc, size_t size, size_t sizeSlab, int selfContained) static int allocSlabEntry(struct slabEntry **desc, size_t size, size_t sizeSlab,
int selfContained)
{ {
uint nbPage, i; uint nbPage, i;
pr_devel("%s for size %d is self %d\n", __func__, size, selfContained); pr_devel("%s for size %d is self %d\n", __func__, size, selfContained);
sizeSlab = MAX(sizeSlab, PAGE_SIZE); sizeSlab = MAX(sizeSlab, PAGE_SIZE);
if (size > sizeSlab) { if (size > sizeSlab) {
pr_devel("size of element %d are bigger than slab size %d\n", size, sizeSlab); pr_devel("size of element %d are bigger than slab size %d\n", size, sizeSlab);
@ -149,6 +163,7 @@ int allocSlabEntry(struct slabEntry **desc, size_t size, size_t sizeSlab, int se
paddr_t alloc = allocPhyPage(nbPage); paddr_t alloc = allocPhyPage(nbPage);
if (alloc == (paddr_t)NULL) if (alloc == (paddr_t)NULL)
return -ENOMEM; return -ENOMEM;
for (i = 0; i < nbPage; i++) { for (i = 0; i < nbPage; i++) {
if (pageMap((vaddr_t)alloc + i * PAGE_SIZE, alloc + i * PAGE_SIZE, PAGING_MEM_WRITE)) if (pageMap((vaddr_t)alloc + i * PAGE_SIZE, alloc + i * PAGE_SIZE, PAGING_MEM_WRITE))
goto free_page; goto free_page;
@ -161,17 +176,19 @@ int allocSlabEntry(struct slabEntry **desc, size_t size, size_t sizeSlab, int se
*desc = malloc(sizeof(struct slabEntry)); *desc = malloc(sizeof(struct slabEntry));
(*desc)->freeEl = (void *)alloc; (*desc)->freeEl = (void *)alloc;
} }
list_singleton(*desc, *desc); list_singleton(*desc, *desc);
(*desc)->page = (vaddr_t)alloc; (*desc)->page = (vaddr_t)alloc;
(*desc)->full = 0; (*desc)->full = 0;
(*desc)->size = sizeSlab; (*desc)->size = sizeSlab;
// pr_devel("got page %d for size %d first %d", alloc, size, (*desc)->freeEl);
return formatPage((*desc), size, sizeSlab, selfContained); return formatPage((*desc), size, sizeSlab, selfContained);
free_page: free_page:
for (uint j = 0; j < i; j++) { for (uint j = 0; j < i; j++) {
pageUnmap((vaddr_t)alloc + i * PAGE_SIZE); pageUnmap((vaddr_t)alloc + i * PAGE_SIZE);
} }
return -ENOMEM; return -ENOMEM;
} }
@ -179,36 +196,42 @@ static int formatPage(struct slabEntry *desc, size_t size, size_t sizeSlab, int
{ {
char *cur = desc->freeEl; char *cur = desc->freeEl;
ulong nbEl = sizeSlab / size - 1; ulong nbEl = sizeSlab / size - 1;
if (selfContained) if (selfContained)
nbEl = (sizeSlab - sizeof(struct slabDesc)) / size - 1; nbEl = (sizeSlab - sizeof(struct slabDesc)) / size - 1;
ulong i;
for (i = 0; i < nbEl; i++) { for (ulong i = 0; i < nbEl; i++) {
*((vaddr_t *)cur) = (vaddr_t)cur + size; *((vaddr_t *)cur) = (vaddr_t)cur + size;
cur += size; cur += size;
} }
*((vaddr_t *)cur) = (vaddr_t)NULL; *((vaddr_t *)cur) = (vaddr_t)NULL;
// pr_devel("last at %d allocated %d\n", cur, i + 1);
return 0; return 0;
} }
static void *allocFromSlab(struct slabEntry *slab) static void *allocFromSlab(struct slabEntry *slab)
{ {
vaddr_t *next = slab->freeEl; vaddr_t *next = slab->freeEl;
if (*next == (vaddr_t)NULL) { if (*next == (vaddr_t)NULL) {
pr_devel("Slab @%d is now full\n", slab); pr_devel("Slab @%d is now full\n", slab);
slab->full = 1; slab->full = 1;
} else { } else {
slab->freeEl = (void *)(*next); slab->freeEl = (void *)(*next);
} }
return (void *)next; return (void *)next;
} }
void *malloc(size_t size) void *malloc(size_t size)
{ {
struct slabEntry *slabEntry;
struct slabDesc *slab = NULL; struct slabDesc *slab = NULL;
uint slubIdx; uint slubIdx;
void *ret; void *ret;
int flags; int flags;
int slabIdx;
disable_IRQs(flags); disable_IRQs(flags);
@ -223,8 +246,6 @@ void *malloc(size_t size)
return NULL; return NULL;
} }
struct slabEntry *slabEntry;
int slabIdx;
list_foreach(&slab->slab, slabEntry, slabIdx) list_foreach(&slab->slab, slabEntry, slabIdx)
{ {
if (!slabEntry->full) { if (!slabEntry->full) {
@ -241,40 +262,48 @@ void *malloc(size_t size)
struct slabEntry *slabList = &slab->slab; struct slabEntry *slabList = &slab->slab;
size_t slabSize = MAX(PAGE_SIZE, size); size_t slabSize = MAX(PAGE_SIZE, size);
int retSlab; int retSlab;
if ((retSlab = allocSlabEntry(&newSlabEntry, slab->size, slabSize, if ((retSlab = allocSlabEntry(&newSlabEntry, slab->size, slabSize,
IS_SELF_CONTAINED(&slab->slab)))) { IS_SELF_CONTAINED(&slab->slab)))) {
pr_devel("Fail to allocSlabEntry %d\n", retSlab); pr_devel("Fail to allocSlabEntry %d\n", retSlab);
restore_IRQs(flags); restore_IRQs(flags);
return NULL; return NULL;
} }
pr_devel("Allocate new slab for object of size %d\n", slab->size); pr_devel("Allocate new slab for object of size %d\n", slab->size);
list_add_tail(slabList, newSlabEntry); list_add_tail(slabList, newSlabEntry);
ret = allocFromSlab(newSlabEntry); ret = allocFromSlab(newSlabEntry);
restore_IRQs(flags); restore_IRQs(flags);
return ret; return ret;
} }
int freeFromSlab(void *ptr, struct slabEntry *slab) static int freeFromSlab(void *ptr, struct slabEntry *slab)
{ {
struct slabEntry *slabEntry; struct slabEntry *slabEntry;
int slabIdx; int slabIdx;
list_foreach(slab, slabEntry, slabIdx) list_foreach(slab, slabEntry, slabIdx)
{ {
if ((slabEntry->page <= (vaddr_t)ptr) && if ((slabEntry->page <= (vaddr_t)ptr) &&
((vaddr_t)ptr < (slabEntry->page + slabEntry->size))) { ((vaddr_t)ptr < (slabEntry->page + slabEntry->size))) {
// pr_devel("free place! was %d is now %d\n", slabEntry->freeEl, ptr);
if (slabEntry->full) { if (slabEntry->full) {
*((vaddr_t *)ptr) = (vaddr_t)NULL; *((vaddr_t *)ptr) = (vaddr_t)NULL;
} else { } else {
*((vaddr_t *)ptr) = (vaddr_t)slabEntry->freeEl; *((vaddr_t *)ptr) = (vaddr_t)slabEntry->freeEl;
} }
slabEntry->freeEl = ptr; slabEntry->freeEl = ptr;
slabEntry->full = 0; slabEntry->full = 0;
return 1; return 1;
} }
} }
return 0; return 0;
} }
void free(void *ptr) void free(void *ptr)
{ {
if (!ptr) if (!ptr)
@ -285,6 +314,7 @@ void free(void *ptr)
int flags; int flags;
disable_IRQs(flags); disable_IRQs(flags);
list_foreach(slub, slab, slabIdx) list_foreach(slub, slab, slabIdx)
{ {
struct slabEntry *slabEntry; struct slabEntry *slabEntry;
@ -297,6 +327,7 @@ void free(void *ptr)
} }
} }
} }
restore_IRQs(flags); restore_IRQs(flags);
pr_devel("free: slab not found\n"); pr_devel("free: slab not found\n");
} }

View File

@ -2,6 +2,22 @@
#include "paging.h" #include "paging.h"
#include "stdarg.h" #include "stdarg.h"
/*
* Initialize malloc system
*/
int allocSetup(void);
/*
* Allow malloc to allocate elements of this precise size.
* Otherwise the allocation will be in the closest biggest pool.
* */
int allocBookSlab(size_t size, size_t sizeSlab, int selfContained);
void *malloc(size_t size);
void free(void *ptr);
/* Stuct definition shared for test purpose
*/
struct slabEntry { struct slabEntry {
vaddr_t page; vaddr_t page;
size_t size; size_t size;
@ -17,8 +33,3 @@ struct slabDesc {
struct slabDesc *next; struct slabDesc *next;
struct slabDesc *prev; struct slabDesc *prev;
}; };
int allocSetup(void);
int allocBookSlab(size_t size, size_t sizeSlab, int selfContained);
void *malloc(size_t size);
void free(void *ptr);

View File

@ -13,17 +13,23 @@ static struct kthread *threadWithTimeout;
void kthreadExit() void kthreadExit()
{ {
uint32_t flags; uint32_t flags;
disable_IRQs(flags); disable_IRQs(flags);
struct kthread *current = currentThread; struct kthread *current = currentThread;
struct kthread *next = kthreadSelectNext(); struct kthread *next = kthreadSelectNext();
if (next == current) if (next == current)
assert("cannot exit thread"); assert("cannot exit thread");
currentThread->state = EXITING; currentThread->state = EXITING;
currentThread = next; currentThread = next;
currentThread->state = RUNNING; currentThread->state = RUNNING;
cpu_context_exit_to(next->cpuState, (cpu_kstate_function_arg1_t *)kthreadDelete, cpu_context_exit_to(next->cpuState, (cpu_kstate_function_arg1_t *)kthreadDelete,
(uint32_t)current); (uint32_t)current);
restore_IRQs(flags); restore_IRQs(flags);
return; return;
} }

View File

@ -68,14 +68,18 @@ void VGAPrintf(uint color, uint bgColor, int startX, int startY, const char *for
} }
va_end(ap); va_end(ap);
restore_IRQs(flags); restore_IRQs(flags);
} }
static void printCharDetails(const char str, uint color, uint bgColor, int startX, int startY) static void printCharDetails(const char str, uint color, uint bgColor, int startX, int startY)
{ {
int pos = VGA_WIDTH * startY + startX;
if (pos > VGA_WIDTH * VGA_HEIGHT || pos < 0)
return;
long int colorAttr = (bgColor << 4 | (color & 0x0f)) << 8; long int colorAttr = (bgColor << 4 | (color & 0x0f)) << 8;
vga[VGA_WIDTH * startY + startX] = colorAttr | str; vga[pos] = colorAttr | str;
} }
void VGAScrollUp(void) void VGAScrollUp(void)
@ -84,12 +88,13 @@ void VGAScrollUp(void)
int flags; int flags;
disable_IRQs(flags); disable_IRQs(flags);
for (int i = 1; i < VGA_HEIGHT - 1; i++) { // last line is status line. Do not scroll it for (int i = 1; i < VGA_HEIGHT - VGA_STATUS_LINE_HEIGHT;
i++) { // last line is status line. Do not scroll it
memcpy((void *)&vga[VGA_WIDTH * (i - 1)], (void *)&vga[VGA_WIDTH * i], memcpy((void *)&vga[VGA_WIDTH * (i - 1)], (void *)&vga[VGA_WIDTH * i],
VGA_WIDTH * sizeof(short)); VGA_WIDTH * sizeof(short));
} }
for (int i = 0; i < VGA_WIDTH; i++) { for (int i = 0; i < VGA_WIDTH; i++) {
vga[(VGA_HEIGHT - 2) * VGA_WIDTH + i] = colorAttr; vga[(VGA_HEIGHT - 1 - VGA_STATUS_LINE_HEIGHT) * VGA_WIDTH + i] = colorAttr;
} }
restore_IRQs(flags); restore_IRQs(flags);
} }
@ -102,7 +107,7 @@ void VGAPutc(const char str)
if (str == '\n') { if (str == '\n') {
line++; line++;
col = 0; col = 0;
if (line >= VGA_HEIGHT - 1) { if (line >= VGA_HEIGHT - VGA_STATUS_LINE_HEIGHT) {
VGAScrollUp(); VGAScrollUp();
line--; line--;
} }
@ -113,6 +118,8 @@ void VGAPutc(const char str)
if (col < 0) { if (col < 0) {
col = VGA_WIDTH - 1; col = VGA_WIDTH - 1;
line--; line--;
if (line < 0)
line = 0;
} }
printCharDetails(' ', vgaColor, vgaBgColor, col, line); printCharDetails(' ', vgaColor, vgaBgColor, col, line);
} else { } else {
@ -121,7 +128,7 @@ void VGAPutc(const char str)
col = 0; col = 0;
line++; line++;
} }
if (line >= VGA_HEIGHT - 1) { if (line >= VGA_HEIGHT - VGA_STATUS_LINE_HEIGHT) {
VGAScrollUp(); VGAScrollUp();
line--; line--;
} }

View File

@ -17,6 +17,8 @@
#define VGA_WIDTH 80 #define VGA_WIDTH 80
#define VGA_HEIGHT 25 #define VGA_HEIGHT 25
#define VGA_STATUS_LINE_HEIGHT 1
int VGASetup(uint bgColor, uint color); int VGASetup(uint bgColor, uint color);
void VGAPutc(const char str); void VGAPutc(const char str);
void VGAPrintf(uint color, uint bgColor, int startX, int startY, const char *format, ...); void VGAPrintf(uint color, uint bgColor, int startX, int startY, const char *format, ...);