dynamic memory manager (malloc & free)
authorMinep <zelong56@gmail.com>
Tue, 1 Mar 2022 16:31:12 +0000 (16:31 +0000)
committerMinep <zelong56@gmail.com>
Tue, 1 Mar 2022 16:31:12 +0000 (16:31 +0000)
debugging experience improved

17 files changed:
lunaix-os/.vscode/c_cpp_properties.json
lunaix-os/GRUB_TEMPLATE
lunaix-os/config-grub.sh
lunaix-os/includes/arch/x86/interrupts.h
lunaix-os/includes/arch/x86/types.h [deleted file]
lunaix-os/includes/lunaix/mm/dmm.h
lunaix-os/includes/lunaix/mm/page.h
lunaix-os/includes/lunaix/mm/vmm.h
lunaix-os/includes/lunaix/spike.h [new file with mode: 0644]
lunaix-os/kernel/asm/x86/idt.c
lunaix-os/kernel/asm/x86/interrupt.S
lunaix-os/kernel/asm/x86/interrupts.c
lunaix-os/kernel/k_main.c
lunaix-os/kernel/mm/dmm.c
lunaix-os/kernel/mm/vmm.c
lunaix-os/linker.ld
lunaix-os/makefile

index 0840db114b3f75d58586c634ff195f27ac550b52..dd47757e385de54b3606f9f593c70117453dcdfc 100644 (file)
@@ -7,7 +7,8 @@
             ],
             "compilerArgs": [
                 "-ffreestanding",
-                "-D__ARCH_IA32"
+                "-D__ARCH_IA32",
+                "-D__LUNAIXOS_DEBUG__"
             ],
             "defines": [],
             "compilerPath": "${HOME}/opt/cross-compiler/bin/i686-elf-gcc",
index 6652d2c715e3bc247545790e2425770d07eb2afe..02c932ffb40e7ed26db7d7f3e17e1817348c8650 100644 (file)
@@ -1,3 +1,6 @@
+default=0
+timeout=0
+
 menuentry "$_OS_NAME" {
        multiboot /boot/$_OS_NAME.bin
 }
\ No newline at end of file
index def0ecfe593569adf8c8c9ba176f20082e7f4772..ff4d4b639daba06f25bfbb5a3695213359873bf0 100755 (executable)
@@ -2,4 +2,4 @@
 
 export _OS_NAME=$1
 
-echo $(cat GRUB_TEMPLATE | envsubst)
\ No newline at end of file
+cat GRUB_TEMPLATE | envsubst > "$2"
\ No newline at end of file
index b608cd6217a2f22d028746ac42ecaf0c2f637148..6265720f0fd5dced88f0d9109045639024af69ee 100644 (file)
@@ -1,6 +1,29 @@
 #ifndef __LUNAIX_INTERRUPTS_H
 #define __LUNAIX_INTERRUPTS_H
 
+#define FAULT_DIVISION_ERROR            0x0
+#define FAULT_TRAP_DEBUG_EXCEPTION      0x1
+#define INT_NMI                         0x2
+#define TRAP_BREAKPOINT                 0x3
+#define TRAP_OVERFLOW                   0x4
+#define FAULT_BOUND_EXCEED              0x5
+#define FAULT_INVALID_OPCODE            0x6
+#define FAULT_NO_MATH_PROCESSOR         0x7
+#define ABORT_DOUBLE_FAULT              0x8
+#define FAULT_RESERVED_0                0x9
+#define FAULT_INVALID_TSS               0xa
+#define FAULT_SEG_NOT_PRESENT           0xb
+#define FAULT_STACK_SEG_FAULT           0xc
+#define FAULT_GENERAL_PROTECTION        0xd
+#define FAULT_PAGE_FAULT                0xe
+#define FAULT_RESERVED_1                0xf
+#define FAULT_X87_FAULT                 0x10
+#define FAULT_ALIGNMENT_CHECK           0x11
+#define ABORT_MACHINE_CHECK             0x12
+#define FAULT_SIMD_FP_EXCEPTION         0x13
+#define FAULT_VIRTUALIZATION_EXCEPTION  0x14
+#define FAULT_CONTROL_PROTECTION        0x15
+
 typedef struct {
     unsigned int vector;
     unsigned int err_code;
@@ -14,6 +37,9 @@ typedef struct {
 void
 _asm_isr0();
 
+void
+_asm_isr13();
+
 void
 interrupt_handler(isr_param* param);
 
diff --git a/lunaix-os/includes/arch/x86/types.h b/lunaix-os/includes/arch/x86/types.h
deleted file mode 100644 (file)
index ba2839a..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-// Ref: Intel Manuel Vol.3 Figure 6-1
-
-#ifndef __LUNAIX_TYPES_H
-#define __LUNAIX_TYPES_H
-
-#define FAULT_DIVISION_ERROR            0x0
-#define FAULT_TRAP_DEBUG_EXCEPTION      0x1
-#define INT_NMI                         0x2
-#define TRAP_BREAKPOINT                 0x3
-#define TRAP_OVERFLOW                   0x4
-#define FAULT_BOUND_EXCEED              0x5
-#define FAULT_INVALID_OPCODE            0x6
-#define FAULT_NO_MATH_PROCESSOR         0x7
-#define ABORT_DOUBLE_FAULT              0x8
-#define FAULT_RESERVED_0                0x9
-#define FAULT_INVALID_TSS               0xa
-#define FAULT_SEG_NOT_PRESENT           0xb
-#define FAULT_STACK_SEG_FAULT           0xc
-#define FAULT_GENERAL_PROTECTION        0xd
-#define FAULT_PAGE_FAULT                0xe
-#define FAULT_RESERVED_1                0xf
-#define FAULT_X87_FAULT                 0x10
-#define FAULT_ALIGNMENT_CHECK           0x11
-#define ABORT_MACHINE_CHECK             0x12
-#define FAULT_SIMD_FP_EXCEPTION         0x13
-#define FAULT_VIRTUALIZATION_EXCEPTION  0x14
-#define FAULT_CONTROL_PROTECTION        0x15
-
-
-#endif /* __LUNAIX_TYPES_H */
index 6ea878b57cfdd12e016807c50f4c05d2586019f3..e8c3b6fa43a8697b05d55d0f37e3dff0aef47ffd 100644 (file)
@@ -4,13 +4,20 @@
 
 #include <stddef.h>
 
-void
-lxsbrk(void* current, void* next);
+#define HEAP_INIT_SIZE 4096
 
-void
-lxmalloc(size_t size);
+int
+dmm_init();
+
+int
+lxsbrk(void* addr);
+void*
+lxbrk(size_t size);
+
+void*
+lx_malloc(size_t size);
 
 void
-lxfree(size_t size);
+lx_free(void* ptr);
 
 #endif /* __LUNAIX_DMM_H */
index e97de1d80b41cab1660ee0e2b886666fb195d5c5..4ea31bc0a5221cdb4642ebb955cdaf5642bae486 100644 (file)
@@ -3,7 +3,8 @@
 #include <stdint.h>
 #include <lunaix/constants.h>
 
-#define PG_SIZE_BITS              12
+#define PG_SIZE_BITS                12
+#define PG_SIZE                     (1 << PG_SIZE_BITS)
 #define PG_INDEX_BITS               10
 
 #define PG_MAX_ENTRIES              1024U
index 950928c2384264c0391cf9044540baceb019a38e..a0c455fdf4c765576c86faa7b4cbede8210cdd3d 100644 (file)
@@ -55,6 +55,18 @@ vmm_fmap_page(void* va, void* pa, pt_attr tattr);
 void*
 vmm_alloc_page(void* va, pt_attr tattr);
 
+
+/**
+ * @brief 尝试分配多个连续的虚拟页
+ * 
+ * @param va 起始虚拟地址
+ * @param sz 大小(必须为4K对齐)
+ * @param tattr 属性
+ * @return int 是否成功
+ */
+int
+vmm_alloc_pages(void* va, size_t sz, pt_attr tattr);
+
 /**
  * @brief 删除一个映射
  *
diff --git a/lunaix-os/includes/lunaix/spike.h b/lunaix-os/includes/lunaix/spike.h
new file mode 100644 (file)
index 0000000..fa4e797
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef __LUNAIX_SPIKE_H
+#define __LUNAIX_SPIKE_H
+
+// Some helper functions. As helpful as Spike the Dragon! :)
+
+// 除法向上取整
+#define CEIL(v, k)          (((v) + (1 << (k)) - 1) >> (k))
+
+// 除法向下取整
+#define FLOOR(v, k)         ((v) >> (k))
+
+// 获取v最近的最大k倍数
+#define ROUNDUP(v, k)       (((v) + (k) - 1) & ~((k) - 1))
+
+// 获取v最近的最小k倍数
+#define ROUNDDOWN(v, k)     ((v) & ~((k) - 1))
+
+static void inline spin() {
+    while(1);
+}
+
+#endif /* __LUNAIX_SPIKE_H */
index 9f2bd069180f0ea5c26ab3439f39f44ea0563f6e..19b63e0d2ad788589c2fced1024d1593b2affc2c 100644 (file)
@@ -1,6 +1,5 @@
 #include <arch/x86/idt.h>
 #include <arch/x86/interrupts.h>
-#include <arch/x86/types.h>
 #include <stdint.h>
 
 #define IDT_ENTRY 32
@@ -19,4 +18,5 @@ void _set_idt_entry(uint32_t vector, uint16_t seg_selector, void (*isr)(), uint8
 void
 _init_idt() {
     _set_idt_entry(FAULT_DIVISION_ERROR, 0x08, _asm_isr0, 0);
+    _set_idt_entry(FAULT_GENERAL_PROTECTION, 0x08, _asm_isr13, 0);
 }
\ No newline at end of file
index 584b1437435adba2845e2483eeb0a4b9ea306b8c..5f3d333c10d6cf0f50f6ad3c175702e6d34fdc9f 100644 (file)
@@ -11,6 +11,7 @@
 
 .section .text
     isr_template 0
+    isr_template 13, no_error_code=0
 
     interrupt_wrapper:
 
index 686c43de3ec6771cf4cda3a0780d63743ce19772..7ed34273d0e2d6abbae1e19ed54b7531d499d816 100644 (file)
@@ -1,8 +1,16 @@
 #include <arch/x86/interrupts.h>
+#include <lunaix/assert.h>
 #include <libc/stdio.h>
 
-void isr0 (isr_param* param) {
-    printf("[PANIC] Exception (%d) CS=0x%X, EIP=0x%X", param->vector, param->cs, param->eip);
+void panic (const char* msg, isr_param* param) {
+    tty_set_theme(VGA_COLOR_BLACK, VGA_COLOR_LIGHT_RED);
+    tty_clear_line(10);
+    tty_clear_line(11);
+    tty_clear_line(12);
+    tty_set_cpos(0, 11);
+    printf(" INT %u: [0x%x: 0x%x] %s", param->vector, param->cs, param->eip, msg);
+    __spin:
+        goto __spin;
 }
 
 void 
@@ -10,7 +18,13 @@ interrupt_handler(isr_param* param) {
     switch (param->vector)
     {
         case 0:
-            isr0(param);
-            break;
+            panic("Division by 0", param);
+            break;  // never reach
+        case FAULT_GENERAL_PROTECTION:
+            panic("General Protection", param);
+            break;  // never reach
+        default:
+            panic("Unknown Interrupt", param);
+            break;  // never reach
     }
 }
\ No newline at end of file
index b1fe4886184c7aa98c1b442cb924fc7751754920..3899336d00950bd5d150611c18ecc2c0726f11ff 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdint.h>
 #include <lunaix/mm/vmm.h>
+#include <lunaix/mm/dmm.h>
 #include <hal/cpu.h>
 #include <libc/stdio.h>
 
@@ -19,5 +20,22 @@ _kernel_main()
     void* k_start = vmm_v2p(&__kernel_start);
     printf("The kernel's base address mapping: %p->%p\n", &__kernel_start, k_start);
 
+    dmm_init();
+
+    // test malloc & free
+    
+    uint32_t** arr = (uint32_t**) lx_malloc(10 * sizeof(uint32_t*));
+    
+    for (size_t i = 0; i < 10; i++)
+    {
+        arr[i] = (uint32_t*) lx_malloc((i + 1) * 2);
+    }
+
+    for (size_t i = 0; i < 10; i++)
+    {
+        lx_free(arr[i]);
+    }
+    
+    lx_free(arr);
     // assert(0);
 }
\ No newline at end of file
index 04a024919a0a9e5104b210956064eff024151d5e..81426dae62935c0cff7bcd66bb4dbe456b4616b8 100644 (file)
+/**
+ * @file dmm.c
+ * @author Lunaixsky
+ * @brief Dynamic memory manager dedicated to kernel heap. It is not portable at
+ * this moment.
+ * @version 0.1
+ * @date 2022-02-28
+ *
+ * @copyright Copyright (c) Lunaixsky 2022
+ *
+ */
+
 #include <lunaix/mm/dmm.h>
+#include <lunaix/mm/page.h>
 #include <lunaix/mm/vmm.h>
 
-// This is a temporary design. 
-//  We can do better when we are ready for multitasking
-void
-lxsbrk(void* current, void* next) {
-    // TODO: sbrk 
+#include <lunaix/assert.h>
+#include <lunaix/constants.h>
+#include <lunaix/spike.h>
+
+#include <stdbool.h>
+
+#define M_ALLOCATED 0x1
+#define M_PREV_FREE 0x2
+
+#define M_NOT_ALLOCATED 0x0
+#define M_PREV_ALLOCATED 0x0
+
+#define CHUNK_S(header) ((header) & ~0x3)
+#define CHUNK_PF(header) ((header)&M_PREV_FREE)
+#define CHUNK_A(header) ((header)&M_ALLOCATED)
+
+#define PACK(size, flags) (((size) & ~0x3) | (flags))
+
+#define SW(p, w) (*((uint32_t*)(p)) = w)
+#define LW(p) (*((uint32_t*)(p)))
+
+#define HPTR(bp) ((uint32_t*)(bp)-1)
+#define BPTR(bp) ((uint8_t*)(bp) + WSIZE)
+#define FPTR(hp, size) ((uint32_t*)(hp + size - WSIZE))
+#define NEXT_CHK(hp) ((uint8_t*)(hp) + CHUNK_S(LW(hp)))
+
+#define BOUNDARY 4
+#define WSIZE 4
+
+extern uint8_t __kernel_heap_start;
+
+void* current_heap_top = NULL;
+
+void*
+coalesce(uint8_t* chunk_ptr);
+
+void*
+lx_grow_heap(size_t sz);
+
+int
+dmm_init()
+{
+    assert((uintptr_t)&__kernel_heap_start % BOUNDARY == 0);
+
+    current_heap_top = &__kernel_heap_start;
+    uint8_t* heap_start = &__kernel_heap_start;
+    
+    vmm_alloc_page(current_heap_top, PG_PREM_RW);
+
+    SW(heap_start,     PACK(4, M_ALLOCATED));
+    SW(heap_start + WSIZE, PACK(0, M_ALLOCATED));
+    current_heap_top += WSIZE;
+
+    return lx_grow_heap(HEAP_INIT_SIZE);
 }
 
-void
-lxmalloc(size_t size) {
-    // TODO: Malloc 
+int
+lxsbrk(void* addr)
+{
+    return lxbrk(addr - current_heap_top) != NULL;
+}
+
+void*
+lxbrk(size_t size)
+{   
+    if (size == 0) {
+        return NULL;
+    }
+
+    // plus WSIZE is the overhead for epilogue marker
+    size += WSIZE;
+    void* next = current_heap_top + ROUNDUP((uintptr_t)size, WSIZE);
+
+    if (next >= K_STACK_START) {
+        return NULL;
+    }
+
+    // Check the invariant
+    assert(size % BOUNDARY == 0)
+
+    uintptr_t heap_top_pg = PG_ALIGN(current_heap_top);
+      if (heap_top_pg != PG_ALIGN(next))
+    {
+        // if next do require new pages to be allocated
+        if (!vmm_alloc_pages(heap_top_pg + PG_SIZE, ROUNDUP(size, PG_SIZE), PG_PRESENT | PG_WRITE)) {
+            // TODO: OOM, panic here! Rather than spinning.
+            spin();
+            // return NULL
+        }
+    
+    }
+
+    uintptr_t old = current_heap_top;
+    current_heap_top = next - WSIZE;
+    return old;
+}
+
+void*
+lx_grow_heap(size_t sz) {
+    uintptr_t start;
+
+    sz = ROUNDUP(sz, BOUNDARY);
+    if (!(start = lxbrk(sz))) {
+        return NULL;
+    }
+
+    uint32_t old_marker = *((uint32_t*)start);
+    uint32_t free_hdr = PACK(sz, CHUNK_PF(old_marker));
+    SW(start, free_hdr);
+    SW(FPTR(start, sz), free_hdr);
+    SW(NEXT_CHK(start), PACK(0, M_ALLOCATED | M_PREV_FREE));
+
+    return coalesce(start);
+}
+
+void*
+lx_malloc(size_t size)
+{
+    // Simplest first fit approach.
+
+    uint8_t* ptr = &__kernel_heap_start;
+    // round to largest 4B aligned value
+    //  and space for header
+    size = ROUNDUP(size, BOUNDARY) + WSIZE;
+    while (ptr < current_heap_top) {
+        uint32_t header = *((uint32_t*)ptr);
+        size_t chunk_size = CHUNK_S(header);
+        if (chunk_size >= size && !CHUNK_A(header)) {
+            // found!
+            *((uint32_t*)ptr) = PACK(size, CHUNK_PF(header) | M_ALLOCATED);
+            uint8_t* n_hdrptr = (uint8_t*)(ptr + size);
+            uint32_t diff = chunk_size - size;
+            if (!diff) {
+                // if the current free block is fully occupied
+                uint32_t n_hdr = LW(n_hdrptr);
+                // notify the next block about our avaliability
+                SW(n_hdrptr, n_hdr & ~0x2);
+            } else {
+                // if there is remaining free space left
+                uint32_t remainder_hdr =
+                  PACK(diff, M_NOT_ALLOCATED | M_PREV_ALLOCATED);
+                SW(n_hdrptr, remainder_hdr);
+                SW(FPTR(n_hdrptr, diff), remainder_hdr);
+
+                coalesce(n_hdrptr);
+            }
+            return BPTR(ptr);
+        }
+        ptr += chunk_size;
+    }
+
+    return NULL;
 }
 
 void
-lxfree(size_t size) {
-    // TODO: Free 
+lx_free(void* ptr)
+{
+    uint8_t* chunk_ptr = (uint8_t*)ptr - WSIZE;
+    uint32_t hdr = LW(chunk_ptr);
+    uint8_t* next_hdr = chunk_ptr + CHUNK_S(hdr);
+
+    SW(chunk_ptr, hdr & ~M_ALLOCATED);
+    SW(FPTR(chunk_ptr, CHUNK_S(hdr)), hdr & ~M_ALLOCATED);
+    SW(next_hdr, LW(next_hdr) | M_PREV_FREE);
+
+    coalesce(chunk_ptr);
+}
+
+void*
+coalesce(uint8_t* chunk_ptr)
+{
+    uint32_t hdr = LW(chunk_ptr);
+    uint32_t pf = CHUNK_PF(hdr);
+    uint32_t sz = CHUNK_S(hdr);
+    uint32_t ftr = LW(chunk_ptr + sz - WSIZE);
+
+    uint32_t n_hdr = LW(chunk_ptr + sz);
+
+    if (CHUNK_A(n_hdr) && pf) {
+        // case 1: prev is free
+        uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
+        size_t prev_chunk_sz = CHUNK_S(prev_ftr);
+        uint32_t new_hdr = PACK(prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
+        SW(chunk_ptr - prev_chunk_sz, new_hdr);
+        SW(FPTR(chunk_ptr, sz), new_hdr);
+        chunk_ptr -= prev_chunk_sz;
+    } else if (!CHUNK_A(n_hdr) && !pf) {
+        // case 2: next is free
+        size_t next_chunk_sz = CHUNK_S(n_hdr);
+        uint32_t new_hdr = PACK(next_chunk_sz + sz, pf);
+        SW(chunk_ptr, new_hdr);
+        SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
+    } else if (!CHUNK_A(n_hdr) && pf) {
+        // case 3: both free
+        uint32_t prev_ftr = LW(chunk_ptr - WSIZE);
+        size_t next_chunk_sz = CHUNK_S(n_hdr);
+        size_t prev_chunk_sz = CHUNK_S(prev_ftr);
+        uint32_t new_hdr =
+          PACK(next_chunk_sz + prev_chunk_sz + sz, CHUNK_PF(prev_ftr));
+        SW(chunk_ptr - prev_chunk_sz, new_hdr);
+        SW(FPTR(chunk_ptr, sz + next_chunk_sz), new_hdr);
+        chunk_ptr -= prev_chunk_sz;
+    }
+
+    // case 4: prev and next are not free
+    return chunk_ptr;
 }
\ No newline at end of file
index 2f8d726ef86c669c23d100ac729f7765c1b00211..63ff9ac4bba11b05358a0bc1f0106f90c53844c7 100644 (file)
@@ -5,6 +5,8 @@
 #include <lunaix/assert.h>
 #include <hal/cpu.h>
 
+#include <stdbool.h>
+
 void
 vmm_init()
 {
@@ -26,7 +28,7 @@ vmm_init_pd()
 }
 
 int
-__vmm_map_internal(uint32_t l1_inx, uint32_t l2_inx, uintptr_t pa, pt_attr attr) {
+__vmm_map_internal(uint32_t l1_inx, uint32_t l2_inx, uintptr_t pa, pt_attr attr, int forced) {
     ptd_t* l1pt = (ptd_t*)L1_BASE_VADDR;
     pt_t* l2pt = (pt_t*)L2_VADDR(l1_inx);
 
@@ -42,7 +44,11 @@ __vmm_map_internal(uint32_t l1_inx, uint32_t l2_inx, uintptr_t pa, pt_attr attr)
         }
 
         l1pt[l1_inx] = NEW_L1_ENTRY(attr, new_l1pt_pa);
-        memset((void*)L2_VADDR(l1_inx), 0, PM_PAGE_SIZE);
+        memset((void*)L2_VADDR(l1_inx), 0, PG_SIZE);
+    }
+
+    if (!forced && l2pt[l2_inx]) {
+        return 0;
     }
 
     l2pt[l2_inx] = NEW_L2_ENTRY(attr, pa);
@@ -88,7 +94,7 @@ vmm_map_page(void* va, void* pa, pt_attr tattr)
         return NULL;
     }
 
-    if (!__vmm_map_internal(l1_index, l2_index, pa, tattr)) {
+    if (!__vmm_map_internal(l1_index, l2_index, pa, tattr, false)) {
         return NULL;
     }
 
@@ -107,7 +113,7 @@ vmm_fmap_page(void* va, void* pa, pt_attr tattr) {
     uint32_t l1_index = L1_INDEX(va);
     uint32_t l2_index = L2_INDEX(va);
 
-    if (!__vmm_map_internal(l1_index, l2_index, pa, tattr)) {
+    if (!__vmm_map_internal(l1_index, l2_index, pa, tattr, true)) {
         return NULL;
     }
 
@@ -127,6 +133,32 @@ vmm_alloc_page(void* vpn, pt_attr tattr)
     return result;
 }
 
+int
+vmm_alloc_pages(void* va, size_t sz, pt_attr tattr) {
+    assert((uintptr_t)va % PG_SIZE == 0)
+    assert(sz % PG_SIZE == 0)
+    
+    void* va_ = va;
+    for (size_t i = 0; i < (sz >> PG_SIZE_BITS); i++, va_ += PG_SIZE)
+    {
+        void* pp = pmm_alloc_page();
+        uint32_t l1_index = L1_INDEX(va_);
+        uint32_t l2_index = L2_INDEX(va_);
+        if (!pp || !__vmm_map_internal(l1_index, l2_index, pp, tattr, false)) {
+            // if one failed, release previous allocated pages.
+            va_ = va;
+            for (size_t j = 0; j < i; j++, va_ += PG_SIZE)
+            {
+                vmm_unmap_page(va_);
+            }
+            
+            return false;
+        }
+    }
+    
+    return true;
+}
+
 void
 vmm_unmap_page(void* va)
 {
@@ -141,9 +173,10 @@ vmm_unmap_page(void* va)
     if (l1pte) {
         pt_t* l2pt = (pt_t*)L2_VADDR(l1_index);
         uint32_t l2pte = l2pt[l2_index];
-        if (IS_CACHED(l2pte) && pmm_free_page((void*)l2pte)) {
-            cpu_invplg(va);
+        if (IS_CACHED(l2pte)) {
+            pmm_free_page((void*)l2pte);
         }
+        cpu_invplg(va);
         l2pt[l2_index] = 0;
     }
 }
index bac8600d93978b4612ff598dde819fe7766f18fa..a81dad551c1dce19da6e2cea25ce1984b6c00ea0 100644 (file)
@@ -61,5 +61,5 @@ SECTIONS {
     }
 
     __kernel_end = ALIGN(4K);
-    __heap_start = ALIGN(4K);    /* 内核结束的地方即堆开始的地方 */
+    __kernel_heap_start = ALIGN(4K);    /* 内核结束的地方即堆开始的地方 */
 }
\ No newline at end of file
index abcde29f211aee6157a09d3529e784bd343262be..9d3c79942ac68bc71367b1204d82cac001294cef 100644 (file)
@@ -33,7 +33,7 @@ $(BIN_DIR)/$(OS_BIN): $(OBJECT_DIR) $(BIN_DIR) $(SRC)
        @$(CC) -T linker.ld -o $(BIN_DIR)/$(OS_BIN) $(SRC) $(LDFLAGS)
 
 $(BUILD_DIR)/$(OS_ISO): $(ISO_DIR) $(BIN_DIR)/$(OS_BIN) GRUB_TEMPLATE
-       @./config-grub.sh ${OS_NAME} $(ISO_GRUB_DIR)/grub.cfg
+       @./config-grub.sh ${OS_NAME} $(ISO_GRUB_DIR)/grub.cfg
        @cp $(BIN_DIR)/$(OS_BIN) $(ISO_BOOT_DIR)
        @grub-mkrescue -o $(BUILD_DIR)/$(OS_ISO) $(ISO_DIR)
 
@@ -48,6 +48,7 @@ all-debug: clean $(BUILD_DIR)/$(OS_ISO)
 
 clean:
        @rm -rf $(BUILD_DIR)
+       @sleep 1
 
 run: $(BUILD_DIR)/$(OS_ISO)
        @qemu-system-i386 -cdrom $(BUILD_DIR)/$(OS_ISO) -monitor telnet::$(QEMU_MON_PORT),server,nowait &