Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
/*-
 * Copyright (c) 2018 VMware, Inc.
 *
 * SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0)
 */

/* This file implements defines and helper functions. */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/uio.h>

#include <machine/bus.h>

#include "vmci.h"
#include "vmci_defs.h"
#include "vmci_kernel_defs.h"
#include "vmci_kernel_if.h"
#include "vmci_queue.h"

struct vmci_queue_kernel_if {
	size_t			num_pages;	/* Num pages incl. header. */
	struct vmci_dma_alloc	*dmas;		/* For dma alloc. */
};

/*
 *------------------------------------------------------------------------------
 *
 * vmci_init_lock
 *
 *     Initializes the lock. Must be called before use.
 *
 * Results:
 *     Always VMCI_SUCCESS.
 *
 * Side effects:
 *     Thread can block.
 *
 *------------------------------------------------------------------------------
 */

int
vmci_init_lock(vmci_lock *lock, char *name)
{

	mtx_init(lock, name, NULL, MTX_DEF | MTX_NOWITNESS);
	return (VMCI_SUCCESS);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_cleanup_lock
 *
 *     Cleanup the lock. Must be called before deallocating lock.
 *
 * Results:
 *     None
 *
 * Side effects:
 *     Deletes kernel lock state
 *
 *------------------------------------------------------------------------------
 */

void
vmci_cleanup_lock(vmci_lock *lock)
{

	mtx_destroy(lock);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_grab_lock
 *
 *     Grabs the given lock.
 *
 * Results:
 *      None
 *
 * Side effects:
 *      Thread can block.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_grab_lock(vmci_lock *lock)
{

	mtx_lock(lock);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_release_lock
 *
 *     Releases the given lock.
 *
 * Results:
 *     None
 *
 * Side effects:
 *     A thread blocked on this lock may wake up.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_release_lock(vmci_lock *lock)
{

	mtx_unlock(lock);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_grab_lock_bh
 *
 *     Grabs the given lock.
 *
 * Results:
 *     None
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_grab_lock_bh(vmci_lock *lock)
{

	mtx_lock(lock);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_release_lock_bh
 *
 *     Releases the given lock.
 *
 * Results:
 *     None
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_release_lock_bh(vmci_lock *lock)
{

	mtx_unlock(lock);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_alloc_kernel_mem
 *
 *     Allocate physically contiguous memory for the VMCI driver.
 *
 * Results:
 *     The address allocated or NULL on error.
 *
 *
 * Side effects:
 *     Memory may be allocated.
 *
 *------------------------------------------------------------------------------
 */

void *
vmci_alloc_kernel_mem(size_t size, int flags)
{
	void *ptr;

	if ((flags & VMCI_MEMORY_ATOMIC) != 0)
		ptr = contigmalloc(size, M_DEVBUF, M_NOWAIT, 0, 0xFFFFFFFF,
		    8, 1024 * 1024);
	else
		ptr = contigmalloc(size, M_DEVBUF, M_WAITOK, 0, 0xFFFFFFFF,
		    8, 1024 * 1024);

	return (ptr);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_free_kernel_mem
 *
 *     Free kernel memory allocated for the VMCI driver.
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     Memory is freed.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_free_kernel_mem(void *ptr, size_t size)
{

	contigfree(ptr, size, M_DEVBUF);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_can_schedule_delayed_work --
 *
 *     Checks to see if the given platform supports delayed work callbacks.
 *
 * Results:
 *     true if it does. false otherwise.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

bool
vmci_can_schedule_delayed_work(void)
{

	return (true);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_schedule_delayed_work --
 *
 *     Schedule the specified callback.
 *
 * Results:
 *     Zero on success, error code otherwise.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

int
vmci_schedule_delayed_work(vmci_work_fn *work_fn, void *data)
{

	return (vmci_schedule_delayed_work_fn(work_fn, data));
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_create_event --
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_create_event(vmci_event *event)
{

	sema_init(event, 0, "vmci_event");
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_destroy_event --
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_destroy_event(vmci_event *event)
{

	if (mtx_owned(&event->sema_mtx))
		sema_destroy(event);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_signal_event --
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_signal_event(vmci_event *event)
{

	sema_post(event);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_wait_on_event --
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_wait_on_event(vmci_event *event, vmci_event_release_cb release_cb,
    void *client_data)
{

	release_cb(client_data);
	sema_wait(event);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_mutex_init --
 *
 *     Initializes the mutex. Must be called before use.
 *
 * Results:
 *     Success.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

int
vmci_mutex_init(vmci_mutex *mutex, char *name)
{

	mtx_init(mutex, name, NULL, MTX_DEF | MTX_NOWITNESS);
	return (VMCI_SUCCESS);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_mutex_destroy --
 *
 *     Destroys the mutex.
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_mutex_destroy(vmci_mutex *mutex)
{

	mtx_destroy(mutex);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_mutex_acquire --
 *
 *     Acquires the mutex.
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     Thread may block.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_mutex_acquire(vmci_mutex *mutex)
{

	mtx_lock(mutex);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_mutex_release --
 *
 *     Releases the mutex.
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     May wake up the thread blocking on this mutex.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_mutex_release(vmci_mutex *mutex)
{

	mtx_unlock(mutex);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_alloc_queue --
 *
 *     Allocates kernel queue pages of specified size with IOMMU mappings, plus
 *     space for the queue structure/kernel interface and the queue header.
 *
 * Results:
 *     Pointer to the queue on success, NULL otherwise.
 *
 * Side effects:
 *     Memory is allocated.
 *
 *------------------------------------------------------------------------------
 */

void *
vmci_alloc_queue(uint64_t size, uint32_t flags)
{
	struct vmci_queue *queue;
	size_t i;
	const size_t num_pages = CEILING(size, PAGE_SIZE) + 1;
	const size_t dmas_size = num_pages * sizeof(struct vmci_dma_alloc);
	const size_t queue_size =
	    sizeof(*queue) + sizeof(*(queue->kernel_if)) + dmas_size;

	/* Size should be enforced by vmci_qpair_alloc(), double-check here. */
	if (size > VMCI_MAX_GUEST_QP_MEMORY) {
		ASSERT(false);
		return (NULL);
	}

	queue = malloc(queue_size, M_DEVBUF, M_NOWAIT);
	if (!queue)
		return (NULL);

	queue->q_header = NULL;
	queue->saved_header = NULL;
	queue->kernel_if = (struct vmci_queue_kernel_if *)(queue + 1);
	queue->kernel_if->num_pages = num_pages;
	queue->kernel_if->dmas = (struct vmci_dma_alloc *)(queue->kernel_if +
	    1);
	for (i = 0; i < num_pages; i++) {
		vmci_dma_malloc(PAGE_SIZE, 1, &queue->kernel_if->dmas[i]);
		if (!queue->kernel_if->dmas[i].dma_vaddr) {
			/* Size excl. the header. */
			vmci_free_queue(queue, i * PAGE_SIZE);
			return (NULL);
		}
	}

	/* Queue header is the first page. */
	queue->q_header = (void *)queue->kernel_if->dmas[0].dma_vaddr;

	return ((void *)queue);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_free_queue --
 *
 *     Frees kernel VA space for a given queue and its queue header, and frees
 *     physical data pages.
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     Memory is freed.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_free_queue(void *q, uint64_t size)
{
	struct vmci_queue *queue = q;

	if (queue) {
		const size_t num_pages = CEILING(size, PAGE_SIZE) + 1;
		uint64_t i;

		/* Given size doesn't include header, so add in a page here. */
		for (i = 0; i < num_pages; i++)
			vmci_dma_free(&queue->kernel_if->dmas[i]);
		free(queue, M_DEVBUF);
	}
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_alloc_ppn_set --
 *
 *     Allocates two list of PPNs --- one for the pages in the produce queue,
 *     and the other for the pages in the consume queue. Intializes the list of
 *     PPNs with the page frame numbers of the KVA for the two queues (and the
 *     queue headers).
 *
 * Results:
 *     Success or failure.
 *
 * Side effects:
 *     Memory may be allocated.
 *
 *-----------------------------------------------------------------------------
 */

int
vmci_alloc_ppn_set(void *prod_q, uint64_t num_produce_pages, void *cons_q,
    uint64_t num_consume_pages, struct ppn_set *ppn_set)
{
	struct vmci_queue *consume_q = cons_q;
	struct vmci_queue *produce_q = prod_q;
	vmci_ppn_list consume_ppns;
	vmci_ppn_list produce_ppns;
	uint64_t i;

	if (!produce_q || !num_produce_pages || !consume_q ||
	    !num_consume_pages || !ppn_set)
		return (VMCI_ERROR_INVALID_ARGS);

	if (ppn_set->initialized)
		return (VMCI_ERROR_ALREADY_EXISTS);

	produce_ppns =
	    vmci_alloc_kernel_mem(num_produce_pages * sizeof(*produce_ppns),
	    VMCI_MEMORY_NORMAL);
	if (!produce_ppns)
		return (VMCI_ERROR_NO_MEM);

	consume_ppns =
	    vmci_alloc_kernel_mem(num_consume_pages * sizeof(*consume_ppns),
	    VMCI_MEMORY_NORMAL);
	if (!consume_ppns) {
		vmci_free_kernel_mem(produce_ppns,
		    num_produce_pages * sizeof(*produce_ppns));
		return (VMCI_ERROR_NO_MEM);
	}

	for (i = 0; i < num_produce_pages; i++) {
		unsigned long pfn;

		produce_ppns[i] =
		    pfn = produce_q->kernel_if->dmas[i].dma_paddr >> PAGE_SHIFT;

		/*
		 * Fail allocation if PFN isn't supported by hypervisor.
		 */

		if (sizeof(pfn) >
		    sizeof(*produce_ppns) && pfn != produce_ppns[i])
			goto ppn_error;
	}
	for (i = 0; i < num_consume_pages; i++) {
		unsigned long pfn;

		consume_ppns[i] =
		    pfn = consume_q->kernel_if->dmas[i].dma_paddr >> PAGE_SHIFT;

		/*
		 * Fail allocation if PFN isn't supported by hypervisor.
		 */

		if (sizeof(pfn) >
		    sizeof(*consume_ppns) && pfn != consume_ppns[i])
			goto ppn_error;
	}

	ppn_set->num_produce_pages = num_produce_pages;
	ppn_set->num_consume_pages = num_consume_pages;
	ppn_set->produce_ppns = produce_ppns;
	ppn_set->consume_ppns = consume_ppns;
	ppn_set->initialized = true;
	return (VMCI_SUCCESS);

ppn_error:
	vmci_free_kernel_mem(produce_ppns, num_produce_pages *
	    sizeof(*produce_ppns));
	vmci_free_kernel_mem(consume_ppns, num_consume_pages *
	    sizeof(*consume_ppns));
	return (VMCI_ERROR_INVALID_ARGS);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_free_ppn_set --
 *
 *     Frees the two list of PPNs for a queue pair.
 *
 * Results:
 *     None.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_free_ppn_set(struct ppn_set *ppn_set)
{

	ASSERT(ppn_set);
	if (ppn_set->initialized) {
		/* Do not call these functions on NULL inputs. */
		ASSERT(ppn_set->produce_ppns && ppn_set->consume_ppns);
		vmci_free_kernel_mem(ppn_set->produce_ppns,
		    ppn_set->num_produce_pages *
		    sizeof(*ppn_set->produce_ppns));
		vmci_free_kernel_mem(ppn_set->consume_ppns,
		    ppn_set->num_consume_pages *
		    sizeof(*ppn_set->consume_ppns));
	}
	memset(ppn_set, 0, sizeof(*ppn_set));
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_populate_ppn_list --
 *
 *     Populates the list of PPNs in the hypercall structure with the PPNS
 *     of the produce queue and the consume queue.
 *
 * Results:
 *     VMCI_SUCCESS.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

int
vmci_populate_ppn_list(uint8_t *call_buf, const struct ppn_set *ppn_set)
{

	ASSERT(call_buf && ppn_set && ppn_set->initialized);
	memcpy(call_buf, ppn_set->produce_ppns,
	    ppn_set->num_produce_pages * sizeof(*ppn_set->produce_ppns));
	memcpy(call_buf + ppn_set->num_produce_pages *
	    sizeof(*ppn_set->produce_ppns), ppn_set->consume_ppns,
	    ppn_set->num_consume_pages * sizeof(*ppn_set->consume_ppns));

	return (VMCI_SUCCESS);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_memcpy_{to,from}iovec --
 *
 *     These helper routines will copy the specified bytes to/from memory that's
 *     specified as a struct iovec.  The routines can not verify the correctness
 *     of the struct iovec's contents.
 *
 * Results:
 *      None.
 *
 * Side effects:
 *      None.
 *
 *------------------------------------------------------------------------------
 */

static inline void
vmci_memcpy_toiovec(struct iovec *iov, uint8_t *src, size_t len)
{

	while (len > 0) {
		if (iov->iov_len) {
			size_t to_copy = MIN(iov->iov_len, len);
			memcpy(iov->iov_base, src, to_copy);
			src += to_copy;
			len -= to_copy;
			iov->iov_base = (void *)((uintptr_t) iov->iov_base +
			    to_copy);
			iov->iov_len -= to_copy;
		}
		iov++;
	}
}

static inline void
vmci_memcpy_fromiovec(uint8_t *dst, struct iovec *iov, size_t len)
{

	while (len > 0) {
		if (iov->iov_len) {
			size_t to_copy = MIN(iov->iov_len, len);
			memcpy(dst, iov->iov_base, to_copy);
			dst += to_copy;
			len -= to_copy;
			iov->iov_base = (void *)((uintptr_t) iov->iov_base +
			    to_copy);
			iov->iov_len -= to_copy;
		}
		iov++;
	}
}

/*
 *------------------------------------------------------------------------------
 *
 * __vmci_memcpy_to_queue --
 *
 *     Copies from a given buffer or iovector to a VMCI Queue. Assumes that
 *     offset + size does not wrap around in the queue.
 *
 * Results:
 *     Zero on success, negative error code on failure.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

#pragma GCC diagnostic ignored "-Wcast-qual"
static int
__vmci_memcpy_to_queue(struct vmci_queue *queue, uint64_t queue_offset,
    const void *src, size_t size, bool is_iovec)
{
	struct vmci_queue_kernel_if *kernel_if = queue->kernel_if;
	size_t bytes_copied = 0;

	while (bytes_copied < size) {
		const uint64_t page_index =
		    (queue_offset + bytes_copied) / PAGE_SIZE;
		const size_t page_offset =
		    (queue_offset + bytes_copied) & (PAGE_SIZE - 1);
		void *va;
		size_t to_copy;

		/* Skip header. */
		va = (void *)kernel_if->dmas[page_index + 1].dma_vaddr;

		ASSERT(va);
		/*
		 * Fill up the page if we have enough payload, or else
		 * copy the remaining bytes.
		 */
		to_copy = MIN(PAGE_SIZE - page_offset, size - bytes_copied);

		if (is_iovec) {
			struct iovec *iov = (struct iovec *)src;

			/* The iovec will track bytes_copied internally. */
			vmci_memcpy_fromiovec((uint8_t *)va + page_offset,
			    iov, to_copy);
		} else
			memcpy((uint8_t *)va + page_offset,
			    (uint8_t *)src + bytes_copied, to_copy);
		bytes_copied += to_copy;
	}

	return (VMCI_SUCCESS);
}

/*
 *------------------------------------------------------------------------------
 *
 * __vmci_memcpy_from_queue --
 *
 *     Copies to a given buffer or iovector from a VMCI Queue. Assumes that
 *     offset + size does not wrap around in the queue.
 *
 * Results:
 *     Zero on success, negative error code on failure.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

static int
__vmci_memcpy_from_queue(void *dest, const struct vmci_queue *queue,
    uint64_t queue_offset, size_t size, bool is_iovec)
{
	struct vmci_queue_kernel_if *kernel_if = queue->kernel_if;
	size_t bytes_copied = 0;

	while (bytes_copied < size) {
		const uint64_t page_index =
		    (queue_offset + bytes_copied) / PAGE_SIZE;
		const size_t page_offset =
		    (queue_offset + bytes_copied) & (PAGE_SIZE - 1);
		void *va;
		size_t to_copy;

		/* Skip header. */
		va = (void *)kernel_if->dmas[page_index + 1].dma_vaddr;

		ASSERT(va);
		/*
		 * Fill up the page if we have enough payload, or else
		 * copy the remaining bytes.
		 */
		to_copy = MIN(PAGE_SIZE - page_offset, size - bytes_copied);

		if (is_iovec) {
			struct iovec *iov = (struct iovec *)dest;

			/* The iovec will track bytesCopied internally. */
			vmci_memcpy_toiovec(iov, (uint8_t *)va +
			    page_offset, to_copy);
		} else
			memcpy((uint8_t *)dest + bytes_copied,
			    (uint8_t *)va + page_offset, to_copy);

		bytes_copied += to_copy;
	}

	return (VMCI_SUCCESS);
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_memcpy_to_queue --
 *
 *     Copies from a given buffer to a VMCI Queue.
 *
 * Results:
 *     Zero on success, negative error code on failure.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

int
vmci_memcpy_to_queue(struct vmci_queue *queue, uint64_t queue_offset,
    const void *src, size_t src_offset, size_t size, int buf_type,
    bool can_block)
{

	ASSERT(can_block);

	return (__vmci_memcpy_to_queue(queue, queue_offset,
	    (uint8_t *)src + src_offset, size, false));
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_memcpy_from_queue --
 *
 *      Copies to a given buffer from a VMCI Queue.
 *
 * Results:
 *      Zero on success, negative error code on failure.
 *
 * Side effects:
 *      None.
 *
 *------------------------------------------------------------------------------
 */

int
vmci_memcpy_from_queue(void *dest, size_t dest_offset,
    const struct vmci_queue *queue, uint64_t queue_offset, size_t size,
    int buf_type, bool can_block)
{

	ASSERT(can_block);

	return (__vmci_memcpy_from_queue((uint8_t *)dest + dest_offset,
	    queue, queue_offset, size, false));
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_memcpy_to_queue_local --
 *
 *     Copies from a given buffer to a local VMCI queue. This is the
 *     same as a regular copy.
 *
 * Results:
 *     Zero on success, negative error code on failure.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

int
vmci_memcpy_to_queue_local(struct vmci_queue *queue, uint64_t queue_offset,
    const void *src, size_t src_offset, size_t size, int buf_type,
    bool can_block)
{

	ASSERT(can_block);

	return (__vmci_memcpy_to_queue(queue, queue_offset,
	    (uint8_t *)src + src_offset, size, false));
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_memcpy_from_queue_local --
 *
 *     Copies to a given buffer from a VMCI Queue.
 *
 * Results:
 *     Zero on success, negative error code on failure.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

int
vmci_memcpy_from_queue_local(void *dest, size_t dest_offset,
    const struct vmci_queue *queue, uint64_t queue_offset, size_t size,
    int buf_type, bool can_block)
{

	ASSERT(can_block);

	return (__vmci_memcpy_from_queue((uint8_t *)dest + dest_offset,
	    queue, queue_offset, size, false));
}

/*------------------------------------------------------------------------------
 *
 * vmci_memcpy_to_queue_v --
 *
 *     Copies from a given iovec from a VMCI Queue.
 *
 * Results:
 *     Zero on success, negative error code on failure.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

int
vmci_memcpy_to_queue_v(struct vmci_queue *queue, uint64_t queue_offset,
    const void *src, size_t src_offset, size_t size, int buf_type,
    bool can_block)
{

	ASSERT(can_block);

	/*
	 * We ignore src_offset because src is really a struct iovec * and will
	 * maintain offset internally.
	 */
	return (__vmci_memcpy_to_queue(queue, queue_offset, src, size,
	    true));
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_memcpy_from_queue_v --
 *
 *     Copies to a given iovec from a VMCI Queue.
 *
 * Results:
 *     Zero on success, negative error code on failure.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

int
vmci_memcpy_from_queue_v(void *dest, size_t dest_offset,
    const struct vmci_queue *queue, uint64_t queue_offset, size_t size,
    int buf_type, bool can_block)
{

	ASSERT(can_block);

	/*
	 * We ignore dest_offset because dest is really a struct iovec * and
	 * will maintain offset internally.
	 */
	return (__vmci_memcpy_from_queue(dest, queue, queue_offset, size,
	    true));
}

/*
 *------------------------------------------------------------------------------
 *
 * vmci_read_port_bytes --
 *
 *     Copy memory from an I/O port to kernel memory.
 *
 * Results:
 *     No results.
 *
 * Side effects:
 *     None.
 *
 *------------------------------------------------------------------------------
 */

void
vmci_read_port_bytes(vmci_io_handle handle, vmci_io_port port, uint8_t *buffer,
    size_t buffer_length)
{

	insb(port, buffer, buffer_length);
}