Skip to content

Commit

Permalink
add support for nested virtualization
Browse files Browse the repository at this point in the history
Signed-off-by: Abiola Ibrahim <git@abiosoft.com>
  • Loading branch information
abiosoft committed Sep 21, 2024
1 parent c319894 commit 1135133
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions osversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
18 changes: 18 additions & 0 deletions platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions virtualization_15.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// virtualization_15.h

#pragma once

#import "virtualization_helper.h"
#import <Virtualization/Virtualization.h>

/* macOS 15 API */
bool isNestedVirtualizationSupported();
void setNestedVirtualizationEnabled(void *config, bool nestedVirtualizationEnabled);
33 changes: 33 additions & 0 deletions virtualization_15.m
Original file line number Diff line number Diff line change
@@ -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();
}
7 changes: 7 additions & 0 deletions virtualization_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1135133

Please sign in to comment.