[PATCH v2 2/2] target/arm/hvf: configure IPA granule on macOS 26

Lucas Amaral posted 2 patches 4 weeks ago
Maintainers: Cameron Esfahani <dirty@apple.com>, Roman Bolshakov <rbolshakov@ddn.com>, Phil Dennis-Jordan <phil@philjordan.eu>, Alexander Graf <agraf@csgraf.de>, Peter Maydell <peter.maydell@linaro.org>
[PATCH v2 2/2] target/arm/hvf: configure IPA granule on macOS 26
Posted by Lucas Amaral 4 weeks ago
Read the ipa-granule property (set by hvf_accel_init) and configure
the HVF stage-2 translation granule via hv_vm_config_set_ipa_granule()
on macOS 26+.  When ipa-granule=4k, use HV_IPA_GRANULE_4KB to allow
HVF to map memory at 4KB granularity, matching 4KB-page guests.

If macOS < 26 and a sub-host-page granule was requested, warn and
fall back to the host page size (16KB).

After resolution, warn if the map granule exceeds the guest page size,
as Venus blob BAR mappings require page-aligned offsets.

Signed-off-by: Lucas Amaral <lucaaamaral@gmail.com>
---
 target/arm/hvf/hvf.c | 49 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/target/arm/hvf/hvf.c b/target/arm/hvf/hvf.c
index 7952b01..a6f139d 100644
--- a/target/arm/hvf/hvf.c
+++ b/target/arm/hvf/hvf.c
@@ -11,7 +11,9 @@
 
 #include "qemu/osdep.h"
 #include "qemu/error-report.h"
+#include "qemu/units.h"
 #include "qemu/log.h"
+#include "exec/target_page.h"
 
 #include "system/runstate.h"
 #include "system/hvf.h"
@@ -960,6 +962,53 @@ hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range)
     }
     chosen_ipa_bit_size = pa_range;
 
+    /*
+     * Configure IPA granule from the ipa-granule property.
+     * hvf_get_map_granule() was set by hvf_accel_init() before this call.
+     */
+    {
+        uint64_t granule = hvf_get_map_granule();
+        bool granule_set = false;
+
+#ifdef MAC_OS_VERSION_26_0
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_26_0
+        if (__builtin_available(macOS 26, *)) {
+            hv_ipa_granule_t hv_gran = (granule <= 4 * KiB)
+                ? HV_IPA_GRANULE_4KB : HV_IPA_GRANULE_16KB;
+            ret = hv_vm_config_set_ipa_granule(config, hv_gran);
+            if (ret != HV_SUCCESS) {
+                error_report("HVF: failed to set IPA granule: %s",
+                             hvf_return_string(ret));
+                goto cleanup;
+            }
+            granule_set = true;
+        }
+#endif
+#endif
+
+        if (!granule_set && granule < qemu_real_host_page_size()) {
+            warn_report("HVF: ipa-granule=%zuKB requested but macOS < 26; "
+                        "falling back to host page size (%zuKB)",
+                        (size_t)(granule / KiB),
+                        (size_t)(qemu_real_host_page_size() / KiB));
+            hvf_set_map_granule(qemu_real_host_page_size());
+        }
+
+        /*
+         * Venus blob mapping safety: warn if the resolved map granule
+         * exceeds the guest page size, as virtio-gpu blob BAR mappings
+         * require page-aligned offsets and would hang otherwise.
+         */
+        if (hvf_get_map_granule() > qemu_target_page_size()) {
+            warn_report("HVF map granule (%zu) > guest page size (%zu); "
+                        "Venus blob BAR mappings may hang. "
+                        "Consider ipa-granule=4k (requires macOS 26+) or "
+                        "guest F_BLOB_ALIGNMENT support.",
+                        (size_t)hvf_get_map_granule(),
+                        qemu_target_page_size());
+        }
+    }
+
     ret = hv_vm_create(config);
 
 cleanup:
-- 
2.52.0
Re: [PATCH v2 2/2] target/arm/hvf: configure IPA granule on macOS 26
Posted by Mohamed Mediouni 4 weeks ago

> On 11. Mar 2026, at 03:27, Lucas Amaral <lucaaamaral@gmail.com> wrote:
> 
> Read the ipa-granule property (set by hvf_accel_init) and configure
> the HVF stage-2 translation granule via hv_vm_config_set_ipa_granule()
> on macOS 26+.  When ipa-granule=4k, use HV_IPA_GRANULE_4KB to allow
> HVF to map memory at 4KB granularity, matching 4KB-page guests.
> 
> If macOS < 26 and a sub-host-page granule was requested, warn and
> fall back to the host page size (16KB).
> 
> After resolution, warn if the map granule exceeds the guest page size,
> as Venus blob BAR mappings require page-aligned offsets.
> 
> Signed-off-by: Lucas Amaral <lucaaamaral@gmail.com>
> ---
> target/arm/hvf/hvf.c | 49 ++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 49 insertions(+)
> 
> diff --git a/target/arm/hvf/hvf.c b/target/arm/hvf/hvf.c
> index 7952b01..a6f139d 100644
> --- a/target/arm/hvf/hvf.c
> +++ b/target/arm/hvf/hvf.c
> @@ -11,7 +11,9 @@
> 
> #include "qemu/osdep.h"
> #include "qemu/error-report.h"
> +#include "qemu/units.h"
> #include "qemu/log.h"
> +#include "exec/target_page.h"
> 
> #include "system/runstate.h"
> #include "system/hvf.h"
> @@ -960,6 +962,53 @@ hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range)
>     }
>     chosen_ipa_bit_size = pa_range;
> 
> +    /*
> +     * Configure IPA granule from the ipa-granule property.
> +     * hvf_get_map_granule() was set by hvf_accel_init() before this call.
> +     */
> +    {
> +        uint64_t granule = hvf_get_map_granule();
> +        bool granule_set = false;
> +
> +#ifdef MAC_OS_VERSION_26_0
> +#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_26_0
Cosmetic:

#if (__MAC_OS_X_VERSION_MAX_ALLOWED >= 260000)

To match hvf_sme_stubs.h
> 
> +        if (__builtin_available(macOS 26, *)) {
> +            hv_ipa_granule_t hv_gran = (granule <= 4 * KiB)
> +                ? HV_IPA_GRANULE_4KB : HV_IPA_GRANULE_16KB;
> +            ret = hv_vm_config_set_ipa_granule(config, hv_gran);
> +            if (ret != HV_SUCCESS) {
> +                error_report("HVF: failed to set IPA granule: %s",
> +                             hvf_return_string(ret));
> +                goto cleanup;
> +            }
> +            granule_set = true;
> +        }
…and a single endif
> +#endif
> +#endif
> +
> +        if (!granule_set && granule < qemu_real_host_page_size()) {
> +            warn_report("HVF: ipa-granule=%zuKB requested but macOS < 26; "
> +                        "falling back to host page size (%zuKB)",
> +                        (size_t)(granule / KiB),
> +                        (size_t)(qemu_real_host_page_size() / KiB));
> +            hvf_set_map_granule(qemu_real_host_page_size());
> +        }
> +        /*
> +         * Venus blob mapping safety: warn if the resolved map granule
> +         * exceeds the guest page size, as virtio-gpu blob BAR mappings
> +         * require page-aligned offsets and would hang otherwise.
> +         */
> +        if (hvf_get_map_granule() > qemu_target_page_size()) {
> +            warn_report("HVF map granule (%zu) > guest page size (%zu); "
> +                        "Venus blob BAR mappings may hang. "
> +                        "Consider ipa-granule=4k (requires macOS 26+) or "
> +                        "guest F_BLOB_ALIGNMENT support.",
> +                        (size_t)hvf_get_map_granule(),
> +                        qemu_target_page_size());
> +        }
The warning below doesn’t have much sense to have here I think.
At least not in this form.

With that warning removed: 

Reviewed-by: Mohamed Mediouni <mohamed@unpredictable.fr>
> +    }
> +
>     ret = hv_vm_create(config);
> 
> cleanup:
> -- 
> 2.52.0
> 
>