* same with xv6
[mascara-docs.git] / i386 / ucla / src / lab4 / inc / memlayout.h
blob66cfabe6d27a94eede3f666e1d64b12bc6a17c37
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 * KSTACKTOP | Kernel Stack | RW/-- KSTKSIZE |
38 * | - - - - - - - - - - - - - - -| PTSIZE
39 * | Invalid Memory (*) | --/-- |
40 * ULIM ------> +------------------------------+ 0xefc00000 --+
41 * | Cur. Page Table (User R-) | R-/R- PTSIZE
42 * UVPT ----> +------------------------------+ 0xef800000
43 * | RO PAGES | R-/R- PTSIZE
44 * UPAGES ----> +------------------------------+ 0xef400000
45 * | RO ENVS | R-/R- PTSIZE
46 * UTOP,UENVS ------> +------------------------------+ 0xef000000
47 * UXSTACKTOP -/ | User Exception Stack | RW/RW PGSIZE
48 * +------------------------------+ 0xeefff000
49 * | Empty Memory (*) | --/-- PGSIZE
50 * USTACKTOP ---> +------------------------------+ 0xeeffe000
51 * | Normal User Stack | RW/RW PGSIZE
52 * +------------------------------+ 0xeeffd000
53 * | |
54 * | |
55 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56 * . .
57 * . .
58 * . .
59 * |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
60 * | Program Data & Heap |
61 * UTEXT --------> +------------------------------+ 0x00800000
62 * PFTEMP -------> | Empty Memory (*) | PTSIZE
63 * | |
64 * UTEMP --------> +------------------------------+ 0x00400000 --+
65 * | Empty Memory (*) | |
66 * | - - - - - - - - - - - - - - -| |
67 * | User STAB Data (optional) | PTSIZE
68 * USTABDATA ----> +------------------------------+ 0x00200000 |
69 * | Empty Memory (*) | |
70 * 0 ------------> +------------------------------+ --+
72 * (*) Note: The kernel ensures that "Invalid Memory" (ULIM) is *never*
73 * mapped. "Empty Memory" is normally unmapped, but user programs may
74 * map pages there if desired. JOS user programs map pages temporarily
75 * at UTEMP.
79 // All physical memory mapped at this address
80 #define KERNBASE 0xF0000000
82 // At IOPHYSMEM (640K) there is a 384K hole for I/O. From the kernel,
83 // IOPHYSMEM can be addressed at KERNBASE + IOPHYSMEM. The hole ends
84 // at physical address EXTPHYSMEM.
85 #define IOPHYSMEM 0x0A0000
86 #define EXTPHYSMEM 0x100000
88 // Kernel stack.
89 #define KSTACKTOP KERNBASE
90 #define KSTKSIZE (8*PGSIZE) // size of a kernel stack
91 #define ULIM (KSTACKTOP - PTSIZE)
94 * User read-only mappings! Anything below here til UTOP are readonly to user.
95 * They are global pages mapped in at env allocation time.
98 // User read-only virtual page table.
99 // Entry PDX[UVPT] in the PD contains a pointer to the page directory itself,
100 // thereby turning the PD into a page table, which maps all the PTEs
101 // containing the page mappings for the entire virtual address space into that
102 // 4 Meg region starting at UVPT.
103 #define UVPT (ULIM - PTSIZE)
104 // Read-only copies of the Page structures
105 #define UPAGES (UVPT - PTSIZE)
106 // Read-only copies of the global env structures
107 #define UENVS (UPAGES - PTSIZE)
110 * Top of user VM. User can manipulate VA from UTOP-1 and down!
113 // Top of user-accessible VM
114 #define UTOP UENVS
115 // Top of one-page user exception stack
116 #define UXSTACKTOP UTOP
117 // Next page left invalid to guard against exception stack overflow; then:
118 // Top of normal user stack
119 #define USTACKTOP (UTOP - 2*PGSIZE)
121 // Where user programs generally begin
122 #define UTEXT (2*PTSIZE)
124 // Used for temporary page mappings. Typed 'void*' for convenience
125 #define UTEMP ((void*) PTSIZE)
126 // Used for temporary page mappings for the user page-fault handler
127 // (should not conflict with other temporary page mappings)
128 #define PFTEMP ((void*) ((uintptr_t) UTEMP + PTSIZE - PGSIZE))
129 // The location of the user-level STABS data structure
130 #define USTABDATA (PTSIZE / 2)
133 #ifndef __ASSEMBLER__
135 typedef uint32_t pte_t;
136 typedef uint32_t pde_t;
138 #if JOS_USER
140 * The page directory entry corresponding to the virtual address range
141 * [VPT, VPT + PTSIZE) points to the page directory itself. Thus, the page
142 * directory is treated as a page table as well as a page directory.
144 * One result of treating the page directory as a page table is that all PTEs
145 * can be accessed through a "virtual page table" at virtual address VPT (to
146 * which vpt is set in entry.S). The PTE for page number N is stored in
147 * vpt[N]. (It's worth drawing a diagram of this!)
149 * A second consequence is that the contents of the current page directory
150 * will always be available at virtual address (VPT + (VPT >> PGSHIFT)), to
151 * which vpd is set in entry.S.
153 extern volatile pte_t vpt[]; // VA of "virtual page table"
154 extern volatile pde_t vpd[]; // VA of current page directory
155 #endif
157 #endif /* !__ASSEMBLER__ */
158 #endif /* !JOS_INC_MEMLAYOUT_H */