refactor: simplify the vmm design, single responsibility. But using it should with...
[lunaix-os.git] / lunaix-os / kernel / proc0.c
1 #include <arch/x86/boot/multiboot.h>
2 #include <lunaix/common.h>
3 #include <lunaix/lunistd.h>
4 #include <lunaix/mm/pmm.h>
5 #include <lunaix/mm/vmm.h>
6 #include <lunaix/peripheral/ps2kbd.h>
7 #include <lunaix/proc.h>
8 #include <lunaix/spike.h>
9 #include <lunaix/syscall.h>
10 #include <lunaix/syslog.h>
11 #include <stddef.h>
12
13 #include <hal/acpi/acpi.h>
14 #include <hal/apic.h>
15 #include <hal/ioapic.h>
16
17 LOG_MODULE("PROC0")
18
19 extern void
20 _lxinit_main(); /* lxinit.c */
21
22 void
23 init_platform();
24
25 void
26 lock_reserved_memory();
27
28 // #define ENABLE_USER_MODE
29
30 /**
31  * @brief LunaixOS的零号进程,该进程永远为可执行。
32  *
33  * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
34  *
35  * 同时,该进程也负责fork出我们的init进程。
36  *
37  */
38 void
39 __proc0()
40 {
41     init_platform();
42 #ifdef ENABLE_USER_MODE
43     asm volatile("movw %0, %%ax\n"
44                  "movw %%ax, %%es\n"
45                  "movw %%ax, %%ds\n"
46                  "movw %%ax, %%fs\n"
47                  "movw %%ax, %%gs\n"
48                  "pushl %0\n"
49                  "pushl %1\n"
50                  "pushl %2\n"
51                  "pushl %3\n"
52                  "retf" ::"i"(UDATA_SEG),
53                  "i"(USTACK_TOP & ~0xf),
54                  "i"(UCODE_SEG),
55                  "r"(&&usr));
56 #endif
57 usr:
58     if (!fork()) {
59         asm("jmp _lxinit_main");
60     }
61
62     while (1) {
63         yield();
64     }
65 }
66
67 extern uint8_t __kernel_start;            /* link/linker.ld */
68 extern uint8_t __kernel_end;              /* link/linker.ld */
69 extern uint8_t __init_hhk_end;            /* link/linker.ld */
70 extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
71
72 void
73 init_platform()
74 {
75     assert_msg(kalloc_init(), "Fail to initialize heap");
76
77     size_t hhk_init_pg_count = ((uintptr_t)(&__init_hhk_end)) >> PG_SIZE_BITS;
78     kprintf(KINFO "[MM] Releaseing %d pages from 0x0.\n", hhk_init_pg_count);
79
80     // Fuck it, I will no longer bother this little 1MiB
81     // I just release 4 pages for my APIC & IOAPIC remappings
82     for (size_t i = 0; i < 3; i++) {
83         vmm_del_mapping(PD_REFERENCED, (void*)(i << PG_SIZE_BITS));
84     }
85
86     // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
87     lock_reserved_memory();
88
89     acpi_init(_k_init_mb_info);
90     uintptr_t ioapic_addr = acpi_get_context()->madt.ioapic->ioapic_addr;
91     pmm_mark_page_occupied(
92       KERNEL_PID, FLOOR(__APIC_BASE_PADDR, PG_SIZE_BITS), 0);
93     pmm_mark_page_occupied(KERNEL_PID, FLOOR(ioapic_addr, PG_SIZE_BITS), 0);
94
95     vmm_set_mapping(
96       PD_REFERENCED, APIC_BASE_VADDR, __APIC_BASE_PADDR, PG_PREM_RW);
97     vmm_set_mapping(PD_REFERENCED, IOAPIC_BASE_VADDR, ioapic_addr, PG_PREM_RW);
98
99     apic_init();
100     ioapic_init();
101     timer_init(SYS_TIMER_FREQUENCY_HZ);
102     clock_init();
103     ps2_kbd_init();
104
105     syscall_install();
106
107     for (size_t i = 256; i < hhk_init_pg_count; i++) {
108         vmm_del_mapping(PD_REFERENCED, (void*)(i << PG_SIZE_BITS));
109     }
110 }
111
112 void
113 lock_reserved_memory()
114 {
115     multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
116     size_t map_size =
117       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
118     v_mapping mapping;
119     for (unsigned int i = 0; i < map_size; i++) {
120         multiboot_memory_map_t mmap = mmaps[i];
121         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE) {
122             continue;
123         }
124         uint8_t* pa = PG_ALIGN(mmap.addr_low);
125         size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
126         for (size_t j = 0; j < pg_num; j++) {
127             uintptr_t _pa = pa + (j << PG_SIZE_BITS);
128             if (vmm_lookup(_pa, &mapping) && *mapping.pte) {
129                 continue;
130             }
131             vmm_set_mapping(PD_REFERENCED, _pa, _pa, PG_PREM_R);
132             pmm_mark_page_occupied(KERNEL_PID, _pa >> 12, 0);
133         }
134     }
135 }