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