Skip to main content

openhcl_boot/arch/x86_64/
mod.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#![cfg(target_arch = "x86_64")]
5
6//! x86_64 architecture-specific implementations.
7
8mod address_space;
9pub mod hypercall;
10mod memory;
11pub mod snp;
12pub mod tdx;
13mod vp;
14mod vsm;
15
16use crate::host_params::shim_params::IsolationType;
17#[cfg(feature = "cvm_boot_log")]
18use crate::host_params::shim_params::ShimParams;
19pub use address_space::TdxHypercallPage;
20pub use memory::setup_vtl2_memory;
21pub use memory::verify_imported_regions_hash;
22use safe_intrinsics::cpuid;
23pub use vp::setup_vtl2_vp;
24pub use vsm::get_isolation_type;
25use x86defs::cpuid::CpuidFunction;
26
27pub fn physical_address_bits(isolation: IsolationType) -> u8 {
28    if isolation.is_hardware_isolated() {
29        unimplemented!("can't trust host cpuid");
30    }
31    const DEFAULT_PHYSICAL_ADDRESS_SIZE: u8 = 32;
32
33    let max_extended = {
34        let result = cpuid(CpuidFunction::ExtendedMaxFunction.0, 0);
35        result.eax
36    };
37    if max_extended >= CpuidFunction::ExtendedAddressSpaceSizes.0 {
38        let result = cpuid(CpuidFunction::ExtendedAddressSpaceSizes.0, 0);
39        (result.eax & 0xFF) as u8
40    } else {
41        DEFAULT_PHYSICAL_ADDRESS_SIZE
42    }
43}
44
45/// Perform any architecture and isolation-specific initialization required
46/// before the boot shim can use serial logging. For SNP, this sets up the
47/// GHCB page so that IOIO exits can be used for port I/O.
48#[cfg(feature = "cvm_boot_log")]
49pub fn initialize_serial_io(p: &ShimParams) {
50    if p.isolation_type == IsolationType::Snp {
51        snp::Ghcb::initialize();
52    }
53}
54
55/// Tear down architecture and isolation-specific state set up by
56/// [`initialize_serial_io`]. For SNP, this restores the GHCB page to its
57/// original private/accepted state.
58#[cfg(feature = "cvm_boot_log")]
59pub fn uninitialize_serial_io(p: &ShimParams) {
60    if p.isolation_type == IsolationType::Snp {
61        snp::Ghcb::uninitialize();
62    }
63}
64
65/// Zeroes BSS, installs the stack, enables SSE, relocates the image, and enters Rust.
66///
67/// # Safety
68/// Must be entered with the boot shim register state and the image identity mapped.
69// TODO: Use extern "custom" once supported by the minimum Rust version.
70#[cfg(minimal_rt)]
71// SAFETY: The minimal_rt boot shim has no CRT entry point; this is its sole `_start`.
72#[unsafe(no_mangle)]
73// SAFETY: Assembly owns stack initialization and preserves the boot arguments
74// until it transfers control to Rust without returning.
75#[unsafe(naked)]
76unsafe extern "C" fn _start() -> ! {
77    core::arch::naked_asm!(
78        "mov rbx, rdi",
79        // BSS is omitted from the IGVM and may be dirty after a reboot. Clear it
80        // before installing the stack, which is itself in BSS.
81        "lea rdi, __bss_start[rip]",
82        "lea rcx, _end[rip]",
83        "sub rcx, rdi",
84        "xor eax, eax",
85        "cld",
86        "rep stosb",
87        "mov rdi, rbx",
88        "lea rsp, {STACK_SIZE} + {stack}[rip]",
89        "mov dword ptr {stack}[rip], {STACK_COOKIE}",
90        "mov rax, cr4",
91        "or rax, 0x600", // OSFXSR and OSXMMEXCPT
92        "mov cr4, rax",
93        "push rsi",
94        "push rdi",
95        "lea rdx, _DYNAMIC[rip]",
96        "lea rdi, __ehdr_start[rip]",
97        "mov rsi, rdi",
98        "call {relocate}",
99        "pop rdi",
100        "mov rsi, [rsp]", // leave the saved argument on the stack for alignment
101        "jmp {start}",
102        relocate = sym minimal_rt::reloc::relocate,
103        start = sym crate::rt::start,
104        stack = sym crate::rt::STACK,
105        STACK_COOKIE = const crate::rt::STACK_COOKIE,
106        STACK_SIZE = const crate::rt::STACK_SIZE,
107    );
108}