2014-02-12 21:47:13 +00:00
|
|
|
/*
|
2023-09-10 00:18:08 +00:00
|
|
|
* Copyright (c) 2013-2023 Ali Mashtizadeh
|
2014-02-12 21:47:13 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2014-07-17 00:21:18 +00:00
|
|
|
#include <stdbool.h>
|
2014-02-12 21:47:13 +00:00
|
|
|
#include <stdint.h>
|
2014-07-17 00:21:18 +00:00
|
|
|
#include <stdarg.h>
|
2014-12-09 06:37:40 +00:00
|
|
|
#include <string.h>
|
2014-02-12 21:47:13 +00:00
|
|
|
|
2014-07-22 06:43:01 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include <sys/kassert.h>
|
|
|
|
#include <sys/kdebug.h>
|
|
|
|
#include <sys/kmem.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/spinlock.h>
|
|
|
|
|
|
|
|
// PGSIZE
|
2023-10-23 20:27:55 +00:00
|
|
|
#include <machine/cpu.h>
|
2014-12-09 06:37:40 +00:00
|
|
|
#include <machine/pmap.h>
|
2014-02-12 21:47:13 +00:00
|
|
|
|
2014-06-30 22:52:35 +00:00
|
|
|
/* 'FREEPAGE' */
|
|
|
|
#define FREEPAGE_MAGIC_FREE 0x4652454550414745ULL
|
|
|
|
/* 'ALLOCATE' */
|
|
|
|
#define FREEPAGE_MAGIC_INUSE 0x414c4c4f43415445ULL
|
|
|
|
|
2014-07-17 00:21:18 +00:00
|
|
|
Spinlock pallocLock;
|
|
|
|
uint64_t totalPages;
|
|
|
|
uint64_t freePages;
|
|
|
|
|
2014-02-12 21:47:13 +00:00
|
|
|
typedef struct FreePage
|
|
|
|
{
|
2014-06-30 22:52:35 +00:00
|
|
|
uint64_t magic;
|
|
|
|
LIST_ENTRY(FreePage) entries;
|
2014-02-12 21:47:13 +00:00
|
|
|
} FreePage;
|
|
|
|
|
2014-12-09 06:37:40 +00:00
|
|
|
typedef struct PageInfo
|
|
|
|
{
|
|
|
|
uint64_t refCount;
|
|
|
|
} PageInfo;
|
|
|
|
|
|
|
|
XMem *pageInfoXMem;
|
|
|
|
PageInfo *pageInfoTable;
|
|
|
|
uint64_t pageInfoLength;
|
2014-06-30 22:52:35 +00:00
|
|
|
LIST_HEAD(FreeListHead, FreePage) freeList;
|
2014-02-12 21:47:13 +00:00
|
|
|
|
2014-12-09 06:37:40 +00:00
|
|
|
/*
|
|
|
|
* Initializes the page allocator
|
|
|
|
*/
|
2014-06-30 22:52:35 +00:00
|
|
|
void
|
|
|
|
PAlloc_Init()
|
2014-02-12 21:47:13 +00:00
|
|
|
{
|
2014-07-17 00:21:18 +00:00
|
|
|
totalPages = 0;
|
|
|
|
freePages = 0;
|
|
|
|
|
2015-01-22 22:11:26 +00:00
|
|
|
Spinlock_Init(&pallocLock, "PAlloc Lock", SPINLOCK_TYPE_NORMAL);
|
2014-07-17 00:21:18 +00:00
|
|
|
|
2014-06-30 22:52:35 +00:00
|
|
|
LIST_INIT(&freeList);
|
2014-12-09 06:37:40 +00:00
|
|
|
pageInfoXMem = NULL;
|
|
|
|
pageInfoTable = NULL;
|
|
|
|
}
|
|
|
|
|
2023-09-10 00:18:08 +00:00
|
|
|
/**
|
|
|
|
* PAlloc_LateInit --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* The late init call is made after the page tables are initialized using a
|
|
|
|
* small boot memory region (2nd 16MBs). This is where initialize the XMem
|
|
|
|
* region that represents the PageInfo array, and map memory into it.
|
2014-12-09 06:37:40 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
PAlloc_LateInit()
|
|
|
|
{
|
|
|
|
void *pageInfoOld = pageInfoTable;
|
|
|
|
|
|
|
|
pageInfoXMem = XMem_New();
|
|
|
|
if (!XMem_Allocate(pageInfoXMem, pageInfoLength)) {
|
|
|
|
Panic("Cannot back pageInfoTable!");
|
|
|
|
}
|
|
|
|
|
|
|
|
pageInfoTable = (PageInfo *)XMem_GetBase(pageInfoXMem);
|
|
|
|
memcpy(pageInfoTable, pageInfoOld, pageInfoLength);
|
|
|
|
|
|
|
|
// Free old pages
|
2014-02-12 21:47:13 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 00:18:08 +00:00
|
|
|
/**
|
|
|
|
* PAlloc_AddRegion --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Add a physical memory region to the page allocator.
|
2023-09-10 00:18:08 +00:00
|
|
|
*/
|
2014-06-30 22:52:35 +00:00
|
|
|
void
|
|
|
|
PAlloc_AddRegion(uintptr_t start, uintptr_t len)
|
2014-02-12 21:47:13 +00:00
|
|
|
{
|
|
|
|
uintptr_t i;
|
|
|
|
FreePage *pg;
|
|
|
|
|
|
|
|
if ((start % PGSIZE) != 0)
|
2014-07-17 00:21:18 +00:00
|
|
|
Panic("Region start is not page aligned!");
|
2014-02-12 21:47:13 +00:00
|
|
|
if ((len % PGSIZE) != 0)
|
2014-07-17 00:21:18 +00:00
|
|
|
Panic("Region length is not page aligned!");
|
2014-02-12 21:47:13 +00:00
|
|
|
|
2014-12-09 06:37:40 +00:00
|
|
|
/*
|
|
|
|
* PageInfo table isn't initialized on the first call to this function. We
|
|
|
|
* must allocate a temporary table that will be copied into the XMem region
|
|
|
|
* inside PAlloc_LateInit.
|
2014-12-09 06:47:57 +00:00
|
|
|
*
|
|
|
|
* Note that the PageInfo table is invalid for regions that are not added
|
|
|
|
* to the free list such as MMIO regions.
|
2014-12-09 06:37:40 +00:00
|
|
|
*/
|
|
|
|
if (pageInfoTable == NULL) {
|
|
|
|
// Physical Address Offsets
|
|
|
|
uintptr_t base = (uintptr_t)DMVA2PA(start);
|
|
|
|
uintptr_t end = base + len;
|
|
|
|
|
|
|
|
pageInfoLength = ROUNDUP(end / PGSIZE * sizeof(PageInfo), PGSIZE);
|
|
|
|
pageInfoTable = (PageInfo *)start;
|
|
|
|
|
|
|
|
start += pageInfoLength;
|
|
|
|
len -= pageInfoLength;
|
|
|
|
|
|
|
|
for (i = 0; i < (base / PGSIZE); i++) {
|
|
|
|
pageInfoTable[i].refCount = 1;
|
|
|
|
}
|
|
|
|
for (i = (base / PGSIZE); i < (end / PGSIZE); i++) {
|
|
|
|
pageInfoTable[i].refCount = 0;
|
|
|
|
}
|
|
|
|
for (i = 0; i < (pageInfoLength / PGSIZE); i++) {
|
|
|
|
pageInfoTable[i + (base / PGSIZE)].refCount = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Only the first call to AddRegion should occur before the XMem region
|
|
|
|
* is initialized.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ASSERT(pageInfoXMem != NULL);
|
|
|
|
|
|
|
|
uintptr_t base = (uintptr_t)DMVA2PA(start);
|
|
|
|
uintptr_t end = base + len;
|
|
|
|
|
|
|
|
uintptr_t newLength = ROUNDUP(end / PGSIZE * sizeof(PageInfo), PGSIZE);
|
|
|
|
|
|
|
|
if (!XMem_Allocate(pageInfoXMem, newLength))
|
|
|
|
Panic("Cannot allocate XMem region!");
|
|
|
|
|
|
|
|
// Initialize new pages
|
|
|
|
for (i = (base / PGSIZE); i < (end / PGSIZE); i++) {
|
|
|
|
pageInfoTable[i].refCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 00:21:18 +00:00
|
|
|
Spinlock_Lock(&pallocLock);
|
2014-02-12 21:47:13 +00:00
|
|
|
for (i = 0; i < len; i += PGSIZE)
|
|
|
|
{
|
2014-07-17 00:21:18 +00:00
|
|
|
pg = (void *)(start + i);
|
2014-06-30 22:52:35 +00:00
|
|
|
pg->magic = FREEPAGE_MAGIC_FREE;
|
2014-07-17 00:21:18 +00:00
|
|
|
|
|
|
|
totalPages++;
|
|
|
|
freePages++;
|
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&freeList, pg, entries);
|
2014-02-12 21:47:13 +00:00
|
|
|
}
|
2014-07-17 00:21:18 +00:00
|
|
|
Spinlock_Unlock(&pallocLock);
|
2014-02-12 21:47:13 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 00:18:08 +00:00
|
|
|
/**
|
|
|
|
* PAllocGetInfo --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Lookup the PageInfo structure for a given physical address.
|
2023-09-10 00:18:08 +00:00
|
|
|
*/
|
2014-12-09 06:37:40 +00:00
|
|
|
static inline PageInfo *
|
|
|
|
PAllocGetInfo(void *pg)
|
|
|
|
{
|
|
|
|
uintptr_t entry = (uintptr_t)DMVA2PA(pg) / PGSIZE;
|
|
|
|
return &pageInfoTable[entry];
|
|
|
|
}
|
|
|
|
|
2023-09-10 00:18:08 +00:00
|
|
|
/**
|
|
|
|
* PAlloc_AllocPage --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Allocate a physical page and return the page's address in the Kernel's ident
|
|
|
|
* mapped memory region.
|
2023-09-10 00:18:08 +00:00
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* @retval NULL if no memory is available.
|
|
|
|
* @return Newly allocated physical page.
|
2023-09-10 00:18:08 +00:00
|
|
|
*/
|
2014-06-30 22:52:35 +00:00
|
|
|
void *
|
|
|
|
PAlloc_AllocPage()
|
2014-02-12 21:47:13 +00:00
|
|
|
{
|
2014-12-09 06:37:40 +00:00
|
|
|
PageInfo *info;
|
2014-07-17 00:21:18 +00:00
|
|
|
FreePage *pg;
|
|
|
|
|
|
|
|
Spinlock_Lock(&pallocLock);
|
|
|
|
pg = LIST_FIRST(&freeList);
|
2023-09-03 18:56:44 +00:00
|
|
|
ASSERT(pg != NULL);
|
2014-06-30 22:52:35 +00:00
|
|
|
LIST_REMOVE(pg, entries);
|
2014-02-18 18:48:35 +00:00
|
|
|
|
2023-09-03 18:56:44 +00:00
|
|
|
ASSERT(pg->magic == FREEPAGE_MAGIC_FREE);
|
|
|
|
|
2014-12-09 06:37:40 +00:00
|
|
|
info = PAllocGetInfo(pg);
|
2023-09-03 18:56:44 +00:00
|
|
|
ASSERT(info != NULL);
|
2014-12-09 06:37:40 +00:00
|
|
|
ASSERT(info->refCount == 0);
|
|
|
|
info->refCount++;
|
|
|
|
|
2014-06-30 22:52:35 +00:00
|
|
|
pg->magic = FREEPAGE_MAGIC_INUSE;
|
2014-02-18 18:48:35 +00:00
|
|
|
|
2014-07-17 00:21:18 +00:00
|
|
|
freePages--;
|
|
|
|
Spinlock_Unlock(&pallocLock);
|
|
|
|
|
2023-09-04 20:03:34 +00:00
|
|
|
memset(pg, 0, PGSIZE);
|
|
|
|
|
2014-02-18 18:48:35 +00:00
|
|
|
return (void *)pg;
|
2014-02-12 21:47:13 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 00:18:08 +00:00
|
|
|
/**
|
|
|
|
* PAllocFreePage --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Free a page.
|
2023-09-10 00:18:08 +00:00
|
|
|
*/
|
2015-02-16 21:23:21 +00:00
|
|
|
static void
|
2014-12-09 06:37:40 +00:00
|
|
|
PAllocFreePage(void *region)
|
2014-02-12 21:47:13 +00:00
|
|
|
{
|
2014-02-18 18:48:35 +00:00
|
|
|
FreePage *pg = (FreePage *)region;
|
2014-07-17 00:21:18 +00:00
|
|
|
|
2023-09-04 20:03:34 +00:00
|
|
|
ASSERT(((uintptr_t)region % PGSIZE) == 0);
|
|
|
|
|
2014-06-30 22:52:35 +00:00
|
|
|
LIST_INSERT_HEAD(&freeList, pg, entries);
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Application can write this magic, but for
|
|
|
|
// debug builds we can use this as a double free check.
|
|
|
|
ASSERT(pg->magic != FREEPAGE_MAGIC_FREE);
|
2014-12-09 06:37:40 +00:00
|
|
|
|
|
|
|
PageInfo *info = PAllocGetInfo(pg);
|
|
|
|
ASSERT(info->refCount == 0);
|
2014-06-30 22:52:35 +00:00
|
|
|
#endif
|
2023-09-04 20:03:34 +00:00
|
|
|
|
2014-06-30 22:52:35 +00:00
|
|
|
pg->magic = FREEPAGE_MAGIC_FREE;
|
2014-07-17 00:21:18 +00:00
|
|
|
freePages++;
|
2014-12-09 06:37:40 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 00:18:08 +00:00
|
|
|
/**
|
|
|
|
* PAlloc_Retain --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Increment the reference count for a physical page.
|
2023-09-10 00:18:08 +00:00
|
|
|
*/
|
2014-12-09 06:37:40 +00:00
|
|
|
void
|
|
|
|
PAlloc_Retain(void *pg)
|
|
|
|
{
|
|
|
|
PageInfo *info = PAllocGetInfo(pg);
|
|
|
|
|
|
|
|
Spinlock_Lock(&pallocLock);
|
|
|
|
ASSERT(info->refCount != 0);
|
|
|
|
info->refCount++;
|
|
|
|
Spinlock_Unlock(&pallocLock);
|
|
|
|
}
|
|
|
|
|
2023-09-10 00:18:08 +00:00
|
|
|
/**
|
|
|
|
* PAlloc_Release --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Deccrement the reference count for a physical page. If the reference count
|
|
|
|
* is zero the page will be freed.
|
2023-09-10 00:18:08 +00:00
|
|
|
*/
|
2014-12-09 06:37:40 +00:00
|
|
|
void
|
|
|
|
PAlloc_Release(void *pg)
|
|
|
|
{
|
|
|
|
PageInfo *info = PAllocGetInfo(pg);
|
2014-07-17 00:21:18 +00:00
|
|
|
|
2014-12-09 06:37:40 +00:00
|
|
|
Spinlock_Lock(&pallocLock);
|
2023-09-04 20:03:34 +00:00
|
|
|
ASSERT(info->refCount != 0);
|
2014-12-09 06:37:40 +00:00
|
|
|
info->refCount--;
|
|
|
|
if (info->refCount == 0)
|
|
|
|
PAllocFreePage(pg);
|
2014-07-17 00:21:18 +00:00
|
|
|
Spinlock_Unlock(&pallocLock);
|
|
|
|
}
|
|
|
|
|
2015-02-16 21:23:21 +00:00
|
|
|
static void
|
2014-07-17 00:21:18 +00:00
|
|
|
Debug_PAllocStats(int argc, const char *argv[])
|
|
|
|
{
|
2024-10-31 15:04:05 +00:00
|
|
|
kprintf("Total Pages: %lu\n", totalPages);
|
|
|
|
kprintf("Allocated Pages: %lu\n", totalPages - freePages);
|
|
|
|
kprintf("Free Pages: %lu\n", freePages);
|
2014-02-12 21:47:13 +00:00
|
|
|
}
|
|
|
|
|
2014-07-20 20:45:57 +00:00
|
|
|
REGISTER_DBGCMD(pallocstats, "Page allocator statistics", Debug_PAllocStats);
|
|
|
|
|
2015-02-16 21:23:21 +00:00
|
|
|
static void
|
2014-12-09 06:37:40 +00:00
|
|
|
Debug_PAllocDump(int argc, const char *argv[])
|
|
|
|
{
|
|
|
|
struct FreePage *it;
|
|
|
|
|
|
|
|
LIST_FOREACH(it, &freeList, entries) {
|
|
|
|
if (it->magic != FREEPAGE_MAGIC_FREE)
|
|
|
|
kprintf("Magic Corrupted! (%lx)\n", it->magic);
|
|
|
|
kprintf("Free %lx\n", (uintptr_t)it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_DBGCMD(pallocdump, "Dump page allocator's free list", Debug_PAllocDump);
|
|
|
|
|