Please disable your adblock and script blockers to view this page

Before the BSD Kernel starts: Part One on AMD64


OS
BIOS
CS
UEFI
Basic Input/Output System
IBM
Unified Extensible Firmware Interface
GPT
MBR
PCI
Intel® Architecture
DRAM
ELF
PE (Portable
DL
PBR
Logical Block Address
ESI
0x400
PAE
LM
EFER
GDT


Maciej Grochowski System
Linkable Format
GPT
KERNELBASE

No matching tags

No matching tags


CS
DRAM
BDA


IO
kernel!The
BDA

No matching tags

Positivity     41.00%   
   Negativity   59.00%
The New York Times
SOURCE: https://www.moritz.systems/blog/before-the-bsd-kernel-starts-part-one-on-amd64/
Write a review: Hacker News
Summary

In both cases I will discuss the topic in the context of NetBSD, the operating system known for its portability.The CPU starting point is called the reset vector: the CPU bootstraps, then fetches and executes the first physical address at location 0xFFFFFFF0. Using this technique, the instruction pointer addresses relative to the last 64 KiB fragment of the physical memory, which is usually wired to read-only flash memory, where part of the platform firmware (BIOS/UEFI) is located.BIOS (Basic Input/Output System) is a term used for legacy platform initialization firmware and an interface between the operating system and platform. Describing UEFI and how it differs from BIOS is out of the scope of this article, but what is important to know is that both BIOS and UEFI based firmware will perform platform initialization, and later load the operating system from the physical medium. Once that is done, initialization code will search for a legacy operating system to boot, load the MBR sector from disk to the memory and execute it.BIOS loads the first sector, called the MBR (512 bytes), from the beginning of the hard disk. Before I describe the process of executing kernel and making it operational in the long mode, we need to know what the starting point of a typical UNIX kernel is.The two most common executable file formats are ELF (Executable and Linkable Format) and PE (Portable Executable). I don’t want go too deeper into disk layout as such a description would end up with multiple tables and descriptions, so I will focus on the NetBSD kernel initialization for the default configuration.After the BIOS finds a valid sector (with 0xAA55 signature), it loads the first disk sector (MBR) to physical address 0x7c00 . The boot program reads a kernel binary from the file system, interprets different sections, and loads them into memory. The parameters were placed on the stack by the boot program in the previous stage.The NetBSD start function executes in virtual mode (32-bit mode) and initializes the processor up to the point where it can switch to the long mode. So we have the start and end of the kernel image, and using that offset we need to calculate the following offsets for the next sections such as page tables, process zero stack and I/O memory for legacy devices that are getting mapped to virtual addresses but not allocated in the physical memory.

As said here by