Lecture 2: PC hardware and x86 programming

Preparation

Appendix A: PC hardware

  • Processor and memory
  • I/O

Appendix B: The boot loader

The BIOS loads the boot sector at memory address 0x7c00 and then jumps (sets the processor’s %ip) to that address.
When the boot loader begins executing, the processor is simulating an Intel 8088, and the loader’s job is to put the processor in a more modern operating mode, to load the xv6 kernel from disk into memory, and then to transfer control to the kernel.

Code: Assembly bootstrap

解释了 /boot/boot.S文件(再加上代码注释,非常详细)
该文件大概流程:

  1. disable interrupts
  2. set up the important data segment registers (DS, ES, SS)
  3. enable the 21st address bit using I/O to the keyboard controller on ports 0x64 and 0x60
  4. enables protected mode and 32-bit mode
  5. first action in 32-bit mode is to initialize the data segment registers
  6. set up a stack in an unused region of memory
  7. calls the C function bootmain

For historical reasons we have used the term virtual address to refer to addresses manipulated by programs. An xv6 virtual address is the same as an x86 logical address.
And in xv6, the logical address is equal to the linear address to which the segmentation hardware maps it.
xv6 makes almost no use of segments; it uses the paging hardware instead, as Chapter 2 describes. The boot loader sets up the segment descriptor table gdt so that all segments have a base address of zero.
When the boot loader is processing, the linear address is equal to the physical address, without paging hardware enabled.
Once the kernel starts executing, it will set up the paging hardware.

xv6-figure-B-1
The relationship between logical, linear, and physical addresses.

xv6-figure-B-2
Segments in protected mode.

(关于GDT,还可以看看这个

Code: C bootstrap

解释了 /boot/main.c文件(再加上代码注释,非常详细)
该文件大概流程:

  1. copy the kernel executable which starting at the second sector to address 0x10000
  2. check that this probably is an ELF binary, and not an uninitialized disk
  3. call the kernel’s entry point

the kernel has been compiled and linked so that it expects to find itself at virtual addresses starting at 0x80100000.
Once the kernel starts executing, it will set up the paging hardware to map virtual addresses starting at 0x80100000 to physical addresses starting at 0x00100000.

Real world

In real world, kernels are stored in ordinary file systems, where they may not be contiguous, or are loaded over a network. These complications require the boot loader to be able to drive a variety of disk and network controllers and understand various file systems and network protocols.
In other words, the boot loader itself must be a small operating system.
Therefore, PC operating systems often use a two-step boot process.

  • First, a simple boot loader like the one in this appendix loads a full-featured boot-loader from a known disk location, often relying on the less space-constrained BIOS for disk access rather than trying to drive the disk itself.
  • Then the full loader, relieved of the 512-byte limit, can implement the complexity needed to locate, load, and execute the desired kernel.

However, Modern PCs avoid many of the above complexities, because they support the Unified Extensible Firmware Interface (UEFI), which allows the PC to read a larger boot loader from the disk (and start it in protected and 32-bit mode).

Handouts

PC hardware and x86

  • Stored program computer
  • Stack
  • Memory-mapped I/O
  • Software = hardware

x86 and PC architecture

  • PC architecture
  • x86 instruction set
  • gcc calling conventions
  • PC emulation