feat: spec-compliant AHCI HBA initialization
[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     ahci_list_device();
128
129     syscall_install();
130
131     console_start_flushing();
132     console_flush();
133
134     unlock_reserved_memory();
135
136     for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
137         vmm_del_mapping(PD_REFERENCED, (void*)i);
138         pmm_free_page(KERNEL_PID, (void*)i);
139     }
140 }
141
142 void
143 lock_reserved_memory()
144 {
145     __do_reserved_memory(0);
146 }
147
148 void
149 unlock_reserved_memory()
150 {
151     __do_reserved_memory(1);
152 }
153
154 void
155 __do_reserved_memory(int unlock)
156 {
157     multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
158     size_t map_size =
159       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
160     // v_mapping mapping;
161     for (unsigned int i = 0; i < map_size; i++) {
162         multiboot_memory_map_t mmap = mmaps[i];
163         uint8_t* pa = PG_ALIGN(mmap.addr_low);
164         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
165             // Don't fuck up our kernel code or any free area!
166             continue;
167         }
168         size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
169         size_t j = 0;
170         if (!unlock) {
171             for (; j < pg_num; j++) {
172                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
173                 if (_pa >= KERNEL_MM_BASE) {
174                     // Don't fuck up our kernel space!
175                     break;
176                 }
177                 vmm_set_mapping(PD_REFERENCED, _pa, _pa, PG_PREM_R, VMAP_NULL);
178                 pmm_mark_page_occupied(
179                   KERNEL_PID, _pa >> PG_SIZE_BITS, PP_FGLOCKED);
180             }
181             // Save the progress for later unmapping.
182             mmaps[i].len_low = j * PG_SIZE;
183         } else {
184             for (; j < pg_num; j++) {
185                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
186                 vmm_del_mapping(PD_REFERENCED, _pa);
187                 if (mmap.type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
188                     pmm_mark_page_free(_pa >> PG_SIZE_BITS);
189                 }
190             }
191         }
192     }
193 }