Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default registers and segments value on booting x86 machine

I found that usually programmers fixup registers (and sometimes segments) on their very first lines of bootloaders and they advice usually to have this habit. For instance:

inc cx
dec bx
inc bp
dec di
xor ax, ax

What I know is that: BIOS clears all registers during the boot procedure!

Is it a good habit to initialize registers and segments in a bootloader? Why? What are the default registers, segments and pointers value (Maybe chipset dependent)?

like image 289
Jaafar Avatar asked Apr 12 '17 02:04

Jaafar


People also ask

What is FS register in x86?

FS is used to point to the thread information block (TIB) on windows processes . one typical example is (SEH) which store a pointer to a callback function in FS:[0x00] . GS is commonly used as a pointer to a thread local storage (TLS) .

What are segment registers used for?

A segment register changes the memory address accessed by 16 bits at a time, because its value is shifted left by 4 bits (or multiplied by 16) to cover the entire 20-bit address space. The segment register value is added to the addressing register's 16-bit value to produce the actual 20-bit memory address.

What is GS in x86?

In x86 family processors from 386 onwards, GS is one of the so-called segment registers. However, in protected mode environments segment registers work as selector registers. A virtual memory selector represents its own mapping of virtual address space together with its own access regime.

What is the SS register?

The stack segment register (SS) is usually used to store information about the memory segment that stores the call stack of currently executed program. SP points to current stack top. By default, the stack grows downward in memory, so newer values are placed at lower memory addresses.


1 Answers

Since you mention settings of segment registers and your code appears to be 16-bit code I will assume you are discussing legacy IBM-PC bootloaders (PC-BIOS) and not (EFI/UEFI). In legacy bootloaders for the majority of equipment that has been manufactured there is very little you can assume.

By the time the PC-BIOS loads the boot sector from an available boot device and transfer control to it the state of all the registers but one have a usable value. With the exception of some non-standard (and not 100% compatible BIOSes) from the 80s and 90s the register DL will contain the boot drive number the BIOS booted from. This value is also the one used to call Int 13h disk service routines.

SS:SP likely points to somewhere in RAM but where that is differs from BIOS to BIOS. One should setup their own stack pointer (SS and SP) especially if you intend to load data into memory. You could unintentionally overwrite the stack with data unless you specifically set it yourself.

Some argue that CS:IP are always set to 0x0000:0x7c00 (CS=0x0000, IP=0x7c00) when control is transferred to your bootloader (usually through a FAR JMP). Unfortunately this isn't guaranteed. Some bootloaders are known to use 0x07c0:0x0000 which also points to physical address 0x07c00 (0x07c0<<4+0x0000). This is because different segment:offset addressing can represent the same physical address (like 0x07c00). I wrote a Stackoverflow question/answer that captures a situation where assuming CS is always 0x0000 can lead to some interesting bugs depending on the environment.

The direction flag (DF in FLAGS register) used for the string instructions (like CMPS and MOVS) should not be assumed to be a particular direction. Most code uses forward movement (DF=0) but there is no guarantee that is the direction the BIOS set it to before jumping to the bootloader. Because of that one should explicitly clear it with CLD for forward movement or set it with STD for backward movement.

Besides the aforementioned DL register you should not assume any of the general purpose registers are initialized at all. Often I see bootloaders that assume they are zero. This is almost never the case.

Many of these things are discussed in my Stackoverflow General Bootloader Tips.

like image 83
Michael Petch Avatar answered Sep 20 '22 11:09

Michael Petch