MIT jos 6.828 Fall 2014 训练记录(lab 1)

 

注: 源代码参见我的github:https://github.com/YaoZengzeng/jos

 

Part 1: PC Bootstrap

+------------------+  <- 0xFFFFFFFF (4GB)    最高位的一部分内存被BIOS保留,用于一些32位设备的使用
|      32-bit      |               当内存大于4GB的时候,这一部分内存就会成为另一个hole
|  memory mapped   |
|     devices      |
|                  |
/\/\/\/\/\/\/\/\/\/\

/\/\/\/\/\/\/\/\/\/\
|                  |
|      Unused      |
|                  |
+------------------+  <- depends on amount of RAM
|                  |
|                  |
| Extended Memory  |
|                  |
|                  |
+------------------+  <- 0x00100000 (1MB)   ---> BIOS 主要用于负责系统的初始化,例如初始化显卡,以及统计内存的大小,接着加载操作系统
|     BIOS ROM     |
+------------------+  <- 0x000F0000 (960KB)
|  16-bit devices, |
|  expansion ROMs  |
+------------------+  <- 0x000C0000 (768KB)
|   VGA Display    |
+------------------+  <- 0x000A0000 (640KB)  --> 0~640KB是low memory, 640KB~1MB hole, 1MB~end extended memory
|                  |
|    Low Memory    |
|                  |
+------------------+  <- 0x00000000

在实模式下,physical address = 16 * segment + offset,处理器重置以后,执行的第一条指令位于[f000:fff0],因为BIOS位于这个区域内,而且一般第一条指令都是跳转指令,因为[f000:fff0]已经位于BIOS的末端了。

Part 2: The Boot Loader
1、一个扇区(sector)是512字节,是磁盘中的最小操作粒度,每次的读写操作必须是整数个扇区并且沿着扇区的边界进行。

2、如果一块磁盘是可以启动的,那么第一个扇区被称为启动扇区(boot sector),其中存放了boot loader的代码。当BIOS发现一个可启动磁盘的时候,它会将启动扇区加载到物理地址为0x7c00~0x7dff的区域,然后使用jmp指令跳转
到该地址,并将控制传递给boot loader。

3、boot loader主要有两个方面的作用,一方面用于将处理器从实模式转换为32位的保护模式,因为只有在保护模式下才能访问1MB以上的物理地址空间,另一方面通过x86特殊的IO指令,利用IDE磁盘特殊的设备寄存器,从IDE磁盘中读取
内核。

4、
(1)At what point does the processor start executing 32-bit code? What exactly causes the switch from 16- to 32-bit mode?
在boot/boot.S执行如下指令, 改变寄存器的状态:
lgdt gdtdesc
movl %cr0, %eax
orl %CR0_PE_ON, %eax
movl %eax, %cr0

(2)What is the last instruction of the boot loader executed, and what is the first instruction of the kernel it just loaded?
在boot/main.c 中((void (*)(void)) (ELFHDR->e_entry))();是boot loader的最后一条指令。

(3)Where is the first instruction of the kernel?
start address 0x0010000c

(4)How does the boot loader decide how many sectors it must read in order to fetch the entire kernel from disk? Where does it find this information?
因为kernel是ELF格式的二进制文件,首先读取一个ELF的header,其中包含了kernel所有信息

5、ELF格式的二进制文件结构可以被认为有一个包含了各种加载信息的header(包括一个固定长度的ELF header和可变长度的program header),之后再跟随多个program section,其中program section
是一段连续的代码或数据并且能被加载到特定的内存地址。其中我们感兴趣的program section 包括:
.text: 程序的可执行指令
.rodata: 只读的数据
.data: 包含了程序的初始化数据
.bss: 紧跟在.data section之后,用于存放未初始化的全局变量。C语言中要求未初始化的全局变量的值都为0,因此在二进制文件中无需为它保留存储空间。linker只是记录了.bss段的大小和地址,loader或者
程序自身负责零化.bss段。

6、VMA(link address)是这个section期望开始执行的地址。LMA(load address)是这个section应该被加载到内存的地址。通常来说,link address和load address是一样的。

7、ELF header中有一个非常重要的字段叫e_entry,其中包含了程序的entry point的link address,也就是程序开始执行的地址。

Part 3: The Kernel
1、通常操作系统的内核会被链接并且运行在一个非常高的virtual address,例如0xf0100000,因为需要把低位的地址剩下给用户程序使用。但是因为许多机器没有高达0xf0100000的物理地址,因此我们不能将内核
直接放在那,所以我们使用了处理器的内存管理硬件将虚拟地址0xf0100000映射到0x00100000。这样既然内核的虚拟地址足够高,同时内核加载的物理地址仅仅位于BIOS之上,一般的机器都能满足条件。

2、
(1) Explain the interface between printf.c and console.c. Specifically, what function does console.c export? How is this function used by printf.c?
console.c export函数cputchar(),putch(...)->cputchar(...)

(2)
Explain the following from console.c:
1      if (crt_pos >= CRT_SIZE) {
2              int i;
3              memcpy(crt_buf, crt_buf + CRT_COLS, (CRT_SIZE - CRT_COLS) * sizeof(uint16_t));
4              for (i = CRT_SIZE - CRT_COLS; i < CRT_SIZE; i++)
5                      crt_buf[i] = 0x0700 | ' ';
6              crt_pos -= CRT_COLS;
7      }
将屏幕的内容向上移动一行
(4)Run the following code.
    unsigned int i = 0x00646c72;
    cprintf("H%x Wo%s", 57616, &i);
What is the output? "He11o World".

(5) In the following code, what is going to be printed after 'y='? (note: the answer is not a specific value.) Why does this happen?
    cprintf("x=%d y=%d", 3);

3、当进入一个C函数时,先将被调用函数的五个参数压入栈中,接着再将当前执行函数的压入return instruction pointer(eip,一般是call指令的下一条指令)栈中,用于从被调用函数返回时继续执行当前函数。
最后将前一个函数的base pointer(ebp寄存器的内容)压入堆栈,然后再将当前esp寄存器的内容存入ebp寄存器中。由此开始新一轮函数的执行。

posted on 2016-09-16 17:24  姚灯灯!  阅读(795)  评论(0编辑  收藏  举报

导航