forked from crash-utility/crash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qemu-load.c
1157 lines (974 loc) · 25.8 KB
/
qemu-load.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Qemu save VM loader
*
* Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
* Written by Paolo Bonzini.
*
* Portions Copyright (C) 2009 David Anderson
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#define _GNU_SOURCE
#include "qemu-load.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/mman.h>
#include "kvmdump.h"
struct qemu_device *
device_alloc (struct qemu_device_list *dl, size_t sz,
struct qemu_device_vtbl *vtbl,
uint32_t section_id, uint32_t instance_id, uint32_t version_id)
{
struct qemu_device *d = calloc (1, sz);
d->vtbl = vtbl;
d->list = dl;
d->section_id = section_id;
d->instance_id = instance_id;
d->version_id = version_id;
if (!dl->head)
dl->head = dl->tail = d;
else {
dl->tail->next = d;
d->prev = dl->tail;
dl->tail = d;
}
return d;
}
struct qemu_device *
device_find (struct qemu_device_list *dl, uint32_t section_id)
{
struct qemu_device *d;
d = dl->head;
while (d && d->section_id != section_id)
d = d->next;
return d;
}
struct qemu_device *
device_find_instance (struct qemu_device_list *dl, const char *name,
uint32_t instance_id)
{
struct qemu_device *d;
d = dl->head;
while (d && (strcmp (d->vtbl->name, name) || d->instance_id != instance_id))
d = d->next;
return d;
}
void
device_free (struct qemu_device *d)
{
struct qemu_device_list *dl = d->list;
if (d->prev)
d->prev->next = d->next;
else
dl->head = d->next;
if (d->next)
d->next->prev = d->prev;
else
dl->tail = d->prev;
d->prev = d->next = NULL;
if (d->vtbl->free)
d->vtbl->free (d, dl);
}
void
device_list_free (struct qemu_device_list *l)
{
if (!l)
return;
while (l->head)
device_free (l->head);
}
/* File access. */
static inline uint16_t
get_be16 (FILE *fp)
{
uint8_t a = getc (fp);
uint8_t b = getc (fp);
return (a << 8) | b;
}
static inline uint16_t
get_le16 (FILE *fp)
{
uint8_t b = getc (fp);
uint8_t a = getc (fp);
return (a << 8) | b;
}
static inline uint32_t
get_be32 (FILE *fp)
{
uint16_t a = get_be16 (fp);
uint16_t b = get_be16 (fp);
return (a << 16) | b;
}
static inline uint32_t
get_le32 (FILE *fp)
{
uint16_t b = get_le16 (fp);
uint16_t a = get_le16 (fp);
return (a << 16) | b;
}
static inline uint64_t
get_be64 (FILE *fp)
{
uint32_t a = get_be32 (fp);
uint32_t b = get_be32 (fp);
return ((uint64_t)a << 32) | b;
}
static inline uint64_t
get_le64 (FILE *fp)
{
uint32_t b = get_le32 (fp);
uint32_t a = get_le32 (fp);
return ((uint64_t)a << 32) | b;
}
static inline void
get_qemu128 (FILE *fp, union qemu_uint128_t *result)
{
result->i[1] = get_le32 (fp);
result->i[0] = get_le32 (fp);
result->i[3] = get_le32 (fp);
result->i[2] = get_le32 (fp);
}
/* RAM loader. */
#define RAM_SAVE_FLAG_FULL 0x01
#define RAM_SAVE_FLAG_COMPRESS 0x02
#define RAM_SAVE_FLAG_MEM_SIZE 0x04
#define RAM_SAVE_FLAG_PAGE 0x08
#define RAM_SAVE_FLAG_EOS 0x10
#define RAM_SAVE_FLAG_CONTINUE 0x20
#define RAM_SAVE_ADDR_MASK (~4095LL)
#define RAM_OFFSET_COMPRESSED (~(off_t)255)
static void
ram_alloc (struct qemu_device_ram *dram, uint64_t size)
{
// size_t old_npages = dram->offsets ? 0 : dram->last_ram_offset / 4096;
// size_t new_npages = size / 4096;
// assert (size <= SIZE_MAX);
// if (dram->offsets)
// dram->offsets = realloc (dram->offsets,
// new_npages * sizeof (off_t));
// else
// dram->offsets = malloc (new_npages * sizeof (off_t));
//
// assert (dram->offsets);
// while (old_npages < new_npages)
// dram->offsets[old_npages++] = RAM_OFFSET_COMPRESSED | 0;
dram->last_ram_offset = size;
}
#ifndef ATTRIBUTE_UNUSED
#define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
#endif
static int
get_string (FILE *fp, char *name)
{
size_t items ATTRIBUTE_UNUSED;
int sz = (uint8_t) getc (fp);
if (sz == EOF)
return -1;
items = fread (name, sz, 1, fp);
name[sz] = 0;
return sz;
}
static int
get_string_len (FILE *fp, char *name, uint32_t sz)
{
size_t items ATTRIBUTE_UNUSED;
if (sz == EOF)
return -1;
items = fread (name, sz, 1, fp);
name[sz] = 0;
return sz;
}
static void
ram_read_blocks (FILE *fp, uint64_t size)
{
char name[257];
/* The RAM block table is a list of block names followed by
their sizes. Read it until the sizes sum up to SIZE bytes. */
while (size) {
get_string (fp, name);
size -= get_be64 (fp);
}
}
static uint32_t
ram_load (struct qemu_device *d, FILE *fp, enum qemu_save_section sec)
{
char name[257];
struct qemu_device_ram *dram = (struct qemu_device_ram *)d;
uint64_t header;
static int pc_ram = 0;
for (;;) {
uint64_t addr;
off_t entry;
header = get_be64 (fp);
if (feof (fp) || ferror (fp))
return 0;
if (header & RAM_SAVE_FLAG_EOS)
break;
assert (!(header & RAM_SAVE_FLAG_FULL));
addr = header & RAM_SAVE_ADDR_MASK;
if (header & RAM_SAVE_FLAG_MEM_SIZE) {
ram_alloc (dram, addr);
if (d->version_id >= 4)
ram_read_blocks(fp, addr);
continue;
}
if (d->version_id >= 4 && !(header & RAM_SAVE_FLAG_CONTINUE)) {
get_string(fp, name);
if (strcmp(name, "pc.ram") == 0)
pc_ram = 1;
else
pc_ram = 0;
}
if (header & RAM_SAVE_FLAG_COMPRESS) {
entry = RAM_OFFSET_COMPRESSED | getc(fp);
if ((d->version_id == 3) ||
(d->version_id >= 4 && pc_ram))
store_mapfile_offset(addr, &entry);
}
else if (header & RAM_SAVE_FLAG_PAGE) {
entry = ftell(fp);
if ((d->version_id == 3) ||
(d->version_id >= 4 && pc_ram))
store_mapfile_offset(addr, &entry);
fseek (fp, 4096, SEEK_CUR);
}
}
dram->fp = fp;
return QEMU_FEATURE_RAM;
}
static void
ram_free (struct qemu_device *d, struct qemu_device_list *dl)
{
struct qemu_device_ram *dram = (struct qemu_device_ram *)d;
free (dram->offsets);
}
int
ram_read_phys_page (struct qemu_device_ram *dram, void *buf, uint64_t addr)
{
off_t ofs;
ssize_t bytes ATTRIBUTE_UNUSED;
if (addr >= dram->last_ram_offset)
return false;
assert ((addr & 0xfff) == 0);
// ofs = dram->offsets[addr / 4096];
if (load_mapfile_offset(addr, &ofs) < 0)
return 0;
if ((ofs & RAM_OFFSET_COMPRESSED) == RAM_OFFSET_COMPRESSED)
memset (buf, ofs & 255, 4096);
else
bytes = pread (fileno (dram->fp), buf, 4096, ofs);
return true;
}
static struct qemu_device *
ram_init_load (struct qemu_device_list *dl,
uint32_t section_id, uint32_t instance_id,
uint32_t version_id, bool live, FILE *fp)
{
static struct qemu_device_vtbl ram = {
"ram",
ram_load,
ram_free
};
assert (version_id == 3 || version_id == 4);
kvm->mapinfo.ram_version_id = version_id;
return device_alloc (dl, sizeof (struct qemu_device_ram),
&ram, section_id, instance_id, version_id);
}
#define BLK_MIG_FLAG_EOS 2
static uint32_t
block_load (struct qemu_device *d, FILE *fp, enum qemu_save_section sec)
{
uint64_t header;
header = get_be64 (fp);
assert (header == BLK_MIG_FLAG_EOS);
return 0;
}
static struct qemu_device *
block_init_load (struct qemu_device_list *dl,
uint32_t section_id, uint32_t instance_id,
uint32_t version_id, bool live, FILE *fp)
{
static struct qemu_device_vtbl block = {
"block",
block_load,
NULL
};
return device_alloc (dl, sizeof (struct qemu_device),
&block, section_id, instance_id, version_id);
}
/* RHEL5 marker. */
static uint32_t
rhel5_marker_load (struct qemu_device *d, FILE *fp, enum qemu_save_section sec)
{
return 0;
}
static struct qemu_device *
rhel5_marker_init_load (struct qemu_device_list *dl,
uint32_t section_id, uint32_t instance_id,
uint32_t version_id, bool live, FILE *fp)
{
static struct qemu_device_vtbl rhel5_marker = {
"__rhel5",
rhel5_marker_load,
NULL
};
assert (!live);
return device_alloc (dl, sizeof (struct qemu_device),
&rhel5_marker, section_id, instance_id,
version_id);
}
/* cpu_common loader. */
struct qemu_device_cpu_common {
struct qemu_device base;
uint32_t halted;
uint32_t irq;
};
static uint32_t
cpu_common_load (struct qemu_device *d, FILE *fp, enum qemu_save_section sec)
{
struct qemu_device_cpu_common *cpu = (struct qemu_device_cpu_common *)d;
cpu->halted = get_be32 (fp);
cpu->irq = get_be32 (fp);
return 0;
}
static struct qemu_device *
cpu_common_init_load (struct qemu_device_list *dl,
uint32_t section_id, uint32_t instance_id,
uint32_t version_id, bool live, FILE *fp)
{
static struct qemu_device_vtbl cpu_common = {
"cpu_common",
cpu_common_load,
NULL
};
assert (!live);
return device_alloc (dl, sizeof (struct qemu_device_cpu_common),
&cpu_common, section_id, instance_id, version_id);
}
/* CPU loader. */
static inline uint64_t
get_be_long (FILE *fp, int size)
{
uint32_t a = size == 32 ? 0 : get_be32 (fp);
uint32_t b = get_be32 (fp);
return ((uint64_t)a << 32) | b;
}
static inline void
get_be_fp80 (FILE *fp, union qemu_fpu_reg *result)
{
result->mmx = get_be64 (fp);
result->bytes[9] = getc (fp);
result->bytes[8] = getc (fp);
}
static void
cpu_load_seg (FILE *fp, struct qemu_x86_seg *seg, int size)
{
seg->selector = get_be32 (fp);
seg->base = get_be_long (fp, size);
seg->limit = get_be32 (fp);
seg->flags = get_be32 (fp);
}
static bool
v12_has_xsave_state(FILE *fp)
{
char name[257];
bool ret = true;
long offset = ftell(fp); // save offset
/*
* peek into byte stream to check for APIC vmstate
*/
if (getc(fp) == QEMU_VM_SECTION_FULL) {
get_be32(fp); // skip section id
get_string(fp, name);
if (strcmp(name, "apic") == 0)
ret = false;
}
fseek(fp, offset, SEEK_SET); // restore offset
return ret;
}
static uint32_t
cpu_load (struct qemu_device *d, FILE *fp, int size)
{
struct qemu_device_x86 *dx86 = (struct qemu_device_x86 *)d;
uint32_t qemu_hflags = 0, qemu_hflags2 = 0;
int nregs;
uint32_t version_id = dx86->dev_base.version_id;
uint32_t rhel5_version_id;
int i;
off_t restart;
struct qemu_device *drhel5;
struct qemu_device_cpu_common *dcpu;
if (kvm->flags & KVMHOST_32)
size = 32;
restart = ftello(fp);
retry:
nregs = size == 32 ? 8 : 16;
drhel5 = device_find_instance (d->list, "__rhel5", 0);
if (drhel5 || (version_id >= 7 && version_id <= 9)) {
rhel5_version_id = version_id;
version_id = 7;
} else {
rhel5_version_id = 0;
version_id = dx86->dev_base.version_id;
}
dprintf("cpu_load: rhel5_version_id: %d (effective) version_id: %d\n",
rhel5_version_id, version_id);
dcpu = (struct qemu_device_cpu_common *)
device_find_instance (d->list, "cpu_common", d->instance_id);
if (dcpu) {
dx86->halted = dcpu->halted;
dx86->irq = dcpu->irq;
// device_free ((struct qemu_device *) dcpu);
}
for (i = 0; i < nregs; i++)
dx86->regs[i] = get_be_long (fp, size);
dx86->eip = get_be_long (fp, size);
dx86->eflags = get_be_long (fp, size);
qemu_hflags = get_be32 (fp);
dx86->fpucw = get_be16 (fp);
dx86->fpusw = get_be16 (fp);
dx86->fpu_free = get_be16 (fp);
if (get_be16 (fp))
for (i = 0; i < 8; i++)
dx86->st[i].mmx = get_be64 (fp);
else
for (i = 0; i < 8; i++)
get_be_fp80 (fp, &dx86->st[i]);
cpu_load_seg (fp, &dx86->es, size);
cpu_load_seg (fp, &dx86->cs, size);
cpu_load_seg (fp, &dx86->ss, size);
cpu_load_seg (fp, &dx86->ds, size);
cpu_load_seg (fp, &dx86->fs, size);
cpu_load_seg (fp, &dx86->gs, size);
cpu_load_seg (fp, &dx86->ldt, size);
cpu_load_seg (fp, &dx86->tr, size);
cpu_load_seg (fp, &dx86->gdt, size);
cpu_load_seg (fp, &dx86->idt, size);
dx86->sysenter.cs = get_be32 (fp);
dx86->sysenter.esp = get_be_long (fp, version_id <= 6 ? 32 : size);
dx86->sysenter.eip = get_be_long (fp, version_id <= 6 ? 32 : size);
dx86->cr0 = get_be_long (fp, size);
dx86->cr2 = get_be_long (fp, size);
dx86->cr3 = get_be_long (fp, size);
dx86->cr4 = get_be_long (fp, size);
for (i = 0; i < 8; i++)
dx86->dr[i] = get_be_long (fp, size);
dx86->a20_masked = get_be32 (fp) != 0xffffffff;
dx86->mxcsr = get_be32 (fp);
for (i = 0; i < nregs; i++)
get_qemu128 (fp, &dx86->xmm[i]);
if (size == 64) {
dx86->efer = get_be64 (fp);
dx86->star = get_be64 (fp);
dx86->lstar = get_be64 (fp);
dx86->cstar = get_be64 (fp);
dx86->fmask = get_be64 (fp);
dx86->kernel_gs_base = get_be64 (fp);
}
dx86->smbase = get_be32 (fp);
dx86->soft_mmu = qemu_hflags & (1 << 2);
dx86->smm = qemu_hflags & (1 << 19);
if (version_id == 4)
goto store;
dx86->pat = get_be64 (fp);
qemu_hflags2 = get_be32 (fp);
dx86->global_if = qemu_hflags2 & (1 << 0);
dx86->in_nmi = qemu_hflags2 & (1 << 2);
if (version_id < 6)
dx86->halted = get_be32 (fp);
dx86->svm.hsave = get_be64 (fp);
dx86->svm.vmcb = get_be64 (fp);
dx86->svm.tsc_offset = get_be64 (fp);
dx86->svm.in_vmm = qemu_hflags & (1 << 21);
dx86->svm.guest_if_mask = qemu_hflags2 & (1 << 1);
dx86->svm.guest_intr_masking = qemu_hflags2 & (1 << 3);
dx86->svm.intercept_mask = get_be64 (fp);
dx86->svm.cr_read_mask = get_be16 (fp);
dx86->svm.cr_write_mask = get_be16 (fp);
dx86->svm.dr_read_mask = get_be16 (fp);
dx86->svm.dr_write_mask = get_be16 (fp);
dx86->svm.exception_intercept_mask = get_be32 (fp);
dx86->cr8 = getc (fp);
if (version_id >= 8) {
for (i = 0; i < 11; i++)
dx86->fixed_mtrr[i] = get_be64 (fp);
dx86->deftype_mtrr = get_be64 (fp);
for (i = 0; i < 8; i++) {
dx86->variable_mtrr[i].base = get_be64 (fp);
dx86->variable_mtrr[i].mask = get_be64 (fp);
}
}
/* This was present only when KVM was enabled up to v8.
* Furthermore, it changed format in v9. */
if (version_id >= 9) {
int32_t pending_irq = (int32_t) get_be32 (fp);
if (pending_irq >= 0 && pending_irq <= 255)
dx86->kvm.int_bitmap[pending_irq / 64] |=
(uint64_t)1 << (pending_irq & 63);
dx86->kvm.mp_state = get_be32 (fp);
dx86->kvm.tsc = get_be64 (fp);
}
else if (d->list->features & QEMU_FEATURE_KVM) {
for (i = 0; i < 4; i++)
dx86->kvm.int_bitmap[i] = get_be64 (fp);
dx86->kvm.tsc = get_be64 (fp);
if (version_id >= 5)
dx86->kvm.mp_state = get_be32 (fp);
}
if (version_id >= 11) {
dx86->kvm.exception_injected = get_be32 (fp);
}
if (rhel5_version_id >= 8) {
dx86->kvm.system_time_msr = get_be64 (fp);
dx86->kvm.wall_clock_msr = get_be64 (fp);
}
if (version_id >= 11 || rhel5_version_id >= 9) {
dx86->kvm.soft_interrupt = getc (fp);
dx86->kvm.nmi_injected = getc (fp);
dx86->kvm.nmi_pending = getc (fp);
dx86->kvm.has_error_code = getc (fp);
dx86->kvm.sipi_vector = get_be32 (fp);
}
if (version_id >= 10) {
dx86->mce.mcg_cap = get_be64 (fp);
dx86->mce.mcg_status = get_be64 (fp);
dx86->mce.mcg_ctl = get_be64 (fp);
for (i = 0; i < 10 * 4; i++)
dx86->mce.mce_banks[i] = get_be64 (fp);
}
if (version_id >= 11) {
dx86->tsc_aux = get_be64 (fp);
dx86->kvm.system_time_msr = get_be64 (fp);
dx86->kvm.wall_clock_msr = get_be64 (fp);
}
if (version_id >= 12 && v12_has_xsave_state(fp)) {
dx86->xcr0 = get_be64 (fp);
dx86->xstate_bv = get_be64 (fp);
for (i = 0; i < nregs; i++)
get_qemu128 (fp, &dx86->ymmh_regs[i]);
}
store:
if (!kvmdump_regs_store(d->instance_id, dx86)) {
size = 32;
kvm->flags |= KVMHOST_32;
fseeko(fp, restart, SEEK_SET);
dprintf("cpu_load: invalid registers: retry with 32-bit host\n");
goto retry;
}
if (dcpu)
device_free ((struct qemu_device *) dcpu);
return QEMU_FEATURE_CPU;
}
static uint32_t
cpu_load_32 (struct qemu_device *d, FILE *fp, enum qemu_save_section sec)
{
return cpu_load (d, fp, 32);
}
static struct qemu_device *
cpu_init_load_32 (struct qemu_device_list *dl,
uint32_t section_id, uint32_t instance_id,
uint32_t version_id, bool live, FILE *fp)
{
struct qemu_device_x86 *dx86;
static struct qemu_device_vtbl cpu = {
"cpu",
cpu_load_32,
NULL
};
assert (!live);
// assert (version_id >= 4 && version_id <= 9);
assert (version_id >= 4 && version_id <= 12);
kvm->mapinfo.cpu_version_id = version_id;
dx86 = (struct qemu_device_x86 *)
device_alloc (dl, sizeof (struct qemu_device_x86),
&cpu, section_id, instance_id, version_id);
return (struct qemu_device *) dx86;
}
static uint32_t
cpu_load_64 (struct qemu_device *d, FILE *fp, enum qemu_save_section sec)
{
return cpu_load (d, fp, 64);
}
static struct qemu_device *
cpu_init_load_64 (struct qemu_device_list *dl,
uint32_t section_id, uint32_t instance_id,
uint32_t version_id, bool live, FILE *fp)
{
struct qemu_device_x86 *dx86;
static struct qemu_device_vtbl cpu = {
"cpu",
cpu_load_64,
NULL
};
assert (!live);
// assert (version_id >= 4 && version_id <= 9);
assert (version_id >= 4 && version_id <= 12);
kvm->mapinfo.cpu_version_id = version_id;
dx86 = (struct qemu_device_x86 *)
device_alloc (dl, sizeof (struct qemu_device_x86),
&cpu, section_id, instance_id, version_id);
return (struct qemu_device *) dx86;
}
/* IOAPIC loader. */
static uint32_t
apic_load (struct qemu_device *d, FILE *fp, enum qemu_save_section sec)
{
switch (d->version_id) {
case 1: fseek (fp, 173, SEEK_CUR); break;
case 2:
case 3: fseek (fp, 181, SEEK_CUR); break;
}
return 0;
}
static struct qemu_device *
apic_init_load (struct qemu_device_list *dl,
uint32_t section_id, uint32_t instance_id,
uint32_t version_id, bool live, FILE *fp)
{
static struct qemu_device_vtbl apic = {
"apic",
apic_load,
NULL
};
assert (!live);
return device_alloc (dl, sizeof (struct qemu_device),
&apic, section_id, instance_id, version_id);
}
/* timer loader. */
static uint32_t
timer_load (struct qemu_device *d, FILE *fp, enum qemu_save_section sec)
{
fseek (fp, 24, SEEK_CUR);
return QEMU_FEATURE_TIMER;
}
static struct qemu_device *
timer_init_load (struct qemu_device_list *dl,
uint32_t section_id, uint32_t instance_id,
uint32_t version_id, bool live, FILE *fp)
{
static struct qemu_device_vtbl timer = {
"timer",
timer_load,
NULL
};
assert (!live);
return device_alloc (dl, sizeof (struct qemu_device),
&timer, section_id, instance_id, version_id);
}
/* kvmclock loader. */
static uint32_t
kvmclock_load (struct qemu_device *d, FILE *fp, enum qemu_save_section sec)
{
fseek (fp, 8, SEEK_CUR);
return QEMU_FEATURE_KVM;
}
static struct qemu_device *
kvmclock_init_load (struct qemu_device_list *dl,
uint32_t section_id, uint32_t instance_id,
uint32_t version_id, bool live, FILE *fp)
{
static struct qemu_device_vtbl kvmclock = {
"kvmclock",
kvmclock_load,
NULL
};
assert (!live);
return device_alloc (dl, sizeof (struct qemu_device),
&kvmclock, section_id, instance_id, version_id);
}
/* kvm-tpr-opt loader. */
static uint32_t
kvm_tpr_opt_load (struct qemu_device *d, FILE *fp, enum qemu_save_section sec)
{
fseek (fp, 144, SEEK_CUR);
return QEMU_FEATURE_KVM;
}
static struct qemu_device *
kvm_tpr_opt_init_load (struct qemu_device_list *dl,
uint32_t section_id, uint32_t instance_id,
uint32_t version_id, bool live, FILE *fp)
{
static struct qemu_device_vtbl kvm_tpr_opt = {
"kvm-tpr-opt",
kvm_tpr_opt_load,
NULL
};
assert (!live);
return device_alloc (dl, sizeof (struct qemu_device),
&kvm_tpr_opt, section_id, instance_id, version_id);
}
/* Putting it together. */
const struct qemu_device_loader devices_x86_64[] = {
{ "__rhel5", rhel5_marker_init_load },
{ "cpu_common", cpu_common_init_load },
{ "kvm-tpr-opt", kvm_tpr_opt_init_load },
{ "kvmclock", kvmclock_init_load },
{ "cpu", cpu_init_load_64 },
{ "apic", apic_init_load },
{ "block", block_init_load },
{ "ram", ram_init_load },
{ "timer", timer_init_load },
{ NULL, NULL }
};
const struct qemu_device_loader devices_x86_32[] = {
{ "__rhel5", rhel5_marker_init_load },
{ "cpu_common", cpu_common_init_load },
{ "kvm-tpr-opt", kvm_tpr_opt_init_load },
{ "kvmclock", kvmclock_init_load },
{ "cpu", cpu_init_load_32 },
{ "apic", apic_init_load },
{ "block", block_init_load },
{ "ram", ram_init_load },
{ "timer", timer_init_load },
{ NULL, NULL }
};
#define QEMU_VM_FILE_MAGIC 0x5145564D
#define LIBVIRT_QEMU_VM_FILE_MAGIC 0x4c696276
struct libvirt_header {
char magic[16];
uint32_t version;
uint32_t xml_length;
uint32_t was_running;
uint32_t padding[16];
};
static long device_search(const struct qemu_device_loader *, FILE *);
static struct qemu_device *
device_get (const struct qemu_device_loader *devices,
struct qemu_device_list *dl, enum qemu_save_section sec, FILE *fp)
{
char name[257];
uint32_t section_id, instance_id, version_id;
// bool live;
const struct qemu_device_loader *devp;
long next_device_offset;
next_device:
devp = devices;
if (sec == QEMU_VM_SUBSECTION) {
get_string(fp, name);
goto search_device;
}
section_id = get_be32 (fp);
if (sec != QEMU_VM_SECTION_START &&
sec != QEMU_VM_SECTION_FULL)
return device_find (dl, section_id);
get_string(fp, name);
instance_id = get_be32 (fp);
version_id = get_be32 (fp);
while (devp->name && strcmp (devp->name, name))
devp++;
if (!devp->name) {
search_device:
dprintf("device_get: unknown/unsupported: \"%s\"\n", name);
if ((next_device_offset = device_search(devices, fp))) {
fseek(fp, next_device_offset, SEEK_CUR);
sec = getc(fp);
if (sec == QEMU_VM_EOF)
return NULL;
goto next_device;
}
return NULL;
}
return devp->init_load (dl, section_id, instance_id, version_id,
sec == QEMU_VM_SECTION_START, fp);
}
struct qemu_device_list *
qemu_load (const struct qemu_device_loader *devices, uint32_t required_features,
FILE *fp)
{
struct qemu_device_list *result = NULL;
struct qemu_device *last = NULL;;
size_t items ATTRIBUTE_UNUSED;
uint32_t footerSecId ATTRIBUTE_UNUSED;
char name[257];
switch (get_be32 (fp)) {
case QEMU_VM_FILE_MAGIC:
break;
case LIBVIRT_QEMU_VM_FILE_MAGIC: {
struct libvirt_header header;
memcpy (header.magic, "Libv", 4);
items = fread (&header.magic[4], sizeof (header) - 4, 1, fp);
if (memcmp ("LibvirtQemudSave", header.magic, 16))
goto fail;
fseek (fp, header.xml_length, SEEK_CUR);
if (get_be32 (fp) != QEMU_VM_FILE_MAGIC)
goto fail;
break;
}
default:
goto fail;
}
if (get_be32 (fp) != 3)
return NULL;
dprintf("\n");
result = calloc (1, sizeof (struct qemu_device_list));
for (;;) {
struct qemu_device *d;
uint32_t features;
enum qemu_save_section sec = getc (fp);
if (feof (fp) || ferror (fp))
break;
if (sec == QEMU_VM_EOF)
break;
if (sec == QEMU_VM_SECTION_FOOTER) {
footerSecId = get_be32 (fp);
continue;
}
if (sec == QEMU_VM_CONFIGURATION) {
uint32_t len = get_be32 (fp);
get_string_len (fp, name, len);
continue;
}
d = device_get (devices, result, sec, fp);
if (!d)
break;
if (d != last) {
dprintf("qemu_load: \"%s\"\n", d->vtbl->name);
last = d;
}
features = d->vtbl->load (d, fp, sec);
if (feof (fp) || ferror (fp))
break;
if (sec == QEMU_VM_SECTION_END || sec == QEMU_VM_SECTION_FULL)
result->features |= features;