Skip to main content

loader_defs/
linux.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Linux loader definitions.
5//!
6//! These structures are defined in the Linux kernel and can be found in the kernel docs under
7//! [`The Linux/x86 Boot Protocol`](https://www.kernel.org/doc/html/latest/x86/boot.html).
8
9#![expect(missing_docs)]
10#![expect(non_camel_case_types)]
11
12use bitfield_struct::bitfield;
13use static_assertions::const_assert_eq;
14use zerocopy::FromBytes;
15use zerocopy::FromZeros;
16use zerocopy::Immutable;
17use zerocopy::IntoBytes;
18use zerocopy::KnownLayout;
19
20mod packed_nums {
21    pub type u16_ne = zerocopy::U16<zerocopy::NativeEndian>;
22    pub type u32_ne = zerocopy::U32<zerocopy::NativeEndian>;
23    pub type u64_ne = zerocopy::U64<zerocopy::NativeEndian>;
24}
25use self::packed_nums::*;
26
27#[repr(C)]
28#[derive(Debug, Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
29pub struct apm_bios_info {
30    pub version: u16,
31    pub cseg: u16,
32    pub offset: u32,
33    pub cseg_16: u16,
34    pub dseg: u16,
35    pub flags: u16,
36    pub cseg_len: u16,
37    pub cseg_16_len: u16,
38    pub dseg_len: u16,
39}
40
41#[repr(C)]
42#[derive(Debug, Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
43pub struct screen_info {
44    pub orig_x: u8,
45    pub orig_y: u8,
46    pub ext_mem_k: u16,
47    pub orig_video_page: u16,
48    pub orig_video_mode: u8,
49    pub orig_video_cols: u8,
50    pub flags: u8,
51    pub unused2: u8,
52    pub orig_video_ega_bx: u16,
53    pub unused3: u16,
54    pub orig_video_lines: u8,
55    pub orig_video_is_vga: u8,
56    pub orig_video_points: u16,
57    pub lfb_width: u16,
58    pub lfb_height: u16,
59    pub lfb_depth: u16,
60    pub lfb_base: u32,
61    pub lfb_size: u32,
62    pub cl_magic: u16,
63    pub cl_offset: u16,
64    pub lfb_linelength: u16,
65    pub red_size: u8,
66    pub red_pos: u8,
67    pub green_size: u8,
68    pub green_pos: u8,
69    pub blue_size: u8,
70    pub blue_pos: u8,
71    pub rsvd_size: u8,
72    pub rsvd_pos: u8,
73    pub vesapm_seg: u16,
74    pub vesapm_off: u16,
75    pub pages: u16,
76    pub vesa_attributes: u16,
77    pub capabilities: u32_ne,
78    pub _reserved: [u8; 6],
79}
80
81#[repr(C)]
82#[derive(Debug, Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
83pub struct sys_desc_table {
84    pub length: u16,
85    pub table: [u8; 14],
86}
87
88#[repr(C)]
89#[derive(Debug, Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
90pub struct olpc_ofw_header {
91    pub ofw_magic: u32,
92    pub ofw_version: u32,
93    pub cif_handler: u32,
94    pub irq_desc_table: u32,
95}
96
97#[repr(C)]
98#[derive(Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
99pub struct edid_info {
100    pub dummy: [u8; 128],
101}
102
103#[repr(C)]
104#[derive(Debug, Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
105pub struct efi_info {
106    pub efi_loader_signature: u32,
107    pub efi_systab: u32,
108    pub efi_memdesc_size: u32,
109    pub efi_memdesc_version: u32,
110    pub efi_memmap: u32,
111    pub efi_memmap_size: u32,
112    pub efi_systab_hi: u32,
113    pub efi_memmap_hi: u32,
114}
115
116#[repr(C)]
117#[derive(Debug, Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
118pub struct setup_header {
119    pub setup_sects: u8,
120    pub root_flags: u16_ne,
121    pub syssize: u32_ne,
122    pub ram_size: u16_ne,
123    pub vid_mode: u16_ne,
124    pub root_dev: u16_ne,
125    pub boot_flag: u16_ne,
126    pub jump: u16_ne,
127    pub header: u32_ne,
128    pub version: u16_ne,
129    pub realmode_swtch: u32_ne,
130    pub start_sys: u16_ne,
131    pub kernel_version: u16_ne,
132    pub type_of_loader: u8,
133    pub loadflags: u8,
134    pub setup_move_size: u16_ne,
135    pub code32_start: u32_ne,
136    pub ramdisk_image: u32_ne,
137    pub ramdisk_size: u32_ne,
138    pub bootsect_kludge: u32_ne,
139    pub heap_end_ptr: u16_ne,
140    pub ext_loader_ver: u8,
141    pub ext_loader_type: u8,
142    pub cmd_line_ptr: u32_ne,
143    pub initrd_addr_max: u32_ne,
144    pub kernel_alignment: u32_ne,
145    pub relocatable_kernel: u8,
146    pub min_alignment: u8,
147    pub xloadflags: u16_ne,
148    pub cmdline_size: u32_ne,
149    pub hardware_subarch: u32_ne,
150    pub hardware_subarch_data: u64_ne,
151    pub payload_offset: u32_ne,
152    pub payload_length: u32_ne,
153    pub setup_data: u64_ne,
154    pub pref_address: u64_ne,
155    pub init_size: u32_ne,
156    pub handover_offset: u32_ne,
157}
158
159// TODO: zerocopy doesn't support const new methods, so define them as u32 for now. (https://github.com/microsoft/openvmm/issues/759)
160pub const E820_RAM: u32 = 1;
161pub const E820_RESERVED: u32 = 2;
162pub const E820_ACPI: u32 = 3;
163pub const E820_NVS: u32 = 4;
164pub const E820_UNUSABLE: u32 = 5;
165
166#[repr(C)]
167#[derive(Debug, Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
168pub struct e820entry {
169    pub addr: u64_ne,
170    pub size: u64_ne,
171    pub typ: u32_ne,
172}
173
174#[repr(C)]
175#[derive(Debug, Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
176pub struct edd_info {
177    pub device: u8,
178    pub version: u8,
179    pub interface_support: u16,
180    pub legacy_max_cylinder: u16,
181    pub legacy_max_head: u8,
182    pub legacy_sectors_per_track: u8,
183    pub params: edd_device_params,
184}
185
186#[repr(C)]
187#[derive(Debug, Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
188pub struct edd_device_params {
189    pub length: u16,
190    pub info_flags: u16,
191    pub num_default_cylinders: u32_ne,
192    pub num_default_heads: u32_ne,
193    pub sectors_per_track: u32_ne,
194    pub number_of_sectors: u64_ne,
195    pub bytes_per_sector: u16,
196    pub dpte_ptr: u32_ne,
197    pub key: u16,
198    pub device_path_info_length: u8,
199    pub reserved2: u8,
200    pub reserved3: u16,
201    pub host_bus_type: [u8; 4],
202    pub interface_type: [u8; 8],
203    pub interface_path: [u8; 8],
204    pub device_path: [u8; 16],
205    pub reserved4: u8,
206    pub checksum: u8,
207}
208
209#[repr(C)]
210#[derive(Debug, Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
211pub struct ist_info {
212    pub signature: u32,
213    pub command: u32,
214    pub event: u32,
215    pub perf_level: u32,
216}
217
218#[repr(C)]
219#[derive(Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
220pub struct boot_params {
221    pub screen_info: screen_info,
222    pub apm_bios_info: apm_bios_info,
223    pub _pad2: [u8; 4],
224    pub tboot_addr: u64,
225    pub ist_info: ist_info,
226    /// Physical address of the ACPI RSDP, at zero-page offset 0x070. When it
227    /// is nonzero, Linux 4.20 and later use it instead of searching for the
228    /// RSDP.
229    pub acpi_rsdp_addr: u64,
230    pub _pad3: [u8; 8],
231    pub hd0_info: [u8; 16],
232    pub hd1_info: [u8; 16],
233    pub sys_desc_table: sys_desc_table,
234    pub olpc_ofw_header: olpc_ofw_header,
235    pub ext_ramdisk_image: u32,
236    pub ext_ramdisk_size: u32,
237    pub ext_cmd_line_ptr: u32,
238    pub _pad4: [[u8; 29]; 4],
239    pub edid_info: edid_info,
240    pub efi_info: efi_info,
241    pub alt_mem_k: u32,
242    pub scratch: u32,
243    pub e820_entries: u8,
244    pub eddbuf_entries: u8,
245    pub edd_mbr_sig_buf_entries: u8,
246    pub kbd_status: u8,
247    pub _pad5: [u8; 3],
248    pub sentinel: u8,
249    pub _pad6: [u8; 1],
250    pub hdr: setup_header,
251    pub _pad7: [u8; 40],
252    pub edd_mbr_sig_buffer: [u32; 16],
253    pub e820_map: [e820entry; 128],
254    pub _pad8: [u8; 48],
255    pub eddbuf: [edd_info; 6],
256    pub _pad9: [[u8; 23]; 12],
257}
258
259impl Default for boot_params {
260    fn default() -> Self {
261        FromZeros::new_zeroed()
262    }
263}
264
265const_assert_eq!(size_of::<boot_params>(), 4096);
266
267// This must be aligned so that it doesn't straddle a page boundary.
268#[repr(C, align(16))]
269#[derive(Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
270pub struct setup_data {
271    pub next: u64,
272    pub ty: u32,
273    pub len: u32,
274}
275
276pub const SETUP_E820_EXT: u32 = 1;
277pub const SETUP_DTB: u32 = 2;
278pub const SETUP_CC_BLOB: u32 = 7;
279
280#[repr(C)]
281#[derive(Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
282pub struct cc_blob_sev_info {
283    pub magic: u32,
284    pub version: u16,
285    pub _reserved: u16,
286    pub secrets_phys: u64,
287    pub secrets_len: u32,
288    pub _rsvd1: u32,
289    pub cpuid_phys: u64,
290    pub cpuid_len: u32,
291    pub _rsvd2: u32,
292}
293
294pub const CC_BLOB_SEV_INFO_MAGIC: u32 = 0x45444d41;
295
296#[repr(C, align(16))]
297#[derive(Copy, Clone, IntoBytes, Immutable, KnownLayout, FromBytes)]
298pub struct cc_setup_data {
299    pub header: setup_data,
300    pub cc_blob_address: u32,
301    pub _padding: [u32; 3],
302}
303
304/// Magic value for a measured [`SnpBootShimParams`] page.
305pub const SNP_BOOT_SHIM_PARAMS_MAGIC: u64 = u64::from_le_bytes(*b"SNPBSHIM");
306/// Version of the [`SnpBootShimParams`] handoff ABI.
307pub const SNP_BOOT_SHIM_PARAMS_VERSION: u32 = 4;
308/// Magic value for the measured platform extension.
309pub const SNP_BOOT_SHIM_PLATFORM_MAGIC: u64 = u64::from_le_bytes(*b"SNPACPI3");
310/// Version of the platform extension.
311pub const SNP_BOOT_SHIM_PLATFORM_VERSION: u32 = 3;
312/// Maximum host device-tree size accepted by the fixed SNP profile.
313pub const SNP_BOOT_SHIM_DT_SIZE: u64 = 64 * 1024;
314/// Reserved capacity for the complete generated ACPI table set.
315pub const SNP_BOOT_SHIM_ACPI_SIZE: u64 = 64 * 1024;
316/// Temporary allocator capacity for bounded full ACPI generation.
317pub const SNP_BOOT_SHIM_HEAP_SIZE: u64 = 1024 * 1024;
318/// The stack that the shim gives to the Linux EFI stub. UEFI spec 2.3.4
319/// requires at least 128 KiB on x64.
320pub const SNP_BOOT_SHIM_EFI_STACK_SIZE: u64 = 256 * 1024;
321/// Maximum number of PCIe host bridges supported by the SNP bootshim.
322pub const SNP_BOOT_SHIM_MAX_PCIE_BRIDGES: usize = 8;
323/// Maximum number of CPUs in the host device tree.
324pub const SNP_BOOT_SHIM_MAX_CPUS: usize = 255;
325/// Maximum number of RAM ranges in the host device tree.
326pub const SNP_BOOT_SHIM_MAX_RAM_RANGES: usize = 64;
327/// Exclusive upper bound of the RAM that the shim can accept.
328pub const SNP_BOOT_SHIM_MAX_RAM_END: u64 = 512 << 30;
329const SNP_BOOT_SHIM_PARAMS_HEADER_SIZE: usize = 48;
330/// Maximum number of RAM ranges that fit in one [`SnpBootShimParams`] page.
331pub const SNP_BOOT_SHIM_MAX_RANGES: usize = (hvdef::HV_PAGE_SIZE as usize
332    - SNP_BOOT_SHIM_PARAMS_HEADER_SIZE)
333    / size_of::<SnpBootShimRange>();
334
335/// A contiguous range of private RAM for the SNP bootshim to accept.
336#[repr(C)]
337#[derive(Debug, Copy, Clone, Eq, PartialEq, IntoBytes, Immutable, KnownLayout, FromBytes)]
338pub struct SnpBootShimRange {
339    /// The first 4-KiB guest page number in the range.
340    pub start_gpn: u64,
341    /// The number of 4-KiB pages in the range.
342    pub page_count: u64,
343}
344
345/// The measured handoff from the IGVM generator to the SNP Linux bootshim.
346///
347/// The generator imports one page with this layout and starts the bootshim
348/// with RSI pointing to it. The bootshim validates the complete header and the
349/// active prefix of `ranges` before it uses any omitted RAM or enters Linux.
350#[repr(C, align(4096))]
351#[derive(Debug, Copy, Clone, Eq, PartialEq, IntoBytes, Immutable, KnownLayout, FromBytes)]
352pub struct SnpBootShimParams {
353    /// Must equal [`SNP_BOOT_SHIM_PARAMS_MAGIC`].
354    pub magic: u64,
355    /// Must equal [`SNP_BOOT_SHIM_PARAMS_VERSION`].
356    pub version: u32,
357    /// Number of valid entries at the start of `ranges`.
358    pub range_count: u32,
359    /// Exclusive end of the RAM that the image measures. The host device tree
360    /// describes this RAM and any RAM above it.
361    pub ram_end: u64,
362    /// Page-aligned GPA of the mandatory [`SnpBootShimPlatformParams`].
363    pub platform_gpa: u64,
364    /// Must be zero.
365    pub reserved: [u64; 2],
366    /// Sorted, non-overlapping RAM ranges omitted from measured page data.
367    ///
368    /// These pages are not accepted at launch. The shim accepts only the ones
369    /// that it and the Linux EFI stub need, and reports the rest to Linux as
370    /// unaccepted memory.
371    pub ranges: [SnpBootShimRange; SNP_BOOT_SHIM_MAX_RANGES],
372}
373
374/// Options for the SNP bootshim, set by the image generator.
375#[bitfield(u32)]
376#[derive(PartialEq, Eq, IntoBytes, Immutable, KnownLayout, FromBytes)]
377pub struct SnpBootShimPlatformFlags {
378    /// The shim accepts all RAM before it calls the Linux EFI stub, instead
379    /// of reporting the RAM that it does not need as unaccepted.
380    pub accept_all_memory: bool,
381    /// Must be zero.
382    #[bits(31)]
383    pub reserved: u32,
384}
385
386/// Measured locations and capacities for the shim's work and for the Linux
387/// EFI boot.
388///
389/// The device-tree contents are unmeasured host input. These bounds, unlike
390/// those contents, are part of the measured image and cannot be supplied by DT.
391/// Every range is page aligned. A size of zero means that the item is absent.
392#[repr(C)]
393#[derive(Debug, Copy, Clone, Eq, PartialEq, IntoBytes, Immutable, KnownLayout, FromBytes)]
394pub struct SnpBootShimPlatformParams {
395    pub magic: u64,
396    pub version: u32,
397    pub flags: SnpBootShimPlatformFlags,
398    /// The parameter area for the host device tree.
399    pub dt_gpa: u64,
400    pub dt_size: u64,
401    /// Temporary memory for building ACPI. It lies in `ranges`.
402    pub heap_gpa: u64,
403    pub heap_size: u64,
404    /// Zero-filled, imported storage for the ACPI tables.
405    pub acpi_output_gpa: u64,
406    pub acpi_output_size: u64,
407    /// A zero-filled, imported page for the RSDP.
408    pub rsdp_gpa: u64,
409    /// SNP page-table encryption bit; PCIe addresses must lie below this bit.
410    pub c_bit_mask: u64,
411    /// The stack for the Linux EFI stub. It lies in `ranges`.
412    pub stack_gpa: u64,
413    pub stack_size: u64,
414    /// Memory that the shim accepts and reports as free, so that the EFI stub
415    /// can place the decompressed kernel. It lies in `ranges`.
416    pub boot_pool_gpa: u64,
417    pub boot_pool_size: u64,
418    /// The kernel's PE/COFF image, laid out at its section addresses.
419    pub kernel_gpa: u64,
420    pub kernel_size: u64,
421    /// The address of the kernel's PE/COFF entry point.
422    pub kernel_entry: u64,
423    /// The initrd. `initrd_size` is the exact size in bytes.
424    pub initrd_gpa: u64,
425    pub initrd_size: u64,
426    /// The NUL-terminated UTF-16 command line. `cmdline_size` is the exact
427    /// size in bytes, including the NUL.
428    pub cmdline_gpa: u64,
429    pub cmdline_size: u64,
430    /// The SMBIOS 3.0 entry point, followed by the structure table that it
431    /// points to.
432    pub smbios_gpa: u64,
433    pub smbios_size: u64,
434    /// The SNP secrets, CPUID, and CC-blob pages.
435    pub secrets_gpa: u64,
436    pub cpuid_gpa: u64,
437    pub cc_blob_gpa: u64,
438}
439
440const_assert_eq!(size_of::<SnpBootShimPlatformParams>(), 208);
441const_assert_eq!(core::mem::offset_of!(boot_params, acpi_rsdp_addr), 0x70);
442
443const_assert_eq!(size_of::<SnpBootShimRange>(), 16);
444const_assert_eq!(align_of::<SnpBootShimRange>(), 8);
445const_assert_eq!(
446    core::mem::offset_of!(SnpBootShimParams, ranges),
447    SNP_BOOT_SHIM_PARAMS_HEADER_SIZE
448);
449const_assert_eq!(size_of::<SnpBootShimParams>(), hvdef::HV_PAGE_SIZE as usize);
450const_assert_eq!(
451    align_of::<SnpBootShimParams>(),
452    hvdef::HV_PAGE_SIZE as usize
453);