matos/tests/test.c

92 lines
2.2 KiB
C
Raw Normal View History

#include "list.h"
#include "mem.h"
2018-11-12 23:07:59 +01:00
#include "paging.h"
2018-11-08 22:09:12 +01:00
#include "serial.h"
#include "vga.h"
2018-11-12 18:06:46 +01:00
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;
2018-11-09 17:07:39 +01:00
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) {
if (page->phy_addr != (ulong)freeCount) {
printf("Error page %d modified\n", (ulong)page);
}
if (unrefPhyPage((ulong)page) < 0) {
printf("Failed to free page %d\n", (ulong)page);
}
freeCount++;
}
printf("%d pages freed\n", freeCount);
2018-11-09 17:07:39 +01:00
if ((page = (struct mem_desc *)allocPhyPage()) == NULL) {
printf("Error ! Cannot allocate memory\n");
} else {
2018-11-09 17:07:39 +01:00
unrefPhyPage((ulong)page);
}
}
2018-11-12 23:07:59 +01:00
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) {
if (pageMap((vaddr_t)page, (paddr_t)page, PAGING_MEM_WRITE)){
printf("Fail to map page %d\n", allocCount);
break;
}
2018-11-12 23:07:59 +01:00
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) {
if (page->phy_addr != (ulong)freeCount) {
printf("Error page %d modified\n", (ulong)page);
}
if (unrefPhyPage((ulong)page) < 0) {
2018-11-12 23:07:59 +01:00
printf("Failed to free page %d\n", (ulong)page);
}
pageUnmap((vaddr_t)page);
freeCount++;
}
printf("%d pages freed\n", freeCount);
if ((page = (struct mem_desc *)allocPhyPage()) == NULL) {
printf("Error ! Cannot allocate memory\n");
} else {
unrefPhyPage((ulong)page);
}
}
2018-11-12 18:06:46 +01:00
void run_test(void)
{
2018-11-12 23:07:59 +01:00
testPaging();
2018-11-08 22:09:12 +01:00
printf("Testing Serial");
serialWrite('h');
serialWrite('e');
serialWrite('l');
serialWrite('l');
serialWrite('o');
}