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