-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxen_shm.c
1576 lines (1263 loc) · 48 KB
/
xen_shm.c
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
/*
* Xen shared memory module
*
* Authors: Vincent Brillault <[email protected]>
* Pierre Pfister <[email protected]>
*
* This file contains the code and private headers of the
* Xen shared memory module. See the headers file for
* precisions about this module.
*
*/
/*
* Kernel module information (submitted at the end of the file)
*/
#define MOD_AUTHORS "Vincent Brillault <[email protected]>, Pierre Pfister <[email protected]>"
#define MOD_DESC "Xen Shared Memory Module"
#define MOD_LICENSE "GPL"
/*
* The headers the module use
*/
#include <asm/atomic.h>
#include <asm/page.h>
#include <asm/signal.h>
#include <asm/xen/hypercall.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/mmu_notifier.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/uaccess.h>
#include <linux/version.h>
#include <linux/vmalloc.h>
#include <linux/wait.h>
#include <xen/balloon.h>
#include <xen/interface/xen.h>
#include <xen/interface/event_channel.h>
#include <xen/events.h>
#include <xen/grant_table.h>
#include <xen/page.h>
/*
* The public header of this module
*/
#include "xen_shm.h"
/*
* Debugging system
*/
#define XEN_SHM_DEBUG 0
#if XEN_SHM_DEBUG
# define PRINTK(...) printk(__VA_ARGS__)
#else /* !XEN_SHM_DEBUG */
# define PRINTK(...)
#endif /* ?XEN_SHM_DEBUG */
/*
* Deal with old linux/xen
*/
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
# define GNTTAB_UNMAP_REFS(unmap_ops, kmap_ops, page, count) gnttab_unmap_refs(unmap_ops, page, count)
#elif LINUX_VERSION_CODE < KERNEL_VERSION(3, 5, 0)
# define GNTTAB_UNMAP_REFS(unmap_ops, kmap_ops, page, count) gnttab_unmap_refs(unmap_ops, page, count, true)
#else
# define GNTTAB_UNMAP_REFS(unmap_ops, kmap_ops, page, count) gnttab_unmap_refs(unmap_ops, kmap_ops, page, count)
#endif /* LINUX_VERSION_CODE ? */
/*
* Fix USHRT_MAX declaration on strange linux
*/
#ifndef USHRT_MAX
# define USHRT_MAX ((u16)(~0U))
#endif
/*
* The delayed closure mechanism needs regular tentative to destroy remaining grant entries.
* Here are the macros that specify when it should be done.
*/
#ifndef DELAYED_FREE_ON_CLOSE
# define DELAYED_FREE_ON_CLOSE 1 //Try to free delayed data when some descriptor is closed
#endif /* DELAYED_FREE_ON_CLOSE */
#ifndef DELAYED_FREE_ON_OPEN
# define DELAYED_FREE_ON_OPEN 1 //Try to free delayed data when some descriptor is opened
#endif /* DELAYED_FREE_ON_OPEN */
/*
* Private Types & Structures
*/
/* Internal module state */
enum xen_shm_state_t {
XEN_SHM_STATE_OPENED, /* Freshly opened device, can move to offerer or receiver */
XEN_SHM_STATE_OFFERER, /* Memory is allocated. Event pipe created. */
XEN_SHM_STATE_RECEIVER, /* The first shared page (header page) is mapped. Pipe is connected. */
XEN_SHM_STATE_RECEIVER_MAPPED /* First page is mapped in the kernel, other pages in the userspace. Pipe is connected. */
};
/* Both sides maintain a state in the shared page. It is used for termination. */
typedef uint8_t xen_shm_meta_page_state;
#define XEN_SHM_META_PAGE_STATE_NONE 0x01 //The peer didn't do anything
#define XEN_SHM_META_PAGE_STATE_OPENED 0x02 //The peer is using the pages
#define XEN_SHM_META_PAGE_STATE_CLOSED 0x03 //Receiver: Pages are unmapped -- Offerer: The receiver must close asap
/*
* Defines the instance information.
* They are stored in the private data field of 'struct file'
* It can also be stored in the delayed free queue in order to be "closed later"
*/
struct xen_shm_instance_data {
enum xen_shm_state_t state; //The state of this instance
/* Use PTE or nor ?*/
int use_ptemod;
/* Pages info */
uint8_t pages_count; //The total number of consecutive allocated pages (with the header page)
unsigned long shared_memory; //Offerer only: The kernel addresses of the allocated pages
/* Xen domids */
domid_t local_domid; //The local domain id
domid_t distant_domid; //The distant domain id
/* Event channel data */
evtchn_port_t local_ec_port; //The allocated local event port number
evtchn_port_t dist_ec_port; //Receiver only: The distant event port number
/* Delayed memory next element */
struct xen_shm_instance_data* next_delayed; //Used in the delayed_free queue (so after closure) to provide a linked list structure
/* Wait queue for the event channel */
wait_queue_head_t wait_queue; //The wait queue used to make some process wait
unsigned int ec_irq; //The event channel irq
uint8_t initial_signal; //0 before the initial signal has been received, 1 after
uint8_t user_signal; //0 when a process is waiting, the handler sets it to one and wakes-up the queue
uint8_t latent_user_signal; //0 when the signal is handled, 1 when a signal has been received
/* State depend variables */
/* Both */
grant_ref_t first_page_grant; //The first page grant reference
/* Offerrer only */
unsigned int offerer_alloc_order; //Offerer only: Saved value of 'order'. Is used when freeing the pages
/* Receiver only */
struct vm_struct *unmapped_area; //Receiver only: Virtual memeroy space allocated on the receiver
struct vm_area_struct *user_mem; //Receiver only: Address where the user have mapped the shared memory
struct page *user_pages[XEN_SHM_ALLOC_ALIGNED_PAGES];
grant_handle_t grant_map_handles[XEN_SHM_ALLOC_ALIGNED_PAGES]; //Receiver only: pages_count grant handles
struct gnttab_map_grant_ref map_ops[XEN_SHM_ALLOC_ALIGNED_PAGES];
struct gnttab_unmap_grant_ref unmap_ops[XEN_SHM_ALLOC_ALIGNED_PAGES];
struct gnttab_map_grant_ref kmap_ops[XEN_SHM_ALLOC_ALIGNED_PAGES];
struct mm_struct *mm;
struct mmu_notifier mn;
};
/*
* The first page is used to share meta-data
* This is the used structure
*/
struct xen_shm_meta_page_data {
xen_shm_meta_page_state offerer_state; //The state of the offerer
xen_shm_meta_page_state receiver_state; //The state of the receiver
/* The number of shared pages (with the header-page).
* The offerer writes it and the receiver must check if he agrees */
uint8_t pages_count;
/*
* Information about the event channel
*/
evtchn_port_t offerer_ec_port; //Offerer's event channel port
atomic_t ec_mutex_count; //An atomic value used by the mutex wait feature
/*
* An array containing 'pages_count' grant referances.
* The first grant ref. must be sent to the receiver, but they are all written here.
*/
grant_ref_t grant_refs[XEN_SHM_ALLOC_ALIGNED_PAGES];
};
/*
* Global values
*/
#define XEN_SHM_DEV_COUNT 1
static domid_t xen_shm_domid = 0; //Must only be used in open Use instance_data to get it otherwise.
static int xen_shm_major_number = XEN_SHM_MAJOR_NUMBER;
static int xen_shm_minor_number = 0;
static dev_t xen_shm_device = 0;
static struct cdev xen_shm_cdev;
static struct xen_shm_instance_data* xen_shm_delayed_free_queue = NULL;
/*
* Module parameters
* As we don't want to define the checkers for domid_t, let's say it's a ushort
*/
module_param(xen_shm_domid, ushort, S_IRUSR | S_IRGRP);
MODULE_PARM_DESC(xen_shm_domid, "Local domain id");
/**********************************************************************************/
/*****************
* Event Channel *
*****************/
/*
* Signal handler
*/
static irqreturn_t
xen_shm_event_handler(int irq, void* arg)
{
struct xen_shm_instance_data* data;
data = (struct xen_shm_instance_data*) arg;
if(!data->initial_signal) {
if(data->state == XEN_SHM_STATE_OFFERER) { //Responds to the initial signal
notify_remote_via_evtchn(data->local_ec_port);
}
data->initial_signal = 1;
} else {
data->user_signal = 1;
data->latent_user_signal = 1;
}
wake_up_interruptible(&data->wait_queue);
return IRQ_HANDLED; //Can also return IRQ_NONE or IRQ_WAKE_THREAD
}
/*
* Generic canal openning
*/
static int
__xen_shm_open_ec(struct xen_shm_instance_data* data, int offerer)
{
int retval;
struct evtchn_alloc_unbound alloc_unbound;
struct evtchn_bind_interdomain bind_op;
struct evtchn_close close_op;
if(offerer) { //Offerer case
alloc_unbound.dom = DOMID_SELF;
alloc_unbound.remote_dom = (data->distant_domid == data->local_domid)?DOMID_SELF:data->distant_domid;
/* Open a unbound channel */
if(HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &alloc_unbound)) {
return -EIO;
}
data->local_ec_port = alloc_unbound.port;
} else { //Receiver case
bind_op.remote_dom = data->distant_domid;
bind_op.remote_port = data->dist_ec_port;
if(HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain, &bind_op)) {
return -EIO;
}
data->local_ec_port = bind_op.local_port;
}
/* Set handler on unbound channel */
retval = bind_evtchn_to_irqhandler(data->local_ec_port, xen_shm_event_handler, 0, "xen_shm", data);
if(retval <= 0) {
close_op.port = data->local_ec_port;
if(HYPERVISOR_event_channel_op(EVTCHNOP_close, &close_op)) {
printk(KERN_WARNING "xen_shm: Couldn't close event channel (state leak) \n");
}
return -EIO;
}
data->ec_irq = retval;
return 0;
}
static int
__xen_shm_open_ec_offerer(struct xen_shm_instance_data* data)
{
return __xen_shm_open_ec(data, 1);
}
static int
__xen_shm_open_ec_receiver(struct xen_shm_instance_data* data)
{
/* Read the distant port from the meta page */
struct xen_shm_meta_page_data *meta_page_p;
meta_page_p = (struct xen_shm_meta_page_data*) data->shared_memory;
data->dist_ec_port = meta_page_p->offerer_ec_port;
return __xen_shm_open_ec( data, 0);
}
/*
* Generic canal closing
*/
static int
__xen_shm_close_ec(struct xen_shm_instance_data* data)
{
unbind_from_irqhandler(data->ec_irq, data); //Also close the channel
return 0;
}
static int
__xen_shm_close_ec_receiver(struct xen_shm_instance_data* data)
{
return __xen_shm_close_ec(data);
}
static int
__xen_shm_close_ec_offerer(struct xen_shm_instance_data* data)
{
return __xen_shm_close_ec(data);
}
/**********************************************************************************/
/******************************
* User mapped memory helpers *
******************************/
static int
__xen_shm_contruct_receiver_k_ops(pte_t *pte, pgtable_t token, unsigned long addr, void *inc)
{
struct xen_shm_instance_data *data;
struct xen_shm_meta_page_data* meta_page_p;
unsigned int offset;
u64 pte_maddr;
data = (struct xen_shm_instance_data*) inc;
offset = (addr - data->user_mem->vm_start) >> PAGE_SHIFT;
pte_maddr = arbitrary_virt_to_machine(pte).maddr;
meta_page_p = (struct xen_shm_meta_page_data*) data->shared_memory;
PRINTK(KERN_DEBUG "xen_shm: Constructing pte (un)map_op\n");
PRINTK(KERN_DEBUG "xen_shm: addr:%p pte_maddr %llu\n", data->map_ops + offset, pte_maddr);
PRINTK(KERN_DEBUG "xen_shm: addr:%p pte_maddr %llu\n", data->unmap_ops + offset, pte_maddr);
gnttab_set_map_op(data->map_ops + offset, pte_maddr,
GNTMAP_host_map | GNTMAP_application_map | GNTMAP_contains_pte,
meta_page_p->grant_refs[offset + 1], data->distant_domid);
gnttab_set_unmap_op(data->unmap_ops + offset, pte_maddr,
GNTMAP_host_map | GNTMAP_application_map | GNTMAP_contains_pte,
-1 /* Non valid handler */);
return 0;
}
static void
__xen_shm_unmap_receiver_grant_pages(struct xen_shm_instance_data *data, int offset, int nb)
{
int err;
PRINTK(KERN_DEBUG "xen_shm: Global unmap @%p: offset:%i count:%i\n", data, offset, nb);
while(nb > 0) {
if (data->unmap_ops[offset].handle != -1) {
PRINTK(KERN_DEBUG "xen_shm: Unmaping : addr:%p pte_maddr %llu\n", data->unmap_ops + offset, (phys_addr_t) data->user_pages + offset);
err = GNTTAB_UNMAP_REFS(data->unmap_ops + offset, data->use_ptemod ? data->kmap_ops + offset : NULL, data->user_pages + offset, 1);
if (err != 0) {
printk(KERN_WARNING "xen_shm: error while unmapping refs: %i\n", err);
}
data->unmap_ops[offset].handle = -1;
--nb;
}
++offset;
if (offset >= data->pages_count - 1) {
if (nb != 0) {
printk(KERN_WARNING "xen_shm: Still %i pages to unmap but no more space\n", nb);
}
return;
}
}
}
/**********************************************************************************/
/*********************
* Memory management *
*********************/
static int
__xen_shm_allocate_shared_memory_offerer(struct xen_shm_instance_data* data)
{
uint32_t tmp_page_count;
unsigned int order;
unsigned long alloc;
// Computing the order of allocation size
order = 0;
tmp_page_count = data->pages_count;
while (tmp_page_count != 0 ) {
order++;
tmp_page_count = tmp_page_count >> 1;
}
if (tmp_page_count==1<<(order-1)) {
order--;
}
// Allocating the pages
alloc = __get_free_pages(GFP_KERNEL, order);
if (alloc == 0) {
printk(KERN_WARNING "xen_shm: could not alloc space for 2^%i pages\n", (int) order);
return -ENOMEM;
}
data->offerer_alloc_order = order;
data->shared_memory = alloc;
return 0;
}
//Free the offerer memory pages
static void
__xen_shm_free_shared_memory_offerer(struct xen_shm_instance_data* data)
{
if (data->shared_memory != 0) {
free_pages(data->shared_memory, data->offerer_alloc_order);
}
}
//Free the receiver memory pages
static void
__xen_shm_free_shared_memory_receiver(struct xen_shm_instance_data* data)
{
if (data->unmapped_area != NULL) {
free_vm_area(data->unmapped_area);
}
}
/*
* Is called when a free cannot be done imidiatly. The data must be put in a queue and deleted later.
*/
static void
__xen_shm_add_delayed_free(struct xen_shm_instance_data* data)
{
printk(KERN_WARNING "xen_shm: Data cannot be free. Adding to queue. !\n");
data->next_delayed = xen_shm_delayed_free_queue;
xen_shm_delayed_free_queue = data->next_delayed;
}
/*
* After the users asked to close the file, it does everything to undo the mapping/granting.
* Returns 0 if data can be safely forgot. A negative value if it cannot yet.
*/
static int
__xen_shm_prepare_free(struct xen_shm_instance_data* data, bool first)
{
struct gnttab_unmap_grant_ref unmap_op;
struct xen_shm_meta_page_data *meta_page_p;
meta_page_p = (struct xen_shm_meta_page_data*) data->shared_memory;
/*
* Xen grant table state must be restored (unmap on receiver side and end grant on offerer side)
*/
switch(data->state) {
case XEN_SHM_STATE_OPENED:
return 0;
case XEN_SHM_STATE_OFFERER:
//Try to free data pages first
while (data->pages_count != 0) {
if (gnttab_end_foreign_access_ref(meta_page_p->grant_refs[data->pages_count-1], 0)) {
data->pages_count--;
} else {
//printk(KERN_WARNING "xen_shm: Grant ref %i (from first grant %i) still in use !\n", meta_page_p->grant_refs[data->pages_count-1], data->first_page_grant);
goto fail;
}
}
//Closing event channel
__xen_shm_close_ec_offerer(data);
//Freeing memory
__xen_shm_free_shared_memory_offerer(data);
return 0;
case XEN_SHM_STATE_RECEIVER_MAPPED:
if (!data->use_ptemod) {
PRINTK(KERN_DEBUG "xen_shm: __xen_shm_prepare_free unmap\n");
__xen_shm_unmap_receiver_grant_pages(data, 0, data->pages_count - 1);
}
// Continue ! (no break)
case XEN_SHM_STATE_RECEIVER:
// Unmap the first page
gnttab_set_unmap_op(&unmap_op, ((unsigned long) data->shared_memory), GNTMAP_host_map, data->grant_map_handles[0]);
HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &unmap_op, 1);
if(unmap_op.status == 0) {
data->pages_count--;
} else {
if (first) {
printk(KERN_WARNING "xen_shm: Could not unmap a page in receiver mode. Error:%i !\n", unmap_op.status);
}
goto fail;
}
//Closing event channel
__xen_shm_close_ec_receiver(data);
//Freeing memory
__xen_shm_free_shared_memory_receiver(data);
return 0;
default:
printk(KERN_WARNING "xen_shm: Impossible state !\n");
return -2;
break;
}
fail:
printk(KERN_WARNING "xen_shm: Failed to prepare free (first grant %i)\n", data->first_page_grant);
return -1;
}
#if (DELAYED_FREE_ON_CLOSE || DELAYED_FREE_ON_OPEN)
static void
__xen_shm_free_delayed_queue(void)
{
struct xen_shm_instance_data* current_i;
struct xen_shm_instance_data* previous;
struct xen_shm_instance_data* to_delete;
previous = NULL;
current_i = xen_shm_delayed_free_queue;
while (current_i != NULL) {
if (__xen_shm_prepare_free(current_i, false) == 0) { //On peut supprimer
to_delete = current_i;
if (previous == NULL) { //Premier élément de la liste
xen_shm_delayed_free_queue = current_i->next_delayed;
} else {
previous->next_delayed = current_i->next_delayed;
}
current_i = current_i->next_delayed; //Next
printk(KERN_WARNING "xen_shm: Finally freeing instance (%i) from the queue\n", to_delete->first_page_grant);
kfree(to_delete);
} else {
previous = current_i;
current_i = current_i->next_delayed;
}
}
}
#endif /* (DELAYED_FREE_ON_CLOSE || DELAYED_FREE_ON_OPEN) */
/**********************************************************************************/
/***************************
* mmu_notifier operations *
***************************/
static void
__xen_shm_mn_invl_range_start(struct mmu_notifier *mn,
struct mm_struct *mm,
unsigned long start, unsigned long end)
{
struct xen_shm_instance_data *data;
unsigned long mstart, mend;
data = container_of(mn, struct xen_shm_instance_data, mn);
if (data->user_mem == NULL) {
/* Wrong state no need to do anything */
return;
}
PRINTK(KERN_DEBUG "xen_shm: Invalidating range %lu-%lu (%lu-%lu)\n", start, end, data->user_mem->vm_start, data->user_mem->vm_end);
if (start >= data->user_mem->vm_end || data->user_mem->vm_start >= end) {
/* Not sthe right area */
return;
}
mstart = max(start, data->user_mem->vm_start);
mend = min(end, data->user_mem->vm_end);
__xen_shm_unmap_receiver_grant_pages(data, (mstart - data->user_mem->vm_start) >> PAGE_SHIFT, (mend - mstart) >> PAGE_SHIFT);
}
static void
__xen_shm_mn_invl_page(struct mmu_notifier *mn,
struct mm_struct *mm,
unsigned long address)
{
PRINTK(KERN_DEBUG "xen_shm: Invalidating page %lu\n", address);
__xen_shm_mn_invl_range_start(mn, mm, address, address + PAGE_SIZE);
}
static void
__xen_shm_mn_release(struct mmu_notifier *mn,
struct mm_struct *mm)
{
struct xen_shm_instance_data *data;
data = container_of(mn, struct xen_shm_instance_data, mn);
if (data->user_mem == NULL) {
/* Wrong state no need to do anything */
return;
}
PRINTK(KERN_DEBUG "xen_shm: mn release @%p\n", data);
__xen_shm_unmap_receiver_grant_pages(data, 0, data->pages_count - 1);
}
struct mmu_notifier_ops xen_shm_mmu_ops = {
.invalidate_range_start = __xen_shm_mn_invl_range_start,
.invalidate_page = __xen_shm_mn_invl_page,
.release = __xen_shm_mn_release,
};
/**********************************************************************************/
/******************
* IOCTLs helpers *
******************/
/*
* Helper for XEN_SHM_IOCTL_INIT_OFFERER
*/
static int
__xen_shm_ioctl_init_offerer(struct xen_shm_instance_data* data,
struct xen_shm_ioctlarg_offerer* arg)
{
int error;
int page;
char *page_pointer;
struct xen_shm_meta_page_data *meta_page_p;
atomic_t atomic = ATOMIC_INIT(1);
if (data->state != XEN_SHM_STATE_OPENED) {
/* Command is invalid in this state */
return -ENOTTY;
}
if (arg->pages_count == 0 || arg->pages_count > XEN_SHM_MAX_SHARED_PAGES) {
/* Cannot allocate this amount of pages */
printk(KERN_WARNING "xen_shm: Pages count is out of bound (%i).",arg->pages_count );
return -EINVAL;
}
/*
* Completing file data
*/
data->distant_domid = (arg->dist_domid == DOMID_SELF) ? data->local_domid : arg->dist_domid;
data->pages_count = arg->pages_count + 1;
/*
* Allocating memory
*/
error = __xen_shm_allocate_shared_memory_offerer(data);
if (error < 0) {
return error;
}
/* Initialize header page */
meta_page_p = (struct xen_shm_meta_page_data*) data->shared_memory;
meta_page_p->offerer_state = XEN_SHM_META_PAGE_STATE_NONE;
meta_page_p->receiver_state = XEN_SHM_META_PAGE_STATE_NONE;
meta_page_p->ec_mutex_count = atomic;
meta_page_p->pages_count = data->pages_count;
/* Grant mapping and fill header page */
page_pointer = (char*) data->shared_memory;
for (page=0; page < data->pages_count; page++) {
meta_page_p->grant_refs[page] = gnttab_grant_foreign_access(data->distant_domid , virt_to_mfn(page_pointer), 0); //Granting access
if (meta_page_p->grant_refs[page] < 0) { //In case of error
error = meta_page_p->grant_refs[page];
printk(KERN_WARNING "xen_shm: could not grant %ith page (%i)\n",page, (int) meta_page_p->grant_refs[page]);
goto undo_grant;
}
page_pointer += PAGE_SIZE; //Go to next page
}
//Set argument respond
arg->grant = meta_page_p->grant_refs[0];
arg->local_domid = data->local_domid;
//Set first grant ref
data->first_page_grant = meta_page_p->grant_refs[0];
/* Open event channel and connect it to handler */
if((error = __xen_shm_open_ec_offerer(data)) != 0) {
goto undo_grant;
}
meta_page_p->offerer_ec_port = data->local_ec_port;
/* If OK, states are changed*/
data->state = XEN_SHM_STATE_OFFERER;
meta_page_p->offerer_state = XEN_SHM_META_PAGE_STATE_OPENED;
return 0;
undo_grant:
page--;
for (; page>=0; page--) {
gnttab_end_foreign_access_ref(meta_page_p->grant_refs[page], 0);
}
__xen_shm_free_shared_memory_offerer(data);
return error;
}
/*
* Helper for XEN_SHM_IOCTL_INIT_RECEIVER
*/
static int
__xen_shm_ioctl_init_receiver(struct xen_shm_instance_data* data,
struct xen_shm_ioctlarg_receiver* arg)
{
int error;
struct gnttab_map_grant_ref map_op;
struct gnttab_unmap_grant_ref unmap_op;
struct xen_shm_meta_page_data* meta_page_p;
if (data->state != XEN_SHM_STATE_OPENED) {
/* Command is invalid in this state */
return -ENOTTY;
}
if (arg->pages_count == 0 || arg->pages_count > XEN_SHM_MAX_SHARED_PAGES) {
/* Cannot allocate this amount of pages */
printk(KERN_WARNING "xen_shm: Pages count is out of bound (%i).",arg->pages_count );
return -EINVAL;
}
/*
* Completing file data
*/
data->distant_domid = (arg->dist_domid==DOMID_SELF)?data->local_domid:arg->dist_domid;
data->first_page_grant = arg->grant;
data->pages_count = arg->pages_count + 1;
/*
* Allocating memory space
*/
data->unmapped_area = alloc_vm_area(PAGE_SIZE, NULL);
if (data->unmapped_area == NULL) {
printk(KERN_WARNING "xen_shm: Cannot allocate vm area.");
return -ENOMEM;
}
data->shared_memory = (unsigned long) data->unmapped_area->addr;
/*
* Finding the first page
*/
gnttab_set_map_op(&map_op, (phys_addr_t) data->unmapped_area->addr, GNTMAP_host_map, data->first_page_grant, data->distant_domid);
if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &map_op, 1)) {
printk(KERN_WARNING "xen_shm: HYPERVISOR map grant ref failed\n");
error = -EFAULT;
goto undo_alloc;
}
if (map_op.status < 0) {
printk(KERN_WARNING "xen_shm: HYPERVISOR map grant ref failed with error %i \n", map_op.status);
error = -EINVAL;
goto undo_alloc;
}
data->grant_map_handles[0] = map_op.handle;
/*
* Checking compatibility
*/
meta_page_p = (struct xen_shm_meta_page_data*) data->shared_memory;
if (data->pages_count != meta_page_p->pages_count ) { //Not the same number of pages on both sides
printk(KERN_WARNING "xen_shm: Pages count is incorrect. Locally %i VS %i distantly.\n", (int)data->pages_count, (int)meta_page_p->pages_count );
error = -EINVAL;
goto undo_map;
}
/*
* The rest will be mapped on 'mmap' command
*/
/*
* Connecting the event channel
*/
/* Open event channel and connect it to handler */
if((error = __xen_shm_open_ec_receiver(data)) != 0) {
goto undo_map;
}
/* If OK, states are changed*/
data->state = XEN_SHM_STATE_RECEIVER;
meta_page_p->receiver_state = XEN_SHM_META_PAGE_STATE_OPENED;
/* Send the initial signal */
notify_remote_via_evtchn(data->local_ec_port);
return 0;
undo_map:
gnttab_set_unmap_op(&unmap_op, (unsigned long) data->unmapped_area->addr, GNTMAP_host_map, data->grant_map_handles[0]);
HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &unmap_op, 1);
undo_alloc:
__xen_shm_free_shared_memory_receiver(data);
return error;
}
/*
* Detect broken pipes
*/
static int
__xen_shm_is_broken_pipe(struct xen_shm_meta_page_data* meta_page_p) {
return (meta_page_p->offerer_state == XEN_SHM_META_PAGE_STATE_CLOSED
|| meta_page_p->receiver_state == XEN_SHM_META_PAGE_STATE_CLOSED);
}
/*
* Helper for XEN_SHM_IOCTL_WAIT and XEN_SHM_IOCTL_AWAIT
*/
static int
__xen_shm_ioctl_await(struct xen_shm_instance_data* data,
struct xen_shm_ioctlarg_await* arg)
{
struct xen_shm_meta_page_data* meta_page_p;
unsigned long jiffies;
int retval;
int user_flag;
int init_flag;
int mutex_flag;
int user_latent_flag;
user_flag = arg->request_flags & XEN_SHM_IOCTL_AWAIT_USER;
user_latent_flag = arg->request_flags & XEN_SHM_IOCTL_AWAIT_LATENT_USER;
init_flag = arg->request_flags & XEN_SHM_IOCTL_AWAIT_INIT;
mutex_flag = arg->request_flags & XEN_SHM_IOCTL_AWAIT_MUTEX;
if(data->state == XEN_SHM_STATE_OPENED) //Not opened yet
{
return -ENOTTY;
}
meta_page_p = (struct xen_shm_meta_page_data*) data->shared_memory;
if(__xen_shm_is_broken_pipe(meta_page_p)) {//Broken pipe, before mutex
return -EPIPE;
}
if(mutex_flag && !atomic_dec_and_test(&meta_page_p->ec_mutex_count)) {
atomic_inc(&meta_page_p->ec_mutex_count);
return -EDEADLK;
}
//Condition telling wether the pipe is known to be closed
data->user_signal = 0; //Trigger the wait
if(arg->timeout_ms == 0) {
retval = wait_event_interruptible(data->wait_queue,
(user_flag&&data->user_signal) || (init_flag&&data->initial_signal)
|| (user_latent_flag&&data->latent_user_signal) ||
__xen_shm_is_broken_pipe(meta_page_p));
if(retval < 0) {
goto unlock_and_return;
}
} else {
jiffies = (arg->timeout_ms*HZ)/1000;
jiffies = (jiffies==0)?1:jiffies; //Cannot be null
retval = wait_event_interruptible_timeout(data->wait_queue,
(user_flag&&data->user_signal) || (init_flag&&data->initial_signal)
|| (user_latent_flag&&data->latent_user_signal) ||
__xen_shm_is_broken_pipe(meta_page_p),
jiffies);
if(retval < 0) {
goto unlock_and_return;
}
arg->remaining_ms = (retval==jiffies)?arg->timeout_ms:(retval*1000)/HZ;
retval = 0;
}
if(user_flag || user_latent_flag) {
data->latent_user_signal = 0;
}
if(__xen_shm_is_broken_pipe(meta_page_p)) {
retval = -EPIPE;
goto unlock_and_return;
}
#undef XEN_SHM_IOCTL_AWAIT_COND
retval = 0;
unlock_and_return:
if(mutex_flag) {
atomic_inc(&meta_page_p->ec_mutex_count);
}
return retval;
}
/*
* Helper for XEN_SHM_IOCTL_SSIG
*/
static int
__xen_shm_ioctl_ssig(struct xen_shm_instance_data* data) {
if(data->state == XEN_SHM_STATE_OPENED) {
return -ENOTTY;
}
if(__xen_shm_is_broken_pipe((struct xen_shm_meta_page_data*) data->shared_memory)) {
return -EPIPE;
}
notify_remote_via_evtchn(data->local_ec_port);
return 0;
}
/**********************************************************************************/
/*******************
* file operations *
*******************/
/*
* OPEN.
* Called when a user wants to open the device
* Memory is not allocated yet because the size will be specified by the user with an ioctl.
*/
static int
xen_shm_open(struct inode * inode, struct file * filp)
{
struct xen_shm_instance_data* instance_data;
/*
* Initialize the filp private data related to this instance.
*/