2019-04-11 23:08:59 +02:00
|
|
|
#include "alloc.h"
|
|
|
|
#include "assert.h"
|
2018-11-19 13:56:19 +01:00
|
|
|
#include "klibc.h"
|
2019-04-11 23:08:59 +02:00
|
|
|
#include "list.h"
|
2018-11-09 16:30:42 +01:00
|
|
|
#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"
|
2018-11-19 13:56:19 +01:00
|
|
|
#include "stack.h"
|
2018-11-08 22:09:12 +01:00
|
|
|
#include "vga.h"
|
|
|
|
|
2018-11-12 18:06:46 +01:00
|
|
|
void testPhymem(void)
|
2018-11-09 16:30:42 +01:00
|
|
|
{
|
|
|
|
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) {
|
2018-11-09 16:30:42 +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) {
|
2019-04-11 23:08:59 +02:00
|
|
|
assertmsg(page->phy_addr == (ulong)freeCount, "page %d modified", page);
|
|
|
|
assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n",
|
|
|
|
(ulong)page);
|
2018-11-09 16:30:42 +01:00
|
|
|
freeCount++;
|
|
|
|
}
|
|
|
|
printf("%d pages freed\n", freeCount);
|
|
|
|
|
2019-04-16 20:02:26 +02:00
|
|
|
assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL,
|
|
|
|
"Cannot allocate memory\n");
|
2019-04-11 23:08:59 +02:00
|
|
|
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 *)));
|
2019-04-15 23:09:09 +02:00
|
|
|
free(malloc2);
|
|
|
|
void *malloc3 = malloc(sizeof(void *));
|
|
|
|
assertmsg((char *)malloc2 == (char *)malloc3, " %d %d\n", malloc2, malloc3);
|
2019-04-11 23:08:59 +02:00
|
|
|
assert(malloc(1024));
|
|
|
|
assert(malloc(1024));
|
|
|
|
assert(malloc(1024));
|
|
|
|
assert(malloc(1024));
|
|
|
|
assert(malloc(1024));
|
2018-11-09 16:30:42 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2019-04-11 23:08:59 +02:00
|
|
|
assertmsg(pageMap((vaddr_t)page, (paddr_t)page, PAGING_MEM_WRITE) == 0,
|
2019-04-16 20:02:26 +02:00
|
|
|
"Fail to map page %d\n", allocCount);
|
|
|
|
memset(page, allocCount, PAGE_SIZE);
|
2018-11-12 23:07:59 +01:00
|
|
|
allocCount++;
|
|
|
|
list_add_tail(allocated_page_list, page);
|
|
|
|
}
|
|
|
|
printf("%d pages allocated\n", allocCount);
|
|
|
|
|
|
|
|
while ((page = list_pop_head(allocated_page_list)) != NULL) {
|
2019-04-16 20:02:26 +02:00
|
|
|
assertmsg((char)page->phy_addr == (char)freeCount,
|
|
|
|
"page modified %d but is %d\n", freeCount, page->phy_addr);
|
2019-04-11 23:08:59 +02:00
|
|
|
assertmsg(unrefPhyPage((ulong)page) >= 0, "Failed to free page %d\n",
|
|
|
|
(ulong)page);
|
2018-11-12 23:07:59 +01:00
|
|
|
pageUnmap((vaddr_t)page);
|
|
|
|
freeCount++;
|
|
|
|
}
|
|
|
|
printf("%d pages freed\n", freeCount);
|
|
|
|
|
2019-04-16 20:02:26 +02:00
|
|
|
assertmsg((page = (struct mem_desc *)allocPhyPage()) != NULL,
|
|
|
|
"Cannot allocate memory\n");
|
2019-04-11 23:08:59 +02:00
|
|
|
unrefPhyPage((ulong)page);
|
2018-11-12 23:07:59 +01:00
|
|
|
}
|
|
|
|
|
2019-04-11 23:08:59 +02:00
|
|
|
static void test_backtrace_2(int a, int b)
|
|
|
|
{
|
|
|
|
printStackTrace(a + b);
|
2018-11-19 13:56:19 +01:00
|
|
|
}
|
|
|
|
|
2019-04-11 23:08:59 +02:00
|
|
|
static void test_backtrace_1(int a)
|
|
|
|
{
|
2018-11-19 13:56:19 +01:00
|
|
|
test_backtrace_2(a, 3);
|
|
|
|
}
|
|
|
|
|
2019-04-11 23:08:59 +02:00
|
|
|
void test_backtrace()
|
|
|
|
{
|
2018-11-19 13:56:19 +01:00
|
|
|
test_backtrace_1(2);
|
|
|
|
}
|
|
|
|
|
2018-11-12 18:06:46 +01:00
|
|
|
void run_test(void)
|
2018-11-09 16:30:42 +01:00
|
|
|
{
|
2018-11-12 23:07:59 +01:00
|
|
|
testPaging();
|
2018-11-19 13:56:19 +01:00
|
|
|
printf("Testing Serial\n");
|
2018-11-08 22:09:12 +01:00
|
|
|
serialWrite('h');
|
|
|
|
serialWrite('e');
|
|
|
|
serialWrite('l');
|
|
|
|
serialWrite('l');
|
|
|
|
serialWrite('o');
|
2019-04-11 23:08:59 +02:00
|
|
|
testAlloc();
|
2018-11-19 13:56:19 +01:00
|
|
|
printf("Testing backtrace\n");
|
|
|
|
test_backtrace();
|
2018-11-08 22:09:12 +01:00
|
|
|
}
|