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