Skip to content

Commit c1c2edc

Browse files
Alexandra IordacheSamuel Ortiz
Alexandra Iordache
authored and
Samuel Ortiz
committed
add x86_64 and aarch64 modules
Phase 2 of code reorganization: separate x86_64 from aarch64 functionality in modules. Signed-off-by: Alexandra Iordache <[email protected]>
1 parent c69c23a commit c1c2edc

13 files changed

+73
-45
lines changed

coverage_config_aarch64.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 71.4,
2+
"coverage_score": 74.1,
33
"exclude_path": "",
44
"crate_features": ""
55
}

coverage_config_x86_64.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 77.1,
2+
"coverage_score": 74.7,
33
"exclude_path": "",
44
"crate_features": ""
55
}

src/loader/aarch64/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) 2019 Intel Corporation. All rights reserved.
2+
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
//
4+
// Copyright 2017 The Chromium OS Authors. All rights reserved.
5+
// Use of this source code is governed by a BSD-style license that can be
6+
// found in the LICENSE-BSD-3-Clause file.
7+
//
8+
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
9+
10+
//! Traits and structs for loading `aarch64` kernels into guest memory.
11+
12+
#![cfg(target_arch = "aarch64")]

src/loader/mod.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//
1010
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
1111

12-
//! Traits and Structs
12+
//! Traits and Structs for loading kernels into guest memory.
1313
//! - [KernelLoader](trait.KernelLoader.html): load kernel image into guest memory
1414
//! - [KernelLoaderResult](struct.KernelLoaderResult.html): the structure which loader
1515
//! 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
3333
#[cfg_attr(feature = "cargo-clippy", allow(clippy::all))]
3434
pub mod bootparam;
3535

36-
#[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))]
37-
pub mod elf;
38-
39-
#[cfg(all(feature = "bzimage", any(target_arch = "x86", target_arch = "x86_64")))]
40-
pub mod bzimage;
41-
42-
#[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))]
43-
pub use elf::Elf;
44-
#[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))]
45-
pub use elf::Error as ElfError;
36+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
37+
mod x86_64;
38+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
39+
pub use x86_64::*;
4640

47-
#[cfg(all(feature = "bzimage", any(target_arch = "x86", target_arch = "x86_64")))]
48-
pub use bzimage::BzImage;
49-
#[cfg(all(feature = "bzimage", any(target_arch = "x86", target_arch = "x86_64")))]
50-
pub use bzimage::Error as BzImageError;
41+
#[cfg(target_arch = "aarch64")]
42+
mod aarch64;
43+
#[cfg(target_arch = "aarch64")]
44+
pub use aarch64::*;
5145

5246
#[derive(Debug, PartialEq)]
5347
/// Kernel loader errors.
5448
pub enum Error {
5549
/// Failed to load bzimage.
5650
#[cfg(all(feature = "bzimage", any(target_arch = "x86", target_arch = "x86_64")))]
57-
Bzimage(BzImageError),
51+
Bzimage(bzimage::Error),
5852

5953
/// Failed to load elf image.
6054
#[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))]
61-
Elf(ElfError),
55+
Elf(elf::Error),
6256

6357
/// Failed writing command line to guest memory.
6458
CommandLineCopy,
@@ -96,15 +90,15 @@ impl Display for Error {
9690
}
9791

9892
#[cfg(all(feature = "elf", any(target_arch = "x86", target_arch = "x86_64")))]
99-
impl From<ElfError> for Error {
100-
fn from(err: ElfError) -> Self {
93+
impl From<elf::Error> for Error {
94+
fn from(err: elf::Error) -> Self {
10195
Error::Elf(err)
10296
}
10397
}
10498

10599
#[cfg(all(feature = "bzimage", any(target_arch = "x86", target_arch = "x86_64")))]
106-
impl From<BzImageError> for Error {
107-
fn from(err: BzImageError) -> Self {
100+
impl From<bzimage::Error> for Error {
101+
fn from(err: bzimage::Error) -> Self {
108102
Error::Bzimage(err)
109103
}
110104
}
@@ -114,7 +108,7 @@ impl From<BzImageError> for Error {
114108
/// This specifies where the kernel is loading and passes additional
115109
/// information for the rest of the boot process to be completed by
116110
/// the VMM.
117-
#[derive(Default)]
111+
#[derive(Clone, Copy, Debug, Default, PartialEq)]
118112
pub struct KernelLoaderResult {
119113
/// Address in the guest memory where the kernel image starts to be loaded
120114
pub kernel_load: GuestAddress,

src/loader/bzimage/mod.rs renamed to src/loader/x86_64/bzimage/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use std::mem;
1818

1919
use vm_memory::{Address, ByteValued, Bytes, GuestAddress, GuestMemory, GuestUsize};
2020

21-
use super::{bootparam, Error as KernelLoaderError, KernelLoader, KernelLoaderResult, Result};
21+
use super::super::{
22+
bootparam, Error as KernelLoaderError, KernelLoader, KernelLoaderResult, Result,
23+
};
2224

2325
#[derive(Debug, PartialEq)]
2426
/// Bzimage kernel loader errors.
@@ -85,12 +87,12 @@ impl KernelLoader for BzImage {
8587
/// let gm = GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), mem_size)]).unwrap();
8688
/// let mut kernel_image = vec![];
8789
/// kernel_image.extend_from_slice(include_bytes!("bzimage"));
88-
/// assert!(BzImage::load(
90+
/// bzimage::BzImage::load(
8991
/// &gm,
9092
/// Some(kernel_addr),
9193
/// &mut Cursor::new(&kernel_image),
9294
/// Some(himem_start),
93-
/// ).is_ok());
95+
/// ).unwrap();
9496
/// ```
9597
///
9698
/// [`GuestMemory`]: https://docs.rs/vm-memory/latest/vm_memory/guest_memory/trait.GuestMemory.html
File renamed without changes.

src/loader/elf/mod.rs renamed to src/loader/x86_64/elf/mod.rs

+20-18
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::mem;
1818

1919
use vm_memory::{Address, ByteValued, Bytes, GuestAddress, GuestMemory, GuestUsize};
2020

21-
use super::{Error as KernelLoaderError, KernelLoader, KernelLoaderResult, Result};
21+
use super::super::{Error as KernelLoaderError, KernelLoader, KernelLoaderResult, Result};
2222

2323
#[allow(dead_code)]
2424
#[allow(non_camel_case_types)]
@@ -151,12 +151,12 @@ impl KernelLoader for Elf {
151151
/// let gm = GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), mem_size)]).unwrap();
152152
/// let mut kernel_image = vec![];
153153
/// kernel_image.extend_from_slice(include_bytes!("test_elf.bin"));
154-
/// assert!(Elf::load(
154+
/// elf::Elf::load(
155155
/// &gm,
156156
/// Some(kernel_addr),
157157
/// &mut Cursor::new(&kernel_image),
158158
/// Some(himem_start),
159-
/// ).is_ok());
159+
/// ).unwrap();
160160
/// ```
161161
///
162162
/// [`GuestMemory`]: https://docs.rs/vm-memory/latest/vm_memory/guest_memory/trait.GuestMemory.html
@@ -182,7 +182,7 @@ impl KernelLoader for Elf {
182182
Self::validate_header(&ehdr)?;
183183
if let Some(addr) = highmem_start_address {
184184
if (ehdr.e_entry as u64) < addr.raw_value() {
185-
Err(Error::InvalidEntryAddress)?;
185+
return Err(Error::InvalidEntryAddress.into());
186186
}
187187
}
188188

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

259259
let n_align = phdr.p_align;
260260

261-
// Seek to the beginning of the note segment
261+
// Seek to the beginning of the note segment.
262262
kernel_image
263263
.seek(SeekFrom::Start(phdr.p_offset))
264264
.map_err(|_| Error::SeekNoteHeader)?;
265265

266-
// Now that the segment has been found, we must locate an ELF note with the
267-
// correct type that encodes the PVH entry point if there is one.
266+
// Now that the segment has been found, we must locate an ELF note with the correct type that
267+
// encodes the PVH entry point if there is one.
268268
let mut nhdr: elf::Elf64_Nhdr = Default::default();
269269
let mut read_size: usize = 0;
270270
let nhdr_sz = mem::size_of::<elf::Elf64_Nhdr>();
@@ -274,12 +274,12 @@ where
274274
.read_from(0, kernel_image, nhdr_sz)
275275
.map_err(|_| Error::ReadNoteHeader)?;
276276

277-
// If the note header found is not the desired one, keep reading until
278-
// the end of the segment
277+
// If the note header found is not the desired one, keep reading until the end of the
278+
// segment.
279279
if nhdr.n_type == XEN_ELFNOTE_PHYS32_ENTRY {
280280
break;
281281
}
282-
// Skip the note header plus the size of its fields (with alignment)
282+
// Skip the note header plus the size of its fields (with alignment).
283283
read_size += nhdr_sz
284284
+ align_up(u64::from(nhdr.n_namesz), n_align)
285285
+ align_up(u64::from(nhdr.n_descsz), n_align);
@@ -290,21 +290,23 @@ where
290290
}
291291

292292
if read_size >= phdr.p_filesz as usize {
293-
return Ok(None); // PVH ELF note not found, nothing else to do.
293+
// PVH ELF note not found, nothing else to do.
294+
return Ok(None);
294295
}
296+
295297
// Otherwise the correct note type was found.
296-
// The note header struct has already been read, so we can seek from the
297-
// current position and just skip the name field contents.
298+
// The note header struct has already been read, so we can seek from the current position and
299+
// just skip the name field contents.
298300
kernel_image
299301
.seek(SeekFrom::Current(
300302
align_up(u64::from(nhdr.n_namesz), n_align) as i64,
301303
))
302304
.map_err(|_| Error::SeekNoteHeader)?;
303305

304-
// The PVH entry point is a 32-bit address, so the descriptor field
305-
// must be capable of storing all such addresses.
306+
// The PVH entry point is a 32-bit address, so the descriptor field must be capable of storing
307+
// all such addresses.
306308
if (nhdr.n_descsz as usize) < mem::size_of::<u32>() {
307-
Err(Error::InvalidPvhNote)?;
309+
return Err(Error::InvalidPvhNote.into());
308310
}
309311

310312
let mut pvh_addr_bytes = [0; mem::size_of::<u32>()];
File renamed without changes.
File renamed without changes.

src/loader/x86_64/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2019 Intel Corporation. All rights reserved.
2+
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
//
4+
// Copyright 2017 The Chromium OS Authors. All rights reserved.
5+
// Use of this source code is governed by a BSD-style license that can be
6+
// found in the LICENSE-BSD-3-Clause file.
7+
//
8+
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
9+
10+
//! Traits and structs for loading `x86_64` kernels into guest memory.
11+
12+
#![cfg(any(target_arch = "x86", target_arch = "x86_64"))]
13+
14+
#[cfg(feature = "elf")]
15+
pub mod elf;
16+
17+
#[cfg(feature = "bzimage")]
18+
pub mod bzimage;

0 commit comments

Comments
 (0)