feat: AHCI probing
[lunaix-os.git] / lunaix-os / kernel / proc0.c
1 #include <arch/x86/boot/multiboot.h>
2 #include <lunaix/common.h>
3 #include <lunaix/lunistd.h>
4 #include <lunaix/lxconsole.h>
5 #include <lunaix/mm/pmm.h>
6 #include <lunaix/mm/vmm.h>
7 #include <lunaix/peripheral/ps2kbd.h>
8 #include <lunaix/proc.h>
9 #include <lunaix/spike.h>
10 #include <lunaix/syscall.h>
11 #include <lunaix/syslog.h>
12 #include <stddef.h>
13
14 #include <hal/acpi/acpi.h>
15 #include <hal/ahci.h>
16 #include <hal/apic.h>
17 #include <hal/ioapic.h>
18 #include <hal/pci.h>
19
20 LOG_MODULE("PROC0")
21
22 extern void
23 _lxinit_main(); /* lxinit.c */
24
25 void
26 init_platform();
27
28 void
29 lock_reserved_memory();
30
31 void
32 unlock_reserved_memory();
33
34 void
35 __do_reserved_memory(int unlock);
36
37 //#define USE_DEMO
38 #define DEMO_SIGNAL
39
40 extern void
41 _pconsole_main();
42
43 extern void
44 _signal_demo_main();
45
46 extern void
47 _lxinit_main();
48
49 void __USER__
50 __proc0_usr()
51 {
52     pid_t p;
53     if (!fork()) {
54         _pconsole_main();
55     }
56
57     if (!(p = fork())) {
58 #ifndef USE_DEMO
59         _exit(0);
60 #elif defined DEMO_SIGNAL
61         _signal_demo_main();
62 #else
63         _lxinit_main();
64 #endif
65     }
66
67     waitpid(p, 0, 0);
68
69     while (1) {
70         yield();
71     }
72 }
73
74 /**
75  * @brief LunaixOS的零号进程,该进程永远为可执行。
76  *
77  * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
78  *
79  * 同时,该进程也负责fork出我们的init进程。
80  *
81  */
82 void
83 __proc0()
84 {
85     init_platform();
86
87     init_proc_user_space(__current);
88
89     asm volatile("movw %0, %%ax\n"
90                  "movw %%ax, %%es\n"
91                  "movw %%ax, %%ds\n"
92                  "movw %%ax, %%fs\n"
93                  "movw %%ax, %%gs\n"
94                  "pushl %0\n"
95                  "pushl %1\n"
96                  "pushl %2\n"
97                  "pushl %3\n"
98                  "retf" ::"i"(UDATA_SEG),
99                  "i"(USTACK_TOP & ~0xf),
100                  "i"(UCODE_SEG),
101                  "r"(__proc0_usr)
102                  : "eax", "memory");
103 }
104
105 extern uint8_t __kernel_start;            /* link/linker.ld */
106 extern uint8_t __kernel_end;              /* link/linker.ld */
107 extern uint8_t __init_hhk_end;            /* link/linker.ld */
108 extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
109
110 void
111 init_platform()
112 {
113     assert_msg(kalloc_init(), "Fail to initialize heap");
114
115     // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
116     lock_reserved_memory();
117
118     acpi_init(_k_init_mb_info);
119     apic_init();
120     ioapic_init();
121     timer_init(SYS_TIMER_FREQUENCY_HZ);
122     clock_init();
123     ps2_kbd_init();
124     pci_init();
125     ahci_init();
126     pci_print_device();
127
128     syscall_install();
129
130     console_start_flushing();
131     console_flush();
132
133     unlock_reserved_memory();
134
135     for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
136         vmm_del_mapping(PD_REFERENCED, (void*)i);
137         pmm_free_page(KERNEL_PID, (void*)i);
138     }
139 }
140
141 void
142 lock_reserved_memory()
143 {
144     __do_reserved_memory(0);
145 }
146
147 void
148 unlock_reserved_memory()
149 {
150     __do_reserved_memory(1);
151 }
152
153 void
154 __do_reserved_memory(int unlock)
155 {
156     multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
157     size_t map_size =
158       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
159     // v_mapping mapping;
160     for (unsigned int i = 0; i < map_size; i++) {
161         multiboot_memory_map_t mmap = mmaps[i];
162         uint8_t* pa = PG_ALIGN(mmap.addr_low);
163         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
164             // Don't fuck up our kernel code or any free area!
165             continue;
166         }
167         size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
168         size_t j = 0;
169         if (!unlock) {
170             for (; j < pg_num; j++) {
171                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
172                 if (_pa >= KERNEL_MM_BASE) {
173                     // Don't fuck up our kernel space!
174                     break;
175                 }
176                 vmm_set_mapping(PD_REFERENCED, _pa, _pa, PG_PREM_R, VMAP_NULL);
177             }
178             // Save the progress for later unmapping.
179             mmaps[i].len_low = j * PG_SIZE;
180         } else {
181             for (; j < pg_num; j++) {
182                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
183                 vmm_del_mapping(PD_REFERENCED, _pa);
184             }
185         }
186     }
187 }