forked from crash-utility/crash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xendump.c
2859 lines (2419 loc) · 75.7 KB
/
xendump.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
/*
* xendump.c
*
* Copyright (C) 2006-2011, 2013 David Anderson
* Copyright (C) 2006-2011, 2013 Red Hat, Inc. All rights reserved.
*
* 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.
*/
#include "defs.h"
#include "xendump.h"
static struct xendump_data xendump_data = { 0 };
static struct xendump_data *xd = &xendump_data;
static int xc_save_verify(char *);
static int xc_core_verify(char *, char *);
static int xc_save_read(void *, int, ulong, physaddr_t);
static int xc_core_read(void *, int, ulong, physaddr_t);
static int xc_core_mfns(ulong, FILE *);
static void poc_store(ulong, off_t);
static off_t poc_get(ulong, int *);
static void xen_dump_vmconfig(FILE *);
static void xc_core_create_pfn_tables(void);
static ulong xc_core_pfn_to_page_index(ulong);
static int xc_core_pfn_valid(ulong);
static void xendump_print(char *fmt, ...);
static int xc_core_elf_verify(char *, char *);
static void xc_core_elf_dump(void);
static char *xc_core_elf_mfn_to_page(ulong, char *);
static int xc_core_elf_mfn_to_page_index(ulong);
static ulong xc_core_elf_pfn_valid(ulong);
static ulong xc_core_elf_pfn_to_page_index(ulong);
static void xc_core_dump_Elf32_Ehdr(Elf32_Ehdr *);
static void xc_core_dump_Elf64_Ehdr(Elf64_Ehdr *);
static void xc_core_dump_Elf32_Shdr(Elf32_Off offset, int);
static void xc_core_dump_Elf64_Shdr(Elf64_Off offset, int);
static char *xc_core_strtab(uint32_t, char *);
static void xc_core_dump_elfnote(off_t, size_t, int);
static void xc_core_elf_pfn_init(void);
#define ELFSTORE 1
#define ELFREAD 0
/*
* Determine whether a file is a xendump creation, and if TRUE,
* initialize the xendump_data structure.
*/
int
is_xendump(char *file)
{
int verified;
char buf[BUFSIZE];
if ((xd->xfd = open(file, O_RDWR)) < 0) {
if ((xd->xfd = open(file, O_RDONLY)) < 0) {
sprintf(buf, "%s: open", file);
perror(buf);
return FALSE;
}
}
if (read(xd->xfd, buf, BUFSIZE) != BUFSIZE)
return FALSE;
if (machine_type("X86") || machine_type("X86_64"))
xd->page_size = 4096;
else if (machine_type("IA64") && !machdep->pagesize)
xd->page_size = 16384;
else
xd->page_size = machdep->pagesize;
verified = xc_save_verify(buf) || xc_core_verify(file, buf);
if (!verified)
close(xd->xfd);
return (verified);
}
/*
* Verify whether the dump was created by the xc_domain_dumpcore()
* library function in libxc/xc_core.c.
*/
static int
xc_core_verify(char *file, char *buf)
{
struct xc_core_header *xcp;
xcp = (struct xc_core_header *)buf;
if (xc_core_elf_verify(file, buf))
return TRUE;
if ((xcp->xch_magic != XC_CORE_MAGIC) &&
(xcp->xch_magic != XC_CORE_MAGIC_HVM))
return FALSE;
if (!xcp->xch_nr_vcpus) {
error(INFO,
"faulty xc_core dump file header: xch_nr_vcpus is 0\n\n");
fprintf(stderr, " xch_magic: %x\n", xcp->xch_magic);
fprintf(stderr, " xch_nr_vcpus: %d\n", xcp->xch_nr_vcpus);
fprintf(stderr, " xch_nr_pages: %d\n", xcp->xch_nr_pages);
fprintf(stderr, " xch_ctxt_offset: %d\n", xcp->xch_ctxt_offset);
fprintf(stderr, " xch_index_offset: %d\n", xcp->xch_index_offset);
fprintf(stderr, " xch_pages_offset: %d\n\n", xcp->xch_pages_offset);
clean_exit(1);
}
xd->xc_core.header.xch_magic = xcp->xch_magic;
xd->xc_core.header.xch_nr_vcpus = xcp->xch_nr_vcpus;
xd->xc_core.header.xch_nr_pages = xcp->xch_nr_pages;
xd->xc_core.header.xch_ctxt_offset = (ulong)xcp->xch_ctxt_offset;
xd->xc_core.header.xch_index_offset = (ulong)xcp->xch_index_offset;
xd->xc_core.header.xch_pages_offset = (ulong)xcp->xch_pages_offset;
xd->flags |= (XENDUMP_LOCAL | XC_CORE_ORIG | XC_CORE_P2M_CREATE);
if (xc_core_mfns(XC_CORE_64BIT_HOST, stderr))
xd->flags |= XC_CORE_64BIT_HOST;
if (!xd->page_size)
error(FATAL,
"unknown page size: use -p <pagesize> command line option\n");
if (!(xd->page = (char *)malloc(xd->page_size)))
error(FATAL, "cannot malloc page space.");
if (!(xd->poc = (struct pfn_offset_cache *)calloc
(PFN_TO_OFFSET_CACHE_ENTRIES,
sizeof(struct pfn_offset_cache))))
error(FATAL, "cannot malloc pfn_offset_cache\n");
xd->last_pfn = ~(0UL);
if (CRASHDEBUG(1))
xendump_memory_dump(stderr);
return TRUE;
}
/*
* Do the work for read_xendump() for the XC_CORE dumpfile format.
*/
static int
xc_core_read(void *bufptr, int cnt, ulong addr, physaddr_t paddr)
{
ulong pfn, page_index;
off_t offset;
int redundant;
if (xd->flags & (XC_CORE_P2M_CREATE|XC_CORE_PFN_CREATE))
xc_core_create_pfn_tables();
pfn = (ulong)BTOP(paddr);
if ((offset = poc_get(pfn, &redundant))) {
if (!redundant) {
if (lseek(xd->xfd, offset, SEEK_SET) == -1)
return SEEK_ERROR;
if (read(xd->xfd, xd->page, xd->page_size) !=
xd->page_size)
return READ_ERROR;
xd->last_pfn = pfn;
}
BCOPY(xd->page + PAGEOFFSET(paddr), bufptr, cnt);
return cnt;
}
if ((page_index = xc_core_pfn_to_page_index(pfn)) ==
PFN_NOT_FOUND)
return READ_ERROR;
offset = (off_t)xd->xc_core.header.xch_pages_offset +
((off_t)(page_index) * (off_t)xd->page_size);
if (lseek(xd->xfd, offset, SEEK_SET) == -1)
return SEEK_ERROR;
if (read(xd->xfd, xd->page, xd->page_size) != xd->page_size)
return READ_ERROR;
poc_store(pfn, offset);
BCOPY(xd->page + PAGEOFFSET(paddr), bufptr, cnt);
return cnt;
}
/*
* Verify whether the dumpfile was created by the "xm save" facility.
* This gets started by the "save" function in XendCheckpoint.py, and
* then by xc_save.c, with the work done in the xc_linux_save() library
* function in libxc/xc_linux_save.c.
*/
#define MAX_BATCH_SIZE 1024
/*
* Number of P2M entries in a page.
*/
#define ULPP (xd->page_size/sizeof(unsigned long))
/*
* Number of P2M entries in the pfn_to_mfn_frame_list.
*/
#define P2M_FL_ENTRIES (((xd->xc_save.nr_pfns)+ULPP-1)/ULPP)
/*
* Size in bytes of the pfn_to_mfn_frame_list.
*/
#define P2M_FL_SIZE ((P2M_FL_ENTRIES)*sizeof(unsigned long))
#define XTAB (0xf<<28) /* invalid page */
#define LTAB_MASK XTAB
static int
xc_save_verify(char *buf)
{
int i, batch_count, done_batch, *intptr;
ulong flags, *ulongptr;
ulong batch_index, total_pages_read;
ulong N;
if (!STRNEQ(buf, XC_SAVE_SIGNATURE))
return FALSE;
if (lseek(xd->xfd, strlen(XC_SAVE_SIGNATURE), SEEK_SET) == -1)
return FALSE;
flags = XC_SAVE;
if (CRASHDEBUG(1)) {
fprintf(stderr, "\"%s\"\n", buf);
fprintf(stderr, "endian: %d %s\n", __BYTE_ORDER,
__BYTE_ORDER == __BIG_ENDIAN ? "__BIG_ENDIAN" :
(__BYTE_ORDER == __LITTLE_ENDIAN ?
"__LITTLE_ENDIAN" : "???"));
}
/*
* size of vmconfig data structure (big-endian)
*/
if (read(xd->xfd, buf, sizeof(int)) != sizeof(int))
return FALSE;
intptr = (int *)buf;
if (CRASHDEBUG(1) && BYTE_SWAP_REQUIRED(__BIG_ENDIAN)) {
fprintf(stderr, "byte-swap required for this:\n");
for (i = 0; i < sizeof(int); i++)
fprintf(stderr, "[%x]", buf[i] & 0xff);
fprintf(stderr, ": %x -> ", *intptr);
}
xd->xc_save.vmconfig_size = swab32(*intptr);
if (CRASHDEBUG(1))
fprintf(stderr, "%x\n", xd->xc_save.vmconfig_size);
if (!(xd->xc_save.vmconfig_buf = (char *)malloc
(xd->xc_save.vmconfig_size)))
error(FATAL, "cannot malloc xc_save vmconfig space.");
if (!xd->page_size)
error(FATAL,
"unknown page size: use -p <pagesize> command line option\n");
if (!(xd->page = (char *)malloc(xd->page_size)))
error(FATAL, "cannot malloc page space.");
if (!(xd->poc = (struct pfn_offset_cache *)calloc
(PFN_TO_OFFSET_CACHE_ENTRIES,
sizeof(struct pfn_offset_cache))))
error(FATAL, "cannot malloc pfn_offset_cache\n");
xd->last_pfn = ~(0UL);
if (!(xd->xc_save.region_pfn_type = (ulong *)calloc
(MAX_BATCH_SIZE, sizeof(ulong))))
error(FATAL, "cannot malloc region_pfn_type\n");
if (read(xd->xfd, xd->xc_save.vmconfig_buf,
xd->xc_save.vmconfig_size) != xd->xc_save.vmconfig_size)
goto xc_save_bailout;
/*
* nr_pfns (native byte order)
*/
if (read(xd->xfd, buf, sizeof(ulong)) != sizeof(ulong))
goto xc_save_bailout;
ulongptr = (ulong *)buf;
if (CRASHDEBUG(1)) {
for (i = 0; i < sizeof(ulong); i++)
fprintf(stderr, "[%x]", buf[i] & 0xff);
fprintf(stderr, ": %lx (nr_pfns)\n", *ulongptr);
}
xd->xc_save.nr_pfns = *ulongptr;
if (machine_type("IA64"))
goto xc_save_ia64;
/*
* Get a local copy of the live_P2M_frame_list
*/
if (!(xd->xc_save.p2m_frame_list = (unsigned long *)malloc(P2M_FL_SIZE)))
error(FATAL, "cannot allocate p2m_frame_list array");
if (!(xd->xc_save.batch_offsets = (off_t *)calloc((size_t)P2M_FL_ENTRIES,
sizeof(off_t))))
error(FATAL, "cannot allocate batch_offsets array");
xd->xc_save.batch_count = P2M_FL_ENTRIES;
if (read(xd->xfd, xd->xc_save.p2m_frame_list, P2M_FL_SIZE) !=
P2M_FL_SIZE)
goto xc_save_bailout;
if (CRASHDEBUG(1))
fprintf(stderr, "pre-batch file pointer: %lld\n",
(ulonglong)lseek(xd->xfd, 0L, SEEK_CUR));
/*
* ...
* int batch_count
* ulong region pfn_type[batch_count]
* page 0
* page 1
* ...
* page batch_count-1
* (repeat)
*/
total_pages_read = 0;
batch_index = 0;
done_batch = FALSE;
while (!done_batch) {
xd->xc_save.batch_offsets[batch_index] = (off_t)
lseek(xd->xfd, 0L, SEEK_CUR);
if (read(xd->xfd, &batch_count, sizeof(int)) != sizeof(int))
goto xc_save_bailout;
if (CRASHDEBUG(1))
fprintf(stderr, "batch[%ld]: %d ",
batch_index, batch_count);
batch_index++;
if (batch_index >= P2M_FL_ENTRIES) {
fprintf(stderr, "more than %ld batches encountered?\n",
P2M_FL_ENTRIES);
goto xc_save_bailout;
}
switch (batch_count)
{
case 0:
if (CRASHDEBUG(1)) {
fprintf(stderr,
": Batch work is done: %ld pages read (P2M_FL_ENTRIES: %ld)\n",
total_pages_read, P2M_FL_ENTRIES);
}
done_batch = TRUE;
continue;
case -1:
if (CRASHDEBUG(1))
fprintf(stderr, ": Entering page verify mode\n");
continue;
default:
if (batch_count > MAX_BATCH_SIZE) {
if (CRASHDEBUG(1))
fprintf(stderr,
": Max batch size exceeded. Giving up.\n");
done_batch = TRUE;
continue;
}
if (CRASHDEBUG(1))
fprintf(stderr, "\n");
break;
}
if (read(xd->xfd, xd->xc_save.region_pfn_type, batch_count * sizeof(ulong)) !=
batch_count * sizeof(ulong))
goto xc_save_bailout;
for (i = 0; i < batch_count; i++) {
unsigned long pagetype;
unsigned long pfn;
pfn = xd->xc_save.region_pfn_type[i] & ~LTAB_MASK;
pagetype = xd->xc_save.region_pfn_type[i] & LTAB_MASK;
if (pagetype == XTAB)
/* a bogus/unmapped page: skip it */
continue;
if (pfn > xd->xc_save.nr_pfns) {
if (CRASHDEBUG(1))
fprintf(stderr,
"batch_count: %d pfn %ld out of range",
batch_count, pfn);
}
if (lseek(xd->xfd, xd->page_size, SEEK_CUR) == -1)
goto xc_save_bailout;
total_pages_read++;
}
}
/*
* Get the list of PFNs that are not in the psuedo-phys map
*/
if (read(xd->xfd, &xd->xc_save.pfns_not,
sizeof(xd->xc_save.pfns_not)) != sizeof(xd->xc_save.pfns_not))
goto xc_save_bailout;
if (CRASHDEBUG(1))
fprintf(stderr, "PFNs not in pseudo-phys map: %d\n",
xd->xc_save.pfns_not);
if ((total_pages_read + xd->xc_save.pfns_not) !=
xd->xc_save.nr_pfns)
error(WARNING,
"nr_pfns: %ld != (total pages: %ld + pages not saved: %d)\n",
xd->xc_save.nr_pfns, total_pages_read,
xd->xc_save.pfns_not);
xd->xc_save.pfns_not_offset = lseek(xd->xfd, 0L, SEEK_CUR);
if (lseek(xd->xfd, sizeof(ulong) * xd->xc_save.pfns_not, SEEK_CUR) == -1)
goto xc_save_bailout;
xd->xc_save.vcpu_ctxt_offset = lseek(xd->xfd, 0L, SEEK_CUR);
lseek(xd->xfd, 0, SEEK_END);
lseek(xd->xfd, -((off_t)(xd->page_size)), SEEK_CUR);
xd->xc_save.shared_info_page_offset = lseek(xd->xfd, 0L, SEEK_CUR);
xd->flags |= (XENDUMP_LOCAL | flags);
kt->xen_flags |= (CANONICAL_PAGE_TABLES|XEN_SUSPEND);
if (CRASHDEBUG(1))
xendump_memory_dump(stderr);
return TRUE;
xc_save_ia64:
/*
* Completely different format for ia64:
*
* ...
* pfn #
* page data
* pfn #
* page data
* ...
*/
free(xd->poc);
xd->poc = NULL;
free(xd->xc_save.region_pfn_type);
xd->xc_save.region_pfn_type = NULL;
if (!(xd->xc_save.ia64_page_offsets =
(ulong *)calloc(xd->xc_save.nr_pfns, sizeof(off_t))))
error(FATAL, "cannot allocate ia64_page_offsets array");
/*
* version
*/
if (read(xd->xfd, buf, sizeof(ulong)) != sizeof(ulong))
goto xc_save_bailout;
xd->xc_save.ia64_version = *((ulong *)buf);
if (CRASHDEBUG(1))
fprintf(stderr, "ia64 version: %lx\n",
xd->xc_save.ia64_version);
/*
* xen_domctl_arch_setup structure
*/
if (read(xd->xfd, buf, sizeof(xen_domctl_arch_setup_t)) !=
sizeof(xen_domctl_arch_setup_t))
goto xc_save_bailout;
if (CRASHDEBUG(1)) {
xen_domctl_arch_setup_t *setup =
(xen_domctl_arch_setup_t *)buf;
fprintf(stderr, "xen_domctl_arch_setup:\n");
fprintf(stderr, " flags: %lx\n", (ulong)setup->flags);
fprintf(stderr, " bp: %lx\n", (ulong)setup->bp);
fprintf(stderr, " maxmem: %lx\n", (ulong)setup->maxmem);
fprintf(stderr, " xsi_va: %lx\n", (ulong)setup->xsi_va);
fprintf(stderr, "hypercall_imm: %x\n", setup->hypercall_imm);
}
for (i = N = 0; i < xd->xc_save.nr_pfns; i++) {
if (read(xd->xfd, &N, sizeof(N)) != sizeof(N))
goto xc_save_bailout;
if (N < xd->xc_save.nr_pfns)
xd->xc_save.ia64_page_offsets[N] =
lseek(xd->xfd, 0, SEEK_CUR);
else
error(WARNING,
"[%d]: pfn of %lx (0x%lx) in ia64 canonical page list exceeds %ld\n",
i, N, N, xd->xc_save.nr_pfns);
if (CRASHDEBUG(1)) {
if ((i < 10) || (N >= (xd->xc_save.nr_pfns-10)))
fprintf(stderr, "[%d]: %ld\n%s", i, N,
i == 9 ? "...\n" : "");
}
if ((N+1) >= xd->xc_save.nr_pfns)
break;
if (lseek(xd->xfd, xd->page_size, SEEK_CUR) == -1)
goto xc_save_bailout;
}
if (CRASHDEBUG(1)) {
for (i = N = 0; i < xd->xc_save.nr_pfns; i++) {
if (!xd->xc_save.ia64_page_offsets[i])
N++;
}
fprintf(stderr, "%ld out of %ld pfns not dumped\n",
N, xd->xc_save.nr_pfns);
}
xd->flags |= (XENDUMP_LOCAL | flags | XC_SAVE_IA64);
kt->xen_flags |= (CANONICAL_PAGE_TABLES|XEN_SUSPEND);
if (CRASHDEBUG(1))
xendump_memory_dump(stderr);
return TRUE;
xc_save_bailout:
error(INFO,
"xc_save_verify: \"LinuxGuestRecord\" file handling/format error\n");
if (xd->xc_save.p2m_frame_list) {
free(xd->xc_save.p2m_frame_list);
xd->xc_save.p2m_frame_list = NULL;
}
if (xd->xc_save.batch_offsets) {
free(xd->xc_save.batch_offsets);
xd->xc_save.batch_offsets = NULL;
}
if (xd->xc_save.vmconfig_buf) {
free(xd->xc_save.vmconfig_buf);
xd->xc_save.vmconfig_buf = NULL;
}
if (xd->page) {
free(xd->page);
xd->page = NULL;
}
return FALSE;
}
/*
* Do the work for read_xendump() for the XC_SAVE dumpfile format.
*/
static int
xc_save_read(void *bufptr, int cnt, ulong addr, physaddr_t paddr)
{
int b, i, redundant;
ulong reqpfn;
int batch_count;
off_t file_offset;
reqpfn = (ulong)BTOP(paddr);
if (CRASHDEBUG(8))
fprintf(xd->ofp,
"xc_save_read(bufptr: %lx cnt: %d addr: %lx paddr: %llx (%ld, 0x%lx)\n",
(ulong)bufptr, cnt, addr, (ulonglong)paddr, reqpfn, reqpfn);
if (xd->flags & XC_SAVE_IA64) {
if (reqpfn >= xd->xc_save.nr_pfns) {
if (CRASHDEBUG(1))
fprintf(xd->ofp,
"xc_save_read: pfn %lx too large: nr_pfns: %lx\n",
reqpfn, xd->xc_save.nr_pfns);
return SEEK_ERROR;
}
file_offset = xd->xc_save.ia64_page_offsets[reqpfn];
if (!file_offset) {
if (CRASHDEBUG(1))
fprintf(xd->ofp,
"xc_save_read: pfn %lx not stored in xendump\n",
reqpfn);
return SEEK_ERROR;
}
if (reqpfn != xd->last_pfn) {
if (lseek(xd->xfd, file_offset, SEEK_SET) == -1)
return SEEK_ERROR;
if (read(xd->xfd, xd->page, xd->page_size) != xd->page_size)
return READ_ERROR;
} else {
xd->redundant++;
xd->cache_hits++;
}
xd->accesses++;
xd->last_pfn = reqpfn;
BCOPY(xd->page + PAGEOFFSET(paddr), bufptr, cnt);
return cnt;
}
if ((file_offset = poc_get(reqpfn, &redundant))) {
if (!redundant) {
if (lseek(xd->xfd, file_offset, SEEK_SET) == -1)
return SEEK_ERROR;
if (read(xd->xfd, xd->page, xd->page_size) != xd->page_size)
return READ_ERROR;
xd->last_pfn = reqpfn;
} else if (CRASHDEBUG(1))
console("READ %ld (0x%lx) skipped!\n", reqpfn, reqpfn);
BCOPY(xd->page + PAGEOFFSET(paddr), bufptr, cnt);
return cnt;
}
/*
* ...
* int batch_count
* ulong region pfn_type[batch_count]
* page 0
* page 1
* ...
* page batch_count-1
* (repeat)
*/
for (b = 0; b < xd->xc_save.batch_count; b++) {
if (lseek(xd->xfd, xd->xc_save.batch_offsets[b], SEEK_SET) == -1)
return SEEK_ERROR;
if (CRASHDEBUG(8))
fprintf(xd->ofp, "check batch[%d]: offset: %llx\n",
b, (ulonglong)xd->xc_save.batch_offsets[b]);
if (read(xd->xfd, &batch_count, sizeof(int)) != sizeof(int))
return READ_ERROR;
switch (batch_count)
{
case 0:
if (CRASHDEBUG(1) && !STREQ(pc->curcmd, "search")) {
fprintf(xd->ofp,
"batch[%d]: has count of zero -- bailing out on pfn %ld\n",
b, reqpfn);
}
return READ_ERROR;
case -1:
return READ_ERROR;
default:
if (CRASHDEBUG(8))
fprintf(xd->ofp,
"batch[%d]: offset: %llx batch count: %d\n",
b, (ulonglong)xd->xc_save.batch_offsets[b],
batch_count);
break;
}
if (read(xd->xfd, xd->xc_save.region_pfn_type, batch_count * sizeof(ulong)) !=
batch_count * sizeof(ulong))
return READ_ERROR;
for (i = 0; i < batch_count; i++) {
unsigned long pagetype;
unsigned long pfn;
pfn = xd->xc_save.region_pfn_type[i] & ~LTAB_MASK;
pagetype = xd->xc_save.region_pfn_type[i] & LTAB_MASK;
if (pagetype == XTAB)
/* a bogus/unmapped page: skip it */
continue;
if (pfn > xd->xc_save.nr_pfns) {
if (CRASHDEBUG(1))
fprintf(stderr,
"batch_count: %d pfn %ld out of range",
batch_count, pfn);
}
if (pfn == reqpfn) {
file_offset = lseek(xd->xfd, 0, SEEK_CUR);
poc_store(pfn, file_offset);
if (read(xd->xfd, xd->page, xd->page_size) !=
xd->page_size)
return READ_ERROR;
BCOPY(xd->page + PAGEOFFSET(paddr), bufptr, cnt);
return cnt;
}
if (lseek(xd->xfd, xd->page_size, SEEK_CUR) == -1)
return SEEK_ERROR;
}
}
return READ_ERROR;
}
/*
* Stash a pfn's offset. If they're all in use, put it in the
* least-used slot that's closest to the beginning of the array.
*/
static void
poc_store(ulong pfn, off_t file_offset)
{
int i;
struct pfn_offset_cache *poc, *plow;
ulong curlow;
curlow = ~(0UL);
plow = NULL;
poc = xd->poc;
for (i = 0; i < PFN_TO_OFFSET_CACHE_ENTRIES; i++, poc++) {
if (poc->cnt == 0) {
poc->cnt = 1;
poc->pfn = pfn;
poc->file_offset = file_offset;
xd->last_pfn = pfn;
return;
}
if (poc->cnt < curlow) {
curlow = poc->cnt;
plow = poc;
}
}
plow->cnt = 1;
plow->pfn = pfn;
plow->file_offset = file_offset;
xd->last_pfn = pfn;
}
/*
* Check whether a pfn's offset has been cached.
*/
static off_t
poc_get(ulong pfn, int *redundant)
{
int i;
struct pfn_offset_cache *poc;
xd->accesses++;
if (pfn == xd->last_pfn) {
xd->redundant++;
*redundant = TRUE;
return 1;
} else
*redundant = FALSE;
poc = xd->poc;
for (i = 0; i < PFN_TO_OFFSET_CACHE_ENTRIES; i++, poc++) {
if (poc->cnt && (poc->pfn == pfn)) {
poc->cnt++;
xd->cache_hits++;
return poc->file_offset;
}
}
return 0;
}
/*
* Perform any post-dumpfile determination stuff here.
*/
int
xendump_init(char *unused, FILE *fptr)
{
if (!XENDUMP_VALID())
return FALSE;
xd->ofp = fptr;
return TRUE;
}
int
read_xendump(int fd, void *bufptr, int cnt, ulong addr, physaddr_t paddr)
{
if (pc->curcmd_flags & XEN_MACHINE_ADDR)
return READ_ERROR;
switch (xd->flags & (XC_SAVE|XC_CORE_ORIG|XC_CORE_ELF))
{
case XC_SAVE:
return xc_save_read(bufptr, cnt, addr, paddr);
case XC_CORE_ORIG:
case XC_CORE_ELF:
return xc_core_read(bufptr, cnt, addr, paddr);
default:
return READ_ERROR;
}
}
int
read_xendump_hyper(int fd, void *bufptr, int cnt, ulong addr, physaddr_t paddr)
{
ulong pfn, page_index;
off_t offset;
pfn = (ulong)BTOP(paddr);
/* ODA: pfn == mfn !!! */
if ((page_index = xc_core_mfn_to_page_index(pfn)) == PFN_NOT_FOUND)
return READ_ERROR;
offset = (off_t)xd->xc_core.header.xch_pages_offset +
((off_t)(page_index) * (off_t)xd->page_size);
if (lseek(xd->xfd, offset, SEEK_SET) == -1)
return SEEK_ERROR;
if (read(xd->xfd, xd->page, xd->page_size) != xd->page_size)
return READ_ERROR;
BCOPY(xd->page + PAGEOFFSET(paddr), bufptr, cnt);
return cnt;
}
int
write_xendump(int fd, void *bufptr, int cnt, ulong addr, physaddr_t paddr)
{
return WRITE_ERROR;
}
uint
xendump_page_size(void)
{
if (!XENDUMP_VALID())
return 0;
return xd->page_size;
}
/*
* xendump_free_memory(), and xendump_memory_used()
* are debug only, and typically unnecessary to implement.
*/
int
xendump_free_memory(void)
{
return 0;
}
int
xendump_memory_used(void)
{
return 0;
}
/*
* This function is dump-type independent, used here to
* to dump the xendump_data structure contents.
*/
int
xendump_memory_dump(FILE *fp)
{
int i, linefeed, used, others;
ulong *ulongptr;
Elf32_Off offset32;
Elf64_Off offset64;
FILE *fpsave;
fprintf(fp, " flags: %lx (", xd->flags);
others = 0;
if (xd->flags & XENDUMP_LOCAL)
fprintf(fp, "%sXENDUMP_LOCAL", others++ ? "|" : "");
if (xd->flags & XC_SAVE)
fprintf(fp, "%sXC_SAVE", others++ ? "|" : "");
if (xd->flags & XC_CORE_ORIG)
fprintf(fp, "%sXC_CORE_ORIG", others++ ? "|" : "");
if (xd->flags & XC_CORE_ELF)
fprintf(fp, "%sXC_CORE_ELF", others++ ? "|" : "");
if (xd->flags & XC_CORE_P2M_CREATE)
fprintf(fp, "%sXC_CORE_P2M_CREATE", others++ ? "|" : "");
if (xd->flags & XC_CORE_PFN_CREATE)
fprintf(fp, "%sXC_CORE_PFN_CREATE", others++ ? "|" : "");
if (xd->flags & XC_CORE_NO_P2M)
fprintf(fp, "%sXC_CORE_NO_P2M", others++ ? "|" : "");
if (xd->flags & XC_SAVE_IA64)
fprintf(fp, "%sXC_SAVE_IA64", others++ ? "|" : "");
if (xd->flags & XC_CORE_64BIT_HOST)
fprintf(fp, "%sXC_CORE_64BIT_HOST", others++ ? "|" : "");
fprintf(fp, ")\n");
fprintf(fp, " xfd: %d\n", xd->xfd);
fprintf(fp, " page_size: %d\n", xd->page_size);
fprintf(fp, " ofp: %lx\n", (ulong)xd->ofp);
fprintf(fp, " page: %lx\n", (ulong)xd->page);
fprintf(fp, " panic_pc: %lx\n", xd->panic_pc);
fprintf(fp, " panic_sp: %lx\n", xd->panic_sp);
fprintf(fp, " accesses: %ld\n", (ulong)xd->accesses);
fprintf(fp, " cache_hits: %ld ", (ulong)xd->cache_hits);
if (xd->accesses)
fprintf(fp, "(%ld%%)\n", xd->cache_hits * 100 / xd->accesses);
else
fprintf(fp, "\n");
fprintf(fp, " last_pfn: %ld\n", xd->last_pfn);
fprintf(fp, " redundant: %ld ", (ulong)xd->redundant);
if (xd->accesses)
fprintf(fp, "(%ld%%)\n", xd->redundant * 100 / xd->accesses);
else
fprintf(fp, "\n");
for (i = used = 0; i < PFN_TO_OFFSET_CACHE_ENTRIES; i++)
if (xd->poc && xd->poc[i].cnt)
used++;
if (xd->poc)
fprintf(fp, " poc[%d]: %lx %s", PFN_TO_OFFSET_CACHE_ENTRIES,
(ulong)xd->poc, xd->poc ? "" : "(none)");
else
fprintf(fp, " poc[0]: (unused)\n");
for (i = 0; i < PFN_TO_OFFSET_CACHE_ENTRIES; i++) {
if (!xd->poc)
break;
if (!xd->poc[i].cnt) {
if (!i)
fprintf(fp, "(none used)\n");
break;
} else if (!i)
fprintf(fp, "(%d used)\n", used);
if (CRASHDEBUG(2))
fprintf(fp,
" [%d]: pfn: %ld (0x%lx) count: %ld file_offset: %llx\n",
i,
xd->poc[i].pfn,
xd->poc[i].pfn,
xd->poc[i].cnt,
(ulonglong)xd->poc[i].file_offset);
}
if (!xd->poc)
fprintf(fp, "\n");
fprintf(fp, "\n xc_save:\n");
fprintf(fp, " nr_pfns: %ld (0x%lx)\n",
xd->xc_save.nr_pfns, xd->xc_save.nr_pfns);
fprintf(fp, " vmconfig_size: %d (0x%x)\n", xd->xc_save.vmconfig_size,
xd->xc_save.vmconfig_size);
fprintf(fp, " vmconfig_buf: %lx\n", (ulong)xd->xc_save.vmconfig_buf);
if (xd->flags & XC_SAVE)
xen_dump_vmconfig(fp);
fprintf(fp, " p2m_frame_list: %lx ", (ulong)xd->xc_save.p2m_frame_list);
if ((xd->flags & XC_SAVE) && xd->xc_save.p2m_frame_list) {
fprintf(fp, "\n");
ulongptr = xd->xc_save.p2m_frame_list;
for (i = 0; i < P2M_FL_ENTRIES; i++, ulongptr++)
fprintf(fp, "%ld ", *ulongptr);
fprintf(fp, "\n");
} else
fprintf(fp, "(none)\n");
fprintf(fp, " pfns_not: %d\n", xd->xc_save.pfns_not);
fprintf(fp, " pfns_not_offset: %lld\n",
(ulonglong)xd->xc_save.pfns_not_offset);