-
Notifications
You must be signed in to change notification settings - Fork 278
/
netdump.c
5442 lines (4841 loc) · 148 KB
/
netdump.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
/* netdump.c
*
* Copyright (C) 2002-2019 David Anderson
* Copyright (C) 2002-2019 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.
*
* Author: David Anderson
*/
#define _LARGEFILE64_SOURCE 1 /* stat64() */
#include "defs.h"
#include "netdump.h"
#include "sadump.h"
#include "xen_dom0.h"
static struct vmcore_data vmcore_data = { 0 };
static struct vmcore_data *nd = &vmcore_data;
static struct proc_kcore_data proc_kcore_data = { 0 };
static struct proc_kcore_data *pkd = &proc_kcore_data;
static void netdump_print(char *, ...);
static size_t resize_elf_header(int, char *, char **, char **, ulong);
static void dump_Elf32_Ehdr(Elf32_Ehdr *);
static void dump_Elf32_Phdr(Elf32_Phdr *, int);
static size_t dump_Elf32_Nhdr(Elf32_Off offset, int);
static void dump_Elf64_Ehdr(Elf64_Ehdr *);
static void dump_Elf64_Phdr(Elf64_Phdr *, int);
static void dump_Elf64_Shdr(Elf64_Shdr *shdr);
static size_t dump_Elf64_Nhdr(Elf64_Off offset, int);
static void get_netdump_regs_32(struct bt_info *, ulong *, ulong *);
static void get_netdump_regs_ppc(struct bt_info *, ulong *, ulong *);
static void get_netdump_regs_ppc64(struct bt_info *, ulong *, ulong *);
static void get_netdump_regs_arm(struct bt_info *, ulong *, ulong *);
static void get_netdump_regs_arm64(struct bt_info *, ulong *, ulong *);
static void get_netdump_regs_mips(struct bt_info *, ulong *, ulong *);
static void get_netdump_regs_riscv(struct bt_info *, ulong *, ulong *);
static void get_netdump_regs_loongarch64(struct bt_info *, ulong *, ulong *);
static void check_dumpfile_size(char *);
static int proc_kcore_init_32(FILE *, int);
static int proc_kcore_init_64(FILE *, int);
static char *get_regs_from_note(char *, ulong *, ulong *);
static void kdump_get_osrelease(void);
static char *vmcoreinfo_read_string(const char *);
#define ELFSTORE 1
#define ELFREAD 0
#define MIN_PAGE_SIZE (4096)
/*
* Architectures that have configurable page sizes,
* can differ from the host machine's page size.
*/
#define READ_PAGESIZE_FROM_VMCOREINFO() \
(machine_type("IA64") || machine_type("PPC64") || machine_type("PPC") || machine_type("ARM64"))
/*
* kdump installs NT_PRSTATUS elf notes only to the cpus
* that were online during dumping. Hence we call into
* this function after reading the cpu map from the kernel,
* to remap the NT_PRSTATUS notes only to the online cpus.
*/
void
map_cpus_to_prstatus(void)
{
void **nt_ptr;
int online, i, j, nrcpus;
size_t size;
if (pc->flags2 & QEMU_MEM_DUMP_ELF) /* notes exist for all cpus */
return;
if (!(online = get_cpus_online()) || (online == kt->cpus))
return;
if (CRASHDEBUG(1))
error(INFO,
"cpus: %d online: %d NT_PRSTATUS notes: %d (remapping)\n",
kt->cpus, online, nd->num_prstatus_notes);
size = NR_CPUS * sizeof(void *);
nt_ptr = (void **)GETBUF(size);
BCOPY(nd->nt_prstatus_percpu, nt_ptr, size);
BZERO(nd->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)) {
nd->nt_prstatus_percpu[i] = nt_ptr[j++];
nd->num_prstatus_notes =
MAX(nd->num_prstatus_notes, i+1);
}
}
FREEBUF(nt_ptr);
}
/*
* Determine whether a file is a netdump/diskdump/kdump creation,
* and if TRUE, initialize the vmcore_data structure.
*/
int
is_netdump(char *file, ulong source_query)
{
int i, fd, swap;
Elf32_Ehdr *elf32;
Elf32_Phdr *load32;
Elf64_Ehdr *elf64;
Elf64_Phdr *load64;
char *eheader, *sect0;
char buf[BUFSIZE];
ssize_t size;
size_t len, tot;
Elf32_Off offset32;
Elf64_Off offset64;
ulong format;
if ((fd = open(file, O_RDWR)) < 0) {
if ((fd = open(file, O_RDONLY)) < 0) {
sprintf(buf, "%s: open", file);
perror(buf);
return FALSE;
}
}
size = SAFE_NETDUMP_ELF_HEADER_SIZE;
if ((eheader = (char *)malloc(size)) == NULL) {
fprintf(stderr, "cannot malloc ELF header buffer\n");
clean_exit(1);
}
if (FLAT_FORMAT()) {
if (!read_flattened_format(fd, 0, eheader, size))
goto bailout;
} else {
size = read(fd, eheader, size);
if (size < 0) {
sprintf(buf, "%s: ELF header read", file);
perror(buf);
goto bailout;
} else if (size < MIN_NETDUMP_ELF_HEADER_SIZE) {
fprintf(stderr, "%s: file too small!\n", file);
goto bailout;
}
}
load32 = NULL;
load64 = NULL;
format = 0;
elf32 = (Elf32_Ehdr *)&eheader[0];
elf64 = (Elf64_Ehdr *)&eheader[0];
/*
* Verify the ELF header, and determine the dumpfile format.
*
* For now, kdump vmcores differ from netdump/diskdump like so:
*
* 1. The first kdump PT_LOAD segment is packed just after
* the ELF header, whereas netdump/diskdump page-align
* the first PT_LOAD segment.
* 2. Each kdump PT_LOAD segment has a p_align field of zero,
* whereas netdump/diskdump have their p_align fields set
* to the system page-size.
*
* If either kdump difference is seen, presume kdump -- this
* is obviously subject to change.
*/
if (!STRNEQ(eheader, ELFMAG) || eheader[EI_VERSION] != EV_CURRENT)
goto bailout;
swap = (((eheader[EI_DATA] == ELFDATA2LSB) &&
(__BYTE_ORDER == __BIG_ENDIAN)) ||
((eheader[EI_DATA] == ELFDATA2MSB) &&
(__BYTE_ORDER == __LITTLE_ENDIAN)));
if ((elf32->e_ident[EI_CLASS] == ELFCLASS32) &&
(swap16(elf32->e_type, swap) == ET_CORE) &&
(swap32(elf32->e_version, swap) == EV_CURRENT) &&
(swap16(elf32->e_phnum, swap) >= 2)) {
switch (swap16(elf32->e_machine, swap))
{
case EM_386:
if (machine_type_mismatch(file, "X86", NULL,
source_query))
goto bailout;
break;
case EM_ARM:
if (machine_type_mismatch(file, "ARM", NULL,
source_query))
goto bailout;
break;
case EM_PPC:
if (machine_type_mismatch(file, "PPC", NULL,
source_query))
goto bailout;
break;
case EM_MIPS:
if (machine_type_mismatch(file, "MIPS", NULL,
source_query))
goto bailout;
break;
default:
if (machine_type_mismatch(file, "(unknown)", NULL,
source_query))
goto bailout;
}
if (endian_mismatch(file, elf32->e_ident[EI_DATA],
source_query))
goto bailout;
if (elf32->e_phoff != sizeof(Elf32_Ehdr)) {
if (CRASHDEBUG(1))
error(WARNING, "%s: first PHdr not following "
"EHdr (PHdr offset = %u)\n", file,
elf32->e_phoff);
/* it's okay as long as we've read enough data */
if (elf32->e_phoff > size - 2 * sizeof(Elf32_Phdr)) {
error(WARNING, "%s: PHdr to far into file!\n",
file);
goto bailout;
}
}
/* skip the NOTE program header */
load32 = (Elf32_Phdr *)
&eheader[elf32->e_phoff+sizeof(Elf32_Phdr)];
if ((load32->p_offset & (MIN_PAGE_SIZE-1)) ||
(load32->p_align == 0))
format = KDUMP_ELF32;
else
format = NETDUMP_ELF32;
} else if ((elf64->e_ident[EI_CLASS] == ELFCLASS64) &&
(swap16(elf64->e_type, swap) == ET_CORE) &&
(swap32(elf64->e_version, swap) == EV_CURRENT) &&
(swap16(elf64->e_phnum, swap) >= 2)) {
switch (swap16(elf64->e_machine, swap))
{
case EM_IA_64:
if (machine_type_mismatch(file, "IA64", NULL,
source_query))
goto bailout;
break;
case EM_PPC64:
if (machine_type_mismatch(file, "PPC64", NULL,
source_query))
goto bailout;
break;
case EM_X86_64:
if (machine_type_mismatch(file, "X86_64", NULL,
source_query))
goto bailout;
break;
case EM_S390:
if (machine_type_mismatch(file, "S390X", NULL,
source_query))
goto bailout;
break;
case EM_386:
if (machine_type_mismatch(file, "X86", NULL,
source_query))
goto bailout;
break;
case EM_ARM:
if (machine_type_mismatch(file, "ARM", NULL,
source_query))
goto bailout;
break;
case EM_AARCH64:
if (machine_type_mismatch(file, "ARM64", NULL,
source_query))
goto bailout;
break;
case EM_MIPS:
if (machine_type_mismatch(file, "MIPS", "MIPS64",
source_query))
goto bailout;
break;
case EM_RISCV:
if (machine_type_mismatch(file, "RISCV64", NULL,
source_query))
goto bailout;
break;
case EM_LOONGARCH:
if (machine_type_mismatch(file, "LOONGARCH64", NULL,
source_query))
goto bailout;
break;
default:
if (machine_type_mismatch(file, "(unknown)", NULL,
source_query))
goto bailout;
}
if (endian_mismatch(file, elf64->e_ident[EI_DATA],
source_query))
goto bailout;
if (elf64->e_phoff != sizeof(Elf64_Ehdr)) {
if (CRASHDEBUG(1))
error(WARNING, "%s: first PHdr not following "
"EHdr (PHdr offset = %u)\n", file,
elf64->e_phoff);
/* it's okay as long as we've read enough data */
if (elf64->e_phoff > size - 2 * sizeof(Elf64_Phdr)) {
error(WARNING, "%s: PHdr to far into file!\n",
file);
goto bailout;
}
}
/* skip the NOTE program header */
load64 = (Elf64_Phdr *)
&eheader[elf64->e_phoff+sizeof(Elf64_Phdr)];
if ((load64->p_offset & (MIN_PAGE_SIZE-1)) ||
(load64->p_align == 0))
format = KDUMP_ELF64;
else
format = NETDUMP_ELF64;
} else {
if (CRASHDEBUG(2))
error(INFO, "%s: not a %s ELF dumpfile\n",
file, source_query == NETDUMP_LOCAL ?
"netdump" : "kdump");
goto bailout;
}
if (source_query == KCORE_LOCAL) {
close(fd);
return TRUE;
}
switch (format)
{
case NETDUMP_ELF32:
case NETDUMP_ELF64:
if (source_query & (NETDUMP_LOCAL|NETDUMP_REMOTE))
break;
else
goto bailout;
case KDUMP_ELF32:
case KDUMP_ELF64:
if (source_query & KDUMP_LOCAL)
break;
else
goto bailout;
}
sect0 = NULL;
if (!(size = resize_elf_header(fd, file, &eheader, §0, format)))
goto bailout;
nd->ndfd = fd;
nd->elf_header = eheader;
nd->flags = format | source_query;
switch (format)
{
case NETDUMP_ELF32:
case KDUMP_ELF32:
nd->header_size = size;
nd->elf32 = (Elf32_Ehdr *)&nd->elf_header[0];
nd->num_pt_load_segments = nd->elf32->e_phnum - 1;
if ((nd->pt_load_segments = (struct pt_load_segment *)
malloc(sizeof(struct pt_load_segment) *
nd->num_pt_load_segments)) == NULL) {
fprintf(stderr, "cannot malloc PT_LOAD segment buffers\n");
clean_exit(1);
}
nd->notes32 = (Elf32_Phdr *)
&nd->elf_header[nd->elf32->e_phoff];
nd->load32 = nd->notes32 + 1;
if (format == NETDUMP_ELF32)
nd->page_size = (uint)nd->load32->p_align;
dump_Elf32_Ehdr(nd->elf32);
dump_Elf32_Phdr(nd->notes32, ELFREAD);
for (i = 0; i < nd->num_pt_load_segments; i++)
dump_Elf32_Phdr(nd->load32 + i, ELFSTORE+i);
offset32 = nd->notes32->p_offset;
for (tot = 0; tot < nd->notes32->p_filesz; tot += len) {
if (!(len = dump_Elf32_Nhdr(offset32, ELFSTORE)))
break;
offset32 += len;
}
break;
case NETDUMP_ELF64:
case KDUMP_ELF64:
nd->header_size = size;
nd->elf64 = (Elf64_Ehdr *)&nd->elf_header[0];
/*
* Extended Numbering support
* See include/uapi/linux/elf.h and elf(5) for more information
*/
if (nd->elf64->e_phnum == PN_XNUM) {
nd->sect0_64 = (Elf64_Shdr *)sect0;
nd->num_pt_load_segments = nd->sect0_64->sh_info - 1;
} else
nd->num_pt_load_segments = nd->elf64->e_phnum - 1;
if ((nd->pt_load_segments = (struct pt_load_segment *)
malloc(sizeof(struct pt_load_segment) *
nd->num_pt_load_segments)) == NULL) {
fprintf(stderr, "cannot malloc PT_LOAD segment buffers\n");
clean_exit(1);
}
nd->notes64 = (Elf64_Phdr *)
&nd->elf_header[nd->elf64->e_phoff];
nd->load64 = nd->notes64 + 1;
if (format == NETDUMP_ELF64)
nd->page_size = (uint)nd->load64->p_align;
dump_Elf64_Ehdr(nd->elf64);
dump_Elf64_Phdr(nd->notes64, ELFREAD);
for (i = 0; i < nd->num_pt_load_segments; i++)
dump_Elf64_Phdr(nd->load64 + i, ELFSTORE+i);
offset64 = nd->notes64->p_offset;
for (tot = 0; tot < nd->notes64->p_filesz; tot += len) {
if (!(len = dump_Elf64_Nhdr(offset64, ELFSTORE)))
break;
offset64 += len;
}
break;
}
if (CRASHDEBUG(1))
netdump_memory_dump(fp);
pc->read_vmcoreinfo = vmcoreinfo_read_string;
if ((source_query == KDUMP_LOCAL) &&
(pc->flags2 & GET_OSRELEASE))
kdump_get_osrelease();
if ((source_query == KDUMP_LOCAL) &&
(pc->flags2 & GET_LOG)) {
pc->dfd = nd->ndfd;
pc->readmem = read_kdump;
nd->flags |= KDUMP_LOCAL;
pc->flags |= KDUMP;
get_log_from_vmcoreinfo(file);
}
return nd->header_size;
bailout:
close(fd);
free(eheader);
return FALSE;
}
/*
* Search through all PT_LOAD segments to determine the
* file offset where the physical memory segment(s) start
* in the vmcore, and consider everything prior to that as
* header contents.
*/
static size_t
resize_elf_header(int fd, char *file, char **eheader_ptr, char **sect0_ptr,
ulong format)
{
int i;
char buf[BUFSIZE];
char *eheader;
Elf32_Ehdr *elf32;
Elf32_Phdr *load32;
Elf64_Ehdr *elf64;
Elf64_Phdr *load64;
Elf32_Off p_offset32;
Elf64_Off p_offset64;
size_t header_size;
uint num_pt_load_segments;
eheader = *eheader_ptr;
header_size = num_pt_load_segments = 0;
elf32 = (Elf32_Ehdr *)&eheader[0];
elf64 = (Elf64_Ehdr *)&eheader[0];
switch (format)
{
case NETDUMP_ELF32:
case KDUMP_ELF32:
num_pt_load_segments = elf32->e_phnum - 1;
header_size = MAX(sizeof(Elf32_Ehdr), elf32->e_phoff) +
(sizeof(Elf32_Phdr) * (num_pt_load_segments + 1));
break;
case NETDUMP_ELF64:
case KDUMP_ELF64:
/*
* Extended Numbering support
* See include/uapi/linux/elf.h and elf(5) for more information
*/
if (elf64->e_phnum == PN_XNUM) {
Elf64_Shdr *shdr64;
shdr64 = (Elf64_Shdr *)malloc(sizeof(*shdr64));
if (!shdr64) {
fprintf(stderr,
"cannot malloc a section header buffer\n");
return 0;
}
if (FLAT_FORMAT()) {
if (!read_flattened_format(fd, elf64->e_shoff,
shdr64, elf64->e_shentsize))
return 0;
} else {
if (lseek(fd, elf64->e_shoff, SEEK_SET) !=
elf64->e_shoff) {
sprintf(buf, "%s: section header lseek",
file);
perror(buf);
return 0;
}
if (read(fd, shdr64, elf64->e_shentsize) !=
elf64->e_shentsize) {
sprintf(buf, "%s: section header read",
file);
perror(buf);
return 0;
}
}
num_pt_load_segments = shdr64->sh_info - 1;
*sect0_ptr = (char *)shdr64;
} else
num_pt_load_segments = elf64->e_phnum - 1;
header_size = MAX(sizeof(Elf64_Ehdr), elf64->e_phoff) +
(sizeof(Elf64_Phdr) * (num_pt_load_segments + 1));
break;
}
if ((eheader = (char *)realloc(eheader, header_size)) == NULL) {
fprintf(stderr, "cannot realloc interim ELF header buffer\n");
clean_exit(1);
} else
*eheader_ptr = eheader;
elf32 = (Elf32_Ehdr *)&eheader[0];
elf64 = (Elf64_Ehdr *)&eheader[0];
if (FLAT_FORMAT()) {
if (!read_flattened_format(fd, 0, eheader, header_size))
return 0;
} else {
if (lseek(fd, 0, SEEK_SET) != 0) {
sprintf(buf, "%s: lseek", file);
perror(buf);
return 0;
}
if (read(fd, eheader, header_size) != header_size) {
sprintf(buf, "%s: ELF header read", file);
perror(buf);
return 0;
}
}
switch (format)
{
case NETDUMP_ELF32:
case KDUMP_ELF32:
load32 = (Elf32_Phdr *)&eheader[elf32->e_phoff+sizeof(Elf32_Phdr)];
p_offset32 = load32->p_offset;
for (i = 0; i < num_pt_load_segments; i++, load32 += 1) {
if (load32->p_offset &&
(p_offset32 > load32->p_offset))
p_offset32 = load32->p_offset;
}
header_size = (size_t)p_offset32;
break;
case NETDUMP_ELF64:
case KDUMP_ELF64:
load64 = (Elf64_Phdr *)&eheader[elf64->e_phoff+sizeof(Elf64_Phdr)];
p_offset64 = load64->p_offset;
for (i = 0; i < num_pt_load_segments; i++, load64 += 1) {
if (load64->p_offset &&
(p_offset64 > load64->p_offset))
p_offset64 = load64->p_offset;
}
header_size = (size_t)p_offset64;
break;
}
if ((eheader = (char *)realloc(eheader, header_size)) == NULL) {
perror("realloc");
fprintf(stderr, "cannot realloc resized ELF header buffer\n");
clean_exit(1);
} else
*eheader_ptr = eheader;
if (FLAT_FORMAT()) {
if (!read_flattened_format(fd, 0, eheader, header_size))
return 0;
} else {
if (lseek(fd, 0, SEEK_SET) != 0) {
sprintf(buf, "%s: lseek", file);
perror(buf);
return 0;
}
if (read(fd, eheader, header_size) != header_size) {
sprintf(buf, "%s: ELF header read", file);
perror(buf);
return 0;
}
}
return header_size;
}
/*
* Return the e_version number of an ELF file
* (or -1 if its not readable ELF file)
*/
int
file_elf_version(char *file)
{
int fd, size;
Elf32_Ehdr *elf32;
Elf64_Ehdr *elf64;
char header[MIN_NETDUMP_ELF_HEADER_SIZE];
char buf[BUFSIZE];
if ((fd = open(file, O_RDONLY)) < 0) {
sprintf(buf, "%s: open", file);
perror(buf);
return -1;
}
size = MIN_NETDUMP_ELF_HEADER_SIZE;
if (read(fd, header, size) != size) {
sprintf(buf, "%s: read", file);
perror(buf);
close(fd);
return -1;
}
close(fd);
elf32 = (Elf32_Ehdr *)&header[0];
elf64 = (Elf64_Ehdr *)&header[0];
if (STRNEQ(elf32->e_ident, ELFMAG) &&
(elf32->e_ident[EI_CLASS] == ELFCLASS32) &&
(elf32->e_ident[EI_DATA] == ELFDATA2LSB) &&
(elf32->e_ident[EI_VERSION] == EV_CURRENT)) {
return (elf32->e_version);
} else if (STRNEQ(elf64->e_ident, ELFMAG) &&
(elf64->e_ident[EI_CLASS] == ELFCLASS64) &&
(elf64->e_ident[EI_VERSION] == EV_CURRENT)) {
return (elf64->e_version);
}
return -1;
}
/*
* Check whether any PT_LOAD segment goes beyond the file size.
*/
static void
check_dumpfile_size(char *file)
{
int i;
struct stat64 stat;
struct pt_load_segment *pls;
uint64_t segment_end;
if (is_ramdump_image())
return;
if (stat64(file, &stat) < 0)
return;
if (S_ISBLK(stat.st_mode)) {
error(NOTE, "%s: No dump complete check for block devices\n",
file);
return;
}
for (i = 0; i < nd->num_pt_load_segments; i++) {
pls = &nd->pt_load_segments[i];
segment_end = pls->file_offset +
(pls->phys_end - pls->phys_start);
if (segment_end > stat.st_size) {
error(WARNING, "%s: may be truncated or incomplete\n"
" PT_LOAD p_offset: %lld\n"
" p_filesz: %lld\n"
" bytes required: %lld\n"
" dumpfile size: %lld\n\n",
file, pls->file_offset,
pls->phys_end - pls->phys_start,
segment_end, stat.st_size);
return;
}
}
}
/*
* Perform any post-dumpfile determination stuff here.
*/
int
netdump_init(char *unused, FILE *fptr)
{
if (!VMCORE_VALID())
return FALSE;
machdep->is_cpu_prstatus_valid = diskdump_is_cpu_prstatus_valid;
nd->ofp = fptr;
check_dumpfile_size(pc->dumpfile);
return TRUE;
}
/*
* Read from a netdump-created dumpfile.
*/
int
read_netdump(int fd, void *bufptr, int cnt, ulong addr, physaddr_t paddr)
{
off_t offset;
ssize_t read_ret;
struct pt_load_segment *pls;
int i;
offset = 0;
/*
* The Elf32_Phdr has 32-bit fields for p_paddr, p_filesz and
* p_memsz, so for now, multiple PT_LOAD segment support is
* restricted to 64-bit machines for netdump/diskdump vmcores.
* However, kexec/kdump has introduced the optional use of a
* 64-bit ELF header for 32-bit processors.
*/
switch (DUMPFILE_FORMAT(nd->flags))
{
case NETDUMP_ELF32:
offset = (off_t)paddr + (off_t)nd->header_size;
break;
case NETDUMP_ELF64:
case KDUMP_ELF32:
case KDUMP_ELF64:
if (nd->num_pt_load_segments == 1) {
offset = (off_t)paddr + (off_t)nd->header_size -
(off_t)nd->pt_load_segments[0].phys_start;
break;
}
for (i = offset = 0; i < nd->num_pt_load_segments; i++) {
pls = &nd->pt_load_segments[i];
if ((paddr >= pls->phys_start) &&
(paddr < pls->phys_end)) {
offset = (off_t)(paddr - pls->phys_start) +
pls->file_offset;
break;
}
if (pls->zero_fill && (paddr >= pls->phys_end) &&
(paddr < pls->zero_fill)) {
memset(bufptr, 0, cnt);
if (CRASHDEBUG(8))
fprintf(fp, "read_netdump: zero-fill: "
"addr: %lx paddr: %llx cnt: %d\n",
addr, (ulonglong)paddr, cnt);
return cnt;
}
}
if (!offset) {
if (CRASHDEBUG(8))
fprintf(fp, "read_netdump: READ_ERROR: "
"offset not found for paddr: %llx\n",
(ulonglong)paddr);
return READ_ERROR;
}
break;
}
if (CRASHDEBUG(8))
fprintf(fp, "read_netdump: addr: %lx paddr: %llx cnt: %d offset: %llx\n",
addr, (ulonglong)paddr, cnt, (ulonglong)offset);
if (FLAT_FORMAT()) {
if (!read_flattened_format(nd->ndfd, offset, bufptr, cnt)) {
if (CRASHDEBUG(8))
fprintf(fp, "read_netdump: READ_ERROR: "
"read_flattened_format failed for offset:"
" %llx\n",
(ulonglong)offset);
return READ_ERROR;
}
} else {
if (lseek(nd->ndfd, offset, SEEK_SET) == -1) {
if (CRASHDEBUG(8))
fprintf(fp, "read_netdump: SEEK_ERROR: "
"offset: %llx\n", (ulonglong)offset);
return SEEK_ERROR;
}
read_ret = read(nd->ndfd, bufptr, cnt);
if (read_ret != cnt) {
/*
* First check whether zero_excluded has been set.
*/
if ((read_ret >= 0) &&
(*diskdump_flags & ZERO_EXCLUDED)) {
if (CRASHDEBUG(8))
fprintf(fp, "read_netdump: zero-fill: "
"addr: %lx paddr: %llx cnt: %d\n",
addr + read_ret,
(ulonglong)paddr + read_ret,
cnt - (int)read_ret);
bufptr += read_ret;
bzero(bufptr, cnt - read_ret);
return cnt;
}
if (CRASHDEBUG(8))
fprintf(fp, "read_netdump: READ_ERROR: "
"offset: %llx\n", (ulonglong)offset);
return READ_ERROR;
}
}
return cnt;
}
/*
* Write to a netdump-created dumpfile. Note that cmd_wr() does not
* allow writes to dumpfiles, so you can't get here from there.
* But, if it would ever be helpful, here it is...
*/
int
write_netdump(int fd, void *bufptr, int cnt, ulong addr, physaddr_t paddr)
{
off_t offset;
struct pt_load_segment *pls;
int i;
offset = 0;
switch (DUMPFILE_FORMAT(nd->flags))
{
case NETDUMP_ELF32:
offset = (off_t)paddr + (off_t)nd->header_size;
break;
case NETDUMP_ELF64:
case KDUMP_ELF32:
case KDUMP_ELF64:
if (nd->num_pt_load_segments == 1) {
offset = (off_t)paddr + (off_t)nd->header_size;
break;
}
for (i = offset = 0; i < nd->num_pt_load_segments; i++) {
pls = &nd->pt_load_segments[i];
if ((paddr >= pls->phys_start) &&
(paddr < pls->phys_end)) {
offset = (off_t)(paddr - pls->phys_start) +
pls->file_offset;
break;
}
}
if (!offset)
return READ_ERROR;
break;
}
if (lseek(nd->ndfd, offset, SEEK_SET) == -1)
return SEEK_ERROR;
if (write(nd->ndfd, bufptr, cnt) != cnt)
return READ_ERROR;
return cnt;
}
/*
* Set the file pointer for debug output.
*/
FILE *
set_netdump_fp(FILE *fp)
{
if (!VMCORE_VALID())
return NULL;
nd->ofp = fp;
return fp;
}
/*
* Generic print routine to handle integral and remote daemon output.
*/
static void
netdump_print(char *fmt, ...)
{
char buf[BUFSIZE];
va_list ap;
if (!fmt || !strlen(fmt) || !VMCORE_VALID())
return;
va_start(ap, fmt);
(void)vsnprintf(buf, BUFSIZE, fmt, ap);
va_end(ap);
if (nd->ofp)
fprintf(nd->ofp, "%s", buf);
else
console(buf);
}
uint
netdump_page_size(void)
{
if (!VMCORE_VALID())
return 0;
return nd->page_size;
}
int
netdump_free_memory(void)
{
return (VMCORE_VALID() ? 0 : 0);
}
int netdump_memory_used(void)
{
return (VMCORE_VALID() ? 0 : 0);
}
/*
* The netdump server will eventually use the NT_TASKSTRUCT section
* to pass the task address. Until such time, look at the ebp of the
* user_regs_struct, which is located at the end of the NT_PRSTATUS
* elf_prstatus structure, minus one integer:
*
* struct elf_prstatus
* {
* ...
* elf_gregset_t pr_reg; (maps to user_regs_struct)
* int pr_fpvalid;
* };
*
* If it's a kernel stack address who's adjusted task_struct value is
* equal to one of the active set tasks, we'll presume it's legit.
*
*/
ulong
get_netdump_panic_task(void)
{
#ifdef DAEMON
return nd->task_struct;
#else
int i, crashing_cpu;
size_t len;
char *user_regs;
ulong ebp, esp, task;