File: netdump.c

package info (click to toggle)
crash 7.0.8-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 36,348 kB
  • ctags: 13,100
  • sloc: ansic: 135,839; makefile: 495
file content (4101 lines) | stat: -rw-r--r-- 111,941 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
/* netdump.c 
 *
 * Copyright (C) 2002-2014 David Anderson
 * Copyright (C) 2002-2014 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"

static struct vmcore_data vmcore_data = { 0 };
static struct vmcore_data *nd = &vmcore_data;
static struct xen_kdump_data xen_kdump_data = { 0 };
static struct proc_kcore_data proc_kcore_data = { 0 };
static struct proc_kcore_data *pkd = &proc_kcore_data;
static void netdump_print(char *, ...);
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 size_t dump_Elf64_Nhdr(Elf64_Off offset, int);
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 physaddr_t xen_kdump_p2m(physaddr_t);
static void check_dumpfile_size(char *);
static int proc_kcore_init_32(FILE *fp);
static int proc_kcore_init_64(FILE *fp);
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 (!(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))
			nd->nt_prstatus_percpu[i] = nt_ptr[j++];
	}

	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[MIN_NETDUMP_ELF_HEADER_SIZE];
	char buf[BUFSIZE];
	size_t size, len, tot;
        Elf32_Off offset32;
        Elf64_Off offset64;
	ulong tmp_flags;
	char *tmp_elf_header;

	if ((fd = open(file, O_RDWR)) < 0) {
        	if ((fd = open(file, O_RDONLY)) < 0) {
                        sprintf(buf, "%s: open", file);
                        perror(buf);
                        return FALSE;
		}
	}

	size = MIN_NETDUMP_ELF_HEADER_SIZE;

	if (FLAT_FORMAT()) {
		if (!read_flattened_format(fd, 0, eheader, size))
			goto bailout;
	} else {
		if (read(fd, eheader, size) != size) {
			sprintf(buf, "%s: read", file);
			perror(buf);
			goto bailout;
		}
	}

	load32 = NULL;
	load64 = NULL;
	tmp_flags = 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;

		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;

                load32 = (Elf32_Phdr *)
                        &eheader[sizeof(Elf32_Ehdr)+sizeof(Elf32_Phdr)];
                size = (size_t)load32->p_offset;

		if ((load32->p_offset & (MIN_PAGE_SIZE-1)) ||
		    (load32->p_align == 0))
                	tmp_flags |= KDUMP_ELF32;
		else
                	tmp_flags |= 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;

		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;

                load64 = (Elf64_Phdr *)
                        &eheader[sizeof(Elf64_Ehdr)+sizeof(Elf64_Phdr)];
                size = (size_t)load64->p_offset;
		if ((load64->p_offset & (MIN_PAGE_SIZE-1)) ||
		    (load64->p_align == 0))
                	tmp_flags |= KDUMP_ELF64;
		else
                	tmp_flags |= 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 (DUMPFILE_FORMAT(tmp_flags))
	{
	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;
	}

	if ((tmp_elf_header = (char *)malloc(size)) == NULL) {
		fprintf(stderr, "cannot malloc ELF header buffer\n");
		clean_exit(1);
	}

	if (FLAT_FORMAT()) {
		if (!read_flattened_format(fd, 0, tmp_elf_header, size)) {
			free(tmp_elf_header);
			goto bailout;
		}
	} else {
		if (lseek(fd, 0, SEEK_SET) != 0) {
			sprintf(buf, "%s: lseek", file);
			perror(buf);
			goto bailout;
		}
		if (read(fd, tmp_elf_header, size) != size) {
			sprintf(buf, "%s: read", file);
			perror(buf);
			free(tmp_elf_header);
			goto bailout;
		}
	}

	nd->ndfd = fd;
	nd->elf_header = tmp_elf_header;
	nd->flags = tmp_flags;
	nd->flags |= source_query;

	switch (DUMPFILE_FORMAT(nd->flags))
	{
	case NETDUMP_ELF32:
	case KDUMP_ELF32:
		nd->header_size = load32->p_offset;
        	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[sizeof(Elf32_Ehdr)];
        	nd->load32 = (Elf32_Phdr *)
		    &nd->elf_header[sizeof(Elf32_Ehdr)+sizeof(Elf32_Phdr)];
		if (DUMPFILE_FORMAT(nd->flags) == 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 = load64->p_offset;
                nd->elf64 = (Elf64_Ehdr *)&nd->elf_header[0];
		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[sizeof(Elf64_Ehdr)];
                nd->load64 = (Elf64_Phdr *)
                    &nd->elf_header[sizeof(Elf64_Ehdr)+sizeof(Elf64_Phdr)];
		if (DUMPFILE_FORMAT(nd->flags) == 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);
	return FALSE;
}

/*
 *  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;

	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;

	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;
	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;
		}
		if (read(nd->ndfd, bufptr, cnt) != 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;

	if (!VMCORE_VALID() || !get_active_set())
		goto panic_task_undetermined;

	if (nd->task_struct) {
		if (CRASHDEBUG(1))
			error(INFO, 
			    "get_netdump_panic_task: NT_TASKSTRUCT: %lx\n", 
				nd->task_struct);
		return nd->task_struct;
	}

        switch (DUMPFILE_FORMAT(nd->flags))
        {
        case NETDUMP_ELF32:
        case NETDUMP_ELF64:
		crashing_cpu = -1;
		break;

        case KDUMP_ELF32:
        case KDUMP_ELF64:
		crashing_cpu = -1;
		if (kernel_symbol_exists("crashing_cpu")) {
			get_symbol_data("crashing_cpu", sizeof(int), &i);
			if ((i >= 0) && in_cpu_map(ONLINE_MAP, i)) {
				crashing_cpu = i;
				if (CRASHDEBUG(1))
					error(INFO, 
		  "get_netdump_panic_task: active_set[crashing_cpu: %d]: %lx\n",
						crashing_cpu,
						tt->active_set[crashing_cpu]);
			}
		}

		if ((nd->num_prstatus_notes > 1) && (crashing_cpu == -1))
			goto panic_task_undetermined;
		break;

	default:
		crashing_cpu = -1;
		break;
	}

        if (nd->elf32 && (nd->elf32->e_machine == EM_386)) {
		Elf32_Nhdr *note32 = NULL;

                if (nd->num_prstatus_notes > 1) {
			if (crashing_cpu != -1)
				note32 = (Elf32_Nhdr *)
					nd->nt_prstatus_percpu[crashing_cpu];
                } else
                        note32 = (Elf32_Nhdr *)nd->nt_prstatus;

		if (!note32)
			goto panic_task_undetermined;

	        len = sizeof(Elf32_Nhdr);
	        len = roundup(len + note32->n_namesz, 4);
	        len = roundup(len + note32->n_descsz, 4);
		
		user_regs = ((char *)note32 + len)
			- SIZE(user_regs_struct) - sizeof(int);
		ebp = ULONG(user_regs + OFFSET(user_regs_struct_ebp));
		esp = ULONG(user_regs + OFFSET(user_regs_struct_esp));
check_ebp_esp:
		if (CRASHDEBUG(1)) 
			error(INFO, 
			    "get_netdump_panic_task: NT_PRSTATUS esp: %lx ebp: %lx\n",
				esp, ebp);
		if (IS_KVADDR(esp)) {
			task = stkptr_to_task(esp);
			if (CRASHDEBUG(1))
				error(INFO, 
			    "get_netdump_panic_task: esp: %lx -> task: %lx\n",
					esp, task);
			for (i = 0; task && (i < NR_CPUS); i++) {
				if (task == tt->active_set[i]) 
					return task;
			} 
		}
                if (IS_KVADDR(ebp)) {
                        task = stkptr_to_task(ebp);
			if (CRASHDEBUG(1))
				error(INFO, 
			    "get_netdump_panic_task: ebp: %lx -> task: %lx\n",
					ebp, task);
                        for (i = 0; task && (i < NR_CPUS); i++) {
                                if (task == tt->active_set[i]) 
                                      return task;
                        }
                }
	} else if (nd->elf64) {
		Elf64_Nhdr *note64 = NULL;

                if (nd->num_prstatus_notes > 1) {
			if (crashing_cpu != -1)
				note64 = (Elf64_Nhdr *)
					nd->nt_prstatus_percpu[crashing_cpu];
                } else
                        note64 = (Elf64_Nhdr *)nd->nt_prstatus;

		if (!note64)
			goto panic_task_undetermined;

	        len = sizeof(Elf64_Nhdr);
	        len = roundup(len + note64->n_namesz, 4);
		user_regs = (char *)((char *)note64 + len +
			MEMBER_OFFSET("elf_prstatus", "pr_reg"));

		if (nd->elf64->e_machine == EM_386) {
                	ebp = ULONG(user_regs + OFFSET(user_regs_struct_ebp));
                	esp = ULONG(user_regs + OFFSET(user_regs_struct_esp));
			goto check_ebp_esp;
		}

		if (nd->elf64->e_machine == EM_PPC64) {
			/*
			 * Get the GPR1 register value.
			 */
			esp = *(ulong *)((char *)user_regs + 8);
			if (CRASHDEBUG(1)) 
				error(INFO, 
			    	"get_netdump_panic_task: NT_PRSTATUS esp: %lx\n", esp);
			if (IS_KVADDR(esp)) {
				task = stkptr_to_task(esp);
				if (CRASHDEBUG(1))
					error(INFO, 
			    		"get_netdump_panic_task: esp: %lx -> task: %lx\n",
						esp, task);
				for (i = 0; task && (i < NR_CPUS); i++) {
					if (task == tt->active_set[i]) 
						return task;
				}
			}
		}

		if (nd->elf64->e_machine == EM_X86_64) {
			if ((crashing_cpu != -1) && (crashing_cpu <= kt->cpus))
				return (tt->active_set[crashing_cpu]);
		}
	} 

panic_task_undetermined:

	if (CRASHDEBUG(1))
		error(INFO, "get_netdump_panic_task: failed\n");

	return NO_TASK;
#endif
}

/*
 *  Get the switch_stack address of the passed-in task.  Currently only
 *  the panicking task reports its switch-stack address.
 */
ulong 
get_netdump_switch_stack(ulong task)
{
#ifdef DAEMON
	if (nd->task_struct == task)
		return nd->switch_stack;
	return 0;
#else
	if (!VMCORE_VALID() || !get_active_set())
		return 0;

	if (nd->task_struct == task)
		return nd->switch_stack;

	return 0;
#endif
}

int
netdump_memory_dump(FILE *fp)
{
	int i, others, wrap, flen;
	size_t len, tot;
	FILE *fpsave;
	Elf32_Off offset32;
	Elf32_Off offset64;
	struct pt_load_segment *pls;

	if (!VMCORE_VALID())
		return FALSE;

	fpsave = nd->ofp;
	nd->ofp = fp;

	if (FLAT_FORMAT())
		dump_flat_header(nd->ofp);

	netdump_print("vmcore_data: \n");
	netdump_print("                  flags: %lx (", nd->flags);
	others = 0;
	if (nd->flags & NETDUMP_LOCAL)
		netdump_print("%sNETDUMP_LOCAL", others++ ? "|" : "");
	if (nd->flags & KDUMP_LOCAL)
		netdump_print("%sKDUMP_LOCAL", others++ ? "|" : "");
	if (nd->flags & NETDUMP_REMOTE)
		netdump_print("%sNETDUMP_REMOTE", others++ ? "|" : "");
	if (nd->flags & NETDUMP_ELF32)
		netdump_print("%sNETDUMP_ELF32", others++ ? "|" : "");
	if (nd->flags & NETDUMP_ELF64)
		netdump_print("%sNETDUMP_ELF64", others++ ? "|" : "");
	if (nd->flags & KDUMP_ELF32)
		netdump_print("%sKDUMP_ELF32", others++ ? "|" : "");
	if (nd->flags & KDUMP_ELF64)
		netdump_print("%sKDUMP_ELF64", others++ ? "|" : "");
	if (nd->flags & PARTIAL_DUMP)
		netdump_print("%sPARTIAL_DUMP", others++ ? "|" : "");
	if (nd->flags & QEMU_MEM_DUMP_KDUMP_BACKUP)
		netdump_print("%sQEMU_MEM_DUMP_KDUMP_BACKUP", others++ ? "|" : "");
	netdump_print(") %s\n", FLAT_FORMAT() ? "[FLAT]" : "");
	if ((pc->flags & RUNTIME) && symbol_exists("dump_level")) {
		int dump_level;
                if (readmem(symbol_value("dump_level"), KVADDR, &dump_level,
                    sizeof(dump_level), "dump_level", QUIET|RETURN_ON_ERROR)) {
			netdump_print("             dump_level: %d (0x%x) %s", 
				dump_level, dump_level, 
				dump_level > 0 ? "(" : "");

#define DUMP_EXCLUDE_CACHE 0x00000001   /* Exclude LRU & SwapCache pages*/
#define DUMP_EXCLUDE_CLEAN 0x00000002   /* Exclude all-zero pages */
#define DUMP_EXCLUDE_FREE  0x00000004   /* Exclude free pages */
#define DUMP_EXCLUDE_ANON  0x00000008   /* Exclude Anon pages */
#define DUMP_SAVE_PRIVATE  0x00000010   /* Save private pages */

		        others = 0;
        		if (dump_level & DUMP_EXCLUDE_CACHE)
                		netdump_print("%sDUMP_EXCLUDE_CACHE", 
					others++ ? "|" : "");
        		if (dump_level & DUMP_EXCLUDE_CLEAN)
                		netdump_print("%sDUMP_EXCLUDE_CLEAN", 
					others++ ? "|" : "");
        		if (dump_level & DUMP_EXCLUDE_FREE)
                		netdump_print("%sDUMP_EXCLUDE_FREE", 
					others++ ? "|" : "");
        		if (dump_level & DUMP_EXCLUDE_ANON)
                		netdump_print("%sDUMP_EXCLUDE_ANON", 
					others++ ? "|" : "");
        		if (dump_level & DUMP_SAVE_PRIVATE)
                		netdump_print("%sDUMP_SAVE_PRIVATE", 
					others++ ? "|" : "");
			netdump_print("%s\n", dump_level > 0 ? ")" : "");
		} else
			netdump_print("             dump_level: (unknown)\n");
	} else if (!(pc->flags & RUNTIME) && symbol_exists("dump_level"))
		netdump_print("             dump_level: (undetermined)\n");

	netdump_print("                   ndfd: %d\n", nd->ndfd);
	netdump_print("                    ofp: %lx\n", nd->ofp);
	netdump_print("            header_size: %d\n", nd->header_size);
	netdump_print("   num_pt_load_segments: %d\n", nd->num_pt_load_segments);
	for (i = 0; i < nd->num_pt_load_segments; i++) {
		pls = &nd->pt_load_segments[i];
		netdump_print("     pt_load_segment[%d]:\n", i);
		netdump_print("            file_offset: %lx\n", 
			pls->file_offset);
		netdump_print("             phys_start: %llx\n", 
			pls->phys_start);
		netdump_print("               phys_end: %llx\n", 
			pls->phys_end);
		netdump_print("              zero_fill: %llx\n", 
			pls->zero_fill);
	}
	netdump_print("             elf_header: %lx\n", nd->elf_header);
	netdump_print("                  elf32: %lx\n", nd->elf32);
	netdump_print("                notes32: %lx\n", nd->notes32);
	netdump_print("                 load32: %lx\n", nd->load32);
	netdump_print("                  elf64: %lx\n", nd->elf64);
	netdump_print("                notes64: %lx\n", nd->notes64);
	netdump_print("                 load64: %lx\n", nd->load64);
	netdump_print("            nt_prstatus: %lx\n", nd->nt_prstatus);
	netdump_print("            nt_prpsinfo: %lx\n", nd->nt_prpsinfo);
	netdump_print("          nt_taskstruct: %lx\n", nd->nt_taskstruct);
	netdump_print("            task_struct: %lx\n", nd->task_struct);
	netdump_print("              page_size: %d\n", nd->page_size);
	netdump_print("           switch_stack: %lx\n", nd->switch_stack);
	netdump_print("         xen_kdump_data: %s\n",
		XEN_CORE_DUMPFILE() ? " " : "(unused)");
	if (XEN_CORE_DUMPFILE()) {
		netdump_print("                    flags: %lx (", nd->xen_kdump_data->flags);
		others = 0;
        	if (nd->xen_kdump_data->flags & KDUMP_P2M_INIT)
                	netdump_print("%sKDUMP_P2M_INIT", others++ ? "|" : "");
        	if (nd->xen_kdump_data->flags & KDUMP_CR3)
                	netdump_print("%sKDUMP_CR3", others++ ? "|" : "");
        	if (nd->xen_kdump_data->flags & KDUMP_MFN_LIST)
                	netdump_print("%sKDUMP_MFN_LIST", others++ ? "|" : "");
		netdump_print(")\n");
		netdump_print("                  p2m_mfn: %lx\n", 
			nd->xen_kdump_data->p2m_mfn);
		netdump_print("                      cr3: %lx\n", 
			nd->xen_kdump_data->cr3);
		netdump_print("            last_mfn_read: %lx\n", 
			nd->xen_kdump_data->last_mfn_read);
		netdump_print("            last_pmd_read: %lx\n", 
			nd->xen_kdump_data->last_pmd_read);
		netdump_print("                     page: %lx\n", 
			nd->xen_kdump_data->page);
		netdump_print("                 accesses: %ld\n", 
			nd->xen_kdump_data->accesses);
		netdump_print("               cache_hits: %ld ", 
			nd->xen_kdump_data->cache_hits);
      		if (nd->xen_kdump_data->accesses)
                	netdump_print("(%ld%%)", 
			    nd->xen_kdump_data->cache_hits * 100 / nd->xen_kdump_data->accesses);
		netdump_print("\n               p2m_frames: %d\n", 
			nd->xen_kdump_data->p2m_frames);
		netdump_print("           xen_phys_start: %lx\n", 
			nd->xen_kdump_data->xen_phys_start);
		netdump_print("        xen_major_version: %d\n", 
			nd->xen_kdump_data->xen_major_version);
		netdump_print("        xen_minor_version: %d\n", 
			nd->xen_kdump_data->xen_minor_version);
		netdump_print("       p2m_mfn_frame_list: %lx\n", 
			nd->xen_kdump_data->p2m_mfn_frame_list);
		for (i = 0; i < nd->xen_kdump_data->p2m_frames; i++)
			netdump_print("%lx ", 
				nd->xen_kdump_data->p2m_mfn_frame_list[i]);
		if (i) netdump_print("\n");
	}
	netdump_print("     num_prstatus_notes: %d\n", nd->num_prstatus_notes);
	netdump_print("         num_qemu_notes: %d\n", nd->num_qemu_notes);
	netdump_print("             vmcoreinfo: %lx\n", (ulong)nd->vmcoreinfo);
	netdump_print("        size_vmcoreinfo: %d\n", nd->size_vmcoreinfo);
	netdump_print("     nt_prstatus_percpu: ");
        wrap = sizeof(void *) == SIZEOF_32BIT ? 8 : 4;
        flen = sizeof(void *) == SIZEOF_32BIT ? 8 : 16;
	if (nd->num_prstatus_notes == 1)
                netdump_print("%.*lx\n", flen, nd->nt_prstatus_percpu[0]);
	else {
        	for (i = 0; i < nd->num_prstatus_notes; i++) {
                	if ((i % wrap) == 0)
                        	netdump_print("\n        ");
                	netdump_print("%.*lx ", flen, 
				nd->nt_prstatus_percpu[i]);
        	}
	}
	netdump_print("\n");
	netdump_print("         nt_qemu_percpu: ");
	if (nd->num_qemu_notes == 1)
		netdump_print("%.*lx\n", flen, nd->nt_qemu_percpu[0]);
	else {
	       	for (i = 0; i < nd->num_qemu_notes; i++) {
                	if ((i % wrap) == 0)
                        	netdump_print("\n        ");
                	netdump_print("%.*lx ", flen, 
				nd->nt_qemu_percpu[i]);
        	}
	}
	netdump_print("\n");
	netdump_print("       backup_src_start: %llx\n", nd->backup_src_start);
	netdump_print("        backup_src_size: %lx\n", nd->backup_src_size);
	netdump_print("          backup_offset: %llx\n", nd->backup_offset);
	netdump_print("\n");

        switch (DUMPFILE_FORMAT(nd->flags))
	{
	case NETDUMP_ELF32:
	case KDUMP_ELF32:
		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, ELFREAD);
        	offset32 = nd->notes32->p_offset;
        	for (tot = 0; tot < nd->notes32->p_filesz; tot += len) {
                	if (!(len = dump_Elf32_Nhdr(offset32, ELFREAD)))
				break;
			offset32 += len;
        	}
		break;

	case NETDUMP_ELF64:
	case KDUMP_ELF64:
		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, ELFREAD);
        	offset64 = nd->notes64->p_offset;
        	for (tot = 0; tot < nd->notes64->p_filesz; tot += len) {
                	if (!(len = dump_Elf64_Nhdr(offset64, ELFREAD)))
				break;
                	offset64 += len;
        	}
		break;
	}

	dump_ramdump_data();

	nd->ofp = fpsave;
        return TRUE;
}

/* 
 *  Dump an ELF file header.
 */
static void 
dump_Elf32_Ehdr(Elf32_Ehdr *elf)
{
	char buf[BUFSIZE];

	BZERO(buf, BUFSIZE);
	BCOPY(elf->e_ident, buf, SELFMAG); 
	netdump_print("Elf32_Ehdr:\n");
	netdump_print("                e_ident: \\%o%s\n", buf[0], 
		&buf[1]);
	netdump_print("      e_ident[EI_CLASS]: %d ", elf->e_ident[EI_CLASS]);
	switch (elf->e_ident[EI_CLASS])
	{
	case ELFCLASSNONE:
		netdump_print("(ELFCLASSNONE)");
		break;
	case ELFCLASS32:
		netdump_print("(ELFCLASS32)\n");
		break;
	case ELFCLASS64:
		netdump_print("(ELFCLASS64)\n");
		break;
	case ELFCLASSNUM:
		netdump_print("(ELFCLASSNUM)\n");
		break;
	default:
		netdump_print("(?)\n");
		break;
	}
	netdump_print("       e_ident[EI_DATA]: %d ", elf->e_ident[EI_DATA]);
	switch (elf->e_ident[EI_DATA])
	{
	case ELFDATANONE:
		netdump_print("(ELFDATANONE)\n");
		break;
	case ELFDATA2LSB: 
		netdump_print("(ELFDATA2LSB)\n");
		break;
	case ELFDATA2MSB:
		netdump_print("(ELFDATA2MSB)\n");
		break;
	case ELFDATANUM:
		netdump_print("(ELFDATANUM)\n");
		break;
        default:
                netdump_print("(?)\n");
	}
	netdump_print("    e_ident[EI_VERSION]: %d ", 
		elf->e_ident[EI_VERSION]);
	if (elf->e_ident[EI_VERSION] == EV_CURRENT)
		netdump_print("(EV_CURRENT)\n");
	else
		netdump_print("(?)\n");
	netdump_print("      e_ident[EI_OSABI]: %d ", elf->e_ident[EI_OSABI]);
	switch (elf->e_ident[EI_OSABI])
	{
	case ELFOSABI_SYSV:   
		netdump_print("(ELFOSABI_SYSV)\n");
		break;
	case ELFOSABI_HPUX:    
		netdump_print("(ELFOSABI_HPUX)\n");
		break;
	case ELFOSABI_ARM:      
		netdump_print("(ELFOSABI_ARM)\n");
		break;
	case ELFOSABI_STANDALONE:
		netdump_print("(ELFOSABI_STANDALONE)\n");
		break;
	case ELFOSABI_LINUX:
		netdump_print("(ELFOSABI_LINUX)\n");
		break;
        default:
                netdump_print("(?)\n");
	}
	netdump_print(" e_ident[EI_ABIVERSION]: %d\n", 
		elf->e_ident[EI_ABIVERSION]);

	netdump_print("                 e_type: %d ", elf->e_type);
	switch (elf->e_type)
	{
	case ET_NONE:
		netdump_print("(ET_NONE)\n");
		break;
	case ET_REL:
		netdump_print("(ET_REL)\n");
		break;
	case ET_EXEC:
		netdump_print("(ET_EXEC)\n");
		break;
	case ET_DYN:
		netdump_print("(ET_DYN)\n");
		break;
	case ET_CORE:
		netdump_print("(ET_CORE)\n");
		break;
	case ET_NUM:
		netdump_print("(ET_NUM)\n");
		break;
	case ET_LOOS:
		netdump_print("(ET_LOOS)\n");
		break;
	case ET_HIOS:
		netdump_print("(ET_HIOS)\n");
		break;
	case ET_LOPROC:
		netdump_print("(ET_LOPROC)\n");
		break;
	case ET_HIPROC:
		netdump_print("(ET_HIPROC)\n");
		break;
	default:
		netdump_print("(?)\n");
	}

        netdump_print("              e_machine: %d ", elf->e_machine);
	switch (elf->e_machine) 
	{
	case EM_ARM:
		netdump_print("(EM_ARM)\n");
		break;
	case EM_386:
		netdump_print("(EM_386)\n");
		break;
	default:
		netdump_print("(unsupported)\n");
		break;
	}

        netdump_print("              e_version: %ld ", elf->e_version);
	netdump_print("%s\n", elf->e_version == EV_CURRENT ? 
		"(EV_CURRENT)" : "");

        netdump_print("                e_entry: %lx\n", elf->e_entry);
        netdump_print("                e_phoff: %lx\n", elf->e_phoff);
        netdump_print("                e_shoff: %lx\n", elf->e_shoff);
        netdump_print("                e_flags: %lx\n", elf->e_flags);
        netdump_print("               e_ehsize: %x\n", elf->e_ehsize);
        netdump_print("            e_phentsize: %x\n", elf->e_phentsize);
        netdump_print("                e_phnum: %x\n", elf->e_phnum);
        netdump_print("            e_shentsize: %x\n", elf->e_shentsize);
        netdump_print("                e_shnum: %x\n", elf->e_shnum);
        netdump_print("             e_shstrndx: %x\n", elf->e_shstrndx);
}

static void 
dump_Elf64_Ehdr(Elf64_Ehdr *elf)
{
	char buf[BUFSIZE];

	BZERO(buf, BUFSIZE);
	BCOPY(elf->e_ident, buf, SELFMAG); 
	netdump_print("Elf64_Ehdr:\n");
	netdump_print("                e_ident: \\%o%s\n", buf[0], 
		&buf[1]);
	netdump_print("      e_ident[EI_CLASS]: %d ", elf->e_ident[EI_CLASS]);
	switch (elf->e_ident[EI_CLASS])
	{
	case ELFCLASSNONE:
		netdump_print("(ELFCLASSNONE)");
		break;
	case ELFCLASS32:
		netdump_print("(ELFCLASS32)\n");
		break;
	case ELFCLASS64:
		netdump_print("(ELFCLASS64)\n");
		break;
	case ELFCLASSNUM:
		netdump_print("(ELFCLASSNUM)\n");
		break;
	default:
		netdump_print("(?)\n");
		break;
	}
	netdump_print("       e_ident[EI_DATA]: %d ", elf->e_ident[EI_DATA]);
	switch (elf->e_ident[EI_DATA])
	{
	case ELFDATANONE:
		netdump_print("(ELFDATANONE)\n");
		break;
	case ELFDATA2LSB: 
		netdump_print("(ELFDATA2LSB)\n");
		break;
	case ELFDATA2MSB:
		netdump_print("(ELFDATA2MSB)\n");
		break;
	case ELFDATANUM:
		netdump_print("(ELFDATANUM)\n");
		break;
        default:
                netdump_print("(?)\n");
	}
	netdump_print("    e_ident[EI_VERSION]: %d ", 
		elf->e_ident[EI_VERSION]);
	if (elf->e_ident[EI_VERSION] == EV_CURRENT)
		netdump_print("(EV_CURRENT)\n");
	else
		netdump_print("(?)\n");
	netdump_print("      e_ident[EI_OSABI]: %d ", elf->e_ident[EI_OSABI]);
	switch (elf->e_ident[EI_OSABI])
	{
	case ELFOSABI_SYSV:   
		netdump_print("(ELFOSABI_SYSV)\n");
		break;
	case ELFOSABI_HPUX:    
		netdump_print("(ELFOSABI_HPUX)\n");
		break;
	case ELFOSABI_ARM:      
		netdump_print("(ELFOSABI_ARM)\n");
		break;
	case ELFOSABI_STANDALONE:
		netdump_print("(ELFOSABI_STANDALONE)\n");
		break;
	case ELFOSABI_LINUX:
		netdump_print("(ELFOSABI_LINUX)\n");
		break;
        default:
                netdump_print("(?)\n");
	}
	netdump_print(" e_ident[EI_ABIVERSION]: %d\n", 
		elf->e_ident[EI_ABIVERSION]);

	netdump_print("                 e_type: %d ", elf->e_type);
	switch (elf->e_type)
	{
	case ET_NONE:
		netdump_print("(ET_NONE)\n");
		break;
	case ET_REL:
		netdump_print("(ET_REL)\n");
		break;
	case ET_EXEC:
		netdump_print("(ET_EXEC)\n");
		break;
	case ET_DYN:
		netdump_print("(ET_DYN)\n");
		break;
	case ET_CORE:
		netdump_print("(ET_CORE)\n");
		break;
	case ET_NUM:
		netdump_print("(ET_NUM)\n");
		break;
	case ET_LOOS:
		netdump_print("(ET_LOOS)\n");
		break;
	case ET_HIOS:
		netdump_print("(ET_HIOS)\n");
		break;
	case ET_LOPROC:
		netdump_print("(ET_LOPROC)\n");
		break;
	case ET_HIPROC:
		netdump_print("(ET_HIPROC)\n");
		break;
	default:
		netdump_print("(?)\n");
	}

        netdump_print("              e_machine: %d ", elf->e_machine);
        switch (elf->e_machine)
        {
	case EM_386:
		netdump_print("(EM_386)\n");
		break;
        case EM_IA_64:
                netdump_print("(EM_IA_64)\n");
                break;
        case EM_PPC64:
                netdump_print("(EM_PPC64)\n");
                break;
        case EM_X86_64:
                netdump_print("(EM_X86_64)\n");
                break;
	case EM_S390:
                netdump_print("(EM_S390)\n");
                break;
	case EM_ARM:
                netdump_print("(EM_ARM)\n");
                break;
	case EM_AARCH64:
                netdump_print("(EM_AARCH64)\n");
                break;
        default:
                netdump_print("(unsupported)\n");
                break;
        }

        netdump_print("              e_version: %ld ", elf->e_version);
	netdump_print("%s\n", elf->e_version == EV_CURRENT ? 
		"(EV_CURRENT)" : "");

        netdump_print("                e_entry: %lx\n", elf->e_entry);
        netdump_print("                e_phoff: %lx\n", elf->e_phoff);
        netdump_print("                e_shoff: %lx\n", elf->e_shoff);
        netdump_print("                e_flags: %lx\n", elf->e_flags);
        netdump_print("               e_ehsize: %x\n", elf->e_ehsize);
        netdump_print("            e_phentsize: %x\n", elf->e_phentsize);
        netdump_print("                e_phnum: %x\n", elf->e_phnum);
        netdump_print("            e_shentsize: %x\n", elf->e_shentsize);
        netdump_print("                e_shnum: %x\n", elf->e_shnum);
        netdump_print("             e_shstrndx: %x\n", elf->e_shstrndx);
}

/*
 *  Dump a program segment header 
 */
static void
dump_Elf32_Phdr(Elf32_Phdr *prog, int store_pt_load_data)
{
	int others;
	struct pt_load_segment *pls;

        if ((char *)prog > (nd->elf_header + nd->header_size))
		error(FATAL,
		    "Elf32_Phdr pointer: %lx  ELF header end: %lx\n\n",
			(char *)prog, nd->elf_header + nd->header_size);

	if (store_pt_load_data) 
		pls = &nd->pt_load_segments[store_pt_load_data-1];
	else
		pls = NULL;

	netdump_print("Elf32_Phdr:\n");
	netdump_print("                 p_type: %lx ", prog->p_type);
	switch (prog->p_type)
	{
	case PT_NULL: 
		netdump_print("(PT_NULL)\n");
		break;
	case PT_LOAD:
		netdump_print("(PT_LOAD)\n");
		break;
	case PT_DYNAMIC: 
		netdump_print("(PT_DYNAMIC)\n");
		break;
	case PT_INTERP: 
		netdump_print("(PT_INTERP)\n");
		break;
	case PT_NOTE:  
		netdump_print("(PT_NOTE)\n");
		break;
	case PT_SHLIB: 
		netdump_print("(PT_SHLIB)\n");
		break;
	case PT_PHDR:  
		netdump_print("(PT_PHDR)\n");
		break;
	case PT_NUM:
		netdump_print("(PT_NUM)\n");
		break;
	case PT_LOOS:
		netdump_print("(PT_LOOS)\n");
		break;
	case PT_HIOS:
		netdump_print("(PT_HIOS)\n");
		break;
	case PT_LOPROC:
		netdump_print("(PT_LOPROC)\n");
		break;
	case PT_HIPROC:
		netdump_print("(PT_HIPROC)\n");
		break;
	default:
		netdump_print("(?)\n");
	}

	netdump_print("               p_offset: %ld (%lx)\n", prog->p_offset, 
		prog->p_offset);
	if (store_pt_load_data)
		pls->file_offset = prog->p_offset;
	netdump_print("                p_vaddr: %lx\n", prog->p_vaddr);
	netdump_print("                p_paddr: %lx\n", prog->p_paddr);
	if (store_pt_load_data)
		pls->phys_start = prog->p_paddr; 
	netdump_print("               p_filesz: %lu (%lx)\n", prog->p_filesz, 
		prog->p_filesz);
	if (store_pt_load_data) {
		pls->phys_end = pls->phys_start + prog->p_filesz;
		pls->zero_fill = (prog->p_filesz == prog->p_memsz) ? 
			0 : pls->phys_start + prog->p_memsz;
	}
	netdump_print("                p_memsz: %lu (%lx)\n", prog->p_memsz,
		prog->p_memsz);
	netdump_print("                p_flags: %lx (", prog->p_flags);
	others = 0;
	if (prog->p_flags & PF_X)
		netdump_print("PF_X", others++);
	if (prog->p_flags & PF_W)
		netdump_print("%sPF_W", others++ ? "|" : "");
	if (prog->p_flags & PF_R)
		netdump_print("%sPF_R", others++ ? "|" : "");
	netdump_print(")\n");
	netdump_print("                p_align: %ld\n", prog->p_align);
}

static void 
dump_Elf64_Phdr(Elf64_Phdr *prog, int store_pt_load_data)
{
	int others;
	struct pt_load_segment *pls;

	if (store_pt_load_data)
		pls = &nd->pt_load_segments[store_pt_load_data-1];
	else
		pls = NULL;

        if ((char *)prog > (nd->elf_header + nd->header_size))
		error(FATAL,
		    "Elf64_Phdr pointer: %lx  ELF header end: %lx\n\n",
			(char *)prog, nd->elf_header + nd->header_size);

	netdump_print("Elf64_Phdr:\n");
	netdump_print("                 p_type: %lx ", prog->p_type);
	switch (prog->p_type)
	{
	case PT_NULL: 
		netdump_print("(PT_NULL)\n");
		break;
	case PT_LOAD:
		netdump_print("(PT_LOAD)\n");
		break;
	case PT_DYNAMIC: 
		netdump_print("(PT_DYNAMIC)\n");
		break;
	case PT_INTERP: 
		netdump_print("(PT_INTERP)\n");
		break;
	case PT_NOTE:  
		netdump_print("(PT_NOTE)\n");
		break;
	case PT_SHLIB: 
		netdump_print("(PT_SHLIB)\n");
		break;
	case PT_PHDR:  
		netdump_print("(PT_PHDR)\n");
		break;
	case PT_NUM:
		netdump_print("(PT_NUM)\n");
		break;
	case PT_LOOS:
		netdump_print("(PT_LOOS)\n");
		break;
	case PT_HIOS:
		netdump_print("(PT_HIOS)\n");
		break;
	case PT_LOPROC:
		netdump_print("(PT_LOPROC)\n");
		break;
	case PT_HIPROC:
		netdump_print("(PT_HIPROC)\n");
		break;
	default:
		netdump_print("(?)\n");
	}

	netdump_print("               p_offset: %lld (%llx)\n", prog->p_offset, 
		prog->p_offset);
	if (store_pt_load_data)
		pls->file_offset = prog->p_offset;
	netdump_print("                p_vaddr: %llx\n", prog->p_vaddr);
	netdump_print("                p_paddr: %llx\n", prog->p_paddr);
	if (store_pt_load_data)
		pls->phys_start = prog->p_paddr; 
	netdump_print("               p_filesz: %llu (%llx)\n", prog->p_filesz, 
		prog->p_filesz);
	if (store_pt_load_data) {
		pls->phys_end = pls->phys_start + prog->p_filesz;
		pls->zero_fill = (prog->p_filesz == prog->p_memsz) ?
			0 : pls->phys_start + prog->p_memsz;
	}
	netdump_print("                p_memsz: %llu (%llx)\n", prog->p_memsz,
		prog->p_memsz);
	netdump_print("                p_flags: %lx (", prog->p_flags);
	others = 0;
	if (prog->p_flags & PF_X)
		netdump_print("PF_X", others++);
	if (prog->p_flags & PF_W)
		netdump_print("%sPF_W", others++ ? "|" : "");
	if (prog->p_flags & PF_R)
		netdump_print("%sPF_R", others++ ? "|" : "");
	netdump_print(")\n");
	netdump_print("                p_align: %lld\n", prog->p_align);
}

/*
 * VMCOREINFO
 *
 * This is a ELF note intented for makedumpfile that is exported by the
 * kernel that crashes and presented as ELF note to the /proc/vmcore
 * of the panic kernel.
 */

#define VMCOREINFO_NOTE_NAME        "VMCOREINFO"
#define VMCOREINFO_NOTE_NAME_BYTES  (sizeof(VMCOREINFO_NOTE_NAME))

/*
 * Reads a string value from VMCOREINFO.
 *
 * Returns a string (that has to be freed by the caller) that contains the
 * value for key or NULL if the key has not been found.
 */
static char *
vmcoreinfo_read_string(const char *key)
{
	int i, j, end;
	size_t value_length;
	size_t key_length = strlen(key);
	char *vmcoreinfo = (char *)nd->vmcoreinfo;
	char *value = NULL;

	if (!nd->vmcoreinfo)
		return NULL;

	/* the '+ 1' is the equal sign */
	for (i = 0; i < (nd->size_vmcoreinfo - key_length + 1); i++) {
		/*
		 * We must also check if we're at the beginning of VMCOREINFO
		 * or the separating newline is there, and of course if we 
		 * have a equal sign after the key.
		 */
		if ((strncmp(vmcoreinfo+i, key, key_length) == 0) &&
		    (i == 0 || vmcoreinfo[i-1] == '\n') &&
		    (vmcoreinfo[i+key_length] == '=')) {

			end = -1;

			/* Found -- search for the next newline. */
			for (j = i + key_length + 1; 
			     j < nd->size_vmcoreinfo; j++) {
				if (vmcoreinfo[j] == '\n') {
					end = j;
					break;
				}
			}

			/* 
			 * If we didn't find an end, we assume it's the end 
			 * of VMCOREINFO data. 
			 */
			if (end == -1) {
				/* Point after the end. */
				end = nd->size_vmcoreinfo + 1;
			}

			value_length = end - (1+ i + key_length);
			value = calloc(value_length+1, sizeof(char));
			if (value)
				strncpy(value, vmcoreinfo + i + key_length + 1, 
					value_length);
			break;
		}
	}

	return value;
}

/*
 * Reads an integer value from VMCOREINFO.
 */
static long
vmcoreinfo_read_integer(const char *key, long default_value)
{
	char *string;
	long retval = default_value;

	string = vmcoreinfo_read_string(key);
	if (string) {
		retval = atol(string);
		free(string);
	}

	return retval;
}

/*
 *  Dump a note section header -- the actual data is defined by netdump
 */

static size_t 
dump_Elf32_Nhdr(Elf32_Off offset, int store)
{
	int i, lf, words;
	Elf32_Nhdr *note;
	size_t len;
	char buf[BUFSIZE];
	char *ptr;
	ulong *uptr;
	int xen_core, vmcoreinfo, eraseinfo, qemuinfo;
	uint64_t remaining, notesize;

	note = (Elf32_Nhdr *)((char *)nd->elf32 + offset);

        BZERO(buf, BUFSIZE);
	xen_core = vmcoreinfo = eraseinfo = qemuinfo = FALSE;
        ptr = (char *)note + sizeof(Elf32_Nhdr);

	if (ptr > (nd->elf_header + nd->header_size)) {
		error(WARNING, 
	    	    "Elf32_Nhdr pointer: %lx ELF header end: %lx\n",
			(char *)note, nd->elf_header + nd->header_size);
		return 0;
	} else
		remaining = (uint64_t)((nd->elf_header + nd->header_size) - ptr);

	notesize = (uint64_t)note->n_namesz + (uint64_t)note->n_descsz;

	if ((note->n_namesz == 0) || !remaining || (notesize > remaining)) {
		error(WARNING, 
		    "possibly corrupt Elf32_Nhdr: "
		    "n_namesz: %ld n_descsz: %ld n_type: %lx\n%s",
			note->n_namesz, note->n_descsz, note->n_type,
			note->n_namesz || note->n_descsz || !remaining ? 
			"\n" : "");
		if (note->n_namesz || note->n_descsz || !remaining)
			return 0;
	}

        netdump_print("Elf32_Nhdr:\n");
        netdump_print("               n_namesz: %ld ", note->n_namesz);

        BCOPY(ptr, buf, note->n_namesz);
        netdump_print("(\"%s\")\n", buf);

        netdump_print("               n_descsz: %ld\n", note->n_descsz);
        netdump_print("                 n_type: %lx ", note->n_type);
	switch (note->n_type)
	{
	case NT_PRSTATUS:
		netdump_print("(NT_PRSTATUS)\n");
		if (store) { 
			if (!nd->nt_prstatus)
				nd->nt_prstatus = (void *)note;
			for (i = 0; i < NR_CPUS; i++) {
				if (!nd->nt_prstatus_percpu[i]) {
					nd->nt_prstatus_percpu[i] = (void *)note;
					nd->num_prstatus_notes++;
					break;
				}
			}
		}
		if (machine_type("PPC") && (nd->num_prstatus_notes > 0))
			pc->flags2 |= ELF_NOTES;
		break;
	case NT_PRPSINFO:
		netdump_print("(NT_PRPSINFO)\n");
		if (store)
			nd->nt_prpsinfo = (void *)note;
		break;
	case NT_TASKSTRUCT:
		netdump_print("(NT_TASKSTRUCT)\n");
		if (store) {
			nd->nt_taskstruct = (void *)note;
			nd->task_struct = *((ulong *)(ptr + note->n_namesz));
			nd->switch_stack = *((ulong *)
				(ptr + note->n_namesz + sizeof(ulong)));
		}
		break;
        case NT_DISKDUMP:
                netdump_print("(NT_DISKDUMP)\n");
		uptr = (ulong *)(ptr + note->n_namesz);
		if (*uptr && store)
			nd->flags |= PARTIAL_DUMP;
		break;
#ifdef NOTDEF
	/*
	 *  Note: Based upon the original, abandoned, proposal for
	 *  its contents -- keep around for potential future use.
	 */
	case NT_KDUMPINFO:
		netdump_print("(NT_KDUMPINFO)\n");
		if (store) {
			uptr = (note->n_namesz == 5) ?
				(ulong *)(ptr + ((note->n_namesz + 3) & ~3)) :
				(ulong *)(ptr + note->n_namesz);
			nd->page_size = (uint)(1 << *uptr);
			uptr++;
			nd->task_struct = *uptr;
		}
		break;
#endif
	default:
		xen_core = STRNEQ(buf, "XEN CORE") || STRNEQ(buf, "Xen");
		vmcoreinfo = STRNEQ(buf, "VMCOREINFO");
		eraseinfo = STRNEQ(buf, "ERASEINFO");
		qemuinfo = STRNEQ(buf, "QEMU");
		if (xen_core) {
			netdump_print("(unknown Xen n_type)\n"); 
			if (store)
				error(WARNING, "unknown Xen n_type: %lx\n\n", 
					note->n_type);
		} else if (vmcoreinfo) {
			netdump_print("(unused)\n");
			nd->vmcoreinfo = (char *)(ptr + note->n_namesz + 1);
			nd->size_vmcoreinfo = note->n_descsz;
			if (READ_PAGESIZE_FROM_VMCOREINFO() && store)
				nd->page_size = (uint)
					vmcoreinfo_read_integer("PAGESIZE", 0);
			pc->flags2 |= VMCOREINFO;
		} else if (eraseinfo) {
			netdump_print("(unused)\n");
			if (note->n_descsz)
				pc->flags2 |= ERASEINFO_DATA;
		} else
			netdump_print("(?)\n");

		if (qemuinfo)
			pc->flags2 |= QEMU_MEM_DUMP;
		break;

	case NT_XEN_KDUMP_CR3: 
                netdump_print("(NT_XEN_KDUMP_CR3) [obsolete]\n");
		if (store)
			error(WARNING, 
			    "obsolete Xen n_type: %lx (NT_XEN_KDUMP_CR3)\n\n", 
				note->n_type);
		/* FALL THROUGH */

	case XEN_ELFNOTE_CRASH_INFO:
		/*
		 *  x86 and x86_64: p2m mfn appended to crash_xen_info_t structure
		 */
		if (note->n_type == XEN_ELFNOTE_CRASH_INFO)
                	netdump_print("(XEN_ELFNOTE_CRASH_INFO)\n");
		xen_core = TRUE;
		if (store) { 
			pc->flags |= XEN_CORE;
			nd->xen_kdump_data = &xen_kdump_data;
			nd->xen_kdump_data->last_mfn_read = UNINITIALIZED;
			nd->xen_kdump_data->last_pmd_read = UNINITIALIZED;

			if ((note->n_type == NT_XEN_KDUMP_CR3) &&
			    ((note->n_descsz/sizeof(ulong)) == 1)) {
				nd->xen_kdump_data->flags |= KDUMP_CR3;
				/*
				 *  Use the first cr3 found.
				 */
				if (!nd->xen_kdump_data->cr3) {
					uptr = (ulong *)(ptr + note->n_namesz);
					uptr = (ulong *)roundup((ulong)uptr, 4);
					nd->xen_kdump_data->cr3 = *uptr;
				}
			} else {
				nd->xen_kdump_data->flags |= KDUMP_MFN_LIST;
				uptr = (ulong *)(ptr + note->n_namesz);
				uptr = (ulong *)roundup((ulong)uptr, 4);
				words = note->n_descsz/sizeof(ulong);
				/*
				 *  If already set, overridden with --pfm_mfn
				 */
				if (!nd->xen_kdump_data->p2m_mfn)
					nd->xen_kdump_data->p2m_mfn = *(uptr+(words-1));
				if (words > 9 && !nd->xen_kdump_data->xen_phys_start)
					nd->xen_kdump_data->xen_phys_start = *(uptr+(words-2));
				nd->xen_kdump_data->xen_major_version = *uptr;
				nd->xen_kdump_data->xen_minor_version = *(uptr+1);
			}
		}
		break;

	case XEN_ELFNOTE_CRASH_REGS:
      		/* 
		 *  x86 and x86_64: cr0, cr2, cr3, cr4 
		 */
		xen_core = TRUE;	
               	netdump_print("(XEN_ELFNOTE_CRASH_REGS)\n");
		break;
	}

	uptr = (ulong *)(ptr + note->n_namesz);

	/*
	 * kdumps are off-by-1, because their n_namesz is 5 for "CORE".
 	 */
	if ((nd->flags & KDUMP_ELF32) && (note->n_namesz == 5))
		uptr = (ulong *)(ptr + ((note->n_namesz + 3) & ~3));

	if (xen_core)
		uptr = (ulong *)roundup((ulong)uptr, 4);

	if (store && qemuinfo) {
		for(i=0; i<NR_CPUS; i++) {
			if (!nd->nt_qemu_percpu[i]) {
				nd->nt_qemu_percpu[i] = (void *)uptr;
				nd->num_qemu_notes++;
				break;
			}
		}
	}

	if (vmcoreinfo || eraseinfo) {
                netdump_print("                         ");
                ptr += note->n_namesz + 1;
                for (i = 0; i < note->n_descsz; i++, ptr++) {
                        netdump_print("%c", *ptr);
                        if (*ptr == '\n')
                                netdump_print("                         ");
                }
                lf = 0;
	} else {
		for (i = lf = 0; i < note->n_descsz/sizeof(ulong); i++) {
			if (((i%4)==0)) {
				netdump_print("%s                         ", 
					i ? "\n" : "");
				lf++;
			} else
				lf = 0;
			netdump_print("%08lx ", *uptr++);
		}
	}
	if (!lf || (note->n_type == NT_TASKSTRUCT) ||
	    (note->n_type == NT_DISKDUMP) || xen_core)
		netdump_print("\n");

  	len = sizeof(Elf32_Nhdr);
  	len = roundup(len + note->n_namesz, 4);
  	len = roundup(len + note->n_descsz, 4);

	return len;
}


static size_t 
dump_Elf64_Nhdr(Elf64_Off offset, int store)
{
	int i, lf, words;
	Elf64_Nhdr *note;
	size_t len;
	char buf[BUFSIZE];
	char *ptr;
	ulonglong *uptr;
	int *iptr;
	ulong *up;
	int xen_core, vmcoreinfo, eraseinfo, qemuinfo;
	uint64_t remaining, notesize;

	note = (Elf64_Nhdr *)((char *)nd->elf64 + offset);

        BZERO(buf, BUFSIZE);
        ptr = (char *)note + sizeof(Elf64_Nhdr);
	xen_core = vmcoreinfo = eraseinfo = qemuinfo = FALSE;

	if (ptr > (nd->elf_header + nd->header_size)) {
		error(WARNING, 
	    	    "Elf64_Nhdr pointer: %lx  ELF header end: %lx\n\n",
			(char *)note, nd->elf_header + nd->header_size);
		return 0;
	} else
		remaining = (uint64_t)((nd->elf_header + nd->header_size) - ptr);

	notesize = (uint64_t)note->n_namesz + (uint64_t)note->n_descsz;

	if ((note->n_namesz == 0) || !remaining || (notesize > remaining)) {
		error(WARNING, 
		    "possibly corrupt Elf64_Nhdr: "
		    "n_namesz: %ld n_descsz: %ld n_type: %lx\n%s",
			note->n_namesz, note->n_descsz, note->n_type,
			note->n_namesz || note->n_descsz || !remaining ? 
			"\n" : "");
		if (note->n_namesz || note->n_descsz || !remaining)
			return 0;
	}

        netdump_print("Elf64_Nhdr:\n");
        netdump_print("               n_namesz: %ld ", note->n_namesz);

        BCOPY(ptr, buf, note->n_namesz);
        netdump_print("(\"%s\")\n", buf);

        netdump_print("               n_descsz: %ld\n", note->n_descsz);
        netdump_print("                 n_type: %lx ", note->n_type);
	switch (note->n_type)
	{
	case NT_PRSTATUS:
		netdump_print("(NT_PRSTATUS)\n");
		if (store) {
			if (!nd->nt_prstatus)
				nd->nt_prstatus = (void *)note;
			for (i = 0; i < NR_CPUS; i++) {
				if (!nd->nt_prstatus_percpu[i]) {
					nd->nt_prstatus_percpu[i] = (void *)note;
					nd->num_prstatus_notes++;
					break;
				}
			}
		}
		break;
	case NT_PRPSINFO:
		netdump_print("(NT_PRPSINFO)\n");
		if (store)
			nd->nt_prpsinfo = (void *)note;
		break;
	case NT_FPREGSET:
		netdump_print("(NT_FPREGSET)\n");
		break;
	case NT_S390_TIMER:
		netdump_print("(NT_S390_TIMER)\n");
		break;
	case NT_S390_TODCMP:
		netdump_print("(NT_S390_TODCMP)\n");
		break;
	case NT_S390_TODPREG:
		netdump_print("(NT_S390_TODPREG)\n");
		break;
	case NT_S390_CTRS:
		netdump_print("(NT_S390_CTRS)\n");
		break;
	case NT_S390_PREFIX:
		netdump_print("(NT_S390_PREFIX)\n");
		break;
	case NT_TASKSTRUCT:
		netdump_print("(NT_TASKSTRUCT)\n");
		if (STRNEQ(buf, "SNAP"))
			pc->flags2 |= LIVE_DUMP;
		if (store) {
			nd->nt_taskstruct = (void *)note;
			nd->task_struct = *((ulong *)(ptr + note->n_namesz));
                        nd->switch_stack = *((ulong *)
                                (ptr + note->n_namesz + sizeof(ulong)));
		}
		break;
        case NT_DISKDUMP:
                netdump_print("(NT_DISKDUMP)\n");
		iptr = (int *)(ptr + note->n_namesz);
		if (*iptr && store)
			nd->flags |= PARTIAL_DUMP;
		if (note->n_descsz < sizeof(ulonglong))
			netdump_print("                         %08x", *iptr);
		break;
#ifdef NOTDEF
	/*
	 *  Note: Based upon the original, abandoned, proposal for
	 *  its contents -- keep around for potential future use.
	 */
        case NT_KDUMPINFO:
                netdump_print("(NT_KDUMPINFO)\n");
		if (store) {
			uint32_t *u32ptr;

			if (nd->elf64->e_machine == EM_386) {
				u32ptr = (note->n_namesz == 5) ?
				    (uint *)(ptr + ((note->n_namesz + 3) & ~3)) :
	                            (uint *)(ptr + note->n_namesz);
				nd->page_size = 1 << *u32ptr;
				u32ptr++;
				nd->task_struct = *u32ptr;
			} else {
	                       	uptr = (note->n_namesz == 5) ?
				    (ulonglong *)(ptr + ((note->n_namesz + 3) & ~3)) :
	                            (ulonglong *)(ptr + note->n_namesz);
				nd->page_size = (uint)(1 << *uptr);
				uptr++;
				nd->task_struct = *uptr;
			}
		}
                break;
#endif
	default:
		xen_core = STRNEQ(buf, "XEN CORE") || STRNEQ(buf, "Xen");
		vmcoreinfo = STRNEQ(buf, "VMCOREINFO");
		eraseinfo = STRNEQ(buf, "ERASEINFO");
		qemuinfo = STRNEQ(buf, "QEMU");
                if (xen_core) {
                        netdump_print("(unknown Xen n_type)\n");
			if (store)
                        	error(WARNING, 
				    "unknown Xen n_type: %lx\n\n", note->n_type);
		} else if (vmcoreinfo) {
                        netdump_print("(unused)\n");

			nd->vmcoreinfo = (char *)nd->elf64 + offset +
				(sizeof(Elf64_Nhdr) +
				((note->n_namesz + 3) & ~3));
			nd->size_vmcoreinfo = note->n_descsz;

			if (READ_PAGESIZE_FROM_VMCOREINFO() && store)
				nd->page_size = (uint)
					vmcoreinfo_read_integer("PAGESIZE", 0);
			pc->flags2 |= VMCOREINFO;
		} else if (eraseinfo) {
			netdump_print("(unused)\n");
			if (note->n_descsz)
				pc->flags2 |= ERASEINFO_DATA;
                } else
                        netdump_print("(?)\n");

		if (qemuinfo)
			pc->flags2 |= QEMU_MEM_DUMP;
                break;

	case NT_XEN_KDUMP_CR3: 
                netdump_print("(NT_XEN_KDUMP_CR3) [obsolete]\n");
               	if (store)
                	error(WARNING,
                            "obsolete Xen n_type: %lx (NT_XEN_KDUMP_CR3)\n\n",
                                note->n_type);
		/* FALL THROUGH */

	case XEN_ELFNOTE_CRASH_INFO:
		/*
		 *  x86 and x86_64: p2m mfn appended to crash_xen_info_t structure
		 */
		if (note->n_type == XEN_ELFNOTE_CRASH_INFO)
                	netdump_print("(XEN_ELFNOTE_CRASH_INFO)\n");
		xen_core = TRUE;
		if (store) {
			pc->flags |= XEN_CORE;
			nd->xen_kdump_data = &xen_kdump_data;
			nd->xen_kdump_data->last_mfn_read = UNINITIALIZED;
			nd->xen_kdump_data->last_pmd_read = UNINITIALIZED;

			if ((note->n_type == NT_XEN_KDUMP_CR3) &&
			    ((note->n_descsz/sizeof(ulong)) == 1)) {
				nd->xen_kdump_data->flags |= KDUMP_CR3;
	                        /*
	                         *  Use the first cr3 found.
	                         */
	                        if (!nd->xen_kdump_data->cr3) {
					up = (ulong *)(ptr + note->n_namesz);
	                                up = (ulong *)roundup((ulong)up, 4);
	                                nd->xen_kdump_data->cr3 = *up;
	                        }
			} else {
				nd->xen_kdump_data->flags |= KDUMP_MFN_LIST;
				up = (ulong *)(ptr + note->n_namesz);
	                        up = (ulong *)roundup((ulong)up, 4);
				words = note->n_descsz/sizeof(ulong);
				/*
				 *  If already set, overridden with --p2m_mfn
				 */
	                        if (!nd->xen_kdump_data->p2m_mfn)
	                        	nd->xen_kdump_data->p2m_mfn = *(up+(words-1));
				if (words > 9 && !nd->xen_kdump_data->xen_phys_start)
					nd->xen_kdump_data->xen_phys_start = *(up+(words-2));
				nd->xen_kdump_data->xen_major_version = *up;
				nd->xen_kdump_data->xen_minor_version = *(up+1);
			}
		}
                break;

        case XEN_ELFNOTE_CRASH_REGS:
      		/* 
		 *  x86 and x86_64: cr0, cr2, cr3, cr4 
		 */
                xen_core = TRUE;
                netdump_print("(XEN_ELFNOTE_CRASH_REGS)\n");
                break;
	}

	if (machine_type("S390X")) {
		if (store)
			machdep->dumpfile_init(nd->num_prstatus_notes, note);

		uptr = (ulonglong *)
	    	    ((void *)note + roundup(sizeof(*note) + note->n_namesz, 4));
	} else {
		uptr = (ulonglong *)(ptr + note->n_namesz);
	
		/*
		 * kdumps are off-by-1, because their n_namesz is 5 for "CORE".
		 */
		if ((nd->flags & KDUMP_ELF64) && (note->n_namesz == 5))
			uptr = (ulonglong *)(ptr + ((note->n_namesz + 3) & ~3));
	
		if (xen_core)
			uptr = (ulonglong *)roundup((ulong)uptr, 4);
	}

	if (store && qemuinfo) {
		for(i=0; i<NR_CPUS; i++) {
			if (!nd->nt_qemu_percpu[i]) {
				nd->nt_qemu_percpu[i] = (void *)uptr;
				nd->num_qemu_notes++;
				break;
			}
		}
	}

	if (BITS32() && (xen_core || (note->n_type == NT_PRSTATUS))) {
		iptr = (int *)uptr;
		for (i = lf = 0; i < note->n_descsz/sizeof(ulong); i++) {
			if (((i%4)==0)) {
				netdump_print("%s                         ", 
					i ? "\n" : "");
				lf++;
			} else
				lf = 0;
			netdump_print("%08lx ", *iptr++);
		}
	} else if (vmcoreinfo || eraseinfo) {
		netdump_print("                         ");
		ptr += note->n_namesz + 1;
		for (i = 0; i < note->n_descsz; i++, ptr++) {
			netdump_print("%c", *ptr);
			if (*ptr == '\n')
				netdump_print("                         ");
		}
		lf = 0;
	} else if (note->n_descsz == 4) {
		i = 0; lf = 1;
		iptr = (int *)uptr;
		netdump_print("                         %08lx\n", *iptr); 
	} else {
		for (i = lf = 0; i < note->n_descsz/sizeof(ulonglong); i++) {
			if (((i%2)==0)) {
				netdump_print("%s                         ", 
					i ? "\n" : "");
				lf++;
			} else
				lf = 0;
			netdump_print("%016llx ", *uptr++);
		}
	}
	if (!lf)
		netdump_print("\n");
	else if (i && (i&1))
		netdump_print("\n");

  	len = sizeof(Elf64_Nhdr);
  	len = roundup(len + note->n_namesz, 4);
  	len = roundup(len + note->n_descsz, 4);

	return len;
}

/*
 *  Send the request to the proper architecture hander.
 */
void
get_netdump_regs(struct bt_info *bt, ulong *eip, ulong *esp)
{
	int e_machine;

        if (nd->elf32)
        	e_machine = nd->elf32->e_machine;
        else if (nd->elf64)
       		e_machine = nd->elf64->e_machine;
        else
        	e_machine = EM_NONE;

        switch (e_machine) 
	{
	case EM_386:
		return get_netdump_regs_x86(bt, eip, esp);
		break;

	case EM_IA_64:
	       /* For normal backtraces, this information will be obtained
		* frome the switch_stack structure, which is pointed to by
		* the thread.ksp field of the task_struct. But it's still
		* needed by the "bt -t" option.
		*/
		machdep->get_stack_frame(bt, eip, esp);
		break;

	case EM_PPC:
		return get_netdump_regs_ppc(bt, eip, esp);
		break;

	case EM_PPC64:
		return get_netdump_regs_ppc64(bt, eip, esp);
		break;

	case EM_X86_64:
		return get_netdump_regs_x86_64(bt, eip, esp);
		break;

	case EM_S390:
		machdep->get_stack_frame(bt, eip, esp);
		break;

	case EM_ARM:
		return get_netdump_regs_arm(bt, eip, esp);
		break;

	case EM_AARCH64:
		return get_netdump_regs_arm64(bt, eip, esp);
		break;

	default:
		error(FATAL, 
		   "support for ELF machine type %d not available\n",
			e_machine);  
	}
}

/* 
 * get regs from elf note, and return the address of user_regs. 
 */
static char * 
get_regs_from_note(char *note, ulong *ip, ulong *sp)
{
	Elf32_Nhdr *note32;
	Elf64_Nhdr *note64;
	size_t len;
	char *user_regs;
	long offset_sp, offset_ip;

	if (machine_type("X86_64")) {
		note64 = (Elf64_Nhdr *)note;
		len = sizeof(Elf64_Nhdr);
		len = roundup(len + note64->n_namesz, 4);
		len = roundup(len + note64->n_descsz, 4);
		offset_sp = OFFSET(user_regs_struct_rsp);
		offset_ip = OFFSET(user_regs_struct_rip);
	} else if (machine_type("X86")) {
		note32 = (Elf32_Nhdr *)note;
		len = sizeof(Elf32_Nhdr);
		len = roundup(len + note32->n_namesz, 4);
		len = roundup(len + note32->n_descsz, 4);
		offset_sp = OFFSET(user_regs_struct_esp);
		offset_ip = OFFSET(user_regs_struct_eip);
	} else
		return NULL;

	user_regs = note + len - SIZE(user_regs_struct) - sizeof(long);
	*sp = ULONG(user_regs + offset_sp);
	*ip = ULONG(user_regs + offset_ip);

	return user_regs;
}

static void
display_regs_from_elf_notes(int cpu)
{
	Elf32_Nhdr *note32;
	Elf64_Nhdr *note64;
	size_t len;
	char *user_regs;

	if (cpu >= nd->num_prstatus_notes) {
		error(INFO, "registers not collected for cpu %d\n", cpu);
		return;
	}

	if (machine_type("X86_64")) {
		if (nd->num_prstatus_notes > 1)
                	note64 = (Elf64_Nhdr *)
				nd->nt_prstatus_percpu[cpu];
		else
                	note64 = (Elf64_Nhdr *)nd->nt_prstatus;
		len = sizeof(Elf64_Nhdr);
		len = roundup(len + note64->n_namesz, 4);
		len = roundup(len + note64->n_descsz, 4);
		user_regs = ((char *)note64) + len - SIZE(user_regs_struct) - sizeof(long);

		fprintf(fp,
		    "    RIP: %016llx  RSP: %016llx  RFLAGS: %08llx\n"
		    "    RAX: %016llx  RBX: %016llx  RCX: %016llx\n"
		    "    RDX: %016llx  RSI: %016llx  RDI: %016llx\n"
		    "    RBP: %016llx   R8: %016llx   R9: %016llx\n"
		    "    R10: %016llx  R11: %016llx  R12: %016llx\n"
		    "    R13: %016llx  R14: %016llx  R15: %016llx\n"
		    "    CS: %04x  SS: %04x\n",
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_rip)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_rsp)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_eflags)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_rax)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_rbx)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_rcx)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_rdx)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_rsi)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_rdi)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_rbp)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_r8)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_r9)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_r10)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_r11)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_r12)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_r13)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_r14)),
		    ULONGLONG(user_regs + OFFSET(user_regs_struct_r15)),
		    USHORT(user_regs + OFFSET(user_regs_struct_cs)),
		    USHORT(user_regs + OFFSET(user_regs_struct_ss))
		);
	} else if (machine_type("X86")) {
		if (nd->num_prstatus_notes > 1)
                	note32 = (Elf32_Nhdr *)
				nd->nt_prstatus_percpu[cpu];
		else
                	note32 = (Elf32_Nhdr *)nd->nt_prstatus;
		len = sizeof(Elf32_Nhdr);
		len = roundup(len + note32->n_namesz, 4);
		len = roundup(len + note32->n_descsz, 4);
		user_regs = ((char *)note32) + len - SIZE(user_regs_struct) - sizeof(long);

		fprintf(fp,
		    "    EAX: %08x  EBX: %08x  ECX: %08x  EDX: %08x\n"
		    "    ESP: %08x  EIP: %08x  ESI: %08x  EDI: %08x\n"
		    "    CS: %04x       DS: %04x       ES: %04x       FS: %04x\n"
		    "    GS: %04x       SS: %04x\n"
		    "    EBP: %08x  EFLAGS: %08x\n",
		    UINT(user_regs + OFFSET(user_regs_struct_eax)),
		    UINT(user_regs + OFFSET(user_regs_struct_ebx)),
		    UINT(user_regs + OFFSET(user_regs_struct_ecx)),
		    UINT(user_regs + OFFSET(user_regs_struct_edx)),
		    UINT(user_regs + OFFSET(user_regs_struct_esp)),
		    UINT(user_regs + OFFSET(user_regs_struct_eip)),
		    UINT(user_regs + OFFSET(user_regs_struct_esi)),
		    UINT(user_regs + OFFSET(user_regs_struct_edi)),
		    USHORT(user_regs + OFFSET(user_regs_struct_cs)),
		    USHORT(user_regs + OFFSET(user_regs_struct_ds)),
		    USHORT(user_regs + OFFSET(user_regs_struct_es)),
		    USHORT(user_regs + OFFSET(user_regs_struct_fs)),
		    USHORT(user_regs + OFFSET(user_regs_struct_gs)),
		    USHORT(user_regs + OFFSET(user_regs_struct_ss)),
		    UINT(user_regs + OFFSET(user_regs_struct_ebp)),
		    UINT(user_regs + OFFSET(user_regs_struct_eflags))
		);
	} else if (machine_type("ARM64")) {
		if (nd->num_prstatus_notes > 1)
                	note64 = (Elf64_Nhdr *)
				nd->nt_prstatus_percpu[cpu];
		else
                	note64 = (Elf64_Nhdr *)nd->nt_prstatus;
		len = sizeof(Elf64_Nhdr);
		len = roundup(len + note64->n_namesz, 4);
		len = roundup(len + note64->n_descsz, 4);
//		user_regs = ((char *)note64) + len - SIZE(user_regs_struct) - sizeof(long);
		fprintf(fp, "display_regs_from_elf_notes: ARM64 register dump TBD\n");
	}
}

void
dump_registers_for_elf_dumpfiles(void)
{
        int c;

        if (!(machine_type("X86") || machine_type("X86_64") || machine_type("ARM64")))
                error(FATAL, "-r option not supported for this dumpfile\n");

	if (NETDUMP_DUMPFILE()) {
                display_regs_from_elf_notes(0);
		return;
	}

        for (c = 0; c < kt->cpus; c++) {
                fprintf(fp, "%sCPU %d:\n", c ? "\n" : "", c);
                display_regs_from_elf_notes(c);
        }
}

struct x86_64_user_regs_struct {
        unsigned long r15,r14,r13,r12,rbp,rbx,r11,r10;
        unsigned long r9,r8,rax,rcx,rdx,rsi,rdi,orig_rax;
        unsigned long rip,cs,eflags;
        unsigned long rsp,ss;
        unsigned long fs_base, gs_base;
        unsigned long ds,es,fs,gs;
};

void 
get_netdump_regs_x86_64(struct bt_info *bt, ulong *ripp, ulong *rspp)
{
        Elf64_Nhdr *note;
        size_t len;
        char *user_regs;
	ulong regs_size, rsp_offset, rip_offset;
	ulong rip, rsp;

        if (is_task_active(bt->task)) 
                bt->flags |= BT_DUMPFILE_SEARCH;

	if (((NETDUMP_DUMPFILE() || KDUMP_DUMPFILE()) &&
   	      VALID_STRUCT(user_regs_struct) && (bt->task == tt->panic_task)) ||
	      (KDUMP_DUMPFILE() && (kt->flags & DWARF_UNWIND) && 
	      (bt->flags & BT_DUMPFILE_SEARCH))) {
		if (nd->num_prstatus_notes > 1)
                	note = (Elf64_Nhdr *)
				nd->nt_prstatus_percpu[bt->tc->processor];
		else
                	note = (Elf64_Nhdr *)nd->nt_prstatus;

		if (!note)
			goto no_nt_prstatus_exists;

                len = sizeof(Elf64_Nhdr);
                len = roundup(len + note->n_namesz, 4);
                len = roundup(len + note->n_descsz, 4);

		regs_size = VALID_STRUCT(user_regs_struct) ?
			SIZE(user_regs_struct) : 
			sizeof(struct x86_64_user_regs_struct);
		rsp_offset = VALID_MEMBER(user_regs_struct_rsp) ?
			OFFSET(user_regs_struct_rsp) : 
			offsetof(struct x86_64_user_regs_struct, rsp);
		rip_offset = VALID_MEMBER(user_regs_struct_rip) ?
			OFFSET(user_regs_struct_rip) :
                        offsetof(struct x86_64_user_regs_struct, rip);

                user_regs = ((char *)note + len) - regs_size - sizeof(long);
		rsp = ULONG(user_regs + rsp_offset);
		rip = ULONG(user_regs + rip_offset);

		if (INSTACK(rsp, bt) || 
		    in_alternate_stack(bt->tc->processor, rsp)) {
			if (CRASHDEBUG(1))
				netdump_print("ELF prstatus rsp: %lx rip: %lx\n", 
					rsp, rip);

			if (KDUMP_DUMPFILE()) {
				*rspp = rsp;
				*ripp = rip;

				if (*ripp && *rspp)
					bt->flags |= BT_KDUMP_ELF_REGS;
			}
			
			bt->machdep = (void *)user_regs;
		}
	}

	if (ELF_NOTES_VALID() && 
	    (bt->flags & BT_DUMPFILE_SEARCH) && DISKDUMP_DUMPFILE() && 
	    (note = (Elf64_Nhdr *)
	     diskdump_get_prstatus_percpu(bt->tc->processor))) {

		if (!note)
			goto no_nt_prstatus_exists;

		user_regs = get_regs_from_note((char *)note, &rip, &rsp);

		if (INSTACK(rsp, bt) || 
		    in_alternate_stack(bt->tc->processor, rsp)) {
			if (CRASHDEBUG(1))
				netdump_print("ELF prstatus rsp: %lx rip: %lx\n",
					rsp, rip);

			*rspp = rsp;
			*ripp = rip;

			if (*ripp && *rspp)
				bt->flags |= BT_KDUMP_ELF_REGS;

			bt->machdep = (void *)user_regs;
		}
	}

no_nt_prstatus_exists:
        machdep->get_stack_frame(bt, ripp, rspp);
}

/*
 *  Netdump doesn't save state of the active tasks in the TSS, so poke around
 *  the raw stack for some reasonable hooks.
 */

void
get_netdump_regs_x86(struct bt_info *bt, ulong *eip, ulong *esp)
{
	int i, search, panic, panic_task, altered;
	char *sym;
	ulong *up;
	ulong ipintr_eip, ipintr_esp, ipintr_func;
	ulong halt_eip, halt_esp, panic_eip, panic_esp;
	int check_hardirq, check_softirq;
	ulong stackbase, stacktop;
	Elf32_Nhdr *note;
	char *user_regs ATTRIBUTE_UNUSED;
	ulong ip, sp;

	if (!is_task_active(bt->task)) {
		machdep->get_stack_frame(bt, eip, esp);
		return;
	}

	panic_task = tt->panic_task == bt->task ? TRUE : FALSE;

	ipintr_eip = ipintr_esp = ipintr_func = panic = altered = 0;
	halt_eip = halt_esp = panic_eip = panic_esp = 0;
	check_hardirq = check_softirq = tt->flags & IRQSTACKS ? TRUE : FALSE;
	search = ((bt->flags & BT_TEXT_SYMBOLS) && (tt->flags & TASK_INIT_DONE))
		|| (machdep->flags & OMIT_FRAME_PTR);
	stackbase = bt->stackbase;
	stacktop = bt->stacktop;
retry:
	for (i = 0, up = (ulong *)bt->stackbuf; i < LONGS_PER_STACK; i++, up++){
		sym = closest_symbol(*up);

		if (XEN_CORE_DUMPFILE()) {
			if (STREQ(sym, "xen_machine_kexec")) {
				*eip = *up;
				*esp = bt->stackbase + ((char *)(up+1) - bt->stackbuf);
				return;
			}
			if (STREQ(sym, "crash_kexec")) {
                        	halt_eip = *up;
				halt_esp = bt->stackbase + ((char *)(up+1) - bt->stackbuf);
			}
		} else if (STREQ(sym, "netconsole_netdump") || 
		    STREQ(sym, "netpoll_start_netdump") ||
		    STREQ(sym, "start_disk_dump") ||
		    (STREQ(sym, "crash_kexec") && !KVMDUMP_DUMPFILE()) ||
		    STREQ(sym, "disk_dump")) {
crash_kexec:
			*eip = *up;
			*esp = search ?
			    bt->stackbase + ((char *)(up+1) - bt->stackbuf) :
				*(up-1);
			return;
		}

                if (STREQ(sym, "panic")) {
                        *eip = *up;
                        *esp = search ?
			    bt->stackbase + ((char *)(up+1) - bt->stackbuf) :
				*(up-1);
			panic_eip = *eip;
			panic_esp = *esp;
			panic = TRUE;
                        continue;   /* keep looking for die */
                }

                if (STREQ(sym, "die")) {
                        *eip = *up;
                        *esp = search ? 
			    bt->stackbase + ((char *)(up+1) - bt->stackbuf) :
				*(up-1);
                        for (i++, up++; i < LONGS_PER_STACK; i++, up++) {
                                sym = closest_symbol(*up);
                                if (STREQ(sym, "sysrq_handle_crash"))
                                        goto next_sysrq;
                        }
                        return;
                }

                if (STREQ(sym, "sysrq_handle_crash")) {
next_sysrq:
                        *eip = *up;
			*esp = bt->stackbase + ((char *)(up+4) - bt->stackbuf);
			pc->flags |= SYSRQ;
			for (i++, up++; i < LONGS_PER_STACK; i++, up++) {
				sym = closest_symbol(*up);
				if (STREQ(sym, "crash_kexec") && !KVMDUMP_DUMPFILE())
					goto crash_kexec;
				if (STREQ(sym, "sysrq_handle_crash")) 
					goto next_sysrq; 
			}
			if (!panic)
				return;
                }

		/* 
		 *  Obsolete -- replaced by sysrq_handle_crash 
		 */
                if (STREQ(sym, "sysrq_handle_netdump")) {
                        *eip = *up;
                        *esp = search ?
                            bt->stackbase + ((char *)(up+1) - bt->stackbuf) :
                                *(up-1);
                        pc->flags |= SYSRQ;
                        return;
                }

                if (STREQ(sym, "crash_nmi_callback")) {
                        *eip = *up;
                        *esp = search ?
                            bt->stackbase + ((char *)(up+1) - bt->stackbuf) :
                                *(up-1);
                        return;
                }

                if (STREQ(sym, "stop_this_cpu")) {
                        *eip = *up;
                        *esp = search ?
                            bt->stackbase + ((char *)(up+1) - bt->stackbuf) :
                                *(up-1);
                        return;
                }

                if (STREQ(sym, "smp_call_function_interrupt")) {
			if (ipintr_eip && IS_VMALLOC_ADDR(ipintr_func) &&
		  	    IS_KERNEL_STATIC_TEXT(*(up - 2)))
				continue;
                        ipintr_eip = *up;
                        ipintr_esp = search ?
			    bt->stackbase + ((char *)(up+1) - bt->stackbuf) :
			    bt->stackbase + ((char *)(up-1) - bt->stackbuf);
			ipintr_func = *(up - 2);
                }

                if (XEN_CORE_DUMPFILE() && !panic_task && (bt->tc->pid == 0) &&
                    STREQ(sym, "safe_halt")) {
                        halt_eip = *up;
			halt_esp = bt->stackbase + ((char *)(up+1) - bt->stackbuf);
                }

                if (XEN_CORE_DUMPFILE() && !panic_task && (bt->tc->pid == 0) &&
                    !halt_eip && STREQ(sym, "xen_idle")) {
                        halt_eip = *up;
			halt_esp = bt->stackbase + ((char *)(up+1) - bt->stackbuf);
                }
	}

	if (panic) {
		*eip = panic_eip;
		*esp = panic_esp;
		return;
	}

	if (ipintr_eip) {
        	*eip = ipintr_eip;
        	*esp = ipintr_esp;
		return;
	}

	if (halt_eip && halt_esp) {
        	*eip = halt_eip;
        	*esp = halt_esp;
		return;
	}

	bt->flags &= ~(BT_HARDIRQ|BT_SOFTIRQ);

	if (check_hardirq &&
	    (tt->hardirq_tasks[bt->tc->processor] == bt->tc->task)) {
		bt->stackbase = tt->hardirq_ctx[bt->tc->processor];
		bt->stacktop = bt->stackbase + STACKSIZE();
		alter_stackbuf(bt);
		bt->flags |= BT_HARDIRQ;
		check_hardirq = FALSE;
		altered = TRUE;
		goto retry;
	}

        if (check_softirq &&
            (tt->softirq_tasks[bt->tc->processor] == bt->tc->task)) {
                bt->stackbase = tt->softirq_ctx[bt->tc->processor];
                bt->stacktop = bt->stackbase + STACKSIZE();
		alter_stackbuf(bt);
		bt->flags |= BT_SOFTIRQ;
                check_softirq = FALSE;
		altered = TRUE;
                goto retry;
        }

	if (ELF_NOTES_VALID() && DISKDUMP_DUMPFILE() &&
	    (note = (Elf32_Nhdr *)
	     diskdump_get_prstatus_percpu(bt->tc->processor))) {
		user_regs = get_regs_from_note((char *)note, &ip, &sp);
		if (is_kernel_text(ip) &&
		    (((sp >= GET_STACKBASE(bt->task)) &&
		      (sp < GET_STACKTOP(bt->task))) ||
		    in_alternate_stack(bt->tc->processor, sp))) {
			bt->flags |= BT_KERNEL_SPACE;
			*eip = ip;
			*esp = sp;
			return;
		}

		if (!is_kernel_text(ip) && in_user_stack(bt->tc->task, sp)) {
			bt->flags |= BT_USER_SPACE;
			*eip = ip;
			*esp = sp;
			return;
		}
	}

	if (CRASHDEBUG(1))
		error(INFO, 
    "get_netdump_regs_x86: cannot find anything useful (task: %lx)\n", bt->task);

	if (altered) {
                bt->stackbase = stackbase;
                bt->stacktop = stacktop;
		alter_stackbuf(bt);
	}

        if (XEN_CORE_DUMPFILE() && !panic_task && is_task_active(bt->task) &&
	    !(bt->flags & (BT_TEXT_SYMBOLS_ALL|BT_TEXT_SYMBOLS)))
		error(FATAL, 
		    "starting backtrace locations of the active (non-crashing) "
		    "xen tasks\n    cannot be determined: try -t or -T options\n");
 
	if (KVMDUMP_DUMPFILE() || SADUMP_DUMPFILE())
		bt->flags &= ~(ulonglong)BT_DUMPFILE_SEARCH;

	machdep->get_stack_frame(bt, eip, esp);
}

static void
get_netdump_regs_ppc(struct bt_info *bt, ulong *eip, ulong *esp)
{
	Elf32_Nhdr *note;
	size_t len;

	ppc_relocate_nt_prstatus_percpu(nd->nt_prstatus_percpu,
					&nd->num_prstatus_notes);

	if ((bt->task == tt->panic_task) ||
		(is_task_active(bt->task) && nd->num_prstatus_notes > 1)) {
		/*	
		 * Registers are saved during the dump process for the 
		 * panic task. Whereas in kdump, regs are captured for all 
		 * CPUs if they responded to an IPI.
		 */
                if (nd->num_prstatus_notes > 1) {
			if (!nd->nt_prstatus_percpu[bt->tc->processor])
				error(FATAL, 
		          	    "cannot determine NT_PRSTATUS ELF note "
				    "for %s task: %lx\n", 
					(bt->task == tt->panic_task) ?
					"panic" : "active", bt->task);	
                        note = (Elf32_Nhdr *)
                                nd->nt_prstatus_percpu[bt->tc->processor];
		} else
			note = (Elf32_Nhdr *)nd->nt_prstatus;

		if (!note)
			goto no_nt_prstatus_exists;

		len = sizeof(Elf32_Nhdr);
		len = roundup(len + note->n_namesz, 4);
		bt->machdep = (void *)((char *)note + len + 
			MEMBER_OFFSET("elf_prstatus", "pr_reg"));
	}

no_nt_prstatus_exists:
	machdep->get_stack_frame(bt, eip, esp);
}

static void
get_netdump_regs_ppc64(struct bt_info *bt, ulong *eip, ulong *esp)
{
	Elf64_Nhdr *note;
	size_t len;

	if ((bt->task == tt->panic_task) ||
		(is_task_active(bt->task) && nd->num_prstatus_notes > 1)) {
		/*	
		 * Registers are saved during the dump process for the 
		 * panic task. Whereas in kdump, regs are captured for all 
		 * CPUs if they responded to an IPI.
		 */
                if (nd->num_prstatus_notes > 1) {
			if (!nd->nt_prstatus_percpu[bt->tc->processor])
				error(FATAL, 
		          	    "cannot determine NT_PRSTATUS ELF note "
				    "for %s task: %lx\n", 
					(bt->task == tt->panic_task) ?
					"panic" : "active", bt->task);	
                        note = (Elf64_Nhdr *)
                                nd->nt_prstatus_percpu[bt->tc->processor];
		} else
			note = (Elf64_Nhdr *)nd->nt_prstatus;

		if (!note)
			goto no_nt_prstatus_exists;

		len = sizeof(Elf64_Nhdr);
		len = roundup(len + note->n_namesz, 4);
		bt->machdep = (void *)((char *)note + len + 
			MEMBER_OFFSET("elf_prstatus", "pr_reg"));
	}

no_nt_prstatus_exists:
	machdep->get_stack_frame(bt, eip, esp);
}

static void
get_netdump_regs_arm(struct bt_info *bt, ulong *eip, ulong *esp)
{
	machdep->get_stack_frame(bt, eip, esp);
}

static void
get_netdump_regs_arm64(struct bt_info *bt, ulong *eip, ulong *esp)
{
	machdep->get_stack_frame(bt, eip, esp);
}

int 
is_partial_netdump(void)
{
	return (nd->flags & PARTIAL_DUMP ? TRUE : FALSE);
}


/*
 *  kexec/kdump generated vmcore files are similar enough in
 *  nature to netdump/diskdump such that most vmcore access
 *  functionality may be borrowed from the equivalent netdump
 *  function.  If not, re-work them here.
 */
int
is_kdump(char *file, ulong source_query)
{
        return is_netdump(file, source_query);
}

int
kdump_init(char *unused, FILE *fptr)
{
	return netdump_init(unused, fptr);
}

ulong 
get_kdump_panic_task(void)
{
	return get_netdump_panic_task();
}

int
read_kdump(int fd, void *bufptr, int cnt, ulong addr, physaddr_t paddr)
{
	physaddr_t paddr_in = paddr;

	if ((nd->flags & QEMU_MEM_DUMP_KDUMP_BACKUP) &&
	    (paddr >= nd->backup_src_start) &&
	    (paddr < nd->backup_src_start + nd->backup_src_size)) {

		paddr += nd->backup_offset - nd->backup_src_start;

		if (CRASHDEBUG(1))
			error(INFO,
			    "qemu_mem_dump: kdump backup region: %#llx => %#llx\n",
			    paddr_in, paddr);
	}

	if (XEN_CORE_DUMPFILE() && !XEN_HYPER_MODE()) {
	    	if (!(nd->xen_kdump_data->flags & KDUMP_P2M_INIT)) {
        		if (!machdep->xen_kdump_p2m_create)
                		error(FATAL,
                            "xen kdump dumpfiles not supported on this architecture\n");

			if ((nd->xen_kdump_data->page = 
			    (char *)malloc(PAGESIZE())) == NULL)
				error(FATAL,
				    "cannot malloc xen kdump data page\n");

			if (!machdep->xen_kdump_p2m_create(nd->xen_kdump_data))
                		error(FATAL,
                    	    "cannot create xen kdump pfn-to-mfn mapping\n");

        		nd->xen_kdump_data->flags |= KDUMP_P2M_INIT;
		}

		if ((paddr = xen_kdump_p2m(paddr)) == P2M_FAILURE) {
			if (CRASHDEBUG(8)) 
				fprintf(fp, "read_kdump: xen_kdump_p2m(%llx): "
					"P2M_FAILURE\n", (ulonglong)paddr_in);
			return READ_ERROR;
		}
		if (CRASHDEBUG(8))
			fprintf(fp, "read_kdump: xen_kdump_p2m(%llx): %llx\n",
				(ulonglong)paddr_in, (ulonglong)paddr);
	}

	return read_netdump(fd, bufptr, cnt, addr, paddr);
}

int
write_kdump(int fd, void *bufptr, int cnt, ulong addr, physaddr_t paddr)
{
	return write_netdump(fd, bufptr, cnt, addr, paddr);
}

void
get_kdump_regs(struct bt_info *bt, ulong *eip, ulong *esp)
{
	get_netdump_regs(bt, eip, esp);
}

uint
kdump_page_size(void)
{
        uint pagesz;

        if (!VMCORE_VALID())
                return 0;

	if (!(pagesz = nd->page_size))
                pagesz = (uint)getpagesize();

        return pagesz;
}

int 
kdump_free_memory(void)
{
	return netdump_free_memory();
}

int 
kdump_memory_used(void)
{
	return netdump_memory_used();
}

int 
kdump_memory_dump(FILE *fp)
{
	return netdump_memory_dump(fp);
}

/*
 *  Translate a xen domain's pseudo-physical address into the
 *  xen machine address.  Since there's no compression involved,
 *  just the last phys_to_machine_mapping[] page read is cached, 
 *  which essentially caches 1024 p2m translations. 
 */
static physaddr_t 
xen_kdump_p2m(physaddr_t pseudo)
{
	ulong pfn, mfn_frame; 
	ulong *mfnptr;
	ulong mfn_idx, frame_idx;
	physaddr_t paddr;
	struct xen_kdump_data *xkd = nd->xen_kdump_data;

	if (pc->curcmd_flags & XEN_MACHINE_ADDR)
		return pseudo;

#ifdef IA64
	return ia64_xen_kdump_p2m(xkd, pseudo);
#endif

	xkd->accesses++;

	pfn = (ulong)BTOP(pseudo);
	mfn_idx = pfn / (PAGESIZE()/sizeof(ulong));
	frame_idx = pfn % (PAGESIZE()/sizeof(ulong));
	if (mfn_idx >= xkd->p2m_frames) {
		if (CRASHDEBUG(8))
			fprintf(fp, "xen_kdump_p2m: paddr/pfn: %llx/%lx: "
			    "mfn_idx nonexistent\n",
				(ulonglong)pseudo, pfn);
		return P2M_FAILURE;
	}
	mfn_frame = xkd->p2m_mfn_frame_list[mfn_idx];

	if (mfn_frame == xkd->last_mfn_read)
		xkd->cache_hits++;
	else {
		if (CRASHDEBUG(8))
			fprintf(fp, "xen_kdump_p2m: paddr/pfn: %llx/%lx: "
			    "read mfn_frame: %llx\n",
				(ulonglong)pseudo, pfn, PTOB(mfn_frame));
		if (read_netdump(0, xkd->page, PAGESIZE(), 0, 
		    (physaddr_t)PTOB(mfn_frame)) != PAGESIZE())
			return P2M_FAILURE;
	}

	xkd->last_mfn_read = mfn_frame;

	mfnptr = ((ulong *)(xkd->page)) + frame_idx;
	paddr = (physaddr_t)PTOB((ulonglong)(*mfnptr));  
	paddr |= PAGEOFFSET(pseudo);

	if (CRASHDEBUG(7))
		fprintf(fp, 
		    "xen_kdump_p2m(%llx): mfn_idx: %ld frame_idx: %ld"
		    " mfn_frame: %lx mfn: %lx => %llx\n",
			(ulonglong)pseudo, mfn_idx, frame_idx, 
			mfn_frame, *mfnptr, (ulonglong)paddr);
	
	return paddr;
}

struct vmcore_data *
get_kdump_vmcore_data(void)
{
	if (!VMCORE_VALID() || !KDUMP_DUMPFILE())
		return NULL;

	return &vmcore_data;
}

/*
 *  Override the dom0 p2m mfn in the XEN_ELFNOTE_CRASH_INFO note
 *  in order to initiate a crash session of a guest kernel.
 */
void
xen_kdump_p2m_mfn(char *arg)
{
	ulong value;
	int errflag;

	errflag = 0;
	value = htol(arg, RETURN_ON_ERROR|QUIET, &errflag);
	if (!errflag) {
		xen_kdump_data.p2m_mfn = value;
		if (CRASHDEBUG(1))
			error(INFO, 
			    "xen_kdump_data.p2m_mfn override: %lx\n",  
				value); 
	} else 
		error(WARNING, "invalid p2m_mfn argument: %s\n", arg);
}

/*
 *  Fujitsu dom0/HV sadump-generated dumpfile, which requires
 *  the --p2m_mfn command line argument.
 */
int
is_sadump_xen(void)
{
	if (xen_kdump_data.p2m_mfn) {
		if (!XEN_CORE_DUMPFILE()) {
			pc->flags |= XEN_CORE;
			nd->xen_kdump_data = &xen_kdump_data;
			nd->xen_kdump_data->last_mfn_read = UNINITIALIZED;
			nd->xen_kdump_data->last_pmd_read = UNINITIALIZED;
			nd->xen_kdump_data->flags |= KDUMP_MFN_LIST;
		}
		return TRUE;
	}

	return FALSE;
}

void
set_xen_phys_start(char *arg)
{
	ulong value;
	int errflag = 0;

	value = htol(arg, RETURN_ON_ERROR|QUIET, &errflag);
	if (!errflag)
		xen_kdump_data.xen_phys_start = value;
	else 
		error(WARNING, "invalid xen_phys_start argument: %s\n", arg);
}

ulong
xen_phys_start(void)
{
	return nd->xen_kdump_data->xen_phys_start;
}

int
xen_major_version(void)
{
	return nd->xen_kdump_data->xen_major_version;
}

int
xen_minor_version(void)
{
	return nd->xen_kdump_data->xen_minor_version;
}


/*
 *  The following set of functions are not used by the crash
 *  source code, but are available to extension modules for
 *  gathering register sets from ELF NT_PRSTATUS note sections.
 *
 *  Contributed by: Sharyathi Nagesh (sharyath@in.ibm.com)
 */

static void *get_ppc_regs_from_elf_notes(struct task_context *);
static void *get_ppc64_regs_from_elf_notes(struct task_context *);
static void *get_x86_regs_from_elf_notes(struct task_context *);
static void *get_x86_64_regs_from_elf_notes(struct task_context *);
static void *get_arm_regs_from_elf_notes(struct task_context *);

int get_netdump_arch(void)
{
	int e_machine;

	if (nd->elf32)
		e_machine = nd->elf32->e_machine;
	else if (nd->elf64)
		e_machine = nd->elf64->e_machine;
	else
		e_machine = EM_NONE;

	return e_machine;
}

int 
exist_regs_in_elf_notes(struct task_context *tc)
{
	if ((tc->task == tt->panic_task) ||
	    (is_task_active(tc->task) && (nd->num_prstatus_notes > 1) &&
	     (tc->processor < nd->num_prstatus_notes)))
		return TRUE;
	else
		return FALSE;
}

void * 
get_regs_from_elf_notes(struct task_context *tc)
{
	int e_machine = get_netdump_arch();

	switch (e_machine)
	{
	case EM_386:
	case EM_PPC:
	case EM_PPC64:
	case EM_X86_64:
	case EM_ARM:
		break;
	case EM_AARCH64:
		error(FATAL, 
			"get_regs_from_elf_notes: ARM64 support TBD\n");
	default:
		error(FATAL,
		      "support for ELF machine type %d not available\n",
		      e_machine);
	}

	if (!exist_regs_in_elf_notes(tc))
		error(FATAL, "cannot determine register set "
		      "for active task: %lx comm: \"%s\"\n",
		      tc->task, tc->comm);

	switch(e_machine)
	{
	case EM_386:
		return get_x86_regs_from_elf_notes(tc);
	case EM_PPC:
		return get_ppc_regs_from_elf_notes(tc);
	case EM_PPC64:
		return get_ppc64_regs_from_elf_notes(tc);
	case EM_X86_64:
		return get_x86_64_regs_from_elf_notes(tc);
	case EM_ARM:
		return get_arm_regs_from_elf_notes(tc);
	case EM_AARCH64:
		break;  /* TBD */
	}

	return NULL;
}

static void * 
get_x86_regs_from_elf_notes(struct task_context *tc)
{
	Elf32_Nhdr *note_32;
	Elf64_Nhdr *note_64;
	void *note;
	size_t len;
	void *pt_regs;

	len = 0;
	pt_regs = NULL;

	if (nd->num_prstatus_notes > 1)
		note = (void *)nd->nt_prstatus_percpu[tc->processor];
	else
		note = (void *)nd->nt_prstatus;

	if (!note)
		goto no_nt_prstatus_exists;

	if (nd->elf32) {
		note_32 = (Elf32_Nhdr *)note;
		len = sizeof(Elf32_Nhdr);
		len = roundup(len + note_32->n_namesz, 4);
	} else if (nd->elf64) {
		note_64 = (Elf64_Nhdr *)note;
		len = sizeof(Elf64_Nhdr);
		len = roundup(len + note_64->n_namesz, 4);
	}

	pt_regs = (void *)((char *)note + len +
			   MEMBER_OFFSET("elf_prstatus", "pr_reg"));
	/* NEED TO BE FIXED: Hack to get the proper alignment */
	pt_regs +=4;

no_nt_prstatus_exists:
	return pt_regs;

}

static void * 
get_x86_64_regs_from_elf_notes(struct task_context *tc)
{
	Elf64_Nhdr *note;
	size_t len;
	void *pt_regs;

	pt_regs = NULL;

	if (nd->num_prstatus_notes > 1)
		note = (Elf64_Nhdr *)nd->nt_prstatus_percpu[tc->processor];
	else
		note = (Elf64_Nhdr *)nd->nt_prstatus;

	if (!note)
		goto no_nt_prstatus_exists;

	len = sizeof(Elf64_Nhdr);
	len = roundup(len + note->n_namesz, 4);
	pt_regs = (void *)((char *)note + len +
			   MEMBER_OFFSET("elf_prstatus", "pr_reg"));

no_nt_prstatus_exists:
	return pt_regs;
}

static void * 
get_ppc_regs_from_elf_notes(struct task_context *tc)
{
	Elf32_Nhdr *note;
	size_t len;
	void *pt_regs;
	extern struct vmcore_data *nd;

	pt_regs = NULL;

	/*
	 * Registers are always saved during the dump process for the
	 * panic task.  Kdump also captures registers for all CPUs if
	 * they responded to an IPI.
	 */
	if (nd->num_prstatus_notes > 1) {
		note = (Elf32_Nhdr *)nd->nt_prstatus_percpu[tc->processor];
	} else
		note = (Elf32_Nhdr *)nd->nt_prstatus;

	if (!note)
		goto no_nt_prstatus_exists;

	len = sizeof(Elf32_Nhdr);
	len = roundup(len + note->n_namesz, 4);
	pt_regs = (void *)((char *)note + len +
			   MEMBER_OFFSET("elf_prstatus", "pr_reg"));

no_nt_prstatus_exists:
	return pt_regs;
}

static void * 
get_ppc64_regs_from_elf_notes(struct task_context *tc)
{
	Elf64_Nhdr *note;
	size_t len;
	void *pt_regs;
	extern struct vmcore_data *nd;

	pt_regs = NULL;

	/*
	 * Registers are always saved during the dump process for the
	 * panic task.  Kdump also captures registers for all CPUs if
	 * they responded to an IPI.
	 */
	if (nd->num_prstatus_notes > 1) {
		note = (Elf64_Nhdr *)nd->nt_prstatus_percpu[tc->processor];
	} else
		note = (Elf64_Nhdr *)nd->nt_prstatus;

	if (!note)
		goto no_nt_prstatus_exists;

	len = sizeof(Elf64_Nhdr);
	len = roundup(len + note->n_namesz, 4);
	pt_regs = (void *)((char *)note + len +
			   MEMBER_OFFSET("elf_prstatus", "pr_reg"));

no_nt_prstatus_exists:
	return pt_regs;
}

/*
 * In case of ARM we need to determine correct PHYS_OFFSET from the kdump file.
 * This is done by taking lowest physical address (LMA) from given load
 * segments. Normally this is the right one.
 *
 * Alternative would be to store phys_base in VMCOREINFO but current kernel
 * kdump doesn't do that yet.
 */
int arm_kdump_phys_base(ulong *phys_base)
{
	struct pt_load_segment *pls;
	ulong paddr = ULONG_MAX;
	int i;

	for (i = 0; i < nd->num_pt_load_segments; i++) {
		pls = &nd->pt_load_segments[i];
		if (pls->phys_start < paddr)
			paddr = pls->phys_start;
	}

	if (paddr != ULONG_MAX) {
		*phys_base = paddr;
		return TRUE;
	}
	return FALSE;
}

static void *
get_arm_regs_from_elf_notes(struct task_context *tc)
{
	Elf32_Nhdr *note_32;
	Elf64_Nhdr *note_64;
	void *note;
	size_t len;
	void *pt_regs;

	len = 0;
	pt_regs = NULL;

	if (nd->num_prstatus_notes > 1)
		note = (void *)nd->nt_prstatus_percpu[tc->processor];
	else
		note = (void *)nd->nt_prstatus;

	if (!note)
		goto no_nt_prstatus_exists;

	if (nd->elf32) {
		note_32 = (Elf32_Nhdr *)note;
		len = sizeof(Elf32_Nhdr);
		len = roundup(len + note_32->n_namesz, 4);
	} else if (nd->elf64) {
		note_64 = (Elf64_Nhdr *)note;
		len = sizeof(Elf64_Nhdr);
		len = roundup(len + note_64->n_namesz, 4);
	}

	pt_regs = (void *)((char *)note + len +
			   MEMBER_OFFSET("elf_prstatus", "pr_reg"));

no_nt_prstatus_exists:
	return pt_regs;
}

/*
 *  Read from /proc/kcore.
 */
int
read_proc_kcore(int fd, void *bufptr, int cnt, ulong addr, physaddr_t paddr) 
{
	int i; 
	size_t readcnt;
	ulong kvaddr;
	Elf32_Phdr *lp32;
	Elf64_Phdr *lp64;
	off_t offset;

	if (!machdep->verify_paddr(paddr)) {
		if (CRASHDEBUG(1))
			error(INFO, "verify_paddr(%lx) failed\n", paddr);
		return READ_ERROR;
	}

	/*
	 *  Turn the physical address into a unity-mapped kernel 
	 *  virtual address, which should work for 64-bit architectures,
	 *  and for lowmem access for 32-bit architectures.
	 */
	offset = UNINITIALIZED;
	if (machine_type("ARM64"))
		kvaddr =  PTOV((ulong)paddr);
	else
		kvaddr = (ulong)paddr | machdep->kvbase;
	readcnt = cnt;

	switch (pkd->flags & (KCORE_ELF32|KCORE_ELF64)) 
	{
	case KCORE_ELF32:
		for (i = 0; i < pkd->segments; i++) {
			lp32 = pkd->load32 + i;
			if ((kvaddr >= lp32->p_vaddr) &&
			    (kvaddr < (lp32->p_vaddr + lp32->p_memsz))) {
				offset = (off_t)(kvaddr - lp32->p_vaddr) + 
					(off_t)lp32->p_offset;
				break;
			}
		}
		/*
		 *  If it's not accessible via unity-mapping, check whether
		 *  it's a request for a vmalloc address that can be found 
                 *  in the header.
		 */
		if (pc->curcmd_flags & MEMTYPE_KVADDR)
			pc->curcmd_flags &= ~MEMTYPE_KVADDR;
		else
			break;

		for (i = 0; i < pkd->segments; i++) {
			lp32 = pkd->load32 + i;
			if ((addr >= lp32->p_vaddr) &&
			    (addr < (lp32->p_vaddr + lp32->p_memsz))) {
				offset = (off_t)(addr - lp32->p_vaddr) + 
					(off_t)lp32->p_offset;
				break;
			}
		}

		break;

	case KCORE_ELF64:
		for (i = 0; i < pkd->segments; i++) {
			lp64 = pkd->load64 + i;
			if ((kvaddr >= lp64->p_vaddr) &&
			    (kvaddr < (lp64->p_vaddr + lp64->p_memsz))) {
				offset = (off_t)(kvaddr - lp64->p_vaddr) + 
					(off_t)lp64->p_offset;
				break;
			}
		}

		break;
	}

	if (offset == UNINITIALIZED)
		return SEEK_ERROR;

        if (lseek(fd, offset, SEEK_SET) != offset)
		perror("lseek");

	if (read(fd, bufptr, readcnt) != readcnt)
		return READ_ERROR;

	return cnt;
}

/*
 *  place holder -- cannot write to /proc/kcore
 */
int
write_proc_kcore(int fd, void *bufptr, int cnt, ulong addr, physaddr_t paddr)
{
	error(FATAL, "cannot write to /proc/kcore\n");
	return FALSE;
}

int
is_proc_kcore(char *file, ulong source_query)
{
	if (STREQ(file, "/proc/kcore") || same_file(file, "/proc/kcore")) {
		if (!is_netdump(file, source_query))
			error(FATAL, 
			    "cannot translate the ELF header of /proc/kcore\n");
		pkd->flags |= KCORE_LOCAL;
		return TRUE;
	} else
		return FALSE;
}

int
proc_kcore_init(FILE *fp)
{
	if (BITS32())
		return proc_kcore_init_32(fp);
	else 
		return proc_kcore_init_64(fp);
}

static int
proc_kcore_init_32(FILE *fp)
{
	Elf32_Ehdr *elf32;
	Elf32_Phdr *load32;
	char eheader[MAX_KCORE_ELF_HEADER_SIZE];
	char buf[BUFSIZE];
	size_t size;

	size = MAX_KCORE_ELF_HEADER_SIZE;

	if (read(pc->mfd, eheader, size) != size) {
		sprintf(buf, "/proc/kcore: read");
		perror(buf);
		goto bailout;
	}

	if (lseek(pc->mfd, 0, SEEK_SET) != 0) {
		sprintf(buf, "/proc/kcore: lseek");
		perror(buf);
		goto bailout;
	}

	elf32 = (Elf32_Ehdr *)&eheader[0];
	load32 = (Elf32_Phdr *)&eheader[sizeof(Elf32_Ehdr)+sizeof(Elf32_Phdr)];

	pkd->segments = elf32->e_phnum - 1;

	size = (ulong)(load32+(elf32->e_phnum)) - (ulong)elf32;
	if ((pkd->elf_header = (char *)malloc(size)) == NULL) {
		error(INFO, "/proc/kcore: cannot malloc ELF header buffer\n");
		clean_exit(1);
	}

	BCOPY(&eheader[0], &pkd->elf_header[0], size);	
	pkd->elf32 = (Elf32_Ehdr *)pkd->elf_header;
	pkd->load32 = (Elf32_Phdr *)
		&pkd->elf_header[sizeof(Elf32_Ehdr)+sizeof(Elf32_Phdr)];
	pkd->flags |= KCORE_ELF32;
	
	if (CRASHDEBUG(1))
		kcore_memory_dump(fp);

	return TRUE;
bailout:
	return FALSE;
}

static int
proc_kcore_init_64(FILE *fp)
{
	Elf64_Ehdr *elf64;
	Elf64_Phdr *load64;
	char eheader[MAX_KCORE_ELF_HEADER_SIZE];
	char buf[BUFSIZE];
	size_t size;

	size = MAX_KCORE_ELF_HEADER_SIZE;

	if (read(pc->mfd, eheader, size) != size) {
		sprintf(buf, "/proc/kcore: read");
		perror(buf);
		goto bailout;
	}

	if (lseek(pc->mfd, 0, SEEK_SET) != 0) {
		sprintf(buf, "/proc/kcore: lseek");
		perror(buf);
		goto bailout;
	}

	elf64 = (Elf64_Ehdr *)&eheader[0];
	load64 = (Elf64_Phdr *)&eheader[sizeof(Elf64_Ehdr)+sizeof(Elf64_Phdr)];

	pkd->segments = elf64->e_phnum - 1;

	size = (ulong)(load64+(elf64->e_phnum)) - (ulong)elf64;
	if ((pkd->elf_header = (char *)malloc(size)) == NULL) {
		error(INFO, "/proc/kcore: cannot malloc ELF header buffer\n");
		clean_exit(1);
	}

	BCOPY(&eheader[0], &pkd->elf_header[0], size);	
	pkd->elf64 = (Elf64_Ehdr *)pkd->elf_header;
	pkd->load64 = (Elf64_Phdr *)
		&pkd->elf_header[sizeof(Elf64_Ehdr)+sizeof(Elf64_Phdr)];
	pkd->flags |= KCORE_ELF64;
	
	if (CRASHDEBUG(1))
		kcore_memory_dump(fp);

	return TRUE;
bailout:
	return FALSE;
}

int
kcore_memory_dump(FILE *ofp)
{
	int i, others;
	Elf32_Phdr *lp32;
	Elf64_Phdr *lp64;

	if (!(pkd->flags & KCORE_LOCAL))
		return FALSE;

	fprintf(ofp, "proc_kcore_data:\n");
	fprintf(ofp, "       flags: %lx (", nd->flags);
	others = 0;
	if (pkd->flags & KCORE_LOCAL)
		fprintf(ofp, "%sKCORE_LOCAL", others++ ? "|" : "");
	if (pkd->flags & KCORE_ELF32)
		fprintf(ofp, "%sKCORE_ELF32", others++ ? "|" : "");
	if (pkd->flags & KCORE_ELF64)
		fprintf(ofp, "%sKCORE_ELF64", others++ ? "|" : "");
	fprintf(ofp, ")\n");
	fprintf(ofp, "    segments: %d\n",
		pkd->segments);
	fprintf(ofp, "  elf_header: %lx\n", (ulong)pkd->elf_header);
	fprintf(ofp, "       elf64: %lx\n", (ulong)pkd->elf64);
	fprintf(ofp, "      load64: %lx\n", (ulong)pkd->load64);
	fprintf(ofp, "       elf32: %lx\n", (ulong)pkd->elf32);
	fprintf(ofp, "      load32: %lx\n\n", (ulong)pkd->load32);

	for (i = 0; i < pkd->segments; i++) {
		if (pkd->flags & KCORE_ELF32)
			break;

		lp64 = pkd->load64 + i;

		fprintf(ofp, "  Elf64_Phdr:\n");
		fprintf(ofp, "        p_type: %x\n", lp64->p_type);
		fprintf(ofp, "       p_flags: %x\n", lp64->p_flags);
		fprintf(ofp, "      p_offset: %llx\n", (ulonglong)lp64->p_offset);
		fprintf(ofp, "       p_vaddr: %llx\n", (ulonglong)lp64->p_vaddr);
		fprintf(ofp, "       p_paddr: %llx\n", (ulonglong)lp64->p_paddr);
		fprintf(ofp, "      p_filesz: %llx\n", (ulonglong)lp64->p_filesz);
		fprintf(ofp, "       p_memsz: %llx\n", (ulonglong)lp64->p_memsz);
		fprintf(ofp, "       p_align: %lld\n", (ulonglong)lp64->p_align);
		fprintf(ofp, "\n");
	}

	for (i = 0; i < pkd->segments; i++) {
		if (pkd->flags & KCORE_ELF64)
			break;

		lp32 = pkd->load32 + i;

		fprintf(ofp, "  Elf32_Phdr:\n");
		fprintf(ofp, "        p_type: %x\n", lp32->p_type);
		fprintf(ofp, "       p_flags: %x\n", lp32->p_flags);
		fprintf(ofp, "      p_offset: %x\n", lp32->p_offset);
		fprintf(ofp, "       p_vaddr: %x\n", lp32->p_vaddr);
		fprintf(ofp, "       p_paddr: %x\n", lp32->p_paddr);
		fprintf(ofp, "      p_filesz: %x\n", lp32->p_filesz);
		fprintf(ofp, "       p_memsz: %x\n", lp32->p_memsz);
		fprintf(ofp, "       p_align: %d\n", lp32->p_align);
		fprintf(ofp, "\n");
	}

	return TRUE;
}

static void
kdump_get_osrelease(void)
{
	char *string;

	if ((string = vmcoreinfo_read_string("OSRELEASE"))) {
		fprintf(fp, "%s\n", string);
		free(string);
	} else 
		pc->flags2 &= ~GET_OSRELEASE;
}

void
dump_registers_for_qemu_mem_dump(void)
{
	int i;
	QEMUCPUState *ptr;
	FILE *fpsave;

	fpsave = nd->ofp;
	nd->ofp = fp;

	for (i=0; i<nd->num_qemu_notes; i++) {
		ptr = (QEMUCPUState *)nd->nt_qemu_percpu[i];

		if (i)
			netdump_print("\n");
		netdump_print("CPU %d:\n", i);

		if (CRASHDEBUG(1))
			netdump_print("  version:%08lx      size:%08lx\n",
				ptr->version, ptr->size);
		netdump_print("  rax:%016llx  rbx:%016llx  rcx:%016llx\n",
			ptr->rax, ptr->rbx, ptr->rcx);
		netdump_print("  rdx:%016llx  rsi:%016llx  rdi:%016llx\n",
			ptr->rdx, ptr->rsi, ptr->rdi);
		netdump_print("  rsp:%016llx  rbp:%016llx  ",
			ptr->rsp, ptr->rbp);
	
		if (DUMPFILE_FORMAT(nd->flags) == KDUMP_ELF64) {
			netdump_print(" r8:%016llx\n",
				ptr->r8);
			netdump_print("   r9:%016llx  r10:%016llx  r11:%016llx\n",
				ptr->r9, ptr->r10, ptr->r11);
			netdump_print("  r12:%016llx  r13:%016llx  r14:%016llx\n",
				ptr->r12, ptr->r13, ptr->r14);
			netdump_print("  r15:%016llx",
				ptr->r15);
		} else
                        netdump_print("\n");

		netdump_print("  rip:%016llx  rflags:%08llx\n",
			ptr->rip, ptr->rflags);
		netdump_print("  cs:\n    selector:%08lx  limit:%08lx  flags:%08lx\n\
    pad:%08lx  base:%016llx\n",
			ptr->cs.selector, ptr->cs.limit, ptr->cs.flags,
			ptr->cs.pad, ptr->cs.base);
		netdump_print("  ds:\n    selector:%08lx  limit:%08lx  flags:%08lx\n\
    pad:%08lx  base:%016llx\n",
			ptr->ds.selector, ptr->ds.limit, ptr->ds.flags,
			ptr->ds.pad, ptr->ds.base);
		netdump_print("  es:\n    selector:%08lx  limit:%08lx  flags:%08lx\n\
    pad:%08lx  base:%016llx\n",
			ptr->es.selector, ptr->es.limit, ptr->es.flags,
			ptr->es.pad, ptr->es.base);
		netdump_print("  fs:\n    selector:%08lx  limit:%08lx  flags:%08lx\n\
    pad:%08lx  base:%016llx\n",
			ptr->fs.selector, ptr->fs.limit, ptr->fs.flags,
			ptr->fs.pad, ptr->fs.base);
		netdump_print("  gs:\n    selector:%08lx  limit:%08lx  flags:%08lx\n\
    pad:%08lx  base:%016llx\n",
			ptr->gs.selector, ptr->gs.limit, ptr->gs.flags,
			ptr->gs.pad, ptr->gs.base);
		netdump_print("  ss:\n    selector:%08lx  limit:%08lx  flags:%08lx\n\
    pad:%08lx  base:%016llx\n",
			ptr->ss.selector, ptr->ss.limit, ptr->ss.flags,
			ptr->ss.pad, ptr->ss.base);
		netdump_print("  ldt:\n    selector:%08lx  limit:%08lx  flags:%08lx\n\
    pad:%08lx  base:%016llx\n",
			ptr->ldt.selector, ptr->ldt.limit, ptr->ldt.flags,
			ptr->ldt.pad, ptr->ldt.base);
		netdump_print("  tr:\n    selector:%08lx  limit:%08lx  flags:%08lx\n\
    pad:%08lx  base:%016llx\n",
			ptr->tr.selector, ptr->tr.limit, ptr->tr.flags,
			ptr->tr.pad, ptr->tr.base);
		netdump_print("  gdt:\n    selector:%08lx  limit:%08lx  flags:%08lx\n\
    pad:%08lx  base:%016llx\n",
			ptr->gdt.selector, ptr->gdt.limit, ptr->gdt.flags,
			ptr->gdt.pad, ptr->gdt.base);
		netdump_print("  idt:\n    selector:%08lx  limit:%08lx  flags:%08lx\n\
    pad:%08lx  base:%016llx\n",
			ptr->idt.selector, ptr->idt.limit, ptr->idt.flags,
			ptr->idt.pad, ptr->idt.base);
		netdump_print("  cr[0]:%016llx  cr[1]:%016llx  cr[2]:%016llx\n",
			ptr->cr[0], ptr->cr[1], ptr->cr[2]);
		netdump_print("  cr[3]:%016llx  cr[4]:%016llx\n",
			ptr->cr[3], ptr->cr[4]);
	}

	nd->ofp = fpsave;
}

/* 
 * kdump saves the first 640kB physical memory for BIOS to use the
 * range on boot of 2nd kernel. Read request to the 640k should be 
 * translated to the back up region. This function searches kexec
 * resources for the backup region.
 */
void
kdump_backup_region_init(void)
{
	char buf[BUFSIZE];
	ulong i, total, kexec_crash_image_p, elfcorehdr_p;
	Elf32_Off e_phoff32;
	Elf64_Off e_phoff64;
	uint16_t e_phnum, e_phentsize;
	ulonglong backup_offset;
	ulonglong backup_src_start;
	ulong backup_src_size;
	int kimage_segment_len;
	size_t bufsize;
	struct vmcore_data *vd;
	struct sadump_data *sd;
	int is_32_bit;  
	char typename[BUFSIZE];

	e_phoff32 = e_phoff64 = 0;
	vd = NULL;
	sd = NULL;

	if (SADUMP_DUMPFILE()) {
		sd = get_sadump_data();
		is_32_bit = FALSE;
		sprintf(typename, "sadump");
	} else if (pc->flags2 & QEMU_MEM_DUMP) {
		vd = get_kdump_vmcore_data();
		if (vd->flags & KDUMP_ELF32)
			is_32_bit = TRUE;
		else
			is_32_bit = FALSE;
		sprintf(typename, "qemu mem dump");
	} else
		return;

	if (!readmem(symbol_value("kexec_crash_image"), KVADDR,
		     &kexec_crash_image_p, sizeof(ulong),
		     "kexec backup region: kexec_crash_image",
		     QUIET|RETURN_ON_ERROR))
		goto error;

	if (!kexec_crash_image_p) {
		if (CRASHDEBUG(1))
			error(INFO, "%s: kexec_crash_image not loaded\n", typename);
		return;
	}

	kimage_segment_len = get_array_length("kimage.segment", NULL,
					      STRUCT_SIZE("kexec_segment"));

	if (!readmem(kexec_crash_image_p + MEMBER_OFFSET("kimage", "segment"),
		     KVADDR, buf, MEMBER_SIZE("kimage", "segment"),
		     "kexec backup region: kexec_crash_image->segment",
		     QUIET|RETURN_ON_ERROR))
		goto error;

	elfcorehdr_p = 0;
	for (i = 0; i < kimage_segment_len; ++i) {
		char e_ident[EI_NIDENT];
		ulong mem;

		mem = ULONG(buf + i * STRUCT_SIZE("kexec_segment") +
			    MEMBER_OFFSET("kexec_segment", "mem"));
		if (!mem)
			continue;

		if (!readmem(mem, PHYSADDR, e_ident, SELFMAG,
			     "elfcorehdr: e_ident",
			     QUIET|RETURN_ON_ERROR))
			goto error;

		if (strncmp(ELFMAG, e_ident, SELFMAG) == 0) {
			elfcorehdr_p = mem;
			break;
		}
	}
	if (!elfcorehdr_p) {
		if (CRASHDEBUG(1))
			error(INFO,
	"%s: elfcorehdr not found in segments of kexec_crash_image\n", typename);
		goto error;
	}
	
	if (is_32_bit) {
		if (!readmem(elfcorehdr_p, PHYSADDR, buf, STRUCT_SIZE("elf32_hdr"),
			"elfcorehdr", QUIET|RETURN_ON_ERROR))
			goto error;

		e_phnum = USHORT(buf + MEMBER_OFFSET("elf32_hdr", "e_phnum"));
		e_phentsize = USHORT(buf + MEMBER_OFFSET("elf32_hdr", "e_phentsize"));
		e_phoff32 = ULONG(buf + MEMBER_OFFSET("elf32_hdr", "e_phoff"));
	} else {
		if (!readmem(elfcorehdr_p, PHYSADDR, buf, STRUCT_SIZE("elf64_hdr"),
			    "elfcorehdr", QUIET|RETURN_ON_ERROR))
			goto error;

		e_phnum = USHORT(buf + MEMBER_OFFSET("elf64_hdr", "e_phnum"));
		e_phentsize = USHORT(buf + MEMBER_OFFSET("elf64_hdr", "e_phentsize"));
		e_phoff64 = ULONG(buf + MEMBER_OFFSET("elf64_hdr", "e_phoff"));
	}

	backup_src_start = backup_src_size = backup_offset = 0;

	for (i = 0; i < e_phnum; ++i) {
		uint32_t p_type;
		Elf32_Off p_offset32;
		Elf64_Off p_offset64;
		Elf32_Addr p_paddr32;
		Elf64_Addr p_paddr64;
		uint32_t p_memsz32;
		uint64_t p_memsz64;

		if (is_32_bit) {
			if (!readmem(elfcorehdr_p + e_phoff32 + i * e_phentsize,
				    PHYSADDR, buf, e_phentsize,
				    "elfcorehdr: program header",
				    QUIET|RETURN_ON_ERROR))
				goto error;

			p_type = UINT(buf+MEMBER_OFFSET("elf32_phdr","p_type"));
			p_offset32 = ULONG(buf+MEMBER_OFFSET("elf32_phdr","p_offset"));
			p_paddr32 = ULONG(buf+MEMBER_OFFSET("elf32_phdr","p_paddr"));
			p_memsz32 = ULONG(buf+MEMBER_OFFSET("elf32_phdr","p_memsz"));
		} else {
			if (!readmem(elfcorehdr_p + e_phoff64 + i * e_phentsize,
				    PHYSADDR, buf, e_phentsize,
				    "elfcorehdr: program header",
				    QUIET|RETURN_ON_ERROR))
				goto error;

			p_type = UINT(buf+MEMBER_OFFSET("elf64_phdr","p_type"));
			p_offset64 = ULONG(buf+MEMBER_OFFSET("elf64_phdr","p_offset"));
			p_paddr64 = ULONG(buf+MEMBER_OFFSET("elf64_phdr","p_paddr"));
			p_memsz64 = ULONG(buf+MEMBER_OFFSET("elf64_phdr","p_memsz"));
		}

		/*
		 * kexec marks backup region PT_LOAD by assigning
		 * backup region address in p_offset, and p_addr in
		 * p_offsets for other PT_LOAD entries.
		 */
		if (is_32_bit) {
			if (p_type == PT_LOAD &&
			    p_paddr32 <= KEXEC_BACKUP_SRC_END &&
			    p_paddr32 != p_offset32) {

				backup_src_start = p_paddr32;
				backup_src_size = p_memsz32;
				backup_offset = p_offset32;

				if (CRASHDEBUG(1))
					error(INFO,
				"%s: kexec backup region found: "
				"START: %#016llx SIZE: %#016lx OFFSET: %#016llx\n",
				typename, backup_src_start, backup_src_size, backup_offset);

				break;
			}
		} else {
			if (p_type == PT_LOAD &&
			    p_paddr64 <= KEXEC_BACKUP_SRC_END &&
			    p_paddr64 != p_offset64) {

				backup_src_start = p_paddr64;
				backup_src_size = p_memsz64;
				backup_offset = p_offset64;

				if (CRASHDEBUG(1))
					error(INFO,
				"%s: kexec backup region found: "
				"START: %#016llx SIZE: %#016lx OFFSET: %#016llx\n",
				typename, backup_src_start, backup_src_size, backup_offset);

				break;
			}
		}
	}

	if (!backup_offset) {
		if (CRASHDEBUG(1))
	error(WARNING, "%s: backup region not found in elfcorehdr\n", typename);
		return;
	}

	bufsize = BUFSIZE;
	for (total = 0; total < backup_src_size; total += bufsize) {
		char backup_buf[BUFSIZE];
		int j;

		if (backup_src_size - total < BUFSIZE)
			bufsize = backup_src_size - total;

		if (!readmem(backup_offset + total, PHYSADDR, backup_buf,
			     bufsize, "backup source", QUIET|RETURN_ON_ERROR))
			goto error;

		/*
		 * We're assuming the backup region is initialized
		 * with 0 filled if kdump has not run.
		 */
		for (j = 0; j < bufsize; ++j) {
			if (backup_buf[j]) {

				if (SADUMP_DUMPFILE()) {
					sd->flags |= SADUMP_KDUMP_BACKUP;
					sd->backup_src_start = backup_src_start;
					sd->backup_src_size = backup_src_size;
					sd->backup_offset = backup_offset;
				} else if (pc->flags2 & QEMU_MEM_DUMP) {
					vd->flags |= QEMU_MEM_DUMP_KDUMP_BACKUP;
					vd->backup_src_start = backup_src_start;
					vd->backup_src_size = backup_src_size;
					vd->backup_offset = backup_offset;
				}

				if (CRASHDEBUG(1))
error(INFO, "%s: backup region is used: %llx\n", typename, backup_offset + total + j);

				return;
			}
		}
	}

	if (CRASHDEBUG(1))
		error(INFO, "%s: kexec backup region not used\n", typename);

	return;

error:
	error(WARNING, "failed to init kexec backup region\n");
}