-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdfv_server.c
More file actions
1643 lines (1313 loc) · 41.2 KB
/
dfv_server.c
File metadata and controls
1643 lines (1313 loc) · 41.2 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
/*
* Device File-based I/O Virtualization (DFV)
* File: dfv_server.c
*
* Copyright (c) 2014 Rice University, Houston, TX, USA
* All rights reserved.
*
* Authors: Ardalan Amiri Sani <arrdalan@gmail.com>
*
* Originally based on the Device Virtualization project
*
* Copyright (c) 2010 Nokia Research Center, Palo Alto, USA
* All rights reserved.
*
* Authors: Sreekumar Nair
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
* substantially similar to the "NO WARRANTY" disclaimer below
* ("Disclaimer") and any redistribution must be conditioned upon
* including a substantially similar Disclaimer requirement for further
* binary redistribution.
* 3. Neither the names of the above-listed copyright holders nor the names
* of any contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* Alternatively, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2 as published by the Free
* Software Foundation.
*
* NO WARRANTY
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*/
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/pagemap.h>
#include "dfv_linux_code.h"
#include "dfv_common.h"
#include "dfv_server.h"
#include "dfv_drm.h"
struct list_head guest_vm_list;
struct list_head guest_list;
struct list_head guest_thread_list;
static struct dfv_state *add_dfvstate(struct guest_struct *guest)
{
struct dfv_state *dfvstate = NULL;
dfvstate = kmalloc(sizeof(*dfvstate), GFP_KERNEL);
dfvstate->serverfd = guest->serverfd;
list_add(&dfvstate->list, &guest->dfv_device_list);
return dfvstate;
}
static struct dfv_state *get_dfvstate(struct guest_struct *guest, int sfd)
{
struct dfv_state *dfvstate = NULL;
list_for_each_entry(dfvstate, &guest->dfv_device_list, list) {
if (dfvstate->serverfd == sfd) {
return dfvstate;
}
}
return NULL;
}
static int remove_dfvstate(struct guest_struct *guest, int sfd)
{
struct dfv_state *dfvstate = NULL, *d_tmp;
if (!guest) {
DFVPRINTK_ERR("Error: guest is NULL\n");
return -EINVAL;
}
list_for_each_entry_safe(dfvstate, d_tmp, &guest->dfv_device_list, list) {
if (dfvstate->serverfd == sfd) {
list_del(&dfvstate->list);
kfree(dfvstate);
return 0;
}
}
return -EINVAL;
}
static void dfv_fop_open(struct guest_thread_struct *guest_thread,
struct dfv_op_args *req_args, struct dfv_op_args *res_args)
{
int fd;
int index = 0;
struct file *file = NULL;
struct inode *inode = NULL;
char *dfvpathname;
struct dfv_state *new_dfvstate;
struct guest_struct *guest = guest_thread->guest;
/*
* Preventing a overflow. The serverfd is passed to the client using
* a 32-bit entry (for 32-bit architectures that we support currently).
* While serverfd of 0xFFFFFFFF is still valid (it is
* the last one before we overflow), we throw error at this point to
* prevent the overflow in the next open.
*
* TODO: we should reclaim the released serverfds in the release
* file operation.
*/
if (guest->serverfd == 0xFFFFFFFF) {
DFVPRINTK_ERR("Error: serverfd is too large.\n");
OPEN_RESULT = -EINVAL;
return;
}
dfvpathname = get_pathname_from_abs_name((int) OPEN_PATHNAME);
if (dfvpathname == NULL) {
DFVPRINTK_ERR("Error: No equivalent pathname was found.\n");
OPEN_RESULT = -EINVAL;
return;
}
fd = sys_open_kernel(dfvpathname, OPEN_FLAGS, OPEN_MODE, &file);
inode = file->f_path.dentry->d_inode;
OPEN_SERVERFD = guest->serverfd;
new_dfvstate = add_dfvstate(guest);
if (new_dfvstate == NULL) {
DFVPRINTK_ERR("Error: could not create new_dfvstate\n");
OPEN_RESULT = -EFAULT;
goto out_err;
}
new_dfvstate->file = file;
new_dfvstate->inode = inode;
new_dfvstate->fdtable = current->files;
new_dfvstate->fd = fd;
guest_thread->num_open_fds++;
guest->num_open_fds++;
guest->guest_vm->num_open_fds++;
guest->serverfd++;
/*
* If a file operation is not defined on the server,
* then it should be null on the client as well!
*/
OPEN_SERVERFOPS = 0;
#define DFVFO(x) { \
index = dfvbitmap[DFV_FOP_ ## x].bitmap_index; \
DFVPRINTK("file operation: " #x "=%#x\n", \
(unsigned int) file->f_op->x); \
if ((file->f_op->x) != NULL) { \
OPEN_SERVERFOPS |= (1 << index); \
} \
}
DFV_FILE_OPERATIONS
#undef DFVFO
OPEN_RESULT = 0;
OPRES_POSITION = 0;
return;
/*
* The num_open_fds of guest_thread, guest, and guest VM should not
* have been increased before jumping here. Otherwise, we need
* to revert that.
*/
out_err:
remove_dfvstate(guest, guest->serverfd);
remove_guest_thread(guest_thread);
if (file->f_op->release)
file->f_op->release(inode, file);
}
static void dfv_fop_read(struct guest_thread_struct *guest_thread,
struct dfv_op_args *req_args, struct dfv_op_args *res_args)
{
int result = -EFAULT;
int sfd = OPREQ_SERVERFD;
struct guest_struct *guest = guest_thread->guest;
struct file *file;
struct dfv_state *proxy = get_dfvstate(guest, sfd);
if (proxy == NULL) {
DFVPRINTK_ERR("Error: could not find dfvstate\n");
READ_RESULT = -EINVAL;
return;
}
file = proxy->file;
if ((!file) || (!file->f_op) || (!file->f_op->read)) {
DFVPRINTK_ERR("Error: device read function not specified.\n");
READ_RESULT = -EINVAL;
return;
}
result = file->f_op->read(file,
(char __user *) READ_BUF,
(size_t) READ_COUNT,
(loff_t *) &READ_OFFSET);
OPRES_POSITION = file->f_pos;
READ_RESULT = result;
READ_NEWOFFSET = READ_OFFSET;
}
static void dfv_fop_write(struct guest_thread_struct *guest_thread,
struct dfv_op_args *req_args, struct dfv_op_args *res_args)
{
int result = -EFAULT;
int sfd = OPREQ_SERVERFD;
struct guest_struct *guest = guest_thread->guest;
struct file *file;
struct dfv_state *proxy = get_dfvstate(guest, sfd);
if (proxy == NULL) {
DFVPRINTK_ERR("Error: could not find dfvstate\n");
WRITE_RESULT = -EINVAL;
return;
}
file = proxy->file;
if ((!file) || (!file->f_op) || (!file->f_op->write)) {
DFVPRINTK_ERR("Error: device write function not specified.\n");
WRITE_RESULT = -EINVAL;
return;
}
result = file->f_op->write(file,
(char __user *) WRITE_BUF,
(size_t) WRITE_COUNT,
(loff_t *) &WRITE_OFFSET);
OPRES_POSITION = file->f_pos;
WRITE_NEWOFFSET = WRITE_OFFSET;
WRITE_RESULT = result;
}
static inline bool is_valid_poll_result(int result, unsigned int key)
{
if ( ((key & POLLIN_SET) && (result & POLLIN_SET))
|| ((key & POLLIN_SET) && (result & POLLIN_SET))
|| ((key & POLLIN_SET) && (result & POLLIN_SET)) )
return true;
return false;
}
static void dfv_fop_poll(struct guest_thread_struct *guest_thread,
struct dfv_op_args *req_args, struct dfv_op_args *res_args)
{
int result = -EFAULT;
int sfd = OPREQ_SERVERFD;
struct guest_struct *guest = guest_thread->guest;
ktime_t expires, *to = NULL;
struct file *file;
unsigned int key = POLL_KEY;
unsigned long slack = 0;
int timed_out = 0;
poll_table *wait = NULL;
struct dfv_state *proxy = get_dfvstate(guest, sfd);
if (proxy == NULL) {
DFVPRINTK_ERR("Error: could not find dfvstate\n");
POLL_RESULT = -EINVAL;
return;
}
file = proxy->file;
if ((!file) || (!file->f_op) || (!file->f_op->poll)) {
DFVPRINTK_ERR("Error: device poll function not specified\n");
POLL_RESULT = -EINVAL;
return;
}
if (guest_thread->use_non_blocking_poll && guest_thread->poll_sleep &&
POLL_NULLKEY == 0) {
wake_up(guest_thread->poll_wait_queue);
}
if (POLL_NULLKEY == 0) {
guest_thread->table = kmalloc(sizeof(*guest_thread->table),
GFP_KERNEL);
poll_initwait(guest_thread->table);
wait = &guest_thread->table->pt;
wait->key = key;
if (guest_thread->use_non_blocking_poll)
poll_wait(file, guest_thread->poll_wait_queue, wait);
}
result = file->f_op->poll(file, wait);
/*
* Blocking poll is useful if the communication latency between the
* client and server is high. Note that the code below is not
* properly tested. Use non-blocking poll for I/O virtualization.
*/
if (!guest_thread->use_non_blocking_poll && POLL_NULLKEY == 0) {
if (is_valid_poll_result(result, key)) {
goto no_wait;
}
slack = POLL_SLACK;
timed_out = 0;
if (POLL_WAIT_TIME != POLL_INFINITE_WAIT) {
/*
* Yes, we are ignoring the latency of communication from
* the client to the server, which is a few to a few tens
* of microseconds.
*/
expires = ktime_add_ns(ktime_get(), POLL_WAIT_TIME);
to = &expires;
}
if (!poll_schedule_timeout(guest_thread->table,
TASK_INTERRUPTIBLE, to, slack))
timed_out = 1;
if (!timed_out) {
result = file->f_op->poll(file, NULL);
}
no_wait:
poll_freewait(guest_thread->table);
kfree(guest_thread->table);
guest_thread->table = NULL;
}
else if (guest_thread->use_non_blocking_poll && POLL_NULLKEY == 0) {
if (!is_valid_poll_result(result, key)) {
guest_thread->need_poll_wait = true;
guest_thread->poll_wait_time = POLL_WAIT_TIME;
guest_thread->poll_slack = POLL_SLACK;
guest_thread->poll_file = file;
guest_thread->poll_key = key;
}
else {
poll_freewait(guest_thread->table);
kfree(guest_thread->table);
guest_thread->table = NULL;
guest_thread->need_poll_wait = false;
}
}
OPRES_POSITION = file->f_pos;
POLL_RESULT = result;
}
void wait_for_poll(struct guest_thread_struct *guest_thread)
{
int timed_out = 0;
ktime_t expires, *to = NULL;
unsigned long slack = guest_thread->poll_slack;
int result;
struct poll_wqueues *table;
/*
* guest->thread->table might change so we use table in the rest of this
* function. Moreover, we do an atomic test and set here to make sure
* only one waitqueue task uses this table.
*/
preempt_disable();
table = guest_thread->table;
guest_thread->table = NULL;
preempt_enable();
if (guest_thread->poll_wait_time != POLL_INFINITE_WAIT) {
expires = ktime_add_ns(ktime_get(), guest_thread->poll_wait_time);
to = &expires;
}
guest_thread->poll_sleep = true; /* FIXME: need a lock here */
if (!poll_schedule_timeout(table, TASK_INTERRUPTIBLE, to, slack)) {
timed_out = 1;
}
guest_thread->poll_sleep = false;
poll_freewait(table);
kfree(table);
if (!timed_out) {
result = guest_thread->poll_file->f_op->poll(
guest_thread->poll_file, NULL);
if (is_valid_poll_result(result, guest_thread->poll_key)) {
guest_thread->guest_vm->send_poll_notification(guest_thread);
}
}
}
EXPORT_SYMBOL(wait_for_poll);
static int dfv_insert_vma(struct guest_struct *guest,
struct vm_area_struct *new_vma)
{
struct vma_list_struct *vma_entry = kmalloc(sizeof(*vma_entry),
GFP_KERNEL);
vma_entry->vma = new_vma;
INIT_LIST_HEAD(&vma_entry->gfn_list);
INIT_LIST_HEAD(&vma_entry->pfn_list);
list_add(&vma_entry->list, &guest->vma_list);
return 1;
}
int __dfv_add_vma(struct vm_area_struct *vma)
{
struct guest_struct *guest = NULL;
struct guest_thread_struct *guest_thread = NULL;
guest = (struct guest_struct *) current->dfvguest;
guest_thread = (struct guest_thread_struct *) current->dfvguest_thread;
dfv_insert_vma(guest, vma);
return 0;
}
static struct vm_area_struct *dfv_find_vma_by_range(struct guest_struct *guest,
unsigned long startaddr, unsigned long endaddr)
{
struct vma_list_struct *vma_entry = NULL;
list_for_each_entry(vma_entry, &guest->vma_list, list) {
if (vma_entry->vma->vm_start == startaddr
&& vma_entry->vma->vm_end == endaddr) {
return vma_entry->vma;
}
}
DFVPRINTK_ERR("Error: could not find the vma\n");
return NULL;
}
static int dfv_remove_vma(struct guest_struct *guest, unsigned long startaddr,
unsigned long endaddr)
{
struct vma_list_struct *vma_entry = NULL, *v_tmp;
list_for_each_entry_safe(vma_entry, v_tmp, &guest->vma_list, list) {
if (vma_entry->vma->vm_start == startaddr
&& vma_entry->vma->vm_end == endaddr) {
list_del(&vma_entry->list);
kfree(vma_entry->vma);
kfree(vma_entry);
return 1;
}
}
DFVPRINTK_ERR("Error: could not find the vma\n");
return 0;
}
struct vma_list_struct *get_vma_entry(struct guest_struct *guest,
struct vm_area_struct *vma)
{
struct vma_list_struct *vma_entry = NULL;
list_for_each_entry(vma_entry, &guest->vma_list, list) {
if (vma_entry->vma == vma) {
return vma_entry;
}
}
DFVPRINTK_ERR("Error: could not find the vma_entry\n");
return NULL;
}
EXPORT_SYMBOL(get_vma_entry);
struct vma_list_struct *get_vma_entry_by_addr(struct guest_struct *guest,
unsigned long addr)
{
struct vma_list_struct *vma_entry = NULL;
struct vm_area_struct *vma;
list_for_each_entry(vma_entry, &guest->vma_list, list) {
vma = vma_entry->vma;
if (addr >= vma->vm_start && addr < vma->vm_end) {
return vma_entry;
}
}
DFVPRINTK_ERR("Error: could not find the vma_entry\n");
return NULL;
}
static void dfv_fop_mmap(struct guest_thread_struct *guest_thread,
struct dfv_op_args *req_args, struct dfv_op_args *res_args)
{
int result = -EFAULT;
int index = 0;
int sfd = OPREQ_SERVERFD;
struct guest_struct *guest = guest_thread->guest;
struct vm_area_struct *vma = NULL;
unsigned long startaddr = MMAP_STARTADDR;
unsigned long endaddr = MMAP_ENDADDR;
unsigned long vmflags = MMAP_VMFLAGS;
unsigned long pgoff = MMAP_PGOFF;
struct file *file;
struct dfv_state *proxy = get_dfvstate(guest, sfd);
if (proxy == NULL) {
DFVPRINTK_ERR("Error: could not find dfvstate\n");
MMAP_RESULT = -EINVAL;
return;
}
file = proxy->file;
if ((!file) || (!file->f_op) || (!file->f_op->mmap)) {
DFVPRINTK_ERR("Error: device mmap function not specified.\n");
MMAP_RESULT = -EINVAL;
return;
}
vma = kmalloc(sizeof(*vma), GFP_KERNEL);
if (!vma) {
DFVPRINTK_ERR("Error: out of memory\n");
MMAP_RESULT = -ENOMEM;
return;
}
vma->vm_mm = current->mm;
vma->vm_start = startaddr;
vma->vm_end = endaddr;
vma->vm_flags = vmflags;
vma->vm_page_prot = vm_get_page_prot(vmflags);
vma->vm_pgoff = pgoff;
vma->vm_file = file;
dfv_insert_vma(guest, vma);
result = file->f_op->mmap(file, vma);
MMAP_SERVERVMOPS = 0;
/* Don't continue if mmap failed. */
if (result) {
MMAP_RESULT = result;
goto err_out;
}
if (!vma->vm_ops) {
goto add_close;
}
#define DFVVMO(x) { \
index = dfvbitmap[DFV_VMOP_ ## x].bitmap_index; \
DFVPRINTK("VM operation: " #x "=%#x\n", \
(unsigned int) vma->vm_ops->x); \
if ((vma->vm_ops->x) != NULL) { \
MMAP_SERVERVMOPS |= (1 << index); \
} \
}
DFV_VM_OPERATIONS
#undef DFVVMO
/*
* We need our dfv_vmop_close to be called, regardless of whether the
* driver implements the vma_ops->close operation or not. We need it so
* that we can clean up the mmapped pages.
*/
add_close:
index = dfvbitmap[DFV_VMOP_close].bitmap_index;
MMAP_SERVERVMOPS |= (1 << index);
OPRES_POSITION = file->f_pos;
MMAP_RESULT = result;
MMAP_NEWVMFLAGS = vma->vm_flags;
err_out:
return;
}
static void dfv_fop_unlocked_ioctl(struct guest_thread_struct *guest_thread,
struct dfv_op_args *req_args, struct dfv_op_args *res_args)
{
int result = -EFAULT;
int sfd = OPREQ_SERVERFD;
struct guest_struct *guest = guest_thread->guest;
unsigned int cmd = UNLOCKED_IOCTL_CMD;
unsigned long arg = UNLOCKED_IOCTL_ARG;
struct file *file;
struct dfv_state *proxy = get_dfvstate(guest, sfd);
if (proxy == NULL) {
DFVPRINTK_ERR("Error: could not find dfvstate, "
"guest_vm_id=%d, guest_id = %d, sfd=%d\n",
guest->guest_vm_id, guest->guest_id, sfd);
UNLOCKED_IOCTL_RESULT = -EINVAL;
return;
}
file = proxy->file;
if ((!file) || (!file->f_op) || (!file->f_op->unlocked_ioctl)) {
DFVPRINTK_ERR("Error: device unlocked_ioctl function not specified\n");
UNLOCKED_IOCTL_RESULT = -EINVAL;
return;
}
result = file->f_op->unlocked_ioctl(file, cmd, arg);
OPRES_POSITION = file->f_pos;
UNLOCKED_IOCTL_RESULT = result;
}
static void dfv_fop_llseek(struct guest_thread_struct *guest_thread,
struct dfv_op_args *req_args, struct dfv_op_args *res_args)
{
int result = -EFAULT;
int sfd = OPREQ_SERVERFD;
struct guest_struct *guest = guest_thread->guest;
struct file *file;
struct dfv_state *proxy = get_dfvstate(guest, sfd);
if (proxy == NULL) {
DFVPRINTK_ERR("dfv_fop_llseek: Error: could not find dfvstate\n");
LLSEEK_RESULT = -EINVAL;
return;
}
file = proxy->file;
if ((!file) || (!file->f_op) || (!file->f_op->llseek)) {
DFVPRINTK_ERR("Error: device llseek function not specified\n");
LLSEEK_RESULT = -EINVAL;
return;
}
result = file->f_op->llseek(file,
(loff_t) LLSEEK_OFFSET,
(int) LLSEEK_DIRECTION);
OPRES_POSITION = file->f_pos;
LLSEEK_RESULT = result;
}
static void dfv_fop_flush(struct guest_thread_struct *guest_thread,
struct dfv_op_args *req_args, struct dfv_op_args *res_args)
{
int result = -EFAULT;
int sfd = OPREQ_SERVERFD;
struct guest_struct *guest = guest_thread->guest;
struct file *file;
fl_owner_t id;
struct dfv_state *proxy = get_dfvstate(guest, sfd);
if (proxy == NULL) {
DFVPRINTK_ERR("Error: could not find dfvstate\n");
FLUSH_RESULT = -EINVAL;
return;
}
file = proxy->file;
id = proxy->fdtable;
if ((!file) || (!file->f_op) || (!file->f_op->flush)) {
DFVPRINTK_ERR("Error: device flush function not specified\n");
FLUSH_RESULT = -EINVAL;
return;
}
result = file->f_op->flush(file, id);
OPRES_POSITION = file->f_pos;
FLUSH_RESULT = result;
}
static void dfv_fop_release(struct guest_thread_struct *guest_thread,
struct dfv_op_args *req_args, struct dfv_op_args *res_args)
{
int remove_result;
int result = -EFAULT;
int sfd = OPREQ_SERVERFD;
struct guest_struct *guest = guest_thread->guest;
struct file *file;
struct inode *inode;
struct dfv_state *proxy = get_dfvstate(guest, sfd);
if (proxy == NULL) {
DFVPRINTK_ERR("Error: could not find dfvstate\n");
RELEASE_RESULT = -EINVAL;
return;
}
file = proxy->file;
inode = proxy->inode;
if ((!file) || (!file->f_op) || (!file->f_op->release)) {
DFVPRINTK_ERR("Error: device release function not specified\n");
RELEASE_RESULT = -EINVAL;
return;
}
result = file->f_op->release(inode, file);
remove_result = remove_dfvstate(guest, sfd);
if (remove_result) {
DFVPRINTK_ERR("Error: could not remove dfvstate\n");
}
guest_thread->num_open_fds--;
guest->num_open_fds--;
guest->guest_vm->num_open_fds--;
remove_guest_thread(guest_thread);
OPRES_POSITION = file->f_pos;
RELEASE_RESULT = result;
}
static void mark_file_as_dfv(struct file *filp)
{
struct fown_struct *fown;
if (filp) {
fown = &filp->f_owner;
fown->dfv_own = 1;
}
}
static void dfv_fop_fasync(struct guest_thread_struct *guest_thread,
struct dfv_op_args *req_args, struct dfv_op_args *res_args)
{
int result = -EFAULT;
int sfd = OPREQ_SERVERFD;
struct guest_struct *guest = guest_thread->guest;
struct file *file;
struct dfv_state *proxy = get_dfvstate(guest, sfd);
if (proxy == NULL) {
DFVPRINTK_ERR("Error: could not find dfvstate\n");
FASYNC_RESULT = -EINVAL;
return;
}
file = proxy->file;
mark_file_as_dfv(file);
if ((!file) || (!file->f_op) || (!file->f_op->fasync)) {
DFVPRINTK_ERR("Error: device fasync function not specified\n");
FASYNC_RESULT = -EINVAL;
return;
}
if (!file->private_data)
DFVPRINTK_ERR("Error: no private_data!\n");
result = file->f_op->fasync(proxy->fd, file, (int) FASYNC_ON);
FASYNC_RESULT = result;
}
/*
* FIXME: for now, we share this work between all guests. This is because
* the work is used for injecting sigio interrupts to the foreground guest
* only, and there will be no contention. If this model changes at any time, the
* work can be moved to the guest_struct or guest_vm_struct to be specific
* to each guest or guest_vm.
*/
static struct work_struct dfv_sigio_wq; /* work for sigio interrupts */
static void dfv_send_sigio_wq(struct work_struct *work)
{
struct guest_struct *fg_guest = get_fg_guest();
if (fg_guest)
fg_guest->guest_vm->send_sigio(fg_guest);
}
/*
* This routine gets called in interrupt context. Therefore, it cannot
* contain code that may sleep.
*/
void __dfv_send_sigio(struct fown_struct *fown, int fd, int band)
{
schedule_work(&dfv_sigio_wq);
}
#define UNSUPPORTED_FOP_FUNCTION(func) \
static void func(struct guest_thread_struct *guest_thread, \
struct dfv_op_args *req_args, struct dfv_op_args *res_args) \
{ \
DFVPRINTK_ERR("Error: unsupported file operation: " \
#func "\n"); \
}
UNSUPPORTED_FOP_FUNCTION(dfv_fop_aio_read)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_aio_write)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_readdir)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_ioctl)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_compat_ioctl)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_fsync)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_aio_fsync)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_lock)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_sendpage)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_get_unmapped_area)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_check_flags)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_flock)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_splice_write)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_splice_read)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_setlease)
UNSUPPORTED_FOP_FUNCTION(dfv_fop_fallocate)
/* Support for mmap ops: */
static void dfv_vmop_open(struct guest_thread_struct *guest_thread,
struct dfv_op_args *req_args, struct dfv_op_args *res_args)
{
struct guest_struct *guest = guest_thread->guest;
struct vm_area_struct * vma = NULL;
unsigned long startaddr = VM_OPEN_STARTADDR;
unsigned long endaddr = VM_OPEN_ENDADDR;
unsigned long vmflags = VM_OPEN_VMFLAGS;
unsigned long pgoff = VM_OPEN_PGOFF;
vma = dfv_find_vma_by_range(guest, startaddr, endaddr);
if (vma) {
if ((!vma->vm_ops) || (!vma->vm_ops->open)) {
DFVPRINTK_ERR("Error: vmop open not specified.\n");
return;
}
vma->vm_flags = vmflags;
vma->vm_page_prot = vm_get_page_prot(vmflags);
vma->vm_pgoff = pgoff;
vma->vm_ops->open(vma);
} else {
DFVPRINTK_ERR("Error: operation failed\n");
}
}
static void dfv_vmop_close(struct guest_thread_struct *guest_thread,
struct dfv_op_args *req_args, struct dfv_op_args *res_args)
{
struct guest_struct *guest = guest_thread->guest;
struct vm_area_struct * vma = NULL;
unsigned long startaddr = VM_CLOSE_STARTADDR;
unsigned long endaddr = VM_CLOSE_ENDADDR;
unsigned long vmflags = VM_CLOSE_VMFLAGS;
unsigned long pgoff = VM_CLOSE_PGOFF;
vma = dfv_find_vma_by_range(guest, startaddr, endaddr);
/*
* FIXME: This shouldn't happen, but shouldn't we call
* revert_pgtables() even if vma is NULL?
*/
if (vma) {
vma->vm_flags = vmflags;
vma->vm_page_prot = vm_get_page_prot(vmflags);
vma->vm_pgoff = pgoff;
if (vma->vm_ops && vma->vm_ops->close)
vma->vm_ops->close(vma);
guest_thread->guest_vm->revert_pgtables(guest_thread, guest,
vma, startaddr, endaddr-1);
dfv_remove_vma(guest, startaddr, endaddr);
} else {
DFVPRINTK_ERR("Error: operation failed\n");
}
}
static void dfv_eop_fault1(struct guest_thread_struct *guest_thread,
struct dfv_op_args *req_args, struct dfv_op_args *res_args)
{
int sfd = OPREQ_SERVERFD;
struct guest_struct *guest = guest_thread->guest;
unsigned long vma_startaddr = FAULT1_STARTADDR;
unsigned long vma_endaddr = FAULT1_ENDADDR;
unsigned long vma_vmflags = FAULT1_VMFLAGS;
unsigned long vma_pgoff = FAULT1_PGOFF;
struct dfv_state *proxy = get_dfvstate(guest, sfd);
if (proxy == NULL) {
DFVPRINTK_ERR("Error: could not find dfvstate\n");
FAULT1_RESULT = -EINVAL;
return;
}
guest_thread->dfvvma = dfv_find_vma_by_range(guest, vma_startaddr, vma_endaddr);
if (guest_thread->dfvvma) {
guest_thread->dfvvma->vm_flags = vma_vmflags;
guest_thread->dfvvma->vm_page_prot = vm_get_page_prot(vma_vmflags);
guest_thread->dfvvma->vm_pgoff = vma_pgoff;
FAULT1_RESULT = 0;
} else {
FAULT1_RESULT = VM_FAULT_ERROR;
}
}
static void dfv_eop_fault2(struct guest_thread_struct *guest_thread,
struct dfv_op_args *req_args, struct dfv_op_args *res_args)
{
int result = -EFAULT;
struct vm_fault vmf;
unsigned int vmf_flags = FAULT2_FLAGS;
pgoff_t vmf_pgoff = FAULT2_PGOFF;
unsigned long vmf_virtual_address = FAULT2_VIRTADDR;
int sfd = OPREQ_SERVERFD;
struct guest_struct *guest = guest_thread->guest;
struct dfv_state *proxy = get_dfvstate(guest, sfd);
if (proxy == NULL) {
DFVPRINTK_ERR("Error: could not find dfvstate\n");
FAULT2_RESULT = -EINVAL;
return;
}
if (guest_thread->dfvvma) {
if ((!guest_thread->dfvvma->vm_ops) ||
(!guest_thread->dfvvma->vm_ops->fault)) {
DFVPRINTK_ERR("Error: vmop fault not specified.\n");
FAULT2_RESULT = VM_FAULT_ERROR;
return;
}
vmf.virtual_address = (void __user *) vmf_virtual_address;
vmf.flags = vmf_flags;
vmf.pgoff = vmf_pgoff;
vmf.page = NULL;
result = guest_thread->dfvvma->vm_ops->fault(guest_thread->dfvvma,
&vmf);
/*
* the fault handler might return the page rather than
* installing it. In this case, we install the page here
*/
/* FIXME: we should merge the first two cases. */
if (result & VM_FAULT_LOCKED) {
int insert_ret;
unsigned long pfn;
pfn = page_to_pfn(vmf.page);
guest_thread->dfvvma->vm_flags |= VM_MIXEDMAP;
guest_thread->dfvvma->vm_flags |= VM_WRITE;
pgprot_val(guest_thread->dfvvma->vm_page_prot) |= VM_WRITE;
guest_thread->dfvvma->vm_mm = current->mm;
insert_ret = vm_insert_mixed(guest_thread->dfvvma,
vmf_virtual_address, pfn);
if (!insert_ret) {
FAULT2_RESULT = VM_FAULT_NOPAGE;