forked from Bareflank/MicroV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnttab.cpp
1424 lines (1172 loc) · 38.3 KB
/
gnttab.cpp
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
//
// Copyright (C) 2019 Assured Information Security, 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.
#include <hve/arch/intel_x64/vcpu.h>
#include <iommu/iommu.h>
#include <printv.h>
#include <xen/hvm.h>
#include <xen/gnttab.h>
#include <xen/domain.h>
#include <xen/vcpu.h>
#include <utility>
extern ::microv::intel_x64::vcpu *vcpu0;
namespace microv {
using atomic_hdr_t = volatile std::atomic<grant_entry_header_t>;
static_assert(sizeof(atomic_hdr_t) == 4);
static_assert(atomic_hdr_t::is_always_lock_free);
static_assert(std::is_standard_layout<grant_entry_header_t>::value);
struct gnttab_copy_operand {
uint8_t *buf{nullptr};
class xen_page *xpg{nullptr};
const gnttab_copy_t::gnttab_copy_ptr *copy_ptr{nullptr};
atomic_hdr_t *gte_hdr{nullptr};
bool is_src{false};
bool gfn_is_direct{false};
bool unmap_buf{false};
};
struct gnttab_map_operand {
xen_domid_t domid{DOMID_INVALID};
xen_domain *dom{nullptr};
xen_gnttab *gnt{nullptr};
};
struct gnttab_unmap_operand {
xen_domid_t domid{DOMID_INVALID};
xen_domain *dom{nullptr};
xen_gnttab *gnt{nullptr};
};
extern bool gfn_in_winpv_hole(uintptr_t gfn) noexcept;
/*
* mappable_gtf
*
* Check if the given GTF value indicates a mappable grant entry
* The GTF value is from the shared entry in the granter's table.
*
* @param gtf the flags to test
* @return true iff the gtf represents a mappable entry
*/
static inline bool mappable_gtf(const uint16_t gtf) noexcept
{
/* Only allow GTF_permit_access type */
if ((gtf & GTF_type_mask) != GTF_permit_access) {
return false;
}
return (gtf & (GTF_PWT | GTF_PCD | GTF_PAT | GTF_sub_page)) == 0;
}
/*
* supported_map_flags
*
* Check the given GNTMAP_* flags are supported by the current implementation
*
* @param gntmap the flags to test
* @return true iff the value is supported
*/
static inline bool supported_map_flags(const uint32_t gntmap) noexcept
{
constexpr auto host_rw = GNTMAP_host_map;
constexpr auto host_ro = GNTMAP_host_map | GNTMAP_readonly;
return gntmap == host_rw || gntmap == host_ro;
}
/*
* already_mapped
*
* Check if the given value indicates an entry that has already been mapped
* The GTF value is from the shared entry in the granter's table.
*
* @param gtf the flags to test
* @return true iff the gtf is already mapped
*/
static inline bool already_mapped(const uint16_t gtf) noexcept
{
return (gtf & (GTF_reading | GTF_writing)) != 0;
}
/*
* has_read_access
*
* Check if a domain has read access to the given grant entry
*
* @param domid the domain to check
* @param hdr the header of the shared grant entry to check
* @return true iff the domain has read access
*/
static inline bool has_read_access(xen_domid_t domid,
const grant_entry_header_t *hdr) noexcept
{
return (domid == hdr->domid) && ((hdr->flags & GTF_permit_access) != 0);
}
/*
* has_write_access
*
* Check if a domain has write access to the given grant entry
*
* @param domid the domain to check
* @param hdr the header of the shared grant entry to check
* @return true iff the domain has write access
*/
static inline bool has_write_access(xen_domid_t domid,
const grant_entry_header_t *hdr) noexcept
{
const bool access = hdr->flags & GTF_permit_access;
const bool readonly = hdr->flags & GTF_readonly;
return domid == hdr->domid && access && !readonly;
}
static inline xen_domain *get_dom(const xen_vcpu *curv,
xen_domid_t domid) noexcept
{
if (domid == DOMID_SELF || domid == curv->m_xen_dom->m_id) {
return curv->m_xen_dom;
}
if (domid == DOMID_ROOTVM) {
return vcpu0->dom()->xen_dom();
}
/* If the source domain isn't the current domain, take out a reference */
return get_xen_domain(domid);
}
static inline void put_dom(const xen_vcpu *curv, xen_domid_t domid) noexcept
{
if (domid == DOMID_SELF || domid == curv->m_xen_dom->m_id ||
domid == DOMID_ROOTVM) {
return;
}
put_xen_domain(domid);
}
static inline bool valid_map_arg(const gnttab_map_grant_ref_t *map) noexcept
{
if (!supported_map_flags(map->flags)) {
printv("%s: unsupported GNTMAP flags:0x%x\n", __func__, map->flags);
return false;
}
if ((map->ref & 0xFFFF0000U) != 0U) {
printv(
"%s: OOB ref %u would overflow map handle\n", __func__, map->ref);
return false;
}
return true;
}
static inline int get_map_handle(xen_vcpu *vcpu,
const gnttab_map_grant_ref_t *map,
grant_handle_t *new_hdl)
{
const uint32_t fref = map->ref;
const uint32_t fdom = static_cast<uint32_t>(map->dom) << 16;
const grant_handle_t hdl = fdom | fref;
auto gnt = vcpu->m_xen_dom->m_gnttab.get();
if (gnt->map_handles.count(hdl) != 0) {
printv("%s: handle 0x%x already mapped\n", __func__, hdl);
return GNTST_no_device_space;
}
*new_hdl = hdl;
return GNTST_okay;
}
/*
* ldomid is the domain invoking the GNTTABOP_map_grant_ref hypercall, and
* is wanting to map in memory from the foreign domain given by fdomid. This
* function is checking to make sure that fdom has granted ldom the frame
* with permissions appropriate for the map, and if so, we pin the frame by
* setting either GTF_reading or GTF_writing in the grant entry in fdom's
* grant table. fdom is not allowed to free the page as long as it is
* pinned. The unpin happens whenever ldom unmaps it via
* GNTTABOP_unmap_grant_ref.
*/
static int pin_granted_page(xen_vcpu *vcpu,
xen_gnttab *gnt,
const gnttab_map_grant_ref_t *map) noexcept
{
auto atomic_hdr =
reinterpret_cast<atomic_hdr_t *>(gnt->shared_header(map->ref));
auto hdr = atomic_hdr->load();
const auto map_rw = (map->flags & GNTMAP_readonly) == 0;
const auto pin_flags = GTF_reading | (map_rw ? GTF_writing : 0);
if (already_mapped(hdr.flags)) {
printv(
"%s: WARNING: attempted to remap entry: ref:%u dom:0x%x"
" oldflags:0x%x newflags:0x%x\n",
__func__,
map->ref,
map->dom,
hdr.flags,
hdr.flags | pin_flags);
return GNTST_general_error;
}
const auto ldomid = vcpu->m_xen_dom->m_id;
const auto fdomid = map->dom;
constexpr int retries = 4;
for (int i = 0; i < retries; i++) {
if (!mappable_gtf(hdr.flags)) {
printv("%s: invalid flags: gtf:0x%x ref:%u dom:0x%x\n",
__func__,
hdr.flags,
map->ref,
fdomid);
return GNTST_bad_gntref;
}
if (map_rw) {
if (!has_write_access(ldomid, &hdr)) {
printv(
"%s: dom 0x%x doesnt have write access to ref %u"
" in dom 0x%x",
__func__,
ldomid,
map->ref,
fdomid);
return GNTST_permission_denied;
}
} else if (!has_read_access(ldomid, &hdr)) {
printv(
"%s: dom 0x%x doesnt have read access to ref %u"
" in dom 0x%x",
__func__,
ldomid,
map->ref,
fdomid);
return GNTST_permission_denied;
}
grant_entry_header_t desire = hdr;
desire.flags |= pin_flags;
if (atomic_hdr->compare_exchange_strong(hdr, desire)) {
return GNTST_okay;
}
}
printv("%s: dom 0x%x ref %u is unstable\n", __func__, fdomid, map->ref);
return GNTST_general_error;
}
static inline void unpin_granted_page(grant_entry_header_t *gte_hdr) noexcept
{
constexpr uint32_t pins = GTF_reading | GTF_writing;
constexpr uint32_t clear_pins = ~pins;
auto hdr = reinterpret_cast<volatile std::atomic<uint32_t> *>(gte_hdr);
hdr->fetch_and(clear_pins);
}
static inline int map_foreign_frame(xen_vcpu *vcpu,
gnttab_map_grant_ref_t *map,
xen_page *fpg,
xen_pfn_t fgfn,
grant_handle_t map_handle)
{
auto lgpa = map->host_addr;
auto lgnt = vcpu->m_xen_dom->m_gnttab.get();
if (!lgnt->map_handles.try_emplace(map_handle, lgpa).second) {
printv("%s: failed to add map handle 0x%x for gpa 0x%lx",
__func__,
map_handle,
lgpa);
return GNTST_no_device_space;
}
map->handle = map_handle;
map->dev_bus_addr = 0;
auto perm = (map->flags & GNTMAP_readonly) ? pg_perm_r : pg_perm_rw;
auto lmem = vcpu->m_xen_dom->m_memory.get();
xen_pfn_t lgfn = xen_frame(lgpa);
if (fpg) {
lmem->add_foreign_page(lgfn, perm, pg_mtype_wb, fpg->page);
} else {
lmem->add_raw_page(lgfn, perm, pg_mtype_wb, fgfn);
}
return GNTST_okay;
}
static inline int unmap_foreign_frame(xen_domain *ldom,
uint64_t lgpa,
grant_handle_t map_handle)
{
const auto lgfn = xen_frame(lgpa);
if (auto rc = ldom->m_memory.get()->remove_page(lgfn, false); rc) {
printv("%s: failed to remove_page: gfn=0x%lx, rc=%d\n",
__func__,
lgfn,
rc);
return GNTST_general_error;
}
ldom->m_gnttab.get()->map_handles.erase(map_handle);
return GNTST_okay;
}
static void xen_gnttab_map_grant_ref(xen_vcpu *vcpu,
gnttab_map_grant_ref_t *map,
gnttab_map_operand *op)
{
if (!valid_map_arg(map)) {
map->status = GNTST_general_error;
return;
}
if (vcpu->m_xen_dom->m_id == DOMID_ROOTVM) {
expects(gfn_in_winpv_hole(xen_frame(map->host_addr)));
}
grant_handle_t new_hdl{};
int rc = get_map_handle(vcpu, map, &new_hdl);
if (rc != GNTST_okay) {
map->status = rc;
return;
}
auto fdom = op->dom;
auto fgnt = op->gnt;
xen_pfn_t fgfn;
xen_page *fpg{nullptr};
if (fgnt->invalid_ref(map->ref)) {
printv("%s: OOB ref:0x%x for dom:0x%x\n", __func__, map->ref, map->dom);
if (map->dom == DOMID_ROOTVM && map->ref == GNTTAB_RESERVED_XENSTORE) {
fgfn = fdom->m_hvm->get_param(HVM_PARAM_STORE_PFN);
fpg = fdom->m_memory.get()->find_page(fgfn);
expects(fpg);
goto map_frame;
}
rc = GNTST_bad_gntref;
goto out;
}
rc = pin_granted_page(vcpu, fgnt, map);
if (rc != GNTST_okay) {
goto out;
}
fgfn = fgnt->shared_gfn(map->ref);
fpg = fdom->m_memory.get()->find_page(fgfn);
if (!fpg) {
if (map->dom != DOMID_ROOTVM) {
printv("%s: gfn 0x%lx not mapped in dom 0x%x\n",
__func__,
fgfn,
map->dom);
rc = GNTST_general_error;
unpin_granted_page(fgnt->shared_header(map->ref));
goto out;
}
}
map_frame:
rc = map_foreign_frame(vcpu, map, fpg, fgfn, new_hdl);
if (rc != GNTST_okay) {
unpin_granted_page(fgnt->shared_header(map->ref));
}
out:
map->status = rc;
}
void xen_gnttab_unmap_grant_ref(xen_vcpu *vcpu,
gnttab_unmap_grant_ref_t *unmap,
gnttab_unmap_operand *op)
{
const grant_handle_t map_handle = unmap->handle;
const grant_ref_t fref = map_handle & 0xFFFF;
const xen_domid_t fdomid = op->domid;
const uint64_t lgpa = unmap->host_addr;
auto ldom = vcpu->m_xen_dom;
auto lgnt = ldom->m_gnttab.get();
auto itr = lgnt->map_handles.find(map_handle);
if (itr == lgnt->map_handles.end()) {
printv("%s: handle:%x not found\n", __func__, map_handle);
unmap->status = GNTST_bad_handle;
return;
} else if (itr->second != lgpa) {
printv("%s: handle.addr=0x%lx != unmap.gpa=0x%lx\n",
__func__,
itr->second,
lgpa);
unmap->status = GNTST_bad_virt_addr;
return;
}
auto fdom = op->dom;
auto fgnt = op->gnt;
if (fgnt->invalid_ref(fref)) {
printv("%s: bad fref:%u\n", __func__, fref);
if (fdomid == DOMID_ROOTVM && fref == GNTTAB_RESERVED_XENSTORE) {
goto unmap_frame;
}
unmap->status = GNTST_bad_handle;
return;
}
unpin_granted_page(fgnt->shared_header(fref));
unmap_frame:
unmap->status = unmap_foreign_frame(ldom, lgpa, map_handle);
}
/*
* map_copy_page
*
* Return virtual 4k-aligned address mapped rw to the host
* frame referenced by the xen_page argument.
*
* @param pg the xen_page to map
* @return the virtual address to access pg->page->hfn.
*/
static inline uint8_t *map_copy_page(const class xen_page *pg)
{
void *ptr = g_mm->alloc_map(UV_PAGE_SIZE);
g_cr3->map_4k(ptr, xen_addr(pg->page->hfn));
return reinterpret_cast<uint8_t *>(ptr);
}
/*
* unmap_copy_page
*
* Unmap the virtual address previously allocated with map_copy_page
*
* @param ptr the address previously returned from map_copy_page
*/
static inline void unmap_copy_page(uint8_t *ptr)
{
g_cr3->unmap(ptr);
g_mm->free_map(ptr);
}
static inline xen_domain *get_copy_dom(const xen_vcpu *curv,
xen_domid_t domid) noexcept
{
if (domid == DOMID_SELF || domid == curv->m_xen_dom->m_id) {
return curv->m_xen_dom;
}
if (domid == DOMID_ROOTVM) {
return vcpu0->dom()->xen_dom();
}
/* If the source domain isn't the current domain, take out a reference */
return get_xen_domain(domid);
}
static inline void put_copy_dom(const xen_vcpu *curv,
xen_domid_t domid) noexcept
{
if (domid == DOMID_SELF || domid == curv->m_xen_dom->m_id ||
domid == DOMID_ROOTVM) {
return;
}
put_xen_domain(domid);
}
static bool valid_copy_args(gnttab_copy_t *copy)
{
auto src = ©->source;
auto dst = ©->dest;
auto src_use_gfn = (copy->flags & GNTCOPY_source_gref) == 0;
auto dst_use_gfn = (copy->flags & GNTCOPY_dest_gref) == 0;
if (src_use_gfn && src->domid != DOMID_SELF) {
copy->status = GNTST_permission_denied;
printv("%s: src: only DOMID_SELF can use gfn-based copy", __func__);
return false;
}
if (dst_use_gfn && dst->domid != DOMID_SELF) {
printv("%s: dst: only DOMID_SELF can use gfn-based copy", __func__);
copy->status = GNTST_permission_denied;
return false;
}
if (src->offset + copy->len > XEN_PAGE_SIZE) {
printv("%s: src: offset(%u) + len(%u) > XEN_PAGE_SIZE(%lu)",
__func__,
src->offset,
copy->len,
XEN_PAGE_SIZE);
copy->status = GNTST_bad_copy_arg;
return false;
}
if (dst->offset + copy->len > XEN_PAGE_SIZE) {
printv("%s: dst: offset(%u) + len(%u) > XEN_PAGE_SIZE(%lu)",
__func__,
src->offset,
copy->len,
XEN_PAGE_SIZE);
copy->status = GNTST_bad_copy_arg;
return false;
}
return true;
}
static xen_page *winpv_xen_page() noexcept
{
static page winpv_pg{0};
static xen_page winpv_xen_pg{0, pg_perm_rw, pg_mtype_wb, &winpv_pg};
return &winpv_xen_pg;
}
static inline bool has_access(const gnttab_copy_operand *op,
xen_domid_t domid,
const grant_entry_header_t *hdr) noexcept
{
return op->is_src ? has_read_access(domid, hdr)
: has_write_access(domid, hdr);
}
static int get_copy_access(gnttab_copy_operand *op,
xen_domid_t domid,
xen_gnttab *gnt,
grant_ref_t ref)
{
auto atomic_hdr = reinterpret_cast<atomic_hdr_t *>(gnt->shared_header(ref));
grant_entry_header_t hdr = atomic_hdr->load();
/*
* If a prior xen_gnttab_map_grant_ref pinned the
* frame, we return without modifying any flags.
*/
if (already_mapped(hdr.flags)) {
if (!has_access(op, domid, &hdr)) {
printv(
"%s: ref %u already mapped but dom 0x%x doesnt have"
" %s access\n",
__func__,
ref,
domid,
op->is_src ? "read" : "write");
return GNTST_permission_denied;
}
return GNTST_okay;
}
constexpr int retries = 4;
const uint16_t desired_flags = op->is_src ? GTF_reading : GTF_writing;
grant_entry_header_t expect = hdr;
for (int i = 0; i < retries; i++) {
if (!has_access(op, domid, &expect)) {
printv("%s: dom 0x%x doesn't have %s access to ref %u\n",
__func__,
domid,
op->is_src ? "read" : "write",
ref);
return GNTST_permission_denied;
}
grant_entry_header_t desire = expect;
desire.flags |= desired_flags;
if (atomic_hdr->compare_exchange_strong(expect, desire)) {
op->gte_hdr = atomic_hdr;
return GNTST_okay;
}
}
printv("%s: grant entry %u is unstable\n", __func__, ref);
return GNTST_general_error;
}
static inline void put_copy_access(const gnttab_copy_operand *op) noexcept
{
constexpr uint32_t clear_read = ~((uint32_t)GTF_reading);
constexpr uint32_t clear_write = ~((uint32_t)GTF_writing);
const uint32_t mask = (op->is_src) ? clear_read : clear_write;
auto hdr = reinterpret_cast<volatile std::atomic<uint32_t> *>(op->gte_hdr);
hdr->fetch_and(mask);
}
static int get_copy_gfn(gnttab_copy_operand *op,
xen_domid_t current_domid,
xen_domain *dom,
xen_pfn_t *gfn)
{
auto ref = op->copy_ptr->u.ref;
auto gnt = dom->m_gnttab.get();
if (gnt->invalid_ref(ref)) {
printv("%s: bad %s ref(%u)\n",
__func__,
(op->is_src) ? "src" : "dst",
ref);
return GNTST_bad_gntref;
}
int rc = get_copy_access(op, current_domid, gnt, ref);
if (rc < 0) {
return rc;
}
*gfn = gnt->shared_gfn(ref);
return rc;
}
static inline void put_copy_gfn(gnttab_copy_operand *op) noexcept
{
if (!op->gte_hdr) {
return;
}
put_copy_access(op);
op->gte_hdr = nullptr;
}
static int get_copy_page(xen_domain *dom, xen_pfn_t gfn, xen_page **xpg)
{
xen_page *pg = dom->m_memory.get()->find_page(gfn);
if (pg) {
*xpg = pg;
return GNTST_okay;
}
if (dom->m_id != DOMID_ROOTVM) {
printv("%s: gfn 0x%lx doesnt map to page\n", __func__, gfn);
return GNTST_general_error;
}
pg = winpv_xen_page();
pg->page->ptr = nullptr;
pg->page->hfn = gfn;
pg->page->src = pg_src_root;
pg->gfn = gfn;
*xpg = pg;
return GNTST_okay;
}
static int get_copy_buf(gnttab_copy_operand *op)
{
xen_page *xpg = op->xpg;
expects(xpg->backed());
if (xpg->page->ptr) {
op->buf = reinterpret_cast<uint8_t *>(xpg->page->ptr);
return GNTST_okay;
}
op->buf = map_copy_page(xpg);
if (!op->buf) {
printv("%s: map_copy_page failed: gfn=0x%lx hfn=0x%lx\n",
__func__,
xpg->gfn,
xpg->page->hfn);
return GNTST_general_error;
}
op->unmap_buf = true;
return GNTST_okay;
}
static inline void put_copy_buf(gnttab_copy_operand *op)
{
if (op->unmap_buf) {
unmap_copy_page(op->buf);
op->unmap_buf = false;
}
}
static int get_copy_operand(xen_vcpu *vcpu, gnttab_copy_operand *op)
{
auto domid = op->copy_ptr->domid;
auto dom = get_copy_dom(vcpu, domid);
if (!dom) {
printv("%s: failed to get %s dom 0x%0x\n",
__func__,
(op->is_src) ? "src" : "dst",
domid);
return GNTST_bad_domain;
}
xen_pfn_t gfn{0};
if (op->gfn_is_direct) {
gfn = op->copy_ptr->u.gmfn;
} else {
int rc = get_copy_gfn(op, vcpu->m_xen_dom->m_id, dom, &gfn);
if (rc != GNTST_okay) {
put_copy_dom(vcpu, domid);
return rc;
}
}
int rc = get_copy_page(dom, gfn, &op->xpg);
if (rc != GNTST_okay) {
put_copy_gfn(op);
put_copy_dom(vcpu, domid);
return rc;
}
rc = get_copy_buf(op);
if (rc != GNTST_okay) {
put_copy_gfn(op);
put_copy_dom(vcpu, domid);
return rc;
}
return GNTST_okay;
}
static inline int get_copy_src_operand(xen_vcpu *vcpu,
const gnttab_copy_t *copy,
gnttab_copy_operand *op)
{
op->copy_ptr = ©->source;
op->is_src = true;
op->gfn_is_direct = (copy->flags & GNTCOPY_source_gref) == 0;
return get_copy_operand(vcpu, op);
}
static inline int get_copy_dst_operand(xen_vcpu *vcpu,
const gnttab_copy_t *copy,
gnttab_copy_operand *op)
{
op->copy_ptr = ©->dest;
op->is_src = false;
op->gfn_is_direct = (copy->flags & GNTCOPY_dest_gref) == 0;
return get_copy_operand(vcpu, op);
}
static inline void put_copy_operand(xen_vcpu *vcpu, gnttab_copy_operand *op)
{
put_copy_buf(op);
put_copy_gfn(op);
put_copy_dom(vcpu, op->copy_ptr->domid);
}
static void xen_gnttab_copy(xen_vcpu *vcpu, gnttab_copy_t *copy)
{
if (!valid_copy_args(copy)) {
return;
}
int rc{GNTST_okay};
gnttab_copy_operand src_op{};
gnttab_copy_operand dst_op{};
rc = get_copy_src_operand(vcpu, copy, &src_op);
if (rc != GNTST_okay) {
copy->status = rc;
return;
}
rc = get_copy_dst_operand(vcpu, copy, &dst_op);
if (rc != GNTST_okay) {
copy->status = rc;
put_copy_operand(vcpu, &src_op);
return;
}
uint8_t *src = src_op.buf + copy->source.offset;
uint8_t *dst = dst_op.buf + copy->dest.offset;
memcpy(dst, src, copy->len);
copy->status = rc;
put_copy_operand(vcpu, &dst_op);
put_copy_operand(vcpu, &src_op);
return;
}
bool xen_gnttab_copy(xen_vcpu *vcpu)
{
auto uvv = vcpu->m_uv_vcpu;
auto num = uvv->rdx();
auto map = uvv->map_gva_4k<gnttab_copy_t>(uvv->rsi(), num);
auto cop = map.get();
int rc;
int i;
for (i = 0; i < num; i++) {
xen_gnttab_copy(vcpu, &cop[i]);
rc = cop[i].status;
if (rc != GNTST_okay) {
printv("%s: op[%u] failed, rc=%d\n", __func__, i, rc);
break;
}
}
uvv->set_rax(rc);
return true;
}
bool xen_gnttab_map_grant_ref(xen_vcpu *vcpu)
{
auto uvv = vcpu->m_uv_vcpu;
auto num = uvv->rdx();
auto map = uvv->map_gva_4k<gnttab_map_grant_ref_t>(uvv->rsi(), num);
auto ops = map.get();
int rc;
int i;
struct gnttab_map_operand op {};
for (i = 0; i < num; i++) {
if (op.domid != ops[i].dom) {
if (op.dom) {
put_dom(vcpu, op.domid);
op.dom = nullptr;
op.gnt = nullptr;
}
op.domid = ops[i].dom;
op.dom = get_dom(vcpu, op.domid);
if (!op.dom) {
printv("%s: failed to get dom 0x%x\n", __func__, op.domid);
rc = GNTST_bad_domain;
break;
}
op.gnt = op.dom->m_gnttab.get();
}
xen_gnttab_map_grant_ref(vcpu, &ops[i], &op);
rc = ops[i].status;
if (rc != GNTST_okay) {
printv("%s: ERROR: op[%u] failed, rc=%d\n", __func__, i, rc);
break;
}
}
/*
* We dont need to invept here since the only modifications that
* occur are from not present -> present+access rights.
*
* The current IOMMU implementation also does not support CM,
* so we don't need to flush the IOTLB either.
*/
if (op.dom) {
put_dom(vcpu, op.domid);
}
uvv->set_rax(rc);
return true;
}
bool xen_gnttab_unmap_grant_ref(xen_vcpu *vcpu)
{
auto uvv = vcpu->m_uv_vcpu;
auto num = uvv->rdx();
auto map = uvv->map_gva_4k<gnttab_unmap_grant_ref_t>(uvv->rsi(), num);
auto ops = map.get();
int rc;
int i;
struct gnttab_unmap_operand op {};
for (i = 0; i < num; i++) {
xen_domid_t domid = static_cast<xen_domid_t>(ops[i].handle >> 16);
if (op.domid != domid) {
if (op.dom) {
put_dom(vcpu, op.domid);
op.dom = nullptr;
op.gnt = nullptr;
}
op.domid = domid;
op.dom = get_dom(vcpu, op.domid);
if (!op.dom) {
printv("%s: failed to get dom 0x%x\n", __func__, op.domid);
rc = GNTST_bad_domain;
break;
}
op.gnt = op.dom->m_gnttab.get();
}
xen_gnttab_unmap_grant_ref(vcpu, &ops[i], &op);
rc = ops[i].status;
if (rc != GNTST_okay) {
printv("%s: ERROR: op[%u] failed, rc=%d\n", __func__, i, rc);
break;
}
}
if (i > 0) {
vcpu->invept();
auto dom = vcpu->m_uv_dom;
for (auto iommu : dom->m_iommu_set) {
if (!iommu->psi_supported()) {
iommu->flush_iotlb_domain(dom);
continue;
}
for (auto p = 0; p < i; p++) {
iommu->flush_iotlb_page_range(
dom, ops[p].host_addr, UV_PAGE_SIZE);
}
}
}
if (op.dom) {
put_dom(vcpu, op.domid);
}
uvv->set_rax(rc);
return true;
}
bool xen_gnttab_query_size(xen_vcpu *vcpu)
{
auto uvv = vcpu->m_uv_vcpu;
/* Multiple query_size are unsupported ATM */
expects(uvv->rdx() == 1);