MIT6.828-Lab2-Part3-Exercise5-设置UTOP之上的内核地址空间

 

设置UTOP之上的内核地址空间

  1. 完善调用 cehck_page() 之后的代码,使其能够通过 check_kern_pgdir() and check_page_installed_pgdir() 的验证

根据 inc/memlayout.h 和 注释 来填写

// boot_map_region的原型如下:
// static void boot_map_region(pde_t *pgdir, uintptr_t va, size_t size, physaddr_t pa, int perm);

	check_page();

	//////////////////////////////////////////////////////////////////////
	// Now we set up virtual memory

	//////////////////////////////////////////////////////////////////////
	// Map 'pages' read-only by the user at linear address UPAGES
	// Permissions:
	//    - the new image at UPAGES -- kernel R, user R
	//      (ie. perm = PTE_U | PTE_P)
	//    - pages itself -- kernel RW, user NONE
	// Your code goes here:
	// 将pages数组映射到UPAGES的虚拟地址位置,大小为PTSIZE,即一个页表的大小4MB
	// 权限根据注释设置为 PTE_U | PTE_P,PTE_P本身在调用函数的时候,里面会加上
	boot_map_region(kern_pgdir, UPAGES, PTSIZE, PADDR(pages), PTE_U);

	//////////////////////////////////////////////////////////////////////
	// Use the physical memory that 'bootstack' refers to as the kernel
	// stack.  The kernel stack grows down from virtual address KSTACKTOP.
	// We consider the entire range from [KSTACKTOP-PTSIZE, KSTACKTOP)
	// 内核栈使用bootstack指针所指向的物理内存。虚拟地址起始地址是KSTACKTOP,向下生长PTSIZE大小
	// to be the kernel stack, but break this into two pieces:
	// 这一块内存还分为两个部分
	//     * [KSTACKTOP-KSTKSIZE, KSTACKTOP) -- backed by physical memory
	//        第一部分:这部分虚拟地址空间分配给内核栈使用,由物理内存提供支持
	//     * [KSTACKTOP-PTSIZE, KSTACKTOP-KSTKSIZE) -- not backed; so if
	//       the kernel overflows its stack, it will fault rather than
	//       overwrite memory.  Known as a "guard page".
	//				第二部分:这部分内存没有被分配给内核栈使用,被称作“保护页”,用于保护内核栈的溢出
	//				如果内核栈溢出,就会触发缺页异常,因为这部分内存没有被映射到物理内存上。
	//     Permissions: kernel RW, user NONE
	//				两部分权限都是内核可读可写,用户无法访问。
	// Your code goes here:
	// 
	boot_map_region(kern_pgdir, KSTACKTOP-KSTKSIZE, KSTKSIZE, PADDR(bootstack), PTE_W);
	
	//////////////////////////////////////////////////////////////////////
	// Map all of physical memory at KERNBASE.
	// 将所有的物理内存映射到从KERNBASE开始的虚拟地址空间
	// Ie.  the VA range [KERNBASE, 2^32) should map to
	//      the PA range [0, 2^32 - KERNBASE)
	// 实现上面说的这个映射关系即可,很直观
	// We might not have 2^32 - KERNBASE bytes of physical memory, but
	// we just set up the mapping anyway.
	// Permissions: kernel RW, user NONE
	// Your code goes here:
	boot_map_region(kern_pgdir, KERNBASE, 0xffffffff-KERNBASE, 0, PTE_W);


	// Check that the initial page directory has been set up correctly.
	check_kern_pgdir();

参考资料

  1. cnblog–fatsheep9146