-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path0001-Support-KRR-guest-agent.patch
More file actions
2427 lines (2347 loc) · 62.9 KB
/
0001-Support-KRR-guest-agent.patch
File metadata and controls
2427 lines (2347 loc) · 62.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From 3e29373aa5f4707950b85aff92fdcdfeb638c73a Mon Sep 17 00:00:00 2001
From: Silver Zhang <silvertianren@gmail.com>
Date: Fri, 18 Apr 2025 22:36:08 -0400
Subject: [PATCH] Support KRR guest agent
---
arch/x86/entry/common.c | 3 +
arch/x86/entry/vdso/vclock_gettime.c | 2 +
arch/x86/include/asm/archrandom.h | 4 +
arch/x86/include/asm/kernel_rr.h | 165 +++++++
arch/x86/include/asm/msr.h | 56 ++-
arch/x86/include/asm/pgtable_64.h | 4 +-
arch/x86/include/asm/syscall.h | 1 +
arch/x86/include/asm/traps.h | 2 +
arch/x86/include/asm/uaccess.h | 8 +
arch/x86/include/asm/uaccess_64.h | 10 +-
arch/x86/kernel/Makefile | 7 +
arch/x86/kernel/kernel_rr.c | 644 +++++++++++++++++++++++++++
arch/x86/kernel/kvm_ivshmem.c | 593 ++++++++++++++++++++++++
arch/x86/kernel/rr_serialize.c | 127 ++++++
arch/x86/kernel/traps.c | 17 +-
arch/x86/mm/fault.c | 2 +
drivers/char/random.c | 12 +
fs/proc/task_mmu.c | 15 +-
include/asm-generic/qspinlock.h | 3 +
include/linux/highmem-internal.h | 8 +-
include/linux/kernel.h | 3 +
include/linux/pgtable.h | 2 +-
include/linux/uaccess.h | 8 +
include/uapi/linux/kvm_para.h | 2 +
init/main.c | 3 +
io_uring/io_uring.c | 2 +-
io_uring/io_uring.h | 4 +-
kernel/entry/common.c | 3 +
kernel/sched/idle.c | 5 +
kernel/smp.c | 4 +
kernel/stop_machine.c | 3 +
lib/iov_iter.c | 2 +
lib/strncpy_from_user.c | 1 +
mm/gup.c | 2 +-
mm/memory.c | 15 +-
35 files changed, 1718 insertions(+), 24 deletions(-)
create mode 100644 arch/x86/include/asm/kernel_rr.h
create mode 100644 arch/x86/kernel/kernel_rr.c
create mode 100644 arch/x86/kernel/kvm_ivshmem.c
create mode 100644 arch/x86/kernel/rr_serialize.c
diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c
index 6c2826417..b018a695a 100644
--- a/arch/x86/entry/common.c
+++ b/arch/x86/entry/common.c
@@ -72,6 +72,8 @@ static __always_inline bool do_syscall_x32(struct pt_regs *regs, int nr)
__visible noinstr void do_syscall_64(struct pt_regs *regs, int nr)
{
+ rr_handle_syscall(regs);
+
add_random_kstack_offset();
nr = syscall_enter_from_user_mode(regs, nr);
@@ -84,6 +86,7 @@ __visible noinstr void do_syscall_64(struct pt_regs *regs, int nr)
instrumentation_end();
syscall_exit_to_user_mode(regs);
+ rr_release_smp_exec(CTX_SYSCALL);
}
#endif
diff --git a/arch/x86/entry/vdso/vclock_gettime.c b/arch/x86/entry/vdso/vclock_gettime.c
index 7d70935b6..ebe02c9a3 100644
--- a/arch/x86/entry/vdso/vclock_gettime.c
+++ b/arch/x86/entry/vdso/vclock_gettime.c
@@ -8,6 +8,8 @@
* 32 Bit compat layer by Stefani Seibold <stefani@seibold.net>
* sponsored by Rohde & Schwarz GmbH & Co. KG Munich/Germany
*/
+#define VDSO_BUILD 0
+
#include <linux/time.h>
#include <linux/kernel.h>
#include <linux/types.h>
diff --git a/arch/x86/include/asm/archrandom.h b/arch/x86/include/asm/archrandom.h
index 02bae8e07..6648311ff 100644
--- a/arch/x86/include/asm/archrandom.h
+++ b/arch/x86/include/asm/archrandom.h
@@ -12,6 +12,7 @@
#include <asm/processor.h>
#include <asm/cpufeature.h>
+#include <asm/kernel_rr.h>
#define RDRAND_RETRY_LOOPS 10
@@ -37,6 +38,9 @@ static inline bool __must_check rdseed_long(unsigned long *v)
asm volatile("rdseed %[out]"
CC_SET(c)
: CC_OUT(c) (ok), [out] "=r" (*v));
+
+ rr_record_rdseed(*v);
+
return ok;
}
diff --git a/arch/x86/include/asm/kernel_rr.h b/arch/x86/include/asm/kernel_rr.h
new file mode 100644
index 000000000..684e70811
--- /dev/null
+++ b/arch/x86/include/asm/kernel_rr.h
@@ -0,0 +1,165 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_KERNEL_RR_H
+#define _ASM_X86_KERNEL_RR_H
+#include <linux/types.h>
+#include <linux/kvm.h>
+
+#define EVENT_TYPE_INTERRUPT 0
+#define EVENT_TYPE_EXCEPTION 1
+#define EVENT_TYPE_SYSCALL 2
+#define EVENT_TYPE_CFU 4
+#define EVENT_TYPE_RANDOM 5
+#define EVENT_TYPE_RDTSC 6
+#define EVENT_TYPE_GFU 8
+#define EVENT_TYPE_STRNLEN 9
+#define EVENT_TYPE_RDSEED 10
+#define EVENT_TYPE_RELEASE 11
+#define EVENT_TYPE_PTE 14
+
+#define CFU_BUFFER_SIZE 4096
+
+#define CTX_SYSCALL 0
+#define CTX_INTR 1
+#define CTX_SWITCH 2
+#define CTX_IDLE 3
+#define CTX_LOCKWAIT 4
+#define CTX_EXCP 5
+
+
+typedef struct {
+ int id;
+ unsigned long value;
+ unsigned long inst_cnt;
+ unsigned long rip;
+} rr_io_input;
+
+typedef struct {
+ int id;
+ int vector;
+ unsigned long ecx;
+ int from;
+ unsigned long spin_count;
+ unsigned long inst_cnt;
+ unsigned long rip;
+ struct kvm_regs regs;
+} rr_interrupt;
+
+typedef struct {
+ int id;
+ unsigned long val;
+ unsigned long ptr;
+ int size;
+} rr_gfu;
+
+typedef struct {
+ int id;
+ unsigned long src_addr;
+ unsigned long dest_addr;
+ unsigned long len;
+ unsigned long rdx;
+ unsigned char *data;
+} rr_cfu;
+
+typedef struct {
+ int id;
+ int exception_index;
+ int error_code;
+ unsigned long cr2, cr3;
+ struct kvm_regs regs;
+ unsigned long spin_count;
+ unsigned long inst_cnt;
+} rr_exception;
+
+typedef struct {
+ int id;
+ struct kvm_regs regs;
+ unsigned long kernel_gsbase, msr_gsbase, cr3;
+ unsigned long spin_count;
+} rr_syscall;
+
+typedef struct {
+ int id;
+ unsigned long buf;
+ unsigned long len;
+ __u8 data[1024];
+} rr_random;
+
+typedef struct rr_event_log_guest_t {
+ int type;
+ int id;
+ union {
+ rr_interrupt interrupt;
+ rr_exception exception;
+ rr_syscall syscall;
+ rr_io_input io_input;
+ rr_cfu cfu;
+ rr_random rand;
+ rr_gfu gfu;
+ } event;
+ unsigned long inst_cnt;
+ unsigned long rip;
+} rr_event_log_guest;
+
+
+typedef struct rr_event_guest_queue_header_t {
+ unsigned int current_pos;
+ unsigned int total_pos;
+ unsigned int header_size;
+ unsigned int entry_size;
+ unsigned int rr_enabled;
+ unsigned long current_byte;
+ unsigned long total_size;
+ unsigned long rotated_bytes;
+} rr_event_guest_queue_header;
+
+typedef struct rr_event_entry_header_t {
+ int type;
+} rr_event_entry_header;
+
+void rr_record_rdseed(unsigned long val);
+void *rr_alloc_new_event_entry(unsigned long size, int type);
+bool rr_queue_inited(void);
+int rr_enabled(void);
+void rr_record_gfu(unsigned long val, unsigned long ptr);
+void rr_record_random(void *buf, int len);
+void rr_record_release(int cpu_id);
+
+void init_smp_exec_lock(void);
+long rr_acquire_smp_exec(int ctx, int disable_irq);
+void rr_release_smp_exec(int ctx);
+long rr_do_acquire_smp_exec(int disable_irq, int cpu_id, int ctx);
+void rr_handle_irqentry(void);
+// bool rr_is_switch_to_user(struct task_struct *task, bool before);
+void rr_bug(int expected, int cur);
+void rr_set_lock_owner(int owner);
+void rr_begin_cfu(const void __user *from, void *to, long unsigned int n);
+void *rr_gfu_begin(const void __user *ptr, int size, int align);
+void rr_record_gfu_end(unsigned long val, void *event);
+void *rr_cfu_begin(const void __user *from, void *to, long unsigned int n);
+void rr_cfu_end(void *addr, void *to, long unsigned int n);
+void *rr_record_pte_begin(unsigned long ptr);
+void rr_record_pte_end(void *event, unsigned long pte_val);
+unsigned long rr_record_pte_clear(pte_t *xp);
+pte_t rr_read_pte(pte_t *pte);
+pte_t rr_read_pte_once(pte_t *pte);
+unsigned long *rr_rdtsc_begin(void);
+
+/* === io uring related functions === */
+void rr_begin_record_io_uring(void);
+void rr_end_record_io_uring(unsigned int value, unsigned long addr);
+void rr_record_io_uring_entry(void *data, int size, unsigned long addr);
+
+#define RECORD_SQ_TAIL(stmt, addr) ({ \
+ unsigned int tail; \
+ rr_begin_record_io_uring(); \
+ tail = (stmt); \
+ rr_end_record_io_uring(tail, (unsigned long)addr); \
+ tail; \
+})
+
+#define RECORD_IO_URING_ENTRY(data, size) ({ \
+ rr_record_io_uring_entry((void *)data, size, (unsigned long)data); \
+ data; \
+})
+
+#endif /* _ASM_X86_KERNEL_RR_H */
diff --git a/arch/x86/include/asm/msr.h b/arch/x86/include/asm/msr.h
index 65ec1965c..9630fe384 100644
--- a/arch/x86/include/asm/msr.h
+++ b/arch/x86/include/asm/msr.h
@@ -6,6 +6,30 @@
#ifndef __ASSEMBLY__
+#ifndef VDSO_BUILD
+#include <asm/pgtable_64_types.h>
+#include <asm/kernel_rr.h>
+#include <linux/irqflags.h>
+
+#define RR_RDTSC_BEGIN(expr) ({ \
+ unsigned long flags; \
+ void *event; \
+ if (!rr_queue_inited() || !rr_enabled()) { \
+ expr; \
+ } else { \
+ local_irq_save(flags); \
+ event = rr_alloc_new_event_entry(sizeof(rr_io_input), EVENT_TYPE_RDTSC); \
+ if (event == NULL) { \
+ panic("Failed to allocate entry"); \
+ } \
+ input = (rr_io_input *)event; \
+ input->id = 0; \
+ expr; \
+ local_irq_restore(flags); \
+ } \
+})
+
+#endif
#include <asm/asm.h>
#include <asm/errno.h>
#include <asm/cpumask.h>
@@ -181,9 +205,21 @@ static __always_inline unsigned long long rdtsc(void)
{
DECLARE_ARGS(val, low, high);
- asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
+#ifndef VDSO_BUILD
+ rr_io_input *input = NULL;
+
+ RR_RDTSC_BEGIN(asm volatile("rdtsc" : EAX_EDX_RET(val, low, high)));
+ if (unlikely(input == NULL)) {
+ return EAX_EDX_VAL(val, low, high);
+ }
+
+ input->value = EAX_EDX_VAL(val, low, high);
+ return input->value;
+#else
+ asm volatile("rdtsc" : EAX_EDX_RET(val, low, high));
return EAX_EDX_VAL(val, low, high);
+#endif
}
/**
@@ -198,6 +234,9 @@ static __always_inline unsigned long long rdtsc_ordered(void)
{
DECLARE_ARGS(val, low, high);
+#ifndef VDSO_BUILD
+ rr_io_input *input = NULL;
+
/*
* The RDTSC instruction is not ordered relative to memory
* access. The Intel SDM and the AMD APM are both vague on this
@@ -212,6 +251,20 @@ static __always_inline unsigned long long rdtsc_ordered(void)
* Thus, use the preferred barrier on the respective CPU, aiming for
* RDTSCP as the default.
*/
+ RR_RDTSC_BEGIN(asm volatile(ALTERNATIVE_2("rdtsc",
+ "lfence; rdtsc", X86_FEATURE_LFENCE_RDTSC,
+ "rdtscp", X86_FEATURE_RDTSCP)
+ : EAX_EDX_RET(val, low, high)
+ /* RDTSCP clobbers ECX with MSR_TSC_AUX. */
+ :: "ecx"));
+
+ if (unlikely(input == NULL)) {
+ return EAX_EDX_VAL(val, low, high);
+ }
+
+ input->value = EAX_EDX_VAL(val, low, high);
+ return input->value;
+#else
asm volatile(ALTERNATIVE_2("rdtsc",
"lfence; rdtsc", X86_FEATURE_LFENCE_RDTSC,
"rdtscp", X86_FEATURE_RDTSCP)
@@ -220,6 +273,7 @@ static __always_inline unsigned long long rdtsc_ordered(void)
:: "ecx");
return EAX_EDX_VAL(val, low, high);
+#endif
}
static inline unsigned long long native_read_pmc(int counter)
diff --git a/arch/x86/include/asm/pgtable_64.h b/arch/x86/include/asm/pgtable_64.h
index e479491da..7f942c4cc 100644
--- a/arch/x86/include/asm/pgtable_64.h
+++ b/arch/x86/include/asm/pgtable_64.h
@@ -15,6 +15,7 @@
#include <linux/bitops.h>
#include <linux/threads.h>
#include <asm/fixmap.h>
+#include <asm/kernel_rr.h>
extern p4d_t level4_kernel_pgt[512];
extern p4d_t level4_ident_pgt[512];
@@ -91,7 +92,8 @@ static inline void native_pmd_clear(pmd_t *pmd)
static inline pte_t native_ptep_get_and_clear(pte_t *xp)
{
#ifdef CONFIG_SMP
- return native_make_pte(xchg(&xp->pte, 0));
+ pteval_t p = rr_record_pte_clear(xp);
+ return native_make_pte(p);
#else
/* native_local_ptep_get_and_clear,
but duplicated because of cyclic dependency */
diff --git a/arch/x86/include/asm/syscall.h b/arch/x86/include/asm/syscall.h
index 5b85987a5..f1db101b7 100644
--- a/arch/x86/include/asm/syscall.h
+++ b/arch/x86/include/asm/syscall.h
@@ -129,6 +129,7 @@ static inline int syscall_get_arch(struct task_struct *task)
void do_syscall_64(struct pt_regs *regs, int nr);
void do_int80_syscall_32(struct pt_regs *regs);
long do_fast_syscall_32(struct pt_regs *regs);
+void rr_handle_syscall(struct pt_regs *regs);
#endif /* CONFIG_X86_32 */
diff --git a/arch/x86/include/asm/traps.h b/arch/x86/include/asm/traps.h
index 47ecfff2c..5233eb15e 100644
--- a/arch/x86/include/asm/traps.h
+++ b/arch/x86/include/asm/traps.h
@@ -47,4 +47,6 @@ void __noreturn handle_stack_overflow(struct pt_regs *regs,
struct stack_info *info);
#endif
+void rr_handle_exception(struct pt_regs *regs, int vector, int error_code, unsigned long cr2);
+
#endif /* _ASM_X86_TRAPS_H */
diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index 1cc756eaf..89b2cbad6 100644
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -12,6 +12,7 @@
#include <asm/page.h>
#include <asm/smap.h>
#include <asm/extable.h>
+#include <asm/kernel_rr.h>
#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
static inline bool pagefault_disabled(void);
@@ -98,13 +99,16 @@ extern int __get_user_bad(void);
#define do_get_user_call(fn,x,ptr) \
({ \
int __ret_gu; \
+ void *rr_event; \
register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \
__chk_user_ptr(ptr); \
+ rr_event = rr_gfu_begin(ptr, sizeof(*(ptr)), 1); \
asm volatile("call __" #fn "_%P4" \
: "=a" (__ret_gu), "=r" (__val_gu), \
ASM_CALL_CONSTRAINT \
: "0" (ptr), "i" (sizeof(*(ptr)))); \
instrument_get_user(__val_gu); \
+ rr_record_gfu_end(__val_gu, rr_event); \
(x) = (__force __typeof__(*(ptr))) __val_gu; \
__builtin_expect(__ret_gu, 0); \
})
@@ -563,11 +567,14 @@ static __must_check __always_inline bool user_access_begin(const void __user *pt
__put_user_size((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)), label)
#ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT
+
#define unsafe_get_user(x, ptr, err_label) \
do { \
__inttype(*(ptr)) __gu_val; \
+ void *event = rr_gfu_begin(ptr, sizeof(*(ptr)), 0); \
__get_user_size(__gu_val, (ptr), sizeof(*(ptr)), err_label); \
(x) = (__force __typeof__(*(ptr)))__gu_val; \
+ rr_record_gfu_end(__gu_val, event); \
} while (0)
#else // !CONFIG_CC_HAS_ASM_GOTO_OUTPUT
#define unsafe_get_user(x, ptr, err_label) \
@@ -576,6 +583,7 @@ do { \
__inttype(*(ptr)) __gu_val; \
__get_user_size(__gu_val, (ptr), sizeof(*(ptr)), __gu_err); \
(x) = (__force __typeof__(*(ptr)))__gu_val; \
+ rr_record_gfu(__gu_val, (unsigned long)ptr); \
if (unlikely(__gu_err)) goto err_label; \
} while (0)
#endif // CONFIG_CC_HAS_ASM_GOTO_OUTPUT
diff --git a/arch/x86/include/asm/uaccess_64.h b/arch/x86/include/asm/uaccess_64.h
index d13d71af5..52a6082e3 100644
--- a/arch/x86/include/asm/uaccess_64.h
+++ b/arch/x86/include/asm/uaccess_64.h
@@ -11,6 +11,7 @@
#include <asm/alternative.h>
#include <asm/cpufeatures.h>
#include <asm/page.h>
+#include <asm/kernel_rr.h>
/*
* Copy To/From Userspace
@@ -49,7 +50,14 @@ copy_user_generic(void *to, const void *from, unsigned len)
static __always_inline __must_check unsigned long
raw_copy_from_user(void *dst, const void __user *src, unsigned long size)
{
- return copy_user_generic(dst, (__force void *)src, size);
+ unsigned long ret;
+ void *addr;
+
+ addr = rr_cfu_begin(src, dst, size);
+ ret = copy_user_generic(dst, (__force void *)src, size);
+ rr_cfu_end(addr, dst, size - ret);
+
+ return ret;
}
static __always_inline __must_check unsigned long
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index f901658d9..a8c6fa3e5 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -32,6 +32,10 @@ KCSAN_SANITIZE := n
KMSAN_SANITIZE_head$(BITS).o := n
KMSAN_SANITIZE_nmi.o := n
+KASAN_SANITIZE_kernel_rr.o := n
+KASAN_SANITIZE_rr_serialize.o := n
+KASAN_SANITIZE_kvm_ivshmem.o := n
+
# If instrumentation of this dir is enabled, boot hangs during first second.
# Probably could be more selective here, but note that files related to irqs,
# boot, dumpstack/stacktrace, etc are either non-interesting or can lead to
@@ -69,6 +73,9 @@ obj-y += static_call.o
obj-y += process.o
obj-y += fpu/
obj-y += ptrace.o
+obj-y += rr_serialize.o
+obj-y += kernel_rr.o
+obj-y += kvm_ivshmem.o
obj-$(CONFIG_X86_32) += tls.o
obj-$(CONFIG_IA32_EMULATION) += tls.o
obj-y += step.o
diff --git a/arch/x86/kernel/kernel_rr.c b/arch/x86/kernel/kernel_rr.c
new file mode 100644
index 000000000..47ef005fe
--- /dev/null
+++ b/arch/x86/kernel/kernel_rr.c
@@ -0,0 +1,644 @@
+#include <asm/pgtable_64_types.h>
+#include <asm/kernel_rr.h>
+#include <asm/traps.h>
+#include <linux/ptrace.h>
+#include <asm/msr.h>
+#include <linux/highmem-internal.h>
+
+
+static void rr_record_syscall(struct pt_regs *regs, int cpu_id, unsigned long spin_count)
+{
+ unsigned long flags;
+ void *event = NULL;
+ rr_syscall *syscall = NULL;
+
+ if (!rr_queue_inited()) {
+ return;
+ }
+
+ if (!rr_enabled()) {
+ return;
+ }
+
+ local_irq_save(flags);
+
+ event = rr_alloc_new_event_entry(sizeof(rr_syscall), EVENT_TYPE_SYSCALL);
+ if (event == NULL) {
+ panic("Failed to allocate entry");
+ //goto finish;
+ }
+
+ syscall = (rr_syscall *)event;
+
+ syscall->id = cpu_id;
+ syscall->spin_count = 0;
+ syscall->regs.rax = regs->orig_ax;
+ syscall->regs.rbx = regs->bx;
+ syscall->regs.rcx = regs->cx;
+ syscall->regs.rdx = regs->dx;
+ syscall->regs.rsi = regs->si;
+ syscall->regs.rdi = regs->di;
+ syscall->regs.rsp = regs->sp;
+ syscall->regs.rbp = regs->bp;
+ syscall->regs.r8 = regs->r8;
+ syscall->regs.r9 = regs->r9;
+ syscall->regs.r10 = regs->r10;
+ syscall->regs.r11 = regs->r11;
+ syscall->regs.r12 = regs->r12;
+ syscall->regs.r13 = regs->r13;
+ syscall->regs.r14 = regs->r14;
+ syscall->regs.r15 = regs->r15;
+ syscall->cr3 = __read_cr3();
+ syscall->spin_count = spin_count;
+
+ local_irq_restore(flags);
+}
+
+static void rr_record_exception(struct pt_regs *regs,
+ int vector, int error_code,
+ unsigned long cr2, int cpu_id,
+ unsigned long spin_count)
+{
+
+ unsigned long flags;
+ void *event;
+ rr_exception *exception = NULL;
+
+ if (!rr_queue_inited()) {
+ return;
+ }
+
+ if (!rr_enabled()) {
+ return;
+ }
+
+ local_irq_save(flags);
+
+ event = rr_alloc_new_event_entry(sizeof(rr_exception), EVENT_TYPE_EXCEPTION);
+ if (event == NULL) {
+ panic("Failed to allocate entry");
+ //goto finish;
+ }
+
+ exception = (rr_exception *)event;
+
+ exception->id = cpu_id;
+ exception->exception_index = vector;
+ exception->cr2 = cr2;
+ exception->error_code = error_code;
+ exception->regs.rax = regs->ax;
+ exception->regs.rbx = regs->bx;
+ exception->regs.rcx = regs->cx;
+ exception->regs.rdx = regs->dx;
+ exception->regs.rsi = regs->si;
+ exception->regs.rdi = regs->di;
+ exception->regs.rsp = regs->sp;
+ exception->regs.rbp = regs->bp;
+ exception->regs.r8 = regs->r8;
+ exception->regs.r9 = regs->r9;
+ exception->regs.r10 = regs->r10;
+ exception->regs.r11 = regs->r11;
+ exception->regs.r12 = regs->r12;
+ exception->regs.r13 = regs->r13;
+ exception->regs.r14 = regs->r14;
+ exception->regs.r15 = regs->r15;
+ exception->regs.rflags = regs->flags;
+ exception->regs.rip = regs->ip;
+
+ exception->spin_count = spin_count;
+
+ local_irq_restore(flags);
+}
+
+
+void rr_handle_syscall(struct pt_regs *regs)
+{
+ int cpu_id;
+ unsigned long flags;
+ long spin_count;
+
+ local_irq_save(flags);
+
+ preempt_disable();
+ cpu_id = smp_processor_id();
+
+ spin_count = rr_do_acquire_smp_exec(0, cpu_id, CTX_SYSCALL);
+
+ if (spin_count < 0)
+ spin_count = 0;
+
+ rr_record_syscall(regs, cpu_id, spin_count);
+
+ preempt_enable();
+ local_irq_restore(flags);
+}
+
+
+void rr_handle_exception(struct pt_regs *regs, int vector, int error_code, unsigned long cr2)
+{
+ int cpu_id;
+ unsigned long flags;
+ long spin_count;
+
+ local_irq_save(flags);
+
+ preempt_disable();
+ cpu_id = smp_processor_id();
+
+ spin_count = rr_do_acquire_smp_exec(0, cpu_id, CTX_EXCP);
+
+ if (spin_count < 0)
+ spin_count = 0;
+
+ rr_record_exception(regs, vector, error_code, cr2, cpu_id, spin_count);
+
+ preempt_enable();
+ local_irq_restore(flags);
+}
+
+
+static void rr_record_irqentry(int cpu_id, unsigned long spin_count)
+{
+ rr_event_log_guest *event;
+ rr_interrupt *interrupt;
+
+ if (!rr_queue_inited()) {
+ return;
+ }
+
+ if (!rr_enabled()) {
+ return;
+ }
+
+ event = rr_alloc_new_event_entry(sizeof(rr_interrupt), EVENT_TYPE_INTERRUPT);
+ if (event == NULL) {
+ panic("Failed to allocate");
+ }
+
+ interrupt = (rr_interrupt *)event;
+
+ interrupt->id = cpu_id;
+ interrupt->from = 3;
+ interrupt->spin_count = spin_count;
+}
+
+
+void rr_handle_irqentry(void)
+{
+ int cpu_id;
+ unsigned long flags;
+ long spin_count;
+
+ local_irq_save(flags);
+
+ preempt_disable();
+ cpu_id = smp_processor_id();
+
+ spin_count = rr_do_acquire_smp_exec(0, cpu_id, CTX_INTR);
+ if (spin_count < 0) {
+ goto finish;
+ }
+
+ rr_record_irqentry(cpu_id, spin_count);
+
+finish:
+ preempt_enable();
+ local_irq_restore(flags);
+}
+
+
+void *rr_gfu_begin(const void __user *ptr, int size, int align)
+{
+ unsigned long flags;
+ void *event;
+ rr_gfu *gfu;
+
+ if (!rr_queue_inited()) {
+ return NULL;
+ }
+
+ if (!rr_enabled()) {
+ return NULL;
+ }
+
+ local_irq_save(flags);
+
+ event = rr_alloc_new_event_entry(sizeof(rr_gfu), EVENT_TYPE_GFU);
+ if (event == NULL) {
+ panic("Failed to allocate entry");
+ }
+
+ gfu = (rr_gfu *)event;
+
+ gfu->id = 0;
+ gfu->ptr = (unsigned long)ptr;
+ gfu->size = size;
+
+ local_irq_restore(flags);
+
+ return event;
+}
+
+void *rr_cfu_begin(const void __user *from, void *to, long unsigned int n)
+{
+ unsigned long flags;
+ void *event;
+ rr_cfu *cfu = NULL;
+ unsigned long len;
+ void *addr;
+
+ if (!rr_queue_inited()) {
+ return NULL;
+ }
+
+ if (!rr_enabled()) {
+ return NULL;
+ }
+
+ local_irq_save(flags);
+
+ len = sizeof(rr_cfu) + (n + 1) * sizeof(unsigned char);
+ event = rr_alloc_new_event_entry(len, EVENT_TYPE_CFU);
+ if (event == NULL) {
+ panic("Failed to allocate entry");
+ }
+
+ cfu = (rr_cfu *)event;
+
+ cfu->id = 0;
+ cfu->src_addr = (unsigned long)from;
+ cfu->dest_addr = (unsigned long)to;
+ cfu->len = n + 1;
+ cfu->data = NULL;
+ addr = (void *)((unsigned long)cfu + sizeof(rr_cfu));
+
+ local_irq_restore(flags);
+
+ return addr;
+}
+
+void rr_cfu_end(void *addr, void *to, long unsigned int n)
+{
+ unsigned long flags;
+
+ if (!rr_queue_inited()) {
+ return;
+ }
+
+ if (!rr_enabled()) {
+ return;
+ }
+
+ if (!addr) {
+ return;
+ }
+
+ local_irq_save(flags);
+ memcpy(addr, to, n);
+ local_irq_restore(flags);
+}
+
+void rr_record_gfu_end(unsigned long val, void *event)
+{
+ rr_gfu *gfu;
+
+ if (!event)
+ return;
+
+ gfu = (rr_gfu *)event;
+ gfu->val = val;
+}
+
+void rr_record_gfu(unsigned long val, unsigned long ptr)
+{
+ unsigned long flags;
+ void *event;
+ rr_gfu *gfu;
+
+ if (!rr_queue_inited()) {
+ return;
+ }
+
+ if (!rr_enabled()) {
+ return;
+ }
+
+ local_irq_save(flags);
+
+ event = rr_alloc_new_event_entry(sizeof(rr_gfu), EVENT_TYPE_GFU);
+ if (event == NULL) {
+ panic("Failed to allocate");
+ goto finish;
+ }
+
+ gfu = (rr_gfu *)event;
+
+ gfu->id = 0;
+ gfu->val = val;
+ gfu->ptr = ptr;
+
+finish:
+ local_irq_restore(flags);
+}
+
+
+void rr_record_rdseed(unsigned long val)
+{
+ unsigned long flags;
+ void *event;
+ rr_gfu *gfu = NULL;
+
+ if (!rr_queue_inited()) {
+ return;
+ }
+
+ if (!rr_enabled()) {
+ return;
+ }
+
+ local_irq_save(flags);
+
+ event = rr_alloc_new_event_entry(sizeof(rr_gfu), EVENT_TYPE_RDSEED);
+ if (event == NULL) {
+ panic("Failed to allocate");
+ goto finish;
+ }
+
+ gfu = (rr_gfu *)event;
+
+ gfu->id = 0;
+ gfu->val = val;
+
+finish:
+ local_irq_restore(flags);
+}
+
+void rr_record_release(int cpu_id)
+{
+ rr_event_log_guest *event;
+
+ if (!rr_queue_inited()) {
+ return;
+ }
+
+ if (!rr_enabled()) {
+ return;
+ }
+
+ event = rr_alloc_new_event_entry(sizeof(rr_event_log_guest), EVENT_TYPE_RELEASE);
+ if (event == NULL) {
+ panic("Failed to allocate");
+ return;
+ }
+
+ event->type = EVENT_TYPE_RELEASE;
+ event->id = cpu_id;
+}
+
+void rr_begin_cfu(const void __user *from, void *to, long unsigned int n)
+{ return; }
+
+unsigned long rr_record_pte_clear(pte_t *xp)
+{
+ unsigned long flags;
+ void *event;
+ rr_gfu *gfu = NULL;
+
+ pteval_t p = xchg(&xp->pte, 0);
+
+ if (!rr_queue_inited()) {
+ return p;
+ }
+
+ if (!rr_enabled()) {
+ return p;
+ }
+
+ if (!(p & _PAGE_USER)) {
+ return p;
+ }
+
+ local_irq_save(flags);
+
+ event = rr_alloc_new_event_entry(sizeof(rr_gfu), EVENT_TYPE_PTE);
+ if (event == NULL) {
+ panic("Failed to allocate entry");
+ }
+
+ gfu = (rr_gfu *)event;
+
+ gfu->id = 0;
+ gfu->ptr = (unsigned long)xp;
+ gfu->val = p;
+
+ local_irq_restore(flags);
+
+ return p;
+}
+
+pte_t rr_read_pte(pte_t *pte)
+{
+ pte_t rr_pte;
+ unsigned long flags;
+ void *event;
+ rr_gfu *gfu;
+
+ rr_pte = *pte;
+
+ if (!rr_queue_inited()) {
+ return rr_pte;
+ }
+
+ if (!rr_enabled()) {
+ return rr_pte;
+ }
+
+ if (!(rr_pte.pte & _PAGE_USER)) {
+ return rr_pte;
+ }
+
+ local_irq_save(flags);
+
+ event = rr_alloc_new_event_entry(sizeof(rr_gfu), EVENT_TYPE_PTE);
+ if (event == NULL) {
+ panic("Failed to allocate entry");
+ }
+
+ gfu = (rr_gfu *)event;
+
+ gfu->id = 0;
+ gfu->ptr = (unsigned long)pte;
+ gfu->val = rr_pte.pte;
+
+ local_irq_restore(flags);