@@ -20,10 +20,10 @@ use thiserror::Error;
2020use uhyve_interface:: GuestPhysAddr ;
2121
2222use crate :: {
23- HypervisorError , arch ,
23+ HypervisorError ,
2424 consts:: * ,
2525 fdt:: Fdt ,
26- generate_address ,
26+ generate_guest_start_address ,
2727 isolation:: filemap:: UhyveFileMap ,
2828 mem:: MmapMemory ,
2929 os:: KickSignal ,
@@ -62,8 +62,6 @@ pub type DefaultBackend = crate::macos::XhyveVm;
6262pub ( crate ) mod internal {
6363 use std:: sync:: Arc ;
6464
65- use uhyve_interface:: GuestPhysAddr ;
66-
6765 use crate :: {
6866 HypervisorResult ,
6967 vcpu:: VirtualCPU ,
@@ -83,11 +81,7 @@ pub(crate) mod internal {
8381 enable_stats : bool ,
8482 ) -> HypervisorResult < Self :: VCPU > ;
8583
86- fn new (
87- peripherals : Arc < VmPeripherals > ,
88- params : & Params ,
89- guest_addr : GuestPhysAddr ,
90- ) -> HypervisorResult < Self > ;
84+ fn new ( peripherals : Arc < VmPeripherals > , params : & Params ) -> HypervisorResult < Self > ;
9185 }
9286}
9387
@@ -152,6 +146,18 @@ impl<VirtBackend: VirtualizationBackend> UhyveVm<VirtBackend> {
152146 info ! ( "Loading a pre Hermit v0.10.0 kernel" ) ;
153147 }
154148
149+ // Kernels with different Uhyve interface versions may have differing addresses for the
150+ // serial port. As we begun embedding the uhyve-interface version in unikernel images
151+ // much later than v1, but before v2, we assume that all images that don't have a version
152+ // embedded must be v1.
153+ //
154+ // Further, it is used for establishing an appropriate random start addrses for ASLR.
155+ let uhyve_interface_version = object
156+ . uhyve_interface_version ( )
157+ . unwrap_or ( UhyveIfVersion ( 1 ) ) ;
158+
159+ debug ! ( "Detected Uhyve interface version: {uhyve_interface_version}" ) ;
160+
155161 // The memory layout of uhyve looks as follows:
156162 //
157163 // 0x0000_0000 ┌───────────────────┐
@@ -180,19 +186,13 @@ impl<VirtBackend: VirtualizationBackend> UhyveVm<VirtBackend> {
180186 // │ │
181187 // └───────────────────┘
182188
183- let ( guest_address, kernel_address) = if let Some ( start_addr) = object. start_addr ( ) {
184- if params. aslr {
185- warn ! ( "ASLR is enabled but kernel is not relocatable - disabling ASLR" ) ;
186- }
187- ( arch:: RAM_START , GuestPhysAddr :: from ( start_addr) )
188- } else {
189- let guest_address = if params. aslr {
190- generate_address ( object. mem_size ( ) )
191- } else {
192- arch:: RAM_START
193- } ;
194- ( guest_address, ( guest_address + KERNEL_OFFSET ) )
195- } ;
189+ let ( guest_address, kernel_address) = generate_guest_start_address (
190+ uhyve_interface_version,
191+ params. aslr ,
192+ object. mem_size ( ) ,
193+ object. start_addr ( ) ,
194+ memory_size,
195+ ) ;
196196
197197 debug ! ( "Guest starts at {guest_address:#x}" ) ;
198198 debug ! ( "Kernel gets loaded to {kernel_address:#x}" ) ;
@@ -266,8 +266,7 @@ impl<VirtBackend: VirtualizationBackend> UhyveVm<VirtBackend> {
266266 serial,
267267 } ) ;
268268
269- let virt_backend =
270- VirtBackend :: BACKEND :: new ( peripherals. clone ( ) , & kernel_info. params , guest_address) ?;
269+ let virt_backend = VirtBackend :: BACKEND :: new ( peripherals. clone ( ) , & kernel_info. params ) ?;
271270
272271 let cpu_count = kernel_info. params . cpu_count . get ( ) ;
273272
@@ -290,14 +289,6 @@ impl<VirtBackend: VirtualizationBackend> UhyveVm<VirtBackend> {
290289
291290 let freq = vcpus[ 0 ] . get_cpu_frequency ( ) ;
292291
293- // Kernels with different Uhyve interface versions may have differing addresses for the
294- // serial port. As we begun embedding the uhyve-interface version in unikernel images
295- // much later than v1, but before v2, we assume that all images that don't have a version
296- // embedded must be v1.
297- let uhyve_interface_version = object
298- . uhyve_interface_version ( )
299- . unwrap_or ( UhyveIfVersion ( 1 ) ) ;
300-
301292 let serial_port = SerialPortBase :: new ( match uhyve_interface_version. 0 {
302293 1 => uhyve_interface:: v1:: HypercallAddress :: Uart as _ ,
303294 2 => uhyve_interface:: v2:: HypercallAddress :: SerialWriteBuffer as _ ,
0 commit comments