[MIT6.828]LAB2 Exercise & Question总结

本文详细介绍了JOS内核中的内存管理实现,包括页分配、页释放等关键函数的实现原理。通过具体代码示例解释了物理内存与虚拟内存的映射过程,以及如何在内核中管理和分配内存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E1:In the file kern/pmap.c, you must implement code for the following functions.
    boot_alloc()
    page_init()
    page_alloc()
    page_free()


最后在i386_vm_init()中把painc()那句删掉,然后指定地方加入


在page_init中,那些被占用页的引用次数是从后面的代码中得到的,但因为这些页不会被释放的,所以写成什么都无所谓,但是写成正确的还是有助于系统完整性。

 

E2:看intel手册,明白分段保护的机制。

 

E3:实验看虚拟地址和对应的物理地址是否是相同的数据,略过。

 

E4:In the file kern/pmap.c, you must implement code for the following functions.
        pgdir_walk()
        boot_map_segment()
        page_lookup()
        page_remove()
        page_insert()

 

Exercise 5.  Fill in the missing code in i386_vm_init() after the call to page_check().

在指定位置添加如下代码即可完成物理页结构,内核栈,全部256MB内存的映射:

 

Q1:Assuming that the following JOS kernel code is correct, what type should variable x have, uintptr_t or physaddr_t?
    mystery_t x;
    char* value = return_a_pointer();
    *value = 10;
    x = (mystery_t) value;
value是可以直接操作虚拟内存的char * ,x是从value强制转化而来,没有经过虚拟内存转物理内存的步骤,所以mystery_t应该也是虚拟内存的一种,故x是uintptr_t类型。

Q2:What entries (rows) in the page directory have been filled in at this point? What addresses do they map and where do they point? In other words, fill out this table as much as possible:

Q3:After check_boot_pgdir(), i386_vm_init() maps the first four MB of virtual address space to the first four MB of physical memory, then deletes this mapping at the end of the function. Why is this mapping necessary? What would happen if it were omitted? Does this actually limit our kernel to be 4MB? What must be true if our kernel were larger than 4MB?
在开启了分页,但是虚拟内存的段基址仍然是0xf0000000的时候,内核的虚拟地址转化位线性地址后是定位到pgdir[0]页目录项0,所以pgdir[0]必须保存有内核物理页表地址。pgdir[0]=pgdir[PDX(KERNBASE)];这句代码就是做的这件事。直到后面加载完新的GDT时候,内核虚拟地址等同于线性地址,线性地址直接映射到pgdir[PDX(KERNBASE)]上,pgdir[0]就可以清空了。

Q4:(From Lecture 4) We have placed the kernel and user environment in the same address space. Why will user programs not be able to read or write the kernel's memory? What specific mechanisms protect the kernel memory?
因为我们的页表和页目录有PTE_U位,可以来控制用户是否可以访问某页。

 

Q5:What is the maximum amount of physical memory that this operating system can support? Why?
从目前的代码来看,一共映射了0--256M-1的物理内存地址空间,所以最多是访问256MB的内存。

 

Q6:How much space overhead is there for managing memory, if we actually had the maximum amount of physical memory? How is this overhead broken down?

目前来看就是这么多,如果要计算物理内存位256MB的极限情况,把256MB带入phymem即可。     

总结:大胆猜想,小心求证,高屋建瓴,细致入微。

实验概述 本次实验是MIT 6.828操作系统课程的第一次实验,主要内容是编写一个简单的操作系统内核,并在QEMU虚拟机上运行。本次实验共有9个练习,其中练习9要求实现一个简单的用户程序并运行。 练习9要求我们实现一个简单的用户程序,该程序能够在屏幕上输出一些信息,并等待用户输入,输入结束后将输入内容输出到屏幕上。用户程序的具体要求如下: - 输出一些信息,例如“Hello World!”。 - 等待用户输入,可以使用getchar()函数实现。 - 将用户输入内容输出到屏幕上。 实验过程 1. 编写用户程序 我们首先在lab1目录下创建一个user文件夹,用于存放用户程序。然后创建一个名为“test.c”的文件,编写用户程序的代码如下: ``` #include <stdio.h> int main() { printf("Hello World!\n"); char c = getchar(); printf("You entered: %c\n", c); return 0; } ``` 这段代码的功能是输出“Hello World!”并等待用户输入,输入结束后将输入内容输出到屏幕上。 2. 修改Makefile文件 为了能够编译用户程序,我们需要修改Makefile文件。具体修改如下: ``` UPROGS=\ _cat\ _echo\ _forktest\ _grep\ _init\ _kill\ _ln\ _ls\ _mkdir\ _rm\ _sh\ _stressfs\ _usertests\ _wc\ _test\ # 添加用户程序的名称 $(OBJDIR)/_test: $(OBJDIR)/test.o $(LIBDIR)/ulib.o | $(OBJDIR) $(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $@ $^ $(OBJDIR)/test.o: test.c | $(OBJDIR) $(CC) $(CFLAGS) -c -o $@ $< ``` 在UPROGS变量中添加上刚刚编写的用户程序的名称“_test”,然后在Makefile文件的末尾添加如上代码。 3. 编译内核和用户程序 在终端运行命令“make”,编译内核和用户程序。 4. 运行QEMU虚拟机 在终端运行命令“make qemu”,启动QEMU虚拟机。 5. 运行用户程序 在QEMU虚拟机中,输入“test”,即可运行刚刚编写的用户程序。运行结果如下: ``` Hello World! This is a test. You entered: T ``` 可以看到,程序首先输出了“Hello World!”这个信息,然后等待用户输入。我们输入了“This is a test.”这个字符串,然后按下回车键,程序将输入内容输出到了屏幕上。 实验总结 本次实验要求我们实现一个简单的用户程序并运行。通过编写代码、修改Makefile文件、编译内核和用户程序、启动虚拟机以及运行用户程序等步骤,我们成功地完成了本次实验,并学会了如何在操作系统内核中运行用户程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值