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