1
<meta>
2
This patch series has been through months of review and
3
refinement. It now has end-to-end Reviewed-by: tags and
4
all code patches but one have Tested-by: tags. No significant
5
issues have been found via review for some weeks.
6
7
The patch set creates two new subsystems:
8
hw/display/apple-gfx
9
hw/vmapple
10
so it doesn't fall within the responsibility of existing
11
maintainers. How do we proceed to get this merged now that
12
10.0 development is open?
13
</meta>
14
15
1
This patch set introduces a new ARM and macOS HVF specific machine type
16
This patch set introduces a new ARM and macOS HVF specific machine type
2
called "vmapple", as well as a family of display devices based on the
17
called "vmapple", as well as a family of display devices based on the
3
ParavirtualizedGraphics.framework in macOS. One of the display adapter
18
ParavirtualizedGraphics.framework in macOS. One of the display adapter
4
variants, apple-gfx-mmio, is required for the new machine type, while
19
variants, apple-gfx-mmio, is required for the new machine type, while
5
apple-gfx-pci can be used to enable 3D graphics acceleration with x86-64
20
apple-gfx-pci can be used to enable 3D graphics acceleration with x86-64
...
...
31
hosts only because ParavirtualizedGraphics.framework is a black box
46
hosts only because ParavirtualizedGraphics.framework is a black box
32
implementing most of the logic behind the apple-gfx device.)
47
implementing most of the logic behind the apple-gfx device.)
33
* PCI devices use legacy IRQs, not MSI/MSI-X. As far as I can tell,
48
* PCI devices use legacy IRQs, not MSI/MSI-X. As far as I can tell,
34
we'd need to include the GICv3 ITS, but it's unclear to me what
49
we'd need to include the GICv3 ITS, but it's unclear to me what
35
exactly needs wiring up.
50
exactly needs wiring up.
36
* Due to lack of MSI(-X), event delivery from USB devices to the guest
51
* Due to a quirk (bug?) in the macOS XHCI driver when MSI-X is not
37
macOS isn't working correctly. My current conclusion is that the
52
available, correct functioning of the USB controller (and thus
38
OS's XHCI driver simply was never designed to work with legacy IRQs.
53
keyboard/tablet) requires a small workaround in the XHCI controller
39
The upshot is that keyboard and mouse/tablet input is very laggy.
54
device. This is part of another patch series:
40
The solution would be to implement MSI(-X) support or figure out how
55
https://patchew.org/QEMU/20241208191646.64857-1-phil@philjordan.eu/
41
to make hcd-xhci-sysbus work with the macOS guest, if at all possible.
42
(EHCI and UHCI/OHCI controllers are not an option as the VMAPPLE
43
guest kernel does not include drivers for these.)
44
* The guest OS must first be provisioned using Virtualization.framework;
56
* The guest OS must first be provisioned using Virtualization.framework;
45
the disk images can subsequently be used in Qemu. (See docs.)
57
the disk images can subsequently be used in Qemu. (See docs.)
46
58
47
The apple-gfx device can be used independently from the vmapple machine
59
The apple-gfx device can be used independently from the vmapple machine
48
type, at least in the PCI variant. It mainly targets x86-64 macOS guests
60
type, at least in the PCI variant. It mainly targets x86-64 macOS guests
...
...
69
CPU-based drawing. For maximum efficiency, the Metal texture
81
CPU-based drawing. For maximum efficiency, the Metal texture
70
containing the guest framebuffer could be drawn directly to
82
containing the guest framebuffer could be drawn directly to
71
a Metal view in the host window, staying on the GPU. (Similar
83
a Metal view in the host window, staying on the GPU. (Similar
72
to the OpenGL/virgl render path on other platforms.)
84
to the OpenGL/virgl render path on other platforms.)
73
85
74
My part of this work has been sponsored by Sauce Labs Inc.
86
Some of my part of this work has been sponsored by Sauce Labs Inc.
75
87
76
---
88
---
77
89
78
v2 -> v3:
90
v2 -> v3:
79
91
...
...
135
docs.
147
docs.
136
* Various smaller fixes in apple-gfx/-mmio, apple-gfx-pci, vmapple/aes,
148
* Various smaller fixes in apple-gfx/-mmio, apple-gfx-pci, vmapple/aes,
137
vmapple/cfg, vmapple/virtio-blk, and vmapple machine type.
149
vmapple/cfg, vmapple/virtio-blk, and vmapple machine type.
138
* Added SPDX license identifiers where they were missing.
150
* Added SPDX license identifiers where they were missing.
139
151
140
v5 -> v6
152
v5 -> v6:
141
153
142
* 01/15 (main/Cocoa/runloop): Combined functions, fixed whitespace
154
* 01/15 (main/Cocoa/runloop): Combined functions, fixed whitespace
143
* 02/15 (apple-gfx): Further refinement of PVG threading: reduced some callback
155
* 02/15 (apple-gfx): Further refinement of PVG threading: reduced some callback
144
tasks from BHs to merely acquiring RCU read lock; replaced some libdispatch
156
tasks from BHs to merely acquiring RCU read lock; replaced some libdispatch
145
tasks with BHs; last remaining synchronous BH now uses emphemeral
157
tasks with BHs; last remaining synchronous BH now uses emphemeral
146
QemuSemaphore.
158
QemuSemaphore.
147
* 02/15 (apple-gfx): Readability improvements and other smaller tweaks
159
* 02/15 (apple-gfx): Readability improvements and other smaller tweaks
148
(see patch change notes for details)
160
(see patch change notes for details)
149
* 04/15 (display modes): Replaced use of alloca() with NSMutableArray.
161
* 04/15 (display modes): Replaced use of alloca() with NSMutableArray.
162
163
v6 -> v7:
164
165
* 02/15 (apple-gfx): Use g_ptr_array_find() helper function, coding style tweak
166
* 03/15 (apple-gfx-pci): Removed an unused function parameter
167
* 04/15 (apple-gfx display mode property): Simplified error handling in
168
property parsing.
169
* 10/15 (vmapple/aes): Coding style tweaks.
170
* 12/15 (vmapple/cfg): Changed error messages for overrun of properties with
171
fixed-length strings to be more useful to users than developers.
172
* 15/15 (vmapple machine type): Tiny error handling fix, un-inlined function
173
174
v7 -> v8:
175
176
* 02/15 (apple-gfx): Naming and type use improvements, fixes for a bug and a
177
leak.
178
* 04/15 (apple-gfx display mode property): Type use improvement
179
* 10/15 (vmapple/aes): Guest error logging tweaks.
180
* 11/15 (vmapple/bdif): Replaced uses of cpu_physical_memory_read with
181
dma_memory_read, and a g_free call with g_autofree.
182
* 12/15 (vmapple/cfg): Macro hygiene fix: consistently enclosing arguments in
183
parens.
184
* 15/15 (vmapple machine type): Use less verbose pattern for defining uuid
185
property.
186
187
v8 -> v9:
188
189
* 01/16 (ui & main loop): Set qemu_main to NULL for GTK UI as well.
190
* 02/16 (apple-gfx): Pass device pointer to graphic_console_init(), various
191
     non-functional changes.
192
* 03/16 (apple-gfx-pci): Fixup of changed common call, whitespace and comment
193
formatting tweaks.
194
* 04/16 (apple-gfx display modes): Re-ordered type definitions so we can drop
195
a 'struct' keyword.
196
* 10/16 (vmapple/aes): Replaced a use of cpu_physical_memory_write with
197
dma_memory_write, minor style tweak.
198
* 11/16 (vmapple/bdif): Replaced uses of cpu_physical_memory_write with
199
dma_memory_write.
200
* 13/16 (vmapple/virtio-blk): Correctly specify class_size for
201
VMAppleVirtIOBlkClass.
202
* 15/16 (vmapple machine type): Documentation improvements, fixed variable
203
name and struct field used during pvpanic device creation.
204
* 16/16 (NEW/RFC vmapple/virtio-blk): Proposed change to replace type hierarchy
205
with a variant property. This seems cleaner and less confusing than the
206
original approach to me, but I'm not sure if it warrants creation of a new
207
QAPI enum and property type definition.
208
209
v9 -> v10:
210
211
* 01/15 (ui & main loop): Added comments to qemu_main declaration and GTK.
212
* 02/15 (apple-gfx): Reworked the way frame rendering code is threaded to use
213
BHs for sections requiring BQL.
214
* 02/15 (apple-gfx): Fixed ./configure error on non-macOS platforms.
215
* 10/15 (vmapple/aes): Code style and comment improvements.
216
* 12/15 (vmapple/cfg): Slightly tidier error reporting for overlong property
217
values.
218
* 13/15 (vmapple/virtio-blk): Folded v9 patch 16/16 into this one, changing
219
the device type design to provide a single device type with a variant
220
     property instead of 2 different subtypes for aux and root volumes.
221
* 15/15 (vmapple machine type): Documentation fixup for changed virtio-blk
222
device type; small improvements to shell commands in documentation;
223
improved propagation of errors during cfg device instantiation.
224
225
v10 -> v11:
226
227
* 01/15 (ui & main loop): Simplified main.c, better comments & commit message
228
* 02/15 (apple-gfx): Give each PV display instance a unique serial number.
229
* 02 & 03/15 (apple-gfx, -pci): Formatting/style tweaks
230
* 15/15 (vmapple machine type): Improvements to shell code in docs
231
232
v11 -> v12:
233
234
* 01/15 (ui & main loop): More precise wording of code comments.
235
* 02/15 (apple-gfx): Fixed memory management regressions introduced in v10;
236
improved error handling; various more conmetic code adjustments
237
* 09/15 (GPEX): Fixed uses of deleted GPEX_NUM_IRQS constant that have been
238
added to QEMU since this patch was originally written.
239
240
v12 -> v13:
241
242
* 15/15 (vmapple machine type): Bumped the machine type version from 9.2
243
to 10.0.
244
* All patches in the series now have been positively reviewed and received
245
corresponding reviewed-by tags.
246
247
v13 -> v14:
248
249
* 6/15 (hw/vmapple directory): Changed myself from reviewer
250
to maintainer, as that seemed appropriate at this point.
251
* 15/15 (vmapple machine type): Gate creation of XHCI and
252
USB HID devices behind if (defaults_enabled()).
150
253
151
Alexander Graf (9):
254
Alexander Graf (9):
152
hw: Add vmapple subdir
255
hw: Add vmapple subdir
153
hw/misc/pvpanic: Add MMIO interface
256
hw/misc/pvpanic: Add MMIO interface
154
hvf: arm: Ignore writes to CNTP_CTL_EL0
257
hvf: arm: Ignore writes to CNTP_CTL_EL0
...
...
166
hw/display/apple-gfx: Adds PCI implementation
269
hw/display/apple-gfx: Adds PCI implementation
167
hw/display/apple-gfx: Adds configurable mode list
270
hw/display/apple-gfx: Adds configurable mode list
168
MAINTAINERS: Add myself as maintainer for apple-gfx, reviewer for HVF
271
MAINTAINERS: Add myself as maintainer for apple-gfx, reviewer for HVF
169
hw/block/virtio-blk: Replaces request free function with g_free
272
hw/block/virtio-blk: Replaces request free function with g_free
170
273
171
MAINTAINERS | 15 +
274
MAINTAINERS | 15 +
172
contrib/vmapple/uuid.sh | 9 +
275
contrib/vmapple/uuid.sh | 9 +
173
docs/system/arm/vmapple.rst | 60 +++
276
docs/system/arm/vmapple.rst | 63 ++
174
docs/system/target-arm.rst | 1 +
277
docs/system/target-arm.rst | 1 +
175
hw/Kconfig | 1 +
278
hw/Kconfig | 1 +
176
hw/arm/sbsa-ref.c | 2 +-
279
hw/arm/sbsa-ref.c | 2 +-
177
hw/arm/virt.c | 2 +-
280
hw/arm/virt.c | 2 +-
178
hw/block/virtio-blk.c | 58 ++-
281
hw/block/virtio-blk.c | 58 +-
179
hw/display/Kconfig | 13 +
282
hw/core/qdev-properties-system.c | 8 +
180
hw/display/apple-gfx-mmio.m | 289 +++++++++++
283
hw/display/Kconfig | 13 +
181
hw/display/apple-gfx-pci.m | 156 ++++++
284
hw/display/apple-gfx-mmio.m | 289 +++++++++
182
hw/display/apple-gfx.h | 77 +++
285
hw/display/apple-gfx-pci.m | 157 +++++
183
hw/display/apple-gfx.m | 872 +++++++++++++++++++++++++++++++++
286
hw/display/apple-gfx.h | 77 +++
184
hw/display/meson.build | 5 +
287
hw/display/apple-gfx.m | 880 ++++++++++++++++++++++++++++
185
hw/display/trace-events | 30 ++
288
hw/display/meson.build | 7 +
186
hw/i386/microvm.c | 2 +-
289
hw/display/trace-events | 30 +
187
hw/loongarch/virt.c | 2 +-
290
hw/i386/microvm.c | 2 +-
188
hw/meson.build | 1 +
291
hw/loongarch/virt.c | 12 +-
189
hw/mips/loongson3_virt.c | 2 +-
292
hw/meson.build | 1 +
190
hw/misc/Kconfig | 4 +
293
hw/mips/loongson3_virt.c | 2 +-
191
hw/misc/meson.build | 1 +
294
hw/misc/Kconfig | 4 +
192
hw/misc/pvpanic-mmio.c | 61 +++
295
hw/misc/meson.build | 1 +
193
hw/openrisc/virt.c | 12 +-
296
hw/misc/pvpanic-mmio.c | 61 ++
194
hw/pci-host/gpex.c | 43 +-
297
hw/openrisc/virt.c | 12 +-
195
hw/riscv/virt.c | 12 +-
298
hw/pci-host/gpex.c | 43 +-
196
hw/vmapple/Kconfig | 32 ++
299
hw/riscv/virt.c | 12 +-
197
hw/vmapple/aes.c | 578 ++++++++++++++++++++++
300
hw/vmapple/Kconfig | 32 +
198
hw/vmapple/bdif.c | 261 ++++++++++
301
hw/vmapple/aes.c | 581 ++++++++++++++++++
199
hw/vmapple/cfg.c | 203 ++++++++
302
hw/vmapple/bdif.c | 275 +++++++++
200
hw/vmapple/meson.build | 5 +
303
hw/vmapple/cfg.c | 196 +++++++
201
hw/vmapple/trace-events | 21 +
304
hw/vmapple/meson.build | 5 +
202
hw/vmapple/trace.h | 1 +
305
hw/vmapple/trace-events | 21 +
203
hw/vmapple/virtio-blk.c | 226 +++++++++
306
hw/vmapple/trace.h | 1 +
204
hw/vmapple/vmapple.c | 659 +++++++++++++++++++++++++
307
hw/vmapple/virtio-blk.c | 205 +++++++
205
hw/xtensa/virt.c | 2 +-
308
hw/vmapple/vmapple.c | 648 ++++++++++++++++++++
206
include/hw/misc/pvpanic.h | 1 +
309
hw/xen/xen-pvh-common.c | 2 +-
207
include/hw/pci-host/gpex.h | 7 +-
310
hw/xtensa/virt.c | 2 +-
208
include/hw/pci/pci_ids.h | 1 +
311
include/hw/misc/pvpanic.h | 1 +
209
include/hw/virtio/virtio-blk.h | 11 +-
312
include/hw/pci-host/gpex.h | 7 +-
210
include/hw/vmapple/vmapple.h | 25 +
313
include/hw/pci/pci_ids.h | 1 +
211
include/qemu-main.h | 3 +-
314
include/hw/qdev-properties-system.h | 5 +
212
include/qemu/cutils.h | 15 +
315
include/hw/virtio/virtio-blk.h | 11 +-
213
include/qemu/typedefs.h | 1 +
316
include/hw/vmapple/vmapple.h | 23 +
214
meson.build | 5 +
317
include/qemu-main.h | 14 +-
215
system/main.c | 50 +-
318
include/qemu/cutils.h | 15 +
216
target/arm/hvf/hvf.c | 9 +
319
meson.build | 5 +
217
ui/cocoa.m | 54 +-
320
qapi/virtio.json | 14 +
218
ui/sdl2.c | 4 +
321
system/main.c | 37 +-
219
util/hexdump.c | 18 +
322
target/arm/hvf/hvf.c | 9 +
220
49 files changed, 3816 insertions(+), 106 deletions(-)
323
ui/cocoa.m | 54 +-
324
ui/gtk.c | 4 +
325
ui/sdl2.c | 4 +
326
util/hexdump.c | 18 +
327
53 files changed, 3842 insertions(+), 110 deletions(-)
221
create mode 100755 contrib/vmapple/uuid.sh
328
create mode 100755 contrib/vmapple/uuid.sh
222
create mode 100644 docs/system/arm/vmapple.rst
329
create mode 100644 docs/system/arm/vmapple.rst
223
create mode 100644 hw/display/apple-gfx-mmio.m
330
create mode 100644 hw/display/apple-gfx-mmio.m
224
create mode 100644 hw/display/apple-gfx-pci.m
331
create mode 100644 hw/display/apple-gfx-pci.m
225
create mode 100644 hw/display/apple-gfx.h
332
create mode 100644 hw/display/apple-gfx.h
...
...
235
create mode 100644 hw/vmapple/virtio-blk.c
342
create mode 100644 hw/vmapple/virtio-blk.c
236
create mode 100644 hw/vmapple/vmapple.c
343
create mode 100644 hw/vmapple/vmapple.c
237
create mode 100644 include/hw/vmapple/vmapple.h
344
create mode 100644 include/hw/vmapple/vmapple.h
238
345
239
--
346
--
240
2.39.3 (Apple Git-145)
347
2.39.5 (Apple Git-154)
241
348
242
349
diff view generated by jsdifflib
Deleted patch
1
macOS's Cocoa event handling must be done on the initial (main) thread
2
of the process. Furthermore, if library or application code uses
3
libdispatch, the main dispatch queue must be handling events on the main
4
thread as well.
5
1
6
So far, this has affected Qemu in both the Cocoa and SDL UIs, although
7
in different ways: the Cocoa UI replaces the default qemu_main function
8
with one that spins Qemu's internal main event loop off onto a
9
background thread. SDL (which uses Cocoa internally) on the other hand
10
uses a polling approach within Qemu's main event loop. Events are
11
polled during the SDL UI's dpy_refresh callback, which happens to run
12
on the main thread by default.
13
14
As UIs are mutually exclusive, this works OK as long as nothing else
15
needs platform-native event handling. In the next patch, a new device is
16
introduced based on the ParavirtualizedGraphics.framework in macOS.
17
This uses libdispatch internally, and only works when events are being
18
handled on the main runloop. With the current system, it works when
19
using either the Cocoa or the SDL UI. However, it does not when running
20
headless. Moreover, any attempt to install a similar scheme to the
21
Cocoa UI's main thread replacement fails when combined with the SDL
22
UI.
23
24
This change tidies up main thread management to be more flexible.
25
26
* The qemu_main global function pointer is a custom function for the
27
main thread, and it may now be NULL. When it is, the main thread
28
runs the main Qemu loop. This represents the traditional setup.
29
* When non-null, spawning the main Qemu event loop on a separate
30
thread is now done centrally rather than inside the Cocoa UI code.
31
* For most platforms, qemu_main is indeed NULL by default, but on
32
Darwin, it defaults to a function that runs the CFRunLoop.
33
* The Cocoa UI sets qemu_main to a function which runs the
34
NSApplication event handling runloop, as is usual for a Cocoa app.
35
* The SDL UI overrides the qemu_main function to NULL, thus
36
specifying that Qemu's main loop must run on the main
37
thread.
38
* For other UIs, or in the absence of UIs, the platform's default
39
behaviour is followed.
40
41
This means that on macOS, the platform's runloop events are always
42
handled, regardless of chosen UI. The new PV graphics device will
43
thus work in all configurations. There is no functional change on other
44
operating systems.
45
46
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
47
---
48
49
v5:
50
51
* Simplified the way of setting/clearing the main loop by going back
52
to setting qemu_main directly, but narrowing the scope of what it
53
needs to do, and it can now be NULL.
54
55
v6:
56
57
* Folded function qemu_run_default_main_on_new_thread's code into
58
main()
59
* Removed whitespace changes left over on lines near code removed
60
between v4 and v5
61
62
include/qemu-main.h | 3 +--
63
include/qemu/typedefs.h | 1 +
64
system/main.c | 50 ++++++++++++++++++++++++++++++++++----
65
ui/cocoa.m | 54 ++++++++++-------------------------------
66
ui/sdl2.c | 4 +++
67
5 files changed, 64 insertions(+), 48 deletions(-)
68
69
diff --git a/include/qemu-main.h b/include/qemu-main.h
70
index XXXXXXX..XXXXXXX 100644
71
--- a/include/qemu-main.h
72
+++ b/include/qemu-main.h
73
@@ -XXX,XX +XXX,XX @@
74
#ifndef QEMU_MAIN_H
75
#define QEMU_MAIN_H
76
77
-int qemu_default_main(void);
78
-extern int (*qemu_main)(void);
79
+extern qemu_main_fn qemu_main;
80
81
#endif /* QEMU_MAIN_H */
82
diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h
83
index XXXXXXX..XXXXXXX 100644
84
--- a/include/qemu/typedefs.h
85
+++ b/include/qemu/typedefs.h
86
@@ -XXX,XX +XXX,XX @@ typedef struct IRQState *qemu_irq;
87
* Function types
88
*/
89
typedef void (*qemu_irq_handler)(void *opaque, int n, int level);
90
+typedef int (*qemu_main_fn)(void);
91
92
#endif /* QEMU_TYPEDEFS_H */
93
diff --git a/system/main.c b/system/main.c
94
index XXXXXXX..XXXXXXX 100644
95
--- a/system/main.c
96
+++ b/system/main.c
97
@@ -XXX,XX +XXX,XX @@
98
99
#include "qemu/osdep.h"
100
#include "qemu-main.h"
101
+#include "qemu/main-loop.h"
102
#include "sysemu/sysemu.h"
103
104
-#ifdef CONFIG_SDL
105
-#include <SDL.h>
106
+#ifdef CONFIG_DARWIN
107
+#include <CoreFoundation/CoreFoundation.h>
108
#endif
109
110
-int qemu_default_main(void)
111
+static int qemu_default_main(void)
112
{
113
int status;
114
115
@@ -XXX,XX +XXX,XX @@ int qemu_default_main(void)
116
return status;
117
}
118
119
-int (*qemu_main)(void) = qemu_default_main;
120
+/*
121
+ * Various macOS system libraries, including the Cocoa UI and anything using
122
+ * libdispatch, such as ParavirtualizedGraphics.framework, requires that the
123
+ * main runloop, on the main (initial) thread be running or at least regularly
124
+ * polled for events. A special mode is therefore supported, where the QEMU
125
+ * main loop runs on a separate thread and the main thread handles the
126
+ * CF/Cocoa runloop.
127
+ */
128
+
129
+static void *call_qemu_default_main(void *opaque)
130
+{
131
+ int status;
132
+
133
+ bql_lock();
134
+ status = qemu_default_main();
135
+ bql_unlock();
136
+
137
+ exit(status);
138
+}
139
+
140
+#ifdef CONFIG_DARWIN
141
+static int os_darwin_cfrunloop_main(void)
142
+{
143
+ CFRunLoopRun();
144
+ abort();
145
+}
146
+
147
+qemu_main_fn qemu_main = os_darwin_cfrunloop_main;
148
+#else
149
+qemu_main_fn qemu_main;
150
+#endif
151
152
int main(int argc, char **argv)
153
{
154
+ QemuThread main_loop_thread;
155
+
156
qemu_init(argc, argv);
157
- return qemu_main();
158
+ if (qemu_main) {
159
+ qemu_thread_create(&main_loop_thread, "qemu_main",
160
+ call_qemu_default_main, NULL, QEMU_THREAD_DETACHED);
161
+ bql_unlock();
162
+ return qemu_main();
163
+ } else {
164
+ qemu_default_main();
165
+ }
166
}
167
diff --git a/ui/cocoa.m b/ui/cocoa.m
168
index XXXXXXX..XXXXXXX 100644
169
--- a/ui/cocoa.m
170
+++ b/ui/cocoa.m
171
@@ -XXX,XX +XXX,XX @@
172
int height;
173
} QEMUScreen;
174
175
+@class QemuCocoaPasteboardTypeOwner;
176
+
177
static void cocoa_update(DisplayChangeListener *dcl,
178
int x, int y, int w, int h);
179
180
@@ -XXX,XX +XXX,XX @@ static void cocoa_switch(DisplayChangeListener *dcl,
181
static NSInteger cbchangecount = -1;
182
static QemuClipboardInfo *cbinfo;
183
static QemuEvent cbevent;
184
+static QemuCocoaPasteboardTypeOwner *cbowner;
185
186
// Utility functions to run specified code block with the BQL held
187
typedef void (^CodeBlock)(void);
188
@@ -XXX,XX +XXX,XX @@ - (void) dealloc
189
{
190
COCOA_DEBUG("QemuCocoaAppController: dealloc\n");
191
192
- if (cocoaView)
193
- [cocoaView release];
194
+ [cocoaView release];
195
+ [cbowner release];
196
+ cbowner = nil;
197
+
198
[super dealloc];
199
}
200
201
@@ -XXX,XX +XXX,XX @@ - (void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSPasteboardType)t
202
203
@end
204
205
-static QemuCocoaPasteboardTypeOwner *cbowner;
206
-
207
static void cocoa_clipboard_notify(Notifier *notifier, void *data);
208
static void cocoa_clipboard_request(QemuClipboardInfo *info,
209
QemuClipboardType type);
210
@@ -XXX,XX +XXX,XX @@ static void cocoa_clipboard_request(QemuClipboardInfo *info,
211
}
212
}
213
214
-/*
215
- * The startup process for the OSX/Cocoa UI is complicated, because
216
- * OSX insists that the UI runs on the initial main thread, and so we
217
- * need to start a second thread which runs the qemu_default_main():
218
- * in main():
219
- * in cocoa_display_init():
220
- * assign cocoa_main to qemu_main
221
- * create application, menus, etc
222
- * in cocoa_main():
223
- * create qemu-main thread
224
- * enter OSX run loop
225
- */
226
-
227
-static void *call_qemu_main(void *opaque)
228
-{
229
- int status;
230
-
231
- COCOA_DEBUG("Second thread: calling qemu_default_main()\n");
232
- bql_lock();
233
- status = qemu_default_main();
234
- bql_unlock();
235
- COCOA_DEBUG("Second thread: qemu_default_main() returned, exiting\n");
236
- [cbowner release];
237
- exit(status);
238
-}
239
-
240
static int cocoa_main(void)
241
{
242
- QemuThread thread;
243
-
244
- COCOA_DEBUG("Entered %s()\n", __func__);
245
-
246
- bql_unlock();
247
- qemu_thread_create(&thread, "qemu_main", call_qemu_main,
248
- NULL, QEMU_THREAD_DETACHED);
249
-
250
- // Start the main event loop
251
COCOA_DEBUG("Main thread: entering OSX run loop\n");
252
[NSApp run];
253
COCOA_DEBUG("Main thread: left OSX run loop, which should never happen\n");
254
@@ -XXX,XX +XXX,XX @@ static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
255
256
COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
257
258
- qemu_main = cocoa_main;
259
-
260
// Pull this console process up to being a fully-fledged graphical
261
// app with a menubar and Dock icon
262
ProcessSerialNumber psn = { 0, kCurrentProcess };
263
@@ -XXX,XX +XXX,XX @@ static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
264
qemu_clipboard_peer_register(&cbpeer);
265
266
[pool release];
267
+
268
+ /*
269
+ * The Cocoa UI will run the NSApplication runloop on the main thread
270
+ * rather than the default Core Foundation one.
271
+ */
272
+ qemu_main = cocoa_main;
273
}
274
275
static QemuDisplay qemu_display_cocoa = {
276
diff --git a/ui/sdl2.c b/ui/sdl2.c
277
index XXXXXXX..XXXXXXX 100644
278
--- a/ui/sdl2.c
279
+++ b/ui/sdl2.c
280
@@ -XXX,XX +XXX,XX @@
281
#include "sysemu/sysemu.h"
282
#include "ui/win32-kbd-hook.h"
283
#include "qemu/log.h"
284
+#include "qemu-main.h"
285
286
static int sdl2_num_outputs;
287
static struct sdl2_console *sdl2_console;
288
@@ -XXX,XX +XXX,XX @@ static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
289
}
290
291
atexit(sdl_cleanup);
292
+
293
+ /* SDL's event polling (in dpy_refresh) must happen on the main thread. */
294
+ qemu_main = NULL;
295
}
296
297
static QemuDisplay qemu_display_sdl2 = {
298
--
299
2.39.3 (Apple Git-145)
diff view generated by jsdifflib
Deleted patch
1
MacOS provides a framework (library) that allows any vmm to implement a
2
paravirtualized 3d graphics passthrough to the host metal stack called
3
ParavirtualizedGraphics.Framework (PVG). The library abstracts away
4
almost every aspect of the paravirtualized device model and only provides
5
and receives callbacks on MMIO access as well as to share memory address
6
space between the VM and PVG.
7
1
8
This patch implements a QEMU device that drives PVG for the VMApple
9
variant of it.
10
11
Signed-off-by: Alexander Graf <graf@amazon.com>
12
Co-authored-by: Alexander Graf <graf@amazon.com>
13
14
Subsequent changes:
15
16
* Cherry-pick/rebase conflict fixes, API use updates.
17
* Moved from hw/vmapple/ (useful outside that machine type)
18
* Overhaul of threading model, many thread safety improvements.
19
* Asynchronous rendering.
20
* Memory and object lifetime fixes.
21
* Refactoring to split generic and (vmapple) MMIO variant specific
22
code.
23
24
Implementation wise, most of the complexity lies in the differing threading
25
models of ParavirtualizedGraphics.framework, which uses libdispatch and
26
internal locks, versus QEMU, which heavily uses the BQL, especially during
27
memory-mapped device I/O. Great care has therefore been taken to prevent
28
deadlocks by never calling into PVG methods while holding the BQL, and
29
similarly never acquiring the BQL in a callback from PVG. Different strategies
30
have been used (libdispatch, blocking and non-blocking BHs, RCU, etc.)
31
depending on the specific requirements at each framework entry and exit point.
32
33
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
34
---
35
36
v2:
37
38
* Cherry-pick/rebase conflict fixes
39
* BQL function renaming
40
* Moved from hw/vmapple/ (useful outside that machine type)
41
* Code review comments: Switched to DEFINE_TYPES macro & little endian
42
MMIO.
43
* Removed some dead/superfluous code
44
* Mad set_mode thread & memory safe
45
* Added migration blocker due to lack of (de-)serialisation.
46
* Fixes to ObjC refcounting and autorelease pool usage.
47
* Fixed ObjC new/init misuse
48
* Switched to ObjC category extension for private property.
49
* Simplified task memory mapping and made it thread safe.
50
* Refactoring to split generic and vmapple MMIO variant specific
51
code.
52
* Switched to asynchronous MMIO writes on x86-64
53
* Rendering and graphics update are now done asynchronously
54
* Fixed cursor handling
55
* Coding convention fixes
56
* Removed software cursor compositing
57
58
v3:
59
60
* Rebased on latest upstream, fixed breakages including switching to Resettable methods.
61
* Squashed patches dealing with dGPUs, MMIO area size, and GPU picking.
62
* Allow re-entrant MMIO; this simplifies the code and solves the divergence
63
between x86-64 and arm64 variants.
64
65
v4:
66
67
* Renamed '-vmapple' device variant to '-mmio'
68
* MMIO device type now requires aarch64 host and guest
69
* Complete overhaul of the glue code for making Qemu's and
70
ParavirtualizedGraphics.framework's threading and synchronisation models
71
work together. Calls into PVG are from dispatch queues while the
72
BQL-holding initiating thread processes AIO context events; callbacks from
73
PVG are scheduled as BHs on the BQL/main AIO context, awaiting completion
74
where necessary.
75
* Guest frame rendering state is covered by the BQL, with only the PVG calls
76
outside the lock, and serialised on the named render_queue.
77
* Simplified logic for dropping frames in-flight during mode changes, fixed
78
bug in pending frames logic.
79
* Addressed smaller code review notes such as: function naming, object type
80
declarations, type names/declarations/casts, code formatting, #include
81
order, over-cautious ObjC retain/release, what goes in init vs realize,
82
etc.
83
84
v5:
85
86
* Smaller non-functional fixes in response to review comments, such as using
87
NULL for the AIO_WAIT_WHILE context argument, type name formatting,
88
deleting leftover debug code, logging improvements, state struct field
89
order and documentation improvements, etc.
90
* Instead of a single condition variable for all synchronous BH job types,
91
there is now one for each callback block. This reduces the number
92
of threads being awoken unnecessarily to near zero.
93
* MMIO device variant: Unified the BH job for raising interrupts.
94
* Use DMA APIs for PVG framework's guest memory read requests.
95
* Thread safety improvements: ensure mutable AppleGFXState fields are not
96
accessed outside the appropriate lock. Added dedicated mutex for the task
97
list.
98
* Retain references to MemoryRegions for which there exist mappings in each
99
PGTask, and for IOSurface mappings.
100
101
v6:
102
103
* Switched PGTask_s's' mapped_regions from GPtrArray to GArray
104
* Allow DisplaySurface to manage its own vram now that texture -> vram copy
105
occurs under BQL.
106
* Memory mapping operations now use RCU_READ_LOCK_GUARD() where possible
107
instead of a heavy-weight BH job to acquire the BQL.
108
* Changed PVG cursor and mode setting callbacks to kick off BHs instead of
109
libdispatch tasks which then locked the BQL explicitly.
110
* The single remaining callback which must wait for a BH to complete now
111
creates an ephemeral QemuSemaphore to await completion.
112
* Re-removed tracking of mapped surface manager memory regions. Just look up
113
and ref/unref the memory regions in the map/unmap callbacks.
114
* Re-ordered functions in apple-gfx.m to group them by area of functionality.
115
* Improved comments and tweaked some names.
116
117
hw/display/Kconfig | 9 +
118
hw/display/apple-gfx-mmio.m | 281 +++++++++++++
119
hw/display/apple-gfx.h | 65 +++
120
hw/display/apple-gfx.m | 773 ++++++++++++++++++++++++++++++++++++
121
hw/display/meson.build | 4 +
122
hw/display/trace-events | 28 ++
123
meson.build | 4 +
124
7 files changed, 1164 insertions(+)
125
create mode 100644 hw/display/apple-gfx-mmio.m
126
create mode 100644 hw/display/apple-gfx.h
127
create mode 100644 hw/display/apple-gfx.m
128
129
diff --git a/hw/display/Kconfig b/hw/display/Kconfig
130
index XXXXXXX..XXXXXXX 100644
131
--- a/hw/display/Kconfig
132
+++ b/hw/display/Kconfig
133
@@ -XXX,XX +XXX,XX @@ config XLNX_DISPLAYPORT
134
135
config DM163
136
bool
137
+
138
+config MAC_PVG
139
+ bool
140
+ default y
141
+
142
+config MAC_PVG_MMIO
143
+ bool
144
+ depends on MAC_PVG && AARCH64
145
+
146
diff --git a/hw/display/apple-gfx-mmio.m b/hw/display/apple-gfx-mmio.m
147
new file mode 100644
148
index XXXXXXX..XXXXXXX
149
--- /dev/null
150
+++ b/hw/display/apple-gfx-mmio.m
151
@@ -XXX,XX +XXX,XX @@
152
+/*
153
+ * QEMU Apple ParavirtualizedGraphics.framework device, MMIO (arm64) variant
154
+ *
155
+ * Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
156
+ *
157
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
158
+ * See the COPYING file in the top-level directory.
159
+ *
160
+ * SPDX-License-Identifier: GPL-2.0-or-later
161
+ *
162
+ * ParavirtualizedGraphics.framework is a set of libraries that macOS provides
163
+ * which implements 3d graphics passthrough to the host as well as a
164
+ * proprietary guest communication channel to drive it. This device model
165
+ * implements support to drive that library from within QEMU as an MMIO-based
166
+ * system device for macOS on arm64 VMs.
167
+ */
168
+
169
+#include "qemu/osdep.h"
170
+#import <ParavirtualizedGraphics/ParavirtualizedGraphics.h>
171
+#include "apple-gfx.h"
172
+#include "monitor/monitor.h"
173
+#include "hw/sysbus.h"
174
+#include "hw/irq.h"
175
+#include "trace.h"
176
+#include "qemu/log.h"
177
+
178
+OBJECT_DECLARE_SIMPLE_TYPE(AppleGFXMMIOState, APPLE_GFX_MMIO)
179
+
180
+/*
181
+ * ParavirtualizedGraphics.Framework only ships header files for the PCI
182
+ * variant which does not include IOSFC descriptors and host devices. We add
183
+ * their definitions here so that we can also work with the ARM version.
184
+ */
185
+typedef bool(^IOSFCRaiseInterrupt)(uint32_t vector);
186
+typedef bool(^IOSFCUnmapMemory)(
187
+ void *, void *, void *, void *, void *, void *);
188
+typedef bool(^IOSFCMapMemory)(
189
+ uint64_t phys, uint64_t len, bool ro, void **va, void *, void *);
190
+
191
+@interface PGDeviceDescriptor (IOSurfaceMapper)
192
+@property (readwrite, nonatomic) bool usingIOSurfaceMapper;
193
+@end
194
+
195
+@interface PGIOSurfaceHostDeviceDescriptor : NSObject
196
+-(PGIOSurfaceHostDeviceDescriptor *)init;
197
+@property (readwrite, nonatomic, copy, nullable) IOSFCMapMemory mapMemory;
198
+@property (readwrite, nonatomic, copy, nullable) IOSFCUnmapMemory unmapMemory;
199
+@property (readwrite, nonatomic, copy, nullable) IOSFCRaiseInterrupt raiseInterrupt;
200
+@end
201
+
202
+@interface PGIOSurfaceHostDevice : NSObject
203
+-(instancetype)initWithDescriptor:(PGIOSurfaceHostDeviceDescriptor *)desc;
204
+-(uint32_t)mmioReadAtOffset:(size_t)offset;
205
+-(void)mmioWriteAtOffset:(size_t)offset value:(uint32_t)value;
206
+@end
207
+
208
+struct AppleGFXMapSurfaceMemoryJob;
209
+struct AppleGFXMMIOState {
210
+ SysBusDevice parent_obj;
211
+
212
+ AppleGFXState common;
213
+
214
+ qemu_irq irq_gfx;
215
+ qemu_irq irq_iosfc;
216
+ MemoryRegion iomem_iosfc;
217
+ PGIOSurfaceHostDevice *pgiosfc;
218
+};
219
+
220
+typedef struct AppleGFXMMIOJob {
221
+ AppleGFXMMIOState *state;
222
+ uint64_t offset;
223
+ uint64_t value;
224
+ bool completed;
225
+} AppleGFXMMIOJob;
226
+
227
+static void iosfc_do_read(void *opaque)
228
+{
229
+ AppleGFXMMIOJob *job = opaque;
230
+ job->value = [job->state->pgiosfc mmioReadAtOffset:job->offset];
231
+ qatomic_set(&job->completed, true);
232
+ aio_wait_kick();
233
+}
234
+
235
+static uint64_t iosfc_read(void *opaque, hwaddr offset, unsigned size)
236
+{
237
+ AppleGFXMMIOJob job = {
238
+ .state = opaque,
239
+ .offset = offset,
240
+ .completed = false,
241
+ };
242
+ dispatch_queue_t queue =
243
+ dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
244
+
245
+ dispatch_async_f(queue, &job, iosfc_do_read);
246
+ AIO_WAIT_WHILE(NULL, !qatomic_read(&job.completed));
247
+
248
+ trace_apple_gfx_mmio_iosfc_read(offset, job.value);
249
+ return job.value;
250
+}
251
+
252
+static void iosfc_do_write(void *opaque)
253
+{
254
+ AppleGFXMMIOJob *job = opaque;
255
+ [job->state->pgiosfc mmioWriteAtOffset:job->offset value:job->value];
256
+ qatomic_set(&job->completed, true);
257
+ aio_wait_kick();
258
+}
259
+
260
+static void iosfc_write(void *opaque, hwaddr offset, uint64_t val,
261
+ unsigned size)
262
+{
263
+ AppleGFXMMIOJob job = {
264
+ .state = opaque,
265
+ .offset = offset,
266
+ .value = val,
267
+ .completed = false,
268
+ };
269
+ dispatch_queue_t queue =
270
+ dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
271
+
272
+ dispatch_async_f(queue, &job, iosfc_do_write);
273
+ AIO_WAIT_WHILE(NULL, !qatomic_read(&job.completed));
274
+
275
+ trace_apple_gfx_mmio_iosfc_write(offset, val);
276
+}
277
+
278
+static const MemoryRegionOps apple_iosfc_ops = {
279
+ .read = iosfc_read,
280
+ .write = iosfc_write,
281
+ .endianness = DEVICE_LITTLE_ENDIAN,
282
+ .valid = {
283
+ .min_access_size = 4,
284
+ .max_access_size = 8,
285
+ },
286
+ .impl = {
287
+ .min_access_size = 4,
288
+ .max_access_size = 8,
289
+ },
290
+};
291
+
292
+static void raise_irq_bh(void *opaque)
293
+{
294
+ qemu_irq *irq = opaque;
295
+
296
+ qemu_irq_pulse(*irq);
297
+}
298
+
299
+static void *apple_gfx_mmio_map_surface_memory(uint64_t guest_physical_address,
300
+ uint64_t length, bool read_only)
301
+{
302
+ mach_vm_address_t mem;
303
+ MemoryRegion *region = NULL;
304
+
305
+ RCU_READ_LOCK_GUARD();
306
+ mem = apple_gfx_host_address_for_gpa_range(guest_physical_address,
307
+ length, read_only, &region);
308
+
309
+ if (mem != 0) {
310
+ memory_region_ref(region);
311
+ }
312
+
313
+ return (void *)mem;
314
+}
315
+
316
+static bool apple_gfx_mmio_unmap_surface_memory(void *virtual_address)
317
+{
318
+ MemoryRegion *region;
319
+ ram_addr_t offset = 0;
320
+
321
+ RCU_READ_LOCK_GUARD();
322
+ region = memory_region_from_host(virtual_address, &offset);
323
+ if (region) {
324
+ trace_apple_gfx_iosfc_unmap_memory_region(virtual_address, region);
325
+ memory_region_unref(region);
326
+ return true;
327
+ } else {
328
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: memory at %p to be unmapped not "
329
+ "found.\n",
330
+ __func__,
331
+ virtual_address);
332
+ return false;
333
+ }
334
+}
335
+
336
+static PGIOSurfaceHostDevice *apple_gfx_prepare_iosurface_host_device(
337
+ AppleGFXMMIOState *s)
338
+{
339
+ PGIOSurfaceHostDeviceDescriptor *iosfc_desc =
340
+ [PGIOSurfaceHostDeviceDescriptor new];
341
+ PGIOSurfaceHostDevice *iosfc_host_dev = nil;
342
+
343
+ iosfc_desc.mapMemory =
344
+ ^bool(uint64_t phys, uint64_t len, bool ro, void **va, void *e, void *f) {
345
+ *va = apple_gfx_mmio_map_surface_memory(phys, len, ro);
346
+
347
+ trace_apple_gfx_iosfc_map_memory(phys, len, ro, va, e, f, *va);
348
+
349
+ return *va != NULL;
350
+ };
351
+
352
+ iosfc_desc.unmapMemory =
353
+ ^bool(void *va, void *b, void *c, void *d, void *e, void *f) {
354
+ return apple_gfx_mmio_unmap_surface_memory(s);
355
+ };
356
+
357
+ iosfc_desc.raiseInterrupt = ^bool(uint32_t vector) {
358
+ trace_apple_gfx_iosfc_raise_irq(vector);
359
+ aio_bh_schedule_oneshot(qemu_get_aio_context(),
360
+ raise_irq_bh, &s->irq_iosfc);
361
+ return true;
362
+ };
363
+
364
+ iosfc_host_dev =
365
+ [[PGIOSurfaceHostDevice alloc] initWithDescriptor:iosfc_desc];
366
+ [iosfc_desc release];
367
+ return iosfc_host_dev;
368
+}
369
+
370
+static void apple_gfx_mmio_realize(DeviceState *dev, Error **errp)
371
+{
372
+ @autoreleasepool {
373
+ AppleGFXMMIOState *s = APPLE_GFX_MMIO(dev);
374
+ PGDeviceDescriptor *desc = [PGDeviceDescriptor new];
375
+
376
+ desc.raiseInterrupt = ^(uint32_t vector) {
377
+ trace_apple_gfx_raise_irq(vector);
378
+ aio_bh_schedule_oneshot(qemu_get_aio_context(),
379
+ raise_irq_bh, &s->irq_gfx);
380
+ };
381
+
382
+ desc.usingIOSurfaceMapper = true;
383
+ s->pgiosfc = apple_gfx_prepare_iosurface_host_device(s);
384
+
385
+ apple_gfx_common_realize(&s->common, desc, errp);
386
+
387
+ [desc release];
388
+ desc = nil;
389
+ }
390
+}
391
+
392
+static void apple_gfx_mmio_init(Object *obj)
393
+{
394
+ AppleGFXMMIOState *s = APPLE_GFX_MMIO(obj);
395
+
396
+ apple_gfx_common_init(obj, &s->common, TYPE_APPLE_GFX_MMIO);
397
+
398
+ sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->common.iomem_gfx);
399
+ memory_region_init_io(&s->iomem_iosfc, obj, &apple_iosfc_ops, s,
400
+ TYPE_APPLE_GFX_MMIO, 0x10000);
401
+ sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem_iosfc);
402
+ sysbus_init_irq(SYS_BUS_DEVICE(s), &s->irq_gfx);
403
+ sysbus_init_irq(SYS_BUS_DEVICE(s), &s->irq_iosfc);
404
+}
405
+
406
+static void apple_gfx_mmio_reset(Object *obj, ResetType type)
407
+{
408
+ AppleGFXMMIOState *s = APPLE_GFX_MMIO(obj);
409
+ [s->common.pgdev reset];
410
+}
411
+
412
+
413
+static void apple_gfx_mmio_class_init(ObjectClass *klass, void *data)
414
+{
415
+ DeviceClass *dc = DEVICE_CLASS(klass);
416
+ ResettableClass *rc = RESETTABLE_CLASS(klass);
417
+
418
+ rc->phases.hold = apple_gfx_mmio_reset;
419
+ dc->hotpluggable = false;
420
+ dc->realize = apple_gfx_mmio_realize;
421
+}
422
+
423
+static TypeInfo apple_gfx_mmio_types[] = {
424
+ {
425
+ .name = TYPE_APPLE_GFX_MMIO,
426
+ .parent = TYPE_SYS_BUS_DEVICE,
427
+ .instance_size = sizeof(AppleGFXMMIOState),
428
+ .class_init = apple_gfx_mmio_class_init,
429
+ .instance_init = apple_gfx_mmio_init,
430
+ }
431
+};
432
+DEFINE_TYPES(apple_gfx_mmio_types)
433
diff --git a/hw/display/apple-gfx.h b/hw/display/apple-gfx.h
434
new file mode 100644
435
index XXXXXXX..XXXXXXX
436
--- /dev/null
437
+++ b/hw/display/apple-gfx.h
438
@@ -XXX,XX +XXX,XX @@
439
+/*
440
+ * Data structures and functions shared between variants of the macOS
441
+ * ParavirtualizedGraphics.framework based apple-gfx display adapter.
442
+ *
443
+ * SPDX-License-Identifier: GPL-2.0-or-later
444
+ */
445
+
446
+#ifndef QEMU_APPLE_GFX_H
447
+#define QEMU_APPLE_GFX_H
448
+
449
+#define TYPE_APPLE_GFX_MMIO "apple-gfx-mmio"
450
+#define TYPE_APPLE_GFX_PCI "apple-gfx-pci"
451
+
452
+#include "qemu/osdep.h"
453
+#include <dispatch/dispatch.h>
454
+#import <ParavirtualizedGraphics/ParavirtualizedGraphics.h>
455
+#include "qemu/typedefs.h"
456
+#include "exec/memory.h"
457
+#include "ui/surface.h"
458
+
459
+@class PGDeviceDescriptor;
460
+@protocol PGDevice;
461
+@protocol PGDisplay;
462
+@protocol MTLDevice;
463
+@protocol MTLTexture;
464
+@protocol MTLCommandQueue;
465
+
466
+typedef QTAILQ_HEAD(, PGTask_s) PGTaskList;
467
+
468
+typedef struct AppleGFXState {
469
+ /* Initialised on init/realize() */
470
+ MemoryRegion iomem_gfx;
471
+ id<PGDevice> pgdev;
472
+ id<PGDisplay> pgdisp;
473
+ QemuConsole *con;
474
+ id<MTLDevice> mtl;
475
+ id<MTLCommandQueue> mtl_queue;
476
+ dispatch_queue_t render_queue;
477
+
478
+ /* List `tasks` is protected by task_mutex */
479
+ QemuMutex task_mutex;
480
+ PGTaskList tasks;
481
+
482
+ /* Mutable state (BQL protected) */
483
+ QEMUCursor *cursor;
484
+ DisplaySurface *surface;
485
+ id<MTLTexture> texture;
486
+ int32_t pending_frames; /* # guest frames in the rendering pipeline */
487
+ bool gfx_update_requested; /* QEMU display system wants a new frame */
488
+ bool new_frame_ready; /* Guest has rendered a frame, ready to be used */
489
+ bool using_managed_texture_storage;
490
+
491
+ /* Mutable state (atomic) */
492
+ bool cursor_show;
493
+} AppleGFXState;
494
+
495
+void apple_gfx_common_init(Object *obj, AppleGFXState *s, const char* obj_name);
496
+void apple_gfx_common_realize(AppleGFXState *s, PGDeviceDescriptor *desc,
497
+ Error **errp);
498
+uintptr_t apple_gfx_host_address_for_gpa_range(uint64_t guest_physical,
499
+ uint64_t length, bool read_only,
500
+ MemoryRegion **mapping_in_region);
501
+
502
+#endif
503
+
504
diff --git a/hw/display/apple-gfx.m b/hw/display/apple-gfx.m
505
new file mode 100644
506
index XXXXXXX..XXXXXXX
507
--- /dev/null
508
+++ b/hw/display/apple-gfx.m
509
@@ -XXX,XX +XXX,XX @@
510
+/*
511
+ * QEMU Apple ParavirtualizedGraphics.framework device
512
+ *
513
+ * Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
514
+ *
515
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
516
+ * See the COPYING file in the top-level directory.
517
+ *
518
+ * SPDX-License-Identifier: GPL-2.0-or-later
519
+ *
520
+ * ParavirtualizedGraphics.framework is a set of libraries that macOS provides
521
+ * which implements 3d graphics passthrough to the host as well as a
522
+ * proprietary guest communication channel to drive it. This device model
523
+ * implements support to drive that library from within QEMU.
524
+ */
525
+
526
+#include "qemu/osdep.h"
527
+#import <ParavirtualizedGraphics/ParavirtualizedGraphics.h>
528
+#include <mach/mach_vm.h>
529
+#include "apple-gfx.h"
530
+#include "trace.h"
531
+#include "qemu-main.h"
532
+#include "exec/address-spaces.h"
533
+#include "migration/blocker.h"
534
+#include "monitor/monitor.h"
535
+#include "qemu/main-loop.h"
536
+#include "qemu/cutils.h"
537
+#include "qemu/log.h"
538
+#include "qapi/visitor.h"
539
+#include "qapi/error.h"
540
+#include "sysemu/dma.h"
541
+#include "ui/console.h"
542
+
543
+static const PGDisplayCoord_t apple_gfx_modes[] = {
544
+ { .x = 1440, .y = 1080 },
545
+ { .x = 1280, .y = 1024 },
546
+};
547
+
548
+/* ------ PGTask and task operations: new/destroy/map/unmap ------ */
549
+
550
+/*
551
+ * This implements the type declared in <ParavirtualizedGraphics/PGDevice.h>
552
+ * which is opaque from the framework's point of view. It is used in callbacks
553
+ * in the form of its typedef PGTask_t, which also already exists in the
554
+ * framework headers.
555
+ *
556
+ * A "task" in PVG terminology represents a host-virtual contiguous address
557
+ * range which is reserved in a large chunk on task creation. The mapMemory
558
+ * callback then requests ranges of guest system memory (identified by their
559
+ * GPA) to be mapped into subranges of this reserved address space.
560
+ * This type of operation isn't well-supported by QEMU's memory subsystem,
561
+ * but it is fortunately trivial to achieve with Darwin's mach_vm_remap() call,
562
+ * which allows us to refer to the same backing memory via multiple virtual
563
+ * address ranges. The Mach VM APIs are therefore used throughout for managing
564
+ * task memory.
565
+ */
566
+struct PGTask_s {
567
+ QTAILQ_ENTRY(PGTask_s) node;
568
+ AppleGFXState *s;
569
+ mach_vm_address_t address;
570
+ uint64_t len;
571
+ /*
572
+ * All unique MemoryRegions for which a mapping has been created in in this
573
+ * task, and on which we have thus called memory_region_ref(). There are
574
+ * usually very few regions of system RAM in total, so we expect this array
575
+ * to be very short. Therefore, no need for sorting or fancy search
576
+ * algorithms, linear search will do.
577
+ * Protected by AppleGFXState's task_mutex.
578
+ */
579
+ GPtrArray *mapped_regions;
580
+};
581
+
582
+static Error *apple_gfx_mig_blocker;
583
+
584
+static PGTask_t *apple_gfx_new_task(AppleGFXState *s, uint64_t len)
585
+{
586
+ mach_vm_address_t task_mem;
587
+ PGTask_t *task;
588
+ kern_return_t r;
589
+
590
+ r = mach_vm_allocate(mach_task_self(), &task_mem, len, VM_FLAGS_ANYWHERE);
591
+ if (r != KERN_SUCCESS) {
592
+ return NULL;
593
+ }
594
+
595
+ task = g_new0(PGTask_t, 1);
596
+ task->s = s;
597
+ task->address = task_mem;
598
+ task->len = len;
599
+ task->mapped_regions = g_ptr_array_sized_new(2 /* Usually enough */);
600
+
601
+ QEMU_LOCK_GUARD(&s->task_mutex);
602
+ QTAILQ_INSERT_TAIL(&s->tasks, task, node);
603
+
604
+ return task;
605
+}
606
+
607
+static void apple_gfx_destroy_task(AppleGFXState *s, PGTask_t *task)
608
+{
609
+ GPtrArray *regions = task->mapped_regions;
610
+ MemoryRegion *region;
611
+ size_t i;
612
+
613
+ for (i = 0; i < regions->len; ++i) {
614
+ region = g_ptr_array_index(regions, i);
615
+ memory_region_unref(region);
616
+ }
617
+ g_ptr_array_unref(regions);
618
+
619
+ mach_vm_deallocate(mach_task_self(), task->address, task->len);
620
+
621
+ QEMU_LOCK_GUARD(&s->task_mutex);
622
+ QTAILQ_REMOVE(&s->tasks, task, node);
623
+ g_free(task);
624
+}
625
+
626
+uintptr_t apple_gfx_host_address_for_gpa_range(uint64_t guest_physical,
627
+ uint64_t length, bool read_only,
628
+ MemoryRegion **mapping_in_region)
629
+{
630
+ MemoryRegion *ram_region;
631
+ uintptr_t host_address;
632
+ hwaddr ram_region_offset = 0;
633
+ hwaddr ram_region_length = length;
634
+
635
+ ram_region = address_space_translate(&address_space_memory,
636
+ guest_physical,
637
+ &ram_region_offset,
638
+ &ram_region_length, !read_only,
639
+ MEMTXATTRS_UNSPECIFIED);
640
+
641
+ if (!ram_region || ram_region_length < length ||
642
+ !memory_access_is_direct(ram_region, !read_only)) {
643
+ return 0;
644
+ }
645
+
646
+ host_address = (uintptr_t)memory_region_get_ram_ptr(ram_region);
647
+ if (host_address == 0) {
648
+ return 0;
649
+ }
650
+ host_address += ram_region_offset;
651
+ *mapping_in_region = ram_region;
652
+ return host_address;
653
+}
654
+
655
+/* Returns false if the region is already in the array */
656
+static bool add_new_region(GPtrArray *regions, MemoryRegion *region)
657
+{
658
+ MemoryRegion *existing;
659
+ size_t i;
660
+
661
+ for (i = 0; i < regions->len; ++i) {
662
+ existing = g_ptr_array_index(regions, i);
663
+ if (existing == region) {
664
+ return false;
665
+ }
666
+ }
667
+ g_ptr_array_add(regions, region);
668
+ return true;
669
+}
670
+
671
+static bool apple_gfx_task_map_memory(AppleGFXState *s, PGTask_t *task,
672
+ uint64_t virtual_offset,
673
+ PGPhysicalMemoryRange_t *ranges,
674
+ uint32_t range_count, bool read_only)
675
+{
676
+ kern_return_t r;
677
+ mach_vm_address_t target, source;
678
+ vm_prot_t cur_protection, max_protection;
679
+ bool success = true;
680
+ MemoryRegion *region;
681
+
682
+ RCU_READ_LOCK_GUARD();
683
+ QEMU_LOCK_GUARD(&s->task_mutex);
684
+
685
+ trace_apple_gfx_map_memory(task, range_count, virtual_offset, read_only);
686
+ for (int i = 0; i < range_count; i++) {
687
+ PGPhysicalMemoryRange_t *range = &ranges[i];
688
+
689
+ target = task->address + virtual_offset;
690
+ virtual_offset += range->physicalLength;
691
+
692
+ trace_apple_gfx_map_memory_range(i, range->physicalAddress,
693
+ range->physicalLength);
694
+
695
+ region = NULL;
696
+ source = apple_gfx_host_address_for_gpa_range(range->physicalAddress,
697
+ range->physicalLength,
698
+ read_only, &region);
699
+ if (source == 0) {
700
+ success = false;
701
+ continue;
702
+ }
703
+
704
+ if (add_new_region(task->mapped_regions, region)) {
705
+ memory_region_ref(region);
706
+ }
707
+
708
+ cur_protection = 0;
709
+ max_protection = 0;
710
+ // Map guest RAM at range->physicalAddress into PG task memory range
711
+ r = mach_vm_remap(mach_task_self(),
712
+ &target, range->physicalLength, vm_page_size - 1,
713
+ VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE,
714
+ mach_task_self(),
715
+ source, false /* shared mapping, no copy */,
716
+ &cur_protection, &max_protection,
717
+ VM_INHERIT_COPY);
718
+ trace_apple_gfx_remap(r, source, target);
719
+ g_assert(r == KERN_SUCCESS);
720
+ }
721
+
722
+ return success;
723
+}
724
+
725
+static void apple_gfx_task_unmap_memory(AppleGFXState *s, PGTask_t *task,
726
+ uint64_t virtual_offset, uint64_t length)
727
+{
728
+ kern_return_t r;
729
+ mach_vm_address_t range_address;
730
+
731
+ trace_apple_gfx_unmap_memory(task, virtual_offset, length);
732
+
733
+ /*
734
+ * Replace task memory range with fresh 0 pages, undoing the mapping
735
+ * from guest RAM.
736
+ */
737
+ range_address = task->address + virtual_offset;
738
+ r = mach_vm_allocate(mach_task_self(), &range_address, length,
739
+ VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE);
740
+ g_assert(r == KERN_SUCCESS);
741
+}
742
+
743
+/* ------ Rendering and frame management ------ */
744
+
745
+static void apple_gfx_render_frame_completed(AppleGFXState *s,
746
+ uint32_t width, uint32_t height);
747
+
748
+static void apple_gfx_render_new_frame_bql_unlock(AppleGFXState *s)
749
+{
750
+ BOOL r;
751
+ bool managed_texture = s->using_managed_texture_storage;
752
+ uint32_t width = surface_width(s->surface);
753
+ uint32_t height = surface_height(s->surface);
754
+ MTLRegion region = MTLRegionMake2D(0, 0, width, height);
755
+ id<MTLCommandBuffer> command_buffer = [s->mtl_queue commandBuffer];
756
+ id<MTLTexture> texture = s->texture;
757
+
758
+ assert(bql_locked());
759
+ [texture retain];
760
+
761
+ bql_unlock();
762
+
763
+ /* This is not safe to call from the BQL due to PVG-internal locks causing
764
+ * deadlocks. */
765
+ r = [s->pgdisp encodeCurrentFrameToCommandBuffer:command_buffer
766
+ texture:texture
767
+ region:region];
768
+ if (!r) {
769
+ [texture release];
770
+ bql_lock();
771
+ --s->pending_frames;
772
+ bql_unlock();
773
+ qemu_log_mask(LOG_GUEST_ERROR,
774
+ "%s: encodeCurrentFrameToCommandBuffer:texture:region: "
775
+ "failed\n", __func__);
776
+ return;
777
+ }
778
+
779
+ if (managed_texture) {
780
+ /* "Managed" textures exist in both VRAM and RAM and must be synced. */
781
+ id<MTLBlitCommandEncoder> blit = [command_buffer blitCommandEncoder];
782
+ [blit synchronizeResource:texture];
783
+ [blit endEncoding];
784
+ }
785
+ [texture release];
786
+ [command_buffer addCompletedHandler:
787
+ ^(id<MTLCommandBuffer> cb)
788
+ {
789
+ dispatch_async(s->render_queue, ^{
790
+ apple_gfx_render_frame_completed(s, width, height);
791
+ });
792
+ }];
793
+ [command_buffer commit];
794
+}
795
+
796
+static void copy_mtl_texture_to_surface_mem(id<MTLTexture> texture, void *vram)
797
+{
798
+ /* TODO: Skip this entirely on a pure Metal or headless/guest-only
799
+ * rendering path, else use a blit command encoder? Needs careful
800
+ * (double?) buffering design. */
801
+ size_t width = texture.width, height = texture.height;
802
+ MTLRegion region = MTLRegionMake2D(0, 0, width, height);
803
+ [texture getBytes:vram
804
+ bytesPerRow:(width * 4)
805
+ bytesPerImage:(width * height * 4)
806
+ fromRegion:region
807
+ mipmapLevel:0
808
+ slice:0];
809
+}
810
+
811
+static void apple_gfx_render_frame_completed(AppleGFXState *s,
812
+ uint32_t width, uint32_t height)
813
+{
814
+ bql_lock();
815
+ --s->pending_frames;
816
+ assert(s->pending_frames >= 0);
817
+
818
+ /* Only update display if mode hasn't changed since we started rendering. */
819
+ if (width == surface_width(s->surface) &&
820
+ height == surface_height(s->surface)) {
821
+ copy_mtl_texture_to_surface_mem(s->texture, surface_data(s->surface));
822
+ if (s->gfx_update_requested) {
823
+ s->gfx_update_requested = false;
824
+ dpy_gfx_update_full(s->con);
825
+ graphic_hw_update_done(s->con);
826
+ s->new_frame_ready = false;
827
+ } else {
828
+ s->new_frame_ready = true;
829
+ }
830
+ }
831
+ if (s->pending_frames > 0) {
832
+ apple_gfx_render_new_frame_bql_unlock(s);
833
+ } else {
834
+ bql_unlock();
835
+ }
836
+}
837
+
838
+static void apple_gfx_fb_update_display(void *opaque)
839
+{
840
+ AppleGFXState *s = opaque;
841
+
842
+ assert(bql_locked());
843
+ if (s->new_frame_ready) {
844
+ dpy_gfx_update_full(s->con);
845
+ s->new_frame_ready = false;
846
+ graphic_hw_update_done(s->con);
847
+ } else if (s->pending_frames > 0) {
848
+ s->gfx_update_requested = true;
849
+ } else {
850
+ graphic_hw_update_done(s->con);
851
+ }
852
+}
853
+
854
+static const GraphicHwOps apple_gfx_fb_ops = {
855
+ .gfx_update = apple_gfx_fb_update_display,
856
+ .gfx_update_async = true,
857
+};
858
+
859
+/* ------ Mouse cursor and display mode setting ------ */
860
+
861
+static void set_mode(AppleGFXState *s, uint32_t width, uint32_t height)
862
+{
863
+ MTLTextureDescriptor *textureDescriptor;
864
+
865
+ if (s->surface &&
866
+ width == surface_width(s->surface) &&
867
+ height == surface_height(s->surface)) {
868
+ return;
869
+ }
870
+
871
+ [s->texture release];
872
+
873
+ s->surface = qemu_create_displaysurface(width, height);
874
+
875
+ @autoreleasepool {
876
+ textureDescriptor =
877
+ [MTLTextureDescriptor
878
+ texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
879
+ width:width
880
+ height:height
881
+ mipmapped:NO];
882
+ textureDescriptor.usage = s->pgdisp.minimumTextureUsage;
883
+ s->texture = [s->mtl newTextureWithDescriptor:textureDescriptor];
884
+ }
885
+
886
+ s->using_managed_texture_storage =
887
+ (s->texture.storageMode == MTLStorageModeManaged);
888
+ dpy_gfx_replace_surface(s->con, s->surface);
889
+}
890
+
891
+static void update_cursor(AppleGFXState *s)
892
+{
893
+ assert(bql_locked());
894
+ dpy_mouse_set(s->con, s->pgdisp.cursorPosition.x,
895
+ s->pgdisp.cursorPosition.y, qatomic_read(&s->cursor_show));
896
+}
897
+
898
+static void update_cursor_bh(void *opaque)
899
+{
900
+ AppleGFXState *s = opaque;
901
+ update_cursor(s);
902
+}
903
+
904
+typedef struct AppleGFXSetCursorGlyphJob {
905
+ AppleGFXState *s;
906
+ NSBitmapImageRep *glyph;
907
+ PGDisplayCoord_t hotspot;
908
+} AppleGFXSetCursorGlyphJob;
909
+
910
+static void set_cursor_glyph(void *opaque)
911
+{
912
+ AppleGFXSetCursorGlyphJob *job = opaque;
913
+ AppleGFXState *s = job->s;
914
+ NSBitmapImageRep *glyph = job->glyph;
915
+ uint32_t bpp = glyph.bitsPerPixel;
916
+ size_t width = glyph.pixelsWide;
917
+ size_t height = glyph.pixelsHigh;
918
+ size_t padding_bytes_per_row = glyph.bytesPerRow - width * 4;
919
+ const uint8_t* px_data = glyph.bitmapData;
920
+
921
+ trace_apple_gfx_cursor_set(bpp, width, height);
922
+
923
+ if (s->cursor) {
924
+ cursor_unref(s->cursor);
925
+ s->cursor = NULL;
926
+ }
927
+
928
+ if (bpp == 32) { /* Shouldn't be anything else, but just to be safe...*/
929
+ s->cursor = cursor_alloc(width, height);
930
+ s->cursor->hot_x = job->hotspot.x;
931
+ s->cursor->hot_y = job->hotspot.y;
932
+
933
+ uint32_t *dest_px = s->cursor->data;
934
+
935
+ for (size_t y = 0; y < height; ++y) {
936
+ for (size_t x = 0; x < width; ++x) {
937
+ /* NSBitmapImageRep's red & blue channels are swapped
938
+ * compared to QEMUCursor's. */
939
+ *dest_px =
940
+ (px_data[0] << 16u) |
941
+ (px_data[1] << 8u) |
942
+ (px_data[2] << 0u) |
943
+ (px_data[3] << 24u);
944
+ ++dest_px;
945
+ px_data += 4;
946
+ }
947
+ px_data += padding_bytes_per_row;
948
+ }
949
+ dpy_cursor_define(s->con, s->cursor);
950
+ update_cursor(s);
951
+ }
952
+ [glyph release];
953
+
954
+ g_free(job);
955
+}
956
+
957
+/* ------ DMA (device reading system memory) ------ */
958
+
959
+typedef struct AppleGFXReadMemoryJob {
960
+ QemuSemaphore sem;
961
+ hwaddr physical_address;
962
+ uint64_t length;
963
+ void *dst;
964
+ bool success;
965
+} AppleGFXReadMemoryJob;
966
+
967
+static void apple_gfx_do_read_memory(void *opaque)
968
+{
969
+ AppleGFXReadMemoryJob *job = opaque;
970
+ MemTxResult r;
971
+
972
+ r = dma_memory_read(&address_space_memory, job->physical_address,
973
+ job->dst, job->length, MEMTXATTRS_UNSPECIFIED);
974
+ job->success = r == MEMTX_OK;
975
+
976
+ qemu_sem_post(&job->sem);
977
+}
978
+
979
+static bool apple_gfx_read_memory(AppleGFXState *s, hwaddr physical_address,
980
+ uint64_t length, void *dst)
981
+{
982
+ AppleGFXReadMemoryJob job = {
983
+ .physical_address = physical_address, .length = length, .dst = dst
984
+ };
985
+
986
+ trace_apple_gfx_read_memory(physical_address, length, dst);
987
+
988
+ /* Performing DMA requires BQL, so do it in a BH. */
989
+ qemu_sem_init(&job.sem, 0);
990
+ aio_bh_schedule_oneshot(qemu_get_aio_context(),
991
+ apple_gfx_do_read_memory, &job);
992
+ qemu_sem_wait(&job.sem);
993
+ qemu_sem_destroy(&job.sem);
994
+ return job.success;
995
+}
996
+
997
+/* ------ Memory-mapped device I/O operations ------ */
998
+
999
+static dispatch_queue_t get_background_queue(void)
1000
+{
1001
+ return dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
1002
+}
1003
+
1004
+typedef struct AppleGFXIOJob {
1005
+ AppleGFXState *state;
1006
+ uint64_t offset;
1007
+ uint64_t value;
1008
+ bool completed;
1009
+} AppleGFXIOJob;
1010
+
1011
+static void apple_gfx_do_read(void *opaque)
1012
+{
1013
+ AppleGFXIOJob *job = opaque;
1014
+ job->value = [job->state->pgdev mmioReadAtOffset:job->offset];
1015
+ qatomic_set(&job->completed, true);
1016
+ aio_wait_kick();
1017
+}
1018
+
1019
+static uint64_t apple_gfx_read(void *opaque, hwaddr offset, unsigned size)
1020
+{
1021
+ AppleGFXIOJob job = {
1022
+ .state = opaque,
1023
+ .offset = offset,
1024
+ .completed = false,
1025
+ };
1026
+ dispatch_queue_t queue = get_background_queue();
1027
+
1028
+ dispatch_async_f(queue, &job, apple_gfx_do_read);
1029
+ AIO_WAIT_WHILE(NULL, !qatomic_read(&job.completed));
1030
+
1031
+ trace_apple_gfx_read(offset, job.value);
1032
+ return job.value;
1033
+}
1034
+
1035
+static void apple_gfx_do_write(void *opaque)
1036
+{
1037
+ AppleGFXIOJob *job = opaque;
1038
+ [job->state->pgdev mmioWriteAtOffset:job->offset value:job->value];
1039
+ qatomic_set(&job->completed, true);
1040
+ aio_wait_kick();
1041
+}
1042
+
1043
+static void apple_gfx_write(void *opaque, hwaddr offset, uint64_t val,
1044
+ unsigned size)
1045
+{
1046
+ /*
1047
+ * The methods mmioReadAtOffset: and especially mmioWriteAtOffset: can
1048
+ * trigger synchronous operations on other dispatch queues, which in turn
1049
+ * may call back out on one or more of the callback blocks. For this reason,
1050
+ * and as we are holding the BQL, we invoke the I/O methods on a pool
1051
+ * thread and handle AIO tasks while we wait. Any work in the callbacks
1052
+ * requiring the BQL will in turn schedule BHs which this thread will
1053
+ * process while waiting.
1054
+ */
1055
+ AppleGFXIOJob job = {
1056
+ .state = opaque,
1057
+ .offset = offset,
1058
+ .value = val,
1059
+ .completed = false,
1060
+ };
1061
+ dispatch_queue_t queue = get_background_queue();
1062
+
1063
+ dispatch_async_f(queue, &job, apple_gfx_do_write);
1064
+ AIO_WAIT_WHILE(NULL, !qatomic_read(&job.completed));
1065
+
1066
+ trace_apple_gfx_write(offset, val);
1067
+}
1068
+
1069
+static const MemoryRegionOps apple_gfx_ops = {
1070
+ .read = apple_gfx_read,
1071
+ .write = apple_gfx_write,
1072
+ .endianness = DEVICE_LITTLE_ENDIAN,
1073
+ .valid = {
1074
+ .min_access_size = 4,
1075
+ .max_access_size = 8,
1076
+ },
1077
+ .impl = {
1078
+ .min_access_size = 4,
1079
+ .max_access_size = 4,
1080
+ },
1081
+};
1082
+
1083
+static size_t apple_gfx_get_default_mmio_range_size(void)
1084
+{
1085
+ size_t mmio_range_size;
1086
+ @autoreleasepool {
1087
+ PGDeviceDescriptor *desc = [PGDeviceDescriptor new];
1088
+ mmio_range_size = desc.mmioLength;
1089
+ [desc release];
1090
+ }
1091
+ return mmio_range_size;
1092
+}
1093
+
1094
+/* ------ Initialisation and startup ------ */
1095
+
1096
+void apple_gfx_common_init(Object *obj, AppleGFXState *s, const char* obj_name)
1097
+{
1098
+ size_t mmio_range_size = apple_gfx_get_default_mmio_range_size();
1099
+
1100
+ trace_apple_gfx_common_init(obj_name, mmio_range_size);
1101
+ memory_region_init_io(&s->iomem_gfx, obj, &apple_gfx_ops, s, obj_name,
1102
+ mmio_range_size);
1103
+
1104
+ /* TODO: PVG framework supports serialising device state: integrate it! */
1105
+}
1106
+
1107
+static void apple_gfx_register_task_mapping_handlers(AppleGFXState *s,
1108
+ PGDeviceDescriptor *desc)
1109
+{
1110
+ desc.createTask = ^(uint64_t vmSize, void * _Nullable * _Nonnull baseAddress) {
1111
+ PGTask_t *task = apple_gfx_new_task(s, vmSize);
1112
+ *baseAddress = (void *)task->address;
1113
+ trace_apple_gfx_create_task(vmSize, *baseAddress);
1114
+ return task;
1115
+ };
1116
+
1117
+ desc.destroyTask = ^(PGTask_t * _Nonnull task) {
1118
+ trace_apple_gfx_destroy_task(task, task->mapped_regions->len);
1119
+
1120
+ apple_gfx_destroy_task(s, task);
1121
+ };
1122
+
1123
+ desc.mapMemory = ^bool(PGTask_t * _Nonnull task, uint32_t range_count,
1124
+ uint64_t virtual_offset, bool read_only,
1125
+ PGPhysicalMemoryRange_t * _Nonnull ranges) {
1126
+ return apple_gfx_task_map_memory(s, task, virtual_offset,
1127
+ ranges, range_count, read_only);
1128
+ };
1129
+
1130
+ desc.unmapMemory = ^bool(PGTask_t * _Nonnull task, uint64_t virtual_offset,
1131
+ uint64_t length) {
1132
+ apple_gfx_task_unmap_memory(s, task, virtual_offset, length);
1133
+ return true;
1134
+ };
1135
+
1136
+ desc.readMemory = ^bool(uint64_t physical_address, uint64_t length,
1137
+ void * _Nonnull dst) {
1138
+ return apple_gfx_read_memory(s, physical_address, length, dst);
1139
+ };
1140
+}
1141
+
1142
+static PGDisplayDescriptor *apple_gfx_prepare_display_descriptor(AppleGFXState *s)
1143
+{
1144
+ PGDisplayDescriptor *disp_desc = [PGDisplayDescriptor new];
1145
+
1146
+ disp_desc.name = @"QEMU display";
1147
+ disp_desc.sizeInMillimeters = NSMakeSize(400., 300.); /* A 20" display */
1148
+ disp_desc.queue = dispatch_get_main_queue();
1149
+ disp_desc.newFrameEventHandler = ^(void) {
1150
+ trace_apple_gfx_new_frame();
1151
+ dispatch_async(s->render_queue, ^{
1152
+ /* Drop frames if we get too far ahead. */
1153
+ bql_lock();
1154
+ if (s->pending_frames >= 2) {
1155
+ bql_unlock();
1156
+ return;
1157
+ }
1158
+ ++s->pending_frames;
1159
+ if (s->pending_frames > 1) {
1160
+ bql_unlock();
1161
+ return;
1162
+ }
1163
+ @autoreleasepool {
1164
+ apple_gfx_render_new_frame_bql_unlock(s);
1165
+ }
1166
+ });
1167
+ };
1168
+ disp_desc.modeChangeHandler = ^(PGDisplayCoord_t sizeInPixels,
1169
+ OSType pixelFormat) {
1170
+ trace_apple_gfx_mode_change(sizeInPixels.x, sizeInPixels.y);
1171
+
1172
+ BQL_LOCK_GUARD();
1173
+ set_mode(s, sizeInPixels.x, sizeInPixels.y);
1174
+ };
1175
+ disp_desc.cursorGlyphHandler = ^(NSBitmapImageRep *glyph,
1176
+ PGDisplayCoord_t hotspot) {
1177
+ AppleGFXSetCursorGlyphJob *job = g_malloc0(sizeof(*job));
1178
+ job->s = s;
1179
+ job->glyph = glyph;
1180
+ job->hotspot = hotspot;
1181
+ [glyph retain];
1182
+ aio_bh_schedule_oneshot(qemu_get_aio_context(),
1183
+ set_cursor_glyph, job);
1184
+ };
1185
+ disp_desc.cursorShowHandler = ^(BOOL show) {
1186
+ trace_apple_gfx_cursor_show(show);
1187
+ qatomic_set(&s->cursor_show, show);
1188
+ aio_bh_schedule_oneshot(qemu_get_aio_context(),
1189
+ update_cursor_bh, s);
1190
+ };
1191
+ disp_desc.cursorMoveHandler = ^(void) {
1192
+ trace_apple_gfx_cursor_move();
1193
+ aio_bh_schedule_oneshot(qemu_get_aio_context(),
1194
+ update_cursor_bh, s);
1195
+ };
1196
+
1197
+ return disp_desc;
1198
+}
1199
+
1200
+static NSArray<PGDisplayMode*>* apple_gfx_prepare_display_mode_array(void)
1201
+{
1202
+ PGDisplayMode *modes[ARRAY_SIZE(apple_gfx_modes)];
1203
+ NSArray<PGDisplayMode*>* mode_array = nil;
1204
+ int i;
1205
+
1206
+ for (i = 0; i < ARRAY_SIZE(apple_gfx_modes); i++) {
1207
+ modes[i] =
1208
+ [[PGDisplayMode alloc] initWithSizeInPixels:apple_gfx_modes[i] refreshRateInHz:60.];
1209
+ }
1210
+
1211
+ mode_array = [NSArray arrayWithObjects:modes count:ARRAY_SIZE(apple_gfx_modes)];
1212
+
1213
+ for (i = 0; i < ARRAY_SIZE(apple_gfx_modes); i++) {
1214
+ [modes[i] release];
1215
+ modes[i] = nil;
1216
+ }
1217
+
1218
+ return mode_array;
1219
+}
1220
+
1221
+static id<MTLDevice> copy_suitable_metal_device(void)
1222
+{
1223
+ id<MTLDevice> dev = nil;
1224
+ NSArray<id<MTLDevice>> *devs = MTLCopyAllDevices();
1225
+
1226
+ /* Prefer a unified memory GPU. Failing that, pick a non-removable GPU. */
1227
+ for (size_t i = 0; i < devs.count; ++i) {
1228
+ if (devs[i].hasUnifiedMemory) {
1229
+ dev = devs[i];
1230
+ break;
1231
+ }
1232
+ if (!devs[i].removable) {
1233
+ dev = devs[i];
1234
+ }
1235
+ }
1236
+
1237
+ if (dev != nil) {
1238
+ [dev retain];
1239
+ } else {
1240
+ dev = MTLCreateSystemDefaultDevice();
1241
+ }
1242
+ [devs release];
1243
+
1244
+ return dev;
1245
+}
1246
+
1247
+void apple_gfx_common_realize(AppleGFXState *s, PGDeviceDescriptor *desc,
1248
+ Error **errp)
1249
+{
1250
+ PGDisplayDescriptor *disp_desc = nil;
1251
+
1252
+ if (apple_gfx_mig_blocker == NULL) {
1253
+ error_setg(&apple_gfx_mig_blocker,
1254
+ "Migration state blocked by apple-gfx display device");
1255
+ if (migrate_add_blocker(&apple_gfx_mig_blocker, errp) < 0) {
1256
+ return;
1257
+ }
1258
+ }
1259
+
1260
+ qemu_mutex_init(&s->task_mutex);
1261
+ QTAILQ_INIT(&s->tasks);
1262
+ s->render_queue = dispatch_queue_create("apple-gfx.render",
1263
+ DISPATCH_QUEUE_SERIAL);
1264
+ s->mtl = copy_suitable_metal_device();
1265
+ s->mtl_queue = [s->mtl newCommandQueue];
1266
+
1267
+ desc.device = s->mtl;
1268
+
1269
+ apple_gfx_register_task_mapping_handlers(s, desc);
1270
+
1271
+ s->pgdev = PGNewDeviceWithDescriptor(desc);
1272
+
1273
+ disp_desc = apple_gfx_prepare_display_descriptor(s);
1274
+ s->pgdisp = [s->pgdev newDisplayWithDescriptor:disp_desc
1275
+ port:0 serialNum:1234];
1276
+ [disp_desc release];
1277
+ s->pgdisp.modeList = apple_gfx_prepare_display_mode_array();
1278
+
1279
+ s->con = graphic_console_init(NULL, 0, &apple_gfx_fb_ops, s);
1280
+
1281
+ qatomic_set(&s->cursor_show, true);
1282
+}
1283
diff --git a/hw/display/meson.build b/hw/display/meson.build
1284
index XXXXXXX..XXXXXXX 100644
1285
--- a/hw/display/meson.build
1286
+++ b/hw/display/meson.build
1287
@@ -XXX,XX +XXX,XX @@ system_ss.add(when: 'CONFIG_ARTIST', if_true: files('artist.c'))
1288
1289
system_ss.add(when: 'CONFIG_ATI_VGA', if_true: [files('ati.c', 'ati_2d.c', 'ati_dbg.c'), pixman])
1290
1291
+system_ss.add(when: 'CONFIG_MAC_PVG', if_true: [files('apple-gfx.m'), pvg, metal])
1292
+if cpu == 'aarch64'
1293
+ system_ss.add(when: 'CONFIG_MAC_PVG_MMIO', if_true: [files('apple-gfx-mmio.m'), pvg, metal])
1294
+endif
1295
1296
if config_all_devices.has_key('CONFIG_VIRTIO_GPU')
1297
virtio_gpu_ss = ss.source_set()
1298
diff --git a/hw/display/trace-events b/hw/display/trace-events
1299
index XXXXXXX..XXXXXXX 100644
1300
--- a/hw/display/trace-events
1301
+++ b/hw/display/trace-events
1302
@@ -XXX,XX +XXX,XX @@ dm163_bits_ppi(unsigned dest_width) "dest_width : %u"
1303
dm163_leds(int led, uint32_t value) "led %d: 0x%x"
1304
dm163_channels(int channel, uint8_t value) "channel %d: 0x%x"
1305
dm163_refresh_rate(uint32_t rr) "refresh rate %d"
1306
+
1307
+# apple-gfx.m
1308
+apple_gfx_read(uint64_t offset, uint64_t res) "offset=0x%"PRIx64" res=0x%"PRIx64
1309
+apple_gfx_write(uint64_t offset, uint64_t val) "offset=0x%"PRIx64" val=0x%"PRIx64
1310
+apple_gfx_create_task(uint32_t vm_size, void *va) "vm_size=0x%x base_addr=%p"
1311
+apple_gfx_destroy_task(void *task, unsigned int num_mapped_regions) "task=%p, task->mapped_regions->len=%u"
1312
+apple_gfx_map_memory(void *task, uint32_t range_count, uint64_t virtual_offset, uint32_t read_only) "task=%p range_count=0x%x virtual_offset=0x%"PRIx64" read_only=%d"
1313
+apple_gfx_map_memory_range(uint32_t i, uint64_t phys_addr, uint64_t phys_len) "[%d] phys_addr=0x%"PRIx64" phys_len=0x%"PRIx64
1314
+apple_gfx_remap(uint64_t retval, uint64_t source, uint64_t target) "retval=%"PRId64" source=0x%"PRIx64" target=0x%"PRIx64
1315
+apple_gfx_unmap_memory(void *task, uint64_t virtual_offset, uint64_t length) "task=%p virtual_offset=0x%"PRIx64" length=0x%"PRIx64
1316
+apple_gfx_read_memory(uint64_t phys_address, uint64_t length, void *dst) "phys_addr=0x%"PRIx64" length=0x%"PRIx64" dest=%p"
1317
+apple_gfx_raise_irq(uint32_t vector) "vector=0x%x"
1318
+apple_gfx_new_frame(void) ""
1319
+apple_gfx_mode_change(uint64_t x, uint64_t y) "x=%"PRId64" y=%"PRId64
1320
+apple_gfx_cursor_set(uint32_t bpp, uint64_t width, uint64_t height) "bpp=%d width=%"PRId64" height=0x%"PRId64
1321
+apple_gfx_cursor_show(uint32_t show) "show=%d"
1322
+apple_gfx_cursor_move(void) ""
1323
+apple_gfx_common_init(const char *device_name, size_t mmio_size) "device: %s; MMIO size: %zu bytes"
1324
+
1325
+# apple-gfx-mmio.m
1326
+apple_gfx_mmio_iosfc_read(uint64_t offset, uint64_t res) "offset=0x%"PRIx64" res=0x%"PRIx64
1327
+apple_gfx_mmio_iosfc_write(uint64_t offset, uint64_t val) "offset=0x%"PRIx64" val=0x%"PRIx64
1328
+apple_gfx_iosfc_map_memory(uint64_t phys, uint64_t len, uint32_t ro, void *va, void *e, void *f, void* va_result) "phys=0x%"PRIx64" len=0x%"PRIx64" ro=%d va=%p e=%p f=%p -> *va=%p"
1329
+apple_gfx_iosfc_map_memory_new_region(size_t i, void *region, uint64_t start, uint64_t end) "index=%zu, region=%p, 0x%"PRIx64"-0x%"PRIx64
1330
+apple_gfx_iosfc_unmap_memory(void *a, void *b, void *c, void *d, void *e, void *f) "a=%p b=%p c=%p d=%p e=%p f=%p"
1331
+apple_gfx_iosfc_unmap_memory_region(void* mem, void *region) "unmapping @ %p from memory region %p"
1332
+apple_gfx_iosfc_raise_irq(uint32_t vector) "vector=0x%x"
1333
+
1334
diff --git a/meson.build b/meson.build
1335
index XXXXXXX..XXXXXXX 100644
1336
--- a/meson.build
1337
+++ b/meson.build
1338
@@ -XXX,XX +XXX,XX @@ socket = []
1339
version_res = []
1340
coref = []
1341
iokit = []
1342
+pvg = []
1343
+metal = []
1344
emulator_link_args = []
1345
midl = not_found
1346
widl = not_found
1347
@@ -XXX,XX +XXX,XX @@ elif host_os == 'darwin'
1348
coref = dependency('appleframeworks', modules: 'CoreFoundation')
1349
iokit = dependency('appleframeworks', modules: 'IOKit', required: false)
1350
host_dsosuf = '.dylib'
1351
+ pvg = dependency('appleframeworks', modules: 'ParavirtualizedGraphics')
1352
+ metal = dependency('appleframeworks', modules: 'Metal')
1353
elif host_os == 'sunos'
1354
socket = [cc.find_library('socket'),
1355
cc.find_library('nsl'),
1356
--
1357
2.39.3 (Apple Git-145)
1358
1359
diff view generated by jsdifflib
Deleted patch
1
This change wires up the PCI variant of the paravirtualised
2
graphics device, mainly useful for x86-64 macOS guests, implemented
3
by macOS's ParavirtualizedGraphics.framework. It builds on code
4
shared with the vmapple/mmio variant of the PVG device.
5
1
6
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
7
---
8
9
v4:
10
11
* Threading improvements analogous to those in common apple-gfx code
12
and mmio device variant.
13
* Smaller code review issues addressed.
14
15
v5:
16
17
* Minor error handling improvement.
18
19
hw/display/Kconfig | 4 +
20
hw/display/apple-gfx-pci.m | 149 +++++++++++++++++++++++++++++++++++++
21
hw/display/meson.build | 1 +
22
3 files changed, 154 insertions(+)
23
create mode 100644 hw/display/apple-gfx-pci.m
24
25
diff --git a/hw/display/Kconfig b/hw/display/Kconfig
26
index XXXXXXX..XXXXXXX 100644
27
--- a/hw/display/Kconfig
28
+++ b/hw/display/Kconfig
29
@@ -XXX,XX +XXX,XX @@ config MAC_PVG_MMIO
30
bool
31
depends on MAC_PVG && AARCH64
32
33
+config MAC_PVG_PCI
34
+ bool
35
+ depends on MAC_PVG && PCI
36
+ default y if PCI_DEVICES
37
diff --git a/hw/display/apple-gfx-pci.m b/hw/display/apple-gfx-pci.m
38
new file mode 100644
39
index XXXXXXX..XXXXXXX
40
--- /dev/null
41
+++ b/hw/display/apple-gfx-pci.m
42
@@ -XXX,XX +XXX,XX @@
43
+/*
44
+ * QEMU Apple ParavirtualizedGraphics.framework device, PCI variant
45
+ *
46
+ * Copyright © 2023-2024 Phil Dennis-Jordan
47
+ *
48
+ * SPDX-License-Identifier: GPL-2.0-or-later
49
+ *
50
+ * ParavirtualizedGraphics.framework is a set of libraries that macOS provides
51
+ * which implements 3d graphics passthrough to the host as well as a
52
+ * proprietary guest communication channel to drive it. This device model
53
+ * implements support to drive that library from within QEMU as a PCI device
54
+ * aimed primarily at x86-64 macOS VMs.
55
+ */
56
+
57
+#include "apple-gfx.h"
58
+#include "hw/pci/pci_device.h"
59
+#include "hw/pci/msi.h"
60
+#include "qapi/error.h"
61
+#include "trace.h"
62
+#import <ParavirtualizedGraphics/ParavirtualizedGraphics.h>
63
+
64
+OBJECT_DECLARE_SIMPLE_TYPE(AppleGFXPCIState, APPLE_GFX_PCI)
65
+
66
+struct AppleGFXPCIState {
67
+ PCIDevice parent_obj;
68
+
69
+ AppleGFXState common;
70
+};
71
+
72
+static const char* apple_gfx_pci_option_rom_path = NULL;
73
+
74
+static void apple_gfx_init_option_rom_path(void)
75
+{
76
+ NSURL *option_rom_url = PGCopyOptionROMURL();
77
+ const char *option_rom_path = option_rom_url.fileSystemRepresentation;
78
+ apple_gfx_pci_option_rom_path = g_strdup(option_rom_path);
79
+ [option_rom_url release];
80
+}
81
+
82
+static void apple_gfx_pci_init(Object *obj)
83
+{
84
+ AppleGFXPCIState *s = APPLE_GFX_PCI(obj);
85
+
86
+ if (!apple_gfx_pci_option_rom_path) {
87
+ /* The following is done on device not class init to avoid running
88
+ * ObjC code before fork() in -daemonize mode. */
89
+ PCIDeviceClass *pci = PCI_DEVICE_CLASS(object_get_class(obj));
90
+ apple_gfx_init_option_rom_path();
91
+ pci->romfile = apple_gfx_pci_option_rom_path;
92
+ }
93
+
94
+ apple_gfx_common_init(obj, &s->common, TYPE_APPLE_GFX_PCI);
95
+}
96
+
97
+typedef struct AppleGFXPCIInterruptJob {
98
+ PCIDevice *device;
99
+ uint32_t vector;
100
+} AppleGFXPCIInterruptJob;
101
+
102
+static void apple_gfx_pci_raise_interrupt(void *opaque)
103
+{
104
+ AppleGFXPCIInterruptJob *job = opaque;
105
+
106
+ if (msi_enabled(job->device)) {
107
+ msi_notify(job->device, job->vector);
108
+ }
109
+ g_free(job);
110
+}
111
+
112
+static void apple_gfx_pci_interrupt(PCIDevice *dev, AppleGFXPCIState *s,
113
+ uint32_t vector)
114
+{
115
+ AppleGFXPCIInterruptJob *job;
116
+
117
+ trace_apple_gfx_raise_irq(vector);
118
+ job = g_malloc0(sizeof(*job));
119
+ job->device = dev;
120
+ job->vector = vector;
121
+ aio_bh_schedule_oneshot(qemu_get_aio_context(),
122
+ apple_gfx_pci_raise_interrupt, job);
123
+}
124
+
125
+static void apple_gfx_pci_realize(PCIDevice *dev, Error **errp)
126
+{
127
+ AppleGFXPCIState *s = APPLE_GFX_PCI(dev);
128
+ int ret;
129
+
130
+ pci_register_bar(dev, PG_PCI_BAR_MMIO,
131
+ PCI_BASE_ADDRESS_SPACE_MEMORY, &s->common.iomem_gfx);
132
+
133
+ ret = msi_init(dev, 0x0 /* config offset; 0 = find space */,
134
+ PG_PCI_MAX_MSI_VECTORS, true /* msi64bit */,
135
+ false /*msi_per_vector_mask*/, errp);
136
+ if (ret != 0) {
137
+ return;
138
+ }
139
+
140
+ @autoreleasepool {
141
+ PGDeviceDescriptor *desc = [PGDeviceDescriptor new];
142
+ desc.raiseInterrupt = ^(uint32_t vector) {
143
+ apple_gfx_pci_interrupt(dev, s, vector);
144
+ };
145
+
146
+ apple_gfx_common_realize(&s->common, desc, errp);
147
+ [desc release];
148
+ desc = nil;
149
+ }
150
+}
151
+
152
+static void apple_gfx_pci_reset(Object *obj, ResetType type)
153
+{
154
+ AppleGFXPCIState *s = APPLE_GFX_PCI(obj);
155
+ [s->common.pgdev reset];
156
+}
157
+
158
+static void apple_gfx_pci_class_init(ObjectClass *klass, void *data)
159
+{
160
+ DeviceClass *dc = DEVICE_CLASS(klass);
161
+ PCIDeviceClass *pci = PCI_DEVICE_CLASS(klass);
162
+ ResettableClass *rc = RESETTABLE_CLASS(klass);
163
+
164
+ rc->phases.hold = apple_gfx_pci_reset;
165
+ dc->desc = "macOS Paravirtualized Graphics PCI Display Controller";
166
+ dc->hotpluggable = false;
167
+ set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
168
+
169
+ pci->vendor_id = PG_PCI_VENDOR_ID;
170
+ pci->device_id = PG_PCI_DEVICE_ID;
171
+ pci->class_id = PCI_CLASS_DISPLAY_OTHER;
172
+ pci->realize = apple_gfx_pci_realize;
173
+
174
+ // TODO: Property for setting mode list
175
+}
176
+
177
+static TypeInfo apple_gfx_pci_types[] = {
178
+ {
179
+ .name = TYPE_APPLE_GFX_PCI,
180
+ .parent = TYPE_PCI_DEVICE,
181
+ .instance_size = sizeof(AppleGFXPCIState),
182
+ .class_init = apple_gfx_pci_class_init,
183
+ .instance_init = apple_gfx_pci_init,
184
+ .interfaces = (InterfaceInfo[]) {
185
+ { INTERFACE_PCIE_DEVICE },
186
+ { },
187
+ },
188
+ }
189
+};
190
+DEFINE_TYPES(apple_gfx_pci_types)
191
+
192
diff --git a/hw/display/meson.build b/hw/display/meson.build
193
index XXXXXXX..XXXXXXX 100644
194
--- a/hw/display/meson.build
195
+++ b/hw/display/meson.build
196
@@ -XXX,XX +XXX,XX @@ system_ss.add(when: 'CONFIG_MAC_PVG', if_true: [files('apple-gfx.m'), pv
197
if cpu == 'aarch64'
198
system_ss.add(when: 'CONFIG_MAC_PVG_MMIO', if_true: [files('apple-gfx-mmio.m'), pvg, metal])
199
endif
200
+system_ss.add(when: 'CONFIG_MAC_PVG_PCI', if_true: [files('apple-gfx-pci.m'), pvg, metal])
201
202
if config_all_devices.has_key('CONFIG_VIRTIO_GPU')
203
virtio_gpu_ss = ss.source_set()
204
--
205
2.39.3 (Apple Git-145)
206
207
diff view generated by jsdifflib
Deleted patch
1
This change adds a property 'display_modes' on the graphics device
2
which permits specifying a list of display modes. (screen resolution
3
and refresh rate)
4
1
5
The property is an array of a custom type to make the syntax slightly
6
less awkward to use, for example:
7
8
-device '{"driver":"apple-gfx-pci", "display-modes":["1920x1080@60", "3840x2160@60"]}'
9
10
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
11
---
12
13
v4:
14
15
* Switched to the native array property type, which recently gained
16
     command line support.
17
* The property has also been added to the -mmio variant.
18
* Tidied up the code a little.
19
20
v5:
21
22
* Better error handling and buffer management in property parsing and
23
output.
24
25
v6:
26
27
* Switched to using NSMutableArray for the mode list to avoid need for
28
allocating a temporary array - previously done with alloca.
29
30
hw/display/apple-gfx-mmio.m | 8 +++
31
hw/display/apple-gfx-pci.m | 9 ++-
32
hw/display/apple-gfx.h | 12 ++++
33
hw/display/apple-gfx.m | 137 +++++++++++++++++++++++++++++++-----
34
hw/display/trace-events | 2 +
35
5 files changed, 148 insertions(+), 20 deletions(-)
36
37
diff --git a/hw/display/apple-gfx-mmio.m b/hw/display/apple-gfx-mmio.m
38
index XXXXXXX..XXXXXXX 100644
39
--- a/hw/display/apple-gfx-mmio.m
40
+++ b/hw/display/apple-gfx-mmio.m
41
@@ -XXX,XX +XXX,XX @@ static void apple_gfx_mmio_reset(Object *obj, ResetType type)
42
[s->common.pgdev reset];
43
}
44
45
+static Property apple_gfx_mmio_properties[] = {
46
+ DEFINE_PROP_ARRAY("display-modes", AppleGFXMMIOState,
47
+ common.num_display_modes, common.display_modes,
48
+ qdev_prop_display_mode, AppleGFXDisplayMode),
49
+ DEFINE_PROP_END_OF_LIST(),
50
+};
51
52
static void apple_gfx_mmio_class_init(ObjectClass *klass, void *data)
53
{
54
@@ -XXX,XX +XXX,XX @@ static void apple_gfx_mmio_class_init(ObjectClass *klass, void *data)
55
rc->phases.hold = apple_gfx_mmio_reset;
56
dc->hotpluggable = false;
57
dc->realize = apple_gfx_mmio_realize;
58
+
59
+ device_class_set_props(dc, apple_gfx_mmio_properties);
60
}
61
62
static TypeInfo apple_gfx_mmio_types[] = {
63
diff --git a/hw/display/apple-gfx-pci.m b/hw/display/apple-gfx-pci.m
64
index XXXXXXX..XXXXXXX 100644
65
--- a/hw/display/apple-gfx-pci.m
66
+++ b/hw/display/apple-gfx-pci.m
67
@@ -XXX,XX +XXX,XX @@ static void apple_gfx_pci_reset(Object *obj, ResetType type)
68
[s->common.pgdev reset];
69
}
70
71
+static Property apple_gfx_pci_properties[] = {
72
+ DEFINE_PROP_ARRAY("display-modes", AppleGFXPCIState,
73
+ common.num_display_modes, common.display_modes,
74
+ qdev_prop_display_mode, AppleGFXDisplayMode),
75
+ DEFINE_PROP_END_OF_LIST(),
76
+};
77
+
78
static void apple_gfx_pci_class_init(ObjectClass *klass, void *data)
79
{
80
DeviceClass *dc = DEVICE_CLASS(klass);
81
@@ -XXX,XX +XXX,XX @@ static void apple_gfx_pci_class_init(ObjectClass *klass, void *data)
82
pci->class_id = PCI_CLASS_DISPLAY_OTHER;
83
pci->realize = apple_gfx_pci_realize;
84
85
- // TODO: Property for setting mode list
86
+ device_class_set_props(dc, apple_gfx_pci_properties);
87
}
88
89
static TypeInfo apple_gfx_pci_types[] = {
90
diff --git a/hw/display/apple-gfx.h b/hw/display/apple-gfx.h
91
index XXXXXXX..XXXXXXX 100644
92
--- a/hw/display/apple-gfx.h
93
+++ b/hw/display/apple-gfx.h
94
@@ -XXX,XX +XXX,XX @@
95
#import <ParavirtualizedGraphics/ParavirtualizedGraphics.h>
96
#include "qemu/typedefs.h"
97
#include "exec/memory.h"
98
+#include "hw/qdev-properties.h"
99
#include "ui/surface.h"
100
101
@class PGDeviceDescriptor;
102
@@ -XXX,XX +XXX,XX @@
103
104
typedef QTAILQ_HEAD(, PGTask_s) PGTaskList;
105
106
+struct AppleGFXDisplayMode;
107
typedef struct AppleGFXState {
108
/* Initialised on init/realize() */
109
MemoryRegion iomem_gfx;
110
@@ -XXX,XX +XXX,XX @@ typedef struct AppleGFXState {
111
id<MTLDevice> mtl;
112
id<MTLCommandQueue> mtl_queue;
113
dispatch_queue_t render_queue;
114
+ struct AppleGFXDisplayMode *display_modes;
115
+ uint32_t num_display_modes;
116
117
/* List `tasks` is protected by task_mutex */
118
QemuMutex task_mutex;
119
@@ -XXX,XX +XXX,XX @@ typedef struct AppleGFXState {
120
bool cursor_show;
121
} AppleGFXState;
122
123
+typedef struct AppleGFXDisplayMode {
124
+ uint16_t width_px;
125
+ uint16_t height_px;
126
+ uint16_t refresh_rate_hz;
127
+} AppleGFXDisplayMode;
128
+
129
void apple_gfx_common_init(Object *obj, AppleGFXState *s, const char* obj_name);
130
void apple_gfx_common_realize(AppleGFXState *s, PGDeviceDescriptor *desc,
131
Error **errp);
132
@@ -XXX,XX +XXX,XX @@ uintptr_t apple_gfx_host_address_for_gpa_range(uint64_t guest_physical,
133
uint64_t length, bool read_only,
134
MemoryRegion **mapping_in_region);
135
136
+extern const PropertyInfo qdev_prop_display_mode;
137
+
138
#endif
139
140
diff --git a/hw/display/apple-gfx.m b/hw/display/apple-gfx.m
141
index XXXXXXX..XXXXXXX 100644
142
--- a/hw/display/apple-gfx.m
143
+++ b/hw/display/apple-gfx.m
144
@@ -XXX,XX +XXX,XX @@
145
#include "sysemu/dma.h"
146
#include "ui/console.h"
147
148
-static const PGDisplayCoord_t apple_gfx_modes[] = {
149
- { .x = 1440, .y = 1080 },
150
- { .x = 1280, .y = 1024 },
151
+static const AppleGFXDisplayMode apple_gfx_default_modes[] = {
152
+ { 1920, 1080, 60 },
153
+ { 1440, 1080, 60 },
154
+ { 1280, 1024, 60 },
155
};
156
157
/* ------ PGTask and task operations: new/destroy/map/unmap ------ */
158
@@ -XXX,XX +XXX,XX @@ static void apple_gfx_register_task_mapping_handlers(AppleGFXState *s,
159
return disp_desc;
160
}
161
162
-static NSArray<PGDisplayMode*>* apple_gfx_prepare_display_mode_array(void)
163
+static NSArray<PGDisplayMode *> *apple_gfx_create_display_mode_array(
164
+ const AppleGFXDisplayMode display_modes[], uint32_t display_mode_count)
165
{
166
- PGDisplayMode *modes[ARRAY_SIZE(apple_gfx_modes)];
167
- NSArray<PGDisplayMode*>* mode_array = nil;
168
- int i;
169
-
170
- for (i = 0; i < ARRAY_SIZE(apple_gfx_modes); i++) {
171
- modes[i] =
172
- [[PGDisplayMode alloc] initWithSizeInPixels:apple_gfx_modes[i] refreshRateInHz:60.];
173
- }
174
-
175
- mode_array = [NSArray arrayWithObjects:modes count:ARRAY_SIZE(apple_gfx_modes)];
176
-
177
- for (i = 0; i < ARRAY_SIZE(apple_gfx_modes); i++) {
178
- [modes[i] release];
179
- modes[i] = nil;
180
+ uint32_t i;
181
+ PGDisplayMode *mode_obj;
182
+ NSMutableArray<PGDisplayMode *> *mode_array =
183
+ [[NSMutableArray alloc] initWithCapacity:display_mode_count];
184
+
185
+ for (i = 0; i < display_mode_count; i++) {
186
+ const AppleGFXDisplayMode *mode = &display_modes[i];
187
+ trace_apple_gfx_display_mode(i, mode->width_px, mode->height_px);
188
+ PGDisplayCoord_t mode_size = { mode->width_px, mode->height_px };
189
+
190
+ mode_obj =
191
+ [[PGDisplayMode alloc] initWithSizeInPixels:mode_size
192
+ refreshRateInHz:mode->refresh_rate_hz];
193
+ [mode_array addObject:mode_obj];
194
+ [mode_obj release];
195
}
196
197
return mode_array;
198
@@ -XXX,XX +XXX,XX @@ void apple_gfx_common_realize(AppleGFXState *s, PGDeviceDescriptor *desc,
199
Error **errp)
200
{
201
PGDisplayDescriptor *disp_desc = nil;
202
+ const AppleGFXDisplayMode *display_modes = apple_gfx_default_modes;
203
+ int num_display_modes = ARRAY_SIZE(apple_gfx_default_modes);
204
+ NSArray<PGDisplayMode *> *mode_array;
205
206
if (apple_gfx_mig_blocker == NULL) {
207
error_setg(&apple_gfx_mig_blocker,
208
@@ -XXX,XX +XXX,XX @@ void apple_gfx_common_realize(AppleGFXState *s, PGDeviceDescriptor *desc,
209
s->pgdisp = [s->pgdev newDisplayWithDescriptor:disp_desc
210
port:0 serialNum:1234];
211
[disp_desc release];
212
- s->pgdisp.modeList = apple_gfx_prepare_display_mode_array();
213
+
214
+ if (s->display_modes != NULL && s->num_display_modes > 0) {
215
+ trace_apple_gfx_common_realize_modes_property(s->num_display_modes);
216
+ display_modes = s->display_modes;
217
+ num_display_modes = s->num_display_modes;
218
+ }
219
+ s->pgdisp.modeList = mode_array =
220
+ apple_gfx_create_display_mode_array(display_modes, num_display_modes);
221
+ [mode_array release];
222
223
s->con = graphic_console_init(NULL, 0, &apple_gfx_fb_ops, s);
224
225
qatomic_set(&s->cursor_show, true);
226
}
227
+
228
+/* ------ Display mode list device property ------ */
229
+
230
+static void apple_gfx_get_display_mode(Object *obj, Visitor *v,
231
+ const char *name, void *opaque,
232
+ Error **errp)
233
+{
234
+ Property *prop = opaque;
235
+ AppleGFXDisplayMode *mode = object_field_prop_ptr(obj, prop);
236
+ /* 3 uint16s (max 5 digits) + 2 separator characters + nul. */
237
+ char buffer[5 * 3 + 2 + 1];
238
+ char *pos = buffer;
239
+
240
+ int rc = snprintf(buffer, sizeof(buffer),
241
+ "%"PRIu16"x%"PRIu16"@%"PRIu16,
242
+ mode->width_px, mode->height_px,
243
+ mode->refresh_rate_hz);
244
+ assert(rc < sizeof(buffer));
245
+
246
+ visit_type_str(v, name, &pos, errp);
247
+}
248
+
249
+static void apple_gfx_set_display_mode(Object *obj, Visitor *v,
250
+ const char *name, void *opaque,
251
+ Error **errp)
252
+{
253
+ Property *prop = opaque;
254
+ AppleGFXDisplayMode *mode = object_field_prop_ptr(obj, prop);
255
+ Error *local_err = NULL;
256
+ const char *endptr;
257
+ g_autofree char *str = NULL;
258
+ int ret;
259
+ int val;
260
+
261
+ visit_type_str(v, name, &str, &local_err);
262
+ if (local_err) {
263
+ error_propagate(errp, local_err);
264
+ return;
265
+ }
266
+
267
+ endptr = str;
268
+
269
+ ret = qemu_strtoi(endptr, &endptr, 10, &val);
270
+ if (ret || val > UINT16_MAX || val <= 0) {
271
+ error_setg(errp, "width in '%s' must be a decimal integer number "
272
+ "of pixels in the range 1..65535", name);
273
+ return;
274
+ }
275
+ mode->width_px = val;
276
+ if (*endptr != 'x') {
277
+ goto separator_error;
278
+ }
279
+
280
+ ret = qemu_strtoi(endptr + 1, &endptr, 10, &val);
281
+ if (ret || val > UINT16_MAX || val <= 0) {
282
+ error_setg(errp, "height in '%s' must be a decimal integer number "
283
+ "of pixels in the range 1..65535", name);
284
+ return;
285
+ }
286
+ mode->height_px = val;
287
+ if (*endptr != '@') {
288
+ goto separator_error;
289
+ }
290
+
291
+ ret = qemu_strtoi(endptr + 1, &endptr, 10, &val);
292
+ if (ret || val > UINT16_MAX || val <= 0) {
293
+ error_setg(errp, "refresh rate in '%s'"
294
+ " must be a positive decimal integer (Hertz)", name);
295
+ }
296
+ mode->refresh_rate_hz = val;
297
+ return;
298
+
299
+separator_error:
300
+ error_setg(errp, "Each display mode takes the format "
301
+ "'<width>x<height>@<rate>'");
302
+}
303
+
304
+const PropertyInfo qdev_prop_display_mode = {
305
+ .name = "display_mode",
306
+ .description =
307
+ "Display mode in pixels and Hertz, as <width>x<height>@<refresh-rate> "
308
+ "Example: 3840x2160@60",
309
+ .get = apple_gfx_get_display_mode,
310
+ .set = apple_gfx_set_display_mode,
311
+};
312
diff --git a/hw/display/trace-events b/hw/display/trace-events
313
index XXXXXXX..XXXXXXX 100644
314
--- a/hw/display/trace-events
315
+++ b/hw/display/trace-events
316
@@ -XXX,XX +XXX,XX @@ apple_gfx_cursor_set(uint32_t bpp, uint64_t width, uint64_t height) "bpp=%d widt
317
apple_gfx_cursor_show(uint32_t show) "show=%d"
318
apple_gfx_cursor_move(void) ""
319
apple_gfx_common_init(const char *device_name, size_t mmio_size) "device: %s; MMIO size: %zu bytes"
320
+apple_gfx_common_realize_modes_property(uint32_t num_modes) "using %u modes supplied by 'display-modes' device property"
321
+apple_gfx_display_mode(uint32_t mode_idx, uint16_t width_px, uint16_t height_px) "mode %2"PRIu32": %4"PRIu16"x%4"PRIu16
322
323
# apple-gfx-mmio.m
324
apple_gfx_mmio_iosfc_read(uint64_t offset, uint64_t res) "offset=0x%"PRIx64" res=0x%"PRIx64
325
--
326
2.39.3 (Apple Git-145)
diff view generated by jsdifflib
Deleted patch
1
I'm happy to take responsibility for the macOS PV graphics code. As
2
HVF patches don't seem to get much attention at the moment, I'm also
3
adding myself as designated reviewer for HVF and x86 HVF to try and
4
improve that.
5
1
6
I anticipate that the resulting workload should be covered by the
7
funding I'm receiving for improving Qemu in combination with macOS. As
8
of right now this runs out at the end of 2024; I expect the workload on
9
apple-gfx should be relatively minor and manageable in my spare time
10
beyond that. I may have to remove myself from more general HVF duties
11
once the contract runs out if it's more than I can manage.
12
13
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
14
---
15
MAINTAINERS | 7 +++++++
16
1 file changed, 7 insertions(+)
17
18
diff --git a/MAINTAINERS b/MAINTAINERS
19
index XXXXXXX..XXXXXXX 100644
20
--- a/MAINTAINERS
21
+++ b/MAINTAINERS
22
@@ -XXX,XX +XXX,XX @@ F: target/arm/hvf/
23
X86 HVF CPUs
24
M: Cameron Esfahani <dirty@apple.com>
25
M: Roman Bolshakov <rbolshakov@ddn.com>
26
+R: Phil Dennis-Jordan <phil@philjordan.eu>
27
W: https://wiki.qemu.org/Features/HVF
28
S: Maintained
29
F: target/i386/hvf/
30
@@ -XXX,XX +XXX,XX @@ F: target/i386/hvf/
31
HVF
32
M: Cameron Esfahani <dirty@apple.com>
33
M: Roman Bolshakov <rbolshakov@ddn.com>
34
+R: Phil Dennis-Jordan <phil@philjordan.eu>
35
W: https://wiki.qemu.org/Features/HVF
36
S: Maintained
37
F: accel/hvf/
38
@@ -XXX,XX +XXX,XX @@ F: hw/display/edid*
39
F: include/hw/display/edid.h
40
F: qemu-edid.c
41
42
+macOS PV Graphics (apple-gfx)
43
+M: Phil Dennis-Jordan <phil@philjordan.eu>
44
+S: Maintained
45
+F: hw/display/apple-gfx*
46
+
47
PIIX4 South Bridge (i82371AB)
48
M: Hervé Poussineau <hpoussin@reactos.org>
49
M: Philippe Mathieu-Daudé <philmd@linaro.org>
50
--
51
2.39.3 (Apple Git-145)
52
53
diff view generated by jsdifflib
Deleted patch
1
From: Alexander Graf <graf@amazon.com>
2
1
3
We will introduce a number of devices that are specific to the vmapple
4
target machine. To keep them all tidily together, let's put them into
5
a single target directory.
6
7
Signed-off-by: Alexander Graf <graf@amazon.com>
8
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
9
Reviewed-by: Akihiko Odaki <akihiko.odaki@daynix.com>
10
---
11
MAINTAINERS | 7 +++++++
12
hw/Kconfig | 1 +
13
hw/meson.build | 1 +
14
hw/vmapple/Kconfig | 1 +
15
hw/vmapple/meson.build | 0
16
hw/vmapple/trace-events | 2 ++
17
hw/vmapple/trace.h | 1 +
18
meson.build | 1 +
19
8 files changed, 14 insertions(+)
20
create mode 100644 hw/vmapple/Kconfig
21
create mode 100644 hw/vmapple/meson.build
22
create mode 100644 hw/vmapple/trace-events
23
create mode 100644 hw/vmapple/trace.h
24
25
diff --git a/MAINTAINERS b/MAINTAINERS
26
index XXXXXXX..XXXXXXX 100644
27
--- a/MAINTAINERS
28
+++ b/MAINTAINERS
29
@@ -XXX,XX +XXX,XX @@ F: hw/hyperv/hv-balloon*.h
30
F: include/hw/hyperv/dynmem-proto.h
31
F: include/hw/hyperv/hv-balloon.h
32
33
+VMapple
34
+M: Alexander Graf <agraf@csgraf.de>
35
+R: Phil Dennis-Jordan <phil@philjordan.eu>
36
+S: Maintained
37
+F: hw/vmapple/*
38
+F: include/hw/vmapple/*
39
+
40
Subsystems
41
----------
42
Overall Audio backends
43
diff --git a/hw/Kconfig b/hw/Kconfig
44
index XXXXXXX..XXXXXXX 100644
45
--- a/hw/Kconfig
46
+++ b/hw/Kconfig
47
@@ -XXX,XX +XXX,XX @@ source ufs/Kconfig
48
source usb/Kconfig
49
source virtio/Kconfig
50
source vfio/Kconfig
51
+source vmapple/Kconfig
52
source xen/Kconfig
53
source watchdog/Kconfig
54
55
diff --git a/hw/meson.build b/hw/meson.build
56
index XXXXXXX..XXXXXXX 100644
57
--- a/hw/meson.build
58
+++ b/hw/meson.build
59
@@ -XXX,XX +XXX,XX @@ subdir('ufs')
60
subdir('usb')
61
subdir('vfio')
62
subdir('virtio')
63
+subdir('vmapple')
64
subdir('watchdog')
65
subdir('xen')
66
subdir('xenpv')
67
diff --git a/hw/vmapple/Kconfig b/hw/vmapple/Kconfig
68
new file mode 100644
69
index XXXXXXX..XXXXXXX
70
--- /dev/null
71
+++ b/hw/vmapple/Kconfig
72
@@ -0,0 +1 @@
73
+
74
diff --git a/hw/vmapple/meson.build b/hw/vmapple/meson.build
75
new file mode 100644
76
index XXXXXXX..XXXXXXX
77
diff --git a/hw/vmapple/trace-events b/hw/vmapple/trace-events
78
new file mode 100644
79
index XXXXXXX..XXXXXXX
80
--- /dev/null
81
+++ b/hw/vmapple/trace-events
82
@@ -XXX,XX +XXX,XX @@
83
+# See docs/devel/tracing.rst for syntax documentation.
84
+
85
diff --git a/hw/vmapple/trace.h b/hw/vmapple/trace.h
86
new file mode 100644
87
index XXXXXXX..XXXXXXX
88
--- /dev/null
89
+++ b/hw/vmapple/trace.h
90
@@ -0,0 +1 @@
91
+#include "trace/trace-hw_vmapple.h"
92
diff --git a/meson.build b/meson.build
93
index XXXXXXX..XXXXXXX 100644
94
--- a/meson.build
95
+++ b/meson.build
96
@@ -XXX,XX +XXX,XX @@ if have_system
97
'hw/usb',
98
'hw/vfio',
99
'hw/virtio',
100
+ 'hw/vmapple',
101
'hw/watchdog',
102
'hw/xen',
103
'hw/gpio',
104
--
105
2.39.3 (Apple Git-145)
diff view generated by jsdifflib
Deleted patch
1
From: Alexander Graf <graf@amazon.com>
2
1
3
In addition to the ISA and PCI variants of pvpanic, let's add an MMIO
4
platform device that we can use in embedded arm environments.
5
6
Signed-off-by: Alexander Graf <graf@amazon.com>
7
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
8
Tested-by: Philippe Mathieu-Daudé <philmd@linaro.org>
9
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
10
Reviewed-by: Akihiko Odaki <akihiko.odaki@daynix.com>
11
---
12
v3:
13
14
* Rebased on upstream, updated a header path
15
hw/misc/Kconfig | 4 +++
16
hw/misc/meson.build | 1 +
17
hw/misc/pvpanic-mmio.c | 61 +++++++++++++++++++++++++++++++++++++++
18
include/hw/misc/pvpanic.h | 1 +
19
4 files changed, 67 insertions(+)
20
create mode 100644 hw/misc/pvpanic-mmio.c
21
22
diff --git a/hw/misc/Kconfig b/hw/misc/Kconfig
23
index XXXXXXX..XXXXXXX 100644
24
--- a/hw/misc/Kconfig
25
+++ b/hw/misc/Kconfig
26
@@ -XXX,XX +XXX,XX @@ config PVPANIC_ISA
27
depends on ISA_BUS
28
select PVPANIC_COMMON
29
30
+config PVPANIC_MMIO
31
+ bool
32
+ select PVPANIC_COMMON
33
+
34
config AUX
35
bool
36
select I2C
37
diff --git a/hw/misc/meson.build b/hw/misc/meson.build
38
index XXXXXXX..XXXXXXX 100644
39
--- a/hw/misc/meson.build
40
+++ b/hw/misc/meson.build
41
@@ -XXX,XX +XXX,XX @@ system_ss.add(when: 'CONFIG_ARMSSE_MHU', if_true: files('armsse-mhu.c'))
42
43
system_ss.add(when: 'CONFIG_PVPANIC_ISA', if_true: files('pvpanic-isa.c'))
44
system_ss.add(when: 'CONFIG_PVPANIC_PCI', if_true: files('pvpanic-pci.c'))
45
+system_ss.add(when: 'CONFIG_PVPANIC_MMIO', if_true: files('pvpanic-mmio.c'))
46
system_ss.add(when: 'CONFIG_AUX', if_true: files('auxbus.c'))
47
system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files(
48
'aspeed_hace.c',
49
diff --git a/hw/misc/pvpanic-mmio.c b/hw/misc/pvpanic-mmio.c
50
new file mode 100644
51
index XXXXXXX..XXXXXXX
52
--- /dev/null
53
+++ b/hw/misc/pvpanic-mmio.c
54
@@ -XXX,XX +XXX,XX @@
55
+/*
56
+ * QEMU simulated pvpanic device (MMIO frontend)
57
+ *
58
+ * Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
59
+ *
60
+ * SPDX-License-Identifier: GPL-2.0-or-later
61
+ */
62
+
63
+#include "qemu/osdep.h"
64
+
65
+#include "hw/qdev-properties.h"
66
+#include "hw/misc/pvpanic.h"
67
+#include "hw/sysbus.h"
68
+#include "standard-headers/misc/pvpanic.h"
69
+
70
+OBJECT_DECLARE_SIMPLE_TYPE(PVPanicMMIOState, PVPANIC_MMIO_DEVICE)
71
+
72
+#define PVPANIC_MMIO_SIZE 0x2
73
+
74
+struct PVPanicMMIOState {
75
+ SysBusDevice parent_obj;
76
+
77
+ PVPanicState pvpanic;
78
+};
79
+
80
+static void pvpanic_mmio_initfn(Object *obj)
81
+{
82
+ PVPanicMMIOState *s = PVPANIC_MMIO_DEVICE(obj);
83
+
84
+ pvpanic_setup_io(&s->pvpanic, DEVICE(s), PVPANIC_MMIO_SIZE);
85
+ sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->pvpanic.mr);
86
+}
87
+
88
+static Property pvpanic_mmio_properties[] = {
89
+ DEFINE_PROP_UINT8("events", PVPanicMMIOState, pvpanic.events,
90
+ PVPANIC_PANICKED | PVPANIC_CRASH_LOADED),
91
+ DEFINE_PROP_END_OF_LIST(),
92
+};
93
+
94
+static void pvpanic_mmio_class_init(ObjectClass *klass, void *data)
95
+{
96
+ DeviceClass *dc = DEVICE_CLASS(klass);
97
+
98
+ device_class_set_props(dc, pvpanic_mmio_properties);
99
+ set_bit(DEVICE_CATEGORY_MISC, dc->categories);
100
+}
101
+
102
+static const TypeInfo pvpanic_mmio_info = {
103
+ .name = TYPE_PVPANIC_MMIO_DEVICE,
104
+ .parent = TYPE_SYS_BUS_DEVICE,
105
+ .instance_size = sizeof(PVPanicMMIOState),
106
+ .instance_init = pvpanic_mmio_initfn,
107
+ .class_init = pvpanic_mmio_class_init,
108
+};
109
+
110
+static void pvpanic_register_types(void)
111
+{
112
+ type_register_static(&pvpanic_mmio_info);
113
+}
114
+
115
+type_init(pvpanic_register_types)
116
diff --git a/include/hw/misc/pvpanic.h b/include/hw/misc/pvpanic.h
117
index XXXXXXX..XXXXXXX 100644
118
--- a/include/hw/misc/pvpanic.h
119
+++ b/include/hw/misc/pvpanic.h
120
@@ -XXX,XX +XXX,XX @@
121
122
#define TYPE_PVPANIC_ISA_DEVICE "pvpanic"
123
#define TYPE_PVPANIC_PCI_DEVICE "pvpanic-pci"
124
+#define TYPE_PVPANIC_MMIO_DEVICE "pvpanic-mmio"
125
126
#define PVPANIC_IOPORT_PROP "ioport"
127
128
--
129
2.39.3 (Apple Git-145)
130
131
diff view generated by jsdifflib
Deleted patch
1
From: Alexander Graf <graf@amazon.com>
2
1
3
MacOS unconditionally disables interrupts of the physical timer on boot
4
and then continues to use the virtual one. We don't really want to support
5
a full physical timer emulation, so let's just ignore those writes.
6
7
Signed-off-by: Alexander Graf <graf@amazon.com>
8
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
9
Reviewed-by: Akihiko Odaki <akihiko.odaki@daynix.com>
10
---
11
target/arm/hvf/hvf.c | 9 +++++++++
12
1 file changed, 9 insertions(+)
13
14
diff --git a/target/arm/hvf/hvf.c b/target/arm/hvf/hvf.c
15
index XXXXXXX..XXXXXXX 100644
16
--- a/target/arm/hvf/hvf.c
17
+++ b/target/arm/hvf/hvf.c
18
@@ -XXX,XX +XXX,XX @@
19
20
#include "qemu/osdep.h"
21
#include "qemu/error-report.h"
22
+#include "qemu/log.h"
23
24
#include "sysemu/runstate.h"
25
#include "sysemu/hvf.h"
26
@@ -XXX,XX +XXX,XX @@ void hvf_arm_init_debug(void)
27
#define SYSREG_OSLSR_EL1 SYSREG(2, 0, 1, 1, 4)
28
#define SYSREG_OSDLR_EL1 SYSREG(2, 0, 1, 3, 4)
29
#define SYSREG_CNTPCT_EL0 SYSREG(3, 3, 14, 0, 1)
30
+#define SYSREG_CNTP_CTL_EL0 SYSREG(3, 3, 14, 2, 1)
31
#define SYSREG_PMCR_EL0 SYSREG(3, 3, 9, 12, 0)
32
#define SYSREG_PMUSERENR_EL0 SYSREG(3, 3, 9, 14, 0)
33
#define SYSREG_PMCNTENSET_EL0 SYSREG(3, 3, 9, 12, 1)
34
@@ -XXX,XX +XXX,XX @@ static int hvf_sysreg_write(CPUState *cpu, uint32_t reg, uint64_t val)
35
case SYSREG_OSLAR_EL1:
36
env->cp15.oslsr_el1 = val & 1;
37
return 0;
38
+ case SYSREG_CNTP_CTL_EL0:
39
+ /*
40
+ * Guests should not rely on the physical counter, but macOS emits
41
+ * disable writes to it. Let it do so, but ignore the requests.
42
+ */
43
+ qemu_log_mask(LOG_UNIMP, "Unsupported write to CNTP_CTL_EL0\n");
44
+ return 0;
45
case SYSREG_OSDLR_EL1:
46
/* Dummy register */
47
return 0;
48
--
49
2.39.3 (Apple Git-145)
diff view generated by jsdifflib
Deleted patch
1
From: Alexander Graf <graf@amazon.com>
2
1
3
Some boards such as vmapple don't do real legacy PCI IRQ swizzling.
4
Instead, they just keep allocating more board IRQ lines for each new
5
legacy IRQ. Let's support that mode by giving instantiators a new
6
"nr_irqs" property they can use to support more than 4 legacy IRQ lines.
7
In this mode, GPEX will export more IRQ lines, one for each device.
8
9
Signed-off-by: Alexander Graf <graf@amazon.com>
10
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
11
Reviewed-by: Akihiko Odaki <akihiko.odaki@daynix.com>
12
---
13
14
v4:
15
16
* Turned pair of IRQ arrays into array of structs.
17
* Simplified swizzling logic selection.
18
19
hw/arm/sbsa-ref.c | 2 +-
20
hw/arm/virt.c | 2 +-
21
hw/i386/microvm.c | 2 +-
22
hw/loongarch/virt.c | 2 +-
23
hw/mips/loongson3_virt.c | 2 +-
24
hw/openrisc/virt.c | 12 +++++------
25
hw/pci-host/gpex.c | 43 ++++++++++++++++++++++++++++++--------
26
hw/riscv/virt.c | 12 +++++------
27
hw/xtensa/virt.c | 2 +-
28
include/hw/pci-host/gpex.h | 7 +++----
29
10 files changed, 55 insertions(+), 31 deletions(-)
30
31
diff --git a/hw/arm/sbsa-ref.c b/hw/arm/sbsa-ref.c
32
index XXXXXXX..XXXXXXX 100644
33
--- a/hw/arm/sbsa-ref.c
34
+++ b/hw/arm/sbsa-ref.c
35
@@ -XXX,XX +XXX,XX @@ static void create_pcie(SBSAMachineState *sms)
36
/* Map IO port space */
37
sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
38
39
- for (i = 0; i < GPEX_NUM_IRQS; i++) {
40
+ for (i = 0; i < PCI_NUM_PINS; i++) {
41
sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
42
qdev_get_gpio_in(sms->gic, irq + i));
43
gpex_set_irq_num(GPEX_HOST(dev), i, irq + i);
44
diff --git a/hw/arm/virt.c b/hw/arm/virt.c
45
index XXXXXXX..XXXXXXX 100644
46
--- a/hw/arm/virt.c
47
+++ b/hw/arm/virt.c
48
@@ -XXX,XX +XXX,XX @@ static void create_pcie(VirtMachineState *vms)
49
/* Map IO port space */
50
sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, base_pio);
51
52
- for (i = 0; i < GPEX_NUM_IRQS; i++) {
53
+ for (i = 0; i < PCI_NUM_PINS; i++) {
54
sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
55
qdev_get_gpio_in(vms->gic, irq + i));
56
gpex_set_irq_num(GPEX_HOST(dev), i, irq + i);
57
diff --git a/hw/i386/microvm.c b/hw/i386/microvm.c
58
index XXXXXXX..XXXXXXX 100644
59
--- a/hw/i386/microvm.c
60
+++ b/hw/i386/microvm.c
61
@@ -XXX,XX +XXX,XX @@ static void create_gpex(MicrovmMachineState *mms)
62
mms->gpex.mmio64.base, mmio64_alias);
63
}
64
65
- for (i = 0; i < GPEX_NUM_IRQS; i++) {
66
+ for (i = 0; i < PCI_NUM_PINS; i++) {
67
sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
68
x86ms->gsi[mms->gpex.irq + i]);
69
}
70
diff --git a/hw/loongarch/virt.c b/hw/loongarch/virt.c
71
index XXXXXXX..XXXXXXX 100644
72
--- a/hw/loongarch/virt.c
73
+++ b/hw/loongarch/virt.c
74
@@ -XXX,XX +XXX,XX @@ static void virt_devices_init(DeviceState *pch_pic,
75
memory_region_add_subregion(get_system_memory(), VIRT_PCI_IO_BASE,
76
pio_alias);
77
78
- for (i = 0; i < GPEX_NUM_IRQS; i++) {
79
+ for (i = 0; i < PCI_NUM_PINS; i++) {
80
sysbus_connect_irq(d, i,
81
qdev_get_gpio_in(pch_pic, 16 + i));
82
gpex_set_irq_num(GPEX_HOST(gpex_dev), i, 16 + i);
83
diff --git a/hw/mips/loongson3_virt.c b/hw/mips/loongson3_virt.c
84
index XXXXXXX..XXXXXXX 100644
85
--- a/hw/mips/loongson3_virt.c
86
+++ b/hw/mips/loongson3_virt.c
87
@@ -XXX,XX +XXX,XX @@ static inline void loongson3_virt_devices_init(MachineState *machine,
88
virt_memmap[VIRT_PCIE_PIO].base, s->pio_alias);
89
sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, virt_memmap[VIRT_PCIE_PIO].base);
90
91
- for (i = 0; i < GPEX_NUM_IRQS; i++) {
92
+ for (i = 0; i < PCI_NUM_PINS; i++) {
93
irq = qdev_get_gpio_in(pic, PCIE_IRQ_BASE + i);
94
sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, irq);
95
gpex_set_irq_num(GPEX_HOST(dev), i, PCIE_IRQ_BASE + i);
96
diff --git a/hw/openrisc/virt.c b/hw/openrisc/virt.c
97
index XXXXXXX..XXXXXXX 100644
98
--- a/hw/openrisc/virt.c
99
+++ b/hw/openrisc/virt.c
100
@@ -XXX,XX +XXX,XX @@ static void create_pcie_irq_map(void *fdt, char *nodename, int irq_base,
101
{
102
int pin, dev;
103
uint32_t irq_map_stride = 0;
104
- uint32_t full_irq_map[GPEX_NUM_IRQS * GPEX_NUM_IRQS * 6] = {};
105
+ uint32_t full_irq_map[PCI_NUM_PINS * PCI_NUM_PINS * 6] = {};
106
uint32_t *irq_map = full_irq_map;
107
108
/*
109
@@ -XXX,XX +XXX,XX @@ static void create_pcie_irq_map(void *fdt, char *nodename, int irq_base,
110
* possible slot) seeing the interrupt-map-mask will allow the table
111
* to wrap to any number of devices.
112
*/
113
- for (dev = 0; dev < GPEX_NUM_IRQS; dev++) {
114
+ for (dev = 0; dev < PCI_NUM_PINS; dev++) {
115
int devfn = dev << 3;
116
117
- for (pin = 0; pin < GPEX_NUM_IRQS; pin++) {
118
- int irq_nr = irq_base + ((pin + PCI_SLOT(devfn)) % GPEX_NUM_IRQS);
119
+ for (pin = 0; pin < PCI_NUM_PINS; pin++) {
120
+ int irq_nr = irq_base + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS);
121
int i = 0;
122
123
/* Fill PCI address cells */
124
@@ -XXX,XX +XXX,XX @@ static void create_pcie_irq_map(void *fdt, char *nodename, int irq_base,
125
}
126
127
qemu_fdt_setprop(fdt, nodename, "interrupt-map", full_irq_map,
128
- GPEX_NUM_IRQS * GPEX_NUM_IRQS *
129
+ PCI_NUM_PINS * PCI_NUM_PINS *
130
irq_map_stride * sizeof(uint32_t));
131
132
qemu_fdt_setprop_cells(fdt, nodename, "interrupt-map-mask",
133
@@ -XXX,XX +XXX,XX @@ static void openrisc_virt_pcie_init(OR1KVirtState *state,
134
memory_region_add_subregion(get_system_memory(), pio_base, alias);
135
136
/* Connect IRQ lines. */
137
- for (i = 0; i < GPEX_NUM_IRQS; i++) {
138
+ for (i = 0; i < PCI_NUM_PINS; i++) {
139
pcie_irq = get_per_cpu_irq(cpus, num_cpus, irq_base + i);
140
141
sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, pcie_irq);
142
diff --git a/hw/pci-host/gpex.c b/hw/pci-host/gpex.c
143
index XXXXXXX..XXXXXXX 100644
144
--- a/hw/pci-host/gpex.c
145
+++ b/hw/pci-host/gpex.c
146
@@ -XXX,XX +XXX,XX @@
147
#include "qemu/osdep.h"
148
#include "qapi/error.h"
149
#include "hw/irq.h"
150
+#include "hw/pci/pci_bus.h"
151
#include "hw/pci-host/gpex.h"
152
#include "hw/qdev-properties.h"
153
#include "migration/vmstate.h"
154
@@ -XXX,XX +XXX,XX @@
155
* GPEX host
156
*/
157
158
+struct GPEXIrq {
159
+ qemu_irq irq;
160
+ int irq_num;
161
+};
162
+
163
static void gpex_set_irq(void *opaque, int irq_num, int level)
164
{
165
GPEXHost *s = opaque;
166
167
- qemu_set_irq(s->irq[irq_num], level);
168
+ qemu_set_irq(s->irq[irq_num].irq, level);
169
}
170
171
int gpex_set_irq_num(GPEXHost *s, int index, int gsi)
172
{
173
- if (index >= GPEX_NUM_IRQS) {
174
+ if (index >= s->num_irqs) {
175
return -EINVAL;
176
}
177
178
- s->irq_num[index] = gsi;
179
+ s->irq[index].irq_num = gsi;
180
return 0;
181
}
182
183
@@ -XXX,XX +XXX,XX @@ static PCIINTxRoute gpex_route_intx_pin_to_irq(void *opaque, int pin)
184
{
185
PCIINTxRoute route;
186
GPEXHost *s = opaque;
187
- int gsi = s->irq_num[pin];
188
+ int gsi = s->irq[pin].irq_num;
189
190
route.irq = gsi;
191
if (gsi < 0) {
192
@@ -XXX,XX +XXX,XX @@ static PCIINTxRoute gpex_route_intx_pin_to_irq(void *opaque, int pin)
193
return route;
194
}
195
196
+static int gpex_swizzle_map_irq_fn(PCIDevice *pci_dev, int pin)
197
+{
198
+ PCIBus *bus = pci_device_root_bus(pci_dev);
199
+
200
+ return (PCI_SLOT(pci_dev->devfn) + pin) % bus->nirq;
201
+}
202
+
203
static void gpex_host_realize(DeviceState *dev, Error **errp)
204
{
205
PCIHostState *pci = PCI_HOST_BRIDGE(dev);
206
@@ -XXX,XX +XXX,XX @@ static void gpex_host_realize(DeviceState *dev, Error **errp)
207
PCIExpressHost *pex = PCIE_HOST_BRIDGE(dev);
208
int i;
209
210
+ s->irq = g_malloc0_n(s->num_irqs, sizeof(*s->irq));
211
+
212
pcie_host_mmcfg_init(pex, PCIE_MMCFG_SIZE_MAX);
213
sysbus_init_mmio(sbd, &pex->mmio);
214
215
@@ -XXX,XX +XXX,XX @@ static void gpex_host_realize(DeviceState *dev, Error **errp)
216
sysbus_init_mmio(sbd, &s->io_ioport);
217
}
218
219
- for (i = 0; i < GPEX_NUM_IRQS; i++) {
220
- sysbus_init_irq(sbd, &s->irq[i]);
221
- s->irq_num[i] = -1;
222
+ for (i = 0; i < s->num_irqs; i++) {
223
+ sysbus_init_irq(sbd, &s->irq[i].irq);
224
+ s->irq[i].irq_num = -1;
225
}
226
227
pci->bus = pci_register_root_bus(dev, "pcie.0", gpex_set_irq,
228
- pci_swizzle_map_irq_fn, s, &s->io_mmio,
229
- &s->io_ioport, 0, 4, TYPE_PCIE_BUS);
230
+ gpex_swizzle_map_irq_fn,
231
+ s, &s->io_mmio, &s->io_ioport, 0,
232
+ s->num_irqs, TYPE_PCIE_BUS);
233
234
pci_bus_set_route_irq_fn(pci->bus, gpex_route_intx_pin_to_irq);
235
qdev_realize(DEVICE(&s->gpex_root), BUS(pci->bus), &error_fatal);
236
}
237
238
+static void gpex_host_unrealize(DeviceState *dev)
239
+{
240
+ GPEXHost *s = GPEX_HOST(dev);
241
+
242
+ g_free(s->irq);
243
+}
244
+
245
static const char *gpex_host_root_bus_path(PCIHostState *host_bridge,
246
PCIBus *rootbus)
247
{
248
@@ -XXX,XX +XXX,XX @@ static Property gpex_host_properties[] = {
249
gpex_cfg.mmio64.base, 0),
250
DEFINE_PROP_SIZE(PCI_HOST_ABOVE_4G_MMIO_SIZE, GPEXHost,
251
gpex_cfg.mmio64.size, 0),
252
+ DEFINE_PROP_UINT8("num-irqs", GPEXHost, num_irqs, PCI_NUM_PINS),
253
DEFINE_PROP_END_OF_LIST(),
254
};
255
256
@@ -XXX,XX +XXX,XX @@ static void gpex_host_class_init(ObjectClass *klass, void *data)
257
258
hc->root_bus_path = gpex_host_root_bus_path;
259
dc->realize = gpex_host_realize;
260
+ dc->unrealize = gpex_host_unrealize;
261
set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
262
dc->fw_name = "pci";
263
device_class_set_props(dc, gpex_host_properties);
264
diff --git a/hw/riscv/virt.c b/hw/riscv/virt.c
265
index XXXXXXX..XXXXXXX 100644
266
--- a/hw/riscv/virt.c
267
+++ b/hw/riscv/virt.c
268
@@ -XXX,XX +XXX,XX @@ static void create_pcie_irq_map(RISCVVirtState *s, void *fdt, char *nodename,
269
{
270
int pin, dev;
271
uint32_t irq_map_stride = 0;
272
- uint32_t full_irq_map[GPEX_NUM_IRQS * GPEX_NUM_IRQS *
273
+ uint32_t full_irq_map[PCI_NUM_PINS * PCI_NUM_PINS *
274
FDT_MAX_INT_MAP_WIDTH] = {};
275
uint32_t *irq_map = full_irq_map;
276
277
@@ -XXX,XX +XXX,XX @@ static void create_pcie_irq_map(RISCVVirtState *s, void *fdt, char *nodename,
278
* possible slot) seeing the interrupt-map-mask will allow the table
279
* to wrap to any number of devices.
280
*/
281
- for (dev = 0; dev < GPEX_NUM_IRQS; dev++) {
282
+ for (dev = 0; dev < PCI_NUM_PINS; dev++) {
283
int devfn = dev * 0x8;
284
285
- for (pin = 0; pin < GPEX_NUM_IRQS; pin++) {
286
- int irq_nr = PCIE_IRQ + ((pin + PCI_SLOT(devfn)) % GPEX_NUM_IRQS);
287
+ for (pin = 0; pin < PCI_NUM_PINS; pin++) {
288
+ int irq_nr = PCIE_IRQ + ((pin + PCI_SLOT(devfn)) % PCI_NUM_PINS);
289
int i = 0;
290
291
/* Fill PCI address cells */
292
@@ -XXX,XX +XXX,XX @@ static void create_pcie_irq_map(RISCVVirtState *s, void *fdt, char *nodename,
293
}
294
295
qemu_fdt_setprop(fdt, nodename, "interrupt-map", full_irq_map,
296
- GPEX_NUM_IRQS * GPEX_NUM_IRQS *
297
+ PCI_NUM_PINS * PCI_NUM_PINS *
298
irq_map_stride * sizeof(uint32_t));
299
300
qemu_fdt_setprop_cells(fdt, nodename, "interrupt-map-mask",
301
@@ -XXX,XX +XXX,XX @@ static inline DeviceState *gpex_pcie_init(MemoryRegion *sys_mem,
302
303
sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, pio_base);
304
305
- for (i = 0; i < GPEX_NUM_IRQS; i++) {
306
+ for (i = 0; i < PCI_NUM_PINS; i++) {
307
irq = qdev_get_gpio_in(irqchip, PCIE_IRQ + i);
308
309
sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, irq);
310
diff --git a/hw/xtensa/virt.c b/hw/xtensa/virt.c
311
index XXXXXXX..XXXXXXX 100644
312
--- a/hw/xtensa/virt.c
313
+++ b/hw/xtensa/virt.c
314
@@ -XXX,XX +XXX,XX @@ static void create_pcie(MachineState *ms, CPUXtensaState *env, int irq_base,
315
/* Connect IRQ lines. */
316
extints = xtensa_get_extints(env);
317
318
- for (i = 0; i < GPEX_NUM_IRQS; i++) {
319
+ for (i = 0; i < PCI_NUM_PINS; i++) {
320
void *q = extints[irq_base + i];
321
322
sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, q);
323
diff --git a/include/hw/pci-host/gpex.h b/include/hw/pci-host/gpex.h
324
index XXXXXXX..XXXXXXX 100644
325
--- a/include/hw/pci-host/gpex.h
326
+++ b/include/hw/pci-host/gpex.h
327
@@ -XXX,XX +XXX,XX @@ OBJECT_DECLARE_SIMPLE_TYPE(GPEXHost, GPEX_HOST)
328
#define TYPE_GPEX_ROOT_DEVICE "gpex-root"
329
OBJECT_DECLARE_SIMPLE_TYPE(GPEXRootState, GPEX_ROOT_DEVICE)
330
331
-#define GPEX_NUM_IRQS 4
332
-
333
struct GPEXRootState {
334
/*< private >*/
335
PCIDevice parent_obj;
336
@@ -XXX,XX +XXX,XX @@ struct GPEXConfig {
337
PCIBus *bus;
338
};
339
340
+typedef struct GPEXIrq GPEXIrq;
341
struct GPEXHost {
342
/*< private >*/
343
PCIExpressHost parent_obj;
344
@@ -XXX,XX +XXX,XX @@ struct GPEXHost {
345
MemoryRegion io_mmio;
346
MemoryRegion io_ioport_window;
347
MemoryRegion io_mmio_window;
348
- qemu_irq irq[GPEX_NUM_IRQS];
349
- int irq_num[GPEX_NUM_IRQS];
350
+ GPEXIrq *irq;
351
+ uint8_t num_irqs;
352
353
bool allow_unmapped_accesses;
354
355
--
356
2.39.3 (Apple Git-145)
diff view generated by jsdifflib
Deleted patch
1
From: Alexander Graf <graf@amazon.com>
2
1
3
VMApple contains an "aes" engine device that it uses to encrypt and
4
decrypt its nvram. It has trivial hard coded keys it uses for that
5
purpose.
6
7
Add device emulation for this device model.
8
9
Signed-off-by: Alexander Graf <graf@amazon.com>
10
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
11
---
12
13
v3:
14
15
* Rebased on latest upstream and fixed minor breakages.
16
* Replaced legacy device reset method with Resettable method
17
18
v4:
19
20
* Improved logging of unimplemented functions and guest errors.
21
* Better adherence to naming and coding conventions.
22
* Cleaner error handling and recovery, including using g_autoptr
23
24
v5:
25
26
* More logging improvements
27
* Use xxx64_overflow() functions for hexdump buffer size calculations.
28
29
hw/vmapple/Kconfig | 2 +
30
hw/vmapple/aes.c | 578 +++++++++++++++++++++++++++++++++++
31
hw/vmapple/meson.build | 1 +
32
hw/vmapple/trace-events | 14 +
33
include/hw/vmapple/vmapple.h | 17 ++
34
include/qemu/cutils.h | 15 +
35
util/hexdump.c | 18 ++
36
7 files changed, 645 insertions(+)
37
create mode 100644 hw/vmapple/aes.c
38
create mode 100644 include/hw/vmapple/vmapple.h
39
40
diff --git a/hw/vmapple/Kconfig b/hw/vmapple/Kconfig
41
index XXXXXXX..XXXXXXX 100644
42
--- a/hw/vmapple/Kconfig
43
+++ b/hw/vmapple/Kconfig
44
@@ -1 +1,3 @@
45
+config VMAPPLE_AES
46
+ bool
47
48
diff --git a/hw/vmapple/aes.c b/hw/vmapple/aes.c
49
new file mode 100644
50
index XXXXXXX..XXXXXXX
51
--- /dev/null
52
+++ b/hw/vmapple/aes.c
53
@@ -XXX,XX +XXX,XX @@
54
+/*
55
+ * QEMU Apple AES device emulation
56
+ *
57
+ * Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
58
+ *
59
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
60
+ * See the COPYING file in the top-level directory.
61
+ *
62
+ * SPDX-License-Identifier: GPL-2.0-or-later
63
+ */
64
+
65
+#include "qemu/osdep.h"
66
+#include "trace.h"
67
+#include "crypto/hash.h"
68
+#include "crypto/aes.h"
69
+#include "crypto/cipher.h"
70
+#include "hw/irq.h"
71
+#include "hw/sysbus.h"
72
+#include "hw/vmapple/vmapple.h"
73
+#include "migration/vmstate.h"
74
+#include "qemu/cutils.h"
75
+#include "qemu/log.h"
76
+#include "qemu/module.h"
77
+#include "sysemu/dma.h"
78
+
79
+OBJECT_DECLARE_SIMPLE_TYPE(AESState, APPLE_AES)
80
+
81
+#define MAX_FIFO_SIZE 9
82
+
83
+#define CMD_KEY 0x1
84
+#define CMD_KEY_CONTEXT_SHIFT 27
85
+#define CMD_KEY_CONTEXT_MASK (0x1 << CMD_KEY_CONTEXT_SHIFT)
86
+#define CMD_KEY_SELECT_MAX_IDX 0x7
87
+#define CMD_KEY_SELECT_SHIFT 24
88
+#define CMD_KEY_SELECT_MASK (CMD_KEY_SELECT_MAX_IDX << CMD_KEY_SELECT_SHIFT)
89
+#define CMD_KEY_KEY_LEN_NUM 4u
90
+#define CMD_KEY_KEY_LEN_SHIFT 22
91
+#define CMD_KEY_KEY_LEN_MASK ((CMD_KEY_KEY_LEN_NUM - 1u) << CMD_KEY_KEY_LEN_SHIFT)
92
+#define CMD_KEY_ENCRYPT_SHIFT 20
93
+#define CMD_KEY_ENCRYPT_MASK (0x1 << CMD_KEY_ENCRYPT_SHIFT)
94
+#define CMD_KEY_BLOCK_MODE_SHIFT 16
95
+#define CMD_KEY_BLOCK_MODE_MASK (0x3 << CMD_KEY_BLOCK_MODE_SHIFT)
96
+#define CMD_IV 0x2
97
+#define CMD_IV_CONTEXT_SHIFT 26
98
+#define CMD_IV_CONTEXT_MASK (0x3 << CMD_KEY_CONTEXT_SHIFT)
99
+#define CMD_DSB 0x3
100
+#define CMD_SKG 0x4
101
+#define CMD_DATA 0x5
102
+#define CMD_DATA_KEY_CTX_SHIFT 27
103
+#define CMD_DATA_KEY_CTX_MASK (0x1 << CMD_DATA_KEY_CTX_SHIFT)
104
+#define CMD_DATA_IV_CTX_SHIFT 25
105
+#define CMD_DATA_IV_CTX_MASK (0x3 << CMD_DATA_IV_CTX_SHIFT)
106
+#define CMD_DATA_LEN_MASK 0xffffff
107
+#define CMD_STORE_IV 0x6
108
+#define CMD_STORE_IV_ADDR_MASK 0xffffff
109
+#define CMD_WRITE_REG 0x7
110
+#define CMD_FLAG 0x8
111
+#define CMD_FLAG_STOP_MASK BIT(26)
112
+#define CMD_FLAG_RAISE_IRQ_MASK BIT(27)
113
+#define CMD_FLAG_INFO_MASK 0xff
114
+#define CMD_MAX 0x10
115
+
116
+#define CMD_SHIFT 28
117
+
118
+#define REG_STATUS 0xc
119
+#define REG_STATUS_DMA_READ_RUNNING BIT(0)
120
+#define REG_STATUS_DMA_READ_PENDING BIT(1)
121
+#define REG_STATUS_DMA_WRITE_RUNNING BIT(2)
122
+#define REG_STATUS_DMA_WRITE_PENDING BIT(3)
123
+#define REG_STATUS_BUSY BIT(4)
124
+#define REG_STATUS_EXECUTING BIT(5)
125
+#define REG_STATUS_READY BIT(6)
126
+#define REG_STATUS_TEXT_DPA_SEEDED BIT(7)
127
+#define REG_STATUS_UNWRAP_DPA_SEEDED BIT(8)
128
+
129
+#define REG_IRQ_STATUS 0x18
130
+#define REG_IRQ_STATUS_INVALID_CMD BIT(2)
131
+#define REG_IRQ_STATUS_FLAG BIT(5)
132
+#define REG_IRQ_ENABLE 0x1c
133
+#define REG_WATERMARK 0x20
134
+#define REG_Q_STATUS 0x24
135
+#define REG_FLAG_INFO 0x30
136
+#define REG_FIFO 0x200
137
+
138
+static const uint32_t key_lens[CMD_KEY_KEY_LEN_NUM] = {
139
+ [0] = 16,
140
+ [1] = 24,
141
+ [2] = 32,
142
+ [3] = 64,
143
+};
144
+
145
+typedef struct Key {
146
+ uint32_t key_len;
147
+ uint8_t key[32];
148
+} Key;
149
+
150
+typedef struct IV {
151
+ uint32_t iv[4];
152
+} IV;
153
+
154
+static Key builtin_keys[CMD_KEY_SELECT_MAX_IDX + 1] = {
155
+ [1] = {
156
+ .key_len = 32,
157
+ .key = { 0x1 },
158
+ },
159
+ [2] = {
160
+ .key_len = 32,
161
+ .key = { 0x2 },
162
+ },
163
+ [3] = {
164
+ .key_len = 32,
165
+ .key = { 0x3 },
166
+ }
167
+};
168
+
169
+struct AESState {
170
+ SysBusDevice parent_obj;
171
+
172
+ qemu_irq irq;
173
+ MemoryRegion iomem1;
174
+ MemoryRegion iomem2;
175
+ AddressSpace *as;
176
+
177
+ uint32_t status;
178
+ uint32_t q_status;
179
+ uint32_t irq_status;
180
+ uint32_t irq_enable;
181
+ uint32_t watermark;
182
+ uint32_t flag_info;
183
+ uint32_t fifo[MAX_FIFO_SIZE];
184
+ uint32_t fifo_idx;
185
+ Key key[2];
186
+ IV iv[4];
187
+ bool is_encrypt;
188
+ QCryptoCipherMode block_mode;
189
+};
190
+
191
+static void aes_update_irq(AESState *s)
192
+{
193
+ qemu_set_irq(s->irq, !!(s->irq_status & s->irq_enable));
194
+}
195
+
196
+static uint64_t aes1_read(void *opaque, hwaddr offset, unsigned size)
197
+{
198
+ AESState *s = opaque;
199
+ uint64_t res = 0;
200
+
201
+ switch (offset) {
202
+ case REG_STATUS:
203
+ res = s->status;
204
+ break;
205
+ case REG_IRQ_STATUS:
206
+ res = s->irq_status;
207
+ break;
208
+ case REG_IRQ_ENABLE:
209
+ res = s->irq_enable;
210
+ break;
211
+ case REG_WATERMARK:
212
+ res = s->watermark;
213
+ break;
214
+ case REG_Q_STATUS:
215
+ res = s->q_status;
216
+ break;
217
+ case REG_FLAG_INFO:
218
+ res = s->flag_info;
219
+ break;
220
+
221
+ default:
222
+ qemu_log_mask(LOG_UNIMP, "%s: Unknown AES MMIO offset %" PRIx64 "\n",
223
+ __func__, offset);
224
+ break;
225
+ }
226
+
227
+ trace_aes_read(offset, res);
228
+
229
+ return res;
230
+}
231
+
232
+static void fifo_append(AESState *s, uint64_t val)
233
+{
234
+ if (s->fifo_idx == MAX_FIFO_SIZE) {
235
+ /* Exceeded the FIFO. Bail out */
236
+ return;
237
+ }
238
+
239
+ s->fifo[s->fifo_idx++] = val;
240
+}
241
+
242
+static bool has_payload(AESState *s, uint32_t elems)
243
+{
244
+ return s->fifo_idx >= (elems + 1);
245
+}
246
+
247
+static bool cmd_key(AESState *s)
248
+{
249
+ uint32_t cmd = s->fifo[0];
250
+ uint32_t key_select = (cmd & CMD_KEY_SELECT_MASK) >> CMD_KEY_SELECT_SHIFT;
251
+ uint32_t ctxt = (cmd & CMD_KEY_CONTEXT_MASK) >> CMD_KEY_CONTEXT_SHIFT;
252
+ uint32_t key_len;
253
+
254
+ switch ((cmd & CMD_KEY_BLOCK_MODE_MASK) >> CMD_KEY_BLOCK_MODE_SHIFT) {
255
+ case 0:
256
+ s->block_mode = QCRYPTO_CIPHER_MODE_ECB;
257
+ break;
258
+ case 1:
259
+ s->block_mode = QCRYPTO_CIPHER_MODE_CBC;
260
+ break;
261
+ default:
262
+ return false;
263
+ }
264
+
265
+ s->is_encrypt = cmd & CMD_KEY_ENCRYPT_MASK;
266
+ key_len = key_lens[((cmd & CMD_KEY_KEY_LEN_MASK) >> CMD_KEY_KEY_LEN_SHIFT)];
267
+
268
+ if (key_select) {
269
+ trace_aes_cmd_key_select_builtin(ctxt, key_select,
270
+ s->is_encrypt ? "en" : "de",
271
+ QCryptoCipherMode_str(s->block_mode));
272
+ s->key[ctxt] = builtin_keys[key_select];
273
+ } else {
274
+ trace_aes_cmd_key_select_new(ctxt, key_len,
275
+ s->is_encrypt ? "en" : "de",
276
+ QCryptoCipherMode_str(s->block_mode));
277
+ if (key_len > sizeof(s->key[ctxt].key)) {
278
+ return false;
279
+ }
280
+ if (!has_payload(s, key_len / sizeof(uint32_t))) {
281
+ /* wait for payload */
282
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: No payload\n", __func__);
283
+ return false;
284
+ }
285
+ memcpy(&s->key[ctxt].key, &s->fifo[1], key_len);
286
+ s->key[ctxt].key_len = key_len;
287
+ }
288
+
289
+ return true;
290
+}
291
+
292
+static bool cmd_iv(AESState *s)
293
+{
294
+ uint32_t cmd = s->fifo[0];
295
+ uint32_t ctxt = (cmd & CMD_IV_CONTEXT_MASK) >> CMD_IV_CONTEXT_SHIFT;
296
+
297
+ if (!has_payload(s, 4)) {
298
+ /* wait for payload */
299
+ return false;
300
+ }
301
+ memcpy(&s->iv[ctxt].iv, &s->fifo[1], sizeof(s->iv[ctxt].iv));
302
+ trace_aes_cmd_iv(ctxt, s->fifo[1], s->fifo[2], s->fifo[3], s->fifo[4]);
303
+
304
+ return true;
305
+}
306
+
307
+static void dump_data(const char *desc, const void *p, size_t len)
308
+{
309
+ static const size_t MAX_LEN = 0x1000;
310
+ char hex[MAX_LEN * 2 + 1] = "";
311
+
312
+ if (len > MAX_LEN) {
313
+ return;
314
+ }
315
+
316
+ qemu_hexdump_to_buffer(hex, sizeof(hex), p, len);
317
+ trace_aes_dump_data(desc, hex);
318
+}
319
+
320
+static bool cmd_data(AESState *s)
321
+{
322
+ uint32_t cmd = s->fifo[0];
323
+ uint32_t ctxt_iv = 0;
324
+ uint32_t ctxt_key = (cmd & CMD_DATA_KEY_CTX_MASK) >> CMD_DATA_KEY_CTX_SHIFT;
325
+ uint32_t len = cmd & CMD_DATA_LEN_MASK;
326
+ uint64_t src_addr = s->fifo[2];
327
+ uint64_t dst_addr = s->fifo[3];
328
+ QCryptoCipherAlgo alg;
329
+ g_autoptr(QCryptoCipher) cipher = NULL;
330
+ g_autoptr(GByteArray) src = NULL;
331
+ g_autoptr(GByteArray) dst = NULL;
332
+ MemTxResult r;
333
+
334
+ src_addr |= ((uint64_t)s->fifo[1] << 16) & 0xffff00000000ULL;
335
+ dst_addr |= ((uint64_t)s->fifo[1] << 32) & 0xffff00000000ULL;
336
+
337
+ trace_aes_cmd_data(ctxt_key, ctxt_iv, src_addr, dst_addr, len);
338
+
339
+ if (!has_payload(s, 3)) {
340
+ /* wait for payload */
341
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: No payload\n", __func__);
342
+ return false;
343
+ }
344
+
345
+ if (ctxt_key >= ARRAY_SIZE(s->key) ||
346
+ ctxt_iv >= ARRAY_SIZE(s->iv)) {
347
+ /* Invalid input */
348
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: Invalid key or iv\n", __func__);
349
+ return false;
350
+ }
351
+
352
+ src = g_byte_array_sized_new(len);
353
+ g_byte_array_set_size(src, len);
354
+ dst = g_byte_array_sized_new(len);
355
+ g_byte_array_set_size(dst, len);
356
+
357
+ r = dma_memory_read(s->as, src_addr, src->data, len, MEMTXATTRS_UNSPECIFIED);
358
+ if (r != MEMTX_OK) {
359
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: DMA read of %"PRIu32" bytes "
360
+ "from 0x%"PRIx64" failed. (r=%d)\n",
361
+ __func__, len, src_addr, r);
362
+ return false;
363
+ }
364
+
365
+ dump_data("cmd_data(): src_data=", src->data, len);
366
+
367
+ switch (s->key[ctxt_key].key_len) {
368
+ case 128 / 8:
369
+ alg = QCRYPTO_CIPHER_ALGO_AES_128;
370
+ break;
371
+ case 192 / 8:
372
+ alg = QCRYPTO_CIPHER_ALGO_AES_192;
373
+ break;
374
+ case 256 / 8:
375
+ alg = QCRYPTO_CIPHER_ALGO_AES_256;
376
+ break;
377
+ default:
378
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: Invalid key length\n", __func__);
379
+ return false;
380
+ }
381
+ cipher = qcrypto_cipher_new(alg, s->block_mode,
382
+ s->key[ctxt_key].key,
383
+ s->key[ctxt_key].key_len, NULL);
384
+ if (!cipher) {
385
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: Failed to create cipher object\n",
386
+ __func__);
387
+ return false;
388
+ }
389
+ if (s->block_mode != QCRYPTO_CIPHER_MODE_ECB) {
390
+ if (qcrypto_cipher_setiv(cipher, (void *)s->iv[ctxt_iv].iv,
391
+ sizeof(s->iv[ctxt_iv].iv), NULL) != 0) {
392
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: Failed to set IV\n", __func__);
393
+ return false;
394
+ }
395
+ }
396
+ if (s->is_encrypt) {
397
+ if (qcrypto_cipher_encrypt(cipher, src->data, dst->data, len, NULL) != 0) {
398
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: Encryption failed\n", __func__);
399
+ return false;
400
+ }
401
+ } else {
402
+ if (qcrypto_cipher_decrypt(cipher, src->data, dst->data, len, NULL) != 0) {
403
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: Decryption failed\n", __func__);
404
+ return false;
405
+ }
406
+ }
407
+
408
+ dump_data("cmd_data(): dst_data=", dst->data, len);
409
+ r = dma_memory_write(s->as, dst_addr, dst->data, len, MEMTXATTRS_UNSPECIFIED);
410
+ if (r != MEMTX_OK) {
411
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: DMA write of %"PRIu32" bytes "
412
+ "to 0x%"PRIx64" failed. (r=%d)\n",
413
+ __func__, len, src_addr, r);
414
+ return false;
415
+ }
416
+
417
+ return true;
418
+}
419
+
420
+static bool cmd_store_iv(AESState *s)
421
+{
422
+ uint32_t cmd = s->fifo[0];
423
+ uint32_t ctxt = (cmd & CMD_IV_CONTEXT_MASK) >> CMD_IV_CONTEXT_SHIFT;
424
+ uint64_t addr = s->fifo[1];
425
+
426
+ if (!has_payload(s, 1)) {
427
+ /* wait for payload */
428
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: No payload\n", __func__);
429
+ return false;
430
+ }
431
+
432
+ if (ctxt >= ARRAY_SIZE(s->iv)) {
433
+ /* Invalid context selected */
434
+ return false;
435
+ }
436
+
437
+ addr |= ((uint64_t)cmd << 32) & 0xff00000000ULL;
438
+ cpu_physical_memory_write(addr, &s->iv[ctxt].iv, sizeof(s->iv[ctxt].iv));
439
+
440
+ trace_aes_cmd_store_iv(ctxt, addr, s->iv[ctxt].iv[0], s->iv[ctxt].iv[1],
441
+ s->iv[ctxt].iv[2], s->iv[ctxt].iv[3]);
442
+
443
+ return true;
444
+}
445
+
446
+static bool cmd_flag(AESState *s)
447
+{
448
+ uint32_t cmd = s->fifo[0];
449
+ uint32_t raise_irq = cmd & CMD_FLAG_RAISE_IRQ_MASK;
450
+
451
+ /* We always process data when it's coming in, so fire an IRQ immediately */
452
+ if (raise_irq) {
453
+ s->irq_status |= REG_IRQ_STATUS_FLAG;
454
+ }
455
+
456
+ s->flag_info = cmd & CMD_FLAG_INFO_MASK;
457
+
458
+ trace_aes_cmd_flag(!!raise_irq, s->flag_info);
459
+
460
+ return true;
461
+}
462
+
463
+static void fifo_process(AESState *s)
464
+{
465
+ uint32_t cmd = s->fifo[0] >> CMD_SHIFT;
466
+ bool success = false;
467
+
468
+ if (!s->fifo_idx) {
469
+ return;
470
+ }
471
+
472
+ switch (cmd) {
473
+ case CMD_KEY:
474
+ success = cmd_key(s);
475
+ break;
476
+ case CMD_IV:
477
+ success = cmd_iv(s);
478
+ break;
479
+ case CMD_DATA:
480
+ success = cmd_data(s);
481
+ break;
482
+ case CMD_STORE_IV:
483
+ success = cmd_store_iv(s);
484
+ break;
485
+ case CMD_FLAG:
486
+ success = cmd_flag(s);
487
+ break;
488
+ default:
489
+ s->irq_status |= REG_IRQ_STATUS_INVALID_CMD;
490
+ break;
491
+ }
492
+
493
+ if (success) {
494
+ s->fifo_idx = 0;
495
+ }
496
+
497
+ trace_aes_fifo_process(cmd, success ? 1 : 0);
498
+}
499
+
500
+static void aes1_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
501
+{
502
+ AESState *s = opaque;
503
+
504
+ trace_aes_write(offset, val);
505
+
506
+ switch (offset) {
507
+ case REG_IRQ_STATUS:
508
+ s->irq_status &= ~val;
509
+ break;
510
+ case REG_IRQ_ENABLE:
511
+ s->irq_enable = val;
512
+ break;
513
+ case REG_FIFO:
514
+ fifo_append(s, val);
515
+ fifo_process(s);
516
+ break;
517
+ default:
518
+ qemu_log_mask(LOG_UNIMP,
519
+ "%s: Unknown AES MMIO offset %"PRIx64", data %"PRIx64"\n",
520
+ __func__, offset, val);
521
+ return;
522
+ }
523
+
524
+ aes_update_irq(s);
525
+}
526
+
527
+static const MemoryRegionOps aes1_ops = {
528
+ .read = aes1_read,
529
+ .write = aes1_write,
530
+ .endianness = DEVICE_NATIVE_ENDIAN,
531
+ .valid = {
532
+ .min_access_size = 4,
533
+ .max_access_size = 8,
534
+ },
535
+ .impl = {
536
+ .min_access_size = 4,
537
+ .max_access_size = 4,
538
+ },
539
+};
540
+
541
+static uint64_t aes2_read(void *opaque, hwaddr offset, unsigned size)
542
+{
543
+ uint64_t res = 0;
544
+
545
+ switch (offset) {
546
+ case 0:
547
+ res = 0;
548
+ break;
549
+ default:
550
+ qemu_log_mask(LOG_UNIMP,
551
+ "%s: Unknown AES MMIO 2 offset %"PRIx64"\n",
552
+ __func__, offset);
553
+ break;
554
+ }
555
+
556
+ trace_aes_2_read(offset, res);
557
+
558
+ return res;
559
+}
560
+
561
+static void aes2_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
562
+{
563
+ trace_aes_2_write(offset, val);
564
+
565
+ switch (offset) {
566
+ default:
567
+ qemu_log_mask(LOG_UNIMP,
568
+ "%s: Unknown AES MMIO 2 offset %"PRIx64", data %"PRIx64"\n",
569
+ __func__, offset, val);
570
+ return;
571
+ }
572
+}
573
+
574
+static const MemoryRegionOps aes2_ops = {
575
+ .read = aes2_read,
576
+ .write = aes2_write,
577
+ .endianness = DEVICE_NATIVE_ENDIAN,
578
+ .valid = {
579
+ .min_access_size = 4,
580
+ .max_access_size = 8,
581
+ },
582
+ .impl = {
583
+ .min_access_size = 4,
584
+ .max_access_size = 4,
585
+ },
586
+};
587
+
588
+static void aes_reset(Object *obj, ResetType type)
589
+{
590
+ AESState *s = APPLE_AES(obj);
591
+
592
+ s->status = 0x3f80;
593
+ s->q_status = 2;
594
+ s->irq_status = 0;
595
+ s->irq_enable = 0;
596
+ s->watermark = 0;
597
+}
598
+
599
+static void aes_init(Object *obj)
600
+{
601
+ AESState *s = APPLE_AES(obj);
602
+
603
+ memory_region_init_io(&s->iomem1, obj, &aes1_ops, s, TYPE_APPLE_AES, 0x4000);
604
+ memory_region_init_io(&s->iomem2, obj, &aes2_ops, s, TYPE_APPLE_AES, 0x4000);
605
+ sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem1);
606
+ sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem2);
607
+ sysbus_init_irq(SYS_BUS_DEVICE(s), &s->irq);
608
+ s->as = &address_space_memory;
609
+}
610
+
611
+static void aes_class_init(ObjectClass *klass, void *data)
612
+{
613
+ ResettableClass *rc = RESETTABLE_CLASS(klass);
614
+
615
+ rc->phases.hold = aes_reset;
616
+}
617
+
618
+static const TypeInfo aes_info = {
619
+ .name = TYPE_APPLE_AES,
620
+ .parent = TYPE_SYS_BUS_DEVICE,
621
+ .instance_size = sizeof(AESState),
622
+ .class_init = aes_class_init,
623
+ .instance_init = aes_init,
624
+};
625
+
626
+static void aes_register_types(void)
627
+{
628
+ type_register_static(&aes_info);
629
+}
630
+
631
+type_init(aes_register_types)
632
diff --git a/hw/vmapple/meson.build b/hw/vmapple/meson.build
633
index XXXXXXX..XXXXXXX 100644
634
--- a/hw/vmapple/meson.build
635
+++ b/hw/vmapple/meson.build
636
@@ -0,0 +1 @@
637
+system_ss.add(when: 'CONFIG_VMAPPLE_AES', if_true: files('aes.c'))
638
diff --git a/hw/vmapple/trace-events b/hw/vmapple/trace-events
639
index XXXXXXX..XXXXXXX 100644
640
--- a/hw/vmapple/trace-events
641
+++ b/hw/vmapple/trace-events
642
@@ -XXX,XX +XXX,XX @@
643
# See docs/devel/tracing.rst for syntax documentation.
644
645
+# aes.c
646
+aes_read(uint64_t offset, uint64_t res) "offset=0x%"PRIx64" res=0x%"PRIx64
647
+aes_cmd_key_select_builtin(uint32_t ctx, uint32_t key_id, const char *direction, const char *cipher) "[%d] Selecting builtin key %d to %scrypt with %s"
648
+aes_cmd_key_select_new(uint32_t ctx, uint32_t key_len, const char *direction, const char *cipher) "[%d] Selecting new key size=%d to %scrypt with %s"
649
+aes_cmd_iv(uint32_t ctx, uint32_t iv0, uint32_t iv1, uint32_t iv2, uint32_t iv3) "[%d] 0x%08x 0x%08x 0x%08x 0x%08x"
650
+aes_cmd_data(uint32_t key, uint32_t iv, uint64_t src, uint64_t dst, uint32_t len) "[key=%d iv=%d] src=0x%"PRIx64" dst=0x%"PRIx64" len=0x%x"
651
+aes_cmd_store_iv(uint32_t ctx, uint64_t addr, uint32_t iv0, uint32_t iv1, uint32_t iv2, uint32_t iv3) "[%d] addr=0x%"PRIx64"x -> 0x%08x 0x%08x 0x%08x 0x%08x"
652
+aes_cmd_flag(uint32_t raise, uint32_t flag_info) "raise=%d flag_info=0x%x"
653
+aes_fifo_process(uint32_t cmd, uint32_t success) "cmd=%d success=%d"
654
+aes_write(uint64_t offset, uint64_t val) "offset=0x%"PRIx64" val=0x%"PRIx64
655
+aes_2_read(uint64_t offset, uint64_t res) "offset=0x%"PRIx64" res=0x%"PRIx64
656
+aes_2_write(uint64_t offset, uint64_t val) "offset=0x%"PRIx64" val=0x%"PRIx64
657
+aes_dump_data(const char *desc, const char *hex) "%s%s"
658
+
659
diff --git a/include/hw/vmapple/vmapple.h b/include/hw/vmapple/vmapple.h
660
new file mode 100644
661
index XXXXXXX..XXXXXXX
662
--- /dev/null
663
+++ b/include/hw/vmapple/vmapple.h
664
@@ -XXX,XX +XXX,XX @@
665
+/*
666
+ * Devices specific to the VMApple machine type
667
+ *
668
+ * Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
669
+ *
670
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
671
+ * See the COPYING file in the top-level directory.
672
+ *
673
+ * SPDX-License-Identifier: GPL-2.0-or-later
674
+ */
675
+
676
+#ifndef HW_VMAPPLE_VMAPPLE_H
677
+#define HW_VMAPPLE_VMAPPLE_H
678
+
679
+#define TYPE_APPLE_AES "apple-aes"
680
+
681
+#endif /* HW_VMAPPLE_VMAPPLE_H */
682
diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
683
index XXXXXXX..XXXXXXX 100644
684
--- a/include/qemu/cutils.h
685
+++ b/include/qemu/cutils.h
686
@@ -XXX,XX +XXX,XX @@ GString *qemu_hexdump_line(GString *str, const void *buf, size_t len,
687
void qemu_hexdump(FILE *fp, const char *prefix,
688
const void *bufptr, size_t size);
689
690
+/**
691
+ * qemu_hexdump_to_buffer:
692
+ * @buffer: output string buffer
693
+ * @buffer_size: amount of available space in buffer. Must be at least
694
+ * data_size*2+1.
695
+ * @data: input bytes
696
+ * @data_size: number of bytes in data
697
+ *
698
+ * Converts the @data_size bytes in @data into hex digit pairs, writing them to
699
+ * @buffer. Finally, a nul terminating character is written; @buffer therefore
700
+ * needs space for (data_size*2+1) chars.
701
+ */
702
+void qemu_hexdump_to_buffer(char *restrict buffer, size_t buffer_size,
703
+ const uint8_t *restrict data, size_t data_size);
704
+
705
#endif
706
diff --git a/util/hexdump.c b/util/hexdump.c
707
index XXXXXXX..XXXXXXX 100644
708
--- a/util/hexdump.c
709
+++ b/util/hexdump.c
710
@@ -XXX,XX +XXX,XX @@
711
712
#include "qemu/osdep.h"
713
#include "qemu/cutils.h"
714
+#include "qemu/host-utils.h"
715
716
static inline char hexdump_nibble(unsigned x)
717
{
718
@@ -XXX,XX +XXX,XX @@ void qemu_hexdump(FILE *fp, const char *prefix,
719
}
720
721
}
722
+
723
+void qemu_hexdump_to_buffer(char *restrict buffer, size_t buffer_size,
724
+ const uint8_t *restrict data, size_t data_size)
725
+{
726
+ size_t i;
727
+ uint64_t required_buffer_size;
728
+ bool overflow = umul64_overflow(data_size, 2, &required_buffer_size);
729
+ overflow |= uadd64_overflow(required_buffer_size, 1, &required_buffer_size);
730
+ assert(buffer_size >= required_buffer_size && !overflow);
731
+
732
+ for (i = 0; i < data_size; i++) {
733
+ uint8_t val = data[i];
734
+ *(buffer++) = hexdump_nibble(val >> 4);
735
+ *(buffer++) = hexdump_nibble(val & 0xf);
736
+ }
737
+ *buffer = '\0';
738
+}
739
--
740
2.39.3 (Apple Git-145)
741
742
diff view generated by jsdifflib
Deleted patch
1
From: Alexander Graf <graf@amazon.com>
2
1
3
The VMApple machine exposes AUX and ROOT block devices (as well as USB OTG
4
emulation) via virtio-pci as well as a special, simple backdoor platform
5
device.
6
7
This patch implements this backdoor platform device to the best of my
8
understanding. I left out any USB OTG parts; they're only needed for
9
guest recovery and I don't understand the protocol yet.
10
11
Signed-off-by: Alexander Graf <graf@amazon.com>
12
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
13
---
14
15
v4:
16
17
* Moved most header code to .c, rest to vmapple.h
18
* Better compliance with coding, naming, and formatting conventions.
19
20
hw/vmapple/Kconfig | 3 +
21
hw/vmapple/bdif.c | 261 +++++++++++++++++++++++++++++++++++
22
hw/vmapple/meson.build | 1 +
23
hw/vmapple/trace-events | 5 +
24
include/hw/vmapple/vmapple.h | 2 +
25
5 files changed, 272 insertions(+)
26
create mode 100644 hw/vmapple/bdif.c
27
28
diff --git a/hw/vmapple/Kconfig b/hw/vmapple/Kconfig
29
index XXXXXXX..XXXXXXX 100644
30
--- a/hw/vmapple/Kconfig
31
+++ b/hw/vmapple/Kconfig
32
@@ -XXX,XX +XXX,XX @@
33
config VMAPPLE_AES
34
bool
35
36
+config VMAPPLE_BDIF
37
+ bool
38
+
39
diff --git a/hw/vmapple/bdif.c b/hw/vmapple/bdif.c
40
new file mode 100644
41
index XXXXXXX..XXXXXXX
42
--- /dev/null
43
+++ b/hw/vmapple/bdif.c
44
@@ -XXX,XX +XXX,XX @@
45
+/*
46
+ * VMApple Backdoor Interface
47
+ *
48
+ * Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
49
+ *
50
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
51
+ * See the COPYING file in the top-level directory.
52
+ *
53
+ * SPDX-License-Identifier: GPL-2.0-or-later
54
+ */
55
+
56
+#include "qemu/osdep.h"
57
+#include "qemu/units.h"
58
+#include "qemu/log.h"
59
+#include "qemu/module.h"
60
+#include "trace.h"
61
+#include "hw/vmapple/vmapple.h"
62
+#include "hw/sysbus.h"
63
+#include "hw/block/block.h"
64
+#include "qapi/error.h"
65
+#include "sysemu/block-backend.h"
66
+
67
+OBJECT_DECLARE_SIMPLE_TYPE(VMAppleBdifState, VMAPPLE_BDIF)
68
+
69
+struct VMAppleBdifState {
70
+ SysBusDevice parent_obj;
71
+
72
+ BlockBackend *aux;
73
+ BlockBackend *root;
74
+ MemoryRegion mmio;
75
+};
76
+
77
+#define VMAPPLE_BDIF_SIZE 0x00200000
78
+
79
+#define REG_DEVID_MASK 0xffff0000
80
+#define DEVID_ROOT 0x00000000
81
+#define DEVID_AUX 0x00010000
82
+#define DEVID_USB 0x00100000
83
+
84
+#define REG_STATUS 0x0
85
+#define REG_STATUS_ACTIVE BIT(0)
86
+#define REG_CFG 0x4
87
+#define REG_CFG_ACTIVE BIT(1)
88
+#define REG_UNK1 0x8
89
+#define REG_BUSY 0x10
90
+#define REG_BUSY_READY BIT(0)
91
+#define REG_UNK2 0x400
92
+#define REG_CMD 0x408
93
+#define REG_NEXT_DEVICE 0x420
94
+#define REG_UNK3 0x434
95
+
96
+typedef struct VblkSector {
97
+ uint32_t pad;
98
+ uint32_t pad2;
99
+ uint32_t sector;
100
+ uint32_t pad3;
101
+} VblkSector;
102
+
103
+typedef struct VblkReqCmd {
104
+ uint64_t addr;
105
+ uint32_t len;
106
+ uint32_t flags;
107
+} VblkReqCmd;
108
+
109
+typedef struct VblkReq {
110
+ VblkReqCmd sector;
111
+ VblkReqCmd data;
112
+ VblkReqCmd retval;
113
+} VblkReq;
114
+
115
+#define VBLK_DATA_FLAGS_READ 0x00030001
116
+#define VBLK_DATA_FLAGS_WRITE 0x00010001
117
+
118
+#define VBLK_RET_SUCCESS 0
119
+#define VBLK_RET_FAILED 1
120
+
121
+static uint64_t bdif_read(void *opaque, hwaddr offset, unsigned size)
122
+{
123
+ uint64_t ret = -1;
124
+ uint64_t devid = offset & REG_DEVID_MASK;
125
+
126
+ switch (offset & ~REG_DEVID_MASK) {
127
+ case REG_STATUS:
128
+ ret = REG_STATUS_ACTIVE;
129
+ break;
130
+ case REG_CFG:
131
+ ret = REG_CFG_ACTIVE;
132
+ break;
133
+ case REG_UNK1:
134
+ ret = 0x420;
135
+ break;
136
+ case REG_BUSY:
137
+ ret = REG_BUSY_READY;
138
+ break;
139
+ case REG_UNK2:
140
+ ret = 0x1;
141
+ break;
142
+ case REG_UNK3:
143
+ ret = 0x0;
144
+ break;
145
+ case REG_NEXT_DEVICE:
146
+ switch (devid) {
147
+ case DEVID_ROOT:
148
+ ret = 0x8000000;
149
+ break;
150
+ case DEVID_AUX:
151
+ ret = 0x10000;
152
+ break;
153
+ }
154
+ break;
155
+ }
156
+
157
+ trace_bdif_read(offset, size, ret);
158
+ return ret;
159
+}
160
+
161
+static void le2cpu_sector(VblkSector *sector)
162
+{
163
+ sector->sector = le32_to_cpu(sector->sector);
164
+}
165
+
166
+static void le2cpu_reqcmd(VblkReqCmd *cmd)
167
+{
168
+ cmd->addr = le64_to_cpu(cmd->addr);
169
+ cmd->len = le32_to_cpu(cmd->len);
170
+ cmd->flags = le32_to_cpu(cmd->flags);
171
+}
172
+
173
+static void le2cpu_req(VblkReq *req)
174
+{
175
+ le2cpu_reqcmd(&req->sector);
176
+ le2cpu_reqcmd(&req->data);
177
+ le2cpu_reqcmd(&req->retval);
178
+}
179
+
180
+static void vblk_cmd(uint64_t devid, BlockBackend *blk, uint64_t value,
181
+ uint64_t static_off)
182
+{
183
+ VblkReq req;
184
+ VblkSector sector;
185
+ uint64_t off = 0;
186
+ char *buf = NULL;
187
+ uint8_t ret = VBLK_RET_FAILED;
188
+ int r;
189
+
190
+ cpu_physical_memory_read(value, &req, sizeof(req));
191
+ le2cpu_req(&req);
192
+
193
+ if (req.sector.len != sizeof(sector)) {
194
+ ret = VBLK_RET_FAILED;
195
+ goto out;
196
+ }
197
+
198
+ /* Read the vblk command */
199
+ cpu_physical_memory_read(req.sector.addr, &sector, sizeof(sector));
200
+ le2cpu_sector(&sector);
201
+
202
+ off = sector.sector * 512ULL + static_off;
203
+
204
+ /* Sanity check that we're not allocating bogus sizes */
205
+ if (req.data.len > 128 * MiB) {
206
+ goto out;
207
+ }
208
+
209
+ buf = g_malloc0(req.data.len);
210
+ switch (req.data.flags) {
211
+ case VBLK_DATA_FLAGS_READ:
212
+ r = blk_pread(blk, off, req.data.len, buf, 0);
213
+ trace_bdif_vblk_read(devid == DEVID_AUX ? "aux" : "root",
214
+ req.data.addr, off, req.data.len, r);
215
+ if (r < 0) {
216
+ goto out;
217
+ }
218
+ cpu_physical_memory_write(req.data.addr, buf, req.data.len);
219
+ ret = VBLK_RET_SUCCESS;
220
+ break;
221
+ case VBLK_DATA_FLAGS_WRITE:
222
+ /* Not needed, iBoot only reads */
223
+ break;
224
+ default:
225
+ break;
226
+ }
227
+
228
+out:
229
+ g_free(buf);
230
+ cpu_physical_memory_write(req.retval.addr, &ret, 1);
231
+}
232
+
233
+static void bdif_write(void *opaque, hwaddr offset,
234
+ uint64_t value, unsigned size)
235
+{
236
+ VMAppleBdifState *s = opaque;
237
+ uint64_t devid = (offset & REG_DEVID_MASK);
238
+
239
+ trace_bdif_write(offset, size, value);
240
+
241
+ switch (offset & ~REG_DEVID_MASK) {
242
+ case REG_CMD:
243
+ switch (devid) {
244
+ case DEVID_ROOT:
245
+ vblk_cmd(devid, s->root, value, 0x0);
246
+ break;
247
+ case DEVID_AUX:
248
+ vblk_cmd(devid, s->aux, value, 0x0);
249
+ break;
250
+ }
251
+ break;
252
+ }
253
+}
254
+
255
+static const MemoryRegionOps bdif_ops = {
256
+ .read = bdif_read,
257
+ .write = bdif_write,
258
+ .endianness = DEVICE_NATIVE_ENDIAN,
259
+ .valid = {
260
+ .min_access_size = 1,
261
+ .max_access_size = 8,
262
+ },
263
+ .impl = {
264
+ .min_access_size = 1,
265
+ .max_access_size = 8,
266
+ },
267
+};
268
+
269
+static void bdif_init(Object *obj)
270
+{
271
+ VMAppleBdifState *s = VMAPPLE_BDIF(obj);
272
+
273
+ memory_region_init_io(&s->mmio, obj, &bdif_ops, obj,
274
+ "VMApple Backdoor Interface", VMAPPLE_BDIF_SIZE);
275
+ sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
276
+}
277
+
278
+static Property bdif_properties[] = {
279
+ DEFINE_PROP_DRIVE("aux", VMAppleBdifState, aux),
280
+ DEFINE_PROP_DRIVE("root", VMAppleBdifState, root),
281
+ DEFINE_PROP_END_OF_LIST(),
282
+};
283
+
284
+static void bdif_class_init(ObjectClass *klass, void *data)
285
+{
286
+ DeviceClass *dc = DEVICE_CLASS(klass);
287
+
288
+ dc->desc = "VMApple Backdoor Interface";
289
+ device_class_set_props(dc, bdif_properties);
290
+}
291
+
292
+static const TypeInfo bdif_info = {
293
+ .name = TYPE_VMAPPLE_BDIF,
294
+ .parent = TYPE_SYS_BUS_DEVICE,
295
+ .instance_size = sizeof(VMAppleBdifState),
296
+ .instance_init = bdif_init,
297
+ .class_init = bdif_class_init,
298
+};
299
+
300
+static void bdif_register_types(void)
301
+{
302
+ type_register_static(&bdif_info);
303
+}
304
+
305
+type_init(bdif_register_types)
306
diff --git a/hw/vmapple/meson.build b/hw/vmapple/meson.build
307
index XXXXXXX..XXXXXXX 100644
308
--- a/hw/vmapple/meson.build
309
+++ b/hw/vmapple/meson.build
310
@@ -1 +1,2 @@
311
system_ss.add(when: 'CONFIG_VMAPPLE_AES', if_true: files('aes.c'))
312
+system_ss.add(when: 'CONFIG_VMAPPLE_BDIF', if_true: files('bdif.c'))
313
diff --git a/hw/vmapple/trace-events b/hw/vmapple/trace-events
314
index XXXXXXX..XXXXXXX 100644
315
--- a/hw/vmapple/trace-events
316
+++ b/hw/vmapple/trace-events
317
@@ -XXX,XX +XXX,XX @@ aes_2_read(uint64_t offset, uint64_t res) "offset=0x%"PRIx64" res=0x%"PRIx64
318
aes_2_write(uint64_t offset, uint64_t val) "offset=0x%"PRIx64" val=0x%"PRIx64
319
aes_dump_data(const char *desc, const char *hex) "%s%s"
320
321
+# bdif.c
322
+bdif_read(uint64_t offset, uint32_t size, uint64_t value) "offset=0x%"PRIx64" size=0x%x value=0x%"PRIx64
323
+bdif_write(uint64_t offset, uint32_t size, uint64_t value) "offset=0x%"PRIx64" size=0x%x value=0x%"PRIx64
324
+bdif_vblk_read(const char *dev, uint64_t addr, uint64_t offset, uint32_t len, int r) "dev=%s addr=0x%"PRIx64" off=0x%"PRIx64" size=0x%x r=%d"
325
+
326
diff --git a/include/hw/vmapple/vmapple.h b/include/hw/vmapple/vmapple.h
327
index XXXXXXX..XXXXXXX 100644
328
--- a/include/hw/vmapple/vmapple.h
329
+++ b/include/hw/vmapple/vmapple.h
330
@@ -XXX,XX +XXX,XX @@
331
332
#define TYPE_APPLE_AES "apple-aes"
333
334
+#define TYPE_VMAPPLE_BDIF "vmapple-bdif"
335
+
336
#endif /* HW_VMAPPLE_VMAPPLE_H */
337
--
338
2.39.3 (Apple Git-145)
339
340
diff view generated by jsdifflib
Deleted patch
1
From: Alexander Graf <graf@amazon.com>
2
1
3
Instead of device tree or other more standardized means, VMApple passes
4
platform configuration to the first stage boot loader in a binary encoded
5
format that resides at a dedicated RAM region in physical address space.
6
7
This patch models this configuration space as a qdev device which we can
8
then map at the fixed location in the address space. That way, we can
9
influence and annotate all configuration fields easily.
10
11
Signed-off-by: Alexander Graf <graf@amazon.com>
12
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
13
---
14
15
v3:
16
17
* Replaced legacy device reset method with Resettable method
18
19
v4:
20
21
* Fixed initialisation of default values for properties
22
* Dropped superfluous endianness conversions
23
* Moved most header code to .c, device name #define goes in vmapple.h
24
25
v5:
26
27
* Improved error reporting in case of string property buffer overflow.
28
29
hw/vmapple/Kconfig | 3 +
30
hw/vmapple/cfg.c | 203 +++++++++++++++++++++++++++++++++++
31
hw/vmapple/meson.build | 1 +
32
include/hw/vmapple/vmapple.h | 2 +
33
4 files changed, 209 insertions(+)
34
create mode 100644 hw/vmapple/cfg.c
35
36
diff --git a/hw/vmapple/Kconfig b/hw/vmapple/Kconfig
37
index XXXXXXX..XXXXXXX 100644
38
--- a/hw/vmapple/Kconfig
39
+++ b/hw/vmapple/Kconfig
40
@@ -XXX,XX +XXX,XX @@ config VMAPPLE_AES
41
config VMAPPLE_BDIF
42
bool
43
44
+config VMAPPLE_CFG
45
+ bool
46
+
47
diff --git a/hw/vmapple/cfg.c b/hw/vmapple/cfg.c
48
new file mode 100644
49
index XXXXXXX..XXXXXXX
50
--- /dev/null
51
+++ b/hw/vmapple/cfg.c
52
@@ -XXX,XX +XXX,XX @@
53
+/*
54
+ * VMApple Configuration Region
55
+ *
56
+ * Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
57
+ *
58
+ * SPDX-License-Identifier: GPL-2.0-or-later
59
+ *
60
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
61
+ * See the COPYING file in the top-level directory.
62
+ */
63
+
64
+#include "qemu/osdep.h"
65
+#include "hw/vmapple/vmapple.h"
66
+#include "hw/sysbus.h"
67
+#include "qemu/log.h"
68
+#include "qemu/module.h"
69
+#include "qapi/error.h"
70
+#include "net/net.h"
71
+
72
+OBJECT_DECLARE_SIMPLE_TYPE(VMAppleCfgState, VMAPPLE_CFG)
73
+
74
+#define VMAPPLE_CFG_SIZE 0x00010000
75
+
76
+typedef struct VMAppleCfg {
77
+ uint32_t version; /* 0x000 */
78
+ uint32_t nr_cpus; /* 0x004 */
79
+ uint32_t unk1; /* 0x008 */
80
+ uint32_t unk2; /* 0x00c */
81
+ uint32_t unk3; /* 0x010 */
82
+ uint32_t unk4; /* 0x014 */
83
+ uint64_t ecid; /* 0x018 */
84
+ uint64_t ram_size; /* 0x020 */
85
+ uint32_t run_installer1; /* 0x028 */
86
+ uint32_t unk5; /* 0x02c */
87
+ uint32_t unk6; /* 0x030 */
88
+ uint32_t run_installer2; /* 0x034 */
89
+ uint32_t rnd; /* 0x038 */
90
+ uint32_t unk7; /* 0x03c */
91
+ MACAddr mac_en0; /* 0x040 */
92
+ uint8_t pad1[2];
93
+ MACAddr mac_en1; /* 0x048 */
94
+ uint8_t pad2[2];
95
+ MACAddr mac_wifi0; /* 0x050 */
96
+ uint8_t pad3[2];
97
+ MACAddr mac_bt0; /* 0x058 */
98
+ uint8_t pad4[2];
99
+ uint8_t reserved[0xa0]; /* 0x060 */
100
+ uint32_t cpu_ids[0x80]; /* 0x100 */
101
+ uint8_t scratch[0x200]; /* 0x180 */
102
+ char serial[32]; /* 0x380 */
103
+ char unk8[32]; /* 0x3a0 */
104
+ char model[32]; /* 0x3c0 */
105
+ uint8_t unk9[32]; /* 0x3e0 */
106
+ uint32_t unk10; /* 0x400 */
107
+ char soc_name[32]; /* 0x404 */
108
+} VMAppleCfg;
109
+
110
+struct VMAppleCfgState {
111
+ SysBusDevice parent_obj;
112
+ VMAppleCfg cfg;
113
+
114
+ MemoryRegion mem;
115
+ char *serial;
116
+ char *model;
117
+ char *soc_name;
118
+};
119
+
120
+static void vmapple_cfg_reset(Object *obj, ResetType type)
121
+{
122
+ VMAppleCfgState *s = VMAPPLE_CFG(obj);
123
+ VMAppleCfg *cfg;
124
+
125
+ cfg = memory_region_get_ram_ptr(&s->mem);
126
+ memset(cfg, 0, VMAPPLE_CFG_SIZE);
127
+ *cfg = s->cfg;
128
+}
129
+
130
+static bool strlcpy_set_error(char *restrict dst, const char *restrict src,
131
+ size_t dst_size, Error **errp,
132
+ const char *parent_func, const char *location,
133
+ const char *buffer_name, const char *extra_info)
134
+{
135
+ size_t len;
136
+
137
+ len = g_strlcpy(dst, src, dst_size);
138
+ if (len < dst_size) { /* len does not count nul terminator */
139
+ return true;
140
+ }
141
+
142
+ error_setg(errp,
143
+ "%s (%s) strlcpy error: Destination buffer %s too small "
144
+ "(need %zu, have %zu) %s",
145
+ parent_func, location, buffer_name, len + 1, dst_size, extra_info);
146
+ return false;
147
+}
148
+
149
+/*
150
+ * String copying wrapper that returns and reports a runtime error in
151
+ * case of truncation due to insufficient destination buffer space.
152
+ */
153
+#define strlcpy_array_return_error(dst_array, src, errp, extra_info) \
154
+ do { \
155
+ if (!strlcpy_set_error((dst_array), (src), ARRAY_SIZE(dst_array), \
156
+ (errp), __func__, stringify(__LINE__), \
157
+ # dst_array, extra_info)) { \
158
+ return; \
159
+ } \
160
+ } while (0)
161
+
162
+static void vmapple_cfg_realize(DeviceState *dev, Error **errp)
163
+{
164
+ VMAppleCfgState *s = VMAPPLE_CFG(dev);
165
+ uint32_t i;
166
+
167
+ if (!s->serial) {
168
+ s->serial = g_strdup("1234");
169
+ }
170
+ if (!s->model) {
171
+ s->model = g_strdup("VM0001");
172
+ }
173
+ if (!s->soc_name) {
174
+ s->soc_name = g_strdup("Apple M1 (Virtual)");
175
+ }
176
+
177
+ strlcpy_array_return_error(s->cfg.serial, s->serial, errp,
178
+ "setting 'serial' property on VMApple cfg device");
179
+ strlcpy_array_return_error(s->cfg.model, s->model, errp,
180
+ "setting 'model' property on VMApple cfg device");
181
+ strlcpy_array_return_error(s->cfg.soc_name, s->soc_name, errp,
182
+ "setting 'soc_name' property on VMApple cfg device");
183
+ strlcpy_array_return_error(s->cfg.unk8, "D/A", errp, "");
184
+ s->cfg.version = 2;
185
+ s->cfg.unk1 = 1;
186
+ s->cfg.unk2 = 1;
187
+ s->cfg.unk3 = 0x20;
188
+ s->cfg.unk4 = 0;
189
+ s->cfg.unk5 = 1;
190
+ s->cfg.unk6 = 1;
191
+ s->cfg.unk7 = 0;
192
+ s->cfg.unk10 = 1;
193
+
194
+ if (s->cfg.nr_cpus > ARRAY_SIZE(s->cfg.cpu_ids)) {
195
+ error_setg(errp,
196
+ "Failed to create %u CPUs, vmapple machine supports %zu max",
197
+ s->cfg.nr_cpus, ARRAY_SIZE(s->cfg.cpu_ids));
198
+ return;
199
+ }
200
+ for (i = 0; i < s->cfg.nr_cpus; i++) {
201
+ s->cfg.cpu_ids[i] = i;
202
+ }
203
+}
204
+
205
+static void vmapple_cfg_init(Object *obj)
206
+{
207
+ VMAppleCfgState *s = VMAPPLE_CFG(obj);
208
+
209
+ memory_region_init_ram(&s->mem, obj, "VMApple Config", VMAPPLE_CFG_SIZE,
210
+ &error_fatal);
211
+ sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mem);
212
+}
213
+
214
+static Property vmapple_cfg_properties[] = {
215
+ DEFINE_PROP_UINT32("nr-cpus", VMAppleCfgState, cfg.nr_cpus, 1),
216
+ DEFINE_PROP_UINT64("ecid", VMAppleCfgState, cfg.ecid, 0),
217
+ DEFINE_PROP_UINT64("ram-size", VMAppleCfgState, cfg.ram_size, 0),
218
+ DEFINE_PROP_UINT32("run_installer1", VMAppleCfgState, cfg.run_installer1, 0),
219
+ DEFINE_PROP_UINT32("run_installer2", VMAppleCfgState, cfg.run_installer2, 0),
220
+ DEFINE_PROP_UINT32("rnd", VMAppleCfgState, cfg.rnd, 0),
221
+ DEFINE_PROP_MACADDR("mac-en0", VMAppleCfgState, cfg.mac_en0),
222
+ DEFINE_PROP_MACADDR("mac-en1", VMAppleCfgState, cfg.mac_en1),
223
+ DEFINE_PROP_MACADDR("mac-wifi0", VMAppleCfgState, cfg.mac_wifi0),
224
+ DEFINE_PROP_MACADDR("mac-bt0", VMAppleCfgState, cfg.mac_bt0),
225
+ DEFINE_PROP_STRING("serial", VMAppleCfgState, serial),
226
+ DEFINE_PROP_STRING("model", VMAppleCfgState, model),
227
+ DEFINE_PROP_STRING("soc_name", VMAppleCfgState, soc_name),
228
+ DEFINE_PROP_END_OF_LIST(),
229
+};
230
+
231
+static void vmapple_cfg_class_init(ObjectClass *klass, void *data)
232
+{
233
+ DeviceClass *dc = DEVICE_CLASS(klass);
234
+ ResettableClass *rc = RESETTABLE_CLASS(klass);
235
+
236
+ dc->realize = vmapple_cfg_realize;
237
+ dc->desc = "VMApple Configuration Region";
238
+ device_class_set_props(dc, vmapple_cfg_properties);
239
+ rc->phases.hold = vmapple_cfg_reset;
240
+}
241
+
242
+static const TypeInfo vmapple_cfg_info = {
243
+ .name = TYPE_VMAPPLE_CFG,
244
+ .parent = TYPE_SYS_BUS_DEVICE,
245
+ .instance_size = sizeof(VMAppleCfgState),
246
+ .instance_init = vmapple_cfg_init,
247
+ .class_init = vmapple_cfg_class_init,
248
+};
249
+
250
+static void vmapple_cfg_register_types(void)
251
+{
252
+ type_register_static(&vmapple_cfg_info);
253
+}
254
+
255
+type_init(vmapple_cfg_register_types)
256
diff --git a/hw/vmapple/meson.build b/hw/vmapple/meson.build
257
index XXXXXXX..XXXXXXX 100644
258
--- a/hw/vmapple/meson.build
259
+++ b/hw/vmapple/meson.build
260
@@ -XXX,XX +XXX,XX @@
261
system_ss.add(when: 'CONFIG_VMAPPLE_AES', if_true: files('aes.c'))
262
system_ss.add(when: 'CONFIG_VMAPPLE_BDIF', if_true: files('bdif.c'))
263
+system_ss.add(when: 'CONFIG_VMAPPLE_CFG', if_true: files('cfg.c'))
264
diff --git a/include/hw/vmapple/vmapple.h b/include/hw/vmapple/vmapple.h
265
index XXXXXXX..XXXXXXX 100644
266
--- a/include/hw/vmapple/vmapple.h
267
+++ b/include/hw/vmapple/vmapple.h
268
@@ -XXX,XX +XXX,XX @@
269
270
#define TYPE_VMAPPLE_BDIF "vmapple-bdif"
271
272
+#define TYPE_VMAPPLE_CFG "vmapple-cfg"
273
+
274
#endif /* HW_VMAPPLE_VMAPPLE_H */
275
--
276
2.39.3 (Apple Git-145)
277
278
diff view generated by jsdifflib
Deleted patch
1
From: Alexander Graf <graf@amazon.com>
2
1
3
Apple has its own virtio-blk PCI device ID where it deviates from the
4
official virtio-pci spec slightly: It puts a new "apple type"
5
field at a static offset in config space and introduces a new barrier
6
command.
7
8
This patch first creates a mechanism for virtio-blk downstream classes to
9
handle unknown commands. It then creates such a downstream class and a new
10
vmapple-virtio-blk-pci class which support the additional apple type config
11
identifier as well as the barrier command.
12
13
It then exposes 2 subclasses from that that we can use to expose root and
14
aux virtio-blk devices: "vmapple-virtio-root" and "vmapple-virtio-aux".
15
16
Signed-off-by: Alexander Graf <graf@amazon.com>
17
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
18
---
19
20
v4:
21
22
* Use recommended object type declaration pattern.
23
* Correctly log unimplemented code paths.
24
* Most header code moved to .c, type name #defines moved to vmapple.h
25
26
v5:
27
28
* Corrected handling of potentially unaligned writes to virtio config area.
29
* Simplified passing through device variant type to subobject.
30
31
hw/block/virtio-blk.c | 19 ++-
32
hw/vmapple/Kconfig | 3 +
33
hw/vmapple/meson.build | 1 +
34
hw/vmapple/virtio-blk.c | 226 +++++++++++++++++++++++++++++++++
35
include/hw/pci/pci_ids.h | 1 +
36
include/hw/virtio/virtio-blk.h | 12 +-
37
include/hw/vmapple/vmapple.h | 4 +
38
7 files changed, 261 insertions(+), 5 deletions(-)
39
create mode 100644 hw/vmapple/virtio-blk.c
40
41
diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c
42
index XXXXXXX..XXXXXXX 100644
43
--- a/hw/block/virtio-blk.c
44
+++ b/hw/block/virtio-blk.c
45
@@ -XXX,XX +XXX,XX @@ static void virtio_blk_init_request(VirtIOBlock *s, VirtQueue *vq,
46
req->mr_next = NULL;
47
}
48
49
-static void virtio_blk_free_request(VirtIOBlockReq *req)
50
+void virtio_blk_free_request(VirtIOBlockReq *req)
51
{
52
g_free(req);
53
}
54
55
-static void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)
56
+void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)
57
{
58
VirtIOBlock *s = req->dev;
59
VirtIODevice *vdev = VIRTIO_DEVICE(s);
60
@@ -XXX,XX +XXX,XX @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
61
break;
62
}
63
default:
64
- virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
65
- virtio_blk_free_request(req);
66
+ {
67
+ /*
68
+ * Give subclasses a chance to handle unknown requests. This way the
69
+ * class lookup is not in the hot path.
70
+ */
71
+ VirtIOBlkClass *vbk = VIRTIO_BLK_GET_CLASS(s);
72
+ if (!vbk->handle_unknown_request ||
73
+ !vbk->handle_unknown_request(req, mrb, type)) {
74
+ virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
75
+ virtio_blk_free_request(req);
76
+ }
77
+ }
78
}
79
return 0;
80
}
81
@@ -XXX,XX +XXX,XX @@ static const TypeInfo virtio_blk_info = {
82
.instance_size = sizeof(VirtIOBlock),
83
.instance_init = virtio_blk_instance_init,
84
.class_init = virtio_blk_class_init,
85
+ .class_size = sizeof(VirtIOBlkClass),
86
};
87
88
static void virtio_register_types(void)
89
diff --git a/hw/vmapple/Kconfig b/hw/vmapple/Kconfig
90
index XXXXXXX..XXXXXXX 100644
91
--- a/hw/vmapple/Kconfig
92
+++ b/hw/vmapple/Kconfig
93
@@ -XXX,XX +XXX,XX @@ config VMAPPLE_BDIF
94
config VMAPPLE_CFG
95
bool
96
97
+config VMAPPLE_VIRTIO_BLK
98
+ bool
99
+
100
diff --git a/hw/vmapple/meson.build b/hw/vmapple/meson.build
101
index XXXXXXX..XXXXXXX 100644
102
--- a/hw/vmapple/meson.build
103
+++ b/hw/vmapple/meson.build
104
@@ -XXX,XX +XXX,XX @@
105
system_ss.add(when: 'CONFIG_VMAPPLE_AES', if_true: files('aes.c'))
106
system_ss.add(when: 'CONFIG_VMAPPLE_BDIF', if_true: files('bdif.c'))
107
system_ss.add(when: 'CONFIG_VMAPPLE_CFG', if_true: files('cfg.c'))
108
+system_ss.add(when: 'CONFIG_VMAPPLE_VIRTIO_BLK', if_true: files('virtio-blk.c'))
109
diff --git a/hw/vmapple/virtio-blk.c b/hw/vmapple/virtio-blk.c
110
new file mode 100644
111
index XXXXXXX..XXXXXXX
112
--- /dev/null
113
+++ b/hw/vmapple/virtio-blk.c
114
@@ -XXX,XX +XXX,XX @@
115
+/*
116
+ * VMApple specific VirtIO Block implementation
117
+ *
118
+ * Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
119
+ *
120
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
121
+ * See the COPYING file in the top-level directory.
122
+ *
123
+ * SPDX-License-Identifier: GPL-2.0-or-later
124
+ *
125
+ * VMApple uses almost standard VirtIO Block, but with a few key differences:
126
+ *
127
+ * - Different PCI device/vendor ID
128
+ * - An additional "type" identifier to differentiate AUX and Root volumes
129
+ * - An additional BARRIER command
130
+ */
131
+
132
+#include "qemu/osdep.h"
133
+#include "hw/vmapple/vmapple.h"
134
+#include "hw/virtio/virtio-blk.h"
135
+#include "hw/virtio/virtio-pci.h"
136
+#include "qemu/bswap.h"
137
+#include "qemu/log.h"
138
+#include "qemu/module.h"
139
+#include "qapi/error.h"
140
+
141
+OBJECT_DECLARE_TYPE(VMAppleVirtIOBlk, VMAppleVirtIOBlkClass, VMAPPLE_VIRTIO_BLK)
142
+
143
+typedef struct VMAppleVirtIOBlkClass {
144
+ VirtIOBlkClass parent;
145
+
146
+ void (*get_config)(VirtIODevice *vdev, uint8_t *config);
147
+} VMAppleVirtIOBlkClass;
148
+
149
+typedef struct VMAppleVirtIOBlk {
150
+ VirtIOBlock parent_obj;
151
+
152
+ uint32_t apple_type;
153
+} VMAppleVirtIOBlk;
154
+
155
+/*
156
+ * vmapple-virtio-blk-pci: This extends VirtioPCIProxy.
157
+ */
158
+#define TYPE_VMAPPLE_VIRTIO_BLK_PCI "vmapple-virtio-blk-pci-base"
159
+OBJECT_DECLARE_SIMPLE_TYPE(VMAppleVirtIOBlkPCI, VMAPPLE_VIRTIO_BLK_PCI)
160
+
161
+#define VIRTIO_BLK_T_APPLE_BARRIER 0x10000
162
+
163
+#define VIRTIO_APPLE_TYPE_ROOT 1
164
+#define VIRTIO_APPLE_TYPE_AUX 2
165
+
166
+static bool vmapple_virtio_blk_handle_unknown_request(VirtIOBlockReq *req,
167
+ MultiReqBuffer *mrb,
168
+ uint32_t type)
169
+{
170
+ switch (type) {
171
+ case VIRTIO_BLK_T_APPLE_BARRIER:
172
+ qemu_log_mask(LOG_UNIMP, "%s: Barrier requests are currently no-ops\n",
173
+ __func__);
174
+ virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
175
+ virtio_blk_free_request(req);
176
+ return true;
177
+ default:
178
+ return false;
179
+ }
180
+}
181
+
182
+/*
183
+ * VMApple virtio-blk uses the same config format as normal virtio, with one
184
+ * exception: It adds an "apple type" specififer at the same location that
185
+ * the spec reserves for max_secure_erase_sectors. Let's hook into the
186
+ * get_config code path here, run it as usual and then patch in the apple type.
187
+ */
188
+static void vmapple_virtio_blk_get_config(VirtIODevice *vdev, uint8_t *config)
189
+{
190
+ VMAppleVirtIOBlk *dev = VMAPPLE_VIRTIO_BLK(vdev);
191
+ VMAppleVirtIOBlkClass *vvbk = VMAPPLE_VIRTIO_BLK_GET_CLASS(dev);
192
+ struct virtio_blk_config *blkcfg = (struct virtio_blk_config *)config;
193
+
194
+ vvbk->get_config(vdev, config);
195
+
196
+ g_assert(dev->parent_obj.config_size >= endof(struct virtio_blk_config, zoned));
197
+
198
+ /* Apple abuses the field for max_secure_erase_sectors as type id */
199
+ stl_he_p(&blkcfg->max_secure_erase_sectors, dev->apple_type);
200
+}
201
+
202
+static void vmapple_virtio_blk_class_init(ObjectClass *klass, void *data)
203
+{
204
+ VirtIOBlkClass *vbk = VIRTIO_BLK_CLASS(klass);
205
+ VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
206
+ VMAppleVirtIOBlkClass *vvbk = VMAPPLE_VIRTIO_BLK_CLASS(klass);
207
+
208
+ vbk->handle_unknown_request = vmapple_virtio_blk_handle_unknown_request;
209
+ vvbk->get_config = vdc->get_config;
210
+ vdc->get_config = vmapple_virtio_blk_get_config;
211
+}
212
+
213
+static const TypeInfo vmapple_virtio_blk_info = {
214
+ .name = TYPE_VMAPPLE_VIRTIO_BLK,
215
+ .parent = TYPE_VIRTIO_BLK,
216
+ .instance_size = sizeof(VMAppleVirtIOBlk),
217
+ .class_init = vmapple_virtio_blk_class_init,
218
+};
219
+
220
+/* PCI Devices */
221
+
222
+struct VMAppleVirtIOBlkPCI {
223
+ VirtIOPCIProxy parent_obj;
224
+ VMAppleVirtIOBlk vdev;
225
+ uint32_t apple_type;
226
+};
227
+
228
+
229
+static Property vmapple_virtio_blk_pci_properties[] = {
230
+ DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
231
+ DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
232
+ VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
233
+ DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
234
+ DEV_NVECTORS_UNSPECIFIED),
235
+ DEFINE_PROP_END_OF_LIST(),
236
+};
237
+
238
+static void vmapple_virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
239
+{
240
+ VMAppleVirtIOBlkPCI *dev = VMAPPLE_VIRTIO_BLK_PCI(vpci_dev);
241
+ DeviceState *vdev = DEVICE(&dev->vdev);
242
+ VirtIOBlkConf *conf = &dev->vdev.parent_obj.conf;
243
+
244
+ if (conf->num_queues == VIRTIO_BLK_AUTO_NUM_QUEUES) {
245
+ conf->num_queues = virtio_pci_optimal_num_queues(0);
246
+ }
247
+
248
+ if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
249
+ vpci_dev->nvectors = conf->num_queues + 1;
250
+ }
251
+
252
+ /*
253
+ * We don't support zones, but we need the additional config space size.
254
+ * Let's just expose the feature so the rest of the virtio-blk logic
255
+ * allocates enough space for us. The guest will ignore zones anyway.
256
+ */
257
+ virtio_add_feature(&dev->vdev.parent_obj.host_features, VIRTIO_BLK_F_ZONED);
258
+ /* Propagate the apple type down to the virtio-blk device */
259
+ dev->vdev.apple_type = dev->apple_type;
260
+ /* and spawn the virtio-blk device */
261
+ qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
262
+
263
+ /*
264
+ * The virtio-pci machinery adjusts its vendor/device ID based on whether
265
+ * we support modern or legacy virtio. Let's patch it back to the Apple
266
+ * identifiers here.
267
+ */
268
+ pci_config_set_vendor_id(vpci_dev->pci_dev.config, PCI_VENDOR_ID_APPLE);
269
+ pci_config_set_device_id(vpci_dev->pci_dev.config,
270
+ PCI_DEVICE_ID_APPLE_VIRTIO_BLK);
271
+}
272
+
273
+static void vmapple_virtio_blk_pci_class_init(ObjectClass *klass, void *data)
274
+{
275
+ DeviceClass *dc = DEVICE_CLASS(klass);
276
+ VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
277
+ PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
278
+
279
+ set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
280
+ device_class_set_props(dc, vmapple_virtio_blk_pci_properties);
281
+ k->realize = vmapple_virtio_blk_pci_realize;
282
+ pcidev_k->vendor_id = PCI_VENDOR_ID_APPLE;
283
+ pcidev_k->device_id = PCI_DEVICE_ID_APPLE_VIRTIO_BLK;
284
+ pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
285
+ pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
286
+}
287
+
288
+static void vmapple_virtio_blk_pci_instance_init(Object *obj)
289
+{
290
+ VMAppleVirtIOBlkPCI *dev = VMAPPLE_VIRTIO_BLK_PCI(obj);
291
+
292
+ virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
293
+ TYPE_VMAPPLE_VIRTIO_BLK);
294
+}
295
+
296
+static const VirtioPCIDeviceTypeInfo vmapple_virtio_blk_pci_info = {
297
+ .base_name = TYPE_VMAPPLE_VIRTIO_BLK_PCI,
298
+ .generic_name = "vmapple-virtio-blk-pci",
299
+ .instance_size = sizeof(VMAppleVirtIOBlkPCI),
300
+ .instance_init = vmapple_virtio_blk_pci_instance_init,
301
+ .class_init = vmapple_virtio_blk_pci_class_init,
302
+};
303
+
304
+static void vmapple_virtio_root_instance_init(Object *obj)
305
+{
306
+ VMAppleVirtIOBlkPCI *dev = VMAPPLE_VIRTIO_BLK_PCI(obj);
307
+
308
+ dev->apple_type = VIRTIO_APPLE_TYPE_ROOT;
309
+}
310
+
311
+static const TypeInfo vmapple_virtio_root_info = {
312
+ .name = TYPE_VMAPPLE_VIRTIO_ROOT,
313
+ .parent = "vmapple-virtio-blk-pci",
314
+ .instance_size = sizeof(VMAppleVirtIOBlkPCI),
315
+ .instance_init = vmapple_virtio_root_instance_init,
316
+};
317
+
318
+static void vmapple_virtio_aux_instance_init(Object *obj)
319
+{
320
+ VMAppleVirtIOBlkPCI *dev = VMAPPLE_VIRTIO_BLK_PCI(obj);
321
+
322
+ dev->apple_type = VIRTIO_APPLE_TYPE_AUX;
323
+}
324
+
325
+static const TypeInfo vmapple_virtio_aux_info = {
326
+ .name = TYPE_VMAPPLE_VIRTIO_AUX,
327
+ .parent = "vmapple-virtio-blk-pci",
328
+ .instance_size = sizeof(VMAppleVirtIOBlkPCI),
329
+ .instance_init = vmapple_virtio_aux_instance_init,
330
+};
331
+
332
+static void vmapple_virtio_blk_register_types(void)
333
+{
334
+ type_register_static(&vmapple_virtio_blk_info);
335
+ virtio_pci_types_register(&vmapple_virtio_blk_pci_info);
336
+ type_register_static(&vmapple_virtio_root_info);
337
+ type_register_static(&vmapple_virtio_aux_info);
338
+}
339
+
340
+type_init(vmapple_virtio_blk_register_types)
341
diff --git a/include/hw/pci/pci_ids.h b/include/hw/pci/pci_ids.h
342
index XXXXXXX..XXXXXXX 100644
343
--- a/include/hw/pci/pci_ids.h
344
+++ b/include/hw/pci/pci_ids.h
345
@@ -XXX,XX +XXX,XX @@
346
#define PCI_DEVICE_ID_APPLE_UNI_N_AGP 0x0020
347
#define PCI_DEVICE_ID_APPLE_U3_AGP 0x004b
348
#define PCI_DEVICE_ID_APPLE_UNI_N_GMAC 0x0021
349
+#define PCI_DEVICE_ID_APPLE_VIRTIO_BLK 0x1a00
350
351
#define PCI_VENDOR_ID_SUN 0x108e
352
#define PCI_DEVICE_ID_SUN_EBUS 0x1000
353
diff --git a/include/hw/virtio/virtio-blk.h b/include/hw/virtio/virtio-blk.h
354
index XXXXXXX..XXXXXXX 100644
355
--- a/include/hw/virtio/virtio-blk.h
356
+++ b/include/hw/virtio/virtio-blk.h
357
@@ -XXX,XX +XXX,XX @@
358
#include "qapi/qapi-types-virtio.h"
359
360
#define TYPE_VIRTIO_BLK "virtio-blk-device"
361
-OBJECT_DECLARE_SIMPLE_TYPE(VirtIOBlock, VIRTIO_BLK)
362
+OBJECT_DECLARE_TYPE(VirtIOBlock, VirtIOBlkClass, VIRTIO_BLK)
363
364
/* This is the last element of the write scatter-gather list */
365
struct virtio_blk_inhdr
366
@@ -XXX,XX +XXX,XX @@ typedef struct MultiReqBuffer {
367
bool is_write;
368
} MultiReqBuffer;
369
370
+typedef struct VirtIOBlkClass {
371
+ /*< private >*/
372
+ VirtioDeviceClass parent;
373
+ /*< public >*/
374
+ bool (*handle_unknown_request)(VirtIOBlockReq *req, MultiReqBuffer *mrb,
375
+ uint32_t type);
376
+} VirtIOBlkClass;
377
+
378
void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq);
379
+void virtio_blk_free_request(VirtIOBlockReq *req);
380
+void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status);
381
382
#endif
383
diff --git a/include/hw/vmapple/vmapple.h b/include/hw/vmapple/vmapple.h
384
index XXXXXXX..XXXXXXX 100644
385
--- a/include/hw/vmapple/vmapple.h
386
+++ b/include/hw/vmapple/vmapple.h
387
@@ -XXX,XX +XXX,XX @@
388
389
#define TYPE_VMAPPLE_CFG "vmapple-cfg"
390
391
+#define TYPE_VMAPPLE_VIRTIO_BLK "vmapple-virtio-blk"
392
+#define TYPE_VMAPPLE_VIRTIO_ROOT "vmapple-virtio-root"
393
+#define TYPE_VMAPPLE_VIRTIO_AUX "vmapple-virtio-aux"
394
+
395
#endif /* HW_VMAPPLE_VMAPPLE_H */
396
--
397
2.39.3 (Apple Git-145)
398
399
diff view generated by jsdifflib
Deleted patch
1
The virtio_blk_free_request() function has been a 1-liner forwarding
2
to g_free() for a while now. We may as well call g_free on the request
3
pointer directly.
4
1
5
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
6
Reviewed-by: Akihiko Odaki <akihiko.odaki@daynix.com>
7
---
8
hw/block/virtio-blk.c | 43 +++++++++++++++-------------------
9
hw/vmapple/virtio-blk.c | 2 +-
10
include/hw/virtio/virtio-blk.h | 1 -
11
3 files changed, 20 insertions(+), 26 deletions(-)
12
13
diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c
14
index XXXXXXX..XXXXXXX 100644
15
--- a/hw/block/virtio-blk.c
16
+++ b/hw/block/virtio-blk.c
17
@@ -XXX,XX +XXX,XX @@ static void virtio_blk_init_request(VirtIOBlock *s, VirtQueue *vq,
18
req->mr_next = NULL;
19
}
20
21
-void virtio_blk_free_request(VirtIOBlockReq *req)
22
-{
23
- g_free(req);
24
-}
25
-
26
void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status)
27
{
28
VirtIOBlock *s = req->dev;
29
@@ -XXX,XX +XXX,XX @@ static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
30
if (acct_failed) {
31
block_acct_failed(blk_get_stats(s->blk), &req->acct);
32
}
33
- virtio_blk_free_request(req);
34
+ g_free(req);
35
}
36
37
blk_error_action(s->blk, action, is_read, error);
38
@@ -XXX,XX +XXX,XX @@ static void virtio_blk_rw_complete(void *opaque, int ret)
39
40
virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
41
block_acct_done(blk_get_stats(s->blk), &req->acct);
42
- virtio_blk_free_request(req);
43
+ g_free(req);
44
}
45
}
46
47
@@ -XXX,XX +XXX,XX @@ static void virtio_blk_flush_complete(void *opaque, int ret)
48
49
virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
50
block_acct_done(blk_get_stats(s->blk), &req->acct);
51
- virtio_blk_free_request(req);
52
+ g_free(req);
53
}
54
55
static void virtio_blk_discard_write_zeroes_complete(void *opaque, int ret)
56
@@ -XXX,XX +XXX,XX @@ static void virtio_blk_discard_write_zeroes_complete(void *opaque, int ret)
57
if (is_write_zeroes) {
58
block_acct_done(blk_get_stats(s->blk), &req->acct);
59
}
60
- virtio_blk_free_request(req);
61
+ g_free(req);
62
}
63
64
static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s, VirtQueue *vq)
65
@@ -XXX,XX +XXX,XX @@ static void virtio_blk_handle_scsi(VirtIOBlockReq *req)
66
67
fail:
68
virtio_blk_req_complete(req, status);
69
- virtio_blk_free_request(req);
70
+ g_free(req);
71
}
72
73
static inline void submit_requests(VirtIOBlock *s, MultiReqBuffer *mrb,
74
@@ -XXX,XX +XXX,XX @@ static void virtio_blk_zone_report_complete(void *opaque, int ret)
75
76
out:
77
virtio_blk_req_complete(req, err_status);
78
- virtio_blk_free_request(req);
79
+ g_free(req);
80
g_free(data->zone_report_data.zones);
81
g_free(data);
82
}
83
@@ -XXX,XX +XXX,XX @@ static void virtio_blk_handle_zone_report(VirtIOBlockReq *req,
84
return;
85
out:
86
virtio_blk_req_complete(req, err_status);
87
- virtio_blk_free_request(req);
88
+ g_free(req);
89
}
90
91
static void virtio_blk_zone_mgmt_complete(void *opaque, int ret)
92
@@ -XXX,XX +XXX,XX @@ static void virtio_blk_zone_mgmt_complete(void *opaque, int ret)
93
}
94
95
virtio_blk_req_complete(req, err_status);
96
- virtio_blk_free_request(req);
97
+ g_free(req);
98
}
99
100
static int virtio_blk_handle_zone_mgmt(VirtIOBlockReq *req, BlockZoneOp op)
101
@@ -XXX,XX +XXX,XX @@ static int virtio_blk_handle_zone_mgmt(VirtIOBlockReq *req, BlockZoneOp op)
102
return 0;
103
out:
104
virtio_blk_req_complete(req, err_status);
105
- virtio_blk_free_request(req);
106
+ g_free(req);
107
return err_status;
108
}
109
110
@@ -XXX,XX +XXX,XX @@ static void virtio_blk_zone_append_complete(void *opaque, int ret)
111
112
out:
113
virtio_blk_req_complete(req, err_status);
114
- virtio_blk_free_request(req);
115
+ g_free(req);
116
g_free(data);
117
}
118
119
@@ -XXX,XX +XXX,XX @@ static int virtio_blk_handle_zone_append(VirtIOBlockReq *req,
120
121
out:
122
virtio_blk_req_complete(req, err_status);
123
- virtio_blk_free_request(req);
124
+ g_free(req);
125
return err_status;
126
}
127
128
@@ -XXX,XX +XXX,XX @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
129
virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
130
block_acct_invalid(blk_get_stats(s->blk),
131
is_write ? BLOCK_ACCT_WRITE : BLOCK_ACCT_READ);
132
- virtio_blk_free_request(req);
133
+ g_free(req);
134
return 0;
135
}
136
137
@@ -XXX,XX +XXX,XX @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
138
VIRTIO_BLK_ID_BYTES));
139
iov_from_buf(in_iov, in_num, 0, serial, size);
140
virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
141
- virtio_blk_free_request(req);
142
+ g_free(req);
143
break;
144
}
145
case VIRTIO_BLK_T_ZONE_APPEND & ~VIRTIO_BLK_T_OUT:
146
@@ -XXX,XX +XXX,XX @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
147
if (unlikely(!(type & VIRTIO_BLK_T_OUT) ||
148
out_len > sizeof(dwz_hdr))) {
149
virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
150
- virtio_blk_free_request(req);
151
+ g_free(req);
152
return 0;
153
}
154
155
@@ -XXX,XX +XXX,XX @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
156
is_write_zeroes);
157
if (err_status != VIRTIO_BLK_S_OK) {
158
virtio_blk_req_complete(req, err_status);
159
- virtio_blk_free_request(req);
160
+ g_free(req);
161
}
162
163
break;
164
@@ -XXX,XX +XXX,XX @@ static int virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
165
if (!vbk->handle_unknown_request ||
166
!vbk->handle_unknown_request(req, mrb, type)) {
167
virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
168
- virtio_blk_free_request(req);
169
+ g_free(req);
170
}
171
}
172
}
173
@@ -XXX,XX +XXX,XX @@ void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq)
174
while ((req = virtio_blk_get_request(s, vq))) {
175
if (virtio_blk_handle_request(req, &mrb)) {
176
virtqueue_detach_element(req->vq, &req->elem, 0);
177
- virtio_blk_free_request(req);
178
+ g_free(req);
179
break;
180
}
181
}
182
@@ -XXX,XX +XXX,XX @@ static void virtio_blk_dma_restart_bh(void *opaque)
183
while (req) {
184
next = req->next;
185
virtqueue_detach_element(req->vq, &req->elem, 0);
186
- virtio_blk_free_request(req);
187
+ g_free(req);
188
req = next;
189
}
190
break;
191
@@ -XXX,XX +XXX,XX @@ static void virtio_blk_reset(VirtIODevice *vdev)
192
/* No other threads can access req->vq here */
193
virtqueue_detach_element(req->vq, &req->elem, 0);
194
195
- virtio_blk_free_request(req);
196
+ g_free(req);
197
}
198
}
199
200
diff --git a/hw/vmapple/virtio-blk.c b/hw/vmapple/virtio-blk.c
201
index XXXXXXX..XXXXXXX 100644
202
--- a/hw/vmapple/virtio-blk.c
203
+++ b/hw/vmapple/virtio-blk.c
204
@@ -XXX,XX +XXX,XX @@ static bool vmapple_virtio_blk_handle_unknown_request(VirtIOBlockReq *req,
205
qemu_log_mask(LOG_UNIMP, "%s: Barrier requests are currently no-ops\n",
206
__func__);
207
virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
208
- virtio_blk_free_request(req);
209
+ g_free(req);
210
return true;
211
default:
212
return false;
213
diff --git a/include/hw/virtio/virtio-blk.h b/include/hw/virtio/virtio-blk.h
214
index XXXXXXX..XXXXXXX 100644
215
--- a/include/hw/virtio/virtio-blk.h
216
+++ b/include/hw/virtio/virtio-blk.h
217
@@ -XXX,XX +XXX,XX @@ typedef struct VirtIOBlkClass {
218
} VirtIOBlkClass;
219
220
void virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq);
221
-void virtio_blk_free_request(VirtIOBlockReq *req);
222
void virtio_blk_req_complete(VirtIOBlockReq *req, unsigned char status);
223
224
#endif
225
--
226
2.39.3 (Apple Git-145)
diff view generated by jsdifflib
Deleted patch
1
From: Alexander Graf <graf@amazon.com>
2
1
3
Apple defines a new "vmapple" machine type as part of its proprietary
4
macOS Virtualization.Framework vmm. This machine type is similar to the
5
virt one, but with subtle differences in base devices, a few special
6
vmapple device additions and a vastly different boot chain.
7
8
This patch reimplements this machine type in QEMU. To use it, you
9
have to have a readily installed version of macOS for VMApple,
10
run on macOS with -accel hvf, pass the Virtualization.Framework
11
boot rom (AVPBooter) in via -bios, pass the aux and root volume as pflash
12
and pass aux and root volume as virtio drives. In addition, you also
13
need to find the machine UUID and pass that as -M vmapple,uuid= parameter:
14
15
$ qemu-system-aarch64 -accel hvf -M vmapple,uuid=0x1234 -m 4G \
16
-bios /System/Library/Frameworks/Virtualization.framework/Versions/A/Resources/AVPBooter.vmapple2.bin
17
-drive file=aux,if=pflash,format=raw \
18
-drive file=root,if=pflash,format=raw \
19
-drive file=aux,if=none,id=aux,format=raw \
20
-device vmapple-virtio-aux,drive=aux \
21
-drive file=root,if=none,id=root,format=raw \
22
-device vmapple-virtio-root,drive=root
23
24
With all these in place, you should be able to see macOS booting
25
successfully.
26
27
Known issues:
28
- Keyboard and mouse/tablet input is laggy. The reason for this is
29
either that macOS's XHCI driver is broken when the device/platform
30
does not support MSI/MSI-X, or there's some unfortunate interplay
31
with Qemu's XHCI implementation in this scenario.
32
- Currently only macOS 12 guests are supported. The boot process for
33
13+ will need further investigation and adjustment.
34
35
Signed-off-by: Alexander Graf <graf@amazon.com>
36
Co-authored-by: Phil Dennis-Jordan <phil@philjordan.eu>
37
Signed-off-by: Phil Dennis-Jordan <phil@philjordan.eu>
38
---
39
40
v3:
41
* Rebased on latest upstream, updated affinity and NIC creation
42
API usage
43
* Included Apple-variant virtio-blk in build dependency
44
* Updated API usage for setting 'redist-region-count' array-typed property on GIC.
45
* Switched from virtio HID devices (for which macOS 12 does not contain
46
drivers) to an XHCI USB controller and USB HID devices.
47
48
v4:
49
* Fixups for v4 changes to the other patches in the set.
50
* Corrected the assert macro to use
51
* Removed superfluous endian conversions corresponding to cfg's.
52
* Init error handling improvement.
53
* No need to select CPU type on TCG, as only HVF is supported.
54
* Machine type version bumped to 9.2
55
* #include order improved
56
57
v5:
58
* Fixed memory reservation for ecam alias region.
59
* Better error handling setting properties on devices.
60
* Simplified the machine ECID/UUID extraction script and actually created a
61
file for it rather than quoting its code in documentation.
62
63
MAINTAINERS | 1 +
64
contrib/vmapple/uuid.sh | 9 +
65
docs/system/arm/vmapple.rst | 60 ++++
66
docs/system/target-arm.rst | 1 +
67
hw/vmapple/Kconfig | 20 ++
68
hw/vmapple/meson.build | 1 +
69
hw/vmapple/vmapple.c | 659 ++++++++++++++++++++++++++++++++++++
70
7 files changed, 751 insertions(+)
71
create mode 100755 contrib/vmapple/uuid.sh
72
create mode 100644 docs/system/arm/vmapple.rst
73
create mode 100644 hw/vmapple/vmapple.c
74
75
diff --git a/MAINTAINERS b/MAINTAINERS
76
index XXXXXXX..XXXXXXX 100644
77
--- a/MAINTAINERS
78
+++ b/MAINTAINERS
79
@@ -XXX,XX +XXX,XX @@ R: Phil Dennis-Jordan <phil@philjordan.eu>
80
S: Maintained
81
F: hw/vmapple/*
82
F: include/hw/vmapple/*
83
+F: docs/system/arm/vmapple.rst
84
85
Subsystems
86
----------
87
diff --git a/contrib/vmapple/uuid.sh b/contrib/vmapple/uuid.sh
88
new file mode 100755
89
index XXXXXXX..XXXXXXX
90
--- /dev/null
91
+++ b/contrib/vmapple/uuid.sh
92
@@ -XXX,XX +XXX,XX @@
93
+#!/bin/sh
94
+# Used for converting a guest provisioned using Virtualization.framework
95
+# for use with the QEMU 'vmapple' aarch64 machine type.
96
+#
97
+# Extracts the Machine UUID from Virtualization.framework VM JSON file.
98
+# (as produced by 'macosvm', passed as command line argument)
99
+
100
+plutil -extract machineId raw "$1" | base64 -d | plutil -extract ECID raw -
101
+
102
diff --git a/docs/system/arm/vmapple.rst b/docs/system/arm/vmapple.rst
103
new file mode 100644
104
index XXXXXXX..XXXXXXX
105
--- /dev/null
106
+++ b/docs/system/arm/vmapple.rst
107
@@ -XXX,XX +XXX,XX @@
108
+VMApple machine emulation
109
+========================================================================================
110
+
111
+VMApple is the device model that the macOS built-in hypervisor called "Virtualization.framework"
112
+exposes to Apple Silicon macOS guests. The "vmapple" machine model in QEMU implements the same
113
+device model, but does not use any code from Virtualization.Framework.
114
+
115
+Prerequisites
116
+-------------
117
+
118
+To run the vmapple machine model, you need to
119
+
120
+ * Run on Apple Silicon
121
+ * Run on macOS 12.0 or above
122
+ * Have an already installed copy of a Virtualization.Framework macOS 12 virtual machine. I will
123
+ assume that you installed it using the macosvm CLI.
124
+
125
+First, we need to extract the UUID from the virtual machine that you installed. You can do this
126
+by running the shell script in contrib/vmapple/uuid.sh on the macosvm.json file.
127
+
128
+.. code-block:: bash
129
+ :caption: uuid.sh script to extract the UUID from a macosvm.json file
130
+
131
+ $ contrib/vmapple/uuid.sh "path/to/macosvm.json"
132
+
133
+Now we also need to trim the aux partition. It contains metadata that we can just discard:
134
+
135
+.. code-block:: bash
136
+ :caption: Command to trim the aux file
137
+
138
+ $ dd if="aux.img" of="aux.img.trimmed" bs=$(( 0x4000 )) skip=1
139
+
140
+How to run
141
+----------
142
+
143
+Then, we can launch QEMU with the Virtualization.Framework pre-boot environment and the readily
144
+installed target disk images. I recommend to port forward the VM's ssh and vnc ports to the host
145
+to get better interactive access into the target system:
146
+
147
+.. code-block:: bash
148
+ :caption: Example execution command line
149
+
150
+ $ UUID=$(uuid.sh macosvm.json)
151
+ $ AVPBOOTER=/System/Library/Frameworks/Virtualization.framework/Resources/AVPBooter.vmapple2.bin
152
+ $ AUX=aux.img.trimmed
153
+ $ DISK=disk.img
154
+ $ qemu-system-aarch64 \
155
+ -serial mon:stdio \
156
+ -m 4G \
157
+ -accel hvf \
158
+ -M vmapple,uuid=$UUID \
159
+ -bios $AVPBOOTER \
160
+ -drive file="$AUX",if=pflash,format=raw \
161
+ -drive file="$DISK",if=pflash,format=raw \
162
+ -drive file="$AUX",if=none,id=aux,format=raw \
163
+ -drive file="$DISK",if=none,id=root,format=raw \
164
+ -device vmapple-virtio-aux,drive=aux \
165
+ -device vmapple-virtio-root,drive=root \
166
+ -net user,ipv6=off,hostfwd=tcp::2222-:22,hostfwd=tcp::5901-:5900 \
167
+ -net nic,model=virtio-net-pci \
168
diff --git a/docs/system/target-arm.rst b/docs/system/target-arm.rst
169
index XXXXXXX..XXXXXXX 100644
170
--- a/docs/system/target-arm.rst
171
+++ b/docs/system/target-arm.rst
172
@@ -XXX,XX +XXX,XX @@ Board-specific documentation
173
arm/stellaris
174
arm/stm32
175
arm/virt
176
+ arm/vmapple
177
arm/xenpvh
178
arm/xlnx-versal-virt
179
arm/xlnx-zynq
180
diff --git a/hw/vmapple/Kconfig b/hw/vmapple/Kconfig
181
index XXXXXXX..XXXXXXX 100644
182
--- a/hw/vmapple/Kconfig
183
+++ b/hw/vmapple/Kconfig
184
@@ -XXX,XX +XXX,XX @@ config VMAPPLE_CFG
185
config VMAPPLE_VIRTIO_BLK
186
bool
187
188
+config VMAPPLE
189
+ bool
190
+ depends on ARM
191
+ depends on HVF
192
+ default y if ARM
193
+ imply PCI_DEVICES
194
+ select ARM_GIC
195
+ select PLATFORM_BUS
196
+ select PCI_EXPRESS
197
+ select PCI_EXPRESS_GENERIC_BRIDGE
198
+ select PL011 # UART
199
+ select PL031 # RTC
200
+ select PL061 # GPIO
201
+ select GPIO_PWR
202
+ select PVPANIC_MMIO
203
+ select VMAPPLE_AES
204
+ select VMAPPLE_BDIF
205
+ select VMAPPLE_CFG
206
+ select MAC_PVG_MMIO
207
+ select VMAPPLE_VIRTIO_BLK
208
diff --git a/hw/vmapple/meson.build b/hw/vmapple/meson.build
209
index XXXXXXX..XXXXXXX 100644
210
--- a/hw/vmapple/meson.build
211
+++ b/hw/vmapple/meson.build
212
@@ -XXX,XX +XXX,XX @@ system_ss.add(when: 'CONFIG_VMAPPLE_AES', if_true: files('aes.c'))
213
system_ss.add(when: 'CONFIG_VMAPPLE_BDIF', if_true: files('bdif.c'))
214
system_ss.add(when: 'CONFIG_VMAPPLE_CFG', if_true: files('cfg.c'))
215
system_ss.add(when: 'CONFIG_VMAPPLE_VIRTIO_BLK', if_true: files('virtio-blk.c'))
216
+specific_ss.add(when: 'CONFIG_VMAPPLE', if_true: files('vmapple.c'))
217
diff --git a/hw/vmapple/vmapple.c b/hw/vmapple/vmapple.c
218
new file mode 100644
219
index XXXXXXX..XXXXXXX
220
--- /dev/null
221
+++ b/hw/vmapple/vmapple.c
222
@@ -XXX,XX +XXX,XX @@
223
+/*
224
+ * VMApple machine emulation
225
+ *
226
+ * Copyright © 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
227
+ *
228
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
229
+ * See the COPYING file in the top-level directory.
230
+ *
231
+ * SPDX-License-Identifier: GPL-2.0-or-later
232
+ *
233
+ * VMApple is the device model that the macOS built-in hypervisor called
234
+ * "Virtualization.framework" exposes to Apple Silicon macOS guests. The
235
+ * machine model in this file implements the same device model in QEMU, but
236
+ * does not use any code from Virtualization.Framework.
237
+ */
238
+
239
+#include "qemu/osdep.h"
240
+#include "qemu/bitops.h"
241
+#include "qemu/datadir.h"
242
+#include "qemu/error-report.h"
243
+#include "qemu/guest-random.h"
244
+#include "qemu/help-texts.h"
245
+#include "qemu/log.h"
246
+#include "qemu/module.h"
247
+#include "qemu/option.h"
248
+#include "qemu/units.h"
249
+#include "monitor/qdev.h"
250
+#include "hw/boards.h"
251
+#include "hw/irq.h"
252
+#include "hw/loader.h"
253
+#include "hw/qdev-properties.h"
254
+#include "hw/sysbus.h"
255
+#include "hw/usb.h"
256
+#include "hw/arm/boot.h"
257
+#include "hw/arm/primecell.h"
258
+#include "hw/char/pl011.h"
259
+#include "hw/intc/arm_gic.h"
260
+#include "hw/intc/arm_gicv3_common.h"
261
+#include "hw/misc/pvpanic.h"
262
+#include "hw/pci-host/gpex.h"
263
+#include "hw/usb/xhci.h"
264
+#include "hw/virtio/virtio-pci.h"
265
+#include "hw/vmapple/vmapple.h"
266
+#include "net/net.h"
267
+#include "qapi/error.h"
268
+#include "qapi/qmp/qlist.h"
269
+#include "qapi/visitor.h"
270
+#include "qapi/qapi-visit-common.h"
271
+#include "standard-headers/linux/input.h"
272
+#include "sysemu/hvf.h"
273
+#include "sysemu/kvm.h"
274
+#include "sysemu/reset.h"
275
+#include "sysemu/runstate.h"
276
+#include "sysemu/sysemu.h"
277
+#include "target/arm/internals.h"
278
+#include "target/arm/kvm_arm.h"
279
+
280
+struct VMAppleMachineClass {
281
+ MachineClass parent;
282
+};
283
+
284
+struct VMAppleMachineState {
285
+ MachineState parent;
286
+
287
+ Notifier machine_done;
288
+ struct arm_boot_info bootinfo;
289
+ MemMapEntry *memmap;
290
+ const int *irqmap;
291
+ DeviceState *gic;
292
+ DeviceState *cfg;
293
+ Notifier powerdown_notifier;
294
+ PCIBus *bus;
295
+ MemoryRegion fw_mr;
296
+ MemoryRegion ecam_alias;
297
+ uint64_t uuid;
298
+};
299
+
300
+#define DEFINE_VMAPPLE_MACHINE_LATEST(major, minor, latest) \
301
+ static void vmapple##major##_##minor##_class_init(ObjectClass *oc, \
302
+ void *data) \
303
+ { \
304
+ MachineClass *mc = MACHINE_CLASS(oc); \
305
+ vmapple_machine_##major##_##minor##_options(mc); \
306
+ mc->desc = "QEMU " # major "." # minor " Apple Virtual Machine"; \
307
+ if (latest) { \
308
+ mc->alias = "vmapple"; \
309
+ } \
310
+ } \
311
+ static const TypeInfo machvmapple##major##_##minor##_info = { \
312
+ .name = MACHINE_TYPE_NAME("vmapple-" # major "." # minor), \
313
+ .parent = TYPE_VMAPPLE_MACHINE, \
314
+ .class_init = vmapple##major##_##minor##_class_init, \
315
+ }; \
316
+ static void machvmapple_machine_##major##_##minor##_init(void) \
317
+ { \
318
+ type_register_static(&machvmapple##major##_##minor##_info); \
319
+ } \
320
+ type_init(machvmapple_machine_##major##_##minor##_init);
321
+
322
+#define DEFINE_VMAPPLE_MACHINE_AS_LATEST(major, minor) \
323
+ DEFINE_VMAPPLE_MACHINE_LATEST(major, minor, true)
324
+#define DEFINE_VMAPPLE_MACHINE(major, minor) \
325
+ DEFINE_VMAPPLE_MACHINE_LATEST(major, minor, false)
326
+
327
+#define TYPE_VMAPPLE_MACHINE MACHINE_TYPE_NAME("vmapple")
328
+OBJECT_DECLARE_TYPE(VMAppleMachineState, VMAppleMachineClass, VMAPPLE_MACHINE)
329
+
330
+/* Number of external interrupt lines to configure the GIC with */
331
+#define NUM_IRQS 256
332
+
333
+enum {
334
+ VMAPPLE_FIRMWARE,
335
+ VMAPPLE_CONFIG,
336
+ VMAPPLE_MEM,
337
+ VMAPPLE_GIC_DIST,
338
+ VMAPPLE_GIC_REDIST,
339
+ VMAPPLE_UART,
340
+ VMAPPLE_RTC,
341
+ VMAPPLE_PCIE,
342
+ VMAPPLE_PCIE_MMIO,
343
+ VMAPPLE_PCIE_ECAM,
344
+ VMAPPLE_GPIO,
345
+ VMAPPLE_PVPANIC,
346
+ VMAPPLE_APV_GFX,
347
+ VMAPPLE_APV_IOSFC,
348
+ VMAPPLE_AES_1,
349
+ VMAPPLE_AES_2,
350
+ VMAPPLE_BDOOR,
351
+ VMAPPLE_MEMMAP_LAST,
352
+};
353
+
354
+static MemMapEntry memmap[] = {
355
+ [VMAPPLE_FIRMWARE] = { 0x00100000, 0x00100000 },
356
+ [VMAPPLE_CONFIG] = { 0x00400000, 0x00010000 },
357
+
358
+ [VMAPPLE_GIC_DIST] = { 0x10000000, 0x00010000 },
359
+ [VMAPPLE_GIC_REDIST] = { 0x10010000, 0x00400000 },
360
+
361
+ [VMAPPLE_UART] = { 0x20010000, 0x00010000 },
362
+ [VMAPPLE_RTC] = { 0x20050000, 0x00001000 },
363
+ [VMAPPLE_GPIO] = { 0x20060000, 0x00001000 },
364
+ [VMAPPLE_PVPANIC] = { 0x20070000, 0x00000002 },
365
+ [VMAPPLE_BDOOR] = { 0x30000000, 0x00200000 },
366
+ [VMAPPLE_APV_GFX] = { 0x30200000, 0x00010000 },
367
+ [VMAPPLE_APV_IOSFC] = { 0x30210000, 0x00010000 },
368
+ [VMAPPLE_AES_1] = { 0x30220000, 0x00004000 },
369
+ [VMAPPLE_AES_2] = { 0x30230000, 0x00004000 },
370
+ [VMAPPLE_PCIE_ECAM] = { 0x40000000, 0x10000000 },
371
+ [VMAPPLE_PCIE_MMIO] = { 0x50000000, 0x1fff0000 },
372
+
373
+ /* Actual RAM size depends on configuration */
374
+ [VMAPPLE_MEM] = { 0x70000000ULL, GiB},
375
+};
376
+
377
+static const int irqmap[] = {
378
+ [VMAPPLE_UART] = 1,
379
+ [VMAPPLE_RTC] = 2,
380
+ [VMAPPLE_GPIO] = 0x5,
381
+ [VMAPPLE_APV_IOSFC] = 0x10,
382
+ [VMAPPLE_APV_GFX] = 0x11,
383
+ [VMAPPLE_AES_1] = 0x12,
384
+ [VMAPPLE_PCIE] = 0x20,
385
+};
386
+
387
+#define GPEX_NUM_IRQS 16
388
+
389
+static void create_bdif(VMAppleMachineState *vms, MemoryRegion *mem)
390
+{
391
+ DeviceState *bdif;
392
+ SysBusDevice *bdif_sb;
393
+ DriveInfo *di_aux = drive_get(IF_PFLASH, 0, 0);
394
+ DriveInfo *di_root = drive_get(IF_PFLASH, 0, 1);
395
+
396
+ if (!di_aux) {
397
+ error_report("No AUX device. Please specify one as pflash drive.");
398
+ exit(1);
399
+ }
400
+
401
+ if (!di_root) {
402
+ /* Fall back to the first IF_VIRTIO device as root device */
403
+ di_root = drive_get(IF_VIRTIO, 0, 0);
404
+ }
405
+
406
+ if (!di_root) {
407
+ error_report("No root device. Please specify one as virtio drive.");
408
+ exit(1);
409
+ }
410
+
411
+ /* PV backdoor device */
412
+ bdif = qdev_new(TYPE_VMAPPLE_BDIF);
413
+ bdif_sb = SYS_BUS_DEVICE(bdif);
414
+ sysbus_mmio_map(bdif_sb, 0, vms->memmap[VMAPPLE_BDOOR].base);
415
+
416
+ qdev_prop_set_drive(DEVICE(bdif), "aux", blk_by_legacy_dinfo(di_aux));
417
+ qdev_prop_set_drive(DEVICE(bdif), "root", blk_by_legacy_dinfo(di_root));
418
+
419
+ sysbus_realize_and_unref(bdif_sb, &error_fatal);
420
+}
421
+
422
+static void create_pvpanic(VMAppleMachineState *vms, MemoryRegion *mem)
423
+{
424
+ SysBusDevice *cfg;
425
+
426
+ vms->cfg = qdev_new(TYPE_PVPANIC_MMIO_DEVICE);
427
+ cfg = SYS_BUS_DEVICE(vms->cfg);
428
+ sysbus_mmio_map(cfg, 0, vms->memmap[VMAPPLE_PVPANIC].base);
429
+
430
+ sysbus_realize_and_unref(cfg, &error_fatal);
431
+}
432
+
433
+static void create_cfg(VMAppleMachineState *vms, MemoryRegion *mem)
434
+{
435
+ SysBusDevice *cfg;
436
+ MachineState *machine = MACHINE(vms);
437
+ uint32_t rnd = 1;
438
+
439
+ vms->cfg = qdev_new(TYPE_VMAPPLE_CFG);
440
+ cfg = SYS_BUS_DEVICE(vms->cfg);
441
+ sysbus_mmio_map(cfg, 0, vms->memmap[VMAPPLE_CONFIG].base);
442
+
443
+ qemu_guest_getrandom_nofail(&rnd, sizeof(rnd));
444
+
445
+ qdev_prop_set_uint32(vms->cfg, "nr-cpus", machine->smp.cpus);
446
+ qdev_prop_set_uint64(vms->cfg, "ecid", vms->uuid);
447
+ qdev_prop_set_uint64(vms->cfg, "ram-size", machine->ram_size);
448
+ qdev_prop_set_uint32(vms->cfg, "rnd", rnd);
449
+
450
+ sysbus_realize_and_unref(cfg, &error_fatal);
451
+}
452
+
453
+static void create_gfx(VMAppleMachineState *vms, MemoryRegion *mem)
454
+{
455
+ int irq_gfx = vms->irqmap[VMAPPLE_APV_GFX];
456
+ int irq_iosfc = vms->irqmap[VMAPPLE_APV_IOSFC];
457
+ SysBusDevice *gfx;
458
+
459
+ gfx = SYS_BUS_DEVICE(qdev_new("apple-gfx-mmio"));
460
+ sysbus_mmio_map(gfx, 0, vms->memmap[VMAPPLE_APV_GFX].base);
461
+ sysbus_mmio_map(gfx, 1, vms->memmap[VMAPPLE_APV_IOSFC].base);
462
+ sysbus_connect_irq(gfx, 0, qdev_get_gpio_in(vms->gic, irq_gfx));
463
+ sysbus_connect_irq(gfx, 1, qdev_get_gpio_in(vms->gic, irq_iosfc));
464
+ sysbus_realize_and_unref(gfx, &error_fatal);
465
+}
466
+
467
+static void create_aes(VMAppleMachineState *vms, MemoryRegion *mem)
468
+{
469
+ int irq = vms->irqmap[VMAPPLE_AES_1];
470
+ SysBusDevice *aes;
471
+
472
+ aes = SYS_BUS_DEVICE(qdev_new(TYPE_APPLE_AES));
473
+ sysbus_mmio_map(aes, 0, vms->memmap[VMAPPLE_AES_1].base);
474
+ sysbus_mmio_map(aes, 1, vms->memmap[VMAPPLE_AES_2].base);
475
+ sysbus_connect_irq(aes, 0, qdev_get_gpio_in(vms->gic, irq));
476
+ sysbus_realize_and_unref(aes, &error_fatal);
477
+}
478
+
479
+static inline int arm_gic_ppi_index(int cpu_nr, int ppi_index)
480
+{
481
+ return NUM_IRQS + cpu_nr * GIC_INTERNAL + ppi_index;
482
+}
483
+
484
+static void create_gic(VMAppleMachineState *vms, MemoryRegion *mem)
485
+{
486
+ MachineState *ms = MACHINE(vms);
487
+ /* We create a standalone GIC */
488
+ SysBusDevice *gicbusdev;
489
+ QList *redist_region_count;
490
+ int i;
491
+ unsigned int smp_cpus = ms->smp.cpus;
492
+
493
+ vms->gic = qdev_new(gicv3_class_name());
494
+ qdev_prop_set_uint32(vms->gic, "revision", 3);
495
+ qdev_prop_set_uint32(vms->gic, "num-cpu", smp_cpus);
496
+ /*
497
+ * Note that the num-irq property counts both internal and external
498
+ * interrupts; there are always 32 of the former (mandated by GIC spec).
499
+ */
500
+ qdev_prop_set_uint32(vms->gic, "num-irq", NUM_IRQS + 32);
501
+
502
+ uint32_t redist0_capacity =
503
+ vms->memmap[VMAPPLE_GIC_REDIST].size / GICV3_REDIST_SIZE;
504
+ uint32_t redist0_count = MIN(smp_cpus, redist0_capacity);
505
+
506
+ redist_region_count = qlist_new();
507
+ qlist_append_int(redist_region_count, redist0_count);
508
+ qdev_prop_set_array(vms->gic, "redist-region-count", redist_region_count);
509
+
510
+ gicbusdev = SYS_BUS_DEVICE(vms->gic);
511
+ sysbus_realize_and_unref(gicbusdev, &error_fatal);
512
+ sysbus_mmio_map(gicbusdev, 0, vms->memmap[VMAPPLE_GIC_DIST].base);
513
+ sysbus_mmio_map(gicbusdev, 1, vms->memmap[VMAPPLE_GIC_REDIST].base);
514
+
515
+ /*
516
+ * Wire the outputs from each CPU's generic timer and the GICv3
517
+ * maintenance interrupt signal to the appropriate GIC PPI inputs,
518
+ * and the GIC's IRQ/FIQ/VIRQ/VFIQ interrupt outputs to the CPU's inputs.
519
+ */
520
+ for (i = 0; i < smp_cpus; i++) {
521
+ DeviceState *cpudev = DEVICE(qemu_get_cpu(i));
522
+
523
+ /* Map the virt timer to PPI 27 */
524
+ qdev_connect_gpio_out(cpudev, GTIMER_VIRT,
525
+ qdev_get_gpio_in(vms->gic,
526
+ arm_gic_ppi_index(i, 27)));
527
+
528
+ /* Map the GIC IRQ and FIQ lines to CPU */
529
+ sysbus_connect_irq(gicbusdev, i, qdev_get_gpio_in(cpudev, ARM_CPU_IRQ));
530
+ sysbus_connect_irq(gicbusdev, i + smp_cpus,
531
+ qdev_get_gpio_in(cpudev, ARM_CPU_FIQ));
532
+ }
533
+}
534
+
535
+static void create_uart(const VMAppleMachineState *vms, int uart,
536
+ MemoryRegion *mem, Chardev *chr)
537
+{
538
+ hwaddr base = vms->memmap[uart].base;
539
+ int irq = vms->irqmap[uart];
540
+ DeviceState *dev = qdev_new(TYPE_PL011);
541
+ SysBusDevice *s = SYS_BUS_DEVICE(dev);
542
+
543
+ qdev_prop_set_chr(dev, "chardev", chr);
544
+ sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
545
+ memory_region_add_subregion(mem, base,
546
+ sysbus_mmio_get_region(s, 0));
547
+ sysbus_connect_irq(s, 0, qdev_get_gpio_in(vms->gic, irq));
548
+}
549
+
550
+static void create_rtc(const VMAppleMachineState *vms)
551
+{
552
+ hwaddr base = vms->memmap[VMAPPLE_RTC].base;
553
+ int irq = vms->irqmap[VMAPPLE_RTC];
554
+
555
+ sysbus_create_simple("pl031", base, qdev_get_gpio_in(vms->gic, irq));
556
+}
557
+
558
+static DeviceState *gpio_key_dev;
559
+static void vmapple_powerdown_req(Notifier *n, void *opaque)
560
+{
561
+ /* use gpio Pin 3 for power button event */
562
+ qemu_set_irq(qdev_get_gpio_in(gpio_key_dev, 0), 1);
563
+}
564
+
565
+static void create_gpio_devices(const VMAppleMachineState *vms, int gpio,
566
+ MemoryRegion *mem)
567
+{
568
+ DeviceState *pl061_dev;
569
+ hwaddr base = vms->memmap[gpio].base;
570
+ int irq = vms->irqmap[gpio];
571
+ SysBusDevice *s;
572
+
573
+ pl061_dev = qdev_new("pl061");
574
+ /* Pull lines down to 0 if not driven by the PL061 */
575
+ qdev_prop_set_uint32(pl061_dev, "pullups", 0);
576
+ qdev_prop_set_uint32(pl061_dev, "pulldowns", 0xff);
577
+ s = SYS_BUS_DEVICE(pl061_dev);
578
+ sysbus_realize_and_unref(s, &error_fatal);
579
+ memory_region_add_subregion(mem, base, sysbus_mmio_get_region(s, 0));
580
+ sysbus_connect_irq(s, 0, qdev_get_gpio_in(vms->gic, irq));
581
+ gpio_key_dev = sysbus_create_simple("gpio-key", -1,
582
+ qdev_get_gpio_in(pl061_dev, 3));
583
+}
584
+
585
+static void vmapple_firmware_init(VMAppleMachineState *vms,
586
+ MemoryRegion *sysmem)
587
+{
588
+ hwaddr size = vms->memmap[VMAPPLE_FIRMWARE].size;
589
+ hwaddr base = vms->memmap[VMAPPLE_FIRMWARE].base;
590
+ const char *bios_name;
591
+ int image_size;
592
+ char *fname;
593
+
594
+ bios_name = MACHINE(vms)->firmware;
595
+ if (!bios_name) {
596
+ error_report("No firmware specified");
597
+ exit(1);
598
+ }
599
+
600
+ fname = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
601
+ if (!fname) {
602
+ error_report("Could not find ROM image '%s'", bios_name);
603
+ exit(1);
604
+ }
605
+
606
+ memory_region_init_ram(&vms->fw_mr, NULL, "firmware", size, &error_fatal);
607
+ image_size = load_image_mr(fname, &vms->fw_mr);
608
+
609
+ g_free(fname);
610
+ if (image_size < 0) {
611
+ error_report("Could not load ROM image '%s'", bios_name);
612
+ exit(1);
613
+ }
614
+
615
+ memory_region_add_subregion(get_system_memory(), base, &vms->fw_mr);
616
+}
617
+
618
+static void create_pcie(VMAppleMachineState *vms)
619
+{
620
+ hwaddr base_mmio = vms->memmap[VMAPPLE_PCIE_MMIO].base;
621
+ hwaddr size_mmio = vms->memmap[VMAPPLE_PCIE_MMIO].size;
622
+ hwaddr base_ecam = vms->memmap[VMAPPLE_PCIE_ECAM].base;
623
+ hwaddr size_ecam = vms->memmap[VMAPPLE_PCIE_ECAM].size;
624
+ int irq = vms->irqmap[VMAPPLE_PCIE];
625
+ MemoryRegion *mmio_alias;
626
+ MemoryRegion *mmio_reg;
627
+ MemoryRegion *ecam_reg;
628
+ DeviceState *dev;
629
+ int i;
630
+ PCIHostState *pci;
631
+ DeviceState *usb_controller;
632
+ USBBus *usb_bus;
633
+
634
+ dev = qdev_new(TYPE_GPEX_HOST);
635
+ qdev_prop_set_uint32(dev, "num-irqs", GPEX_NUM_IRQS);
636
+ sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
637
+
638
+ /* Map only the first size_ecam bytes of ECAM space */
639
+ ecam_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0);
640
+ memory_region_init_alias(&vms->ecam_alias, OBJECT(dev), "pcie-ecam",
641
+ ecam_reg, 0, size_ecam);
642
+ memory_region_add_subregion(get_system_memory(), base_ecam,
643
+ &vms->ecam_alias);
644
+
645
+ /*
646
+ * Map the MMIO window from [0x50000000-0x7fff0000] in PCI space into
647
+ * system address space at [0x50000000-0x7fff0000].
648
+ */
649
+ mmio_alias = g_new0(MemoryRegion, 1);
650
+ mmio_reg = sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 1);
651
+ memory_region_init_alias(mmio_alias, OBJECT(dev), "pcie-mmio",
652
+ mmio_reg, base_mmio, size_mmio);
653
+ memory_region_add_subregion(get_system_memory(), base_mmio, mmio_alias);
654
+
655
+ for (i = 0; i < GPEX_NUM_IRQS; i++) {
656
+ sysbus_connect_irq(SYS_BUS_DEVICE(dev), i,
657
+ qdev_get_gpio_in(vms->gic, irq + i));
658
+ gpex_set_irq_num(GPEX_HOST(dev), i, irq + i);
659
+ }
660
+
661
+ pci = PCI_HOST_BRIDGE(dev);
662
+ vms->bus = pci->bus;
663
+ g_assert(vms->bus);
664
+
665
+ while ((dev = qemu_create_nic_device("virtio-net-pci", true, NULL))) {
666
+ qdev_realize_and_unref(dev, BUS(vms->bus), &error_fatal);
667
+ }
668
+
669
+ usb_controller = qdev_new(TYPE_QEMU_XHCI);
670
+ qdev_realize_and_unref(usb_controller, BUS(pci->bus), &error_fatal);
671
+
672
+ usb_bus = USB_BUS(object_resolve_type_unambiguous(TYPE_USB_BUS,
673
+ &error_fatal));
674
+ usb_create_simple(usb_bus, "usb-kbd");
675
+ usb_create_simple(usb_bus, "usb-tablet");
676
+}
677
+
678
+static void vmapple_reset(void *opaque)
679
+{
680
+ VMAppleMachineState *vms = opaque;
681
+ hwaddr base = vms->memmap[VMAPPLE_FIRMWARE].base;
682
+
683
+ cpu_set_pc(first_cpu, base);
684
+}
685
+
686
+static void mach_vmapple_init(MachineState *machine)
687
+{
688
+ VMAppleMachineState *vms = VMAPPLE_MACHINE(machine);
689
+ MachineClass *mc = MACHINE_GET_CLASS(machine);
690
+ const CPUArchIdList *possible_cpus;
691
+ MemoryRegion *sysmem = get_system_memory();
692
+ int n;
693
+ unsigned int smp_cpus = machine->smp.cpus;
694
+ unsigned int max_cpus = machine->smp.max_cpus;
695
+
696
+ vms->memmap = memmap;
697
+ machine->usb = true;
698
+
699
+ possible_cpus = mc->possible_cpu_arch_ids(machine);
700
+ assert(possible_cpus->len == max_cpus);
701
+ for (n = 0; n < possible_cpus->len; n++) {
702
+ Object *cpu;
703
+ CPUState *cs;
704
+
705
+ if (n >= smp_cpus) {
706
+ break;
707
+ }
708
+
709
+ cpu = object_new(possible_cpus->cpus[n].type);
710
+ object_property_set_int(cpu, "mp-affinity",
711
+ possible_cpus->cpus[n].arch_id, &error_fatal);
712
+
713
+ cs = CPU(cpu);
714
+ cs->cpu_index = n;
715
+
716
+ numa_cpu_pre_plug(&possible_cpus->cpus[cs->cpu_index], DEVICE(cpu),
717
+ &error_fatal);
718
+
719
+ if (object_property_find(cpu, "has_el3")) {
720
+ object_property_set_bool(cpu, "has_el3", false, &error_fatal);
721
+ }
722
+ if (object_property_find(cpu, "has_el2")) {
723
+ object_property_set_bool(cpu, "has_el2", false, &error_fatal);
724
+ }
725
+ object_property_set_int(cpu, "psci-conduit", QEMU_PSCI_CONDUIT_HVC,
726
+ NULL);
727
+
728
+ /* Secondary CPUs start in PSCI powered-down state */
729
+ if (n > 0) {
730
+ object_property_set_bool(cpu, "start-powered-off", true,
731
+ &error_fatal);
732
+ }
733
+
734
+ object_property_set_link(cpu, "memory", OBJECT(sysmem), &error_abort);
735
+ qdev_realize(DEVICE(cpu), NULL, &error_fatal);
736
+ object_unref(cpu);
737
+ }
738
+
739
+ memory_region_add_subregion(sysmem, vms->memmap[VMAPPLE_MEM].base,
740
+ machine->ram);
741
+
742
+ create_gic(vms, sysmem);
743
+ create_bdif(vms, sysmem);
744
+ create_pvpanic(vms, sysmem);
745
+ create_aes(vms, sysmem);
746
+ create_gfx(vms, sysmem);
747
+ create_uart(vms, VMAPPLE_UART, sysmem, serial_hd(0));
748
+ create_rtc(vms);
749
+ create_pcie(vms);
750
+
751
+ create_gpio_devices(vms, VMAPPLE_GPIO, sysmem);
752
+
753
+ vmapple_firmware_init(vms, sysmem);
754
+ create_cfg(vms, sysmem);
755
+
756
+ /* connect powerdown request */
757
+ vms->powerdown_notifier.notify = vmapple_powerdown_req;
758
+ qemu_register_powerdown_notifier(&vms->powerdown_notifier);
759
+
760
+ vms->bootinfo.ram_size = machine->ram_size;
761
+ vms->bootinfo.board_id = -1;
762
+ vms->bootinfo.loader_start = vms->memmap[VMAPPLE_MEM].base;
763
+ vms->bootinfo.skip_dtb_autoload = true;
764
+ vms->bootinfo.firmware_loaded = true;
765
+ arm_load_kernel(ARM_CPU(first_cpu), machine, &vms->bootinfo);
766
+
767
+ qemu_register_reset(vmapple_reset, vms);
768
+}
769
+
770
+static CpuInstanceProperties
771
+vmapple_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
772
+{
773
+ MachineClass *mc = MACHINE_GET_CLASS(ms);
774
+ const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
775
+
776
+ assert(cpu_index < possible_cpus->len);
777
+ return possible_cpus->cpus[cpu_index].props;
778
+}
779
+
780
+
781
+static int64_t vmapple_get_default_cpu_node_id(const MachineState *ms, int idx)
782
+{
783
+ return idx % ms->numa_state->num_nodes;
784
+}
785
+
786
+static const CPUArchIdList *vmapple_possible_cpu_arch_ids(MachineState *ms)
787
+{
788
+ int n;
789
+ unsigned int max_cpus = ms->smp.max_cpus;
790
+
791
+ if (ms->possible_cpus) {
792
+ assert(ms->possible_cpus->len == max_cpus);
793
+ return ms->possible_cpus;
794
+ }
795
+
796
+ ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
797
+ sizeof(CPUArchId) * max_cpus);
798
+ ms->possible_cpus->len = max_cpus;
799
+ for (n = 0; n < ms->possible_cpus->len; n++) {
800
+ ms->possible_cpus->cpus[n].type = ms->cpu_type;
801
+ ms->possible_cpus->cpus[n].arch_id =
802
+ arm_build_mp_affinity(n, GICV3_TARGETLIST_BITS);
803
+ ms->possible_cpus->cpus[n].props.has_thread_id = true;
804
+ ms->possible_cpus->cpus[n].props.thread_id = n;
805
+ }
806
+ return ms->possible_cpus;
807
+}
808
+
809
+static void vmapple_get_uuid(Object *obj, Visitor *v, const char *name,
810
+ void *opaque, Error **errp)
811
+{
812
+ VMAppleMachineState *vms = VMAPPLE_MACHINE(obj);
813
+
814
+ visit_type_uint64(v, name, &vms->uuid, errp);
815
+}
816
+
817
+static void vmapple_set_uuid(Object *obj, Visitor *v, const char *name,
818
+ void *opaque, Error **errp)
819
+{
820
+ VMAppleMachineState *vms = VMAPPLE_MACHINE(obj);
821
+ Error *error = NULL;
822
+
823
+ visit_type_uint64(v, name, &vms->uuid, &error);
824
+ if (error) {
825
+ error_propagate(errp, error);
826
+ return;
827
+ }
828
+}
829
+
830
+static void vmapple_machine_class_init(ObjectClass *oc, void *data)
831
+{
832
+ MachineClass *mc = MACHINE_CLASS(oc);
833
+
834
+ mc->init = mach_vmapple_init;
835
+ mc->max_cpus = 32;
836
+ mc->block_default_type = IF_VIRTIO;
837
+ mc->no_cdrom = 1;
838
+ mc->pci_allow_0_address = true;
839
+ mc->minimum_page_bits = 12;
840
+ mc->possible_cpu_arch_ids = vmapple_possible_cpu_arch_ids;
841
+ mc->cpu_index_to_instance_props = vmapple_cpu_index_to_props;
842
+ mc->default_cpu_type = ARM_CPU_TYPE_NAME("host");
843
+ mc->get_default_cpu_node_id = vmapple_get_default_cpu_node_id;
844
+ mc->default_ram_id = "mach-vmapple.ram";
845
+
846
+ object_register_sugar_prop(TYPE_VIRTIO_PCI, "disable-legacy",
847
+ "on", true);
848
+
849
+ object_class_property_add(oc, "uuid", "uint64", vmapple_get_uuid,
850
+ vmapple_set_uuid, NULL, NULL);
851
+ object_class_property_set_description(oc, "uuid", "Machine UUID (SDOM)");
852
+}
853
+
854
+static void vmapple_instance_init(Object *obj)
855
+{
856
+ VMAppleMachineState *vms = VMAPPLE_MACHINE(obj);
857
+
858
+ vms->irqmap = irqmap;
859
+}
860
+
861
+static const TypeInfo vmapple_machine_info = {
862
+ .name = TYPE_VMAPPLE_MACHINE,
863
+ .parent = TYPE_MACHINE,
864
+ .abstract = true,
865
+ .instance_size = sizeof(VMAppleMachineState),
866
+ .class_size = sizeof(VMAppleMachineClass),
867
+ .class_init = vmapple_machine_class_init,
868
+ .instance_init = vmapple_instance_init,
869
+};
870
+
871
+static void machvmapple_machine_init(void)
872
+{
873
+ type_register_static(&vmapple_machine_info);
874
+}
875
+type_init(machvmapple_machine_init);
876
+
877
+static void vmapple_machine_9_2_options(MachineClass *mc)
878
+{
879
+}
880
+DEFINE_VMAPPLE_MACHINE_AS_LATEST(9, 2)
881
+
882
--
883
2.39.3 (Apple Git-145)
884
885
diff view generated by jsdifflib