-
Notifications
You must be signed in to change notification settings - Fork 3
/
makedumpfile.c
11978 lines (10398 loc) · 303 KB
/
makedumpfile.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
/*
* makedumpfile.c
*
* Copyright (C) 2006, 2007, 2008, 2009, 2011 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 "makedumpfile.h"
#include "print_info.h"
#include "dwarf_info.h"
#include "elf_info.h"
#include "erase_info.h"
#include "sadump_info.h"
#include "cache.h"
#include <stddef.h>
#include <ctype.h>
#include <sys/time.h>
#include <limits.h>
#include <assert.h>
#include <zlib.h>
struct symbol_table symbol_table;
struct size_table size_table;
struct offset_table offset_table;
struct array_table array_table;
struct number_table number_table;
struct srcfile_table srcfile_table;
struct save_control sc;
struct vm_table vt = { 0 };
struct DumpInfo *info = NULL;
struct SplitBlock *splitblock = NULL;
struct vmap_pfns *gvmem_pfns;
int nr_gvmem_pfns;
extern int find_vmemmap();
char filename_stdout[] = FILENAME_STDOUT;
/* Cache statistics */
static unsigned long long cache_hit;
static unsigned long long cache_miss;
static void first_cycle(mdf_pfn_t start, mdf_pfn_t max, struct cycle *cycle)
{
cycle->start_pfn = round(start, info->pfn_cyclic);
cycle->end_pfn = cycle->start_pfn + info->pfn_cyclic;
if (cycle->end_pfn > max)
cycle->end_pfn = max;
/*
* Mitigate statistics problem in ELF dump mode.
* A cycle must start with a pfn that is divisible by BITPERBYTE.
* See create_bitmap_from_memhole().
*/
if (info->flag_elf_dumpfile && cycle->start_pfn < start)
cycle->start_pfn = round(start, BITPERBYTE);
cycle->exclude_pfn_start = 0;
cycle->exclude_pfn_end = 0;
}
static void update_cycle(mdf_pfn_t max, struct cycle *cycle)
{
cycle->start_pfn= cycle->end_pfn;
cycle->end_pfn= cycle->start_pfn + info->pfn_cyclic;
if (cycle->end_pfn > max)
cycle->end_pfn = max;
}
static int end_cycle(mdf_pfn_t max, struct cycle *cycle)
{
return (cycle->start_pfn >= max)?TRUE:FALSE;
}
#define for_each_cycle(start, max, C) \
for (first_cycle(start, max, C); !end_cycle(max, C); \
update_cycle(max, C))
/*
* The numbers of the excluded pages
*/
mdf_pfn_t pfn_zero;
mdf_pfn_t pfn_memhole;
mdf_pfn_t pfn_cache;
mdf_pfn_t pfn_cache_private;
mdf_pfn_t pfn_private_filter_pages;
mdf_pfn_t pfn_user;
mdf_pfn_t pfn_free;
mdf_pfn_t pfn_hwpoison;
mdf_pfn_t pfn_offline;
mdf_pfn_t pfn_elf_excluded;
mdf_pfn_t num_dumped;
int retcd = FAILED; /* return code */
#define INITIALIZE_LONG_TABLE(table, value) \
do { \
size_member = sizeof(long); \
num_member = sizeof(table) / size_member; \
ptr_long_table = (long *)&table; \
for (i = 0; i < num_member; i++, ptr_long_table++) \
*ptr_long_table = value; \
} while (0)
static void setup_page_is_buddy(void);
void
initialize_tables(void)
{
int i, size_member, num_member;
unsigned long long *ptr_symtable;
long *ptr_long_table;
/*
* Initialize the symbol table.
*/
size_member = sizeof(symbol_table.mem_map);
num_member = sizeof(symbol_table) / size_member;
ptr_symtable = (unsigned long long *)&symbol_table;
for (i = 0; i < num_member; i++, ptr_symtable++)
*ptr_symtable = NOT_FOUND_SYMBOL;
INITIALIZE_LONG_TABLE(size_table, NOT_FOUND_STRUCTURE);
INITIALIZE_LONG_TABLE(offset_table, NOT_FOUND_STRUCTURE);
INITIALIZE_LONG_TABLE(array_table, NOT_FOUND_STRUCTURE);
INITIALIZE_LONG_TABLE(number_table, NOT_FOUND_NUMBER);
}
/*
* Translate a domain-0's physical address to machine address.
*/
unsigned long long
ptom_xen(unsigned long long paddr)
{
unsigned long mfn;
unsigned long long maddr;
mdf_pfn_t pfn;
unsigned long long mfn_idx, frame_idx;
pfn = paddr_to_pfn(paddr);
mfn_idx = pfn / MFNS_PER_FRAME;
frame_idx = pfn % MFNS_PER_FRAME;
if (mfn_idx >= info->p2m_frames) {
ERRMSG("Invalid mfn_idx(%llu).\n", mfn_idx);
return NOT_PADDR;
}
maddr = pfn_to_paddr(info->p2m_mfn_frame_list[mfn_idx])
+ sizeof(unsigned long) * frame_idx;
if (!readmem(PADDR, maddr, &mfn, sizeof(mfn))) {
ERRMSG("Can't get mfn.\n");
return NOT_PADDR;
}
maddr = pfn_to_paddr(mfn);
maddr |= PAGEOFFSET(paddr);
return maddr;
}
/*
* Get the number of the page descriptors from the ELF info.
*/
int
get_max_mapnr(void)
{
unsigned long long max_paddr;
if (info->flag_refiltering) {
if (info->dh_memory->header_version >= 6)
info->max_mapnr = info->kh_memory->max_mapnr_64;
else
info->max_mapnr = info->dh_memory->max_mapnr;
return TRUE;
}
if (info->flag_sadump) {
info->max_mapnr = sadump_get_max_mapnr();
return TRUE;
}
max_paddr = get_max_paddr();
info->max_mapnr = paddr_to_pfn(roundup(max_paddr, PAGESIZE()));
DEBUG_MSG("\n");
DEBUG_MSG("max_mapnr : %llx\n", info->max_mapnr);
return TRUE;
}
/*
* Get the number of the page descriptors for Xen.
*/
int
get_dom0_mapnr()
{
unsigned long max_pfn;
if (SYMBOL(max_pfn) != NOT_FOUND_SYMBOL) {
if (!readmem(VADDR, SYMBOL(max_pfn), &max_pfn, sizeof max_pfn)) {
ERRMSG("Can't read domain-0 max_pfn.\n");
return FALSE;
}
info->dom0_mapnr = max_pfn;
} else if (info->p2m_frames) {
unsigned long mfns[MFNS_PER_FRAME];
unsigned long mfn_idx = info->p2m_frames - 1;
unsigned long long maddr;
unsigned i;
maddr = pfn_to_paddr(info->p2m_mfn_frame_list[mfn_idx]);
if (!readmem(PADDR, maddr, &mfns, sizeof(mfns))) {
ERRMSG("Can't read %ld domain-0 mfns at 0x%llu\n",
(long)MFNS_PER_FRAME, maddr);
return FALSE;
}
for (i = 0; i < MFNS_PER_FRAME; ++i)
if (!mfns[i])
break;
info->dom0_mapnr = mfn_idx * MFNS_PER_FRAME + i;
} else {
/* dom0_mapnr is unavailable, which may be non-critical */
return TRUE;
}
DEBUG_MSG("domain-0 pfn : %llx\n", info->dom0_mapnr);
return TRUE;
}
int
is_in_same_page(unsigned long vaddr1, unsigned long vaddr2)
{
if (round(vaddr1, info->page_size) == round(vaddr2, info->page_size))
return TRUE;
return FALSE;
}
static inline int
isHugetlb(unsigned long dtor)
{
return ((NUMBER(HUGETLB_PAGE_DTOR) != NOT_FOUND_NUMBER)
&& (NUMBER(HUGETLB_PAGE_DTOR) == dtor))
|| ((SYMBOL(free_huge_page) != NOT_FOUND_SYMBOL)
&& (SYMBOL(free_huge_page) == dtor));
}
static int
isOffline(unsigned long flags, unsigned int _mapcount)
{
if (NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE) == NOT_FOUND_NUMBER)
return FALSE;
if (flags & (1UL << NUMBER(PG_slab)))
return FALSE;
if (_mapcount == (int)NUMBER(PAGE_OFFLINE_MAPCOUNT_VALUE))
return TRUE;
return FALSE;
}
static int
is_cache_page(unsigned long flags)
{
if (isLRU(flags))
return TRUE;
/* PG_swapcache is valid only if:
* a. PG_swapbacked bit is set, or
* b. PG_swapbacked did not exist (kernels before 4.10-rc1).
*/
if ((NUMBER(PG_swapbacked) == NOT_FOUND_NUMBER || isSwapBacked(flags))
&& isSwapCache(flags))
return TRUE;
return FALSE;
}
static int
is_filtered_page(unsigned long filter, unsigned long flags, unsigned long private)
{
if (isPrivate(flags) && private == filter)
return TRUE;
return FALSE;
}
static inline unsigned long
calculate_len_buf_out(long page_size)
{
unsigned long len_buf_out_zlib, len_buf_out_lzo, len_buf_out_snappy;
unsigned long len_buf_out;
len_buf_out_zlib = len_buf_out_lzo = len_buf_out_snappy = 0;
#ifdef USELZO
len_buf_out_lzo = page_size + page_size / 16 + 64 + 3;
#endif
#ifdef USESNAPPY
len_buf_out_snappy = snappy_max_compressed_length(page_size);
#endif
len_buf_out_zlib = compressBound(page_size);
len_buf_out = MAX(len_buf_out_zlib,
MAX(len_buf_out_lzo,
len_buf_out_snappy));
return len_buf_out;
}
#define BITMAP_SECT_LEN 4096
static inline int is_dumpable(struct dump_bitmap *, mdf_pfn_t, struct cycle *cycle);
unsigned long
pfn_to_pos(mdf_pfn_t pfn)
{
unsigned long desc_pos;
mdf_pfn_t i;
desc_pos = info->valid_pages[pfn / BITMAP_SECT_LEN];
for (i = round(pfn, BITMAP_SECT_LEN); i < pfn; i++)
if (is_dumpable(info->bitmap_memory, i, NULL))
desc_pos++;
return desc_pos;
}
unsigned long
pfn_to_pos_parallel(mdf_pfn_t pfn, struct dump_bitmap* bitmap_memory_parallel)
{
unsigned long desc_pos;
mdf_pfn_t i;
desc_pos = info->valid_pages[pfn / BITMAP_SECT_LEN];
for (i = round(pfn, BITMAP_SECT_LEN); i < pfn; i++)
if (is_dumpable(bitmap_memory_parallel, i, NULL))
desc_pos++;
return desc_pos;
}
int
read_page_desc(unsigned long long paddr, page_desc_t *pd)
{
struct disk_dump_header *dh;
unsigned long desc_pos;
mdf_pfn_t pfn;
off_t offset;
/*
* Find page descriptor
*/
dh = info->dh_memory;
offset
= (DISKDUMP_HEADER_BLOCKS + dh->sub_hdr_size + dh->bitmap_blocks)
* dh->block_size;
pfn = paddr_to_pfn(paddr);
desc_pos = pfn_to_pos(pfn);
offset += (off_t)desc_pos * sizeof(page_desc_t);
if (lseek(info->fd_memory, offset, SEEK_SET) < 0) {
ERRMSG("Can't seek %s. %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
/*
* Read page descriptor
*/
if (read(info->fd_memory, pd, sizeof(*pd)) != sizeof(*pd)) {
ERRMSG("Can't read %s. %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
/*
* Sanity check
*/
if (pd->size > dh->block_size)
return FALSE;
return TRUE;
}
int
read_page_desc_parallel(int fd_memory, unsigned long long paddr,
page_desc_t *pd,
struct dump_bitmap* bitmap_memory_parallel)
{
struct disk_dump_header *dh;
unsigned long desc_pos;
mdf_pfn_t pfn;
off_t offset;
/*
* Find page descriptor
*/
dh = info->dh_memory;
offset
= (DISKDUMP_HEADER_BLOCKS + dh->sub_hdr_size + dh->bitmap_blocks)
* dh->block_size;
pfn = paddr_to_pfn(paddr);
desc_pos = pfn_to_pos_parallel(pfn, bitmap_memory_parallel);
offset += (off_t)desc_pos * sizeof(page_desc_t);
if (lseek(fd_memory, offset, SEEK_SET) < 0) {
ERRMSG("Can't seek %s. %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
/*
* Read page descriptor
*/
if (read(fd_memory, pd, sizeof(*pd)) != sizeof(*pd)) {
ERRMSG("Can't read %s. %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
/*
* Sanity check
*/
if (pd->size > dh->block_size)
return FALSE;
return TRUE;
}
static void
unmap_cache(struct cache_entry *entry)
{
munmap(entry->bufptr, entry->buflen);
}
static int
update_mmap_range(off_t offset, int initial) {
off_t start_offset, end_offset;
off_t map_size;
off_t max_offset = get_max_file_offset();
off_t pt_load_end = offset_to_pt_load_end(offset);
/*
* offset for mmap() must be page aligned.
*/
start_offset = roundup(offset, info->page_size);
end_offset = MIN(max_offset, round(pt_load_end, info->page_size));
if (!pt_load_end || (end_offset - start_offset) <= 0)
return FALSE;
map_size = MIN(end_offset - start_offset, info->mmap_region_size);
info->mmap_buf = mmap(NULL, map_size, PROT_READ, MAP_PRIVATE,
info->fd_memory, start_offset);
if (info->mmap_buf == MAP_FAILED) {
if (!initial)
DEBUG_MSG("Can't map [%llx-%llx] with mmap()\n %s",
(ulonglong)start_offset,
(ulonglong)(start_offset + map_size),
strerror(errno));
return FALSE;
}
info->mmap_start_offset = start_offset;
info->mmap_end_offset = start_offset + map_size;
return TRUE;
}
static int
update_mmap_range_parallel(int fd_memory, off_t offset,
struct mmap_cache *mmap_cache)
{
off_t start_offset, end_offset;
off_t map_size;
off_t max_offset = get_max_file_offset();
off_t pt_load_end = offset_to_pt_load_end(offset);
/*
* mmap_buf must be cleaned
*/
if (mmap_cache->mmap_buf != MAP_FAILED)
munmap(mmap_cache->mmap_buf, mmap_cache->mmap_end_offset
- mmap_cache->mmap_start_offset);
/*
* offset for mmap() must be page aligned.
*/
start_offset = roundup(offset, info->page_size);
end_offset = MIN(max_offset, round(pt_load_end, info->page_size));
if (!pt_load_end || (end_offset - start_offset) <= 0)
return FALSE;
map_size = MIN(end_offset - start_offset, info->mmap_region_size);
mmap_cache->mmap_buf = mmap(NULL, map_size, PROT_READ, MAP_PRIVATE,
fd_memory, start_offset);
if (mmap_cache->mmap_buf == MAP_FAILED) {
return FALSE;
}
mmap_cache->mmap_start_offset = start_offset;
mmap_cache->mmap_end_offset = start_offset + map_size;
return TRUE;
}
static int
is_mapped_with_mmap(off_t offset) {
if (info->flag_usemmap == MMAP_ENABLE
&& offset >= info->mmap_start_offset
&& offset < info->mmap_end_offset)
return TRUE;
else
return FALSE;
}
static int
is_mapped_with_mmap_parallel(off_t offset, struct mmap_cache *mmap_cache) {
if (offset >= mmap_cache->mmap_start_offset
&& offset < mmap_cache->mmap_end_offset)
return TRUE;
else
return FALSE;
}
int
initialize_mmap(void) {
unsigned long long phys_start;
info->mmap_region_size = MAP_REGION;
info->mmap_buf = MAP_FAILED;
get_pt_load(0, &phys_start, NULL, NULL, NULL);
if (!update_mmap_range(paddr_to_offset(phys_start), 1))
return FALSE;
return TRUE;
}
static char *
mappage_elf(unsigned long long paddr)
{
off_t offset, offset2;
if (info->flag_usemmap != MMAP_ENABLE)
return NULL;
offset = paddr_to_offset(paddr);
if (!offset || page_is_fractional(offset))
return NULL;
offset2 = paddr_to_offset(paddr + info->page_size);
if (!offset2)
return NULL;
if (offset2 - offset != info->page_size)
return NULL;
if (!is_mapped_with_mmap(offset) &&
!update_mmap_range(offset, 0)) {
ERRMSG("Can't read the dump memory(%s) with mmap().\n",
info->name_memory);
ERRMSG("This kernel might have some problems about mmap().\n");
ERRMSG("read() will be used instead of mmap() from now.\n");
/*
* Fall back to read().
*/
info->flag_usemmap = MMAP_DISABLE;
return NULL;
}
if (offset < info->mmap_start_offset ||
offset + info->page_size > info->mmap_end_offset)
return NULL;
return info->mmap_buf + (offset - info->mmap_start_offset);
}
static char *
mappage_elf_parallel(int fd_memory, unsigned long long paddr,
struct mmap_cache *mmap_cache)
{
off_t offset, offset2;
int flag_usemmap;
pthread_rwlock_rdlock(&info->usemmap_rwlock);
flag_usemmap = info->flag_usemmap;
pthread_rwlock_unlock(&info->usemmap_rwlock);
if (flag_usemmap != MMAP_ENABLE)
return NULL;
offset = paddr_to_offset(paddr);
if (!offset || page_is_fractional(offset))
return NULL;
offset2 = paddr_to_offset(paddr + info->page_size - 1);
if (!offset2)
return NULL;
if (offset2 - offset != info->page_size - 1)
return NULL;
if (!is_mapped_with_mmap_parallel(offset, mmap_cache) &&
!update_mmap_range_parallel(fd_memory, offset, mmap_cache)) {
ERRMSG("Can't read the dump memory(%s) with mmap().\n",
info->name_memory);
ERRMSG("This kernel might have some problems about mmap().\n");
ERRMSG("read() will be used instead of mmap() from now.\n");
/*
* Fall back to read().
*/
pthread_rwlock_wrlock(&info->usemmap_rwlock);
info->flag_usemmap = MMAP_DISABLE;
pthread_rwlock_unlock(&info->usemmap_rwlock);
return NULL;
}
if (offset < mmap_cache->mmap_start_offset ||
offset + info->page_size > mmap_cache->mmap_end_offset)
return NULL;
return mmap_cache->mmap_buf + (offset - mmap_cache->mmap_start_offset);
}
static int
read_from_vmcore(off_t offset, void *bufptr, unsigned long size)
{
const off_t failed = (off_t)-1;
if (lseek(info->fd_memory, offset, SEEK_SET) == failed) {
ERRMSG("Can't seek the dump memory(%s). (offset: %llx) %s\n",
info->name_memory, (unsigned long long)offset, strerror(errno));
return FALSE;
}
if (read(info->fd_memory, bufptr, size) != size) {
ERRMSG("Can't read the dump memory(%s). %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
return TRUE;
}
static int
read_from_vmcore_parallel(int fd_memory, off_t offset, void *bufptr,
unsigned long size)
{
const off_t failed = (off_t)-1;
if (lseek(fd_memory, offset, SEEK_SET) == failed) {
ERRMSG("Can't seek the dump memory(%s). (offset: %llx) %s\n",
info->name_memory, (unsigned long long)offset, strerror(errno));
return FALSE;
}
if (read(fd_memory, bufptr, size) != size) {
ERRMSG("Can't read the dump memory(%s). %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
return TRUE;
}
/*
* This function is specific for reading page from ELF.
*
* If reading the separated page on different PT_LOAD segments,
* this function gets the page data from both segments. This is
* worthy of ia64 /proc/vmcore. In ia64 /proc/vmcore, region 5
* segment is overlapping to region 7 segment. The following is
* example (page_size is 16KBytes):
*
* region | paddr | memsz
* --------+--------------------+--------------------
* 5 | 0x0000000004000000 | 0x0000000000638ce0
* 7 | 0x0000000004000000 | 0x0000000000db3000
*
* In the above example, the last page of region 5 is 0x4638000
* and the segment does not contain complete data of this page.
* Then this function gets the data of 0x4638000 - 0x4638ce0
* from region 5, and gets the remaining data from region 7.
*/
static int
readpage_elf(unsigned long long paddr, void *bufptr)
{
int idx;
off_t offset, size;
void *p, *endp;
unsigned long long phys_start, phys_end;
p = bufptr;
endp = p + info->page_size;
while (p < endp) {
idx = closest_pt_load(paddr, endp - p);
if (idx < 0)
break;
get_pt_load_extents(idx, &phys_start, &phys_end, &offset, &size);
if (phys_start > paddr) {
memset(p, 0, phys_start - paddr);
p += phys_start - paddr;
paddr = phys_start;
}
offset += paddr - phys_start;
if (size > paddr - phys_start) {
size -= paddr - phys_start;
if (size > endp - p)
size = endp - p;
if (!read_from_vmcore(offset, p, size)) {
ERRMSG("Can't read the dump memory(%s).\n",
info->name_memory);
return FALSE;
}
p += size;
paddr += size;
}
if (p < endp) {
size = phys_end - paddr;
if (size > endp - p)
size = endp - p;
memset(p, 0, size);
p += size;
paddr += size;
}
}
if (p == bufptr) {
ERRMSG("Attempt to read non-existent page at 0x%llx.\n",
paddr);
return FALSE;
} else if (p < endp)
memset(p, 0, endp - p);
return TRUE;
}
static int
readpage_elf_parallel(int fd_memory, unsigned long long paddr, void *bufptr)
{
int idx;
off_t offset, size;
void *p, *endp;
unsigned long long phys_start, phys_end;
p = bufptr;
endp = p + info->page_size;
while (p < endp) {
idx = closest_pt_load(paddr, endp - p);
if (idx < 0)
break;
get_pt_load_extents(idx, &phys_start, &phys_end, &offset, &size);
if (phys_start > paddr) {
memset(p, 0, phys_start - paddr);
p += phys_start - paddr;
paddr = phys_start;
}
offset += paddr - phys_start;
if (size > paddr - phys_start) {
size -= paddr - phys_start;
if (size > endp - p)
size = endp - p;
if (!read_from_vmcore_parallel(fd_memory, offset, p,
size)) {
ERRMSG("Can't read the dump memory(%s).\n",
info->name_memory);
return FALSE;
}
p += size;
paddr += size;
}
if (p < endp) {
size = phys_end - paddr;
if (size > endp - p)
size = endp - p;
memset(p, 0, size);
p += size;
paddr += size;
}
}
if (p == bufptr) {
ERRMSG("Attempt to read non-existent page at 0x%llx.\n",
paddr);
return FALSE;
} else if (p < endp)
memset(p, 0, endp - p);
return TRUE;
}
static int
readpage_kdump_compressed(unsigned long long paddr, void *bufptr)
{
page_desc_t pd;
char buf[info->page_size], *rdbuf;
int ret;
unsigned long retlen;
if (!is_dumpable(info->bitmap_memory, paddr_to_pfn(paddr), NULL)) {
ERRMSG("pfn(%llx) is excluded from %s.\n",
paddr_to_pfn(paddr), info->name_memory);
return FALSE;
}
if (!read_page_desc(paddr, &pd)) {
ERRMSG("Can't read page_desc: %llx\n", paddr);
return FALSE;
}
if (lseek(info->fd_memory, pd.offset, SEEK_SET) < 0) {
ERRMSG("Can't seek %s. %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
/*
* Read page data
*/
rdbuf = pd.flags & (DUMP_DH_COMPRESSED_ZLIB | DUMP_DH_COMPRESSED_LZO |
DUMP_DH_COMPRESSED_SNAPPY) ? buf : bufptr;
if (read(info->fd_memory, rdbuf, pd.size) != pd.size) {
ERRMSG("Can't read %s. %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
if (pd.flags & DUMP_DH_COMPRESSED_ZLIB) {
retlen = info->page_size;
ret = uncompress((unsigned char *)bufptr, &retlen,
(unsigned char *)buf, pd.size);
if ((ret != Z_OK) || (retlen != info->page_size)) {
ERRMSG("Uncompress failed: %d\n", ret);
return FALSE;
}
#ifdef USELZO
} else if (info->flag_lzo_support
&& (pd.flags & DUMP_DH_COMPRESSED_LZO)) {
retlen = info->page_size;
ret = lzo1x_decompress_safe((unsigned char *)buf, pd.size,
(unsigned char *)bufptr, &retlen,
LZO1X_MEM_DECOMPRESS);
if ((ret != LZO_E_OK) || (retlen != info->page_size)) {
ERRMSG("Uncompress failed: %d\n", ret);
return FALSE;
}
#endif
#ifdef USESNAPPY
} else if ((pd.flags & DUMP_DH_COMPRESSED_SNAPPY)) {
ret = snappy_uncompressed_length(buf, pd.size, (size_t *)&retlen);
if (ret != SNAPPY_OK) {
ERRMSG("Uncompress failed: %d\n", ret);
return FALSE;
}
ret = snappy_uncompress(buf, pd.size, bufptr, (size_t *)&retlen);
if ((ret != SNAPPY_OK) || (retlen != info->page_size)) {
ERRMSG("Uncompress failed: %d\n", ret);
return FALSE;
}
#endif
}
return TRUE;
}
static int
readpage_kdump_compressed_parallel(int fd_memory, unsigned long long paddr,
void *bufptr,
struct dump_bitmap* bitmap_memory_parallel)
{
page_desc_t pd;
char buf[info->page_size], *rdbuf;
int ret;
unsigned long retlen;
if (!is_dumpable(bitmap_memory_parallel, paddr_to_pfn(paddr), NULL)) {
ERRMSG("pfn(%llx) is excluded from %s.\n",
paddr_to_pfn(paddr), info->name_memory);
return FALSE;
}
if (!read_page_desc_parallel(fd_memory, paddr, &pd,
bitmap_memory_parallel)) {
ERRMSG("Can't read page_desc: %llx\n", paddr);
return FALSE;
}
if (lseek(fd_memory, pd.offset, SEEK_SET) < 0) {
ERRMSG("Can't seek %s. %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
/*
* Read page data
*/
rdbuf = pd.flags & (DUMP_DH_COMPRESSED_ZLIB | DUMP_DH_COMPRESSED_LZO |
DUMP_DH_COMPRESSED_SNAPPY) ? buf : bufptr;
if (read(fd_memory, rdbuf, pd.size) != pd.size) {
ERRMSG("Can't read %s. %s\n",
info->name_memory, strerror(errno));
return FALSE;
}
if (pd.flags & DUMP_DH_COMPRESSED_ZLIB) {
retlen = info->page_size;
ret = uncompress((unsigned char *)bufptr, &retlen,
(unsigned char *)buf, pd.size);
if ((ret != Z_OK) || (retlen != info->page_size)) {
ERRMSG("Uncompress failed: %d\n", ret);
return FALSE;
}
#ifdef USELZO
} else if (info->flag_lzo_support
&& (pd.flags & DUMP_DH_COMPRESSED_LZO)) {
retlen = info->page_size;
ret = lzo1x_decompress_safe((unsigned char *)buf, pd.size,
(unsigned char *)bufptr, &retlen,
LZO1X_MEM_DECOMPRESS);
if ((ret != LZO_E_OK) || (retlen != info->page_size)) {
ERRMSG("Uncompress failed: %d\n", ret);
return FALSE;
}
#endif
#ifdef USESNAPPY
} else if ((pd.flags & DUMP_DH_COMPRESSED_SNAPPY)) {
ret = snappy_uncompressed_length(buf, pd.size, (size_t *)&retlen);
if (ret != SNAPPY_OK) {
ERRMSG("Uncompress failed: %d\n", ret);
return FALSE;
}
ret = snappy_uncompress(buf, pd.size, bufptr, (size_t *)&retlen);
if ((ret != SNAPPY_OK) || (retlen != info->page_size)) {
ERRMSG("Uncompress failed: %d\n", ret);
return FALSE;
}
#endif
}
return TRUE;
}
int
readmem(int type_addr, unsigned long long addr, void *bufptr, size_t size)
{
size_t read_size, size_orig = size;
unsigned long long paddr;
unsigned long long pgaddr;
void *pgbuf;
struct cache_entry *cached;
next_page:
switch (type_addr) {
case VADDR:
if ((paddr = vaddr_to_paddr(addr)) == NOT_PADDR) {
ERRMSG("Can't convert a virtual address(%llx) to physical address.\n",
addr);
goto error;
}
break;
case PADDR:
paddr = addr;
break;
case VADDR_XEN:
if ((paddr = kvtop_xen(addr)) == NOT_PADDR) {
ERRMSG("Can't convert a virtual address(%llx) to machine address.\n",
addr);
goto error;