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