2020-04-24 00:36:01 +03:00
|
|
|
# stivale boot protocol specification
|
|
|
|
|
|
|
|
The stivale boot protocol aims to be a *simple* to implement protocol which
|
|
|
|
provides the kernel with most of the features one may need in a *modern*
|
2020-04-30 22:19:12 +03:00
|
|
|
x86_64 context (although 32-bit x86 is also supported).
|
2020-04-24 00:36:01 +03:00
|
|
|
|
|
|
|
## General information
|
|
|
|
|
|
|
|
In order to have a stivale compliant kernel, one must have a kernel executable
|
2020-04-30 22:19:12 +03:00
|
|
|
in the `elf64` or `elf32` format and have a `.stivalehdr` section (described below).
|
2020-04-24 00:36:01 +03:00
|
|
|
Other executable formats are not supported.
|
|
|
|
|
2020-04-30 22:19:12 +03:00
|
|
|
stivale will recognise whether the ELF file is 32-bit or 64-bit and load the kernel
|
|
|
|
into the appropriate CPU mode.
|
|
|
|
|
|
|
|
stivale natively supports (only for 64-bit kernels) and encourages higher half kernels.
|
2020-09-21 15:08:45 +03:00
|
|
|
The kernel can load itself at `0xffffffff80000000` (as defined in the linker script)
|
2020-04-24 00:36:01 +03:00
|
|
|
and the bootloader will take care of everything, no AT linker script directives needed.
|
|
|
|
|
2020-09-21 15:08:45 +03:00
|
|
|
If the kernel loads itself in the lower half, the bootloader will not perform the
|
|
|
|
higher half relocation.
|
2020-04-24 00:36:01 +03:00
|
|
|
|
2020-09-21 15:08:45 +03:00
|
|
|
*Note: In order to maintain compatibility with Limine and other stivale-compliant*
|
|
|
|
*bootloaders it is strongly advised never to load the kernel or any of its*
|
|
|
|
*sections below the 1 MiB physical memory mark. This may work with some stivale*
|
|
|
|
*loaders, but it WILL NOT work with Limine and it's explicitly discouraged.*
|
2020-04-24 00:36:01 +03:00
|
|
|
|
|
|
|
## Kernel entry machine state
|
|
|
|
|
2020-04-30 22:19:12 +03:00
|
|
|
### 64-bit kernel
|
|
|
|
|
2020-05-30 16:44:14 +03:00
|
|
|
`rip` will be the entry point as defined in the ELF file, unless the `entry_point`
|
|
|
|
field in the stivale header is set to a non-0 value, in which case, it is set to
|
|
|
|
the value of `entry_point`.
|
2020-04-24 00:36:01 +03:00
|
|
|
|
2020-09-11 17:35:14 +03:00
|
|
|
At entry, the bootloader will have setup paging mappings as such:
|
|
|
|
|
|
|
|
```
|
2020-09-18 11:57:38 +03:00
|
|
|
Base Physical Address - Size -> Virtual address
|
|
|
|
0x0000000000000000 - 4 GiB plus any additional memory map entry -> 0x0000000000000000
|
|
|
|
0x0000000000000000 - 4 GiB plus any additional memory map entry -> 0xffff800000000000 (4-level paging only)
|
|
|
|
0x0000000000000000 - 4 GiB plus any additional memory map entry -> 0xff00000000000000 (5-level paging only)
|
|
|
|
0x0000000000000000 - 0x80000000 -> 0xffffffff80000000
|
2020-09-11 17:35:14 +03:00
|
|
|
```
|
2020-04-24 00:36:01 +03:00
|
|
|
|
2020-12-28 01:51:27 +03:00
|
|
|
If the kernel is dynamic and not statically linked, the bootloader will relocate it,
|
|
|
|
potentially performing KASLR (as specified by the config).
|
2020-05-30 16:44:14 +03:00
|
|
|
|
2020-04-24 00:36:01 +03:00
|
|
|
The kernel should NOT modify the bootloader page tables, and it should only use them
|
|
|
|
to bootstrap its own virtual memory manager and its own page tables.
|
|
|
|
|
|
|
|
At entry all segment registers are loaded as 64 bit code/data segments, limits and
|
|
|
|
bases are ignored since this is Long Mode.
|
|
|
|
|
|
|
|
DO NOT reload segment registers or rely on the provided GDT. The kernel MUST load
|
|
|
|
its own GDT as soon as possible and not rely on the bootloader's.
|
|
|
|
|
|
|
|
The IDT is in an undefined state. Kernel must load its own.
|
|
|
|
|
|
|
|
IF flag, VM flag, and direction flag are cleared on entry. Other flags undefined.
|
|
|
|
|
|
|
|
PG is enabled (`cr0`), PE is enabled (`cr0`), PAE is enabled (`cr4`),
|
|
|
|
LME is enabled (`EFER`).
|
2020-08-11 18:53:37 +03:00
|
|
|
If stivale header flag bit 1 is set, then, if available, 5-level paging is enabled
|
|
|
|
(LA57 bit in `cr4`).
|
2020-04-24 00:36:01 +03:00
|
|
|
|
|
|
|
The A20 gate is enabled.
|
|
|
|
|
2020-06-01 05:47:55 +03:00
|
|
|
PIC/APIC IRQs are all masked.
|
|
|
|
|
2020-09-18 11:57:38 +03:00
|
|
|
`rsp` is set to the requested stack as per stivale header. If the requested value is
|
|
|
|
non-null, an invalid return address of 0 is pushed to the stack before jumping
|
|
|
|
to the kernel.
|
2020-04-24 00:36:01 +03:00
|
|
|
|
|
|
|
`rdi` will point to the stivale structure (described below).
|
|
|
|
|
2020-07-09 04:24:54 +03:00
|
|
|
All other general purpose registers are set to 0.
|
|
|
|
|
2020-04-30 22:19:12 +03:00
|
|
|
### 32-bit kernel
|
|
|
|
|
2020-05-30 16:44:14 +03:00
|
|
|
`eip` will be the entry point as defined in the ELF file, unless the `entry_point`
|
|
|
|
field in the stivale header is set to a non-0 value, in which case, it is set to
|
|
|
|
the value of `entry_point`.
|
2020-04-30 22:19:12 +03:00
|
|
|
|
|
|
|
At entry all segment registers are loaded as 32 bit code/data segments.
|
|
|
|
All segment bases are `0x00000000` and all limits are `0xffffffff`.
|
|
|
|
|
|
|
|
DO NOT reload segment registers or rely on the provided GDT. The kernel MUST load
|
|
|
|
its own GDT as soon as possible and not rely on the bootloader's.
|
|
|
|
|
|
|
|
The IDT is in an undefined state. Kernel must load its own.
|
|
|
|
|
|
|
|
IF flag, VM flag, and direction flag are cleared on entry. Other flags undefined.
|
|
|
|
|
|
|
|
PE is enabled (`cr0`).
|
|
|
|
|
|
|
|
The A20 gate is enabled.
|
|
|
|
|
2020-06-01 05:47:55 +03:00
|
|
|
PIC/APIC IRQs are all masked.
|
|
|
|
|
2020-09-18 11:57:38 +03:00
|
|
|
`esp` is set to the requested stack as per stivale header. An invalid return address
|
|
|
|
of 0 is pushed to the stack before jumping to the kernel.
|
2020-04-30 22:19:12 +03:00
|
|
|
|
|
|
|
A pointer to the stivale structure (described below) is pushed onto this stack
|
|
|
|
before the entry point is called.
|
|
|
|
|
2020-07-09 04:24:54 +03:00
|
|
|
All other general purpose registers are set to 0.
|
|
|
|
|
2020-09-21 15:08:45 +03:00
|
|
|
## Bootloader-reserved memory
|
|
|
|
|
|
|
|
In order for stivale to function, it needs to reserve memory areas for either internal
|
2020-12-10 10:47:37 +03:00
|
|
|
usage (such as page tables, GDT), or for kernel interfacing (such as returned
|
2020-09-21 15:08:45 +03:00
|
|
|
structures).
|
|
|
|
|
|
|
|
stivale ensures that none of these areas are found in any of the sections
|
|
|
|
marked as "usable" in the memory map.
|
|
|
|
|
|
|
|
The location of these areas may vary and it is implementation specific;
|
|
|
|
these areas may be in any non-usable memory map section, or in unmarked memory.
|
|
|
|
|
|
|
|
The OS must make sure to be done consuming bootloader information and services
|
|
|
|
before switching to its own address space, as unmarked memory areas in use by
|
|
|
|
the bootloader may become unavailable.
|
|
|
|
|
|
|
|
Once the OS is done needing the bootloader, memory map areas marked as "bootloader
|
2020-12-10 10:47:37 +03:00
|
|
|
reclaimable" may be used as usable memory. These areas are guaranteed to be
|
|
|
|
4096-byte aligned, and they are guaranteed to not overlap other sections of the memory map.
|
2020-09-21 15:08:45 +03:00
|
|
|
|
2020-04-24 00:36:01 +03:00
|
|
|
## stivale header (.stivalehdr)
|
|
|
|
|
|
|
|
The kernel executable shall have a section `.stivalehdr` which will contain
|
|
|
|
the header that the bootloader will parse.
|
|
|
|
|
|
|
|
Said header looks like this:
|
|
|
|
```c
|
|
|
|
struct stivale_header {
|
2020-09-18 11:57:38 +03:00
|
|
|
uint64_t stack; // This is the stack address which will be in ESP/RSP
|
2020-08-11 18:53:37 +03:00
|
|
|
// when the kernel is loaded.
|
2020-09-18 11:57:38 +03:00
|
|
|
// It can only be set to NULL for 64-bit kernels. 32-bit
|
|
|
|
// kernels are mandated to provide a vaild stack.
|
|
|
|
// 64-bit and 32-bit valid stacks must be at least 256 bytes
|
|
|
|
// in usable space and must be 16 byte aligned addresses.
|
2020-08-11 18:53:37 +03:00
|
|
|
|
|
|
|
uint16_t flags; // Flags
|
|
|
|
// bit 0 0 = text mode, 1 = graphics framebuffer mode
|
|
|
|
// bit 1 0 = 4-level paging, 1 = use 5-level paging (if
|
2020-12-28 01:51:27 +03:00
|
|
|
// available)
|
|
|
|
// Ignored if booting a 32-bit kernel.
|
|
|
|
// bit 2 Formerly used to indicate whether to enable KASLR,
|
|
|
|
// this flag is now reserved as KASLR is enabled in the
|
|
|
|
// bootloader configuration instead. Presently
|
|
|
|
// reserved and unused.
|
2020-08-11 18:53:37 +03:00
|
|
|
// All other bits undefined.
|
|
|
|
|
|
|
|
uint16_t framebuffer_width; // These 3 values are parsed if a graphics mode
|
|
|
|
uint16_t framebuffer_height; // is requested. If all values are set to 0
|
|
|
|
uint16_t framebuffer_bpp; // then the bootloader will pick the best possible
|
|
|
|
// video mode automatically (recommended).
|
|
|
|
uint64_t entry_point; // If not 0, this field will be jumped to at entry
|
|
|
|
// instead of the ELF entry point.
|
2020-04-24 00:36:01 +03:00
|
|
|
} __attribute__((packed));
|
|
|
|
```
|
|
|
|
|
|
|
|
## stivale structure
|
|
|
|
|
|
|
|
The stivale structure returned by the bootloader looks like this:
|
|
|
|
```c
|
|
|
|
struct stivale_struct {
|
2020-08-11 18:53:37 +03:00
|
|
|
uint64_t cmdline; // Pointer to a null-terminated cmdline
|
|
|
|
uint64_t memory_map_addr; // Pointer to the memory map (entries described below)
|
|
|
|
uint64_t memory_map_entries; // Count of memory map entries
|
|
|
|
uint64_t framebuffer_addr; // Address of the framebuffer and related info
|
|
|
|
uint16_t framebuffer_pitch;
|
|
|
|
uint16_t framebuffer_width;
|
|
|
|
uint16_t framebuffer_height;
|
|
|
|
uint16_t framebuffer_bpp;
|
|
|
|
uint64_t rsdp; // Pointer to the ACPI RSDP structure
|
|
|
|
uint64_t module_count; // Count of modules that stivale loaded according to config
|
|
|
|
uint64_t modules; // Pointer to the first entry in the linked list of modules (described below)
|
|
|
|
uint64_t epoch; // UNIX epoch at boot, read from system RTC
|
|
|
|
uint64_t flags; // Flags
|
|
|
|
// bit 0: 1 if booted with BIOS, 0 if booted with UEFI
|
2020-12-05 04:10:02 +03:00
|
|
|
// bit 1: 1 if extended colour information passed, 0 if not
|
2020-08-11 18:53:37 +03:00
|
|
|
// All other bits undefined.
|
2020-12-05 04:10:02 +03:00
|
|
|
// Extended colour information follows, only access if bit 1 of flags is set.
|
|
|
|
uint8_t fb_memory_model; // Memory model: 1=RGB, all other values undefined
|
|
|
|
uint8_t fb_red_mask_size; // RGB mask sizes and left shifts
|
|
|
|
uint8_t fb_red_mask_shift;
|
|
|
|
uint8_t fb_green_mask_size;
|
|
|
|
uint8_t fb_green_mask_shift;
|
|
|
|
uint8_t fb_blue_mask_size;
|
|
|
|
uint8_t fb_blue_mask_shift;
|
2020-08-11 18:43:39 +03:00
|
|
|
} __attribute__((packed));
|
|
|
|
```
|
|
|
|
|
2020-08-11 18:53:37 +03:00
|
|
|
## Memory map entry
|
2020-08-11 18:43:39 +03:00
|
|
|
|
|
|
|
```c
|
2020-08-11 18:53:37 +03:00
|
|
|
struct mmap_entry {
|
2020-04-24 00:52:47 +03:00
|
|
|
uint64_t base; // Base of the memory section
|
|
|
|
uint64_t length; // Length of the section
|
2020-08-11 18:53:37 +03:00
|
|
|
uint32_t type; // Type (described below)
|
2020-04-24 00:52:47 +03:00
|
|
|
uint32_t unused;
|
|
|
|
} __attribute__((packed));
|
|
|
|
```
|
|
|
|
|
|
|
|
`type` is an enumeration that can have the following values:
|
|
|
|
|
2020-06-06 19:47:21 +03:00
|
|
|
```
|
2020-09-21 15:08:45 +03:00
|
|
|
1 - Usable RAM
|
|
|
|
2 - Reserved
|
|
|
|
3 - ACPI reclaimable
|
|
|
|
4 - ACPI NVS
|
|
|
|
5 - Bad memory
|
|
|
|
10 - Kernel/Modules
|
|
|
|
0x1000 - Bootloader Reclaimable
|
2020-06-06 19:47:21 +03:00
|
|
|
```
|
2020-04-24 00:52:47 +03:00
|
|
|
|
|
|
|
All other values are undefined.
|
|
|
|
|
2020-06-06 18:09:52 +03:00
|
|
|
The kernel and modules loaded **are not** marked as usable memory. They are marked
|
2020-08-11 18:53:37 +03:00
|
|
|
as Kernel/Modules (type 10).
|
2020-06-06 18:09:52 +03:00
|
|
|
|
|
|
|
The entries are guaranteed to be sorted by base address, lowest to highest.
|
|
|
|
|
2020-12-10 10:47:37 +03:00
|
|
|
Usable and bootloader reclaimable entries are guaranteed to be 4096 byte aligned for both base and length.
|
|
|
|
|
|
|
|
Usable and bootloader reclaimable entries are guaranteed not to overlap with any other entry.
|
2020-06-06 18:09:52 +03:00
|
|
|
|
2020-12-10 10:47:37 +03:00
|
|
|
To the contrary, all non-usable entries (including kernel/modules) are not guaranteed any alignment, nor
|
|
|
|
is it guaranteed that they do not overlap other entries (except usable and bootloader reclaimable entries).
|
2020-06-06 18:09:52 +03:00
|
|
|
|
2020-08-11 18:53:37 +03:00
|
|
|
## Modules
|
2020-04-24 00:36:01 +03:00
|
|
|
|
2020-08-11 18:53:37 +03:00
|
|
|
The `modules` variable points to the first entry of the linked list of module
|
|
|
|
structures.
|
|
|
|
A module structure looks like this:
|
2020-04-24 00:36:01 +03:00
|
|
|
```c
|
|
|
|
struct stivale_module {
|
|
|
|
uint64_t begin; // Address where the module is loaded
|
|
|
|
uint64_t end; // End address of the module
|
2020-08-11 18:53:37 +03:00
|
|
|
char string[128]; // String passed to the module (by config file)
|
|
|
|
uint64_t next; // Pointer to the next module (if any), check module_count
|
|
|
|
// in the stivale_struct
|
2020-04-24 00:36:01 +03:00
|
|
|
} __attribute__((packed));
|
|
|
|
```
|