-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathqemu.patch
14503 lines (14364 loc) · 435 KB
/
qemu.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 Makefile.target Makefile.target
index ae02495951..57b6dd4c0e 100644
--- Makefile.target
+++ Makefile.target
@@ -12,7 +12,7 @@ endif
$(call set-vpath, $(SRC_PATH):$(BUILD_DIR))
ifdef CONFIG_LINUX
-QEMU_CFLAGS += -I../linux-headers
+QEMU_CFLAGS += -I../linux-headers -msse2 -msse -march=native -maes
endif
QEMU_CFLAGS += -iquote .. -iquote $(SRC_PATH)/target/$(TARGET_BASE_ARCH) -DNEED_CPU_H
@@ -157,6 +157,7 @@ obj-y += memory_mapping.o
obj-y += dump.o
obj-$(TARGET_X86_64) += win_dump.o
obj-y += migration/ram.o
+obj-$(CONFIG_PERISCOPE) += migration/periscope.o migration/periscope-syzkaller.o migration/periscope-profiler.o migration/periscope-afl.o migration/periscope-exec.o migration/periscope-kcov.o migration/periscope_dma.o migration/periscope-timers.o migration/periscope-delta-snap-hash.o
LIBS := $(libs_softmmu) $(LIBS)
# Hardware support
diff --git accel/kvm/kvm-all.c accel/kvm/kvm-all.c
index 241db496c3..b73f7bfcd0 100644
--- accel/kvm/kvm-all.c
+++ accel/kvm/kvm-all.c
@@ -31,6 +31,8 @@
#include "exec/gdbstub.h"
#include "sysemu/kvm_int.h"
#include "sysemu/cpus.h"
+#include "migration/periscope.h"
+#include "migration/periscope_dma.h"
#include "qemu/bswap.h"
#include "exec/memory.h"
#include "exec/ram_addr.h"
@@ -40,9 +42,12 @@
#include "hw/irq.h"
#include "sysemu/sev.h"
#include "sysemu/balloon.h"
+#include "migration/periscope-timers.h"
#include "hw/boards.h"
+#include "qemu/cutils.h"
+
/* This check must be after config-host.h is included */
#ifdef CONFIG_EVENTFD
#include <sys/eventfd.h>
@@ -258,6 +263,58 @@ int kvm_physical_memory_addr_from_host(KVMState *s, void *ram,
return 0;
}
+int kvm_update_user_memory_region(void* old, void* new, size_t len)
+{
+ KVMState *s = kvm_state;
+ KVMMemoryListener *kml = &s->memory_listener;
+ void *rnew = NULL;
+ int ret = 0;
+
+ //static qemu_timeval tv_restore_begin, tv_restore_end;
+ //qemu_gettimeofday(&tv_restore_begin);
+
+ for (int i = 0; i < s->nr_slots; i++) {
+ KVMSlot *slot = &kml->slots[i];
+ if(i>6) break;
+ if ((old <= slot->ram && old + len > slot->ram)) {
+ struct kvm_userspace_memory_region mem;
+ rnew = new + (slot->ram - old);
+
+ mem.guest_phys_addr = slot->start_addr;
+ mem.userspace_addr = (unsigned long)slot->ram;
+ mem.flags = slot->flags;
+
+ mem.slot = slot->slot | ((kml->as_id) << 16);
+
+ /* Set the slot size to 0 before setting the slot to the desired
+ * value. This is needed based on KVM commit 75d61fbc. */
+ //mem.memory_size = 0;
+
+ //kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
+
+ mem.memory_size = slot->memory_size;
+
+
+ mem.userspace_addr = (unsigned long)rnew;
+ //ret = kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
+ ret = kvm_vm_ioctl(s, KVM_UPDATE_USER_MEMORY_REGION, &mem);
+ slot->old_flags = mem.flags;
+ slot->ram = rnew;
+ trace_kvm_set_user_memory(mem.slot, mem.flags, mem.guest_phys_addr,
+ mem.memory_size, mem.userspace_addr, ret);
+
+ }
+ }
+
+ //qemu_timeval elapsed;
+ //timersub(&tv_restore_end, &tv_restore_begin, &elapsed);
+ //printf("periscope: reset mem time %lu ms\n",
+ // elapsed.tv_sec * 1000L + elapsed.tv_usec / 1000L);
+
+ return ret;
+}
+
+
static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot, bool new)
{
KVMState *s = kvm_state;
@@ -466,6 +523,8 @@ static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section,
memory_region_get_ram_addr(section->mr);
ram_addr_t pages = int128_get64(section->size) / getpagesize();
+ trace_kvm_get_dirty_pages_log_range(start, pages);
+
cpu_physical_memory_set_dirty_lebitmap(bitmap, start, pages);
return 0;
}
@@ -846,6 +905,8 @@ static void kvm_log_sync(MemoryListener *listener,
KVMMemoryListener *kml = container_of(listener, KVMMemoryListener, listener);
int r;
+ trace_kvm_log_sync();
+
r = kvm_physical_sync_dirty_bitmap(kml, section);
if (r < 0) {
abort();
@@ -1768,17 +1829,50 @@ void kvm_set_sigmask_len(KVMState *s, unsigned int sigmask_len)
s->sigmask_len = sigmask_len;
}
-static void kvm_handle_io(uint16_t port, MemTxAttrs attrs, void *data, int direction,
+static void kvm_cpu_kick_self(void);
+
+static void kvm_handle_io(CPUState* cpu,
+ uint16_t port, MemTxAttrs attrs, void *data, int direction,
int size, uint32_t count)
{
int i;
uint8_t *ptr = data;
for (i = 0; i < count; i++) {
+ FlatView *fv = address_space_to_flatview(&address_space_io);
+ hwaddr l;
+ hwaddr addr1;
+ MemoryRegion *mr;
+ l = size;
+ mr = flatview_translate(fv, port, &addr1, &l, false, attrs);
+
+ if (periscope_mmio_check(mr, size, direction == KVM_EXIT_IO_OUT) < 0) {
+ kvm_cpu_kick_self();
+ kvm_vcpu_ioctl(cpu, KVM_RUN, 0);
+
+ if (periscope_maybe_checkpoint_request() == 0) {
+ kvm_cpu_synchronize_state(cpu);
+
+ vm_stop(RUN_STATE_SAVE_VM);
+ }
+
+ periscope_restore_request();
+ vm_stop(RUN_STATE_RESTORE_VM);
+ return;
+ }
+
address_space_rw(&address_space_io, port, attrs,
ptr, size,
direction == KVM_EXIT_IO_OUT);
+
ptr += size;
+
+ if (direction != KVM_EXIT_IO_OUT && mr && mr->name &&
+ strstart(mr->name, "periscope-", NULL)) {
+ if (periscope_maybe_checkpoint_request() == 0) {
+ vm_stop(RUN_STATE_SAVE_VM);
+ }
+ }
}
}
@@ -1942,6 +2036,25 @@ static void kvm_eat_signals(CPUState *cpu)
} while (sigismember(&chkset, SIG_IPI));
}
+int kvm_enable_dma_trace(uint64_t gpa)
+{
+ KVMState *s = kvm_state;
+ int ret;
+ //printf("%s: Enter\n", __FUNCTION__);
+ ret = kvm_vm_ioctl(s, KVM_ENABLE_DMA_TRACE, gpa);
+ return ret;
+}
+
+int kvm_disable_dma_trace(uint64_t gpa)
+{
+ KVMState *s = kvm_state;
+ int ret;
+ //printf("%s: Enter\n", __FUNCTION__);
+ ret = kvm_vm_ioctl(s, KVM_DISABLE_DMA_TRACE, gpa);
+ return ret;
+}
+
+
int kvm_cpu_exec(CPUState *cpu)
{
struct kvm_run *run = cpu->kvm_run;
@@ -2021,15 +2134,84 @@ int kvm_cpu_exec(CPUState *cpu)
case KVM_EXIT_IO:
DPRINTF("handle_io\n");
/* Called outside BQL */
- kvm_handle_io(run->io.port, attrs,
+ kvm_handle_io(cpu, run->io.port, attrs,
(uint8_t *)run + run->io.data_offset,
run->io.direction,
run->io.size,
run->io.count);
ret = 0;
+
+ if (periscope_checkpoint_requested() || periscope_restore_requested())
+ ret = EXCP_INTERRUPT;
+
break;
case KVM_EXIT_MMIO:
DPRINTF("handle_mmio\n");
+
+ FlatView *fv = address_space_to_flatview(&address_space_memory);
+ hwaddr l;
+ hwaddr addr1;
+ MemoryRegion *mr;
+ l = run->mmio.len;
+ mr = flatview_translate(fv, run->mmio.phys_addr, &addr1, &l, false, attrs);
+
+ if (periscope_mmio_check(mr, run->mmio.len, run->mmio.is_write) < 0) {
+ kvm_cpu_kick_self();
+ run_ret = kvm_vcpu_ioctl(cpu, KVM_RUN, 0);
+
+ if (periscope_maybe_checkpoint_request() == 0) {
+ kvm_cpu_synchronize_state(cpu);
+
+ vm_stop(RUN_STATE_SAVE_VM);
+ }
+
+ periscope_restore_request();
+ vm_stop(RUN_STATE_RESTORE_VM);
+
+ ret = EXCP_INTERRUPT;
+ break;
+ }
+
+ if(mr && mr->name && strcmp(mr->name, "pc.ram") == 0) {
+ periscope_dmar *dmar = periscope_dma_get(run->mmio.phys_addr, 1);
+ if(dmar->mr != NULL) {
+ if(run->mmio.is_write) {
+ periscope_dma_write_access(dmar, run->mmio.phys_addr, run->mmio.data, run->mmio.len);
+ // mirror data to actual guest ram
+ dma_memory_write(&address_space_memory, run->mmio.phys_addr, run->mmio.data, run->mmio.len);
+ //printf("written value @ %llx -> %lx\n", run->mmio.phys_addr, *(uint64_t*)run->mmio.data);
+ } else { // read
+ for(int i=0; i<run->mmio.len; ++i) { // for each accessed byte (to handle partial overwrites)
+ int r = periscope_dma_read_access(dmar, run->mmio.phys_addr + i, ((uint8_t*)run->mmio.data) + i);
+ if(r == 1) { // not fuzzed -> data in guest ram will be left unchanged
+ //printf("not fuzzing dma @ %llx\n", run->mmio.phys_addr + i);
+ } else if (r == 0) { // fuzzed
+ printf("fuzzing dma @ %llx\n", run->mmio.phys_addr);
+
+ // TODO
+ // for now set some arbitraty address offset, which falls into the mmio region
+ // we could also directly invoke the fuzzer
+ address_space_rw(&address_space_memory,
+ dmar->mr->addr + 123, attrs,
+ ((uint8_t*)run->mmio.data) + i,
+ 1,
+ run->mmio.is_write);
+
+ //printf("fuzzed value %lx\n", *((uint64_t*)run->mmio.data)+i);
+ // mirror data to actual guest ram
+ dma_memory_write(&address_space_memory, run->mmio.phys_addr + i, ((uint8_t*)run->mmio.data)+i, 1);
+ } else {
+ printf("ERROR could not get dma value\n");
+
+ }
+ }
+ }
+ periscop_dma_maybe_remove(dmar, run->mmio.phys_addr);
+ }
+ ret = 0;
+ break;
+ }
+
/* Called outside BQL */
address_space_rw(&address_space_memory,
run->mmio.phys_addr, attrs,
@@ -2037,9 +2219,142 @@ int kvm_cpu_exec(CPUState *cpu)
run->mmio.len,
run->mmio.is_write);
ret = 0;
+
+ if (!run->mmio.is_write && mr && mr->name &&
+ strstart(mr->name, "periscope-", NULL)) {
+ if (periscope_maybe_checkpoint_request() == 0) {
+ vm_stop(RUN_STATE_SAVE_VM);
+
+ ret = EXCP_INTERRUPT;
+ }
+ }
+
+ break;
+
+#ifndef KVM_HC_PERISCOPE
+#define KVM_EXIT_PERISCOPE_GET_PROG 100
+#define KVM_EXIT_PERISCOPE_END 101
+#define KVM_EXIT_PERISCOPE_DEBUG 110
+#endif
+
+ case KVM_EXIT_PERISCOPE_GET_PROG:
+ periscope_notify_boot();
+ run->hypercall.ret = periscope_get_agent_id();
+ ret = 0;
+ break;
+ case KVM_EXIT_PERISCOPE_END:
+ if (should_restore_at_agent_exit(run->hypercall.args[0])) {
+ kvm_cpu_kick_self();
+ run_ret = kvm_vcpu_ioctl(cpu, KVM_RUN, 0);
+
+ // no need for checkpoint here
+ // request a restore right away
+ periscope_restore_request();
+ vm_stop(RUN_STATE_RESTORE_VM);
+
+ ret = EXCP_INTERRUPT;
+ break;
+ }
+ if (should_shutdown_at_agent_exit(run->hypercall.args[0])) {
+ qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
+ ret = EXCP_INTERRUPT;
+ }
+ ret = 0;
+ break;
+ case KVM_EXIT_PERISCOPE_DEBUG:
+ kvm_cpu_synchronize_state(cpu);
+ ret = 0;
+
+ switch(run->hypercall.args[0]) {
+ case SYZKALLER_HC_ROOT_CHKPT:
+ printf("periscope: root checkpoint requested\n");
+ if (periscope_snapshot_inited()) {
+ run->hypercall.ret = -1;
+ ret = 0;
+ break;
+ }
+ if (periscope_maybe_checkpoint_request() == 0) {
+ ret = EXCP_INTERRUPT;
+#define FORCE_SKIP_HC
+#undef FORCE_SKIP_HC
+#ifdef FORCE_SKIP_HC
+ // forcefully skip this hypercall before chkpt
+ kvm_cpu_synchronize_state(cpu);
+ X86CPU *x86_cpu = X86_CPU(cpu);
+ x86_cpu->env.eip += 3; // vm(m)call is 3 bytes
+ kvm_arch_put_registers(cpu, KVM_PUT_RUNTIME_STATE);
+ cpu->vcpu_dirty = true;
+
+ kvm_cpu_kick_self();
+ run_ret = kvm_vcpu_ioctl(cpu, KVM_RUN, 0);
+#endif
+ }
+ else {
+ printf("periscope: failed to request root checkpoint\n");
+ }
+ periscope_restore_request();
+ ret = EXCP_INTERRUPT;
+ break;
+ case SYZKALLER_HC_RECV_EXEC:
+ syzkaller_receive_execute(cpu, run->hypercall.args[1]);
+ break;
+ case SYZKALLER_HC_REPLY_EXEC:
+ syzkaller_reply_execute(run->hypercall.args[1]);
+ if (!periscope_snapshot_inited()) {
+ ret = 0;
+ break;
+ }
+ periscope_restore_request();
+ ret = EXCP_INTERRUPT;
+ break;
+ case SYZKALLER_HC_RECV_HANDSHAKE:
+ syzkaller_receive_handshake(cpu, run->hypercall.args[1]);
+ ret = 0;
+ break;
+ case SYZKALLER_HC_REPLY_HANDSHAKE:
+ syzkaller_reply_handshake();
+ ret = 0;
+ break;
+ case SYZKALLER_HC_MAYBE_CHKPT:
+ run->hypercall.ret = syzkaller_maybe_checkpoint(run->hypercall.args[1], run->hypercall.args[2]);
+ if (run->hypercall.ret == 0) {
+#ifdef FORCE_SKIP_HC
+ // forcefully skip this hypercall before chkpt
+ kvm_cpu_synchronize_state(cpu);
+ X86CPU *x86_cpu = X86_CPU(cpu);
+ x86_cpu->env.eip += 3; // vm(m)call is 3 bytes
+ kvm_arch_put_registers(cpu, KVM_PUT_RUNTIME_STATE);
+ cpu->vcpu_dirty = true;
+
+ kvm_cpu_kick_self();
+ run_ret = kvm_vcpu_ioctl(cpu, KVM_RUN, 0);
+#endif
+ ret = EXCP_INTERRUPT;
+ break;
+ }
+ ret = 0;
+ break;
+ case SYZKALLER_HC_FORKSRV_CTX:
+ syzkaller_submit_forkserver_context(run->hypercall.args[1]);
+ ret = 0;
+ break;
+ case PERISCOPE_DEBUG_HC_BENCHMARK:
+ periscope_benchmark_hypercall(run->hypercall.args[1]);
+ break;
+ case PERISCOPE_DEBUG_HC_NEXT:
+ periscope_fetch_next_input();
+ ret = 0;
+ break;
+ default:
+ periscope_debug_hypercall(run->hypercall.args[0], run->hypercall.args[1], run->hypercall.args[2]);
+ ret = 0;
+ break;
+ }
break;
case KVM_EXIT_IRQ_WINDOW_OPEN:
DPRINTF("irq_window_open\n");
+ if (periscope_irq_check() == 0) {
+ }
ret = EXCP_INTERRUPT;
break;
case KVM_EXIT_SHUTDOWN:
@@ -2048,6 +2363,7 @@ int kvm_cpu_exec(CPUState *cpu)
ret = EXCP_INTERRUPT;
break;
case KVM_EXIT_UNKNOWN:
+ printf("periscope: kvm exit unknown\n");
fprintf(stderr, "KVM: unknown exit, hardware reason %" PRIx64 "\n",
(uint64_t)run->hw.hardware_exit_reason);
ret = -1;
@@ -2085,6 +2401,15 @@ int kvm_cpu_exec(CPUState *cpu)
}
} while (ret == 0);
+ if (periscope_shutdown_requested()) {
+ printf("periscope: shutdown request noticed. restoring VM...");
+
+ // TODO: error reporting?
+ periscope_restore_request();
+
+ vm_stop(RUN_STATE_RESTORE_VM);
+ }
+
cpu_exec_end(cpu);
qemu_mutex_lock_iothread();
@@ -2144,7 +2469,17 @@ int kvm_vcpu_ioctl(CPUState *cpu, int type, ...)
va_end(ap);
trace_kvm_vcpu_ioctl(cpu->cpu_index, type, arg);
+
+#ifdef PERISCOPE_TIMERS
+ peri_timer *pt = NULL;
+ if(type == KVM_RUN)
+ pt = start_interval("periscope_kvm_run.timer");
+#endif
ret = ioctl(cpu->kvm_fd, type, arg);
+#ifdef PERISCOPE_TIMERS
+ if(pt)
+ stop_interval(pt);
+#endif
if (ret == -1) {
ret = -errno;
}
diff --git accel/kvm/trace-events accel/kvm/trace-events
index 33c5b1b3af..155d02554b 100644
--- accel/kvm/trace-events
+++ accel/kvm/trace-events
@@ -1,5 +1,8 @@
# See docs/devel/tracing.txt for syntax documentation.
+kvm_get_dirty_pages_log_range(uint64_t start, uint64_t pages) "start 0x%" PRIx64 " pages 0x%" PRIx64
+kvm_log_sync(void) ""
+
# kvm-all.c
kvm_ioctl(int type, void *arg) "type 0x%x, arg %p"
kvm_vm_ioctl(int type, void *arg) "type 0x%x, arg %p"
diff --git configure configure
index 1c563a7027..aea4f17f3e 100755
--- configure
+++ configure
@@ -434,6 +434,7 @@ cpuid_h="no"
avx2_opt=""
zlib="yes"
capstone=""
+agamotto=""
lzo=""
snappy=""
bzip2=""
@@ -1504,6 +1505,8 @@ for opt do
;;
--enable-capstone=system) capstone="system"
;;
+ --with-agamotto=*) agamotto="$optarg"
+ ;;
--with-git=*) git="$optarg"
;;
--enable-git-update) git_update=yes
@@ -1816,6 +1819,7 @@ disabled with --disable-FEATURE, default is enabled if available:
sheepdog sheepdog block driver support
crypto-afalg Linux AF_ALG crypto backend driver
capstone capstone disassembler support
+ agamotto agamotto checkpoint support
debug-mutex mutex debugging support
libpmem libpmem support
@@ -4987,6 +4991,14 @@ EOF
fi
fi
+##########################################
+# agamotto
+
+if test -e "$agamotto"; then
+ QEMU_CFLAGS="$QEMU_CFLAGS -I$agamotto"
+ LIBS="-L$agamotto -lagamotto_nomain $LIBS"
+fi
+
##########################################
# capstone
@@ -7200,6 +7212,9 @@ fi
if test "$capstone" != "no" ; then
echo "CONFIG_CAPSTONE=y" >> $config_host_mak
fi
+if test -e "$agamotto"; then
+ echo "CONFIG_PERISCOPE=y" >> $config_host_mak
+fi
if test "$debug_mutex" = "yes" ; then
echo "CONFIG_DEBUG_MUTEX=y" >> $config_host_mak
fi
diff --git exec.c exec.c
index 6ab62f4eee..3c4874c0b5 100644
--- exec.c
+++ exec.c
@@ -1381,6 +1381,55 @@ bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,
return dirty;
}
+DirtyBitmapSnapshot *cpu_physical_memory_snapshot_and_get_dirty
+ (ram_addr_t start, ram_addr_t length, unsigned client)
+{
+ DirtyMemoryBlocks *blocks;
+ unsigned long align = 1UL << (TARGET_PAGE_BITS + BITS_PER_LEVEL);
+ ram_addr_t first = QEMU_ALIGN_DOWN(start, align);
+ ram_addr_t last = QEMU_ALIGN_UP(start + length, align);
+ DirtyBitmapSnapshot *snap;
+ unsigned long page, end, dest;
+
+ snap = g_malloc0(sizeof(*snap) +
+ ((last - first) >> (TARGET_PAGE_BITS + 3)));
+ snap->start = first;
+ snap->end = last;
+
+ page = first >> TARGET_PAGE_BITS;
+ end = last >> TARGET_PAGE_BITS;
+ dest = 0;
+
+ rcu_read_lock();
+
+ blocks = atomic_rcu_read(&ram_list.dirty_memory[client]);
+
+ while (page < end) {
+ unsigned long idx = page / DIRTY_MEMORY_BLOCK_SIZE;
+ unsigned long offset = page % DIRTY_MEMORY_BLOCK_SIZE;
+ unsigned long num = MIN(end - page, DIRTY_MEMORY_BLOCK_SIZE - offset);
+
+ assert(QEMU_IS_ALIGNED(offset, (1 << BITS_PER_LEVEL)));
+ assert(QEMU_IS_ALIGNED(num, (1 << BITS_PER_LEVEL)));
+ offset >>= BITS_PER_LEVEL;
+
+ bitmap_copy_atomic(snap->dirty + dest,
+ blocks->blocks[idx] + offset,
+ num);
+ page += num;
+ dest += num >> BITS_PER_LEVEL;
+ }
+
+ rcu_read_unlock();
+
+ if (tcg_enabled()) {
+ tlb_reset_dirty_range_all(start, length);
+ }
+
+ return snap;
+}
+
+
DirtyBitmapSnapshot *cpu_physical_memory_snapshot_and_clear_dirty
(ram_addr_t start, ram_addr_t length, unsigned client)
{
@@ -2625,6 +2674,9 @@ RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset,
if (host - block->host < block->max_length) {
goto found;
}
+ if (host - block->host_restore < block->max_length) {
+ goto found;
+ }
}
rcu_read_unlock();
diff --git hmp-commands-info.hx hmp-commands-info.hx
index c59444c461..03578b398c 100644
--- hmp-commands-info.hx
+++ hmp-commands-info.hx
@@ -431,6 +431,20 @@ STEXI
@item info snapshots
@findex info snapshots
Show the currently saved VM snapshots.
+ETEXI
+
+ {
+ .name = "snapshots-minimal",
+ .args_type = "",
+ .params = "",
+ .help = "show the currently saved minimal VM snapshots",
+ .cmd = hmp_info_snapshots_minimal,
+ },
+
+STEXI
+@item info snapshots-minimal
+@findex info snapshots-minimal
+Show the currently saved minimal VM snapshots.
ETEXI
{
diff --git hmp-commands.hx hmp-commands.hx
index 9b4035965c..593ca3ea04 100644
--- hmp-commands.hx
+++ hmp-commands.hx
@@ -365,6 +365,23 @@ a snapshot with the same tag, it is replaced. More info at
Since 4.0, savevm stopped allowing the snapshot id to be set, accepting
only @var{tag} as parameter.
+ETEXI
+
+ {
+ .name = "savevm-minimal",
+ .args_type = "name:s?",
+ .params = "[tag|id]",
+ .help = "save a VM snapshot. If no tag or id are provided, a new snapshot is created",
+ .cmd = hmp_savevm_minimal,
+ },
+
+STEXI
+@item savevm-minimal [@var{tag}|@var{id}]
+@findex savevm-minimal
+Create a snapshot of the whole virtual machine. If @var{tag} is
+provided, it is used as human readable identifier. If there is already
+a snapshot with the same tag or ID, it is replaced. More info at
+@ref{vm_snapshots}.
ETEXI
{
@@ -383,6 +400,22 @@ Set the whole virtual machine to the snapshot identified by the tag
@var{tag}.
Since 4.0, loadvm stopped accepting snapshot id as parameter.
+ETEXI
+
+ {
+ .name = "loadvm-minimal",
+ .args_type = "name:s",
+ .params = "tag|id",
+ .help = "restore a VM snapshot from its tag or id",
+ .cmd = hmp_loadvm_minimal,
+ .command_completion = loadvm_minimal_completion,
+ },
+
+STEXI
+@item loadvm-minimal @var{tag}|@var{id}
+@findex loadvm-minimal
+Set the whole virtual machine to the snapshot identified by the tag
+@var{tag} or the unique snapshot ID @var{id}.
ETEXI
{
@@ -1911,6 +1944,39 @@ ETEXI
STEXI
@item qom-set @var{path} @var{property} @var{value}
Set QOM property @var{property} of object at location @var{path} to value @var{value}
+ETEXI
+{
+ .name = "kcov-ioctl",
+ .args_type = "cmd:i,arg:i",
+ .params = "cmd,arg",
+ .help = "kcov-ioctl cmd arg",
+ .cmd = hmp_kcov_ioctl,
+},
+STEXI
+@item kcov-ioctl @var{cmd} @var{arg}
+Exec kcov-ioctl @var{cmd} @var{arg}.
+ETEXI
+{
+ .name = "kcov-get-area-offset",
+ .args_type = "",
+ .params = "",
+ .help = "kcov-get-area-offset",
+ .cmd = hmp_kcov_get_area_offset,
+},
+STEXI
+@item kcov-get-area-offset
+Exec kcov-get-area-offset
+ETEXI
+{
+ .name = "kcov-print-coverage",
+ .args_type = "",
+ .params = "",
+ .help = "kcov-print-coverage",
+ .cmd = hmp_kcov_print_coverage,
+},
+STEXI
+@item kcov-print-coverage
+Exec kcov-print-coverage
ETEXI
{
@@ -1926,3 +1992,4 @@ ETEXI
STEXI
@end table
ETEXI
+
diff --git hmp.c hmp.c
index 8eec768088..89aba7e115 100644
--- hmp.c
+++ hmp.c
@@ -38,6 +38,7 @@
#include "qapi/qapi-commands-run-state.h"
#include "qapi/qapi-commands-tpm.h"
#include "qapi/qapi-commands-ui.h"
+#include "qapi/qapi-commands-kcov.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qerror.h"
#include "qapi/string-input-visitor.h"
@@ -1466,6 +1467,23 @@ void hmp_loadvm(Monitor *mon, const QDict *qdict)
hmp_handle_error(mon, &err);
}
+void hmp_loadvm_minimal(Monitor *mon, const QDict *qdict)
+{
+ printf("TODO: hmp_loadvm_minimal\n");
+
+#if 0
+ int saved_vm_running = runstate_is_running();
+ const char *name = qdict_get_str(qdict, "name");
+ Error *err = NULL;
+ vm_stop(RUN_STATE_RESTORE_VM);
+
+ if (load_snapshot(name, &err) == 0 && saved_vm_running) {
+ vm_start();
+ }
+ hmp_handle_error(mon, &err);
+#endif
+}
+
void hmp_savevm(Monitor *mon, const QDict *qdict)
{
Error *err = NULL;
@@ -1474,6 +1492,17 @@ void hmp_savevm(Monitor *mon, const QDict *qdict)
hmp_handle_error(mon, &err);
}
+void hmp_savevm_minimal(Monitor *mon, const QDict *qdict)
+{
+ printf("TODO: hmp_savevm_minimal\n");
+#if 0
+ Error *err = NULL;
+
+ save_snapshot(qdict_get_try_str(qdict, "name"), &err);
+ hmp_handle_error(mon, &err);
+#endif
+}
+
void hmp_delvm(Monitor *mon, const QDict *qdict)
{
BlockDriverState *bs;
@@ -1630,6 +1659,11 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
}
+void hmp_info_snapshots_minimal(Monitor *mon, const QDict *qdict)
+{
+ printf("TODO: hmp_info_snapshots_minimal\n");
+}
+
void hmp_announce_self(Monitor *mon, const QDict *qdict)
{
qmp_announce_self(migrate_announce_params(), NULL);
@@ -3154,3 +3188,20 @@ void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict)
}
hmp_handle_error(mon, &err);
}
+
+void hmp_kcov_get_area_offset(Monitor *mon, const QDict *qdict)
+{
+ qmp_kcov_get_area_offset(NULL);
+}
+
+void hmp_kcov_ioctl(Monitor *mon, const QDict *qdict)
+{
+ int64_t cmd = qdict_get_int(qdict, "cmd");
+ int64_t arg = qdict_get_int(qdict, "arg");
+ qmp_kcov_ioctl(cmd, arg, NULL);
+}
+
+void hmp_kcov_print_coverage(Monitor *mon, const QDict *qdict)
+{
+ qmp_kcov_print_coverage(NULL);
+}
diff --git hmp.h hmp.h
index 43617f2646..ac6ba96d79 100644
--- hmp.h
+++ hmp.h
@@ -66,9 +66,12 @@ void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict);
void hmp_drive_mirror(Monitor *mon, const QDict *qdict);
void hmp_drive_backup(Monitor *mon, const QDict *qdict);
void hmp_loadvm(Monitor *mon, const QDict *qdict);
+void hmp_loadvm_minimal(Monitor *mon, const QDict *qdict);
void hmp_savevm(Monitor *mon, const QDict *qdict);
+void hmp_savevm_minimal(Monitor *mon, const QDict *qdict);
void hmp_delvm(Monitor *mon, const QDict *qdict);
void hmp_info_snapshots(Monitor *mon, const QDict *qdict);
+void hmp_info_snapshots_minimal(Monitor *mon, const QDict *qdict);
void hmp_migrate_cancel(Monitor *mon, const QDict *qdict);
void hmp_migrate_continue(Monitor *mon, const QDict *qdict);
void hmp_migrate_incoming(Monitor *mon, const QDict *qdict);
@@ -140,6 +143,7 @@ void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
const char *str);
void delvm_completion(ReadLineState *rs, int nb_args, const char *str);
void loadvm_completion(ReadLineState *rs, int nb_args, const char *str);
+void loadvm_minimal_completion(ReadLineState *rs, int nb_args, const char *str);
void hmp_rocker(Monitor *mon, const QDict *qdict);
void hmp_rocker_ports(Monitor *mon, const QDict *qdict);
void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict);
@@ -151,4 +155,7 @@ void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict);
void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict);
void hmp_info_sev(Monitor *mon, const QDict *qdict);
+void hmp_kcov_ioctl(Monitor *mon, const QDict *qdict);
+void hmp_kcov_get_area_offset(Monitor *mon, const QDict *qdict);
+void hmp_kcov_print_coverage(Monitor *mon, const QDict *qdict);
#endif
diff --git hw/Makefile.objs hw/Makefile.objs
index 82aa7fab8e..7ce452da1c 100644
--- hw/Makefile.objs
+++ hw/Makefile.objs
@@ -19,6 +19,7 @@ devices-dirs-$(CONFIG_IPMI) += ipmi/
devices-dirs-$(CONFIG_SOFTMMU) += isa/
devices-dirs-$(CONFIG_SOFTMMU) += misc/
devices-dirs-$(CONFIG_SOFTMMU) += net/
+devices-dirs-$(CONFIG_SOFTMMU) += periscope/
devices-dirs-$(CONFIG_SOFTMMU) += rdma/
devices-dirs-$(CONFIG_SOFTMMU) += nvram/
devices-dirs-$(CONFIG_SOFTMMU) += pci/
diff --git hw/i386/intel_iommu.c hw/i386/intel_iommu.c
index 2558f48fe6..aa7a695f19 100644
--- hw/i386/intel_iommu.c
+++ hw/i386/intel_iommu.c
@@ -301,6 +301,23 @@ out:
return entry;
}
+static inline uint16_t vtd_make_source_id(uint8_t bus_num, uint8_t devfn)
+{
+ return ((bus_num & 0xffUL) << 8) | (devfn & 0xffUL);
+}
+
+int vtd_lookup_gfn(IntelIOMMUState *s, uint8_t bus_num, uint8_t devfn,
+ hwaddr addr, uint64_t *gfn)
+{
+ uint16_t source_id = vtd_make_source_id(bus_num, devfn);
+ VTDIOTLBEntry *entry = vtd_lookup_iotlb(s, source_id, addr);
+ if (!entry) {
+ return -1;
+ }
+ *gfn = entry->gfn;
+ return 0;
+}
+
/* Must be with IOMMU lock held */
static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
uint16_t domain_id, hwaddr addr, uint64_t slpte,
@@ -1358,6 +1375,77 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
return 0;
}
+int vtd_dev_to_domain_id(IntelIOMMUState *s, uint8_t bus_num,
+ uint8_t devfn, uint16_t *domain_id)
+{
+ VTDContextEntry ce;
+ int ret = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
+ if (ret == 0) {
+ *domain_id = VTD_CONTEXT_ENTRY_DID(ce.hi);
+ }
+ return ret;
+}
+
+
+int vtd_iova_to_gpa_writable(IntelIOMMUState *s, uint8_t bus_num,
+ uint8_t devfn, uint64_t iova, uint64_t *gpa) {
+ VTDContextEntry ce;
+ int ret = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
+ if (ret < 0) {
+ return ret;
+ }
+
+ bool is_write = true;
+ uint64_t slpte;
+ uint32_t slpte_level;
+ bool reads, writes;
+
+ uint8_t aw_bits = VTD_HOST_ADDRESS_WIDTH;
+
+ ret = vtd_iova_to_slpte(s, &ce, iova, is_write,
+ &slpte, &slpte_level,
+ &reads, &writes, aw_bits);
+ if (ret < 0) {
+ return ret;
+ }
+
+ uint64_t page_mask = vtd_slpt_level_page_mask(slpte_level);
+
+ *gpa = vtd_get_slpte_addr(slpte, aw_bits) & page_mask;
+
+ return 0;
+}
+
+
+int vtd_iova_to_gpa(IntelIOMMUState *s, uint8_t bus_num,
+ uint8_t devfn, uint64_t iova, uint64_t *gpa) {
+ VTDContextEntry ce;
+ int ret = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
+ if (ret < 0) {
+ return ret;
+ }
+
+ bool is_write = false;
+ uint64_t slpte;
+ uint32_t slpte_level;
+ bool reads, writes;
+
+ uint8_t aw_bits = VTD_HOST_ADDRESS_WIDTH;
+
+ ret = vtd_iova_to_slpte(s, &ce, iova, is_write,
+ &slpte, &slpte_level,
+ &reads, &writes, aw_bits);
+ if (ret < 0) {
+ return ret;
+ }
+
+ uint64_t page_mask = vtd_slpt_level_page_mask(slpte_level);
+
+ *gpa = vtd_get_slpte_addr(slpte, aw_bits) & page_mask;
+
+ return 0;
+}
+
static int vtd_sync_shadow_page_hook(IOMMUTLBEntry *entry,
void *private)
{
@@ -1525,11 +1613,6 @@ static void vtd_switch_address_space_all(IntelIOMMUState *s)
}
}
-static inline uint16_t vtd_make_source_id(uint8_t bus_num, uint8_t devfn)
-{
- return ((bus_num & 0xffUL) << 8) | (devfn & 0xffUL);
-}
-
static const bool vtd_qualified_faults[] = {
[VTD_FR_RESERVED] = false,
[VTD_FR_ROOT_ENTRY_P] = false,
diff --git hw/i386/kvmvapic.c hw/i386/kvmvapic.c
index 70f6f26a94..1d659ab911 100644
--- hw/i386/kvmvapic.c
+++ hw/i386/kvmvapic.c
@@ -583,6 +583,8 @@ static int vapic_map_rom_writable(VAPICROMState *s)
as = sysbus_address_space(&s->busdev);
if (s->rom_mapped_writable) {
+ // XXX this only works for resets done by periscope!
+ return 0;
memory_region_del_subregion(as, &s->rom);
object_unparent(OBJECT(&s->rom));
}
diff --git hw/i386/pc.c hw/i386/pc.c
index f2c15bf1f2..54428ac3e4 100644
--- hw/i386/pc.c
+++ hw/i386/pc.c
@@ -77,6 +77,7 @@
#include "hw/i386/intel_iommu.h"