initial commit
[mit-jos.git] / inc / memlayout.h
blobd39c5874c31edbe4247a1234df13a74a7f9b6a18
1 #ifndef JOS_INC_MEMLAYOUT_H
2 #define JOS_INC_MEMLAYOUT_H
4 #ifndef __ASSEMBLER__
5 #include <inc/types.h>
6 #include <inc/queue.h>
7 #include <inc/mmu.h>
8 #endif /* not __ASSEMBLER__ */
11 * This file contains definitions for memory management in our OS,
12 * which are relevant to both the kernel and user-mode software.
15 // Global descriptor numbers
16 #define GD_KT 0x08 // kernel text
17 #define GD_KD 0x10 // kernel data
18 #define GD_UT 0x18 // user text
19 #define GD_UD 0x20 // user data
20 #define GD_TSS 0x28 // Task segment selector
23 * Virtual memory map: Permissions
24 * kernel/user
26 * 4 Gig --------> +------------------------------+
27 * | | RW/--
28 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 * : . :
30 * : . :
31 * : . :
32 * |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| RW/--
33 * | | RW/--
34 * | Remapped Physical Memory | RW/--
35 * | | RW/--
36 * KERNBASE -----> +------------------------------+ 0xf0000000
37 * | Cur. Page Table (Kern. RW) | RW/-- PTSIZE
38 * VPT,KSTACKTOP--> +------------------------------+ 0xefc00000 --+
39 * | Kernel Stack | RW/-- KSTKSIZE |
40 * | - - - - - - - - - - - - - - -| PTSIZE
41 * | Invalid Memory (*) | --/-- |
42 * ULIM ------> +------------------------------+ 0xef800000 --+
43 * | Cur. Page Table (User R-) | R-/R- PTSIZE
44 * UVPT ----> +------------------------------+ 0xef400000
45 * | RO PAGES | R-/R- PTSIZE
46 * UPAGES ----> +------------------------------+ 0xef000000
47 * | RO ENVS | R-/R- PTSIZE
48 * UTOP,UENVS ------> +------------------------------+ 0xeec00000
49 * UXSTACKTOP -/ | User Exception Stack | RW/RW PGSIZE
50 * +------------------------------+ 0xeebff000
51 * | Empty Memory (*) | --/-- PGSIZE
52 * USTACKTOP ---> +------------------------------+ 0xeebfe000
53 * | Normal User Stack | RW/RW PGSIZE
54 * +------------------------------+ 0xeebfd000
55 * | |
56 * | |
57 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58 * . .
59 * . .
60 * . .
61 * |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
62 * | Program Data & Heap |
63 * UTEXT --------> +------------------------------+ 0x00800000
64 * PFTEMP -------> | Empty Memory (*) | PTSIZE
65 * | |
66 * UTEMP --------> +------------------------------+ 0x00400000 --+
67 * | Empty Memory (*) | |
68 * | - - - - - - - - - - - - - - -| |
69 * | User STAB Data (optional) | PTSIZE
70 * USTABDATA ----> +------------------------------+ 0x00200000 |
71 * | Empty Memory (*) | |
72 * 0 ------------> +------------------------------+ --+
74 * (*) Note: The kernel ensures that "Invalid Memory" (ULIM) is *never*
75 * mapped. "Empty Memory" is normally unmapped, but user programs may
76 * map pages there if desired. JOS user programs map pages temporarily
77 * at UTEMP.
81 // All physical memory mapped at this address
82 #define KERNBASE 0xF0000000
84 // At IOPHYSMEM (640K) there is a 384K hole for I/O. From the kernel,
85 // IOPHYSMEM can be addressed at KERNBASE + IOPHYSMEM. The hole ends
86 // at physical address EXTPHYSMEM.
87 #define IOPHYSMEM 0x0A0000
88 #define EXTPHYSMEM 0x100000
90 // Virtual page table. Entry PDX[VPT] in the PD contains a pointer to
91 // the page directory itself, thereby turning the PD into a page table,
92 // which maps all the PTEs containing the page mappings for the entire
93 // virtual address space into that 4 Meg region starting at VPT.
94 #define VPT (KERNBASE - PTSIZE)
95 #define KSTACKTOP VPT
96 #define KSTKSIZE (8*PGSIZE) // size of a kernel stack
97 #define ULIM (KSTACKTOP - PTSIZE)
100 * User read-only mappings! Anything below here til UTOP are readonly to user.
101 * They are global pages mapped in at env allocation time.
104 // Same as VPT but read-only for users
105 #define UVPT (ULIM - PTSIZE)
106 // Read-only copies of the Page structures
107 #define UPAGES (UVPT - PTSIZE)
108 // Read-only copies of the global env structures
109 #define UENVS (UPAGES - PTSIZE)
112 * Top of user VM. User can manipulate VA from UTOP-1 and down!
115 // Top of user-accessible VM
116 #define UTOP UENVS
117 // Top of one-page user exception stack
118 #define UXSTACKTOP UTOP
119 // Next page left invalid to guard against exception stack overflow; then:
120 // Top of normal user stack
121 #define USTACKTOP (UTOP - 2*PGSIZE)
123 // Where user programs generally begin
124 #define UTEXT (2*PTSIZE)
126 // Used for temporary page mappings. Typed 'void*' for convenience
127 #define UTEMP ((void*) PTSIZE)
128 // Used for temporary page mappings for the user page-fault handler
129 // (should not conflict with other temporary page mappings)
130 #define PFTEMP (UTEMP + PTSIZE - PGSIZE)
131 // The location of the user-level STABS data structure
132 #define USTABDATA (PTSIZE / 2)
135 #ifndef __ASSEMBLER__
138 * The page directory entry corresponding to the virtual address range
139 * [VPT, VPT + PTSIZE) points to the page directory itself. Thus, the page
140 * directory is treated as a page table as well as a page directory.
142 * One result of treating the page directory as a page table is that all PTEs
143 * can be accessed through a "virtual page table" at virtual address VPT (to
144 * which vpt is set in entry.S). The PTE for page number N is stored in
145 * vpt[N]. (It's worth drawing a diagram of this!)
147 * A second consequence is that the contents of the current page directory
148 * will always be available at virtual address (VPT + (VPT >> PGSHIFT)), to
149 * which vpd is set in entry.S.
151 typedef uint32_t pte_t;
152 typedef uint32_t pde_t;
154 extern volatile pte_t vpt[]; // VA of "virtual page table"
155 extern volatile pde_t vpd[]; // VA of current page directory
159 * Page descriptor structures, mapped at UPAGES.
160 * Read/write to the kernel, read-only to user programs.
162 * Each Page describes one physical page.
163 * You can map a Page * to the corresponding physical address
164 * with page2pa() in kern/pmap.h.
166 LIST_HEAD(Page_list, Page);
167 typedef LIST_ENTRY(Page) Page_LIST_entry_t;
169 struct Page {
170 Page_LIST_entry_t pp_link; /* free list link */
172 // pp_ref is the count of pointers (usually in page table entries)
173 // to this page, for pages allocated using page_alloc.
174 // Pages allocated at boot time using pmap.c's
175 // boot_alloc do not have valid reference count fields.
177 uint16_t pp_ref;
180 #endif /* !__ASSEMBLER__ */
181 #endif /* !JOS_INC_MEMLAYOUT_H */