feat: readdir fix and demo
[lunaix-os.git] / lunaix-os / kernel / proc0.c
1 #include <arch/x86/boot/multiboot.h>
2 #include <lunaix/common.h>
3 #include <lunaix/fs.h>
4 #include <lunaix/fs/twifs.h>
5 #include <lunaix/lunistd.h>
6 #include <lunaix/lxconsole.h>
7 #include <lunaix/mm/cake.h>
8 #include <lunaix/mm/pmm.h>
9 #include <lunaix/mm/valloc.h>
10 #include <lunaix/mm/vmm.h>
11 #include <lunaix/peripheral/ps2kbd.h>
12 #include <lunaix/proc.h>
13 #include <lunaix/spike.h>
14 #include <lunaix/syscall.h>
15 #include <lunaix/syslog.h>
16 #include <stddef.h>
17
18 #include <hal/acpi/acpi.h>
19 #include <hal/ahci/ahci.h>
20 #include <hal/apic.h>
21 #include <hal/ioapic.h>
22 #include <hal/pci.h>
23
24 #include <klibc/string.h>
25
26 LOG_MODULE("PROC0")
27
28 extern void
29 _lxinit_main(); /* lxinit.c */
30
31 void
32 init_platform();
33
34 void
35 lock_reserved_memory();
36
37 void
38 unlock_reserved_memory();
39
40 void
41 __do_reserved_memory(int unlock);
42
43 #define USE_DEMO
44 // #define DEMO_SIGNAL
45 #define DEMO_READDIR
46
47 extern void
48 _pconsole_main();
49
50 extern void
51 _signal_demo_main();
52
53 extern void
54 _lxinit_main();
55
56 extern void
57 _readdir_main();
58
59 void __USER__
60 __proc0_usr()
61 {
62     pid_t p;
63     if (!fork()) {
64         _pconsole_main();
65     }
66
67     if (!(p = fork())) {
68 #ifndef USE_DEMO
69         _exit(0);
70 #elif defined DEMO_SIGNAL
71         _signal_demo_main();
72 #elif defined DEMO_READDIR
73         _readdir_main();
74 #else
75         _lxinit_main();
76 #endif
77     }
78
79     waitpid(p, 0, 0);
80
81     while (1) {
82         yield();
83     }
84 }
85
86 /**
87  * @brief LunaixOS的零号进程,该进程永远为可执行。
88  *
89  * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
90  *
91  * 同时,该进程也负责fork出我们的init进程。
92  *
93  */
94 void
95 __proc0()
96 {
97     init_platform();
98
99     init_proc_user_space(__current);
100
101     asm volatile("movw %0, %%ax\n"
102                  "movw %%ax, %%es\n"
103                  "movw %%ax, %%ds\n"
104                  "movw %%ax, %%fs\n"
105                  "movw %%ax, %%gs\n"
106                  "pushl %0\n"
107                  "pushl %1\n"
108                  "pushl %2\n"
109                  "pushl %3\n"
110                  "retf" ::"i"(UDATA_SEG),
111                  "i"(USTACK_TOP & ~0xf),
112                  "i"(UCODE_SEG),
113                  "r"(__proc0_usr)
114                  : "eax", "memory");
115 }
116
117 extern uint8_t __kernel_start;            /* link/linker.ld */
118 extern uint8_t __kernel_end;              /* link/linker.ld */
119 extern uint8_t __init_hhk_end;            /* link/linker.ld */
120 extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
121
122 char test_sequence[] = "Once upon a time, in a magical land of Equestria. "
123                        "There were two regal sisters who ruled together "
124                        "and created harmony for all the land.";
125
126 void
127 __test_disk_io()
128 {
129     struct hba_port* port = ahci_get_port(0);
130     struct hba_device* dev = port->device;
131     char* buffer = vzalloc_dma(port->device->block_size);
132     strcpy(buffer, test_sequence);
133     kprintf("WRITE: %s\n", buffer);
134     int result;
135
136     // 写入第一扇区 (LBA=0)
137     result = dev->ops.write_buffer(dev, 0, buffer, dev->block_size);
138     if (!result) {
139         kprintf(KWARN "fail to write: %x\n", dev->last_error);
140     }
141
142     memset(buffer, 0, dev->block_size);
143
144     // 读出我们刚刚写的内容!
145     result = dev->ops.read_buffer(dev, 0, buffer, dev->block_size);
146     kprintf(KDEBUG "%x, %x\n", port->regs[HBA_RPxIS], port->regs[HBA_RPxTFD]);
147     if (!result) {
148         kprintf(KWARN "fail to read: %x\n", dev->last_error);
149     } else {
150         kprint_hex(buffer, 256);
151     }
152
153     vfree_dma(buffer);
154 }
155
156 void
157 init_platform()
158 {
159     // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
160     lock_reserved_memory();
161
162     assert_msg(kalloc_init(), "Fail to initialize heap");
163
164     acpi_init(_k_init_mb_info);
165     apic_init();
166     ioapic_init();
167     timer_init(SYS_TIMER_FREQUENCY_HZ);
168     clock_init();
169     ps2_kbd_init();
170     pci_init();
171     ahci_init();
172     // ahci_list_device();
173
174     fsm_init();
175     vfs_init();
176     twifs_init();
177
178     vfs_mount("/", "twifs", -1);
179
180     //__test_disk_io();
181
182     // cake_stats();
183
184     syscall_install();
185
186     console_start_flushing();
187     console_flush();
188
189     unlock_reserved_memory();
190
191     for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
192         vmm_del_mapping(PD_REFERENCED, (void*)i);
193         pmm_free_page(KERNEL_PID, (void*)i);
194     }
195 }
196
197 void
198 lock_reserved_memory()
199 {
200     __do_reserved_memory(0);
201 }
202
203 void
204 unlock_reserved_memory()
205 {
206     __do_reserved_memory(1);
207 }
208
209 void
210 __do_reserved_memory(int unlock)
211 {
212     multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
213     size_t map_size =
214       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
215     // v_mapping mapping;
216     for (unsigned int i = 0; i < map_size; i++) {
217         multiboot_memory_map_t mmap = mmaps[i];
218         uint8_t* pa = PG_ALIGN(mmap.addr_low);
219         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
220             // Don't fuck up our kernel code or any free area!
221             continue;
222         }
223         size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
224         size_t j = 0;
225         if (!unlock) {
226             for (; j < pg_num; j++) {
227                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
228                 if (_pa >= KERNEL_MM_BASE) {
229                     // Don't fuck up our kernel space!
230                     break;
231                 }
232                 vmm_set_mapping(PD_REFERENCED, _pa, _pa, PG_PREM_R, VMAP_NULL);
233                 pmm_mark_page_occupied(
234                   KERNEL_PID, _pa >> PG_SIZE_BITS, PP_FGLOCKED);
235             }
236             // Save the progress for later unmapping.
237             mmaps[i].len_low = j * PG_SIZE;
238         } else {
239             for (; j < pg_num; j++) {
240                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
241                 vmm_del_mapping(PD_REFERENCED, _pa);
242                 if (mmap.type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
243                     pmm_mark_page_free(_pa >> PG_SIZE_BITS);
244                 }
245             }
246         }
247     }
248 }