Merge branch 'signal-dev'
[lunaix-os.git] / lunaix-os / kernel / proc0.c
1 #include <arch/x86/boot/multiboot.h>
2 #include <lunaix/common.h>
3 #include <lunaix/lunistd.h>
4 #include <lunaix/mm/pmm.h>
5 #include <lunaix/mm/vmm.h>
6 #include <lunaix/peripheral/ps2kbd.h>
7 #include <lunaix/proc.h>
8 #include <lunaix/spike.h>
9 #include <lunaix/syscall.h>
10 #include <lunaix/syslog.h>
11 #include <stddef.h>
12
13 #include <hal/acpi/acpi.h>
14 #include <hal/apic.h>
15 #include <hal/ioapic.h>
16
17 LOG_MODULE("PROC0")
18
19 extern void
20 _lxinit_main(); /* lxinit.c */
21
22 void
23 init_platform();
24
25 void
26 lock_reserved_memory();
27
28 void
29 unlock_reserved_memory();
30
31 void
32 __do_reserved_memory(int unlock);
33
34 void __USER__
35 __proc0_usr()
36 {
37     if (!fork()) {
38         asm("jmp _lxinit_main");
39     }
40
41     while (1) {
42         yield();
43     }
44 }
45
46 /**
47  * @brief LunaixOS的零号进程,该进程永远为可执行。
48  *
49  * 这主要是为了保证调度器在没有进程可调度时依然有事可做。
50  *
51  * 同时,该进程也负责fork出我们的init进程。
52  *
53  */
54 void
55 __proc0()
56 {
57     init_platform();
58
59     init_proc_user_space(__current);
60
61     asm volatile("movw %0, %%ax\n"
62                  "movw %%ax, %%es\n"
63                  "movw %%ax, %%ds\n"
64                  "movw %%ax, %%fs\n"
65                  "movw %%ax, %%gs\n"
66                  "pushl %0\n"
67                  "pushl %1\n"
68                  "pushl %2\n"
69                  "pushl %3\n"
70                  "retf" ::"i"(UDATA_SEG),
71                  "i"(USTACK_TOP & ~0xf),
72                  "i"(UCODE_SEG),
73                  "r"(__proc0_usr)
74                  : "eax", "memory");
75 }
76
77 extern uint8_t __kernel_start;            /* link/linker.ld */
78 extern uint8_t __kernel_end;              /* link/linker.ld */
79 extern uint8_t __init_hhk_end;            /* link/linker.ld */
80 extern multiboot_info_t* _k_init_mb_info; /* k_init.c */
81
82 void
83 init_platform()
84 {
85     assert_msg(kalloc_init(), "Fail to initialize heap");
86
87     // Fuck it, I will no longer bother this little 1MiB
88     // I just release 4 pages for my APIC & IOAPIC remappings
89     // for (size_t i = 0; i < 3; i++) {
90     //     vmm_del_mapping(PD_REFERENCED, (void*)(i << PG_SIZE_BITS));
91     // }
92
93     // 锁定所有系统预留页(内存映射IO,ACPI之类的),并且进行1:1映射
94     lock_reserved_memory();
95
96     acpi_init(_k_init_mb_info);
97     uintptr_t ioapic_addr = acpi_get_context()->madt.ioapic->ioapic_addr;
98     pmm_mark_page_occupied(
99       KERNEL_PID, FLOOR(__APIC_BASE_PADDR, PG_SIZE_BITS), 0);
100     pmm_mark_page_occupied(KERNEL_PID, FLOOR(ioapic_addr, PG_SIZE_BITS), 0);
101
102     vmm_set_mapping(
103       PD_REFERENCED, MMIO_APIC, __APIC_BASE_PADDR, PG_PREM_RW, VMAP_NULL);
104     vmm_set_mapping(
105       PD_REFERENCED, MMIO_IOAPIC, ioapic_addr, PG_PREM_RW, VMAP_NULL);
106
107     apic_init();
108     ioapic_init();
109     timer_init(SYS_TIMER_FREQUENCY_HZ);
110     clock_init();
111     ps2_kbd_init();
112
113     syscall_install();
114
115     unlock_reserved_memory();
116
117     for (size_t i = 0; i < (uintptr_t)(&__init_hhk_end); i += PG_SIZE) {
118         vmm_del_mapping(PD_REFERENCED, (void*)i);
119         pmm_free_page(KERNEL_PID, (void*)i);
120     }
121 }
122
123 void
124 lock_reserved_memory()
125 {
126     __do_reserved_memory(0);
127 }
128
129 void
130 unlock_reserved_memory()
131 {
132     __do_reserved_memory(1);
133 }
134
135 void
136 __do_reserved_memory(int unlock)
137 {
138     multiboot_memory_map_t* mmaps = _k_init_mb_info->mmap_addr;
139     size_t map_size =
140       _k_init_mb_info->mmap_length / sizeof(multiboot_memory_map_t);
141     // v_mapping mapping;
142     for (unsigned int i = 0; i < map_size; i++) {
143         multiboot_memory_map_t mmap = mmaps[i];
144         uint8_t* pa = PG_ALIGN(mmap.addr_low);
145         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE || pa <= MEM_4MB) {
146             // Don't fuck up our kernel code or any free area!
147             continue;
148         }
149         size_t pg_num = CEIL(mmap.len_low, PG_SIZE_BITS);
150         size_t j = 0;
151         if (!unlock) {
152             for (; j < pg_num; j++) {
153                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
154                 if (_pa >= KERNEL_MM_BASE) {
155                     // Don't fuck up our kernel space!
156                     break;
157                 }
158                 vmm_set_mapping(PD_REFERENCED, _pa, _pa, PG_PREM_R, VMAP_NULL);
159             }
160             // Save the progress for later unmapping.
161             mmaps[i].len_low = j * PG_SIZE;
162         } else {
163             for (; j < pg_num; j++) {
164                 uintptr_t _pa = pa + (j << PG_SIZE_BITS);
165                 vmm_del_mapping(PD_REFERENCED, _pa);
166             }
167         }
168     }
169 }