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