#ifndef _PAGE_H
#define _PAGE_H
+/*
+
+| CR0的PG | CR4的PAE | CR4的PSE | PDE的PS | 页面规模 | 物理地址规模 |
+| ------- | -------- | -------- | ------- | -------- | ------------ |
+| 0 | × | × | × | ― | 禁止分页 |
+| 1 | 0 | 0 | × | 4KB | 32位 |
+| 1 | 0 | 1 | 0 | 4KB | 32位 |
+| 1 | 0 | 1 | 1 | 4MB | 32位 |
+| 1 | 1 | × | 0 | 4KB | 36位 |
+| 1 | 1 | × | 1 | 2MB | 36位 |
+
+*/
+
#define PAGE_P 0x1 // 在内存中
#define PAGE_WR 0x2 // 表示可读写
#define PAGE_US 0x4 // 用户级
#ifndef ASM
#include <bits.h>
#include <types.h>
-#define get_npd(addr) (((u32)(addr)) >> 22)
-#define get_npt(addr) ((((u32)(addr)) >> 12) & 0x3FF)
+#define get_npde(addr) (((u32)(addr)) >> 22)
+#define get_npte(addr) ((((u32)(addr)) >> 12) & 0x3FF)
#include <list.h>
void set_page_shared(void *x) {
unsigned long addr = (unsigned long)x;
addr = PAGE_ALIGN(addr);
- unsigned int npd = get_npd(addr);
+ unsigned int npd = get_npde(addr);
init_pgd[npd] |= PAGE_US;
pte_t *pte = pa2va(init_pgd[npd] & PAGE_MASK);
- pte[get_npt(addr)] |= PAGE_US;
+ pte[get_npte(addr)] |= PAGE_US;
}
void init_paging() {
memset((void *)pgtb_addr, 0, PAGE_SIZE);
- init_pgd[get_npd(page_addr)] = (pde_t)((unsigned long)va2pa(pgtb_addr) | PAGE_P | PAGE_WR);
+ init_pgd[get_npde(page_addr)] = (pde_t)((unsigned long)va2pa(pgtb_addr) | PAGE_P | PAGE_WR);
}
pte = ((pte_t *)pgtb_addr) + ti;
}
// paging for kernel space
- unsigned long delta = get_npd(PAGE_OFFSET);
+ unsigned long delta = get_npde(PAGE_OFFSET);
for (i = delta; i < PDECNT_PER_PAGE; ++i) {
init_pgd[i] = init_pgd[i - delta];
init_pgd[i - delta] = 0;
// 接下来为显存建立页映射
unsigned long vram_phys_addr = system.vbe_phys_addr;
- for (int pde_inx = 0; pde_inx < get_npd(VRAM_VADDR_SIZE); pde_inx++) {
+ for (int pde_inx = 0; pde_inx < get_npde(VRAM_VADDR_SIZE); pde_inx++) {
pgtb_addr = (unsigned long *)(alloc_from_bootmem(PAGE_SIZE, "vrampaging"));
if (0 == pgtb_addr) {
panic("no pages for paging...");
}
// 后续要初始化,所以此处不用memset
// memset((void *)pgtb_addr, 0, PAGE_SIZE);
- init_pgd[get_npd(VRAM_VADDR_BASE) + pde_inx] = (pde_t)((unsigned long)va2pa(pgtb_addr) | PAGE_P | PAGE_WR);
+ init_pgd[get_npde(VRAM_VADDR_BASE) + pde_inx] = (pde_t)((unsigned long)va2pa(pgtb_addr) | PAGE_P | PAGE_WR);
for (int pte_inx = 0; pte_inx < PTECNT_PER_PAGE; pte_inx++) {
pgtb_addr[pte_inx] = vram_phys_addr | PAGE_P | PAGE_WR;
unsigned long page = alloc_one_page(0);
assert(page != 0);
- int npde = get_npd(addr);
- int npte = get_npt(addr);
+ int npde = get_npde(addr);
+ int npte = get_npte(addr);
if (page_dir[npde] == 0) {
page_tbl = (pte_t *)alloc_one_page(0);
panic("%s invalid addr", __func__);
}
- int npde = get_npd(addr);
- int npte = get_npt(addr);
+ int npde = get_npde(addr);
+ int npte = get_npte(addr);
pde_t *page_dir = (pde_t *)pa2va(current->cr3);
pte_t *page_tbl = pa2va(PAGE_ALIGN(page_dir[npde]));