/* See COPYRIGHT for copyright information. */
#include <inc/x86.h>
#include <inc/mmu.h>
#include <inc/error.h>
#include <inc/string.h>
#include <inc/assert.h>
#include <kern/pmap.h>
#include <kern/kclock.h>
#include <kern/env.h>
#include <kern/cpu.h>
// These variables are set by i386_detect_memory()
size_t npages; // Amount of physical memory (in pages)
static size_t npages_basemem; // Amount of base memory (in pages)
// These variables are set in mem_init()
pde_t *kern_pgdir; // Kernel's initial page directory
struct Page *pages; // Physical page state array
static struct Page *page_free_list; // Free list of physical pages
// --------------------------------------------------------------
// Detect machine's physical memory setup.
// --------------------------------------------------------------
static int
nvram_read(int r)
{
return mc146818_read(r) | (mc146818_read(r + 1) << 8);
}
static void
i386_detect_memory(void)
{
size_t npages_extmem;
// Use CMOS calls to measure available base & extended memory.
// (CMOS calls return results in kilobytes.)
npages_basemem = (nvram_read(NVRAM_BASELO) * 1024) / PGSIZE;
npages_extmem = (nvram_read(NVRAM_EXTLO) * 1024) / PGSIZE;
// Calculate the number of physical pages available in both base
// and extended memory.
if (npages_extmem)
npages = (EXTPHYSMEM / PGSIZE) + npages_extmem;
else
npages = npages_basemem;
cprintf("Physical memory: %uK available, base = %uK, extended = %uK\n",
npages * PGSIZE / 1024,
npages_basemem * PGSIZE / 1024,
npages_extmem * PGSIZE / 1024);
}
// --------------------------------------------------------------
// Set up memory mappings above UTOP.
// --------------------------------------------------------------
static void mem_init_mp(void);
static void check_page_free_list(bool only_low_memory);
static void check_page_alloc(void);
static void check_kern_pgdir(void);
static physaddr_t check_va2pa(pde_t *pgdir, uintptr_t va);
static void check_page(void);
static void check_page_installed_pgdir(void);
static void boot_map_region(pde_t *pgdir, uintptr_t va, size_t size, physaddr_t pa, int perm);
// This simple physical memory allocator is used only while JOS is setting
// up its virtual memory system. page_alloc() is the real allocator.
//
// If n>0, allocates enough pages of contiguous physical memory to hold 'n'
// bytes. Doesn't initialize the memory. Returns a kernel virtual address.
//
// If n==0, returns the address of the next free page without allocating
// anything.
//
// If we're out of memory, boot_alloc should panic.
// This function may ONLY be used during initialization,
// before the page_free_list list has been set up.
static void *
boot_alloc(uint32_t n)
{
static char *nextfree; // virtual address of next byte of free memory
char *result;
// Initialize nextfree if this is the first time.
// 'end' is a magic symbol automatically generated by the linker,
// which points to the end of the kernel's bss segment:
// the first virtual address that the linker did *not* assign
// to any kernel code or global variables.
if (!nextfree) {
extern char end[];
nextfree = ROUNDUP((char *) end, PGSIZE);
}
//panic("this virtual address %x\n",nextfree);
// Allocate a chunk large enough to hold 'n' bytes, then update
// nextfree. Make sure nextfree is kept aligned
// to a multiple of PGSIZE.
//
// LAB 2: Your code here.
if(n == 0)
return nextfree;
result = nextfree;
nextfree += (n/PGSIZE + 1)*PGSIZE;
if((int)nextfree >= npages * PGSIZE + KERNBASE)
panic("Run out of memory!!\n");
return result;
}
// Set up a two-level page table:
// kern_pgdir is its linear (virtual) address of the root
//
// This function only sets up the kernel part of the address space
// (ie. addresses >= UTOP). The user part of the address space
// will be setup later.
//
// From UTOP to ULIM, the user is allowed to read but not write.
// Above ULIM the user cannot read or write.
void
mem_init(void)
{
uint32_t cr0;
size_t n;
// Find out how much memory the machine has (npages & npages_basemem).
i386_detect_memory();
// Remove this line when you're ready to test this function.
//panic("mem_init: This function is not finished\n");
//////////////////////////////////////////////////////////////////////
// create initial page directory.
kern_pgdir = (pde_t *) boot_alloc(PGSIZE);
memset(kern_pgdir, 0, PGSIZE);
//////////////////////////////////////////////////////////////////////
// Recursively insert PD in itself as a page table, to form
// a virtual page table at virtual address UVPT.
// (For now, you don't have understand the greater purpose of the
// following two lines.)
// Permissions: kernel R, user R
kern_pgdir[PDX(UVPT)] = PADDR(kern_pgdir) | PTE_U | PTE_P;
//////////////////////////////////////////////////////////////////////
// Allocate an array of npages 'struct Page's and store it in 'pages'.
// The kernel uses this array to keep track of physical pages: for
// each physical page, there is a corresponding struct Page in this
// array. 'npages' is the number of physical pages in memory.
// Your code goes here:
pages = (struct Page *)boot_alloc(npages * sizeof(struct Page));
//int i = 0;
//for(; i < npages; i++){
// struct Page *curn_p = pages + i;
// curn_p -> pp_link = curn_p + 1;
// curn_p -> pp_ref = 1;
//}
//panic("PDX(0)");
//////////////////////////////////////////////////////////////////////
// Make 'envs' point to an array of size 'NENV' of 'struct Env'.
// LAB 3: Your code here.
envs = (struct Env *)boot_alloc(NENV * sizeof(struct Env));
//////////////////////////////////////////////////////////////////////
// Now that we've allocated the initial kernel data structures, we set
// up the list of free physical pages. Once we've done so, all further
// memory management will go through the page_* functions. In
// particular, we can now map memory using boot_map_region
// or page_insert
page_init();
check_page_free_list(1);
check_page_alloc();
//panic("Lab2-Part1 complete!\n");
check_page();
//panic("Lab2-Part2 complete!\n");
//////////////////////////////////////////////////////////////////////
// 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:
//pte_t *p = (pte_t *)0xf03fd000;
boot_map_region(kern_pgdir,UPAGES, npages * sizeof(struct Page), PADDR(pages), PTE_U|PTE_P);
//cprintf("##%x", PTE_ADDR(p[PTX(UPAGES)]));
//////////////////////////////////////////////////////////////////////
// Map the 'envs' array read-only by the user at linear address UENVS
// (ie. perm = PTE_U | PTE_P).
// Permissions:
// - the new image at UENVS -- kernel R, user R
// - envs itself -- kernel RW, user NONE
// LAB 3: Your code here.
boot_map_region(kern_pgdir,UENVS, NENV * sizeof(struct Env), PADDR(envs), PTE_U|PTE_P);
//////////////////////////////////////////////////////////////////////
// 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)
// 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:
// cprintf("\n%x\n", KSTACKTOP - KSTKSIZE);
boot_map_region(kern_pgdir, KSTACKTOP - KSTKSIZE, KSTKSIZE, PADDR(bootstack), PTE_P|PTE_W);
//////////////////////////////////////////////////////////////////////
// Map all
没有合适的资源?快使用搜索试试~ 我知道了~
mit6.828_Jos_lab4.rar_jos lab4
共843个文件
o:67个
c:59个
asm:32个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 183 浏览量
2022-09-20
21:47:52
上传
评论
收藏 2.58MB RAR 举报
温馨提示
MIT6.828课程JOS的LAB4的源代码。造福大众,从我做起
资源推荐
资源详情
资源评论
收起资源包目录
mit6.828_Jos_lab4.rar_jos lab4 (843个子文件)
001a2ef9f4a98c4cd471ff5c9dd7a27557fe1c 7KB
012cd2593ff14937a42dc13ed5ed418f71ddf0 9KB
02620fb50a86eb519d3209ffeb00903d69d027 549B
026f26a4981400521229a5907ef987ba08fea3 80B
0270cb3394409eff73c0a633094c2cb900c540 2KB
028c7d346e576ba3f13acceeb3792c70b8031d 174B
02ae3bebd6f27218d57ef2747be4aaf9065db6 758B
030cd352c6ff1ea266ab6484b0cdb8a65e6541 392B
03aa4a9cdafa41dcd7ccefd33d81f951498e3c 1KB
03f9e020c41e31b3f6e6a168a8ad8667eb6af1 429B
04b504dfa72822cc2c47507a66e32fc3255c70 650B
04df52ef7020645e5d6b150431a46823b2bf84 4KB
057a139273c030141694142bb2911d842f3ef6 172B
05c83738667fef4461f4e8aafcc8df03916bcf 6KB
06dab741ef0e7049f6d70bde921f01e27fdc66 355B
0799392d351f539785d6c8a1a1ca597bc56763 242B
08333a752120b645a8d8e94785beab95ae0e10 3KB
08379f5189813c4533d8ab1bcfca4855de6f5c 352B
0888410fd68d021ababb59fbdcecd886f129d0 11KB
0898a379c282ee2472ecf0ba9178da3e23f32d 405B
08a860ff9ea80e438ceb3b4b0cb2e00aac842d 122B
0a75ad2049486dfb6f86a6e7014451395aa780 4KB
0aeeb0af001034a83c192414647009a2d90d3a 1KB
0b1f451d0ff1d6a9ee2b0f9a46f3aea35558ea 7KB
0b9206db51d1560684d6c159da0ab7ea61b599 316B
0b97df1f1b87fa2263975ff494675cbec5b220 674B
0bacb4652b8d8a2a2205ca6c529c6f902ad66a 1KB
0d1e72da1bdefba032be041d426b0932ef0c70 172B
0ec192ffd4875cdb5b807b270f5560aa8b3699 372B
0eddc6ae733c42696e9d1f62bca00168cc1922 9KB
10144c6a20dcd88b63518a4a105d685c7ea78a 284B
1077b8301b4747460c3c004eede14623eb194f 358B
12f9205ffa141216da41d707757c90d07f8fe2 520B
1319af0c6f9cffa808d93f93d9e83eef036f0c 416B
1407ab32a4750c24dab8a4a86bedbce928161d 3KB
140b1f32111105ac7ccd55a09a7ccd9cef8425 471B
143b16d0cfe30d7c8d296bb31f0d1e351afff0 518B
146ce561f13fb6fbeaecd2bfc72e3f6ed524df 202B
157023fef148c3d63c5e2c04b2d8c08519d5d4 206B
16a418002c3f1622979b342cdf37c12a8652fa 723B
17ba93a475d4d8e0aa0c9d60c922fc6430f9d6 79B
17c7debc006eb5999289f3f102c00688763a7c 342B
17c8981d1cc6d8740d40ab13c973557c0e595c 531B
17fdb9c7371a2de39ca22f46ceb644ff778cfb 599B
18b31af40d2803cb8d5bf6a38827e24cea165e 2KB
18d0cb1168f765a349d237b3662df8c06198a8 667B
19a17a9d6de1f445cfd10ee56503752c74f946 485B
1a5e1497115b9fe55ea665e5236fb4fdc613ba 1KB
1a72cb9a93876e2fe723314c3eefac9fc21736 228B
1acc12a03f91885a4f9b010218c6f52eadbd8b 481B
1ae49d6ef8886690fa9de6bdaddcedd102ca4b 1KB
1b20f3c8d82bed01ed441e60e1b1ffc88427c7 964B
1b36889a2fed853d13fcdd889c0d0139bbcee4 733B
1b59e596dffa9cde9fb09ac89b2c0ec23e4136 346B
1b69e6ac6f14403609ab4c1fa2e56a6ed5ff27 4KB
1b71dd4ba901f4cf10f3c1be3a4663bbb4eb5c 3KB
1bfc9b18401eb8a1ad70e11b4bb5ac8960abf8 240B
1c7b218ce94fd47cdf1cf34c4d73b3bf8223cf 162B
1c9038bc07986a60957966237ac8d2faf00f16 1KB
1c9ea4f12939950b9842b1fd73b8b84cc900ed 590B
1cbe66a2b63723ff10ff86aa9e5c53afd62327 3KB
1d0ff3484945ebae17df801b89370257d6e8fa 833B
1e32c5845859025caf87dafdc6cb0d04095203 210B
1eaf87f0b5c4e9c95432c69da1cd01dfc5d520 735B
1ebd68d8fa5dfa936b40761c0cb3e746519157 1KB
2068abc83e3d0eb57fecbfe6f86081bef9d631 6KB
20da2df035a5b73d3c0bbeacc21d63a533d0f2 3KB
20da614b4618feb5a8730aba2598794afce91d 63B
219f03eba15680098b1de78380380300347019 639B
21e3700b3d7484b22b4878c2a800298421f414 1KB
224cc54a5be05216153104d19318fa51601f3a 63B
23b10e9bc1206d51eeac1e86d1c33ca1ec2c68 653B
23d9998dfbe43f189d8185da63595e88ec968b 673B
2506cdd9c243fafa19d1bc38ce0e7e96f57d58 550B
254f67d7f6ca804cbec640989c4fde524d1e52 171B
25886a382f9fe9db4fca62a1f8916faeb6628d 3KB
266520b428d9c3baf83e3be16b39c3946a2900 2KB
271786cd877ad15d643bfd3efc7511c2739a51 804B
2796a491c0929e2620fcfbdc68f5419a42611d 79B
27f23b0915587863b5820e47ed884f30e10348 3KB
28c651ab33931d41ca3cac57d152fd21fed048 2KB
28d557bf9aadfef7ccdc3e301410183bbab386 10KB
28d829bb84682ab85acff73b6411e44dae8cef 976B
291d4d404188602fb5584e620282810538e021 108B
29bcbd86cb7eccf37bc04346f2ab3d7884da47 79B
2a0f40a9d8f04c3c9996c2ccbf030a9f71a6e0 626B
2a386f4ccf8a6be64c58a313676e0fae0376aa 1KB
2ae487ab3f4b02d28f0b3671e32852e5601fc3 985B
2bab6c472835fc90f4d98e147023b70b09bd88 79B
2bda0e6da4535d13b80501a8775dff6967006c 3KB
2c0b069ef04bd0f662ca3ab98ff66ddd495c98 1003B
2e01fd4c601cc47a475ae410ba9ee47943fe61 2KB
2faf3caa09875cdcdaa21221e16c591fa4a7d7 3KB
30d5392884b8efc293810e65a72ba369e38a62 1KB
3156fca7845512770df01c82248c7b41889f78 299B
322fc59519468eb042d6a975565637761019cc 61B
324d164cea5e36eea05931924f11f47882065a 3KB
32ca57c7d2a9d6722af4217a8f177ee6ba73c6 284B
32cf9c9bf0fa0ea6482c3a3ac41778d384b7d9 650B
32fa1e1c61093775267236ab602ebe0a65f8cb 2KB
共 843 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
资源评论
alvarocfc
- 粉丝: 105
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功