|
| 1 | +// Copyright © 2020, Oracle and/or its affiliates. |
| 2 | +// |
| 3 | +// Copyright (c) 2019 Intel Corporation. All rights reserved. |
| 4 | +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | +// |
| 6 | +// Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 7 | +// Use of this source code is governed by a BSD-style license that can be |
| 8 | +// found in the LICENSE-BSD-3-Clause file. |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause |
| 11 | + |
| 12 | +//! Traits and structs for configuring and loading boot parameters on `x86_64` using the Linux |
| 13 | +//! boot protocol. |
| 14 | +
|
| 15 | +use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemory}; |
| 16 | + |
| 17 | +use super::super::{BootConfigurator, Error as BootConfiguratorError, Result}; |
| 18 | +use crate::loader::bootparam::boot_params; |
| 19 | + |
| 20 | +use std::error::Error as StdError; |
| 21 | +use std::fmt; |
| 22 | +use std::mem; |
| 23 | + |
| 24 | +/// Boot configurator for the Linux boot protocol. |
| 25 | +pub struct LinuxBootConfigurator {} |
| 26 | + |
| 27 | +/// Errors specific to the Linux boot protocol configuration. |
| 28 | +#[derive(Debug, PartialEq)] |
| 29 | +pub enum Error { |
| 30 | + /// The zero page extends past the end of guest memory. |
| 31 | + ZeroPagePastRamEnd, |
| 32 | + /// Error writing to the zero page of guest memory. |
| 33 | + ZeroPageSetup, |
| 34 | +} |
| 35 | + |
| 36 | +impl StdError for Error { |
| 37 | + fn description(&self) -> &str { |
| 38 | + use Error::*; |
| 39 | + match self { |
| 40 | + ZeroPagePastRamEnd => "The zero page extends past the end of guest memory.", |
| 41 | + ZeroPageSetup => "Error writing to the zero page of guest memory.", |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl fmt::Display for Error { |
| 47 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 48 | + write!( |
| 49 | + f, |
| 50 | + "Linux Boot Configurator Error: {}", |
| 51 | + StdError::description(self) |
| 52 | + ) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl From<Error> for BootConfiguratorError { |
| 57 | + fn from(err: Error) -> Self { |
| 58 | + BootConfiguratorError::Linux(err) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl BootConfigurator for LinuxBootConfigurator { |
| 63 | + /// Writes the boot parameters (configured elsewhere) into guest memory. |
| 64 | + /// |
| 65 | + /// # Arguments |
| 66 | + /// |
| 67 | + /// * `header` - boot parameters encapsulated in a [`boot_params`] struct. |
| 68 | + /// * `sections` - unused. |
| 69 | + /// * `guest_memory` - guest's physical memory. |
| 70 | + /// |
| 71 | + /// [`boot_params`]: ../loader/bootparam/struct.boot_e820_entry.html |
| 72 | + fn write_bootparams<T, S, M>( |
| 73 | + header: (T, GuestAddress), |
| 74 | + _sections: Option<(Vec<S>, GuestAddress)>, |
| 75 | + guest_memory: &M, |
| 76 | + ) -> Result<()> |
| 77 | + where |
| 78 | + T: ByteValued, |
| 79 | + S: ByteValued, |
| 80 | + M: GuestMemory, |
| 81 | + { |
| 82 | + // The VMM has filled a `boot_params` struct and its e820 map. |
| 83 | + // This will be written in guest memory at the zero page. |
| 84 | + guest_memory |
| 85 | + .checked_offset(header.1, mem::size_of::<boot_params>()) |
| 86 | + .ok_or(Error::ZeroPagePastRamEnd)?; |
| 87 | + guest_memory |
| 88 | + .write_obj(header.0, header.1) |
| 89 | + .map_err(|_| Error::ZeroPageSetup)?; |
| 90 | + |
| 91 | + Ok(()) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +#[cfg(test)] |
| 96 | +mod tests { |
| 97 | + use super::*; |
| 98 | + use std::mem; |
| 99 | + use vm_memory::{Address, GuestAddress, GuestMemoryMmap}; |
| 100 | + |
| 101 | + const KERNEL_BOOT_FLAG_MAGIC: u16 = 0xaa55; |
| 102 | + const KERNEL_HDR_MAGIC: u32 = 0x53726448; |
| 103 | + const KERNEL_LOADER_OTHER: u8 = 0xff; |
| 104 | + const KERNEL_MIN_ALIGNMENT_BYTES: u32 = 0x1000000; |
| 105 | + const MEM_SIZE: u64 = 0x1000000; |
| 106 | + |
| 107 | + fn create_guest_mem() -> GuestMemoryMmap { |
| 108 | + GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), (MEM_SIZE as usize))]).unwrap() |
| 109 | + } |
| 110 | + |
| 111 | + fn build_bootparams_common() -> boot_params { |
| 112 | + let mut params = boot_params::default(); |
| 113 | + params.hdr.boot_flag = KERNEL_BOOT_FLAG_MAGIC; |
| 114 | + params.hdr.header = KERNEL_HDR_MAGIC; |
| 115 | + params.hdr.kernel_alignment = KERNEL_MIN_ALIGNMENT_BYTES; |
| 116 | + params.hdr.type_of_loader = KERNEL_LOADER_OTHER; |
| 117 | + params |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn test_configure_linux_boot() { |
| 122 | + let zero_page_addr = GuestAddress(0x30000); |
| 123 | + |
| 124 | + let params = build_bootparams_common(); |
| 125 | + // This is where we'd append e820 entries, cmdline, PCI, ACPI etc. |
| 126 | + |
| 127 | + let guest_memory = create_guest_mem(); |
| 128 | + |
| 129 | + // Error case: boot params don't fit in guest memory (zero page address too close to end). |
| 130 | + let bad_zeropg_addr = GuestAddress( |
| 131 | + guest_memory.last_addr().raw_value() - mem::size_of::<boot_params>() as u64 + 1, |
| 132 | + ); |
| 133 | + assert_eq!( |
| 134 | + LinuxBootConfigurator::write_bootparams::<boot_params, boot_params, GuestMemoryMmap>( |
| 135 | + (params, bad_zeropg_addr), |
| 136 | + None, |
| 137 | + &guest_memory, |
| 138 | + ) |
| 139 | + .err(), |
| 140 | + Some(Error::ZeroPagePastRamEnd.into()), |
| 141 | + ); |
| 142 | + |
| 143 | + // Success case. |
| 144 | + assert!( |
| 145 | + LinuxBootConfigurator::write_bootparams::<boot_params, boot_params, GuestMemoryMmap>( |
| 146 | + (params, zero_page_addr), |
| 147 | + None, |
| 148 | + &guest_memory |
| 149 | + ) |
| 150 | + .is_ok() |
| 151 | + ); |
| 152 | + } |
| 153 | +} |
0 commit comments