Browse Source

fix some problems

greatbridf 9 months ago
parent
commit
15dabfee26

+ 1 - 1
doc/mem_layout.txt

@@ -1,6 +1,6 @@
 physical memory
 
-0x0000 - 0x1000: early kernel data
+0x0000 - 0x1000: GDT, TSS, LDT and some early kernel data
 0x1000 - 0x2000: kernel stage1
 0x2000 - ?     : kernel image
 

+ 2 - 2
include/kernel/log.hpp

@@ -8,7 +8,7 @@
     if (1) {\
         char buf[512]; \
         snprintf(buf, sizeof(buf), fmt "\n" __VA_OPT__(,) __VA_ARGS__); \
-        console->print(buf); \
+        if (console) console->print(buf); \
     }
 
-#define kmsg(msg) if (console) console->print(msg)
+#define kmsg(msg) if (console) console->print(msg "\n")

+ 2 - 0
src/boot.s

@@ -50,6 +50,8 @@ _fill_loop1:
     adc $0, %edx
     loop _fill_loop1
 
+	mov $0x80000000, %edx
+
     # set PCD, PWT
     or $0x00000018, %ebx
     mov $256, %ecx

+ 9 - 0
src/kernel.ld

@@ -41,6 +41,15 @@ SECTIONS
         . = ALIGN(16);
         *(.rodata.kinit)
 
+        KINIT_START_ADDR = .;
+        QUAD(ABSOLUTE(KINIT_START));
+
+        KINIT_END_ADDR = .;
+        QUAD(ABSOLUTE(KINIT_END));
+
+        KINIT_PAGES = .;
+        QUAD((KINIT_END - KINIT_START) / 0x1000);
+
         . = ALIGN(16);
         start_ctors = .;
         KEEP(*(.init_array));

+ 1 - 1
src/kernel/hw/ahci.cc

@@ -473,7 +473,7 @@ public:
             auto* port = new ahci_port(ghc_port);
             if (port->init() != 0) {
                 delete port;
-                kmsg("An error occurred while configuring an ahci port\n");
+                kmsg("An error occurred while configuring an ahci port");
                 continue;
             }
 

+ 2 - 2
src/kernel/interrupt.cpp

@@ -116,7 +116,7 @@ extern "C" void interrupt_handler(
         } break;
         case 14: {
             kernel::mem::paging::handle_page_fault(with_code->error_code);
-            context->int_no = (unsigned long)context + 0x80;
+            context->int_no = (unsigned long)context + 0x88;
         }
         }
         freeze();
@@ -137,6 +137,6 @@ extern "C" void interrupt_handler(
     if (real_context->cs == 0x1b && current_thread->signals.pending_signal())
         current_thread->signals.handle(real_context, mmxregs);
 
-    context->int_no = (unsigned long)context + 0x78;
+    context->int_no = (unsigned long)context + 0x80;
     return;
 }

+ 18 - 25
src/kernel/process.cpp

@@ -16,6 +16,7 @@
 
 #include <kernel/async/lock.hpp>
 #include <kernel/log.hpp>
+#include <kernel/mem/paging.hpp>
 #include <kernel/module.hpp>
 #include <kernel/process.hpp>
 #include <kernel/signal.hpp>
@@ -228,7 +229,7 @@ void process::send_signal(signo_type signal)
 
 void kernel_threadd_main(void)
 {
-    kmsg("kernel thread daemon started\n");
+    kmsg("kernel thread daemon started");
 
     for (;;) {
         if (kthreadd_new_thd_func) {
@@ -340,7 +341,7 @@ void proclist::kill(pid_t pid, int exit_code)
 
     // init should never exit
     if (proc.ppid == 0) {
-        kmsg("kernel panic: init exited!\n");
+        kmsg("kernel panic: init exited!");
         freeze();
     }
 
@@ -385,20 +386,17 @@ void proclist::kill(pid_t pid, int exit_code)
 
 static void release_kinit()
 {
-    // TODO: LONG MODE
-    // kernel::paccess pa(EARLY_KERNEL_PD_PAGE);
-    // auto pd = (pd_t)pa.ptr();
-    // assert(pd);
-    // (*pd)[0].v = 0;
-
-    // // free pt#0
-    // __free_raw_page(0x00002);
-
-    // free .stage1 and .kinit
-    // for (uint32_t i = ((uint32_t)__stage1_start >> 12);
-    //         i < ((uint32_t)__kinit_end >> 12); ++i) {
-    //     __free_raw_page(i);
-    // }
+    // free .kinit
+    using namespace kernel::mem::paging;
+    extern uintptr_t KINIT_START_ADDR, KINIT_END_ADDR, KINIT_PAGES;
+
+    auto range = vaddr_range{KERNEL_PAGE_TABLE_ADDR,
+        KINIT_START_ADDR, KINIT_END_ADDR, true};
+
+    for (auto pte : range)
+        pte.clear();
+
+    create_zone(0x2000, 0x2000 + 0x1000 * KINIT_PAGES);
 }
 
 void NORETURN _kernel_init(void)
@@ -417,14 +415,10 @@ void NORETURN _kernel_init(void)
         if (!mod)
             continue;
 
-        auto ret = insmod(mod);
-        if (ret == kernel::module::MODULE_SUCCESS)
+        if (auto ret = insmod(mod); ret == kernel::module::MODULE_SUCCESS)
             continue;
 
-        char buf[256];
-        snprintf(buf, sizeof(buf),
-            "[kernel] An error occured while loading \"%s\"\n", mod->name);
-        kmsg(buf);
+        kmsgf("[kernel] An error occured while loading \"%s\"", mod->name);
     }
 
     // mount fat32 /mnt directory
@@ -457,7 +451,7 @@ void NORETURN _kernel_init(void)
 
     d.exec_dent = fs::vfs_open(*fs::fs_root, types::path{d.argv[0].c_str()});
     if (!d.exec_dent) {
-        kmsg("kernel panic: init not found!\n");
+        kmsg("kernel panic: init not found!");
         freeze();
     }
 
@@ -530,8 +524,6 @@ void NORETURN init_scheduler(void)
 extern "C" void asm_ctx_switch(uint32_t** curr_esp, uint32_t** next_esp);
 bool schedule()
 {
-    freeze();
-
     if (kernel::async::preempt_count() != 0)
         return true;
 
@@ -550,6 +542,7 @@ bool schedule()
 
     curr_thd = current_thread;
 
+    freeze();
     // TODO: LONG MODE
     // current_thread = next_thd;
     // tss.esp0 = (uint32_t)next_thd->kstack.esp;

+ 1 - 1
src/kernel/vfs/tmpfs.cc

@@ -327,7 +327,7 @@ public:
             return 0;
         }
 
-        kmsg("[tmpfs] warning: file entry not found in vfe\n");
+        kmsg("[tmpfs] warning: file entry not found in vfe");
         return -EIO;
     }
 

+ 8 - 9
src/kinit.cpp

@@ -148,9 +148,8 @@ static inline void setup_buddy(uintptr_t addr_max)
     pdpte.set(PA_KERNEL_PAGE_TABLE, 0x105000);
 
     auto pd = pdpte.parse();
-    for (int i = 0; i < count; ++i, start_pfn += 0x200000) {
+    for (int i = 0; i < count; ++i, start_pfn += 0x200000)
         pd[std::get<3>(idx)+i].set(PA_KERNEL_DATA_HUGE, start_pfn);
-    }
 
     PAGE_ARRAY = (page*)0xffffff8040000000ULL;
     memset(PAGE_ARRAY, 0x00, addr_max * sizeof(page));
@@ -164,22 +163,22 @@ static inline void setup_buddy(uintptr_t addr_max)
 
         auto start = ent.base;
         auto end = start + ent.len;
-        if (end <= 0x106000)
+        if (end <= start_pfn)
             continue;
 
-        if (start < 0x106000)
-            start = 0x106000;
-
-        if (start < 0x200000 && end >= 0x200000) {
-            mem::paging::create_zone(start, 0x200000);
+        if (start < start_pfn)
             start = start_pfn;
-        }
 
         if (start > end)
             continue;
 
         mem::paging::create_zone(start, end);
     }
+
+    // free .stage1
+    create_zone(0x1000, 0x2000);
+    // unused space
+    create_zone(0x106000, 0x200000);
 }
 
 SECTION(".text.kinit")

+ 1 - 5
src/types/libstdcpp.cpp

@@ -1,7 +1,6 @@
 #include <assert.h>
 #include <kernel/log.hpp>
 #include <kernel/process.hpp>
-#include <stdio.h>
 #include <types/types.h>
 
 extern "C" void NORETURN __stack_chk_fail(void)
@@ -19,9 +18,6 @@ extern "C" void NORETURN __cxa_pure_virtual(void)
 void NORETURN
 __assert_fail(const char* statement, const char* file, int line, const char* func)
 {
-    char buf[256];
-    snprintf(buf, sizeof(buf), "Kernel assertion failed: (%s), %s:%d, %s\n",
-        statement, file, line, func);
-    kmsg(buf);
+    kmsgf("Kernel assertion failed: (%s), %s:%d, %s", statement, file, line, func);
     freeze();
 }