xref: /DragonOS/kernel/src/mm/mm.h (revision 5db5a5652c9f4ae77bc8dc298f02057299442208)
1 #pragma once
2 
3 #include <asm/current.h>
4 #include <common/glib.h>
5 #include <mm/mm-types.h>
6 #include <process/process.h>
7 
8 extern void rs_pseudo_map_phys(uint64_t virt_addr, uint64_t phys_addr, uint64_t size);
9 extern void rs_map_phys(uint64_t virt_addr, uint64_t phys_addr, uint64_t size, uint64_t flags);
10 extern uint64_t rs_unmap_at_low_addr();
11 
12 // 内核层的起始地址
13 #define PAGE_OFFSET 0xffff800000000000UL
14 #define KERNEL_BASE_LINEAR_ADDR 0xffff800000000000UL
15 #define USER_MAX_LINEAR_ADDR 0x00007fffffffffffUL
16 // MMIO虚拟地址空间:1TB
17 #define MMIO_BASE 0xffffa10000000000UL
18 #define MMIO_TOP 0xffffa20000000000UL
19 
20 #define PAGE_4K_SHIFT 12
21 #define PAGE_2M_SHIFT 21
22 #define PAGE_1G_SHIFT 30
23 #define PAGE_GDT_SHIFT 39
24 
25 // 不同大小的页的容量
26 #define PAGE_4K_SIZE (1UL << PAGE_4K_SHIFT)
27 #define PAGE_2M_SIZE (1UL << PAGE_2M_SHIFT)
28 #define PAGE_1G_SIZE (1UL << PAGE_1G_SHIFT)
29 
30 // 屏蔽低于x的数值
31 #define PAGE_4K_MASK (~(PAGE_4K_SIZE - 1))
32 #define PAGE_2M_MASK (~(PAGE_2M_SIZE - 1))
33 
34 // 将addr按照x的上边界对齐
35 #define PAGE_4K_ALIGN(addr) (((unsigned long)(addr) + PAGE_4K_SIZE - 1) & PAGE_4K_MASK)
36 #define PAGE_2M_ALIGN(addr) (((unsigned long)(addr) + PAGE_2M_SIZE - 1) & PAGE_2M_MASK)
37 
38 // 虚拟地址与物理地址转换
39 #define virt_2_phys(addr) ((unsigned long)(addr)-PAGE_OFFSET)
40 #define phys_2_virt(addr) ((unsigned long *)((unsigned long)(addr) + PAGE_OFFSET))
41 
42 // 在这个地址以上的虚拟空间,用来进行特殊的映射
43 #define SPECIAL_MEMOEY_MAPPING_VIRT_ADDR_BASE 0xffffa00000000000UL
44 #define FRAME_BUFFER_MAPPING_OFFSET 0x3000000UL
45 #define IO_APIC_MAPPING_OFFSET 0xfec00000UL
46 #define LOCAL_APIC_MAPPING_OFFSET 0xfee00000UL
47 #define AHCI_MAPPING_OFFSET 0xff200000UL // AHCI 映射偏移量,之后使用了4M的地址
48 
49 // ===== 页面属性 =====
50 // 页面在页表中已被映射 mapped=1 unmapped=0
51 #define PAGE_PGT_MAPPED (1 << 0)
52 
53 // 内核初始化所占用的页 init-code=1 normal-code/data=0
54 #define PAGE_KERNEL_INIT (1 << 1)
55 
56 // 1=设备MMIO映射的内存 0=物理内存
57 #define PAGE_DEVICE (1 << 2)
58 
59 // 内核层页 kernel=1 memory=0
60 #define PAGE_KERNEL (1 << 3)
61 
62 // 共享的页 shared=1 single-use=0
63 #define PAGE_SHARED (1 << 4)
64 
65 // =========== 页表项权限 ========
66 
67 //	bit 63	Execution Disable:
68 #define PAGE_XD (1UL << 63)
69 
70 //	bit 12	Page Attribute Table
71 #define PAGE_PAT (1UL << 12)
72 // 对于PTE而言,第7位是PAT
73 #define PAGE_4K_PAT (1UL << 7)
74 
75 //	bit 8	Global Page:1,global;0,part
76 #define PAGE_GLOBAL (1UL << 8)
77 
78 //	bit 7	Page Size:1,big page;0,small page;
79 #define PAGE_PS (1UL << 7)
80 
81 //	bit 6	Dirty:1,dirty;0,clean;
82 #define PAGE_DIRTY (1UL << 6)
83 
84 //	bit 5	Accessed:1,visited;0,unvisited;
85 #define PAGE_ACCESSED (1UL << 5)
86 
87 //	bit 4	Page Level Cache Disable
88 #define PAGE_PCD (1UL << 4)
89 
90 //	bit 3	Page Level Write Through
91 #define PAGE_PWT (1UL << 3)
92 
93 //	bit 2	User Supervisor:1,user and supervisor;0,supervisor;
94 #define PAGE_U_S (1UL << 2)
95 
96 //	bit 1	Read Write:1,read and write;0,read;
97 #define PAGE_R_W (1UL << 1)
98 
99 //	bit 0	Present:1,present;0,no present;
100 #define PAGE_PRESENT (1UL << 0)
101 
102 // 1,0
103 #define PAGE_KERNEL_PGT (PAGE_R_W | PAGE_PRESENT)
104 
105 // 1,0
106 #define PAGE_KERNEL_DIR (PAGE_R_W | PAGE_PRESENT)
107 
108 // 1,0 (4级页表在3级页表中的页表项的属性)
109 #define PAGE_KERNEL_PDE (PAGE_R_W | PAGE_PRESENT)
110 
111 // 7,1,0
112 #define PAGE_KERNEL_PAGE (PAGE_PS | PAGE_R_W | PAGE_PRESENT)
113 
114 #define PAGE_KERNEL_4K_PAGE (PAGE_R_W | PAGE_PRESENT)
115 
116 #define PAGE_USER_PGT (PAGE_U_S | PAGE_R_W | PAGE_PRESENT)
117 
118 // 2,1,0
119 #define PAGE_USER_DIR (PAGE_U_S | PAGE_R_W | PAGE_PRESENT)
120 
121 // 1,0 (4级页表在3级页表中的页表项的属性)
122 #define PAGE_USER_PDE (PAGE_U_S | PAGE_R_W | PAGE_PRESENT)
123 // 7,2,1,0
124 #define PAGE_USER_PAGE (PAGE_PS | PAGE_U_S | PAGE_R_W | PAGE_PRESENT)
125 
126 #define PAGE_USER_4K_PAGE (PAGE_U_S | PAGE_R_W | PAGE_PRESENT)
127 
128 /**
129  * @brief 刷新TLB的宏定义
130  * 由于任何写入cr3的操作都会刷新TLB,因此这个宏定义可以刷新TLB
131  */
132 #define flush_tlb()                                  \
133     do                                               \
134     {                                                \
135         ul tmp;                                      \
136         io_mfence();                                 \
137         __asm__ __volatile__("movq %%cr3, %0\n\t"    \
138                              "movq %0, %%cr3\n\t"    \
139                              : "=r"(tmp)::"memory"); \
140                                                      \
141     } while (0);
142 
143 
144 // 导出内核程序的几个段的起止地址
145 extern char _text;
146 extern char _etext;
147 extern char _data;
148 extern char _edata;
149 extern char _rodata;
150 extern char _erodata;
151 extern char _bss;
152 extern char _ebss;
153 extern char _end;
154 
155 /**
156  * @brief 读取CR3寄存器的值(存储了页目录的基地址)
157  *
158  * @return unsigned*  cr3的值的指针
159  */
160 unsigned long *get_CR3()
161 {
162     ul *tmp;
163     __asm__ __volatile__("movq %%cr3, %0\n\t"
164                          : "=r"(tmp)::"memory");
165     return tmp;
166 }
167 
168 /*
169  *  vm_area_struct中的vm_flags的可选值
170  * 对应的结构体请见mm-types.h
171  */
172 #define VM_NONE 0
173 #define VM_READ (1 << 0)
174 #define VM_WRITE (1 << 1)
175 #define VM_EXEC (1 << 2)
176 #define VM_SHARED (1 << 3)
177 #define VM_IO (1 << 4) // MMIO的内存区域
178 #define VM_SOFTDIRTY (1 << 5)
179 #define VM_MAYSHARE (1 << 6) // 该vma可被共享
180 #define VM_USER (1 << 7)     // 该vma可被用户态访问
181 #define VM_DONTCOPY (1 << 8) // 当fork的时候不拷贝该虚拟内存区域
182 
183 /* VMA basic access permission flags */
184 #define VM_ACCESS_FLAGS (VM_READ | VM_WRITE | VM_EXEC)
185