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