From 7c7b5f05d39b7739d990f71256a2267ec67a6913 Mon Sep 17 00:00:00 2001 From: Minep Date: Thu, 24 Aug 2023 13:46:14 +0100 Subject: [PATCH] refactor: use a more decent physical memory map fix: pde's perm overrides pte's when pte has lower access level. feat: template generator now with infinite horizon when inferencing the memory region's base and size --- lunaix-os/arch/i386/boot/kpt_setup.c | 1 - lunaix-os/arch/i386/hal/ioapic.c | 1 - lunaix-os/arch/i386/includes/sys/apic.h | 1 - lunaix-os/arch/i386/includes/sys/mm/mempart.h | 12 +++ lunaix-os/arch/i386/mm/pfault.c | 1 - lunaix-os/includes/lunaix/common.h | 45 ---------- lunaix-os/includes/lunaix/ds/llist.h | 2 +- lunaix-os/includes/lunaix/mm/page.h | 1 - lunaix-os/includes/lunaix/mm/vmm.h | 63 ++++++++++--- lunaix-os/includes/lunaix/types.h | 14 +++ lunaix-os/kernel/debug/trace.c | 2 +- lunaix-os/kernel/exe/elf32/elf32bfmt.c | 1 - lunaix-os/kernel/exe/elf32/ldelf32.c | 4 +- lunaix-os/kernel/exe/exec.c | 3 +- lunaix-os/kernel/kinit.c | 13 +-- lunaix-os/kernel/mm/cake.c | 2 +- lunaix-os/kernel/mm/mmap.c | 10 +-- lunaix-os/kernel/mm/mmio.c | 2 +- lunaix-os/kernel/mm/vmap.c | 90 +++++++++++++++++-- lunaix-os/kernel/mm/vmm.c | 12 ++- lunaix-os/kernel/peripheral/ps2kbd.c | 3 +- lunaix-os/kernel/proc0.c | 1 - lunaix-os/kernel/process/process.c | 7 +- lunaix-os/kernel/process/signal.c | 10 ++- lunaix-os/kernel/tty/tty.c | 1 - lunaix-os/link/linker.ld | 25 ++++-- lunaix-os/scripts/expand.py | 32 +++---- lunaix-os/scripts/templates/i386/config.json | 13 +++ 28 files changed, 246 insertions(+), 126 deletions(-) delete mode 100644 lunaix-os/includes/lunaix/common.h diff --git a/lunaix-os/arch/i386/boot/kpt_setup.c b/lunaix-os/arch/i386/boot/kpt_setup.c index 741731c..50d95ee 100644 --- a/lunaix-os/arch/i386/boot/kpt_setup.c +++ b/lunaix-os/arch/i386/boot/kpt_setup.c @@ -1,6 +1,5 @@ #define __BOOT_CODE__ -#include #include #include diff --git a/lunaix-os/arch/i386/hal/ioapic.c b/lunaix-os/arch/i386/hal/ioapic.c index 5d233f4..7e7800d 100644 --- a/lunaix-os/arch/i386/hal/ioapic.c +++ b/lunaix-os/arch/i386/hal/ioapic.c @@ -1,7 +1,6 @@ #include #include -#include #include #include diff --git a/lunaix-os/arch/i386/includes/sys/apic.h b/lunaix-os/arch/i386/includes/sys/apic.h index ce62af2..5583190 100644 --- a/lunaix-os/arch/i386/includes/sys/apic.h +++ b/lunaix-os/arch/i386/includes/sys/apic.h @@ -2,7 +2,6 @@ #define __LUNAIX_APIC_H #include -#include #include #define __APIC_BASE_PADDR 0xFEE00000 diff --git a/lunaix-os/arch/i386/includes/sys/mm/mempart.h b/lunaix-os/arch/i386/includes/sys/mm/mempart.h index 33d5c8b..3f52763 100644 --- a/lunaix-os/arch/i386/includes/sys/mm/mempart.h +++ b/lunaix-os/arch/i386/includes/sys/mm/mempart.h @@ -13,6 +13,18 @@ #define KERNEL_STACK_SIZE 0x100000UL #define KERNEL_STACK_END 0x3ffff0UL +#define USR_EXEC 0x400000UL +#define USR_EXEC_SIZE 0x20000000UL +#define USR_EXEC_END 0x203fffffUL + +#define USR_MMAP 0x20400000UL +#define USR_MMAP_SIZE 0x9f800000UL +#define USR_MMAP_END 0xbfbfffffUL + +#define USR_STACK 0xbfc00000UL +#define USR_STACK_SIZE 0x400000UL +#define USR_STACK_END 0xbffffff0UL + #define KERNEL_EXEC 0xc0000000UL #define KERNEL_EXEC_SIZE 0x4000000UL #define KERNEL_EXEC_END 0xc3ffffffUL diff --git a/lunaix-os/arch/i386/mm/pfault.c b/lunaix-os/arch/i386/mm/pfault.c index b028d84..2a2c6f3 100644 --- a/lunaix-os/arch/i386/mm/pfault.c +++ b/lunaix-os/arch/i386/mm/pfault.c @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/lunaix-os/includes/lunaix/common.h b/lunaix-os/includes/lunaix/common.h deleted file mode 100644 index cb827fb..0000000 --- a/lunaix-os/includes/lunaix/common.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef __LUNAIX_CONSTANTS_H -#define __LUNAIX_CONSTANTS_H - -#define MEM_1MB 0x100000 -#define MEM_4MB 0x400000 - -#define USER_START 0x400000 - -#define KSTACK_SIZE MEM_1MB -#define KSTACK_START (USER_START - KSTACK_SIZE) -#define KSTACK_TOP ((USER_START - 1) & ~0xf) -#define within_kstack(addr) (KSTACK_START <= (addr) && (addr) <= KSTACK_TOP) - -#define VGA_FRAMEBUFFER 0xB8000 - -#define KCODE_SEG 0x08 -#define KDATA_SEG 0x10 - -#define KSIZE (MEM_4MB * 16) - -#define USTACK_SIZE MEM_4MB -#define USTACK_TOP 0x9ffffff0 -#define USTACK_END (0x9fffffff - USTACK_SIZE + 1) -#define UMMAP_START 0x4D000000 -#define UMMAP_END (USTACK_END - MEM_4MB) - -#ifndef __ASM__ -#include -// From Linux kernel v2.6.0 -/** - * container_of - cast a member of a structure out to the containing structure - * - * @ptr: the pointer to the member. - * @type: the type of the container struct this is embedded in. - * @member: the name of the member within the struct. - * - */ -#define container_of(ptr, type, member) \ - ({ \ - const typeof(((type*)0)->member)* __mptr = (ptr); \ - (ptr) ? (type*)((char*)__mptr - offsetof(type, member)) : 0; \ - }) - -#endif -#endif /* __LUNAIX_CONSTANTS_H */ diff --git a/lunaix-os/includes/lunaix/ds/llist.h b/lunaix-os/includes/lunaix/ds/llist.h index 65ba474..ecce44a 100644 --- a/lunaix-os/includes/lunaix/ds/llist.h +++ b/lunaix-os/includes/lunaix/ds/llist.h @@ -12,7 +12,7 @@ #ifndef __LUNAIX_LLIST_H #define __LUNAIX_LLIST_H -#include +#include struct llist_header { diff --git a/lunaix-os/includes/lunaix/mm/page.h b/lunaix-os/includes/lunaix/mm/page.h index 7b8ba22..c28cca0 100644 --- a/lunaix-os/includes/lunaix/mm/page.h +++ b/lunaix-os/includes/lunaix/mm/page.h @@ -1,6 +1,5 @@ #ifndef __LUNAIX_PAGE_H #define __LUNAIX_PAGE_H -#include #include #define PG_SIZE_BITS 12 diff --git a/lunaix-os/includes/lunaix/mm/vmm.h b/lunaix-os/includes/lunaix/mm/vmm.h index 3f102db..ec420a2 100644 --- a/lunaix-os/includes/lunaix/mm/vmm.h +++ b/lunaix-os/includes/lunaix/mm/vmm.h @@ -2,8 +2,7 @@ #define __LUNAIX_VMM_H #include #include -#include -#include +#include // Virtual memory manager #define VMAP_NULL 0 @@ -118,16 +117,6 @@ vmm_ioremap(ptr_t paddr, size_t size); void* vmm_next_free(ptr_t start, int options); -/** - * @brief 将连续的物理地址空间映射到内核虚拟地址空间 - * - * @param paddr 物理地址空间的基地址 - * @param size 物理地址空间的大小 - * @return void* - */ -void* -vmm_vmap(ptr_t paddr, size_t size, pt_attr attr); - /** * @brief 将当前地址空间的虚拟地址转译为物理地址。 * @@ -147,4 +136,54 @@ vmm_v2p(ptr_t va); ptr_t vmm_v2pat(ptr_t mnt, ptr_t va); +/* + 表示一个 vmap 区域 + (One must not get confused with vmap_area in Linux!) +*/ +struct vmap_area +{ + ptr_t start; + size_t size; + pt_attr area_attr; +}; + +/** + * @brief 将连续的物理地址空间映射到内核虚拟地址空间 + * + * @param paddr 物理地址空间的基地址 + * @param size 物理地址空间的大小 + * @return void* + */ +void* +vmap(ptr_t paddr, size_t size, pt_attr attr, int flags); + +/** + * @brief 创建一个 vmap 区域 + * + * @param paddr + * @param attr + * @return ptr_t + */ +struct vmap_area* +vmap_varea(size_t size, pt_attr attr); + +/** + * @brief 在 vmap区域内映射一个单页 + * + * @param paddr + * @param attr + * @return ptr_t + */ +ptr_t +vmap_area_page(struct vmap_area* area, ptr_t paddr, pt_attr attr); + +/** + * @brief 在 vmap区域删除一个已映射的页 + * + * @param paddr + * @return ptr_t + */ +ptr_t +vmap_area_rmpage(struct vmap_area* area, ptr_t vaddr); + #endif /* __LUNAIX_VMM_H */ diff --git a/lunaix-os/includes/lunaix/types.h b/lunaix-os/includes/lunaix/types.h index 8b0c0d6..a6af3c1 100644 --- a/lunaix-os/includes/lunaix/types.h +++ b/lunaix-os/includes/lunaix/types.h @@ -27,4 +27,18 @@ typedef unsigned int cpu_t; typedef u64_t lba_t; +/** + * container_of - cast a member of a structure out to the containing structure + * + * @ptr: the pointer to the member. + * @type: the type of the container struct this is embedded in. + * @member: the name of the member within the struct. + * + */ +#define container_of(ptr, type, member) \ + ({ \ + const typeof(((type*)0)->member)* __mptr = (ptr); \ + (ptr) ? (type*)((char*)__mptr - offsetof(type, member)) : 0; \ + }) + #endif /* __LUNAIX_TYPES_H */ diff --git a/lunaix-os/kernel/debug/trace.c b/lunaix-os/kernel/debug/trace.c index ae183d4..3d712c0 100644 --- a/lunaix-os/kernel/debug/trace.c +++ b/lunaix-os/kernel/debug/trace.c @@ -27,7 +27,7 @@ trace_modksyms_init(struct boot_handoff* bhctx) ptr_t end = ROUNDUP(mod->end, PG_SIZE); ptr_t ksym_va = - (ptr_t)vmm_vmap(mod->start, (end - mod->start), PG_PREM_R); + (ptr_t)vmap(mod->start, (end - mod->start), PG_PREM_R, 0); assert(ksym_va); trace_ctx.ksym_table = (struct ksyms*)ksym_va; diff --git a/lunaix-os/kernel/exe/elf32/elf32bfmt.c b/lunaix-os/kernel/exe/elf32/elf32bfmt.c index 8d28143..3fb671c 100644 --- a/lunaix-os/kernel/exe/elf32/elf32bfmt.c +++ b/lunaix-os/kernel/exe/elf32/elf32bfmt.c @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/lunaix-os/kernel/exe/elf32/ldelf32.c b/lunaix-os/kernel/exe/elf32/ldelf32.c index 97db4bd..3dff1a8 100644 --- a/lunaix-os/kernel/exe/elf32/ldelf32.c +++ b/lunaix-os/kernel/exe/elf32/ldelf32.c @@ -5,6 +5,8 @@ #include #include +#include + int elf32_smap(struct load_context* ldctx, const struct elf32* elf, @@ -102,7 +104,7 @@ load_executable(struct load_context* context, const struct v_file* exefile) goto done_close_elf32; } - load_base = UMMAP_START; + load_base = USR_MMAP; } context->entry = elf.eheader.e_entry + load_base; diff --git a/lunaix-os/kernel/exe/exec.c b/lunaix-os/kernel/exe/exec.c index df47bb7..7afd0e1 100644 --- a/lunaix-os/kernel/exe/exec.c +++ b/lunaix-os/kernel/exe/exec.c @@ -11,6 +11,7 @@ #include #include +#include #include @@ -94,7 +95,7 @@ exec_load(struct exec_container* container, struct v_file* executable) if (container->vms_mnt == VMS_SELF) { // we are loading executable into current addr space - ptr_t ustack = USTACK_TOP; + ptr_t ustack = USR_STACK_END; size_t argv_len = 0, envp_len = 0; ptr_t argv_ptr = 0, envp_ptr = 0; diff --git a/lunaix-os/kernel/kinit.c b/lunaix-os/kernel/kinit.c index 607ba20..6b6e5e4 100644 --- a/lunaix-os/kernel/kinit.c +++ b/lunaix-os/kernel/kinit.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -55,7 +54,7 @@ kernel_bootstrap(struct boot_handoff* bhctx) trace_modksyms_init(bhctx); // crt - tty_init(ioremap(VGA_FRAMEBUFFER, PG_SIZE)); + tty_init(ioremap(0xB8000, PG_SIZE)); tty_set_theme(VGA_COLOR_WHITE, VGA_COLOR_BLACK); lxconsole_init(); @@ -138,16 +137,12 @@ spawn_proc0() cpu_chvmspace(proc0->page_table); // 为内核创建一个专属栈空间。 - for (size_t i = 0; i < (KSTACK_SIZE >> PG_SIZE_BITS); i++) { + for (size_t i = 0; i < KERNEL_STACK_SIZE; i += PG_SIZE) { ptr_t pa = pmm_alloc_page(KERNEL_PID, 0); - vmm_set_mapping(VMS_SELF, - KSTACK_START + (i << PG_SIZE_BITS), - pa, - PG_PREM_RW, - VMAP_NULL); + vmm_set_mapping(VMS_SELF, KERNEL_STACK + i, pa, PG_PREM_RW, VMAP_NULL); } - proc_init_transfer(proc0, KSTACK_TOP, (ptr_t)__proc0, 0); + proc_init_transfer(proc0, KERNEL_STACK_END, (ptr_t)__proc0, 0); // 向调度器注册进程。 commit_process(proc0); diff --git a/lunaix-os/kernel/mm/cake.c b/lunaix-os/kernel/mm/cake.c index 6434303..0e8f334 100644 --- a/lunaix-os/kernel/mm/cake.c +++ b/lunaix-os/kernel/mm/cake.c @@ -32,7 +32,7 @@ __alloc_cake(unsigned int cake_pg) if (!pa) { return NULL; } - return vmm_vmap(pa, cake_pg * PG_SIZE, PG_PREM_RW); + return vmap(pa, cake_pg * PG_SIZE, PG_PREM_RW, 0); } struct cake_s* diff --git a/lunaix-os/kernel/mm/mmap.c b/lunaix-os/kernel/mm/mmap.c index 8a4c9bc..d183c63 100644 --- a/lunaix-os/kernel/mm/mmap.c +++ b/lunaix-os/kernel/mm/mmap.c @@ -10,7 +10,7 @@ #include // any size beyond this is bullshit -#define BS_SIZE (KERNEL_EXEC - UMMAP_START) +#define BS_SIZE (KERNEL_EXEC - USR_MMAP) int mem_has_overlap(vm_regions_t* regions, ptr_t start, ptr_t end) @@ -66,7 +66,7 @@ mem_map(void** addr_out, { assert_msg(addr, "addr can not be NULL"); - ptr_t last_end = USER_START, found_loc = addr; + ptr_t last_end = USR_EXEC, found_loc = addr; struct mm_region *pos, *n; vm_regions_t* vm_regions = ¶m->pvms->regions; @@ -103,7 +103,7 @@ mem_map(void** addr_out, return ENOMEM; found: - if (found_loc >= KERNEL_EXEC || found_loc < USER_START) { + if (found_loc >= KERNEL_EXEC || found_loc < USR_EXEC) { return ENOMEM; } @@ -306,8 +306,8 @@ __DEFINE_LXSYSCALL3(void*, sys_mmap, void*, addr, size_t, length, va_list, lst) } if (!addr_ptr) { - addr_ptr = UMMAP_START; - } else if (addr_ptr < UMMAP_START || addr_ptr + length >= UMMAP_END) { + addr_ptr = USR_MMAP; + } else if (addr_ptr < USR_MMAP || addr_ptr + length >= USR_MMAP_END) { if (!(options & (MAP_FIXED | MAP_FIXED_NOREPLACE))) { errno = ENOMEM; goto done; diff --git a/lunaix-os/kernel/mm/mmio.c b/lunaix-os/kernel/mm/mmio.c index cac4a89..43cc9e1 100644 --- a/lunaix-os/kernel/mm/mmio.c +++ b/lunaix-os/kernel/mm/mmio.c @@ -6,7 +6,7 @@ void* ioremap(ptr_t paddr, u32_t size) { - void* ptr = vmm_vmap(paddr, size, PG_PREM_RW | PG_DISABLE_CACHE); + void* ptr = vmap(paddr, size, PG_PREM_RW | PG_DISABLE_CACHE, 0); if (ptr) { pmm_mark_chunk_occupied(KERNEL_PID, diff --git a/lunaix-os/kernel/mm/vmap.c b/lunaix-os/kernel/mm/vmap.c index 4b1b311..968ba35 100644 --- a/lunaix-os/kernel/mm/vmap.c +++ b/lunaix-os/kernel/mm/vmap.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -7,7 +8,7 @@ static ptr_t start = VMAP; void* -vmm_vmap(ptr_t paddr, size_t size, pt_attr attr) +vmap(ptr_t paddr, size_t size, pt_attr attr, int flags) { // next fit assert_msg((paddr & 0xfff) == 0, "vmap: bad alignment"); @@ -17,12 +18,12 @@ vmm_vmap(ptr_t paddr, size_t size, pt_attr attr) size_t examed_size = 0, wrapped = 0; x86_page_table* pd = (x86_page_table*)L1_BASE_VADDR; - while (!wrapped || current_addr >= start) { + while (!wrapped || current_addr < start) { size_t l1inx = L1_INDEX(current_addr); if (!(pd->entry[l1inx])) { // empty 4mb region - examed_size += MEM_4MB; - current_addr = (current_addr & 0xffc00000) + MEM_4MB; + examed_size += MEM_4M; + current_addr = (current_addr & 0xffc00000) + MEM_4M; } else { x86_page_table* ptd = (x86_page_table*)(L2_VADDR(l1inx)); size_t i = L2_INDEX(current_addr), j = 0; @@ -54,11 +55,90 @@ vmm_vmap(ptr_t paddr, size_t size, pt_attr attr) done: ptr_t alloc_begin = current_addr - examed_size; + start = alloc_begin + size; + + if ((flags & VMAP_NOMAP)) { + for (size_t i = 0; i < size; i += PG_SIZE) { + vmm_set_mapping(VMS_SELF, alloc_begin + i, -1, 0, 0); + } + + return (void*)alloc_begin; + } + for (size_t i = 0; i < size; i += PG_SIZE) { vmm_set_mapping(VMS_SELF, alloc_begin + i, paddr + i, attr, 0); pmm_ref_page(KERNEL_PID, paddr + i); } - start = alloc_begin + size; return (void*)alloc_begin; +} + +/* + This is a kernel memory region that represent a contiguous virtual memory + address such that all memory allocation/deallocation can be concentrated + into a single big chunk, which will help to mitigate the external + fragmentation in the VMAP address domain. It is significant if our + allocation granule is single page or in some use cases. + + XXX (vmap_area) + A potential performance improvement on pcache? (need more analysis!) + -> In exchange of a fixed size buffer pool. (does it worth?) +*/ + +struct vmap_area* +vmap_varea(size_t size, pt_attr attr) +{ + ptr_t start = (ptr_t)vmap(0, size, attr ^ PG_PRESENT, VMAP_NOMAP); + + if (!start) { + return NULL; + } + + struct vmap_area* varea = valloc(sizeof(struct vmap_area)); + *varea = + (struct vmap_area){ .start = start, .size = size, .area_attr = attr }; + + return varea; +} + +ptr_t +vmap_area_page(struct vmap_area* area, ptr_t paddr, pt_attr attr) +{ + ptr_t current = area->start; + size_t bound = current + area->size; + + while (current < bound) { + x86_pte_t* pte = + (x86_pte_t*)(L2_VADDR(L1_INDEX(current)) | L2_INDEX(current)); + if (PG_IS_PRESENT(*pte)) { + current += PG_SIZE; + continue; + } + + *pte = NEW_L2_ENTRY(attr | PG_PRESENT, paddr); + cpu_flush_page(current); + break; + } + + return current; +} + +ptr_t +vmap_area_rmpage(struct vmap_area* area, ptr_t vaddr) +{ + ptr_t current = area->start; + size_t bound = current + area->size; + + if (current > vaddr || vaddr > bound) { + return 0; + } + + x86_pte_t* pte = + (x86_pte_t*)(L2_VADDR(L1_INDEX(current)) | L2_INDEX(current)); + ptr_t pa = PG_ENTRY_ADDR(*pte); + + *pte = NEW_L2_ENTRY(0, -1); + cpu_flush_page(current); + + return pa; } \ No newline at end of file diff --git a/lunaix-os/kernel/mm/vmm.c b/lunaix-os/kernel/mm/vmm.c index c3ba814..4cfb1a6 100644 --- a/lunaix-os/kernel/mm/vmm.c +++ b/lunaix-os/kernel/mm/vmm.c @@ -1,9 +1,9 @@ -#include #include #include #include #include #include +#include LOG_MODULE("VMM") @@ -41,7 +41,8 @@ vmm_set_mapping(ptr_t mnt, ptr_t va, ptr_t pa, pt_attr attr, int options) // See if attr make sense assert(attr <= 128); - if (!l1pt->entry[l1_inx]) { + x86_pte_t* l1pte = &l1pt->entry[l1_inx]; + if (!*l1pte) { x86_page_table* new_l1pt_pa = (x86_page_table*)pmm_alloc_page(KERNEL_PID, PP_FGPERSIST); @@ -51,14 +52,17 @@ vmm_set_mapping(ptr_t mnt, ptr_t va, ptr_t pa, pt_attr attr, int options) } // This must be writable - l1pt->entry[l1_inx] = - NEW_L1_ENTRY(attr | PG_WRITE | PG_PRESENT, new_l1pt_pa); + *l1pte = NEW_L1_ENTRY(attr | PG_WRITE | PG_PRESENT, new_l1pt_pa); // make sure our new l2 table is visible to CPU cpu_flush_page((ptr_t)l2pt); memset((void*)l2pt, 0, PG_SIZE); } else { + if ((attr & PG_ALLOW_USER) && !(*l1pte & PG_ALLOW_USER)) { + *l1pte |= PG_ALLOW_USER; + } + x86_pte_t pte = l2pt->entry[l2_inx]; if (pte && (options & VMAP_IGNORE)) { return 1; diff --git a/lunaix-os/kernel/peripheral/ps2kbd.c b/lunaix-os/kernel/peripheral/ps2kbd.c index 6690faf..8ba1b91 100644 --- a/lunaix-os/kernel/peripheral/ps2kbd.c +++ b/lunaix-os/kernel/peripheral/ps2kbd.c @@ -1,13 +1,12 @@ #include -#include #include #include #include #include #include -#include #include +#include #include diff --git a/lunaix-os/kernel/proc0.c b/lunaix-os/kernel/proc0.c index c30feec..3c7b225 100644 --- a/lunaix-os/kernel/proc0.c +++ b/lunaix-os/kernel/proc0.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include diff --git a/lunaix-os/kernel/process/process.c b/lunaix-os/kernel/process/process.c index 2407ecf..370a542 100644 --- a/lunaix-os/kernel/process/process.c +++ b/lunaix-os/kernel/process/process.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -154,13 +153,13 @@ init_proc_user_space(struct proc_info* pcb) struct mm_region* mapped; struct mmap_param param = { .vms_mnt = VMS_MOUNT_1, .pvms = &pcb->mm, - .mlen = USTACK_SIZE, + .mlen = USR_STACK_SIZE, .proct = PROT_READ | PROT_WRITE, .flags = MAP_ANON | MAP_PRIVATE | MAP_FIXED, .type = REGION_TYPE_STACK }; int status = 0; - if ((status = mem_map(NULL, &mapped, USTACK_END, NULL, ¶m))) { + if ((status = mem_map(NULL, &mapped, USR_STACK, NULL, ¶m))) { kprint_panic("fail to alloc user stack: %d", status); } @@ -263,7 +262,7 @@ copy_kernel_stack(struct proc_info* proc, ptr_t usedMnt) vmm_mount_pd(VMS_MOUNT_1, pt_copy); // 将新进程的页表挂载到挂载点#2 // copy the kernel stack - for (size_t i = KSTACK_START >> 12; i <= KSTACK_TOP >> 12; i++) { + for (size_t i = KERNEL_STACK >> 12; i <= KERNEL_STACK_END >> 12; i++) { volatile x86_pte_t* ppte = &PTE_MOUNTED(VMS_MOUNT_1, i); /* diff --git a/lunaix-os/kernel/process/signal.c b/lunaix-os/kernel/process/signal.c index 8c4322f..c4e1662 100644 --- a/lunaix-os/kernel/process/signal.c +++ b/lunaix-os/kernel/process/signal.c @@ -6,15 +6,19 @@ #include #include -LOG_MODULE("SIG") - #include +#include + +LOG_MODULE("SIG") + extern struct scheduler sched_ctx; /* kernel/sched.c */ #define UNMASKABLE (sigset(SIGKILL) | sigset(SIGTERM)) #define TERMSIG (sigset(SIGSEGV) | sigset(SIGINT) | UNMASKABLE) #define CORE (sigset(SIGSEGV)) +#define within_kstack(addr) \ + (KERNEL_STACK <= (addr) && (addr) <= KERNEL_STACK_END) static inline void signal_terminate(int errcode) @@ -57,7 +61,7 @@ signal_dispatch() ptr_t ustack = __current->ustack_top; - if ((int)(ustack - USTACK_END) < (int)sizeof(struct proc_sig)) { + if ((int)(ustack - USR_STACK) < (int)sizeof(struct proc_sig)) { // 用户栈没有空间存放信号上下文 return 0; } diff --git a/lunaix-os/kernel/tty/tty.c b/lunaix-os/kernel/tty/tty.c index e90899b..6f98855 100644 --- a/lunaix-os/kernel/tty/tty.c +++ b/lunaix-os/kernel/tty/tty.c @@ -1,5 +1,4 @@ #include -#include #include #include #include diff --git a/lunaix-os/link/linker.ld b/lunaix-os/link/linker.ld index 71bc151..f2510aa 100644 --- a/lunaix-os/link/linker.ld +++ b/lunaix-os/link/linker.ld @@ -1,12 +1,5 @@ ENTRY(start_) -/* - FUTURE: Use disk reader - A bit of messy here. - We will pull our higher half kernel out of this shit - and load it separately once we have our disk reader. -*/ - SECTIONS { . = 0x100000; @@ -33,7 +26,10 @@ SECTIONS { } __kexec_boot_end = ALIGN(4K); - /* Relocation of our higher half kernel */ + /* ---- boot end ---- */ + + /* ---- kernel start ---- */ + . += 0xC0000000; /* 好了,我们的内核…… */ @@ -71,11 +67,24 @@ SECTIONS { /* ---- */ + /* align to 8 bytes, so it can cover both 32 and 64 bits address line*/ + . = ALIGN(8); + PROVIDE(__lga_pci_dev_drivers_start = .); KEEP(*(.lga.pci_dev_drivers)); PROVIDE(__lga_pci_dev_drivers_end = .); + + /* ---- */ + + . = ALIGN(8); + + PROVIDE(__lga_platdev_db_start = .); + + KEEP(*(.lga.platdev_db)); + + PROVIDE(__lga_platdev_db_end = .); } .bss BLOCK(4K) : AT ( ADDR(.bss) - 0xC0000000 ) { diff --git a/lunaix-os/scripts/expand.py b/lunaix-os/scripts/expand.py index 041bb78..9fe3d94 100644 --- a/lunaix-os/scripts/expand.py +++ b/lunaix-os/scripts/expand.py @@ -284,33 +284,35 @@ class MemoryMapObject(DataObject): if "width" in record: self.__width = DataObject.create("width", record["width"]) - def __process(self, start_addr, idx, regions, size_lookahead = False): + def __process(self, start_addr, idx, regions): if idx >= len(regions): raise Exception("Unbounded region definition") e = regions[idx] + if "start" not in e: + ne = regions[idx + 1] + if "start" not in ne or "size" not in e: + e["start"] = start_addr + else: + self.__process(start_addr + e["size"], idx + 1, regions) + e["start"] = ne['start'] - e["size"] + if "block" in e: b = e["block"] - 1 - start_addr = (start_addr + b) & ~b + e["start"] = (e["start"] + b) & ~b - if "start" not in e: - e["start"] = start_addr - elif e["start"] < start_addr: + if e["start"] < start_addr: raise Exception(f"starting addr {hex(e['start'])} overrlapping with {hex(start_addr)}") - else: - start_addr = e["start"] - if "size" not in e: - if size_lookahead: - raise Exception("could not infer size from unbounded region") - tmp_addr = self.__process(start_addr, idx + 1, regions, size_lookahead=True) - e["size"] = tmp_addr - start_addr + start_addr = e["start"] - if not size_lookahead: - start_addr += e["size"] + if "size" not in e: + self.__process(start_addr, idx + 1, regions) + ne = regions[idx + 1] + e["size"] = ne['start'] - start_addr - return start_addr + return start_addr + e["size"] def expand(self, param={}): super().expand(param) diff --git a/lunaix-os/scripts/templates/i386/config.json b/lunaix-os/scripts/templates/i386/config.json index a335539..984f902 100644 --- a/lunaix-os/scripts/templates/i386/config.json +++ b/lunaix-os/scripts/templates/i386/config.json @@ -58,6 +58,19 @@ "size": "1@1M", "stk_align": 16 }, + { + "name": "usr_exec", + "start": "4@1M", + "size": "512@1M" + }, + { + "name": "usr_mmap" + }, + { + "name": "usr_stack", + "size": "1@4M", + "stk_align": 16 + }, { "name": "kernel_exec", "start": "3@1G", -- 2.27.0