VirtualMachine

public final class VirtualMachine

A type representing a full VM (virtual machine) including cpus and memory.

  • Undocumented

    Declaration

    Swift

    public private(set) var vcpus: [VCPU] { get }
  • Undocumented

    Declaration

    Swift

    public private(set) var memoryRegions: [MemoryRegion] { get }
  • Initialise the VM subsystem

    Declaration

    Swift

    public init(logger: Logger) throws

    Parameters

    logger

    Logger object for debug and trace messages.

  • Adds a memory region into the physical address space of the VM.

    The VM requires at least one MemoryRegion to be added before starting the vCPU.
    

    Precondition

    guestAddress must be page aligned.

    Precondition

    size is non-zero and is a multiple of the page size of the VM.

    Declaration

    Swift

    public func addMemory(at guestAddress: UInt64, size: UInt64, readOnly: Bool = false) throws -> MemoryRegion

    Parameters

    guestAddress

    Physical Address inside the VM where the region will start.

    size

    Size in bytes of the memory region.

    readOnly

    Flag to indicate if the memory should be treated as ROM or RAM by the Virtual CPUs.

    Return Value

    The new MemoryRegion.

  • Returns the memory region containing a specific physical address in the address space of the VM.

    Declaration

    Swift

    public func memoryRegion(containing guestAddress: PhysicalAddress) -> MemoryRegion?

    Parameters

    guestAddress

    The Physical Address in the VM.

    Return Value

    The MemoryRegion containing the address or nil if no region is found.

  • Returns an UnsafeMutableRawPointer to a region of bytes at a specified physical address.

    Throws

    HVError.invalidMemory if the region in not inside a specif MemoryRegion.

    Declaration

    Swift

    public func memory(at guestAddress: PhysicalAddress, count: UInt64) throws -> UnsafeMutableRawPointer

    Parameters

    guestAddress

    Physical Address inside the VM where the region will start.

    count

    The size in bytes of the region.

    Return Value

    An UnsafeMutableRawPointer pointing to a region of count bytes.

  • Add a VCPU to the Virtual Machine.

    Creates a new Thread and initialises a vCPU in that thread. The `startup` function
    is executed to setup the vCPU and then it waits until the `.start()` method is called
    to begin executing code.
    

    Declaration

    Swift

    @discardableResult
    public func addVCPU() throws -> VCPU

    Return Value

    The VCPU that has been added to the VM.

  • Query all of the vCPUs to determine if they have been shutdown

    Use this method to check the shutdown status of all of the vCPUs.
    Before shutting down the VirtualMachine, all of the vCPUs must be
    in the shutdown state.
    The `.shutdownAllVcpus` method can be called to shutdown all the
    vCPUS.
    

    Declaration

    Swift

    public func areVcpusShutdown() -> Bool

    Return Value

    true if all vCPUs have been shutdown, false if any are still running.

  • Request all of the vCPUs to enter the shutdown state.

    Use this method to request all vCPUs to shutdown.
    

    Declaration

    Swift

    @discardableResult
    public func shutdownAllVcpus() -> Bool

    Return Value

    true if all vCPUs have been shutdown, false if any are still running.

  • Shutdown the VM.

    shutdown() must be called before the VirtualMachine object is deallocated.
    Before calling, all vCPUS must be individually shutdown and a check is run
    to ensure the vCPUS are in the shutdown state.
    All MemoryRegions and VCPUS are deallocated by this method and the underlying
    hypervisor is shutdown by the OS.
    

    Throws

    VMError.vcpusStillRunning if any vCPU is still running.

    Throws

    VMError.vmShutdownFailure if an internal subsystem error occurs.

    Declaration

    Swift

    public func shutdown() throws
  • Undocumented

    See more

    Declaration

    Swift

    public final class VCPU