refactor: full rewrite of signal feature
[lunaix-os.git] / lunaix-os / kernel / proc0.c
1 #include <lunaix/block.h>
2 #include <lunaix/common.h>
3 #include <lunaix/exec.h>
4 #include <lunaix/foptions.h>
5 #include <lunaix/fs.h>
6 #include <lunaix/fs/probe_boot.h>
7 #include <lunaix/fs/twifs.h>
8 #include <lunaix/lxconsole.h>
9 #include <lunaix/mm/cake.h>
10 #include <lunaix/mm/pmm.h>
11 #include <lunaix/mm/valloc.h>
12 #include <lunaix/mm/vmm.h>
13 #include <lunaix/peripheral/ps2kbd.h>
14 #include <lunaix/peripheral/serial.h>
15 #include <lunaix/spike.h>
16 #include <lunaix/syscall.h>
17 #include <lunaix/syslog.h>
18 #include <lunaix/types.h>
19
20 #include <sdbg/protocol.h>
21
22 #include <hal/acpi/acpi.h>
23 #include <hal/ahci/ahci.h>
24 #include <hal/apic.h>
25 #include <hal/ioapic.h>
26 #include <hal/pci.h>
27 #include <hal/rtc.h>
28
29 #include <arch/x86/boot/multiboot.h>
30
31 #include <klibc/string.h>
32
33 LOG_MODULE("PROC0")
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 int
48 mount_bootmedium()
49 {
50     struct v_dnode* dnode;
51     int errno = 0;
52     struct device* dev = probe_boot_medium();
53     if (!dev) {
54         kprintf(KERROR "fail to acquire device. (%d)", errno);
55         return 0;
56     }
57
58     if ((errno = vfs_mount("/mnt/lunaix-os", "iso9660", dev, 0))) {
59         kprintf(KERROR "fail to mount boot medium. (%d)", errno);
60         return 0;
61     }
62
63     return 1;
64 }
65
66 int
67 exec_initd()
68 {
69     int errno = 0;
70
71     if ((errno = exec_kexecve("/mnt/lunaix-os/usr/bin/init", NULL, NULL))) {
72         goto fail;
73     }
74
75     fail("should not reach");
76
77 fail:
78     kprintf(KERROR "fail to load initd. (%d)", errno);
79     return 0;
80 }
81
82 /**
83  * @brief LunaixOS的零号进程,该进程永远为可执行。
84  *
85  * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
86  *
87  * 同时,该进程也负责fork出我们的init进程。
88  *
89  */
90 void
91 __proc0()
92 {
93     init_platform();
94
95     init_proc_user_space(__current);
96
97     if (!mount_bootmedium() || !exec_initd()) {
98         while (1) {
99             asm("hlt");
100         }
101         // should not reach
102     }
103 }
104
105 extern u8_t __kernel_start;               /* link/linker.ld */
106 extern u8_t __kernel_end;                 /* link/linker.ld */
107 extern u8_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     kprintf(KINFO "\033[11;0mLunaixOS (gcc v%s, %s)\033[39;49m\n",
114             __VERSION__,
115             __TIME__);
116
117     // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
118     lock_reserved_memory();
119
120     // firmware
121     acpi_init(_k_init_mb_info);
122
123     // die
124     apic_init();
125     ioapic_init();
126
127     // debugger
128     serial_init();
129     sdbg_init();
130
131     // timers & clock
132     rtc_init();
133     timer_init(SYS_TIMER_FREQUENCY_HZ);
134     clock_init();
135
136     // peripherals & chipset features
137     ps2_kbd_init();
138     block_init();
139     ahci_init();
140
141     pci_init();
142
143     // console
144     console_start_flushing();
145     console_flush();
146
147     // expose cake allocator states to vfs
148     cake_export();
149
150     unlock_reserved_memory();
151
152     // clean up
153     for (size_t i = 0; i < (ptr_t)(&__init_hhk_end); i += PG_SIZE) {
154         vmm_del_mapping(VMS_SELF, (ptr_t)i);
155         pmm_free_page(KERNEL_PID, (ptr_t)i);
156     }
157 }
158
159 void
160 lock_reserved_memory()
161 {
162     __do_reserved_memory(0);
163 }
164
165 void
166 unlock_reserved_memory()
167 {
168     __do_reserved_memory(1);
169 }
170
171 void
172 __do_reserved_memory(int unlock)
173 {
174     multiboot_memory_map_t* mmaps =
175       (multiboot_memory_map_t*)_k_init_mb_info->mmap_addr;
176
177     size_t map_size =
178       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
179
180     // v_mapping mapping;
181     for (unsigned int i = 0; i < map_size; i++) {
182         multiboot_memory_map_t mmap = mmaps[i];
183         ptr_t pa = PG_ALIGN(mmap.addr_low);
184
185         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
186             // Don't fuck up our kernel code or any free area!
187             continue;
188         }
189
190         size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
191         size_t j = 0;
192
193         if (!unlock) {
194             kprintf("mem: freeze: %p..%p type=%x\n",
195                     pa,
196                     pa + pg_num * PG_SIZE,
197                     mmap.type);
198
199             for (; j < pg_num; j++) {
200                 ptr_t _pa = pa + (j << PG_SIZE_BITS);
201                 if (_pa >= KERNEL_MM_BASE) {
202                     // Don't fuck up our kernel space!
203                     break;
204                 }
205                 vmm_set_mapping(VMS_SELF, _pa, _pa, PG_PREM_R, VMAP_NULL);
206                 pmm_mark_page_occupied(
207                   KERNEL_PID, _pa >> PG_SIZE_BITS, PP_FGLOCKED);
208             }
209
210             // Save the progress for later unmapping.
211             mmaps[i].len_low = j * PG_SIZE;
212         } else {
213             kprintf("mem: reclaim: %p..%p type=%x\n",
214                     pa,
215                     pa + pg_num * PG_SIZE,
216                     mmap.type);
217
218             for (; j < pg_num; j++) {
219                 ptr_t _pa = pa + (j << PG_SIZE_BITS);
220                 vmm_del_mapping(VMS_SELF, _pa);
221                 if (mmap.type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
222                     pmm_mark_page_free(_pa >> PG_SIZE_BITS);
223                 }
224             }
225         }
226     }
227 }