mm: refactor page.c related names to snake case

This commit is contained in:
Matheus
2026-07-01 12:06:30 -03:00
parent 6f8700e678
commit 4d1caa6ede
4 changed files with 153 additions and 153 deletions
+3 -3
View File
@@ -238,7 +238,7 @@ static char *args[] = {
static spinlock_t lock;
DEFINE_KERNEL_ARGUMENT(global_syscall_logging, bool)
#endif
extern size_t freepagecount;
extern size_t mm_free_page_count;
__attribute__((no_caller_saved_registers)) void arch_syscall_log(int syscall, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6) {
#ifdef SYSCALL_LOGGING
char argbuff[768];
@@ -252,7 +252,7 @@ __attribute__((no_caller_saved_registers)) void arch_syscall_log(int syscall, ui
arch_cpu_user_access_begin();
snprintf(argbuff, 768, syscall < SYSCALL_COUNT ? args[syscall] : "N/A", a1, a2, a3, a4, a5, a6);
arch_cpu_user_access_end();
snprintf(printbuff, 1024, "\e[92msyscall: pid %d tid %d: %s: %s (%lu cached pages, %lu free pages)\n\e[0m", proc->pid, thread->tid, syscall < SYSCALL_COUNT ? name[syscall] : "invalid syscall", argbuff, vmmcache_cachedpages, freepagecount);
snprintf(printbuff, 1024, "\e[92msyscall: pid %d tid %d: %s: %s (%lu cached pages, %lu free pages)\n\e[0m", proc->pid, thread->tid, syscall < SYSCALL_COUNT ? name[syscall] : "invalid syscall", argbuff, vmmcache_cachedpages, mm_free_page_count);
arch_interrupt_disable();
spinlock_acquire(&lock);
@@ -271,7 +271,7 @@ __attribute__((no_caller_saved_registers)) void arch_syscall_log_return(uint64_t
if (!(proc->flags & PROC_FLAG_SYSTRACE) && GET_KERNEL_ARGUMENT(global_syscall_logging, bool) == false)
return;
snprintf(printbuff, 1024, "\e[94msyscall return: pid %d tid %d: %lu %s (%lu cached pages, %lu free pages)\n\e[0m", proc->pid, thread->tid, ret, strerror(errno), vmmcache_cachedpages, freepagecount);
snprintf(printbuff, 1024, "\e[94msyscall return: pid %d tid %d: %lu %s (%lu cached pages, %lu free pages)\n\e[0m", proc->pid, thread->tid, ret, strerror(errno), vmmcache_cachedpages, mm_free_page_count);
arch_interrupt_disable();
spinlock_acquire(&lock);
LOGSTR(printbuff);
+11 -11
View File
@@ -20,18 +20,18 @@
typedef struct page_t {
struct vnode_t *backing;
uintmax_t offset;
struct page_t *hashnext;
struct page_t *hashprev;
struct page_t *vnodenext;
struct page_t *vnodeprev;
struct page_t *hash_next;
struct page_t *hash_prev;
struct page_t *vnode_next;
struct page_t *vnode_prev;
union {
struct {
struct page_t *freenext;
struct page_t *freeprev;
struct page_t *free_next;
struct page_t *free_prev;
};
struct {
struct page_t *writenext;
struct page_t *writeprev;
struct page_t *write_next;
struct page_t *write_prev;
};
};
uintmax_t refcount;
@@ -49,9 +49,9 @@ void mm_release_range(void *addr, size_t size);
void mm_page_init();
void mm_get_page_statistics(size_t *total_pages, size_t *free_pages);
extern uintptr_t hhdmbase;
extern uintptr_t hhdm_base;
#define MAKE_HHDM(x) (void *)((uintptr_t)x + hhdmbase)
#define FROM_HHDM(x) (void *)((uintptr_t)x - hhdmbase)
#define MAKE_HHDM(x) (void *)((uintptr_t)x + hhdm_base)
#define FROM_HHDM(x) (void *)((uintptr_t)x - hhdm_base)
#endif
+98 -98
View File
@@ -8,21 +8,21 @@
#include <kernel/vmmcache.h>
#include <kernel/init.h>
uintptr_t hhdmbase;
static size_t memorysize;
static size_t pagecount;
size_t freepagecount;
uintptr_t hhdm_base;
static size_t memory_size;
static size_t page_count;
size_t mm_free_page_count;
static mutex_t freelistmutex;
static page_t *freelists[MEMORY_SECTION_COUNT];
static page_t *freetails[MEMORY_SECTION_COUNT];
static page_t *standbylists[MEMORY_SECTION_COUNT];
static page_t *standbytails[MEMORY_SECTION_COUNT];
static mutex_t free_list_mutex;
static page_t *free_lists[MEMORY_SECTION_COUNT];
static page_t *free_tails[MEMORY_SECTION_COUNT];
static page_t *standby_lists[MEMORY_SECTION_COUNT];
static page_t *standby_tails[MEMORY_SECTION_COUNT];
typedef struct {
uintmax_t baseid;
uintmax_t topid;
uintmax_t searchstart;
uintmax_t base_id;
uintmax_t top_id;
uintmax_t search_start;
} section_t;
#define TOP_1MB (0x100000 / PAGE_SIZE)
@@ -34,7 +34,7 @@ static section_t sections[MEMORY_SECTION_COUNT] = {
{TOP_4GB, 0xffffffffffffffffl, TOP_4GB}
};
static volatile struct limine_hhdm_request hhdmreq = {
static volatile struct limine_hhdm_request hhdm_req = {
.id = LIMINE_HHDM_REQUEST_ID,
.revision = 0
};
@@ -46,81 +46,81 @@ volatile struct limine_memmap_request mm_page_limine_map = {
static page_t* pages;
#define PAGE_GETID(page) (((uintptr_t)(page) - (uintptr_t)pages) / sizeof(page_t))
#define PAGE_BOUNDARYCHECK(pageid) \
__assert((pageid) * PAGE_SIZE < (uintptr_t)pages || (pageid) * PAGE_SIZE >= (uintptr_t)&pages[pagecount])
#define PAGE_GET_ID(page) (((uintptr_t)(page) - (uintptr_t)pages) / sizeof(page_t))
#define PAGE_BOUNDARY_CHECK(page_id) \
__assert((page_id) * PAGE_SIZE < (uintptr_t)pages || (page_id) * PAGE_SIZE >= (uintptr_t)&pages[page_count])
static void insertinfreelist(page_t *page) {
uintmax_t pageid = PAGE_GETID(page);
PAGE_BOUNDARYCHECK(pageid);
static void insert_in_free_list(page_t *page) {
uintmax_t page_id = PAGE_GET_ID(page);
PAGE_BOUNDARY_CHECK(page_id);
struct page_t **list;
struct page_t **tail;
int section;
if (pageid < TOP_1MB)
if (page_id < TOP_1MB)
section = MEMORY_SECTION_1MB;
else if (pageid < TOP_4GB)
else if (page_id < TOP_4GB)
section = MEMORY_SECTION_4GB;
else
section = MEMORY_SECTION_DEFAULT;
list = page->backing ? &standbylists[section] : &freelists[section];
tail = page->backing ? &standbytails[section] : &freetails[section];
list = page->backing ? &standby_lists[section] : &free_lists[section];
tail = page->backing ? &standby_tails[section] : &free_tails[section];
if (sections[section].searchstart > pageid)
sections[section].searchstart = pageid;
if (sections[section].search_start > page_id)
sections[section].search_start = page_id;
page->freenext = *list;
page->freeprev = NULL;
page->free_next = *list;
page->free_prev = NULL;
*list = page;
if (page->freenext)
page->freenext->freeprev = page;
if (page->free_next)
page->free_next->free_prev = page;
else
*tail = page;
++freepagecount;
++mm_free_page_count;
}
static void removefromfreelist(page_t *page) {
uintmax_t pageid = PAGE_GETID(page);
PAGE_BOUNDARYCHECK(pageid);
static void remove_from_free_list(page_t *page) {
uintmax_t page_id = PAGE_GET_ID(page);
PAGE_BOUNDARY_CHECK(page_id);
struct page_t **list;
struct page_t **tail;
int section;
if (pageid < TOP_1MB)
if (page_id < TOP_1MB)
section = MEMORY_SECTION_1MB;
else if (pageid < TOP_4GB)
else if (page_id < TOP_4GB)
section = MEMORY_SECTION_4GB;
else
section = MEMORY_SECTION_DEFAULT;
list = page->backing ? &standbylists[section] : &freelists[section];
tail = page->backing ? &standbytails[section] : &freetails[section];
list = page->backing ? &standby_lists[section] : &free_lists[section];
tail = page->backing ? &standby_tails[section] : &free_tails[section];
if (page->freeprev)
page->freeprev->freenext = page->freenext;
if (page->free_prev)
page->free_prev->free_next = page->free_next;
else
*list = page->freenext;
*list = page->free_next;
if (page->freenext)
page->freenext->freeprev = page->freeprev;
if (page->free_next)
page->free_next->free_prev = page->free_prev;
else
*tail = page->freeprev;
*tail = page->free_prev;
page->freenext = NULL;
page->freeprev = NULL;
page->free_next = NULL;
page->free_prev = NULL;
--freepagecount;
--mm_free_page_count;
}
static void internalhold(page_t *page) {
static void internal_hold(page_t *page) {
if (++page->refcount == 1) {
// this is only valid on standby pages, in case of free pages its an use after free
__assert((page->flags & PAGE_FLAGS_FREE) == 0);
removefromfreelist(page);
remove_from_free_list(page);
}
}
@@ -129,32 +129,32 @@ page_t *mm_get_page(void *address) {
}
void *mm_get_page_address(page_t *page) {
return (void *)(PAGE_GETID(page) * PAGE_SIZE);
return (void *)(PAGE_GET_ID(page) * PAGE_SIZE);
}
void mm_hold_page(void *addr) {
page_t *page = &pages[((uintptr_t)addr / PAGE_SIZE)];
MUTEX_ACQUIRE(&freelistmutex);
internalhold(page);
MUTEX_RELEASE(&freelistmutex);
MUTEX_ACQUIRE(&free_list_mutex);
internal_hold(page);
MUTEX_RELEASE(&free_list_mutex);
}
void mm_release_page(void *addr) {
page_t *page = &pages[(uintptr_t)addr / PAGE_SIZE];
MUTEX_ACQUIRE(&freelistmutex);
MUTEX_ACQUIRE(&free_list_mutex);
__assert(page->refcount != 0);
if (--page->refcount == 0) {
__assert((page->flags & PAGE_FLAGS_DIRTY) == 0);
insertinfreelist(page);
insert_in_free_list(page);
if (page->backing == NULL)
page->flags |= PAGE_FLAGS_FREE;
}
MUTEX_RELEASE(&freelistmutex);
MUTEX_RELEASE(&free_list_mutex);
}
static void doalloc(page_t *page) {
static void do_alloc(page_t *page) {
__assert(page->refcount == 0);
memset(page, 0, sizeof(page_t));
page->refcount = 1;
@@ -162,74 +162,74 @@ static void doalloc(page_t *page) {
void *mm_alloc_page(int section) {
retry:
MUTEX_ACQUIRE(&freelistmutex);
MUTEX_ACQUIRE(&free_list_mutex);
page_t *page = NULL;
// try to take a free anonymous page
for (int i = section; i >= 0; --i) {
page = freelists[i];
page = free_lists[i];
if (page) {
removefromfreelist(page);
remove_from_free_list(page);
__assert(page->refcount == 0);
break;
}
}
bool cachepage = false;
bool cache_page = false;
// if that wasn't possible, try to take from the cache standby list
if (page == NULL) {
for (int i = section; i >= 0; --i) {
page = standbytails[i];
page = standby_tails[i];
if (page) {
cachepage = true;
internalhold(page);
cache_page = true;
internal_hold(page);
break;
}
}
}
MUTEX_RELEASE(&freelistmutex);
MUTEX_RELEASE(&free_list_mutex);
if (cachepage && vmmcache_takepage(page) == EAGAIN) {
if (cache_page && vmmcache_takepage(page) == EAGAIN) {
// someone already got the page from the cache between us holding it and taking it
mm_release_page(mm_get_page_address(page));
page = NULL;
// retry it from the start, as an anonymous page could have been released while the lock was not held
goto retry;
} else if (cachepage) {
} else if (cache_page) {
// we got the page from the cache, all is good and we hold the only reference to it.
// set the refcount to 0, as expected by the doalloc call
// set the refcount to 0, as expected by the do_alloc call
page->refcount = 0;
}
void *address = NULL;
if (page) {
address = (void *)(PAGE_GETID(page) * PAGE_SIZE);
doalloc(page);
address = (void *)(PAGE_GET_ID(page) * PAGE_SIZE);
do_alloc(page);
}
return address;
}
void mm_force_free_page(void *address, size_t count) {
MUTEX_ACQUIRE(&freelistmutex);
memorysize += PAGE_SIZE * count;
MUTEX_ACQUIRE(&free_list_mutex);
memory_size += PAGE_SIZE * count;
__assert(((uintptr_t)address % PAGE_SIZE) == 0);
uintmax_t baseid = (uintptr_t)address / PAGE_SIZE;
uintmax_t base_id = (uintptr_t)address / PAGE_SIZE;
for (int i = 0; i < count; ++i) {
uintmax_t pageid = baseid + i;
PAGE_BOUNDARYCHECK(pageid);
page_t *page = &pages[pageid];
uintmax_t page_id = base_id + i;
PAGE_BOUNDARY_CHECK(page_id);
page_t *page = &pages[page_id];
page->flags |= PAGE_FLAGS_FREE;
insertinfreelist(page);
insert_in_free_list(page);
}
MUTEX_RELEASE(&freelistmutex);
MUTEX_RELEASE(&free_list_mutex);
}
void mm_page_init() {
__assert(hhdmreq.response);
hhdmbase = hhdmreq.response->offset;
__assert(hhdm_req.response);
hhdm_base = hhdm_req.response->offset;
__assert(mm_page_limine_map.response);
// get size of memory, top of usable memory, biggest section and print memory map
@@ -240,30 +240,30 @@ void mm_page_init() {
struct limine_memmap_entry *e = mm_page_limine_map.response->entries[i];
printf("mm: %016p -> %016p: %d\n", e->base, e->base + e->length, e->type);
if (e->type == LIMINE_MEMMAP_USABLE || e->type == LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE || e->type == LIMINE_MEMMAP_EXECUTABLE_AND_MODULES) {
size_t sectiontop = e->base + e->length;
if (sectiontop > top)
top = sectiontop;
size_t section_top = e->base + e->length;
if (section_top > top)
top = section_top;
}
if (e->type == LIMINE_MEMMAP_USABLE) {
if (biggest == NULL || e->length > biggest->length)
biggest = e;
memorysize += e->length;
memory_size += e->length;
}
}
pages = MAKE_HHDM((page_t *)biggest->base);
pagecount = ROUND_UP(top, PAGE_SIZE) / PAGE_SIZE;
memset(pages, 0, pagecount * sizeof(page_t));
printf("mm: %d pages used for page list\n", ROUND_UP(pagecount * sizeof(page_t), PAGE_SIZE) / PAGE_SIZE);
page_count = ROUND_UP(top, PAGE_SIZE) / PAGE_SIZE;
memset(pages, 0, page_count * sizeof(page_t));
printf("mm: %d pages used for page list\n", ROUND_UP(page_count * sizeof(page_t), PAGE_SIZE) / PAGE_SIZE);
// initialize usable memory
for (size_t i = 0; i < mm_page_limine_map.response->entry_count; ++i) {
struct limine_memmap_entry *e = mm_page_limine_map.response->entries[i];
if (e->type == LIMINE_MEMMAP_USABLE) {
int firstusablepage = e == biggest ? ROUND_UP(e->base + pagecount * sizeof(page_t), PAGE_SIZE) / PAGE_SIZE : e->base / PAGE_SIZE;
for (int i = firstusablepage; i < (e->base + e->length) / PAGE_SIZE; ++i) {
int first_usable_page = e == biggest ? ROUND_UP(e->base + page_count * sizeof(page_t), PAGE_SIZE) / PAGE_SIZE : e->base / PAGE_SIZE;
for (int i = first_usable_page; i < (e->base + e->length) / PAGE_SIZE; ++i) {
pages[i].flags |= PAGE_FLAGS_FREE;
insertinfreelist(&pages[i]);
insert_in_free_list(&pages[i]);
}
} else if (e->type == LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE) {
for (int i = 0; i < e->length / PAGE_SIZE; ++i) {
@@ -273,7 +273,7 @@ void mm_page_init() {
}
}
MUTEX_INIT(&freelistmutex);
MUTEX_INIT(&free_list_mutex);
}
INIT_ROUTINE_DEFINE(mm_page, INIT_ROUTINE_FLAGS_NONE, mm_page_init, arch_early);
@@ -286,14 +286,14 @@ void *mm_alloc_pages(size_t size, int section) {
if (size == 1)
return mm_alloc_page(section);
MUTEX_ACQUIRE(&freelistmutex);
MUTEX_ACQUIRE(&free_list_mutex);
uintmax_t page = 0;
size_t found = 0;
for (; section >= 0; --section) {
for (page = sections[section].searchstart; page < sections[section].topid; ++page) {
if (page >= pagecount)
for (page = sections[section].search_start; page < sections[section].top_id; ++page) {
if (page >= page_count)
break;
if (pages[page].flags & PAGE_FLAGS_FREE) {
@@ -313,12 +313,12 @@ void *mm_alloc_pages(size_t size, int section) {
page = page - (found - 1);
addr = (void *)(page * PAGE_SIZE);
for (int i = 0; i < size; ++i) {
removefromfreelist(&pages[page + i]);
doalloc(&pages[page + i]);
remove_from_free_list(&pages[page + i]);
do_alloc(&pages[page + i]);
}
}
MUTEX_RELEASE(&freelistmutex);
MUTEX_RELEASE(&free_list_mutex);
return addr;
}
@@ -331,6 +331,6 @@ void mm_release_range(void *addr, size_t size) {
}
void mm_get_page_statistics(size_t *total_pages, size_t *free_pages) {
*total_pages = memorysize / PAGE_SIZE;
*free_pages = freepagecount;
*total_pages = memory_size / PAGE_SIZE;
*free_pages = mm_free_page_count;
}
+41 -41
View File
@@ -45,7 +45,7 @@ static page_t *findpage(vnode_t *vnode, uintmax_t offset) {
while (page) {
if (page->backing == vnode && page->offset == offset)
break;
page = page->hashnext;
page = page->hash_next;
}
return page;
@@ -55,19 +55,19 @@ static page_t *findpage(vnode_t *vnode, uintmax_t offset) {
static void putpage(page_t *page) {
uintmax_t entry = getentry(page->backing, page->offset);
// add to table list
page->hashprev = NULL;
page->hashnext = table[entry];
page->hash_prev = NULL;
page->hash_next = table[entry];
if (table[entry])
table[entry]->hashprev = page;
table[entry]->hash_prev = page;
table[entry] = page;
// add to vnode list
// XXX this could be a bit slow if a large portion of the file is in memory and it gets purged
page->vnodeprev = NULL;
page->vnodenext = page->backing->pages;
page->vnode_prev = NULL;
page->vnode_next = page->backing->pages;
if (page->backing->pages)
page->backing->pages->vnodeprev = page;
page->backing->pages->vnode_prev = page;
page->backing->pages = page;
++vmmcache_cachedpages;
@@ -78,28 +78,28 @@ static void removepage(page_t *page) {
uintmax_t entry = getentry(page->backing, page->offset);
// remove from table list
if (page->hashnext)
page->hashnext->hashprev = page->hashprev;
if (page->hash_next)
page->hash_next->hash_prev = page->hash_prev;
if (page->hashprev)
page->hashprev->hashnext = page->hashnext;
if (page->hash_prev)
page->hash_prev->hash_next = page->hash_next;
else
table[entry] = page->hashnext;
table[entry] = page->hash_next;
page->hashnext = NULL;
page->hashprev = NULL;
page->hash_next = NULL;
page->hash_prev = NULL;
// remove from vnode list
if (page->vnodenext)
page->vnodenext->vnodeprev = page->vnodeprev;
if (page->vnode_next)
page->vnode_next->vnode_prev = page->vnode_prev;
if (page->vnodeprev)
page->vnodeprev->vnodenext = page->vnodenext;
if (page->vnode_prev)
page->vnode_prev->vnode_next = page->vnode_next;
else
page->backing->pages = page->vnodenext;
page->backing->pages = page->vnode_next;
page->vnodenext = NULL;
page->vnodeprev = NULL;
page->vnode_next = NULL;
page->vnode_prev = NULL;
--vmmcache_cachedpages;
}
@@ -270,7 +270,7 @@ int vmmcache_truncate(vnode_t *vnode, uintmax_t offset) {
while (page) {
page_t *oldpage = page;
page = page->vnodenext;
page = page->vnode_next;
// only truncate past a certain offset
if (oldpage->offset < offset)
@@ -278,7 +278,7 @@ int vmmcache_truncate(vnode_t *vnode, uintmax_t offset) {
oldpage->flags |= PAGE_FLAGS_TRUNCATED;
removepage(oldpage);
oldpage->vnodenext = pagelist;
oldpage->vnode_next = pagelist;
pagelist = oldpage;
}
@@ -287,7 +287,7 @@ int vmmcache_truncate(vnode_t *vnode, uintmax_t offset) {
// make sure to unref if they are pinned
while (pagelist) {
page_t *page = pagelist;
pagelist = pagelist->vnodenext;
pagelist = pagelist->vnode_next;
if (page->flags & PAGE_FLAGS_PINNED)
mm_release_page(mm_get_page_address(page));
}
@@ -337,24 +337,24 @@ int vmmcache_syncvnode(vnode_t *vnode, uintmax_t offset, size_t size) {
// TODO create a proper vnode dirty list as to not have to loop through the ENTIRE thing in memory
page_t *page = vnode->pages;
page_t *vnodedirtylist = NULL;
for (; page; page = page->vnodenext) {
for (; page; page = page->vnode_next) {
if (page->offset < offset || page->offset >= top || (page->flags & PAGE_FLAGS_DIRTY) == 0 || (page->flags & PAGE_FLAGS_VNODE_SYNCING))
continue;
// remove from write list and add to an internal list using the write pointers
// in a singly linked list way
if (page->writenext)
page->writenext->writeprev = page->writeprev;
if (page->write_next)
page->write_next->write_prev = page->write_prev;
else
dirtylistend = page->writeprev;
dirtylistend = page->write_prev;
if (page->writeprev)
page->writeprev->writenext = page->writenext;
if (page->write_prev)
page->write_prev->write_next = page->write_next;
else
dirtylist = page->writenext;
dirtylist = page->write_next;
page->writenext = vnodedirtylist;
page->writeprev = NULL;
page->write_next = vnodedirtylist;
page->write_prev = NULL;
page->flags |= PAGE_FLAGS_VNODE_SYNCING;
vnodedirtylist = page;
}
@@ -367,8 +367,8 @@ int vmmcache_syncvnode(vnode_t *vnode, uintmax_t offset, size_t size) {
// retry the write and keep on syncing the pages to disk
HOLD_LOCK();
page_t *page = vnodedirtylist;
vnodedirtylist = vnodedirtylist->writenext;
page->writenext = NULL;
vnodedirtylist = vnodedirtylist->write_next;
page->write_next = NULL;
page->flags &= ~PAGE_FLAGS_VNODE_SYNCING;
// another thread could already have synced this page, verify if it is still dirty
@@ -417,10 +417,10 @@ int vmmcache_makedirty(page_t *page) {
page->flags |= PAGE_FLAGS_DIRTY;
if ((page->flags & PAGE_FLAGS_VNODE_SYNCING) == 0) {
page->writeprev = NULL;
page->writenext = dirtylist;
page->write_prev = NULL;
page->write_next = dirtylist;
if (dirtylist)
dirtylist->writeprev = page;
dirtylist->write_prev = page;
else
dirtylistend = page;
@@ -461,13 +461,13 @@ static void writer() {
continue;
}
dirtylistend = page->writeprev;
dirtylistend = page->write_prev;
if (dirtylistend)
dirtylistend->writenext = NULL;
dirtylistend->write_next = NULL;
else
dirtylist = NULL;
page->writeprev = NULL;
page->write_prev = NULL;
// TODO notify error on vmmcache_syncvnode
syncpage((page_t *)page, true);
}