Don't pass "-nics 0" to qemu, because this option name has changed
[pintos-anon] / src / threads / mmu.h
index 4d4aa6ac3ced70add98f368abdf0ce6a6c26e1b3..9f71d4a3a9d2b970264371eceb310da098a25d6c 100644 (file)
@@ -26,7 +26,7 @@
      the final physical address.
 
 
-   32                    22                     12                      0
+    31                  22 21                  12 11                   0
    +--------------------------------------------------------------------+
    | Page Directory Index |   Page Table Index   |    Page Offset       |
    +--------------------------------------------------------------------+
 
 #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 (const void *va) {
@@ -105,6 +106,20 @@ static inline void *pg_round_down (const void *va) {
    virtual address space belongs to the kernel. */
 #define        PHYS_BASE ((void *) LOADER_PHYS_BASE)
 
+/* Returns true if VADDR is a user virtual address. */
+static inline bool
+is_user_vaddr (const void *vaddr) 
+{
+  return vaddr < PHYS_BASE;
+}
+
+/* Returns true if VADDR is a kernel virtual address. */
+static inline bool
+is_kernel_vaddr (const void *vaddr) 
+{
+  return vaddr >= PHYS_BASE;
+}
+
 /* Returns kernel virtual address at which physical address PADDR
    is mapped. */
 static inline void *
@@ -120,18 +135,19 @@ ptov (uintptr_t paddr)
 static inline uintptr_t
 vtop (const void *vaddr)
 {
-  ASSERT (vaddr >= PHYS_BASE);
+  ASSERT (is_kernel_vaddr (vaddr));
 
   return (uintptr_t) vaddr - (uintptr_t) PHYS_BASE;
 }
 \f
 /* Page directories and page tables.
 
-   For more information see [IA32-v3] pages 3-23 to 3-28.
+   For more information see [IA32-v3a] 3.7.6 "Page-Directory and
+   Page-Table Entries".
 
    PDEs and PTEs share a common format:
 
-   32                                   12                       0
+   31                                 12 11                     0
    +------------------------------------+------------------------+
    |         Physical Address           |         Flags          |
    +------------------------------------+------------------------+
@@ -168,7 +184,7 @@ static inline uint32_t *pde_get_pt (uint32_t pde) {
 }
 
 /* Obtains page table index from a virtual address. */
-static inline unsigned pt_no (void *va) {
+static inline unsigned pt_no (const void *va) {
   return ((uintptr_t) va & PTMASK) >> PTSHIFT;
 }
 
@@ -189,10 +205,9 @@ static inline uint32_t pte_create_user (uint32_t *page, bool writable) {
   return pte_create_kernel (page, writable) | PG_U;
 }
 
-/* Returns a pointer to the page that page table entry PTE, which
-   must "present", points to. */
+/* Returns a pointer to the page that page table entry PTE points
+   to. */
 static inline void *pte_get_page (uint32_t pte) {
-  ASSERT (pte & PG_P);
   return ptov (pte & ~PGMASK);
 }