refactor: add user space printf.
[lunaix-os.git] / lunaix-os / kernel / proc0.c
index a2102aeca029313b203bc7360b65060874650571..f926ffda7df7926c6a063aabdaab1f0fa7b36898 100644 (file)
@@ -1,16 +1,33 @@
-#include <arch/x86/boot/multiboot.h>
+#include <lunaix/block.h>
 #include <lunaix/common.h>
+#include <lunaix/fctrl.h>
+#include <lunaix/fs.h>
+#include <lunaix/fs/twifs.h>
 #include <lunaix/lunistd.h>
+#include <lunaix/lxconsole.h>
+#include <lunaix/mm/cake.h>
+#include <lunaix/mm/pmm.h>
+#include <lunaix/mm/valloc.h>
 #include <lunaix/mm/vmm.h>
 #include <lunaix/peripheral/ps2kbd.h>
+#include <lunaix/proc.h>
 #include <lunaix/spike.h>
 #include <lunaix/syscall.h>
 #include <lunaix/syslog.h>
-#include <stddef.h>
+#include <lunaix/types.h>
 
 #include <hal/acpi/acpi.h>
+#include <hal/ahci/ahci.h>
 #include <hal/apic.h>
 #include <hal/ioapic.h>
+#include <hal/pci.h>
+#include <hal/rtc.h>
+
+#include <arch/x86/boot/multiboot.h>
+
+#include <klibc/string.h>
+
+#include <ulibc/stdio.h>
 
 LOG_MODULE("PROC0")
 
@@ -23,6 +40,75 @@ init_platform();
 void
 lock_reserved_memory();
 
+void
+unlock_reserved_memory();
+
+void
+__do_reserved_memory(int unlock);
+
+#define USE_DEMO
+// #define DEMO_SIGNAL
+// #define DEMO_READDIR
+// #define DEMO_IOTEST
+#define DEMO_INPUT_TEST
+
+extern void
+_pconsole_main();
+
+extern void
+_signal_demo_main();
+
+extern void
+_lxinit_main();
+
+extern void
+_readdir_main();
+
+extern void
+_iotest_main();
+
+extern void
+input_test();
+
+void __USER__
+__proc0_usr()
+{
+    // 打开tty设备(控制台),作为标准输入输出。
+    //  tty设备属于序列设备(Sequential Device),该类型设备的上层读写
+    //  无须经过Lunaix的缓存层,而是直接下发到底层驱动。(不受FO_DIRECT的影响)
+    int fdstdout = open("/dev/tty", 0);
+    int fdstdin = dup2(stdout, 1);
+
+    pid_t p;
+    // if (!fork()) {
+    //     _pconsole_main();
+    // }
+
+    if (!(p = fork())) {
+#ifndef USE_DEMO
+        _exit(0);
+#elif defined DEMO_SIGNAL
+        _signal_demo_main();
+#elif defined DEMO_READDIR
+        _readdir_main();
+#elif defined DEMO_IOTEST
+        _iotest_main();
+#elif defined DEMO_INPUT_TEST
+        input_test();
+#else
+        _lxinit_main();
+#endif
+        printf("==== test end ====\n");
+        _exit(0);
+    }
+
+    waitpid(p, 0, 0);
+
+    while (1) {
+        yield();
+    }
+}
+
 /**
  * @brief LunaixOS的零号进程,该进程永远为可执行。
  *
@@ -35,11 +121,23 @@ void
 __proc0()
 {
     init_platform();
-    if (!fork()) {
-        asm("jmp _lxinit_main");
-    }
 
-    asm("1: jmp 1b");
+    init_proc_user_space(__current);
+
+    asm volatile("movw %0, %%ax\n"
+                 "movw %%ax, %%es\n"
+                 "movw %%ax, %%ds\n"
+                 "movw %%ax, %%fs\n"
+                 "movw %%ax, %%gs\n"
+                 "pushl %0\n"
+                 "pushl %1\n"
+                 "pushl %2\n"
+                 "pushl %3\n"
+                 "retf" ::"i"(UDATA_SEG),
+                 "i"(USTACK_TOP & ~0xf),
+                 "i"(UCODE_SEG),
+                 "r"(__proc0_usr)
+                 : "eax", "memory");
 }
 
 extern uint8_t __kernel_start;            /* link/linker.ld */
@@ -50,60 +148,82 @@ extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
 void
 init_platform()
 {
-    assert_msg(kalloc_init(), "Fail to initialize heap");
-
-    size_t hhk_init_pg_count = ((uintptr_t)(&__init_hhk_end)) >> PG_SIZE_BITS;
-    kprintf(KINFO "[MM] Releaseing %d pages from 0x0.\n", hhk_init_pg_count);
-
-    // Fuck it, I will no longer bother this little 1MiB
-    // I just release 4 pages for my APIC & IOAPIC remappings
-    for (size_t i = 0; i < 3; i++) {
-        vmm_unmap_page(KERNEL_PID, (void*)(i << PG_SIZE_BITS));
-    }
-
     // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
     lock_reserved_memory();
 
+    rtc_init();
     acpi_init(_k_init_mb_info);
-    uintptr_t ioapic_addr = acpi_get_context()->madt.ioapic->ioapic_addr;
-    pmm_mark_page_occupied(
-      KERNEL_PID, FLOOR(__APIC_BASE_PADDR, PG_SIZE_BITS), 0);
-    pmm_mark_page_occupied(KERNEL_PID, FLOOR(ioapic_addr, PG_SIZE_BITS), 0);
-
-    vmm_set_mapping(KERNEL_PID, APIC_BASE_VADDR, __APIC_BASE_PADDR, PG_PREM_RW);
-    vmm_set_mapping(KERNEL_PID, IOAPIC_BASE_VADDR, ioapic_addr, PG_PREM_RW);
-
     apic_init();
     ioapic_init();
     timer_init(SYS_TIMER_FREQUENCY_HZ);
     clock_init();
     ps2_kbd_init();
+    pci_init();
+    block_init();
+    ahci_init();
 
     syscall_install();
 
-    for (size_t i = 256; i < hhk_init_pg_count; i++) {
-        vmm_unmap_page(KERNEL_PID, (void*)(i << PG_SIZE_BITS));
+    console_start_flushing();
+    console_flush();
+
+    unlock_reserved_memory();
+
+    for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
+        vmm_del_mapping(PD_REFERENCED, (void*)i);
+        pmm_free_page(KERNEL_PID, (void*)i);
     }
 }
 
 void
 lock_reserved_memory()
+{
+    __do_reserved_memory(0);
+}
+
+void
+unlock_reserved_memory()
+{
+    __do_reserved_memory(1);
+}
+
+void
+__do_reserved_memory(int unlock)
 {
     multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
     size_t map_size =
       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
+    // v_mapping mapping;
     for (unsigned int i = 0; i < map_size; i++) {
         multiboot_memory_map_t mmap = mmaps[i];
-        if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE) {
+        uint8_t* pa = PG_ALIGN(mmap.addr_low);
+        if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
+            // Don't fuck up our kernel code or any free area!
             continue;
         }
-        uint8_t* pa = PG_ALIGN(mmap.addr_low);
         size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
-        for (size_t j = 0; j < pg_num; j++) {
-            vmm_set_mapping(KERNEL_PID,
-                            (pa + (j << PG_SIZE_BITS)),
-                            (pa + (j << PG_SIZE_BITS)),
-                            PG_PREM_R);
+        size_t j = 0;
+        if (!unlock) {
+            for (; j < pg_num; j++) {
+                uintptr_t _pa = pa + (j << PG_SIZE_BITS);
+                if (_pa >= KERNEL_MM_BASE) {
+                    // Don't fuck up our kernel space!
+                    break;
+                }
+                vmm_set_mapping(PD_REFERENCED, _pa, _pa, PG_PREM_R, VMAP_NULL);
+                pmm_mark_page_occupied(
+                  KERNEL_PID, _pa >> PG_SIZE_BITS, PP_FGLOCKED);
+            }
+            // Save the progress for later unmapping.
+            mmaps[i].len_low = j * PG_SIZE;
+        } else {
+            for (; j < pg_num; j++) {
+                uintptr_t _pa = pa + (j << PG_SIZE_BITS);
+                vmm_del_mapping(PD_REFERENCED, _pa);
+                if (mmap.type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
+                    pmm_mark_page_free(_pa >> PG_SIZE_BITS);
+                }
+            }
         }
     }
 }
\ No newline at end of file