From a9f2b48a95bcd08068e40b1d0d04ab2df7cc1e07 Mon Sep 17 00:00:00 2001 From: quackerd Date: Mon, 24 Feb 2020 02:16:26 -0500 Subject: [PATCH] wip --- CMakeLists.txt | 5 +- arch/CMakeLists.txt | 5 +- arch/boot.asm | 6 ++ arch/cpu.c | 2 +- arch/cpu.h | 2 +- arch/intr.c | 8 +- arch/main.c | 42 ++++++--- arch/mem.c | 66 --------------- arch/paging.c | 91 ++++++++++++++++++++ arch/paging.h | 93 ++++++++++++++++++++ arch/pmap.c | 146 ++++++++++++++++++++++++++++++++ arch/pmap_p.h | 12 +++ arch/print.c | 6 +- common/CMakeLists.txt | 7 ++ {kern => common}/avl_tree.c | 9 +- compile_flags.txt | 1 + gdb_qemu.dbg | 3 + inc/arch/intr.h | 2 +- inc/arch/mem.h | 10 --- inc/arch/mlayout.h | 44 +++++++--- inc/arch/pmap.h | 27 ++++++ inc/arch/print.h | 2 +- inc/{kern => common}/avl_tree.h | 2 +- inc/{kern => common}/bitset.h | 0 inc/{kern => common}/cdef.h | 3 +- inc/{kern => common}/lds.h | 2 +- inc/{kern => common}/libkern.h | 6 +- inc/{kern => common}/list.h | 6 +- inc/ke/brute.h | 16 ++++ inc/{kern => ke}/kinit.h | 2 +- inc/{kern => ke}/poison.h | 0 inc/{kern => ke}/print.h | 4 +- inc/ke/spin_lock.h | 27 ++++++ inc/{kern => ke}/status.h | 2 +- inc/kern/brute.h | 16 ---- inc/kern/spin_lock.h | 27 ------ inc/mm/phys.h | 28 ++++++ inc/test/ktest.h | 4 +- {kern => ke}/CMakeLists.txt | 3 +- {kern => ke}/kmain.c | 13 ++- {kern => ke}/libkern.c | 4 +- {kern => ke}/print.c | 17 ++-- {kern => ke}/spin_lock.c | 12 +-- mm/CMakeLists.txt | 7 ++ mm/phys.c | 38 +++++++++ scripts/linker.lds | 2 +- test/CMakeLists.txt | 1 - test/avl_test.c | 8 +- test/ktest.c | 6 +- test/list_test.c | 6 +- test/qsort_test.c | 6 +- 51 files changed, 640 insertions(+), 217 deletions(-) delete mode 100644 arch/mem.c create mode 100644 arch/paging.c create mode 100644 arch/paging.h create mode 100644 arch/pmap.c create mode 100644 arch/pmap_p.h create mode 100644 common/CMakeLists.txt rename {kern => common}/avl_tree.c (99%) create mode 100644 compile_flags.txt create mode 100644 gdb_qemu.dbg delete mode 100644 inc/arch/mem.h create mode 100644 inc/arch/pmap.h rename inc/{kern => common}/avl_tree.h (98%) rename inc/{kern => common}/bitset.h (100%) rename inc/{kern => common}/cdef.h (92%) rename inc/{kern => common}/lds.h (91%) rename inc/{kern => common}/libkern.h (92%) rename inc/{kern => common}/list.h (95%) create mode 100644 inc/ke/brute.h rename inc/{kern => ke}/kinit.h (96%) rename inc/{kern => ke}/poison.h (100%) rename inc/{kern => ke}/print.h (70%) create mode 100644 inc/ke/spin_lock.h rename inc/{kern => ke}/status.h (85%) delete mode 100644 inc/kern/brute.h delete mode 100644 inc/kern/spin_lock.h create mode 100644 inc/mm/phys.h rename {kern => ke}/CMakeLists.txt (75%) rename {kern => ke}/kmain.c (74%) rename {kern => ke}/libkern.c (98%) rename {kern => ke}/print.c (91%) rename {kern => ke}/spin_lock.c (79%) create mode 100644 mm/CMakeLists.txt create mode 100644 mm/phys.c diff --git a/CMakeLists.txt b/CMakeLists.txt index a739a89..f196615 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.10) # disable in-source build set(CMAKE_DISABLE_IN_SOURCE_BUILD ON) set(CMAKE_DISABLE_SOURCE_CHANGES ON) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) project(bond) @@ -69,8 +70,10 @@ set(DMP_FLAGS ${DMP_FLAGS_${ARCH}}) set(SUBMODULES - kern + common + ke arch + mm scripts test) diff --git a/arch/CMakeLists.txt b/arch/CMakeLists.txt index 8d3c07a..88b6d6c 100644 --- a/arch/CMakeLists.txt +++ b/arch/CMakeLists.txt @@ -3,9 +3,10 @@ set(CC_SRC cpu.c main.c intr.c - mem.c print.c - brute.c) + brute.c + pmap.c + paging.c) set(AS_SRC boot.asm diff --git a/arch/boot.asm b/arch/boot.asm index fb9460c..05297e3 100644 --- a/arch/boot.asm +++ b/arch/boot.asm @@ -19,6 +19,7 @@ bits 32 ; Identity map the first 4G memory, where the kernel binary and multiboot info is ; Map the first 4G memory to KERN_PMAP temporarily so we have access to printf ; Map the first 1G memory, which contains the kernel, to KERN_BASE_START +; Map the nth PML4 to itself for recursive page tables arch_init_32: cli ; close interrupt cld ; set direction @@ -75,6 +76,11 @@ arch_init_32: add eax, GET_PDPT(KERN_BASE_START) * 8 mov dword [eax], 10000011b ; ebx lower bits is attribute = R/W + SU + 1G page, high bits = physical 0th GB + ; map the recursive mapping + mov eax, GET_PADDR(kern_early_pml4) + add eax, GET_PML4(KERN_RPT_START) * 8 + mov dword [eax], GET_PADDR(kern_early_pml4) + 11b + BOCHS_BREAK ; enable PAE diff --git a/arch/cpu.c b/arch/cpu.c index 24f4979..1234ad9 100644 --- a/arch/cpu.c +++ b/arch/cpu.c @@ -1,4 +1,4 @@ -#include +#include #include "cpu.h" diff --git a/arch/cpu.h b/arch/cpu.h index 3981c30..3b3f6ef 100644 --- a/arch/cpu.h +++ b/arch/cpu.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #define HAL_CORE_COUNT 1 struct ATTR_PACKED hal_gdt_ptr { diff --git a/arch/intr.c b/arch/intr.c index 6f3c7e9..2a411a6 100644 --- a/arch/intr.c +++ b/arch/intr.c @@ -1,9 +1,9 @@ #include -#include +#include #include -#include -#include -#include +#include +#include +#include #include "cpu.h" diff --git a/arch/main.c b/arch/main.c index 465b5ec..bdece26 100644 --- a/arch/main.c +++ b/arch/main.c @@ -1,10 +1,18 @@ -#include -#include +#include +#include #include #include +#include +#include // private headers #include "multiboot2.h" +#include "pmap_p.h" + +static const char* _loader_name; + +// kernel entry point +extern void kmain(); ATTR_USED void arch_main(void *mb_info) @@ -12,36 +20,46 @@ arch_main(void *mb_info) /* init printf related stuff */ arch_print_init(); - kprintf("Multiboot info: 0x%p\n", mb_info); - kprintf("Initializing arch layer...\n"); + kprintf("Processing multiboot info @ 0x%p...\n", mb_info); for (struct multiboot_tag *tag = (struct multiboot_tag *) ((uintptr) mb_info + 8); tag->type != MULTIBOOT_TAG_TYPE_END; tag = (struct multiboot_tag *) ((multiboot_uint8_t *) tag + ((tag->size + 7) & ~7u))) { - kprintf("Tag 0x%p: %d, Size %d\n", (void *) tag, tag->type, tag->size); +// kprintf("Tag 0x%p: %d, Size %d\n", (void *) tag, tag->type, tag->size); switch (tag->type) { case MULTIBOOT_TAG_TYPE_MMAP: + kprintf("Found multiboot memory map.\n"); for (struct multiboot_mmap_entry *entry = ((struct multiboot_tag_mmap *) tag)->entries; (multiboot_uint8_t *) entry < (multiboot_uint8_t *) tag + tag->size; entry = (multiboot_memory_map_t *) ((uintptr) entry + - ((struct multiboot_tag_mmap *) tag)->entry_size)) - kprintf(" base_addr = 0x%lx," - " length = 0x%lx, type = 0x%x\n", + ((struct multiboot_tag_mmap *) tag)->entry_size)) { + kprintf("Adding to pmap seg: base = 0x%lx," + " length = 0x%lx, type = 0x%x.\n", (ulong) entry->addr, (ulong) entry->len, entry->type); + if (entry->type == MULTIBOOT_MEMORY_AVAILABLE) { + // add physical segments to mm phys subsystem + arch_mem_addseg(entry->addr, entry->len); + } + } break; case MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME: - kprintf("BoND is loaded by: %s\n", ((struct multiboot_tag_string *) tag)->string); + kprintf("Found multiboot loader name.\n"); + _loader_name = ((struct multiboot_tag_string *) tag)->string; break; default: - kprintf("Unhandled multiboot tag type: %d\n", tag->type); + kprintf("Ignoring multiboot tag type: %d size: 0x%x\n", tag->type, tag->size); break; } } - kprintf("Arch layer initialized.\n"); + kprintf("BoND is loaded by: %s\n", _loader_name); + kprintf("kernel start: 0x%p end: 0x%p\n", (void*)KERN_IMG_START, (void*)ARCH_ML_KIMAGE_STOP); - arch_brute(); + kprintf("Initializing memory..."); + arch_mem_init(); + + kmain(); } diff --git a/arch/mem.c b/arch/mem.c deleted file mode 100644 index 91a7ddd..0000000 --- a/arch/mem.c +++ /dev/null @@ -1,66 +0,0 @@ -#include -#include -#include - -/** - Page Table Definitions -**/ - -#define PML4_PRESENT (1ull << 0) -#define PML4_WRITE (1ull << 1) -#define PML4_USER (1ull << 2) -#define PML4_WRITE_THROUGH (1ull << 3) -#define PML4_CACHE_DISABLED (1ull << 4) -#define PML4_ACCESSED (1ull << 5) -#define PML4_EXECUTION_DISABLED (1ull << 63) - -#define PDPT_PRESENT (1ull << 0) -#define PDPT_WRITE (1ull << 1) -#define PDPT_USER (1ull << 2) -#define PDPT_WRITE_THROUGH (1ull << 3) -#define PDPT_CACHE_DISABLED (1ull << 4) -#define PDPT_ACCESSED (1ull << 5) -#define PDPT_EXECUTION_DISABLED (1ull << 63) - -#define PD_PRESENT (1ull << 0) -#define PD_WRITE (1ull << 1) -#define PD_USER (1ull << 2) -#define PD_WRITE_THROUGH (1ull << 3) -#define PD_CACHE_DISABLED (1ull << 4) -#define PD_ACCESSED (1ull << 5) -#define PD_EXECUTION_DISABLED (1ull << 63) - -#define PT_PRESENT (1ull << 0) -#define PT_WRITE (1ull << 1) -#define PT_USER (1ull << 2) -#define PT_WRITE_THROUGH (1ull << 3) -#define PT_CACHE_DISABLED (1ull << 4) -#define PT_ACCESSED (1ull << 5) -#define PT_DIRTY (1ull << 6) -#define PT_ATTRIBUTE_TABLE (1ull << 7) -#define PT_GLOBAL (1ull << 8) -#define PT_EXECUTION_DISABLED (1ull << 63) - -#define PML4_ENTRY_NUM(vaddr) (((vaddr) >> 39) & 0x1FF) -#define PDPT_ENTRY_NUM(vaddr) (((vaddr) >> 30) & 0x1FF) -#define PD_ENTRY_NUM(vaddr) (((vaddr) >> 21) & 0x1FF) -#define PT_ENTRY_NUM(vaddr) (((vaddr) >> 12) & 0x1FF) - -void -write_page_tbl(void *base, uintptr pdpt_addr, uint64 attr) -{ - if (base == NULL) - { - return; - } - uint64 entry = (pdpt_addr & 0xFFFFFFFFFF000ul) | attr; - ((uint8 *) base)[0] = (uint8) (entry & 0xFFul); - ((uint8 *) base)[1] = (uint8) ((entry >> 8u) & 0xFFu); - ((uint8 *) base)[2] = (uint8) ((entry >> 16u) & 0xFFu); - ((uint8 *) base)[3] = (uint8) ((entry >> 24u) & 0xFFu); - ((uint8 *) base)[4] = (uint8) ((entry >> 32u) & 0xFFu); - ((uint8 *) base)[5] = (uint8) ((entry >> 40u) & 0xFFu); - ((uint8 *) base)[6] = (uint8) ((entry >> 48u) & 0xFFu); - ((uint8 *) base)[7] = (uint8) ((entry >> 56u) & 0xFFu); -} - diff --git a/arch/paging.c b/arch/paging.c new file mode 100644 index 0000000..24c5ada --- /dev/null +++ b/arch/paging.c @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include "paging.h" + +mm_paddr page_alloc_zero() +{ + return 0; +} + +/* map physical memory to virtual */ +// XXX: this should undo stuff if pages are not enough +// TODO: use huge pages later (2MB + 1GB) +static int +_map_page(arch_pml4e *pml4, mm_paddr paddr, mm_paddr vaddr, uint attr) +{ + int uspace = vaddr < ARCH_ML_KSPACE_START; + + if (uspace && (vaddr > ARCH_ML_USPACE_END)) { + BRUTE("non-canonical vaddr"); + } + + /* must be 4k aligned */ + if((paddr & (ARCH_KPAGE_SZ - 1)) != 0 || + (vaddr & (ARCH_KPAGE_SZ - 1)) != 0 ){ + BRUTE("addresses not aligned"); + } + + mm_paddr alloc_pt; + + arch_pml4e *pml4_ent = pml4 + PML4_ENTRY_NUM(vaddr); + if (*pml4_ent == 0) { + alloc_pt = page_alloc_zero(); + if (!alloc_pt) { + BRUTE("not enough pages"); + } + arch_write_page_entry(pml4_ent, alloc_pt, PML4E_ATTR_RW | PML4E_ATTR_P | (uspace ? PML4E_ATTR_US : 0)); + } + + arch_pdpte *pdpt_ent = (arch_pdpte*)arch_pmap_map(*pml4_ent, ARCH_KPAGE_SZ) + PDPT_ENTRY_NUM(vaddr); + + if (*pdpt_ent == 0) { + alloc_pt = page_alloc_zero(); + if (!alloc_pt) { + BRUTE("not enough pages"); + } + arch_write_page_entry(pdpt_ent, alloc_pt, PDPTE_ATTR_RW | PDPTE_ATTR_P | (uspace ? PDPTE_ATTR_US : 0)); + } + + arch_pde *pde_ent = (arch_pdpte*)arch_pmap_map(*pdpt_ent, ARCH_KPAGE_SZ) + PD_ENTRY_NUM(vaddr); + + if (*pde_ent == 0) { + alloc_pt = page_alloc_zero(); + if (!alloc_pt) { + BRUTE("not enough pages"); + } + arch_write_page_entry(pde_ent, alloc_pt, PDE_ATTR_RW | PDE_ATTR_P | (uspace ? PDE_ATTR_US : 0)); + } + + arch_pte *pte_ent = (arch_pdpte*)arch_pmap_map(*pde_ent, ARCH_KPAGE_SZ) + PT_ENTRY_NUM(vaddr); + + if (*pte_ent != 0) { + BRUTE("vaddr 0x%p is already mapped", (void*)vaddr); + } + + uint64 pattr = 0; + + pattr |= (attr & ARCH_VADDR_ATTR_PRESENT ? PTE_ATTR_P : 0); + pattr |= (attr & ARCH_VADDR_ATTR_NX ? PTE_ATTR_NX : 0); + pattr |= (attr & ARCH_VADDR_ATTR_READONLY ? 0 : PTE_ATTR_RW); + pattr |= (attr & ARCH_VADDR_ATTR_UNCACHED ? PTE_ATTR_PCD : 0); + + arch_write_page_entry(pte_ent, paddr, pattr); + + return S_OK; +} + +int +arch_map_vaddr(void * base, mm_paddr paddr, uintptr vaddr, usize sz, uint attr) +{ + if ((sz & (ARCH_KPAGE_SZ - 1)) != 0) { + BRUTE("Unaligned size"); + } + + for(mm_paddr caddr = paddr; caddr < paddr + sz; caddr += ARCH_KPAGE_SZ) { + _map_page(base, paddr, vaddr, attr); + } + + return S_OK; +} diff --git a/arch/paging.h b/arch/paging.h new file mode 100644 index 0000000..41c84a3 --- /dev/null +++ b/arch/paging.h @@ -0,0 +1,93 @@ +#pragma once + +#include +#include + +typedef uint64_t arch_pml4e; +typedef uint64_t arch_pdpte; +typedef uint64_t arch_pde; +typedef uint64_t arch_pte; + +/** + Page Table Definitions +**/ + +#define PML4E_ATTR_P (1ul << 0u) +#define PML4E_ATTR_RW (1ul << 1u) +#define PML4E_ATTR_US (1ul << 2u) +#define PML4E_ATTR_PWT (1ul << 3u) +#define PML4E_ATTR_PCD (1ul << 4u) +#define PML4E_ATTR_A (1ul << 5u) +#define PML4E_ATTR_NX (1ul << 63u) + +#define PDPTE_ATTR_P (1ul << 0u) +#define PDPTE_ATTR_RW (1ul << 1u) +#define PDPTE_ATTR_US (1ul << 2u) +#define PDPTE_ATTR_PWT (1ul << 3u) +#define PDPTE_ATTR_PCD (1ul << 4u) +#define PDPTE_ATTR_A (1ul << 5u) +#define PDPTE_ATTR_D (1ul << 6u) +#define PDPTE_ATTR_PS (1ul << 7u) +#define PDPTE_ATTR_G (1ul << 8u) +#define PDPTE_ATTR_PAT (1ul << 12u) +#define PDPTE_ATTR_NX (1ul << 63u) + +#define PDE_ATTR_P (1ul << 0u) +#define PDE_ATTR_RW (1ul << 1u) +#define PDE_ATTR_US (1ul << 2u) +#define PDE_ATTR_PWT (1ul << 3u) +#define PDE_ATTR_PCD (1ul << 4u) +#define PDE_ATTR_A (1ul << 5u) +#define PDE_ATTR_D (1ul << 6u) +#define PDE_ATTR_PS (1ul << 7u) +#define PDE_ATTR_G (1ul << 8u) +#define PDE_ATTR_PAT (1ul << 12u) +#define PDE_ATTR_NX (1ul << 63u) + +#define PTE_ATTR_P (1ul << 0u) +#define PTE_ATTR_RW (1ul << 1u) +#define PTE_ATTR_US (1ul << 2u) +#define PTE_ATTR_PWT (1ul << 3u) +#define PTE_ATTR_PCD (1ul << 4u) +#define PTE_ATTR_A (1ul << 5u) +#define PTE_ATTR_D (1ul << 6u) +#define PTE_ATTR_PS (1ul << 7u) +#define PTE_ATTR_G (1ul << 8u) +#define PTE_ATTR_PAT (1ul << 12u) +#define PTE_ATTR_NX (1ul << 63u) + +#define PML4_ENTRY_NUM(vaddr) (((vaddr) >> 39u) & 0x1FFu) +#define PDPT_ENTRY_NUM(vaddr) (((vaddr) >> 30u) & 0x1FFu) +#define PD_ENTRY_NUM(vaddr) (((vaddr) >> 21u) & 0x1FFu) +#define PT_ENTRY_NUM(vaddr) (((vaddr) >> 12u) & 0x1FFu) + +#define PDPTE_MAPPING_SZ (PDE_MAPPING_SZ * 512) +#define PDE_MAPPING_SZ (PTE_MAPPING_SZ * 512) +#define PTE_MAPPING_SZ (ARCH_KPAGE_SZ) + +static inline void +arch_write_page_entry(uint64_t *base, mm_paddr offset, uint64_t attr) +{ + attr = (offset & 0xFFFFFFFFFF000ul) | attr; + memcpy(base, &attr, sizeof(uint64_t)); +} + +// trace the page table to see if there exists a pdpte entry for a given pml4 +// note _nr means this doesn't depend on the recursive page mapping +static inline int +arch_pdpte_exists_nr(arch_pml4e *pml4, mm_paddr paddr) +{ + +} + +static inline int +arch_pde_exists_nr() +{ + +} + +static inline int +arch_pte_exists_nr() +{ + +} diff --git a/arch/pmap.c b/arch/pmap.c new file mode 100644 index 0000000..c8ef6c6 --- /dev/null +++ b/arch/pmap.c @@ -0,0 +1,146 @@ +#include +#include +#include +#include + +#include "pmap_p.h" +#include "paging.h" + +struct arch_pmap_segs { + mm_paddr start; + mm_paddr stop; +}; + +// the physical memory segments information obtained from multiboot info +static struct arch_pmap_segs _phys_segs[ARCH_PMAP_MAX_PHYS_SEGS]; +static usize _phys_segs_sz = 0; + +// the base addr for mm_page structures +static mm_paddr _mm_pages_base; + +// initializes _pmap region +static void +_pmap_init(mm_paddr *cur_addr, arch_pml4e *kern_pml4) +{ + usize high_mem = _phys_segs[_phys_segs_sz - 1].stop; + + if (high_mem >= ARCH_ML_MAX_RAM) { + BRUTE("Only supports maximum %ld bytes RAM", ARCH_ML_MAX_RAM); + } + + kprintf("Total memory size: %ld bytes", high_mem + 1); + + // map all 1GB sections + usize num = ((high_mem + 1) & ~0x3FFFFFFFu) >> 30u; + for (usize i = 0; i < num; i++) { + kprintf(""); + } + + kprintf("pmap: 1GB segment"); + + // map all 2MB sections + + // map all 4KB sections +} + +static void +_mm_pg_init(mm_paddr *cur_addr, arch_pml4e *kern_pml4) +{ + +} + +// initializes kernel mapping +static void +_kern_mapping_init(mm_paddr *cur_addr, arch_pml4e *kern_pml4) +{ + // map the kernel with 2MB mapping now + KASSERT((ARCH_ML_KIMAGE_PADDR & (PDE_MAPPING_SZ - 1)) == 0, "kernel vaddr not 2MB aligned."); + KASSERT(((uintptr) KERN_IMG_START & (PDE_MAPPING_SZ - 1)) == 0, "kernel paddr not 2MB aligned."); + + const uintptr kern_map_end = ALIGN_UP2((uintptr) (ARCH_ML_KIMAGE_STOP), PDE_MAPPING_SZ); + + kprintf("kern_map_end: 0x%p", (void *) kern_map_end); + + *cur_addr = kern_map_end - KERN_BASE_START; + *kern_pml4 = arch_pmap_map(ARCH_ML_PMAP_START + *cur_addr, ARCH_KPAGE_SZ); + *cur_addr += ARCH_KPAGE_SZ; + + // pdpt for the kernel, kernel must be within the first GB so only 1 + arch_pdpte *kern_pdpt = cur_addr; + cur_addr += ARCH_KPAGE_SZ * kern_num_pdpt; + + arch_write_page_entry(kern_pml4 + PML4_ENTRY_NUM((uintptr) KERN_IMG_START), + kern_pdpt, PML4E_ATTR_P | PML4E_ATTR_RW); + // pd for the kernel + const uintptr kern_pd = cur_addr; + cur_addr += ARCH_KPAGE_SZ; + + kern_pml4 = cur_addr; + cur_addr += ARCH_KPAGE_SZ; +} + +// maps device memory +void * +arch_pmap_mapdev(ATTR_UNUSED uintptr paddr, ATTR_UNUSED usize size) +{ + return NULL; +} + +// maps a physical segment to pmap region +void * +arch_pmap_map(mm_paddr paddr, ATTR_UNUSED usize sz) +{ + return (void *) ARCH_PHYS_TO_PMAP(paddr); +} + +// obtains the paddr of the corresponding struct mm_page for a specific paddr +mm_paddr +arch_paddr_to_mm_page(mm_paddr paddr) +{ + return (paddr / ARCH_KPAGE_SZ) * sizeof(struct mm_page) + _mm_pages_base; +} + +// adds an available physical segment to _phys_segs +void +arch_mem_addseg(mm_paddr start, usize len) +{ + KASSERT(_phys_segs_sz < ARCH_PMAP_MAX_PHYS_SEGS, "too many physical segments!"); + _phys_segs[_phys_segs_sz].start = start; + _phys_segs[_phys_segs_sz].stop = start + len - 1; + _phys_segs_sz++; +} + +// sets up paging for kernel and mm_pages for mm_phys +// specifically, we do: +// 1. Map the kernel to KERN_BASE +// 2. Allocate mm_page for the all physical memory (avail and unavail) and put them after the kernel paddr +// 2.5. Map all mm_page (s) to KERN_MM_PAGE_START +// 3. Direct map all available physical memory to PMAP_BASE +// 4. Save the mapping and switch the page table to the new table +// 5. Save the information to mm_phys for future phys setup +void +arch_mem_init() +{ + // we use 2M (PDE) pages to map the kernel so align the physical address to 2MB + mm_paddr cur_addr = ALIGN_UP2(ARCH_ML_KIMAGE_PADDR, PDE_MAPPING_SZ); + + // allocate the pml4 for the kernel + arch_pml4e *kern_pml4 = (arch_pml4e *) ARCH_PHYS_TO_PMAP(cur_addr); + memset(kern_pml4, 0, ARCH_KPAGE_SZ); + cur_addr += ARCH_KPAGE_SZ; + + _kern_mapping_init(&cur_addr, kern_pml4); + _mm_pg_init(&cur_addr, kern_pml4); + _pmap_init(&cur_addr, kern_pml4); + + // copy the physical segments information to mm_phys + for (usize i = 0; i < _phys_segs_sz; i++) { + mm_phys_add_phys_seg(_phys_segs[i].start, _phys_segs[i].stop); + } + + // add reserved segment information to mm_phys + // we reserve everything from KERN_IMG_PADDR to what we've allocated so far + mm_phys_add_reserved_seg(ARCH_ML_KIMAGE_PADDR, cur_addr - ARCH_ML_KIMAGE_PADDR + 1); + // reserve the 0th page + mm_phys_add_reserved_seg(0, ARCH_KPAGE_SZ); +} diff --git a/arch/pmap_p.h b/arch/pmap_p.h new file mode 100644 index 0000000..fb26e5a --- /dev/null +++ b/arch/pmap_p.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +#define ARCH_PMAP_TO_PHYS(pmaddr) ((pmaddr) - ARCH_ML_PMAP_START) +#define ARCH_PHYS_TO_PMAP(paddr) (ARCH_ML_PMAP_START + (paddr)) + +void +arch_mem_init(); + +void +arch_mem_addseg(uintptr start, usize len); diff --git a/arch/print.c b/arch/print.c index dd2f047..cf15f76 100644 --- a/arch/print.c +++ b/arch/print.c @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #define FB_PADDR (0xb8000) #define FB_ROW (25) diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt new file mode 100644 index 0000000..0b6665d --- /dev/null +++ b/common/CMakeLists.txt @@ -0,0 +1,7 @@ +set(SUBMODULE common) +set(CC_SRC + avl_tree.c + ) + +include(${MK}/kern.cmake) + diff --git a/kern/avl_tree.c b/common/avl_tree.c similarity index 99% rename from kern/avl_tree.c rename to common/avl_tree.c index 32a1412..9940cbc 100644 --- a/kern/avl_tree.c +++ b/common/avl_tree.c @@ -1,7 +1,8 @@ -#include -#include -#include -#include +#include +#include + +#include +#include static inline struct avl_node * _avl_node_max(struct avl_node *node) diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..8132003 --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1 @@ +-Iinc \ No newline at end of file diff --git a/gdb_qemu.dbg b/gdb_qemu.dbg new file mode 100644 index 0000000..dbe9dca --- /dev/null +++ b/gdb_qemu.dbg @@ -0,0 +1,3 @@ +target remote localhost:1234 +symbol-file bond.elf +break arch_main diff --git a/inc/arch/intr.h b/inc/arch/intr.h index d6e34bd..3a54a62 100644 --- a/inc/arch/intr.h +++ b/inc/arch/intr.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include /** * Interrupt context structure diff --git a/inc/arch/mem.h b/inc/arch/mem.h deleted file mode 100644 index 3ca0f38..0000000 --- a/inc/arch/mem.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include -#include - -static inline void * -arch_pmap_map(uintptr paddr, ATTR_UNUSED usize size) -{ - return (void*)(paddr + KERN_PMAP_START); -} diff --git a/inc/arch/mlayout.h b/inc/arch/mlayout.h index afc0639..9091c7f 100644 --- a/inc/arch/mlayout.h +++ b/inc/arch/mlayout.h @@ -4,13 +4,20 @@ * Kernel Memory Layout * ----------------------- 0x0000,0000,0000,0000 - User Space * Application SIZE: 0x0000,8000,0000,0000 (256x PML4, 128TB) - * ----------------------- 0x0000,8000,0000,0000 + * ----------------------- 0x0000,7FFF,FFFF,FFFF * Non-canonical * ----------------------- 0xFFFF,8000,0000,0000 - Kernel Space * Unused * ----------------------- 0xFFFF,9000,0000,0000 * PMAP SIZE: 0x0000,0400,0000,0000 (8x PML4, 4TB) + * ----------------------- 0xFFFF,93FF,FFFF,FFFF * ----------------------- 0xFFFF,9400,0000,0000 + * MM_PAGE SIZE: 0x0000,0400,0000,0000 (8x PML4, 4TB) + * ----------------------- 0xFFFF,97FF,FFFF,FFFF + * ----------------------- 0xFFFF,9800,0000,0000 + * RPT (recursive page tables) + * SIZE: 0x0000,0080,0000,0000 (1x PML4, 512GB) + * ----------------------- 0xFFFF,987F,FFFF,FFFF * Unused * ----------------------- 0xFFFF,FFFF,8000,0000 * Kernel Image SIZE: 0x0000,0000,8000,0000 (2GB) @@ -26,22 +33,31 @@ #define KERN_IMG_PADDR 0x2000000 #define KERN_PAGE_SZ 0x1000 -#define KERN_PMAP_START 0xFFFF900000000000 -#define KERN_PMAP_STOP 0xFFFF940000000000 -#define KERN_BASE_START 0xFFFFFFFF80000000 -#define KERN_BASE_STOP 0x0000000000000000 - +#define KERN_PMAP_START 0xFFFF900000000000 +#define KERN_PMAP_STOP 0xFFFF93FFFFFFFFFF +#define KERN_MM_PAGE_START 0xFFFF940000000000 +#define KERN_MM_PAGE_STOP 0xFFFF97FFFFFFFFFF +#define KERN_RPT_START 0xFFFF980000000000 +#define KERN_RPT_STOP 0xFFFF987FFFFFFFFF +#define KERN_BASE_START 0xFFFFFFFF80000000 +#define KERN_BASE_STOP 0xFFFFFFFFFFFFFFFF #else -#define KERN_IMG_PADDR (0x2000000) -#define KERN_PAGE_SZ (0x1000) +#define ARCH_ML_KIMAGE_PADDR (0x2000000u) +#define ARCH_KPAGE_SZ (0x1000u) -extern const char KERN_IMG_START[]; -extern const char KERN_IMG_STOP[]; +extern const char ARCH_ML_KIMAGE_START[]; +extern const char ARCH_ML_KIMAGE_STOP[]; -#define KERN_PMAP_START (0xFFFF900000000000) -#define KERN_PMAP_STOP (0xFFFF940000000000) -#define KERN_BASE_START (0xFFFFFFFF80000000) -#define KERN_BASE_STOP (0x0000000000000000) +#define ARCH_ML_USPACE_END (0x00007FFFFFFFFFFFu) +#define ARCH_ML_KSPACE_START (0xFFFF800000000000u) +#define ARCH_ML_PMAP_START (0xFFFF900000000000u) +#define ARCH_ML_PMAP_STOP (0xFFFF93FFFFFFFFFFu) +#define ARCH_ML_MMPAGE_START (0xFFFF940000000000u) +#define ARCH_ML_MMPAGE_STOP (0xFFFF97FFFFFFFFFFu) +#define ARCH_ML_KBASE_START (0xFFFFFFFF80000000u) +#define ARCH_ML_KBASE_STOP (0xFFFFFFFFFFFFFFFFu) + +#define ARCH_ML_MAX_RAM (ARCH_ML_PMAP_STOP - ARCH_ML_PMAP_START + 1) #endif diff --git a/inc/arch/pmap.h b/inc/arch/pmap.h new file mode 100644 index 0000000..7a7c303 --- /dev/null +++ b/inc/arch/pmap.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include + +#define ARCH_PMAP_MAX_PHYS_SEGS (64) + +// on x86-64 we always keep full direct mapping in the kernel +// maps normal memory (cacheable) +void * +arch_pmap_map(mm_paddr paddr, usize size); + +mm_paddr +arch_paddr_to_mm_page(mm_paddr paddr); + +// maps device memory (uncacheable) +void * +arch_pmap_mapdev(mm_paddr paddr, usize size); + +#define ARCH_VADDR_ATTR_PRESENT (0x1u) +#define ARCH_VADDR_ATTR_UNCACHED (0x2u) +#define ARCH_VADDR_ATTR_READONLY (0x4u) +#define ARCH_VADDR_ATTR_NX (0x8u) +int +arch_map_vaddr(void * base, mm_paddr paddr, uintptr vaddr, usize sz, uint attr); + diff --git a/inc/arch/print.h b/inc/arch/print.h index 8c15112..7cdadea 100644 --- a/inc/arch/print.h +++ b/inc/arch/print.h @@ -1,5 +1,5 @@ #pragma once -#include +#include #include void diff --git a/inc/kern/avl_tree.h b/inc/common/avl_tree.h similarity index 98% rename from inc/kern/avl_tree.h rename to inc/common/avl_tree.h index 869adb8..2b706c7 100644 --- a/inc/kern/avl_tree.h +++ b/inc/common/avl_tree.h @@ -1,6 +1,6 @@ #pragma once -#include +#include struct avl_node { diff --git a/inc/kern/bitset.h b/inc/common/bitset.h similarity index 100% rename from inc/kern/bitset.h rename to inc/common/bitset.h diff --git a/inc/kern/cdef.h b/inc/common/cdef.h similarity index 92% rename from inc/kern/cdef.h rename to inc/common/cdef.h index c233398..263a27d 100644 --- a/inc/kern/cdef.h +++ b/inc/common/cdef.h @@ -20,6 +20,7 @@ typedef size_t usize; typedef unsigned char uchar; typedef unsigned long ulong; typedef unsigned int uint; +typedef unsigned short ushort; #define KABI __attribute__((sysv_abi)) #define STATIC_ASSERT(cond, msg) _Static_assert((cond), msg) @@ -33,7 +34,7 @@ typedef unsigned int uint; #define ATTR_UNUSED __attribute__((unused)) #define ATTR_USED __attribute__((used)) #define ATTR_SECTION(x) __attribute__ ((section (#x))) -#define ATTR_ALIGN(x) _Alignas(x) +#define ATTR_ALIGN(x) __attribute__((aligned (x))) #define ATTR_FMT_PRINTF __attribute__((format (printf, 1, 2))) #define BOCHS_BREAK __asm__("xchg %bx, %bx") diff --git a/inc/kern/lds.h b/inc/common/lds.h similarity index 91% rename from inc/kern/lds.h rename to inc/common/lds.h index 44f7fc5..2ae09a2 100644 --- a/inc/kern/lds.h +++ b/inc/common/lds.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #define LDS_DECL(name) \ extern const char __start_##name[]; \ diff --git a/inc/kern/libkern.h b/inc/common/libkern.h similarity index 92% rename from inc/kern/libkern.h rename to inc/common/libkern.h index 7e64cb2..424aa79 100644 --- a/inc/kern/libkern.h +++ b/inc/common/libkern.h @@ -1,6 +1,6 @@ #pragma once -#include +#include /* * Common macros, etc @@ -8,7 +8,9 @@ #define OBTAIN_STRUCT_ADDR(member_addr, struct_name, member_name) ((struct_name*)((uintptr)(member_addr) - (uintptr)(&(((struct_name*)0)->member_name)))) -#define CEIL(num, div) \ +#define ALIGN_UP2(num, round) (((num) + (round) - 1) & ~((round) - 1)) + +#define DIV_CEIL(num, div) \ ({ __typeof__(num) _num = (num); \ __typeof__(div) _div = (div); \ ((_num + _div - 1) / _div); }) diff --git a/inc/kern/list.h b/inc/common/list.h similarity index 95% rename from inc/kern/list.h rename to inc/common/list.h index 9c18102..fbadced 100644 --- a/inc/kern/list.h +++ b/inc/common/list.h @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include struct list_entry { struct list_entry *prev; diff --git a/inc/ke/brute.h b/inc/ke/brute.h new file mode 100644 index 0000000..d4ea66f --- /dev/null +++ b/inc/ke/brute.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include +#include + +#define BRUTE(fmt, ...) do { \ + kprintf("Kernel brute at %s:%d: " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \ + arch_brute(); \ +} while(0) + +#define KASSERT(expr, msg, ...) do { \ + if (!(expr)) { \ + BRUTE("Assertion \"" #expr "\" failed: " msg , ##__VA_ARGS__); \ + } \ +} while(0) diff --git a/inc/kern/kinit.h b/inc/ke/kinit.h similarity index 96% rename from inc/kern/kinit.h rename to inc/ke/kinit.h index a3dd42e..5068a32 100644 --- a/inc/kern/kinit.h +++ b/inc/ke/kinit.h @@ -1,6 +1,6 @@ #pragma once -#include +#include typedef void (kinitf)(void*); diff --git a/inc/kern/poison.h b/inc/ke/poison.h similarity index 100% rename from inc/kern/poison.h rename to inc/ke/poison.h diff --git a/inc/kern/print.h b/inc/ke/print.h similarity index 70% rename from inc/kern/print.h rename to inc/ke/print.h index b164701..bc68e0b 100644 --- a/inc/kern/print.h +++ b/inc/ke/print.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include ATTR_FMT_PRINTF int kprintf(const char *fmt, ...); diff --git a/inc/ke/spin_lock.h b/inc/ke/spin_lock.h new file mode 100644 index 0000000..6c11695 --- /dev/null +++ b/inc/ke/spin_lock.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +// implements a simple ticket lock +struct ke_spin_lock { + // LOW 16 bits: cur ticket + // HIGH 16 bits: cur owner + DECL_ATOMIC(uint32) val; +}; + +#define KE_SPIN_LOCK_INITIALIZER {.val = ATOMIC_VAR_INIT(0)} + +STATIC_ASSERT(sizeof(struct ke_spin_lock) == sizeof(uint32), "ke_spin_lock size isn't 32 bits"); + +void +ke_spin_lock_init(struct ke_spin_lock *lock); + +void +ke_spin_lock_acq(struct ke_spin_lock *lock); + +void +ke_spin_lock_rel(struct ke_spin_lock *lock); + +// returns non-zero on success otherwise zero +int +ke_spin_lock_try_acq(struct ke_spin_lock *lock); diff --git a/inc/kern/status.h b/inc/ke/status.h similarity index 85% rename from inc/kern/status.h rename to inc/ke/status.h index 2d29681..60496c7 100644 --- a/inc/kern/status.h +++ b/inc/ke/status.h @@ -1,6 +1,6 @@ #pragma once -#include +#include /** * Specific error codes diff --git a/inc/kern/brute.h b/inc/kern/brute.h deleted file mode 100644 index e91bb68..0000000 --- a/inc/kern/brute.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include -#include -#include - -#define BRUTE(fmt, ...) do { \ - kprintf("Kernel brute: " fmt "\n", ##__VA_ARGS__); \ - arch_brute(); \ -} while(0) - -#define KASSERT(expr, msg, ...) do { \ - if (!(expr)) { \ - BRUTE("Assertion \"" #expr "\" failed at %s:%d: " msg , __FILE__, __LINE__ , ##__VA_ARGS__); \ - } \ -} while(0) diff --git a/inc/kern/spin_lock.h b/inc/kern/spin_lock.h deleted file mode 100644 index 8706937..0000000 --- a/inc/kern/spin_lock.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include - -// implements a simple ticket lock -struct spin_lock { - // LOW 16 bits: cur ticket - // HIGH 16 bits: cur owner - DECL_ATOMIC(uint32) val; -}; - -#define SPIN_LOCK_INITIALIZER {.val = ATOMIC_VAR_INIT(0)} - -STATIC_ASSERT(sizeof(struct spin_lock) == sizeof(uint32), "spin_lock size isn't 32 bits"); - -void -spin_lock_init(struct spin_lock *lock); - -void -spin_lock_acq(struct spin_lock *lock); - -void -spin_lock_rel(struct spin_lock *lock); - -// returns non-zero on success otherwise zero -int -spin_lock_try_acq(struct spin_lock *lock); diff --git a/inc/mm/phys.h b/inc/mm/phys.h new file mode 100644 index 0000000..2259e15 --- /dev/null +++ b/inc/mm/phys.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#include + +typedef uintptr mm_paddr; + +struct mm_page { + struct ke_spin_lock page_lock; // page lock + mm_paddr phys_addr; // physical address of the page + uint8 phys_order; // order of the page in the buddy allocator + uint8 phys_pool; // which pool it belongs to in the buddy allocator + struct list_entry phys_flist_ent; // list entry for the free list in the buddy allocator +}; + +#define MM_PHYS_ORDER_MAX (10) +#define MM_PHYS_ORDER_FREE (MM_PAGE_ORDER_MAX + 1) + +#define MM_PHYS_POOL_DMA (0) +#define MM_PHYS_POOL_GENERIC (1) +#define MM_PHYS_MAX_POOLS (MM_PHYS_POOL_DMA + 1) + +void +mm_phys_add_phys_seg(mm_paddr start, usize len); + +void +mm_phys_add_reserved_seg(mm_paddr start, usize len); diff --git a/inc/test/ktest.h b/inc/test/ktest.h index c75ecdb..bfb9042 100644 --- a/inc/test/ktest.h +++ b/inc/test/ktest.h @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include LDS_DECL(ktest); diff --git a/kern/CMakeLists.txt b/ke/CMakeLists.txt similarity index 75% rename from kern/CMakeLists.txt rename to ke/CMakeLists.txt index e893f66..892420c 100644 --- a/kern/CMakeLists.txt +++ b/ke/CMakeLists.txt @@ -1,6 +1,5 @@ -set(SUBMODULE kern) +set(SUBMODULE ke) set(CC_SRC - avl_tree.c libkern.c kmain.c print.c diff --git a/kern/kmain.c b/ke/kmain.c similarity index 74% rename from kern/kmain.c rename to ke/kmain.c index d429df1..9e3d0bc 100644 --- a/kern/kmain.c +++ b/ke/kmain.c @@ -1,7 +1,7 @@ -#include -#include -#include -#include +#include +#include +#include +#include static int kinit_cmpf(const void *ki1, const void *ki2) @@ -26,10 +26,9 @@ init_kinit() * Kernel entry point * @param boot_info passed by the bootloader */ -ATTR_UNUSED void KABI -kmain(ATTR_UNUSED void *boot_info) +ATTR_USED void +kmain() { - KASSERT(boot_info != NULL, "bootinfo is NULL"); init_kinit(); BRUTE("Control reached end of kmain"); } diff --git a/kern/libkern.c b/ke/libkern.c similarity index 98% rename from kern/libkern.c rename to ke/libkern.c index 398087e..7001405 100644 --- a/kern/libkern.c +++ b/ke/libkern.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include void memswp(void *dst, void *src, usize size) diff --git a/kern/print.c b/ke/print.c similarity index 91% rename from kern/print.c rename to ke/print.c index 212206e..992c2a6 100644 --- a/kern/print.c +++ b/ke/print.c @@ -1,13 +1,13 @@ -#include +#include #include -#include -#include +#include +#include /* max space needed for each byte is when printing it in binary = 8 bits */ #define NBUF_SZ (sizeof(uintmax) * 8) static char nbuf[NBUF_SZ]; -static struct spin_lock print_lock = SPIN_LOCK_INITIALIZER; +static struct ke_spin_lock print_lock = KE_SPIN_LOCK_INITIALIZER; static int _printu(char *buf, uintmax num, uint base, int cap) @@ -64,6 +64,7 @@ _vprintf(const char *fmt, va_list args) case 'p': sz_ptr = 1; base = 16; + usignf = 1; goto pnum; case 'd': goto pnum; @@ -146,9 +147,9 @@ kprintf(const char *fmt, ...) va_list args; va_start(args, fmt); - spin_lock_acq(&print_lock); + ke_spin_lock_acq(&print_lock); ret = _vprintf(fmt, args); - spin_lock_rel(&print_lock); + ke_spin_lock_rel(&print_lock); va_end(args); return ret; @@ -159,9 +160,9 @@ kvprintf(const char *fmt, va_list args) { int ret; - spin_lock_acq(&print_lock); + ke_spin_lock_acq(&print_lock); ret = _vprintf(fmt, args); - spin_lock_rel(&print_lock); + ke_spin_lock_rel(&print_lock); return ret; } diff --git a/kern/spin_lock.c b/ke/spin_lock.c similarity index 79% rename from kern/spin_lock.c rename to ke/spin_lock.c index ad724c1..204654d 100644 --- a/kern/spin_lock.c +++ b/ke/spin_lock.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include static inline uint32 _spin_lock_get_ticket(uint32 val) @@ -14,13 +14,13 @@ _spin_lock_get_owner(uint32 val) } void -spin_lock_init(struct spin_lock *lock) +ke_spin_lock_init(struct ke_spin_lock *lock) { atomic_store(&lock->val, 0); } void -spin_lock_acq(struct spin_lock *lock) +ke_spin_lock_acq(struct ke_spin_lock *lock) { uint32 val; @@ -37,14 +37,14 @@ spin_lock_acq(struct spin_lock *lock) } void -spin_lock_rel(struct spin_lock *lock) +ke_spin_lock_rel(struct ke_spin_lock *lock) { // increment ticket atomic_fetch_add(&lock->val, 1); } int -spin_lock_try_acq(struct spin_lock *lock) +ke_spin_lock_try_acq(struct ke_spin_lock *lock) { uint32 val; diff --git a/mm/CMakeLists.txt b/mm/CMakeLists.txt new file mode 100644 index 0000000..33d4666 --- /dev/null +++ b/mm/CMakeLists.txt @@ -0,0 +1,7 @@ +set(SUBMODULE mm) +set(CC_SRC + phys.c + ) + +include(${MK}/kern.cmake) + diff --git a/mm/phys.c b/mm/phys.c new file mode 100644 index 0000000..f253b12 --- /dev/null +++ b/mm/phys.c @@ -0,0 +1,38 @@ +#include +#include + +struct mm_phys_seg { + mm_paddr start; + mm_paddr stop; +}; + +static ATTR_UNUSED struct list_entry _freelist[MM_PHYS_MAX_POOLS][MM_PHYS_ORDER_MAX]; + +static struct mm_phys_seg _phys_segs[ARCH_PMAP_MAX_PHYS_SEGS]; +static usize _phys_segs_sz = 0; + +static struct mm_phys_seg _reserve_segs[ARCH_PMAP_MAX_PHYS_SEGS]; +static usize _reserve_segs_sz = 0; + +static void +_set_phys_seg(struct mm_phys_seg *seg, mm_paddr start, usize len) +{ + seg->start = start; + seg->stop = start + len - 1; +} + +void +mm_phys_add_phys_seg(mm_paddr start, usize len) +{ + KASSERT(_phys_segs_sz < ARCH_PMAP_MAX_PHYS_SEGS, "too many physical segments!"); + _set_phys_seg(&_phys_segs[_phys_segs_sz], start, len); + _phys_segs_sz++; +} + +void +mm_phys_add_reserved_seg(mm_paddr start, usize len) +{ + KASSERT(_reserve_segs_sz < ARCH_PMAP_MAX_PHYS_SEGS, "too many reserved segments!"); + _set_phys_seg(&_reserve_segs[_reserve_segs_sz], start, len); + _reserve_segs_sz++; +} diff --git a/scripts/linker.lds b/scripts/linker.lds index 0ed693b..e04ed3d 100644 --- a/scripts/linker.lds +++ b/scripts/linker.lds @@ -31,5 +31,5 @@ SECTIONS *(COMMON) } - KERN_IMG_STOP = .; + ARCH_ML_KIMAGE_STOP = .; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0d7651f..fc62ad8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -7,4 +7,3 @@ set(CC_SRC ) include(${MK}/kern.cmake) - diff --git a/test/avl_test.c b/test/avl_test.c index 3d3f660..8ec833e 100644 --- a/test/avl_test.c +++ b/test/avl_test.c @@ -1,8 +1,8 @@ -#include -#include -#include +#include +#include +#include #include -#include +#include struct test_node { struct avl_node tree_entry; diff --git a/test/ktest.c b/test/ktest.c index cb22460..0f588db 100644 --- a/test/ktest.c +++ b/test/ktest.c @@ -1,7 +1,7 @@ #include -#include -#include -#include +#include +#include +#include static uint ktest_cases = 0; diff --git a/test/list_test.c b/test/list_test.c index 2781bd5..d06386d 100644 --- a/test/list_test.c +++ b/test/list_test.c @@ -1,7 +1,7 @@ #include -#include -#include -#include +#include +#include +#include #define ARR_SZ(arr) (sizeof(arr) / sizeof((arr)[0])) diff --git a/test/qsort_test.c b/test/qsort_test.c index 54bc745..4bfbf07 100644 --- a/test/qsort_test.c +++ b/test/qsort_test.c @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include #define MAX_ELE (10)