This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux-kernel-modifications.patch
2236 lines (2180 loc) · 66.7 KB
/
linux-kernel-modifications.patch
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/arch/x86/include/asm/kvm_page_track.h b/arch/x86/include/asm/kvm_page_track.h
index 87bd6025d91d..25281dfe27df 100644
--- a/arch/x86/include/asm/kvm_page_track.h
+++ b/arch/x86/include/asm/kvm_page_track.h
@@ -2,8 +2,14 @@
#ifndef _ASM_X86_KVM_PAGE_TRACK_H
#define _ASM_X86_KVM_PAGE_TRACK_H
+#include<linux/srcu.h>
+
enum kvm_page_track_mode {
KVM_PAGE_TRACK_WRITE,
+ KVM_PAGE_TRACK_ACCESS,
+ KVM_PAGE_TRACK_RESET_ACCESSED, //TODO: hacky, as this is not really for page tracking
+ KVM_PAGE_TRACK_EXEC,
+ KVM_PAGE_TRACK_RESET_EXEC,
KVM_PAGE_TRACK_MAX,
};
diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile
index c589db5d91b3..16fecec230da 100644
--- a/arch/x86/kvm/Makefile
+++ b/arch/x86/kvm/Makefile
@@ -3,6 +3,9 @@
ccflags-y += -I $(srctree)/arch/x86/kvm
ccflags-$(CONFIG_KVM_WERROR) += -Werror
+KBUILD_EXTRA_SYMBOLS := ../../../drivers/crypto/ccp/Module.symvers
+
+
ifeq ($(CONFIG_FRAME_POINTER),y)
OBJECT_FILES_NON_STANDARD_vmenter.o := y
endif
@@ -17,7 +20,7 @@ kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
kvm-y += x86.o emulate.o i8259.o irq.o lapic.o \
i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mtrr.o \
hyperv.o debugfs.o mmu/mmu.o mmu/page_track.o \
- mmu/spte.o
+ mmu/spte.o sev-step.o userspace_page_track_signals.o
kvm-$(CONFIG_X86_64) += mmu/tdp_iter.o mmu/tdp_mmu.o
kvm-$(CONFIG_KVM_XEN) += xen.o
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index b6a676ba1862..5ce3170f1421 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -53,6 +53,11 @@
#include <asm/kvm_page_track.h>
#include "trace.h"
+
+#include <linux/sev-step.h>
+#include <linux/userspace_page_track_signals.h>
+
+
extern bool itlb_multihit_kvm_mitigation;
static int __read_mostly nx_huge_pages = -1;
@@ -1084,7 +1089,8 @@ static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
}
/*
- * Write-protect on the specified @sptep, @pt_protect indicates whether
+ * Apply the protection mode specified in @mode to the specified @sptep,
+ * @pt_protect indicates whether
* spte write-protection is caused by protecting shadow page table.
*
* Note: write protection is difference between dirty logging and spte
@@ -1096,9 +1102,12 @@ static void drop_large_spte(struct kvm_vcpu *vcpu, u64 *sptep)
*
* Return true if tlb need be flushed.
*/
-static bool spte_write_protect(u64 *sptep, bool pt_protect)
+static bool spte_protect(u64 *sptep, bool pt_protect,
+ enum kvm_page_track_mode mode)
{
u64 spte = *sptep;
+ bool shouldFlush = false;
+
if (!is_writable_pte(spte) &&
!(pt_protect && spte_can_locklessly_be_made_writable(spte)))
@@ -1107,22 +1116,45 @@ static bool spte_write_protect(u64 *sptep, bool pt_protect)
rmap_printk("spte %p %llx\n", sptep, *sptep);
if (pt_protect)
- spte &= ~shadow_mmu_writable_mask;
- spte = spte & ~PT_WRITABLE_MASK;
+ spte &= ~EPT_SPTE_MMU_WRITABLE;
+
+ if(mode == KVM_PAGE_TRACK_WRITE) {
+ spte = spte & ~PT_WRITABLE_MASK;
+ shouldFlush = true;
+
+ } else if( mode == KVM_PAGE_TRACK_RESET_ACCESSED) {
+ spte = spte & ~PT_ACCESSED_MASK;
+ } else if(mode == KVM_PAGE_TRACK_ACCESS) {
+ spte = spte & ~PT_PRESENT_MASK;
+ spte = spte & ~PT_WRITABLE_MASK;
+ spte = spte & ~PT_USER_MASK;
+ spte = spte | (0x1ULL << PT64_NX_SHIFT);
+ shouldFlush = true;
+ } else if( mode == KVM_PAGE_TRACK_EXEC) {
+ spte = spte | (0x1ULL << PT64_NX_SHIFT); //nx bit is set, to prevent execution, not removed
+ shouldFlush = true;
+ } else if (mode == KVM_PAGE_TRACK_RESET_EXEC) {
+ spte = spte & (~(0x1ULL << PT64_NX_SHIFT));
+ shouldFlush = true;
+ }else {
+ printk(KERN_WARNING "spte_protect was called with invalid mode"
+ "parameter %d\n",mode);
+ }
- return mmu_spte_update(sptep, spte);
+ shouldFlush |= mmu_spte_update(sptep, spte);
+ return shouldFlush;
}
-static bool __rmap_write_protect(struct kvm *kvm,
+static bool __rmap_protect(struct kvm *kvm,
struct kvm_rmap_head *rmap_head,
- bool pt_protect)
+ bool pt_protect, enum kvm_page_track_mode mode)
{
u64 *sptep;
struct rmap_iterator iter;
bool flush = false;
for_each_rmap_spte(rmap_head, &iter, sptep)
- flush |= spte_write_protect(sptep, pt_protect);
+ flush |= spte_protect(sptep, pt_protect, mode);
return flush;
}
@@ -1192,7 +1224,7 @@ static void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
while (mask) {
rmap_head = __gfn_to_rmap(slot->base_gfn + gfn_offset + __ffs(mask),
PG_LEVEL_4K, slot);
- __rmap_write_protect(kvm, rmap_head, false);
+ __rmap_protect(kvm, rmap_head, false, KVM_PAGE_TRACK_WRITE);
/* clear the first set bit */
mask &= mask - 1;
@@ -1253,31 +1285,34 @@ int kvm_cpu_dirty_log_size(void)
return kvm_x86_ops.cpu_dirty_log_size;
}
-bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm,
- struct kvm_memory_slot *slot, u64 gfn)
+bool kvm_mmu_slot_gfn_protect(struct kvm *kvm,
+ struct kvm_memory_slot *slot, u64 gfn,
+ enum kvm_page_track_mode mode)
{
struct kvm_rmap_head *rmap_head;
int i;
- bool write_protected = false;
+ bool protected = false;
for (i = PG_LEVEL_4K; i <= KVM_MAX_HUGEPAGE_LEVEL; ++i) {
rmap_head = __gfn_to_rmap(gfn, i, slot);
- write_protected |= __rmap_write_protect(kvm, rmap_head, true);
+ protected |= __rmap_protect(kvm, rmap_head, true, mode);
}
+ //luca: this was not in sev-es kernel. Not sure what it does
if (is_tdp_mmu_enabled(kvm))
- write_protected |=
+ protected |=
kvm_tdp_mmu_write_protect_gfn(kvm, slot, gfn);
- return write_protected;
+ return protected;
}
-static bool rmap_write_protect(struct kvm_vcpu *vcpu, u64 gfn)
+static bool rmap_protect(struct kvm_vcpu *vcpu, u64 gfn,
+ enum kvm_page_track_mode mode)
{
struct kvm_memory_slot *slot;
slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
- return kvm_mmu_slot_gfn_write_protect(vcpu->kvm, slot, gfn);
+ return kvm_mmu_slot_gfn_protect(vcpu->kvm, slot, gfn, mode);
}
static bool kvm_zap_rmapp(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
@@ -1561,6 +1596,12 @@ static void kvm_mmu_free_page(struct kvm_mmu_page *sp)
kmem_cache_free(mmu_page_header_cache, sp);
}
+
+
+
+
+
+
static unsigned kvm_page_table_hashfn(gfn_t gfn)
{
return hash_64(gfn, KVM_MMU_HASH_SHIFT);
@@ -1915,7 +1956,7 @@ static void mmu_sync_children(struct kvm_vcpu *vcpu,
bool protected = false;
for_each_sp(pages, sp, parents, i)
- protected |= rmap_write_protect(vcpu, sp->gfn);
+ protected |= rmap_protect(vcpu, sp->gfn, KVM_PAGE_TRACK_WRITE);
if (protected) {
kvm_flush_remote_tlbs(vcpu->kvm);
@@ -2026,7 +2067,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,
* be inconsistent with guest page table.
*/
account_shadowed(vcpu->kvm, sp);
- if (level == PG_LEVEL_4K && rmap_write_protect(vcpu, gfn))
+ if (level == PG_LEVEL_4K && rmap_protect(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
kvm_flush_remote_tlbs_with_address(vcpu->kvm, gfn, 1);
if (level > PG_LEVEL_4K && need_sync)
@@ -3626,6 +3667,46 @@ static int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)
static bool page_fault_handle_page_track(struct kvm_vcpu *vcpu,
u32 error_code, gfn_t gfn)
{
+
+ {
+ int send_err;
+ uint64_t current_rip;
+ int have_rip;
+ int i;
+ bool was_tracked;
+ int modes[] = {KVM_PAGE_TRACK_WRITE,KVM_PAGE_TRACK_ACCESS,KVM_PAGE_TRACK_EXEC};
+
+ was_tracked = false;
+ for( i = 0; i < sizeof(modes) / sizeof(modes[0]); i++ ) {
+ if(kvm_page_track_is_active(vcpu,gfn,modes[i])) {
+ __untrack_single_page(vcpu, gfn, modes[i]);
+ was_tracked = true;
+ }
+ }
+ if( was_tracked ) {
+ //printk("\n##########################\ngot page fault on gfn 0x%llx\n",gfn);
+
+ //hen sev-es is enabled getting rip is expensive and only works when
+ //debug policy is set. Thus guard access with config check
+ have_rip = false;
+ if( uspt_should_get_rip() ) {
+ //! because 0 indicates "no error" but have_rip should be one if successfull
+ have_rip = (!sev_step_get_rip_kvm_vcpu(vcpu,¤t_rip));
+ }
+ if( uspt_batch_tracking_in_progress() ) {
+ if( (send_err = uspt_batch_tracking_save(gfn << PAGE_SHIFT,error_code,have_rip,current_rip)) ) {
+ printk_ratelimited("uspt_batch_tracking_save failed with %d\n##########################\n",send_err);
+ }
+ uspt_batch_tracking_handle_retrack(vcpu,gfn);
+ uspt_batch_tracking_inc_event_idx();
+ } else {
+ if( (send_err = uspt_send_and_block(gfn << PAGE_SHIFT,error_code,have_rip,current_rip)) ) {
+ printk("uspt_send_and_block failed with %d\n##########################\n",send_err);
+ }
+ }
+ }
+ }
+
if (unlikely(error_code & PFERR_RSVD_MASK))
return false;
@@ -3637,8 +3718,10 @@ static bool page_fault_handle_page_track(struct kvm_vcpu *vcpu,
* guest is writing the page which is write tracked which can
* not be fixed by page fault handler.
*/
- if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
+ if (kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_WRITE) || kvm_page_track_is_active(vcpu, gfn, KVM_PAGE_TRACK_ACCESS)) {
return true;
+ }
+
return false;
}
@@ -5603,7 +5686,7 @@ static bool slot_rmap_write_protect(struct kvm *kvm,
struct kvm_rmap_head *rmap_head,
struct kvm_memory_slot *slot)
{
- return __rmap_write_protect(kvm, rmap_head, false);
+ return __rmap_protect(kvm, rmap_head, false,KVM_PAGE_TRACK_WRITE);
}
void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h
index d64ccb417c60..7d7d06aa6843 100644
--- a/arch/x86/kvm/mmu/mmu_internal.h
+++ b/arch/x86/kvm/mmu/mmu_internal.h
@@ -122,8 +122,9 @@ bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
void kvm_mmu_gfn_disallow_lpage(struct kvm_memory_slot *slot, gfn_t gfn);
void kvm_mmu_gfn_allow_lpage(struct kvm_memory_slot *slot, gfn_t gfn);
-bool kvm_mmu_slot_gfn_write_protect(struct kvm *kvm,
- struct kvm_memory_slot *slot, u64 gfn);
+bool kvm_mmu_slot_gfn_protect(struct kvm *kvm,
+ struct kvm_memory_slot *slot, u64 gfn,
+ enum kvm_page_track_mode mode);
void kvm_flush_remote_tlbs_with_address(struct kvm *kvm,
u64 start_gfn, u64 pages);
diff --git a/arch/x86/kvm/mmu/page_track.c b/arch/x86/kvm/mmu/page_track.c
index 34bb0ec69bd8..aa67989833ab 100644
--- a/arch/x86/kvm/mmu/page_track.c
+++ b/arch/x86/kvm/mmu/page_track.c
@@ -99,9 +99,8 @@ void kvm_slot_page_track_add_page(struct kvm *kvm,
*/
kvm_mmu_gfn_disallow_lpage(slot, gfn);
- if (mode == KVM_PAGE_TRACK_WRITE)
- if (kvm_mmu_slot_gfn_write_protect(kvm, slot, gfn))
- kvm_flush_remote_tlbs(kvm);
+ if (kvm_mmu_slot_gfn_protect(kvm, slot, gfn, mode))
+ kvm_flush_remote_tlbs(kvm);
}
EXPORT_SYMBOL_GPL(kvm_slot_page_track_add_page);
diff --git a/arch/x86/kvm/sev-step.c b/arch/x86/kvm/sev-step.c
new file mode 100644
index 000000000000..442938ff11e1
--- /dev/null
+++ b/arch/x86/kvm/sev-step.c
@@ -0,0 +1,251 @@
+
+#include <linux/sev-step.h>
+#include <linux/smp.h>
+#include <linux/vmalloc.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+
+#include "kvm_cache_regs.h"
+#include "svm/svm.h"
+
+
+
+struct kvm* main_vm;
+EXPORT_SYMBOL(main_vm);
+
+//used to store performance counter values; 6 counters, 2 readings per counter
+uint64_t perf_reads[6][2];
+perf_ctl_config_t perf_configs[6];
+int perf_cpu;
+
+
+uint64_t perf_ctl_to_u64(perf_ctl_config_t * config) {
+
+ uint64_t result = 0;
+ result |= ( config->EventSelect & 0xffULL); //[7:0] in result and [7:0] in EventSelect
+ result |= ( (config->UintMask & 0xffULL) << 8 ); //[15:8]
+ result |= ( (config->OsUserMode & 0x3ULL) << 16); //[17:16]
+ result |= ( (config->Edge & 0x1ULL ) << 18 ); // 18
+ result |= ( (config->Int & 0x1ULL ) << 20 ); // 20
+ result |= ( (config->En & 0x1ULL ) << 22 ); //22
+ result |= ( (config->Inv & 0x1ULL ) << 23); //23
+ result |= ( (config->CntMask & 0xffULL) << 24); //[31:24]
+ result |= ( ( (config->EventSelect & 0xf00ULL) >> 8 ) << 32); //[35:32] in result and [11:8] in EventSelect
+ result |= ( (config->HostGuestOnly & 0x3ULL) << 40); // [41:40]
+
+ return result;
+
+}
+
+void write_ctl(perf_ctl_config_t * config, int cpu, uint64_t ctl_msr){
+ wrmsrl_on_cpu(cpu, ctl_msr, perf_ctl_to_u64(config)); //always returns zero
+}
+
+void read_ctr(uint64_t ctr_msr, int cpu, uint64_t* result) {
+ uint64_t tmp;
+ rdmsrl_on_cpu(cpu, ctr_msr, &tmp); //always returns zero
+ *result = tmp & ( (0x1ULL << 48) - 1);
+}
+
+void setup_perfs() {
+ int i;
+
+ perf_cpu = smp_processor_id();
+
+ for( i = 0; i < 6; i++) {
+ perf_configs[i].HostGuestOnly = 0x1; //0x1 means: count only guest
+ perf_configs[i].CntMask = 0x0;
+ perf_configs[i].Inv = 0x0;
+ perf_configs[i].En = 0x0;
+ perf_configs[i].Int = 0x0;
+ perf_configs[i].Edge = 0x0;
+ perf_configs[i].OsUserMode = 0x3; //0x3 means: count userland and kernel events
+ }
+
+ //remember to set .En to enable the individual counter
+
+ perf_configs[0].EventSelect = 0x0c0;
+ perf_configs[0].UintMask = 0x0;
+ perf_configs[0].En = 0x1;
+ write_ctl(&perf_configs[0],perf_cpu, CTL_MSR_0);
+
+ /*programm l2d hit from data cache miss perf for
+ cpu_probe_pointer_chasing_inplace without counting thread.
+ N.B. that this time we count host events
+ */
+ perf_configs[1].EventSelect = 0x064;
+ perf_configs[1].UintMask = 0x70;
+ perf_configs[1].En = 0x1;
+ perf_configs[1].HostGuestOnly = 0x2; //0x2 means: count only host events, as we do the chase here
+ write_ctl(&perf_configs[1],perf_cpu,CTL_MSR_1);
+}
+EXPORT_SYMBOL(setup_perfs);
+
+
+/*
+static int __my_sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
+ unsigned long dst, int size,
+ int *error);
+
+int my_sev_decrypt(struct kvm* kvm, void* dst_vaddr, void* src_vaddr, uint64_t dst_paddr, uint64_t src_paddr, uint64_t len, int* api_res) {
+
+ int call_res;
+ call_res = 0x1337;
+ *api_res = 0x1337;
+
+
+ if( dst_paddr % PAGE_SIZE != 0 || src_paddr % PAGE_SIZE != 0) {
+ printk("decrypt: for now, src_paddr, and dst_paddr must be page aligned");
+ return -1;
+ }
+
+ if( len > PAGE_SIZE ) {
+ printk("decrypt: for now, can be at most 4096 byte");
+ return -1;
+ }
+
+ memset(dst_vaddr,0,PAGE_SIZE);
+
+ //clflush_cache_range(src_vaddr, PAGE_SIZE);
+ //clflush_cache_range(dst_vaddr, PAGE_SIZE);
+ wbinvd_on_all_cpus();
+
+ call_res = __my_sev_issue_dbg_cmd(kvm, __sme_set(src_paddr),
+ __sme_set(dst_paddr), len, api_res);
+
+ return call_res;
+
+}
+EXPORT_SYMBOL(my_sev_decrypt);
+
+static int __my_sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
+ unsigned long dst, int size,
+ int *error)
+{
+ struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+ struct sev_data_dbg *data;
+ int ret;
+
+ data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
+ if (!data)
+ return -ENOMEM;
+
+ data->handle = sev->handle;
+ data->dst_addr = dst;
+ data->src_addr = src;
+ data->len = size;
+
+ //ret = sev_issue_cmd(kvm,
+ // SEV_CMD_DBG_DECRYPT,
+ // data, error);
+ ret = sev_do_cmd(SEV_CMD_DBG_DECRYPT, data, error);
+ kfree(data);
+ return ret;
+}
+
+int decrypt_vmsa(struct vcpu_svm* svm, struct vmcb_save_area* save_area) {
+
+ uint64_t src_paddr, dst_paddr;
+ void * dst_vaddr;
+ void * src_vaddr;
+ struct page * dst_page;
+ int call_res,api_res;
+ call_res = 1337;
+ api_res = 1337;
+
+ src_vaddr = svm->vmsa;
+ src_paddr = svm->vmcb->control.vmsa_pa;
+
+ if( src_paddr % 16 != 0) {
+ printk("decrypt_vmsa: src_paddr was not 16b aligned");
+ }
+
+ if( sizeof( struct vmcb_save_area) % 16 != 0 ) {
+ printk("decrypt_vmsa: size of vmcb_save_area is not 16 b aligned\n");
+ }
+
+ dst_page = alloc_page(GFP_KERNEL);
+ dst_vaddr = vmap(&dst_page, 1, 0, PAGE_KERNEL);
+ dst_paddr = page_to_pfn(dst_page) << PAGE_SHIFT;
+ memset(dst_vaddr,0,PAGE_SIZE);
+
+
+
+ if( dst_paddr % 16 != 0 ) {
+ printk("decrypt_vmsa: dst_paddr was not 16 byte aligned");
+ }
+
+ //printk("src_paddr = 0x%llx dst_paddr = 0x%llx\n", __sme_clr(src_paddr), __sme_clr(dst_paddr));
+ //printk("Sizeof vmcb_save_area is: 0x%lx\n", sizeof( struct vmcb_save_area) );
+
+
+ call_res = __my_sev_issue_dbg_cmd(svm->vcpu.kvm, __sme_set(src_paddr), __sme_set(dst_paddr), sizeof(struct vmcb_save_area), &api_res);
+
+
+ //printk("decrypt_vmsa: result of call was %d, result of api command was %d\n",call_res, api_res);
+
+ //todo error handling
+ if( api_res != 0 ) {
+ printk("api returned error code\n");
+ __free_page(dst_page);
+ return -1;
+ }
+
+ memcpy(save_area, dst_vaddr, sizeof( struct vmcb_save_area) );
+
+
+ __free_page(dst_page);
+
+ return 0;
+
+
+}
+
+
+//
+// Contains a switch to work SEV and SEV-ES
+ //
+uint64_t sev_step_get_rip(struct vcpu_svm* svm) {
+ struct vmcb_save_area* save_area;
+ struct kvm * kvm;
+ struct kvm_sev_info *sev;
+ uint64_t rip;
+
+
+ kvm = svm->vcpu.kvm;
+ sev = &to_kvm_svm(kvm)->sev_info;
+
+ //for sev-es we need to use the debug api, to decrypt the vmsa
+ if( sev->active && sev->es_active) {
+ int res;
+ save_area = vmalloc(sizeof(struct vmcb_save_area) );
+ memset(save_area,0, sizeof(struct vmcb_save_area));
+
+ res = decrypt_vmsa(svm, save_area);
+ if( res != 0) {
+ printk("sev_step_get_rip failed to decrypt\n");
+ return 0;
+ }
+
+ rip = save_area->rip;
+
+ vfree(save_area);
+ } else { //otherwise we can just access as plaintexts
+ rip = svm->vmcb->save.rip;
+ }
+ return rip;
+
+}
+EXPORT_SYMBOL(sev_step_get_rip);
+*/
+
+int sev_step_get_rip_kvm_vcpu(struct kvm_vcpu* vcpu,uint64_t *rip) {
+ /*
+ struct vcpu_svm *svm = container_of(vcpu, struct vcpu_svm, vcpu);
+ if( svm == NULL ) {
+ return 1;
+ }
+ (*rip) = sev_step_get_rip(svm);
+ */
+ return 0;
+}
diff --git a/arch/x86/kvm/userspace_page_track_signals.c b/arch/x86/kvm/userspace_page_track_signals.c
new file mode 100644
index 000000000000..cad505e7dbbf
--- /dev/null
+++ b/arch/x86/kvm/userspace_page_track_signals.c
@@ -0,0 +1,445 @@
+#include <linux/userspace_page_track_signals.h>
+#include <linux/kvm.h>
+#include <linux/timekeeping.h>
+#include <linux/uaccess.h>
+#include <stdbool.h>
+#include <linux/vmalloc.h>
+#include <linux/sev-step.h>
+#include <linux/printk.h>
+#include <linux/ratelimit.h>
+
+
+
+//crude sync mechanism. don't know a good way to act on errors yet.
+uint64_t last_sent_event_id = 1;
+uint64_t last_acked_event_id = 1;
+DEFINE_RWLOCK(event_lock);
+
+page_fault_event_t sent_event;
+static int have_event = 0;
+
+static bool get_rip = true;
+
+static int inited = 0;
+
+
+
+
+
+void uspt_clear(void) {
+ write_lock(&event_lock);
+ inited = 0;
+ last_sent_event_id = 1;
+ last_acked_event_id = 1;
+ have_event = 0;
+ get_rip = false;
+ write_unlock(&event_lock);
+}
+
+int uspt_initialize(int pid,bool should_get_rip) {
+ write_lock(&event_lock);
+
+ inited = 1;
+ last_sent_event_id = 1;
+ last_acked_event_id = 1;
+ have_event = 0;
+ get_rip = should_get_rip;
+ write_unlock(&event_lock);
+ return 0;
+}
+
+int uspt_is_initialiized() {
+ return inited;
+}
+
+bool uspt_should_get_rip() {
+ bool tmp;
+ read_lock(&event_lock);
+ tmp = get_rip;
+ read_unlock(&event_lock);
+ return tmp;
+}
+
+int uspt_send_and_block(uint64_t faulted_gpa, uint32_t error_code,bool have_rip,uint64_t rip) {
+ ktime_t abort_after;
+ page_fault_event_t message_for_user;
+
+ read_lock(&event_lock);
+ if( !uspt_is_initialiized() ) {
+ printk("userspace_page_track_signals: uspt_send_and_block : ctx not initialized!\n");
+ read_unlock(&event_lock);
+ return 1;
+ }
+ read_unlock(&event_lock);
+
+ write_lock(&event_lock);
+ if( last_sent_event_id != last_acked_event_id ) {
+ printk("event id_s out of sync, aborting. Fix this later\n");
+ write_unlock(&event_lock);
+ return 1;
+ } else {
+ //TODO: handle overflow
+ last_sent_event_id++;
+ }
+ message_for_user.id = last_sent_event_id;
+ message_for_user.faulted_gpa = faulted_gpa;
+ message_for_user.error_code = error_code;
+ message_for_user.have_rip_info = have_rip;
+ message_for_user.rip = rip;
+ message_for_user.ns_timestamp = ktime_get_real_ns();
+ message_for_user.have_retired_instructions = false;
+
+ //for poll based system;
+ have_event = 1;
+ sent_event = message_for_user;
+ //printk("uspt_send_and_block sending event %llu\n",sent_event.id);
+
+ write_unlock(&event_lock);
+
+
+ //wait for ack, but with tiemout. Otherwise small bugs in userland easily lead
+ //to a kernel hang
+ abort_after = ktime_get() + 1000000000ULL; //1 sec in nanosecond
+ while( !uspt_is_event_done(sent_event.id) ) {
+ if( ktime_get() > abort_after ) {
+ printk("Waiting for ack of event %llu timed out, continuing\n",sent_event.id);
+ return 3;
+ }
+ }
+ return 0;
+}
+
+int uspt_is_event_done(uint64_t id) {
+ int res;
+ read_lock(&event_lock);
+ res = last_acked_event_id >= id;
+ read_unlock(&event_lock);
+ return res;
+
+}
+
+int uspt_handle_poll_event(page_fault_event_t* userpace_mem) {
+ int err;
+
+ //most of the time we won't have an event
+ read_lock(&event_lock);
+ if( !have_event) {
+ read_unlock(&event_lock);
+ return KVM_USPT_POLL_EVENT_NO_EVENT;
+ }
+ read_unlock(&event_lock);
+
+ write_lock(&event_lock);
+ if( have_event) {
+ err = copy_to_user(userpace_mem, &sent_event, sizeof(page_fault_event_t));
+ have_event = 0;
+ } else {
+ err = KVM_USPT_POLL_EVENT_NO_EVENT;
+ }
+ write_unlock(&event_lock);
+ return err;
+
+}
+
+static int _uspt_handle_ack_event(uint64_t id) {
+ int err = 0;
+ write_lock(&event_lock);
+ if( id == last_sent_event_id) {
+ last_acked_event_id = last_sent_event_id;
+ //printk("successfull ack\n");
+ } else {
+ err = 1;
+ printk("last sent event id is %llu but received ack for %llu\n",last_sent_event_id,id);
+ }
+ write_unlock(&event_lock);
+ return err;
+
+
+}
+
+int uspt_handle_ack_event_ioctl(ack_event_t event) {
+ return _uspt_handle_ack_event(event.id);
+}
+
+
+
+typedef struct {
+ bool is_active;
+ int tracking_type;
+ bool retrack;
+
+ int perf_cpu;
+
+ uint64_t gfn_retrack_backlog[10];
+ int gfn_retrack_backlog_next_idx;
+
+ page_fault_event_t * events;
+ uint64_t event_next_idx;
+ uint64_t events_size;
+
+ bool error_occured;
+
+
+} batch_track_state_t;
+
+DEFINE_SPINLOCK(batch_track_state_lock);
+static batch_track_state_t batch_track_state;
+
+typedef struct {
+ uint64_t idx_for_last_perf_reading;
+ uint64_t last_perf_reading;
+ uint64_t delta_valid_idx;
+ uint64_t delta;
+} perf_state_t;
+
+perf_state_t perf_state;
+
+//setup perf_state and program retired instruction performance counter
+void _perf_state_setup_retired_instructions(void) {
+ perf_ctl_config_t retired_instructions_perf_config;
+ retired_instructions_perf_config.HostGuestOnly = 0x1; //0x1 means: count only guest
+ retired_instructions_perf_config.CntMask = 0x0;
+ retired_instructions_perf_config.Inv = 0x0;
+ retired_instructions_perf_config.Int = 0x0;
+ retired_instructions_perf_config.Edge = 0x0;
+ retired_instructions_perf_config.OsUserMode = 0x3; //0x3 means: count kern and user events
+ retired_instructions_perf_config.EventSelect = 0x0c0;
+ retired_instructions_perf_config.UintMask = 0x0;
+ retired_instructions_perf_config.En = 0x1;
+ write_ctl(&retired_instructions_perf_config,batch_track_state.perf_cpu, CTL_MSR_0);
+}
+
+
+//get retired instructions between current_event_idx-1 and current_event_idx
+//value is cached for multiple calls to the same current_event_idx
+uint64_t _perf_state_update_and_get_delta(uint64_t current_event_idx) {
+ uint64_t current_value;
+
+ //check if value is "cached"
+ if( perf_state.delta_valid_idx == current_event_idx) {
+ if( current_event_idx == 0) {
+ read_ctr(CTR_MSR_0, batch_track_state.perf_cpu, ¤t_value);
+ perf_state.idx_for_last_perf_reading = current_event_idx;
+ perf_state.last_perf_reading = current_event_idx;
+ }
+ return perf_state.delta;
+ }
+
+ //otherwise update, but logic is only valid for two consecutive events
+ if (current_event_idx != perf_state.idx_for_last_perf_reading+1) {
+ printk_ratelimited(KERN_CRIT "_perf_state_update_and_get_delta: last reading was for idx %llu but was queried for %llu\n",perf_state.idx_for_last_perf_reading,current_event_idx);
+ }
+
+ read_ctr(CTR_MSR_0, batch_track_state.perf_cpu, ¤t_value);
+ perf_state.delta = (current_value - perf_state.last_perf_reading);
+ perf_state.delta_valid_idx = current_event_idx;
+
+ perf_state.idx_for_last_perf_reading = current_event_idx;
+ perf_state.last_perf_reading = current_value;
+
+ return perf_state.delta;
+}
+
+void uspt_batch_tracking_inc_event_idx(void) {
+ spin_lock(&batch_track_state_lock);
+ batch_track_state.event_next_idx++;
+ spin_unlock(&batch_track_state_lock);
+}
+
+int uspt_batch_tracking_start(int tracking_type,uint64_t expected_events, int perf_cpu,bool retrack) {
+ page_fault_event_t* events;
+ uint64_t buffer_size;
+ uint64_t idx = 0;
+ spin_lock(&batch_track_state_lock);
+ if( batch_track_state.is_active ) {
+ printk("userspace_page_track_signals: overwriting active batch track config!\n");
+ if( batch_track_state.events != NULL ) {
+ vfree(batch_track_state.events);
+ }
+ }
+ batch_track_state.is_active = false;
+ spin_unlock(&batch_track_state_lock);
+
+ buffer_size = expected_events*sizeof(page_fault_event_t);
+ printk("uspt_batch_tracking_start trying to alloc %llu bytes buffer for events\n",buffer_size);
+ events = vmalloc(buffer_size);
+ if( events == NULL) {
+ printk("userspace_page_track_signals: faperf_cpuiled to alloc %llu bytes for event buffer\n",buffer_size);
+ return 1; //note: lock not held here
+ }
+
+ //access each element once to force them into memory, improving performance
+ //during tracking
+ for( idx = 0; idx < expected_events*sizeof(page_fault_event_t);idx++) {
+ ((volatile uint8_t*)events)[idx] = 0;
+ }
+
+ perf_state.idx_for_last_perf_reading = 0;
+ perf_state.last_perf_reading = 0;
+ perf_state.delta_valid_idx = 0;
+ perf_state.delta = 0;
+ _perf_state_setup_retired_instructions();
+
+
+ spin_lock(&batch_track_state_lock);
+
+ batch_track_state.perf_cpu = perf_cpu;
+ batch_track_state.retrack = retrack;
+
+ batch_track_state.events = events;
+ batch_track_state.event_next_idx = 0;
+ batch_track_state.events_size = expected_events;
+
+ batch_track_state.gfn_retrack_backlog_next_idx = 0;
+ batch_track_state.tracking_type = tracking_type;
+ batch_track_state.error_occured = false;
+
+ batch_track_state.is_active = true;
+
+ spin_unlock(&batch_track_state_lock);
+
+ return 0;
+
+
+}
+
+void uspt_batch_tracking_handle_retrack(struct kvm_vcpu* vcpu, uint64_t current_fault_gfn) {
+ int i;
+ uint64_t ret_instr_delta;
+
+ spin_lock(&batch_track_state_lock);
+
+ if( !batch_track_state.retrack ) {
+ spin_unlock(&batch_track_state_lock);
+ return;
+ }
+
+ if( smp_processor_id() != batch_track_state.perf_cpu) {
+ printk("uspt_batch_tracking_handle_retrack: perf was programmed on logical cpu %d but handler was called on %d. Did you forget to pin the vcpu thread?\n",batch_track_state.perf_cpu,smp_processor_id());
+ }
+ ret_instr_delta = _perf_state_update_and_get_delta(batch_track_state.event_next_idx);
+
+
+ //faulting instructions is probably the same as on last fault
+ //try to add current fault to retrack log and return
+ //for first event idx we do not have a valid ret_instr_delta. Retracking for the frist time is fine, if we loop, we end up here again but with a valid delta on one of the next event
+ if( (ret_instr_delta < 2) && ( batch_track_state.event_next_idx != 0) ) {
+ int next_idx = batch_track_state.gfn_retrack_backlog_next_idx;
+ if( next_idx >= sizeof(batch_track_state.gfn_retrack_backlog)/sizeof(batch_track_state.gfn_retrack_backlog[0])) {
+ printk("uspt_batch_tracking_handle_retrack: retrack backlog full, dropping retrack for fault at 0x%llx\n",current_fault_gfn);
+ } else {
+ batch_track_state.gfn_retrack_backlog[next_idx] = current_fault_gfn;
+ batch_track_state.gfn_retrack_backlog_next_idx++;
+ }
+
+ spin_unlock(&batch_track_state_lock);
+ return;
+ }
+
+ //made progress, retrack everything in backlog and reset idx
+ for( i = 0; i < batch_track_state.gfn_retrack_backlog_next_idx;i++) {
+ __track_single_page(vcpu,batch_track_state.gfn_retrack_backlog[i],batch_track_state.tracking_type);
+ }
+
+ //add current fault to list
+ batch_track_state.gfn_retrack_backlog[0] = current_fault_gfn;
+ batch_track_state.gfn_retrack_backlog_next_idx = 1;
+
+ spin_unlock(&batch_track_state_lock);
+
+}
+
+int uspt_batch_tracking_save(uint64_t faulted_gpa, uint32_t error_code, bool have_rip,uint64_t rip) {
+ uint64_t ret_instr_delta;
+ page_fault_event_t* event;
+
+ spin_lock(&batch_track_state_lock);
+
+ if( !batch_track_state.is_active ) {
+ printk_ratelimited("userspace_page_track_signals: got save but batch tracking is not active!\n");
+ batch_track_state.error_occured = true;
+ spin_unlock(&batch_track_state_lock);
+ return 1;
+ }
+
+
+ if( batch_track_state.event_next_idx >= batch_track_state.events_size) {
+ printk_ratelimited("userspace_page_track_signals: events buffer is full!\n");
+ batch_track_state.error_occured = true;
+ spin_unlock(&batch_track_state_lock);
+ return 1;
+ }
+
+ if( smp_processor_id() != batch_track_state.perf_cpu) {
+ printk("uspt_batch_tracking_handle_retrack: perf was programmed on logical cpu %d but handler was called on %d. Did you forget to pin the vcpu thread?\n",batch_track_state.perf_cpu,smp_processor_id());
+ }
+ ret_instr_delta = _perf_state_update_and_get_delta(batch_track_state.event_next_idx);
+
+
+ if( batch_track_state.events == NULL ) {
+ printk(KERN_CRIT "userspace_page_track_signals: events buf was NULL but \"is_active\" was set! This should never happen!!!\n");
+ spin_unlock(&batch_track_state_lock);
+ return 1;
+ }
+
+ event = &batch_track_state.events[batch_track_state.event_next_idx];
+ event->id = batch_track_state.event_next_idx;
+ event->faulted_gpa = faulted_gpa;
+ event->error_code = error_code;
+ event->have_rip_info = have_rip;
+ event->rip = rip;
+ event->ns_timestamp = ktime_get_real_ns();
+ event->have_retired_instructions = true;
+ event->retired_instructions = ret_instr_delta;
+
+//old inc was here
+
+ if(batch_track_state.gfn_retrack_backlog_next_idx > (sizeof(batch_track_state.gfn_retrack_backlog)/sizeof(batch_track_state.gfn_retrack_backlog[0])) ) {
+ printk_ratelimited("userspace_page_track_signals: gfn retrack backlog overflow!\n");
+ batch_track_state.error_occured = true;
+ spin_unlock(&batch_track_state_lock);
+ return 1;
+ }
+
+ spin_unlock(&batch_track_state_lock);
+ return 0;
+}
+
+int uspt_batch_tracking_stop(page_fault_event_t* results, uint64_t len, bool* error_occured) {
+ spin_lock(&batch_track_state_lock);
+ if( !batch_track_state.is_active ) {
+ printk("userspace_page_track_signals: batch tracking not active\n");
+ spin_unlock(&batch_track_state_lock);
+ return 1;
+
+ }
+ batch_track_state.is_active = false;
+
+ if( len > batch_track_state.event_next_idx) {
+ printk("userspace_page_track_signals: requested %llu events but got only %llu\n",len,batch_track_state.event_next_idx );
+ spin_unlock(&batch_track_state_lock);