Skip to content

Commit

Permalink
Rename kernel_start to kernel_offset
Browse files Browse the repository at this point in the history
The load() method in the KernelLoader trait takes a kernel_start argument
that has different behavior in the Elf, BzImage, and PE implementations.

Elf: kernel_start provides an offset to be added to the default kernel load
address encoded in the phdr.p_paddr field of the Elf header. A VMM using
kernel_start requests that the kernel is loaded in guest memory at
phdr.p_paddr + kernel_start.

BzImage: kernel_start allows to specify an absolute address at which to load
the kernel in guest memory, replacing the default address taken from the
code32_start field of the bzImage header.

PE: kernel_start specifies a 2MB-aligned base address in guest memory.
The kernel must be loaded at kernel_start + text_offset, where text_offset
value is read from the Image header.

Change the name from kernel_start to kernel_offset, and change the
argument description on each implementation to clarify its use.

Related to #12

Signed-off-by: Alejandro Jimenez <alejandro.j.jimenez@oracle.com>
  • Loading branch information
aljimenezb authored and alxiord committed Jun 24, 2020
1 parent 2a62f21 commit c1aad5f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/loader/aarch64/pe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ impl KernelLoader for PE {
/// # Arguments
///
/// * `guest_mem` - The guest memory where the kernel image is loaded.
/// * `kernel_start` - The offset into 'guest_mem' at which to load the kernel.
/// * `kernel_offset` - 2MB-aligned base addres in guest memory at at which to load the kernel.
/// * `kernel_image` - Input Image format kernel image.
/// * `highmem_start_address` - ignored on ARM64.
///
/// # Returns
/// * KernelLoaderResult
fn load<F, M: GuestMemory>(
guest_mem: &M,
kernel_start: Option<GuestAddress>,
kernel_offset: Option<GuestAddress>,
kernel_image: &mut F,
_highmem_start_address: Option<GuestAddress>,
) -> Result<KernelLoaderResult>
Expand Down Expand Up @@ -135,7 +135,7 @@ impl KernelLoader for PE {
text_offset = 0x80000;
}

let mem_offset = kernel_start
let mem_offset = kernel_offset
.unwrap_or(GuestAddress(0))
.checked_add(text_offset)
.ok_or(Error::InvalidImage)?;
Expand Down
4 changes: 2 additions & 2 deletions src/loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,14 @@ pub trait KernelLoader {
/// # Arguments
///
/// * `guest_mem`: [`GuestMemory`] to load the kernel in.
/// * `kernel_start`: Address in guest memory where the kernel is loaded.
/// * `kernel_offset`: Usage varies between implementations.
/// * `kernel_image`: Kernel image to be loaded.
/// * `highmem_start_address`: Address where high memory starts.
///
/// [`GuestMemory`]: https://docs.rs/vm-memory/latest/vm_memory/guest_memory/trait.GuestMemory.html
fn load<F, M: GuestMemory>(
guest_mem: &M,
kernel_start: Option<GuestAddress>,
kernel_offset: Option<GuestAddress>,
kernel_image: &mut F,
highmem_start_address: Option<GuestAddress>,
) -> Result<KernelLoaderResult>
Expand Down
18 changes: 9 additions & 9 deletions src/loader/x86_64/bzimage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl KernelLoader for BzImage {
/// # Arguments
///
/// * `guest_mem`: [`GuestMemory`] to load the kernel in.
/// * `kernel_start`: Address in guest memory where the kernel is loaded.
/// * `kernel_offset`: Address in guest memory where the kernel is loaded.
/// * `kernel_image` - Input bzImage image.
/// * `highmem_start_address`: Address where high memory starts.
///
Expand Down Expand Up @@ -98,7 +98,7 @@ impl KernelLoader for BzImage {
/// [`GuestMemory`]: https://docs.rs/vm-memory/latest/vm_memory/guest_memory/trait.GuestMemory.html
fn load<F, M: GuestMemory>(
guest_mem: &M,
kernel_start: Option<GuestAddress>,
kernel_offset: Option<GuestAddress>,
kernel_image: &mut F,
highmem_start_address: Option<GuestAddress>,
) -> Result<KernelLoaderResult>
Expand Down Expand Up @@ -144,7 +144,7 @@ impl KernelLoader for BzImage {
return Err(KernelLoaderError::InvalidKernelStartAddress);
}

let mem_offset = match kernel_start {
let mem_offset = match kernel_offset {
Some(start) => start,
None => GuestAddress(u64::from(boot_header.code32_start)),
};
Expand Down Expand Up @@ -197,13 +197,13 @@ mod tests {
fn test_load_bzImage() {
let gm = create_guest_mem();
let image = make_bzimage();
let mut kernel_start = GuestAddress(0x200000);
let mut kernel_offset = GuestAddress(0x200000);
let mut highmem_start_address = GuestAddress(0x0);

// load bzImage with good kernel_start and himem_start setting
// load bzImage with good kernel_offset and himem_start setting
let mut loader_result = BzImage::load(
&gm,
Some(kernel_start),
Some(kernel_offset),
&mut Cursor::new(&image),
Some(highmem_start_address),
)
Expand All @@ -215,7 +215,7 @@ mod tests {
assert_eq!(loader_result.setup_header.unwrap().loadflags, 1);
assert_eq!(loader_result.kernel_end, 0x60c320);

// load bzImage without kernel_start
// load bzImage without kernel_offset
loader_result = BzImage::load(
&gm,
None,
Expand All @@ -231,14 +231,14 @@ mod tests {
assert_eq!(loader_result.kernel_load.raw_value(), 0x100000);

// load bzImage with a bad himem setting
kernel_start = GuestAddress(0x1000);
kernel_offset = GuestAddress(0x1000);
highmem_start_address = GuestAddress(0x200000);

assert_eq!(
Some(KernelLoaderError::InvalidKernelStartAddress),
BzImage::load(
&gm,
Some(kernel_start),
Some(kernel_offset),
&mut Cursor::new(&image),
Some(highmem_start_address),
)
Expand Down
12 changes: 6 additions & 6 deletions src/loader/x86_64/elf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl KernelLoader for Elf {
/// # Arguments
///
/// * `guest_mem`: [`GuestMemory`] to load the kernel in.
/// * `kernel_start`: Address in guest memory where the kernel is loaded.
/// * `kernel_offset`: Offset to be added to default kernel load address in guest memory.
/// * `kernel_image` - Input vmlinux image.
/// * `highmem_start_address`: Address where high memory starts.
///
Expand Down Expand Up @@ -154,7 +154,7 @@ impl KernelLoader for Elf {
/// [`GuestMemory`]: https://docs.rs/vm-memory/latest/vm_memory/guest_memory/trait.GuestMemory.html
fn load<F, M: GuestMemory>(
guest_mem: &M,
kernel_start: Option<GuestAddress>,
kernel_offset: Option<GuestAddress>,
kernel_image: &mut F,
highmem_start_address: Option<GuestAddress>,
) -> Result<KernelLoaderResult>
Expand All @@ -181,8 +181,8 @@ impl KernelLoader for Elf {
let mut loader_result: KernelLoaderResult = Default::default();

// Address where the kernel will be loaded.
loader_result.kernel_load = match kernel_start {
Some(start) => GuestAddress(start.raw_value() + (ehdr.e_entry as u64)),
loader_result.kernel_load = match kernel_offset {
Some(k_offset) => GuestAddress(k_offset.raw_value() + (ehdr.e_entry as u64)),
None => GuestAddress(ehdr.e_entry as u64),
};

Expand Down Expand Up @@ -216,8 +216,8 @@ impl KernelLoader for Elf {

// if the vmm does not specify where the kernel should be loaded, just
// load it to the physical address p_paddr for each segment.
let mem_offset = match kernel_start {
Some(start) => start
let mem_offset = match kernel_offset {
Some(k_offset) => k_offset
.checked_add(phdr.p_paddr as u64)
.ok_or(Error::InvalidProgramHeaderAddress)?,
None => GuestAddress(phdr.p_paddr as u64),
Expand Down

0 comments on commit c1aad5f

Please sign in to comment.