Disable unavailable features on older macOS
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
PATCH 2/2 hvf: arm: disable unavailable features on older macOS
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
@@ -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
PATCH 1/2 vmnet: disable unavailable features on older macOS
In reply to Joelle van DyneSome 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
@@ -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
@@ -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
@@ -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
Re: [PATCH 2/2] hvf: arm: disable unavailable features on older macOS
In reply to a message not in the archiveI 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.
Signed-off-by Joelle van Dyne <[email protected]>--- target/arm/hvf/hvf.c | 55 ++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 20 deletions(-)target/arm/hvf/hvf.c +35
-21@@ -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
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
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
3bf445fbb1which removes old ifdeffery needed by 11). This is part of our "supported build platforms" policy documented indocs/about/build-platforms.rst.thanks
-- PMM
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
3bf445fbb1which removes old ifdeffery needed by 11). This is part of our "supported build platforms" policy documented indocs/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 :|