cosven / cosven.github.io

个人零碎笔记,博客草稿,阅读笔记
10 stars 0 forks source link

6.828 实验记录 #63

Open cosven opened 5 years ago

cosven commented 5 years ago

lab1: Booting a PC

https://pdos.csail.mit.edu/6.828/2018/labs/lab1/

Part 1: PC Bootstrap

这个实验主要分为几个部分

  1. 熟悉 x86 汇编(暂时忽略)
  2. 模拟 x86 PC -> 使用 qemu 这个工具
  3. PC 物理地址空间 -> 后面实验结果分析会用到这个图
+------------------+  <- 0xFFFFFFFF (4GB)
|      32-bit      |
|  memory mapped   |
|     devices      |
|                  |
/\/\/\/\/\/\/\/\/\/\

/\/\/\/\/\/\/\/\/\/\
|                  |
|      Unused      |
|                  |
+------------------+  <- depends on amount of RAM
|                  |
|                  |
| Extended Memory  |
|                  |
|                  |
+------------------+  <- 0x00100000 (1MB)
|     BIOS ROM     |
+------------------+  <- 0x000F0000 (960KB)
|  16-bit devices, |
|  expansion ROMs  |
+------------------+  <- 0x000C0000 (768KB)
|   VGA Display    |
+------------------+  <- 0x000A0000 (640KB)
|                  |
|    Low Memory    |
|                  |
+------------------+  <- 0x00000000
#+END_SRC
  1. The ROM BIOS

练习 1:使用 GDB 来调试启动过程,从实验结果得到的一些结论:

  • The IBM PC starts executing at physical address 0x000ffff0, which is at the very top of the 64KB area reserved for the ROM BIOS.
  • The PC starts executing with CS = 0xf000 and IP = 0xfff0.
  • The first instruction to be executed is a jmp instruction, which jumps to the segmented address CS = 0xf000 and IP = 0xe05b.

This is how Intel designed the 8088 processor, which IBM used in their original PC. Because the BIOS in a PC is "hard-wired" to the physical address range 0x000f0000-0x000fffff, this design ensures that the BIOS always gets control of the machine first after power-up or any system restart - which is crucial because on power-up there is no other software anywhere in the machine's RAM that the processor could execute.

练习 2:继续使用 GDB 单步调试,看看启动时候都干了啥?

没有太懂,资料也很少,但是这个过程和 DMA 应该有关,http://www.voidcn.com/article/p-eajspssw-dp.html 这篇博客有一定参考价值。下面贴一下它的结论

When the BIOS runs, it sets up an interrupt descriptor table and initializes various devices such as the VGA display.

interrupt descriptor table: 能在调试中看到一些 CLI CLD 相关指令,似乎是与这个中断描述表有关系。

After initializing the PCI bus and all the important devices the BIOS knows about, it searches for a bootable device such as a floppy, hard drive, or CD-ROM. Eventually, when it finds a bootable disk, the BIOS reads the boot loader from the disk and transfers control to it.

看不太懂,先不管。

cosven commented 5 years ago

Part 2: The Boot Loader

Floppy and hard disks for PCs are divided into 512 byte regions called sectors.

If the disk is bootable, the first sector is called the boot sector

When the BIOS finds a bootable floppy or hard disk, it loads the 512-byte boot sector into memory at physical addresses 0x7c00 through 0x7dff, and then uses a jmp instruction to set the CS:IP to 0000:7c00, passing control to the boot loader.

the way a modern BIOS boots from a CD-ROM is a bit more complicated (and more powerful).

For 6.828, we will use the conventional hard drive boot mechanism

The boot loader consists of one assembly language source file, boot/boot.S, and one C source file, boot/main.c Look through these source files carefully and make sure you understand what's going on.

The boot loader must perform two main functions:

First, the boot loader switches the processor from real mode to 32-bit protected mode, because it is only in this mode that software can access all the memory above 1MB in the processor's physical address space. Second, the boot loader reads the kernel from the hard disk by directly accessing the IDE disk device registers via the x86's special I/O instructions.

阅读 boot.S 源码

这篇博客对代码有比较详细的中文注释和解释,它的参考资料也很好 https://blog.csdn.net/scnu20142005027/article/details/51147402

阅读时产生的一些疑问

  1. A20 地址总线干啥用的?网上很多博客都有提到

  2. PS/2 Controller IO Ports 是啥东西? 看这个资料,解释的 非常好 https://wiki.osdev.org/%228042%22_PS/2_Controller#Overview

PS / 2控制器(通常称为“键盘控制器”)位于主板上。在早期,控制器是一个单芯片(8042)。截至今天,它是Advanced Integrated Peripheral的一部分。 该名称具有误导性,因为控制器不仅仅控制与PS / 2设备的通信。 此处应该有一张图(可以看出它的 0x60 和 0x64 Port 是和 CPU 通信的)。

扫了几篇博客,有的说把 0xdf 写到 0x60 port 会关闭,但看 boot.S 代码,明显这是开启 A20 Gate,有点疑惑

readlnh commented 4 years ago

A20是为了兼容以前的cpu的。不打开a20,即使是在保护模式,也只能访问到1M的内存,这个东西只有x86有。

cosven commented 4 years ago

A20是为了兼容以前的cpu的。不打开a20,即使是在保护模式,也只能访问到1M的内存,这个东西只有x86有。

又发现了一个「只学了第一节」的课程...