From 1135133e47850b1cf58ad640a8c3bc320a4dd8b5 Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Mon, 5 Aug 2024 14:03:32 +0100 Subject: [PATCH] add support for nested virtualization Signed-off-by: Abiola Ibrahim --- osversion.go | 2 ++ platform.go | 18 ++++++++++++++++++ virtualization_15.h | 11 +++++++++++ virtualization_15.m | 33 +++++++++++++++++++++++++++++++++ virtualization_helper.h | 7 +++++++ 5 files changed, 71 insertions(+) create mode 100644 virtualization_15.h create mode 100644 virtualization_15.m diff --git a/osversion.go b/osversion.go index ba54d63..02a8a37 100644 --- a/osversion.go +++ b/osversion.go @@ -105,6 +105,8 @@ func macOSBuildTargetAvailable(version float64) error { target = 130000 // __MAC_13_0 case 14: target = 140000 // __MAC_14_0 + case 15: + target = 150000 // __MAC_15_0 } if allowedVersion < target { return fmt.Errorf("%w for %.1f (the binary was built with __MAC_OS_X_VERSION_MAX_ALLOWED=%d; needs recompilation)", diff --git a/platform.go b/platform.go index 1c3b8f0..928c06b 100644 --- a/platform.go +++ b/platform.go @@ -6,6 +6,7 @@ package vz # include "virtualization_11.h" # include "virtualization_12.h" # include "virtualization_13.h" +# include "virtualization_15.h" */ import "C" import ( @@ -40,6 +41,23 @@ func (m *GenericPlatformConfiguration) MachineIdentifier() *GenericMachineIdenti return m.machineIdentifier } +// IsNestedVirtualizationSupported reports if nested virtualization is supported. +func (m *GenericPlatformConfiguration) IsNestedVirtualizationSupported() bool { + if err := macOSAvailable(15); err != nil { + return false + } + + return (bool)(C.isNestedVirtualizationSupported()) +} + +// SetNestedVirtualizationEnabled toggles nested virtualization. +func (m *GenericPlatformConfiguration) SetNestedVirtualizationEnabled(enable bool) { + C.setNestedVirtualizationEnabled( + objc.Ptr(m), + C.bool(enable), + ) +} + var _ PlatformConfiguration = (*GenericPlatformConfiguration)(nil) // NewGenericPlatformConfiguration creates a new generic platform configuration. diff --git a/virtualization_15.h b/virtualization_15.h new file mode 100644 index 0000000..ff8ed02 --- /dev/null +++ b/virtualization_15.h @@ -0,0 +1,11 @@ +// +// virtualization_15.h + +#pragma once + +#import "virtualization_helper.h" +#import + +/* macOS 15 API */ +bool isNestedVirtualizationSupported(); +void setNestedVirtualizationEnabled(void *config, bool nestedVirtualizationEnabled); diff --git a/virtualization_15.m b/virtualization_15.m new file mode 100644 index 0000000..2955267 --- /dev/null +++ b/virtualization_15.m @@ -0,0 +1,33 @@ +// +// virtualization_15.m +// +#import "virtualization_15.h" + +/*! + @abstract Check if nested virtualization is supported. + @return true if supported. + */ +bool isNestedVirtualizationSupported() +{ +#ifdef INCLUDE_TARGET_OSX_15 + if (@available(macOS 15, *)) { + return (bool) VZGenericPlatformConfiguration.isNestedVirtualizationSupported; + } +#endif + RAISE_UNSUPPORTED_MACOS_EXCEPTION(); +} + +/*! + @abstract Set nestedVirtualizationEnabled. The default is false. + */ +void setNestedVirtualizationEnabled(void *config, bool nestedVirtualizationEnabled) +{ +#ifdef INCLUDE_TARGET_OSX_15 + if (@available(macOS 15, *)) { + VZGenericPlatformConfiguration *platformConfig = (VZGenericPlatformConfiguration *)config; + platformConfig.nestedVirtualizationEnabled = (BOOL) nestedVirtualizationEnabled; + return; + } +#endif + RAISE_UNSUPPORTED_MACOS_EXCEPTION(); +} diff --git a/virtualization_helper.h b/virtualization_helper.h index 44b4227..7914efd 100644 --- a/virtualization_helper.h +++ b/virtualization_helper.h @@ -39,6 +39,13 @@ NSDictionary *dumpProcessinfo(); #pragma message("macOS 14 API has been disabled") #endif +// for macOS 15 API +#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 150000 +#define INCLUDE_TARGET_OSX_15 1 +#else +#pragma message("macOS 15 API has been disabled") +#endif + static inline int mac_os_x_version_max_allowed() { #ifdef __MAC_OS_X_VERSION_MAX_ALLOWED