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