Skip to content

Commit

Permalink
Merge pull request #159 from abiosoft/enable-nested-virtualization
Browse files Browse the repository at this point in the history
add support for nested virtualization
  • Loading branch information
Code-Hex authored Oct 19, 2024
2 parents c319894 + 900e8e6 commit d1fe5bd
Show file tree
Hide file tree
Showing 5 changed files with 80 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
23 changes: 23 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,28 @@ func (m *GenericPlatformConfiguration) MachineIdentifier() *GenericMachineIdenti
return m.machineIdentifier
}

// IsNestedVirtualizationSupported reports if nested virtualization is supported.
func IsNestedVirtualizationSupported() bool {
if err := macOSAvailable(15); err != nil {
return false
}

return (bool)(C.isNestedVirtualizationSupported())
}

// SetNestedVirtualizationEnabled toggles nested virtualization.
func (m *GenericPlatformConfiguration) SetNestedVirtualizationEnabled(enable bool) error {
if err := macOSAvailable(15); err != nil {
return err
}

C.setNestedVirtualizationEnabled(
objc.Ptr(m),
C.bool(enable),
)
return nil
}

var _ PlatformConfiguration = (*GenericPlatformConfiguration)(nil)

// NewGenericPlatformConfiguration creates a new generic platform configuration.
Expand Down
15 changes: 15 additions & 0 deletions virtualization_15.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// virtualization_15.h

#pragma once

// FIXME(codehex): this is dirty hack to avoid clang-format error like below
// "Configuration file(s) do(es) not support C++: /github.com/Code-Hex/vz/.clang-format"
#define NSURLComponents NSURLComponents

#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 d1fe5bd

Please sign in to comment.