Skip to contentSkip to conversation

Disable unavailable features on older macOS

PATCH In review Joelle van Dyne 3 participants 7 messages Dec 23, 2024 – Jan 6, 2025 1 message not in the archive Networking

lore
Share & tools
lore
#0 lore raw

cover Disable unavailable features on older macOS

Some features require APIs introduced in a recent version of macOS. Currently, this is not checked anywhere and so either the build will fail (if building with an older version of Xcode) or will throw a warning and then crash if run on an older machine. The correct way to handle this is with availabilty checks. The checks are a clang extension that only works on Apple platforms but these files are only built for Apple platforms already and link with Apple frameworks.

Joelle van Dyne (2):
vmnet: disable unavailable features on older macOS
hvf: arm: disable unavailable features on older macOS

 net/vmnet-host.c     | 48 ++++++++++++++++++++++++++------------
 net/vmnet-shared.c   | 23 ++++++++++++++----
 target/arm/hvf/hvf.c | 55 ++++++++++++++++++++++++++++----------------
 net/vmnet-bridged.m  | 18 ++++++++++++---
 4 files changed, 101 insertions(+), 43 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/2 vmnet: disable unavailable features on older macOS

Some options require macOS 11 or newer APIs. Instead of crashing (target version not set) or failing to compile (target version set), we will just return an error when the user tries to use the option.

---
 net/vmnet-host.c    | 48 +++++++++++++++++++++++++++++++--------------
 net/vmnet-shared.c  | 23 +++++++++++++++++-----
 net/vmnet-bridged.m | 18 ++++++++++++++---
 3 files changed, 66 insertions(+), 23 deletions(-)
net/vmnet-host.c +33 -15
index 49fb25c224..f3f0ac89e4 100644
@@ -21,12 +21,28 @@
 static bool validate_options(const Netdev *netdev, Error **errp)
 {
     const NetdevVmnetHostOptions *options = &(netdev->u.vmnet_host);
-    QemuUUID net_uuid;
 
-    if (options->net_uuid &&
-        qemu_uuid_parse(options->net_uuid, &net_uuid) < 0) {
-        error_setg(errp, "Invalid UUID provided in 'net-uuid'");
-        return false;
+    if (__builtin_available(macOS 11, *)) {
+        QemuUUID net_uuid;
+        if (options->net_uuid &&
+            qemu_uuid_parse(options->net_uuid, &net_uuid) < 0) {
+            error_setg(errp, "Invalid UUID provided in 'net-uuid'");
+            return false;
+        }
+    } else {
+        if (options->has_isolated) {
+            error_setg(errp,
+                       "vmnet-host.isolated feature is "
+                       "unavailable: outdated vmnet.framework API");
+            return false;
+        }
+
+        if (options->net_uuid) {
+            error_setg(errp,
+                       "vmnet-host.net-uuid feature is "
+                       "unavailable: outdated vmnet.framework API");
+            return false;
+        }
     }
 
     if ((options->start_address ||
@@ -53,16 +69,18 @@ static xpc_object_t build_if_desc(const Netdev *netdev)
                               vmnet_operation_mode_key,
                               VMNET_HOST_MODE);
 
-    xpc_dictionary_set_bool(if_desc,
-                            vmnet_enable_isolation_key,
-                            options->isolated);
-
-    QemuUUID net_uuid;
-    if (options->net_uuid) {
-        qemu_uuid_parse(options->net_uuid, &net_uuid);
-        xpc_dictionary_set_uuid(if_desc,
-                                vmnet_network_identifier_key,
-                                net_uuid.data);
+    if (__builtin_available(macOS 11, *)) {
+        xpc_dictionary_set_bool(if_desc,
+                                vmnet_enable_isolation_key,
+                                options->isolated);
+
+        QemuUUID net_uuid;
+        if (options->net_uuid) {
+            qemu_uuid_parse(options->net_uuid, &net_uuid);
+            xpc_dictionary_set_uuid(if_desc,
+                                    vmnet_network_identifier_key,
+                                    net_uuid.data);
+        }
     }
 
     if (options->start_address) {
net/vmnet-shared.c +18 -5
index 4726b07253..e6c65f3417 100644
@@ -21,6 +21,17 @@ static bool validate_options(const Netdev *netdev, Error **errp)
 {
     const NetdevVmnetSharedOptions *options = &(netdev->u.vmnet_shared);
 
+    if (__builtin_available(macOS 11, *)) {
+        /* clang requires a true branch */
+    } else {
+        if (options->has_isolated) {
+            error_setg(errp,
+                       "vmnet-shared.isolated feature is "
+                       "unavailable: outdated vmnet.framework API");
+            return false;
+        }
+    }
+
     if ((options->start_address ||
          options->end_address ||
          options->subnet_mask) &&
@@ -66,11 +77,13 @@ static xpc_object_t build_if_desc(const Netdev *netdev)
                                   options->subnet_mask);
     }
 
-    xpc_dictionary_set_bool(
-        if_desc,
-        vmnet_enable_isolation_key,
-        options->isolated
-    );
+    if (__builtin_available(macOS 11, *)) {
+        xpc_dictionary_set_bool(
+            if_desc,
+            vmnet_enable_isolation_key,
+            options->isolated
+        );
+    }
 
     return if_desc;
 }
net/vmnet-bridged.m +15 -3
index a04a14fa11..13fa2d16b6 100644
@@ -88,6 +88,16 @@ static bool validate_options(const Netdev *netdev, Error **errp)
         return false;
     }
 
+    if (__builtin_available(macOS 11, *)) {
+        /* clang requires a true branch */
+    } else {
+        if (options->has_isolated) {
+            error_setg(errp,
+                       "vmnet-bridged.isolated feature is "
+                       "unavailable: outdated vmnet.framework API");
+            return false;
+        }
+    }
     return true;
 }
 
@@ -106,9 +116,11 @@ static xpc_object_t build_if_desc(const Netdev *netdev)
                               vmnet_shared_interface_name_key,
                               options->ifname);
 
-    xpc_dictionary_set_bool(if_desc,
-                            vmnet_enable_isolation_key,
-                            options->isolated);
+    if (__builtin_available(macOS 11, *)) {
+        xpc_dictionary_set_bool(if_desc,
+                                vmnet_enable_isolation_key,
+                                options->isolated);
+    }
 
     return if_desc;
 }
Signature · 1 line
2.41.0
#2 lore raw

PATCH 2/2 hvf: arm: disable unavailable features on older macOS

In reply to Joelle van Dyne

IPA size queries were introduced in macOS 13. When QEMU is built targeting a lower version, the compile will fail. If targeting a higher version and the binary is executed on an older version, QEMU will crash. This will restore the behaviour before IPA max size querying was added which means VMs with 64+ GB of RAM will not work if running on < macOS 13.

---
 target/arm/hvf/hvf.c | 55 ++++++++++++++++++++++++++++----------------
 1 file changed, 35 insertions(+), 20 deletions(-)
target/arm/hvf/hvf.c +35 -20
index 0afd96018e..da60476dbc 100644
@@ -897,7 +897,9 @@ static bool hvf_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
     r |= hv_vcpu_get_sys_reg(fd, HV_SYS_REG_MIDR_EL1, &ahcf->midr);
     r |= hv_vcpu_destroy(fd);
 
-    clamp_id_aa64mmfr0_parange_to_ipa_size(&host_isar.id_aa64mmfr0);
+    if (__builtin_available(macOS 13.0, *)) {
+        clamp_id_aa64mmfr0_parange_to_ipa_size(&host_isar.id_aa64mmfr0);
+    }
 
     ahcf->isar = host_isar;
 
@@ -923,26 +925,34 @@ static bool hvf_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
 
 uint32_t hvf_arm_get_default_ipa_bit_size(void)
 {
-    uint32_t default_ipa_size;
-    hv_return_t ret = hv_vm_config_get_default_ipa_size(&default_ipa_size);
-    assert_hvf_ok(ret);
+    if (__builtin_available(macOS 13.0, *)) {
+        uint32_t default_ipa_size;
+        hv_return_t ret = hv_vm_config_get_default_ipa_size(&default_ipa_size);
+        assert_hvf_ok(ret);
 
-    return default_ipa_size;
+        return default_ipa_size;
+    } else {
+        return 0;
+    }
 }
 
 uint32_t hvf_arm_get_max_ipa_bit_size(void)
 {
-    uint32_t max_ipa_size;
-    hv_return_t ret = hv_vm_config_get_max_ipa_size(&max_ipa_size);
-    assert_hvf_ok(ret);
+    if (__builtin_available(macOS 13.0, *)) {
+        uint32_t max_ipa_size;
+        hv_return_t ret = hv_vm_config_get_max_ipa_size(&max_ipa_size);
+        assert_hvf_ok(ret);
 
-    /*
-     * We clamp any IPA size we want to back the VM with to a valid PARange
-     * value so the guest doesn't try and map memory outside of the valid range.
-     * This logic just clamps the passed in IPA bit size to the first valid
-     * PARange value <= to it.
-     */
-    return round_down_to_parange_bit_size(max_ipa_size);
+        /*
+         * We clamp any IPA size we want to back the VM with to a valid PARange
+         * value so the guest doesn't try and map memory outside of the valid
+         * range. This logic just clamps the passed in IPA bit size to the first
+         * valid PARange value <= to it.
+         */
+        return round_down_to_parange_bit_size(max_ipa_size);
+    } else {
+        return 0;
+    }
 }
 
 void hvf_arm_set_cpu_features_from_host(ARMCPU *cpu)
@@ -973,13 +983,18 @@ void hvf_arch_vcpu_destroy(CPUState *cpu)
 hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range)
 {
     hv_return_t ret;
-    hv_vm_config_t config = hv_vm_config_create();
+    hv_vm_config_t config;
 
-    ret = hv_vm_config_set_ipa_size(config, pa_range);
-    if (ret != HV_SUCCESS) {
-        goto cleanup;
+    if (__builtin_available(macOS 13.0, *)) {
+        config = hv_vm_config_create();
+        ret = hv_vm_config_set_ipa_size(config, pa_range);
+        if (ret != HV_SUCCESS) {
+            goto cleanup;
+        }
+        chosen_ipa_bit_size = pa_range;
+    } else {
+        config = NULL;
     }
-    chosen_ipa_bit_size = pa_range;
 
     ret = hv_vm_create(config);
 
Signature · 1 line
2.41.0

A message that is not in the archive (sent off-list or not yet received): CAGCz3vvojZHDdB33OSKH4RY2w6vo+hBTCywSZFWRCX9mTsnijw@mail.gmail.com

#4 lore raw

Re: [PATCH 2/2] hvf: arm: disable unavailable features on older macOS

I think it is acceptable policy to not test against older macOS versions, although I want to point out that a significant population of macOS users are still on macOS 11-13. It is bad practice to just crash when a function is not found. This is why the compiler throws a warning asking you to explicitly add availability guards. If we were to ever treat warnings more strictly, this would be an issue. If we consider older hosts unsupported, we can just test these changes on newer macOS, confirm that it doesn't cause any regression, and make no promises on what happens on older macOS.

Quoted 108 lines · Phil Dennis-Jordan

QEMU has a policy of only supporting macOS host versions for 2 years, see docs/about/build-platforms.rst. So you'll probably not be able to get this upstreamed as only macOS 14 and 15 are now supported. (I'm not a great fan of this policy personally, but I guess it makes CI etc easier to handle.)

Quoted 104 lines · Joelle van Dyne

IPA size queries were introduced in macOS 13. When QEMU is built targeting a lower version, the compile will fail. If targeting a higher version and the binary is executed on an older version, QEMU will crash. This will restore the behaviour before IPA max size querying was added which means VMs with 64+ GB of RAM will not work if running on < macOS 13.

---
 target/arm/hvf/hvf.c | 55 ++++++++++++++++++++++++++++----------------
 1 file changed, 35 insertions(+), 20 deletions(-)
target/arm/hvf/hvf.c +35 -21
index 0afd96018e..da60476dbc 100644
@@ -897,7 +897,9 @@ static bool hvf_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
     r |= hv_vcpu_get_sys_reg(fd, HV_SYS_REG_MIDR_EL1, &ahcf->midr);
     r |= hv_vcpu_destroy(fd);
 
-    clamp_id_aa64mmfr0_parange_to_ipa_size(&host_isar.id_aa64mmfr0);
+    if (__builtin_available(macOS 13.0, *)) {
+        clamp_id_aa64mmfr0_parange_to_ipa_size(&host_isar.id_aa64mmfr0);
+    }
 
     ahcf->isar = host_isar;
 
@@ -923,26 +925,34 @@ static bool hvf_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
 
 uint32_t hvf_arm_get_default_ipa_bit_size(void)
 {
-    uint32_t default_ipa_size;
-    hv_return_t ret = hv_vm_config_get_default_ipa_size(&default_ipa_size);
-    assert_hvf_ok(ret);
+    if (__builtin_available(macOS 13.0, *)) {
+        uint32_t default_ipa_size;
+        hv_return_t ret = hv_vm_config_get_default_ipa_size(&default_ipa_size);
+        assert_hvf_ok(ret);
 
-    return default_ipa_size;
+        return default_ipa_size;
+    } else {
+        return 0;
+    }
 }
 
 uint32_t hvf_arm_get_max_ipa_bit_size(void)
 {
-    uint32_t max_ipa_size;
-    hv_return_t ret = hv_vm_config_get_max_ipa_size(&max_ipa_size);
-    assert_hvf_ok(ret);
+    if (__builtin_available(macOS 13.0, *)) {
+        uint32_t max_ipa_size;
+        hv_return_t ret = hv_vm_config_get_max_ipa_size(&max_ipa_size);
+        assert_hvf_ok(ret);
 
-    /*
-     * We clamp any IPA size we want to back the VM with to a valid PARange
-     * value so the guest doesn't try and map memory outside of the valid range.
-     * This logic just clamps the passed in IPA bit size to the first valid
-     * PARange value <= to it.
-     */
-    return round_down_to_parange_bit_size(max_ipa_size);
+        /*
+         * We clamp any IPA size we want to back the VM with to a valid PARange
+         * value so the guest doesn't try and map memory outside of the valid
+         * range. This logic just clamps the passed in IPA bit size to the first
+         * valid PARange value <= to it.
+         */
+        return round_down_to_parange_bit_size(max_ipa_size);
+    } else {
+        return 0;
+    }
 }
 
 void hvf_arm_set_cpu_features_from_host(ARMCPU *cpu)
@@ -973,13 +983,18 @@ void hvf_arch_vcpu_destroy(CPUState *cpu)
 hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range)
 {
     hv_return_t ret;
-    hv_vm_config_t config = hv_vm_config_create();
+    hv_vm_config_t config;
 
-    ret = hv_vm_config_set_ipa_size(config, pa_range);
-    if (ret != HV_SUCCESS) {
-        goto cleanup;
+    if (__builtin_available(macOS 13.0, *)) {
+        config = hv_vm_config_create();
+        ret = hv_vm_config_set_ipa_size(config, pa_range);
+        if (ret != HV_SUCCESS) {
+            goto cleanup;
+        }
+        chosen_ipa_bit_size = pa_range;
+    } else {
+        config = NULL;
     }
-    chosen_ipa_bit_size = pa_range;
 
     ret = hv_vm_create(config);
 
--

2.41.0

#5 lore raw
In reply to Joelle van Dyne
Quoted 6 lines · Joelle van Dyne

Some features require APIs introduced in a recent version of macOS. Currently, this is not checked anywhere and so either the build will fail (if building with an older version of Xcode) or will throw a warning and then crash if run on an older machine. The correct way to handle this is with availabilty checks. The checks are a clang extension that only works on Apple platforms but these files are only built for Apple platforms already and link with Apple frameworks.

We deliberately only support macos 12 or later and don't want to carry code workarounds to build on earlier versions (see eg commit 3bf445fbb1 which removes old ifdeffery needed by 11). This is part of our "supported build platforms" policy documented in docs/about/build-platforms.rst.

thanks
-- PMM

#6 lore raw

If the policy is macOS 12 or later then you still need patch 2-3 because currently it is broken on macOS 12.

Quoted 23 lines · Peter Maydell
Quoted 1 line · Joelle van Dyne

Some features require APIs introduced in a recent version of macOS.

Currently,

Quoted 1 line

this is not checked anywhere and so either the build will fail (if

building with

Quoted 1 line

an older version of Xcode) or will throw a warning and then crash if run

on an

Quoted 1 line

older machine. The correct way to handle this is with availabilty

checks. The

Quoted 1 line

checks are a clang extension that only works on Apple platforms but

these files

Quoted 1 line

are only built for Apple platforms already and link with Apple

frameworks.

We deliberately only support macos 12 or later and don't want to carry code workarounds to build on earlier versions (see eg commit 3bf445fbb1 which removes old ifdeffery needed by 11). This is part of our "supported build platforms" policy documented in docs/about/build-platforms.rst.

thanks
-- PMM

#7 lore raw
Quoted 2 lines · Joelle van Dyne

If the policy is macOS 12 or later then you still need patch 2-3 because currently it is broken on macOS 12.

Actually our min baseline for macOS is currently macOS 14.

Per the doc Peter mentions, we officially target the most recent release (macOS 15), plus the previous release (macOS 14) for a further 2 years.

Anything older may or may not work at any given time, depending on whether we've done any cleanups that drop stuff only required by the older versions.

Quoted 23 lines · Peter Maydell
Quoted 21 lines · Joelle van Dyne
Quoted 1 line

Some features require APIs introduced in a recent version of macOS.

Currently,

Quoted 1 line

this is not checked anywhere and so either the build will fail (if

building with

Quoted 1 line

an older version of Xcode) or will throw a warning and then crash if run

on an

Quoted 1 line

older machine. The correct way to handle this is with availabilty

checks. The

Quoted 1 line

checks are a clang extension that only works on Apple platforms but

these files

Quoted 1 line

are only built for Apple platforms already and link with Apple

frameworks.

We deliberately only support macos 12 or later and don't want to carry code workarounds to build on earlier versions (see eg commit 3bf445fbb1 which removes old ifdeffery needed by 11). This is part of our "supported build platforms" policy documented in docs/about/build-platforms.rst.

Quoted 3 lines
Quoted 3 lines

thanks
-- PMM

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|