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