xref: /DragonOS/kernel/src/mm/mm.h (revision fae6e9ade46a52976ad5d099643d51cc20876448)
1 #pragma once
2 
3 #include <common/glib.h>
4 #include <process/process.h>
5 
6 #define PAGE_4K_SHIFT 12
7 #define PAGE_2M_SHIFT 21
8 #define PAGE_1G_SHIFT 30
9 #define PAGE_GDT_SHIFT 39
10 
11 // 不同大小的页的容量
12 #define PAGE_4K_SIZE (1UL << PAGE_4K_SHIFT)
13 #define PAGE_2M_SIZE (1UL << PAGE_2M_SHIFT)
14 #define PAGE_1G_SIZE (1UL << PAGE_1G_SHIFT)
15 
16 // 屏蔽低于x的数值
17 #define PAGE_4K_MASK (~(PAGE_4K_SIZE - 1))
18 #define PAGE_2M_MASK (~(PAGE_2M_SIZE - 1))
19 
20 // 将addr按照x的上边界对齐
21 #define PAGE_4K_ALIGN(addr)                                                    \
22   (((unsigned long)(addr) + PAGE_4K_SIZE - 1) & PAGE_4K_MASK)
23 #define PAGE_2M_ALIGN(addr)                                                    \
24   (((unsigned long)(addr) + PAGE_2M_SIZE - 1) & PAGE_2M_MASK)
25 
26 // 虚拟地址与物理地址转换
27 #define virt_2_phys(addr) ((unsigned long)(addr)-PAGE_OFFSET)
28 #define phys_2_virt(addr)                                                      \
29   ((unsigned long *)((unsigned long)(addr) + PAGE_OFFSET))
30 
31 // ===== 页面属性 =====
32 // 页面在页表中已被映射 mapped=1 unmapped=0
33 #define PAGE_PGT_MAPPED (1 << 0)
34 
35 // 内核初始化所占用的页 init-code=1 normal-code/data=0
36 #define PAGE_KERNEL_INIT (1 << 1)
37 
38 // 1=设备MMIO映射的内存 0=物理内存
39 #define PAGE_DEVICE (1 << 2)
40 
41 // 内核层页 kernel=1 memory=0
42 #define PAGE_KERNEL (1 << 3)
43 
44 // 共享的页 shared=1 single-use=0
45 #define PAGE_SHARED (1 << 4)
46 
47 // =========== 页表项权限 ========
48 
49 //	bit 63	Execution Disable:
50 #define PAGE_XD (1UL << 63)
51 
52 //	bit 12	Page Attribute Table
53 #define PAGE_PAT (1UL << 12)
54 // 对于PTE而言,第7位是PAT
55 #define PAGE_4K_PAT (1UL << 7)
56 
57 //	bit 8	Global Page:1,global;0,part
58 #define PAGE_GLOBAL (1UL << 8)
59 
60 //	bit 7	Page Size:1,big page;0,small page;
61 #define PAGE_PS (1UL << 7)
62 
63 //	bit 6	Dirty:1,dirty;0,clean;
64 #define PAGE_DIRTY (1UL << 6)
65 
66 //	bit 5	Accessed:1,visited;0,unvisited;
67 #define PAGE_ACCESSED (1UL << 5)
68 
69 //	bit 4	Page Level Cache Disable
70 #define PAGE_PCD (1UL << 4)
71 
72 //	bit 3	Page Level Write Through
73 #define PAGE_PWT (1UL << 3)
74 
75 //	bit 2	User Supervisor:1,user and supervisor;0,supervisor;
76 #define PAGE_U_S (1UL << 2)
77 
78 //	bit 1	Read Write:1,read and write;0,read;
79 #define PAGE_R_W (1UL << 1)
80 
81 //	bit 0	Present:1,present;0,no present;
82 #define PAGE_PRESENT (1UL << 0)
83 
84 // 1,0
85 #define PAGE_KERNEL_PGT (PAGE_R_W | PAGE_PRESENT)
86 
87 // 1,0
88 #define PAGE_KERNEL_DIR (PAGE_R_W | PAGE_PRESENT)
89 
90 // 1,0 (4级页表在3级页表中的页表项的属性)
91 #define PAGE_KERNEL_PDE (PAGE_R_W | PAGE_PRESENT)
92 
93 // 7,1,0
94 #define PAGE_KERNEL_PAGE (PAGE_PS | PAGE_R_W | PAGE_PRESENT)
95 
96 #define PAGE_KERNEL_4K_PAGE (PAGE_R_W | PAGE_PRESENT)
97 
98 #define PAGE_USER_PGT (PAGE_U_S | PAGE_R_W | PAGE_PRESENT)
99 
100 // 2,1,0
101 #define PAGE_USER_DIR (PAGE_U_S | PAGE_R_W | PAGE_PRESENT)
102 
103 // 1,0 (4级页表在3级页表中的页表项的属性)
104 #define PAGE_USER_PDE (PAGE_U_S | PAGE_R_W | PAGE_PRESENT)
105 // 7,2,1,0
106 #define PAGE_USER_PAGE (PAGE_PS | PAGE_U_S | PAGE_R_W | PAGE_PRESENT)
107 
108 #define PAGE_USER_4K_PAGE (PAGE_U_S | PAGE_R_W | PAGE_PRESENT)
109 
110 // 导出内核程序的几个段的起止地址
111 extern char _text;
112 extern char _etext;
113 extern char _data;
114 extern char _edata;
115 extern char _rodata;
116 extern char _erodata;
117 extern char _bss;
118 extern char _ebss;
119 extern char _end;
120 
121 /*
122  *  vm_area_struct中的vm_flags的可选值
123  * 对应的结构体请见mm-types.h
124  */
125 #define VM_NONE 0
126 #define VM_READ (1 << 0)
127 #define VM_WRITE (1 << 1)
128 #define VM_EXEC (1 << 2)
129 #define VM_SHARED (1 << 3)
130 #define VM_IO (1 << 4) // MMIO的内存区域
131 #define VM_SOFTDIRTY (1 << 5)
132 #define VM_MAYSHARE (1 << 6) // 该vma可被共享
133 #define VM_USER (1 << 7)     // 该vma可被用户态访问
134 #define VM_DONTCOPY (1 << 8) // 当fork的时候不拷贝该虚拟内存区域
135 
136 /* VMA basic access permission flags */
137 #define VM_ACCESS_FLAGS (VM_READ | VM_WRITE | VM_EXEC)
138