From c1aad5f1ecff233f6346ee37dedf9555fc671b45 Mon Sep 17 00:00:00 2001 From: Alejandro Jimenez Date: Wed, 10 Jun 2020 10:45:58 -0400 Subject: [PATCH] Rename kernel_start to kernel_offset 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 https://github.com/rust-vmm/linux-loader/issues/12 Signed-off-by: Alejandro Jimenez --- src/loader/aarch64/pe/mod.rs | 6 +++--- src/loader/mod.rs | 4 ++-- src/loader/x86_64/bzimage/mod.rs | 18 +++++++++--------- src/loader/x86_64/elf/mod.rs | 12 ++++++------ 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/loader/aarch64/pe/mod.rs b/src/loader/aarch64/pe/mod.rs index 280a9882..f7cad9aa 100644 --- a/src/loader/aarch64/pe/mod.rs +++ b/src/loader/aarch64/pe/mod.rs @@ -96,7 +96,7 @@ 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. /// @@ -104,7 +104,7 @@ impl KernelLoader for PE { /// * KernelLoaderResult fn load( guest_mem: &M, - kernel_start: Option, + kernel_offset: Option, kernel_image: &mut F, _highmem_start_address: Option, ) -> Result @@ -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)?; diff --git a/src/loader/mod.rs b/src/loader/mod.rs index 2cb5d830..3a08822d 100644 --- a/src/loader/mod.rs +++ b/src/loader/mod.rs @@ -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( guest_mem: &M, - kernel_start: Option, + kernel_offset: Option, kernel_image: &mut F, highmem_start_address: Option, ) -> Result diff --git a/src/loader/x86_64/bzimage/mod.rs b/src/loader/x86_64/bzimage/mod.rs index b387053a..d51ecbbd 100644 --- a/src/loader/x86_64/bzimage/mod.rs +++ b/src/loader/x86_64/bzimage/mod.rs @@ -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. /// @@ -98,7 +98,7 @@ impl KernelLoader for BzImage { /// [`GuestMemory`]: https://docs.rs/vm-memory/latest/vm_memory/guest_memory/trait.GuestMemory.html fn load( guest_mem: &M, - kernel_start: Option, + kernel_offset: Option, kernel_image: &mut F, highmem_start_address: Option, ) -> Result @@ -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)), }; @@ -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), ) @@ -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, @@ -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), ) diff --git a/src/loader/x86_64/elf/mod.rs b/src/loader/x86_64/elf/mod.rs index 0f4b6605..dbaba0ea 100644 --- a/src/loader/x86_64/elf/mod.rs +++ b/src/loader/x86_64/elf/mod.rs @@ -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. /// @@ -154,7 +154,7 @@ impl KernelLoader for Elf { /// [`GuestMemory`]: https://docs.rs/vm-memory/latest/vm_memory/guest_memory/trait.GuestMemory.html fn load( guest_mem: &M, - kernel_start: Option, + kernel_offset: Option, kernel_image: &mut F, highmem_start_address: Option, ) -> Result @@ -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), }; @@ -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),