matos/core/mem.c

169 lines
3.8 KiB
C
Raw Normal View History

2021-01-23 21:51:00 +01:00
#include "assert.h"
2018-08-06 18:41:45 +02:00
#include "mem.h"
2020-08-28 22:38:05 +02:00
#include "kernel.h"
#include "klibc.h"
2018-08-09 22:19:34 +02:00
#include "list.h"
#include "types.h"
2018-08-06 18:41:45 +02:00
2021-01-22 22:59:45 +01:00
static struct memDesc *pageDesc = (struct memDesc *)&__ld_kernel_end;
static struct memDesc *freePage;
static struct memDesc *usedPage;
2021-01-23 21:51:00 +01:00
paddr_t pageDescEnd;
paddr_t upperMem;
2018-08-09 22:19:34 +02:00
static unsigned long allocatedPage = 0;
2021-01-23 00:42:09 +01:00
int memSetup(paddr_t upperMemKB, paddr_t *lastMemUsedOut)
2018-08-06 18:41:45 +02:00
{
2021-01-22 22:59:45 +01:00
list_init(freePage);
list_init(usedPage);
// Align upper mem (in kB) on page size even if it does loose a page
2021-01-23 21:51:00 +01:00
upperMemKB = ALIGN_DOWN(upperMemKB, PAGE_SIZE / 1024);
2021-01-23 00:42:09 +01:00
unsigned long nbPage = ((upperMemKB) / (PAGE_SIZE / 1024));
2021-01-22 22:59:45 +01:00
2021-01-23 21:21:13 +01:00
printf("Available Mem from 0x%x to 0x%x: %dMB in %d Pages of %dB\n", &__ld_kernel_end,
2021-01-23 00:42:09 +01:00
upperMemKB * 1024, (upperMemKB * 1024 - (uint32_t)&__ld_kernel_end) / (1024 * 1024),
2021-01-22 22:59:45 +01:00
nbPage, PAGE_SIZE);
// Memory description is stored after the kernel. We need some place to store it
2021-01-23 21:51:00 +01:00
pageDescEnd = (unsigned long)pageDesc + nbPage * sizeof(struct memDesc);
*lastMemUsedOut = ALIGN(pageDescEnd, PAGE_SIZE);
upperMem = upperMemKB * 1024;
2021-01-22 22:59:45 +01:00
2021-01-23 00:42:09 +01:00
memAddBank(0, *lastMemUsedOut, 0);
return 0;
}
int memAddBank(paddr_t bottomMem, paddr_t topMem, int isFree)
{
2021-01-23 21:51:00 +01:00
topMem = min(topMem, upperMem);
2021-01-23 00:42:09 +01:00
for (uint i = (bottomMem >> PAGE_SHIFT); i < (topMem >> PAGE_SHIFT); i++) {
2021-01-22 22:59:45 +01:00
struct memDesc *mem = &pageDesc[i];
2021-01-23 00:42:09 +01:00
if (isFree) {
mem->ref = 0;
2021-01-22 22:59:45 +01:00
list_add_tail(freePage, mem);
2021-01-23 00:42:09 +01:00
} else {
mem->ref = 1;
list_add_tail(usedPage, mem);
}
mem->phy_addr = i * PAGE_SIZE;
}
2021-01-23 00:42:09 +01:00
return 0;
2018-08-06 18:41:45 +02:00
}
2018-08-09 22:19:34 +02:00
2021-01-23 21:51:00 +01:00
void memGetStat(uint *free, uint *used)
{
uint idx;
struct memDesc *mem;
list_foreach(freePage, mem, idx)
{
continue;
}
*free = idx;
list_foreach(usedPage, mem, idx)
{
continue;
}
*used = idx;
}
2021-01-22 22:59:45 +01:00
struct memDesc *addr2memDesc(paddr_t addr)
2018-08-09 22:19:34 +02:00
{
int idx = addr >> PAGE_SHIFT;
2021-01-22 22:59:45 +01:00
return pageDesc + idx;
2018-08-09 22:19:34 +02:00
}
2021-01-22 22:59:45 +01:00
struct memDesc *memFindConsecutiveFreePage(uint nbPage)
2018-08-09 22:19:34 +02:00
{
2021-01-22 22:59:45 +01:00
struct memDesc *mem, *head;
2020-08-28 22:38:05 +02:00
uint memIdx, count;
2021-01-22 22:59:45 +01:00
if (list_is_empty(freePage)) {
2020-08-28 22:38:05 +02:00
return NULL;
}
count = 1;
memIdx = 0;
2021-01-22 22:59:45 +01:00
head = freePage;
mem = freePage;
2020-08-28 22:38:05 +02:00
2021-01-22 22:59:45 +01:00
while (count < nbPage && (!memIdx || mem != freePage)) {
2020-08-28 22:38:05 +02:00
memIdx++;
mem = mem->next;
if (mem->phy_addr == head->phy_addr + count * PAGE_SIZE) {
count++;
} else {
count = 1;
head = mem;
}
}
if (count < nbPage) {
return NULL;
}
return head;
}
paddr_t allocPhyPage(uint nbPage)
{
2021-01-22 22:59:45 +01:00
struct memDesc *mem, *head, *next;
2020-08-28 22:38:05 +02:00
uint count;
head = memFindConsecutiveFreePage(nbPage);
if (head == NULL) {
pr_devel("Cannot find %d consecutive page\n", nbPage);
return (unsigned long)NULL;
}
2020-08-28 22:38:05 +02:00
mem = head;
next = head->next;
for (count = 0; count < nbPage; count++) {
2021-01-22 22:59:45 +01:00
list_delete(freePage, mem);
2020-08-28 22:38:05 +02:00
mem->ref = 1;
2021-01-22 22:59:45 +01:00
list_add_tail(usedPage, mem);
2020-08-28 22:38:05 +02:00
mem = next;
next = mem->next;
}
allocatedPage += nbPage;
return head->phy_addr;
2018-08-09 22:19:34 +02:00
}
int unrefPhyPage(paddr_t addr)
2018-08-09 22:19:34 +02:00
{
2021-01-22 22:59:45 +01:00
struct memDesc *mem = addr2memDesc(addr);
if (!mem) {
return -1;
}
mem->ref--;
if (mem->ref == 0) {
allocatedPage--;
2021-01-22 22:59:45 +01:00
list_delete(usedPage, mem);
list_add_tail(freePage, mem); // TODO find the right place to keep free_page sorted;
}
2018-08-09 22:19:34 +02:00
return mem->ref;
2018-08-09 22:19:34 +02:00
}
2018-11-09 17:07:39 +01:00
int refPhyPage(paddr_t addr)
2018-11-09 17:07:39 +01:00
{
2021-01-22 22:59:45 +01:00
struct memDesc *mem = addr2memDesc(addr);
if (!mem) {
return -1;
}
mem->ref++;
if (mem->ref == 1) {
allocatedPage++;
2021-01-22 22:59:45 +01:00
list_add_tail(usedPage, mem);
list_delete(freePage, mem);
}
2018-11-09 17:07:39 +01:00
return 0;
2018-11-09 17:07:39 +01:00
}
unsigned long getNbAllocatedPage(void)
{
return allocatedPage;
}