/* 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>
// 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 PageInfo *pages; // Physical page state array
static struct PageInfo *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 boot_map_region(pde_t *pgdir, uintptr_t va, size_t size, physaddr_t pa, int perm);
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);
// 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);
}
// 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.
cprintf("boot_alloc memory at %x\n", nextfree);
cprintf("Next memory at %x\n", ROUNDUP((char *) (nextfree+n), PGSIZE));
if (n != 0) {
char *next = nextfree;
nextfree = ROUNDUP((char *) (nextfree+n), PGSIZE);
return next;
} else return nextfree;
return NULL;
}
// 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 line.)
// Permissions: kernel R, user R
kern_pgdir[PDX(UVPT)] = PADDR(kern_pgdir) | PTE_U | PTE_P;
//////////////////////////////////////////////////////////////////////
// Allocate an array of npages 'struct PageInfo'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 PageInfo in this
// array. 'npages' is the number of physical pages in memory.
// Your code goes here:
pages = (struct PageInfo *) boot_alloc(sizeof(struct PageInfo) * npages);
cprintf("npages: %d\n", npages);
cprintf("npages_basemem: %d\n", npages_basemem);
cprintf("pages: %x\n", pages);
//////////////////////////////////////////////////////////////////////
// Make 'envs' point to an array of size 'NENV' of 'struct Env'.
// LAB 3: Your code here.
envs = (struct Env *) boot_alloc(sizeof(struct Env) * NENV);
//////////////////////////////////////////////////////////////////////
// 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();
cprintf("so far so good\n");
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:
boot_map_region(kern_pgdir,
UPAGES,
PTSIZE,
PADDR(pages),
PTE_U);
cprintf("PADDR(pages) %x\n", PADDR(pages));
//////////////////////////////////////////////////////////////////////
// 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,
PTSIZE,
PADDR(envs),
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)
// 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);
cprintf("PADDR(bootstack) %x\n", PADDR(bootstack));
//////////////////////////////////////////////////////////////////////
// Map all of physical memory at KERNBASE.
// Ie. the VA range [KERNBASE, 2^32) should map to
// the PA range [0, 2^32 - KERNBASE)
没有合适的资源?快使用搜索试试~ 我知道了~
MIT 6.828 lab3 完整代码
共139个文件
c:34个
h:25个
asm:15个
4星 · 超过85%的资源 需积分: 50 32 下载量 50 浏览量
2016-11-21
09:30:12
上传
评论 2
收藏 1.06MB ZIP 举报
温馨提示
MIT 6.828 lab3的完整代码, 经试验可以完美运行,在写报告的时候可以参考一下。
资源推荐
资源详情
资源评论
收起资源包目录
MIT 6.828 lab3 完整代码 (139个子文件)
kernel.asm 391KB
testbss.asm 93KB
hello.asm 90KB
divzero.asm 90KB
evilhello.asm 90KB
faultreadkernel.asm 90KB
buggyhello2.asm 90KB
faultread.asm 90KB
buggyhello.asm 90KB
badsegment.asm 90KB
faultwritekernel.asm 90KB
faultwrite.asm 90KB
softint.asm 89KB
breakpoint.asm 89KB
boot.asm 14KB
badsegment 30KB
boot 512B
breakpoint 30KB
buggyhello 30KB
buggyhello2 30KB
pmap.c 31KB
entrypgdir.c 28KB
env.c 16KB
console.c 10KB
kdebug.c 7KB
trap.c 7KB
monitor.c 6KB
printfmt.c 6KB
string.c 5KB
main.c 3KB
syscall.c 2KB
init.c 2KB
syscall.c 1KB
printf.c 1KB
libmain.c 770B
readline.c 627B
testbss.c 615B
panic.c 526B
printf.c 517B
kclock.c 343B
console.c 339B
evilhello.c 272B
buggyhello2.c 258B
badsegment.c 224B
buggyhello.c 199B
faultreadkernel.c 195B
hello.c 194B
faultread.c 178B
divzero.c 173B
softint.c 158B
faultwritekernel.c 151B
faultwrite.c 138B
breakpoint.c 125B
exit.c 65B
CODING 1KB
COPYRIGHT 8KB
COPYRIGHT 8KB
init.d 227B
init.d 215B
.deps 6KB
divzero 30KB
.DS_Store 12KB
.DS_Store 10KB
evilhello 30KB
faultread 30KB
faultreadkernel 30KB
faultwrite 30KB
faultwritekernel 30KB
GNUmakefile 7KB
grade-lab1 1KB
grade-lab2 706B
grade-lab3 4KB
mmu.h 11KB
memlayout.h 8KB
x86.h 7KB
kbdreg.h 3KB
pmap.h 2KB
trap.h 2KB
types.h 2KB
stab.h 2KB
env.h 2KB
lib.h 1KB
elf.h 1KB
env.h 1KB
kclock.h 1KB
string.h 874B
stdio.h 829B
assert.h 607B
trap.h 599B
kdebug.h 596B
monitor.h 591B
error.h 587B
console.h 526B
stdarg.h 335B
syscall.h 313B
syscall.h 194B
csa.h 8B
handin-prep 2KB
hello 30KB
kernel.img 4.88MB
共 139 条
- 1
- 2
资源评论
- weixin_397512472018-12-15现在好像版本更新了,不太一样。。?
- 十二以左2019-05-05下载包丢失了,重新下载要求的
yanming1540592408
- 粉丝: 0
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Java的在线宠物用品交易网站设计与实现源码
- 毕业设计-仿生六足机器人的制作全教程源码+电子元器件+程序代码+线路组件图+安装教程+搭建视频教程
- 基于ROS和webots的xrobot机械臂仿真初探C++源码
- 基于ROS的点焊机器人仿真与控制python源码+文档说明+使用说明+详细注释
- 基于vue实现的细粒度交通时空大数据分析系统+源代码+文档说明
- 安卓大作业-基于Electron的交通时空大数据分析挖掘系统客户端(Android)+源代码+文档说明+界面截图
- 基于Java的朱氏集团客户关系管理系统设计源码
- 基于C++的作业提交与批改系统设计源码
- 基于Vue2的移动端电影资讯网站设计源码
- 高分课程设计作业-基于QT的模仿宝石迷阵游戏C++源码+文档说明+界面截图
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功