vmapple: making it work on the latest macOS host releases
v4 cover vmapple: making it work on the latest macOS host releases
Support for newer guest OSes isn't part of this series.
v4:
Rebase and resend without the RFC tag as I didn't get feedback.
Drop patch 1 (version I posted was incomplete anyways). Last patch isn't worth it too tbh.
v2->v3:
Remove the Apple M4 support workaround part for now, to be part of another patchset perhaps Address comments
v1->v2:
Remove some lines that shouldn't have been present...
Mohamed Mediouni (3):
vmapple: apple-gfx: make it work on the latest macOS release Revert "hw/arm: Do not build VMapple machine by default"
vmapple: apple-gfx: move legacy memory management APIs away from
inline
configs/devices/aarch64-softmmu/default.mak | 1 - hw/display/apple-gfx-mmio.m | 70 ++++++++++++++++----- hw/display/apple-gfx.h | 13 ++++ hw/display/apple-gfx.m | 47 +++++++++++++- 4 files changed, 114 insertions(+), 17 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v4 1/3 vmapple: apple-gfx: make it work on the latest macOS release
Follow changes in memory management introduced on macOS 15.4.
The legacy memory management API has been removed for the IOSurface mapper on that macOS version.
Also enable process isolation for a sandboxed GPU process when on a new OS.
--- hw/display/apple-gfx-mmio.m | 65 ++++++++++++++++++++++++++++--------- hw/display/apple-gfx.h | 13 ++++++++ hw/display/apple-gfx.m | 47 ++++++++++++++++++++++++++- 3 files changed, 109 insertions(+), 16 deletions(-)
hw/display/apple-gfx-mmio.m +50 -15
@@ -19,6 +19,7 @@ #include "hw/core/irq.h" #include "apple-gfx.h" #include "trace.h" +#include "system/address-spaces.h" #import <ParavirtualizedGraphics/ParavirtualizedGraphics.h>
@@ -34,14 +35,25 @@ typedef bool(^IOSFCMapMemory)(uint64_t phys, uint64_t len, bool ro, void **va, void *, void *); +@interface PGMemoryMapDescriptor : NSObject +-(void)addRange:(struct PGGuestPhysicalRange_s) range; +@end + @interface PGDeviceDescriptor (IOSurfaceMapper) @property (readwrite, nonatomic) bool usingIOSurfaceMapper; +@property (readwrite, nonatomic) bool enableArgumentBuffers; +@property (readwrite, nonatomic) bool enableProcessIsolation; +@property (readwrite, nonatomic) bool enableProtectedContent; + +@property (readwrite, nonatomic, copy, nullable) PGMemoryMapDescriptor* memoryMapDescriptor; @end @interface PGIOSurfaceHostDeviceDescriptor : NSObject -(PGIOSurfaceHostDeviceDescriptor *)init; @property (readwrite, nonatomic, copy, nullable) IOSFCMapMemory mapMemory; @property (readwrite, nonatomic, copy, nullable) IOSFCUnmapMemory unmapMemory; +@property (readwrite, nonatomic, copy, nullable) PGMemoryMapDescriptor* memoryMapDescriptor; +@property (readwrite, nonatomic) unsigned long long mmioLength; @property (readwrite, nonatomic, copy, nullable) IOSFCRaiseInterrupt raiseInterrupt; @end
@@ -182,20 +194,33 @@ static bool apple_gfx_mmio_unmap_surface_memory(void *ptr) PGIOSurfaceHostDeviceDescriptor *iosfc_desc = [PGIOSurfaceHostDeviceDescriptor new]; PGIOSurfaceHostDevice *iosfc_host_dev; - - iosfc_desc.mapMemory = - ^bool(uint64_t phys, uint64_t len, bool ro, void **va, void *e, void *f) { - *va = apple_gfx_mmio_map_surface_memory(phys, len, ro); - - trace_apple_gfx_iosfc_map_memory(phys, len, ro, va, e, f, *va); - - return *va != NULL; - }; - - iosfc_desc.unmapMemory = - ^bool(void *va, void *b, void *c, void *d, void *e, void *f) { - return apple_gfx_mmio_unmap_surface_memory(va); - }; + PGMemoryMapDescriptor* memoryMapDescriptor = [PGMemoryMapDescriptor new]; + + /* + * The legacy memory management API is no longer present + * for the IOSurface mapper as of macOS 15.4. + */ + if (@available(macOS 15.4, *)) { + FlatView* fv = address_space_to_flatview(&address_space_memory); + flatview_for_each_range(fv, apple_gfx_register_memory_cb, memoryMapDescriptor); + /* Single-page MMIO region: 16KB */ + iosfc_desc.mmioLength = 0x10000; + iosfc_desc.memoryMapDescriptor = memoryMapDescriptor; + } else { + iosfc_desc.mapMemory = + ^bool(uint64_t phys, uint64_t len, bool ro, void **va, void *e, void *f) { + *va = apple_gfx_mmio_map_surface_memory(phys, len, ro); + + trace_apple_gfx_iosfc_map_memory(phys, len, ro, va, e, f, *va); + + return *va != NULL; + }; + + iosfc_desc.unmapMemory = + ^bool(void *va, void *b, void *c, void *d, void *e, void *f) { + return apple_gfx_mmio_unmap_surface_memory(va); + }; + } iosfc_desc.raiseInterrupt = ^bool(uint32_t vector) { trace_apple_gfx_iosfc_raise_irq(vector);
@@ -223,13 +248,23 @@ static void apple_gfx_mmio_realize(DeviceState *dev, Error **errp) }; desc.usingIOSurfaceMapper = true; - s->pgiosfc = apple_gfx_prepare_iosurface_host_device(s); + desc.enableArgumentBuffers = true; + /* + * Process isolation needs PGMemoryMapDescriptor instead of + * the legacy memory management interface present in releases + * older than macOS 15.4. + */ + if (@available(macOS 15.4, *)) { + desc.enableProcessIsolation = true; + } if (!apple_gfx_common_realize(&s->common, dev, desc, errp)) { [s->pgiosfc release]; s->pgiosfc = nil; } + s->pgiosfc = apple_gfx_prepare_iosurface_host_device(s); + [desc release]; desc = nil; }
hw/display/apple-gfx.h +13 -0
@@ -23,6 +23,13 @@ @protocol MTLTexture; @protocol MTLCommandQueue; +typedef struct PGGuestPhysicalRange_s +{ +uint64_t physicalAddress; +uint64_t physicalLength; +void *hostAddress; +}PGGuestPhysicalRange_t ; + typedef QTAILQ_HEAD(, PGTask_s) PGTaskList; typedef struct AppleGFXDisplayMode {
@@ -68,6 +75,12 @@ void *apple_gfx_host_ptr_for_gpa_range(uint64_t guest_physical, uint64_t length, bool read_only, MemoryRegion **mapping_in_region); +bool apple_gfx_register_memory_cb (Int128 start, +Int128 len, +const MemoryRegion *mr, +hwaddr offset_in_region, +void *opaque); +extern const PropertyInfo qdev_prop_apple_gfx_display_mode;#endif
hw/display/apple-gfx.m +46 -1
65 lines not on this page · raw message
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH v4 1/3] vmapple: apple-gfx: make it work on the latest macOS release
Quoted 26 lines · Mohamed Mediouni
Follow changes in memory management introduced on macOS 15.4.
The legacy memory management API has been removed for the IOSurface mapper on that macOS version.
Also enable process isolation for a sandboxed GPU process when on a new OS.
Signed-off-by Mohamed Mediouni <[email protected]>--- hw/display/apple-gfx-mmio.m | 65 ++++++++++++++++++++++++++++--------- hw/display/apple-gfx.h | 13 ++++++++ hw/display/apple-gfx.m | 47 ++++++++++++++++++++++++++- 3 files changed, 109 insertions(+), 16 deletions(-)hw/display/apple-gfx-mmio.m +1
-0@@ -19,6 +19,7 @@ #include "hw/core/irq.h" #include "apple-gfx.h" #include "trace.h" +#include "system/address-spaces.h" #import <ParavirtualizedGraphics/ParavirtualizedGraphics.h>
hw/display/apple-gfx-mmio.m line 39
Quoted 5 lines
hw/display/apple-gfx-mmio.m +2
-0@@ -34,14 +35,25 @@ typedef bool(^IOSFCMapMemory)(uint64_t phys, uint64_t len, bool ro, void **va, void *, void *); +@interface PGMemoryMapDescriptor : NSObject +-(void)addRange:(struct PGGuestPhysicalRange_s) range;
Let's put the definition of PGGuestPhysicalRange_t before this and use it.
Also, the definitions of this interface nad that typedef are better moved to hw/display/apple-gfx.h to share them with:
hw/display/apple-gfx.m
Quoted 29 lines
hw/display/apple-gfx-mmio.m +9
-5+@end + @interface PGDeviceDescriptor (IOSurfaceMapper) @property (readwrite, nonatomic) bool usingIOSurfaceMapper; +@property (readwrite, nonatomic) bool enableArgumentBuffers; +@property (readwrite, nonatomic) bool enableProcessIsolation; +@property (readwrite, nonatomic) bool enableProtectedContent; + +@property (readwrite, nonatomic, copy, nullable) PGMemoryMapDescriptor* memoryMapDescriptor; @end @interface PGIOSurfaceHostDeviceDescriptor : NSObject -(PGIOSurfaceHostDeviceDescriptor *)init; @property (readwrite, nonatomic, copy, nullable) IOSFCMapMemory mapMemory; @property (readwrite, nonatomic, copy, nullable) IOSFCUnmapMemory unmapMemory; +@property (readwrite, nonatomic, copy, nullable) PGMemoryMapDescriptor* memoryMapDescriptor; +@property (readwrite, nonatomic) unsigned long long mmioLength; @property (readwrite, nonatomic, copy, nullable) IOSFCRaiseInterrupt raiseInterrupt; @end@@ -182,20 +194,33 @@ static bool apple_gfx_mmio_unmap_surface_memory(void *ptr) PGIOSurfaceHostDeviceDescriptor *iosfc_desc = [PGIOSurfaceHostDeviceDescriptor new]; PGIOSurfaceHostDevice *iosfc_host_dev; - - iosfc_desc.mapMemory = - ^bool(uint64_t phys, uint64_t len, bool ro, void **va, void *e, void *f) { - *va = apple_gfx_mmio_map_surface_memory(phys, len, ro); -
hw/display/apple-gfx-mmio.m static bool apple_gfx_mmio_unmap_surface_memory(void *ptr) · line 197
Quoted 10 lines
hw/display/apple-gfx-mmio.m +1
-9- trace_apple_gfx_iosfc_map_memory(phys, len, ro, va, e, f, *va); - - return *va != NULL; - }; - - iosfc_desc.unmapMemory = - ^bool(void *va, void *b, void *c, void *d, void *e, void *f) { - return apple_gfx_mmio_unmap_surface_memory(va); - }; + PGMemoryMapDescriptor* memoryMapDescriptor = [PGMemoryMapDescriptor new];
docs/devel/style.rst says "Variables are lower_case_with_underscores".
[PGMemoryMapDescriptor new] shouldn't be evaluated for old macOS.
hw/display/apple-gfx-mmio.m static bool apple_gfx_mmio_unmap_surface_memory(void *ptr) · line 206
Quoted 9 lines
hw/display/apple-gfx-mmio.m +9
-0+ + /* + * The legacy memory management API is no longer present + * for the IOSurface mapper as of macOS 15.4. + */ + if (@available(macOS 15.4, *)) { + FlatView* fv = address_space_to_flatview(&address_space_memory); + flatview_for_each_range(fv, apple_gfx_register_memory_cb, memoryMapDescriptor); + /* Single-page MMIO region: 16KB */
I don't think this explains why it needs to be 0x10000. The reason, in my understanding, is that it is because the memory map configured in apple_gfx_mmio_init() requires it to have that size.
Quoted 64 lines
hw/display/apple-gfx-mmio.m +28
-1+ iosfc_desc.mmioLength = 0x10000; + iosfc_desc.memoryMapDescriptor = memoryMapDescriptor; + } else { + iosfc_desc.mapMemory = + ^bool(uint64_t phys, uint64_t len, bool ro, void **va, void *e, void *f) { + *va = apple_gfx_mmio_map_surface_memory(phys, len, ro); + + trace_apple_gfx_iosfc_map_memory(phys, len, ro, va, e, f, *va); + + return *va != NULL; + }; + + iosfc_desc.unmapMemory = + ^bool(void *va, void *b, void *c, void *d, void *e, void *f) { + return apple_gfx_mmio_unmap_surface_memory(va); + }; + } iosfc_desc.raiseInterrupt = ^bool(uint32_t vector) { trace_apple_gfx_iosfc_raise_irq(vector);@@ -223,13 +248,23 @@ static void apple_gfx_mmio_realize(DeviceState *dev, Error **errp) }; desc.usingIOSurfaceMapper = true; - s->pgiosfc = apple_gfx_prepare_iosurface_host_device(s); + desc.enableArgumentBuffers = true; + /* + * Process isolation needs PGMemoryMapDescriptor instead of + * the legacy memory management interface present in releases + * older than macOS 15.4. + */ + if (@available(macOS 15.4, *)) { + desc.enableProcessIsolation = true; + } if (!apple_gfx_common_realize(&s->common, dev, desc, errp)) { [s->pgiosfc release]; s->pgiosfc = nil; } + s->pgiosfc = apple_gfx_prepare_iosurface_host_device(s); + [desc release]; desc = nil; }hw/display/apple-gfx.h +7
-0@@ -23,6 +23,13 @@ @protocol MTLTexture; @protocol MTLCommandQueue; +typedef struct PGGuestPhysicalRange_s +{ +uint64_t physicalAddress; +uint64_t physicalLength; +void *hostAddress; +}PGGuestPhysicalRange_t ; + typedef QTAILQ_HEAD(, PGTask_s) PGTaskList; typedef struct AppleGFXDisplayMode {
hw/display/apple-gfx.h void *apple_gfx_host_ptr_for_gpa_range(uint64_t guest_physical, · line 82
Quoted 8 lines
hw/display/apple-gfx.h +5
-0@@ -68,6 +75,12 @@ void *apple_gfx_host_ptr_for_gpa_range(uint64_t guest_physical, uint64_t length, bool read_only, MemoryRegion **mapping_in_region); +bool apple_gfx_register_memory_cb (Int128 start, +Int128 len, +const MemoryRegion *mr, +hwaddr offset_in_region, +void *opaque);
The indents are misaligned.
Quoted 26 lines
hw/display/apple-gfx.h +1
-0+extern const PropertyInfo qdev_prop_apple_gfx_display_mode;#endif hw/display/apple-gfx.m +7
-0@@ -21,6 +21,7 @@ #include "system/address-spaces.h" #include "system/dma.h" #include "migration/blocker.h" +#include "system/memory.h" #include "ui/console.h" #include "apple-gfx.h" #include "trace.h"@@ -596,6 +597,41 @@ void apple_gfx_common_init(Object *obj, AppleGFXState *s, const char* obj_name) /* TODO: PVG framework supports serialising device state: integrate it! */ } +@interface PGMemoryMapDescriptor : NSObject +-(void)addRange:(struct PGGuestPhysicalRange_s) range; +@end + +@interface PGDeviceDescriptor (IOSurfaceMapper) +@property (readwrite, nonatomic, copy, nullable) PGMemoryMapDescriptor* memoryMapDescriptor;
hw/display/apple-gfx.m void apple_gfx_common_init(Object *obj, AppleGFXState *s, const char* obj_name) · line 615
Quoted 10 lines
hw/display/apple-gfx.m +10
-0+@end + +bool apple_gfx_register_memory_cb(Int128 start, + Int128 len, + const MemoryRegion *mr, + hwaddr offset_in_region, + void *opaque) { + PGGuestPhysicalRange_t range; + PGMemoryMapDescriptor* memoryMapDescriptor = opaque; + if (mr->ram) {
memory_access_is_direct() should be used instead to reject all incompatible memory regions.
hw/display/apple-gfx.m void apple_gfx_common_init(Object *obj, AppleGFXState *s, const char* obj_name) · line 618
Quoted 3 lines
hw/display/apple-gfx.m +3
-0+ range.physicalAddress = start; + range.physicalLength = len; + range.hostAddress = memory_region_get_ram_ptr(mr);
This should call memory_region_ref() as apple_gfx_task_map_memory() does.
Regards,
Akihiko Odaki
Quoted 37 lines
hw/display/apple-gfx.m +26
-1+ [memoryMapDescriptor addRange:range]; + } + return false; +} + +static void apple_gfx_register_memory(AppleGFXState *s, + PGDeviceDescriptor *desc) +{ + PGMemoryMapDescriptor* memoryMapDescriptor = [PGMemoryMapDescriptor new]; + + FlatView* fv = address_space_to_flatview(&address_space_memory); + flatview_for_each_range(fv, apple_gfx_register_memory_cb, memoryMapDescriptor); + + desc.memoryMapDescriptor = memoryMapDescriptor; +} + static void apple_gfx_register_task_mapping_handlers(AppleGFXState *s, PGDeviceDescriptor *desc) {@@ -763,7 +799,16 @@ bool apple_gfx_common_realize(AppleGFXState *s, DeviceState *dev, desc.device = s->mtl; - apple_gfx_register_task_mapping_handlers(s, desc); + /* + * The legacy memory management interface doesn't allow for + * vGPU sandboxing. As such, always use the new interface + * on macOS 15.4 onwards. + */ + if (@available(macOS 15.4, *)) { + apple_gfx_register_memory(s, desc); + } else { + apple_gfx_register_task_mapping_handlers(s, desc); + } s->cursor_show = true;
PATCH v4 2/3 Revert "hw/arm: Do not build VMapple machine by default"
In reply to Mohamed MediouniThe vmapple virtual machine is now functional on newer macOS host releases. Revert commit 49551752e860f5e403cdacac11ee1d218141fd3d.
--- configs/devices/aarch64-softmmu/default.mak | 1 - 1 file changed, 1 deletion(-)
configs/devices/aarch64-softmmu/default.mak +0 -1
@@ -9,4 +9,3 @@ include ../arm-softmmu/default.mak # CONFIG_XLNX_VERSAL=n # CONFIG_SBSA_REF=n # CONFIG_NPCM8XX=n -CONFIG_VMAPPLE=n
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v4 3/3 vmapple: apple-gfx: move legacy memory management APIs away from inline
In reply to Mohamed MediouniThey're not function pointers but blocks.
Adding this to address review comments but I don't think it's worthwhile.
--- hw/display/apple-gfx-mmio.m | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-)
hw/display/apple-gfx-mmio.m +18 -13
@@ -188,6 +188,22 @@ static bool apple_gfx_mmio_unmap_surface_memory(void *ptr) return true; } +/* Legacy memory management API: Gone in macOS 15.4 and later. */ +static bool (^apple_gfx_map_memory) + (uint64_t phys, uint64_t len, bool ro, void **va, void *e, void *f) + = ^bool(uint64_t phys, uint64_t len, bool ro, void **va, void *e, void *f){ + *va = apple_gfx_mmio_map_surface_memory(phys, len, ro); + + trace_apple_gfx_iosfc_map_memory(phys, len, ro, va, e, f, *va); + + return *va != NULL; +}; + +static bool (^apple_gfx_unmap_memory)(void *va, void *b, void *c, void *d, void *e, void *f) + = ^bool(void *va, void *b, void *c, void *d, void *e, void *f){ + return apple_gfx_mmio_unmap_surface_memory(va); +}; + static PGIOSurfaceHostDevice *apple_gfx_prepare_iosurface_host_device( AppleGFXMMIOState *s) {
@@ -207,19 +223,8 @@ static bool apple_gfx_mmio_unmap_surface_memory(void *ptr) iosfc_desc.mmioLength = 0x10000; iosfc_desc.memoryMapDescriptor = memoryMapDescriptor; } else { - iosfc_desc.mapMemory = - ^bool(uint64_t phys, uint64_t len, bool ro, void **va, void *e, void *f) { - *va = apple_gfx_mmio_map_surface_memory(phys, len, ro); - - trace_apple_gfx_iosfc_map_memory(phys, len, ro, va, e, f, *va); - - return *va != NULL; - }; - - iosfc_desc.unmapMemory = - ^bool(void *va, void *b, void *c, void *d, void *e, void *f) { - return apple_gfx_mmio_unmap_surface_memory(va); - }; + iosfc_desc.mapMemory = apple_gfx_map_memory; + iosfc_desc.unmapMemory = apple_gfx_unmap_memory; } iosfc_desc.raiseInterrupt = ^bool(uint32_t vector) {
Signature · 1 line
2.50.1 (Apple Git-155)
Re: [PATCH v4 3/3] vmapple: apple-gfx: move legacy memory management APIs away from inline
Quoted 2 lines · Mohamed Mediouni
They're not function pointers but blocks.
Adding this to address review comments but I don't think it's worthwhile.
Address review comments by *either* making an argument against review comments or making a change according to them; don't do both and send send a mixed signal.
I think your argument is sensible and this patch should be dropped.
Regards,
Akihiko Odaki
Quoted 55 lines
Signed-off-by Mohamed Mediouni <[email protected]>--- hw/display/apple-gfx-mmio.m | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-)hw/display/apple-gfx-mmio.m +18
-13@@ -188,6 +188,22 @@ static bool apple_gfx_mmio_unmap_surface_memory(void *ptr) return true; } +/* Legacy memory management API: Gone in macOS 15.4 and later. */ +static bool (^apple_gfx_map_memory) + (uint64_t phys, uint64_t len, bool ro, void **va, void *e, void *f) + = ^bool(uint64_t phys, uint64_t len, bool ro, void **va, void *e, void *f){ + *va = apple_gfx_mmio_map_surface_memory(phys, len, ro); + + trace_apple_gfx_iosfc_map_memory(phys, len, ro, va, e, f, *va); + + return *va != NULL; +}; + +static bool (^apple_gfx_unmap_memory)(void *va, void *b, void *c, void *d, void *e, void *f) + = ^bool(void *va, void *b, void *c, void *d, void *e, void *f){ + return apple_gfx_mmio_unmap_surface_memory(va); +}; + static PGIOSurfaceHostDevice *apple_gfx_prepare_iosurface_host_device( AppleGFXMMIOState *s) {@@ -207,19 +223,8 @@ static bool apple_gfx_mmio_unmap_surface_memory(void *ptr) iosfc_desc.mmioLength = 0x10000; iosfc_desc.memoryMapDescriptor = memoryMapDescriptor; } else { - iosfc_desc.mapMemory = - ^bool(uint64_t phys, uint64_t len, bool ro, void **va, void *e, void *f) { - *va = apple_gfx_mmio_map_surface_memory(phys, len, ro); - - trace_apple_gfx_iosfc_map_memory(phys, len, ro, va, e, f, *va); - - return *va != NULL; - }; - - iosfc_desc.unmapMemory = - ^bool(void *va, void *b, void *c, void *d, void *e, void *f) { - return apple_gfx_mmio_unmap_surface_memory(va); - }; + iosfc_desc.mapMemory = apple_gfx_map_memory; + iosfc_desc.unmapMemory = apple_gfx_unmap_memory; } iosfc_desc.raiseInterrupt = ^bool(uint32_t vector) {
Cc'ing Akihiko
Quoted 25 lines · Mohamed Mediouni
Support for newer guest OSes isn't part of this series.
v4:
Rebase and resend without the RFC tag as I didn't get feedback.
Drop patch 1 (version I posted was incomplete anyways). Last patch isn't worth it too tbh.
v2->v3:
Remove the Apple M4 support workaround part for now, to be part of another patchset perhaps Address comments
v1->v2:
Remove some lines that shouldn't have been present...Mohamed Mediouni (3):
vmapple: apple-gfx: make it work on the latest macOS release Revert "hw/arm: Do not build VMapple machine by default"
vmapple: apple-gfx: move legacy memory management APIs away from
inlineconfigs/devices/aarch64-softmmu/default.mak | 1 - hw/display/apple-gfx-mmio.m | 70 ++++++++++++++++----- hw/display/apple-gfx.h | 13 ++++ hw/display/apple-gfx.m | 47 +++++++++++++- 4 files changed, 114 insertions(+), 17 deletions(-)