#include "alloc.h" #include "assert.h" #include "klibc.h" #include "list.h" #include "mem.h" #include "paging.h" #include "serial.h" #include "stack.h" #include "vga.h" void testPhymem(void) { printf("Testing memory PHY\n"); struct mem_desc *allocated_page_list; struct mem_desc *page; // Cast in mem_desc to use it. In fact it's the addr of 4K free memory list_init(allocated_page_list); int allocCount = 0; int freeCount = 0; while ((page = (struct mem_desc *)allocPhyPage()) != NULL) { page->phy_addr = allocCount; allocCount++; list_add_tail(allocated_page_list, page); } printf("%d pages allocated\n", allocCount); while ((page = list_pop_head(allocated_page_list)) != NULL) { assertmsg(page->phy_addr == (ulong)freeCount, "page %d modified", page); assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n", (ulong)page); freeCount++; } printf("%d pages freed\n", freeCount); assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL, "Cannot allocate memory\n"); unrefPhyPage((ulong)page); } static void testAlloc(void) { assert(malloc(1)); assert(malloc(2)); assert(malloc(3)); assert(malloc(4)); void *malloc1 = malloc(sizeof(void *)); void *malloc2 = malloc(sizeof(void *)); assert((char *)malloc2 == ((char *)malloc1 + sizeof(void *))); free(malloc2); void *malloc3 = malloc(sizeof(void *)); assertmsg((char *)malloc2 == (char *)malloc3, " %d %d\n", malloc2, malloc3); assert(malloc(1024)); assert(malloc(1024)); assert(malloc(1024)); assert(malloc(1024)); assert(malloc(1024)); } static void testPaging(void) { printf("Testing paging\n"); struct mem_desc *allocated_page_list; struct mem_desc *page; // Cast in mem_desc to use it. In fact it's the addr of 4K free memory list_init(allocated_page_list); int allocCount = 0; int freeCount = 0; while ((page = (struct mem_desc *)allocPhyPage()) != NULL) { assertmsg(pageMap((vaddr_t)page, (paddr_t)page, PAGING_MEM_WRITE) == 0, "Fail to map page %d\n", allocCount); page->phy_addr = allocCount; allocCount++; list_add_tail(allocated_page_list, page); } printf("%d pages allocated\n", allocCount); while ((page = list_pop_head(allocated_page_list)) != NULL) { assertmsg(page->phy_addr == (ulong)freeCount, "page modified"); assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n", (ulong)page); pageUnmap((vaddr_t)page); freeCount++; } printf("%d pages freed\n", freeCount); assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL, "Cannot allocate memory\n"); unrefPhyPage((ulong)page); } static void test_backtrace_2(int a, int b) { printStackTrace(a + b); } static void test_backtrace_1(int a) { test_backtrace_2(a, 3); } void test_backtrace() { test_backtrace_1(2); } void run_test(void) { testPaging(); printf("Testing Serial\n"); serialWrite('h'); serialWrite('e'); serialWrite('l'); serialWrite('l'); serialWrite('o'); testAlloc(); printf("Testing backtrace\n"); test_backtrace(); }