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