1
0
mirror of https://github.com/0xAX/linux-insides.git synced 2024-11-18 05:58:10 +00:00
linux-insides/Booting/linux-bootstrap-1.md

494 lines
26 KiB
Markdown
Raw Normal View History

2015-01-28 18:48:45 +00:00
Kernel booting process. Part 1.
2015-01-03 18:59:23 +00:00
================================================================================
From the bootloader to the kernel
2015-01-03 18:59:23 +00:00
--------------------------------------------------------------------------------
2016-08-04 00:30:34 +00:00
If you have been reading my previous [blog posts](http://0xax.blogspot.com/search/label/asm), then you can see that, for some time, I have been starting to get involved in low-level programming. I have written some posts about x86_64 assembly programming for Linux and, at the same time, I have also started to dive into the Linux source code. I have a great interest in understanding how low-level things work, how programs run on my computer, how are they located in memory, how the kernel manages processes & memory, how the network stack works at a low level, and many many other things. So, I have decided to write yet another series of posts about the Linux kernel for **x86_64**.
2015-01-03 20:31:14 +00:00
2016-08-04 00:30:34 +00:00
Note that I'm not a professional kernel hacker and I don't write code for the kernel at work. It's just a hobby. I just like low-level stuff, and it is interesting for me to see how these things work. So if you notice anything confusing, or if you have any questions/remarks, ping me on twitter [0xAX](https://twitter.com/0xAX), drop me an [email](anotherworldofworld@gmail.com) or just create an [issue](https://github.com/0xAX/linux-insides/issues/new). I appreciate it. All posts will also be accessible at [linux-insides](https://github.com/0xAX/linux-insides) and, if you find something wrong with my English or the post content, feel free to send a pull request.
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
*Note that this isn't official documentation, just learning and sharing knowledge.*
2015-01-03 18:59:23 +00:00
**Required knowledge**
* Understanding C code
* Understanding assembly code (AT&T syntax)
2016-08-04 00:30:34 +00:00
Anyway, if you just start to learn some tools, I will try to explain some parts during this and the following posts. Alright, this is the end of the simple introduction, and now we can start to dive into the kernel and low-level stuff.
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
All code is actually for the 3.18 kernel. If there are changes, I will update the posts accordingly.
2015-01-03 18:59:23 +00:00
The Magical Power Button, What happens next?
2015-01-03 18:59:23 +00:00
--------------------------------------------------------------------------------
2016-08-04 00:30:34 +00:00
Although this is a series of posts about the Linux kernel, we will not be starting from the kernel code - at least not, in this paragraph. As soon as you press the magical power button on your laptop or desktop computer, it starts working. The motherboard sends a signal to the [power supply](https://en.wikipedia.org/wiki/Power_supply). After receiving the signal, the power supply provides the proper amount of electricity to the computer. Once the motherboard receives the [power good signal](https://en.wikipedia.org/wiki/Power_good_signal), it tries to start the CPU. The CPU resets all leftover data in its registers and sets up predefined values for each of them.
2015-01-03 18:59:23 +00:00
2015-09-17 14:23:50 +00:00
[80386](https://en.wikipedia.org/wiki/Intel_80386) and later CPUs define the following predefined data in CPU registers after the computer resets:
2015-01-03 18:59:23 +00:00
```
IP 0xfff0
CS selector 0xf000
CS base 0xffff0000
```
The processor starts working in [real mode](https://en.wikipedia.org/wiki/Real_mode). Let's back up a little and try to understand memory segmentation in this mode. Real mode is supported on all x86-compatible processors, from the [8086](https://en.wikipedia.org/wiki/Intel_8086) all the way to the modern Intel 64-bit CPUs. The 8086 processor has a 20-bit address bus, which means that it could work with a 0-0xFFFFF address space (1 megabyte). But it only has 16-bit registers, which have a maximum address of 2^16 - 1 or 0xffff (64 kilobytes). [Memory segmentation](http://en.wikipedia.org/wiki/Memory_segmentation) is used to make use of all the address space available. All memory is divided into small, fixed-size segments of 65536 bytes (64 KB). Since we cannot address memory above 64 KB with 16 bit registers, an alternate method is devised. An address consists of two parts: a segment selector, which has a base address, and an offset from this base address. In real mode, the associated base address of a segment selector is `Segment Selector * 16`. Thus, to get a physical address in memory, we need to multiply the segment selector part by 16 and add the offset:
2015-01-03 18:59:23 +00:00
```
PhysicalAddress = Segment Selector * 16 + Offset
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
For example, if `CS:IP` is `0x2000:0x0010`, then the corresponding physical address will be:
2015-01-03 18:59:23 +00:00
```python
>>> hex((0x2000 << 4) + 0x0010)
'0x20010'
```
2016-08-04 00:30:34 +00:00
But, if we take the largest segment selector and offset, `0xffff:0xffff`, then the resulting address will be:
2015-01-03 18:59:23 +00:00
```python
>>> hex((0xffff << 4) + 0xffff)
'0x10ffef'
```
2016-08-04 00:30:34 +00:00
which is 65520 bytes past the first megabyte. Since only one megabyte is accessible in real mode, `0x10ffef` becomes `0x00ffef` with disabled [A20](https://en.wikipedia.org/wiki/A20_line).
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
Ok, now we know about real mode and memory addressing. Let's get back to discussing register values after reset:
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
The `CS` register consists of two parts: the visible segment selector, and the hidden base address. While the base address is normally formed by multiplying the segment selector value by 16, during a hardware reset the segment selector in the CS register is loaded with 0xf000 and the base address is loaded with 0xffff0000; the processor uses this special base address until `CS` is changed.
2015-01-03 18:59:23 +00:00
2015-08-30 20:37:59 +00:00
The starting address is formed by adding the base address to the value in the EIP register:
2015-01-03 18:59:23 +00:00
```python
2015-01-07 17:06:05 +00:00
>>> 0xffff0000 + 0xfff0
2015-01-03 18:59:23 +00:00
'0xfffffff0'
```
We get `0xfffffff0`, which is 16 bytes below 4GB. This point is called the [Reset vector](http://en.wikipedia.org/wiki/Reset_vector). This is the memory location at which the CPU expects to find the first instruction to execute after reset. It contains a [jump](http://en.wikipedia.org/wiki/JMP_%28x86_instruction%29) (`jmp`) instruction that usually points to the BIOS entry point. For example, if we look in the [coreboot](http://www.coreboot.org/) source code, we see:
2015-01-03 18:59:23 +00:00
```assembly
.section ".reset"
.code16
.globl reset_vector
2015-01-03 18:59:23 +00:00
reset_vector:
.byte 0xe9
.int _start - ( . + 2 )
...
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
Here we can see the `jmp` instruction [opcode](http://ref.x86asm.net/coder32.html#xE9), which is 0xe9, and its destination address at `_start - ( . + 2)`. We can also see that the `reset` section is 16 bytes, and that it starts at `0xfffffff0`:
2015-01-03 18:59:23 +00:00
```
SECTIONS {
_ROMTOP = 0xfffffff0;
. = _ROMTOP;
.reset . : {
*(.reset)
. = 15 ;
BYTE(0x00);
}
2015-01-03 18:59:23 +00:00
}
```
2017-03-19 20:08:18 +00:00
Now the BIOS starts; after initializing and checking the hardware, the BIOS needs to find a bootable device. A boot order is stored in the BIOS configuration, controlling which devices the BIOS attempts to boot from. When attempting to boot from a hard drive, the BIOS tries to find a boot sector. On hard drives partitioned with an MBR partition layout, the boot sector is stored in the first 446 bytes of the first sector, where each sector is 512 bytes. The final two bytes of the first sector are `0x55` and `0xaa`, which designates to the BIOS that this device is bootable. For example:
2015-01-03 18:59:23 +00:00
```assembly
;
; Note: this example is written in Intel Assembly syntax
;
2015-01-03 18:59:23 +00:00
[BITS 16]
[ORG 0x7c00]
boot:
2015-01-04 17:05:22 +00:00
mov al, '!'
2015-01-03 18:59:23 +00:00
mov ah, 0x0e
mov bh, 0x00
mov bl, 0x07
2015-01-04 13:57:42 +00:00
int 0x10
2015-01-03 18:59:23 +00:00
jmp $
times 510-($-$$) db 0
2015-01-04 17:05:22 +00:00
2015-01-03 18:59:23 +00:00
db 0x55
2015-01-04 17:05:22 +00:00
db 0xaa
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
Build and run this with:
2015-01-03 18:59:23 +00:00
```
nasm -f bin boot.nasm && qemu-system-x86_64 boot
```
2016-08-04 00:30:34 +00:00
This will instruct [QEMU](http://qemu.org) to use the `boot` binary that we just built as a disk image. Since the binary generated by the assembly code above fulfills the requirements of the boot sector (the origin is set to `0x7c00` and we end with the magic sequence), QEMU will treat the binary as the master boot record (MBR) of a disk image.
2015-02-28 12:23:52 +00:00
2015-08-30 20:37:59 +00:00
You will see:
2015-01-03 18:59:23 +00:00
![Simple bootloader which prints only `!`](http://oi60.tinypic.com/2qbwup0.jpg)
2016-08-04 00:30:34 +00:00
In this example we can see that the code will be executed in 16 bit real mode and will start at `0x7c00` in memory. After starting, it calls the [0x10](http://www.ctyme.com/intr/rb-0106.htm) interrupt, which just prints the `!` symbol; it fills the remaining 510 bytes with zeros and finishes with the two magic bytes `0xaa` and `0x55`.
2015-02-10 16:58:32 +00:00
2016-08-04 00:30:34 +00:00
You can see a binary dump of this using the `objdump` utility:
2015-02-10 16:58:32 +00:00
```
nasm -f bin boot.nasm
2015-02-10 16:58:32 +00:00
objdump -D -b binary -mi386 -Maddr16,data16,intel boot
```
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
A real-world boot sector has code for continuing the boot process and a partition table instead of a bunch of 0's and an exclamation mark :) From this point onwards, the BIOS hands over control to the bootloader.
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
**NOTE**: As explained above, the CPU is in real mode; in real mode, calculating the physical address in memory is done as follows:
2015-01-03 18:59:23 +00:00
```
PhysicalAddress = Segment Selector * 16 + Offset
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
just as explained before. We have only 16 bit general purpose registers; the maximum value of a 16 bit register is `0xffff`, so if we take the largest values, the result will be:
2015-01-03 18:59:23 +00:00
```python
>>> hex((0xffff * 16) + 0xffff)
'0x10ffef'
```
2016-08-04 00:30:34 +00:00
where `0x10ffef` is equal to `1MB + 64KB - 16b`. A [8086](https://en.wikipedia.org/wiki/Intel_8086) processor (which was the first processor with real mode), in contrast, has a 20 bit address line. Since `2^20 = 1048576` is 1MB, this means that the actual available memory is 1MB.
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
General real mode's memory map is as follows:
2015-01-03 18:59:23 +00:00
```
0x00000000 - 0x000003FF - Real Mode Interrupt Vector Table
0x00000400 - 0x000004FF - BIOS Data Area
0x00000500 - 0x00007BFF - Unused
0x00007C00 - 0x00007DFF - Our Bootloader
0x00007E00 - 0x0009FFFF - Unused
0x000A0000 - 0x000BFFFF - Video RAM (VRAM) Memory
0x000B0000 - 0x000B7777 - Monochrome Video Memory
0x000B8000 - 0x000BFFFF - Color Video Memory
0x000C0000 - 0x000C7FFF - Video ROM BIOS
0x000C8000 - 0x000EFFFF - BIOS Shadow Area
0x000F0000 - 0x000FFFFF - System BIOS
```
2016-08-04 00:30:34 +00:00
In the beginning of this post, I wrote that the first instruction executed by the CPU is located at address `0xFFFFFFF0`, which is much larger than `0xFFFFF` (1MB). How can the CPU access this address in real mode? The answer is in the [coreboot](http://www.coreboot.org/Developer_Manual/Memory_map) documentation:
2015-01-03 18:59:23 +00:00
```
2015-01-04 05:49:11 +00:00
0xFFFE_0000 - 0xFFFF_FFFF: 128 kilobyte ROM mapped into address space
2015-01-03 18:59:23 +00:00
```
2015-09-17 14:23:50 +00:00
At the start of execution, the BIOS is not in RAM, but in ROM.
2015-01-03 18:59:23 +00:00
Bootloader
--------------------------------------------------------------------------------
2016-08-04 00:30:34 +00:00
There are a number of bootloaders that can boot Linux, such as [GRUB 2](https://www.gnu.org/software/grub/) and [syslinux](http://www.syslinux.org/wiki/index.php/The_Syslinux_Project). The Linux kernel has a [Boot protocol](https://github.com/torvalds/linux/blob/master/Documentation/x86/boot.txt) which specifies the requirements for a bootloader to implement Linux support. This example will describe GRUB 2.
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
Continuing from before, now that the BIOS has chosen a boot device and transferred control to the boot sector code, execution starts from [boot.img](http://git.savannah.gnu.org/gitweb/?p=grub.git;a=blob;f=grub-core/boot/i386/pc/boot.S;hb=HEAD). This code is very simple, due to the limited amount of space available, and contains a pointer which is used to jump to the location of GRUB 2's core image. The core image begins with [diskboot.img](http://git.savannah.gnu.org/gitweb/?p=grub.git;a=blob;f=grub-core/boot/i386/pc/diskboot.S;hb=HEAD), which is usually stored immediately after the first sector in the unused space before the first partition. The above code loads the rest of the core image, which contains GRUB 2's kernel and drivers for handling filesystems, into memory. After loading the rest of the core image, it executes [grub_main](http://git.savannah.gnu.org/gitweb/?p=grub.git;a=blob;f=grub-core/kern/main.c).
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
`grub_main` initializes the console, gets the base address for modules, sets the root device, loads/parses the grub configuration file, loads modules, etc. At the end of execution, `grub_main` moves grub to normal mode. `grub_normal_execute` (from `grub-core/normal/main.c`) completes the final preparations and shows a menu to select an operating system. When we select one of the grub menu entries, `grub_menu_execute_entry` runs, executing the grub `boot` command and booting the selected operating system.
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
As we can read in the kernel boot protocol, the bootloader must read and fill some fields of the kernel setup header, which starts at the `0x01f1` offset from the kernel setup code. The kernel header [arch/x86/boot/header.S](https://github.com/torvalds/linux/blob/master/arch/x86/boot/header.S) starts from:
2015-01-03 18:59:23 +00:00
```assembly
.globl hdr
2015-01-03 18:59:23 +00:00
hdr:
setup_sects: .byte 0
root_flags: .word ROOT_RDONLY
syssize: .long 0
ram_size: .word 0
vid_mode: .word SVGA_MODE
root_dev: .word 0
boot_flag: .word 0xAA55
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
The bootloader must fill this and the rest of the headers (which are only marked as being type `write` in the Linux boot protocol, such as in [this example](https://github.com/torvalds/linux/blob/master/Documentation/x86/boot.txt#L354)) with values which it has either received from the command line or calculated. (We will not go over full descriptions and explanations for all fields of the kernel setup header now but instead when the discuss how kernel uses them; you can find a description of all fields in the [boot protocol](https://github.com/torvalds/linux/blob/master/Documentation/x86/boot.txt#L156).)
2015-01-03 18:59:23 +00:00
2015-08-30 20:37:59 +00:00
As we can see in the kernel boot protocol, the memory map will be the following after loading the kernel:
2015-01-03 18:59:23 +00:00
```shell
| Protected-mode kernel |
100000 +------------------------+
| I/O memory hole |
0A0000 +------------------------+
| Reserved for BIOS | Leave as much as possible unused
~ ~
| Command line | (Can also be below the X+10000 mark)
X+10000 +------------------------+
| Stack/heap | For use by the kernel real-mode code.
X+08000 +------------------------+
| Kernel setup | The kernel real-mode code.
| Kernel boot sector | The kernel legacy boot sector.
X +------------------------+
| Boot loader | <- Boot sector entry point 0x7C00
001000 +------------------------+
| Reserved for MBR/BIOS |
000800 +------------------------+
| Typically used by MBR |
000600 +------------------------+
| BIOS use only |
000000 +------------------------+
```
2016-08-04 00:30:34 +00:00
So, when the bootloader transfers control to the kernel, it starts at:
2015-01-03 18:59:23 +00:00
```
X + sizeof(KernelBootSector) + 1
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
where `X` is the address of the kernel boot sector being loaded. In my case, `X` is `0x10000`, as we can see in a memory dump:
2015-01-03 18:59:23 +00:00
![kernel first address](http://oi57.tinypic.com/16bkco2.jpg)
2016-08-04 00:30:34 +00:00
The bootloader has now loaded the Linux kernel into memory, filled the header fields, and then jumped to the corresponding memory address. We can now move directly to the kernel setup code.
2015-01-03 18:59:23 +00:00
Start of Kernel Setup
2015-01-03 18:59:23 +00:00
--------------------------------------------------------------------------------
2016-08-04 00:30:34 +00:00
Finally, we are in the kernel! Technically, the kernel hasn't run yet; first, we need to set up the kernel, memory manager, process manager, etc. Kernel setup execution starts from [arch/x86/boot/header.S](https://github.com/torvalds/linux/blob/master/arch/x86/boot/header.S) at [_start](https://github.com/torvalds/linux/blob/master/arch/x86/boot/header.S#L293). It is a little strange at first sight, as there are several instructions before it.
2016-08-04 00:30:34 +00:00
A long time ago, the Linux kernel used to have its own bootloader. Now, however, if you run, for example,
2015-01-03 18:59:23 +00:00
```
qemu-system-x86_64 vmlinuz-3.18-generic
```
2016-08-04 00:30:34 +00:00
then you will see:
2015-01-03 18:59:23 +00:00
![Try vmlinuz in qemu](http://oi60.tinypic.com/r02xkz.jpg)
2016-08-04 00:30:34 +00:00
Actually, `header.S` starts from [MZ](https://en.wikipedia.org/wiki/DOS_MZ_executable) (see image above), the error message printing and following the [PE](https://en.wikipedia.org/wiki/Portable_Executable) header:
2015-01-03 18:59:23 +00:00
```assembly
#ifdef CONFIG_EFI_STUB
# "MZ", MS-DOS header
.byte 0x4d
.byte 0x5a
#endif
...
...
...
pe_header:
.ascii "PE"
.word 0
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
It needs this to load an operating system with [UEFI](https://en.wikipedia.org/wiki/Unified_Extensible_Firmware_Interface). We won't be looking into its inner workings right now and will cover it in upcoming chapters.
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
The actual kernel setup entry point is:
2015-01-03 18:59:23 +00:00
```assembly
2015-01-03 18:59:23 +00:00
// header.S line 292
.globl _start
_start:
```
2016-08-04 00:30:34 +00:00
The bootloader (grub2 and others) knows about this point (`0x200` offset from `MZ`) and makes a jump directly to it, despite the fact that `header.S` starts from the `.bstext` section, which prints an error message:
2015-01-03 18:59:23 +00:00
```
//
// arch/x86/boot/setup.ld
//
. = 0; // current position
.bstext : { *(.bstext) } // put .bstext section to position 0
.bsdata : { *(.bsdata) }
```
2016-08-04 00:30:34 +00:00
The kernel setup entry point is:
2015-01-03 18:59:23 +00:00
```assembly
.globl _start
2015-01-03 18:59:23 +00:00
_start:
.byte 0xeb
.byte start_of_setup-1f
2015-01-03 18:59:23 +00:00
1:
//
// rest of the header
//
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
Here we can see a `jmp` instruction opcode (`0xeb`) that jumps to the `start_of_setup-1f` point. In `Nf` notation, `2f` refers to the following local `2:` label; in our case, it is label `1` that is present right after jump, and it contains the rest of the setup [header](https://github.com/torvalds/linux/blob/master/Documentation/x86/boot.txt#L156). Right after the setup header, we see the `.entrytext` section, which starts at the `start_of_setup` label.
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
This is the first code that actually runs (aside from the previous jump instructions, of course). After the kernel setup received control from the bootloader, the first `jmp` instruction is located at the `0x200` offset from the start of the kernel real mode, i.e., after the first 512 bytes. This we can both read in the Linux kernel boot protocol and see in the grub2 source code:
2015-01-03 18:59:23 +00:00
```C
segment = grub_linux_real_target >> 4;
state.gs = state.fs = state.es = state.ds = state.ss = segment;
state.cs = segment + 0x20;
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
This means that segment registers will have the following values after kernel setup starts:
2015-01-03 18:59:23 +00:00
```
2015-09-20 18:51:17 +00:00
gs = fs = es = ds = ss = 0x1000
2015-01-03 18:59:23 +00:00
cs = 0x1020
```
2016-08-04 00:30:34 +00:00
In my case, the kernel is loaded at `0x10000`.
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
After the jump to `start_of_setup`, the kernel needs to do the following:
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
* Make sure that all segment register values are equal
* Set up a correct stack, if needed
* Set up [bss](https://en.wikipedia.org/wiki/.bss)
2016-08-04 00:30:34 +00:00
* Jump to the C code in [main.c](https://github.com/torvalds/linux/blob/master/arch/x86/boot/main.c)
2015-01-03 18:59:23 +00:00
2015-08-30 20:37:59 +00:00
Let's look at the implementation.
2015-01-03 18:59:23 +00:00
2015-01-05 06:00:13 +00:00
Segment registers align
2015-01-03 18:59:23 +00:00
--------------------------------------------------------------------------------
2016-08-04 00:30:34 +00:00
First of all, the kernel ensures that `ds` and `es` segment registers point to the same address. Next, it clears the direction flag using the `cld` instruction:
2015-01-03 18:59:23 +00:00
```assembly
movw %ds, %ax
movw %ax, %es
cld
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
As I wrote earlier, grub2 loads kernel setup code at address `0x10000` and `cs` at `0x1020` because execution doesn't start from the start of file, but from
2015-01-03 18:59:23 +00:00
```assembly
2015-01-03 18:59:23 +00:00
_start:
.byte 0xeb
.byte start_of_setup-1f
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
`jump`, which is at a 512 byte offset from [4d 5a](https://github.com/torvalds/linux/blob/master/arch/x86/boot/header.S#L47). It also needs to align `cs` from `0x10200` to `0x10000`, as well as all other segment registers. After that, we set up the stack:
2015-01-03 18:59:23 +00:00
```assembly
pushw %ds
pushw $6f
lretw
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
which pushes the value of `ds` to the stack with the address of the [6](https://github.com/torvalds/linux/blob/master/arch/x86/boot/header.S#L494) label and executes the `lretw` instruction. When the `lretw` instruction is called, it loads the address of label `6` into the [instruction pointer](https://en.wikipedia.org/wiki/Program_counter) register and loads `cs` with the value of `ds`. Afterwards, `ds` and `cs` will have the same values.
2015-01-03 18:59:23 +00:00
Stack Setup
2015-01-03 18:59:23 +00:00
--------------------------------------------------------------------------------
2016-08-04 00:30:34 +00:00
Almost all of the setup code is in preparation for the C language environment in real mode. The next [step](https://github.com/torvalds/linux/blob/master/arch/x86/boot/header.S#L467) is checking the `ss` register value and making a correct stack if `ss` is wrong:
2015-01-03 18:59:23 +00:00
```assembly
movw %ss, %dx
cmpw %ax, %dx
movw %sp, %dx
je 2f
2015-01-03 18:59:23 +00:00
```
2015-08-30 20:37:59 +00:00
This can lead to 3 different scenarios:
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
* `ss` has valid value 0x10000 (as do all other segment registers beside `cs`)
2015-01-05 06:00:13 +00:00
* `ss` is invalid and `CAN_USE_HEAP` flag is set (see below)
* `ss` is invalid and `CAN_USE_HEAP` flag is not set (see below)
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
Let's look at all three of these scenarios in turn:
2015-01-03 18:59:23 +00:00
2016-08-04 00:30:34 +00:00
* `ss` has a correct address (0x10000). In this case, we go to label [2](https://github.com/torvalds/linux/blob/master/arch/x86/boot/header.S#L481):
2015-01-03 18:59:23 +00:00
```assembly
2: andw $~3, %dx
jnz 3f
movw $0xfffc, %dx
3: movw %ax, %ss
movzwl %dx, %esp
sti
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
Here we can see the alignment of `dx` (contains `sp` given by bootloader) to 4 bytes and a check for whether or not it is zero. If it is zero, we put `0xfffc` (4 byte aligned address before the maximum segment size of 64 KB) in `dx`. If it is not zero, we continue to use `sp`, given by the bootloader (0xf7f4 in my case). After this, we put the `ax` value into `ss`, which stores the correct segment address of `0x10000` and sets up a correct `sp`. We now have a correct stack:
2015-01-03 18:59:23 +00:00
![stack](http://oi58.tinypic.com/16iwcis.jpg)
2016-08-04 00:30:34 +00:00
* In the second scenario, (`ss` != `ds`). First, we put the value of [_end](https://github.com/torvalds/linux/blob/master/arch/x86/boot/setup.ld#L52) (the address of the end of the setup code) into `dx` and check the `loadflags` header field using the `testb` instruction to see whether we can use the heap. [loadflags](https://github.com/torvalds/linux/blob/master/arch/x86/boot/header.S#L321) is a bitmask header which is defined as:
2015-01-03 18:59:23 +00:00
```C
#define LOADED_HIGH (1<<0)
#define QUIET_FLAG (1<<5)
#define KEEP_SEGMENTS (1<<6)
#define CAN_USE_HEAP (1<<7)
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
and, as we can read in the boot protocol,
2015-01-03 18:59:23 +00:00
```
Field name: loadflags
2015-01-03 18:59:23 +00:00
This field is a bitmask.
Bit 7 (write): CAN_USE_HEAP
Set this bit to 1 to indicate that the value entered in the
heap_end_ptr is valid. If this field is clear, some setup code
functionality will be disabled.
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
If the `CAN_USE_HEAP` bit is set, we put `heap_end_ptr` into `dx` (which points to `_end`) and add `STACK_SIZE` (minimum stack size, 512 bytes) to it. After this, if `dx` is not carried (it will not be carried, dx = _end + 512), jump to label `2` (as in the previous case) and make a correct stack.
2015-01-03 18:59:23 +00:00
![stack](http://oi62.tinypic.com/dr7b5w.jpg)
2015-12-28 11:15:16 +00:00
* When `CAN_USE_HEAP` is not set, we just use a minimal stack from `_end` to `_end + STACK_SIZE`:
2015-01-03 18:59:23 +00:00
![minimal stack](http://oi60.tinypic.com/28w051y.jpg)
BSS Setup
2015-01-03 18:59:23 +00:00
--------------------------------------------------------------------------------
2016-08-04 00:30:34 +00:00
The last two steps that need to happen before we can jump to the main C code are setting up the [BSS](https://en.wikipedia.org/wiki/.bss) area and checking the "magic" signature. First, signature checking:
2015-01-03 18:59:23 +00:00
```assembly
cmpl $0x5a5aaa55, setup_sig
jne setup_bad
2015-01-03 18:59:23 +00:00
```
2015-08-30 20:37:59 +00:00
This simply compares the [setup_sig](https://github.com/torvalds/linux/blob/master/arch/x86/boot/setup.ld#L39) with the magic number `0x5a5aaa55`. If they are not equal, a fatal error is reported.
2015-01-03 18:59:23 +00:00
2015-08-30 20:37:59 +00:00
If the magic number matches, knowing we have a set of correct segment registers and a stack, we only need to set up the BSS section before jumping into the C code.
2016-08-04 00:30:34 +00:00
The BSS section is used to store statically allocated, uninitialized data. Linux carefully ensures this area of memory is first zeroed using the following code:
2015-01-03 18:59:23 +00:00
```assembly
movw $__bss_start, %di
movw $_end+3, %cx
xorl %eax, %eax
subw %di, %cx
shrw $2, %cx
rep; stosl
2015-01-03 18:59:23 +00:00
```
2016-08-04 00:30:34 +00:00
First, the [__bss_start](https://github.com/torvalds/linux/blob/master/arch/x86/boot/setup.ld#L47) address is moved into `di`. Next, the `_end + 3` address (+3 - aligns to 4 bytes) is moved into `cx`. The `eax` register is cleared (using a `xor` instruction), and the bss section size (`cx`-`di`) is calculated and put into `cx`. Then, `cx` is divided by four (the size of a 'word'), and the `stosl` instruction is used repeatedly, storing the value of `eax` (zero) into the address pointed to by `di`, automatically increasing `di` by four, repeating until `cx` reaches zero). The net effect of this code is that zeros are written through all words in memory from `__bss_start` to `_end`:
2015-01-03 18:59:23 +00:00
![bss](http://oi59.tinypic.com/29m2eyr.jpg)
Jump to main
--------------------------------------------------------------------------------
2016-08-04 00:30:34 +00:00
That's all - we have the stack and BSS, so we can jump to the `main()` C function:
2015-01-03 18:59:23 +00:00
```assembly
calll main
2015-01-03 18:59:23 +00:00
```
The `main()` function is located in [arch/x86/boot/main.c](https://github.com/torvalds/linux/blob/master/arch/x86/boot/main.c). You can read about what this does in the next part.
2015-01-03 18:59:23 +00:00
Conclusion
--------------------------------------------------------------------------------
2016-08-04 00:30:34 +00:00
This is the end of the first part about Linux kernel insides. If you have questions or suggestions, ping me on twitter [0xAX](https://twitter.com/0xAX), drop me an [email](anotherworldofworld@gmail.com), or just create an [issue](https://github.com/0xAX/linux-internals/issues/new). In the next part, we will see the first C code that executes in the Linux kernel setup, the implementation of memory routines such as `memset`, `memcpy`, `earlyprintk`, early console implementation and initialization, and much more.
2015-01-03 18:59:23 +00:00
2016-01-05 03:01:32 +00:00
**Please note that English is not my first language and I am really sorry for any inconvenience. If you find any mistakes please send me PR to [linux-insides](https://github.com/0xAX/linux-internals).**
2015-01-03 18:59:23 +00:00
2015-07-03 07:34:21 +00:00
Links
2015-01-03 18:59:23 +00:00
--------------------------------------------------------------------------------
* [Intel 80386 programmer's reference manual 1986](http://css.csail.mit.edu/6.858/2014/readings/i386.pdf)
* [Minimal Boot Loader for Intel® Architecture](https://www.cs.cmu.edu/~410/doc/minimal_boot.pdf)
* [8086](http://en.wikipedia.org/wiki/Intel_8086)
* [80386](http://en.wikipedia.org/wiki/Intel_80386)
* [Reset vector](http://en.wikipedia.org/wiki/Reset_vector)
* [Real mode](http://en.wikipedia.org/wiki/Real_mode)
* [Linux kernel boot protocol](https://www.kernel.org/doc/Documentation/x86/boot.txt)
* [CoreBoot developer manual](http://www.coreboot.org/Developer_Manual)
* [Ralf Brown's Interrupt List](http://www.ctyme.com/intr/int.htm)
* [Power supply](http://en.wikipedia.org/wiki/Power_supply)
* [Power good signal](http://en.wikipedia.org/wiki/Power_good_signal)