forked from crash-utility/crash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskdump.c
3132 lines (2766 loc) · 89.7 KB
/
diskdump.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
/*
* diskdump.c
*
* The diskdump module optionally creates either ELF vmcore
* dumpfiles, or compressed dumpfiles derived from the LKCD format.
* In the case of ELF vmcore files, since they are identical to
* netdump dumpfiles, the facilities in netdump.c are used. For
* compressed dumpfiles, the facilities in this file are used.
*
* Copyright (C) 2004-2015 David Anderson
* Copyright (C) 2004-2015 Red Hat, Inc. All rights reserved.
* Copyright (C) 2005 FUJITSU LIMITED
* Copyright (C) 2005 NEC Corporation
*
* 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 "diskdump.h"
#include "xen_dom0.h"
#include "vmcore.h"
#include "maple_tree.h"
#define BITMAP_SECT_LEN 4096
struct diskdump_data {
char *filename;
ulong flags; /* DISKDUMP_LOCAL, plus anything else... */
int dfd; /* dumpfile file descriptor */
FILE *ofp; /* fprintf(dd->ofp, "xxx"); */
int machine_type; /* machine type identifier */
/* header */
struct disk_dump_header *header;
struct disk_dump_sub_header *sub_header;
struct kdump_sub_header *sub_header_kdump;
unsigned long long max_mapnr; /* 64bit max_mapnr */
size_t data_offset;
int block_size;
int block_shift;
char *bitmap;
off_t bitmap_len;
char *dumpable_bitmap;
int byte, bit;
char *compressed_page; /* copy of compressed page data */
char *curbufptr; /* ptr to uncompressed page buffer */
unsigned char *notes_buf; /* copy of elf notes */
void **nt_prstatus_percpu;
uint num_prstatus_notes;
void **nt_qemu_percpu;
void **nt_qemucs_percpu;
uint num_qemu_notes;
void **nt_vmcoredd_array;
uint num_vmcoredd_notes;
/* page cache */
struct page_cache_hdr { /* header for each cached page */
uint32_t pg_flags;
uint64_t pg_addr;
char *pg_bufptr;
ulong pg_hit_count;
} page_cache_hdr[DISKDUMP_CACHED_PAGES];
char *page_cache_buf; /* base of cached buffer pages */
int evict_index; /* next page to evict */
ulong evictions; /* total evictions done */
ulong cached_reads;
ulong *valid_pages;
int max_sect_len; /* highest bucket of valid_pages */
ulong accesses;
ulong snapshot_task;
};
static struct diskdump_data diskdump_data = { 0 };
static struct diskdump_data *dd = &diskdump_data;
ulong *diskdump_flags = &diskdump_data.flags;
static int __diskdump_memory_dump(FILE *);
static void dump_vmcoreinfo(FILE *);
static void dump_note_offsets(FILE *);
static char *vmcoreinfo_read_string(const char *);
static void diskdump_get_osrelease(void);
static int valid_note_address(unsigned char *);
/* For split dumpfile */
static struct diskdump_data **dd_list = NULL;
static int num_dd = 0;
static int num_dumpfiles = 0;
int dumpfile_is_split(void)
{
return KDUMP_SPLIT();
}
int have_crash_notes(int cpu)
{
ulong crash_notes, notes_ptr;
char *buf, *p;
Elf64_Nhdr *note = NULL;
if (!readmem(symbol_value("crash_notes"), KVADDR, &crash_notes,
sizeof(crash_notes), "crash_notes", RETURN_ON_ERROR)) {
error(WARNING, "cannot read \"crash_notes\"\n");
return FALSE;
}
if ((kt->flags & SMP) && (kt->flags & PER_CPU_OFF))
notes_ptr = crash_notes + kt->__per_cpu_offset[cpu];
else
notes_ptr = crash_notes;
buf = GETBUF(SIZE(note_buf));
if (!readmem(notes_ptr, KVADDR, buf,
SIZE(note_buf), "note_buf_t", RETURN_ON_ERROR)) {
error(WARNING, "cpu %d: cannot read NT_PRSTATUS note\n", cpu);
return FALSE;
}
note = (Elf64_Nhdr *)buf;
p = buf + sizeof(Elf64_Nhdr);
if (note->n_type != NT_PRSTATUS) {
error(WARNING, "cpu %d: invalid NT_PRSTATUS note (n_type != NT_PRSTATUS)\n", cpu);
return FALSE;
}
if (!STRNEQ(p, "CORE")) {
error(WARNING, "cpu %d: invalid NT_PRSTATUS note (name != \"CORE\")\n", cpu);
return FALSE;
}
return TRUE;
}
int
diskdump_is_cpu_prstatus_valid(int cpu)
{
static int crash_notes_exists = -1;
if (crash_notes_exists == -1)
crash_notes_exists = kernel_symbol_exists("crash_notes");
return (!crash_notes_exists || have_crash_notes(cpu));
}
void
map_cpus_to_prstatus_kdump_cmprs(void)
{
void **nt_ptr;
int online, i, j, nrcpus;
size_t size;
if (pc->flags2 & QEMU_MEM_DUMP_COMPRESSED) /* notes exist for all cpus */
goto resize_note_pointers;
if (!(online = get_cpus_online()) || (online == kt->cpus))
goto resize_note_pointers;
if (CRASHDEBUG(1))
error(INFO,
"cpus: %d online: %d NT_PRSTATUS notes: %d (remapping)\n",
kt->cpus, online, dd->num_prstatus_notes);
size = NR_CPUS * sizeof(void *);
nt_ptr = (void **)GETBUF(size);
BCOPY(dd->nt_prstatus_percpu, nt_ptr, size);
BZERO(dd->nt_prstatus_percpu, size);
/*
* Re-populate the array with the notes mapping to online cpus
*/
nrcpus = (kt->kernel_NR_CPUS ? kt->kernel_NR_CPUS : NR_CPUS);
for (i = 0, j = 0; i < nrcpus; i++) {
if (in_cpu_map(ONLINE_MAP, i) && machdep->is_cpu_prstatus_valid(i)) {
dd->nt_prstatus_percpu[i] = nt_ptr[j++];
dd->num_prstatus_notes =
MAX(dd->num_prstatus_notes, i+1);
}
}
FREEBUF(nt_ptr);
resize_note_pointers:
/*
* For architectures that only utilize the note pointers
* within this file, resize the arrays accordingly.
*/
if (machine_type("X86_64") || machine_type("X86") ||
machine_type("ARM64")) {
if ((dd->nt_prstatus_percpu = realloc(dd->nt_prstatus_percpu,
dd->num_prstatus_notes * sizeof(void *))) == NULL)
error(FATAL,
"compressed kdump: cannot realloc NT_PRSTATUS note pointers\n");
if (dd->num_qemu_notes) {
if ((dd->nt_qemu_percpu = realloc(dd->nt_qemu_percpu,
dd->num_qemu_notes * sizeof(void *))) == NULL)
error(FATAL,
"compressed kdump: cannot realloc QEMU note pointers\n");
if ((dd->nt_qemucs_percpu = realloc(dd->nt_qemucs_percpu,
dd->num_qemu_notes * sizeof(void *))) == NULL)
error(FATAL,
"compressed kdump: cannot realloc QEMU note pointers\n");
} else {
free(dd->nt_qemu_percpu);
free(dd->nt_qemucs_percpu);
}
}
}
static void
add_diskdump_data(char* name)
{
#define DDL_SIZE 16
int i;
int sz = sizeof(void *);
struct diskdump_data *ddp;
if (dd_list == NULL) {
dd_list = calloc(DDL_SIZE, sz);
num_dd = DDL_SIZE;
} else {
for (i = 0; i < num_dumpfiles; i++) {
ddp = dd_list[i];
if (same_file(ddp->filename, name))
error(FATAL,
"split dumpfiles are identical:\n"
" %s\n %s\n",
ddp->filename, name);
if (memcmp(ddp->header, dd->header,
sizeof(struct disk_dump_header)))
error(FATAL,
"split dumpfiles derived from different vmcores:\n"
" %s\n %s\n",
ddp->filename, name);
}
}
if (num_dumpfiles == num_dd) {
/* expand list */
struct diskdump_data **tmp;
tmp = calloc(num_dd*2, sz);
memcpy(tmp, dd_list, sz*num_dd);
free(dd_list);
dd_list = tmp;
num_dd *= 2;
}
dd_list[num_dumpfiles] = dd;
dd->flags |= DUMPFILE_SPLIT;
dd->filename = name;
if (CRASHDEBUG(1))
fprintf(fp, "%s: start_pfn=%llu, end_pfn=%llu\n", name,
dd->sub_header_kdump->start_pfn_64,
dd->sub_header_kdump->end_pfn_64);
}
static void
clean_diskdump_data(void)
{
int i;
if (dd_list == NULL)
return;
for (i=1; i<num_dumpfiles; i++)
free(dd_list[i]); /* NOTE: dd_list[0] is static dd */
free(dd_list);
dd_list = NULL;
num_dumpfiles = 0;
dd = &diskdump_data;
}
static inline int
get_bit(char *map, unsigned long byte, int bit)
{
return map[byte] & (1<<bit);
}
static inline int
page_is_ram(unsigned long nr)
{
return get_bit(dd->bitmap, nr >> 3, nr & 7);
}
static inline int
page_is_dumpable(unsigned long nr)
{
return dd->dumpable_bitmap[nr>>3] & (1 << (nr & 7));
}
static inline int
dump_is_partial(const struct disk_dump_header *header)
{
return header->bitmap_blocks >=
divideup(divideup(dd->max_mapnr, 8), dd->block_size) * 2;
}
static int
open_dump_file(char *file)
{
int fd;
fd = open(file, O_RDONLY);
if (fd < 0) {
error(INFO, "diskdump / compressed kdump: unable to open dump file %s\n", file);
return FALSE;
}
if (KDUMP_SPLIT())
dd = calloc(1, sizeof(*dd));
dd->dfd = fd;
return TRUE;
}
void
process_elf32_notes(void *note_buf, unsigned long size_note)
{
Elf32_Nhdr *nt;
size_t index, len = 0;
int num = 0;
int vmcoredd_num = 0;
int qemu_num = 0;
for (index = 0; index < size_note; index += len) {
nt = note_buf + index;
if (nt->n_type == NT_PRSTATUS) {
dd->nt_prstatus_percpu[num] = nt;
num++;
}
len = sizeof(Elf32_Nhdr);
if (STRNEQ((char *)nt + len, "QEMU")) {
ulong *ptr =
(ulong *)((char *)nt + sizeof(Elf32_Nhdr) + nt->n_namesz);
dd->nt_qemucs_percpu[qemu_num] =
(ulong *)roundup((ulong) ptr, 4);
dd->nt_qemu_percpu[qemu_num] = nt;
qemu_num++;
}
if (nt->n_type == NT_XEN_KDUMP_CR3 ||
nt->n_type == XEN_ELFNOTE_CRASH_INFO) {
void *data = (char*)(nt + 1) +
roundup(nt->n_namesz, 4);
process_xen_note(nt->n_type, data, nt->n_descsz);
}
if (nt->n_type == NT_VMCOREDD &&
vmcoredd_num < NR_DEVICE_DUMPS) {
dd->nt_vmcoredd_array[vmcoredd_num] = nt;
vmcoredd_num++;
}
len = roundup(len + nt->n_namesz, 4);
len = roundup(len + nt->n_descsz, 4);
}
if (num > 0) {
pc->flags2 |= ELF_NOTES;
dd->num_prstatus_notes = num;
}
if (qemu_num > 0) {
pc->flags2 |= QEMU_MEM_DUMP_COMPRESSED;
dd->num_qemu_notes = qemu_num;
}
if (vmcoredd_num > 0)
dd->num_vmcoredd_notes = vmcoredd_num;
return;
}
void
process_elf64_notes(void *note_buf, unsigned long size_note)
{
Elf64_Nhdr *nt;
size_t index, len = 0;
int num = 0;
int vmcoredd_num = 0;
int qemu_num = 0;
for (index = 0; index < size_note; index += len) {
nt = note_buf + index;
if (nt->n_type == NT_PRSTATUS) {
dd->nt_prstatus_percpu[num] = nt;
num++;
}
if ((nt->n_type == NT_TASKSTRUCT) &&
(STRNEQ((char *)nt + sizeof(Elf64_Nhdr), "SNAP"))) {
pc->flags2 |= (LIVE_DUMP|SNAP);
dd->snapshot_task =
*((ulong *)((char *)nt + sizeof(Elf64_Nhdr) + nt->n_namesz));
}
len = sizeof(Elf64_Nhdr);
if (STRNEQ((char *)nt + len, "QEMU")) {
ulong *ptr =
(ulong *)((char *)nt + sizeof(Elf64_Nhdr) + nt->n_namesz);
dd->nt_qemucs_percpu[qemu_num] =
(ulong *)roundup((ulong) ptr, 4);
dd->nt_qemu_percpu[qemu_num] = nt;
qemu_num++;
}
if (nt->n_type == NT_XEN_KDUMP_CR3 ||
nt->n_type == XEN_ELFNOTE_CRASH_INFO) {
void *data = (char*)(nt + 1) +
roundup(nt->n_namesz, 4);
process_xen_note(nt->n_type, data, nt->n_descsz);
}
if (nt->n_type == NT_VMCOREDD &&
vmcoredd_num < NR_DEVICE_DUMPS) {
dd->nt_vmcoredd_array[vmcoredd_num] = nt;
vmcoredd_num++;
}
len = roundup(len + nt->n_namesz, 4);
len = roundup(len + nt->n_descsz, 4);
}
if (num > 0) {
pc->flags2 |= ELF_NOTES;
dd->num_prstatus_notes = num;
}
if (qemu_num > 0) {
pc->flags2 |= QEMU_MEM_DUMP_COMPRESSED;
dd->num_qemu_notes = qemu_num;
}
if (vmcoredd_num > 0)
dd->num_vmcoredd_notes = vmcoredd_num;
return;
}
void
x86_process_elf_notes(void *note_ptr, unsigned long size_note)
{
if (machine_type("X86_64"))
process_elf64_notes(note_ptr, size_note);
else if (machine_type("X86"))
process_elf32_notes(note_ptr, size_note);
}
#if defined(__i386__) && (defined(ARM) || defined(MIPS))
/*
* The kdump_sub_header member offsets are different when the crash
* binary is built natively on an ARM host vs. when built with
* "make target=ARM" on an x86/x86_64 host. This is because the
* off_t structure members will be aligned on an 8-byte boundary when
* compiled as an ARM binary -- which will be reflected in the
* kdump_sub_header in a compressed ARM kdump.
*
* When crash is compiled as an x86 binary, these are the
* structure's offsets:
*
* struct kdump_sub_header {
* [0] unsigned long phys_base;
* [4] int dump_level; / header_version 1 and later /
* [8] int split; / header_version 2 and later /
* [12] unsigned long start_pfn; / header_version 2 and later /
* [16] unsigned long end_pfn; / header_version 2 and later /
* [20] off_t offset_vmcoreinfo; / header_version 3 and later /
* [28] unsigned long size_vmcoreinfo; / header_version 3 and later /
* [32] off_t offset_note; / header_version 4 and later /
* [40] unsigned long size_note; / header_version 4 and later /
* [44] off_t offset_eraseinfo; / header_version 5 and later /
* [52] unsigned long size_eraseinfo; / header_version 5 and later /
* [56] unsigned long long start_pfn_64; / header_version 6 and later /
* [64] unsigned long long end_pfn_64; / header_version 6 and later /
* [72] unsigned long long max_mapnr_64; / header_version 6 and later /
* };
*
* But when compiled on an ARM processor, each 64-bit "off_t" would be pushed
* up to an 8-byte boundary:
*
* struct kdump_sub_header {
* [0] unsigned long phys_base;
* [4] int dump_level; / header_version 1 and later /
* [8] int split; / header_version 2 and later /
* [12] unsigned long start_pfn; / header_version 2 and later /
* [16] unsigned long end_pfn; / header_version 2 and later /
* [24] off_t offset_vmcoreinfo; / header_version 3 and later /
* [32] unsigned long size_vmcoreinfo; / header_version 3 and later /
* [40] off_t offset_note; / header_version 4 and later /
* [48] unsigned long size_note; / header_version 4 and later /
* [56] off_t offset_eraseinfo; / header_version 5 and later /
* [64] unsigned long size_eraseinfo; / header_version 5 and later /
* [72] unsigned long long start_pfn_64; / header_version 6 and later /
* [80] unsigned long long end_pfn_64; / header_version 6 and later /
* [88] unsigned long long max_mapnr_64; / header_version 6 and later /
* };
*
*/
struct kdump_sub_header_ARM_target {
unsigned long phys_base;
int dump_level; /* header_version 1 and later */
int split; /* header_version 2 and later */
unsigned long start_pfn; /* header_version 2 and later */
unsigned long end_pfn; /* header_version 2 and later */
int pad1;
off_t offset_vmcoreinfo; /* header_version 3 and later */
unsigned long size_vmcoreinfo; /* header_version 3 and later */
int pad2;
off_t offset_note; /* header_version 4 and later */
unsigned long size_note; /* header_version 4 and later */
int pad3;
off_t offset_eraseinfo; /* header_version 5 and later */
unsigned long size_eraseinfo; /* header_version 5 and later */
int pad4;
unsigned long long start_pfn_64; /* header_version 6 and later */
unsigned long long end_pfn_64; /* header_version 6 and later */
unsigned long long max_mapnr_64; /* header_version 6 and later */
};
static void
arm_kdump_header_adjust(int header_version)
{
struct kdump_sub_header *kdsh;
struct kdump_sub_header_ARM_target *kdsh_ARM_target;
kdsh = dd->sub_header_kdump;
kdsh_ARM_target = (struct kdump_sub_header_ARM_target *)kdsh;
if (header_version >= 3) {
kdsh->offset_vmcoreinfo = kdsh_ARM_target->offset_vmcoreinfo;
kdsh->size_vmcoreinfo = kdsh_ARM_target->size_vmcoreinfo;
}
if (header_version >= 4) {
kdsh->offset_note = kdsh_ARM_target->offset_note;
kdsh->size_note = kdsh_ARM_target->size_note;
}
if (header_version >= 5) {
kdsh->offset_eraseinfo = kdsh_ARM_target->offset_eraseinfo;
kdsh->size_eraseinfo = kdsh_ARM_target->size_eraseinfo;
}
if (header_version >= 6) {
kdsh->start_pfn_64 = kdsh_ARM_target->start_pfn_64;
kdsh->end_pfn_64 = kdsh_ARM_target->end_pfn_64;
kdsh->max_mapnr_64 = kdsh_ARM_target->max_mapnr_64;
} else {
kdsh->start_pfn_64 = kdsh_ARM_target->start_pfn;
kdsh->end_pfn_64 = kdsh_ARM_target->end_pfn;
kdsh->max_mapnr_64 = dd->max_mapnr;
}
}
#endif /* __i386__ && (ARM || MIPS) */
/*
* Read page descriptor.
*/
static int
read_pd(int fd, off_t offset, page_desc_t *pd)
{
int ret;
if (FLAT_FORMAT()) {
if (!read_flattened_format(fd, offset, pd, sizeof(*pd)))
return READ_ERROR;
} else {
if (offset < 0) {
if (CRASHDEBUG(8))
fprintf(fp, "read_pd: invalid offset: %lx\n", offset);
return SEEK_ERROR;
}
if ((ret = pread(fd, pd, sizeof(*pd), offset)) != sizeof(*pd)) {
if (ret == -1 && CRASHDEBUG(8))
fprintf(fp, "read_pd: pread error: %s\n", strerror(errno));
return READ_ERROR;
}
}
return 0;
}
static int
read_dump_header(char *file)
{
struct disk_dump_header *header = NULL;
struct disk_dump_sub_header *sub_header = NULL;
struct kdump_sub_header *sub_header_kdump = NULL;
size_t size;
off_t bitmap_len;
int block_size = (int)sysconf(_SC_PAGESIZE);
off_t offset;
const off_t failed = (off_t)-1;
ulong pfn;
int i, j, max_sect_len;
int is_split = 0;
ulonglong tmp, *bitmap;
if (block_size < 0)
return FALSE;
restart:
if ((header = realloc(header, block_size)) == NULL)
error(FATAL, "diskdump / compressed kdump: cannot malloc block_size buffer\n");
if (FLAT_FORMAT()) {
if (!read_flattened_format(dd->dfd, 0, header, block_size)) {
error(FATAL, "diskdump / compressed kdump: cannot read header\n");
goto err;
}
} else {
if (lseek(dd->dfd, 0, SEEK_SET) == failed) {
if (CRASHDEBUG(1))
error(INFO, "diskdump / compressed kdump: cannot lseek dump header\n");
goto err;
}
if (read(dd->dfd, header, block_size) < block_size) {
if (CRASHDEBUG(1))
error(INFO, "diskdump / compressed kdump: cannot read dump header\n");
goto err;
}
}
/* validate dump header */
if (!memcmp(header->signature, DISK_DUMP_SIGNATURE,
sizeof(header->signature))) {
dd->flags |= DISKDUMP_LOCAL;
} else if (!memcmp(header->signature, KDUMP_SIGNATURE,
sizeof(header->signature))) {
dd->flags |= KDUMP_CMPRS_LOCAL;
if (header->header_version >= 1)
dd->flags |= ERROR_EXCLUDED;
} else {
if (CRASHDEBUG(1))
error(INFO,
"diskdump / compressed kdump: dump does not have panic dump header\n");
goto err;
}
if (CRASHDEBUG(1))
fprintf(fp, "%s: header->utsname.machine: %s\n",
DISKDUMP_VALID() ? "diskdump" : "compressed kdump",
header->utsname.machine);
if (STRNEQ(header->utsname.machine, "i686") &&
machine_type_mismatch(file, "X86", NULL, 0))
goto err;
else if (STRNEQ(header->utsname.machine, "x86_64") &&
machine_type_mismatch(file, "X86_64", NULL, 0))
goto err;
else if (STRNEQ(header->utsname.machine, "ia64") &&
machine_type_mismatch(file, "IA64", NULL, 0))
goto err;
else if (STREQ(header->utsname.machine, "ppc") &&
machine_type_mismatch(file, "PPC", NULL, 0))
goto err;
else if (STRNEQ(header->utsname.machine, "ppc64") &&
machine_type_mismatch(file, "PPC64", NULL, 0))
goto err;
else if (STRNEQ(header->utsname.machine, "arm") &&
machine_type_mismatch(file, "ARM", NULL, 0))
goto err;
else if (STREQ(header->utsname.machine, "mips") &&
machine_type_mismatch(file, "MIPS", NULL, 0))
goto err;
else if (STRNEQ(header->utsname.machine, "mips64") &&
machine_type_mismatch(file, "MIPS64", NULL, 0))
goto err;
else if (STRNEQ(header->utsname.machine, "s390x") &&
machine_type_mismatch(file, "S390X", NULL, 0))
goto err;
else if (STRNEQ(header->utsname.machine, "aarch64") &&
machine_type_mismatch(file, "ARM64", NULL, 0))
goto err;
else if (STRNEQ(header->utsname.machine, "riscv64") &&
machine_type_mismatch(file, "RISCV64", NULL, 0))
goto err;
else if (STRNEQ(header->utsname.machine, "loongarch64") &&
machine_type_mismatch(file, "LOONGARCH64", NULL, 0))
goto err;
if (header->block_size != block_size) {
block_size = header->block_size;
if (CRASHDEBUG(1))
fprintf(fp,
"retrying with different block/page size: %d\n",
header->block_size);
goto restart;
}
dd->block_size = header->block_size;
dd->block_shift = ffs(header->block_size) - 1;
if ((DISKDUMP_VALID() &&
(sizeof(*header) + sizeof(void *) * header->nr_cpus > block_size)) ||
header->nr_cpus <= 0) {
error(WARNING, "%s: invalid nr_cpus value: %d\n",
DISKDUMP_VALID() ? "diskdump" : "compressed kdump",
header->nr_cpus);
if (!machine_type("S390") && !machine_type("S390X") &&
!machine_type("X86") && !machine_type("X86_64")) {
if (DISKDUMP_VALID())
goto err;
}
}
/* read sub header */
offset = (off_t)block_size;
if (DISKDUMP_VALID()) {
if ((sub_header = malloc(block_size)) == NULL)
error(FATAL, "diskdump: cannot malloc sub_header buffer\n");
if (FLAT_FORMAT()) {
if (!read_flattened_format(dd->dfd, offset, sub_header, block_size)) {
error(INFO, "diskdump: cannot read dump sub header\n");
goto err;
}
} else {
if (lseek(dd->dfd, offset, SEEK_SET) == failed) {
error(INFO, "diskdump: cannot lseek dump sub header\n");
goto err;
}
if (read(dd->dfd, sub_header, block_size) < block_size) {
error(INFO, "diskdump: cannot read dump sub header\n");
goto err;
}
}
dd->sub_header = sub_header;
/* the 64bit max_mapnr only exists in sub-header of compressed
* kdump file, if it's not a compressed kdump file, we have to
* use the old 32bit max_mapnr in dumpfile header.
* max_mapnr may be truncated here.
*/
dd->max_mapnr = header->max_mapnr;
} else if (KDUMP_CMPRS_VALID()) {
if ((sub_header_kdump = malloc(block_size)) == NULL)
error(FATAL, "compressed kdump: cannot malloc sub_header_kdump buffer\n");
if (FLAT_FORMAT()) {
if (!read_flattened_format(dd->dfd, offset, sub_header_kdump, block_size)) {
error(INFO, "compressed kdump: cannot read dump sub header\n");
goto err;
}
} else {
if (lseek(dd->dfd, offset, SEEK_SET) == failed) {
error(INFO, "compressed kdump: cannot lseek dump sub header\n");
goto err;
}
if (read(dd->dfd, sub_header_kdump, block_size) < block_size) {
error(INFO, "compressed kdump: cannot read dump sub header\n");
goto err;
}
}
dd->sub_header_kdump = sub_header_kdump;
#if defined(__i386__) && (defined(ARM) || defined(MIPS))
arm_kdump_header_adjust(header->header_version);
#endif
/* use 64bit max_mapnr in compressed kdump file sub-header */
if (header->header_version >= 6)
dd->max_mapnr = dd->sub_header_kdump->max_mapnr_64;
else {
dd->sub_header_kdump->start_pfn_64
= dd->sub_header_kdump->start_pfn;
dd->sub_header_kdump->end_pfn_64
= dd->sub_header_kdump->end_pfn;
}
}
if (header->header_version < 6)
dd->max_mapnr = header->max_mapnr;
/* read memory bitmap */
bitmap_len = (off_t)block_size * header->bitmap_blocks;
dd->bitmap_len = bitmap_len;
offset = (off_t)block_size * (1 + header->sub_hdr_size);
dd->dumpable_bitmap = calloc(bitmap_len, 1);
if (CRASHDEBUG(8))
fprintf(fp, "%s: memory bitmap offset: %llx\n",
DISKDUMP_VALID() ? "diskdump" : "compressed kdump",
(ulonglong)offset);
if (FLAT_FORMAT()) {
if ((dd->bitmap = malloc(bitmap_len)) == NULL)
error(FATAL, "%s: cannot malloc bitmap buffer\n",
DISKDUMP_VALID() ? "diskdump" : "compressed kdump");
if (!read_flattened_format(dd->dfd, offset, dd->bitmap, bitmap_len)) {
error(INFO, "%s: cannot read memory bitmap\n",
DISKDUMP_VALID() ? "diskdump" : "compressed kdump");
goto err;
}
} else {
dd->bitmap = mmap(NULL, bitmap_len, PROT_READ,
MAP_SHARED, dd->dfd, offset);
if (dd->bitmap == MAP_FAILED)
error(FATAL, "%s: cannot mmap bitmap buffer\n",
DISKDUMP_VALID() ? "diskdump" : "compressed kdump");
madvise(dd->bitmap, bitmap_len, MADV_WILLNEED);
}
if (dump_is_partial(header))
memcpy(dd->dumpable_bitmap, dd->bitmap + bitmap_len/2,
bitmap_len/2);
else
memcpy(dd->dumpable_bitmap, dd->bitmap, bitmap_len);
dd->data_offset
= (1UL + header->sub_hdr_size + header->bitmap_blocks)
* header->block_size;
dd->header = header;
if (machine_type("ARM"))
dd->machine_type = EM_ARM;
else if (machine_type("MIPS") || machine_type("MIPS64"))
dd->machine_type = EM_MIPS;
else if (machine_type("X86"))
dd->machine_type = EM_386;
else if (machine_type("X86_64"))
dd->machine_type = EM_X86_64;
else if (machine_type("IA64"))
dd->machine_type = EM_IA_64;
else if (machine_type("PPC"))
dd->machine_type = EM_PPC;
else if (machine_type("PPC64"))
dd->machine_type = EM_PPC64;
else if (machine_type("S390X"))
dd->machine_type = EM_S390;
else if (machine_type("ARM64"))
dd->machine_type = EM_AARCH64;
else if (machine_type("SPARC64"))
dd->machine_type = EM_SPARCV9;
else if (machine_type("RISCV64"))
dd->machine_type = EM_RISCV;
else if (machine_type("LOONGARCH64"))
dd->machine_type = EM_LOONGARCH;
else {
error(INFO, "%s: unsupported machine type: %s\n",
DISKDUMP_VALID() ? "diskdump" : "compressed kdump",
MACHINE_TYPE);
goto err;
}
/* process elf notes data */
if (KDUMP_CMPRS_VALID() && !(dd->flags & NO_ELF_NOTES) &&
(dd->header->header_version >= 4) &&
(sub_header_kdump->offset_note) &&
(sub_header_kdump->size_note) && (machdep->process_elf_notes)) {
size = sub_header_kdump->size_note;
offset = sub_header_kdump->offset_note;
if ((dd->notes_buf = malloc(size)) == NULL)
error(FATAL, "compressed kdump: cannot malloc notes"
" buffer\n");
if ((dd->nt_prstatus_percpu = malloc(NR_CPUS * sizeof(void *))) == NULL)
error(FATAL, "compressed kdump: cannot malloc pointer"
" to NT_PRSTATUS notes\n");
if ((dd->nt_qemu_percpu = malloc(NR_CPUS * sizeof(void *))) == NULL)
error(FATAL, "qemu mem dump compressed: cannot malloc pointer"
" to QEMU notes\n");
if ((dd->nt_qemucs_percpu = malloc(NR_CPUS * sizeof(void *))) == NULL)
error(FATAL, "qemu mem dump compressed: cannot malloc pointer"
" to QEMUCS notes\n");
if ((dd->nt_vmcoredd_array = malloc(NR_DEVICE_DUMPS * sizeof(void *))) == NULL)
error(FATAL, "compressed kdump: cannot malloc array for "
"vmcore device dump notes\n");
if (FLAT_FORMAT()) {
if (!read_flattened_format(dd->dfd, offset, dd->notes_buf, size)) {
error(INFO, "compressed kdump: cannot read notes data"
"\n");
goto err;
}
} else {
if (lseek(dd->dfd, offset, SEEK_SET) == failed) {
error(INFO, "compressed kdump: cannot lseek notes data\n");
goto err;
}
if (read(dd->dfd, dd->notes_buf, size) < size) {
error(INFO, "compressed kdump: cannot read notes data"
"\n");
goto err;
}
}
machdep->process_elf_notes(dd->notes_buf, size);
}
/* Check if dump file contains erasesinfo data */
if (KDUMP_CMPRS_VALID() && (dd->header->header_version >= 5) &&
(sub_header_kdump->offset_eraseinfo) &&
(sub_header_kdump->size_eraseinfo))
pc->flags2 |= ERASEINFO_DATA;
if (KDUMP_CMPRS_VALID() && (dd->header->header_version >= 3) &&
dd->sub_header_kdump->offset_vmcoreinfo &&
dd->sub_header_kdump->size_vmcoreinfo)
pc->flags2 |= VMCOREINFO;
if (KDUMP_CMPRS_VALID() &&
(dd->header->status & DUMP_DH_COMPRESSED_INCOMPLETE))
pc->flags2 |= INCOMPLETE_DUMP;
if (KDUMP_CMPRS_VALID() &&
(dd->header->status & DUMP_DH_EXCLUDED_VMEMMAP))
pc->flags2 |= EXCLUDED_VMEMMAP;
/* For split dumpfile */
if (KDUMP_CMPRS_VALID()) {
is_split = ((dd->header->header_version >= 2) &&
(sub_header_kdump->split));
if ((is_split && (num_dumpfiles != 0) && (dd_list == NULL))||
(!is_split && (num_dumpfiles != 0))) {
clean_diskdump_data();
goto err;
}
if (is_split)
add_diskdump_data(file);
num_dumpfiles++;
}
if (!is_split) {
max_sect_len = divideup(dd->max_mapnr, BITMAP_SECT_LEN);
pfn = 0;
dd->filename = file;
}
else {
unsigned long long start = sub_header_kdump->start_pfn_64;
unsigned long long end = sub_header_kdump->end_pfn_64;
max_sect_len = divideup(end - start + 1, BITMAP_SECT_LEN);
pfn = start;
}
dd->valid_pages = calloc(sizeof(ulong), max_sect_len + 1);
dd->max_sect_len = max_sect_len;
/* It is safe to convert it to (ulonglong *). */
bitmap = (ulonglong *)dd->dumpable_bitmap;
for (i = 1; i < max_sect_len + 1; i++) {
dd->valid_pages[i] = dd->valid_pages[i - 1];
for (j = 0; j < BITMAP_SECT_LEN; j += 64, pfn += 64) {
tmp = bitmap[pfn >> 6];
if (tmp)
dd->valid_pages[i] += hweight64(tmp);
}
}
return TRUE;
err:
free(header);
if (sub_header)
free(sub_header);
if (sub_header_kdump)
free(sub_header_kdump);
if (dd->bitmap) {
if (FLAT_FORMAT())
free(dd->bitmap);
else
munmap(dd->bitmap, dd->bitmap_len);
}
if (dd->dumpable_bitmap)
free(dd->dumpable_bitmap);
if (dd->notes_buf)
free(dd->notes_buf);
if (dd->nt_prstatus_percpu)
free(dd->nt_prstatus_percpu);
if (dd->nt_qemu_percpu)
free(dd->nt_qemu_percpu);
if (dd->nt_qemucs_percpu)
free(dd->nt_qemucs_percpu);
if (dd->nt_vmcoredd_array)
free(dd->nt_vmcoredd_array);
dd->flags &= ~(DISKDUMP_LOCAL|KDUMP_CMPRS_LOCAL);
pc->flags2 &= ~ELF_NOTES;
return FALSE;
}