-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv4v.patch
More file actions
2892 lines (2862 loc) · 76.9 KB
/
Copy pathv4v.patch
File metadata and controls
2892 lines (2862 loc) · 76.9 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
diff --git a/xen/arch/x86/domain_page.c b/xen/arch/x86/domain_page.c
index 3903952..0c82348 100644
--- a/xen/arch/x86/domain_page.c
+++ b/xen/arch/x86/domain_page.c
@@ -157,6 +157,7 @@ void *map_domain_page(unsigned long mfn)
spin_unlock(&dcache->lock);
l1e_write(&MAPCACHE_L1ENT(idx), l1e_from_pfn(mfn, __PAGE_HYPERVISOR));
+ //printk(KERN_INFO "va: %#lx, START: %#lx, END:%#lx\n", MAPCACHE_VIRT_START + pfn_to_paddr(idx), MAPCACHE_VIRT_START, MAPCACHE_VIRT_END);
out:
local_irq_restore(flags);
@@ -175,6 +176,7 @@ void unmap_domain_page(const void *ptr)
return;
ASSERT(va >= MAPCACHE_VIRT_START && va < MAPCACHE_VIRT_END);
+ //printk(KERN_INFO "va: %#lx, START: %#lx, END:%#lx\n", va, MAPCACHE_VIRT_START, MAPCACHE_VIRT_END);
v = mapcache_current_vcpu();
ASSERT(v && is_pv_vcpu(v));
diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c
index ac05160..d6bf8cf 100644
--- a/xen/arch/x86/hvm/hvm.c
+++ b/xen/arch/x86/hvm/hvm.c
@@ -3569,7 +3569,8 @@ static hvm_hypercall_t *const hvm_hypercall64_table[NR_hypercalls] = {
HYPERCALL(hvm_op),
HYPERCALL(sysctl),
HYPERCALL(domctl),
- HYPERCALL(tmem_op)
+ HYPERCALL(tmem_op),
+ HYPERCALL(v4v_op)
};
#define COMPAT_CALL(x) \
@@ -3589,7 +3590,8 @@ static hvm_hypercall_t *const hvm_hypercall32_table[NR_hypercalls] = {
HYPERCALL(hvm_op),
HYPERCALL(sysctl),
HYPERCALL(domctl),
- HYPERCALL(tmem_op)
+ HYPERCALL(tmem_op),
+ HYPERCALL(v4v_op)
};
/* PVH 32bitfixme. */
diff --git a/xen/arch/x86/x86_64/compat/entry.S b/xen/arch/x86/x86_64/compat/entry.S
index 32b3bcc..5fb1199 100644
--- a/xen/arch/x86/x86_64/compat/entry.S
+++ b/xen/arch/x86/x86_64/compat/entry.S
@@ -416,6 +416,7 @@ ENTRY(compat_hypercall_table)
.quad do_domctl
.quad compat_kexec_op
.quad do_tmem_op
+ .quad do_v4v_op
.rept __HYPERVISOR_arch_0-((.-compat_hypercall_table)/8)
.quad compat_ni_hypercall
.endr
@@ -464,6 +465,7 @@ ENTRY(compat_hypercall_args_table)
.byte 1 /* do_domctl */
.byte 2 /* compat_kexec_op */
.byte 1 /* do_tmem_op */
+ .byte 5 /* do_v4v_op */
.rept __HYPERVISOR_arch_0-(.-compat_hypercall_args_table)
.byte 0 /* compat_ni_hypercall */
.endr
diff --git a/xen/arch/x86/x86_64/entry.S b/xen/arch/x86/x86_64/entry.S
index 3ea4683..5eb1def 100644
--- a/xen/arch/x86/x86_64/entry.S
+++ b/xen/arch/x86/x86_64/entry.S
@@ -757,6 +757,7 @@ ENTRY(hypercall_table)
.quad do_domctl
.quad do_kexec_op
.quad do_tmem_op
+ .quad do_v4v_op
.rept __HYPERVISOR_arch_0-((.-hypercall_table)/8)
.quad do_ni_hypercall
.endr
@@ -805,6 +806,7 @@ ENTRY(hypercall_args_table)
.byte 1 /* do_domctl */
.byte 2 /* do_kexec */
.byte 1 /* do_tmem_op */
+ .byte 5 /* do_v4v_op */
.rept __HYPERVISOR_arch_0-(.-hypercall_args_table)
.byte 0 /* do_ni_hypercall */
.endr
diff --git a/xen/common/Makefile b/xen/common/Makefile
index 3683ae3..41e162d 100644
--- a/xen/common/Makefile
+++ b/xen/common/Makefile
@@ -51,6 +51,7 @@ obj-y += tmem_xen.o
obj-y += radix-tree.o
obj-y += rbtree.o
obj-y += lzo.o
+obj-y += v4v.o
obj-bin-$(CONFIG_X86) += $(foreach n,decompress bunzip2 unxz unlzma unlzo unlz4 earlycpio,$(n).init.o)
diff --git a/xen/common/domain.c b/xen/common/domain.c
index 4291e29..df7c615 100644
--- a/xen/common/domain.c
+++ b/xen/common/domain.c
@@ -244,7 +244,8 @@ struct domain *domain_create(
{
struct domain *d, **pd, *old_hwdom = NULL;
enum { INIT_xsm = 1u<<0, INIT_watchdog = 1u<<1, INIT_rangeset = 1u<<2,
- INIT_evtchn = 1u<<3, INIT_gnttab = 1u<<4, INIT_arch = 1u<<5 };
+ INIT_evtchn = 1u<<3, INIT_gnttab = 1u<<4, INIT_arch = 1u<<5,
+ INIT_v4v = 1u<<6 };
int err, init_status = 0;
int poolid = CPUPOOLID_NONE;
@@ -372,6 +373,13 @@ struct domain *domain_create(
spin_unlock(&domlist_update_lock);
}
+ if ( !is_idle_domain(d) )
+ {
+ if ( v4v_init(d) != 0 )
+ goto fail;
+ init_status |= INIT_v4v;
+ }
+
return d;
fail:
@@ -383,6 +391,8 @@ struct domain *domain_create(
xfree(d->pbuf);
if ( init_status & INIT_arch )
arch_domain_destroy(d);
+ if ( init_status & INIT_v4v )
+ v4v_destroy(d);
if ( init_status & INIT_gnttab )
grant_table_destroy(d);
if ( init_status & INIT_evtchn )
@@ -503,6 +513,7 @@ struct domain *get_domain_by_id(domid_t dom)
rcu_read_unlock(&domlist_read_lock);
+ //printk(KERN_INFO "%s: %#lx @ %#lx\n", __func__, (unsigned long)dom, (unsigned long)d);
return d;
}
@@ -580,6 +591,7 @@ int domain_kill(struct domain *d)
domain_pause(d);
d->is_dying = DOMDYING_dying;
spin_barrier(&d->domain_lock);
+ v4v_destroy(d);
evtchn_destroy(d);
gnttab_release_mappings(d);
tmem_destroy(d->tmem_client);
diff --git a/xen/common/event_channel.c b/xen/common/event_channel.c
index 6853842..6cbabf6 100644
--- a/xen/common/event_channel.c
+++ b/xen/common/event_channel.c
@@ -52,6 +52,14 @@
#define consumer_is_xen(e) (!!(e)->xen_consumer)
+/*jo : modification*/
+#define v4v_dprintk(format, args...) \
+ do { \
+ printk("%s:%d " format, \
+ __FILE__, __LINE__, ## args ); \
+ } while ( 1 == 0 )
+
+
/*
* The function alloc_unbound_xen_event_channel() allows an arbitrary
* notifier function to be specified. However, very few unique functions
@@ -197,37 +205,54 @@ static int get_free_port(struct domain *d)
static long evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)
{
- struct evtchn *chn;
- struct domain *d;
- int port;
- domid_t dom = alloc->dom;
+ struct domain *d ;
long rc;
- d = rcu_lock_domain_by_any_id(dom);
+ d = rcu_lock_domain_by_any_id(alloc->dom);
if ( d == NULL )
return -ESRCH;
- spin_lock(&d->event_lock);
+ rc = evtchn_alloc_unbound_domain(d, &alloc->port, alloc->remote_dom, 1);
+ if ( rc )
+ ERROR_EXIT_DOM((int)rc, d);
- if ( (port = get_free_port(d)) < 0 )
- ERROR_EXIT_DOM(port, d);
- chn = evtchn_from_port(d, port);
+ out:
+ rcu_unlock_domain(d);
- rc = xsm_evtchn_unbound(XSM_TARGET, d, chn, alloc->remote_dom);
- if ( rc )
+ return rc;
+}
+
+int evtchn_alloc_unbound_domain(struct domain *d, evtchn_port_t *port,
+ domid_t remote_domid, bool_t call_xsm)
+{
+ struct evtchn *chn;
+ int rc;
+ int free_port;
+
+ spin_lock(&d->event_lock);
+
+ rc = free_port = get_free_port(d);
+ if ( free_port < 0 )
goto out;
+ chn = evtchn_from_port(d, free_port);
+ if ( call_xsm ) {
+ rc = xsm_evtchn_unbound(XSM_TARGET, d, chn, remote_domid);
+ if ( rc )
+ goto out;
+ }
+
chn->state = ECS_UNBOUND;
- if ( (chn->u.unbound.remote_domid = alloc->remote_dom) == DOMID_SELF )
+ if ( (chn->u.unbound.remote_domid = remote_domid) == DOMID_SELF )
chn->u.unbound.remote_domid = current->domain->domain_id;
evtchn_port_init(d, chn);
- alloc->port = port;
+ *port = free_port;
+ /* Everything is fine, returns 0 */
+ rc = 0;
out:
spin_unlock(&d->event_lock);
- rcu_unlock_domain(d);
-
return rc;
}
@@ -630,6 +655,7 @@ int evtchn_send(struct domain *d, unsigned int lport)
switch ( lchn->state )
{
case ECS_INTERDOMAIN:
+ //v4v_dprintk("evt_chn_send : INTERDOMAIN\n");
rd = lchn->u.interdomain.remote_dom;
rport = lchn->u.interdomain.remote_port;
rchn = evtchn_from_port(rd, rport);
@@ -640,12 +666,15 @@ int evtchn_send(struct domain *d, unsigned int lport)
evtchn_set_pending(rvcpu, rport);
break;
case ECS_IPI:
+ //v4v_dprintk("evtchn_send : ECS_IPI\n");
evtchn_set_pending(ld->vcpu[lchn->notify_vcpu_id], lport);
break;
case ECS_UNBOUND:
+ //v4v_dprintk("evtchn_send : UNBOUND\n");
/* silently drop the notification */
break;
default:
+ //v4v_dprintk("evtchn_send : default\n");
ret = -EINVAL;
}
diff --git a/xen/common/v4v.c b/xen/common/v4v.c
new file mode 100644
index 0000000..b7f9067
--- /dev/null
+++ b/xen/common/v4v.c
@@ -0,0 +1,2206 @@
+/******************************************************************************
+ * V4V
+ *
+ * Version 2 of v2v (Virtual-to-Virtual)
+ *
+ * Copyright (c) 2010, Citrix Systems
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <xen/config.h>
+#include <xen/mm.h>
+#include <xen/compat.h>
+#include <xen/init.h>
+#include <xen/lib.h>
+#include <xen/errno.h>
+#include <xen/sched.h>
+#include <xen/domain.h>
+#include <xen/v4v.h>
+#include <xen/event.h>
+#include <xen/guest_access.h>
+#include <asm/paging.h>
+#include <asm/p2m.h>
+#include <xen/keyhandler.h>
+#include <asm/types.h>
+
+DEFINE_XEN_GUEST_HANDLE(v4v_iov_t);
+DEFINE_XEN_GUEST_HANDLE(v4v_addr_t);
+DEFINE_XEN_GUEST_HANDLE(v4v_send_addr_t);
+DEFINE_XEN_GUEST_HANDLE(v4v_ring_t);
+DEFINE_XEN_GUEST_HANDLE(v4v_ring_data_ent_t);
+DEFINE_XEN_GUEST_HANDLE(v4v_ring_data_t);
+DEFINE_XEN_GUEST_HANDLE(v4v_info_t);
+DEFINE_XEN_GUEST_HANDLE(v4v_pfn_t);
+DEFINE_XEN_GUEST_HANDLE(v4vtables_rule_t);
+DEFINE_XEN_GUEST_HANDLE(v4vtables_list_t);
+DEFINE_XEN_GUEST_HANDLE(uint8_t);
+
+struct v4v_pending_ent
+{
+ struct hlist_node node;
+ domid_t id;
+ uint32_t len;
+};
+
+typedef struct v4vtables_rule_node
+{
+ struct list_head list;
+ v4vtables_rule_t rule;
+} v4vtables_rule_node_t;
+
+struct v4v_ring_info
+{
+ /* next node in the hash, protected by L2 */
+ struct hlist_node node;
+ /* this ring's id, protected by L2 */
+ v4v_ring_id_t id;
+ /* L3 */
+ spinlock_t lock;
+ /* cached length of the ring (from ring->len), protected by L3 */
+ uint32_t len;
+ uint32_t npage;
+ /* cached tx pointer location, protected by L3 */
+ uint32_t tx_ptr;
+ /* guest ring, protected by L3 */
+ XEN_GUEST_HANDLE(v4v_ring_t) ring;
+ /* mapped ring pages protected by L3*/
+ uint8_t **mfn_mapping;
+ /* list of mfns of guest ring */
+ mfn_t *mfns;
+ /* list of struct v4v_pending_ent for this ring, L3 */
+ struct hlist_head pending;
+};
+
+/*
+ * The value of the v4v element in a struct domain is
+ * protected by the global lock L1
+ */
+#define V4V_HTABLE_SIZE 32
+struct v4v_domain
+{
+ /* L2 */
+ rwlock_t lock;
+ /* event channel */
+ evtchn_port_t evtchn_port;
+ /* protected by L2 */
+ struct hlist_head ring_hash[V4V_HTABLE_SIZE];
+};
+
+/*
+ * Messages on the ring are padded to 128 bits
+ * Len here refers to the exact length of the data not including the
+ * 128 bit header. The message uses
+ * ((len +0xf) & ~0xf) + sizeof(v4v_ring_message_header) bytes
+ */
+#define V4V_ROUNDUP(a) (((a) +0xf ) & ~0xf)
+
+/*
+ * Helper functions
+ */
+
+static inline uint16_t
+v4v_hash_fn(v4v_ring_id_t *id)
+{
+ uint16_t ret;
+ ret = (uint16_t)(id->addr.port >> 16);
+ ret ^= (uint16_t)id->addr.port;
+ ret ^= id->addr.domain;
+ //ret ^= id->partner;
+
+ ret &= (V4V_HTABLE_SIZE - 1);
+
+ return ret;
+}
+
+static struct v4v_ring_info *v4v_ring_find_info(struct domain *d,
+ v4v_ring_id_t *id);
+
+static struct v4v_ring_info *v4v_ring_find_info_by_addr(struct domain *d,
+ struct v4v_addr *a,
+ domid_t p);
+
+struct list_head v4vtables_rules = LIST_HEAD_INIT(v4vtables_rules);
+
+/*
+ * locks
+ */
+
+/*
+ * locking is organized as follows:
+ *
+ * the global lock v4v_lock: L1 protects the v4v elements
+ * of all struct domain *d in the system, it does not
+ * protect any of the elements of d->v4v, just their
+ * addresses. By extension since the destruction of
+ * a domain with a non-NULL d->v4v will need to free
+ * the d->v4v pointer, holding this lock gauruntees
+ * that no domains pointers in which v4v is interested
+ * become invalid whilst this lock is held.
+ */
+
+static DEFINE_RWLOCK(v4v_lock); /* L1 */
+
+/*
+ * the lock d->v4v->lock: L2: Read on protects the hash table and
+ * the elements in the hash_table d->v4v->ring_hash, and
+ * the node and id fields in struct v4v_ring_info in the
+ * hash table. Write on L2 protects all of the elements of
+ * struct v4v_ring_info. To take L2 you must already have R(L1)
+ * W(L1) implies W(L2) and L3
+ *
+ * the lock v4v_ring_info *ringinfo; ringinfo->lock: L3:
+ * protects len,tx_ptr the guest ring, the
+ * guest ring_data and the pending list. To take L3 you must
+ * already have R(L2). W(L2) implies L3
+ */
+
+/*
+ * lock to protect the filtering rules list: v4vtable_rules
+ *
+ * The write lock is held for viptables_del and viptables_add
+ * The read lock is held for viptable_list
+ */
+static DEFINE_RWLOCK(v4vtables_rules_lock);
+
+
+
+//#define V4V_ANANOS_DEBUG
+//#define V4V_DEBUG
+//#define V4V_EXTRA_DEBUG
+/*
+ * Debugs
+ */
+#ifdef V4V_ANANOS_DEBUG
+#define v4v_aprintk(format, args...) \
+ do { \
+ printk("%d,D%d:%s " format, \
+ __LINE__, current->domain->domain_id, __func__, ## args ); \
+ } while ( 1 == 0 )
+#else
+#define v4v_aprintk(format, ... ) (void)0
+#endif
+
+#ifdef V4V_EXTRA_DEBUG
+#define v4v_ddprintk(format, args...) v4v_aprintk(format, ## args)
+#else
+#define v4v_ddprintk(format, ... ) (void)0
+#endif
+
+
+#ifdef V4V_DEBUG
+#define v4v_dprintk(format, args...) \
+ do { \
+ printk("%s:%d,DOM:%d:%s " format, \
+ __FILE__, __LINE__, current->domain->domain_id, __func__, ## args ); \
+ } while ( 1 == 0 )
+#else
+#define v4v_dprintk(format, ... ) (void)0
+#endif
+
+#define v4v_dprintk_in() v4v_ddprintk("enter\n")
+#define v4v_dprintk_out() v4v_ddprintk("exit\n")
+
+//#ifdef V4V_DEBUG
+static void __attribute__((unused))
+v4v_hexdump(void *_p, int len)
+{
+ uint8_t *buf = (uint8_t *)_p;
+ int i, j;
+
+ for ( i = 0; i < len; i += 16 )
+ {
+ printk(KERN_ERR "%p:", &buf[i]);
+ for ( j = 0; j < 16; ++j )
+ {
+ int k = i + j;
+ if ( k < len )
+ printk(" %02x", buf[k]);
+ else
+ printk(" ");
+ }
+ printk(" ");
+
+ for ( j = 0; j < 16; ++j )
+ {
+ int k = i + j;
+ if ( k < len )
+ printk("%c", ((buf[k] > 32) && (buf[k] < 127)) ? buf[k] : '.');
+ else
+ printk(" ");
+ }
+ printk("\n");
+ }
+}
+//#endif
+
+
+/*
+ * Event channel
+ */
+
+static void
+v4v_signal_domain(struct domain *d)
+{
+ int ret = 234;
+ v4v_dprintk_in();
+ //v4v_dprintk("send guest VIRQ_V4V domid:%d\n", d->domain_id);
+
+ ret = evtchn_send(d, d->v4v->evtchn_port);
+ //v4v_dprintk("ret = %d\n", ret);
+ v4v_dprintk_out();
+}
+
+static void
+v4v_signal_domid(domid_t id)
+{
+ struct domain *d = get_domain_by_id(id);
+ v4v_dprintk_in();
+ if ( !d )
+ return;
+ v4v_signal_domain(d);
+ put_domain(d);
+ v4v_dprintk_out();
+}
+
+
+/*
+ * ring buffer
+ */
+
+static void
+v4v_ring_unmap(struct v4v_ring_info *ring_info)
+{
+ int i;
+
+ v4v_dprintk_in();
+ ASSERT(spin_is_locked(&ring_info->lock));
+
+ for ( i = 0; i < ring_info->npage; ++i )
+ {
+ if ( !ring_info->mfn_mapping[i] )
+ continue;
+ //v4v_dprintk("unmapping page %p from %p\n",
+ // (void*)mfn_x(ring_info->mfns[i]),
+ // ring_info->mfn_mapping[i]);
+
+ unmap_domain_page(ring_info->mfn_mapping[i]);
+ ring_info->mfn_mapping[i] = NULL;
+ }
+
+ v4v_dprintk_out();
+}
+
+static uint8_t *
+v4v_ring_map_page(struct v4v_ring_info *ring_info, int i)
+{
+ v4v_dprintk_in();
+ ASSERT(spin_is_locked(&ring_info->lock));
+
+ if ( i >= ring_info->npage ) {
+ printk("%s: %d\n", __func__, i);
+ return NULL;
+ }
+ if ( ring_info->mfn_mapping[i] )
+ return ring_info->mfn_mapping[i];
+ ring_info->mfn_mapping[i] = map_domain_page(mfn_x(ring_info->mfns[i]));
+
+ v4v_dprintk("mapping page %p to %p\n",
+ (void *)mfn_x(ring_info->mfns[i]),
+ ring_info->mfn_mapping[i]);
+ v4v_dprintk_out();
+ return ring_info->mfn_mapping[i];
+}
+
+static int
+v4v_memcpy_from_guest_ring(void *_dst, struct v4v_ring_info *ring_info,
+ uint32_t offset, uint32_t len)
+{
+ int page = offset >> PAGE_SHIFT;
+
+ uint8_t *src;
+ uint8_t *dst = _dst;
+
+ v4v_dprintk_in();
+ ASSERT(spin_is_locked(&ring_info->lock));
+ offset &= PAGE_SIZE - 1;
+
+ while ( (offset + len) > PAGE_SIZE )
+ {
+ src = v4v_ring_map_page(ring_info, page);
+
+ if ( !src )
+ return -EFAULT;
+
+ v4v_aprintk("dst:%p, src:%p, offset:%#x, len:%#lx\n", dst, src, offset, PAGE_SIZE-offset);
+ memcpy(dst, src + offset, PAGE_SIZE - offset);
+
+ /*jo mod*/
+ unmap_domain_page(ring_info->mfn_mapping[page]);
+ ring_info->mfn_mapping[page] = NULL;
+ /*jo mod end*/
+ page++;
+ page = page % (int)ring_info->npage;
+ len -= PAGE_SIZE - offset;
+ dst += PAGE_SIZE - offset;
+ offset = 0;
+ }
+
+ src = v4v_ring_map_page(ring_info, page);
+ if ( !src )
+ return -EFAULT;
+
+ v4v_aprintk("dst:%p, src:%p, offset:%#x, len:%#x\n", dst, src, offset, len);
+ memcpy(dst, src + offset, len);
+ /*jo mod*/
+ unmap_domain_page(ring_info->mfn_mapping[page]);
+ ring_info->mfn_mapping[page] = NULL;
+ /*jo mod end*/
+
+ v4v_dprintk_out();
+ return 0;
+}
+
+/*jo magic*/
+#ifdef JO_MAGIC
+static int
+v4v_update_rx_ptr(struct v4v_ring_info *ring_info, uint32_t rx_ptr)
+{
+ uint8_t *dst = v4v_ring_map_page(ring_info, 0);
+ uint32_t *p = (uint32_t *)(dst + offsetof(v4v_ring_t, rx_ptr));
+ int ret = 0;
+
+ v4v_dprintk_in();
+ ASSERT(spin_is_locked(&ring_info->lock));
+
+ if ( !dst ) {
+ //v4v_dprintk("%s: PROBLHMA !dist\n",__func__);
+ ret = -EFAULT;
+ goto out;
+ }
+ write_atomic(p, rx_ptr);
+ wmb();
+
+out:
+ v4v_dprintk_out();
+ return ret;
+}
+#endif
+
+static int
+v4v_update_tx_ptr(struct v4v_ring_info *ring_info, uint32_t tx_ptr)
+{
+ uint8_t *dst = v4v_ring_map_page(ring_info, 0);
+ uint32_t *p = (uint32_t *)(dst + offsetof(v4v_ring_t, tx_ptr));
+ int ret = 0;
+
+ v4v_dprintk_in();
+ ASSERT(spin_is_locked(&ring_info->lock));
+
+ if ( !dst ) {
+ printk(KERN_ERR "%s: PROBLHMA !dist\n",__func__);
+ ret = -EFAULT;
+ goto out;
+ }
+ write_atomic(p, tx_ptr);
+ wmb();
+
+out:
+ v4v_dprintk_out();
+ return ret;
+}
+
+static int
+v4v_copy_from_guest_maybe(void *dst, void *src,
+ XEN_GUEST_HANDLE(uint8_t) src_hnd,
+ uint32_t len)
+{
+ int rc = 0;
+
+ v4v_dprintk_in();
+ if ( src )
+ memcpy(dst, src, len);
+ else
+ rc = __copy_from_guest(dst, src_hnd, len);
+
+ v4v_aprintk("rc:%d, dst:%p, src:%p, src_hnd:%p, len:%#x\n", rc, dst, src, src_hnd.p, len);
+
+ v4v_dprintk_out();
+ return rc;
+}
+
+static int
+v4v_memcpy_to_guest_ring(struct v4v_ring_info *ring_info,
+ uint32_t offset,
+ void *src,
+ XEN_GUEST_HANDLE(uint8_t) src_hnd,
+ uint32_t len)
+{
+ int page = offset >> PAGE_SHIFT;
+ uint8_t *dst;
+ int ret = 0;
+
+ v4v_dprintk_in();
+ ASSERT(spin_is_locked(&ring_info->lock));
+
+ //page = page % (int)ring_info->npage;
+ offset &= PAGE_SIZE - 1;
+ while ( (offset + len) > PAGE_SIZE ) {
+ v4v_aprintk("tx_ptr:%#x, offset+len: %#x\n", ring_info->tx_ptr, offset+len);
+ dst = v4v_ring_map_page(ring_info, page);
+ if ( !dst ) {
+ printk(KERN_ERR "%s: PROBLHMA !dist, page=%d, %d, %d\n", __func__, page, PAGE_SHIFT, (int)offset);
+ ret = -EFAULT;
+ goto out;
+ }
+ //if (!page && !offset)
+ // offset+=sizeof(v4v_ring_t);
+
+ v4v_aprintk("dst:%p, src:%p, src_hnd:%p, offset:%#x, len:%#x\n", dst, src, src_hnd.p, offset, (unsigned)PAGE_SIZE - offset);
+ if ( v4v_copy_from_guest_maybe(dst + offset, src, src_hnd,
+ PAGE_SIZE - offset) ) {
+ printk(KERN_ERR "%s: PROBLHMA copy_from_guest_maybe\n", __func__);
+ ret = -EFAULT;
+ goto out;
+ }
+ /*jo mod*/
+ unmap_domain_page(ring_info->mfn_mapping[page]);
+ ring_info->mfn_mapping[page] = NULL;
+ /*jo mod end*/
+ page++;
+ //page = page % (int)ring_info->npage;
+ len -= PAGE_SIZE - offset;
+ if ( src )
+ src += (PAGE_SIZE - offset);
+ else
+ guest_handle_add_offset(src_hnd, PAGE_SIZE - offset);
+ offset = 0;
+ }
+
+ dst = v4v_ring_map_page(ring_info, page);
+ if ( !dst ) {
+ printk(KERN_ERR "%s: PROBLHMA !dist after ring_map_page\n",__func__);
+ ret = -EFAULT;
+ goto out;
+ }
+ // if (!page && !offset) {
+ // offset+=sizeof(v4v_ring_t);
+ // }
+ v4v_aprintk("dst:%p, src:%p, src_hnd:%p, offset:%#x, len:%#x\n", dst, src, src_hnd.p, offset, len);
+ if ( v4v_copy_from_guest_maybe(dst + offset, src, src_hnd, len) ) {
+ printk(KERN_ERR "%s: PROBLHMA from_guet_maybe after ring_map_page\n",__func__);
+ ret = -EFAULT;
+ goto out;
+ }
+ if (len < 0x100) {
+ //v4v_hexdump(dst+offset, len);
+ }
+ /*jo mod*/
+ unmap_domain_page(ring_info->mfn_mapping[page]);
+ ring_info->mfn_mapping[page] = NULL;
+ /*jo mod end*/
+
+out:
+ v4v_dprintk_out();
+ return ret;
+}
+
+static int
+v4v_ringbuf_get_rx_ptr(struct domain *d, struct v4v_ring_info *ring_info,
+ uint32_t * rx_ptr)
+{
+ v4v_ring_t *ringp;
+ int ret = 0;
+
+ v4v_dprintk_in();
+ if ( ring_info->npage == 0 ) {
+ ret = -1;
+ goto out;
+ }
+
+ ringp = map_domain_page(mfn_x(ring_info->mfns[0]));
+
+ if ( !ringp ) {
+ ret = -1;
+ goto out;
+ }
+
+ write_atomic(rx_ptr, ringp->rx_ptr);
+ wmb();
+
+ unmap_domain_page(ringp);
+out:
+ v4v_dprintk_out();
+ return ret;
+}
+
+uint32_t
+v4v_ringbuf_payload_space(struct domain * d, struct v4v_ring_info * ring_info)
+{
+ v4v_ring_t ring;
+ int32_t ret;
+
+ v4v_dprintk_in();
+ ring.tx_ptr = ring_info->tx_ptr;
+ ring.len = ring_info->len;
+
+ if ( v4v_ringbuf_get_rx_ptr(d, ring_info, &ring.rx_ptr) ) {
+ ret = 0;
+ goto out;
+ }
+
+ if ( ring.rx_ptr == ring.tx_ptr ) {
+ ret = ring.len - sizeof (struct v4v_ring_message_header);
+ goto out;
+ }
+
+ ret = ring.rx_ptr - ring.tx_ptr;
+ if ( ret < 0 )
+ ret += ring.len;
+
+ ret -= sizeof (struct v4v_ring_message_header);
+ ret -= V4V_ROUNDUP(1);
+
+out:
+ v4v_dprintk_out();
+ return (ret < 0) ? 0 : ret;
+}
+
+static long
+v4v_iov_count(XEN_GUEST_HANDLE(v4v_iov_t) iovs, int niov)
+{
+ v4v_iov_t iov;
+ size_t ret = 0, niovs = 0;
+
+ v4v_dprintk_in();
+ while ( niov-- )
+ {
+ if ( (ret = copy_from_guest(&iov, iovs, 1)) ) {
+ printk(KERN_ERR "%s: failed, ret= %d\n", __func__, (int)ret);
+ ret = -EFAULT;
+ goto out;
+ }
+ ret = 0;
+
+ niovs += iov.iov_len;
+ ret = niovs;
+
+ /* message bigger than 2G can't be sent */
+ if (niovs > 2L * 1024 * 1024 * 1024) {
+ ret = -EMSGSIZE;
+ goto out;
+ }
+
+ guest_handle_add_offset(iovs, 1);
+ }
+
+out:
+ v4v_dprintk_out();
+ return ret;
+}
+
+static long
+v4v_ringbuf_insertv(struct domain *d,
+ struct v4v_ring_info *ring_info,
+ v4v_ring_id_t *src_id, uint32_t proto,
+ XEN_GUEST_HANDLE(v4v_iov_t) iovs, uint32_t niov,
+ size_t len)
+{
+ v4v_ring_t ring;
+ struct v4v_ring_message_header mh = { 0 };
+ int32_t sp;
+ long happy_ret;
+ int32_t ret = 0;
+ XEN_GUEST_HANDLE(uint8_t) empty_hnd = { 0 };
+
+ ASSERT(spin_is_locked(&ring_info->lock));
+
+ v4v_dprintk_in();
+ happy_ret = len;
+
+ v4v_aprintk("rounduplen: %#lx, sizeof(ring_msg_hdr):%#lx, ring_info->len:%#x\n",
+ V4V_ROUNDUP(len), sizeof (struct v4v_ring_message_header), ring_info->len);
+
+ if ( (V4V_ROUNDUP(len) + sizeof (struct v4v_ring_message_header) ) >=
+ ring_info->len) {
+ ret = -EMSGSIZE;
+ goto out;
+ }
+
+ do
+ {
+ v4v_aprintk("ring_info: %p, ring:%p, sizeof(ring):%#lx\n",
+ ring_info, &ring, sizeof(ring));
+ if ( (ret = v4v_memcpy_from_guest_ring(&ring, ring_info, 0,
+ sizeof (ring))) )
+ break;
+
+ ring.tx_ptr = ring_info->tx_ptr;
+ ring.len = ring_info->len;
+
+ v4v_aprintk("ring->tx_ptr: %#x, ring->rx_ptr:%#x\n", ring.tx_ptr, ring.rx_ptr);
+ if ( ring.rx_ptr == ring.tx_ptr ) {
+ sp = ring_info->len;
+#ifdef JO_MAGIC
+ /*jo magic*/
+ if ( ring.tx_ptr != 0 ) {
+ ring.rx_ptr = 0;
+ ring.tx_ptr = 0;
+ ring_info->tx_ptr = 0;
+ if( (ret = v4v_update_tx_ptr(ring_info, ring.tx_ptr)) ) {
+ printk("%s : failed to update tx_ptr\n",__func__);
+ v4v_ring_unmap(ring_info);
+ goto out;
+ }
+ if( (ret = v4v_update_rx_ptr(ring_info, ring.rx_ptr)) ) {
+ v4v_dprintk("%s : failed to update rx_ptr\n",__func__);
+ v4v_ring_unmap(ring_info);
+ goto out;
+ }
+ v4v_dprintk("%s : rx_ptr, tx_ptr modified!!!\n", __func__);
+ }
+ /*jo magic end*/
+#endif
+ }
+ else
+ {
+ sp = ring.rx_ptr - ring.tx_ptr;
+ if ( sp < 0 )
+ sp += ring.len;
+ }
+ v4v_aprintk("space:%#x\n", sp);
+
+
+ if ( (V4V_ROUNDUP(len) + sizeof (struct v4v_ring_message_header)) >= sp )
+ {
+ //printk(KERN_ERR "%s: EAGAIN len+header:%#lx\n", __func__, (V4V_ROUNDUP(len) + sizeof (struct v4v_ring_message_header)));
+ ret = -EAGAIN;
+ break;
+ }
+
+ mh.len = len + sizeof (struct v4v_ring_message_header);
+ mh.source = src_id->addr;
+ mh.message_type = proto;
+
+ v4v_aprintk("ring->tx_ptr: %#x\n", ring.tx_ptr);
+ if ( (ret = v4v_memcpy_to_guest_ring(ring_info,
+ ring.tx_ptr + sizeof (v4v_ring_t),
+ &mh, empty_hnd,
+ sizeof (mh))) )
+ break;
+
+ ring.tx_ptr += sizeof (mh);
+ if ( ring.tx_ptr >= ring_info->len ) {
+ ring.tx_ptr -= ring_info->len;
+ }
+
+ v4v_aprintk("going to parse niovs: ring->tx_ptr: %#x, \n", ring.tx_ptr);
+ while ( niov-- )
+ {
+ XEN_GUEST_HANDLE(uint8_t) buf_hnd;
+ v4v_iov_t iov;
+
+ if ( (ret = copy_from_guest(&iov, iovs, 1)) )
+ {
+ printk(KERN_ERR "%s:, ret = %d\n", __func__, ret);
+ ret = -EFAULT;
+ break;
+ }
+ ret = 0;
+
+ v4v_aprintk("iov[%d].{base:%p, len:%#x\n", niov + 1, (char*)iov.iov_base, iov.iov_len);
+ buf_hnd.p = (uint8_t *)iov.iov_base; //FIXME
+ len = iov.iov_len;
+
+ if ( unlikely(!guest_handle_okay(buf_hnd, len)) )
+ {
+ printk(KERN_ERR "%s: PROBLHMA guest_handle_okay\n", __func__);
+ ret = -EFAULT;
+ break;
+ }
+
+ //sp = ring.len - (ring.tx_ptr + sizeof(v4v_ring_t));
+ sp = ring.len - ring.tx_ptr;
+ v4v_aprintk("space (len - tx_ptr):%#x, len:%#lx, len-sp:%#lx\n", sp, len, len-sp);
+