SayoriOS 0.3.3

pmm.h
1 #pragma once
2
3 // Scyther Physical Memory Manager by NDRAEY (c) 2023
4 // for SayoriOS
5
6 #include "common.h"
7 #include "multiboot.h"
8
9 typedef uint32_t pt_entry;
10 typedef uint32_t pd_entry;
11
12 extern size_t phys_memory_size;
13 extern size_t used_phys_memory_size;
14
15 extern size_t kernel_start;
16 extern size_t kernel_end;
17
18 #define PAGE_SIZE 0x1000
19 #define PAGE_OFFSET_MASK 0xFFF
20 #define PAGE_TABLE_INDEX_BITS 10
21 #define PAGE_TABLE_INDEX_MASK 0x3FF
22
23 #define PAGE_PRESENT (1U << 0)
24 #define PAGE_WRITEABLE (1U << 1)
25 #define PAGE_USER (1U << 2)
26 #define PAGE_WRITE_THROUGH (1U << 3)
27 #define PAGE_CACHE_DISABLE (1U << 4)
28 #define PAGE_ACCESSED (1U << 5)
29 #define PAGE_DIRTY (1U << 6)
30 #define PAGE_GLOBAL (1U << 8)
31
32 #define PAGE_BITMAP_SIZE (131072 * 4)
33
34 #define PD_INDEX(virt_addr) ((virt_addr) >> 22)
35 #define PT_INDEX(virt_addr) (((virt_addr) >> 12) & 0x3ff)
36
37 typedef uint32_t virtual_addr_t;
38 typedef uint32_t physical_addr_t;
39
40 // The space where first page table starts
42 static uint32_t* page_directory_start = (uint32_t*)(0xffffffff - (4 * MB) + 1);
43
44 // The space where we can modify page directory
46 static uint32_t* page_directory_virt = (uint32_t*)(0xffffffff - (4 * KB) + 1);
47
48
49 extern void load_page_directory(size_t addr);
50 extern void enable_paging();
51
52 physical_addr_t phys_alloc_single_page();
53 physical_addr_t phys_alloc_multi_pages(size_t count);
54 void phys_free_single_page(physical_addr_t addr);
55 void phys_free_multi_pages(physical_addr_t addr, size_t count);
56 void map_single_page(physical_addr_t* page_dir, physical_addr_t physical, virtual_addr_t virtual, uint32_t flags);
57 void unmap_single_page(uint32_t* page_dir, virtual_addr_t virtual);
58 void map_pages(uint32_t* page_dir, physical_addr_t physical, virtual_addr_t virtual, size_t size, uint32_t flags);
59 void phys_not_enough_memory();
60 void blank_page_directory(uint32_t* pagedir_addr);
61 bool phys_is_used_page(physical_addr_t addr);
62 void phys_mark_page_entry(physical_addr_t addr, uint8_t used);
63 uint32_t phys_get_page_data(uint32_t* page_dir, virtual_addr_t virtual);
64 uint32_t virt2phys(const uint32_t *page_dir, virtual_addr_t virtual);
65 void init_paging();
66 uint32_t* get_kernel_page_directory();
67
68 void map_pages_overlapping(physical_addr_t* page_directory, size_t physical_start, size_t virtual_start, size_t size, uint32_t flags);
69 void unmap_pages_overlapping(physical_addr_t* page_directory, size_t virtual, size_t size);
70 void phys_set_flags(uint32_t* page_dir, virtual_addr_t virtual, uint32_t flags);
common.h
Основные определения ядра