Skip to content

Commit

Permalink
add x86_64 and aarch64 modules
Browse files Browse the repository at this point in the history
Phase 2 of code reorganization: separate x86_64 from aarch64
functionality in modules.

Signed-off-by: Alexandra Iordache <[email protected]>
  • Loading branch information
Alexandra Iordache committed Mar 24, 2020
1 parent c7e2664 commit 63fceb9
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 45 deletions.
2 changes: 1 addition & 1 deletion coverage_config_aarch64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 71.4,
"coverage_score": 74.1,
"exclude_path": "",
"crate_features": ""
}
2 changes: 1 addition & 1 deletion coverage_config_x86_64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 77.1,
"coverage_score": 74.7,
"exclude_path": "",
"crate_features": ""
}
12 changes: 12 additions & 0 deletions src/loader/aarch64/mod.rs
Original file line number Diff line number Diff line change
@@ -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")]
38 changes: 16 additions & 22 deletions src/loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -96,15 +90,15 @@ impl Display for Error {
}

#[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))]
impl From<ElfError> for Error {
fn from(err: ElfError) -> Self {
impl From<elf::Error> 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<BzImageError> for Error {
fn from(err: BzImageError) -> Self {
impl From<bzimage::Error> for Error {
fn from(err: bzimage::Error) -> Self {
Error::Bzimage(err)
}
}
Expand All @@ -114,7 +108,7 @@ impl From<BzImageError> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
File renamed without changes.
38 changes: 20 additions & 18 deletions src/loader/elf/mod.rs → src/loader/x86_64/elf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand All @@ -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());
}
}

Expand Down Expand Up @@ -252,19 +252,19 @@ fn parse_elf_note<F>(phdr: &elf::Elf64_Phdr, kernel_image: &mut F) -> Result<Opt
where
F: Read + Seek,
{
// Type of note header that encodes a 32-bit entry point address
// to boot a guest kernel using the PVH boot protocol.
// Type of note header that encodes a 32-bit entry point address to boot a guest kernel using
// the PVH boot protocol.
const XEN_ELFNOTE_PHYS32_ENTRY: u32 = 18;

let n_align = phdr.p_align;

// Seek to the beginning of the note segment
// Seek to the beginning of the note segment.
kernel_image
.seek(SeekFrom::Start(phdr.p_offset))
.map_err(|_| Error::SeekNoteHeader)?;

// Now that the segment has been found, we must locate an ELF note with the
// correct type that encodes the PVH entry point if there is one.
// Now that the segment has been found, we must locate an ELF note with the correct type that
// encodes the PVH entry point if there is one.
let mut nhdr: elf::Elf64_Nhdr = Default::default();
let mut read_size: usize = 0;
let nhdr_sz = mem::size_of::<elf::Elf64_Nhdr>();
Expand All @@ -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);
Expand All @@ -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::<u32>() {
Err(Error::InvalidPvhNote)?;
return Err(Error::InvalidPvhNote.into());
}

let mut pvh_addr_bytes = [0; mem::size_of::<u32>()];
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions src/loader/x86_64/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 63fceb9

Please sign in to comment.