fix: race condition and partial state issue on injecting signal context into user...
[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/lxconsole.h>
5 #include <lunaix/mm/pmm.h>
6 #include <lunaix/mm/vmm.h>
7 #include <lunaix/peripheral/ps2kbd.h>
8 #include <lunaix/proc.h>
9 #include <lunaix/spike.h>
10 #include <lunaix/syscall.h>
11 #include <lunaix/syslog.h>
12 #include <stddef.h>
13
14 #include <hal/acpi/acpi.h>
15 #include <hal/apic.h>
16 #include <hal/ioapic.h>
17
18 LOG_MODULE("PROC0")
19
20 extern void
21 _lxinit_main(); /* lxinit.c */
22
23 void
24 init_platform();
25
26 void
27 lock_reserved_memory();
28
29 void
30 unlock_reserved_memory();
31
32 void
33 __do_reserved_memory(int unlock);
34
35 #define DEMO_SIGNAL
36
37 extern void
38 _pconsole_main();
39
40 extern void
41 _signal_demo_main();
42
43 extern void
44 _lxinit_main();
45
46 void __USER__
47 __proc0_usr()
48 {
49     pid_t p;
50     if (!fork()) {
51         _pconsole_main();
52     }
53
54     if (!(p = fork())) {
55 #ifdef DEMO_SIGNAL
56         _signal_demo_main();
57 #else
58         _lxinit_main();
59 #endif
60     }
61
62     // waitpid(p, 0, 0);
63
64     while (1) {
65         yield();
66     }
67 }
68
69 /**
70  * @brief LunaixOS的零号进程,该进程永远为可执行。
71  *
72  * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
73  *
74  * 同时,该进程也负责fork出我们的init进程。
75  *
76  */
77 void
78 __proc0()
79 {
80     init_platform();
81
82     init_proc_user_space(__current);
83
84     asm volatile("movw %0, %%ax\n"
85                  "movw %%ax, %%es\n"
86                  "movw %%ax, %%ds\n"
87                  "movw %%ax, %%fs\n"
88                  "movw %%ax, %%gs\n"
89                  "pushl %0\n"
90                  "pushl %1\n"
91                  "pushl %2\n"
92                  "pushl %3\n"
93                  "retf" ::"i"(UDATA_SEG),
94                  "i"(USTACK_TOP & ~0xf),
95                  "i"(UCODE_SEG),
96                  "r"(__proc0_usr)
97                  : "eax", "memory");
98 }
99
100 extern uint8_t __kernel_start;            /* link/linker.ld */
101 extern uint8_t __kernel_end;              /* link/linker.ld */
102 extern uint8_t __init_hhk_end;            /* link/linker.ld */
103 extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
104
105 void
106 init_platform()
107 {
108     assert_msg(kalloc_init(), "Fail to initialize heap");
109
110     // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
111     lock_reserved_memory();
112
113     acpi_init(_k_init_mb_info);
114     uintptr_t ioapic_addr = acpi_get_context()->madt.ioapic->ioapic_addr;
115     pmm_mark_page_occupied(
116       KERNEL_PID, FLOOR(__APIC_BASE_PADDR, PG_SIZE_BITS), 0);
117     pmm_mark_page_occupied(KERNEL_PID, FLOOR(ioapic_addr, PG_SIZE_BITS), 0);
118
119     vmm_set_mapping(
120       PD_REFERENCED, MMIO_APIC, __APIC_BASE_PADDR, PG_PREM_RW, VMAP_NULL);
121     vmm_set_mapping(
122       PD_REFERENCED, MMIO_IOAPIC, ioapic_addr, PG_PREM_RW, VMAP_NULL);
123
124     apic_init();
125     ioapic_init();
126     timer_init(SYS_TIMER_FREQUENCY_HZ);
127     clock_init();
128     ps2_kbd_init();
129
130     syscall_install();
131
132     console_start_flushing();
133
134     unlock_reserved_memory();
135
136     for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
137         vmm_del_mapping(PD_REFERENCED, (void*)i);
138         pmm_free_page(KERNEL_PID, (void*)i);
139     }
140 }
141
142 void
143 lock_reserved_memory()
144 {
145     __do_reserved_memory(0);
146 }
147
148 void
149 unlock_reserved_memory()
150 {
151     __do_reserved_memory(1);
152 }
153
154 void
155 __do_reserved_memory(int unlock)
156 {
157     multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
158     size_t map_size =
159       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
160     // v_mapping mapping;
161     for (unsigned int i = 0; i < map_size; i++) {
162         multiboot_memory_map_t mmap = mmaps[i];
163         uint8_t* pa = PG_ALIGN(mmap.addr_low);
164         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
165             // Don't fuck up our kernel code or any free area!
166             continue;
167         }
168         size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
169         size_t j = 0;
170         if (!unlock) {
171             for (; j < pg_num; j++) {
172                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
173                 if (_pa >= KERNEL_MM_BASE) {
174                     // Don't fuck up our kernel space!
175                     break;
176                 }
177                 vmm_set_mapping(PD_REFERENCED, _pa, _pa, PG_PREM_R, VMAP_NULL);
178             }
179             // Save the progress for later unmapping.
180             mmaps[i].len_low = j * PG_SIZE;
181         } else {
182             for (; j < pg_num; j++) {
183                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
184                 vmm_del_mapping(PD_REFERENCED, _pa);
185             }
186         }
187     }
188 }