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