hvf: support changing IPA granule size
PATCH 1/1 hvf: support changing IPA granule size
The IPA granule is the smallest page size hv_vm_map() support. For Venus, we need to support 4KiB pages. macOS 26 introduces a public API for setting the granule size. We can only use this when compiled with macOS 26 SDK and run on macOS 26+. Otherwise, we fall back to an older, private, API which achieves the same purpose.
--- include/system/hvf_int.h | 4 +++- accel/hvf/hvf-all.c | 42 ++++++++++++++++++++++++++++++++- target/arm/hvf/hvf.c | 50 +++++++++++++++++++++++++++++++++++++++- target/i386/hvf/hvf.c | 10 +++++++- 4 files changed, 102 insertions(+), 4 deletions(-)
include/system/hvf_int.h +3 -1
@@ -63,6 +63,7 @@ struct HVFState {hvf_vcpu_caps *hvf_caps;uint64_t vtimer_offset; +uint32_t ipa_granule_size; QTAILQ_HEAD(, hvf_sw_breakpoint) hvf_sw_breakpoints; };extern HVFState *hvf_state;
@@ -82,7 +83,8 @@ void assert_hvf_ok_impl(hv_return_t ret, const char *file, unsigned int line,#define assert_hvf_ok (EX ) assert_hvf_ok_impl((EX), __FILE__, __LINE__, #EX)const char *hvf_return_string (hv_return_t ret);int hvf_arch_init (void ); -hv_return_t hvf_arch_vm_create (MachineState *ms,uint32_t pa_range); +hv_return_t hvf_arch_vm_create (MachineState *ms,uint32_t pa_range, +uint32_t ipa_granule_size);hvf_slot *hvf_find_overlap_slot (uint64_t ,uint64_t );void hvf_kick_vcpu_thread (CPUState *cpu);
accel/hvf/hvf-all.c +41 -1
72 lines not on this page · raw message
target/arm/hvf/hvf.c +49 -1
68 lines not on this page · raw message
target/i386/hvf/hvf.c +9 -1
@@ -225,8 +225,16 @@ int hvf_arch_init(void)return 0 ; } -hv_return_t hvf_arch_vm_create (MachineState *ms,uint32_t pa_range) +hv_return_t hvf_arch_vm_create (MachineState *ms,uint32_t pa_range, +uint32_t ipa_granule_size) { +uint64_t page_size =qemu_real_host_page_size (); + +if (ipa_granule_size !=0 && ipa_granule_size != page_size) { +error_report ("Only supported IPA granule size: 0x%llx" , page_size); +return HV_UNSUPPORTED ; + } +return hv_vm_create (HV_VM_DEFAULT ); }
Signature · 1 line
2.50.1 (Apple Git-155)
Hello,
Quoted 8 lines · Joelle van Dyne
The IPA granule is the smallest page size
hv_vm_map()support. For Venus, we need to support 4KiB pages. macOS 26 introduces a public API for setting the granule size. We can only use this when compiled with macOS 26 SDK and run on macOS 26+. Otherwise, we fall back to an older, private, API which achieves the same purpose.
Let’s have an HVF_NO_PRIVATE_API define (or the opposite) to have a single toggle to disable all private API use at build time
Quoted 21 lines
target/arm/hvf/hvf.c +17
-0+ +/* older macOS need to use a private API */ +if (!set_ipa_granule) { + set_ipa_granule =dlsym (RTLD_NEXT ,"_hv_vm_config_set_ipa_granule" ); + } +if (set_ipa_granule) { +return set_ipa_granule (config, ipa_granule_size); + }else if (ipa_granule_size != page_size) { +error_report ("Failed to find _hv_vm_config_set_ipa_granule" ); +return HV_UNSUPPORTED ; + } + +return HV_SUCCESS ; +} + +hv_return_t hvf_arch_vm_create (MachineState *ms,uint32_t pa_range, +uint32_t ipa_granule_size){ hv_return_t ret; hv_vm_config_t config = hv_vm_config_create();
target/arm/hvf/hvf.c hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range) · line 934
Quoted 3 lines
target/arm/hvf/hvf.c +0
-0@@ -891,6 +932,13 @@ hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range) } chosen_ipa_bit_size = pa_range;
The PA range is actually different depending on the IPA granule size.
An example from M4 Max:
% sysctl -a | grep ipa
kern.hv.ipa_size_16k: 4398046511104
kern.hv.ipa_size_4k: 1099511627776
Quoted 9 lines
target/arm/hvf/hvf.c +7
-0+if (ipa_granule_size) { + ret =hvf_set_ipa_granule (config, ipa_granule_size); +if (ret !=HV_SUCCESS ) { + goto cleanup; + } + } + ret =hv_vm_create (config);
Quoted 43 lines · Mohamed Mediouni
Hello,
Quoted 8 lines · Joelle van Dyne
The IPA granule is the smallest page size
hv_vm_map()support. For Venus, we need to support 4KiB pages. macOS 26 introduces a public API for setting the granule size. We can only use this when compiled with macOS 26 SDK and run on macOS 26+. Otherwise, we fall back to an older, private, API which achieves the same purpose.Let’s have an
HVF_NO_PRIVATE_APIdefine (or the opposite) to have a single toggle to disable all private API use at build timeQuoted 21 lines
target/arm/hvf/hvf.c +17
-0+ +/* older macOS need to use a private API */ +if (!set_ipa_granule) { + set_ipa_granule =dlsym (RTLD_NEXT ,"_hv_vm_config_set_ipa_granule" ); + } +if (set_ipa_granule) { +return set_ipa_granule (config, ipa_granule_size); + }else if (ipa_granule_size != page_size) { +error_report ("Failed to find _hv_vm_config_set_ipa_granule" ); +return HV_UNSUPPORTED ; + } + +return HV_SUCCESS ; +} + +hv_return_t hvf_arch_vm_create (MachineState *ms,uint32_t pa_range, +uint32_t ipa_granule_size){ hv_return_t ret; hv_vm_config_t config = hv_vm_config_create();target/arm/hvf/hvf.c hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range) · line 934
Quoted 3 lines
target/arm/hvf/hvf.c +0
-0@@ -891,6 +932,13 @@ hv_return_t hvf_arch_vm_create(MachineState *ms, uint32_t pa_range) } chosen_ipa_bit_size = pa_range;The PA range is actually different depending on the IPA granule size.
Quoted 6 lines
An example from M4 Max:
% sysctl -a | grep ipa
kern.hv.ipa_size_16k: 4398046511104
kern.hv.ipa_size_4k: 1099511627776
Looks like the Apple APIs will always return the smallest supported IPA space size instead of the current one
so this is actually ok - but doesn’t provide access to the bigger IPA space the hardware supports unless explicitly queried via sysctl…
Something else to add as private API use I guess...
Quoted 11 lines
Quoted 9 lines
target/arm/hvf/hvf.c +6
-0+ if (ipa_granule_size) { + ret =hvf_set_ipa_granule (config, ipa_granule_size); +if (ret !=HV_SUCCESS ) { + goto cleanup; + } + } + ret =hv_vm_create (config);