X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fthreads%2Fmmu.h;h=a1f05ae7b489a5f61f0f6f2e5f36b9bd320f5afa;hb=76b07342aab9c426a0244e0b6b75ba50659a5cc9;hp=304053bfdaba9ccd5721e19f929d9380e9358beb;hpb=b0a700d18f0a0a8c87e1a4fff3a2108e0edb0fbc;p=pintos-anon diff --git a/src/threads/mmu.h b/src/threads/mmu.h index 304053b..a1f05ae 100644 --- a/src/threads/mmu.h +++ b/src/threads/mmu.h @@ -9,19 +9,19 @@ /* Virtual to physical translation works like this on an x86: - - The top 10 bits of the virtual address (bits 22:31) are used + - The top 10 bits of the virtual address (bits 22:32) are used to index into the page directory. If the PDE is marked "present," the physical address of a page table is read from the PDE thus obtained. If the PDE is marked "not present" then a page fault occurs. - - The next 10 bits of the virtual address (bits 12:21) are + - The next 10 bits of the virtual address (bits 12:22) are used to index into the page table. If the PTE is marked "present," the physical address of a data page is read from the PTE thus obtained. If the PTE is marked "not present" then a page fault occurs. - - The bottom 12 bits of the virtual address (bits 0:11) are + - The bottom 12 bits of the virtual address (bits 0:12) are added to the data page's physical base address, producing the final physical address. @@ -57,38 +57,53 @@ #define MASK(SHIFT, CNT) (((1ul << (CNT)) - 1) << (SHIFT)) -/* Page offset (bits 0:11). */ -#define PGSHIFT 0 /* First offset bit. */ -#define PGBITS 12 /* Number of offset bits. */ -#define PGMASK MASK(PGSHIFT, PGBITS) -#define PGSIZE (1 << PGBITS) +/* Page offset (bits 0:12). */ +#define PGSHIFT 0 /* Index of first offset bit. */ +#define PGBITS 12 /* Number of offset bits. */ +#define PGMASK MASK(PGSHIFT, PGBITS) /* Page offset bits (0:12). */ +#define PGSIZE (1 << PGBITS) /* Bytes in a page. */ -/* Page table (bits 12:21). */ -#define PTSHIFT PGBITS /* First page table bit. */ -#define PTBITS 10 /* Number of page table bits. */ -#define PTMASK MASK(PTSHIFT, PTBITS) +/* Page table (bits 12:22). */ +#define PTSHIFT PGBITS /* Index of first page table bit. */ +#define PTBITS 10 /* Number of page table bits. */ +#define PTMASK MASK(PTSHIFT, PTBITS) /* Page table bits (12:22). */ +#define PTSPAN (1 << PTBITS << PGBITS) /* Bytes covered by a page table. */ -/* Page directory (bits 22:31). */ -#define PDSHIFT (PTSHIFT + PTBITS) /* First page dir bit. */ -#define PDBITS 10 /* Number of page dir bits. */ -#define PDMASK MASK(PDSHIFT, PDBITS) +/* Page directory (bits 22:32). */ +#define PDSHIFT (PTSHIFT + PTBITS) /* First page dir bit. */ +#define PDBITS 10 /* Number of page dir bits. */ +#define PDMASK MASK(PDSHIFT, PDBITS) /* Page directory bits (22:32). */ /* Offset within a page. */ -static inline unsigned pg_ofs (void *va) { return (uintptr_t) va & PGMASK; } +static inline unsigned pg_ofs (const void *va) { + return (uintptr_t) va & PGMASK; +} /* Virtual page number. */ -static inline uintptr_t pg_no (void *va) { return (uintptr_t) va >> PTSHIFT; } +static inline uintptr_t pg_no (const void *va) { + return (uintptr_t) va >> PTSHIFT; +} /* Round up to nearest page boundary. */ -static inline void *pg_round_up (void *va) { +static inline void *pg_round_up (const void *va) { return (void *) (((uintptr_t) va + PGSIZE - 1) & ~PGMASK); } /* Round down to nearest page boundary. */ -static inline void *pg_round_down (void *va) { +static inline void *pg_round_down (const void *va) { return (void *) ((uintptr_t) va & ~PGMASK); } +/* Base address of the 1:1 physical-to-virtual mapping. Physical + memory is mapped starting at this virtual address. Thus, + physical address 0 is accessible at PHYS_BASE, physical + address address 0x1234 at (uint8_t *) PHYS_BASE + 0x1234, and + so on. + + This address also marks the end of user programs' address + space. Up to this point in memory, user programs are allowed + to map whatever they like. At this point and above, the + virtual address space belongs to the kernel. */ #define PHYS_BASE ((void *) LOADER_PHYS_BASE) /* Returns kernel virtual address at which physical address PADDR @@ -104,7 +119,7 @@ ptov (uintptr_t paddr) /* Returns physical address at which kernel virtual address VADDR is mapped. */ static inline uintptr_t -vtop (void *vaddr) +vtop (const void *vaddr) { ASSERT (vaddr >= PHYS_BASE); @@ -136,7 +151,9 @@ vtop (void *vaddr) #define PG_D 0x40 /* 1=dirty, 0=not dirty (PTEs only). */ /* Obtains page directory index from a virtual address. */ -static inline uintptr_t pd_no (void *va) { return (uintptr_t) va >> PDSHIFT; } +static inline uintptr_t pd_no (const void *va) { + return (uintptr_t) va >> PDSHIFT; +} /* Returns a PDE that points to page table PT. */ static inline uint32_t pde_create (uint32_t *pt) { @@ -174,7 +191,7 @@ static inline uint32_t pte_create_user (uint32_t *page, bool writable) { } /* Returns a pointer to the page that page table entry PTE, which - must "present", points to. */ + must be "present", points to. */ static inline void *pte_get_page (uint32_t pte) { ASSERT (pte & PG_P); return ptov (pte & ~PGMASK);