-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathvirtio-gpu-sw.c
More file actions
1340 lines (1208 loc) · 47.6 KB
/
virtio-gpu-sw.c
File metadata and controls
1340 lines (1208 loc) · 47.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include "device.h"
#include "utils.h"
#include "vgpu-display.h"
#include "virtio-gpu.h"
#include "virtio.h"
#define PRIV(x) ((virtio_gpu_data_t *) x->priv)
/* Host-side images are allocated per resource with 'calloc()'. Track their
* aggregate size and cap it at 256 MiB.
*
* Backing entries describe guest RAM ranges. Use 4 KiB as the expected minimum
* page granularity, so 512 MiB guest RAM needs at most 'RAM_SIZE / 4096'
* entries plus one extra entry for an unaligned tail.
*/
#define VGPU_SW_MAX_HOSTMEM (256U * 1024U * 1024U)
#define VGPU_SW_BACKING_ENTRY_PAGE_SIZE 4096U
#define VGPU_SW_MAX_BACKING_ENTRIES \
(RAM_SIZE / VGPU_SW_BACKING_ENTRY_PAGE_SIZE + 1U)
/* Host-side 2D resource owned by the software backend. It keeps the copied
* 'image' plus any attached guest backing metadata needed by transfers.
*/
struct vgpu_sw_resource_2d {
uint32_t resource_id;
uint32_t format;
uint32_t width, height;
uint32_t stride;
uint32_t bits_per_pixel;
uint32_t *image;
size_t image_size;
size_t page_cnt;
struct iovec *iovec;
struct list_head list;
};
/* Process-wide singleton: semu currently assumes at most one software
* virtio-gpu backend instance per process.
*/
static LIST_HEAD(g_vgpu_sw_res_2d_list);
static size_t g_vgpu_sw_hostmem;
static size_t vgpu_sw_iov_to_buf(const struct iovec *iov,
unsigned int iov_cnt,
size_t offset,
void *buf,
size_t bytes)
{
size_t done = 0;
if (bytes == 0)
return 0;
/* Each non-empty 'iovec' entry is validated by 'RESOURCE_ATTACH_BACKING'
* before it is stored here. Treat the array as one long byte stream: skip
* whole entries until reaching the starting offset, then copy chunks into
* 'buf'.
*/
for (unsigned int i = 0; i < iov_cnt; i++) {
if (iov[i].iov_len == 0)
continue;
/* Treat a malformed backing entry as an incomplete copy. */
if (!iov[i].iov_base)
return done;
if (offset < iov[i].iov_len) {
size_t remained = bytes - done;
size_t page_avail = iov[i].iov_len - offset;
size_t len = (remained < page_avail) ? remained : page_avail;
void *src = (void *) ((uintptr_t) iov[i].iov_base + offset);
void *dest = (void *) ((uintptr_t) buf + done);
memcpy(dest, src, len);
offset = 0;
done += len;
if (done >= bytes)
break;
} else {
offset -= iov[i].iov_len;
}
}
return done;
}
static bool vgpu_sw_u64_add_overflow(uint64_t a, uint64_t b, uint64_t *out)
{
*out = a + b;
return *out < a;
}
static bool vgpu_sw_u64_mul_overflow(uint64_t a, uint64_t b, uint64_t *out)
{
if (a != 0 && b > UINT64_MAX / a)
return true;
*out = a * b;
return false;
}
static bool vgpu_sw_rect_fits(uint32_t width,
uint32_t height,
const struct virtio_gpu_rect *rect)
{
if (rect->width == 0 || rect->height == 0)
return false;
if (rect->x >= width || rect->y >= height)
return false;
return rect->width <= width - rect->x && rect->height <= height - rect->y;
}
static bool vgpu_sw_transfer_source_fits(
const struct virtio_gpu_trans_to_host_2d *req,
const struct vgpu_sw_resource_2d *res_2d)
{
uint64_t bpp = res_2d->bits_per_pixel / 8;
uint64_t row_bytes, row_stride, last_row, last_row_offset, end_offset;
uint64_t required_bytes, backing_size = 0, backing_end;
if (req->r.height == 0 || req->offset > SIZE_MAX)
return false;
if (vgpu_sw_u64_mul_overflow(req->r.width, bpp, &row_bytes) ||
row_bytes == 0)
return false;
if (vgpu_sw_u64_mul_overflow((uint64_t) res_2d->stride, req->r.height - 1,
&last_row))
return false;
if (vgpu_sw_u64_add_overflow(req->offset, last_row, &last_row_offset))
return false;
if (vgpu_sw_u64_add_overflow(last_row_offset, row_bytes, &end_offset))
return false;
if (vgpu_sw_u64_mul_overflow((uint64_t) res_2d->stride, req->r.height,
&row_stride))
return false;
required_bytes =
row_bytes == res_2d->stride ? row_stride : end_offset - req->offset;
for (size_t i = 0; i < res_2d->page_cnt; i++) {
if (vgpu_sw_u64_add_overflow(backing_size, res_2d->iovec[i].iov_len,
&backing_size))
return false;
}
return !vgpu_sw_u64_add_overflow(req->offset, required_bytes,
&backing_end) &&
backing_end <= backing_size;
}
static bool vgpu_sw_copy_image_from_pages(
struct virtio_gpu_trans_to_host_2d *req,
struct vgpu_sw_resource_2d *res_2d)
{
uint32_t stride = res_2d->stride;
uint32_t bpp = res_2d->bits_per_pixel / 8; /* Bytes per pixel */
uint32_t width = req->r.width;
uint32_t height = req->r.height;
/* When the transfer spans full-width rows with no padding, both source
* ('iovec' at 'req->offset') and destination ('image' at 'r.y') are
* contiguous, so the entire rectangle can be copied in a single helper
* call. This covers all cursor transfers, full-frame updates, and
* full-width dirty bands.
*/
if (req->r.x == 0 && (size_t) width * bpp == stride) {
void *dest =
(void *) ((uintptr_t) res_2d->image + (size_t) req->r.y * stride);
size_t bytes = (size_t) stride * height;
return vgpu_sw_iov_to_buf(res_2d->iovec, res_2d->page_cnt,
(size_t) req->offset, dest, bytes) == bytes;
}
/* Partial-width sub-rect: copy row by row */
for (uint32_t h = 0; h < height; h++) {
/* Source offset is in the image coordinate. The address to copy from
* is the page base address plus the offset.
*/
size_t src_offset = req->offset + (size_t) stride * h;
size_t dest_offset =
((size_t) req->r.y + h) * stride + (size_t) req->r.x * bpp;
void *dest = (void *) ((uintptr_t) res_2d->image + dest_offset);
size_t total = (size_t) width * bpp;
if (vgpu_sw_iov_to_buf(res_2d->iovec, res_2d->page_cnt, src_offset,
dest, total) != total)
return false;
}
return true;
}
static void vgpu_sw_destroy_resource_2d(struct vgpu_sw_resource_2d *res_2d)
{
list_del(&res_2d->list);
g_vgpu_sw_hostmem -= res_2d->image_size;
free(res_2d->image);
free(res_2d->iovec);
free(res_2d);
}
static struct vgpu_sw_resource_2d *vgpu_sw_get_resource_2d(uint32_t resource_id)
{
struct vgpu_sw_resource_2d *res_2d;
list_for_each_entry (res_2d, &g_vgpu_sw_res_2d_list, list) {
if (res_2d->resource_id == resource_id)
return res_2d;
}
return NULL;
}
static struct virtio_gpu_scanout_info *vgpu_sw_get_scanout(
virtio_gpu_state_t *vgpu,
uint32_t scanout_id)
{
if (scanout_id >= PRIV(vgpu)->num_scanouts)
return NULL;
struct virtio_gpu_scanout_info *scanout = &PRIV(vgpu)->scanouts[scanout_id];
return scanout->enabled ? scanout : NULL;
}
static struct vgpu_display_payload *vgpu_sw_create_window_payload(
const struct vgpu_sw_resource_2d *res_2d,
const struct virtio_gpu_scanout_info *scanout,
const char *plane_name)
{
if (!res_2d || !res_2d->image) {
fprintf(stderr, VIRTIO_GPU_LOG_PREFIX "%s(): missing %s image\n",
__func__, plane_name);
return NULL;
}
if (res_2d->bits_per_pixel == 0 || (res_2d->bits_per_pixel % 8) != 0) {
fprintf(stderr, VIRTIO_GPU_LOG_PREFIX "%s(): invalid %s bpp %u\n",
__func__, plane_name, res_2d->bits_per_pixel);
return NULL;
}
size_t bytes_per_pixel = res_2d->bits_per_pixel / 8;
uint32_t src_x = 0;
uint32_t src_y = 0;
uint32_t width = res_2d->width;
uint32_t height = res_2d->height;
if (scanout) {
/* Primary scanouts can expose only a sub-rectangle of the resource.
* Record that view before snapshotting it.
*/
src_x = scanout->src_x;
src_y = scanout->src_y;
width = scanout->src_w;
height = scanout->src_h;
}
if (width == 0 || height == 0) {
fprintf(stderr, VIRTIO_GPU_LOG_PREFIX "%s(): invalid %s size %ux%u\n",
__func__, plane_name, width, height);
return NULL;
}
size_t row_bytes = (size_t) width * bytes_per_pixel;
if (row_bytes / width != bytes_per_pixel) {
fprintf(stderr, VIRTIO_GPU_LOG_PREFIX "%s(): %s row size overflow\n",
__func__, plane_name);
return NULL;
}
if (row_bytes > UINT32_MAX) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX "%s(): %s row size exceeds uint32_t\n",
__func__, plane_name);
return NULL;
}
if (res_2d->stride < row_bytes) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX
"%s(): invalid %s stride %u for row size %zu\n",
__func__, plane_name, res_2d->stride, row_bytes);
return NULL;
}
size_t pixels_size = row_bytes * height;
if (pixels_size / height != row_bytes) {
fprintf(stderr, VIRTIO_GPU_LOG_PREFIX "%s(): %s image size overflow\n",
__func__, plane_name);
return NULL;
}
size_t alloc_size = sizeof(struct vgpu_display_payload) + pixels_size;
if (alloc_size < pixels_size) {
fprintf(stderr, VIRTIO_GPU_LOG_PREFIX "%s(): %s allocation overflow\n",
__func__, plane_name);
return NULL;
}
struct vgpu_display_payload *payload = malloc(alloc_size);
if (!payload) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX "%s(): failed to allocate %s snapshot\n",
__func__, plane_name);
return NULL;
}
payload->cpu.format = res_2d->format;
payload->cpu.width = width;
payload->cpu.height = height;
payload->cpu.stride = (uint32_t) row_bytes;
payload->cpu.bits_per_pixel = res_2d->bits_per_pixel;
payload->cpu.pixels = (uint8_t *) (payload + 1);
/* The cropped view is contiguous only when the source stride matches this
* snapshot's row size. Otherwise each source row still carries padding or
* untouched pixels outside the requested view, so the snapshot must be
* packed row by row.
*/
const uint8_t *src_pixels = (const uint8_t *) res_2d->image +
(size_t) src_y * res_2d->stride +
(size_t) src_x * bytes_per_pixel;
if (res_2d->stride == row_bytes) {
memcpy(payload->cpu.pixels, src_pixels, pixels_size);
} else {
for (uint32_t y = 0; y < height; y++) {
memcpy(payload->cpu.pixels + (size_t) y * row_bytes,
src_pixels + (size_t) y * res_2d->stride, row_bytes);
}
}
return payload;
}
/* Backend Implementation */
static void vgpu_sw_reset(virtio_gpu_state_t *vgpu)
{
/* The display queue may still hold older 'PRIMARY_SET' / 'CURSOR_SET'
* frames published before this reset. Publishing 'CLEAR' advances the
* per-plane generation; 'vgpu_display_pop_cmd()' consumes those clears
* first, then drops older queued frame commands as stale.
*
* Queued frame payloads are deep copies, so destroying resources after the
* clear publication cannot dangle any display payload still in the bridge.
* The display queue is SPSC and consumer-owned, so reset does not drain it
* from the producer side. The bounded queue releases stale payloads when
* the SDL consumer pops them.
*/
for (uint32_t i = 0; i < PRIV(vgpu)->num_scanouts; i++) {
PRIV(vgpu)->scanouts[i].primary_resource_id = 0;
PRIV(vgpu)->scanouts[i].cursor_resource_id = 0;
PRIV(vgpu)->scanouts[i].src_x = 0;
PRIV(vgpu)->scanouts[i].src_y = 0;
PRIV(vgpu)->scanouts[i].src_w = 0;
PRIV(vgpu)->scanouts[i].src_h = 0;
vgpu_display_publish_primary_clear(i);
vgpu_display_publish_cursor_clear(i);
}
struct list_head *curr, *next;
list_for_each_safe (curr, next, &g_vgpu_sw_res_2d_list) {
struct vgpu_sw_resource_2d *res_2d =
list_entry(curr, struct vgpu_sw_resource_2d, list);
vgpu_sw_destroy_resource_2d(res_2d);
}
}
static void vgpu_sw_resource_create_2d_handler(virtio_gpu_state_t *vgpu,
struct virtq_desc *vq_desc,
uint32_t *plen)
{
const struct virtq_desc *response_desc = virtio_gpu_get_response_desc(
vq_desc, sizeof(struct virtio_gpu_ctrl_hdr));
if (!response_desc) {
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
struct virtio_gpu_res_create_2d *request = virtio_gpu_get_request(
vgpu, vq_desc, sizeof(struct virtio_gpu_res_create_2d));
if (!request) {
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
/* Keep 'resource_id' 0 unavailable for real resources. The virtio spec
* explicitly documents 'resource_id = 0' as the 'SET_SCANOUT' disable
* sentinel.
* The Linux virtio-gpu driver also allocates guest-generated resource IDs
* as 'handle + 1', so they are always greater than 0. See
* 'virtgpu_object.c' for details.
*/
if (request->resource_id == 0) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX "%s(): resource id should not be 0\n",
__func__);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
if (request->width == 0 || request->height == 0) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX "%s(): invalid resource size %ux%u\n",
__func__, request->width, request->height);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
/* Reject re-use of an already-live resource id. Without this check the
* guest could orphan the previous resource (its 'image' and 'iovec' would
* leak because 'vgpu_sw_get_resource_2d()' returns the first match) and
* confuse later 'TRANSFER' / 'FLUSH' / 'UNREF' requests that target the
* same id. Spec explicitly allows the device to fail this.
*/
if (vgpu_sw_get_resource_2d(request->resource_id)) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX "%s(): resource id %u already in use\n",
__func__, request->resource_id);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
/* Create 2D resource */
struct vgpu_sw_resource_2d *res_2d = calloc(1, sizeof(*res_2d));
if (!res_2d) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX "%s(): failed to allocate new resource\n",
__func__);
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
res_2d->resource_id = request->resource_id;
/* The software backend currently supports only 32bpp packed formats. */
uint32_t bits_per_pixel;
switch (request->format) {
case VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM:
case VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM:
case VIRTIO_GPU_FORMAT_A8R8G8B8_UNORM:
case VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM:
case VIRTIO_GPU_FORMAT_R8G8B8A8_UNORM:
case VIRTIO_GPU_FORMAT_X8B8G8R8_UNORM:
case VIRTIO_GPU_FORMAT_A8B8G8R8_UNORM:
case VIRTIO_GPU_FORMAT_R8G8B8X8_UNORM:
bits_per_pixel = 32;
break;
default:
fprintf(stderr, VIRTIO_GPU_LOG_PREFIX "%s(): unsupported format %u\n",
__func__, request->format);
free(res_2d);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
/* Set 2D resource */
res_2d->width = request->width;
res_2d->height = request->height;
res_2d->format = request->format;
res_2d->bits_per_pixel = bits_per_pixel;
/* Compute the row stride in a wider type first, then narrow it only after
* checking the final byte count still fits in 'uint32_t'. Otherwise a large
* guest width could wrap during the intermediate multiplication and leave
* a truncated stride in the resource.
*/
size_t stride =
(((size_t) res_2d->width * res_2d->bits_per_pixel + 0x1f) >> 5) *
sizeof(uint32_t);
if (stride > UINT32_MAX) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX "%s(): stride overflow (%u x %u bpp)\n",
__func__, res_2d->width, res_2d->bits_per_pixel);
free(res_2d);
*plen =
virtio_gpu_write_ctrl_response(vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
res_2d->stride = (uint32_t) stride;
/* Guard against integer overflow in image buffer allocation.
* Both 'stride' and 'height' are guest-controlled 'uint32_t' values whose
* product can silently wrap around in 32-bit arithmetic, resulting in
* an undersized 'malloc()' while later transfers write to the full extent.
*/
size_t image_size = (size_t) res_2d->stride * res_2d->height;
if (res_2d->height && image_size / res_2d->height != res_2d->stride) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX "%s(): image size overflow (%u x %u)\n",
__func__, res_2d->width, res_2d->height);
free(res_2d);
*plen =
virtio_gpu_write_ctrl_response(vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
if (image_size > VGPU_SW_MAX_HOSTMEM ||
g_vgpu_sw_hostmem > VGPU_SW_MAX_HOSTMEM - image_size) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX
"%s(): image memory limit exceeded (%zu bytes)\n",
__func__, image_size);
free(res_2d);
*plen =
virtio_gpu_write_ctrl_response(vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
res_2d->image = calloc(1, image_size);
/* Failed to create image buffer */
if (!res_2d->image) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX
"%s(): failed to allocate image buffer (%zu bytes)\n",
__func__, image_size);
free(res_2d);
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
res_2d->image_size = image_size;
g_vgpu_sw_hostmem += image_size;
list_push(&res_2d->list, &g_vgpu_sw_res_2d_list);
*plen = virtio_gpu_write_ctrl_response(vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_OK_NODATA);
if (!*plen)
virtio_gpu_set_fail(vgpu);
}
static void vgpu_sw_cmd_resource_unref_handler(virtio_gpu_state_t *vgpu,
struct virtq_desc *vq_desc,
uint32_t *plen)
{
const struct virtq_desc *response_desc = virtio_gpu_get_response_desc(
vq_desc, sizeof(struct virtio_gpu_ctrl_hdr));
if (!response_desc) {
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
struct virtio_gpu_res_unref *request = virtio_gpu_get_request(
vgpu, vq_desc, sizeof(struct virtio_gpu_res_unref));
if (!request) {
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
struct vgpu_sw_resource_2d *res_2d =
vgpu_sw_get_resource_2d(request->resource_id);
if (!res_2d) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX
"%s(): resource unref references invalid resource id %u\n",
__func__, request->resource_id);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
/* Clear any visible plane using this resource before it is freed. */
for (uint32_t i = 0; i < PRIV(vgpu)->num_scanouts; i++) {
struct virtio_gpu_scanout_info *scanout = &PRIV(vgpu)->scanouts[i];
if (!scanout->enabled)
continue;
if (scanout->primary_resource_id == request->resource_id) {
scanout->primary_resource_id = 0;
scanout->src_x = scanout->src_y = 0;
scanout->src_w = scanout->src_h = 0;
vgpu_display_publish_primary_clear(i);
}
if (scanout->cursor_resource_id == request->resource_id) {
scanout->cursor_resource_id = 0;
vgpu_display_publish_cursor_clear(i);
}
}
vgpu_sw_destroy_resource_2d(res_2d);
*plen = virtio_gpu_write_ctrl_response(vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_OK_NODATA);
if (!*plen)
virtio_gpu_set_fail(vgpu);
}
static void vgpu_sw_cmd_set_scanout_handler(virtio_gpu_state_t *vgpu,
struct virtq_desc *vq_desc,
uint32_t *plen)
{
const struct virtq_desc *response_desc = virtio_gpu_get_response_desc(
vq_desc, sizeof(struct virtio_gpu_ctrl_hdr));
if (!response_desc) {
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
struct virtio_gpu_set_scanout *request = virtio_gpu_get_request(
vgpu, vq_desc, sizeof(struct virtio_gpu_set_scanout));
if (!request) {
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
struct virtio_gpu_scanout_info *scanout =
vgpu_sw_get_scanout(vgpu, request->scanout_id);
if (!scanout) {
fprintf(stderr, VIRTIO_GPU_LOG_PREFIX "%s(): invalid scanout id %u\n",
__func__, request->scanout_id);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
/* Keep 'resource_id' 0 unavailable for real resources. The virtio spec
* explicitly documents 'resource_id = 0' as the 'SET_SCANOUT' disable
* sentinel.
* The Linux virtio-gpu driver also allocates guest-generated resource IDs
* as 'handle + 1', so they are always greater than 0. See
* 'virtgpu_object.c' for details.
*/
if (request->resource_id == 0) {
scanout->primary_resource_id = 0;
scanout->src_x = scanout->src_y = 0;
scanout->src_w = scanout->src_h = 0;
vgpu_display_publish_primary_clear(request->scanout_id);
goto leave;
}
/* Retrieve 2D resource */
struct vgpu_sw_resource_2d *res_2d =
vgpu_sw_get_resource_2d(request->resource_id);
if (!res_2d) {
fprintf(stderr, VIRTIO_GPU_LOG_PREFIX "%s(): invalid resource id %u\n",
__func__, request->resource_id);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
/* Validate that the source rectangle fits within the resource without
* relying on wrapping 32-bit additions.
*/
if (!vgpu_sw_rect_fits(res_2d->width, res_2d->height, &request->r)) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX
"%s(): source rect %u,%u %ux%u exceeds resource %ux%u\n",
__func__, request->r.x, request->r.y, request->r.width,
request->r.height, res_2d->width, res_2d->height);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
/* The source rectangle is displayed into this scanout, view size is bounded
* by the advertised scanout size.
*/
if (request->r.width > scanout->width ||
request->r.height > scanout->height) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX
"%s(): source rect %ux%u exceeds scanout %ux%u\n",
__func__, request->r.width, request->r.height, scanout->width,
scanout->height);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
/* Bind scanout with resource and record the source rectangle */
scanout->primary_resource_id = res_2d->resource_id;
scanout->src_x = request->r.x;
scanout->src_y = request->r.y;
scanout->src_w = request->r.width;
scanout->src_h = request->r.height;
leave:
*plen = virtio_gpu_write_ctrl_response(vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_OK_NODATA);
if (!*plen)
virtio_gpu_set_fail(vgpu);
}
static void vgpu_sw_cmd_resource_flush_handler(virtio_gpu_state_t *vgpu,
struct virtq_desc *vq_desc,
uint32_t *plen)
{
const struct virtq_desc *response_desc = virtio_gpu_get_response_desc(
vq_desc, sizeof(struct virtio_gpu_ctrl_hdr));
if (!response_desc) {
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
struct virtio_gpu_res_flush *request = virtio_gpu_get_request(
vgpu, vq_desc, sizeof(struct virtio_gpu_res_flush));
if (!request) {
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
/* Retrieve 2D resource */
struct vgpu_sw_resource_2d *res_2d =
vgpu_sw_get_resource_2d(request->resource_id);
if (!res_2d) {
fprintf(stderr, VIRTIO_GPU_LOG_PREFIX "%s(): invalid resource id %u\n",
__func__, request->resource_id);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
if (!vgpu_sw_rect_fits(res_2d->width, res_2d->height, &request->r)) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX
"%s(): invalid flush rect %u,%u %ux%u for resource %u size "
"%ux%u\n",
__func__, request->r.x, request->r.y, request->r.width,
request->r.height, request->resource_id, res_2d->width,
res_2d->height);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
/* Flush the resource to every scanout currently bound to it, using the
* source rectangle recorded by 'SET_SCANOUT' to display only the requested
* sub-region of the resource.
*/
for (uint32_t i = 0; i < PRIV(vgpu)->num_scanouts; i++) {
struct virtio_gpu_scanout_info *scanout = &PRIV(vgpu)->scanouts[i];
if (!scanout->enabled ||
scanout->primary_resource_id != request->resource_id)
continue;
/* Keep the producer non-blocking: if the display queue is full or
* snapshot allocation fails below, this flush frame for scanout 'i' is
* dropped and the frontend keeps showing its previous published frame.
*/
if (!vgpu_display_can_publish())
continue;
struct vgpu_display_payload *payload =
vgpu_sw_create_window_payload(res_2d, scanout, "primary");
if (!payload)
continue;
/* The publish path snapshots the whole 'SET_SCANOUT' view for this
* scanout. 'request->r' is not used here to further trim the payload
* for now.
*/
vgpu_display_publish_primary_set(i, payload);
}
*plen = virtio_gpu_write_ctrl_response(vgpu, &request->hdr, response_desc,
VIRTIO_GPU_RESP_OK_NODATA);
if (!*plen)
virtio_gpu_set_fail(vgpu);
}
static void vgpu_sw_cmd_transfer_to_host_2d_handler(virtio_gpu_state_t *vgpu,
struct virtq_desc *vq_desc,
uint32_t *plen)
{
const struct virtq_desc *response_desc = virtio_gpu_get_response_desc(
vq_desc, sizeof(struct virtio_gpu_ctrl_hdr));
if (!response_desc) {
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
struct virtio_gpu_trans_to_host_2d *req = virtio_gpu_get_request(
vgpu, vq_desc, sizeof(struct virtio_gpu_trans_to_host_2d));
if (!req) {
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
/* Retrieve 2D resource */
struct vgpu_sw_resource_2d *res_2d =
vgpu_sw_get_resource_2d(req->resource_id);
if (!res_2d) {
fprintf(stderr, VIRTIO_GPU_LOG_PREFIX "%s(): invalid resource id %u\n",
__func__, req->resource_id);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &req->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
/* Check if backing has been attached */
if (!res_2d->iovec) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX
"%s(): backing not attached for resource %u\n",
__func__, req->resource_id);
*plen = virtio_gpu_write_ctrl_response(vgpu, &req->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_UNSPEC);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
/* Validate that the destination rectangle fits within the resource
* without relying on wrapping 32-bit additions. Mirrors the check in
* 'vgpu_sw_cmd_set_scanout_handler()'.
*/
if (!vgpu_sw_rect_fits(res_2d->width, res_2d->height, &req->r)) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX
"%s(): invalid transfer rect %u,%u %ux%u for resource %u size "
"%ux%u\n",
__func__, req->r.x, req->r.y, req->r.width, req->r.height,
req->resource_id, res_2d->width, res_2d->height);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &req->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
if (!vgpu_sw_transfer_source_fits(req, res_2d)) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX "%s(): transfer source exceeds backing\n",
__func__);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &req->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
/* Transfer frame data from guest to host */
if (!vgpu_sw_copy_image_from_pages(req, res_2d)) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX
"%s(): incomplete transfer from backing\n",
__func__);
*plen = virtio_gpu_write_ctrl_response(vgpu, &req->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_UNSPEC);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
*plen = virtio_gpu_write_ctrl_response(vgpu, &req->hdr, response_desc,
VIRTIO_GPU_RESP_OK_NODATA);
if (!*plen)
virtio_gpu_set_fail(vgpu);
}
static void vgpu_sw_cmd_resource_attach_backing_handler(
virtio_gpu_state_t *vgpu,
struct virtq_desc *vq_desc,
uint32_t *plen)
{
const struct virtq_desc *response_desc = virtio_gpu_get_response_desc(
vq_desc, sizeof(struct virtio_gpu_ctrl_hdr));
if (!response_desc) {
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
struct virtio_gpu_res_attach_backing *backing_info = virtio_gpu_get_request(
vgpu, vq_desc, sizeof(struct virtio_gpu_res_attach_backing));
if (!backing_info) {
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
if (vq_desc[1].flags & VIRTIO_DESC_F_WRITE) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX
"%s(): backing entries descriptor is writable\n",
__func__);
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
if (backing_info->nr_entries == 0 ||
backing_info->nr_entries > VGPU_SW_MAX_BACKING_ENTRIES) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX "%s(): invalid backing entry count %u\n",
__func__, backing_info->nr_entries);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &backing_info->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
/* The entry cap above keeps 'entries_size' small. semu currently targets
* 64-bit hosts, so this path does not guard for 32-bit host overflow yet.
*/
size_t entries_size =
sizeof(struct virtio_gpu_mem_entry) * backing_info->nr_entries;
if (vq_desc[1].len < entries_size) {
fprintf(stderr,
VIRTIO_GPU_LOG_PREFIX
"%s(): backing entries descriptor too small\n",
__func__);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &backing_info->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER);
if (!*plen)
virtio_gpu_set_fail(vgpu);
return;
}
struct virtio_gpu_mem_entry *pages = virtio_gpu_mem_guest_to_host(
vgpu, vq_desc[1].addr, (uint32_t) entries_size);
if (!pages) {
virtio_gpu_set_fail(vgpu);
*plen = 0;
return;
}
/* Retrieve 2D resource */
struct vgpu_sw_resource_2d *res_2d =
vgpu_sw_get_resource_2d(backing_info->resource_id);
if (!res_2d) {
fprintf(stderr, VIRTIO_GPU_LOG_PREFIX "%s(): invalid resource id %u\n",
__func__, backing_info->resource_id);
*plen = virtio_gpu_write_ctrl_response(
vgpu, &backing_info->hdr, response_desc,
VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID);
if (!*plen)