diff --git a/coverage_config_aarch64.json b/coverage_config_aarch64.json index b1b035d8..861b91e6 100644 --- a/coverage_config_aarch64.json +++ b/coverage_config_aarch64.json @@ -1,5 +1,5 @@ { - "coverage_score": 71.4, + "coverage_score": 74.1, "exclude_path": "", "crate_features": "" } diff --git a/coverage_config_x86_64.json b/coverage_config_x86_64.json index 27587e41..27f86931 100644 --- a/coverage_config_x86_64.json +++ b/coverage_config_x86_64.json @@ -1,5 +1,5 @@ { - "coverage_score": 77.1, + "coverage_score": 74.7, "exclude_path": "", "crate_features": "" } diff --git a/src/loader/aarch64/mod.rs b/src/loader/aarch64/mod.rs new file mode 100644 index 00000000..e5105c03 --- /dev/null +++ b/src/loader/aarch64/mod.rs @@ -0,0 +1,12 @@ +// Copyright (c) 2019 Intel Corporation. All rights reserved. +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +//! Traits and structs for loading `aarch64` kernels into guest memory. + +#![cfg(target_arch = "aarch64")] diff --git a/src/loader/mod.rs b/src/loader/mod.rs index 4e8fe579..ba30eff5 100644 --- a/src/loader/mod.rs +++ b/src/loader/mod.rs @@ -9,7 +9,7 @@ // // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause -//! Traits and Structs +//! Traits and Structs for loading kernels into guest memory. //! - [KernelLoader](trait.KernelLoader.html): load kernel image into guest memory //! - [KernelLoaderResult](struct.KernelLoaderResult.html): the structure which loader //! returns to VMM to assist zero page construction and boot environment setup @@ -33,32 +33,26 @@ use vm_memory::{Address, ByteValued, Bytes, GuestAddress, GuestMemory, GuestUsiz #[cfg_attr(feature = "cargo-clippy", allow(clippy::all))] pub mod bootparam; -#[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))] -pub mod elf; - -#[cfg(all(feature = "bzimage", any(target_arch = "x86", target_arch = "x86_64")))] -pub mod bzimage; - -#[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))] -pub use elf::Elf; -#[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))] -pub use elf::Error as ElfError; +#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] +mod x86_64; +#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] +pub use x86_64::*; -#[cfg(all(feature = "bzimage", any(target_arch = "x86", target_arch = "x86_64")))] -pub use bzimage::BzImage; -#[cfg(all(feature = "bzimage", any(target_arch = "x86", target_arch = "x86_64")))] -pub use bzimage::Error as BzImageError; +#[cfg(target_arch = "aarch64")] +mod aarch64; +#[cfg(target_arch = "aarch64")] +pub use aarch64::*; #[derive(Debug, PartialEq)] /// Kernel loader errors. pub enum Error { /// Failed to load bzimage. #[cfg(all(feature = "bzimage", any(target_arch = "x86", target_arch = "x86_64")))] - Bzimage(BzImageError), + Bzimage(bzimage::Error), /// Failed to load elf image. #[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))] - Elf(ElfError), + Elf(elf::Error), /// Failed writing command line to guest memory. CommandLineCopy, @@ -96,15 +90,15 @@ impl Display for Error { } #[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))] -impl From for Error { - fn from(err: ElfError) -> Self { +impl From for Error { + fn from(err: elf::Error) -> Self { Error::Elf(err) } } #[cfg(all(feature = "bzimage", any(target_arch = "x86", target_arch = "x86_64")))] -impl From for Error { - fn from(err: BzImageError) -> Self { +impl From for Error { + fn from(err: bzimage::Error) -> Self { Error::Bzimage(err) } } @@ -114,7 +108,7 @@ impl From for Error { /// This specifies where the kernel is loading and passes additional /// information for the rest of the boot process to be completed by /// the VMM. -#[derive(Default)] +#[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct KernelLoaderResult { /// Address in the guest memory where the kernel image starts to be loaded pub kernel_load: GuestAddress, diff --git a/src/loader/bzimage/mod.rs b/src/loader/x86_64/bzimage/mod.rs similarity index 98% rename from src/loader/bzimage/mod.rs rename to src/loader/x86_64/bzimage/mod.rs index d6aca6b2..dc37f9d3 100644 --- a/src/loader/bzimage/mod.rs +++ b/src/loader/x86_64/bzimage/mod.rs @@ -18,7 +18,9 @@ use std::mem; use vm_memory::{Address, ByteValued, Bytes, GuestAddress, GuestMemory, GuestUsize}; -use super::{bootparam, Error as KernelLoaderError, KernelLoader, KernelLoaderResult, Result}; +use super::super::{ + bootparam, Error as KernelLoaderError, KernelLoader, KernelLoaderResult, Result, +}; #[derive(Debug, PartialEq)] /// Bzimage kernel loader errors. @@ -85,12 +87,12 @@ impl KernelLoader for BzImage { /// let gm = GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), mem_size)]).unwrap(); /// let mut kernel_image = vec![]; /// kernel_image.extend_from_slice(include_bytes!("bzimage")); - /// assert!(BzImage::load( + /// bzimage::BzImage::load( /// &gm, /// Some(kernel_addr), /// &mut Cursor::new(&kernel_image), /// Some(himem_start), - /// ).is_ok()); + /// ).unwrap(); /// ``` /// /// [`GuestMemory`]: https://docs.rs/vm-memory/latest/vm_memory/guest_memory/trait.GuestMemory.html diff --git a/src/loader/elf/elf.rs b/src/loader/x86_64/elf/elf.rs similarity index 100% rename from src/loader/elf/elf.rs rename to src/loader/x86_64/elf/elf.rs diff --git a/src/loader/elf/mod.rs b/src/loader/x86_64/elf/mod.rs similarity index 95% rename from src/loader/elf/mod.rs rename to src/loader/x86_64/elf/mod.rs index 62aa7b23..905a68b4 100644 --- a/src/loader/elf/mod.rs +++ b/src/loader/x86_64/elf/mod.rs @@ -18,7 +18,7 @@ use std::mem; use vm_memory::{Address, ByteValued, Bytes, GuestAddress, GuestMemory, GuestUsize}; -use super::{Error as KernelLoaderError, KernelLoader, KernelLoaderResult, Result}; +use super::super::{Error as KernelLoaderError, KernelLoader, KernelLoaderResult, Result}; #[allow(dead_code)] #[allow(non_camel_case_types)] @@ -151,12 +151,12 @@ impl KernelLoader for Elf { /// let gm = GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), mem_size)]).unwrap(); /// let mut kernel_image = vec![]; /// kernel_image.extend_from_slice(include_bytes!("test_elf.bin")); - /// assert!(Elf::load( + /// elf::Elf::load( /// &gm, /// Some(kernel_addr), /// &mut Cursor::new(&kernel_image), /// Some(himem_start), - /// ).is_ok()); + /// ).unwrap(); /// ``` /// /// [`GuestMemory`]: https://docs.rs/vm-memory/latest/vm_memory/guest_memory/trait.GuestMemory.html @@ -182,7 +182,7 @@ impl KernelLoader for Elf { Self::validate_header(&ehdr)?; if let Some(addr) = highmem_start_address { if (ehdr.e_entry as u64) < addr.raw_value() { - Err(Error::InvalidEntryAddress)?; + return Err(Error::InvalidEntryAddress.into()); } } @@ -252,19 +252,19 @@ fn parse_elf_note(phdr: &elf::Elf64_Phdr, kernel_image: &mut F) -> Result(); @@ -274,12 +274,12 @@ where .read_from(0, kernel_image, nhdr_sz) .map_err(|_| Error::ReadNoteHeader)?; - // If the note header found is not the desired one, keep reading until - // the end of the segment + // If the note header found is not the desired one, keep reading until the end of the + // segment. if nhdr.n_type == XEN_ELFNOTE_PHYS32_ENTRY { break; } - // Skip the note header plus the size of its fields (with alignment) + // Skip the note header plus the size of its fields (with alignment). read_size += nhdr_sz + align_up(u64::from(nhdr.n_namesz), n_align) + align_up(u64::from(nhdr.n_descsz), n_align); @@ -290,21 +290,23 @@ where } if read_size >= phdr.p_filesz as usize { - return Ok(None); // PVH ELF note not found, nothing else to do. + // PVH ELF note not found, nothing else to do. + return Ok(None); } + // Otherwise the correct note type was found. - // The note header struct has already been read, so we can seek from the - // current position and just skip the name field contents. + // The note header struct has already been read, so we can seek from the current position and + // just skip the name field contents. kernel_image .seek(SeekFrom::Current( align_up(u64::from(nhdr.n_namesz), n_align) as i64, )) .map_err(|_| Error::SeekNoteHeader)?; - // The PVH entry point is a 32-bit address, so the descriptor field - // must be capable of storing all such addresses. + // The PVH entry point is a 32-bit address, so the descriptor field must be capable of storing + // all such addresses. if (nhdr.n_descsz as usize) < mem::size_of::() { - Err(Error::InvalidPvhNote)?; + return Err(Error::InvalidPvhNote.into()); } let mut pvh_addr_bytes = [0; mem::size_of::()]; diff --git a/src/loader/elf/start_info.rs b/src/loader/x86_64/elf/start_info.rs similarity index 100% rename from src/loader/elf/start_info.rs rename to src/loader/x86_64/elf/start_info.rs diff --git a/src/loader/elf/test_badnote.bin b/src/loader/x86_64/elf/test_badnote.bin similarity index 100% rename from src/loader/elf/test_badnote.bin rename to src/loader/x86_64/elf/test_badnote.bin diff --git a/src/loader/elf/test_dummynote.bin b/src/loader/x86_64/elf/test_dummynote.bin similarity index 100% rename from src/loader/elf/test_dummynote.bin rename to src/loader/x86_64/elf/test_dummynote.bin diff --git a/src/loader/elf/test_elf.bin b/src/loader/x86_64/elf/test_elf.bin similarity index 100% rename from src/loader/elf/test_elf.bin rename to src/loader/x86_64/elf/test_elf.bin diff --git a/src/loader/elf/test_elfnote.bin b/src/loader/x86_64/elf/test_elfnote.bin similarity index 100% rename from src/loader/elf/test_elfnote.bin rename to src/loader/x86_64/elf/test_elfnote.bin diff --git a/src/loader/x86_64/mod.rs b/src/loader/x86_64/mod.rs new file mode 100644 index 00000000..1ad58b3f --- /dev/null +++ b/src/loader/x86_64/mod.rs @@ -0,0 +1,18 @@ +// Copyright (c) 2019 Intel Corporation. All rights reserved. +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Copyright 2017 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE-BSD-3-Clause file. +// +// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause + +//! Traits and structs for loading `x86_64` kernels into guest memory. + +#![cfg(any(target_arch = "x86", target_arch = "x86_64"))] + +#[cfg(feature = "elf")] +pub mod elf; + +#[cfg(feature = "bzimage")] +pub mod bzimage;