· 9 years ago · Oct 11, 2016, 02:46 PM
1/* See COPYRIGHT for copyright information. */
2
3#include <inc/x86.h>
4#include <inc/mmu.h>
5#include <inc/error.h>
6#include <inc/string.h>
7#include <inc/assert.h>
8
9#include <kern/pmap.h>
10#include <kern/kclock.h>
11
12// These variables are set by i386_detect_memory()
13size_t npages; // Amount of physical memory (in pages)
14static size_t npages_basemem; // Amount of base memory (in pages)
15
16// These variables are set in mem_init()
17pde_t *kern_pgdir; // Kernel's initial page directory
18struct PageInfo *pages; // Physical page state array
19static struct PageInfo *page_free_list; // Free list of physical pages
20
21
22// --------------------------------------------------------------
23// Detect machine's physical memory setup.
24// --------------------------------------------------------------
25
26static int
27nvram_read(int r)
28{
29 return mc146818_read(r) | (mc146818_read(r + 1) << 8);
30}
31
32static void
33i386_detect_memory(void)
34{
35 size_t basemem, extmem, ext16mem, totalmem;
36
37 // Use CMOS calls to measure available base & extended memory.
38 // (CMOS calls return results in kilobytes.)
39 basemem = nvram_read(NVRAM_BASELO);
40 extmem = nvram_read(NVRAM_EXTLO);
41 ext16mem = nvram_read(NVRAM_EXT16LO) * 64;
42
43 // Calculate the number of physical pages available in both base
44 // and extended memory.
45 if (ext16mem)
46 totalmem = 16 * 1024 + ext16mem;
47 else if (extmem)
48 totalmem = 1 * 1024 + extmem;
49 else
50 totalmem = basemem;
51
52 npages = totalmem / (PGSIZE / 1024);
53 npages_basemem = basemem / (PGSIZE / 1024);
54
55 cprintf("Physical memory: %uK available, base = %uK, extended = %uK\n",
56 totalmem, basemem, totalmem - basemem);
57}
58
59
60// --------------------------------------------------------------
61// Set up memory mappings above UTOP.
62// --------------------------------------------------------------
63
64static void boot_map_region(pde_t *pgdir, uintptr_t va, size_t size, physaddr_t pa, int perm);
65static void check_page_free_list(bool only_low_memory);
66static void check_page_alloc(void);
67static void check_kern_pgdir(void);
68static physaddr_t check_va2pa(pde_t *pgdir, uintptr_t va);
69static void check_page(void);
70static void check_page_installed_pgdir(void);
71
72// This simple physical memory allocator is used only while JOS is setting
73// up its virtual memory system. page_alloc() is the real allocator.
74//
75// If n>0, allocates enough pages of contiguous physical memory to hold 'n'
76// bytes. Doesn't initialize the memory. Returns a kernel virtual address.
77//
78// If n==0, returns the address of the next free page without allocating
79// anything.
80//
81// If we're out of memory, boot_alloc should panic.
82// This function may ONLY be used during initialization,
83// before the page_free_list list has been set up.
84static void *
85boot_alloc(uint32_t n)
86{
87 static char *nextfree; // virtual address of next byte of free memory
88 char *result;
89
90 // Initialize nextfree if this is the first time.
91 // 'end' is a magic symbol automatically generated by the linker,
92 // which points to the end of the kernel's bss segment:
93 // the first virtual address that the linker did *not* assign
94 // to any kernel code or global variables.
95 if (!nextfree) {
96 extern char end[];
97 nextfree = ROUNDUP((char *) end, PGSIZE);
98 }
99
100 // Allocate a chunk large enough to hold 'n' bytes, then update
101 // nextfree. Make sure nextfree is kept aligned
102 // to a multiple of PGSIZE.
103
104 cprintf("boot_alloc memory at %x\n", nextfree);
105 cprintf("Next memory at %x\n", ROUNDUP((char *) (nextfree+n), PGSIZE));
106 char *next = nextfree;
107 nextfree = ROUNDUP((char *) (nextfree+n), PGSIZE);
108 return next;
109
110 //return NULL;
111}
112
113// Set up a two-level page table:
114// kern_pgdir is its linear (virtual) address of the root
115//
116// This function only sets up the kernel part of the address space
117// (ie. addresses >= UTOP). The user part of the address space
118// will be setup later.
119//
120// From UTOP to ULIM, the user is allowed to read but not write.
121// Above ULIM the user cannot read or write.
122void
123mem_init(void)
124{
125 uint32_t cr0;
126 size_t n;
127
128 // Find out how much memory the machine has (npages & npages_basemem).
129 i386_detect_memory();
130
131 // Remove this line when you're ready to test this function.
132 panic("mem_init: This function is not finished\n");
133
134 //////////////////////////////////////////////////////////////////////
135 // create initial page directory.
136 kern_pgdir = (pde_t *) boot_alloc(PGSIZE);
137 memset(kern_pgdir, 0, PGSIZE);
138
139 //////////////////////////////////////////////////////////////////////
140 // Recursively insert PD in itself as a page table, to form
141 // a virtual page table at virtual address UVPT.
142 // (For now, you don't have understand the greater purpose of the
143 // following line.)
144
145 // Permissions: kernel R, user R
146 kern_pgdir[PDX(UVPT)] = PADDR(kern_pgdir) | PTE_U | PTE_P;
147
148 //////////////////////////////////////////////////////////////////////
149 // Allocate an array of npages 'struct PageInfo's and store it in 'pages'.
150 // The kernel uses this array to keep track of physical pages: for
151 // each physical page, there is a corresponding struct PageInfo in this
152 // array. 'npages' is the number of physical pages in memory. Use memset
153 // to initialize all fields of each struct PageInfo to 0.
154 // Your code goes here:
155
156
157 //////////////////////////////////////////////////////////////////////
158 // Now that we've allocated the initial kernel data structures, we set
159 // up the list of free physical pages. Once we've done so, all further
160 // memory management will go through the page_* functions. In
161 // particular, we can now map memory using boot_map_region
162 // or page_insert
163 page_init();
164
165 check_page_free_list(1);
166 check_page_alloc();
167 check_page();
168
169 //////////////////////////////////////////////////////////////////////
170 // Now we set up virtual memory
171
172 //////////////////////////////////////////////////////////////////////
173 // Map 'pages' read-only by the user at linear address UPAGES
174 // Permissions:
175 // - the new image at UPAGES -- kernel R, user R
176 // (ie. perm = PTE_U | PTE_P)
177 // - pages itself -- kernel RW, user NONE
178 // Your code goes here:
179
180 //////////////////////////////////////////////////////////////////////
181 // Use the physical memory that 'bootstack' refers to as the kernel
182 // stack. The kernel stack grows down from virtual address KSTACKTOP.
183 // We consider the entire range from [KSTACKTOP-PTSIZE, KSTACKTOP)
184 // to be the kernel stack, but break this into two pieces:
185 // * [KSTACKTOP-KSTKSIZE, KSTACKTOP) -- backed by physical memory
186 // * [KSTACKTOP-PTSIZE, KSTACKTOP-KSTKSIZE) -- not backed; so if
187 // the kernel overflows its stack, it will fault rather than
188 // overwrite memory. Known as a "guard page".
189 // Permissions: kernel RW, user NONE
190 // Your code goes here:
191
192 //////////////////////////////////////////////////////////////////////
193 // Map all of physical memory at KERNBASE.
194 // Ie. the VA range [KERNBASE, 2^32) should map to
195 // the PA range [0, 2^32 - KERNBASE)
196 // We might not have 2^32 - KERNBASE bytes of physical memory, but
197 // we just set up the mapping anyway.
198 // Permissions: kernel RW, user NONE
199 // Your code goes here:
200
201 // Check that the initial page directory has been set up correctly.
202 check_kern_pgdir();
203
204 // Switch from the minimal entry page directory to the full kern_pgdir
205 // page table we just created. Our instruction pointer should be
206 // somewhere between KERNBASE and KERNBASE+4MB right now, which is
207 // mapped the same way by both page tables.
208 //
209 // If the machine reboots at this point, you've probably set up your
210 // kern_pgdir wrong.
211 lcr3(PADDR(kern_pgdir));
212
213 check_page_free_list(0);
214
215 // entry.S set the really important flags in cr0 (including enabling
216 // paging). Here we configure the rest of the flags that we care about.
217 cr0 = rcr0();
218 cr0 |= CR0_PE|CR0_PG|CR0_AM|CR0_WP|CR0_NE|CR0_MP;
219 cr0 &= ~(CR0_TS|CR0_EM);
220 lcr0(cr0);
221
222 // Some more checks, only possible after kern_pgdir is installed.
223 check_page_installed_pgdir();
224}
225
226// --------------------------------------------------------------
227// Tracking of physical pages.
228// The 'pages' array has one 'struct PageInfo' entry per physical page.
229// Pages are reference counted, and free pages are kept on a linked list.
230// --------------------------------------------------------------
231
232//
233// Initialize page structure and memory free list.
234// After this is done, NEVER use boot_alloc again. ONLY use the page
235// allocator functions below to allocate and deallocate physical
236// memory via the page_free_list.
237//
238void
239page_init(void)
240{
241 // The example code here marks all physical pages as free.
242 // However this is not truly the case. What memory is free?
243 // 1) Mark physical page 0 as in use.
244 // This way we preserve the real-mode IDT and BIOS structures
245 // in case we ever need them. (Currently we don't, but...)
246 // 2) The rest of base memory, [PGSIZE, npages_basemem * PGSIZE)
247 // is free.
248 // 3) Then comes the IO hole [IOPHYSMEM, EXTPHYSMEM), which must
249 // never be allocated.
250 // 4) Then extended memory [EXTPHYSMEM, ...).
251 // Some of it is in use, some is free. Where is the kernel
252 // in physical memory? Which pages are already in use for
253 // page tables and other data structures?
254 //
255 // Change the code to reflect this.
256 // NB: DO NOT actually touch the physical memory corresponding to
257 // free pages!
258 size_t i;
259 for (i = 0; i < npages; i++) {
260 pages[i].pp_ref = 0;
261 pages[i].pp_link = page_free_list;
262 page_free_list = &pages[i];
263 }
264}
265
266//
267// Allocates a physical page. If (alloc_flags & ALLOC_ZERO), fills the entire
268// returned physical page with '\0' bytes. Does NOT increment the reference
269// count of the page - the caller must do these if necessary (either explicitly
270// or via page_insert).
271//
272// Be sure to set the pp_link field of the allocated page to NULL so
273// page_free can check for double-free bugs.
274//
275// Returns NULL if out of free memory.
276//
277// Hint: use page2kva and memset
278struct PageInfo *
279page_alloc(int alloc_flags)
280{
281 // Fill this function in
282 return 0;
283}
284
285//
286// Return a page to the free list.
287// (This function should only be called when pp->pp_ref reaches 0.)
288//
289void
290page_free(struct PageInfo *pp)
291{
292 pp->pp_link = page_free_list;
293 page_free_list = pp;
294
295 // Fill this function in
296 // Hint: You may want to panic if pp->pp_ref is nonzero or
297 // pp->pp_link is not NULL.
298}
299
300//
301// Decrement the reference count on a page,
302// freeing it if there are no more refs.
303//
304void
305page_decref(struct PageInfo* pp)
306{
307 if (--pp->pp_ref == 0)
308 page_free(pp);
309}
310
311// Given 'pgdir', a pointer to a page directory, pgdir_walk returns
312// a pointer to the page table entry (PTE) for linear address 'va'.
313// This requires walking the two-level page table structure.
314//
315// The relevant page table page might not exist yet.
316// If this is true, and create == false, then pgdir_walk returns NULL.
317// Otherwise, pgdir_walk allocates a new page table page with page_alloc.
318// - If the allocation fails, pgdir_walk returns NULL.
319// - Otherwise, the new page's reference count is incremented,
320// the page is cleared,
321// and pgdir_walk returns a pointer into the new page table page.
322//
323// Hint 1: you can turn a PageInfo * into the physical address of the
324// page it refers to with page2pa() from kern/pmap.h.
325//
326// Hint 2: the x86 MMU checks permission bits in both the page directory
327// and the page table, so it's safe to leave permissions in the page
328// directory more permissive than strictly necessary.
329//
330// Hint 3: look at inc/mmu.h for useful macros that mainipulate page
331// table and page directory entries.
332//
333pte_t *
334pgdir_walk(pde_t *pgdir, const void *va, int create)
335{
336 // Fill this function in
337 return NULL;
338}
339
340//
341// Map [va, va+size) of virtual address space to physical [pa, pa+size)
342// in the page table rooted at pgdir. Size is a multiple of PGSIZE, and
343// va and pa are both page-aligned.
344// Use permission bits perm|PTE_P for the entries.
345//
346// This function is only intended to set up the ``static'' mappings
347// above UTOP. As such, it should *not* change the pp_ref field on the
348// mapped pages.
349//
350// Hint: the TA solution uses pgdir_walk
351static void
352boot_map_region(pde_t *pgdir, uintptr_t va, size_t size, physaddr_t pa, int perm)
353{
354 // Fill this function in
355}
356
357//
358// Map the physical page 'pp' at virtual address 'va'.
359// The permissions (the low 12 bits) of the page table entry
360// should be set to 'perm|PTE_P'.
361//
362// Requirements
363// - If there is already a page mapped at 'va', it should be page_remove()d.
364// - If necessary, on demand, a page table should be allocated and inserted
365// into 'pgdir'.
366// - pp->pp_ref should be incremented if the insertion succeeds.
367// - The TLB must be invalidated if a page was formerly present at 'va'.
368//
369// Corner-case hint: Make sure to consider what happens when the same
370// pp is re-inserted at the same virtual address in the same pgdir.
371// However, try not to distinguish this case in your code, as this
372// frequently leads to subtle bugs; there's an elegant way to handle
373// everything in one code path.
374//
375// RETURNS:
376// 0 on success
377// -E_NO_MEM, if page table couldn't be allocated
378//
379// Hint: The TA solution is implemented using pgdir_walk, page_remove,
380// and page2pa.
381//
382int
383page_insert(pde_t *pgdir, struct PageInfo *pp, void *va, int perm)
384{
385 // Fill this function in
386 return 0;
387}
388
389//
390// Return the page mapped at virtual address 'va'.
391// If pte_store is not zero, then we store in it the address
392// of the pte for this page. This is used by page_remove and
393// can be used to verify page permissions for syscall arguments,
394// but should not be used by most callers.
395//
396// Return NULL if there is no page mapped at va.
397//
398// Hint: the TA solution uses pgdir_walk and pa2page.
399//
400struct PageInfo *
401page_lookup(pde_t *pgdir, void *va, pte_t **pte_store)
402{
403 // Fill this function in
404 return NULL;
405}
406
407//
408// Unmaps the physical page at virtual address 'va'.
409// If there is no physical page at that address, silently does nothing.
410//
411// Details:
412// - The ref count on the physical page should decrement.
413// - The physical page should be freed if the refcount reaches 0.
414// - The pg table entry corresponding to 'va' should be set to 0.
415// (if such a PTE exists)
416// - The TLB must be invalidated if you remove an entry from
417// the page table.
418//
419// Hint: The TA solution is implemented using page_lookup,
420// tlb_invalidate, and page_decref.
421//
422void
423page_remove(pde_t *pgdir, void *va)
424{
425 // Fill this function in
426}
427
428//
429// Invalidate a TLB entry, but only if the page tables being
430// edited are the ones currently in use by the processor.
431//
432void
433tlb_invalidate(pde_t *pgdir, void *va)
434{
435 // Flush the entry only if we're modifying the current address space.
436 // For now, there is only one address space, so always invalidate.
437 invlpg(va);
438}
439
440
441// --------------------------------------------------------------
442// Checking functions.
443// --------------------------------------------------------------
444
445//
446// Check that the pages on the page_free_list are reasonable.
447//
448static void
449check_page_free_list(bool only_low_memory)
450{
451 struct PageInfo *pp;
452 unsigned pdx_limit = only_low_memory ? 1 : NPDENTRIES;
453 int nfree_basemem = 0, nfree_extmem = 0;
454 char *first_free_page;
455
456 if (!page_free_list)
457 panic("'page_free_list' is a null pointer!");
458
459 if (only_low_memory) {
460 // Move pages with lower addresses first in the free
461 // list, since entry_pgdir does not map all pages.
462 struct PageInfo *pp1, *pp2;
463 struct PageInfo **tp[2] = { &pp1, &pp2 };
464 for (pp = page_free_list; pp; pp = pp->pp_link) {
465 int pagetype = PDX(page2pa(pp)) >= pdx_limit;
466 *tp[pagetype] = pp;
467 tp[pagetype] = &pp->pp_link;
468 }
469 *tp[1] = 0;
470 *tp[0] = pp2;
471 page_free_list = pp1;
472 }
473
474 // if there's a page that shouldn't be on the free list,
475 // try to make sure it eventually causes trouble.
476 for (pp = page_free_list; pp; pp = pp->pp_link)
477 if (PDX(page2pa(pp)) < pdx_limit)
478 memset(page2kva(pp), 0x97, 128);
479
480 first_free_page = (char *) boot_alloc(0);
481 for (pp = page_free_list; pp; pp = pp->pp_link) {
482 // check that we didn't corrupt the free list itself
483 assert(pp >= pages);
484 assert(pp < pages + npages);
485 assert(((char *) pp - (char *) pages) % sizeof(*pp) == 0);
486
487 // check a few pages that shouldn't be on the free list
488 assert(page2pa(pp) != 0);
489 assert(page2pa(pp) != IOPHYSMEM);
490 assert(page2pa(pp) != EXTPHYSMEM - PGSIZE);
491 assert(page2pa(pp) != EXTPHYSMEM);
492 assert(page2pa(pp) < EXTPHYSMEM || (char *) page2kva(pp) >= first_free_page);
493
494 if (page2pa(pp) < EXTPHYSMEM)
495 ++nfree_basemem;
496 else
497 ++nfree_extmem;
498 }
499
500 assert(nfree_basemem > 0);
501 assert(nfree_extmem > 0);
502}
503
504//
505// Check the physical page allocator (page_alloc(), page_free(),
506// and page_init()).
507//
508static void
509check_page_alloc(void)
510{
511 struct PageInfo *pp, *pp0, *pp1, *pp2;
512 int nfree;
513 struct PageInfo *fl;
514 char *c;
515 int i;
516
517 if (!pages)
518 panic("'pages' is a null pointer!");
519
520 // check number of free pages
521 for (pp = page_free_list, nfree = 0; pp; pp = pp->pp_link)
522 ++nfree;
523
524 // should be able to allocate three pages
525 pp0 = pp1 = pp2 = 0;
526 assert((pp0 = page_alloc(0)));
527 assert((pp1 = page_alloc(0)));
528 assert((pp2 = page_alloc(0)));
529
530 assert(pp0);
531 assert(pp1 && pp1 != pp0);
532 assert(pp2 && pp2 != pp1 && pp2 != pp0);
533 assert(page2pa(pp0) < npages*PGSIZE);
534 assert(page2pa(pp1) < npages*PGSIZE);
535 assert(page2pa(pp2) < npages*PGSIZE);
536
537 // temporarily steal the rest of the free pages
538 fl = page_free_list;
539 page_free_list = 0;
540
541 // should be no free memory
542 assert(!page_alloc(0));
543
544 // free and re-allocate?
545 page_free(pp0);
546 page_free(pp1);
547 page_free(pp2);
548 pp0 = pp1 = pp2 = 0;
549 assert((pp0 = page_alloc(0)));
550 assert((pp1 = page_alloc(0)));
551 assert((pp2 = page_alloc(0)));
552 assert(pp0);
553 assert(pp1 && pp1 != pp0);
554 assert(pp2 && pp2 != pp1 && pp2 != pp0);
555 assert(!page_alloc(0));
556
557 // test flags
558 memset(page2kva(pp0), 1, PGSIZE);
559 page_free(pp0);
560 assert((pp = page_alloc(ALLOC_ZERO)));
561 assert(pp && pp0 == pp);
562 c = page2kva(pp);
563 for (i = 0; i < PGSIZE; i++)
564 assert(c[i] == 0);
565
566 // give free list back
567 page_free_list = fl;
568
569 // free the pages we took
570 page_free(pp0);
571 page_free(pp1);
572 page_free(pp2);
573
574 // number of free pages should be the same
575 for (pp = page_free_list; pp; pp = pp->pp_link)
576 --nfree;
577 assert(nfree == 0);
578
579 cprintf("check_page_alloc() succeeded!\n");
580}
581
582//
583// Checks that the kernel part of virtual address space
584// has been setup roughly correctly (by mem_init()).
585//
586// This function doesn't test every corner case,
587// but it is a pretty good sanity check.
588//
589
590static void
591check_kern_pgdir(void)
592{
593 uint32_t i, n;
594 pde_t *pgdir;
595
596 pgdir = kern_pgdir;
597
598 // check pages array
599 n = ROUNDUP(npages*sizeof(struct PageInfo), PGSIZE);
600 for (i = 0; i < n; i += PGSIZE)
601 assert(check_va2pa(pgdir, UPAGES + i) == PADDR(pages) + i);
602
603
604 // check phys mem
605 for (i = 0; i < npages * PGSIZE; i += PGSIZE)
606 assert(check_va2pa(pgdir, KERNBASE + i) == i);
607
608 // check kernel stack
609 for (i = 0; i < KSTKSIZE; i += PGSIZE)
610 assert(check_va2pa(pgdir, KSTACKTOP - KSTKSIZE + i) == PADDR(bootstack) + i);
611 assert(check_va2pa(pgdir, KSTACKTOP - PTSIZE) == ~0);
612
613 // check PDE permissions
614 for (i = 0; i < NPDENTRIES; i++) {
615 switch (i) {
616 case PDX(UVPT):
617 case PDX(KSTACKTOP-1):
618 case PDX(UPAGES):
619 assert(pgdir[i] & PTE_P);
620 break;
621 default:
622 if (i >= PDX(KERNBASE)) {
623 assert(pgdir[i] & PTE_P);
624 assert(pgdir[i] & PTE_W);
625 } else
626 assert(pgdir[i] == 0);
627 break;
628 }
629 }
630 cprintf("check_kern_pgdir() succeeded!\n");
631}
632
633// This function returns the physical address of the page containing 'va',
634// defined by the page directory 'pgdir'. The hardware normally performs
635// this functionality for us! We define our own version to help check
636// the check_kern_pgdir() function; it shouldn't be used elsewhere.
637
638static physaddr_t
639check_va2pa(pde_t *pgdir, uintptr_t va)
640{
641 pte_t *p;
642
643 pgdir = &pgdir[PDX(va)];
644 if (!(*pgdir & PTE_P))
645 return ~0;
646 p = (pte_t*) KADDR(PTE_ADDR(*pgdir));
647 if (!(p[PTX(va)] & PTE_P))
648 return ~0;
649 return PTE_ADDR(p[PTX(va)]);
650}
651
652
653// check page_insert, page_remove, &c
654static void
655check_page(void)
656{
657 struct PageInfo *pp, *pp0, *pp1, *pp2;
658 struct PageInfo *fl;
659 pte_t *ptep, *ptep1;
660 void *va;
661 int i;
662 extern pde_t entry_pgdir[];
663
664 // should be able to allocate three pages
665 pp0 = pp1 = pp2 = 0;
666 assert((pp0 = page_alloc(0)));
667 assert((pp1 = page_alloc(0)));
668 assert((pp2 = page_alloc(0)));
669
670 assert(pp0);
671 assert(pp1 && pp1 != pp0);
672 assert(pp2 && pp2 != pp1 && pp2 != pp0);
673
674 // temporarily steal the rest of the free pages
675 fl = page_free_list;
676 page_free_list = 0;
677
678 // should be no free memory
679 assert(!page_alloc(0));
680
681 // there is no page allocated at address 0
682 assert(page_lookup(kern_pgdir, (void *) 0x0, &ptep) == NULL);
683
684 // there is no free memory, so we can't allocate a page table
685 assert(page_insert(kern_pgdir, pp1, 0x0, PTE_W) < 0);
686
687 // free pp0 and try again: pp0 should be used for page table
688 page_free(pp0);
689 assert(page_insert(kern_pgdir, pp1, 0x0, PTE_W) == 0);
690 assert(PTE_ADDR(kern_pgdir[0]) == page2pa(pp0));
691 assert(check_va2pa(kern_pgdir, 0x0) == page2pa(pp1));
692 assert(pp1->pp_ref == 1);
693 assert(pp0->pp_ref == 1);
694
695 // should be able to map pp2 at PGSIZE because pp0 is already allocated for page table
696 assert(page_insert(kern_pgdir, pp2, (void*) PGSIZE, PTE_W) == 0);
697 assert(check_va2pa(kern_pgdir, PGSIZE) == page2pa(pp2));
698 assert(pp2->pp_ref == 1);
699
700 // should be no free memory
701 assert(!page_alloc(0));
702
703 // should be able to map pp2 at PGSIZE because it's already there
704 assert(page_insert(kern_pgdir, pp2, (void*) PGSIZE, PTE_W) == 0);
705 assert(check_va2pa(kern_pgdir, PGSIZE) == page2pa(pp2));
706 assert(pp2->pp_ref == 1);
707
708 // pp2 should NOT be on the free list
709 // could happen in ref counts are handled sloppily in page_insert
710 assert(!page_alloc(0));
711
712 // check that pgdir_walk returns a pointer to the pte
713 ptep = (pte_t *) KADDR(PTE_ADDR(kern_pgdir[PDX(PGSIZE)]));
714 assert(pgdir_walk(kern_pgdir, (void*)PGSIZE, 0) == ptep+PTX(PGSIZE));
715
716 // should be able to change permissions too.
717 assert(page_insert(kern_pgdir, pp2, (void*) PGSIZE, PTE_W|PTE_U) == 0);
718 assert(check_va2pa(kern_pgdir, PGSIZE) == page2pa(pp2));
719 assert(pp2->pp_ref == 1);
720 assert(*pgdir_walk(kern_pgdir, (void*) PGSIZE, 0) & PTE_U);
721 assert(kern_pgdir[0] & PTE_U);
722
723 // should be able to remap with fewer permissions
724 assert(page_insert(kern_pgdir, pp2, (void*) PGSIZE, PTE_W) == 0);
725 assert(*pgdir_walk(kern_pgdir, (void*) PGSIZE, 0) & PTE_W);
726 assert(!(*pgdir_walk(kern_pgdir, (void*) PGSIZE, 0) & PTE_U));
727
728 // should not be able to map at PTSIZE because need free page for page table
729 assert(page_insert(kern_pgdir, pp0, (void*) PTSIZE, PTE_W) < 0);
730
731 // insert pp1 at PGSIZE (replacing pp2)
732 assert(page_insert(kern_pgdir, pp1, (void*) PGSIZE, PTE_W) == 0);
733 assert(!(*pgdir_walk(kern_pgdir, (void*) PGSIZE, 0) & PTE_U));
734
735 // should have pp1 at both 0 and PGSIZE, pp2 nowhere, ...
736 assert(check_va2pa(kern_pgdir, 0) == page2pa(pp1));
737 assert(check_va2pa(kern_pgdir, PGSIZE) == page2pa(pp1));
738 // ... and ref counts should reflect this
739 assert(pp1->pp_ref == 2);
740 assert(pp2->pp_ref == 0);
741
742 // pp2 should be returned by page_alloc
743 assert((pp = page_alloc(0)) && pp == pp2);
744
745 // unmapping pp1 at 0 should keep pp1 at PGSIZE
746 page_remove(kern_pgdir, 0x0);
747 assert(check_va2pa(kern_pgdir, 0x0) == ~0);
748 assert(check_va2pa(kern_pgdir, PGSIZE) == page2pa(pp1));
749 assert(pp1->pp_ref == 1);
750 assert(pp2->pp_ref == 0);
751
752 // test re-inserting pp1 at PGSIZE
753 assert(page_insert(kern_pgdir, pp1, (void*) PGSIZE, 0) == 0);
754 assert(pp1->pp_ref);
755 assert(pp1->pp_link == NULL);
756
757 // unmapping pp1 at PGSIZE should free it
758 page_remove(kern_pgdir, (void*) PGSIZE);
759 assert(check_va2pa(kern_pgdir, 0x0) == ~0);
760 assert(check_va2pa(kern_pgdir, PGSIZE) == ~0);
761 assert(pp1->pp_ref == 0);
762 assert(pp2->pp_ref == 0);
763
764 // so it should be returned by page_alloc
765 assert((pp = page_alloc(0)) && pp == pp1);
766
767 // should be no free memory
768 assert(!page_alloc(0));
769
770 // forcibly take pp0 back
771 assert(PTE_ADDR(kern_pgdir[0]) == page2pa(pp0));
772 kern_pgdir[0] = 0;
773 assert(pp0->pp_ref == 1);
774 pp0->pp_ref = 0;
775
776 // check pointer arithmetic in pgdir_walk
777 page_free(pp0);
778 va = (void*)(PGSIZE * NPDENTRIES + PGSIZE);
779 ptep = pgdir_walk(kern_pgdir, va, 1);
780 ptep1 = (pte_t *) KADDR(PTE_ADDR(kern_pgdir[PDX(va)]));
781 assert(ptep == ptep1 + PTX(va));
782 kern_pgdir[PDX(va)] = 0;
783 pp0->pp_ref = 0;
784
785 // check that new page tables get cleared
786 memset(page2kva(pp0), 0xFF, PGSIZE);
787 page_free(pp0);
788 pgdir_walk(kern_pgdir, 0x0, 1);
789 ptep = (pte_t *) page2kva(pp0);
790 for(i=0; i<NPTENTRIES; i++)
791 assert((ptep[i] & PTE_P) == 0);
792 kern_pgdir[0] = 0;
793 pp0->pp_ref = 0;
794
795 // give free list back
796 page_free_list = fl;
797
798 // free the pages we took
799 page_free(pp0);
800 page_free(pp1);
801 page_free(pp2);
802
803 cprintf("check_page() succeeded!\n");
804}
805
806// check page_insert, page_remove, &c, with an installed kern_pgdir
807static void
808check_page_installed_pgdir(void)
809{
810 struct PageInfo *pp, *pp0, *pp1, *pp2;
811 struct PageInfo *fl;
812 pte_t *ptep, *ptep1;
813 uintptr_t va;
814 int i;
815
816 // check that we can read and write installed pages
817 pp1 = pp2 = 0;
818 assert((pp0 = page_alloc(0)));
819 assert((pp1 = page_alloc(0)));
820 assert((pp2 = page_alloc(0)));
821 page_free(pp0);
822 memset(page2kva(pp1), 1, PGSIZE);
823 memset(page2kva(pp2), 2, PGSIZE);
824 page_insert(kern_pgdir, pp1, (void*) PGSIZE, PTE_W);
825 assert(pp1->pp_ref == 1);
826 assert(*(uint32_t *)PGSIZE == 0x01010101U);
827 page_insert(kern_pgdir, pp2, (void*) PGSIZE, PTE_W);
828 assert(*(uint32_t *)PGSIZE == 0x02020202U);
829 assert(pp2->pp_ref == 1);
830 assert(pp1->pp_ref == 0);
831 *(uint32_t *)PGSIZE = 0x03030303U;
832 assert(*(uint32_t *)page2kva(pp2) == 0x03030303U);
833 page_remove(kern_pgdir, (void*) PGSIZE);
834 assert(pp2->pp_ref == 0);
835
836 // forcibly take pp0 back
837 assert(PTE_ADDR(kern_pgdir[0]) == page2pa(pp0));
838 kern_pgdir[0] = 0;
839 assert(pp0->pp_ref == 1);
840 pp0->pp_ref = 0;
841
842 // free the pages we took
843 page_free(pp0);
844
845 cprintf("check_page_installed_pgdir() succeeded!\n");
846}