fix bugs found in brk & add simple security checks on lx_free
[lunaix-os.git] / lunaix-os / kernel / k_init.c
1 #include <lunaix/constants.h>
2 #include <lunaix/tty/tty.h>
3
4 #include <lunaix/mm/page.h>
5 #include <lunaix/mm/pmm.h>
6 #include <lunaix/mm/vmm.h>
7 #include <lunaix/mm/kalloc.h>
8 #include <lunaix/spike.h>
9
10 #include <arch/x86/boot/multiboot.h>
11 #include <arch/x86/idt.h>
12
13 #include <libc/stdio.h>
14
15 #include <stdint.h>
16 #include <stddef.h>
17
18
19 extern uint8_t __kernel_start;
20 extern uint8_t __kernel_end;
21 extern uint8_t __init_hhk_end;
22
23 void
24 setup_memory(multiboot_memory_map_t* map, size_t map_size);
25
26 void
27 setup_kernel_runtime();
28
29 void
30 _kernel_pre_init(multiboot_info_t* mb_info) {
31     _init_idt();
32
33     pmm_init(MEM_1MB + (mb_info->mem_upper << 10));
34     vmm_init();
35
36     tty_init((void*)VGA_BUFFER_PADDR);
37     tty_set_theme(VGA_COLOR_GREEN, VGA_COLOR_BLACK);
38 }
39
40 void
41 _kernel_init(multiboot_info_t* mb_info) {
42     printf("[KERNEL] === Initialization === \n");
43
44     printf("[MM] Mem: %d KiB, Extended Mem: %d KiB\n",
45            mb_info->mem_lower,
46            mb_info->mem_upper);
47
48     unsigned int map_size = mb_info->mmap_length / sizeof(multiboot_memory_map_t);
49     setup_memory((multiboot_memory_map_t*)mb_info->mmap_addr, map_size);
50     setup_kernel_runtime();
51 }
52
53 void 
54 _kernel_post_init() {
55     printf("[KERNEL] === Post Initialization === \n");
56     size_t hhk_init_pg_count = ((uintptr_t)(&__init_hhk_end)) >> PG_SIZE_BITS;
57     printf("[MM] Releaseing %d pages from 0x0.\n", hhk_init_pg_count);
58
59     // 清除 hhk_init 与前1MiB的映射
60     for (size_t i = 0; i < hhk_init_pg_count; i++) {
61         vmm_unmap_page((void*)(i << PG_SIZE_BITS));
62     }
63
64     assert_msg(kalloc_init(), "Fail to initialize heap");
65 }
66
67 // 按照 Memory map 标识可用的物理页
68 void
69 setup_memory(multiboot_memory_map_t* map, size_t map_size) {
70     for (unsigned int i = 0; i < map_size; i++) {
71         multiboot_memory_map_t mmap = map[i];
72         printf("[MM] Base: 0x%x, len: %u KiB, type: %u\n",
73                map[i].addr_low,
74                map[i].len_low >> 10,
75                map[i].type);
76         if (mmap.type == MULTIBOOT_MEMORY_AVAILABLE) {
77             // 整数向上取整除法
78             uintptr_t pg = map[i].addr_low + 0x0fffU;
79             pmm_mark_chunk_free(pg >> PG_SIZE_BITS, map[i].len_low >> PG_SIZE_BITS);
80             printf("[MM] Freed %u pages start from 0x%x\n",
81                    map[i].len_low >> PG_SIZE_BITS,
82                    pg & ~0x0fffU);
83         }
84     }
85
86     // 将内核占据的页设为已占用
87     size_t pg_count = (uintptr_t)(&__kernel_end - &__kernel_start) >> PG_SIZE_BITS;
88     pmm_mark_chunk_occupied(V2P(&__kernel_start) >> PG_SIZE_BITS, pg_count);
89     printf("[MM] Allocated %d pages for kernel.\n", pg_count);
90
91
92     size_t vga_buf_pgs = VGA_BUFFER_SIZE >> PG_SIZE_BITS;
93     
94     // 首先,标记VGA部分为已占用
95     pmm_mark_chunk_occupied(VGA_BUFFER_PADDR >> PG_SIZE_BITS, vga_buf_pgs);
96     
97     // 重映射VGA文本缓冲区(以后会变成显存,i.e., framebuffer)
98     for (size_t i = 0; i < vga_buf_pgs; i++)
99     {
100         vmm_map_page(
101             (void*)(VGA_BUFFER_VADDR + (i << PG_SIZE_BITS)), 
102             (void*)(VGA_BUFFER_PADDR + (i << PG_SIZE_BITS)), 
103             PG_PREM_RW
104         );
105     }
106     
107     // 更新VGA缓冲区位置至虚拟地址
108     tty_set_buffer((void*)VGA_BUFFER_VADDR);
109
110     printf("[MM] Mapped VGA to %p.\n", VGA_BUFFER_VADDR);
111 }
112
113 void
114 setup_kernel_runtime() {
115     // 为内核创建一个专属栈空间。
116     for (size_t i = 0; i < (K_STACK_SIZE >> PG_SIZE_BITS); i++) {
117         vmm_alloc_page((void*)(K_STACK_START + (i << PG_SIZE_BITS)), PG_PREM_RW);
118     }
119     printf("[MM] Allocated %d pages for stack start at %p\n", K_STACK_SIZE>>PG_SIZE_BITS, K_STACK_START);
120 }