xref: /xv6-public/vm.c (revision fbb4c094)
1 #include "param.h"
2 #include "types.h"
3 #include "defs.h"
4 #include "x86.h"
5 #include "memlayout.h"
6 #include "mmu.h"
7 #include "proc.h"
8 #include "elf.h"
9 
10 extern char data[];  // defined by kernel.ld
11 pde_t *kpgdir;  // for use in scheduler()
12 
13 // Set up CPU's kernel segment descriptors.
14 // Run once on entry on each CPU.
15 void
16 seginit(void)
17 {
18   struct cpu *c;
19 
20   // Map "logical" addresses to virtual addresses using identity map.
21   // Cannot share a CODE descriptor for both kernel and user
22   // because it would have to have DPL_USR, but the CPU forbids
23   // an interrupt from CPL=0 to DPL=3.
24   c = &cpus[lapiccpunum()];
25   c->gdt[SEG_KCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, 0);
26   c->gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, 0);
27   c->gdt[SEG_UCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, DPL_USER);
28   c->gdt[SEG_UDATA] = SEG(STA_W, 0, 0xffffffff, DPL_USER);
29   c->cpu = c;
30   c->proc = 0;
31   // Map cpu and proc -- these are private per cpu.
32   c->gdt[SEG_KCPU] = SEG(STA_W, &c->cpu, 4, 0);
33   lgdt(c->gdt, sizeof(c->gdt));
34   loadgs(SEG_KCPU << 3);
35 }
36 
37 // Return the address of the PTE in page table pgdir
38 // that corresponds to virtual address va.  If alloc!=0,
39 // create any required page table pages.
40 static pte_t *
41 walkpgdir(pde_t *pgdir, const void *va, int alloc)
42 {
43   pde_t *pde;
44   pte_t *pgtab;
45 
46   pde = &pgdir[PDX(va)];
47   if(*pde & PTE_P){
48     pgtab = (pte_t*)P2V(PTE_ADDR(*pde));
49   } else {
50     if(!alloc || (pgtab = (pte_t*)kalloc()) == 0)
51       return 0;
52     // Make sure all those PTE_P bits are zero.
53     memset(pgtab, 0, PGSIZE);
54     // The permissions here are overly generous, but they can
55     // be further restricted by the permissions in the page table
56     // entries, if necessary.
57     *pde = V2P(pgtab) | PTE_P | PTE_W | PTE_U;
58   }
59   return &pgtab[PTX(va)];
60 }
61 
62 // Create PTEs for virtual addresses starting at va that refer to
63 // physical addresses starting at pa. va and size might not
64 // be page-aligned.
65 static int
66 mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm)
67 {
68   char *a, *last;
69   pte_t *pte;
70 
71   a = (char*)PGROUNDDOWN((uint)va);
72   last = (char*)PGROUNDDOWN(((uint)va) + size - 1);
73   for(;;){
74     if((pte = walkpgdir(pgdir, a, 1)) == 0)
75       return -1;
76     if(*pte & PTE_P)
77       panic("remap");
78     *pte = pa | perm | PTE_P;
79     if(a == last)
80       break;
81     a += PGSIZE;
82     pa += PGSIZE;
83   }
84   return 0;
85 }
86 
87 // There is one page table per process, plus one that's used when
88 // a CPU is not running any process (kpgdir). The kernel uses the
89 // current process's page table during system calls and interrupts;
90 // page protection bits prevent user code from using the kernel's
91 // mappings.
92 //
93 // setupkvm() and exec() set up every page table like this:
94 //
95 //   0..KERNBASE: user memory (text+data+stack+heap), mapped to
96 //                phys memory allocated by the kernel
97 //   KERNBASE..KERNBASE+EXTMEM: mapped to 0..EXTMEM (for I/O space)
98 //   KERNBASE+EXTMEM..data: mapped to EXTMEM..V2P(data)
99 //                for the kernel's instructions and r/o data
100 //   data..KERNBASE+PHYSTOP: mapped to V2P(data)..PHYSTOP,
101 //                                  rw data + free physical memory
102 //   0xfe000000..0: mapped direct (devices such as ioapic)
103 //
104 // The kernel allocates physical memory for its heap and for user memory
105 // between V2P(end) and the end of physical memory (PHYSTOP)
106 // (directly addressable from end..P2V(PHYSTOP)).
107 
108 // This table defines the kernel's mappings, which are present in
109 // every process's page table.
110 static struct kmap {
111   void *virt;
112   uint phys_start;
113   uint phys_end;
114   int perm;
115 } kmap[] = {
116  { (void*)KERNBASE, 0,             EXTMEM,    PTE_W}, // I/O space
117  { (void*)KERNLINK, V2P(KERNLINK), V2P(data), 0},     // kern text+rodata
118  { (void*)data,     V2P(data),     PHYSTOP,   PTE_W}, // kern data+memory
119  { (void*)DEVSPACE, DEVSPACE,      0,         PTE_W}, // more devices
120 };
121 
122 // Set up kernel part of a page table.
123 pde_t*
124 setupkvm(void)
125 {
126   pde_t *pgdir;
127   struct kmap *k;
128 
129   if((pgdir = (pde_t*)kalloc()) == 0)
130     return 0;
131   memset(pgdir, 0, PGSIZE);
132   if (P2V(PHYSTOP) > (void*)DEVSPACE)
133     panic("PHYSTOP too high");
134   for(k = kmap; k < &kmap[NELEM(kmap)]; k++)
135     if(mappages(pgdir, k->virt, k->phys_end - k->phys_start,
136                 (uint)k->phys_start, k->perm) < 0)
137       return 0;
138   return pgdir;
139 }
140 
141 // Allocate one page table for the machine for the kernel address
142 // space for scheduler processes.
143 void
144 kvmalloc(void)
145 {
146   kpgdir = setupkvm();
147   switchkvm();
148 }
149 
150 // Switch h/w page table register to the kernel-only page table,
151 // for when no process is running.
152 void
153 switchkvm(void)
154 {
155   lcr3(V2P(kpgdir));   // switch to the kernel page table
156 }
157 
158 // Switch TSS and h/w page table to correspond to process p.
159 void
160 switchuvm(struct proc *p)
161 {
162   if(p == 0)
163     panic("switchuvm: no process");
164   if(p->kstack == 0)
165     panic("switchuvm: no kstack");
166   if(p->pgdir == 0)
167     panic("switchuvm: no pgdir");
168 
169   pushcli();
170   mycpu()->gdt[SEG_TSS] = SEG16(STS_T32A, &mycpu()->ts, sizeof(mycpu()->ts)-1, 0);
171   mycpu()->gdt[SEG_TSS].s = 0;
172   mycpu()->ts.ss0 = SEG_KDATA << 3;
173   mycpu()->ts.esp0 = (uint)p->kstack + KSTACKSIZE;
174   // setting IOPL=0 in eflags *and* iomb beyond the tss segment limit
175   // forbids I/O instructions (e.g., inb and outb) from user space
176   mycpu()->ts.iomb = (ushort) 0xFFFF;
177   ltr(SEG_TSS << 3);
178   lcr3(V2P(p->pgdir));  // switch to process's address space
179   popcli();
180 }
181 
182 // Load the initcode into address 0 of pgdir.
183 // sz must be less than a page.
184 void
185 inituvm(pde_t *pgdir, char *init, uint sz)
186 {
187   char *mem;
188 
189   if(sz >= PGSIZE)
190     panic("inituvm: more than a page");
191   mem = kalloc();
192   memset(mem, 0, PGSIZE);
193   mappages(pgdir, 0, PGSIZE, V2P(mem), PTE_W|PTE_U);
194   memmove(mem, init, sz);
195 }
196 
197 // Load a program segment into pgdir.  addr must be page-aligned
198 // and the pages from addr to addr+sz must already be mapped.
199 int
200 loaduvm(pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz)
201 {
202   uint i, pa, n;
203   pte_t *pte;
204 
205   if((uint) addr % PGSIZE != 0)
206     panic("loaduvm: addr must be page aligned");
207   for(i = 0; i < sz; i += PGSIZE){
208     if((pte = walkpgdir(pgdir, addr+i, 0)) == 0)
209       panic("loaduvm: address should exist");
210     pa = PTE_ADDR(*pte);
211     if(sz - i < PGSIZE)
212       n = sz - i;
213     else
214       n = PGSIZE;
215     if(readi(ip, P2V(pa), offset+i, n) != n)
216       return -1;
217   }
218   return 0;
219 }
220 
221 // Allocate page tables and physical memory to grow process from oldsz to
222 // newsz, which need not be page aligned.  Returns new size or 0 on error.
223 int
224 allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
225 {
226   char *mem;
227   uint a;
228 
229   if(newsz >= KERNBASE)
230     return 0;
231   if(newsz < oldsz)
232     return oldsz;
233 
234   a = PGROUNDUP(oldsz);
235   for(; a < newsz; a += PGSIZE){
236     mem = kalloc();
237     if(mem == 0){
238       cprintf("allocuvm out of memory\n");
239       deallocuvm(pgdir, newsz, oldsz);
240       return 0;
241     }
242     memset(mem, 0, PGSIZE);
243     if(mappages(pgdir, (char*)a, PGSIZE, V2P(mem), PTE_W|PTE_U) < 0){
244       cprintf("allocuvm out of memory (2)\n");
245       deallocuvm(pgdir, newsz, oldsz);
246       kfree(mem);
247       return 0;
248     }
249   }
250   return newsz;
251 }
252 
253 // Deallocate user pages to bring the process size from oldsz to
254 // newsz.  oldsz and newsz need not be page-aligned, nor does newsz
255 // need to be less than oldsz.  oldsz can be larger than the actual
256 // process size.  Returns the new process size.
257 int
258 deallocuvm(pde_t *pgdir, uint oldsz, uint newsz)
259 {
260   pte_t *pte;
261   uint a, pa;
262 
263   if(newsz >= oldsz)
264     return oldsz;
265 
266   a = PGROUNDUP(newsz);
267   for(; a  < oldsz; a += PGSIZE){
268     pte = walkpgdir(pgdir, (char*)a, 0);
269     if(!pte)
270       a = PGADDR(PDX(a) + 1, 0, 0) - PGSIZE;
271     else if((*pte & PTE_P) != 0){
272       pa = PTE_ADDR(*pte);
273       if(pa == 0)
274         panic("kfree");
275       char *v = P2V(pa);
276       kfree(v);
277       *pte = 0;
278     }
279   }
280   return newsz;
281 }
282 
283 // Free a page table and all the physical memory pages
284 // in the user part.
285 void
286 freevm(pde_t *pgdir)
287 {
288   uint i;
289 
290   if(pgdir == 0)
291     panic("freevm: no pgdir");
292   deallocuvm(pgdir, KERNBASE, 0);
293   for(i = 0; i < NPDENTRIES; i++){
294     if(pgdir[i] & PTE_P){
295       char * v = P2V(PTE_ADDR(pgdir[i]));
296       kfree(v);
297     }
298   }
299   kfree((char*)pgdir);
300 }
301 
302 // Clear PTE_U on a page. Used to create an inaccessible
303 // page beneath the user stack.
304 void
305 clearpteu(pde_t *pgdir, char *uva)
306 {
307   pte_t *pte;
308 
309   pte = walkpgdir(pgdir, uva, 0);
310   if(pte == 0)
311     panic("clearpteu");
312   *pte &= ~PTE_U;
313 }
314 
315 // Given a parent process's page table, create a copy
316 // of it for a child.
317 pde_t*
318 copyuvm(pde_t *pgdir, uint sz)
319 {
320   pde_t *d;
321   pte_t *pte;
322   uint pa, i, flags;
323   char *mem;
324 
325   if((d = setupkvm()) == 0)
326     return 0;
327   for(i = 0; i < sz; i += PGSIZE){
328     if((pte = walkpgdir(pgdir, (void *) i, 0)) == 0)
329       panic("copyuvm: pte should exist");
330     if(!(*pte & PTE_P))
331       panic("copyuvm: page not present");
332     pa = PTE_ADDR(*pte);
333     flags = PTE_FLAGS(*pte);
334     if((mem = kalloc()) == 0)
335       goto bad;
336     memmove(mem, (char*)P2V(pa), PGSIZE);
337     if(mappages(d, (void*)i, PGSIZE, V2P(mem), flags) < 0)
338       goto bad;
339   }
340   return d;
341 
342 bad:
343   freevm(d);
344   return 0;
345 }
346 
347 //PAGEBREAK!
348 // Map user virtual address to kernel address.
349 char*
350 uva2ka(pde_t *pgdir, char *uva)
351 {
352   pte_t *pte;
353 
354   pte = walkpgdir(pgdir, uva, 0);
355   if((*pte & PTE_P) == 0)
356     return 0;
357   if((*pte & PTE_U) == 0)
358     return 0;
359   return (char*)P2V(PTE_ADDR(*pte));
360 }
361 
362 // Copy len bytes from p to user address va in page table pgdir.
363 // Most useful when pgdir is not the current page table.
364 // uva2ka ensures this only works for PTE_U pages.
365 int
366 copyout(pde_t *pgdir, uint va, void *p, uint len)
367 {
368   char *buf, *pa0;
369   uint n, va0;
370 
371   buf = (char*)p;
372   while(len > 0){
373     va0 = (uint)PGROUNDDOWN(va);
374     pa0 = uva2ka(pgdir, (char*)va0);
375     if(pa0 == 0)
376       return -1;
377     n = PGSIZE - (va - va0);
378     if(n > len)
379       n = len;
380     memmove(pa0 + (va - va0), buf, n);
381     len -= n;
382     buf += n;
383     va = va0 + PGSIZE;
384   }
385   return 0;
386 }
387 
388 //PAGEBREAK!
389 // Blank page.
390 //PAGEBREAK!
391 // Blank page.
392 //PAGEBREAK!
393 // Blank page.
394 
395