vtest_renderer.c 23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/**************************************************************************
 *
 * Copyright (C) 2015 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/
24

25
#include <stdlib.h>
26
#include <stdio.h>
Dave Airlie's avatar
Dave Airlie committed
27
#include <string.h>
28
#include <unistd.h>
29
#include <fcntl.h>
30
#include <limits.h>
31

32
#include "virgl_hw.h"
33 34
#include "virglrenderer.h"

35
#include <sys/uio.h>
36
#include <sys/socket.h>
37
#include <sys/mman.h>
38

39
#include "vtest.h"
40
#include "vtest_shm.h"
Dave Airlie's avatar
Dave Airlie committed
41
#include "vtest_protocol.h"
42

43
#include "util.h"
44
#include "util/u_debug.h"
45
#include "util/u_math.h"
46 47
#include "util/u_memory.h"
#include "util/u_hash_table.h"
Dave Airlie's avatar
Dave Airlie committed
48

49

Dave Airlie's avatar
Dave Airlie committed
50
static int ctx_id = 1;
51
static int fence_id = 1;
52
static uint32_t max_length = UINT_MAX;
53 54

static int last_fence;
55
static void vtest_write_fence(UNUSED void *cookie, uint32_t fence_id_in)
56
{
57
   last_fence = fence_id_in;
58 59
}

60
struct virgl_renderer_callbacks vtest_cbs = {
61 62
   .version = 1,
   .write_fence = vtest_write_fence,
63 64 65
};

struct vtest_renderer {
66
   struct vtest_input *input;
67 68 69
   int out_fd;
   unsigned protocol_version;
   struct util_hash_table *iovec_hash;
70 71 72 73
};

struct vtest_renderer renderer;

74 75 76 77 78 79 80 81 82 83
static unsigned
hash_func(void *key)
{
   intptr_t ip = pointer_to_intptr(key);
   return (unsigned)(ip & 0xffffffff);
}

static int
compare_iovecs(void *key1, void *key2)
{
84
   if (key1 < key2) {
85
      return -1;
86
   } else if (key1 > key2) {
87
      return 1;
88
   } else {
89
      return 0;
90
   }
91 92 93 94 95
}

static void free_iovec(void *value)
{
   struct iovec *iovec = value;
96 97
   if (iovec->iov_base)
      munmap(iovec->iov_base, iovec->iov_len);
98 99 100
   free(iovec);
}

101 102 103 104 105 106
static int vtest_block_write(int fd, void *buf, int size)
{
   void *ptr = buf;
   int left;
   int ret;
   left = size;
107

108 109
   do {
      ret = write(fd, ptr, left);
110
      if (ret < 0) {
111
         return -errno;
112 113
      }

114 115 116
      left -= ret;
      ptr += ret;
   } while (left);
117

118 119 120
   return size;
}

121
int vtest_block_read(struct vtest_input *input, void *buf, int size)
122
{
123
   int fd = input->data.fd;
124 125 126
   void *ptr = buf;
   int left;
   int ret;
127 128
   static int savefd = -1;

129 130 131
   left = size;
   do {
      ret = read(fd, ptr, left);
132 133 134 135
      if (ret <= 0) {
         return ret == -1 ? -errno : 0;
      }

136 137 138
      left -= ret;
      ptr += ret;
   } while (left);
139

140 141 142 143 144 145 146 147 148 149 150 151 152 153
   if (getenv("VTEST_SAVE")) {
      if (savefd == -1) {
         savefd = open(getenv("VTEST_SAVE"),
                       O_CLOEXEC|O_CREAT|O_WRONLY|O_TRUNC|O_DSYNC, S_IRUSR|S_IWUSR);
         if (savefd == -1) {
            perror("error opening save file");
            exit(1);
         }
      }
      if (write(savefd, buf, size) != size) {
         perror("failed to save");
         exit(1);
      }
   }
154

155 156 157
   return size;
}

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
static int vtest_send_fd(int socket_fd, int fd)
{
    struct iovec iovec;
    char buf[CMSG_SPACE(sizeof(int))], c;
    struct msghdr msgh = { 0 };
    memset(buf, 0, sizeof(buf));

    iovec.iov_base = &c;
    iovec.iov_len = sizeof(char);

    msgh.msg_name = NULL;
    msgh.msg_namelen = 0;
    msgh.msg_iov = &iovec;
    msgh.msg_iovlen = 1;
    msgh.msg_control = buf;
    msgh.msg_controllen = sizeof(buf);
    msgh.msg_flags = 0;

    struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msgh);
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    cmsg->cmsg_len = CMSG_LEN(sizeof(int));

    *((int *) CMSG_DATA(cmsg)) = fd;

    int size = sendmsg(socket_fd, &msgh, 0);
    if (size < 0) {
      return report_failure("Failed to send fd", -EINVAL);
    }

    return 0;
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204
int vtest_buf_read(struct vtest_input *input, void *buf, int size)
{
   struct vtest_buffer *inbuf = input->data.buffer;
   if (size > inbuf->size) {
      return 0;
   }

   memcpy(buf, inbuf->buffer, size);
   inbuf->buffer += size;
   inbuf->size -= size;

   return size;
}

205
int vtest_create_renderer(struct vtest_input *input, int out_fd, uint32_t length,
206
                          int ctx_flags)
207
{
208 209 210 211
   char *vtestname;
   int ret;

   renderer.iovec_hash = util_hash_table_create(hash_func, compare_iovecs, free_iovec);
212
   renderer.input = input;
213 214 215 216 217 218
   renderer.out_fd = out_fd;

   /* By default we support version 0 unless VCMD_PROTOCOL_VERSION is sent */
   renderer.protocol_version = 0;

   ret = virgl_renderer_init(&renderer,
219
         ctx_flags | VIRGL_RENDERER_THREAD_SYNC, &vtest_cbs);
220
   if (ret) {
221 222
      fprintf(stderr, "failed to initialise renderer.\n");
      return -1;
223
   }
Dave Airlie's avatar
Dave Airlie committed
224

225 226 227 228
   if (length > 1024 * 1024) {
      return -1;
   }

229 230
   vtestname = calloc(1, length + 1);
   if (!vtestname) {
231
      return -1;
232
   }
233

234
   ret = renderer.input->read(renderer.input, vtestname, length);
235 236 237 238
   if (ret != (int)length) {
      ret = -1;
      goto end;
   }
239

240
   ret = virgl_renderer_context_create(ctx_id, strlen(vtestname), vtestname);
241 242

end:
243 244
   free(vtestname);
   return ret;
245 246
}

Dave Airlie's avatar
Dave Airlie committed
247
int vtest_ping_protocol_version(UNUSED uint32_t length_dw)
248
{
249 250 251 252 253 254 255 256 257 258 259
   uint32_t hdr_buf[VTEST_HDR_SIZE];
   int ret;

   hdr_buf[VTEST_CMD_LEN] = VCMD_PING_PROTOCOL_VERSION_SIZE;
   hdr_buf[VTEST_CMD_ID] = VCMD_PING_PROTOCOL_VERSION;
   ret = vtest_block_write(renderer.out_fd, hdr_buf, sizeof(hdr_buf));
   if (ret < 0) {
      return ret;
   }

   return 0;
260 261
}

Dave Airlie's avatar
Dave Airlie committed
262
int vtest_protocol_version(UNUSED uint32_t length_dw)
263
{
264 265 266
   uint32_t hdr_buf[VTEST_HDR_SIZE];
   uint32_t version_buf[VCMD_PROTOCOL_VERSION_SIZE];
   int ret;
267

268
   ret = renderer.input->read(renderer.input, &version_buf, sizeof(version_buf));
269 270
   if (ret != sizeof(version_buf))
      return -1;
271

272 273
   renderer.protocol_version = MIN2(version_buf[VCMD_PROTOCOL_VERSION_VERSION],
                                    VTEST_PROTOCOL_VERSION);
274

275 276 277 278 279 280 281 282
   /*
    * We've deprecated protocol version 1. All of it's called sites are being
    * moved protocol version 2. If the server supports version 2 and the guest
    * supports verison 1, fall back to version 0.
    */
   if (renderer.protocol_version == 1) {
      printf("Older guest Mesa detected, fallbacking to protocol version 0\n");
      renderer.protocol_version = 0;
283 284 285 286 287 288
   }

   /* Protocol version 2 requires shm support. */
   if (!vtest_shm_check()) {
      printf("Shared memory not supported, fallbacking to protocol version 0\n");
      renderer.protocol_version = 0;
289 290
   }

291 292
   hdr_buf[VTEST_CMD_LEN] = VCMD_PROTOCOL_VERSION_SIZE;
   hdr_buf[VTEST_CMD_ID] = VCMD_PROTOCOL_VERSION;
293

294
   version_buf[VCMD_PROTOCOL_VERSION_VERSION] = renderer.protocol_version;
295

296 297
   ret = vtest_block_write(renderer.out_fd, hdr_buf, sizeof(hdr_buf));
   if (ret < 0) {
298
      return ret;
299
   }
300

301 302
   ret = vtest_block_write(renderer.out_fd, version_buf, sizeof(version_buf));
   if (ret < 0) {
303
      return ret;
304
   }
305

306
   return 0;
307 308
}

309 310
void vtest_destroy_renderer(void)
{
311 312
   virgl_renderer_context_destroy(ctx_id);
   virgl_renderer_cleanup(&renderer);
David Riley's avatar
David Riley committed
313 314
   util_hash_table_destroy(renderer.iovec_hash);
   renderer.iovec_hash = NULL;
315
   renderer.input = NULL;
316
   renderer.out_fd = -1;
317 318
}

319
int vtest_send_caps2(UNUSED uint32_t length_dw)
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
   uint32_t hdr_buf[2];
   void *caps_buf;
   int ret;
   uint32_t max_ver, max_size;

   virgl_renderer_get_cap_set(2, &max_ver, &max_size);

   if (max_size == 0) {
      return -1;
   }

   caps_buf = malloc(max_size);
   if (!caps_buf) {
      return -1;
   }

   virgl_renderer_fill_caps(2, 1, caps_buf);

   hdr_buf[0] = max_size + 1;
   hdr_buf[1] = 2;
   ret = vtest_block_write(renderer.out_fd, hdr_buf, 8);
   if (ret < 0) {
      goto end;
   }

   vtest_block_write(renderer.out_fd, caps_buf, max_size);
   if (ret < 0) {
      goto end;
   }
350 351

end:
352 353
   free(caps_buf);
   return 0;
354 355
}

356
int vtest_send_caps(UNUSED uint32_t length_dw)
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
   uint32_t  max_ver, max_size;
   void *caps_buf;
   uint32_t hdr_buf[2];
   int ret;

   virgl_renderer_get_cap_set(1, &max_ver, &max_size);

   caps_buf = malloc(max_size);
   if (!caps_buf) {
      return -1;
   }

   virgl_renderer_fill_caps(1, 1, caps_buf);

   hdr_buf[0] = max_size + 1;
   hdr_buf[1] = 1;
   ret = vtest_block_write(renderer.out_fd, hdr_buf, 8);
   if (ret < 0) {
      goto end;
   }

   vtest_block_write(renderer.out_fd, caps_buf, max_size);
   if (ret < 0) {
      goto end;
   }
383

384
end:
385 386
   free(caps_buf);
   return 0;
387
}
Dave Airlie's avatar
Dave Airlie committed
388

389
int vtest_create_resource(UNUSED uint32_t length_dw)
Dave Airlie's avatar
Dave Airlie committed
390
{
391 392 393 394
   uint32_t res_create_buf[VCMD_RES_CREATE_SIZE];
   struct virgl_renderer_resource_create_args args;
   int ret;

395 396
   ret = renderer.input->read(renderer.input, &res_create_buf,
                              sizeof(res_create_buf));
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
   if (ret != sizeof(res_create_buf)) {
      return -1;
   }

   args.handle = res_create_buf[VCMD_RES_CREATE_RES_HANDLE];
   args.target = res_create_buf[VCMD_RES_CREATE_TARGET];
   args.format = res_create_buf[VCMD_RES_CREATE_FORMAT];
   args.bind = res_create_buf[VCMD_RES_CREATE_BIND];

   args.width = res_create_buf[VCMD_RES_CREATE_WIDTH];
   args.height = res_create_buf[VCMD_RES_CREATE_HEIGHT];
   args.depth = res_create_buf[VCMD_RES_CREATE_DEPTH];
   args.array_size = res_create_buf[VCMD_RES_CREATE_ARRAY_SIZE];
   args.last_level = res_create_buf[VCMD_RES_CREATE_LAST_LEVEL];
   args.nr_samples = res_create_buf[VCMD_RES_CREATE_NR_SAMPLES];
   args.flags = 0;

   ret = virgl_renderer_resource_create(&args, NULL, 0);

   virgl_renderer_ctx_attach_resource(ctx_id, args.handle);
   return ret;
Dave Airlie's avatar
Dave Airlie committed
418 419
}

420
int vtest_create_resource2(UNUSED uint32_t length_dw)
421
{
422 423 424
   uint32_t res_create_buf[VCMD_RES_CREATE2_SIZE];
   struct virgl_renderer_resource_create_args args;
   struct iovec *iovec;
425
   int ret, fd;
426

427 428
   ret = renderer.input->read(renderer.input, &res_create_buf,
                              sizeof(res_create_buf));
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
   if (ret != sizeof(res_create_buf)) {
      return -1;
   }

   args.handle = res_create_buf[VCMD_RES_CREATE2_RES_HANDLE];
   args.target = res_create_buf[VCMD_RES_CREATE2_TARGET];
   args.format = res_create_buf[VCMD_RES_CREATE2_FORMAT];
   args.bind = res_create_buf[VCMD_RES_CREATE2_BIND];

   args.width = res_create_buf[VCMD_RES_CREATE2_WIDTH];
   args.height = res_create_buf[VCMD_RES_CREATE2_HEIGHT];
   args.depth = res_create_buf[VCMD_RES_CREATE2_DEPTH];
   args.array_size = res_create_buf[VCMD_RES_CREATE2_ARRAY_SIZE];
   args.last_level = res_create_buf[VCMD_RES_CREATE2_LAST_LEVEL];
   args.nr_samples = res_create_buf[VCMD_RES_CREATE2_NR_SAMPLES];
   args.flags = 0;

   // Check that the handle doesn't already exist.
   if (util_hash_table_get(renderer.iovec_hash, intptr_to_pointer(args.handle))) {
448
      return -EEXIST;
449
   }
450

451
   ret = virgl_renderer_resource_create(&args, NULL, 0);
452 453
   if (ret)
      return report_failed_call("virgl_renderer_resource_create", ret);
454

455
   virgl_renderer_ctx_attach_resource(ctx_id, args.handle);
456

457 458
   iovec = CALLOC_STRUCT(iovec);
   if (!iovec) {
459
      return -ENOMEM;
460
   }
461

462
   iovec->iov_len = res_create_buf[VCMD_RES_CREATE2_DATA_SIZE];
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

   /* Multi-sample textures have no backing store, but an associated GL resource. */
   if (iovec->iov_len == 0) {
      iovec->iov_base = NULL;
      goto out;
   }

   fd = vtest_new_shm(args.handle, iovec->iov_len);
   if (fd < 0) {
      FREE(iovec);
      return report_failed_call("vtest_new_shm", fd);
   }

   iovec->iov_base = mmap(NULL, iovec->iov_len, PROT_WRITE | PROT_READ,
                          MAP_SHARED, fd, 0);

   if (iovec->iov_base == MAP_FAILED) {
      close(fd);
481
      FREE(iovec);
482
      return -ENOMEM;
483
   }
484

485 486 487 488 489 490 491 492 493 494 495
   ret = vtest_send_fd(renderer.out_fd, fd);
   if (ret < 0) {
      close(fd);
      munmap(iovec->iov_base, iovec->iov_len);
      return report_failed_call("vtest_send_fd", ret);
   }

   /* Closing the file descriptor does not unmap the region. */
   close(fd);

out:
496 497
   virgl_renderer_resource_attach_iov(args.handle, iovec, 1);
   util_hash_table_set(renderer.iovec_hash, intptr_to_pointer(args.handle), iovec);
498
   return 0;
499 500
}

501
int vtest_resource_unref(UNUSED uint32_t length_dw)
Dave Airlie's avatar
Dave Airlie committed
502
{
503 504 505
   uint32_t res_unref_buf[VCMD_RES_UNREF_SIZE];
   int ret;
   uint32_t handle;
Dave Airlie's avatar
Dave Airlie committed
506

507 508
   ret = renderer.input->read(renderer.input, &res_unref_buf,
                              sizeof(res_unref_buf));
509
   if (ret != sizeof(res_unref_buf)) {
Dave Airlie's avatar
Dave Airlie committed
510
      return -1;
511
   }
Dave Airlie's avatar
Dave Airlie committed
512

513 514
   handle = res_unref_buf[VCMD_RES_UNREF_RES_HANDLE];
   virgl_renderer_ctx_attach_resource(ctx_id, handle);
515

516 517
   virgl_renderer_resource_detach_iov(handle, NULL, NULL);
   util_hash_table_remove(renderer.iovec_hash, intptr_to_pointer(handle));
518

519 520
   virgl_renderer_resource_unref(handle);
   return 0;
Dave Airlie's avatar
Dave Airlie committed
521
}
Dave Airlie's avatar
Dave Airlie committed
522 523 524

int vtest_submit_cmd(uint32_t length_dw)
{
525 526
   uint32_t *cbuf;
   int ret;
Dave Airlie's avatar
Dave Airlie committed
527

528
   if (length_dw > max_length / 4) {
529 530
      return -1;
   }
531

532 533 534 535
   cbuf = malloc(length_dw * 4);
   if (!cbuf) {
      return -1;
   }
Dave Airlie's avatar
Dave Airlie committed
536

537
   ret = renderer.input->read(renderer.input, cbuf, length_dw * 4);
538 539 540 541
   if (ret != (int)length_dw * 4) {
      free(cbuf);
      return -1;
   }
Dave Airlie's avatar
Dave Airlie committed
542

543
   virgl_renderer_submit_cmd(cbuf, ctx_id, length_dw);
Dave Airlie's avatar
Dave Airlie committed
544

545 546
   free(cbuf);
   return 0;
Dave Airlie's avatar
Dave Airlie committed
547
}
548 549

#define DECODE_TRANSFER \
550 551 552 553 554 555 556 557 558 559 560 561 562
   do {								\
      handle = thdr_buf[VCMD_TRANSFER_RES_HANDLE];		\
      level = thdr_buf[VCMD_TRANSFER_LEVEL];			\
      stride = thdr_buf[VCMD_TRANSFER_STRIDE];			\
      layer_stride = thdr_buf[VCMD_TRANSFER_LAYER_STRIDE];	\
      box.x = thdr_buf[VCMD_TRANSFER_X];			\
      box.y = thdr_buf[VCMD_TRANSFER_Y];			\
      box.z = thdr_buf[VCMD_TRANSFER_Z];			\
      box.w = thdr_buf[VCMD_TRANSFER_WIDTH];			\
      box.h = thdr_buf[VCMD_TRANSFER_HEIGHT];			\
      box.d = thdr_buf[VCMD_TRANSFER_DEPTH];			\
      data_size = thdr_buf[VCMD_TRANSFER_DATA_SIZE];		\
   } while(0)
563 564


565
int vtest_transfer_get(UNUSED uint32_t length_dw)
566
{
567 568 569 570 571 572 573 574 575
   uint32_t thdr_buf[VCMD_TRANSFER_HDR_SIZE];
   int ret;
   int level;
   uint32_t stride, layer_stride, handle;
   struct virgl_box box;
   uint32_t data_size;
   void *ptr;
   struct iovec iovec;

576 577
   ret = renderer.input->read(renderer.input, thdr_buf,
                              VCMD_TRANSFER_HDR_SIZE * 4);
578
   if (ret != VCMD_TRANSFER_HDR_SIZE * 4) {
579
      return ret;
580
   }
581

582
   DECODE_TRANSFER;
583

584 585 586 587
   if (data_size > max_length) {
      return -ENOMEM;
   }

588 589
   ptr = malloc(data_size);
   if (!ptr) {
590
      return -ENOMEM;
591
   }
592

593 594 595 596 597 598 599 600 601 602 603
   iovec.iov_len = data_size;
   iovec.iov_base = ptr;
   ret = virgl_renderer_transfer_read_iov(handle,
         ctx_id,
         level,
         stride,
         layer_stride,
         &box,
         0,
         &iovec, 1);
   if (ret) {
604
      fprintf(stderr," transfer read failed %d\n", ret);
605 606 607
   }

   ret = vtest_block_write(renderer.out_fd, ptr, data_size);
608

609 610
   free(ptr);
   return ret < 0 ? ret : 0;
611 612
}

613 614 615 616
int vtest_transfer_get_nop(UNUSED uint32_t length_dw)
{
   uint32_t thdr_buf[VCMD_TRANSFER_HDR_SIZE];
   int ret;
617 618 619
   UNUSED int level;
   UNUSED uint32_t stride, layer_stride, handle;
   UNUSED struct virgl_box box;
620 621 622 623 624 625 626 627 628 629 630
   uint32_t data_size;
   void *ptr;

   ret = renderer.input->read(renderer.input, thdr_buf,
                              VCMD_TRANSFER_HDR_SIZE * 4);
   if (ret != VCMD_TRANSFER_HDR_SIZE * 4) {
      return ret;
   }

   DECODE_TRANSFER;

631 632 633 634
   if (data_size > max_length) {
      return -ENOMEM;
   }

635 636 637 638 639 640 641 642 643 644 645 646 647
   ptr = malloc(data_size);
   if (!ptr) {
      return -ENOMEM;
   }

   memset(ptr, 0, data_size);

   ret = vtest_block_write(renderer.out_fd, ptr, data_size);

   free(ptr);
   return ret < 0 ? ret : 0;
}

648
int vtest_transfer_put(UNUSED uint32_t length_dw)
649
{
650 651 652 653 654 655 656 657 658
   uint32_t thdr_buf[VCMD_TRANSFER_HDR_SIZE];
   int ret;
   int level;
   uint32_t stride, layer_stride, handle;
   struct virgl_box box;
   uint32_t data_size;
   void *ptr;
   struct iovec iovec;

659 660
   ret = renderer.input->read(renderer.input, thdr_buf,
                              VCMD_TRANSFER_HDR_SIZE * 4);
661
   if (ret != VCMD_TRANSFER_HDR_SIZE * 4) {
662
      return ret;
663
   }
664

665
   DECODE_TRANSFER;
666

667 668 669 670
   if (data_size > max_length) {
      return -ENOMEM;
   }

671 672
   ptr = malloc(data_size);
   if (!ptr) {
673
      return -ENOMEM;
674
   }
675

676
   ret = renderer.input->read(renderer.input, ptr, data_size);
677
   if (ret < 0) {
678
      return ret;
679
   }
680

681 682 683 684 685 686 687 688 689 690 691
   iovec.iov_len = data_size;
   iovec.iov_base = ptr;
   ret = virgl_renderer_transfer_write_iov(handle,
                                           ctx_id,
                                           level,
                                           stride,
                                           layer_stride,
                                           &box,
                                           0,
                                           &iovec, 1);
   if (ret) {
692
      fprintf(stderr," transfer write failed %d\n", ret);
693 694 695 696
   }

   free(ptr);
   return 0;
697 698
}

699 700 701 702
int vtest_transfer_put_nop(UNUSED uint32_t length_dw)
{
   uint32_t thdr_buf[VCMD_TRANSFER_HDR_SIZE];
   int ret;
703 704 705
   UNUSED int level;
   UNUSED uint32_t stride, layer_stride, handle;
   UNUSED struct virgl_box box;
706 707 708 709 710 711 712 713 714 715 716
   uint32_t data_size;
   void *ptr;

   ret = renderer.input->read(renderer.input, thdr_buf,
                              VCMD_TRANSFER_HDR_SIZE * 4);
   if (ret != VCMD_TRANSFER_HDR_SIZE * 4) {
      return ret;
   }

   DECODE_TRANSFER;

717 718 719 720
   if (data_size > max_length) {
      return -ENOMEM;
   }

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
   ptr = malloc(data_size);
   if (!ptr) {
      return -ENOMEM;
   }

   ret = renderer.input->read(renderer.input, ptr, data_size);
   if (ret < 0) {
      return ret;
   }

   free(ptr);
   return 0;
}


736
#define DECODE_TRANSFER2 \
737 738 739 740 741 742 743 744 745 746 747
   do {							\
      handle = thdr_buf[VCMD_TRANSFER2_RES_HANDLE];	\
      level = thdr_buf[VCMD_TRANSFER2_LEVEL];		\
      box.x = thdr_buf[VCMD_TRANSFER2_X];		\
      box.y = thdr_buf[VCMD_TRANSFER2_Y];		\
      box.z = thdr_buf[VCMD_TRANSFER2_Z];		\
      box.w = thdr_buf[VCMD_TRANSFER2_WIDTH];		\
      box.h = thdr_buf[VCMD_TRANSFER2_HEIGHT];		\
      box.d = thdr_buf[VCMD_TRANSFER2_DEPTH];		\
      offset = thdr_buf[VCMD_TRANSFER2_OFFSET];		\
   } while(0)
748 749


750
int vtest_transfer_get2(UNUSED uint32_t length_dw)
751
{
752 753 754 755 756 757 758 759
   uint32_t thdr_buf[VCMD_TRANSFER2_HDR_SIZE];
   int ret;
   int level;
   uint32_t handle;
   struct virgl_box box;
   uint32_t offset;
   struct iovec *iovec;

760
   ret = renderer.input->read(renderer.input, thdr_buf, sizeof(thdr_buf));
761
   if (ret != sizeof(thdr_buf)) {
762
      return ret;
763
   }
764

765
   DECODE_TRANSFER2;
766

767 768
   iovec = util_hash_table_get(renderer.iovec_hash, intptr_to_pointer(handle));
   if (!iovec) {
769
      return report_failed_call("util_hash_table_get", -ESRCH);
770
   }
771

772
   if (offset >= iovec->iov_len) {
773
      return report_failure("offset larger then length of backing store", -EFAULT);
774 775 776 777 778 779 780 781 782 783 784
   }

   ret = virgl_renderer_transfer_read_iov(handle,
                                          ctx_id,
                                          level,
                                          0,
                                          0,
                                          &box,
                                          offset,
                                          NULL, 0);
   if (ret) {
785
      return report_failed_call("virgl_renderer_transfer_read_iov", ret);
786
   }
787

788
   return 0;
789 790
}

791 792 793 794
int vtest_transfer_get2_nop(UNUSED uint32_t length_dw)
{
   uint32_t thdr_buf[VCMD_TRANSFER2_HDR_SIZE];
   int ret;
795
   UNUSED int level;
796
   uint32_t handle;
797
   UNUSED struct virgl_box box;
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
   uint32_t offset;
   struct iovec *iovec;

   ret = renderer.input->read(renderer.input, thdr_buf, sizeof(thdr_buf));
   if (ret != sizeof(thdr_buf)) {
      return ret;
   }

   DECODE_TRANSFER2;

   iovec = util_hash_table_get(renderer.iovec_hash, intptr_to_pointer(handle));
   if (!iovec) {
      return report_failed_call("util_hash_table_get", -ESRCH);
   }

   if (offset >= iovec->iov_len) {
      return report_failure("offset larger then length of backing store", -EFAULT);
   }

   return 0;
}

820
int vtest_transfer_put2(UNUSED uint32_t length_dw)
821
{
822 823 824 825 826
   uint32_t thdr_buf[VCMD_TRANSFER2_HDR_SIZE];
   int ret;
   int level;
   uint32_t handle;
   struct virgl_box box;
827
   UNUSED uint32_t data_size;
828 829 830
   uint32_t offset;
   struct iovec *iovec;

831
   ret = renderer.input->read(renderer.input, thdr_buf, sizeof(thdr_buf));
832
   if (ret != sizeof(thdr_buf)) {
833
      return ret;
834
   }
835

836
   DECODE_TRANSFER2;
837

838 839
   iovec = util_hash_table_get(renderer.iovec_hash, intptr_to_pointer(handle));
   if (!iovec) {
840
      return report_failed_call("util_hash_table_get", -ESRCH);
841
   }
842

843 844 845 846 847 848 849 850 851
   ret = virgl_renderer_transfer_write_iov(handle,
                                           ctx_id,
                                           level,
                                           0,
                                           0,
                                           &box,
                                           offset,
                                           NULL, 0);
   if (ret) {
852
      return report_failed_call("virgl_renderer_transfer_write_iov", ret);
853
   }
854

855
   return 0;
856 857
}

858 859 860 861
int vtest_transfer_put2_nop(UNUSED uint32_t length_dw)
{
   uint32_t thdr_buf[VCMD_TRANSFER2_HDR_SIZE];
   int ret;
862
   UNUSED int level;
863
   uint32_t handle;
864
   UNUSED struct virgl_box box;
865
   UNUSED uint32_t data_size;
866
   UNUSED uint32_t offset;
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
   struct iovec *iovec;

   ret = renderer.input->read(renderer.input, thdr_buf, sizeof(thdr_buf));
   if (ret != sizeof(thdr_buf)) {
      return ret;
   }

   DECODE_TRANSFER2;

   iovec = util_hash_table_get(renderer.iovec_hash, intptr_to_pointer(handle));
   if (!iovec) {
      return report_failed_call("util_hash_table_get", -ESRCH);
   }

   return 0;
}

884
int vtest_resource_busy_wait(UNUSED uint32_t length_dw)
885
{
886 887 888 889 890 891 892
   uint32_t bw_buf[VCMD_BUSY_WAIT_SIZE];
   int ret, fd;
   int flags;
   uint32_t hdr_buf[VTEST_HDR_SIZE];
   uint32_t reply_buf[1];
   bool busy = false;

893
   ret = renderer.input->read(renderer.input, &bw_buf, sizeof(bw_buf));
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
   if (ret != sizeof(bw_buf)) {
      return -1;
   }

   /*  handle = bw_buf[VCMD_BUSY_WAIT_HANDLE]; unused as of now */
   flags = bw_buf[VCMD_BUSY_WAIT_FLAGS];

   if (flags == VCMD_BUSY_WAIT_FLAG_WAIT) {
      do {
         if (last_fence == (fence_id - 1)) {
            break;
         }

         fd = virgl_renderer_get_poll_fd();
         if (fd != -1) {
            vtest_wait_for_fd_read(fd);
         }

         virgl_renderer_poll();
      } while (1);

      busy = false;
   } else {
      busy = last_fence != (fence_id - 1);
   }

   hdr_buf[VTEST_CMD_LEN] = 1;
   hdr_buf[VTEST_CMD_ID] = VCMD_RESOURCE_BUSY_WAIT;
   reply_buf[0] = busy ? 1 : 0;

   ret = vtest_block_write(renderer.out_fd, hdr_buf, sizeof(hdr_buf));
   if (ret < 0) {
      return ret;
   }

   ret = vtest_block_write(renderer.out_fd, reply_buf, sizeof(reply_buf));
   if (ret < 0) {
      return ret;
   }

   return 0;
935 936 937 938
}

int vtest_renderer_create_fence(void)
{
939 940
   virgl_renderer_create_fence(fence_id++, ctx_id);
   return 0;
941 942 943 944
}

int vtest_poll(void)
{
945 946
   virgl_renderer_poll();
   return 0;
947
}
948 949 950 951 952

void vtest_set_max_length(uint32_t length)
{
   max_length = length;
}