支持完整的内存管理
This commit is contained in:
@@ -14,6 +14,18 @@ umword_t thread_get_pfa(void)
|
||||
asm volatile("mrs %0, far_el2" : "=r"(a));
|
||||
return a;
|
||||
}
|
||||
static void dump_stack(umword_t pc, umword_t x29)
|
||||
{
|
||||
uint64_t result[5];
|
||||
printk("pc:0x%x x29:0x%x\n", pc, x29);
|
||||
result[0] = pc;
|
||||
for (size_t i = 1; i < sizeof(result) / sizeof(umword_t); i++)
|
||||
{
|
||||
result[i] = (*(umword_t *)(x29 + 8)) - 4;
|
||||
x29 = *(umword_t *)(x29);
|
||||
}
|
||||
printk("knl pf stack: 0x%x,0x%x,0x%x,0x%x,0x%x\n", result[0], result[1], result[2], result[3], result[4]);
|
||||
}
|
||||
void thread_sync_entry(entry_frame_t *regs)
|
||||
{
|
||||
umword_t ec = arm_esr_ec(esr_get());
|
||||
@@ -29,6 +41,8 @@ void thread_sync_entry(entry_frame_t *regs)
|
||||
if (task_vma_page_fault(&tk->mm_space.mem_vma, ALIGN_DOWN(addr, PAGE_SIZE)) < 0)
|
||||
{
|
||||
printk("0x20 pfa:0x%x\n", addr);
|
||||
printk("pc:0x%x x29:0x%x\n", regs->pc, regs->regs[29]);
|
||||
// dump_stack(regs->pc, regs->regs[29]);
|
||||
task_knl_kill(th, FALSE);
|
||||
}
|
||||
}
|
||||
@@ -40,6 +54,7 @@ void thread_sync_entry(entry_frame_t *regs)
|
||||
if (task_vma_page_fault(&tk->mm_space.mem_vma, ALIGN_DOWN(addr, PAGE_SIZE)) < 0)
|
||||
{
|
||||
printk("0x24 pfa:0x%x\n", addr);
|
||||
dump_stack(regs->pc, regs->regs[29]);
|
||||
task_knl_kill(th, FALSE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +79,10 @@ static inline paddr_t vma_node_get_paddr(vma_t *data)
|
||||
{
|
||||
return data->paddr << PAGE_SHIFT;
|
||||
}
|
||||
static inline void vma_node_set_paddr(vma_t *data, paddr_t addr)
|
||||
{
|
||||
data->paddr = addr >> PAGE_SHIFT;
|
||||
}
|
||||
static inline bool_t vma_node_get_used(vma_t *data)
|
||||
{
|
||||
return !!(data->flags & VMA_USED_NODE);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <printk.h>
|
||||
#include <buddy.h>
|
||||
#include <arch.h>
|
||||
|
||||
#include <vma.h>
|
||||
typedef uint16_t Elf64_Half;
|
||||
typedef uint32_t Elf64_Word;
|
||||
typedef uint64_t Elf64_Addr;
|
||||
@@ -150,6 +150,9 @@ int elf_load(task_t *task, umword_t elf_data, size_t size, addr_t *entry_addr)
|
||||
offset += ALIGN(elf_phdr->p_memsz, elf_phdr->p_align);
|
||||
}
|
||||
}
|
||||
map_mm(mm_space_get_pdir(&task->mm_space), st_addr,
|
||||
(addr_t)mem, PAGE_SHIFT, mem_size / PAGE_SIZE, 0x7ff);
|
||||
assert(task_vma_alloc(&task->mm_space.mem_vma,
|
||||
vma_addr_create(0xf /*TODO:*/, VMA_ADDR_RESV, st_addr),
|
||||
mem_size, (paddr_t)mem, 0) >= 0);
|
||||
// map_mm(mm_space_get_pdir(&task->mm_space), st_addr,
|
||||
// (addr_t)mem, PAGE_SHIFT, mem_size / PAGE_SIZE, 0x7ff);
|
||||
}
|
||||
|
||||
@@ -399,6 +399,7 @@ int task_vma_alloc(task_vma_t *task_vma, vma_addr_t vaddr, size_t size,
|
||||
vma_node_set_used(node_data);
|
||||
vma_addr_set_flags(&node_data->vaddr, vma_addr_get_flags(vaddr));
|
||||
vma_addr_set_prot(&node_data->vaddr, vma_addr_get_prot(vaddr));
|
||||
vma_node_set_paddr(node_data, paddr);
|
||||
task_vma->idle_tree.cmp = vma_idl_tree_insert_cmp_handler;
|
||||
mln_rbtree_insert(&task_vma->idle_tree, node);
|
||||
|
||||
@@ -637,7 +638,7 @@ int task_vma_page_fault(task_vma_t *task_vma, vaddr_t addr)
|
||||
{
|
||||
return lock_status;
|
||||
}
|
||||
printk("page fault:0x%x.\n", addr);
|
||||
// printk("page fault:0x%x.\n", addr);
|
||||
// 1.查找
|
||||
task = container_of(container_of(task_vma, mm_space_t, mem_vma), task_t, mm_space);
|
||||
task_vma->idle_tree.cmp = vma_idl_tree_wrap_cmp_handler;
|
||||
|
||||
@@ -29,6 +29,9 @@
|
||||
#if ARCH_WORD_SIZE == 64
|
||||
#include <elf64.h>
|
||||
#endif
|
||||
#if IS_ENABLED(CONFIG_MMU)
|
||||
#include <vma.h>
|
||||
#endif
|
||||
#include <cpio.h>
|
||||
|
||||
static uint8_t knl_msg_buf[THREAD_MSG_BUG_LEN];
|
||||
@@ -137,8 +140,11 @@ static void knl_init_2(void)
|
||||
elf_load(init_task, ret_addr, size, &entry);
|
||||
void *init_msg_buf = mm_buddy_alloc_one_page();
|
||||
assert(init_msg_buf);
|
||||
map_mm(mm_space_get_pdir(&init_task->mm_space), 0xE0000000,
|
||||
(addr_t)init_msg_buf, PAGE_SHIFT, 1, 0x7ff);
|
||||
assert(task_vma_alloc(&init_task->mm_space.mem_vma,
|
||||
vma_addr_create(0xf /*TODO:*/, VMA_ADDR_RESV, 0xE0000000),
|
||||
PAGE_SIZE, (paddr_t)init_msg_buf, 0) >= 0);
|
||||
// map_mm(mm_space_get_pdir(&init_task->mm_space), 0xE0000000,
|
||||
// (addr_t)init_msg_buf, PAGE_SHIFT, 1, 0x7ff);
|
||||
thread_set_msg_bug(init_thread, (void *)init_msg_buf, (void *)0xE0000000);
|
||||
thread_user_pf_set(init_thread, (void *)(entry), (void *)0xdeaddead,
|
||||
NULL, 0);
|
||||
@@ -225,5 +231,7 @@ void start_kernel(void)
|
||||
sti();
|
||||
|
||||
while (1)
|
||||
;
|
||||
{
|
||||
knl_main();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,22 @@
|
||||
#include <assert.h>
|
||||
void malloc_test(void)
|
||||
{
|
||||
#define TEST_MEM_SIZE 1024 * 1024 * 32
|
||||
void *mem = malloc(TEST_MEM_SIZE);
|
||||
assert(mem);
|
||||
memset(mem, 0, TEST_MEM_SIZE);
|
||||
|
||||
free(mem);
|
||||
#define TEST_MEM_SIZE 1024 * 1024 * 4
|
||||
void *mem2 = malloc(TEST_MEM_SIZE);
|
||||
assert(mem2);
|
||||
memset(mem2, 0, TEST_MEM_SIZE);
|
||||
free(mem2);
|
||||
#undef TEST_MEM_SIZE
|
||||
|
||||
void *mem[1000];
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
mem[i] = malloc(4096);
|
||||
assert(mem[i]);
|
||||
memset(mem, 0, 4096);
|
||||
}
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
free(mem[i]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user