feat: heap support and re-worked
[lunaix-os.git] / lunaix-os / kernel / proc0.c
1 #include <lunaix/block.h>
2 #include <lunaix/common.h>
3 #include <lunaix/foptions.h>
4 #include <lunaix/fs.h>
5 #include <lunaix/fs/twifs.h>
6 #include <lunaix/lxconsole.h>
7 #include <lunaix/mm/cake.h>
8 #include <lunaix/mm/pmm.h>
9 #include <lunaix/mm/valloc.h>
10 #include <lunaix/mm/vmm.h>
11 #include <lunaix/peripheral/ps2kbd.h>
12 #include <lunaix/peripheral/serial.h>
13 #include <lunaix/spike.h>
14 #include <lunaix/syscall.h>
15 #include <lunaix/syslog.h>
16 #include <lunaix/types.h>
17
18 #include <usr/fcntl.h>
19 #include <usr/sys/lunaix.h>
20 #include <usr/unistd.h>
21
22 #include <sdbg/protocol.h>
23
24 #include <hal/acpi/acpi.h>
25 #include <hal/ahci/ahci.h>
26 #include <hal/apic.h>
27 #include <hal/ioapic.h>
28 #include <hal/pci.h>
29 #include <hal/rtc.h>
30
31 #include <arch/x86/boot/multiboot.h>
32
33 #include <klibc/string.h>
34
35 #include <ulibc/stdio.h>
36
37 LOG_MODULE("PROC0")
38
39 extern void
40 _lxinit_main(); /* lxinit.c */
41
42 void
43 init_platform();
44
45 void
46 lock_reserved_memory();
47
48 void
49 unlock_reserved_memory();
50
51 void
52 __do_reserved_memory(int unlock);
53
54 #define USE_DEMO
55 // #define DEMO_SIGNAL
56 // #define DEMO_READDIR
57 // #define DEMO_IOTEST
58 // #define DEMO_INPUT_TEST
59 #define DEMO_SIMPLE_SH
60
61 extern void
62 _pconsole_main();
63
64 extern void
65 _signal_demo_main();
66
67 extern void
68 _lxinit_main();
69
70 extern void
71 _readdir_main();
72
73 extern void
74 _iotest_main();
75
76 extern void
77 input_test();
78
79 extern void
80 sh_main();
81
82 void __USER__
83 __setup_dir()
84 {
85     int errno;
86     mkdir("/mnt");
87     mkdir("/mnt/lunaix-os");
88
89     if ((errno = mount("/dev/sdb", "/mnt/lunaix-os", "iso9660", 0))) {
90         syslog(2, "fail mounting boot medium. (%d)\n", errno);
91     }
92 }
93
94 void __USER__
95 __proc0_usr()
96 {
97     // 打开tty设备(控制台),作为标准输入输出。
98     //  tty设备属于序列设备(Sequential Device),该类型设备的上层读写
99     //  无须经过Lunaix的缓存层,而是直接下发到底层驱动。(不受FO_DIRECT的影响)
100     int fdstdout = open("/dev/tty", 0);
101     int fdstdin = dup2(stdout, 1);
102
103     __setup_dir();
104
105     pid_t p;
106
107     if (!(p = fork())) {
108 #ifndef USE_DEMO
109         _exit(0);
110 #elif defined DEMO_SIGNAL
111         _signal_demo_main();
112 #elif defined DEMO_READDIR
113         _readdir_main();
114 #elif defined DEMO_IOTEST
115         _iotest_main();
116 #elif defined DEMO_INPUT_TEST
117         input_test();
118 #elif defined DEMO_SIMPLE_SH
119         sh_main();
120 #else
121         _lxinit_main();
122 #endif
123         printf("==== test end ====\n");
124         _exit(0);
125     }
126
127     waitpid(p, 0, 0);
128
129     while (1) {
130         yield();
131     }
132 }
133
134 /**
135  * @brief LunaixOS的零号进程,该进程永远为可执行。
136  *
137  * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
138  *
139  * 同时,该进程也负责fork出我们的init进程。
140  *
141  */
142 void
143 __proc0()
144 {
145     init_platform();
146
147     init_proc_user_space(__current);
148
149     // user space
150     asm volatile("movw %0, %%ax\n"
151                  "movw %%ax, %%es\n"
152                  "movw %%ax, %%ds\n"
153                  "movw %%ax, %%fs\n"
154                  "movw %%ax, %%gs\n"
155                  "pushl %0\n"
156                  "pushl %1\n"
157                  "pushl %2\n"
158                  "pushl %3\n"
159                  "retf" ::"i"(UDATA_SEG),
160                  "i"(USTACK_TOP & ~0xf),
161                  "i"(UCODE_SEG),
162                  "r"(__proc0_usr)
163                  : "eax", "memory");
164 }
165
166 extern uint8_t __kernel_start;            /* link/linker.ld */
167 extern uint8_t __kernel_end;              /* link/linker.ld */
168 extern uint8_t __init_hhk_end;            /* link/linker.ld */
169 extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
170
171 void
172 init_platform()
173 {
174     kprintf(KINFO "\033[11;0mLunaixOS (gcc v%s, %s)\033[39;49m\n",
175             __VERSION__,
176             __TIME__);
177
178     // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
179     lock_reserved_memory();
180
181     // firmware
182     acpi_init(_k_init_mb_info);
183
184     // die
185     apic_init();
186     ioapic_init();
187
188     // debugger
189     serial_init();
190     sdbg_init();
191
192     // timers & clock
193     rtc_init();
194     timer_init(SYS_TIMER_FREQUENCY_HZ);
195     clock_init();
196
197     // peripherals & chipset features
198     ps2_kbd_init();
199     block_init();
200     ahci_init();
201
202     pci_init();
203
204     // console
205     console_start_flushing();
206     console_flush();
207
208     // expose cake allocator states to vfs
209     cake_export();
210
211     unlock_reserved_memory();
212
213     // clean up
214     for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
215         vmm_del_mapping(VMS_SELF, (void*)i);
216         pmm_free_page(KERNEL_PID, (void*)i);
217     }
218 }
219
220 void
221 lock_reserved_memory()
222 {
223     __do_reserved_memory(0);
224 }
225
226 void
227 unlock_reserved_memory()
228 {
229     __do_reserved_memory(1);
230 }
231
232 void
233 __do_reserved_memory(int unlock)
234 {
235     multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
236     size_t map_size =
237       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
238     // v_mapping mapping;
239     for (unsigned int i = 0; i < map_size; i++) {
240         multiboot_memory_map_t mmap = mmaps[i];
241         uint8_t* pa = PG_ALIGN(mmap.addr_low);
242         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
243             // Don't fuck up our kernel code or any free area!
244             continue;
245         }
246         size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
247         size_t j = 0;
248         if (!unlock) {
249             kprintf("mem: freeze: %p..%p type=%x\n",
250                     pa,
251                     pa + pg_num * PG_SIZE,
252                     mmap.type);
253             for (; j < pg_num; j++) {
254                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
255                 if (_pa >= KERNEL_MM_BASE) {
256                     // Don't fuck up our kernel space!
257                     break;
258                 }
259                 vmm_set_mapping(VMS_SELF, _pa, _pa, PG_PREM_R, VMAP_NULL);
260                 pmm_mark_page_occupied(
261                   KERNEL_PID, _pa >> PG_SIZE_BITS, PP_FGLOCKED);
262             }
263             // Save the progress for later unmapping.
264             mmaps[i].len_low = j * PG_SIZE;
265         } else {
266             kprintf("mem: reclaim: %p..%p type=%x\n",
267                     pa,
268                     pa + pg_num * PG_SIZE,
269                     mmap.type);
270             for (; j < pg_num; j++) {
271                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
272                 vmm_del_mapping(VMS_SELF, _pa);
273                 if (mmap.type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
274                     pmm_mark_page_free(_pa >> PG_SIZE_BITS);
275                 }
276             }
277         }
278     }
279 }