Merge branch 'master' into vfs-dev
[lunaix-os.git] / lunaix-os / kernel / proc0.c
1 #include <arch/x86/boot/multiboot.h>
2 #include <lunaix/block.h>
3 #include <lunaix/common.h>
4 #include <lunaix/fctrl.h>
5 #include <lunaix/fs.h>
6 #include <lunaix/fs/twifs.h>
7 #include <lunaix/lunistd.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/proc.h>
15 #include <lunaix/spike.h>
16 #include <lunaix/syscall.h>
17 #include <lunaix/syslog.h>
18 #include <stddef.h>
19
20 #include <hal/acpi/acpi.h>
21 #include <hal/ahci/ahci.h>
22 #include <hal/apic.h>
23 #include <hal/ioapic.h>
24 #include <hal/pci.h>
25
26 #include <klibc/string.h>
27
28 LOG_MODULE("PROC0")
29
30 extern void
31 _lxinit_main(); /* lxinit.c */
32
33 void
34 init_platform();
35
36 void
37 lock_reserved_memory();
38
39 void
40 unlock_reserved_memory();
41
42 void
43 __do_reserved_memory(int unlock);
44
45 #define USE_DEMO
46 // #define DEMO_SIGNAL
47 // #define DEMO_READDIR
48 #define DEMO_IOTEST
49
50 extern void
51 _pconsole_main();
52
53 extern void
54 _signal_demo_main();
55
56 extern void
57 _lxinit_main();
58
59 extern void
60 _readdir_main();
61
62 extern void
63 _iotest_main();
64
65 void __USER__
66 __proc0_usr()
67 {
68     // 打开tty设备(控制台),作为标准输入输出。
69     //  tty设备属于序列设备(Sequential Device),该类型设备的上层读写
70     //  无须经过Lunaix的缓存层,而是直接下发到底层驱动。(不受FO_DIRECT的影响)
71     int stdout = open("/dev/tty", 0);
72     int stdin = dup2(stdout, 1);
73
74     pid_t p;
75     if (!fork()) {
76         _pconsole_main();
77     }
78
79     if (!(p = fork())) {
80 #ifndef USE_DEMO
81         _exit(0);
82 #elif defined DEMO_SIGNAL
83         _signal_demo_main();
84 #elif defined DEMO_READDIR
85         _readdir_main();
86 #elif defined DEMO_IOTEST
87         _iotest_main();
88 #else
89         _lxinit_main();
90 #endif
91     }
92
93     waitpid(p, 0, 0);
94
95     while (1) {
96         yield();
97     }
98 }
99
100 /**
101  * @brief LunaixOS的零号进程,该进程永远为可执行。
102  *
103  * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
104  *
105  * 同时,该进程也负责fork出我们的init进程。
106  *
107  */
108 void
109 __proc0()
110 {
111     init_platform();
112
113     init_proc_user_space(__current);
114
115     asm volatile("movw %0, %%ax\n"
116                  "movw %%ax, %%es\n"
117                  "movw %%ax, %%ds\n"
118                  "movw %%ax, %%fs\n"
119                  "movw %%ax, %%gs\n"
120                  "pushl %0\n"
121                  "pushl %1\n"
122                  "pushl %2\n"
123                  "pushl %3\n"
124                  "retf" ::"i"(UDATA_SEG),
125                  "i"(USTACK_TOP & ~0xf),
126                  "i"(UCODE_SEG),
127                  "r"(__proc0_usr)
128                  : "eax", "memory");
129 }
130
131 extern uint8_t __kernel_start;            /* link/linker.ld */
132 extern uint8_t __kernel_end;              /* link/linker.ld */
133 extern uint8_t __init_hhk_end;            /* link/linker.ld */
134 extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
135
136 void
137 init_platform()
138 {
139     // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
140     lock_reserved_memory();
141
142     assert_msg(kalloc_init(), "Fail to initialize heap");
143
144     acpi_init(_k_init_mb_info);
145     apic_init();
146     ioapic_init();
147     timer_init(SYS_TIMER_FREQUENCY_HZ);
148     clock_init();
149     ps2_kbd_init();
150     pci_init();
151     block_init();
152     ahci_init();
153     // ahci_list_device();
154     // cake_stats();
155
156     syscall_install();
157
158     console_start_flushing();
159     console_flush();
160
161     unlock_reserved_memory();
162
163     for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
164         vmm_del_mapping(PD_REFERENCED, (void*)i);
165         pmm_free_page(KERNEL_PID, (void*)i);
166     }
167 }
168
169 void
170 lock_reserved_memory()
171 {
172     __do_reserved_memory(0);
173 }
174
175 void
176 unlock_reserved_memory()
177 {
178     __do_reserved_memory(1);
179 }
180
181 void
182 __do_reserved_memory(int unlock)
183 {
184     multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
185     size_t map_size =
186       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
187     // v_mapping mapping;
188     for (unsigned int i = 0; i < map_size; i++) {
189         multiboot_memory_map_t mmap = mmaps[i];
190         uint8_t* pa = PG_ALIGN(mmap.addr_low);
191         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
192             // Don't fuck up our kernel code or any free area!
193             continue;
194         }
195         size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
196         size_t j = 0;
197         if (!unlock) {
198             for (; j < pg_num; j++) {
199                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
200                 if (_pa >= KERNEL_MM_BASE) {
201                     // Don't fuck up our kernel space!
202                     break;
203                 }
204                 vmm_set_mapping(PD_REFERENCED, _pa, _pa, PG_PREM_R, VMAP_NULL);
205                 pmm_mark_page_occupied(
206                   KERNEL_PID, _pa >> PG_SIZE_BITS, PP_FGLOCKED);
207             }
208             // Save the progress for later unmapping.
209             mmaps[i].len_low = j * PG_SIZE;
210         } else {
211             for (; j < pg_num; j++) {
212                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
213                 vmm_del_mapping(PD_REFERENCED, _pa);
214                 if (mmap.type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
215                     pmm_mark_page_free(_pa >> PG_SIZE_BITS);
216                 }
217             }
218         }
219     }
220 }