ktstr 0.5.2

Test harness for Linux process schedulers
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
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
//! Run-loop orchestration for `KtstrVm`: spawning AP vCPU threads,
//! the freeze coordinator, the BPF map writer, the BSP loop, and
//! result collection. This is the kernel-boundary heart of the VMM
//! runtime — every method here runs after [`super::setup`] hands the
//! configured [`KtstrKvm`](super::kvm::KtstrKvm) over and before the
//! VM exits.
//!
//! Reopens [`impl KtstrVm`](super::KtstrVm) so the canonical struct
//! definition stays in [`super`].

use anyhow::{Context, Result};
use kvm_ioctls::VcpuExit;
use std::os::fd::AsRawFd;
use std::os::unix::thread::JoinHandleExt;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
use vm_memory::{GuestAddress, GuestMemory};
use vmm_sys_util::epoll::{ControlOperation, Epoll, EpollEvent, EventSet};
use vmm_sys_util::eventfd::{EFD_NONBLOCK, EventFd};
use vmm_sys_util::timerfd::TimerFd;

use crate::monitor;

use super::exit_dispatch::{self, ExitAction, classify_exit, vcpu_run_loop_unified};
use super::host_comms::BulkDrainResult;
use super::pi_mutex::PiMutex;
use super::result::{VmResult, VmRunState};
use super::vcpu::{
    ApFreezeHandles, BpfMapWriteParams, ImmediateExitHandle, VcpuThread, WatchpointArm,
    duration_to_jiffies, load_probe_bss_offset, open_vcpu_perf_capture, pin_current_thread,
    register_vcpu_signal_handler, self_arm_watchpoint, set_rt_priority, set_thread_cpumask,
    vcpu_signal,
};
use super::vmlinux::{cached_vmlinux_bytes, find_vmlinux};
use super::{
    KtstrVm, console, host_comms, vcpu_panic, virtio_blk, virtio_console, virtio_net, wire,
};

#[cfg(target_arch = "aarch64")]
use super::aarch64::kvm;
#[cfg(target_arch = "x86_64")]
use super::x86_64::kvm;

// `DRAM_BASE` is defined in `super` and used here for guest-memory
// host-address resolution. The const is arch-gated; the import
// follows the same gating implicitly via where it is consumed.
use super::DRAM_BASE;

mod dispatch;
mod lazy_init;
mod snapshot;
mod state;
mod watchpoint;

#[cfg(test)]
mod bss_tests;

#[cfg(test)]
mod early_snapshot_guard_tests;

use self::dispatch::{BulkDispatchSinks, dispatch_bulk_message};
#[allow(unused_imports)]
use self::lazy_init::{
    try_init_owned_accessor_with_hint, try_init_owned_prog_accessor_with_hint,
    try_init_prog_per_cpu_offsets,
};
#[allow(unused_imports)]
use self::snapshot::{
    VmlinuxSymbolCache, arm_user_watchpoint, decode_snapshot_request, frame_snapshot_reply,
    poll_eventfd_until_ready_or_timeout, snapshot_tagged_path,
};
use self::state::{
    BspExitReason, FREEZE_RENDEZVOUS_TIMEOUT, FreezeState, SnapshotRequest,
    compute_periodic_boundaries_ns, periodic_tag,
};
use self::watchpoint::{WatchpointPublishResult, republish_watchpoint_on_rebind};

/// Test-only seam: when set, the exit_kind gate's `translate_any_kva`
/// call returns `None` unconditionally — driving the same code path
/// that fires when a previously-published `*scx_root->exit_kind` KVA
/// can no longer translate (slab page freed during teardown). E2E
/// tests for the silent-drop fixes flip this to deterministically
/// exercise the suppression + BPF-latch-rescue branches without
/// staging a real translate failure (which depends on
/// timing-sensitive teardown race windows).
///
/// `pub` because integration tests under `tests/` link against the
/// library's public surface and `#[cfg(test)]`-gated statics are
/// invisible across that boundary. `#[doc(hidden)]` keeps the symbol
/// out of the published rustdoc surface. Production callers must
/// never flip this — setting it forces dump suppression on every
/// dump-eligible exit, masking real failures (unless
/// [`FREEZE_COORD_TEST_FORCE_BSS_TRIGGERED`] is also set, which would
/// instead synthesize a phantom dump on every dump-eligible exit via
/// the BPF-latch rescue path).
#[doc(hidden)]
pub static FREEZE_COORD_TEST_FORCE_TRANSLATE_NONE: AtomicBool = AtomicBool::new(false);

/// Test-only seam: when set, [`bss_read_state`] returns
/// `BssReadState::Triggered` unconditionally — simulating a BPF probe
/// latch fire. Combined with [`FREEZE_COORD_TEST_FORCE_TRANSLATE_NONE`]
/// this drives the "gate would suppress but latch rescues — proceed
/// with dump" path that the silent-drop fix introduced to recover
/// dumps when `*scx_root->exit_kind` is unreadable but the BPF probe
/// has historically observed an error-class exit. See the partner
/// static's doc for visibility / production-safety guidance.
#[doc(hidden)]
pub static FREEZE_COORD_TEST_FORCE_BSS_TRIGGERED: AtomicBool = AtomicBool::new(false);

/// Three-way result of polling the BPF probe's `.bss` latch via the
/// cached guest-physical-address path used by [`bss_read_state`].
///
/// `read_u32` returns `0` for two semantically distinct reasons: the
/// probe has not latched yet (genuine "no fire") AND the cached PA no
/// longer resolves to a live DRAM region (out-of-bounds, hole between
/// regions). Conflating the two masks a stale-cache regression as
/// "still waiting for the trigger" and lets the freeze coordinator
/// drift past a real fire when the probe has been torn down or its
/// vmalloc page recycled. Each consumer decides how to react —
/// production gates the err_triggered flag on `Triggered` only and
/// surfaces `OutOfBounds` as a diagnostic so an operator can correlate
/// late-run BSS misses with map-idr churn.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum BssReadState {
    /// Cache is unset (probe not yet discovered) or `mem` is None
    /// (no NUMA layout published yet — pre-boot window). The read
    /// path short-circuits without touching guest memory.
    NotResolved,
    /// Cache is set and the PA is in-bounds, but the latched u32 is
    /// still `0`. The probe has not flipped its sticky 0→1 latch yet.
    NotTriggered,
    /// Cache is set and the PA is in-bounds; the read returned a
    /// non-zero value. The probe has latched its
    /// `ktstr_err_exit_detected` flag.
    Triggered,
    /// Cache is set but the PA falls outside every live DRAM region.
    /// Distinct from `NotTriggered` so callers can warn on a stale
    /// cache without conflating it with "no fire yet". A bare
    /// `read_u32` on the same PA returns `0` per
    /// `monitor::reader::GuestMem::read_scalar`'s OOB-zero path,
    /// which would hide the regression.
    OutOfBounds,
}

/// Resolve the BPF `.bss` latch read into the three-way
/// [`BssReadState`].
///
/// Pure function so the freeze coordinator's poll loop can be tested
/// in isolation — drives the same `mem.read_u32` and `region_avail`
/// calls the production loop performs at the `bss_state` binding,
/// but without booting a VM. The `OutOfBounds` branch uses
/// `region_avail(pa) >= 4` to confirm the cached PA still resolves
/// to a 4-byte-readable mapping; without that check, an OOB PA would
/// silently report `NotTriggered` because
/// [`monitor::reader::GuestMem::read_u32`] returns zeroes for
/// out-of-bounds PAs.
pub(super) fn bss_read_state(
    mem: Option<&monitor::reader::GuestMem>,
    cached_pa: Option<u64>,
) -> BssReadState {
    match (mem, cached_pa) {
        (Some(m), Some(pa)) => {
            if m.region_avail(pa) < 4 {
                BssReadState::OutOfBounds
            } else if m.read_u32(pa, 0) != 0 {
                BssReadState::Triggered
            } else {
                BssReadState::NotTriggered
            }
        }
        _ => BssReadState::NotResolved,
    }
}

/// Combine the watchpoint hit latch and the bss-latch state into the
/// run-loop's "fire this iteration" verdict. The hardware watchpoint
/// is the primary path (synchronous KVM_EXIT_DEBUG delivery); the
/// bss-latch read is the fallback for kernels where the watchpoint
/// could not be armed (no `scx_root` symbol, BTF stripped of
/// `scx_sched`, KVM_SET_GUEST_DEBUG ioctl rejected). Either signal
/// alone is sufficient to start the late-trigger freeze.
///
/// Only [`BssReadState::Triggered`] counts as a fire on the bss
/// path — `OutOfBounds`, `NotResolved`, and `NotTriggered` all
/// resolve to "no observable fire this iteration" so a stale
/// cached PA after probe unload cannot
/// synthesise a phantom fire from arbitrary DRAM bytes.
pub(super) fn compute_err_triggered(watchpoint_hit: bool, bss_state: BssReadState) -> bool {
    watchpoint_hit || matches!(bss_state, BssReadState::Triggered)
}

/// Predicate that distinguishes a watchpoint-only trigger from a
/// bss-confirmed trigger. Returns `true` when the hardware watchpoint
/// fired but the BPF `.bss` latch did NOT — the trigger is
/// "watchpoint sees a write to `*scx_root->exit_kind`, probe hasn't
/// latched the error class (yet)".
///
/// Used at the `freeze_and_capture` call site (a closure inside
/// the run-loop, not a free fn) to compute the `gate_on_exit_kind`
/// argument: a watchpoint-only trigger sets
/// the gate because the watchpoint catches every write (including the
/// init/teardown clean values `SCX_EXIT_NONE` / `SCX_EXIT_DONE` that
/// would synthesise a bogus dump without the gate); a bss-confirmed
/// trigger skips the gate because `probe.bpf.c:687`'s
/// `__sync_val_compare_and_swap(&ktstr_err_exit_detected, 0, 1)`
/// already proved the kernel observed `kind >= SCX_EXIT_ERROR` at the
/// tp_btf hook firing instant.
pub(super) fn compute_watchpoint_only_trigger(
    watchpoint_hit: bool,
    bss_state: BssReadState,
) -> bool {
    watchpoint_hit && !matches!(bss_state, BssReadState::Triggered)
}

/// Snake-case label for a [`BssReadState`] suitable for embedding in
/// the [`crate::monitor::dump::DegradedFailureDumpReport::bss_latch_state`]
/// wire field. Stable wire format: callers (auto-repro tail renderer,
/// operator `jq` inspection) match against the exact strings returned
/// here — adding a new state requires a new label rather than mutating
/// an existing one.
pub(super) fn bss_state_label(state: BssReadState) -> &'static str {
    match state {
        BssReadState::Triggered => "triggered",
        BssReadState::NotTriggered => "not_triggered",
        BssReadState::OutOfBounds => "out_of_bounds",
        BssReadState::NotResolved => "not_resolved",
    }
}

/// Walk back from `cap` to the largest valid UTF-8 char boundary
/// `<= cap` (and `<= s.len()`). Returns the safe slice length: the
/// caller may then do `&s[..returned]` without panicking.
///
/// Used by the stderr last-ditch preservation paths (`emit_json` /
/// `emit_degraded_json` closure fallbacks + end-of-coord drain) where
/// the naive `&s[..cap]` would panic if `cap` lands inside a multi-
/// byte UTF-8 sequence. The serde-serialized FailureDumpReport carries
/// non-ASCII bytes in any `String` field — task `comm` via
/// `String::from_utf8_lossy` yields U+FFFD (3 bytes) for invalid input,
/// and operator-supplied tags or kernel symbol strings can include
/// Unicode. Without this walk, an ENOSPC/EROFS-triggered stderr emit
/// can panic instead of preserving the payload — exactly the silent-
/// drop failure mode the fallback exists to prevent.
///
/// Worst-case retreat is 3 bytes (UTF-8 sequences are 1-4 bytes; the
/// boundary at byte 0 of a valid sequence is always a char boundary,
/// and 0 itself is always a boundary). Returns 0 for an empty input
/// or when `cap == 0`.
pub(super) fn utf8_safe_truncate_len(s: &str, cap: usize) -> usize {
    let bounded = cap.min(s.len());
    (0..=bounded)
        .rev()
        .find(|&i| s.is_char_boundary(i))
        .unwrap_or(0)
}

/// Indices of vCPUs whose register snapshot is `None` — the vCPUs
/// that either failed to park during the freeze rendezvous or whose
/// `KVM_GET_REGS` ioctl errored mid-shutdown. Surfaced alongside the
/// structured summary in every stderr last-ditch preservation path
/// (`emit_json` fallback, end-of-coord drain) so an operator sees
/// WHICH vCPUs failed — not just the count.
///
/// Pre-computed once at each caller (Captured arm, drain) and passed
/// by `&[usize]` reference into the emit closure. Centralised here
/// so the filter_map pattern lives at one location — a future change
/// to "what counts as a stalled vCPU" updates this one fn rather than
/// every emit site.
pub(super) fn vcpu_none_indices(
    vcpu_regs: &[Option<crate::monitor::dump::VcpuRegSnapshot>],
) -> Vec<usize> {
    vcpu_regs
        .iter()
        .enumerate()
        .filter_map(|(i, r)| if r.is_none() { Some(i) } else { None })
        .collect()
}

/// Format the trailing `path_part` clause for the emit closures'
/// tracing summary line. Disambiguates 3 states an operator needs
/// to distinguish at-a-glance:
///
/// - `(Some(p), _)` — sink configured AND write succeeded; renders
///   ` -> {p}` so the operator sees the on-disk dump path inline.
/// - `(None, true)` — sink configured but the atomic write failed;
///   renders ` (atomic write failed; see preceding warn)` so the
///   operator knows to scroll up for the warn carrying the underlying
///   `io::Error`. The preceding warn fires at the and_then Err arm
///   in both [`emit_json`]-class closures.
/// - `(None, false)` — operator never wired `failure_dump_path`;
///   renders ` (no file sink)` — the quiet expected-behavior case
///   for verifier/shell/template builders.
///
/// Centralised here so emit_json and emit_degraded_json don't each
/// carry the same 3-arm match inline; future emit-site additions reuse
/// the disambiguation rule for free.
pub(super) fn format_path_part(path_str: Option<&str>, write_failed: bool) -> String {
    match (path_str, write_failed) {
        (Some(p), _) => format!(" -> {p}"),
        (None, true) => " (atomic write failed; see preceding warn)".to_string(),
        (None, false) => " (no file sink)".to_string(),
    }
}

/// Format the trailing "(truncated to N bytes ...)" marker appended to
/// stderr last-ditch preservation payload-head lines when the
/// UTF-8-safe truncation actually dropped bytes. Returns empty when
/// the truncated head reaches the full payload (no truncation
/// happened) so the caller's `eprintln!` reads cleanly without a
/// no-op clause.
///
/// Mirrors the `STDERR_DUMP_CAP` cascade pattern used by every
/// stderr-fallback site (emit_json / emit_degraded_json / end-of-coord
/// drain). The cap value is supplied by the caller because the drain
/// site uses a distinct `DRAIN_STDERR_DUMP_CAP` constant for its own
/// scope, but the truncation-marker format string is the same.
///
/// Centralised so the marker text lives in one place rather than three
/// byte-identical inline copies; also exposes the truncation contract
/// for unit testing.
pub(super) fn format_truncation_marker(head_end: usize, total_len: usize) -> String {
    if head_end < total_len {
        let dropped = total_len - head_end;
        format!(
            " (truncated to {head_end} bytes at UTF-8 boundary; {dropped} of {total_len} payload bytes dropped — summary above)"
        )
    } else {
        String::new()
    }
}

/// Shared prefix for every stderr last-ditch preservation `summary`
/// line emitted on a failed atomic-publish OR serialize-failure
/// path. 13 emit sites carry the same
/// `"freeze-coord: STDERR-PRESERVED summary"` literal at the head of
/// their `eprintln!` / `stderr_summary` closure return:
/// - run_vm emit_json Captured (write_failed)
/// - run_vm Captured serialize-failure (serde_json::to_string error)
/// - run_vm emit_degraded_json (write_failed)
/// - TLV CAPTURE Captured + Degraded
/// - on-demand periodic Captured + Degraded
/// - on-demand user-watchpoint Captured + Degraded
/// - on-demand early-snapshot Degraded
/// - dual-snapshot early-pre-late-degraded
/// - dual-snapshot early-only-late-suppressed
/// - end-of-coord drain
///
/// Suffix after this prefix varies per site:
/// `" ({variant}, write failed): {body}"` for most;
/// `" ({variant}, serialize failed): {body}"` for the serde-error path;
/// `": {body}"` for the end-of-coord drain (no per-variant context
/// because `early_retain_tag` carries it separately).
///
/// Operator-grep contract: every stderr-fallback site emits a line
/// matchable by this prefix so a log scraper can pin the entire
/// stderr-preservation surface with one regex. The const exists so
/// adding a new emit site (e.g. a future watchpoint class) reuses
/// the prefix automatically + so a future prefix change updates one
/// site instead of 13.
pub(super) const SNAPSHOT_SUMMARY_PREFIX: &str = "freeze-coord: STDERR-PRESERVED summary";

/// Atomically write a JSON dump payload to a sibling of `dump_path`
/// keyed by `tag`, surfacing operator-visible stderr preservation
/// when the configured sink fails. Used by every on-demand dispatch
/// site (TLV CAPTURE / periodic / user-watchpoint / early-snapshot
/// Degraded) so the 7 sites share one atomic-publish + stderr
/// fallback contract.
///
/// Atomic-publish mirrors the [`KtstrVm`]-scope emit_json /
/// emit_degraded_json cascade in steps 1-4 but DIVERGES from them on
/// step-4 (parent-fsync) failure handling: this tagged-sibling helper
/// rolls back the rename and returns `Err`, whereas the canonical-path
/// closures log the durability gap and return `Ok` per user direction
/// ("operator-has-the-data" discipline on the operator-facing main
/// dump). The split is intentional and documented at both call sites
/// (search "operator-has-the-data discipline" in this file).
///
/// 1. Write payload to a sibling `.json.tmp`.
/// 2. `sync_all()` flushes file data + metadata to disk.
/// 3. `rename()` atomically swaps `.json.tmp` into the tagged path
///    (POSIX rename atomicity).
/// 4. `sync_all()` on the parent-directory fd flushes the rename so a
///    host crash post-rename does not lose the dump on ext4/xfs/btrfs.
///    Per CF3 — POSIX rename(2) atomicity is about ordering visible
///    to other processes, not durability across crashes; directory-
///    entry durability requires fsync on the parent dir, matching
///    the pattern database engines (SQLite, RocksDB) use for journal
///    commits.
///
/// On failure (ENOSPC / EROFS / EACCES / EIO) the helper:
///
/// - Best-effort removes the leftover `.json.tmp` so a future
///   operator does not see a stale sibling next to a stale dump.
/// - If the failure was specifically at the post-rename parent-dir
///   fsync (rename succeeded but durability could not be verified),
///   the helper rolls back by removing the tagged file so the
///   `Err` return remains consistent with "no dump visible to
///   consumers" — keeping the caller's bridge bookkeeping aligned
///   with the filesystem. If rollback itself fails, a warn fires
///   carrying both errors plus the cue
///   "tagged file may be visible on disk despite Err return —
///   operator may need to reconcile bridge state with filesystem"
///   so the operator can spot the rare asymmetric state and
///   reconcile manually.
/// - Fires `tracing::warn!` with `warn_msg` plus the atomic-publish
///   path metadata (`path`, `tmp_path`) and the underlying
///   `io::Error`.
/// - Emits the caller-supplied `stderr_summary` line carrying the
///   structured signals an operator needs for triage (per-site
///   field set; the closure owns its own field choices since each
///   site carries different fields — TLV CAPTURE has request_id +
///   map_count + vcpu_regs_count, periodic has idx, Degraded has
///   reason, etc.).
/// - Emits a payload-head line with [`format_truncation_marker`]
///   appended so the operator sees the structured summary AND a
///   UTF-8-boundary-safe head of the JSON for forensic context.
///
/// Returns:
/// - `Ok(None)` when `dump_path` is `None` — verifier / shell /
///   template iteration that never wired a sink, per ADV V11-1's
///   "no capture requested" case. `stderr_summary` is NOT invoked.
/// - `Ok(Some(tagged))` on successful publish with the published
///   path for the caller's bookkeeping.
/// - `Err(io::Error)` after the stderr fallback has fired. Caller
///   decides on bridge placeholder / reply status / consume-on-
///   success bookkeeping — the helper only owns the file write
///   and the stderr preservation. Today's 10 callers split:
///   7 on-demand sites (TLV CAPTURE / periodic / user-watchpoint /
///   early-snapshot Degraded) `let _ =` the Result because the
///   stderr fallback is their entire recovery contract; the 3
///   early-preservation sites (early-pre-late-degraded,
///   early-only-late-suppressed, end-of-coord drain) destructure
///   `Ok(Some(tagged))` to log a success info trace with the
///   published path and the early-snapshot bookkeeping (consume
///   on success, retain for drain retry on failure).
///
/// # Concurrency
///
/// Single-threaded caller per (dump_path, tag). The freeze
/// coordinator's run-loop closure invokes the 7 on-demand sites
/// sequentially, so the assumption holds today. Concurrent
/// callers with the same tag would race on the `.json.tmp`
/// sibling — `File::create(tmp_path)` with O_TRUNC would clobber
/// the in-flight tmp write of the other caller, silently losing
/// one thread's payload. A future caller dispatched from a
/// different thread must either uniquify `tmp_path` (e.g. via
/// `tempfile::NamedTempFile::new_in(parent)`) or serialize on
/// `tag` before invoking this helper.
/// Same 16 KiB cap as the run_vm-scope `STDERR_DUMP_CAP` and the
/// drain-scope `DRAIN_STDERR_DUMP_CAP`. Operator-grep stability:
/// every stderr-fallback site emits a payload head bounded by the
/// same cap, so an operator-side log parser observing the
/// `truncated to {N} bytes` marker can pin the upper bound at
/// 16 KiB across the whole stderr-fallback surface. Module-scoped
/// (not nested inside [`write_to_tagged_path`]) so unit tests in
/// the sibling test module can import the same value rather than
/// duplicating the literal — a future bump to e.g. 32 KiB updates
/// the production cap AND the test's boundary construction in
/// lockstep.
pub(super) const ONDEMAND_STDERR_DUMP_CAP: usize = 16 * 1024;

pub(super) fn write_to_tagged_path(
    dump_path: Option<&std::path::Path>,
    tag: &str,
    json: &str,
    stderr_summary: impl FnOnce() -> String,
    warn_msg: &'static str,
) -> std::io::Result<Option<std::path::PathBuf>> {
    let Some(base_path) = dump_path else {
        // Ok(None) semantic: dump_path was unwired by the caller
        // (verifier / shell / template iteration; ADV V11-1 "no
        // capture requested" case). Stable contract — the
        // `EarlySnapshotGuard::drain_to_disk` Ok(None) arm carries
        // a `debug_assert!(false, ...)` that depends on this
        // semantic NOT widening to other no-write conditions
        // (e.g. unwritable parent), since the guard's own
        // pre-filter guarantees dump_path is Some when reaching
        // the helper. A widened Ok(None) would fire the
        // debug_assert in test/debug spuriously.
        return Ok(None);
    };

    let tagged = snapshot_tagged_path(base_path, tag);
    if let Some(parent) = tagged.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    let tmp_path = tagged.with_extension("json.tmp");
    let write_atomic = || -> std::io::Result<()> {
        use std::io::Write as _;
        let mut f = std::fs::File::create(&tmp_path)?;
        f.write_all(json.as_bytes())?;
        f.sync_all()?;
        drop(f);
        std::fs::rename(&tmp_path, &tagged)?;
        // After rename succeeds, parent-dir fsync failure means the
        // file IS visible on disk but its directory entry is not
        // durable across a host crash. To preserve the contract that
        // `Err` means "no dump visible to consumers" (so the caller's
        // bridge bookkeeping stays consistent with the filesystem
        // state), roll back the rename by removing the tagged file.
        // Best-effort: if the rollback itself fails, the operator-
        // visible asymmetric state is logged and the original Err
        // propagates.
        if let Some(dir) = tagged.parent()
            && let Err(parent_fsync_err) = std::fs::File::open(dir).and_then(|d| d.sync_all())
        {
            if let Err(rollback_err) = std::fs::remove_file(&tagged) {
                tracing::warn!(
                    path = %tagged.display(),
                    parent_fsync_error = %parent_fsync_err,
                    rollback_error = %rollback_err,
                    "freeze-coord: parent-dir fsync failed AND rollback of tagged file also failed; tagged file may be visible on disk despite Err return — operator may need to reconcile bridge state with filesystem"
                );
            }
            return Err(parent_fsync_err);
        }
        Ok(())
    };
    match write_atomic() {
        Ok(()) => Ok(Some(tagged)),
        Err(e) => {
            let _ = std::fs::remove_file(&tmp_path);
            tracing::warn!(
                path = %tagged.display(),
                tmp_path = %tmp_path.display(),
                error = %e,
                "{warn_msg}"
            );
            eprintln!("{}", stderr_summary());
            let head_end = utf8_safe_truncate_len(json, ONDEMAND_STDERR_DUMP_CAP);
            let truncated_marker = format_truncation_marker(head_end, json.len());
            // Per-site context (which on-demand path, which retain-tag,
            // why it fired) lives in the structured summary line above —
            // the caller supplied it via `stderr_summary`. The payload-
            // head line stays terse to match the run_vm-scope drain
            // discipline at end-of-coord; an operator-grep on
            // "STDERR-PRESERVED payload head" matches every stderr
            // fallback site uniformly.
            eprintln!(
                "freeze-coord: STDERR-PRESERVED payload head{}: {}",
                truncated_marker,
                &json[..head_end]
            );
            Err(e)
        }
    }
}

/// Outcome of one freeze-and-capture cycle. Replaces the prior
/// `Option<(FailureDumpReport, Instant)>` return so the late-trigger
/// dispatch can distinguish three semantically distinct cases instead
/// of conflating "aborted with reason worth recording" and
/// "legit no-dump suppression" into a single `None`:
///
/// - [`Self::Captured`] — full dump assembled; emit as
///   [`crate::monitor::dump::SCHEMA_SINGLE`] (or wrapped into
///   [`crate::monitor::dump::SCHEMA_DUAL`] by the dual-snapshot path).
/// - [`Self::Degraded`] — trigger fired but capture aborted in a way
///   the operator needs to see (rendezvous timed out, etc.). Carries a
///   pre-built [`crate::monitor::dump::DegradedFailureDumpReport`] for
///   immediate emission via the
///   [`crate::monitor::dump::SCHEMA_DEGRADED`] schema. Per memory
///   `feedback_no_silent_drops`: the trigger fired, an operator must
///   see something — the degraded JSON IS that something.
/// - [`Self::Suppressed`] — exit_kind gate decided the trigger was a
///   benign init / teardown write (kind &lt; SCX_EXIT_ERROR) and the
///   BPF `.bss` latch agreed it was not an error. Legit "no dump
///   needed" — no JSON emitted. The watchpoint hit flag is reset and
///   the coordinator transitions to [`FreezeState::Done`] so the
///   late-trigger machine stops watching for further error-class
///   triggers on this rebind cycle. On-demand captures (TLV,
///   periodic, user-watchpoint) remain serviceable since they don't
///   gate on `freeze_state`.
// `Captured` carries a full [`crate::monitor::dump::FailureDumpReport`]
// inline (~680 bytes of map / vcpu_regs / walker data); the other
// variants are small. Boxing the dominant variant would double-
// allocate on the success path — the closure returns the outcome to
// a single immediate consumer (the late-trigger dispatch at one
// site, the on-demand sites at three sites) which destructures and
// drops the box right away. Stack-passing the unboxed
// FailureDumpReport reuses the closure's already-allocated frame.
#[allow(clippy::large_enum_variant)]
pub(super) enum LateCaptureOutcome {
    /// Successfully captured a full failure dump.
    Captured(crate::monitor::dump::FailureDumpReport, Instant),
    /// Aborted in a way that warrants surfacing a degraded JSON.
    /// Boxed to keep the enum's discriminant size bounded — the
    /// degraded report carries per-vCPU register data inline.
    Degraded(Box<crate::monitor::dump::DegradedFailureDumpReport>),
    /// Legit suppression: gate decided clean exit. The watchpoint
    /// hit is reset and the coordinator transitions to
    /// [`FreezeState::Done`]. No late dump emitted. In dual-snapshot
    /// mode, if an early Captured snapshot was previously held, it
    /// is preserved to the
    /// [`crate::monitor::dump::SNAPSHOT_TAG_EARLY_ONLY_LATE_SUPPRESSED`]
    /// tagged sibling path so the operator can read the early
    /// observation; see the late-trigger `Suppressed` arm in
    /// `run_bsp_loop` for the emit detail.
    Suppressed,
}

/// Owns the dual-snapshot early-trigger `FailureDumpReport` plus the
/// drain bookkeeping (retain tag from a failed late-trigger arm
/// write, dump path, dual-snapshot gate). The guard exists to close
/// a silent-drop window: the freeze coordinator's closure runs on
/// a spawned thread, and a panic anywhere in the closure body would
/// unwind past the end-of-coord drain that flushes a held
/// `early_snapshot` to disk. Per memory `feedback_no_silent_drops`,
/// every Captured early MUST reach disk regardless of how the
/// closure exits.
///
/// The guard's [`Self::drain_to_disk`] runs the same emit logic the
/// end-of-coord drain used to inline (atomic publish via
/// [`write_to_tagged_path`] + stderr fallback on write failure).
/// The [`Drop`] impl invokes the same drain so an unwinding panic
/// flushes the snapshot before the thread tears down. Both paths
/// are idempotent: `self.snapshot.take()` returns `None` on second
/// call, making a normal-path drain followed by Drop a no-op.
///
/// Cross-cutting state captured at construction:
/// - `dump_path`: cloned from the closure's `freeze_coord_dump_path`
///   so the Drop body has its own owned PathBuf without lifetime
///   dependencies on the closure's locals.
/// - `dual_snapshot`: copy of `freeze_coord_dual_snapshot` so the
///   drain honors the same gate the end-of-coord drain used to.
/// - `retain_tag`: set by the late-trigger Degraded / Suppressed
///   arms when their tagged-sibling write fails, so the drain
///   lands the recovered file at the operator-correct path rather
///   than the default NEVER_FIRED tag (per N8-2 / #72).
///
/// Excluded from the guard (verified to NOT need panic-safe
/// preservation):
/// - `early_max_age_jiffies` / `early_threshold_jiffies` — consumed
///   only at the late-Captured `DualFailureDumpReport` assembly
///   site; if lost on panic the on-disk drained file just lacks
///   these annotations and the snapshot itself survives.
/// - `early_degraded_reason` — consumed only at the late-trigger
///   `early_skipped_reason` calculator; the underlying degraded
///   JSON already landed on disk at the
///   `SNAPSHOT_TAG_EARLY_DEGRADED` sibling regardless of panic.
///
/// Drop body must not panic (would abort the process via double-
/// panic on the unwinding thread). The drain body uses only
/// `serde_json::to_string` (panic-free for well-typed input),
/// `write_to_tagged_path` (returns `io::Result`, never panics), and
/// `vcpu_none_indices` (pure filter_map, panic-free). No additional
/// catch_unwind wrap needed at first land.
pub(super) struct EarlySnapshotGuard {
    pub(super) snapshot: Option<crate::monitor::dump::FailureDumpReport>,
    pub(super) retain_tag: Option<&'static str>,
    pub(super) dump_path: Option<std::path::PathBuf>,
    pub(super) dual_snapshot: bool,
}

impl EarlySnapshotGuard {
    /// Flush the held snapshot to its tagged sibling via the shared
    /// atomic-publish helper. Idempotent: a second call after the
    /// first taken the snapshot returns immediately. No-op when
    /// `dual_snapshot` is false, when `dump_path` is unset, or when
    /// `snapshot` is already None.
    ///
    /// Invoked at two sites: the end-of-coord drain (terminal
    /// normal-path flush) and the [`Drop`] impl (panic-unwind
    /// flush). Both produce the same operator-visible artifact.
    pub(super) fn drain_to_disk(&mut self) {
        if !self.dual_snapshot {
            return;
        }
        let Some(dump_path) = self.dump_path.as_deref() else {
            return;
        };
        let Some(early) = self.snapshot.take() else {
            return;
        };
        let drain_tag = self
            .retain_tag
            .unwrap_or(crate::monitor::dump::SNAPSHOT_TAG_EARLY_ONLY_LATE_NEVER_FIRED);
        match serde_json::to_string(&early) {
            Ok(json) => {
                match write_to_tagged_path(
                    Some(dump_path),
                    drain_tag,
                    &json,
                    || {
                        let vcpu_none = vcpu_none_indices(&early.vcpu_regs);
                        format!(
                            "{SNAPSHOT_SUMMARY_PREFIX}: schema={} vcpu_regs_count={} vcpu_none_indices={:?} maps_count={} tasks_enriched={} json_bytes={}",
                            early.schema,
                            early.vcpu_regs.len(),
                            vcpu_none,
                            early.maps.len(),
                            early.task_enrichments.len(),
                            json.len(),
                        )
                    },
                    "freeze-coord: early-only (coord exit drain) write failed; emitting structured summary + payload head to stderr as last-ditch preservation",
                ) {
                    Ok(Some(tagged)) => {
                        tracing::info!(
                            path = %tagged.display(),
                            retain_tag_used = self.retain_tag.is_some(),
                            "freeze-coord: early snapshot preserved at coord exit"
                        );
                    }
                    Ok(None) => {
                        // Unreachable: write_to_tagged_path returns
                        // Ok(None) only when its dump_path argument
                        // is None — but drain_to_disk's own
                        // pre-filter at the `let Some(dump_path) =
                        // self.dump_path.as_deref()` arm above
                        // guarantees `Some(dump_path)` reaches the
                        // helper. A None here means a regression
                        // removed the pre-filter; debug builds
                        // fail-loud, release builds stay silent
                        // (matches the prior empty-arm semantics).
                        debug_assert!(
                            false,
                            "Ok(None) unreachable: drain_to_disk pre-filters \
                             dump_path before calling write_to_tagged_path; \
                             a None reaching here means the pre-filter was \
                             removed"
                        );
                    }
                    Err(_) => {}
                }
            }
            Err(e) => tracing::error!(
                error = %e,
                "freeze-coord: early-only (coord exit drain) JSON serialization \
                 failed (FailureDumpReport serialization is infallible for the \
                 concrete shape — see test \
                 failure_dump_report_serialization_is_infallible_for_max_synthetic_input \
                 in src/monitor/dump/tests.rs; if this error ever fires a new \
                 field with a fallible Serialize impl landed)"
            ),
        }
    }
}

impl Drop for EarlySnapshotGuard {
    fn drop(&mut self) {
        // Fires on closure exit (normal OR panic-unwind). Normal path
        // calls drain_to_disk() explicitly before exit, which takes()
        // the snapshot — so this Drop is a no-op in the normal case.
        // On panic-unwind, the normal drain didn't run; this Drop
        // ensures the captured early reaches disk.
        self.drain_to_disk();
    }
}

impl KtstrVm {
    /// Spawn threads and run the BSP. Returns all state needed for
    /// `collect_results`.
    ///
    /// # Failure-dump freeze
    ///
    /// When the BPF probe latches a sched_ext error-class exit
    /// (SCX_EXIT_ERROR / _BPF / _STALL), a host-side coordinator
    /// thread freezes every vCPU long enough to read BPF map state
    /// for post-mortem analysis. The freeze is transparent to test
    /// authors — the test still observes the same failure verdict
    /// and exit path — but adds up to ~10 ms of thaw latency to the
    /// failure path (the parked-vCPU poll cadence). Healthy runs
    /// never enter the freeze path; the latch only fires on an
    /// error-class scheduler exit.
    pub(super) fn run_vm(
        &self,
        run_start: Instant,
        mut vm: kvm::KtstrKvm,
        default_cpu_mask: Option<&[usize]>,
        effective_pinning_plan: Option<&super::host_topology::PinningPlan>,
    ) -> Result<VmRunState> {
        let com1 = Arc::new(PiMutex::new(console::Serial::new(console::COM1_BASE)));
        let com2 = Arc::new(PiMutex::new(console::Serial::new(console::COM2_BASE)));

        // Register serial EventFds with KVM's irqfd for interrupt-driven TX.
        // Split-irqchip mode lacks IOAPIC routing (LAPIC-only kernel
        // emulation; PIC/IOAPIC live in userspace and the framework
        // does not implement the userspace IOAPIC dispatch). Without
        // an IRQ delivery path the guest's serial driver hangs on the
        // first TX/RX wake — the kernel uart driver has no polling
        // fallback. Reject loudly so test setups exceeding the
        // 8-bit xAPIC limit (max APIC ID > 254) are caught here
        // instead of producing a silent guest hang.
        #[cfg(target_arch = "x86_64")]
        if vm.split_irqchip {
            anyhow::bail!(
                "serial COM1/COM2 require irqfd; split-irqchip mode \
                 has no IOAPIC and the kernel uart driver has no \
                 polling fallback — reduce topology so all APIC IDs \
                 are at or below 254 (MAX_XAPIC_ID)",
            );
        }
        #[cfg(target_arch = "x86_64")]
        {
            vm.vm_fd
                .register_irqfd(com1.lock().irq_evt(), console::COM1_IRQ)
                .context("register COM1 irqfd")?;
            vm.vm_fd
                .register_irqfd(com2.lock().irq_evt(), console::COM2_IRQ)
                .context("register COM2 irqfd")?;
        }
        #[cfg(target_arch = "aarch64")]
        {
            vm.vm_fd
                .register_irqfd(com1.lock().irq_evt(), kvm::SERIAL_IRQ)
                .context("register serial irqfd")?;
            vm.vm_fd
                .register_irqfd(com2.lock().irq_evt(), kvm::SERIAL2_IRQ)
                .context("register serial2 irqfd")?;
        }

        // Serialises on-demand captures against themselves: the
        // coordinator sets this Acquire-bool while a TLV-driven
        // snapshot dispatch runs and clears it on completion, so a
        // user-watchpoint hit firing during a CAPTURE-class request
        // does not open a second concurrent capture window. The TX
        // handler is single-threaded on the freeze coord, so the
        // gate's primary defence is against the user-watchpoint
        // dispatcher (which runs in the same iteration body after
        // pending TLV requests drain). Independent of `freeze_state`,
        // which governs only the error-class trigger machine —
        // on-demand captures must service even when
        // `freeze_state == Done` so post-failure `Op::Snapshot` calls
        // still work.
        let on_demand_in_flight = Arc::new(AtomicBool::new(false));

        // Host-side snapshot bridge. Owned by the freeze coordinator
        // and exposed back through `VmRunState` so test code can
        // drain captured reports after the VM exits. The bridge's
        // capture callback returns `None` — the coordinator never
        // calls `bridge.capture()`; instead it runs
        // `freeze_and_capture(false)` directly and stores the
        // resulting report via `bridge.store(name, report)` so the
        // host owns the entire capture pipeline.
        let snapshot_bridge = {
            let cb: crate::scenario::snapshot::CaptureCallback = Arc::new(|_| None);
            crate::scenario::snapshot::SnapshotBridge::new(cb)
        };

        // Probes-ready broadcast EventFd. Shared between the monitor
        // thread's slot-1 wait and the bpf-map-write thread's
        // accessor-init / map-discovery / probes-ready waits — all
        // of which previously slept on independent 100-200 ms timers
        // while polling guest kernel state via .bss latch reads.
        // Replacing the bare sleeps with `poll(POLLIN)` against this
        // eventfd lets ANY waiter that detects its own readiness
        // condition write 1, immediately waking every other waiter
        // for an early re-check. `EFD_NONBLOCK` keeps the writer's
        // `write()` from stalling if the counter is already
        // saturated; readers use `poll`, never `read`, so the level
        // stays high once any writer has fired and the wake fans out
        // to every cloned fd. `try_clone()` uses `dup(2)`, so all
        // clones share the same kernel counter — the broadcast
        // works across as many waiters as we hand out clones to.
        let probes_ready_evt = EventFd::new(EFD_NONBLOCK).context("create probes-ready EventFd")?;
        let probes_ready_evt_for_monitor = probes_ready_evt
            .try_clone()
            .context("clone probes-ready EventFd for monitor")?;
        let probes_ready_evt_for_bpf = probes_ready_evt
            .try_clone()
            .context("clone probes-ready EventFd for bpf-map-write")?;
        // The original is unused once both consumers hold their own
        // dup'd fds. Drop it eagerly so its file descriptor is freed
        // immediately rather than at the end of the run; the clones
        // share the same kernel counter via dup(2) and remain
        // independent.
        drop(probes_ready_evt);

        // Shared parked_evt: every vCPU thread + the virtio-blk
        // worker writes 1 to this counter-mode EventFd immediately
        // after its respective `parked.store(true, Release)` /
        // `paused.store(true, Release)`. The freeze coordinator's
        // rendezvous loop polls this fd alongside kill_evt and
        // bsp_done_evt instead of spin-sleeping on a 100µs cadence.
        // EFD_NONBLOCK so a writer never stalls; counter mode (no
        // EFD_SEMAPHORE) so a single drain consumes any number of
        // coalesced parked signals — the coordinator drains once
        // and re-checks every parked flag.
        //
        // Allocated BEFORE init_virtio_blk so we can plumb the fd
        // into the device's `set_parked_evt` setter immediately
        // after construction, before the worker spawns and observes
        // its first pause.
        let parked_evt = Arc::new(EventFd::new(EFD_NONBLOCK).context("create parked EventFd")?);
        // Shared thaw_evt: written by the freeze coordinator after
        // `freeze.store(false, Release)` so every parked vCPU
        // observes the thaw within microseconds rather than waiting
        // up to 10ms on `park_timeout`. Same EFD_NONBLOCK + counter
        // semantics as parked_evt.
        let thaw_evt = Arc::new(EventFd::new(EFD_NONBLOCK).context("create thaw EventFd")?);

        // Optional virtio-blk: `None` when no disks are attached,
        // `Some` when the builder has at least one `DiskConfig`.
        // Constructed BEFORE we tear down vm.vcpus so the helper
        // can still read `vm.guest_mem` and the irqchip state.
        let virtio_blk = self.init_virtio_blk(&vm)?;
        // Plumb the shared parked_evt into the device so its worker
        // wakes the freeze coordinator's rendezvous on park.
        if let Some(ref blk) = virtio_blk {
            blk.lock().set_parked_evt(parked_evt.clone());
        }

        // Optional virtio-net: `None` when the builder has no
        // `NetConfig` attached, `Some` when configured. Same
        // construction-before-vcpu-takedown rule as virtio-blk.
        let virtio_net = self.init_virtio_net(&vm)?;

        // Virtio-console for host→guest wake delivery. The setup_memory
        // path always emits the device's MMIO node on the kernel
        // cmdline (x86_64) / FDT (aarch64), so the kernel's
        // `virtio_mmio` driver probes for the device unconditionally.
        // The guest's `hvc0_poll_loop` blocks on `/dev/hvc0` and wakes
        // within microseconds when the host pushes a byte. The
        // coordinator and watchdog use this as the host→guest signal
        // channel: the monitor pushes `SIGNAL_VC_DUMP` for SysRq-D
        // dump requests (the dispatch is wake-byte-only — no SHM
        // control byte), the watchdog pushes `SIGNAL_VC_SHUTDOWN` for
        // graceful shutdown, and the bpf-map-write thread pushes
        // `SIGNAL_BPF_WRITE_DONE` to release `wait_for_map_write`.
        let mut vc = virtio_console::VirtioConsole::new();
        vc.set_mem((*vm.guest_mem).clone());
        let virtio_con = Arc::new(PiMutex::new(vc));
        // x86_64: split_irqchip bailed above (line ~137); reaching
        // here implies a unified kernel irqchip, so irqfd registration
        // is safe. aarch64: GICv3 is always kernel-side.
        vm.vm_fd
            .register_irqfd(virtio_con.lock().irq_evt(), kvm::VIRTIO_CONSOLE_IRQ)
            .context("register virtio-console irqfd")?;

        let kill = Arc::new(AtomicBool::new(false));
        // Watchdog-set timeout flag. Distinct from `kill` because
        // `kill` flips on every shutdown path (BSP shutdown, AP
        // panic, watchdog hard timeout) and the consumer
        // (`VmResult::timed_out`) only wants to know when the
        // watchdog fired its hard-deadline branch. The watchdog
        // thread sets this to `true` ONLY on the
        // `Instant::now() >= effective_deadline` arm; the BSP
        // reads it post-loop and the resulting `(exit_code,
        // timed_out)` tuple flows through `run_bsp_loop` →
        // `VmRunState::timed_out` → `VmResult::timed_out`.
        let timed_out_flag = Arc::new(AtomicBool::new(false));
        // Wake fd paired with the `kill` AtomicBool. Setters that
        // flip `kill` (run_vm post-BSP-exit, vCPU shutdown classifier,
        // panic hook) ALSO write to this EventFd so any consumer
        // sleeping on `epoll_wait` returns within microseconds of
        // the flip rather than waiting up to one full poll
        // interval. Production consumers: the monitor loop and the
        // watchdog thread, both spawned below. `EFD_NONBLOCK` keeps
        // the writer's `write()` from stalling if the counter is
        // already saturated; the AtomicBool remains the source of
        // truth — the EventFd is purely a wake signal.
        let kill_evt = Arc::new(EventFd::new(EFD_NONBLOCK).context("create kill EventFd")?);
        // Boot-complete eventfd. Fired by the freeze coordinator
        // when the guest publishes a CRC-valid
        // [`crate::vmm::wire::MSG_TYPE_SYS_RDY`] TLV frame on the
        // virtio-console bulk port. The monitor thread's pre-sample
        // `epoll_wait` registers this fd alongside `kill_evt` and
        // a 5 s timeout — the SYS_RDY frame is the explicit
        // boot-complete signal from the guest's userspace init,
        // sent after `mount_filesystems()` so by the time the
        // monitor wakes the kernel-side prerequisites
        // (`__per_cpu_offset[]` populated by `setup_per_cpu_areas`,
        // `page_offset_base` populated by KASLR randomization) are
        // already met. Replaces an earlier port-0-TX trigger that
        // depended on incidental console traffic. `EFD_NONBLOCK`
        // because the only writer is the coordinator's TLV dispatch
        // and the only reader is the monitor's `epoll_wait`; a
        // stuck or saturated counter is harmless because the wake
        // semantics are level-triggered. Surfaced as a `warn`
        // rather than a hard failure so a kernel without eventfd
        // support (extremely unlikely for KVM-capable hosts) still
        // boots — the monitor will fall through its 5 s timeout
        // without a guest signal.
        let sys_rdy_evt: Option<Arc<EventFd>> = match EventFd::new(EFD_NONBLOCK) {
            Ok(evt) => Some(Arc::new(evt)),
            Err(e) => {
                tracing::warn!(
                    err = %e,
                    "failed to create sys_rdy EventFd; \
                     monitor will not gate on guest-boot signal"
                );
                None
            }
        };
        // Failure-dump freeze rendezvous: broadcast `freeze` flag plus a
        // per-vCPU `parked` ACK, parallel to the existing `kill` +
        // `exited` shutdown rendezvous. The freeze coordinator
        // (spawned below alongside the watchdog) polls the BPF probe's
        // `ktstr_err_exit_detected` .bss flag via `BpfMapAccessor`;
        // when the flag flips it sets `freeze`, kicks every vCPU,
        // awaits N-of-N parked confirmations, runs the dump (placeholder
        // in this batch), and then clears `freeze` to thaw.
        let freeze = Arc::new(AtomicBool::new(false));
        // Scheduler-stats client. Constructed only when the run
        // has a scheduler attached — without a scheduler there is
        // nothing to query, and spawning a drainer thread plus
        // plumbing a client onto `VmResult` would force every test
        // that does `stats_client.unwrap().stats(...)` to wait for
        // its full timeout before discovering "no scheduler". When
        // `scheduler_binary` is `None`, the field on
        // [`VmResult::stats_client`] stays `None` and callers can
        // branch on `.is_none()` to skip the stats path entirely.
        let stats_client = if self.scheduler_binary.is_some() {
            Some(
                crate::vmm::sched_stats::SchedStatsClient::new(
                    virtio_con.clone(),
                    Some(freeze.clone()),
                    // Run-wide kill flag plumbed as the cancel
                    // signal: when the BSP / watchdog flips
                    // `kill`, blocked `request_raw` calls wake and
                    // return `Cancelled` instead of hanging forever.
                    // The host watchdog is the only "timeout" in
                    // the stats path.
                    Some(kill.clone()),
                    // Paired wake fd: the drainer's epoll watches
                    // `kill_evt` so the cancel edge propagates to
                    // a blocked cvar wait within microseconds.
                    Some(kill_evt.clone()),
                )
                .context("construct scheduler-stats client")?,
            )
        } else {
            None
        };
        // Hardware data-write watchpoint state shared between the
        // freeze coordinator (publishes the resolved
        // `*scx_root->exit_kind` KVA into `request_kva`) and every
        // vCPU thread (self-arms when `request_kva` changes; sets
        // `hit` on `KVM_EXIT_DEBUG`). See [`WatchpointArm`] for the
        // full protocol; this Arc is the only carrier and outlives
        // every consumer (the coordinator joins before the vCPU
        // teardown drops the kvm_run mmaps).
        let watchpoint =
            Arc::new(WatchpointArm::new().context("create WatchpointArm.hit_evt EventFd")?);
        let bsp_parked = Arc::new(AtomicBool::new(false));
        let bsp_regs: Arc<std::sync::Mutex<Option<exit_dispatch::VcpuRegSnapshot>>> =
            Arc::new(std::sync::Mutex::new(None));

        let has_immediate_exit = vm.has_immediate_exit;
        let mut vcpus = std::mem::take(&mut vm.vcpus);
        let mut bsp = vcpus.remove(0);

        // Build per-vCPU pin targets from the stored pinning plan.
        // Index i holds the host CPU for vCPU i. BSP is index 0.
        let pin_targets: Vec<Option<usize>> = if let Some(plan) = effective_pinning_plan {
            let total = self.topology.total_cpus() as usize;
            let mut targets = vec![None; total];
            for &(vcpu_id, host_cpu) in &plan.assignments {
                if (vcpu_id as usize) < total {
                    targets[vcpu_id as usize] = Some(host_cpu);
                }
            }
            targets
        } else {
            Vec::new()
        };

        // AP pin targets: indices 1..N.
        let ap_pins: Vec<Option<usize>> = if pin_targets.len() > 1 {
            pin_targets[1..].to_vec()
        } else {
            vec![None; vcpus.len()]
        };

        // No-perf + --cpu-cap: flat CPU list from the LLC plan gets
        // sched_setaffinity'd on every vCPU thread as a mask (not a
        // hard pin). Mutually exclusive with perf-mode's pin_targets.
        let no_perf_mask: Option<&[usize]> = self
            .no_perf_plan
            .as_ref()
            .map(|p| p.cpus.as_slice())
            .or(default_cpu_mask);

        // Per-AP TID slots — each AP thread stamps gettid() into its
        // `AtomicI32` and fires the paired `Latch` at startup so the
        // monitor can open per-vCPU `perf_event_open` counters bound
        // to the right thread. Index = AP index (0-based among APs);
        // the BSP TID is stamped into a separate slot below since it
        // runs on the current thread. The latch lets the
        // perf-capture path block in `Latch::wait_timeout` instead
        // of sleep-polling the atomic — see
        // [`open_vcpu_perf_capture`].
        let ap_tid_slots: Vec<(Arc<AtomicI32>, Arc<crate::sync::Latch>)> = (0..vcpus.len())
            .map(|_| {
                (
                    Arc::new(AtomicI32::new(0)),
                    Arc::new(crate::sync::Latch::new()),
                )
            })
            .collect();

        let (ap_threads, ap_freeze_handles) = self.spawn_ap_threads(
            vcpus,
            has_immediate_exit,
            &com1,
            &com2,
            Some(&virtio_con),
            virtio_blk.as_ref(),
            virtio_net.as_ref(),
            &kill,
            &kill_evt,
            &freeze,
            &watchpoint,
            &ap_pins,
            no_perf_mask,
            &ap_tid_slots,
            Some(&parked_evt),
            Some(&thaw_evt),
        )?;

        // Pin / mask BSP (runs on current thread, pid=0 means calling thread).
        if let Some(Some(host_cpu)) = pin_targets.first() {
            pin_current_thread(*host_cpu, "BSP (vCPU 0)");
        } else if let Some(mask) = no_perf_mask {
            set_thread_cpumask(mask, "BSP (vCPU 0)");
        }
        if self.performance_mode {
            set_rt_priority(1, "BSP (vCPU 0)");
        }

        // Collect vCPU pthread_t handles for monitor stall detection.
        // BSP runs on the current thread; APs have spawned threads.
        let vcpu_pthreads = {
            let mut pts = Vec::with_capacity(1 + ap_threads.len());
            pts.push(unsafe { libc::pthread_self() } as libc::pthread_t);
            for vt in &ap_threads {
                pts.push(vt.handle.as_pthread_t() as libc::pthread_t);
            }
            pts
        };

        // Build the per-vCPU TID vec the monitor needs for
        // `perf_event_open(2)`. Index 0 is the BSP — running on this
        // thread, so SYS_gettid here returns the current thread's
        // TID. Indexes 1..n are AP slots stamped by each AP thread at
        // startup. Slots may still be 0 here if an AP hasn't reached
        // its tid_slot.store; the monitor polls them with a deadline
        // before opening counters and skips per-vCPU perf for any
        // slot still 0 at the deadline.
        let bsp_tid_slot = Arc::new(AtomicI32::new(unsafe {
            libc::syscall(libc::SYS_gettid) as i32
        }));
        // BSP latch is pre-set so `open_vcpu_perf_capture` returns
        // immediately for index 0 — the BSP TID is stamped
        // synchronously above on this very thread.
        let bsp_latch = Arc::new(crate::sync::Latch::new());
        bsp_latch.set();
        let vcpu_tid_slots: Vec<(Arc<AtomicI32>, Arc<crate::sync::Latch>)> =
            std::iter::once((bsp_tid_slot, bsp_latch))
                .chain(ap_tid_slots.iter().cloned())
                .collect();

        // Open per-vCPU `perf_event_open` counters once at run-vm
        // scope so both the monitor thread (per-tick timeline) and
        // the freeze coordinator (freeze-instant snapshot) can read
        // through a shared `Arc`. Polling vCPU TIDs here (rather than
        // inside the monitor closure) lets the freeze coord see a
        // consistent capture immediately when the latch fires —
        // before the monitor has even taken its first sample. AP
        // threads stamp their TID into the slots before they enter
        // KVM_RUN; BSP slot is stamped synchronously above.
        // `Arc<Option<...>>` lets a host that lacks
        // `perf_event_open` permission still run the rest of the
        // dump pipeline; the inner Option is None and every
        // consumer's `as_ref()` chain produces None for that field.
        let perf_capture = Arc::new(open_vcpu_perf_capture(&vcpu_tid_slots));

        // aarch64 TCR_EL1 cache. Populated by the BSP loop on first
        // successful read post-MMU-bringup. `None` on x86_64 (the
        // register does not exist there). Threads that build a
        // `GuestKernel` for page-table walks (monitor, BPF map
        // writer, freeze coordinator's scan_tick path,
        // collect_verifier_stats) load this atomic.
        #[cfg(target_arch = "aarch64")]
        let tcr_el1_cache: Option<Arc<std::sync::atomic::AtomicU64>> =
            Some(Arc::new(std::sync::atomic::AtomicU64::new(0)));
        #[cfg(target_arch = "x86_64")]
        let tcr_el1_cache: Option<Arc<std::sync::atomic::AtomicU64>> = None;

        // CR3 (x86_64) / TTBR1_EL1 (aarch64) cache. Populated lazily
        // by the BSP loop after the kernel has established its
        // initial page tables. Used by host-side `GuestKernel`
        // constructions to walk the page tables for `phys_base`
        // resolution — see [`crate::monitor::symbols::resolve_phys_base`].
        // `0` is the bootstrap value; readers tolerate it (the walk
        // fails and `phys_base` falls back to `0`, which is correct
        // on non-KASLR boots).
        let cr3_cache: Arc<std::sync::atomic::AtomicU64> =
            Arc::new(std::sync::atomic::AtomicU64::new(0));
        // Scheduler-attach watchdog reset. Shared `AtomicU64`
        // written once by the host monitor when it observes
        // `*scx_root` transition from null to non-null in guest
        // memory (a scheduler attached); read each tick by the
        // watchdog so the hard deadline resets to attach moment +
        // `self.workload_duration` instead of being counted from
        // VM boot. `0` (the default) is the "no reset requested"
        // sentinel — the watchdog ignores it and keeps using the
        // original `timeout`-derived deadline. The reset CAN extend
        // past the original deadline (no min clamp) so boot-time
        // delays do not eat into the workload budget. Defined ahead of
        // `start_monitor` so the monitor closure can capture a
        // clone; the watchdog clone is taken below at the
        // watchdog setup site.
        let watchdog_reset_ns: Arc<std::sync::atomic::AtomicU64> =
            Arc::new(std::sync::atomic::AtomicU64::new(0));
        let kern_phys_base: Arc<std::sync::atomic::AtomicU64> =
            Arc::new(std::sync::atomic::AtomicU64::new(0));
        let kern_phys_base_evt = Arc::new(EventFd::new(0).expect("eventfd for kern_phys_base"));
        let accessor_ready_evt = Arc::new(EventFd::new(0).expect("eventfd for accessor_ready"));

        let monitor_handle = self.start_monitor(
            &vm,
            &kill,
            &kill_evt,
            run_start,
            vcpu_pthreads,
            perf_capture.clone(),
            probes_ready_evt_for_monitor,
            Some(virtio_con.clone()),
            sys_rdy_evt.clone(),
            tcr_el1_cache.clone(),
            cr3_cache.clone(),
            watchdog_reset_ns.clone(),
            kern_phys_base.clone(),
            kern_phys_base_evt.clone(),
        )?;
        let watchdog_reset_for_coord = watchdog_reset_ns.clone();
        let watchdog_pause_ns: Arc<std::sync::atomic::AtomicU64> =
            Arc::new(std::sync::atomic::AtomicU64::new(0));
        let watchdog_pause_for_coord = watchdog_pause_ns.clone();
        let workload_duration_for_coord = self.workload_duration;
        // First-ScenarioStart timestamp (nanos since `run_start`),
        // biased by `+1` so `0` means "no ScenarioStart frame
        // observed yet". The dispatch.rs ScenarioStart arm
        // CAS-stamps this on the first frame so the periodic-
        // snapshot loop in the coord run-loop can anchor the
        // 10%–90% workload-duration window at the moment the guest
        // workload actually starts (not at boot or at `run_start`).
        // `Arc<AtomicU64>` gives the coord thread shared ownership
        // with the dispatch sinks; both run in the same thread so
        // Relaxed ordering suffices, but the AtomicU64 keeps the
        // type story uniform with `watchdog_reset_for_coord` /
        // `watchdog_pause_for_coord`.
        let scenario_start_ns: Arc<std::sync::atomic::AtomicU64> =
            Arc::new(std::sync::atomic::AtomicU64::new(0));
        let scenario_start_ns_for_coord = scenario_start_ns.clone();
        // Cumulative wall-clock pause time observed between
        // matched `MSG_TYPE_SCENARIO_PAUSE` / `MSG_TYPE_SCENARIO_RESUME`
        // pairs (nanoseconds). Periodic-snapshot boundaries are
        // anchored to workload time, NOT wall-clock time — when the
        // guest workload pauses, the host's `run_start.elapsed()`
        // ticks during the pause but the workload's logical clock
        // does not. The dispatch.rs `ScenarioResume` arm bumps this
        // atomic by `pause_duration` so the run-loop can subtract it
        // from `run_start.elapsed()` to get effective workload-time
        // for the boundary-crossing check. The matching
        // `watchdog_pause_ns` atomic continues to track only the
        // current pause's start (used by the watchdog deadline-
        // extension path); cumulative pause is a periodic-only
        // concern.
        let scenario_pause_cumulative_ns: Arc<std::sync::atomic::AtomicU64> =
            Arc::new(std::sync::atomic::AtomicU64::new(0));
        let scenario_pause_cumulative_for_coord = scenario_pause_cumulative_ns.clone();
        // Periodic-snapshot count plumbed through KtstrVm for the
        // coord run-loop's periodic-capture cadence. `0` (the
        // default) skips the loop entirely — no boundary
        // computation, no per-iteration check.
        let freeze_coord_num_snapshots = self.num_snapshots;
        // Live periodic-fire count published by the run-loop after
        // each successful capture / placeholder store. Threaded
        // out to `VmResult::periodic_fired` so test code can
        // assert coverage. Written by the coordinator thread,
        // read by run_vm AFTER the coordinator joins, so Relaxed
        // ordering paired with the join's happens-before suffices.
        let periodic_fired_slot: Arc<std::sync::atomic::AtomicU32> =
            Arc::new(std::sync::atomic::AtomicU32::new(0));
        let periodic_fired_for_coord = periodic_fired_slot.clone();

        // BPF map write thread: sleeps, discovers a BPF map, writes a value.
        let bpf_write_handle = self.start_bpf_map_write(
            &vm,
            &kill,
            probes_ready_evt_for_bpf,
            tcr_el1_cache.clone(),
            cr3_cache.clone(),
            virtio_con.clone(),
            kern_phys_base.clone(),
        )?;

        // Run BSP on this thread.
        register_vcpu_signal_handler();
        let timeout = self.timeout;

        // Watchdog thread.
        let bsp_ie = if has_immediate_exit {
            Some(ImmediateExitHandle::from_vcpu(&mut bsp))
        } else {
            None
        };
        let bsp_tid = unsafe { libc::pthread_self() };
        let bsp_done = Arc::new(AtomicBool::new(false));
        let bsp_done_for_wd = bsp_done.clone();
        // BSP-IE-handle liveness gate. The freeze coordinator's
        // captured `ImmediateExitHandle` for the BSP addresses the
        // BSP `VcpuFd`'s kvm_run mmap; that mapping disappears the
        // moment `bsp` (a local in run_vm) falls out of scope. The
        // primary defense against UAF is the `freeze_coord_handle`
        // join inside run_vm BEFORE bsp drops, but this flag is a
        // cheap secondary check the closure consults before any
        // `bsp_ie_handle.set(1)` call so a future restructure that
        // moves the join doesn't silently reintroduce the UAF. Set
        // to `false` by run_vm right before bsp drops; gate every
        // BSP-side immediate_exit write on `bsp_alive.load(Acquire)`.
        let bsp_alive = Arc::new(AtomicBool::new(true));
        let bsp_alive_for_coord = bsp_alive.clone();
        // Wake fd paired with `bsp_done`. Setters (run_vm post-loop,
        // BSP panic hook) flip the AtomicBool AND write `1` to this
        // EventFd so the freeze coordinator's epoll wait returns
        // immediately. Mirrors the `kill` / `kill_evt` pair above.
        // EFD_NONBLOCK so a doubled write (panic hook AND post-loop
        // store) cannot stall — either edge is sufficient.
        let bsp_done_evt = Arc::new(EventFd::new(EFD_NONBLOCK).context("create bsp_done EventFd")?);
        let kill_for_watchdog = kill.clone();
        let timed_out_for_watchdog = timed_out_flag.clone();
        // Wake fds the watchdog blocks on via epoll, paired with the
        // `kill_for_watchdog` and `bsp_done_for_wd` AtomicBools above.
        // The watchdog wakes within microseconds of either flip
        // instead of polling on a 100 ms thread::sleep cadence.
        let kill_evt_for_watchdog = kill_evt.clone();
        let bsp_done_evt_for_wd = bsp_done_evt.clone();
        let rt_watchdog = self.performance_mode;
        let wd_service_cpu = effective_pinning_plan.and_then(|p| p.service_cpu);
        // Clone the virtio-console Arc into the watchdog so the
        // soft-deadline path can push `SIGNAL_VC_SHUTDOWN` to
        // `/dev/hvc0` for graceful shutdown. The guest's
        // `hvc0_poll_loop` blocks on the device read and recognises
        // the byte directly — no SHM signal slot involved.
        let wd_virtio_con = virtio_con.clone();
        // Watchdog-side clones for the scheduler-attach reset
        // signal. The shared `AtomicU64` and the policy decision
        // ("reset is meaningful only when a distinct workload
        // duration was set") are bound here for the watchdog's
        // `move` closure; the matching monitor clone was taken
        // above at `start_monitor` invocation. Skipped at decode
        // time when `workload_duration_for_wd` is `None` — see
        // the watchdog's per-tick reset block.
        let watchdog_reset_for_wd = watchdog_reset_ns.clone();
        let workload_duration_for_wd = self.workload_duration;

        // Freeze coordinator thread: triggers a failure-dump freeze when
        // the BPF probe's `ktstr_err_exit_detected` .bss latch fires
        // (sched_ext error-class exit observed by tp_btf inside
        // probe.bpf.c). The flag lives in the probe BPF program's
        // .bss map — the coordinator polls it via host-side guest
        // physical memory access, NOT via SHM TLV. Discovery is
        // lazy: each iteration tries
        // `BpfMapAccessor::find_map("probe_bp.bss")` (suffix-matched
        // to avoid colliding with a scheduler-under-test's own .bss
        // map) until the probe is loaded into map_idr, then caches
        // the field PA — the .bss value-region PA plus the
        // BTF-resolved byte offset of `ktstr_err_exit_detected`
        // within the section (see `cached_bss_offset`). Subsequent
        // polls run through [`bss_read_state`], which returns a
        // typed Triggered / NotTriggered / OutOfBounds /
        // NotResolved result so a stale PA after a probe unload
        // surfaces as an explicit diagnostic rather than
        // masquerading as "no fire".
        //
        // Sequencing combines Cloud Hypervisor's pause/snapshot
        // pattern (drain dance + N-of-N rendezvous on parked acks)
        // with Firecracker's SIGRTMIN+immediate_exit kick:
        //   1. observe `ktstr_err_exit_detected != 0` via .bss read
        //   2. set `freeze=true`
        //   3. set every vCPU's immediate_exit=1 (two-pass kick: all
        //      flags first, then signal all)
        //   4. signal every vCPU thread (pthread_kill SIGRTMIN)
        //   5. wait for N-of-N parked acks (Acquire-load on each
        //      `parked` flag — synchronizes-with the vCPU's Release
        //      store after the drain dance, providing the happens-
        //      before edge that makes guest-memory reads correct on
        //      weakly-ordered architectures)
        //   6. call dump_state to read BPF map state, vCPU regs,
        //      and per-CPU prog/cputime captures into a
        //      FailureDumpReport, then emit the report as JSON
        //      via tracing::error and the optional file sink
        //   7. clear freeze=false; each parked vCPU polling on
        //      park_timeout(10ms) observes the clear within 10 ms
        //      and resumes — no explicit unpark needed
        //
        // DMA quiescence: virtio-blk's independent worker thread
        // is paused before the vCPU SIGRTMIN kick (see
        // `blk.lock().pause()` in freeze_and_capture below); the
        // rendezvous waits for the worker's paused ack alongside
        // the vCPU parked acks. virtio-net (v0) and virtio-console
        // run synchronously on the vCPU thread, so they freeze
        // automatically once the vCPU rendezvous completes. A
        // future device with its own worker thread would need to
        // be added to the pause sequence.
        let freeze_coord_freeze = freeze.clone();
        let freeze_coord_kill = kill.clone();
        // Optional virtio-blk handle for the failure-dump
        // worker-pause rendezvous. None when no disk is attached.
        // Cloned into the closure so the dump path can call
        // `dev.lock().pause()` BEFORE kicking the vCPUs and
        // `dev.lock().resume()` after the dump completes — without
        // this, the worker thread would continue mutating the
        // backing file (and the avail/used rings) while the host
        // reads guest memory for the dump. Only virtio-blk has an
        // independent worker thread; virtio-net (v0) and
        // virtio-console run synchronously on the vCPU thread and
        // are automatically frozen when the vCPU rendezvous
        // completes (their `mmio_write` handlers must have already
        // returned for the vCPU to reach the parked state).
        let freeze_coord_virtio_blk = virtio_blk.clone();
        // Lock-free `paused` flag handle. The freeze coordinator
        // polls the worker's parked-state in two paths (the
        // rendezvous timeout-diagnostic snapshot and the post-thaw
        // barrier predicate). Both previously read via
        // `d.lock().is_paused()`, which contends with every
        // concurrent device operation that holds the device mutex
        // — `mmio_read`/`mmio_write` from the vCPU thread and any
        // other freeze-coord call site holding the lock. The
        // underlying field is already `Arc<AtomicBool>`, so
        // exposing a clone here lets the rendezvous read it
        // lock-free. The Acquire/Release ordering on the worker's
        // `paused` writes provides the same happens-before edges
        // with the worker's parked-state stores that
        // `is_paused()` does.
        let freeze_coord_virtio_blk_paused: Option<Arc<AtomicBool>> =
            virtio_blk.as_ref().map(|d| d.lock().paused_handle());
        // Clone the virtio-console Arc into the coordinator so it
        // can drain port-1 bulk TLV bytes as the guest writes them
        // (event-driven via the tx_evt eventfd registered into the
        // coord's epoll set below). Bytes are accumulated into
        // `coord_bulk_buf` and parsed at the end of the run; an
        // early SCHED_EXIT TLV flips `kill` so the watchdog and
        // BSP loop exit promptly.
        let freeze_coord_virtio_con = virtio_con.clone();
        // Clone the virtio-console tx_evt so the coord epoll wakes
        // immediately whenever the guest publishes a TX descriptor
        // chain on port 0 or port 1. The tx_evt is shared between
        // those two ports — a spurious wake on port-0 traffic is
        // harmless: the coord just calls `drain_bulk()` and finds
        // an empty buffer. Port 2 TX (scheduler stats) is owned
        // entirely by [`crate::vmm::sched_stats::SchedStatsClient`]
        // and never reaches this coordinator's epoll set.
        let freeze_coord_tx_evt = virtio_con
            .lock()
            .tx_evt()
            .try_clone()
            .context("clone virtio-console tx_evt for coordinator")?;
        let freeze_coord_bsp_parked = bsp_parked.clone();
        let freeze_coord_bsp_regs = bsp_regs.clone();
        let freeze_coord_bsp_done = bsp_done.clone();
        // Watchpoint-arming state shared with every vCPU thread (BSP
        // + APs). The coordinator publishes the resolved
        // `*scx_root->exit_kind` KVA into `request_kva` and polls
        // `hit` instead of the prior BPF .bss latch read. See
        // [`WatchpointArm`] for the full protocol; the Arc outlives
        // every vCPU thread because `collect_results` joins the
        // coordinator BEFORE the AP thread joins drop the VcpuFds.
        let freeze_coord_watchpoint = watchpoint.clone();
        // Shared per-vCPU perf-counter capture. The Arc lets the
        // monitor sampling loop (per-tick timeline) and the freeze
        // coordinator (freeze-instant snapshot) read through the same
        // fds. Inner `Option` is `None` when `perf_event_open` was
        // unavailable on the host; both consumers gracefully degrade
        // to "no perf data" without aborting the run.
        let freeze_coord_perf_capture = perf_capture.clone();
        let freeze_coord_vmlinux = find_vmlinux(&self.kernel);
        // Read vmlinux bytes once at run_vm scope. Shared via Arc
        // with the coordinator closure (for accessor init, dump_btf,
        // dump_cpu_time_symbols) and VmRunState (for
        // collect_verifier_stats). Eliminates the 14-28s cold-cache
        // re-read that caused cleanup hangs.
        let vmlinux_data_shared: Option<Arc<Vec<u8>>> = freeze_coord_vmlinux
            .as_ref()
            .and_then(|p| super::vmlinux::cached_vmlinux_bytes(p));
        // Cached `name -> KVA` map for `Op::WatchSnapshot` arming.
        // Build once here at run_vm scope so every TLV-driven
        // WATCH request is an O(1) HashMap lookup instead of a
        // 50MB+ vmlinux read + ELF parse. None when vmlinux can't
        // be found or the parse failed — `arm_user_watchpoint`
        // will report a clean diagnostic on lookup. Hoisted out of
        // the closure so the spawn-time parse cost is paid once
        // even when the run ends without any WATCH requests.
        let vmlinux_data_for_result = vmlinux_data_shared.clone();
        let prog_accessor_slot: Arc<
            std::sync::Mutex<Option<crate::monitor::bpf_prog::GuestMemProgAccessorOwned>>,
        > = Arc::new(std::sync::Mutex::new(None));
        let prog_accessor_slot_for_coord = prog_accessor_slot.clone();
        let freeze_coord_symbol_cache: Option<Arc<VmlinuxSymbolCache>> = freeze_coord_vmlinux
            .as_deref()
            .and_then(|p| match VmlinuxSymbolCache::from_path(p) {
                Ok(c) => Some(Arc::new(c)),
                Err(e) => {
                    tracing::warn!(
                        path = %p.display(),
                        error = %e,
                        "freeze-coord: vmlinux symbol cache build failed; \
                         Op::WatchSnapshot WATCH requests will return errors"
                    );
                    None
                }
            });
        // Optional file sink for the failure-dump JSON. Cloned out
        // of the builder field so the closure owns a copy and the
        // freeze coord can write the file without touching the env
        // or the parent `KtstrVm`.
        let freeze_coord_dump_path = self.failure_dump_path.clone();
        // Dual-snapshot mode: when true, the freeze coordinator
        // additionally polls per-CPU `rq->scx.runnable_list` for any
        // task whose `jiffies - p->scx.runnable_at` crosses
        // `watchdog_timeout/2`, takes a snapshot at that point, and
        // wraps both early + late snapshots into a
        // [`monitor::dump::DualFailureDumpReport`]. Set by
        // `attempt_auto_repro` for the repro VM only.
        let freeze_coord_dual_snapshot = self.dual_snapshot;
        // Half of the configured watchdog timeout, in nanoseconds.
        // Used by the dual-snapshot scanner to compare against each
        // task's runnable-age in jiffies (converted via the guest's
        // CONFIG_HZ at scan time). The fallback default
        // (`Duration::from_secs(4)` per the builder default) means
        // a coord that never received an explicit
        // `watchdog_timeout()` call still has a coherent half-way
        // mark — 2 s of stall before the early snapshot fires.
        let freeze_coord_watchdog_half = self
            .watchdog_timeout
            .unwrap_or(Duration::from_secs(4))
            .checked_div(2)
            .unwrap_or(Duration::ZERO);
        // Guest CONFIG_HZ resolved from the kernel image. Used to
        // convert the watchdog_half Duration into a jiffies-domain
        // threshold the runnable_at scan can compare against.
        let freeze_coord_hz = monitor::guest_kernel_hz(Some(&self.kernel));
        // GuestMem for the coordinator's .bss-poll path. Built from
        // the same guest_mem the monitor uses; lifetime tied to the
        // VM run.
        // GuestMem owns its host pointer for the duration of the run.
        // Wrapped in `Arc` so the worker thread that lazy-builds the
        // `GuestMemMapAccessorOwned` and the coordinator's own
        // accessor-borrow paths share the same backing mapping.
        // `Arc<GuestMem>` is `Send` because `GuestMem` is `Send + Sync`
        // (see `unsafe impl Send for GuestMem` in `monitor::reader`).
        let freeze_coord_mem: Option<Arc<monitor::reader::GuestMem>> = match vm.numa_layout.as_ref()
        {
            Some(layout) => Some(Arc::new(monitor::reader::GuestMem::from_layout(
                layout,
                &vm.guest_mem,
            ))),
            None => {
                use vm_memory::GuestMemoryRegion;
                if let Ok(host_base) = vm.guest_mem.get_host_address(GuestAddress(DRAM_BASE))
                    && let Some(r) = vm.guest_mem.iter().next()
                {
                    let mem_size = r.len();
                    // SAFETY: host_base came from GuestMemoryMmap's
                    // get_host_address; mapping outlives this GuestMem
                    // (vm.guest_mem outlives the coordinator thread —
                    // collect_results joins the coordinator before vm
                    // is dropped).
                    Some(Arc::new(unsafe {
                        monitor::reader::GuestMem::new(host_base, mem_size)
                    }))
                } else {
                    None
                }
            }
        };
        // Extract a fresh ImmediateExitHandle for the freeze coord —
        // the watchdog grabs another one below for its own kick path.
        // Both views address the same kvm_run.immediate_exit byte
        // (single-byte volatile writes), distinct from the BSP's own
        // owned handle inside its run loop.
        let freeze_coord_bsp_ie_handle = if has_immediate_exit {
            Some(ImmediateExitHandle::from_vcpu(&mut bsp))
        } else {
            None
        };
        let freeze_coord_bsp_tid = unsafe { libc::pthread_self() };
        // Snapshot the AP-side freeze handles. `parked` flags and
        // register-snapshot slots come from `ap_freeze_handles` —
        // populated alongside the threads inside `spawn_ap_threads`,
        // kept out of `VcpuThread` so that struct stays minimal
        // (only `handle` + `exited` + `immediate_exit` are needed
        // for teardown). The freeze coordinator owns these Vecs
        // for the rest of run_vm. `pthread_t`s and immediate-exit
        // handles still come from `ap_threads` because those are
        // teardown-relevant too.
        let ApFreezeHandles {
            parked: freeze_coord_ap_parked,
            regs: freeze_coord_ap_regs,
        } = ap_freeze_handles;
        let freeze_coord_ap_pthreads: Vec<libc::pthread_t> = ap_threads
            .iter()
            .map(|vt| vt.handle.as_pthread_t() as libc::pthread_t)
            .collect();
        // ImmediateExitHandle is Copy+Send+Sync, so the coordinator
        // captures a Vec of them by move. The kvm_run mmap is shared
        // between the spawned vCPU thread (which owns its handle
        // inside VcpuThread) and the coordinator's copy — single-byte
        // volatile writes through `set` from either side address the
        // same MAP_SHARED page.
        let freeze_coord_ap_ies: Vec<Option<ImmediateExitHandle>> =
            ap_threads.iter().map(|vt| vt.immediate_exit).collect();
        // Per-AP `alive` flags paired with the IE handles above. The
        // coordinator's pass-1 kick (in `freeze_and_capture`) and
        // `arm_user_watchpoint` gate each `ie.set` on a fresh
        // Acquire load of the corresponding entry, mirroring the
        // BSP-side `bsp_alive` TOCTOU-tightened gate. Without this,
        // an AP panic-unwind under `panic = "unwind"` (test profile)
        // can drop `vcpu` mid-cycle and the coordinator's
        // `Vec<ImmediateExitHandle>` would issue a `write_volatile`
        // through a freed `kvm_run` mapping. The Vec lives the
        // entire coordinator lifetime; index alignment with
        // `freeze_coord_ap_ies` and `freeze_coord_ap_pthreads` is
        // load-bearing — every AP-loop site uses `iter().enumerate()`
        // (or `zip`) so a future change that drops or reorders any
        // one Vec is loud about the regression.
        let freeze_coord_ap_alive: Vec<Arc<AtomicBool>> =
            ap_threads.iter().map(|vt| vt.alive.clone()).collect();
        // Total vCPU count (BSP + APs). Forwarded into dump_state so
        // PERCPU_ARRAY map rendering knows how many per-CPU slots to
        // read — `bpf_array.pptrs[k]` is a `void __percpu *` whose
        // per-CPU expansion needs `__per_cpu_offset[0..nr_cpu_ids]`.
        let freeze_coord_num_cpus = (ap_threads.len() + 1) as u32;
        // NUMA node count from the configured topology. Forwarded
        // into the scx walker (per-node global DSQ pass) and the
        // per-node NUMA event walker. Defaults to 1 on UMA topologies.
        let freeze_coord_num_nodes = self.topology.num_numa_nodes();
        // Lazy BPF cast-analysis handle produced at builder time.
        // The handle is `Arc<LazyCastMap>` and holds only the
        // scheduler binary path plus a `OnceLock` slot; the
        // analyzer runs only when `.get_full()` is first called
        // at dump time on the freeze-coordinator host thread (NOT
        // a vCPU thread — the freeze rendezvous has already
        // paused vCPUs by the time `dump_state` runs). The clone
        // shares the `OnceLock`, so a periodic-capture dump and
        // the final freeze in the same VM both resolve to the
        // same analyzed `Arc<CastAnalysisOutput>` after the first
        // `.get_full()`.
        let freeze_coord_cast_map: Arc<crate::vmm::cast_analysis_load::LazyCastMap> =
            self.cast_map.clone();
        let freeze_coord_on_demand_in_flight = on_demand_in_flight.clone();
        // Snapshot bridge clone for the coord run-loop. The bridge
        // stores `FailureDumpReport` entries under per-tag keys (TLV
        // CAPTURE, periodic, user-watchpoint paths). On a Degraded
        // outcome the bridge entry is flattened to a SCHEMA_SINGLE
        // placeholder with `is_placeholder=true` so existing in-process
        // `Snapshot` API consumers don't need to branch on the
        // dispatcher's 3-arm match; the canonical SCHEMA_DEGRADED form
        // (with `watchpoint_hit`, `bss_latch_state`, `exit_kind`,
        // `elapsed_ms` fields) lives in the on-disk failure-dump JSON
        // written via [`write_to_tagged_path`]. Consumers wanting the
        // structured trigger-state fields should read the file via
        // [`crate::monitor::dump::FailureDumpReportAny::from_json`]
        // rather than the bridge. The early-snapshot Degraded path
        // writes a tagged sibling via [`write_to_tagged_path`] but
        // does NOT publish to the bridge — late-trigger arms consume
        // held early snapshots from a closure-local `early_snapshot`
        // variable, not the bridge.
        let freeze_coord_snapshot_bridge = snapshot_bridge.clone();
        // Stats-client clone for the periodic-capture path. The
        // periodic-fire branch issues a `stats(&[])` request BEFORE
        // calling `freeze_and_capture(false)` so the JSON it returns
        // reflects the running scheduler — once the freeze rendezvous
        // begins the scheduler's userspace thread is paused and the
        // request would either time out or wedge until thaw. The
        // resulting `serde_json::Value` is bundled with the
        // FailureDumpReport via `SnapshotBridge::store_with_stats` so
        // a later `Sample` view exposes both axes from the same
        // boundary. `None` when no scheduler is configured (the
        // outer `stats_client` builder above returns `None` when
        // `scheduler_binary.is_none()`); in that case periodic
        // captures store `None` in the parallel stats slot and the
        // temporal-stats projection surfaces a per-sample missing-
        // stats failure that the test author can opt to ignore.
        let freeze_coord_stats_client = stats_client.clone();
        // Wake-fd handles for the coord epoll loop. `kill_evt` and
        // `bsp_done_evt` are written by every thread that flips the
        // matching AtomicBool (run_vm post-BSP-exit, vCPU shutdown
        // classifier, BSP panic hook, AP panic hook); the epoll wait
        // fires immediately on either edge instead of polling on a
        // 500 µs sleep cadence. The watchpoint hit_evt clone lets
        // the coord wake on a hardware-watchpoint fire (vCPU thread
        // calls `WatchpointArm::latch_hit`, which writes the
        // eventfd alongside the AtomicBool flip). All three live
        // for the lifetime of the run — `run_vm` joins the coord
        // BEFORE the eventfds drop.
        let freeze_coord_kill_evt = kill_evt.clone();
        // aarch64 TCR_EL1 cache populated by the BSP loop. Threaded
        // into `GuestKernel::new` constructions inside the
        // freeze-coord scan_tick closure (BPF map accessor and
        // prog accessor) so vmalloc-backed kernel reads succeed
        // post-MMU-bringup. None on x86_64.
        let freeze_coord_tcr_el1 = tcr_el1_cache.clone();
        // CR3 (x86_64) / TTBR1_EL1 (aarch64) cache populated by
        // the BSP loop. Threaded into `GuestKernel::new` so the
        // boot-time `phys_base` resolution can walk the live
        // kernel page tables.
        let freeze_coord_cr3 = cr3_cache.clone();
        let freeze_coord_bsp_done_evt = bsp_done_evt.clone();
        // Clone the WatchpointArm.hit_evt for the epoll set. EventFd
        // clones share the underlying counter via dup(2), so the
        // vCPU's `latch_hit` write delivers an edge to every clone.
        let freeze_coord_hit_evt = watchpoint
            .hit_evt
            .try_clone()
            .context("clone WatchpointArm.hit_evt for coordinator")?;
        // Shared parked_evt for the rendezvous wait. Every vCPU
        // thread + the virtio-blk worker writes to this fd
        // immediately after their respective parked/paused Release
        // store; the rendezvous loop polls on this fd alongside
        // kill_evt and bsp_done_evt instead of spin-sleeping.
        let freeze_coord_parked_evt = parked_evt.clone();
        // Shared thaw_evt: the coordinator writes 1 here AFTER the
        // `freeze.store(false, Release)` so every parked vCPU's
        // poll wakes within microseconds rather than waiting on the
        // legacy 10ms park_timeout cadence.
        let freeze_coord_thaw_evt = thaw_evt.clone();
        // Shared bulk-message buffer: the TOKEN_TX handler in the
        // coordinator parses port-1 TLV bytes via `HostAssembler`
        // and drains the per-frame `BulkMessage` values. Without
        // this buffer those messages would be discarded after the
        // SCHED_EXIT scan, leaving `collect_results` blind to every
        // EXIT / TEST / PAYLOAD_METRICS / RAW_PAYLOAD_OUTPUT /
        // PROFRAW frame the guest already published mid-run. The
        // post-exit `drain_bulk()` only catches what arrived AFTER
        // the coordinator stopped draining — not the bulk of a
        // typical run. The Mutex serialises the coord's pushes
        // against `collect_results`'s drain; both occur strictly
        // after the closure spawns and strictly before the
        // coordinator joins, so contention is rare.
        let freeze_coord_bulk_messages: Arc<std::sync::Mutex<Vec<crate::vmm::wire::ShmEntry>>> =
            Arc::new(std::sync::Mutex::new(Vec::new()));
        let freeze_coord_bulk_messages_for_closure = freeze_coord_bulk_messages.clone();

        // Captured sys_rdy eventfd for the coordinator's TLV
        // dispatch loop. The TOKEN_TX handler promotes a CRC-valid
        // `MSG_TYPE_SYS_RDY` frame into a single
        // [`EventFd::write`] on this fd, releasing the monitor
        // thread's pre-sample `epoll_wait`. The `Option<Arc<...>>`
        // is replaced with `None` after the first promotion via
        // [`Option::take`] so subsequent SYS_RDY frames (a hostile
        // guest could in principle resend) skip the eventfd write
        // and do not pump the counter. `None` initially when the
        // sys_rdy machinery was not constructed (`EventFd::new`
        // failed at boot — already logged); in that case the
        // monitor will fall through its 5 s boot-wait timeout
        // without a guest signal. `move` semantics on the closure
        // mean the moved-in `Option` is dropped at coordinator
        // shutdown, releasing the host-side reference.
        let mut freeze_coord_sys_rdy_evt = sys_rdy_evt.clone();

        // One-time probe of the host's hardware-watchpoint slot
        // count via `KVM_CHECK_EXTENSION(KVM_CAP_GUEST_DEBUG_HW_WPS)`.
        // The slot 0 reservation for `*scx_root->exit_kind` plus
        // [`crate::scenario::snapshot::MAX_WATCH_SNAPSHOTS`] user
        // slots means the framework needs at least 4 hardware
        // watchpoint slots to arm every requested
        // [`crate::scenario::ops::Op::WatchSnapshot`]. KVM returns
        // the count via `check_extension_int`; `<= 0` means the
        // capability is unavailable. Log only — do not block VM
        // creation: a kernel without the capability still runs
        // tests, just without the watch-driven snapshots, and a
        // probe failure surfacing here is more actionable than a
        // silent `KVM_SET_GUEST_DEBUG` rejection later.
        let hw_wps = vm.vm_fd.check_extension_int(kvm_ioctls::Cap::DebugHwWps);
        if hw_wps <= 0 {
            tracing::warn!(
                "KVM_CAP_GUEST_DEBUG_HW_WPS unavailable on this host \
                 (returned {hw_wps}); Op::WatchSnapshot triggers may \
                 not arm — falling back to BPF .bss poll for the \
                 error-class freeze trigger"
            );
        } else {
            tracing::info!(
                "KVM host advertises {hw_wps} hardware watchpoint \
                 slots via KVM_CAP_GUEST_DEBUG_HW_WPS"
            );
            if hw_wps < 4 {
                tracing::warn!(
                    "KVM host advertises only {hw_wps} hardware \
                     watchpoint slots; the framework reserves slot 0 \
                     for the *scx_root->exit_kind error-class trigger \
                     plus up to {} user slots for Op::WatchSnapshot \
                     — some watch_snapshot arms may fail",
                    crate::scenario::snapshot::MAX_WATCH_SNAPSHOTS,
                );
            }
        }

        let kern_phys_base_for_result = kern_phys_base.clone();
        // Effective rendezvous-wait deadline shared by the worker-park
        // and post-thaw barriers downstream. Downstream comments
        // reference `FREEZE_RENDEZVOUS_TIMEOUT` (the 30 s const) as
        // the canonical bound; the captured local resolves
        // to that const unless `KtstrVmBuilder::rendezvous_timeout` was
        // set — the override path is test-fixture only. Captured here
        // (outside the spawn) so the closure copies the `Duration`
        // (`Copy`) by value rather than borrowing `self`, which the
        // `'static` thread::spawn bound forbids.
        let rendezvous_timeout = self.rendezvous_timeout.unwrap_or(FREEZE_RENDEZVOUS_TIMEOUT);
        let freeze_coord_handle = std::thread::Builder::new()
            .name("vmm-freeze-coord".into())
            .spawn(move || {
                // Per-CPU runnable_at scanner context. Holds every
                // input the scanner needs, all resolved once and
                // cached for the rest of the run. Only built when
                // dual_snapshot is enabled AND every prerequisite
                // resolves (vmlinux ELF parses, BTF resolves the
                // four runnable_scan offsets, jiffies_64 symbol is
                // present, the GuestKernel handshake completes so
                // we have a cr3_pa / page_offset / l5 view).
                struct RunnableScanCtx {
                    /// KVA of the kernel's global `scx_tasks` LIST_HEAD
                    /// (`kernel/sched/ext.c:47`). The walker reads
                    /// `scx_tasks.next` via the runtime kernel image
                    /// base ([`Self::start_kernel_map`]) and
                    /// container_of's each list entry back to its
                    /// `task_struct`.
                    scx_tasks_kva: u64,
                    /// Per-CPU `struct rq` PAs (one per logical CPU).
                    /// Built by `compute_rq_pas(runqueues_kva,
                    /// __per_cpu_offset[*], page_offset)`. Each entry
                    /// addresses the rq whose `scx.runnable_list`
                    /// the per-rq walker walks; vec index = CPU index.
                    /// Empty when the per-CPU offset array can't be
                    /// resolved (per-rq walk silently falls back to
                    /// the global walk).
                    rq_pas: Vec<u64>,
                    offsets: crate::monitor::btf_offsets::RunnableScanOffsets,
                    jiffies_64_pa: u64,
                    /// PA of `scx_watchdog_timestamp`
                    /// (`kernel/sched/ext.c:94`). The kernel's
                    /// `scx_tick` (`kernel/sched/ext.c:3409`) compares
                    /// `jiffies - scx_watchdog_timestamp` against the
                    /// scheduler's `watchdog_timeout` and fires
                    /// `SCX_EXIT_ERROR_STALL` when the workqueue
                    /// stopped running. Reading the same value here
                    /// gives the dual-snapshot path the global stall
                    /// signal regardless of whether any individual
                    /// task is stuck on a per-rq runnable_list. None
                    /// when the symbol is absent (kernel without
                    /// sched_ext or stripped vmlinux); per-rq /
                    /// global walks still cover the per-task case.
                    watchdog_timestamp_pa: Option<u64>,
                    /// Paging context (cr3_pa / page_offset / l5 /
                    /// tcr_el1) threaded into the runnable_scan helpers.
                    walk: crate::monitor::reader::WalkContext,
                    /// Runtime kernel image base
                    /// (`__START_KERNEL_map` on x86_64,
                    /// `KIMAGE_VADDR` on aarch64). Threaded into the
                    /// runnable_scan helpers so `scx_tasks` and other
                    /// kernel-text-mapped symbols translate via the
                    /// VA-bits-aware base resolved from `TCR_EL1` —
                    /// matches the [`super::super::monitor::guest::GuestKernel`]
                    /// the surrounding accessors share.
                    start_kernel_map: u64,
                    /// Runtime KASLR offset (`phys_base` on x86_64;
                    /// `0` on aarch64 / non-KASLR boots). Required by
                    /// `text_kva_to_pa_with_base` so KASLR kernels
                    /// resolve `scx_tasks` / `jiffies_64` /
                    /// `scx_watchdog_timestamp` correctly.
                    phys_base: u64,
                }
                // Lazy-construct BpfMapAccessorOwned. The constructor
                // parses vmlinux ELF (goblin) and BTF (~MB-scale
                // work) and reads guest-memory bootstrap symbols
                // (`page_offset_base`, `pgtable_l5_enabled`,
                // `init_top_pgt`); the latter aren't readable until
                // the guest kernel has populated them, so a
                // construction attempt at coord-start can fail with
                // a still-booting guest. The fix is the same lazy-
                // discovery pattern that `cached_bss_pa` uses below:
                // try each iteration until success, then cache —
                // gated on `owned_accessor.is_none()` so the heavy
                // parse runs at most once per coordinator (only the
                // failed attempts re-pay it, and only until the
                // first success). A single one-shot construct at
                // coord-start would have left the accessor None
                // permanently if the guest hadn't booted yet,
                // disabling freeze detection AND the dump for the
                // entire run.
                // Cached vmlinux bytes shared across every retry of
                // `try_init_owned_accessor` and
                // `try_init_owned_prog_accessor`. The previous code
                // re-ran `std::fs::read(vmlinux)` inside both helpers
                // on every scan tick — at 50-340 MB per call on cold
                // disk cache the pair could exceed the 12 s post-
                // BSP-done kill timer before the coord ever reached
                // its epoll wait. Reading once at coord scope cuts
                // the per-iteration cost to a few-millisecond
                // `goblin::elf::Elf::parse` against the cached bytes.
                //
                // The borrow lifetime constraint blocks caching the
                // parsed `Elf<'static>` (it borrows from the Vec); the
                // helpers re-parse the cached bytes per call instead.
                // Parsing is microseconds — only the file read was
                // slow.
                let _tvmr = std::time::Instant::now();
                let vmlinux_data: Option<Arc<Vec<u8>>> = vmlinux_data_shared.clone();
                // Worker-populated accessor pair. Built off the freeze
                // coordinator thread so the slow ELF + BTF parse +
                // symbol HashMap (~4 s on debug vmlinux) does not
                // block the coordinator from servicing TOKEN_TX
                // events on its epoll loop. The worker writes both
                // accessors atomically via `OnceLock::set` once the
                // GuestKernel handshake succeeds and both BTF parses
                // land. Subsequent reads from the coordinator are
                // nanosecond-scale `OnceLock::get` calls.
                //
                // `Arc<OnceLock<(...)>>` shape: `Arc` so the worker
                // and coordinator share ownership; `OnceLock` so the
                // publish is one-shot and lock-free on read; the
                // tuple shape so both accessors land atomically — a
                // failure-dump path that builds a `ScxWalkerCapture`
                // must also have the matching `prog_runtime_stats`
                // accessor, so partial pairs would skew the dump.
                //
                // `GuestMemMapAccessorOwned` and
                // `GuestMemProgAccessorOwned` are `Send` because they
                // own `GuestKernel`, which holds `Arc<GuestMem>`
                // (was `&'a GuestMem`). The Arc shape lets the worker
                // own the kernel handle independently of the
                // coordinator's stack.
                let accessors_oncelock: Arc<std::sync::OnceLock<(
                    crate::monitor::bpf_map::GuestMemMapAccessorOwned,
                    Option<crate::monitor::bpf_prog::GuestMemProgAccessorOwned>,
                )>> = Arc::new(std::sync::OnceLock::new());
                // Borrowed views into the OnceLock pair. Reset to
                // `Some(...)` on the first scan tick after the worker
                // publishes; remain `None` while the worker is still
                // retrying. Each loop body that reads these gates on
                // `Some` exactly as the prior `Option<...Owned>`-typed
                // shape did, so no call-site logic changes. Borrowing
                // is lifetime-clean: the OnceLock is moved into the
                // worker's clone via `Arc`, the coordinator retains
                // its own clone, and `&` borrows from a shared `Arc`
                // are valid for the entire freeze_coord closure scope.
                let mut owned_accessor:
                    Option<&crate::monitor::bpf_map::GuestMemMapAccessorOwned> = None;
                let mut owned_prog_accessor:
                    Option<&crate::monitor::bpf_prog::GuestMemProgAccessorOwned> = None;
                let mut coord_kaslr_offset: u64 = 0;
                // Spawn the accessor-init worker before entering the
                // coordinator's epoll loop. The worker:
                //   1. Loops `try_init_owned_accessor` +
                //      `try_init_owned_prog_accessor` against the
                //      shared `Arc<GuestMem>` until both succeed.
                //   2. On success: stores `phys_base + 1` (biased) in
                //      `kern_phys_base` via `compare_exchange(0, ..)`
                //      so the monitor thread observes the value
                //      regardless of whether the guest's port-2
                //      publish landed first.
                //   3. Publishes the pair via `OnceLock::set`.
                //   4. Exits — the OnceLock is read-only thereafter.
                //
                // The worker honors `freeze_coord_kill` between
                // retries and bails immediately on shutdown so a
                // still-booting VM that's killed mid-init does not
                // delay coord teardown. The 60s budget is the same
                // order as `start_bpf_map_write`'s phase-1 deadline;
                // a boot that hasn't published the bootstrap symbols
                // by then is genuinely stuck and the dump path is
                // unavailable for the rest of the run regardless.
                let accessor_init_handle: Option<std::thread::JoinHandle<()>> = match (
                    freeze_coord_mem.as_ref(),
                    freeze_coord_vmlinux.as_ref(),
                    vmlinux_data.as_deref(),
                ) {
                    (Some(mem), Some(vmlinux), Some(data)) => {
                        let mem_for_worker = mem.clone();
                        let vmlinux_for_worker = vmlinux.clone();
                        let data_for_worker = data.clone();
                        let tcr_for_worker = freeze_coord_tcr_el1.clone();
                        let cr3_for_worker = freeze_coord_cr3.clone();
                        let kern_phys_base_for_worker = kern_phys_base.clone();
                        let kern_phys_base_evt_for_worker = kern_phys_base_evt.clone();
                        let accessor_ready_evt_for_worker = accessor_ready_evt.clone();
                        let kill_for_worker = freeze_coord_kill.clone();
                        let kill_evt_for_worker = freeze_coord_kill_evt.clone();
                        let oncelock_for_worker = accessors_oncelock.clone();
                        std::thread::Builder::new()
                            .name("vmm-accessor-init".into())
                            .spawn(move || {
                                let deadline = Instant::now()
                                    + Duration::from_secs(60);
                                let _init_t0 = Instant::now();
                                // poll() on kill_evt so the worker
                                // wakes instantly on shutdown instead
                                // of sleeping through a 100ms window.
                                let kill_fd = {
                                    use std::os::unix::io::AsRawFd;
                                    kill_evt_for_worker.as_raw_fd()
                                };
                                let elf = match goblin::elf::Elf::parse(&data_for_worker) {
                                    Ok(e) => e,
                                    Err(e) => {
                                        tracing::warn!(
                                            error = %e,
                                            "accessor-init: vmlinux ELF parse failed"
                                        );
                                        return;
                                    }
                                };
                                loop {
                                    if kill_for_worker.load(Ordering::Acquire) {
                                        return;
                                    }
                                    if Instant::now() >= deadline {
                                        tracing::warn!(
                                            "freeze-coord accessor-init worker: \
                                             60s deadline exceeded; coordinator \
                                             will run without owned-accessor \
                                             pair (freeze dump path unavailable)"
                                        );
                                        return;
                                    }
                                    // Use the guest-reported phys_base
                                    // (includes kaslr_offset) as a hint
                                    // so GuestKernel gets the correct
                                    // value instead of 0 from the
                                    // failing page-table walk.
                                    let biased = kern_phys_base_for_worker
                                        .load(Ordering::Acquire);
                                    let pb_hint = if biased != 0 {
                                        biased.wrapping_sub(1)
                                    } else {
                                        let pb_evt_fd = {
                                            use std::os::unix::io::AsRawFd;
                                            kern_phys_base_evt_for_worker.as_raw_fd()
                                        };
                                        let mut pfds = [
                                            libc::pollfd { fd: kill_fd, events: libc::POLLIN, revents: 0 },
                                            libc::pollfd { fd: pb_evt_fd, events: libc::POLLIN, revents: 0 },
                                        ];
                                        unsafe { libc::poll(pfds.as_mut_ptr(), 2, 200) };
                                        continue;
                                    };
                                    let tcr_val = tcr_for_worker
                                        .as_ref()
                                        .map(|c| c.load(Ordering::Acquire))
                                        .unwrap_or(0);
                                    let cr3_val = cr3_for_worker.load(Ordering::Acquire);
                                    let map_res = crate::monitor::bpf_map::GuestMemMapAccessorOwned
                                        ::from_elf_with_hint(
                                            mem_for_worker.clone(),
                                            &elf,
                                            &data_for_worker,
                                            &vmlinux_for_worker,
                                            tcr_val,
                                            cr3_val,
                                            pb_hint,
                                        );
                                    if kill_for_worker.load(Ordering::Acquire) {
                                        return;
                                    }
                                    if let Ok(map) = map_res {
                                        let po = map.guest_kernel()
                                            .walk_context().page_offset;
                                        if po & (1u64 << 63) == 0
                                            || po & 0xFFF != 0
                                        {
                                            let mut pfd = libc::pollfd {
                                                fd: kill_fd,
                                                events: libc::POLLIN,
                                                revents: 0,
                                            };
                                            unsafe {
                                                libc::poll(&mut pfd, 1, 100);
                                            }
                                            continue;
                                        }
                                        let phys_base =
                                            map.guest_kernel().phys_base();
                                        let _ = kern_phys_base_for_worker
                                            .compare_exchange(
                                                0,
                                                phys_base.wrapping_add(1),
                                                Ordering::Release,
                                                Ordering::Relaxed,
                                            );
                                        let prog_res = {
                                            let shared_syms = map.guest_kernel().symbols_arc();
                                            let kernel = crate::monitor::guest::GuestKernel
                                                ::from_elf_with_symbols(
                                                    mem_for_worker.clone(),
                                                    shared_syms,
                                                    &elf,
                                                    tcr_val,
                                                    cr3_val,
                                                    pb_hint,
                                                );
                                            kernel.and_then(|k| {
                                                crate::monitor::bpf_prog::GuestMemProgAccessorOwned
                                                    ::finish(k, &elf, &data_for_worker, &vmlinux_for_worker)
                                            })
                                        };
                                        let _ = oncelock_for_worker
                                            .set((map, prog_res.ok()));
                                        let _ = accessor_ready_evt_for_worker.write(1);
                                        return;
                                    }
                                    // Wait on kill_evt with 200ms
                                    // timeout. Wakes instantly on
                                    // kill; retries on timeout.
                                    let mut pfd = libc::pollfd {
                                        fd: kill_fd,
                                        events: libc::POLLIN,
                                        revents: 0,
                                    };
                                    unsafe {
                                        libc::poll(&mut pfd, 1, 200);
                                    }
                                }
                            })
                            .ok()
                    }
                    _ => None,
                };
                // Per-CPU offset array used by `runtime_stats` to
                // locate each CPU's `bpf_prog_stats` slot. Resolved
                // once after `owned_prog_accessor` lands by reading
                // `__per_cpu_offset` from guest memory; cached so
                // every dump iteration reuses it. None until either
                // the prog accessor isn't ready yet or the
                // `__per_cpu_offset` symbol couldn't be located in
                // the kernel's symbol table.
                let mut prog_per_cpu_offsets: Option<Vec<u64>> = None;
                // BTF + arena offsets resolved once at coordinator
                // start. Used by `dump_state` after the rendezvous
                // succeeds to render every BPF map's contents. None
                // values disable rendering for the relevant code path
                // (no BTF → no BTF-driven rendering at all; no arena
                // offsets → arena maps fall back to an explanatory
                // error string in the report).
                //
                // Arena offsets derive from the same parsed `Btf`
                // handle (`from_btf`, not `from_vmlinux`) so the
                // ELF-to-BTF parse runs exactly once per coordinator
                // — a second `from_vmlinux` would re-read and
                // re-parse the same file.
                let dump_btf = vmlinux_data.as_deref().zip(freeze_coord_vmlinux.as_ref())
                    .and_then(|(data, path)| crate::monitor::btf_offsets::load_btf_from_bytes(data, path).ok());
                let dump_arena_offsets = dump_btf
                    .as_ref()
                    .and_then(|btf| crate::monitor::arena::BpfArenaOffsets::from_btf(btf).ok());
                // Per-CPU CPU-time / softirq / IRQ / iowait offsets
                // and the matching `.data..percpu` symbol KVAs.
                // Resolved once at coordinator start, mirroring
                // `dump_arena_offsets`. Both Option-typed: a stripped
                // vmlinux without any of `kernel_cpustat` / `kstat` /
                // `tick_cpu_sched` symbols still resolves the BTF
                // offsets fine, but the dump path checks both sides
                // before constructing a `CpuTimeCapture` so the
                // capture site only fires when the data is actually
                // readable.
                let dump_cpu_time_offsets = dump_btf
                    .as_ref()
                    .and_then(|btf| crate::monitor::btf_offsets::CpuTimeOffsets::from_btf(btf).ok());
                let dump_cpu_time_symbols = vmlinux_data.as_deref()
                    .and_then(|data| crate::monitor::symbols::KernelSymbols::from_vmlinux_bytes(data).ok());
                // SCX walker BTF sub-group offsets. Resolved once at
                // coord start; per-sub-group resolution failures land
                // inside the composite as None so the walker's
                // `missing_groups()` can report which passes are blind
                // (a kernel built without CONFIG_NUMA loses
                // `scx_sched_pnode`, etc.).
                let dump_scx_walker_offsets = dump_btf
                    .as_ref()
                    .and_then(|btf| {
                        crate::monitor::btf_offsets::ScxWalkerOffsets::from_btf(btf).ok()
                    });
                // Per-task enrichment BTF offsets. All-or-nothing —
                // any missing sub-group leaves the composite Err and
                // the enrichment capture is skipped. The walker
                // never runs partially: every Tier-1 field must be
                // resolvable, otherwise the dump path falls back to
                // `REASON_NO_TASK_WALKER`.
                let dump_task_enrichment_offsets = dump_btf
                    .as_ref()
                    .and_then(|btf| {
                        crate::monitor::btf_offsets::TaskEnrichmentOffsets::from_btf(btf).ok()
                    });
                // Per-node NUMA event BTF offsets. Required for the
                // per-node `vm_numa_event[]` walker. Resolved once at
                // coord start; absent on stripped vmlinux or kernels
                // built without `CONFIG_NUMA + CONFIG_VM_EVENT_COUNTERS`.
                let dump_numa_offsets = dump_btf
                    .as_ref()
                    .and_then(|btf| {
                        crate::monitor::btf_offsets::NumaStatsOffsets::from_btf(btf).ok()
                    });
                // Hoisted scan_ctx prerequisites. These are pure
                // functions of the host inputs (vmlinux ELF and the
                // already-loaded BTF), so they succeed or fail
                // deterministically at coord-start — no boot-race
                // window to retry through. Computing once here avoids
                // re-parsing the BTF on every scan_ctx try_resolve
                // iteration. The previous per-iteration retry pattern
                // was harmless functionally (idempotent) but burned
                // ~MB-scale ELF reparse work every SCAN_INTERVAL until
                // owned_accessor caught up. These two values plus
                // `dump_cpu_time_symbols.scx_tasks` and `runqueues`
                // feed RunnableScanCtx construction below — the
                // global walker reads `scx_tasks` directly via
                // `text_kva_to_pa_with_base` (or
                // `GuestKernel::text_kva_to_pa`), the per-rq walker uses
                // `runqueues` + `__per_cpu_offset` to address each
                // CPU's `rq`.
                let scan_offsets = dump_btf.as_ref().and_then(|btf| {
                    crate::monitor::btf_offsets::RunnableScanOffsets::from_btf(btf).ok()
                });
                // jiffies_64 lives on the KernelSymbols instance
                // computed above for the dump capture. Reusing it
                // pays a single from_vmlinux cost per coordinator.
                let scan_jiffies_64_kva =
                    dump_cpu_time_symbols.as_ref().and_then(|s| s.jiffies_64);
                // Lazy-discovered cached PA of `ktstr_err_exit_detected`
                // within the probe BPF program's .bss map. None until
                // the probe loads into map_idr (rust_init phase 2b);
                // discovery retries each iteration until success.
                //
                // Invalidated each scan tick when the source `.bss`
                // map disappears from `map_idr` or rebinds to a
                // different `value_kva` — see the rediscovery guard
                // below. Without that, a probe BPF program that
                // unloads mid-run leaves the freed vmalloc page's
                // PA cached here; the kernel can re-allocate that
                // page for unrelated guest memory, and the next
                // `read_u32(pa, 0)` returns whatever bytes that
                // page now holds (any non-zero value latches a
                // phantom `err_triggered` and synthesizes a bogus
                // failure dump).
                let mut cached_bss_pa: Option<u64> = None;
                // Companion to `cached_bss_pa`: the `value_kva` of
                // the `.bss` map that produced it. Used as a stale-
                // probe canary — if the next scan tick finds the
                // same-named map with a different `value_kva` (the
                // bpf_array slab moved across an unload+reload) the
                // PA is invalidated and re-resolved. Stays in sync
                // with `cached_bss_pa`: both Some or both None.
                let mut cached_bss_value_kva: Option<u64> = None;
                // Cache the BTF-resolved offset of the field within
                // the .bss section. The Datasec walk parses the
                // probe's BTF (a few-KB blob copy + parse) every
                // call — caching keeps that work to once-per-coord-
                // lifetime instead of once-per-discovery-iteration.
                // Resolution can fail two ways:
                //   - guest still booting → retry (offset stays None)
                //   - BTF parse / Datasec walk broken → fall back
                //     to offset 0 once, log a warn, and stop retrying
                //     (warn_logged is the latch).
                let mut cached_bss_offset: Option<u32> = None;
                let mut bss_offset_warn_logged = false;
                // One-shot latch for the cached_bss_pa-points-OOB
                // diagnostic. The OOB read state can occur if the
                // cached PA was resolved against a probe `.bss` map
                // that has since been freed (probe unload mid-run,
                // vmalloc page recycled). The first observation
                // surfaces a warn so an operator inspecting the run
                // knows the .bss path has gone silent; subsequent
                // observations stay debug-level so the logs do not
                // fill up across the remaining run lifetime.
                let mut bss_oob_warn_logged = false;
                // Cached `*scx_root` value (the vmalloc/slab KVA of
                // the live `struct scx_sched`). Tracked across scan
                // ticks so we can detect a sched_ext detach + reattach
                // cycle: when the kernel tears down the scheduler the
                // pointer goes 0 (the slab page is freed); when a new
                // scheduler attaches it points at a fresh slab. Each
                // change re-publishes `request_kva` AND
                // `kind_host_ptr` so vCPU threads re-arm on the new
                // KVA and post-fire `read_volatile` reads land on the
                // current slab — the previous one-shot publish gate
                // pinned `kind_host_ptr` at the original slab page
                // forever, and a stale deref after rebind would touch
                // freed (or repurposed) host memory.
                //
                // Resolution sequence per scan tick:
                //   1. read scx_root_kva from KernelSymbols (resolved
                //      once at coord-start via vmlinux);
                //   2. translate scx_root_kva → root_pa via
                //      `GuestKernel::text_kva_to_pa` (it lives in the
                //      kernel text mapping, not vmalloc);
                //   3. read u64 at root_pa to get sched_kva (the
                //      vmalloc-allocated `struct scx_sched`);
                //   4. compare against `last_sched_kva` — bail on no
                //      change (fast path on every scan tick post-
                //      attach);
                //   5. on change to non-zero: publish
                //      `sched_kva + exit_kind_offset` into
                //      `request_kva` (and the matching host pointer
                //      into `kind_host_ptr`); each vCPU thread polls
                //      that slot before its next KVM_RUN and re-arms;
                //   6. on change to zero (detach): publish 0 / null so
                //      vCPUs disarm via `KVM_SET_GUEST_DEBUG` without
                //      this slot's enable bits and stop tripping on
                //      the now-freed slab address.
                //
                // `*scx_root` only becomes non-NULL once a sched_ext
                // scheduler attaches; before that we silently retry
                // — the BPF .bss fallback (still wired up below)
                // covers the gap.
                let mut last_sched_kva: u64 = 0;
                let mut cached_exit_kind_pa: Option<u64> = None;
                let mut freeze_state = FreezeState::Idle;
                // Cached early snapshot from a midway-trigger freeze.
                // Held until the late freeze fires; then both early
                // and late are wrapped into a DualFailureDumpReport
                // and emitted as one file. The late-trigger Captured
                // / Degraded / Suppressed arms each consume the early
                // via `.take()` and emit it alongside the late as a
                // tagged sibling per the
                // [`crate::monitor::dump::SNAPSHOT_TAG_EARLY_*`]
                // constants; if the late trigger never fires for the
                // run, the end-of-coord drain at the bottom of this
                // closure body emits the early to
                // [`crate::monitor::dump::SNAPSHOT_TAG_EARLY_ONLY_LATE_NEVER_FIRED`].
                // Every captured early reaches disk via one of the
                // four paths.
                //
                // EarlySnapshotGuard wraps `early_snapshot` and
                // `early_retain_tag` so a panic-unwind anywhere in the
                // coord closure body still flushes a held Captured
                // early to disk via the Drop impl. The normal-path
                // end-of-coord drain (below) calls
                // `early_guard.drain_to_disk()` explicitly; Drop's
                // call is a no-op in the normal case because the
                // snapshot.take() in the normal drain leaves the
                // guard's `snapshot` field None.
                //
                // `retain_tag` field: when the late-trigger Degraded /
                // Suppressed arm fails to write its tagged sibling
                // and retains the snapshot for the end-of-coord drain
                // to retry, the failed arm sets
                // `early_guard.retain_tag = Some(...)` so the drain
                // reads it on retry and lands the recovered file at
                // the operator-correct path (e.g.
                // `early-pre-late-degraded` for a failed Degraded-arm
                // write) rather than the default
                // `early-only-late-never-fired` which would
                // misrepresent the case. `None` means "no retry
                // pending — the late never fired" (drain uses its
                // default tag).
                let mut early_guard = EarlySnapshotGuard {
                    snapshot: None,
                    retain_tag: None,
                    dump_path: freeze_coord_dump_path.clone(),
                    dual_snapshot: freeze_coord_dual_snapshot,
                };
                // Stash the early-trigger Degraded reason so the
                // late-trigger emit can surface it via
                // `DualFailureDumpReport::early_skipped_reason`.
                // Without this, an early-Degraded outcome leaves
                // `early_snapshot` None AND `early_peak_max_age_jiffies`
                // already populated (the max_age was read before the
                // Degraded outcome was matched), so the late-arm's
                // `early_skipped_reason` calculator falls through to
                // the existing "max_age never crossed threshold"
                // branch and surfaces an internally contradictory
                // reason (peak >= threshold) — operator reads a
                // misleading "max_age never crossed" when the actual
                // cause was a rendezvous timeout in the early capture.
                // Stashing the reason here makes the early-Degraded
                // tagged sibling discoverable from the dual wrapper.
                let mut early_degraded_reason: Option<String> = None;
                // Per-snapshot scanner metadata, captured at the
                // early-trigger site and threaded into the
                // DualFailureDumpReport wrapper alongside the
                // snapshots themselves. Both fields stay 0 when no
                // early snapshot fires, mirroring the report's
                // `skip_serializing_if = is_zero` behaviour.
                let mut early_max_age_jiffies: u64 = 0;
                let mut early_threshold_jiffies: u64 = 0;
                // Lazy-resolved runnable_at scanner context. Built
                // from the same vmlinux ELF the .bss-poll path uses.
                // None disables the early-trigger path on this
                // iteration but does not block the late path. Becomes
                // Some once every prerequisite resolves; cached for
                // the rest of the run.
                let mut scan_ctx: Option<RunnableScanCtx> = None;
                // Latest skip reason from try_resolve. Captures the
                // specific prerequisite that prevented resolution on
                // the most recent attempt (most useful when scan_ctx
                // is still None at the late-trigger point) so the
                // late-trigger emission can stamp it into
                // `DualFailureDumpReport::early_skipped_reason`. Set
                // back to None on a successful resolve so a once-
                // failed-then-recovered run does not carry stale
                // breadcrumbs forward.
                let mut scan_ctx_skip_reason: Option<&'static str> = None;
                // Retry counter and one-shot warn latch for the
                // scan_ctx resolve. The resolve runs once per
                // SCAN_INTERVAL (250 ms) poll iteration until it
                // succeeds; without a diagnostic an operator who
                // built ktstr against a kernel lacking
                // sched_ext_entity (or stripped of jiffies_64)
                // gets a silent dual-snapshot disable.
                // Wait `SCAN_CTX_WARN_AFTER_ITERS` iterations
                // (~3 s at 250 ms cadence) before warning so legit
                // boot-time delays (owned_accessor not yet ready,
                // GuestKernel handshake mid-flight) don't trigger
                // false alarms. The latch ensures the warn fires at
                // most once per VM run.
                let mut scan_ctx_retries: u32 = 0;
                let mut scan_ctx_warned: bool = false;
                const SCAN_CTX_WARN_AFTER_ITERS: u32 = 12;
                // The accessor-init worker spawned above owns the
                // retry/warn discipline for its two `try_init_*`
                // helpers; the coordinator no longer tracks
                // `accessor_retries` / `accessor_warned` /
                // `accessor_last_err` fields here. The constant below
                // is reused by the `prog_per_cpu_offsets` /
                // `scan_ctx` retry blocks further down.
                const LAZY_ACCESSOR_WARN_AFTER_ITERS: u32 = 10;
                // Sibling state for `try_init_prog_per_cpu_offsets`.
                // Two distinct failure modes warrant different
                // diagnostics: a missing `__per_cpu_offset` symbol
                // (`per_cpu_offset_kva == 0`) is a PERMANENT failure
                // that warns immediately on the first observation —
                // the symbol won't materialise mid-run, so retrying
                // silently masks a stripped vmlinux. Conversely, a
                // present symbol whose live array still has zero
                // slots (`offsets.contains(&0)`) is a TRANSIENT
                // boot-progress condition that resolves once the
                // guest's `setup_per_cpu_areas` populates each
                // CPU's slot; warn after `LAZY_ACCESSOR_WARN_AFTER_ITERS`
                // retries so a guest that genuinely fails to bring
                // up its per-CPU areas surfaces a diagnostic
                // instead of permanently-disabled
                // prog_runtime_stats. Each warn latches via its
                // own `_warned` bool to fire at most once per VM run.
                let mut per_cpu_offsets_retries: u32 = 0;
                let mut per_cpu_offsets_warned: bool = false;
                let mut per_cpu_offsets_kva_warned: bool = false;
                // Half of the configured watchdog timeout, expressed
                // in guest jiffies. Computed once from
                // freeze_coord_watchdog_half + freeze_coord_hz so each
                // poll's comparison is a cheap u64 compare against
                // the scan's max age.
                //
                // ms-precision conversion lives in [`duration_to_jiffies`];
                // see its doc for why the seconds-based form is wrong.
                let half_threshold_jiffies =
                    duration_to_jiffies(freeze_coord_watchdog_half, freeze_coord_hz);
                // Trajectory tracking for the early-trigger diagnostic.
                // Records the max `max_age` observed across the run
                // and how many scan iterations have run. Surfaced in a
                // warn when err_triggered fires while
                // freeze_state == Idle (i.e. the early path never
                // captured) so an operator can distinguish three
                // failure modes from a single log line:
                //
                //   - early_scan_iters == 0   → scan_ctx never resolved
                //                                (scan_ctx_warn already
                //                                fires earlier; this
                //                                cross-checks).
                //   - peak_max_age == 0       → scan ran but never
                //                                observed a live task
                //                                (likely empty
                //                                runnable_list, wrong
                //                                offsets, or the scan
                //                                was reading unmapped
                //                                memory).
                //   - peak_max_age > 0 but    → scan was working but
                //     < half_threshold          the kernel watchdog
                //                                fired before any task
                //                                aged past the
                //                                half-way mark (very
                //                                short stalls or an
                //                                err-class exit that
                //                                isn't a stall, e.g.
                //                                scx_bpf_error()).
                //
                // The `FailureDumpReport` Display impl in
                // `crate::monitor::dump::display` already points
                // operators at `RUST_LOG=ktstr=debug` for scan
                // resolution; this trajectory snapshot is the more
                // actionable signal because it's emitted at the
                // moment of failure with structured fields rather
                // than as a per-iteration debug stream.
                let mut early_peak_max_age_jiffies: u64 = 0;
                let mut early_scan_iters: u64 = 0;
                // Cadence policy. The loop blocks in `epoll_wait`
                // until one of the registered fds fires (kill,
                // bsp_done, virtio-console TX, watchpoint hit,
                // scanner tick) OR `POLL_TIMEOUT_MS` elapses. The
                // previous
                // implementation drove this by `thread::sleep(500
                // µs)` and a `poll_iter % 200 == 0` decimator. The
                // event-driven design wakes the coordinator within
                // microseconds of any trigger source — including
                // the watchpoint hit and the kill / bsp_done flips —
                // and only does heavy work (boot-race accessor
                // construction, BPF .bss-PA lookup, runnable_at
                // scan) when the periodic scanner timerfd fires.
                const POLL_TIMEOUT_MS: i32 = 500;
                // 250 ms gives enough resolution for typical
                // half-watchdog thresholds (e.g. 4000 jiffies on
                // a 1 kHz HZ kernel = 4 s, so a 250 ms scan
                // cadence catches the half-way crossing within
                // 6.25% of the threshold) while halving the
                // freeze coord's scan-tick CPU draw vs the
                // legacy 100 ms cadence. The early-trigger path
                // walks both the global `scx_tasks` list and
                // every per-CPU `rq->scx.runnable_list` per
                // tick; on a many-vCPU host the larger interval
                // matters.
                const SCAN_INTERVAL: Duration = Duration::from_millis(250);
                // Per-fd epoll tokens. Match-on tokens dispatches
                // events without re-reading fd numbers.
                const TOKEN_KILL: u64 = 0;
                const TOKEN_BSP_DONE: u64 = 1;
                const TOKEN_WATCHPOINT: u64 = 3;
                const TOKEN_SCANNER: u64 = 4;
                /// virtio-console tx_evt — wakes whenever the guest
                /// publishes a TX descriptor chain on port 0 or port 1.
                /// The coordinator drains port-1 bulk TLV bytes and
                /// promotes a SCHED_EXIT entry into the run-wide
                /// `kill` flag, and intercepts
                /// [`crate::vmm::wire::MSG_TYPE_SNAPSHOT_REQUEST`]
                /// frames so the matching dispatch (CAPTURE / WATCH)
                /// runs in the same iteration body and the reply
                /// is pushed back to the guest via
                /// [`crate::vmm::virtio_console::VirtioConsole::queue_input_port1`].
                /// Port-0 (console) TX wakes are harmless: the coord
                /// drain returns an empty buffer and the byte stays
                /// in the host stdout thread's `drain_output` slot.
                /// Port 2 TX (scheduler stats) does not reach this
                /// epoll set — the
                /// [`crate::vmm::sched_stats::SchedStatsClient`]
                /// owns its own drainer thread and stats_tx_evt
                /// epoll, leaving this coordinator unaffected by
                /// stats traffic.
                const TOKEN_TX: u64 = 5;
                const TOKEN_ACCESSOR_READY: u64 = 6;
                let epoll = match Epoll::new() {
                    Ok(e) => e,
                    Err(e) => {
                        tracing::error!(
                            error = %e,
                            "freeze-coord: epoll_create1 failed; aborting coordinator"
                        );
                        return;
                    }
                };
                use std::os::unix::io::AsRawFd;
                let mut scanner_tfd = match TimerFd::new() {
                    Ok(t) => t,
                    Err(e) => {
                        tracing::error!(
                            error = %e,
                            "freeze-coord: timerfd_create failed; aborting coordinator"
                        );
                        return;
                    }
                };
                if let Err(e) = scanner_tfd.reset(SCAN_INTERVAL, Some(SCAN_INTERVAL)) {
                    tracing::error!(
                        error = %e,
                        "freeze-coord: timerfd_settime failed; aborting coordinator"
                    );
                    return;
                }
                // Register every fd. Failure to register any one of
                // these would cause the coordinator to silently miss
                // a wake source, so abort instead of degrading.
                for (fd, token, name) in [
                    (freeze_coord_kill_evt.as_raw_fd(), TOKEN_KILL, "kill_evt"),
                    (
                        freeze_coord_bsp_done_evt.as_raw_fd(),
                        TOKEN_BSP_DONE,
                        "bsp_done_evt",
                    ),
                    (
                        freeze_coord_hit_evt.as_raw_fd(),
                        TOKEN_WATCHPOINT,
                        "watchpoint_hit_evt",
                    ),
                    (scanner_tfd.as_raw_fd(), TOKEN_SCANNER, "scanner_tfd"),
                    (freeze_coord_tx_evt.as_raw_fd(), TOKEN_TX, "virtio_console_tx_evt"),
                    (accessor_ready_evt.as_raw_fd(), TOKEN_ACCESSOR_READY, "accessor_ready_evt"),
                ] {
                    if let Err(e) = epoll.ctl(
                        ControlOperation::Add,
                        fd,
                        EpollEvent::new(EventSet::IN, token),
                    ) {
                        tracing::error!(
                            error = %e,
                            fd_name = name,
                            "freeze-coord: epoll_ctl ADD failed; aborting coordinator"
                        );
                        return;
                    }
                }
                let mut events_buf = [EpollEvent::default(); 6];
                // Accumulator for partially-received TLV bulk frames.
                // The kernel's virtio_console TX path issues
                // descriptor chains as the guest writes; a single
                // logical TLV frame can span multiple wakes if the
                // guest's `write_all` was split across pages or
                // descriptor sizes. The streaming
                // [`crate::vmm::bulk::HostAssembler`] retains partial
                // bytes across `feed` calls so a frame split across
                // multiple TX wakes is recovered without loss.
                //
                // SCHED_EXIT promotion: every drained message is
                // inspected for [`wire::MSG_TYPE_SCHED_EXIT`]; when
                // observed, the run-wide `kill` flag flips so the
                // BSP run loop and the watchdog exit promptly
                // instead of waiting for the watchdog deadline.
                let mut bulk_assembler = crate::vmm::bulk::HostAssembler::new();
                // Per-iteration accumulator for guest-side
                // [`crate::vmm::wire::MSG_TYPE_SNAPSHOT_REQUEST`]
                // frames the TOKEN_TX handler decoded. Drained later
                // in the iteration body where `freeze_and_capture` /
                // `thaw_and_barrier` / `arm_user_watchpoint` are in
                // scope; the dispatch frames a
                // `MSG_TYPE_SNAPSHOT_REPLY` TLV and pushes it back
                // through `queue_input_port1`. CRC-failed frames are
                // never appended — a torn frame would otherwise let
                // a hostile guest force a spurious capture, mirroring
                // the SCHED_EXIT promotion gate.
                let mut snapshot_requests_pending: Vec<SnapshotRequest> = Vec::new();
                // CAPTURE requests received before `owned_accessor`
                // adoption are queued here instead of being serviced
                // immediately. Servicing pre-adoption produces a
                // partial-dump report (0 maps, vcpu_regs only — see
                // the `// Partial dump:` branch in
                // `freeze_and_capture`) which is useless to the test
                // author who asked for `Op::snapshot("...")`.
                //
                // The queue is drained at the accessor-adoption site
                // by appending its contents back onto
                // `snapshot_requests_pending`, so the same iteration's
                // CAPTURE drain dispatches them through the normal
                // `freeze_and_capture(false)` flow with the accessor
                // present.
                //
                // If the accessor never adopts (worker permanently
                // failed past its 60 s deadline), the queue is
                // dropped at coord exit and the guest's blocking
                // reader on `/dev/vport0p1` times out at the per-Op
                // 30 s deadline — same observable behaviour as a
                // late-boot rendezvous timeout. WATCH requests are
                // NOT deferred: WATCH only needs the symbol cache,
                // which is independent of `owned_accessor`.
                let mut capture_requests_deferred: Vec<SnapshotRequest> = Vec::new();
                // Periodic-capture state. `periodic_boundaries_ns`
                // is the precomputed list of `Instant` deadlines
                // (encoded as nanos-since-`run_start`) at which the
                // run-loop fires `freeze_and_capture(false)`. Lazily
                // built on the first iteration AFTER BOTH:
                //   1. `KtstrVm::num_snapshots > 0` (periodic capture
                //      is requested), AND
                //   2. `workload_duration_for_coord` is `Some(d)`
                //      (the workload has a duration to slice), AND
                //   3. `scenario_start_ns_for_coord` reads non-zero
                //      (the first ScenarioStart frame has been
                //      observed and stamped by the dispatch arm).
                //
                // Boundaries divide the 10%–90% workload window into
                // `N + 1` equal intervals, producing `N` interior
                // boundaries — `N == 1` lands a single sample at
                // `start + 0.5 d` (midpoint); `N == 3` lands at
                // 0.3 d, 0.5 d, 0.7 d. The 10% pre-boundary buffer
                // and 10% post-boundary buffer give the workload
                // ramp-up / ramp-down room without periodic samples
                // landing on transient state.
                //
                // `next_periodic_idx` tracks how many boundaries
                // have already fired. When the gate
                // (`freeze_coord_on_demand_in_flight`) is held by a
                // concurrent on-demand or watchpoint capture, the
                // periodic boundary is deferred (NOT skipped) until
                // a subsequent iteration finds the gate clear — the
                // 10% buffer is the slack budget for this wait.
                let mut periodic_boundaries_ns: Option<Vec<u64>> = None;
                let mut next_periodic_idx: u32 = 0;
                // Consecutive parked-vCPU rendezvous failures during
                // periodic capture. Reset to 0 on every successful
                // `freeze_and_capture(..)`. After 2 consecutive
                // timeouts the run-loop abandons the remaining
                // periodic boundaries and logs once — repeated
                // 30 s rendezvous waits on a wedged guest would
                // otherwise eat the entire wall-clock budget without
                // producing useful captures, and a single abandoned
                // boundary keeps periodic noise off a guest the
                // operator already knows is degraded.
                let mut periodic_consecutive_timeouts: u32 = 0;
                let mut periodic_abandoned: bool = false;
                const PERIODIC_TIMEOUT_ABANDON_THRESHOLD: u32 = 2;
                // First iteration always runs scan-tick work so
                // boot-race lazy resolution attempts fire
                // immediately rather than waiting up to 100 ms for
                // the timerfd's first edge. Subsequent iterations
                // gate scan-tick on the SCANNER token (or on a
                // POLL_TIMEOUT-driven wake) — the watchpoint event
                // itself never sets scan_tick, which is correct:
                // that trigger is a fast path that should not block
                // the next wake on heavy bss-PA / scan_ctx work.
                let mut scan_tick: bool;
                let mut first_iter = true;
                let mut bsp_done_final_pass = false;
                // Mirror of `bsp_done_final_pass` for the SCHED_EXIT
                // kill-promotion-without-bsp-done case: kill can be
                // promoted by sources other than a clean BSP_DONE —
                // e.g. the guest's
                // sched-exit-monitor fires `send_sched_exit` on
                // scheduler-binary pidfd POLLIN, the host bulk-port
                // dispatch flips `freeze_coord_kill` — but the BPF
                // tp_btf/sched_ext_exit handler that latches
                // `ktstr_err_exit_detected` in `.bss` fires from
                // inside the kernel before the userspace pidfd
                // POLLIN. Without a sched-exit final pass the coord
                // loop exits on the kill edge BEFORE the next BSS
                // read at the iteration top, so the sticky kernel-
                // side latch never gets observed and the failure
                // dump is silently dropped. Granting one more
                // iteration when kill is set (any source) closes
                // that race — the BSS read at the iteration top
                // catches the already-flipped latch and the
                // late-trigger path emits a normal dump. The BSS
                // write happens before the SCHED_EXIT msg arrives
                // on the host (kernel tp_btf precedes userspace
                // pidfd POLLIN), so one final-pass iteration is
                // sufficient.
                let mut sched_exit_final_pass = false;
                'coord: while !freeze_coord_kill.load(Ordering::Acquire)
                    || (freeze_coord_bsp_done.load(Ordering::Acquire)
                        && !bsp_done_final_pass)
                    || (!freeze_coord_bsp_done.load(Ordering::Acquire)
                        && !sched_exit_final_pass)
                {
                    if freeze_coord_bsp_done.load(Ordering::Acquire) {
                        if bsp_done_final_pass {
                            if capture_requests_deferred.is_empty()
                                || owned_accessor.is_some()
                            {
                                break 'coord;
                            }
                            eprintln!(
                                "freeze-coord: staying alive for deferred captures: deferred={} accessor={}",
                                capture_requests_deferred.len(),
                                owned_accessor.is_some()
                            );
                        }
                        bsp_done_final_pass = true;
                    } else if freeze_coord_kill.load(Ordering::Acquire) {
                        // Kill is set but BSP_DONE is not — SCHED_EXIT
                        // or other external kill source. Grant one
                        // more iteration so the BSS-latch read at
                        // the iteration top observes any sticky
                        // kernel-side flip that landed in the same
                        // window the kill promotion did. Second
                        // iteration through this branch (sched_exit_
                        // final_pass already true) exits immediately
                        // — no deferred-capture-stay-alive semantics
                        // here because the scheduler has crashed,
                        // not exited cleanly.
                        if sched_exit_final_pass {
                            break 'coord;
                        }
                        sched_exit_final_pass = true;
                    }
                    // Unified event dispatch: epoll.wait on EVERY
                    // iteration. iter1 uses timeout=0 (non-blocking)
                    // so scan_tick fires immediately; subsequent
                    // iterations block up to POLL_TIMEOUT_MS. This
                    // ensures TOKEN_TX (KERN_ADDRS, SYS_RDY) is
                    // dispatched event-driven on every iteration —
                    // no manual drain calls needed.
                    let poll_ms = if first_iter { 0 } else { POLL_TIMEOUT_MS };
                    if first_iter {
                        scan_tick = true;
                        first_iter = false;
                    } else {
                        scan_tick = false;
                    }
                    if bsp_done_final_pass || sched_exit_final_pass {
                        scan_tick = true;
                    }
                    {
                        let event_count = match epoll.wait(poll_ms, &mut events_buf) {
                            Ok(n) => n,
                            Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
                            Err(e) => {
                                tracing::error!(
                                    error = %e,
                                    "freeze-coord: epoll_wait failed; exiting coordinator"
                                );
                                break 'coord;
                            }
                        };
                        // Drain every fd that fired. Tokens map
                        // 1:1 to source fds; KILL / BSP_DONE both
                        // exit the loop, the others either set
                        // scan_tick (SCANNER) or surface state via
                        // the existing latch reads later in the
                        // body (WATCHPOINT).
                        for ev in &events_buf[..event_count] {
                            match ev.data() {
                                TOKEN_KILL | TOKEN_BSP_DONE => {
                                    let _ = freeze_coord_kill_evt.read();
                                    let _ = freeze_coord_bsp_done_evt.read();
                                }
                                TOKEN_SCANNER => {
                                    // Drain the timerfd's expiry
                                    // counter — re-arming is
                                    // automatic for periodic
                                    // timers, but the counter
                                    // accumulates and would re-
                                    // wake on the next epoll_wait
                                    // if not drained.
                                    let _ = scanner_tfd.wait();
                                    scan_tick = true;
                                }
                                TOKEN_WATCHPOINT => {
                                    // Drain the eventfd counter so
                                    // a subsequent epoll_wait
                                    // doesn't immediately re-fire
                                    // on the same edge. The
                                    // watchpoint.hit AtomicBool is
                                    // the source of truth — its
                                    // state survives the eventfd
                                    // drain and the late-trigger
                                    // detection later in the loop
                                    // re-loads it with Acquire.
                                    let _ = freeze_coord_hit_evt.read();
                                }
                                TOKEN_ACCESSOR_READY => {
                                    let _ = accessor_ready_evt.read();
                                    scan_tick = true;
                                }
                                TOKEN_TX => {
                                    // Drain the tx_evt counter so
                                    // a subsequent epoll_wait
                                    // doesn't immediately re-fire
                                    // on the same edge. The drain
                                    // below uses the device's TX
                                    // buffer (port1_tx_buf) as the
                                    // source of truth — bytes the
                                    // device accumulated since the
                                    // last wake are returned by
                                    // `drain_bulk` and threaded
                                    // through `bulk_assembler`. A
                                    // counter overflow under
                                    // EFD_NONBLOCK is benign
                                    // because the buffer state is
                                    // authoritative.
                                    //
                                    // Critical-section discipline:
                                    // `tx_evt.read()` is a syscall
                                    // and `bulk_assembler.feed()`
                                    // does TLV parsing (memcpy +
                                    // CRC + per-frame cap check).
                                    // Both are kept STRICTLY
                                    // outside the device mutex so
                                    // the vCPU thread emitting
                                    // bytes via virtio-console TX
                                    // never blocks behind the
                                    // coord. The explicit
                                    // `let bytes = { ... };`
                                    // block bounds the lock to the
                                    // single `drain_bulk` call —
                                    // a future refactor that
                                    // moves work into the block
                                    // is loud about the regression.
                                    let _ = freeze_coord_tx_evt.read();
                                    let bytes = {
                                        let mut g =
                                            freeze_coord_virtio_con.lock();
                                        g.drain_bulk()
                                    };
                                    let drained = bulk_assembler.feed(&bytes);
                                    // Per-frame typed dispatch.
                                    // Exhaustive `match
                                    // MsgType::from_wire(...)` so a
                                    // future MsgType variant addition
                                    // is a compile error here — the
                                    // arms call out exactly which
                                    // frames have coordinator-side
                                    // side effects (SchedExit / SysRdy
                                    // / SnapshotRequest), and every
                                    // other variant falls through to a
                                    // single "test-verdict-bearing"
                                    // arm whose only action is to
                                    // accumulate the entry into the
                                    // shared bucket. The prior if-
                                    // ladder of `msg.msg_type ==
                                    // MSG_TYPE_*` checks let a new
                                    // variant slip past the host
                                    // without an explicit decision.
                                    //
                                    // Every CRC-bearing arm gates on
                                    // `msg.crc_ok` so a torn frame
                                    // cannot promote into kill_evt /
                                    // sys_rdy_evt or trigger a
                                    // capture — same hostile-guest
                                    // discipline as the prior code.
                                    let mut bucket: Vec<crate::vmm::wire::ShmEntry> =
                                        Vec::new();
                                    let mut sinks = BulkDispatchSinks {
                                        kill: &freeze_coord_kill,
                                        kill_evt: &freeze_coord_kill_evt,
                                        sys_rdy_evt: &mut freeze_coord_sys_rdy_evt,
                                        snapshot_requests_pending:
                                            &mut snapshot_requests_pending,
                                        kern_phys_base: &kern_phys_base,
                                        kern_phys_base_evt: &kern_phys_base_evt,
                                        watchdog_reset: workload_duration_for_coord.map(|d| {
                                            (watchdog_reset_for_coord.as_ref(), d, run_start)
                                        }),
                                        watchdog_pause_ns: watchdog_pause_for_coord.as_ref(),
                                        scenario_start_ns: scenario_start_ns_for_coord.as_ref(),
                                        scenario_pause_cumulative_ns:
                                            scenario_pause_cumulative_for_coord.as_ref(),
                                        run_start,
                                    };
                                    for msg in &drained.messages {
                                        if let Some(entry) =
                                            dispatch_bulk_message(msg, &mut sinks)
                                        {
                                            bucket.push(entry);
                                        }
                                    }
                                    // Append the verdict-bearing entries
                                    // to the shared bucket so
                                    // `collect_results` can merge them
                                    // into the final `BulkDrainResult`.
                                    // Coordinator-internal control
                                    // frames are filtered inside
                                    // `dispatch_bulk_message` (the
                                    // SysRdy / SnapshotRequest arms
                                    // return None) — keying on
                                    // [`crate::vmm::wire::MsgType::is_coordinator_internal`]
                                    // keeps the filter set in lockstep
                                    // with `collect_results`'s post-run
                                    // drain. Without this stash, every
                                    // TLV frame the guest published
                                    // mid-run is silently dropped —
                                    // only late-arriving bytes that
                                    // landed in `port1_tx_buf` after
                                    // the coord stopped polling reach
                                    // the verdict.
                                    if !bucket.is_empty() {
                                        let mut buf = freeze_coord_bulk_messages_for_closure
                                            .lock()
                                            .unwrap_or_else(|e| e.into_inner());
                                        buf.extend(bucket);
                                    }
                                }
                                _ => {}
                            }
                        }
                        // No break on kill/bsp_done here — the
                        // iteration body must run so the
                        // late-trigger err_triggered check and
                        // freeze_and_capture can fire. The while
                        // condition + inner bsp_done check handle
                        // loop exit after the body completes.
                    }
                    // Adopt the worker-published accessor pair as
                    // soon as it lands. Both halves of the pair land
                    // atomically via `OnceLock::set` so a single
                    // `get()` returns either both or neither — no
                    // partial-Some shape to handle. The worker logs
                    // its own warn/eprintln on a permanent failure
                    // (60 s deadline exceeded), so the coordinator
                    // doesn't track separate retry counters here:
                    // a None pair after the worker has exited just
                    // means the dump path is unavailable for this
                    // run, which the existing call-site `is_some()`
                    // gates already handle gracefully.
                    if scan_tick && owned_accessor.is_none()
                        && let Some((map, prog)) = accessors_oncelock.get()
                    {
                        owned_accessor = Some(map);
                        if let Some(prog) = prog.as_ref() {
                            owned_prog_accessor = Some(prog);
                        }
                        {
                            let kernel = map.guest_kernel();
                            let pb = kernel.phys_base();
                            if pb != 0
                                && let Some(pb_kva) = dump_cpu_time_symbols
                                    .as_ref()
                                    .and_then(|s| s.phys_base_kva)
                                {
                                    let pb_pa = kernel.text_kva_to_pa(pb_kva);
                                    if let Some(ref mem) = freeze_coord_mem {
                                        let real_pb = mem.read_u64(pb_pa, 0);
                                        coord_kaslr_offset = pb.wrapping_sub(real_pb);
                                    }
                                }
                        }
                        // Drain CAPTURE requests deferred during the
                        // pre-adoption window. Append onto
                        // `snapshot_requests_pending` so the existing
                        // CAPTURE drain (further down this iteration
                        // body) dispatches them through the normal
                        // `freeze_and_capture(false)` flow with the
                        // accessor present — no flow duplication.
                        if !capture_requests_deferred.is_empty() {
                            let n = capture_requests_deferred.len();
                            tracing::info!(
                                deferred_count = n,
                                "freeze-coord: draining deferred CAPTURE \
                                 requests after owned_accessor adoption"
                            );
                            snapshot_requests_pending.append(
                                &mut capture_requests_deferred,
                            );
                        }
                    }
                    // Resolve the per-CPU offset array once the prog
                    // accessor lands. Reads `__per_cpu_offset` from
                    // the kernel's static symbol table and uses it
                    // to read each CPU's offset slot. Cached for the
                    // rest of the run — the array is fixed at boot
                    // (per-CPU areas are allocated at kernel init,
                    // see `setup_per_cpu_areas`) and the freeze
                    // coordinator never sees a CPU hot-plug event,
                    // so a single read is enough.
                    //
                    // The `__per_cpu_offset` KVA is sourced from the
                    // already-cached `dump_cpu_time_symbols` —
                    // re-parsing vmlinux every scan tick (~100 ms)
                    // while waiting for the per-CPU areas to come up
                    // would re-read 50 MB+ of ELF and rebuild the
                    // symbol table on every iteration. The KVA is
                    // fixed at kernel link time so a single resolution
                    // suffices for the rest of the run; if
                    // `dump_cpu_time_symbols` is None (vmlinux
                    // unparseable at coord start) or its
                    // `per_cpu_offset` is 0 (symbol stripped),
                    // `try_init_prog_per_cpu_offsets` returns None
                    // and the cache stays unset — same behaviour as
                    // the prior in-helper parse path.
                    if scan_tick
                        && prog_per_cpu_offsets.is_none()
                        && let Some(mem) = freeze_coord_mem.as_deref()
                    {
                        let per_cpu_offset_kva = dump_cpu_time_symbols
                            .as_ref()
                            .map(|s| s.per_cpu_offset)
                            .unwrap_or(0);
                        if per_cpu_offset_kva == 0 {
                            // Permanent failure: the symbol is absent
                            // from `dump_cpu_time_symbols` (vmlinux
                            // unparseable at coord start, or
                            // `__per_cpu_offset` stripped from the
                            // image). Warn immediately on the first
                            // observation — no amount of retrying
                            // will materialise a missing symbol — and
                            // latch via `per_cpu_offsets_kva_warned`
                            // so the warn fires at most once per VM
                            // run. The `prog_per_cpu_offsets` cache
                            // stays None and downstream
                            // prog_runtime_stats capture is
                            // permanently degraded for this run.
                            if !per_cpu_offsets_kva_warned {
                                tracing::warn!(
                                    "freeze-coord: __per_cpu_offset symbol absent from \
                                     dump_cpu_time_symbols (vmlinux unparseable at coord \
                                     start, or symbol stripped) — prog_runtime_stats \
                                     capture is permanently degraded for this run; \
                                     will not retry"
                                );
                                per_cpu_offsets_kva_warned = true;
                            }
                        } else {
                            let phys_base = owned_accessor
                                .as_ref()
                                .map(|a| a.guest_kernel().phys_base())
                                .unwrap_or(0);
                            prog_per_cpu_offsets = try_init_prog_per_cpu_offsets(
                                mem,
                                per_cpu_offset_kva,
                                freeze_coord_tcr_el1.as_ref(),
                                phys_base,
                                freeze_coord_num_cpus,
                            );
                            if prog_per_cpu_offsets.is_none() {
                                // Transient boot-progress condition:
                                // the symbol is present (kva != 0)
                                // but at least one CPU's offset slot
                                // is still zero. The guest's
                                // `setup_per_cpu_areas` populates
                                // every slot before SMP bringup, so
                                // a non-zero retry count after
                                // `LAZY_ACCESSOR_WARN_AFTER_ITERS`
                                // iterations indicates the guest
                                // genuinely failed to bring up its
                                // per-CPU areas (or
                                // `freeze_coord_num_cpus` exceeds
                                // the configured `nr_cpu_ids` so
                                // slots beyond the live count
                                // legitimately read 0).
                                per_cpu_offsets_retries += 1;
                                if !per_cpu_offsets_warned
                                    && per_cpu_offsets_retries
                                        >= LAZY_ACCESSOR_WARN_AFTER_ITERS
                                {
                                    tracing::warn!(
                                        retries = per_cpu_offsets_retries,
                                        num_cpus = freeze_coord_num_cpus,
                                        "freeze-coord: __per_cpu_offset array still has \
                                         zero slots after retries — most commonly a \
                                         still-booting guest (per-CPU areas not yet \
                                         allocated); a permanent failure (num_cpus \
                                         exceeds nr_cpu_ids, partial SMP bringup) \
                                         leaves prog_runtime_stats degraded. Will \
                                         continue retrying."
                                    );
                                    per_cpu_offsets_warned = true;
                                }
                            }
                        }
                    }
                    // Try to discover the probe .bss map and cache the
                    // PA of ktstr_err_exit_detected. Match by suffix
                    // "probe_bp.bss" rather than ".bss" so we don't
                    // race a scheduler-under-test's own .bss map when
                    // multiple BPF programs are loaded — libbpf names
                    // BPF program .bss maps as "<obj_short_name>.bss",
                    // and the probe object's name is "probe_bp" (per
                    // build.rs probe-skel generation, see the
                    // generated probe_skel.rs match arm
                    // `"probe_bp.bss" => bss = Some(map)`).
                    //
                    // Resolve the byte offset of
                    // `ktstr_err_exit_detected` within the probe's
                    // `.bss` section via BTF Datasec rather than
                    // hardcoding 0. The probe BPF program ships its
                    // own split BTF; its Datasec for `.bss` carries
                    // a VarSecinfo per writable global with the
                    // exact byte offset the BPF JIT places it at. A
                    // hardcoded 0 worked while the field was the
                    // sole writable global in `probe.bpf.c`, but a
                    // future addition that reorders globals (or that
                    // adds another writable global before this one)
                    // would silently shift the offset and break the
                    // freeze trigger. The BTF lookup keeps the
                    // detection robust across declaration changes.
                    //
                    // Falls back to offset 0 when the program BTF
                    // can't be loaded yet (guest still booting) or
                    // the Datasec walk fails — same recovery
                    // behaviour as the previous always-zero path.
                    //
                    // Invalidation pass first: a previously-cached
                    // PA is only as valid as the underlying map. If
                    // the probe BPF program unloads (test teardown,
                    // userspace explicit unload, parent process
                    // panicking before Drop) the kernel frees the
                    // bpf_array vmalloc page; any subsequent
                    // `read_u32(cached_bss_pa, 0)` reads whatever
                    // the page allocator hands out next — typically
                    // non-zero for slab pages reused by an unrelated
                    // subsystem. The result latches a phantom
                    // `err_triggered` and synthesizes a bogus
                    // failure dump on a healthy run. Re-walk
                    // `map_idr` and require the same-named map's
                    // `value_kva` to match the one we resolved
                    // against; on mismatch (map gone OR rebound to
                    // a fresh slab) clear the PA + companion
                    // value_kva cache so the discovery block below
                    // re-resolves from scratch. The walk uses a
                    // fresh `as_accessor()` instance — its
                    // `maps_cache` re-fills from a current map_idr
                    // traversal, so a stale entry from a prior dump
                    // cannot keep an unloaded map visible.
                    if scan_tick
                        && cached_bss_pa.is_some()
                        && let Some(owned) = owned_accessor
                    {
                        let accessor = owned.as_accessor();
                        let still_valid = match accessor.find_map("probe_bp.bss") {
                            Some(m) => m.value_kva == cached_bss_value_kva,
                            None => false,
                        };
                        if !still_valid {
                            tracing::warn!(
                                stale_value_kva = cached_bss_value_kva
                                    .map(|k| format!("{k:#x}"))
                                    .unwrap_or_else(|| "None".to_string()),
                                "freeze-coord: probe_bp.bss map gone or \
                                 rebound — invalidating cached_bss_pa to \
                                 prevent reads of a freed vmalloc page \
                                 (probe unload mid-run)"
                            );
                            cached_bss_pa = None;
                            cached_bss_value_kva = None;
                            // bss_field_offset is BTF-derived from
                            // probe.bpf.c globals; the layout
                            // cannot change across an unload+reload
                            // of the same probe object so the
                            // offset cache stays valid. Re-resolving
                            // it would re-pay the BTF parse for no
                            // semantic gain.
                        }
                    }
                    if scan_tick
                        && cached_bss_pa.is_none()
                        && let Some(owned) = owned_accessor
                        && let Some(ref mem) = freeze_coord_mem
                    {
                        let accessor = owned.as_accessor();
                        // Single map_idr walk per discovery attempt.
                        // value_kva is Some for ARRAY maps (the .bss
                        // map is a single-key ARRAY whose flex array
                        // holds the section's bytes); translate it
                        // (vmalloc-backed) to PA via the existing
                        // GuestMem page-walk and cache the result so
                        // subsequent polls are pure DRAM reads.
                        if let Some(map) = accessor.find_map("probe_bp.bss")
                            && let Some(value_kva) = map.value_kva
                        {
                            // Bind kernel once and reuse — pre-fix
                            // owned.guest_kernel() ran three times here
                            // and once again at the BTF Datasec walk
                            // below. The accessor is cheap but the
                            // repetition was noisy at the freeze hot
                            // path's read site.
                            let kernel = owned.guest_kernel();
                            let walk = kernel.walk_context();
                            // BTF-driven offset: load the probe's
                            // program BTF and walk its `.bss`
                            // Datasec for the named global. The
                            // result is cached in `cached_bss_offset`
                            // — only the first successful resolution
                            // pays the BTF parse cost. A None here
                            // before the cache is populated means
                            // either the program BTF isn't loaded
                            // yet (still-booting guest, retry
                            // silently) or the BTF walk is broken
                            // (warn once, fall back to offset 0).
                            if cached_bss_offset.is_none()
                                && map.btf_kva != 0
                                && let Some(ref base) = dump_btf
                            {
                                match load_probe_bss_offset(
                                    kernel,
                                    map.btf_kva,
                                    base,
                                    accessor.offsets(),
                                ) {
                                    Some(off) => {
                                        cached_bss_offset = Some(off);
                                    }
                                    None => {
                                        // map.btf_kva is non-zero
                                        // and dump_btf is loaded,
                                        // so the probe IS loaded
                                        // — a None now means the
                                        // BTF parse / Datasec
                                        // walk failed. Fall back
                                        // to 0 and stop retrying.
                                        if !bss_offset_warn_logged {
                                            tracing::warn!(
                                                "freeze-coord: BTF Datasec resolution \
                                                     failed, falling back to offset 0"
                                            );
                                            bss_offset_warn_logged = true;
                                        }
                                        cached_bss_offset = Some(0);
                                    }
                                }
                                // else: probe not loaded yet
                                // (map.btf_kva == 0 or dump_btf
                                // missing). Leave cached_bss_offset
                                // None so the next iteration retries
                                // without the warn fallback.
                            }
                            let bss_field_offset = cached_bss_offset.unwrap_or(0);
                            // Bound the BTF-derived offset against
                            // the map's declared `value_size`. The
                            // probe's BTF Datasec walk parses
                            // guest-supplied bytes — a corrupted
                            // (or hostile) BTF can return a u32
                            // offset that extends past the ARRAY's
                            // flex-array storage, so the
                            // `wrapping_add(bss_field_offset)`
                            // below would wrap into an unrelated
                            // guest page. Reading the resulting PA
                            // latches a phantom `err_triggered`
                            // and synthesizes a bogus failure
                            // dump. Reject any offset whose 4-byte
                            // read would walk past the map's
                            // value bytes; treat the failure
                            // exactly like a broken BTF walk —
                            // warn once via the existing latch and
                            // fall back to offset 0 for this and
                            // every subsequent iteration so the
                            // detection survives in degraded form
                            // instead of going silent. Saturating
                            // subtract guards `value_size < 4`
                            // (the map could not legitimately
                            // hold a u32 in that case, so
                            // `bss_field_offset > 0` rejects every
                            // non-zero offset, matching the
                            // "value_size too small" intent
                            // without a separate branch).
                            let max_offset = map.value_size.saturating_sub(4);
                            let bss_field_offset = if bss_field_offset > max_offset {
                                if !bss_offset_warn_logged {
                                    tracing::warn!(
                                        bss_field_offset,
                                        value_size = map.value_size,
                                        "freeze-coord: BTF-resolved bss field \
                                         offset exceeds value_size - 4 — \
                                         refusing to cache PA that would \
                                         read past the .bss flex array; \
                                         falling back to offset 0"
                                    );
                                    bss_offset_warn_logged = true;
                                }
                                cached_bss_offset = Some(0);
                                0
                            } else {
                                bss_field_offset
                            };
                            if let Some(translated) = crate::monitor::idr::translate_any_kva(
                                mem,
                                walk.cr3_pa,
                                walk.page_offset,
                                value_kva,
                                walk.l5,
                                walk.tcr_el1,
                            ) {
                                cached_bss_pa =
                                    Some(translated.wrapping_add(bss_field_offset as u64));
                                cached_bss_value_kva = Some(value_kva);
                            }
                        }
                    }
                    // Resolve the watchpoint target KVA
                    // (`*scx_root + exit_kind_offset`) and (re-)
                    // publish it whenever `*scx_root` changes. Runs
                    // every scan tick — the `last_sched_kva == new`
                    // fast path keeps the steady-state cost a single
                    // u64 read.
                    //
                    // Resolution requires:
                    //   - dump_cpu_time_symbols (KernelSymbols) for
                    //     `scx_root` symbol KVA — present whenever
                    //     vmlinux parsed at coord-start;
                    //   - dump_scx_walker_offsets.sched.exit_kind for
                    //     the field offset within `struct scx_sched`
                    //     — present whenever BTF carries the type;
                    //   - owned_accessor's GuestKernel for cr3_pa /
                    //     page_offset / l5 — needed for the same
                    //     direct-mapping translation `cached_bss_pa`
                    //     uses.
                    //
                    // The BPF .bss fallback below continues to update
                    // `cached_bss_pa`; both signals can fire and the
                    // late-trigger arm (a few iterations down the
                    // loop) treats either as ground truth. The
                    // watchpoint's advantages are synchronous
                    // delivery (no 100 ms polling window) AND
                    // independence from the probe BPF program loading
                    // correctly.
                    if scan_tick
                        && owned_accessor.is_some()
                        && let Some(ref syms) = dump_cpu_time_symbols
                        && let Some(scx_root_kva) = syms.scx_root
                        && let Some(ref scx_offsets) = dump_scx_walker_offsets
                        && let Some(ref sched_offs) = scx_offsets.sched
                        && let Some(ref mem) = freeze_coord_mem
                    {
                        // scx_root is a kernel-text-mapped pointer.
                        // The owned_accessor's GuestKernel carries the
                        // VA-bits-aware kernel image base resolved from
                        // TCR_EL1 (mirrors `read_scx_sched_state` in
                        // `monitor/scx_walker.rs`).
                        let kernel_for_root = owned_accessor
                            .as_ref()
                            .expect("owned_accessor.is_some() gate above")
                            .guest_kernel();
                        let root_pa = kernel_for_root.text_kva_to_pa(scx_root_kva);
                        let sched_kva = mem.read_u64(root_pa, 0);
                        // Drive the watchpoint state machine via the
                        // pure helper so unit tests can exercise the
                        // full `(last_sched_kva, sched_kva)` transition
                        // matrix (Unchanged / Detached / RebindDisarmed
                        // / Published / PublishDeferred) without
                        // booting a VM. The helper performs all
                        // ordered atomic stores per the contract on
                        // [`super::vcpu::WatchpointArm`]; the caller
                        // owns `last_sched_kva` and the result-driven
                        // logging.
                        match republish_watchpoint_on_rebind(
                            sched_kva,
                            last_sched_kva,
                            sched_offs.exit_kind as u32,
                            &freeze_coord_watchpoint,
                            kernel_for_root,
                            mem,
                        ) {
                            WatchpointPublishResult::Unchanged => {}
                            WatchpointPublishResult::Detached => {
                                tracing::info!(
                                    "freeze-coord: scx_root cleared (scheduler \
                                     detached); watchpoint disarmed pending next \
                                     attach"
                                );
                                last_sched_kva = 0;
                                // Clear cached_exit_kind_pa: the kernel
                                // RCU-freed the prior scx_sched (per
                                // kernel/sched/ext.c::scx_root_disable
                                // → RCU_INIT_POINTER(scx_root, NULL)),
                                // so the cached PA now points at slab
                                // memory subject to allocator reuse. A
                                // subsequent rendezvous-timeout
                                // Degraded report would read garbage
                                // from the recycled page. Invalidate
                                // here; the next Published arm will
                                // republish a fresh PA once the
                                // scheduler re-attaches.
                                cached_exit_kind_pa = None;
                            }
                            WatchpointPublishResult::RebindDisarmed {
                                previous,
                                next,
                            } => {
                                tracing::info!(
                                    last_sched_kva = format_args!("{:#x}", previous),
                                    new_sched_kva = format_args!("{:#x}", next),
                                    "freeze-coord: scx_root rebind detected \
                                     (A → B); watchpoint disarmed this tick, \
                                     B will be republished next tick after \
                                     vCPUs clear DR0"
                                );
                                last_sched_kva = 0;
                                // Same rationale as the Detached arm:
                                // the prior scx_sched is RCU-pending-
                                // free and the cached PA can begin
                                // reading recycled slab data. The
                                // next iteration's Published arm
                                // republishes for the new scheduler.
                                cached_exit_kind_pa = None;
                            }
                            WatchpointPublishResult::Published {
                                exit_kind_kva,
                                kind_pa,
                            } => {
                                last_sched_kva = sched_kva;
                                cached_exit_kind_pa = Some(kind_pa);
                                tracing::info!(
                                    exit_kind_kva =
                                        format_args!("{:#x}", exit_kind_kva),
                                    sched_kva = format_args!("{:#x}", sched_kva),
                                    kind_pa = format_args!("{:#x}", kind_pa),
                                    "freeze-coord: watchpoint target \
                                     published; vCPU threads will self-arm \
                                     KVM_SET_GUEST_DEBUG on next iteration"
                                );
                            }
                            WatchpointPublishResult::PublishDeferred {
                                exit_kind_kva,
                            } => {
                                tracing::debug!(
                                    exit_kind_kva =
                                        format_args!("{:#x}", exit_kind_kva),
                                    "freeze-coord: exit_kind translate or \
                                     host-ptr lookup failed; deferring \
                                     watchpoint publish"
                                );
                            }
                        }
                    }
                    // Lazy-resolve the per-CPU runnable_at scan
                    // context once `owned_accessor` lands and the
                    // bootstrap symbols are readable. Skipped entirely
                    // when dual_snapshot is off; failed prerequisites
                    // (missing jiffies_64 symbol, BTF without
                    // sched_ext_entity, etc.) leave `scan_ctx` None
                    // and the early-trigger path stays dormant for
                    // the rest of the run — the late path still works.
                    //
                    // Each failed prerequisite emits a per-iteration
                    // `tracing::debug!` line under the
                    // `RUST_LOG=ktstr=debug` filter — the
                    // DualFailureDumpReport's absent-early Display
                    // message points operators here. Per-iteration
                    // (not single-shot) is the right cadence for
                    // debug output: an operator who asked for verbose
                    // logging wants to see the full retry pattern,
                    // not just one snapshot. The aggregate "something
                    // is wrong" signal stays at the warn level (see
                    // `scan_ctx_warned` below) so default-visible
                    // output still surfaces a single line per run.
                    if scan_tick && freeze_coord_dual_snapshot && scan_ctx.is_none() {
                        // try_resolve consumes the hoisted prereqs
                        // (scan_offsets, scan_jiffies_64_kva,
                        // dump_cpu_time_symbols).
                        // The only field that can flip Some after
                        // coord-start is owned_accessor (boot-race);
                        // every other input is a deterministic function
                        // of the host inputs and was already attempted
                        // at coord-start. A None among them means the
                        // dependency is permanently absent — the
                        // diagnostic warn already names which leg
                        // failed. The closure returns the reason
                        // string alongside None so the late-trigger
                        // skip-reason path can quote it directly into
                        // DualFailureDumpReport::early_skipped_reason.
                        let try_resolve = || -> Result<RunnableScanCtx, &'static str> {
                            let owned = owned_accessor
                                .as_ref()
                                .ok_or("owned_accessor not ready (guest still booting)")?;
                            let scan_offsets = scan_offsets
                                .ok_or("RunnableScanOffsets unavailable (BTF lacks sched_ext_entity)")?;
                            let jiffies_64_kva = scan_jiffies_64_kva
                                .ok_or("jiffies_64 symbol absent from vmlinux")?;
                            let syms = dump_cpu_time_symbols
                                .as_ref()
                                .ok_or("KernelSymbols unavailable (vmlinux parse failed)")?;
                            // The global `scx_tasks` LIST_HEAD is the
                            // walker's only memory anchor. Absent on a
                            // stripped vmlinux or a kernel without
                            // sched_ext — fail the resolve so the
                            // late-trigger skip-reason path quotes the
                            // missing symbol.
                            let scx_tasks_kva = syms.scx_tasks.ok_or(
                                "scx_tasks symbol absent from vmlinux \
                                 (kernel without sched_ext or stripped vmlinux)",
                            )?;
                            let mem = freeze_coord_mem
                                .as_ref()
                                .ok_or("GuestMem unavailable")?;
                            let kernel = owned.guest_kernel();
                            let walk = kernel.walk_context();
                            // Translate jiffies_64's KVA to a PA.
                            // Lives in the kernel text/data mapping —
                            // same as scx_root et al. Use the
                            // GuestKernel-resident base so VA_BITS=47
                            // hosts translate correctly.
                            let jiffies_64_pa = kernel.text_kva_to_pa(jiffies_64_kva);
                            // Compute per-CPU rq PAs for the per-rq
                            // runnable_list walker. The KernelOffsets
                            // schema guarantees `runqueues != 0` (see
                            // `monitor/symbols.rs` — its absence is a
                            // construction-time error), so the only
                            // failure path here is reading
                            // `__per_cpu_offset` early during boot:
                            // the per-CPU offset table reads as zero
                            // for not-yet-online CPUs. A zero offset
                            // does NOT yield a zero PA — `compute_rq_pas`
                            // wraps via `wrapping_sub` into the
                            // upper-half KVA region (see
                            // `compute_rq_pas` doc comment in
                            // `monitor/symbols.rs`), so the resulting
                            // PA is bogus, not zero, and there is no
                            // downstream `rq_pa == 0` short-circuit
                            // to suppress it. Caching such a vec is
                            // permanent for the run and would have
                            // every subsequent walk read garbage for
                            // the not-yet-online slots. Mirror the
                            // `prog_per_cpu_offsets` gate above:
                            // defer scan_ctx construction until every
                            // offset slot is non-zero. A retry is
                            // cheap; a cached miss is permanent.
                            let pco_pa = kernel.text_kva_to_pa(syms.per_cpu_offset);
                            let pco_offsets = crate::monitor::symbols::read_per_cpu_offsets(
                                mem,
                                pco_pa,
                                freeze_coord_num_cpus,
                            );
                            if pco_offsets.contains(&0) {
                                return Err(
                                    "not all per_cpu_offsets resolved \
                                     (some CPUs still booting)",
                                );
                            }
                            // The coordinator's scan_ctx path uses
                            // the accessor's own phys_base (from
                            // page-table walk). TODO: derive
                            // kaslr_offset for the coordinator too.
                            let rq_pas = crate::monitor::symbols::compute_rq_pas(
                                syms.runqueues,
                                &pco_offsets,
                                walk.page_offset,
                                syms.per_cpu_start,
                                0,
                            );
                            // scx_watchdog_timestamp is a `.data`
                            // file-scope static — same text-mapping
                            // translation as scx_watchdog_timeout
                            // (which lives a few lines below the
                            // timestamp in kernel/sched/ext.c).
                            // Optional because the symbol is absent
                            // on kernels without sched_ext or
                            // stripped vmlinux; max_runnable_age
                            // skips the contribution when None.
                            let watchdog_timestamp_pa =
                                syms.scx_watchdog_timestamp.map(|kva| kernel.text_kva_to_pa(kva));
                            Ok(RunnableScanCtx {
                                scx_tasks_kva,
                                rq_pas,
                                offsets: scan_offsets,
                                jiffies_64_pa,
                                watchdog_timestamp_pa,
                                walk,
                                start_kernel_map: kernel.start_kernel_map(),
                                phys_base: kernel.phys_base(),
                            })
                        };
                        match try_resolve() {
                            Ok(ctx) => {
                                scan_ctx = Some(ctx);
                                scan_ctx_skip_reason = None;
                            }
                            Err(reason) => {
                                scan_ctx_skip_reason = Some(reason);
                            }
                        }
                    }
                    // Single-shot warn when the resolve has been
                    // failing long enough that "still booting" is no
                    // longer a plausible explanation. Without this
                    // an operator running ktstr against a kernel that
                    // lacks `sched_ext_entity` BTF (sched_ext disabled)
                    // or `jiffies_64` (stripped vmlinux) gets the
                    // dual-snapshot path silently disabled; the late
                    // dump still works, but the early snapshot would
                    // never fire and the missing wrapper could be
                    // mistaken for "stall fired before half-way
                    // threshold". Counting iterations under the
                    // dual-snapshot gate ensures the message only
                    // surfaces in runs where the path was requested.
                    if scan_tick && freeze_coord_dual_snapshot && scan_ctx.is_none() {
                        scan_ctx_retries += 1;
                        if !scan_ctx_warned && scan_ctx_retries >= SCAN_CTX_WARN_AFTER_ITERS {
                            tracing::warn!(
                                "freeze-coord: runnable_at scan prerequisites unavailable \
                                 (most commonly: guest still booting; or BTF lacks \
                                 sched_ext_entity, jiffies_64 symbol missing) — \
                                 early-trigger path delayed — will continue retrying"
                            );
                            scan_ctx_warned = true;
                        }
                    }
                    // Poll for the late-trigger condition. The
                    // hardware watchpoint on `*scx_root->exit_kind`
                    // is the primary path: every vCPU thread sets
                    // `freeze_coord_watchpoint.hit` (Release) on
                    // `KVM_EXIT_DEBUG`, which the Acquire load here
                    // observes synchronously — no 100 ms polling
                    // window. The BPF .bss `cached_bss_pa` read
                    // (gated through [`bss_read_state`] for
                    // PA-validity vs not-fired distinction) is
                    // checked alongside the watchpoint every
                    // iteration: it remains a useful redundancy on
                    // kernels where the watchpoint armed (the
                    // typed three-way result also catches a stale
                    // cached PA that bare `read_u32` would mask as
                    // "no fire") AND a fallback for kernels where
                    // the watchpoint never armed (no `scx_root`
                    // symbol, BTF stripped of `scx_sched`, or
                    // `KVM_SET_GUEST_DEBUG` rejected by the host).
                    //
                    // Once `freeze_state == Done` the late-trigger
                    // dispatch has already taken its terminal
                    // transition — re-evaluating
                    // `compute_err_triggered(...)` is wasted work
                    // for the rest of the run (sticky bss latch
                    // keeps reporting Triggered, sticky watchpoint
                    // hit keeps reporting true). Skip the read
                    // entirely once the state machine has closed.
                    let (watchpoint_hit, bss_state) =
                        if freeze_state == FreezeState::Done {
                            (false, BssReadState::NotResolved)
                        } else {
                            let wp =
                                freeze_coord_watchpoint.hit.load(Ordering::Acquire);
                            let st = bss_read_state(
                                freeze_coord_mem.as_deref(),
                                cached_bss_pa,
                            );
                            // OnlyTriggered counts as "fire";
                            // OutOfBounds and NotResolved /
                            // NotTriggered all mean "no
                            // observable fire this iteration".
                            // Surfacing OOB once with a warn lets
                            // an operator notice when the .bss
                            // path has gone stale without
                            // changing the trigger arithmetic.
                            if matches!(st, BssReadState::OutOfBounds)
                                && !bss_oob_warn_logged
                            {
                                tracing::warn!(
                                    cached_bss_pa =
                                        cached_bss_pa
                                            .map(|p| format!("{p:#x}"))
                                            .unwrap_or_else(|| "None".to_string()),
                                    "freeze-coord: cached BPF .bss PA no \
                                     longer resolves to a 4-byte-readable \
                                     DRAM region — probe map likely freed \
                                     mid-run; .bss late-trigger fallback is \
                                     now silent for the rest of the run \
                                     (watchpoint path, if armed, remains \
                                     active)"
                                );
                                bss_oob_warn_logged = true;
                            }
                            (wp, st)
                        };
                    let mut err_triggered =
                        compute_err_triggered(watchpoint_hit, bss_state);
                    if !err_triggered
                        && scan_tick
                        && freeze_state != FreezeState::Done
                        && let Some(ek_pa) = cached_exit_kind_pa
                        && let Some(ref mem) = freeze_coord_mem
                    {
                        let kind = mem.read_u32(ek_pa, 0);
                        const SCX_EXIT_ERROR: u32 = 1024;
                        if kind >= SCX_EXIT_ERROR {
                            err_triggered = true;
                        }
                    }
                    if !err_triggered
                        && (bsp_done_final_pass || sched_exit_final_pass)
                        && freeze_state != FreezeState::Done
                        && let (Some(owned), Some(mem)) =
                            (owned_accessor.as_ref(), freeze_coord_mem.as_deref())
                        {
                            let kernel = owned.guest_kernel();
                            let walk = kernel.walk_context();
                            let mut ek_kva = freeze_coord_watchpoint
                                .request_kva
                                .load(Ordering::Acquire);
                            if ek_kva == 0
                                && let Some(syms) = dump_cpu_time_symbols.as_ref()
                                    && let Some(root_kva) = syms.scx_root
                                    && let Some(ref offs) = dump_scx_walker_offsets
                                    && let Some(ref so) = offs.sched
                                {
                                    let root_pa = kernel.text_kva_to_pa(root_kva);
                                    let sched_kva = mem.read_u64(root_pa, 0);
                                    if sched_kva != 0 {
                                        ek_kva = sched_kva + so.exit_kind as u64;
                                    }
                                }
                            if ek_kva != 0
                                && let Some(pa) = crate::monitor::idr::translate_any_kva(
                                    mem,
                                    walk.cr3_pa,
                                    walk.page_offset,
                                    ek_kva,
                                    walk.l5,
                                    walk.tcr_el1,
                                ) {
                                    let kind = mem.read_u32(pa, 0);
                                    const SCX_EXIT_ERROR: u32 = 1024;
                                    if kind >= SCX_EXIT_ERROR {
                                        err_triggered = true;
                                    }
                                }
                        }
                    // Closures capture by reference. Building the
                    // full freeze-rendezvous-dump cycle once and
                    // calling it for either the early or late
                    // snapshot keeps the drain-dance contract
                    // (immediate_exit pass 1 → release fence →
                    // signal pass 2 → N-of-N rendezvous) defined in
                    // exactly one place. Returns
                    // `Some(FailureDumpReport)` when the rendezvous
                    // succeeded; None on timeout (the surrounding
                    // logic still thaws). The thaw is the caller's
                    // responsibility so the same closure works for
                    // a state-resetting late freeze (thaw to allow
                    // teardown to run) and a transient early freeze
                    // (thaw to let the test continue).
                    // `gate_on_exit_kind` filters out spurious watchpoint
                    // fires on a non-error `exit_kind` value. The
                    // hardware watchpoint catches every write to
                    // `*scx_root->exit_kind` regardless of value —
                    // including transient writes during init/teardown
                    // that the kernel sets to `SCX_EXIT_NONE` (0) or
                    // `SCX_EXIT_DONE` (1). Without the gate, every
                    // clean scheduler shutdown would synthesize a
                    // bogus failure dump. The gate runs AFTER the
                    // rendezvous succeeds (vCPUs parked → guest
                    // memory consistent) and BEFORE building the
                    // dump: read the 4-byte `exit_kind` value at the
                    // already-resolved KVA, compare against the
                    // error-class boundary `SCX_EXIT_ERROR = 1024`
                    // (per `kernel/sched/ext_internal.h::scx_exit_kind`).
                    // Gate failures return None — the late-trigger
                    // call site treats this as "spurious watchpoint
                    // fire, reset hit and keep watching" rather than
                    // the normal "rendezvous timed out, give up"
                    // semantics. The early (runnable_at) trigger and
                    // BPF-bss late trigger pass `false`: those paths
                    // are already gated on their own conditions
                    // (half-way age threshold; tp_btf handler latch
                    // on error-class kinds), so an extra exit_kind
                    // read would be redundant overhead.
                    let freeze_and_capture =
                        |gate_on_exit_kind: bool|
                            -> LateCaptureOutcome {
                            let skip_freeze =
                                freeze_coord_bsp_done.load(Ordering::Acquire);
                            if skip_freeze {
                                tracing::info!(
                                    gate_on_exit_kind,
                                    "freeze-coord: BSP exited, capturing \
                                     quiesced guest memory without freeze"
                                );
                            } else {
                                tracing::info!(
                                    gate_on_exit_kind,
                                    "freeze-coord: freezing vCPUs for snapshot"
                                );
                            }
                            // Capture wall-clock start for the
                            // post-dump timing summary one-liner.
                            // Returned alongside the report so the
                            // call site can reuse it across the
                            // post-thaw JSON emit (covers freeze
                            // rendezvous → dump_state → numa-stats →
                            // serialise → file-write window with one
                            // anchor).
                            let capture_start = Instant::now();
                            // Soft deadline for the whole capture path
                            // (rendezvous + dump_state + numa stats).
                            // Set to half the configured watchdog so a
                            // slow dump can't keep vCPUs parked past
                            // the kernel's own SCX_EXIT_ERROR_STALL
                            // emission line. `freeze_coord_watchdog_half`
                            // already encodes the divide-by-2 (see
                            // its definition above) and falls back to
                            // 2s when the builder didn't set
                            // watchdog_timeout. Using it here couples
                            // the dump bailout to the same horizon
                            // the per-CPU runnable_at scanner uses
                            // for the dual-snapshot half-way trigger.
                            let capture_deadline = if freeze_coord_watchdog_half
                                > Duration::ZERO
                            {
                                Some(capture_start + freeze_coord_watchdog_half)
                            } else {
                                None
                            };
                            // 'capture labeled block: every exit
                            // from the freeze→park→dump phases
                            // (rendezvous timeout, gate-suppressed,
                            // full dump, partial dump) `break
                            // 'capture <result>` so all paths
                            // converge on the labeled block's value
                            // — which is the closure's return.
                            // The caller is responsible for invoking
                            // `thaw_and_barrier` AFTER it has done
                            // any while-frozen work it needs (the
                            // late-trigger backstop reads guest
                            // memory while quiesced, so the thaw
                            // cannot be unconditional inside the
                            // closure).
                            'capture: {
                            // Cycle-entry snapshot of BSP liveness
                            // used for non-UAF-sensitive bookkeeping:
                            // parked_evt pre-seed gating
                            // (`bsp_parked` lookup), `expected_parks`
                            // accounting (+1 for BSP), pass-2
                            // `pthread_kill`, and the rendezvous-wait
                            // diagnostics. None of those callsites
                            // dereference the BSP's `kvm_run` mmap, so
                            // a stale `true` is benign:
                            // `pthread_kill` against an exited tid
                            // returns ESRCH, an over-counted
                            // `expected_parks` heals on the next
                            // SIGRTMIN/park-ack overshoot path, and
                            // pre-seed reads only the AtomicBool
                            // `bsp_parked` flag.
                            //
                            // The TOCTOU-sensitive
                            // `ImmediateExitHandle::set(1)` against
                            // the BSP's `kvm_run` mmap is gated by
                            // its own fresh Acquire load further
                            // below — see the "Re-load `bsp_alive`
                            // immediately before the BSP `ie.set()`"
                            // comment for the full rationale (a stale
                            // snapshot there would write through a
                            // pointer into freed `kvm_run` pages
                            // after the BSP drops its `VcpuFd`).
                            //
                            // The primary line of defense remains
                            // `freeze_coord_handle.join()` in run_vm
                            // BEFORE the BSP `VcpuFd` falls out of
                            // scope; the in-closure loads are
                            // defense-in-depth.
                            let bsp_alive_at_start =
                                bsp_alive_for_coord.load(Ordering::Acquire);
                            // Drain `parked_evt` BEFORE flipping
                            // `freeze=true` and BEFORE issuing
                            // pass-0 (worker pause), pass-1
                            // (immediate_exit), or pass-2 (SIGRTMIN).
                            // From this point forward every
                            // increment to the parked_evt counter
                            // is unambiguously a park-ack for THIS
                            // cycle. Draining AFTER the kicks is a
                            // race: a fast vCPU or worker may park
                            // and bump the counter between the kick
                            // and the drain — that ack is then
                            // absorbed by the drain instead of
                            // counted toward `parked_count`, and
                            // the rendezvous waits 30 s for an ack
                            // that already fired.
                            //
                            // EAGAIN under EFD_NONBLOCK (counter
                            // already 0 from the prior cycle's
                            // post-thaw barrier drain) is benign.
                            //
                            // The Acquire ordering synchronizes-with
                            // the parker's Release store after its
                            // drain dance — this rendezvous IS the
                            // memory barrier that makes the future
                            // host-side guest-memory reads correct.
                            // The eventfd write ordering is
                            // load-bearing: the AtomicBool Release
                            // happens-before the eventfd write, so
                            // every counter increment we observe in
                            // the loop below implies every
                            // guest-side queue mutation the parker
                            // performed pre-park is visible to the
                            // dump.
                            //
                            // Also drain `thaw_evt` here. The
                            // coordinator writes thaw_evt ONCE per
                            // thaw, and every parked vCPU polls the
                            // SAME fd in `handle_freeze` without
                            // draining (the multi-reader fan-out
                            // wake design pinned at
                            // `vmm/exit_dispatch.rs::handle_freeze`).
                            // Without a per-cycle drain by the
                            // coordinator the counter is monotonic
                            // — every successive freeze cycle sees
                            // a level-high thaw_evt left over from
                            // the previous cycle, which makes
                            // `handle_freeze`'s poll return
                            // immediately on every iteration and
                            // burns CPU spinning on
                            // `freeze.load(Acquire)` until the
                            // coordinator clears `freeze`. Draining
                            // before pass-1 / pass-2 means the next
                            // poll inside `handle_freeze` blocks on
                            // an empty counter and wakes only when
                            // (a) the coordinator's post-rendezvous
                            // `thaw_evt.write(1)` lands or (b) the
                            // 100 ms poll backstop fires.
                            use std::os::fd::AsRawFd;
                            let _ = freeze_coord_parked_evt.read();
                            let _ = freeze_coord_thaw_evt.read();
                            // Reset every per-vCPU regs slot to None
                            // before the freeze kicks. Without this
                            // clear, regs slots written during a
                            // PRIOR successful capture (periodic /
                            // user-watchpoint / late-trigger) stay
                            // `Some(stale_snapshot)` across cycles —
                            // and a vCPU that DOES NOT park this
                            // cycle (the rendezvous-timeout case the
                            // Degraded report is meant to surface)
                            // shows up as `Some(stale)` instead of
                            // the contracted `None`. That violates
                            // the
                            // [`crate::monitor::dump::DegradedFailureDumpReport::vcpu_regs`]
                            // doc invariant ("None identifies vCPUs
                            // that never parked") and silently
                            // misleads the operator about which
                            // vCPUs stalled. The Mutex acquisitions
                            // are cheap (single-digit microseconds);
                            // the writer is the vCPU's SIGRTMIN
                            // handler which only runs AFTER the
                            // freeze kicks below, so this clear
                            // races nothing.
                            *freeze_coord_bsp_regs
                                .lock()
                                .unwrap_or_else(|e| e.into_inner()) = None;
                            for ap in &freeze_coord_ap_regs {
                                *ap.lock().unwrap_or_else(|e| e.into_inner()) = None;
                            }
                            // Snapshot virtio-blk worker liveness
                            // BEFORE pause(). When the device exists
                            // but the worker thread is not yet
                            // spawned (pre-DRIVER_OK) or has been
                            // joined (post-stop / failed-respawn),
                            // pause() short-circuits with the
                            // "no-live-worker" fast path and writes
                            // no parked_evt ack — counting +1 in
                            // that case makes the rendezvous wait
                            // 30 s for a worker that does not
                            // exist. The pre-pause `paused` flag
                            // is the cheapest available proxy: the
                            // worker spawn flips it to false on
                            // entry to the run loop and the
                            // post-thaw barrier guarantees a live
                            // worker has cleared it before this
                            // cycle starts. `paused == true` at
                            // cycle entry therefore means "no live
                            // worker" (the construction sentinel
                            // or post-stop re-armed sentinel from
                            // `resume()`). Gate the +1 below on
                            // `worker_was_running` instead of bare
                            // `is_some()`.
                            let worker_was_running = freeze_coord_virtio_blk_paused
                                .as_ref()
                                .is_some_and(|p| !p.load(Ordering::Acquire));
                            // Pre-seed the parked_evt counter for any
                            // parker whose flag is STILL `true` at
                            // cycle entry. The post-thaw barrier at
                            // the end of every prior cycle SHOULD
                            // have observed every parker clear its
                            // flag before returning, but the barrier
                            // can hit its FREEZE_RENDEZVOUS_TIMEOUT
                            // and break early (logged as
                            // "post-thaw barrier timed out — a parker
                            // did not clear within
                            // FREEZE_RENDEZVOUS_TIMEOUT" further
                            // below). When that happens the parker is
                            // still inside `handle_freeze`'s park
                            // loop with `parked=true`. The next cycle
                            // sets `freeze=true` and SIGRTMINs every
                            // vCPU; the kicked vCPU's poll wakes
                            // (EINTR), re-checks `freeze` (now true),
                            // and stays in the SAME `handle_freeze`
                            // invocation — `parked.store(true)` plus
                            // `parked_evt.write(1)` only run on
                            // ENTRY to `handle_freeze` (the
                            // freeze-park ack path in
                            // `crate::vmm::exit_dispatch`), NOT
                            // per `freeze=true` flip while parked.
                            // Without pre-seeding, the rendezvous
                            // countdown latch never receives an ack
                            // from that parker and waits the full
                            // 30 s for an event that already happened
                            // a cycle ago.
                            //
                            // Pre-seeding +1 to `parked_evt` per
                            // still-parked parker compensates: the
                            // rendezvous loop drains the counter
                            // and credits each as a park-ack for
                            // THIS cycle. This is equivalent to the
                            // historical force-clear of `parked`
                            // flags but targeted — only fires for
                            // the timed-out subset, leaving a
                            // healthy mid-thaw parker (which still
                            // has its `parked=true` from the prior
                            // cycle and is about to clear it within
                            // a few ms) untouched. The worst case
                            // is a healthy parker that races the
                            // pre-seed: its own
                            // `parked_evt.write(1)` on the next
                            // entry to `handle_freeze` adds another
                            // count, which is harmless — the
                            // rendezvous loop only checks
                            // `parked_count >= expected_parks` and
                            // overshoot is fine.
                            //
                            // The bsp/ap loads here are Acquire to
                            // synchronise-with the prior cycle's
                            // post-thaw barrier reads — the post-
                            // thaw barrier already loaded these
                            // with Acquire, but a healthy parker may
                            // have flipped its flag back to false
                            // between the barrier's last load and
                            // this point. The seed only fires when
                            // we still observe `true`, so a healthy
                            // late-clear is a no-op.
                            let mut still_parked: u32 = 0;
                            for ap in freeze_coord_ap_parked.iter() {
                                if ap.load(Ordering::Acquire) {
                                    still_parked = still_parked.saturating_add(1);
                                }
                            }
                            if bsp_alive_at_start
                                && freeze_coord_bsp_parked.load(Ordering::Acquire)
                            {
                                still_parked = still_parked.saturating_add(1);
                            }
                            // virtio-blk worker: only pre-seed when
                            // the worker was running (otherwise
                            // pause() short-circuits and we won't
                            // count +1 anyway). If the worker is
                            // running AND `paused == true`, the
                            // worker is mid-park from the prior
                            // cycle and won't re-write its ack on
                            // the next pause()-driven epoll wake,
                            // mirroring the vCPU case. Pause-fd
                            // writes happen-before `paused.store
                            // (true)`, which happens-before the
                            // worker's `parked_evt.write(1)` —
                            // matching the vCPU sequence in
                            // `handle_freeze`.
                            if worker_was_running
                                && freeze_coord_virtio_blk_paused
                                    .as_ref()
                                    .is_some_and(|p| p.load(Ordering::Acquire))
                            {
                                still_parked = still_parked.saturating_add(1);
                            }
                            if still_parked > 0 {
                                tracing::warn!(
                                    still_parked,
                                    ?rendezvous_timeout,
                                    "freeze-coord: detected stale parked=true \
                                     parker(s) at cycle entry — prior post-thaw \
                                     barrier likely timed out. Pre-seeding \
                                     parked_evt to credit them as acks for this \
                                     cycle so the rendezvous does not wait the \
                                     full rendezvous timeout for events that \
                                     already fired."
                                );
                                if let Err(e) =
                                    freeze_coord_parked_evt.write(still_parked as u64)
                                {
                                    tracing::warn!(
                                        err = %e,
                                        still_parked,
                                        ?rendezvous_timeout,
                                        "freeze-coord: parked_evt pre-seed write \
                                         failed; rendezvous may wait the full \
                                         rendezvous timeout for stale parker(s)"
                                    );
                                }
                            }
                            freeze_coord_freeze.store(true, Ordering::Release);
                            // No force-clear of `parked` flags here.
                            // The post-thaw barrier at the END of
                            // every prior freeze_and_capture cycle
                            // (see `// Post-thaw barrier` below)
                            // is the primary guarantee that every
                            // vCPU has run its trailing
                            // `parked.store(false)` before this
                            // cycle starts. Force-clearing
                            // mid-cycle would erase the legitimate
                            // `parked=true` of a vCPU still in cycle
                            // N's park loop and deadlock the
                            // rendezvous (vCPU never re-stores
                            // parked=true; coord waits 30 s). The
                            // pre-seed above handles the residual
                            // case where the post-thaw barrier
                            // itself timed out.
                            // Pass 0: signal every device worker to
                            // pause. virtio-blk has an independent
                            // worker thread that must be parked
                            // before we read guest memory — otherwise
                            // it can race-mutate the avail/used rings
                            // and the backing file mid-dump,
                            // producing a torn view of in-flight
                            // requests. Other devices (virtio-net,
                            // virtio-console) run on the vCPU thread
                            // and freeze automatically at the vCPU
                            // rendezvous below.
                            //
                            // The worker may be in `pread`/`pwrite`
                            // when this lands; the eventfd write
                            // returns immediately (counter mode +
                            // EFD_NONBLOCK) and the syscall completes
                            // before the worker reaches the next
                            // `epoll_wait` and observes PAUSE_TOKEN.
                            // The rendezvous loop below polls each
                            // worker's `paused` flag with the same
                            // FREEZE_RENDEZVOUS_TIMEOUT budget that
                            // bounds the vCPU wait — workers ack
                            // within ~1 ms in healthy state and the
                            // 30 s ceiling absorbs sick-system stalls.
                            if let Some(ref blk) = freeze_coord_virtio_blk {
                                blk.lock().pause();
                            }
                            // Pass 1: set every immediate_exit=1.
                            // Each ImmediateExitHandle::set is a
                            // single-byte write_volatile into the
                            // corresponding kvm_run mmap (MAP_SHARED,
                            // lifetime tied to the running VcpuFd
                            // that owns it).
                            //
                            // Primary defense for the AP path: the
                            // AP threads are joined in
                            // `collect_results` AFTER the coord
                            // joins, so in the normal lifecycle the
                            // coord cannot outlive an AP's `VcpuFd`.
                            // The exception is panic-unwind under
                            // `panic = "unwind"` (test profile),
                            // where the AP's panic hook fires
                            // synchronously on the panicking thread
                            // and the subsequent stack drop unmaps
                            // the AP's `kvm_run` page mid-cycle —
                            // before any join. Without a per-AP
                            // gate the unguarded `ie.set(1)` above
                            // would `write_volatile` through a
                            // pointer into freed memory.
                            //
                            // Secondary defense: each AP carries an
                            // `Arc<AtomicBool>` (`VcpuThread::alive`)
                            // that the AP's panic hook flips to
                            // `false` BEFORE unwinding starts.
                            // The Acquire load below
                            // synchronizes-with that Release store
                            // (panic hook runs synchronously on the
                            // panicking AP thread before unwind),
                            // so a `true` reading observed here
                            // happens-before any subsequent unwind
                            // drop of `vcpu`. Mirrors the
                            // BSP-side `bsp_alive` TOCTOU-tightened
                            // gate: load fresh at the actual
                            // `ie.set` site, not at cycle entry.
                            // `iter().enumerate()` walks index
                            // alongside the handle so the
                            // `freeze_coord_ap_alive[i]` lookup
                            // stays index-aligned.
                            //
                            // The BSP IE write is gated on
                            // `bsp_alive` because run_vm drops the
                            // BSP before collect_results runs; see
                            // the gate's doc above.
                            for (i, ie) in freeze_coord_ap_ies.iter().enumerate() {
                                if let Some(ie) = ie
                                    && freeze_coord_ap_alive[i]
                                        .load(Ordering::Acquire)
                                {
                                    ie.set(1);
                                }
                            }
                            // Re-load `bsp_alive` immediately before the
                            // BSP `ie.set()` instead of reusing the
                            // cycle-entry snapshot (`bsp_alive_at_start`).
                            // The snapshot is captured at the top of
                            // 'capture and is many milliseconds stale by
                            // the time pass-1 runs (worker pause()+ack,
                            // parked_evt pre-seed, the freeze=true
                            // Release store, and the virtio-blk
                            // pause()-rendezvous all happen in between).
                            // The BSP run-loop can transition
                            // `bsp_alive=false` and drop its `VcpuFd` at
                            // any point in that window. Without a
                            // fresh load, `ImmediateExitHandle::set(1)`
                            // would issue a `write_volatile` through a
                            // pointer into a `kvm_run` mmap whose
                            // backing pages were unmapped when the BSP
                            // `VcpuFd` was dropped (the kernel's
                            // `kvm_vcpu_release` path tears down the
                            // `kvm_run` MAP_SHARED region; subsequent
                            // userspace writes against the stale
                            // pointer are use-after-free into freed
                            // pages). The Acquire load pairs with the
                            // BSP run-loop's Release store of `false`
                            // on its way out: a `bsp_alive_now == true`
                            // observed here happens-before any
                            // `false` the BSP could subsequently
                            // store, which means the BSP `VcpuFd` is
                            // still alive AT the moment of `ie.set()`
                            // and cannot be dropped until the next
                            // load reads false. Pass-2's pthread_kill
                            // and the rendezvous-wait below issue
                            // their own fresh Acquire loads for the
                            // same TOCTOU reason.
                            let bsp_alive_for_ie =
                                bsp_alive_for_coord.load(Ordering::Acquire);
                            if bsp_alive_for_ie
                                && let Some(ref ie) = freeze_coord_bsp_ie_handle
                            {
                                ie.set(1);
                            }
                            // Release fence between pass 1 and pass 2
                            // so all immediate_exit writes are
                            // observable before any vCPU thread
                            // receives the kick signal — without
                            // this, a thread could process its signal,
                            // enter KVM_RUN, and miss the
                            // immediate_exit byte that is supposed to
                            // short-circuit guest entry.
                            std::sync::atomic::fence(Ordering::Release);
                            // Pass 2: signal every vCPU. AP signals
                            // are always safe; the BSP signal is
                            // gated on `bsp_alive_at_start` — the
                            // cycle-entry snapshot — rather than a
                            // fresh load. `pthread_kill` against an
                            // exited tid returns ESRCH and is
                            // harmless either way: a stale `true`
                            // here just adds one ESRCH-suppressing
                            // log line; a stale `false` is fine
                            // because the BSP transitioned dead
                            // between entry and now, so it neither
                            // needs nor can receive the kick. Unlike
                            // `ImmediateExitHandle::set(1)` above,
                            // `pthread_kill` does not dereference
                            // any per-`VcpuFd`-owned mmap, so there
                            // is no use-after-free hazard requiring
                            // a re-load.
                            for &tid in &freeze_coord_ap_pthreads {
                                unsafe {
                                    libc::pthread_kill(tid, vcpu_signal());
                                }
                            }
                            if bsp_alive_at_start {
                                unsafe {
                                    libc::pthread_kill(freeze_coord_bsp_tid, vcpu_signal());
                                }
                            }
                            // Wait for N-of-N parked acks via a
                            // countdown latch over `parked_evt`. The
                            // counter-mode eventfd accumulates one
                            // write per parker (every vCPU + the
                            // virtio-blk worker writes 1 AFTER its
                            // own Release store on parked/paused).
                            // Each `read()` drains the accumulated
                            // count atomically and resets it; the
                            // closure tallies these drains until the
                            // total reaches `expected`. Replaces the
                            // per-iteration O(N) AtomicBool scan with
                            // an O(1) counter add — the AtomicBool
                            // flags remain the synchronizes-with
                            // anchor for the diagnostic timeout-log
                            // path below, but they are no longer the
                            // hot-path readiness check.
                            //
                            // The pre-pass drain above (before the
                            // `freeze=true` flip and the kicks)
                            // ensures every increment we observe
                            // from here on is a park-ack for THIS
                            // cycle, not an ack from cycle N-1
                            // that arrived after the post-thaw
                            // barrier's drain.
                            //
                            // The +1 for virtio-blk is gated on
                            // `worker_was_running` — when the
                            // worker thread is not alive, pause()
                            // is a no-op and writes no parked_evt
                            // ack, so counting +1 would make the
                            // rendezvous wait 30 s for an ack that
                            // never comes.
                            let mut expected_parks: u64 =
                                freeze_coord_ap_parked.len() as u64
                                    + if bsp_alive_at_start { 1 } else { 0 }
                                    + if worker_was_running { 1 } else { 0 };
                            let deadline = Instant::now() + rendezvous_timeout;
                            // Sub-deadline for the virtio-blk worker
                            // ack. `device.rs::stop_worker_and_reclaim_state`
                            // (and any sibling shutdown path) writes
                            // `paused.store(false, Release)` BEFORE
                            // signalling stop_fd and joining the
                            // worker — see lines around device.rs
                            // 3561 (`self.paused.store(false,
                            // Ordering::Release)` + `signal_worker_stop`).
                            // Between that store and the worker
                            // exiting (with no further `paused=true`
                            // store on the shutdown path), the
                            // freeze-coord pre-pause snapshot here
                            // observes `paused == false` and counts
                            // `worker_was_running = true → +1` —
                            // but no live thread will write
                            // `parked_evt` for this cycle. Without a
                            // sub-deadline the rendezvous waits the
                            // full 30 s for an ack the worker
                            // physically cannot send.
                            //
                            // 1 s budget covers a healthy worker's
                            // `pread`/`pwrite` drain on warm page
                            // cache (the same envelope
                            // `DROP_JOIN_TIMEOUT` (1 s) commits to
                            // for the worker join in
                            // `device.rs`). If the worker hasn't
                            // parked within 1 s, it's likely
                            // mid-shutdown (signal_worker_stop
                            // pre-clears paused=false). Dropping
                            // the +1 avoids a 30 s timeout. A
                            // slow-but-alive worker mid-drain
                            // could still mutate ring state
                            // concurrently; this is accepted
                            // because tmpfs backing bounds drain
                            // time below the sub-timeout.
                            const WORKER_PARK_SUB_TIMEOUT: Duration =
                                Duration::from_secs(1);
                            let worker_sub_deadline =
                                Instant::now() + WORKER_PARK_SUB_TIMEOUT;
                            let mut worker_dropped: bool = false;
                            let mut parked_count: u64 = 0;
                            let mut all_parked = false;
                            // Snapshots WHICH loop-break condition fired
                            // so the post-loop Degraded reason can label
                            // accurately. A bare post-loop
                            // `freeze_coord_kill.load(Acquire)` reads the
                            // CURRENT state, not the cause of the break,
                            // and races a kill setter that lands in the
                            // ns window between the deadline-break and
                            // the post-loop read — mislabelling a true
                            // 30s timeout as a kill. Setting the flag
                            // inside the kill-break arm (and only there)
                            // captures the actual break cause.
                            let mut killed_during_rendezvous = false;
                            loop {
                                if freeze_coord_bsp_done.load(Ordering::Acquire) {
                                    break;
                                }
                                // Short-circuit on kill (e.g. SCHED_EXIT
                                // promoted kill mid-rendezvous). Without
                                // this, the loop burns the full
                                // FREEZE_RENDEZVOUS_TIMEOUT (~30s) before
                                // exiting via the deadline branch, then
                                // the Degraded path reports a misleading
                                // ~30s elapsed_ms when the actual cause
                                // was an external kill landing during the
                                // wait. Breaking here routes through the
                                // existing !all_parked branch — which
                                // re-checks bsp_done (the kill may have
                                // been promoted by the BSP exit) and
                                // emits Degraded with a meaningful
                                // elapsed_ms reflecting how long the
                                // rendezvous actually ran. The bss/
                                // watchpoint state in the Degraded
                                // report still surfaces whatever the
                                // trigger source observed before kill
                                // landed.
                                // Check parked-count BEFORE kill so a
                                // coalesced wake (kill_evt + the final
                                // parked_evt arriving in the same epoll
                                // batch) doesn't misattribute a
                                // successful rendezvous as a Degraded
                                // "rendezvous timed out" — the drain
                                // above can bump parked_count to
                                // expected_parks before the kill check
                                // runs, and we want to record the
                                // ground-truth Captured outcome rather
                                // than emit a misleading Degraded
                                // reason that says vCPUs didn't park
                                // when they did.
                                if parked_count >= expected_parks {
                                    all_parked = true;
                                    break;
                                }
                                // Kill check follows parked-count by
                                // design — see the "Check parked-count
                                // BEFORE kill" comment above for the
                                // coalesced-wake misattribution that
                                // motivates this ordering.
                                if freeze_coord_kill.load(Ordering::Acquire) {
                                    killed_during_rendezvous = true;
                                    break;
                                }
                                // Worker sub-timeout. Only fires
                                // when the worker was counted in
                                // `expected_parks` (i.e.
                                // `worker_was_running` was true at
                                // pre-pause snapshot) and we have
                                // not yet decremented for it. The
                                // condition `parked_count <
                                // expected_parks` plus the wall-
                                // clock check (`now >=
                                // worker_sub_deadline`) localises
                                // the bookkeeping change to the
                                // path where the worker really did
                                // not ack. The Acquire load on
                                // `paused` synchronises-with any
                                // worker `Release` it might still
                                // perform on a slow path; if the
                                // worker DID park we observe
                                // `paused == true` and DO NOT
                                // decrement (the matching ack will
                                // arrive imminently or has already
                                // arrived in `parked_count`).
                                if !worker_dropped
                                    && worker_was_running
                                    && Instant::now() >= worker_sub_deadline
                                    && freeze_coord_virtio_blk_paused
                                        .as_ref()
                                        .is_some_and(|p| !p.load(Ordering::Acquire))
                                {
                                    // Final paused re-check before
                                    // decrementing. The condition
                                    // above sampled paused==false,
                                    // but a slow-but-alive worker
                                    // could have transitioned
                                    // paused=true between that
                                    // sample and here. Re-loading
                                    // with Acquire pairs with the
                                    // worker's Release store on
                                    // pause(). If the worker DID
                                    // park, skip the drop and let
                                    // the next loop iteration
                                    // observe the matching
                                    // parked_evt ack — never
                                    // double-count by both dropping
                                    // expected_parks and absorbing
                                    // the eventfd write.
                                    if freeze_coord_virtio_blk_paused
                                        .as_ref()
                                        .is_some_and(|p| p.load(Ordering::Acquire))
                                    {
                                        continue;
                                    }
                                    tracing::warn!(
                                        worker_park_sub_timeout_ms =
                                            WORKER_PARK_SUB_TIMEOUT.as_millis() as u64,
                                        parked_count,
                                        expected_parks,
                                        ?rendezvous_timeout,
                                        "freeze-coord: virtio-blk worker did \
                                         not ack park within sub-timeout AND \
                                         `paused` is still false — most \
                                         likely the worker is mid-shutdown \
                                         (signal_worker_stop already cleared \
                                         paused=false on its way out), so no \
                                         live thread will write parked_evt \
                                         for this cycle. Dropping the +1 \
                                         from expected_parks so the \
                                         rendezvous proceeds without waiting \
                                         the full rendezvous timeout for an \
                                         ack that physically cannot arrive."
                                    );
                                    expected_parks =
                                        expected_parks.saturating_sub(1);
                                    worker_dropped = true;
                                    // Re-check the `all_parked`
                                    // predicate immediately so a
                                    // concurrent vCPU ack that just
                                    // pushed parked_count to the
                                    // (now lower) expected value is
                                    // recognised in this iteration
                                    // rather than after another
                                    // poll cycle.
                                    if parked_count >= expected_parks {
                                        all_parked = true;
                                        break;
                                    }
                                }
                                let now = Instant::now();
                                if now > deadline {
                                    // Diagnostic snapshot of every
                                    // parker's flag, computed once on
                                    // timeout for the error log. Hot
                                    // path no longer reads these
                                    // bools per iteration.
                                    let ap_states: Vec<bool> = freeze_coord_ap_parked
                                        .iter()
                                        .map(|p| p.load(Ordering::Acquire))
                                        .collect();
                                    let bsp_p = freeze_coord_bsp_parked.load(Ordering::Acquire);
                                    // Lock-free read via the
                                    // pre-acquired `paused_handle()`
                                    // Arc — avoids taking the device
                                    // mutex on the timeout-diagnostic
                                    // path. Acquire ordering pairs
                                    // with the worker's Release on
                                    // `paused.store(true)` so the
                                    // diagnostic sees a coherent
                                    // worker state.
                                    let blk_parked = freeze_coord_virtio_blk_paused
                                        .as_ref()
                                        .is_none_or(|p| p.load(Ordering::Acquire));
                                    tracing::error!(
                                        ?ap_states,
                                        bsp_parked = bsp_p,
                                        blk_parked,
                                        parked_count,
                                        expected_parks,
                                        "freeze-coord: timed out waiting for vCPUs / worker to park. \
                                         If blk_parked=false, the worker is most likely stuck in a \
                                         slow pread/pwrite against the backing file — verify the \
                                         backing is fast (tmpfs / warm page cache); the vCPU \
                                         thread's blocking budget is bounded by the freeze \
                                         rendezvous timeout, so a backing slow enough to push \
                                         per-request IO past that bound prevents the rendezvous \
                                         from completing. The worker observes PAUSE_TOKEN only \
                                         between blocking syscalls, so a long pread/pwrite delays \
                                         the park-ack until the syscall returns."
                                    );
                                    break;
                                }
                                let remaining_ms = (deadline - now)
                                    .as_millis()
                                    .min(i32::MAX as u128) as i32;
                                let mut pfds = [
                                    libc::pollfd {
                                        fd: freeze_coord_parked_evt.as_raw_fd(),
                                        events: libc::POLLIN,
                                        revents: 0,
                                    },
                                    libc::pollfd {
                                        fd: freeze_coord_kill_evt.as_raw_fd(),
                                        events: libc::POLLIN,
                                        revents: 0,
                                    },
                                    libc::pollfd {
                                        fd: freeze_coord_bsp_done_evt.as_raw_fd(),
                                        events: libc::POLLIN,
                                        revents: 0,
                                    },
                                ];
                                // SAFETY: pfds is a 3-element pollfd
                                // array; nfds matches. Every poll
                                // outcome (ready, timeout, EINTR,
                                // error) loops back to the
                                // countdown predicate at the top.
                                // EINTR from SIGRTMIN is harmless:
                                // the wait simply restarts.
                                unsafe {
                                    libc::poll(
                                        pfds.as_mut_ptr(),
                                        pfds.len() as libc::nfds_t,
                                        remaining_ms,
                                    );
                                }
                                // Drain parked_evt counter once per
                                // wake. Counter mode: a single read
                                // returns the accumulated count and
                                // resets to 0; multiple coalesced
                                // parker writes are absorbed in one
                                // drain. EAGAIN (counter already 0)
                                // is benign — the poll wake may have
                                // come from kill_evt or
                                // bsp_done_evt (those are NOT
                                // drained here; the outer epoll
                                // loop owns them). Saturating add
                                // is defensive — counter mode
                                // eventfd values cap at 2^64 - 2
                                // and physically cannot overflow
                                // a u64 in any realistic VM run.
                                if let Ok(n) = freeze_coord_parked_evt.read() {
                                    parked_count = parked_count.saturating_add(n);
                                }
                            }
                            // Collect per-vCPU register snapshots.
                            // Reads happens-after the rendezvous
                            // Acquire on each vCPU's `parked` flag,
                            // which synchronizes-with the vCPU
                            // thread's Release store after its
                            // capture_vcpu_regs / regs_slot write —
                            // so these Mutex reads see the captured
                            // values even on weakly-ordered
                            // architectures. Index 0 = BSP, 1..N =
                            // APs.
                            let collect_vcpu_regs = ||
                                -> Vec<Option<exit_dispatch::VcpuRegSnapshot>> {
                                let mut regs:
                                    Vec<Option<exit_dispatch::VcpuRegSnapshot>> =
                                    Vec::with_capacity(1 + freeze_coord_ap_regs.len());
                                regs.push(
                                    *freeze_coord_bsp_regs
                                        .lock()
                                        .unwrap_or_else(|e| e.into_inner()),
                                );
                                for ap in &freeze_coord_ap_regs {
                                    regs.push(
                                        *ap.lock().unwrap_or_else(|e| e.into_inner()),
                                    );
                                }
                                regs
                            };
                            if !all_parked {
                                // Re-check bsp_done AFTER the rendezvous
                                // loop exits. The loop has a
                                // `if freeze_coord_bsp_done.load(...) { break; }`
                                // guard near its top, so a clean BSP
                                // exit mid-rendezvous breaks out with
                                // `all_parked = false` even though the
                                // cause was NOT a rendezvous timeout.
                                // Without this re-check, the Degraded
                                // path below would misattribute the
                                // clean shutdown as
                                // "rendezvous timed out at <Nms>;
                                // 0/N vCPUs parked" — confusing to the
                                // operator because the run is
                                // terminating normally. The pre-cycle
                                // `skip_freeze` snapshot (taken at
                                // cycle entry) is stale for this case:
                                // bsp_done was false at entry but
                                // flipped during the wait.
                                let bsp_done_late =
                                    freeze_coord_bsp_done.load(Ordering::Acquire);
                                if skip_freeze || bsp_done_late {
                                    tracing::info!(
                                        skip_freeze,
                                        bsp_done_late,
                                        "freeze-coord: skip_freeze — vCPUs \
                                         exited, proceeding with dump on \
                                         quiesced memory"
                                    );
                                } else {
                                    // Per memory `feedback_no_silent_drops`:
                                    // the trigger fired (the watchpoint hit
                                    // and/or the BPF `.bss` latch flipped),
                                    // so the operator must see something —
                                    // a debug! log alone hides the
                                    // attempted-but-aborted state. Emit a
                                    // [`crate::monitor::dump::SCHEMA_DEGRADED`]
                                    // report carrying the partial
                                    // `vcpu_regs` (the regs slots for vCPUs
                                    // that DID park before the timeout,
                                    // `None` for the ones that stalled),
                                    // the live watchpoint + bss-latch
                                    // state, the `exit_kind` value when
                                    // the cached PA still resolves, and
                                    // the elapsed milliseconds the
                                    // coordinator spent trying.
                                    let vcpu_regs = collect_vcpu_regs();
                                    let watchpoint_hit = freeze_coord_watchpoint
                                        .hit
                                        .load(Ordering::Acquire);
                                    let bss_state = bss_read_state(
                                        freeze_coord_mem.as_deref(),
                                        cached_bss_pa,
                                    );
                                    let exit_kind = match (
                                        cached_exit_kind_pa,
                                        freeze_coord_mem.as_deref(),
                                    ) {
                                        (Some(pa), Some(mem)) => {
                                            Some(mem.read_u32(pa, 0))
                                        }
                                        _ => None,
                                    };
                                    let elapsed_ms =
                                        capture_start.elapsed().as_millis() as u64;
                                    let total = expected_parks;
                                    let stuck = total.saturating_sub(parked_count);
                                    // Distinguish the kill-during-rendezvous
                                    // case from a true 30s timeout via the
                                    // in-loop `killed_during_rendezvous`
                                    // snapshot declared before the loop.
                                    // A bare post-loop `freeze_coord_kill.
                                    // load(Acquire)` reads CURRENT state,
                                    // not the cause of the break — a kill
                                    // setter (SCHED_EXIT propagation,
                                    // watchdog hard-deadline expiry,
                                    // panic-hook) racing into the ns
                                    // window between the deadline-break
                                    // and the read mislabels a true 30s
                                    // timeout as a kill. The in-loop flag
                                    // captures the actual break cause:
                                    // set inside the kill-check arm
                                    // before the break, false otherwise.
                                    // The kill-aware path lands here when
                                    // kill flipped during the wait, with
                                    // elapsed_ms typically in single-
                                    // digit ms, where "timed out" would
                                    // be contradictory.
                                    let killed = killed_during_rendezvous;
                                    let reason_prefix = if killed {
                                        crate::monitor::dump::REASON_DEGRADED_KILL_DURING_RENDEZVOUS
                                    } else {
                                        crate::monitor::dump::REASON_DEGRADED_RENDEZVOUS_TIMEOUT
                                    };
                                    let reason = format!(
                                        "{reason_prefix}: {elapsed_ms}ms; {parked_count}/{total} vCPUs parked",
                                    );
                                    // Branch the warn body too so the
                                    // tracing-stream consumer reads the
                                    // same kill-vs-timeout disambiguation
                                    // as the on-disk Degraded JSON.
                                    // Without this, an operator scrolling
                                    // traces sees "rendezvous timed out"
                                    // with elapsed_ms=5ms on every kill
                                    // while the JSON correctly carries
                                    // the kill-aware reason — two
                                    // channels disagreeing on the same
                                    // event.
                                    let warn_summary = if killed {
                                        "freeze-coord: dump degraded — vCPU rendezvous aborted by external kill"
                                    } else {
                                        "freeze-coord: dump degraded — vCPU rendezvous timed out"
                                    };
                                    tracing::warn!(
                                        elapsed_ms,
                                        parked_count,
                                        expected_parks = total,
                                        stuck,
                                        killed,
                                        watchpoint_hit,
                                        bss_latch_state = bss_state_label(bss_state),
                                        exit_kind,
                                        "{warn_summary}"
                                    );
                                    let degraded = crate::monitor::dump::DegradedFailureDumpReport {
                                        schema: crate::monitor::dump::SCHEMA_DEGRADED.to_string(),
                                        reason,
                                        vcpu_regs,
                                        watchpoint_hit,
                                        bss_latch_state:
                                            bss_state_label(bss_state).to_string(),
                                        exit_kind,
                                        elapsed_ms,
                                    };
                                    break 'capture LateCaptureOutcome::Degraded(
                                        Box::new(degraded),
                                    );
                                }
                            }
                            // Exit-kind gate. The hardware watchpoint
                            // catches every write to
                            // `*scx_root->exit_kind`, including
                            // transient writes during init/teardown
                            // that the kernel sets to
                            // `SCX_EXIT_NONE` (0) or `SCX_EXIT_DONE`
                            // (1). Without this gate every clean
                            // scheduler shutdown produces a bogus
                            // failure dump. Read the live `exit_kind`
                            // value through the same direct-mapping +
                            // page-walk translation
                            // `read_scx_sched_state` uses; gate on
                            // `kind >= SCX_EXIT_ERROR (= 1024)` per
                            // `kernel/sched/ext_internal.h::scx_exit_kind`.
                            //
                            // Required prerequisites all flow from
                            // the same set the watchpoint resolution
                            // earlier in the loop validated when it
                            // published the `request_kva`:
                            //   - request_kva non-zero (resolved
                            //     `*scx_root + exit_kind_offset`)
                            //   - owned_accessor for cr3_pa /
                            //     page_offset / l5
                            //   - freeze_coord_mem for read_u32
                            // Any prereq absence means the watchpoint
                            // could not have armed (publish path
                            // requires the same handles), so a
                            // gated call without prerequisites is a
                            // logic bug — log + dump anyway rather
                            // than silently swallow the trigger.
                            if gate_on_exit_kind {
                                let exit_kind_kva = freeze_coord_watchpoint
                                    .request_kva
                                    .load(Ordering::Acquire);
                                let gate_decision = match (
                                    exit_kind_kva,
                                    owned_accessor.as_ref(),
                                    freeze_coord_mem.as_deref(),
                                ) {
                                    (0, _, _) | (_, None, _) | (_, _, None) => {
                                        tracing::warn!(
                                            exit_kind_kva = format_args!(
                                                "{:#x}",
                                                exit_kind_kva
                                            ),
                                            owned_accessor_present =
                                                owned_accessor.is_some(),
                                            mem_present = freeze_coord_mem.is_some(),
                                            "freeze-coord: exit_kind gate \
                                             prerequisites missing — proceeding \
                                             with dump (watchpoint should not \
                                             have armed without these)"
                                        );
                                        // Treat missing prereqs as
                                        // "do not gate" so a bona fide
                                        // dump still emits.
                                        true
                                    }
                                    (kva, Some(owned), Some(mem)) => {
                                        let kernel = owned.guest_kernel();
                                        let walk = kernel.walk_context();
                                        let translate_result = if FREEZE_COORD_TEST_FORCE_TRANSLATE_NONE
                                            .load(Ordering::Relaxed)
                                        {
                                            None
                                        } else {
                                            crate::monitor::idr::translate_any_kva(
                                                mem,
                                                walk.cr3_pa,
                                                walk.page_offset,
                                                kva,
                                                walk.l5,
                                                walk.tcr_el1,
                                            )
                                        };
                                        match translate_result {
                                            Some(pa) => {
                                                let kind = mem.read_u32(pa, 0);
                                                // SCX_EXIT_ERROR = 1024 — the
                                                // first error-class value in
                                                // `enum scx_exit_kind`. All
                                                // values below are clean
                                                // (NONE/DONE) or normal
                                                // unregister classes
                                                // (UNREG/SYSRQ/PARENT) that
                                                // do not warrant a failure
                                                // dump.
                                                const SCX_EXIT_ERROR: u32 = 1024;
                                                if kind < SCX_EXIT_ERROR {
                                                    tracing::info!(
                                                        kind,
                                                        exit_kind_kva =
                                                            format_args!("{:#x}", kva),
                                                        "freeze-coord: \
                                                         exit_kind gate \
                                                         suppressed dump \
                                                         (kind < 1024 = clean \
                                                         shutdown / non-error \
                                                         transition)"
                                                    );
                                                    false
                                                } else {
                                                    tracing::debug!(
                                                        kind,
                                                        "freeze-coord: \
                                                         exit_kind gate passed \
                                                         (kind >= 1024)"
                                                    );
                                                    true
                                                }
                                            }
                                            None => {
                                                // KVA was published but no
                                                // longer translates: most
                                                // likely the slab page that
                                                // held `*scx_root` was
                                                // freed during teardown.
                                                // Suppress the dump — there
                                                // is no scheduler state to
                                                // capture anyway.
                                                tracing::info!(
                                                    exit_kind_kva =
                                                        format_args!("{:#x}", kva),
                                                    "freeze-coord: exit_kind \
                                                     gate translate failed \
                                                     (scheduler likely torn \
                                                     down) — suppressing dump"
                                                );
                                                false
                                            }
                                        }
                                    }
                                };
                                if !gate_decision {
                                    // Cross-reference with the BPF probe
                                    // `.bss` latch before suppressing.
                                    // probe.bpf.c sets
                                    // `ktstr_err_exit_detected = 1` via
                                    // a `__sync_val_compare_and_swap`
                                    // 0→1 with release semantics over
                                    // the preceding `ktstr_exit_kind_snap`
                                    // and `ktstr_exit_*` stores — once
                                    // observed Triggered, the kernel
                                    // recorded an error-class exit even
                                    // if the live `*scx_root->exit_kind`
                                    // currently reads as a transient
                                    // clean value (the slab page was
                                    // freed mid-teardown, or the
                                    // kernel overwrote with NONE/DONE
                                    // during scx_ops_disable_workfn
                                    // teardown after firing
                                    // tp_btf/sched_ext_exit). In that
                                    // race window the gate would
                                    // silently drop a real failure
                                    // dump; the BPF latch is the
                                    // historical authority and wins.
                                    let bss_state = if FREEZE_COORD_TEST_FORCE_BSS_TRIGGERED
                                        .load(Ordering::Relaxed)
                                    {
                                        BssReadState::Triggered
                                    } else {
                                        bss_read_state(
                                            freeze_coord_mem.as_deref(),
                                            cached_bss_pa,
                                        )
                                    };
                                    // The cross-reference Triggered
                                    // override is only safe when the
                                    // cached `.bss` PA has not been
                                    // observed OOB in this run. Once
                                    // OOB fires (probe map freed mid-
                                    // run, vmalloc page recycled),
                                    // any subsequent non-zero read on
                                    // the cached PA could come from
                                    // page-allocator reuse with
                                    // unrelated data — synthesising
                                    // a phantom dump. Trust the gate
                                    // suppression in that case rather
                                    // than overriding on potentially
                                    // recycled bytes.
                                    //
                                    // Probe-lifecycle assumption: this
                                    // defense assumes probe BPF map
                                    // unload is bracketed by an
                                    // `OutOfBounds` observation that
                                    // latches `bss_oob_warn_logged`
                                    // BEFORE the next gate check fires.
                                    // Concurrent unload + slab recycle
                                    // within a single iteration body
                                    // (probe unloads AND the page is
                                    // re-allocated AND the gate fires
                                    // between two `bss_read_state(...)`
                                    // calls) is unreachable under the
                                    // current probe lifecycle: the
                                    // probe runs in the guest kernel
                                    // and lives for the guest VM's
                                    // lifetime; the freeze coordinator
                                    // joins inside `run_vm` BEFORE the
                                    // guest VM drops (see the
                                    // watchpoint-invalidation comment
                                    // near `run.vm` teardown), so the
                                    // probe's `.bss` page remains
                                    // valid for the entire coord
                                    // observation window. Revisit if
                                    // probe ownership moves to a
                                    // separate lifecycle (e.g. per-
                                    // test hot-reload that unloads +
                                    // reloads the probe mid-VM-run) —
                                    // that would require the latch
                                    // to fire per (unload, reload)
                                    // cycle rather than once per VM.
                                    if matches!(bss_state, BssReadState::Triggered)
                                        && !bss_oob_warn_logged
                                    {
                                        tracing::warn!(
                                            "freeze-coord: exit_kind gate \
                                             would suppress dump but BPF \
                                             `.bss` latch reports error \
                                             exit — proceeding with dump \
                                             (latch is historical authority)"
                                        );
                                    } else {
                                        break 'capture LateCaptureOutcome::Suppressed;
                                    }
                                }
                            }
                            if let Some(owned) = owned_accessor
                                && let Some(ref btf) = dump_btf
                            {
                                // Build the prog-runtime capture
                                // when both prerequisites are ready.
                                // Each is independent of the other
                                // (the accessor needs prog_idr +
                                // BTF; the offsets need __per_cpu_offset),
                                // so a partial setup yields no
                                // capture rather than a half-correct
                                // one — `dump_state` then writes an
                                // empty `prog_runtime_stats` vec
                                // alongside the full map render.
                                let prog_acc_borrow =
                                    owned_prog_accessor.as_ref().map(|o| o.as_accessor());
                                let prog_capture = match (
                                    prog_acc_borrow.as_ref(),
                                    prog_per_cpu_offsets.as_deref(),
                                ) {
                                    (Some(acc), Some(offsets)) => {
                                        Some(crate::monitor::dump::ProgRuntimeCapture {
                                            accessor: acc,
                                            per_cpu_offsets: offsets,
                                        })
                                    }
                                    _ => None,
                                };
                                let map_accessor = owned.as_accessor();
                                // Bind kernel once for the whole dump
                                // block. Pre-fix this called
                                // owned.guest_kernel() three times
                                // (scx_walker_capture, task_enrichment_capture,
                                // cpu_time_capture). The accessor is a
                                // trivial &-return but the repetition
                                // obscured ownership — every consumer
                                // wants the same kernel handle.
                                let dump_kernel = owned.guest_kernel();
                                // Pre-collect register snapshots: needed
                                // for both the report's vcpu_regs field
                                // AND the per-task enrichment running_pc
                                // mapping (walking rq->scx.curr to the
                                // corresponding vCPU's IP). Capturing
                                // here before the dump means the same
                                // snapshot drives every consumer.
                                let vcpu_regs = collect_vcpu_regs();
                                // SCX walker owned data — backs the
                                // borrow-only `ScxWalkerCapture`. The
                                // capture runs while every vCPU is
                                // paused at the freeze rendezvous, so
                                // each phase emits a tracing::debug
                                // duration line so operators can
                                // budget against the watchdog timeout.
                                let scx_build_t0 = std::time::Instant::now();
                                let scx_owned = crate::vmm::capture_scx::build(
                                    owned,
                                    dump_scx_walker_offsets.as_ref(),
                                    dump_cpu_time_symbols.as_ref(),
                                    prog_per_cpu_offsets.as_deref(),
                                );
                                tracing::debug!(
                                    elapsed_us = scx_build_t0.elapsed().as_micros() as u64,
                                    populated = scx_owned.is_some(),
                                    "freeze-coord: capture_scx::build"
                                );
                                let scx_walker_capture = scx_owned.as_ref().and_then(|so| {
                                    let offsets = dump_scx_walker_offsets.as_ref()?;
                                    Some(crate::monitor::dump::ScxWalkerCapture {
                                        kernel: dump_kernel,
                                        offsets,
                                        scx_root_kva: so.scx_root_kva,
                                        rq_kvas: &so.rq_kvas,
                                        rq_pas: &so.rq_pas,
                                        per_cpu_offsets: prog_per_cpu_offsets
                                            .as_deref()
                                            .unwrap_or(&[]),
                                        nr_nodes: freeze_coord_num_nodes,
                                    })
                                });
                                // Task-enrichment owned data — backs the
                                // borrow-only `TaskEnrichmentCapture`.
                                let task_build_t0 = std::time::Instant::now();
                                let task_owned = crate::vmm::capture_tasks::build(
                                    owned,
                                    scx_owned.as_ref(),
                                    dump_scx_walker_offsets.as_ref(),
                                    dump_task_enrichment_offsets.as_ref(),
                                    &vcpu_regs,
                                );
                                tracing::debug!(
                                    elapsed_us = task_build_t0.elapsed().as_micros() as u64,
                                    populated = task_owned.is_some(),
                                    tasks = task_owned.as_ref().map(|t| t.tasks.len()).unwrap_or(0),
                                    "freeze-coord: capture_tasks::build"
                                );
                                let task_enrichment_capture = task_owned.as_ref().and_then(|to| {
                                    let te_offsets = dump_task_enrichment_offsets.as_ref()?;
                                    Some(crate::monitor::dump::TaskEnrichmentCapture {
                                        kernel: dump_kernel,
                                        offsets: te_offsets,
                                        sched_classes: &to.sched_classes,
                                        lock_slowpaths: &to.lock_slowpaths,
                                        tasks: &to.tasks,
                                    })
                                });
                                // Per-CPU CPU-time / softirq / IRQ
                                // capture context. All four prereqs
                                // must be present to fire: BTF
                                // offsets (resolved at coord start),
                                // KernelSymbols carrying the
                                // `kernel_cpustat`/`kstat`
                                // per-CPU symbol KVAs (also at coord
                                // start), the per-CPU offset array
                                // (lazy-resolved alongside the prog
                                // accessor), and the freeze-coord
                                // GuestMem. Either of `kernel_cpustat`
                                // or `kstat` symbol absent makes the
                                // capture useless — both backing
                                // structs are needed for the dump's
                                // narrative (`tick_cpu_sched` is
                                // optional and feeds only the
                                // iowait_sleeptime field). The
                                // `tick_cpu_sched_kva` is forwarded
                                // to dump/mod.rs as Option so the per-CPU
                                // walker can skip iowait_sleeptime
                                // independently per CPU.
                                let cpu_time_capture = match (
                                    freeze_coord_mem.as_deref(),
                                    dump_cpu_time_offsets.as_ref(),
                                    dump_cpu_time_symbols.as_ref(),
                                    prog_per_cpu_offsets.as_deref(),
                                ) {
                                    (Some(mem), Some(offsets), Some(syms), Some(pcpu)) => {
                                        match (syms.kernel_cpustat, syms.kstat) {
                                            (Some(kcpustat_kva), Some(kstat_kva)) => {
                                                let page_offset = dump_kernel.page_offset();
                                                Some(crate::monitor::dump::CpuTimeCapture {
                                                    mem,
                                                    offsets,
                                                    kernel_cpustat_kva: kcpustat_kva,
                                                    kstat_kva,
                                                    tick_cpu_sched_kva: syms.tick_cpu_sched,
                                                    per_cpu_offsets: pcpu,
                                                    page_offset,
                                                })
                                            }
                                            _ => None,
                                        }
                                    }
                                    _ => None,
                                };
                                // Force the lazy cast-analysis on this
                                // dump's host coordinator thread (NOT a
                                // vCPU thread — vCPUs are paused at the
                                // freeze rendezvous). Bind the resulting
                                // `Option<Arc<CastAnalysisOutput>>`
                                // BEFORE the `DumpContext` literal so
                                // the inner `Arc` outlives
                                // `dump_state`'s borrow. First dump does
                                // the work; subsequent dumps in the same
                                // VM hit the `OnceLock` and return
                                // immediately.
                                //
                                // The full output carries both the cast
                                // map AND the cross-BTF Fwd resolution
                                // index (every parsed embedded BPF
                                // object's BTF + a name-keyed lookup).
                                // Both halves are threaded into
                                // `DumpContext` so the renderer's chase
                                // paths can resolve `BTF_KIND_FWD`
                                // pointees that live in a sibling
                                // object's BTF — the typical multi-
                                // `.bpf.objs` shape where one object
                                // declares `struct foo;` (forward) and
                                // another defines the body.
                                let cast_analysis = freeze_coord_cast_map.get_full();
                                let cast_map_ref = cast_analysis
                                    .as_ref()
                                    .and_then(|out| out.cast_maps.first().map(|m| m.as_ref()));
                                let cross_btf_fwd_index_owned = cast_analysis.as_ref().map(|out| {
                                    crate::monitor::dump::CrossBtfFwdIndex {
                                        btfs: &out.btfs,
                                        fwd_index: &out.fwd_index,
                                    }
                                });
                                let dump_state_t0 = std::time::Instant::now();
                                let mut report = crate::monitor::dump::dump_state(
                                    crate::monitor::dump::DumpContext {
                                        accessor: &map_accessor,
                                        btf,
                                        num_cpus: freeze_coord_num_cpus,
                                        arena_offsets: dump_arena_offsets.as_ref(),
                                        prog_capture: prog_capture.as_ref(),
                                        cpu_time_capture: cpu_time_capture.as_ref(),
                                        task_enrichment_capture: task_enrichment_capture
                                            .as_ref(),
                                        // Per-sample SCX_EV_* event counter
                                        // timeline. Today's freeze coordinator
                                        // does not share the monitor sampler's
                                        // accumulated samples vec — that
                                        // would require an Arc<Mutex<...>>
                                        // hand-off plumbed through
                                        // `start_monitor` / `monitor_loop`.
                                        // Leaving None preserves current
                                        // behavior (event_counter_timeline
                                        // stays empty in the failure dump
                                        // JSON); the timeline is still
                                        // recorded on `VmResult.monitor.samples`
                                        // for the post-run sidecar consumer.
                                        // A future task wiring the share
                                        // populates this with
                                        // `Some(EventCounterCapture { samples })`.
                                        event_counter_capture: None,
                                        scx_walker_capture: scx_walker_capture
                                            .as_ref(),
                                        // Per-vCPU PMU capture is shared
                                        // with the monitor sampler via the
                                        // `freeze_coord_perf_capture` Arc;
                                        // dump_state reads it once at the
                                        // freeze instant into
                                        // `vcpu_perf_at_freeze`. None when
                                        // perf was unavailable on this host
                                        // (paranoid > 2 / no CAP_PERFMON /
                                        // hardware lacks counters).
                                        perf_capture: (*freeze_coord_perf_capture).as_ref(),
                                        deadline: capture_deadline,
                                        // The bound `cast_map_ref` is
                                        // `Option<&CastMap>` derived from
                                        // the full output's inner `Arc`.
                                        // The full output keeps the
                                        // `CastMap` alive for the
                                        // duration of this `dump_state`
                                        // call.
                                        cast_map: cast_map_ref,
                                        // Cross-BTF Fwd resolution
                                        // context — see
                                        // [`DumpContext::cross_btf_fwd_index`].
                                        cross_btf_fwd_index: cross_btf_fwd_index_owned,
                                        alloc_size_types: cast_analysis
                                            .as_ref()
                                            .map(|o| o.alloc_size_types.as_slice())
                                            .unwrap_or(&[]),
                                    },
                                );
                                tracing::debug!(
                                    elapsed_us = dump_state_t0.elapsed().as_micros() as u64,
                                    maps = report.maps.len(),
                                    "freeze-coord: dump_state"
                                );
                                report.vcpu_regs = vcpu_regs;
                                // Per-node NUMA stats — overwrite the
                                // empty default `dump_state` writes when
                                // the producer lands a non-empty Vec.
                                let numa_build_t0 = std::time::Instant::now();
                                let numa_stats = crate::vmm::capture_numa::build(
                                    owned,
                                    dump_numa_offsets.as_ref(),
                                    dump_cpu_time_symbols.as_ref(),
                                    freeze_coord_num_nodes,
                                );
                                tracing::debug!(
                                    elapsed_us = numa_build_t0.elapsed().as_micros() as u64,
                                    nodes = numa_stats.as_ref().map(|s| s.len()).unwrap_or(0),
                                    "freeze-coord: capture_numa::build"
                                );
                                if let Some(stats) = numa_stats
                                    && !stats.is_empty()
                                {
                                    report.per_node_numa = stats;
                                    report.per_node_numa_unavailable = None;
                                }
                                LateCaptureOutcome::Captured(report, capture_start)
                            } else {
                                // Partial dump: vcpu_regs only.
                                let report = crate::monitor::dump::FailureDumpReport {
                                    schema: crate::monitor::dump::SCHEMA_SINGLE.to_string(),
                                    maps: Vec::new(),
                                    vcpu_regs: collect_vcpu_regs(),
                                    sdt_allocations: Vec::new(),
                                    prog_runtime_stats: Vec::new(),
                                    prog_runtime_stats_unavailable: Some(
                                        "dump prerequisites unavailable".to_string(),
                                    ),
                                    per_cpu_time: Vec::new(),
                                    task_enrichments: Vec::new(),
                                    task_enrichments_unavailable: Some(
                                        "dump prerequisites unavailable".to_string(),
                                    ),
                                    event_counter_timeline: Vec::new(),
                                    rq_scx_states: Vec::new(),
                                    dsq_states: Vec::new(),
                                    scx_sched_state: None,
                                    scx_walker_unavailable: Some(
                                        "dump prerequisites unavailable".to_string(),
                                    ),
                                    vcpu_perf_at_freeze: Vec::new(),
                                    per_node_numa: Vec::new(),
                                    per_node_numa_unavailable: Some(
                                        "dump prerequisites unavailable".to_string(),
                                    ),
                                    dump_truncated_at_us: None,
                                    probe_counters: None,
                                    scx_static_ranges: Default::default(),
                                    is_placeholder: false,
                                    sdt_alloc_unavailable: Some(
                                        "dump prerequisites unavailable".to_string(),
                                    ),
                                };
                                tracing::warn!(
                                    owned_accessor = owned_accessor.is_some(),
                                    dump_btf = dump_btf.is_some(),
                                    "freeze-coord: dump prerequisites unavailable; \
                                     emitting partial report with vcpu_regs only"
                                );
                                LateCaptureOutcome::Captured(report, capture_start)
                            }
                        } // end 'capture labeled block (the closure
                          // returns this block's value; the caller
                          // is responsible for invoking
                          // `thaw_and_barrier` AFTER any
                          // while-frozen work it needs to perform
                          // — the late-trigger backstop reads
                          // guest memory while quiesced, so the
                          // thaw cannot be unconditional inside
                          // the closure).
                        };
                    // Unified thaw + post-thaw barrier. Called by
                    // every site after `freeze_and_capture` returns
                    // (and after any while-frozen work the site
                    // needs). Replaces the per-site thaw block that
                    // previously diverged on which ordering rules
                    // fired. Resumes the virtio-blk worker FIRST so
                    // its `paused.load(Acquire)` poll exits before
                    // the freeze flag clears (worker polls `paused`,
                    // vCPUs poll `freeze`; resume-then-freeze=false
                    // means both wake paths land cleanly), then
                    // clears `freeze` and writes `thaw_evt` so every
                    // parked vCPU's poll wakes within microseconds.
                    //
                    // Post-thaw barrier — wait for every parker to
                    // clear its flag (vCPUs run their trailing
                    // `parked.store(false)` in handle_freeze AFTER
                    // observing freeze=false; the worker clears
                    // `paused` on resume()). Cycle N+1's
                    // rendezvous loop assumes all parked flags are
                    // false at entry; without this barrier a
                    // still-mid-thaw vCPU's `parked=true` would
                    // either be cleared by a force-clear and
                    // deadlock the cycle (legitimate parked=true
                    // for cycle N+1 never re-stored), OR be
                    // race-observed as a false positive (vCPU never
                    // parked for cycle N+1).
                    //
                    // No dedicated unparked_evt fd exists
                    // (handle_freeze does not write any eventfd on
                    // its trailing `parked.store(false)`); the
                    // barrier polls the AtomicBools at a 10 ms
                    // cadence — the same backstop handle_freeze
                    // uses for its `freeze.load(Acquire)` re-check
                    // when the thaw_evt poll's level fans across
                    // multiple parkers. EINTR / partial wakes are
                    // harmless; the predicate re-evaluates each
                    // iteration.
                    //
                    // Finally drain `parked_evt` so cycle N+1's
                    // countdown latch starts at 0.
                    let thaw_and_barrier = || {
                        // Always unfreeze + thaw even on teardown
                        // so vCPUs don't stay parked.
                        if let Some(ref blk) = freeze_coord_virtio_blk {
                            blk.lock().resume();
                        }
                        freeze_coord_freeze.store(false, Ordering::Release);
                        let _ = freeze_coord_thaw_evt.write(1);
                        if freeze_coord_bsp_done.load(Ordering::Acquire) {
                            return;
                        }

                        let post_thaw_deadline = Instant::now() + rendezvous_timeout;
                        loop {
                            if freeze_coord_kill.load(Ordering::Acquire)
                                || freeze_coord_bsp_done.load(Ordering::Acquire)
                            {
                                break;
                            }
                            let aps_unparked = freeze_coord_ap_parked
                                .iter()
                                .all(|p| !p.load(Ordering::Acquire));
                            let bsp_unparked = !freeze_coord_bsp_parked
                                .load(Ordering::Acquire);
                            // Lock-free read via the pre-acquired
                            // `paused_handle()` Arc — avoids
                            // taking the device mutex inside the
                            // post-thaw barrier hot loop. Acquire
                            // ordering pairs with the worker's
                            // Release on `paused.store(false)`
                            // (resume path) so the predicate sees
                            // a coherent worker state.
                            let blk_unpaused = freeze_coord_virtio_blk_paused
                                .as_ref()
                                .is_none_or(|p| !p.load(Ordering::Acquire));
                            if aps_unparked && bsp_unparked && blk_unpaused {
                                break;
                            }
                            let now = Instant::now();
                            if now > post_thaw_deadline {
                                let ap_states: Vec<bool> =
                                    freeze_coord_ap_parked
                                        .iter()
                                        .map(|p| p.load(Ordering::Acquire))
                                        .collect();
                                tracing::warn!(
                                    ?ap_states,
                                    bsp_parked = !bsp_unparked,
                                    blk_paused = !blk_unpaused,
                                    ?rendezvous_timeout,
                                    "freeze-coord: post-thaw barrier timed out — \
                                     a parker did not clear within the rendezvous \
                                     timeout; subsequent freeze cycles may see \
                                     stale parked=true and timeout the rendezvous"
                                );
                                break;
                            }
                            let remaining_ms = (post_thaw_deadline - now)
                                .as_millis()
                                .min(i32::MAX as u128) as i32;
                            let mut pfds = [
                                libc::pollfd {
                                    fd: freeze_coord_kill_evt.as_raw_fd(),
                                    events: libc::POLLIN,
                                    revents: 0,
                                },
                                libc::pollfd {
                                    fd: freeze_coord_bsp_done_evt.as_raw_fd(),
                                    events: libc::POLLIN,
                                    revents: 0,
                                },
                            ];
                            // SAFETY: pfds is a 2-element pollfd
                            // array; nfds matches. Bounded 10 ms
                            // wait is the cadence at which the
                            // AtomicBool predicate re-runs.
                            let wait_ms = 10.min(remaining_ms);
                            unsafe {
                                libc::poll(
                                    pfds.as_mut_ptr(),
                                    pfds.len() as libc::nfds_t,
                                    wait_ms,
                                );
                            }
                        }
                        // Drain parked_evt so cycle N+1's countdown
                        // latch starts at 0. EAGAIN (counter already
                        // 0) is benign.
                        let _ = freeze_coord_parked_evt.read();
                    };
                    // Helper: extend the watchdog deadline by the
                    // wall-clock duration of a single
                    // `freeze_and_capture(..)` cycle. Captures eat
                    // host wall-clock that would otherwise count
                    // against the workload's `workload_duration`
                    // budget; without this push, a 5 s test that
                    // fires a 2 s freeze gets only 3 s of guest
                    // execution before the watchdog kicks. Reads the
                    // current encoded reset target (or falls back to
                    // `workload_duration` counted from now) and
                    // writes back the sum + freeze_duration so the
                    // watchdog observes the extended deadline on its
                    // next tick. The watchdog only consults this
                    // atomic when `workload_duration` is set; runs
                    // without a workload budget remain on the
                    // boot-relative `hard_deadline` and this push is
                    // a no-op.
                    //
                    // Shared with the TLV CAPTURE handler, the
                    // user-watchpoint dispatcher, and the periodic-
                    // capture drain so the same arithmetic and
                    // ordering discipline apply at every fire site.
                    let extend_watchdog_for_freeze = |freeze_start: Instant| {
                        if let Some(d) = workload_duration_for_coord {
                            let freeze_duration = freeze_start.elapsed();
                            let prior = watchdog_reset_for_coord.load(Ordering::Acquire);
                            let prior_ns = if prior == 0 {
                                run_start
                                    .elapsed()
                                    .as_nanos()
                                    .saturating_add(d.as_nanos())
                            } else {
                                prior as u128
                            };
                            let new_target_ns =
                                prior_ns.saturating_add(freeze_duration.as_nanos());
                            let encoded = u64::try_from(new_target_ns).unwrap_or(u64::MAX).max(1);
                            watchdog_reset_for_coord.store(encoded, Ordering::Release);
                        }
                    };
                    // Helper: persist the JSON to the optional file
                    // sink, then log a single info-level summary line
                    // referencing the file path + byte count +
                    // capture timing. The JSON is NOT inlined into
                    // the trace log — a 50-map dump runs hundreds of
                    // KB and floods every downstream sink (file
                    // logger, journald, stderr) with a payload that
                    // is already on disk at the dump path.
                    //
                    // On atomic-write failure, emit a structured
                    // summary line + truncated JSON head to stderr
                    // as last-ditch preservation. stderr is a
                    // separate channel from the filesystem and
                    // typically survives the kinds of failures
                    // (ENOSPC, EROFS, EACCES) that fail file writes.
                    // The summary uses the operator-friendly stats
                    // this closure already receives (map_count,
                    // vcpu_regs_count, tasks_enriched, elapsed_ms,
                    // json_bytes) so the structural signal survives
                    // independently of the 16 KiB head truncation
                    // that may drop late fields in the serde-ordered
                    // payload. Silent loss of a Captured
                    // failure-dump on disk-full is the exact
                    // silent-drop class to prevent.
                    //
                    // STDERR last-ditch preservation cap is shared
                    // by the 3 stderr-fallback sites (emit_json,
                    // emit_degraded_json, end-of-coord drain).
                    // UTF-8-safe slicing delegated to the module-
                    // level `utf8_safe_truncate_len` helper above so
                    // the 3 sites share one implementation rather
                    // than each carrying an inline copy that could
                    // drift.
                    //
                    // Why stderr fallback, not Result propagation:
                    // the freeze coordinator is an autonomous thread —
                    // its closure has no caller to receive an `Err`
                    // from a failed emit. Propagating Err to the
                    // thread join site in `run_vm` would block teardown
                    // on a serde-/tracing-/FS-failure path the operator
                    // may want to see immediately, not at join time.
                    // `tracing::warn!` dispatch can itself fail
                    // (subscriber disconnected, journald socket closed,
                    // file-appender out of space) with no visible
                    // signal when it does. stderr is the OS-mediated
                    // last channel: writes survive ENOSPC/EROFS on the
                    // dump filesystem (stderr is typically a separate
                    // pipe to the test harness), survive subscriber
                    // teardown (`eprintln!` bypasses the tracing
                    // pipeline), and surface in the same captured
                    // stream the harness already monitors for panic
                    // messages. Per `feedback_no_silent_drops`: the
                    // dump fires loudly even when the configured sink
                    // AND the structured log channels are both
                    // unavailable.
                    const STDERR_DUMP_CAP: usize = 16 * 1024;
                    #[allow(clippy::too_many_arguments)]
                    let emit_json = |json: &str,
                                     map_count: usize,
                                     vcpu_regs_count: usize,
                                     vcpu_none_indices: &[usize],
                                     tasks_enriched: usize,
                                     elapsed_ms: u64,
                                     truncated_at_us: Option<u64>| {
                        // Stderr fallback gates on `write_failed`
                        // ONLY — sink configured AND write_atomic
                        // failed (ENOSPC / EROFS / EACCES).
                        // Production callers that don't wire
                        // `failure_dump_path` (verifier, shell,
                        // template build per builder default = None)
                        // never asked for failure-dump preservation,
                        // so an err-class scheduler exit there is
                        // not a silent drop — it's "no capture was
                        // ever requested." Only the test-dispatch
                        // path in
                        // `test_support::eval::run_ktstr_test_inner`
                        // (search `failure_dump_path(primary_dump_path)`)
                        // and the auto-repro path in
                        // `test_support::probe::attempt_auto_repro`
                        // (search `failure_dump_path(&repro_dump_path)`)
                        // wire the sink; those are the call sites
                        // where silent-drop avoidance applies. The
                        // tracing::info! summary line at the end of
                        // this closure (path = None branch) remains
                        // the operator-visible signal in the no-sink
                        // case, with no stderr noise for verifier /
                        // shell / template iteration.
                        let mut write_failed = false;
                        let path_str: Option<String> =
                            freeze_coord_dump_path.as_ref().and_then(|p| {
                                if let Some(parent) = p.parent() {
                                    let _ = std::fs::create_dir_all(parent);
                                }
                                // Atomic-publish pattern: write to a sibling
                                // .tmp file, fsync the file, then rename into
                                // place, then fsync the parent directory.
                                // Guarantees a reader of `p` either sees the
                                // previous file (if any) or the complete new
                                // dump — never a truncated / mid-write JSON
                                // file. The fsync on the tmp file holds the
                                // bytes against a host crash between rename()
                                // and writeback; the parent-dir fsync (CF3)
                                // holds the directory-entry update against a
                                // host crash post-rename so the operator
                                // post-reboot sees the new file at `p`
                                // rather than an empty parent. POSIX rename(2)
                                // atomicity covers ordering visible to other
                                // processes, NOT durability — directory-entry
                                // durability requires fsync on the parent dir,
                                // matching what database engines (SQLite,
                                // RocksDB) do for journal commits.
                                let tmp_path = p.with_extension("json.tmp");
                                let write_atomic = || -> std::io::Result<()> {
                                    use std::io::Write as _;
                                    let mut f =
                                        std::fs::File::create(&tmp_path)?;
                                    f.write_all(json.as_bytes())?;
                                    f.sync_all()?;
                                    drop(f);
                                    std::fs::rename(&tmp_path, p)?;
                                    // Canonical-path parent-dir fsync (CF3):
                                    // best-effort durability. If the parent
                                    // fsync fails (ENOSPC / EROFS / EIO), do
                                    // NOT roll back the rename — per user
                                    // direction, the operator-facing
                                    // failure-dump.json on disk is more
                                    // valuable than bridge/FS contract
                                    // symmetry. The file IS visible (rename
                                    // succeeded); only the directory-entry
                                    // durability across a host crash is in
                                    // doubt. Log the durability gap and
                                    // continue — operators inspecting the
                                    // dump dir post-test see the file, with
                                    // a warn flagging the missed fsync. The
                                    // tagged-sibling helper write_to_tagged_path
                                    // takes the opposite stance (it rolls
                                    // back) because per-trigger sibling
                                    // dumps don't have the same operator-
                                    // signal preservation concern.
                                    if let Some(dir) = p.parent()
                                        && let Err(parent_fsync_err) =
                                            std::fs::File::open(dir).and_then(|d| d.sync_all())
                                    {
                                        tracing::warn!(
                                            path = %p.display(),
                                            parent_fsync_error = %parent_fsync_err,
                                            "freeze-coord: failure-dump parent-dir fsync failed after rename; file IS visible on disk but directory-entry durability across a host crash is not guaranteed — operator-facing dump preserved per operator-has-the-data discipline"
                                        );
                                    }
                                    Ok(())
                                };
                                match write_atomic() {
                                    Ok(()) => Some(p.display().to_string()),
                                    Err(e) => {
                                        write_failed = true;
                                        // Best-effort cleanup of the leftover
                                        // tmp file so a future operator
                                        // doesn't see a stale `.json.tmp`
                                        // alongside the previous dump.
                                        let _ = std::fs::remove_file(&tmp_path);
                                        tracing::warn!(
                                            path = %p.display(),
                                            tmp_path = %tmp_path.display(),
                                            error = %e,
                                            "freeze-coord: failure-dump atomic write failed; stderr fallback below"
                                        );
                                        None
                                    }
                                }
                            });
                        // Stderr last-ditch preservation fires ONLY
                        // when a configured sink failed
                        // (`write_failed`). The structured summary
                        // line surfaces the operator-critical
                        // signals (vcpu count + none-indices, map
                        // count, payload bytes) regardless of
                        // payload size, then the truncated JSON head
                        // provides forensic context.
                        // `vcpu_none_indices` shows the operator
                        // WHICH vCPUs were never parked — same
                        // signal the drain summary surfaces.
                        // Callers that never wired
                        // `failure_dump_path` (verifier / shell /
                        // template) get the closure's tail
                        // tracing::info! "no file sink" line and no
                        // stderr noise — no capture was ever
                        // requested.
                        if write_failed {
                            eprintln!(
                                "{SNAPSHOT_SUMMARY_PREFIX} (Captured, write failed): map_count={} vcpu_regs_count={} vcpu_none_indices={:?} tasks_enriched={} elapsed_ms={} json_bytes={}",
                                map_count,
                                vcpu_regs_count,
                                vcpu_none_indices,
                                tasks_enriched,
                                elapsed_ms,
                                json.len(),
                            );
                            let head_end = utf8_safe_truncate_len(
                                json,
                                STDERR_DUMP_CAP,
                            );
                            let truncated_marker =
                                format_truncation_marker(head_end, json.len());
                            eprintln!(
                                "freeze-coord: STDERR-PRESERVED payload head (Captured, write failed){}: {}",
                                truncated_marker,
                                &json[..head_end]
                            );
                        }
                        let json_bytes = json.len();
                        // Disambiguation and consolidation. See
                        // [`format_path_part`] for the 3-state rule.
                        let path_part = format_path_part(path_str.as_deref(), write_failed);
                        let trunc_part = truncated_at_us
                            .map(|us| format!(" (truncated at {us}us)"))
                            .unwrap_or_default();
                        tracing::info!(
                            target: "ktstr::failure_dump",
                            map_count,
                            vcpu_regs_count,
                            tasks_enriched,
                            json_bytes,
                            elapsed_ms,
                            truncated_at_us,
                            path = path_str.as_deref(),
                            "freeze-coord: dump complete{trunc_part}, {map_count} maps, {tasks_enriched} tasks enriched, {elapsed_ms}ms freeze, {json_bytes} bytes{path_part}"
                        );
                    };
                    // Persist a SCHEMA_DEGRADED JSON via the same
                    // atomic-publish dance `emit_json` uses. Separate
                    // closure so the log target / message identifies
                    // the degraded shape distinctly from a full dump
                    // — an operator reading the log sees "dump
                    // degraded" rather than the "dump complete"
                    // banner the full-dump path emits. The reason +
                    // elapsed_ms surface in the log line so a quick
                    // scan of trace output answers the operator's
                    // first question ("what went wrong?") without
                    // opening the JSON.
                    let emit_degraded_json = |json: &str,
                                              reason: &str,
                                              elapsed_ms: u64| {
                        // Symmetric with emit_json: stderr fallback
                        // hoisted OUT of the and_then closure so a
                        // Degraded-path write_atomic failure
                        // surfaces to stderr regardless of the
                        // closure short-circuit, AND gated on
                        // `write_failed` so callers that never
                        // wired `failure_dump_path` (verifier /
                        // shell / template) don't get a stderr
                        // blast for "no capture was requested."
                        // See emit_json above for the full
                        // rationale.
                        let mut write_failed = false;
                        let path_str: Option<String> =
                            freeze_coord_dump_path.as_ref().and_then(|p| {
                                if let Some(parent) = p.parent() {
                                    let _ = std::fs::create_dir_all(parent);
                                }
                                let tmp_path = p.with_extension("json.tmp");
                                let write_atomic = || -> std::io::Result<()> {
                                    use std::io::Write as _;
                                    let mut f =
                                        std::fs::File::create(&tmp_path)?;
                                    f.write_all(json.as_bytes())?;
                                    f.sync_all()?;
                                    drop(f);
                                    std::fs::rename(&tmp_path, p)?;
                                    // Canonical-path parent-dir fsync (CF3):
                                    // best-effort durability — symmetric with
                                    // emit_json above. On parent-fsync failure
                                    // after a successful rename, do NOT roll
                                    // back the renamed file: the operator-
                                    // facing degraded dump on disk is more
                                    // valuable than bridge/FS contract
                                    // symmetry. Log the durability gap and
                                    // continue; operators reading the dump
                                    // dir post-test see the file with a warn
                                    // flagging the missed fsync. The tagged-
                                    // sibling helper write_to_tagged_path
                                    // takes the opposite stance (rollback)
                                    // because per-trigger siblings don't
                                    // carry the same operator-signal
                                    // preservation concern.
                                    if let Some(dir) = p.parent()
                                        && let Err(parent_fsync_err) =
                                            std::fs::File::open(dir).and_then(|d| d.sync_all())
                                    {
                                        tracing::warn!(
                                            path = %p.display(),
                                            parent_fsync_error = %parent_fsync_err,
                                            "freeze-coord: degraded-dump parent-dir fsync failed after rename; file IS visible on disk but directory-entry durability across a host crash is not guaranteed — operator-facing dump preserved per operator-has-the-data discipline"
                                        );
                                    }
                                    Ok(())
                                };
                                match write_atomic() {
                                    Ok(()) => Some(p.display().to_string()),
                                    Err(e) => {
                                        write_failed = true;
                                        let _ = std::fs::remove_file(&tmp_path);
                                        tracing::warn!(
                                            path = %p.display(),
                                            tmp_path = %tmp_path.display(),
                                            error = %e,
                                            "freeze-coord: degraded-dump atomic write failed; stderr fallback below"
                                        );
                                        None
                                    }
                                }
                            });
                        if write_failed {
                            eprintln!(
                                "{SNAPSHOT_SUMMARY_PREFIX} (Degraded, write failed): reason={:?} elapsed_ms={} json_bytes={}",
                                reason,
                                elapsed_ms,
                                json.len(),
                            );
                            let head_end = utf8_safe_truncate_len(json, STDERR_DUMP_CAP);
                            let truncated_marker =
                                format_truncation_marker(head_end, json.len());
                            eprintln!(
                                "freeze-coord: STDERR-PRESERVED payload head (Degraded, write failed){}: {}",
                                truncated_marker,
                                &json[..head_end]
                            );
                        }
                        let json_bytes = json.len();
                        // Same as emit_json. See [`format_path_part`]
                        // for the rule.
                        let path_part = format_path_part(path_str.as_deref(), write_failed);
                        tracing::warn!(
                            target: "ktstr::failure_dump",
                            reason,
                            json_bytes,
                            elapsed_ms,
                            path = path_str.as_deref(),
                            "freeze-coord: degraded dump emitted, {reason}, {elapsed_ms}ms, {json_bytes} bytes{path_part}"
                        );
                    };
                    // On-demand snapshot handler. Drains every
                    // [`crate::vmm::wire::MSG_TYPE_SNAPSHOT_REQUEST`]
                    // frame the TOKEN_TX handler accumulated this
                    // iteration, regardless of `freeze_state`. The
                    // `on_demand_in_flight` AcqRel-bool serialises
                    // CAPTURE/WATCH against the user-watchpoint
                    // dispatcher below — a snapshot capture in
                    // progress here makes the watchpoint loop re-arm
                    // its `hit` flag for the next iteration instead
                    // of opening a second concurrent capture window.
                    //
                    // CAPTURE runs `freeze_and_capture(false)` and
                    // stores the report on the bridge under the
                    // tag, then frames a `MSG_TYPE_SNAPSHOT_REPLY`
                    // TLV (header + 72-byte payload) and pushes it
                    // through `queue_input_port1` so the guest's
                    // blocking reader on `/dev/vport0p1` wakes
                    // within microseconds and observes
                    // `reply.request_id == request.request_id`.
                    // WATCH resolves the symbol via the cached
                    // vmlinux ELF symbol table, allocates a free
                    // user watchpoint slot, publishes the resolved
                    // KVA + tag into `WatchpointArm`, kicks every
                    // vCPU so `self_arm_watchpoint` picks up the
                    // new arm before the next `KVM_RUN`, and
                    // replies OK over the same TLV channel. A
                    // future guest write to the resolved KVA fires
                    // the corresponding `KVM_EXIT_DEBUG` and the
                    // user-watchpoint dispatcher (further down the
                    // iteration) drives the matching capture.
                    let pending = std::mem::take(&mut snapshot_requests_pending);
                    for SnapshotRequest {
                        request_id,
                        kind,
                        tag,
                    } in pending
                    {
                        if kind == crate::vmm::wire::SNAPSHOT_KIND_CAPTURE
                            && owned_accessor.is_none()
                        {
                            tracing::info!(
                                request_id,
                                %tag,
                                "freeze-coord: TLV CAPTURE deferred \
                                 (owned_accessor not yet adopted)"
                            );
                            capture_requests_deferred.push(SnapshotRequest {
                                request_id,
                                kind,
                                tag,
                            });
                            continue;
                        }
                        if freeze_coord_on_demand_in_flight
                            .swap(true, Ordering::AcqRel)
                        {
                            // A user-watchpoint capture is already
                            // in flight (or a prior iteration
                            // somehow left the gate set). Reply
                            // ERR rather than let the guest block
                            // its full 30 s deadline; the test
                            // can retry once the in-flight
                            // capture completes.
                            let reply = frame_snapshot_reply(
                                request_id,
                                crate::vmm::wire::SNAPSHOT_STATUS_ERR,
                                "another snapshot capture is in flight; retry",
                            );
                            freeze_coord_virtio_con
                                .lock()
                                .queue_input_port1(&reply);
                            tracing::warn!(
                                request_id,
                                %tag,
                                kind,
                                "freeze-coord: snapshot request rejected (in-flight gate held)"
                            );
                            continue;
                        }
                        match kind {
                            crate::vmm::wire::SNAPSHOT_KIND_CAPTURE => {
                                tracing::info!(
                                    request_id,
                                    %tag,
                                    "freeze-coord: TLV CAPTURE request"
                                );
                                // CAPTURE has no while-frozen work,
                                // so thaw immediately after the
                                // dump returns. Then extend the
                                // watchdog deadline by the freeze
                                // duration via the shared closure
                                // (TLV CAPTURE / user watchpoint /
                                // periodic-capture all use the same
                                // arithmetic — see
                                // `extend_watchdog_for_freeze` for
                                // the full rationale).
                                let freeze_start = Instant::now();
                                let on_demand = freeze_and_capture(false);
                                thaw_and_barrier();
                                extend_watchdog_for_freeze(freeze_start);
                                let mut reply_status =
                                    crate::vmm::wire::SNAPSHOT_STATUS_OK;
                                let mut reply_reason = String::new();
                                match on_demand {
                                    LateCaptureOutcome::Captured(report, capture_start) => {
                                    let map_count = report.maps.len();
                                    let vcpu_regs_count =
                                        report.vcpu_regs.len();
                                    let tasks_enriched =
                                        report.task_enrichments.len();
                                    // File mirror first via `&report`
                                    // (no clone). Bridge `store`
                                    // consumes the report, so any
                                    // additional reader needs to run
                                    // BEFORE the move. `to_string`
                                    // (compact) replaces
                                    // `to_string_pretty` to halve
                                    // serialization cost — the JSON
                                    // is consumed by tests and tools,
                                    // not by humans, and `jq` /
                                    // `serde_json::from_str` parse
                                    // both forms identically. Avoids
                                    // the prior `report.clone()` deep
                                    // copy of hundreds-of-KB-scale
                                    // dump data.
                                    if freeze_coord_dump_path.is_some() {
                                        match serde_json::to_string(&report) {
                                            Ok(json) => {
                                                let _ = write_to_tagged_path(
                                                    freeze_coord_dump_path.as_deref(),
                                                    &tag,
                                                    &json,
                                                    || {
                                                        format!(
                                                            "{SNAPSHOT_SUMMARY_PREFIX} (on-demand TLV CAPTURE, write failed): request_id={request_id} tag={tag} map_count={map_count} vcpu_regs_count={vcpu_regs_count} tasks_enriched={tasks_enriched} elapsed_ms={} json_bytes={}",
                                                            capture_start.elapsed().as_millis() as u64,
                                                            json.len(),
                                                        )
                                                    },
                                                    "freeze-coord: on-demand TLV CAPTURE dump file write failed",
                                                );
                                            }
                                            Err(e) => tracing::error!(
                                                error = %e,
                                                map_count,
                                                vcpu_regs_count,
                                                "freeze-coord: on-demand dump (JSON serialization failed)"
                                            ),
                                        }
                                    }
                                    let elapsed_ms = capture_start
                                        .elapsed()
                                        .as_millis() as u64;
                                    tracing::info!(
                                        target: "ktstr::failure_dump",
                                        kind = "on_demand_capture",
                                        request_id,
                                        %tag,
                                        map_count,
                                        vcpu_regs_count,
                                        tasks_enriched,
                                        elapsed_ms,
                                        "freeze-coord: snapshot captured and stored on bridge"
                                    );
                                    // Persist on the bridge LAST —
                                    // store moves the report. Test
                                    // code drains the bridge after
                                    // VM exit and walks the reports
                                    // via the public `Snapshot`
                                    // accessor.
                                    freeze_coord_snapshot_bridge.store(&tag, report);
                                    }
                                    LateCaptureOutcome::Degraded(degraded) => {
                                        // Rendezvous timed out building
                                        // the on-demand capture. Write
                                        // the degraded JSON to the same
                                        // tagged dump path and store a
                                        // placeholder on the bridge
                                        // carrying the partial vcpu_regs
                                        // + the degraded reason —
                                        // operator's structured
                                        // diagnostics survive even when
                                        // the bridge entry is a
                                        // legacy-shape placeholder.
                                        if freeze_coord_dump_path.is_some() {
                                            match serde_json::to_string(degraded.as_ref()) {
                                                Ok(json) => {
                                                    let _ = write_to_tagged_path(
                                                        freeze_coord_dump_path.as_deref(),
                                                        &tag,
                                                        &json,
                                                        || {
                                                            format!(
                                                                "{SNAPSHOT_SUMMARY_PREFIX} (on-demand TLV CAPTURE Degraded, write failed): request_id={request_id} tag={tag} reason={:?} json_bytes={}",
                                                                degraded.reason,
                                                                json.len(),
                                                            )
                                                        },
                                                        "freeze-coord: on-demand TLV CAPTURE degraded dump file write failed",
                                                    );
                                                }
                                                Err(e) => tracing::error!(
                                                    error = %e,
                                                    "freeze-coord: on-demand degraded dump (JSON serialization failed)"
                                                ),
                                            }
                                        }
                                        let placeholder = crate::monitor::dump::FailureDumpReport {
                                            vcpu_regs: degraded.vcpu_regs.clone(),
                                            ..crate::monitor::dump::FailureDumpReport::placeholder(
                                                degraded.reason.clone(),
                                            )
                                        };
                                        freeze_coord_snapshot_bridge.store(&tag, placeholder);
                                        reply_status =
                                            crate::vmm::wire::SNAPSHOT_STATUS_ERR;
                                        reply_reason = degraded.reason.clone();
                                        tracing::warn!(
                                            request_id,
                                            %tag,
                                            reason = %degraded.reason,
                                            "freeze-coord: on-demand capture degraded (rendezvous timeout)"
                                        );
                                    }
                                    LateCaptureOutcome::Suppressed => {
                                        // Unreachable in practice: the gate
                                        // only runs when gate_on_exit_kind=true
                                        // and the on-demand path always
                                        // passes false. Defensive: reply
                                        // ERR so a future code path that
                                        // changes the gate flag can't
                                        // silently drop the request.
                                        reply_status =
                                            crate::vmm::wire::SNAPSHOT_STATUS_ERR;
                                        reply_reason =
                                            "on-demand capture suppressed (gate decision)".to_string();
                                        tracing::warn!(
                                            request_id,
                                            %tag,
                                            "freeze-coord: on-demand capture unexpectedly suppressed by gate"
                                        );
                                    }
                                }
                                let reply = frame_snapshot_reply(
                                    request_id,
                                    reply_status,
                                    &reply_reason,
                                );
                                freeze_coord_virtio_con
                                    .lock()
                                    .queue_input_port1(&reply);
                            }
                            crate::vmm::wire::SNAPSHOT_KIND_WATCH => {
                                if coord_kaslr_offset == 0
                                    && owned_accessor.is_none()
                                {
                                    tracing::info!(
                                        request_id,
                                        %tag,
                                        "freeze-coord: TLV WATCH deferred \
                                         (kaslr_offset not yet resolved)"
                                    );
                                    freeze_coord_on_demand_in_flight
                                        .store(false, Ordering::Release);
                                    capture_requests_deferred.push(SnapshotRequest {
                                        request_id,
                                        kind,
                                        tag,
                                    });
                                    continue;
                                }
                                tracing::info!(
                                    request_id,
                                    %tag,
                                    "freeze-coord: TLV WATCH request"
                                );
                                // Reply path branches on whether the
                                // cached vmlinux symbol map is available.
                                // The fall-through (no `continue`) lets
                                // the user-watchpoint loop and the
                                // late-trigger handler later in this
                                // iteration still run, so a WATCH that
                                // cannot resolve does not stall an
                                // already-pending err_triggered dump
                                // for a full poll interval.
                                let (status, reason) = match freeze_coord_symbol_cache.as_ref() {
                                    None => (
                                        crate::vmm::wire::SNAPSHOT_STATUS_ERR,
                                        "vmlinux symbol cache unavailable \
                                         (vmlinux not found or parse failed at \
                                         coord init)"
                                            .to_string(),
                                    ),
                                    Some(symbol_cache) => {
                                        // Pass the bsp_alive Arc by
                                        // reference so each BSP-touching
                                        // site inside `arm_user_watchpoint`
                                        // (the BSP `ie.set` and the BSP
                                        // `pthread_kill`) issues its own
                                        // fresh Acquire load immediately
                                        // before the syscall. A bool
                                        // snapshot taken here would be
                                        // stale by the time the kick
                                        // pass reaches the BSP — long
                                        // enough for the BSP run-loop to
                                        // publish `false` (Release) and
                                        // drop its `VcpuFd`, leaving a
                                        // `true`-snapshot writing through
                                        // freed kvm_run mmap pages.
                                        // `run_vm` flips bsp_alive to
                                        // false only AFTER joining the
                                        // coordinator (see `bsp_alive`
                                        // in run_vm), so a `true`
                                        // reading inside the helper is
                                        // load-bearing for the BSP
                                        // kvm_run mmap's liveness.
                                        match arm_user_watchpoint(
                                            &freeze_coord_watchpoint,
                                            symbol_cache,
                                            &tag,
                                            coord_kaslr_offset,
                                            &freeze_coord_ap_pthreads,
                                            &freeze_coord_ap_ies,
                                            &freeze_coord_ap_alive,
                                            freeze_coord_bsp_tid,
                                            freeze_coord_bsp_ie_handle.as_ref(),
                                            &bsp_alive_for_coord,
                                        ) {
                                            Ok(slot_idx) => {
                                                tracing::info!(
                                                    request_id,
                                                    %tag,
                                                    slot_idx,
                                                    "freeze-coord: hardware watchpoint armed"
                                                );
                                                (
                                                    crate::vmm::wire::SNAPSHOT_STATUS_OK,
                                                    String::new(),
                                                )
                                            }
                                            Err(reason) => {
                                                tracing::warn!(
                                                    request_id,
                                                    %tag,
                                                    %reason,
                                                    "freeze-coord: WATCH register failed"
                                                );
                                                (
                                                    crate::vmm::wire::SNAPSHOT_STATUS_ERR,
                                                    reason,
                                                )
                                            }
                                        }
                                    }
                                };
                                let reply = frame_snapshot_reply(
                                    request_id,
                                    status,
                                    &reason,
                                );
                                freeze_coord_virtio_con
                                    .lock()
                                    .queue_input_port1(&reply);
                            }
                            unknown => {
                                tracing::warn!(
                                    request_id,
                                    %tag,
                                    kind = unknown,
                                    "freeze-coord: TLV snapshot request with unknown kind"
                                );
                                let reply = frame_snapshot_reply(
                                    request_id,
                                    crate::vmm::wire::SNAPSHOT_STATUS_ERR,
                                    &format!("unknown snapshot kind {unknown}"),
                                );
                                freeze_coord_virtio_con
                                    .lock()
                                    .queue_input_port1(&reply);
                            }
                        }
                        freeze_coord_on_demand_in_flight
                            .store(false, Ordering::Release);
                    }
                    // Periodic-capture cadence runs BEFORE the
                    // user-watchpoint dispatch below so periodic
                    // boundaries get priority over Op::Snapshot /
                    // Op::WatchSnapshot fires when both contend for
                    // the same `freeze_coord_on_demand_in_flight`
                    // gate. Iteration ordering within the body:
                    // TLV CAPTURE runs first (request-reply,
                    // self-throttling); periodic runs second with
                    // priority over user-watchpoint hits.
                    // Lazily compute the boundary list once
                    // `num_snapshots > 0`, the workload duration is
                    // known, and the first ScenarioStart has been
                    // stamped — then on every iteration check
                    // whether `now` has crossed the next un-fired
                    // boundary, and fire a host-side
                    // `freeze_and_capture(false)` for each crossed
                    // boundary. Reuses the same gate
                    // (`freeze_coord_on_demand_in_flight`) the
                    // TLV CAPTURE / user-watchpoint paths use —
                    // when the gate is held the boundary is
                    // deferred to the next iteration rather than
                    // skipped, so a burst of on-demand captures
                    // cannot cause a missed periodic sample. The
                    // 10% / 10% pre/post buffers in the boundary
                    // formula are the budget that absorbs this
                    // deferral lag.
                    if freeze_coord_num_snapshots > 0 && !periodic_abandoned {
                        if periodic_boundaries_ns.is_none()
                            && let Some(workload_d) = workload_duration_for_coord
                        {
                            let scenario_anchor =
                                scenario_start_ns_for_coord.load(Ordering::Relaxed);
                            if scenario_anchor != 0 {
                                let boundaries = compute_periodic_boundaries_ns(
                                    scenario_anchor,
                                    workload_d,
                                    freeze_coord_num_snapshots,
                                );
                                tracing::info!(
                                    target: "ktstr::failure_dump",
                                    num_snapshots = freeze_coord_num_snapshots,
                                    scenario_anchor_ns = scenario_anchor,
                                    workload_duration_ns = workload_d.as_nanos() as u64,
                                    "freeze-coord: periodic snapshot boundaries computed"
                                );
                                periodic_boundaries_ns = Some(boundaries);
                            }
                        }
                        if let Some(ref boundaries) = periodic_boundaries_ns {
                            // Drain every crossed boundary in this
                            // iteration. `now_ns` is recomputed at
                            // the top of every inner-loop iteration
                            // so a mid-drain ScenarioPause /
                            // ScenarioResume pair (a single periodic
                            // capture can run for several seconds
                            // through the parked-vCPU rendezvous)
                            // shifts un-fired boundaries forward as
                            // soon as the cumulative pause atomic
                            // updates.
                            loop {
                                if (next_periodic_idx as usize) >= boundaries.len() {
                                    break;
                                }
                                let raw_now_ns =
                                    u64::try_from(run_start.elapsed().as_nanos())
                                        .unwrap_or(u64::MAX);
                                let cumulative_pause = scenario_pause_cumulative_for_coord
                                    .load(Ordering::Acquire);
                                let in_flight_pause_at = watchdog_pause_for_coord
                                    .load(Ordering::Acquire);
                                let in_flight_pause = if in_flight_pause_at > 0 {
                                    raw_now_ns.saturating_sub(in_flight_pause_at)
                                } else {
                                    0
                                };
                                let now_ns = raw_now_ns
                                    .saturating_sub(cumulative_pause)
                                    .saturating_sub(in_flight_pause);
                                if boundaries[next_periodic_idx as usize] > now_ns {
                                    break;
                                }
                                if freeze_coord_kill.load(Ordering::Acquire) {
                                    break;
                                }
                                if freeze_coord_on_demand_in_flight
                                    .swap(true, Ordering::AcqRel)
                                {
                                    // Gate held — defer (do NOT
                                    // skip): leave next_periodic_idx
                                    // as-is so the next iteration
                                    // retries this same boundary
                                    // once the gate clears.
                                    tracing::info!(
                                        target: "ktstr::failure_dump",
                                        idx = next_periodic_idx,
                                        tag = %periodic_tag(next_periodic_idx),
                                        "freeze-coord: periodic snapshot deferred \
                                         (in-flight gate held by another capture); \
                                         retrying next iteration"
                                    );
                                    break;
                                }
                                let tag = periodic_tag(next_periodic_idx);
                                tracing::info!(
                                    target: "ktstr::failure_dump",
                                    idx = next_periodic_idx,
                                    %tag,
                                    "freeze-coord: periodic snapshot boundary crossed"
                                );
                                // Request scx_stats BEFORE the freeze
                                // rendezvous so the scheduler's
                                // userspace thread is still alive to
                                // service the request. Failure modes
                                // (no scheduler, relay error, non-
                                // zero envelope errno) all collapse
                                // to `None` — the parallel stats
                                // slot stays absent and the
                                // temporal-stats projection surfaces
                                // a per-sample missing-stats failure
                                // the test author can opt to ignore.
                                let stats_value: Option<serde_json::Value> =
                                    if let Some(ref client) = freeze_coord_stats_client {
                                        match client.stats(&[]) {
                                            Ok(v) => Some(v),
                                            Err(e) => {
                                                tracing::debug!(
                                                    target: "ktstr::failure_dump",
                                                    %tag,
                                                    error = %e,
                                                    "freeze-coord: periodic stats request \
                                                     failed; bundling None into Sample"
                                                );
                                                None
                                            }
                                        }
                                    } else {
                                        None
                                    };
                                // Sample timestamp anchor = the moment
                                // the stats request COMPLETED (or
                                // failed). Captured AFTER the stats
                                // client returns so the value
                                // reflects when the running
                                // scheduler's stats were observed,
                                // NOT when we entered the
                                // periodic-fire branch. Stats and
                                // BPF freeze can be ~50 ms apart;
                                // the stats-completion timestamp is
                                // the authoritative anchor for the
                                // sample because the JSON content
                                // was observed at this instant. The
                                // BPF state captured by the freeze
                                // that follows is observed up to
                                // FREEZE_RENDEZVOUS_TIMEOUT later.
                                //
                                // Pause-adjusted: subtract cumulative
                                // ScenarioPause/Resume pause time and
                                // any in-flight pause currently
                                // running, mirroring the boundary
                                // check above. Without this, a
                                // scenario that pauses (e.g. for a
                                // multi-second on-demand capture)
                                // would advance the elapsed_ms
                                // anchor through the pause window
                                // and the temporal patterns would
                                // see false-positive rate drops as
                                // the workload appears to "skip" a
                                // window of progress.
                                let anchor_raw_now_ns =
                                    u64::try_from(run_start.elapsed().as_nanos())
                                        .unwrap_or(u64::MAX);
                                let anchor_cumulative_pause =
                                    scenario_pause_cumulative_for_coord
                                        .load(Ordering::Acquire);
                                let anchor_in_flight_pause_at =
                                    watchdog_pause_for_coord.load(Ordering::Acquire);
                                let anchor_in_flight_pause =
                                    if anchor_in_flight_pause_at > 0 {
                                        anchor_raw_now_ns
                                            .saturating_sub(anchor_in_flight_pause_at)
                                    } else {
                                        0
                                    };
                                let sample_elapsed_ms_anchor = anchor_raw_now_ns
                                    .saturating_sub(anchor_cumulative_pause)
                                    .saturating_sub(anchor_in_flight_pause)
                                    / 1_000_000;
                                let freeze_start = Instant::now();
                                let on_demand = freeze_and_capture(false);
                                thaw_and_barrier();
                                extend_watchdog_for_freeze(freeze_start);
                                match on_demand {
                                    LateCaptureOutcome::Captured(report, capture_start) => {
                                    let map_count = report.maps.len();
                                    let vcpu_regs_count = report.vcpu_regs.len();
                                    let tasks_enriched = report.task_enrichments.len();
                                    if freeze_coord_dump_path.is_some() {
                                        match serde_json::to_string(&report) {
                                            Ok(json) => {
                                                let _ = write_to_tagged_path(
                                                    freeze_coord_dump_path.as_deref(),
                                                    &tag,
                                                    &json,
                                                    || {
                                                        format!(
                                                            "{SNAPSHOT_SUMMARY_PREFIX} (on-demand periodic, write failed): idx={next_periodic_idx} tag={tag} map_count={map_count} vcpu_regs_count={vcpu_regs_count} tasks_enriched={tasks_enriched} elapsed_ms={} json_bytes={}",
                                                            capture_start.elapsed().as_millis() as u64,
                                                            json.len(),
                                                        )
                                                    },
                                                    "freeze-coord: on-demand periodic dump file write failed",
                                                );
                                            }
                                            Err(e) => tracing::error!(
                                                error = %e,
                                                map_count,
                                                vcpu_regs_count,
                                                "freeze-coord: on-demand periodic dump (JSON serialization failed)"
                                            ),
                                        }
                                    }
                                    let elapsed_ms =
                                        capture_start.elapsed().as_millis() as u64;
                                    tracing::info!(
                                        target: "ktstr::failure_dump",
                                        kind = "periodic",
                                        idx = next_periodic_idx,
                                        %tag,
                                        map_count,
                                        vcpu_regs_count,
                                        tasks_enriched,
                                        elapsed_ms,
                                        stats_present = stats_value.is_some(),
                                        sample_elapsed_ms = sample_elapsed_ms_anchor,
                                        "freeze-coord: periodic snapshot captured"
                                    );
                                    freeze_coord_snapshot_bridge.store_with_stats(
                                        &tag,
                                        report,
                                        stats_value,
                                        Some(sample_elapsed_ms_anchor),
                                    );
                                    // Successful capture resets the
                                    // consecutive-timeout counter so
                                    // a transient rendezvous miss
                                    // does not arm the abandon
                                    // threshold for unrelated future
                                    // boundaries.
                                    periodic_consecutive_timeouts = 0;
                                    }
                                    LateCaptureOutcome::Degraded(degraded) => {
                                        // Periodic rendezvous timed out.
                                        // Preserve the structured trigger
                                        // state by writing the degraded JSON
                                        // to the periodic tagged path AND
                                        // storing a placeholder on the
                                        // bridge that carries the partial
                                        // vcpu_regs + the degraded reason so
                                        // a temporal-pattern consumer sees
                                        // why the periodic sample is missing
                                        // BPF data.
                                        if freeze_coord_dump_path.is_some() {
                                            match serde_json::to_string(degraded.as_ref()) {
                                                Ok(json) => {
                                                    let _ = write_to_tagged_path(
                                                        freeze_coord_dump_path.as_deref(),
                                                        &tag,
                                                        &json,
                                                        || {
                                                            format!(
                                                                "{SNAPSHOT_SUMMARY_PREFIX} (on-demand periodic Degraded, write failed): idx={next_periodic_idx} tag={tag} reason={:?} json_bytes={}",
                                                                degraded.reason,
                                                                json.len(),
                                                            )
                                                        },
                                                        "freeze-coord: on-demand periodic degraded dump file write failed",
                                                    );
                                                }
                                                Err(e) => tracing::error!(
                                                    error = %e,
                                                    "freeze-coord: on-demand periodic degraded dump (JSON serialization failed)"
                                                ),
                                            }
                                        }
                                        tracing::warn!(
                                            idx = next_periodic_idx,
                                            %tag,
                                            reason = %degraded.reason,
                                            "freeze-coord: periodic capture degraded (rendezvous timeout); storing placeholder + degraded file"
                                        );
                                        let placeholder = crate::monitor::dump::FailureDumpReport {
                                            vcpu_regs: degraded.vcpu_regs.clone(),
                                            ..crate::monitor::dump::FailureDumpReport::placeholder(
                                                degraded.reason.clone(),
                                            )
                                        };
                                        freeze_coord_snapshot_bridge.store_with_stats(
                                            &tag,
                                            placeholder,
                                            stats_value,
                                            Some(sample_elapsed_ms_anchor),
                                        );
                                        periodic_consecutive_timeouts =
                                            periodic_consecutive_timeouts.saturating_add(1);
                                    }
                                    LateCaptureOutcome::Suppressed => {
                                        // Unreachable: gate only runs with
                                        // gate_on_exit_kind=true and the
                                        // periodic path always passes false.
                                        tracing::warn!(
                                            idx = next_periodic_idx,
                                            %tag,
                                            "freeze-coord: periodic capture unexpectedly suppressed by gate"
                                        );
                                        let placeholder =
                                            crate::monitor::dump::FailureDumpReport::placeholder(
                                                "periodic capture suppressed (gate decision)",
                                            );
                                        freeze_coord_snapshot_bridge.store_with_stats(
                                            &tag,
                                            placeholder,
                                            stats_value,
                                            Some(sample_elapsed_ms_anchor),
                                        );
                                    }
                                }
                                freeze_coord_on_demand_in_flight
                                    .store(false, Ordering::Release);
                                next_periodic_idx =
                                    next_periodic_idx.saturating_add(1);
                                // Publish the live fire count so
                                // run_vm can read it after the
                                // coordinator joins and forward
                                // onto VmResult::periodic_fired.
                                periodic_fired_for_coord.store(
                                    next_periodic_idx,
                                    Ordering::Relaxed,
                                );
                                // After PERIODIC_TIMEOUT_ABANDON_THRESHOLD
                                // consecutive rendezvous timeouts the
                                // remaining boundaries are unlikely
                                // to produce useful captures — every
                                // fire costs up to
                                // FREEZE_RENDEZVOUS_TIMEOUT (30 s)
                                // of wall-clock waiting on a wedged
                                // guest. Set the abandon flag and
                                // break the inner drain; the outer
                                // periodic guard short-circuits on
                                // the next iteration.
                                if periodic_consecutive_timeouts
                                    >= PERIODIC_TIMEOUT_ABANDON_THRESHOLD
                                    && !periodic_abandoned
                                {
                                    let remaining = boundaries
                                        .len()
                                        .saturating_sub(next_periodic_idx as usize);
                                    tracing::warn!(
                                        target: "ktstr::failure_dump",
                                        consecutive_timeouts =
                                            periodic_consecutive_timeouts,
                                        threshold =
                                            PERIODIC_TIMEOUT_ABANDON_THRESHOLD,
                                        remaining_boundaries = remaining,
                                        "freeze-coord: periodic capture abandoned \
                                         after {} consecutive rendezvous timeouts \
                                         ({} boundaries skipped)",
                                        periodic_consecutive_timeouts,
                                        remaining,
                                    );
                                    periodic_abandoned = true;
                                    break;
                                }
                            }
                        }
                    }
                    // After every TLV-driven snapshot dispatch path
                    // runs, also service any user-watchpoint hits on
                    // slots 1..=3.
                    // The vCPU's KVM_EXIT_DEBUG handler latches the
                    // matching slot's `hit` flag and writes hit_evt;
                    // the coordinator's epoll fires WATCHPOINT, the
                    // hit_evt drain at the top of the loop already
                    // ran. Walk every slot and dispatch a capture
                    // for each hit.
                    for slot_idx in 0..3 {
                        if !freeze_coord_watchpoint.user[slot_idx]
                            .hit
                            .swap(false, Ordering::AcqRel)
                        {
                            continue;
                        }
                        let tag = freeze_coord_watchpoint.user[slot_idx]
                            .tag
                            .lock()
                            .unwrap_or_else(|e| e.into_inner())
                            .clone();
                        if freeze_coord_on_demand_in_flight
                            .swap(true, Ordering::AcqRel)
                        {
                            // A capture is already in flight (e.g.
                            // a CAPTURE-class TLV request still
                            // holds the gate). Re-arm the slot's
                            // hit flag so a subsequent iteration
                            // services it, and write a fresh
                            // `hit_evt` edge so the outer
                            // `epoll.wait` wakes promptly — the
                            // hit_evt drain at the top of this
                            // iteration consumed the original wake,
                            // and without a new edge the re-armed
                            // hit could sit for the full
                            // POLL_TIMEOUT_MS before re-inspection.
                            // `continue` (rather than `break`) so
                            // OTHER slots in the same iteration
                            // still get checked — each slot's
                            // `hit` is independent (per-slot
                            // hardware watchpoint dispatch), so a
                            // gate-blocked slot N must not strand
                            // an unrelated fire on slot N+1
                            // waiting for the next iteration's
                            // wake. The outer loop's next iteration
                            // re-evaluates the gate and either
                            // services the re-armed slot or hits
                            // the same in-flight branch and
                            // re-arms again — bounded by the
                            // single-threaded freeze coordinator's
                            // serial dispatch of CAPTURE/WATCH,
                            // which always clears the gate before
                            // returning here.
                            freeze_coord_watchpoint.user[slot_idx]
                                .hit
                                .store(true, Ordering::Release);
                            let _ = freeze_coord_watchpoint.hit_evt.write(1);
                            continue;
                        }
                        tracing::info!(
                            slot_idx,
                            %tag,
                            "freeze-coord: user watchpoint fire; capturing"
                        );
                        // User watchpoint has no while-frozen work,
                        // so thaw immediately.
                        let on_demand = freeze_and_capture(false);
                        thaw_and_barrier();
                        match on_demand {
                            LateCaptureOutcome::Captured(report, capture_start) => {
                            let map_count = report.maps.len();
                            // File mirror via `&report` (no clone),
                            // then move the report into the bridge.
                            // See the CAPTURE-class TLV handler
                            // above for the full rationale on the
                            // serialize-then-store ordering and the
                            // `to_string` vs `to_string_pretty`
                            // tradeoff.
                            if freeze_coord_dump_path.is_some() {
                                match serde_json::to_string(&report) {
                                    Ok(json) => {
                                        let _ = write_to_tagged_path(
                                            freeze_coord_dump_path.as_deref(),
                                            &tag,
                                            &json,
                                            || {
                                                format!(
                                                    "{SNAPSHOT_SUMMARY_PREFIX} (on-demand user-watchpoint, write failed): slot_idx={slot_idx} tag={tag} map_count={map_count} elapsed_ms={} json_bytes={}",
                                                    capture_start.elapsed().as_millis() as u64,
                                                    json.len(),
                                                )
                                            },
                                            "freeze-coord: on-demand user-watchpoint dump file write failed",
                                        );
                                    }
                                    Err(e) => tracing::error!(
                                        error = %e,
                                        map_count,
                                        "freeze-coord: on-demand user-watchpoint dump (JSON serialization failed)"
                                    ),
                                }
                            }
                            let elapsed_ms =
                                capture_start.elapsed().as_millis() as u64;
                            tracing::info!(
                                target: "ktstr::failure_dump",
                                kind = "user_watchpoint",
                                slot_idx,
                                %tag,
                                map_count,
                                elapsed_ms,
                                "freeze-coord: user-watchpoint snapshot captured"
                            );
                            freeze_coord_snapshot_bridge.store(&tag, report);
                            }
                            LateCaptureOutcome::Degraded(degraded) => {
                                // User-watchpoint rendezvous timeout.
                                // Preserve the trigger state by writing
                                // the degraded JSON to the tagged path
                                // AND storing a placeholder carrying
                                // partial vcpu_regs + the degraded
                                // reason so a test that registered
                                // `Op::WatchSnapshot` reads a
                                // structured entry from the bridge
                                // instead of silently missing data.
                                if freeze_coord_dump_path.is_some() {
                                    match serde_json::to_string(degraded.as_ref()) {
                                        Ok(json) => {
                                            let _ = write_to_tagged_path(
                                                freeze_coord_dump_path.as_deref(),
                                                &tag,
                                                &json,
                                                || {
                                                    format!(
                                                        "{SNAPSHOT_SUMMARY_PREFIX} (on-demand user-watchpoint Degraded, write failed): slot_idx={slot_idx} tag={tag} reason={:?} json_bytes={}",
                                                        degraded.reason,
                                                        json.len(),
                                                    )
                                                },
                                                "freeze-coord: on-demand user-watchpoint degraded dump file write failed",
                                            );
                                        }
                                        Err(e) => tracing::error!(
                                            error = %e,
                                            "freeze-coord: on-demand user-watchpoint degraded dump (JSON serialization failed)"
                                        ),
                                    }
                                }
                                tracing::warn!(
                                    slot_idx,
                                    %tag,
                                    reason = %degraded.reason,
                                    "freeze-coord: user-watchpoint capture degraded (rendezvous timeout); storing placeholder + degraded file"
                                );
                                let placeholder = crate::monitor::dump::FailureDumpReport {
                                    vcpu_regs: degraded.vcpu_regs.clone(),
                                    ..crate::monitor::dump::FailureDumpReport::placeholder(
                                        degraded.reason.clone(),
                                    )
                                };
                                freeze_coord_snapshot_bridge.store(&tag, placeholder);
                            }
                            LateCaptureOutcome::Suppressed => {
                                // Unreachable: user-watchpoint passes
                                // gate_on_exit_kind=false. Defensive:
                                // publish a placeholder so the test
                                // sees an entry rather than a missing
                                // snapshot.
                                tracing::warn!(
                                    slot_idx,
                                    %tag,
                                    "freeze-coord: user-watchpoint capture unexpectedly suppressed by gate"
                                );
                                let placeholder =
                                    crate::monitor::dump::FailureDumpReport::placeholder(
                                        "user-watchpoint capture suppressed (gate decision)",
                                    );
                                freeze_coord_snapshot_bridge.store(&tag, placeholder);
                            }
                        }
                        // Release the slot for future arm requests.
                        // `arm_user_watchpoint` finds a free slot by
                        // `request_kva.load(Acquire) == 0`; without
                        // clearing here every fire permanently consumes
                        // its slot, exhausting the cap of three after
                        // three captures and rejecting subsequent
                        // `Op::WatchSnapshot` arms with "no free slot".
                        // Clear `request_kva` and `tag` together so
                        // `arm_user_watchpoint`'s tag publish ordering
                        // (tag first, then `request_kva` Release) sees
                        // a clean slot; vCPU `self_arm_watchpoint` calls
                        // observe the zeroed `request_kva` next iteration
                        // and re-issue `KVM_SET_GUEST_DEBUG` without
                        // this slot's DR/WCR enable so the now-stale
                        // KVA stops trapping. `Release` pairs with the
                        // `Acquire` in `arm_user_watchpoint`'s free-slot
                        // search and the per-vCPU `self_arm_watchpoint`
                        // load.
                        {
                            let mut tag_guard = freeze_coord_watchpoint
                                .user[slot_idx]
                                .tag
                                .lock()
                                .unwrap_or_else(|e| e.into_inner());
                            tag_guard.clear();
                        }
                        freeze_coord_watchpoint.user[slot_idx]
                            .request_kva
                            .store(0, Ordering::Release);
                        freeze_coord_on_demand_in_flight
                            .store(false, Ordering::Release);
                    }
                    // Once the late snapshot has been emitted, the
                    // coordinator's only remaining job is to keep
                    // the freeze=false invariant clear, service
                    // any pending TLV snapshot requests, and wait
                    // for teardown. Skip the error-trigger paths
                    // below; the next `epoll.wait` at the top of
                    // the loop blocks until kill / bsp_done /
                    // virtio-console TX / watchpoint / scanner
                    // tick — no separate sleep cadence needed.
                    // Goes AFTER the snapshot-request dispatch so
                    // on-demand captures still service post-Done.
                    if freeze_state == FreezeState::Done {
                        continue;
                    }
                    // Early-snapshot trigger: dual_snapshot mode and
                    // we have a working scan context. Mirror the
                    // kernel's `check_rq_for_timeouts` logic — any
                    // task whose `jiffies - p->scx.runnable_at`
                    // exceeds the half-way mark trips the trigger.
                    // Half-way comes from the configured
                    // watchdog_timeout (already plumbed through
                    // `KtstrTestEntry.watchdog_timeout`), so the
                    // early snapshot lands well before the kernel
                    // would emit SCX_EXIT_ERROR_STALL — gives the
                    // operator pre-stall BPF state to diff against
                    // the late snapshot.
                    if scan_tick
                        && freeze_state == FreezeState::Idle
                        && freeze_coord_dual_snapshot
                        && half_threshold_jiffies > 0
                        && let Some(ref ctx) = scan_ctx
                        && let Some(ref mem) = freeze_coord_mem
                    {
                        let jiffies = mem.read_u64(ctx.jiffies_64_pa, 0);
                        let max_age = crate::monitor::runnable_scan::max_runnable_age(
                            mem,
                            ctx.scx_tasks_kva,
                            &ctx.rq_pas,
                            &ctx.offsets,
                            jiffies,
                            ctx.walk,
                            ctx.watchdog_timestamp_pa,
                            ctx.start_kernel_map,
                            ctx.phys_base,
                        );
                        // Track scan trajectory for the diagnostic
                        // logged when err_triggered fires before the
                        // early path captures. peak survives across
                        // iterations even when each individual
                        // max_age dips back to 0 (a task on the list
                        // gets dispatched between two polls), so an
                        // operator viewing the post-hoc warn sees the
                        // closest the run came to tripping the
                        // threshold.
                        early_scan_iters = early_scan_iters.wrapping_add(1);
                        if max_age > early_peak_max_age_jiffies {
                            early_peak_max_age_jiffies = max_age;
                        }
                        if max_age >= half_threshold_jiffies
                            && !freeze_coord_bsp_done.load(Ordering::Acquire)
                        {
                            tracing::info!(
                                max_age,
                                half_threshold_jiffies,
                                "freeze-coord: dual-snapshot early threshold tripped"
                            );
                            // Persist the trigger metric and the
                            // half-way threshold ONLY when the freeze
                            // capture succeeds. The
                            // `DualFailureDumpReport` doc says "Zero
                            // when `early` is `None`", which a
                            // consumer relies on to detect the
                            // capture-failed case from JSON alone:
                            // a `late`-only wrapper with non-zero
                            // metric values would be ambiguous (did
                            // the early capture fail, or did the
                            // trigger never fire?). Co-gating both
                            // sides on `Some(report)` keeps the
                            // invariant.
                            // Early-trigger only persists the report;
                            // the timing summary line is emitted at
                            // the late-trigger emit_json site (which
                            // is where JSON serialisation happens).
                            // Discarding the early `_capture_start`
                            // avoids a separate timing log for the
                            // early path that would not include
                            // json_bytes.
                            // Early trigger uses runnable_at age as
                            // its precondition; exit_kind has not
                            // necessarily been written yet, so pass
                            // `false` to skip the gate. Early
                            // snapshot has no while-frozen work, so
                            // thaw immediately after the dump
                            // returns. A degraded outcome here
                            // (rendezvous timed out during the early
                            // capture) must surface structured
                            // diagnostics rather than be dropped. The
                            // late path will follow up
                            // (FreezeState::TookEarly → Late) so the
                            // dual-snapshot wrapper can still emit a
                            // Single+Degraded composite from the late
                            // arm; here we write a tagged degraded
                            // JSON so the early-half trigger evidence
                            // survives to disk.
                            match freeze_and_capture(false) {
                                LateCaptureOutcome::Captured(report, _capture_start) => {
                                    early_max_age_jiffies = max_age;
                                    early_threshold_jiffies = half_threshold_jiffies;
                                    early_guard.snapshot = Some(report);
                                }
                                LateCaptureOutcome::Degraded(degraded) => {
                                    // Stash the degraded reason so the
                                    // late-arm's early_skipped_reason
                                    // calculator can surface it via the
                                    // dual wrapper. Clone before the
                                    // tagged-write block consumes
                                    // `degraded.reason` via `{:?}` and
                                    // the warn's `%` formatting.
                                    early_degraded_reason =
                                        Some(degraded.reason.clone());
                                    if freeze_coord_dump_path.is_some() {
                                        match serde_json::to_string(degraded.as_ref()) {
                                            Ok(json) => {
                                                let _ = write_to_tagged_path(
                                                    freeze_coord_dump_path.as_deref(),
                                                    crate::monitor::dump::SNAPSHOT_TAG_EARLY_DEGRADED,
                                                    &json,
                                                    || {
                                                        format!(
                                                            "{SNAPSHOT_SUMMARY_PREFIX} (on-demand early-snapshot Degraded, write failed): tag={} reason={:?} json_bytes={}",
                                                            crate::monitor::dump::SNAPSHOT_TAG_EARLY_DEGRADED,
                                                            degraded.reason,
                                                            json.len(),
                                                        )
                                                    },
                                                    "freeze-coord: on-demand early-snapshot degraded dump file write failed",
                                                );
                                            }
                                            Err(e) => tracing::error!(
                                                error = %e,
                                                "freeze-coord: on-demand early-snapshot degraded dump (JSON serialization failed)"
                                            ),
                                        }
                                    }
                                    tracing::warn!(
                                        reason = %degraded.reason,
                                        "freeze-coord: early-snapshot degraded (rendezvous timeout); late path will retry"
                                    );
                                }
                                LateCaptureOutcome::Suppressed => {
                                    // Symmetric with the other three
                                    // `LateCaptureOutcome::Suppressed`
                                    // arms in this file (TLV CAPTURE
                                    // / periodic / user-watchpoint
                                    // dispatch sites — grep
                                    // `LateCaptureOutcome::Suppressed`
                                    // to find them), which all log +
                                    // continue rather than panic.
                                    // Suppressed is only producible by
                                    // `freeze_and_capture(true)` (the
                                    // exit_kind gate path); the early
                                    // path passes false so this arm is
                                    // unreachable under the current
                                    // invariant. But a future refactor
                                    // changing the bool default — or
                                    // any flag-passing bug — would
                                    // crash the entire VM here instead
                                    // of degrading gracefully. Log
                                    // loudly + continue: early_snapshot
                                    // stays None for this cycle, the
                                    // late path runs normally. `warn`
                                    // matches the other three sites'
                                    // "defensive log + continue" level
                                    // so a monitoring rule catches all
                                    // four uniformly.
                                    tracing::warn!(
                                        "freeze-coord: invariant violated — early-snapshot freeze_and_capture(false) returned Suppressed (expected only when gate_on_exit_kind=true). Continuing without early snapshot."
                                    );
                                }
                            }
                            thaw_and_barrier();
                            freeze_state = FreezeState::TookEarly;
                        }
                    }
                    // Late-snapshot trigger: err_exit_detected has
                    // flipped. The state-machine guard ensures we
                    // only fire once per VM run — TookEarly → late
                    // is allowed (capturing both halves of the
                    // dual-snapshot wrapper); Done is terminal.
                    if err_triggered
                        && (freeze_state == FreezeState::Idle
                            || freeze_state == FreezeState::TookEarly)
                    {
                        tracing::info!(
                            "freeze-coord: ktstr_err_exit_detected latched, freezing vCPUs"
                        );
                        // When dual-snapshot mode is on but the early
                        // path never captured, surface why so the
                        // operator can act without re-running with
                        // RUST_LOG=ktstr=debug. The three diagnoses
                        // (no scan_ctx, scan ran but always-zero,
                        // scan ran but never crossed threshold) map
                        // to distinct fixes: the first points at
                        // missing kernel symbols / BTF, the second
                        // points at offset/translation bugs in the
                        // scan, the third points at err-class exits
                        // that aren't watchdog stalls (where there
                        // is no half-way state to capture). The warn
                        // fires only when state is genuinely Idle —
                        // a successful TookEarly path has already
                        // logged at info level above.
                        if freeze_coord_dual_snapshot
                            && freeze_state == FreezeState::Idle
                        {
                            tracing::warn!(
                                early_scan_iters,
                                early_peak_max_age_jiffies,
                                half_threshold_jiffies,
                                scan_ctx_resolved = scan_ctx.is_some(),
                                "freeze-coord: dual-snapshot late firing without \
                                 early — runnable_at scan never crossed half-way \
                                 threshold (peak_max_age vs half_threshold tells \
                                 you which case: 0 peak with 0 iters = scan_ctx \
                                 unresolved; 0 peak with non-zero iters = scan ran \
                                 but found no aged tasks; non-zero peak under \
                                 threshold = err-class exit fired before stall \
                                 progressed past half-way)"
                            );
                        }
                        // Gate the dump on `*scx_root->exit_kind`
                        // when the watchpoint was the trigger. The
                        // hardware watchpoint catches every write,
                        // including transient init/teardown writes
                        // setting kind to NONE/DONE; gating on
                        // `kind >= 1024` (SCX_EXIT_ERROR boundary)
                        // suppresses those false positives. The BPF
                        // bss path is its own gate (the tp_btf
                        // handler only latches on error-class kinds),
                        // so when bss alone fired the gate is
                        // redundant and we let the dump run
                        // unconditionally — `bss_state == Triggered`
                        // already proves kind >= 1024.
                        let watchpoint_only_trigger =
                            compute_watchpoint_only_trigger(
                                watchpoint_hit, bss_state,
                            );
                        let late_capture =
                            freeze_and_capture(watchpoint_only_trigger);
                        // Late-trigger backstop: while guest memory
                        // is still quiesced (vCPUs parked, virtio-blk
                        // worker paused, freeze flag still set), do a
                        // final runnable_at scan and — if it crosses
                        // the threshold and the early snapshot never
                        // captured — clone the just-captured late
                        // report into the early slot. The early and
                        // late slots end up as identical snapshots in
                        // that case, but the wrapper's
                        // `early_max_age_jiffies` /
                        // `early_threshold_jiffies` fields tell the
                        // consumer the trigger condition was met at
                        // freeze time, and the wrapper Display
                        // surfaces "early=present" rather than
                        // "early=absent" so an operator inspecting a
                        // stall dump sees the runnable_at evidence
                        // even when the host coordinator's poll
                        // cadence missed the half-way crossing.
                        //
                        // The backstop runs unconditionally on a
                        // quiesced guest — same memory the dump just
                        // captured — so a positive max_age here is
                        // ground truth for "tasks were stuck on
                        // runnable_list at the error-exit instant",
                        // not a transient observation that could have
                        // dipped before the next poll. Functionally
                        // independent of (and complementary to) the
                        // per-poll early trigger above: the per-poll
                        // path captures the half-way moment; the
                        // backstop captures the late-instant ground
                        // truth.
                        let mut backstop_max_age: u64 = 0;
                        if freeze_coord_dual_snapshot
                            && early_guard.snapshot.is_none()
                            && half_threshold_jiffies > 0
                            && let LateCaptureOutcome::Captured(ref late, _) = late_capture
                            && let Some(ref ctx) = scan_ctx
                            && let Some(ref mem) = freeze_coord_mem
                        {
                            let jiffies = mem.read_u64(ctx.jiffies_64_pa, 0);
                            backstop_max_age =
                                crate::monitor::runnable_scan::max_runnable_age(
                                    mem,
                                    ctx.scx_tasks_kva,
                                    &ctx.rq_pas,
                                    &ctx.offsets,
                                    jiffies,
                                    ctx.walk,
                                    ctx.watchdog_timestamp_pa,
                                    ctx.start_kernel_map,
                                    ctx.phys_base,
                                );
                            if backstop_max_age >= half_threshold_jiffies {
                                tracing::info!(
                                    backstop_max_age,
                                    half_threshold_jiffies,
                                    "freeze-coord: late-trigger backstop \
                                     promoting late capture to early slot \
                                     (per-poll early path missed the \
                                     half-way crossing — runnable_at scan \
                                     of frozen guest memory shows the \
                                     stall was real)"
                                );
                                early_guard.snapshot = Some(late.clone());
                                early_max_age_jiffies = backstop_max_age;
                                early_threshold_jiffies = half_threshold_jiffies;
                            }
                        }
                        // Compute the structured early-skip reason
                        // BEFORE thaw, while the relevant state
                        // (peak, threshold, scan_ctx, skip_reason) is
                        // current. The reason is consumed when
                        // building the DualFailureDumpReport below; a
                        // None means "early was captured" or
                        // "single-snapshot mode" — the dual wrapper
                        // serializes None via skip_serializing_if so
                        // a populated `early` keeps the JSON tight.
                        let early_skipped_reason: Option<String> =
                            if !freeze_coord_dual_snapshot
                                || early_guard.snapshot.is_some()
                            {
                                None
                            } else if let Some(reason) =
                                early_degraded_reason.as_deref()
                            {
                                // Early-trigger DID fire (max_age
                                // crossed the half threshold) but
                                // freeze_and_capture(false) returned
                                // Degraded. The early dump landed on
                                // disk at the
                                // SNAPSHOT_TAG_EARLY_DEGRADED sibling
                                // (per the early-Degraded arm above);
                                // surface that here so a dual-wrapper
                                // consumer reading the JSON knows the
                                // structured data exists out-of-band
                                // and the reason field carries the
                                // upstream cause. Branch precedence is
                                // intentional: this fires only when the
                                // early-Degraded arm actually ran, and
                                // ordering BEFORE the
                                // `scan_ctx_skip_reason` branch is safe
                                // because an early-Degraded outcome
                                // implies scan_ctx WAS Some (the
                                // early-trigger guard requires it) —
                                // scan_ctx_skip_reason cannot have
                                // populated.
                                Some(format!(
                                    "early capture degraded ({reason}); \
                                     see tagged sibling at \
                                     SNAPSHOT_TAG_EARLY_DEGRADED"
                                ))
                            } else if let Some(reason) = scan_ctx_skip_reason {
                                Some(format!(
                                    "scan prerequisites unavailable: {reason}"
                                ))
                            } else if early_peak_max_age_jiffies == 0
                                && backstop_max_age == 0
                            {
                                Some(
                                    "scx_tick stall — no per-task \
                                     runnable_at data".to_string(),
                                )
                            } else {
                                Some(format!(
                                    "max_age never crossed threshold \
                                     (peak={early_peak_max_age_jiffies}j, \
                                     threshold={half_threshold_jiffies}j)"
                                ))
                            };
                        // Thaw before emission so a slow JSON
                        // serialise doesn't keep vCPUs parked any
                        // longer than the dump strictly needs. The
                        // backstop above (dual-snapshot only) ran
                        // while still frozen, so the backstop's
                        // runnable_at scan saw the same quiesced
                        // memory the dump captured — thawing here
                        // is safe because every site that depends
                        // on quiesced state has completed.
                        thaw_and_barrier();
                        // Branch on three outcomes of `freeze_and_capture`:
                        //   Captured(report, _) → emit the full dump,
                        //                         mark Done, kick kill
                        //                         so the run tears down
                        //                         promptly.
                        //   Degraded(boxed)     → rendezvous timed out;
                        //                         emit SCHEMA_DEGRADED
                        //                         JSON with partial
                        //                         vcpu_regs + trigger-
                        //                         state diagnostics
                        //                         (per memory
                        //                         `feedback_no_silent_drops`),
                        //                         then mark Done + kick
                        //                         kill — retrying a
                        //                         timed-out rendezvous
                        //                         under a sticky bss
                        //                         latch would just hit
                        //                         the same timeout.
                        //   Suppressed          → gate cross-reference
                        //                         confirmed clean exit
                        //                         (kind &lt; SCX_EXIT_ERROR
                        //                         AND bss not Triggered);
                        //                         reset `watchpoint.hit`
                        //                         and mark Done. The
                        //                         original three-arm
                        //                         shape (separate "reset
                        //                         hit + keep watching"
                        //                         vs "mark Done")
                        //                         collapsed once the
                        //                         cross-reference
                        //                         eliminated the bss-
                        //                         flipped-during-
                        //                         rendezvous race
                        //                         (Triggered bss now
                        //                         routes through
                        //                         Captured/Degraded
                        //                         upstream).
                        match late_capture {
                            LateCaptureOutcome::Captured(late, capture_start) => {
                                // capture_start anchors the freeze→emit
                                // timing summary; emit_json reads
                                // Instant::now() - capture_start at log
                                // time so it covers serialise + write.
                                let map_count = late.maps.len();
                                let vcpu_regs_count = late.vcpu_regs.len();
                                // Pre-compute vcpu_none_indices so
                                // emit_json's stderr fallback can
                                // surface WHICH vCPUs stalled — same
                                // operator-critical signal the drain's
                                // stderr summary emits. The emit_json
                                // closure receives this as &[usize]
                                // regardless of whether the file write
                                // succeeds; on failure (or no sink)
                                // the indices reach stderr alongside
                                // the structured summary.
                                let vcpu_none_indices = vcpu_none_indices(&late.vcpu_regs);
                                let tasks_enriched = late.task_enrichments.len();
                                let truncated_at_us = late.dump_truncated_at_us;
                                // `to_string` (compact) replaces
                                // `to_string_pretty` to halve
                                // serialization cost on the hot
                                // failure-dump path. JSON consumers
                                // (sidecar tooling, repro probe) all
                                // parse via serde_json which
                                // tolerates either form identically.
                                // Eager `early_guard.snapshot.take()`
                                // here moves the early snapshot into
                                // `dual` BEFORE serde_json runs. If
                                // serde panicked OR returned Err mid-
                                // serialization, the snapshot would
                                // drop with `dual` — panic-unwind
                                // drops it; an Err return exits the
                                // if-block and `dual` drops there too.
                                // Either way `early_guard.snapshot`
                                // ends up None and the Drop is a no-op
                                // — silent loss without even stderr
                                // fallback (the Err arm below surfaces
                                // LATE-capture data only, not the
                                // early snapshot). The type-system
                                // proof that FailureDumpReport
                                // serialization is infallible for the
                                // concrete shape (see test
                                // `failure_dump_report_serialization_is_infallible_for_max_synthetic_input`
                                // in src/monitor/dump/tests.rs) keeps
                                // both paths safe today. A future
                                // field with a fallible Serialize
                                // impl would trip that test first.
                                let json_result = if freeze_coord_dual_snapshot {
                                    let dual = crate::monitor::dump::DualFailureDumpReport {
                                        schema: crate::monitor::dump::SCHEMA_DUAL
                                            .to_string(),
                                        early: early_guard.snapshot.take(),
                                        late,
                                        early_max_age_jiffies,
                                        early_threshold_jiffies,
                                        early_skipped_reason,
                                    };
                                    serde_json::to_string(&dual)
                                } else {
                                    serde_json::to_string(&late)
                                };
                                match json_result {
                                    Ok(json) => emit_json(
                                        &json,
                                        map_count,
                                        vcpu_regs_count,
                                        &vcpu_none_indices,
                                        tasks_enriched,
                                        capture_start.elapsed().as_millis() as u64,
                                        truncated_at_us,
                                    ),
                                    Err(e) => {
                                        // Serialize failure of a
                                        // well-typed FailureDumpReport
                                        // is near-zero probability
                                        // (only non-string-keys in maps
                                        // or custom Serialize errors —
                                        // neither applies here). The
                                        // structured summary is loud-
                                        // emitted to stderr when the
                                        // user wired a sink (so the
                                        // data loss represents a
                                        // captured observation that
                                        // the user asked for); skipped
                                        // when no sink (verifier /
                                        // shell / template paths never
                                        // asked for preservation).
                                        tracing::error!(
                                            error = %e,
                                            map_count,
                                            vcpu_regs_count,
                                            "freeze-coord: failure dump (JSON serialization failed)"
                                        );
                                        if freeze_coord_dump_path.is_some() {
                                            let elapsed_ms = capture_start
                                                .elapsed()
                                                .as_millis()
                                                as u64;
                                            eprintln!(
                                                "{SNAPSHOT_SUMMARY_PREFIX} (Captured, serialize failed): map_count={} vcpu_regs_count={} vcpu_none_indices={:?} tasks_enriched={} elapsed_ms={} serde_error={:?}",
                                                map_count,
                                                vcpu_regs_count,
                                                vcpu_none_indices,
                                                tasks_enriched,
                                                elapsed_ms,
                                                e.to_string(),
                                            );
                                        }
                                    }
                                }
                                freeze_state = FreezeState::Done;
                                // Error-class exit dump complete: tear
                                // the run down immediately rather than
                                // looping back to epoll_wait under EEVDF
                                // fallback for the remainder of the
                                // host-watchdog window. The dump is
                                // already serialized and emitted above,
                                // the probe ringbuf has drained by the
                                // time sched_ext_exit fired, and serial
                                // output is flushed — no useful work
                                // remains in the post-exit window. Set
                                // the run-level kill AtomicBool and kick
                                // the eventfd so the BSP run loop
                                // (kill.load) and this coord loop
                                // (freeze_coord_kill.load at the
                                // top of the `'coord:` while-guard
                                // above) both observe the edge on
                                // the next wake.
                                tracing::info!(
                                    "freeze-coord: kill triggered after \
                                     error-exit dump capture"
                                );
                                freeze_coord_kill
                                    .store(true, Ordering::Release);
                                // The Result is intentionally dropped:
                                // the only failure modes for eventfd
                                // write are EAGAIN (counter at u64::MAX
                                // — implies the wake is already
                                // signaled past saturation) and EBADF
                                // (fd torn down — implies coord is
                                // already exiting). Both shapes mean
                                // "kill is already observed or about
                                // to be"; no recovery is meaningful.
                                let _ = freeze_coord_kill_evt.write(1);
                            }
                            LateCaptureOutcome::Degraded(degraded) => {
                                // Rendezvous timeout: emit the
                                // pre-built degraded JSON via the same
                                // atomic-publish helper used for full
                                // dumps so the consumer either sees a
                                // complete degraded file or nothing —
                                // never a torn write. After emit, mark
                                // Done and kick kill: retrying a
                                // rendezvous that already timed out
                                // would just hit the same timeout
                                // (sticky kernel-side bss latch).
                                let elapsed_ms = degraded.elapsed_ms;
                                match serde_json::to_string(degraded.as_ref()) {
                                    Ok(json) => emit_degraded_json(
                                        &json,
                                        &degraded.reason,
                                        elapsed_ms,
                                    ),
                                    Err(e) => tracing::error!(
                                        error = %e,
                                        reason = %degraded.reason,
                                        "freeze-coord: degraded dump (JSON serialization failed)"
                                    ),
                                }
                                // Late Degraded doesn't invalidate a
                                // Captured early either — the early
                                // was a runnable-age spike observation
                                // while the late is "rendezvous timed
                                // out". Both signals matter to an
                                // operator. The main dump path already
                                // holds the degraded JSON (atomic-
                                // published above), so we drop the
                                // early to a tagged sibling path. The
                                // write routes through the shared
                                // `write_to_tagged_path` helper (atomic
                                // publish + parent-dir fsync + stderr
                                // fallback) — same as the
                                // `LateCaptureOutcome::Degraded` arm of
                                // the early-snapshot dispatch above.
                                // The tag and signal differ: early-
                                // Degraded means the early itself was
                                // Degraded; early-pre-late-degraded
                                // means the early was Captured and the
                                // LATE was Degraded — distinct cases.
                                // Any captured snapshot must reach
                                // disk.
                                //
                                // base_path check FIRST in the let-
                                // chain (no take()), and `.take()` only
                                // AFTER the write succeeds. Two silent-
                                // drop windows closed:
                                //   - Rust let-chain short-circuits
                                //     left-to-right; an early take()
                                //     before the dump_path check would
                                //     consume the snapshot and skip
                                //     the body when dump_path is None.
                                //   - std::fs::write can fail mid-call
                                //     (ENOSPC / EROFS / EACCES). If
                                //     we'd already consumed `early`
                                //     via .take(), the failed write
                                //     would drop the snapshot with
                                //     only a tracing::warn! breadcrumb.
                                //     Holding the snapshot in
                                //     `early_snapshot` until the write
                                //     returns Ok preserves a retry
                                //     path via the end-of-coord drain
                                //     below (different tag, but a
                                //     wrong-tag emit beats a silent
                                //     drop).
                                if freeze_coord_dual_snapshot
                                    && freeze_coord_dump_path.is_some()
                                    && let Some(early) =
                                        early_guard.snapshot.as_ref()
                                {
                                    let write_succeeded = match serde_json::to_string(early) {
                                        Ok(json) => match write_to_tagged_path(
                                            freeze_coord_dump_path.as_deref(),
                                            crate::monitor::dump::SNAPSHOT_TAG_EARLY_PRE_LATE_DEGRADED,
                                            &json,
                                            || {
                                                let vcpu_none = vcpu_none_indices(&early.vcpu_regs);
                                                format!(
                                                    "{SNAPSHOT_SUMMARY_PREFIX} (early-pre-late-degraded, write failed): schema={} vcpu_regs_count={} vcpu_none_indices={:?} maps_count={} tasks_enriched={} json_bytes={}",
                                                    early.schema,
                                                    early.vcpu_regs.len(),
                                                    vcpu_none,
                                                    early.maps.len(),
                                                    early.task_enrichments.len(),
                                                    json.len(),
                                                )
                                            },
                                            "freeze-coord: early-snapshot pre-late-degraded write failed (early retained for end-of-coord drain retry)",
                                        ) {
                                            Ok(Some(tagged)) => {
                                                tracing::info!(
                                                    path = %tagged.display(),
                                                    "freeze-coord: early snapshot preserved alongside degraded late"
                                                );
                                                true
                                            }
                                            Ok(None) => false,
                                            Err(_) => false,
                                        },
                                        Err(e) => {
                                            tracing::error!(
                                                error = %e,
                                                "freeze-coord: early-snapshot pre-late-degraded JSON serialization failed (early retained for end-of-coord drain retry)"
                                            );
                                            false
                                        }
                                    };
                                    if write_succeeded {
                                        // Consume only on success;
                                        // failure leaves the snapshot
                                        // for the end-of-coord drain
                                        // below to retry.
                                        let _ = early_guard.snapshot.take();
                                    } else {
                                        // Record the tag this arm
                                        // INTENDED so the drain's
                                        // retry lands at the correct
                                        // operator-readable path
                                        // rather than the drain's
                                        // default NEVER_FIRED tag
                                        // (which would misrepresent
                                        // this case — late DID fire
                                        // as Degraded).
                                        early_guard.retain_tag = Some(
                                            crate::monitor::dump::SNAPSHOT_TAG_EARLY_PRE_LATE_DEGRADED,
                                        );
                                    }
                                }
                                freeze_state = FreezeState::Done;
                                // warn (not info) — Degraded means the
                                // trigger fired but capture was lossy
                                // (rendezvous timeout); the kill that
                                // follows is forced teardown, not clean
                                // shutdown. Cf. Captured arm above which
                                // uses info for the canonical success
                                // path. Operators monitoring scheduler-
                                // test runs care about the Degraded
                                // class — info would bury it.
                                tracing::warn!(
                                    "freeze-coord: kill triggered after \
                                     degraded dump capture"
                                );
                                freeze_coord_kill
                                    .store(true, Ordering::Release);
                                // The Result is intentionally dropped —
                                // same rationale as the Captured
                                // arm's `freeze_coord_kill_evt.write`
                                // kick above (EAGAIN at counter
                                // saturation = already-signaled;
                                // EBADF = coord exiting). Both
                                // shapes mean "kill is already
                                // observed or about to be"; no
                                // recovery is meaningful.
                                let _ = freeze_coord_kill_evt.write(1);
                            }
                            LateCaptureOutcome::Suppressed => {
                                // Gate cross-reference decided no dump
                                // is warranted: the live
                                // `*scx_root->exit_kind` read is below
                                // SCX_EXIT_ERROR (clean shutdown), and
                                // any BPF `.bss` Triggered observation
                                // was either absent (probe never
                                // latched an error-class exit) or
                                // suppressed via the
                                // `bss_oob_warn_logged` exception
                                // (probe's PA stale post-rebind, so
                                // the cross-reference declined the
                                // override on potentially-recycled
                                // vmalloc bytes — see the `bss_state =
                                // bss_read_state(...)` block inside
                                // `freeze_and_capture` above for the
                                // gate decision). Either way the
                                // scheduler shut down cleanly; no late
                                // failure dump is warranted. Reset the
                                // watchpoint hit so the per-iteration
                                // latch read at the top of the loop
                                // does not re-fire on this stale edge;
                                // mark Done so the run terminates
                                // promptly.
                                //
                                // In dual_snapshot mode, an early-
                                // trigger capture may already be in
                                // hand (runnable_at exceeded half-
                                // threshold during the stall, the
                                // early-trigger
                                // `freeze_and_capture(false)` returned
                                // Captured — see the
                                // `LateCaptureOutcome::Captured` arm
                                // of the early-snapshot dispatch
                                // above). The late gate-Suppressed
                                // decision does NOT invalidate the
                                // early observation — they're
                                // independent events: the early was a
                                // runnable-age spike, the late is the
                                // watchpoint fire interpreted as a
                                // clean exit. Dropping the early
                                // snapshot here would be a silent
                                // drop of captured data.
                                //
                                // Emit to a tagged sibling path rather
                                // than the main dump path. Symmetric
                                // with the Degraded-arm early-pre-
                                // late-degraded emit above: the main
                                // `{stem}.failure-dump.json` is
                                // reserved for "the scheduler had a
                                // failure-class late exit; here's the
                                // dump" semantics — a Suppressed late
                                // means no failure-class late exit
                                // happened, so the main path stays
                                // empty. The tagged sibling carries
                                // the early observation under
                                // [`crate::monitor::dump::SNAPSHOT_TAG_EARLY_ONLY_LATE_SUPPRESSED`]
                                // so an operator browsing the dump
                                // directory immediately sees the case
                                // from the file name.
                                // base_path check FIRST + `.take()`
                                // only after successful write — see
                                // the Degraded arm above for the
                                // full silent-drop rationale (same
                                // pattern applies here).
                                if freeze_coord_dual_snapshot
                                    && freeze_coord_dump_path.is_some()
                                    && let Some(early) =
                                        early_guard.snapshot.as_ref()
                                {
                                    let write_succeeded = match serde_json::to_string(early) {
                                        Ok(json) => match write_to_tagged_path(
                                            freeze_coord_dump_path.as_deref(),
                                            crate::monitor::dump::SNAPSHOT_TAG_EARLY_ONLY_LATE_SUPPRESSED,
                                            &json,
                                            || {
                                                let vcpu_none = vcpu_none_indices(&early.vcpu_regs);
                                                format!(
                                                    "{SNAPSHOT_SUMMARY_PREFIX} (early-only-late-suppressed, write failed): schema={} vcpu_regs_count={} vcpu_none_indices={:?} maps_count={} tasks_enriched={} json_bytes={}",
                                                    early.schema,
                                                    early.vcpu_regs.len(),
                                                    vcpu_none,
                                                    early.maps.len(),
                                                    early.task_enrichments.len(),
                                                    json.len(),
                                                )
                                            },
                                            "freeze-coord: early-only (late suppressed) write failed (early retained for end-of-coord drain retry)",
                                        ) {
                                            Ok(Some(tagged)) => {
                                                tracing::info!(
                                                    path = %tagged.display(),
                                                    "freeze-coord: early snapshot preserved (late suppressed, clean exit)"
                                                );
                                                true
                                            }
                                            Ok(None) => false,
                                            Err(_) => false,
                                        },
                                        Err(e) => {
                                            tracing::error!(
                                                error = %e,
                                                "freeze-coord: early-only (late suppressed) JSON serialization failed (early retained for end-of-coord drain retry)"
                                            );
                                            false
                                        }
                                    };
                                    if write_succeeded {
                                        let _ = early_guard.snapshot.take();
                                    } else {
                                        // See Degraded arm above for
                                        // the retain-tag rationale.
                                        early_guard.retain_tag = Some(
                                            crate::monitor::dump::SNAPSHOT_TAG_EARLY_ONLY_LATE_SUPPRESSED,
                                        );
                                    }
                                }
                                freeze_coord_watchpoint
                                    .hit
                                    .store(false, Ordering::Release);
                                freeze_state = FreezeState::Done;
                            }
                        }
                        continue;
                    }
                    // End of body. Loop back to the `epoll.wait`
                    // at the top, which blocks until any registered
                    // fd fires (kill, bsp_done, virtio-console TX,
                    // watchpoint hit, scanner tick) or
                    // POLL_TIMEOUT_MS elapses.
                    // The watchpoint hit and bss-pa edges are
                    // delivered as eventfd writes from the vCPU
                    // thread, so the trigger latency is bounded by
                    // epoll_wait's microsecond-scale wakeup, NOT by
                    // any host-side polling cadence. Heavy work
                    // (boot-race accessor construction, scan_ctx
                    // resolve, runnable_at scan) remains gated on
                    // `scan_tick`, which only fires on the SCANNER
                    // timerfd edge (every 100 ms).
                }
                // Final drain of any pending user-watchpoint hits.
                // The hot-path for-loop at the end of each
                // coordinator iteration handles slot[i].hit fires
                // synchronously, but two race windows can leave a
                // hit `true` past loop exit:
                //
                //   1. The "already in flight" branch in the
                //      hot-path for-loop re-arms the slot's `hit`
                //      and `break`s when `on_demand_in_flight` is
                //      true on entry. If kill / bsp_done flips
                //      before the next iteration runs, the
                //      re-armed hit is never serviced.
                //   2. A vCPU's `latch_user_hit` Release that
                //      raced the loop exit (kill flipped between
                //      the for-loop terminating and the next
                //      `epoll.wait`).
                //
                // Without this drain the snapshot the test author
                // requested is silently dropped — `Snapshot::watch`
                // produces no entry, which a passing test
                // misinterprets as "the watched address was never
                // written" instead of "the VMM exited before the
                // capture pipeline serviced the fire". Store a
                // "watch-fired-but-coord-exited" placeholder under
                // the slot's tag so the test's lookup gets a
                // distinguishable result. Same minimal-report
                // shape the in-loop "dump prerequisites
                // unavailable" partial path uses, with a
                // dedicated reason string so consumers can tell
                // the two cases apart.
                for slot_idx in 0..freeze_coord_watchpoint.user.len() {
                    if !freeze_coord_watchpoint.user[slot_idx]
                        .hit
                        .swap(false, Ordering::AcqRel)
                    {
                        continue;
                    }
                    let tag = freeze_coord_watchpoint.user[slot_idx]
                        .tag
                        .lock()
                        .unwrap_or_else(|e| e.into_inner())
                        .clone();
                    // Skip the placeholder entirely when the bridge
                    // already has a real report under this tag. The
                    // in-loop dispatch publishes via
                    // `snapshot_bridge.store(&tag, report)`; a vCPU
                    // re-arm of `hit=true` after that successful
                    // publish (e.g. a second guest write to the
                    // watched KVA in the same tag, or a vCPU
                    // dispatch racing the in-loop hit.swap) leaves
                    // the slot's hit flag set at coord exit. Without
                    // this guard the final drain stomps the
                    // already-published real report with a hollow
                    // "coord exited before capture" placeholder,
                    // which a test misinterprets as "the watchpoint
                    // mostly didn't fire" rather than "the watch
                    // fired AND was captured." The has() lookup
                    // takes the bridge mutex briefly; teardown is
                    // single-threaded with no concurrent store
                    // (every vCPU thread joins AFTER this drain
                    // returns), so the check is race-free.
                    if freeze_coord_snapshot_bridge.has(&tag) {
                        tracing::debug!(
                            slot_idx,
                            %tag,
                            "freeze-coord: user-watchpoint fire pending at coord \
                             exit, but the bridge already has a real report under \
                             this tag — skipping placeholder to preserve the \
                             captured report"
                        );
                        continue;
                    }
                    tracing::warn!(
                        slot_idx,
                        %tag,
                        "freeze-coord: user-watchpoint fire pending at coord exit; \
                         storing placeholder report (no capture possible during \
                         teardown — vCPU rendezvous would race teardown joins)"
                    );
                    let placeholder = crate::monitor::dump::FailureDumpReport::placeholder(
                        "coord exited before capture",
                    );
                    freeze_coord_snapshot_bridge.store(&tag, placeholder);
                }
                // Post-drain advisory: vCPU threads (BSP + APs) are
                // still alive at this point — they only join inside
                // `collect_results` after the coord thread closure
                // returns (see `run_vm` join sequencing: coord first
                // via `freeze_coord_handle.join()`, AP threads later
                // via `wait_for_exit` + `handle.join` inside
                // `collect_results`). Any vCPU that calls
                // `latch_user_hit` between the drain loop above and
                // its eventual join will set `hit = true` AND
                // increment `hit_evt`, but the coordinator's epoll
                // is already gone — nothing services that hit. The
                // count of slots whose `request_kva != 0` here is
                // the upper bound on hits that could still be lost
                // (each such slot is currently armed in
                // KVM_SET_GUEST_DEBUG on every vCPU and capable of
                // firing on the next guest write to its KVA). This
                // warn surfaces the observability gap so an operator
                // who finds a missing snapshot in
                // `Snapshot::watch_results` can tell "VMM lost the
                // hit during teardown" from "guest never wrote to
                // the watched KVA". Acquire load is overkill (the
                // armed slot publication uses Release / vCPU
                // self-arm uses Acquire) but cheap.
                let still_armed = freeze_coord_watchpoint
                    .user
                    .iter()
                    .filter(|slot| slot.request_kva.load(Ordering::Acquire) != 0)
                    .count();
                if still_armed > 0 {
                    tracing::warn!(
                        still_armed,
                        "freeze-coord: post-drain teardown advisory — {still_armed} \
                         user-watchpoint slot(s) remain armed on every vCPU at \
                         coord exit. Hits latched by a vCPU between this drain \
                         and the eventual vCPU join in collect_results are NOT \
                         serviced (the coord epoll is already gone). Tests \
                         observing a missing snapshot in Snapshot::watch_results \
                         should treat this warn as evidence that the watched \
                         address WAS written to, just past the host-side \
                         capture window."
                    );
                }
                // Drain any held early_snapshot at coord exit. The
                // late-trigger arms (the three `LateCaptureOutcome`
                // match arms inside the `if err_triggered &&
                // (freeze_state == Idle || freeze_state ==
                // TookEarly)` block above) each take() the early when
                // they fire — but if the late trigger NEVER fires (no
                // err_exit_detected for the full run), the early
                // stays in this closure's local and drops silently
                // when the coord exits. A Captured early IS a real
                // failure-dump observation worth surfacing.
                //
                // Symmetric with the `LateCaptureOutcome::Suppressed`
                // late-trigger arm above in WRITE PATTERN ONLY (both
                // route through the shared `write_to_tagged_path`
                // helper for atomic publish + parent-dir fsync +
                // stderr fallback). Same operator-discoverable tagged
                // sibling discipline. SIGNAL DIFFERS:
                //   - late-Suppressed: late trigger DID fire, the
                //     gate examined `*scx_root->exit_kind` and
                //     decided clean (kind < SCX_EXIT_ERROR). Tag:
                //     `SNAPSHOT_TAG_EARLY_ONLY_LATE_SUPPRESSED`.
                //   - drain (this site): late trigger NEVER fired
                //     for the run. freeze_state stayed at Idle or
                //     TookEarly. Tag:
                //     `SNAPSHOT_TAG_EARLY_ONLY_LATE_NEVER_FIRED`.
                // Different tags so an operator browsing the dump dir
                // distinguishes "scheduler recovered AND reached
                // clean late exit" from "scheduler recovered AND was
                // terminated/killed before reaching the late stage."
                // base_path check FIRST — same silent-drop rationale
                // as the Degraded + Suppressed arms above. The drain
                // logic lives in `EarlySnapshotGuard::drain_to_disk`
                // so the same emit runs on panic-unwind via the
                // guard's Drop. The normal-path call here drains
                // explicitly so a tracing::info! "preserved at coord
                // exit" line surfaces in healthy runs; the Drop fires
                // in the panic-unwind path with the same atomic-
                // publish + stderr-fallback semantics (see the
                // EarlySnapshotGuard doc for the full rationale on
                // retain_tag carry, idempotence, and panic-safety).
                early_guard.drain_to_disk();
                // Flush any partial-frame bytes the bulk_assembler
                // is still buffering back into the device's
                // `port1_tx_buf`. The assembler retains tail bytes
                // when a TLV frame straddles two TX wakes — without
                // this push-back the residual is dropped on the
                // floor when the assembler is dropped at closure
                // exit, and `collect_results`'s end-of-run
                // `drain_bulk` + `parse_tlv_stream` path never sees
                // them. Pushing them back means
                // `collect_results`'s drain returns the residual
                // alongside any bytes the device accumulated after
                // the last coordinator drain, and `parse_tlv_stream`
                // completes the frame.
                let coord_exit_t = std::time::Instant::now();
                eprintln!("CLEANUP: coord loop exited");
                // Periodic-capture teardown summary. When
                // `num_snapshots > 0`, log the fired/total ratio so
                // an operator reading the test's tracing output can
                // tell at a glance whether the periodic-sampling
                // path delivered. Three distinct shapes surface:
                //   * `0/N` with no scenario_start_ns stamp — the
                //     guest never published a CRC-valid
                //     `MSG_TYPE_SCENARIO_START`, so boundaries were
                //     never computed. Most commonly a guest that
                //     crashed mid-boot or a workload that never
                //     reached the host-comms phase.
                //   * `0/N` with scenario_start_ns stamped — the
                //     boundaries were computed but no boundary was
                //     reached (very-short run, kill before first
                //     boundary). The doc on
                //     `KtstrTestEntry::num_snapshots` warns the
                //     test author to assert `>= some_lower_bound`
                //     rather than `== num_snapshots` exactly for
                //     this case.
                //   * `K/N` with `K < N` — the run terminated mid-
                //     sequence. Same best-effort contract; tests
                //     should assert `>= K` not `== N`.
                if freeze_coord_num_snapshots > 0 {
                    let scenario_anchor =
                        scenario_start_ns_for_coord.load(Ordering::Acquire);
                    if scenario_anchor == 0 {
                        tracing::warn!(
                            target: "ktstr::failure_dump",
                            num_snapshots = freeze_coord_num_snapshots,
                            fired = next_periodic_idx,
                            "freeze-coord: 0/{} periodic snapshots fired — \
                             scenario_start_ns never stamped (no CRC-valid \
                             MSG_TYPE_SCENARIO_START observed). The guest most \
                             likely crashed mid-boot or never reached the \
                             host-comms phase; periodic sampling has no anchor",
                            freeze_coord_num_snapshots,
                        );
                    } else {
                        tracing::info!(
                            target: "ktstr::failure_dump",
                            num_snapshots = freeze_coord_num_snapshots,
                            fired = next_periodic_idx,
                            scenario_anchor_ns = scenario_anchor,
                            "freeze-coord: {}/{} periodic snapshots fired",
                            next_periodic_idx,
                            freeze_coord_num_snapshots,
                        );
                    }
                }
                let residual = bulk_assembler.take_residual();
                if !residual.is_empty() {
                    freeze_coord_virtio_con.lock().push_back_bulk(&residual);
                }
                // Drop the borrowed views into the OnceLock-owned
                // accessor pair before joining the init worker. The
                // worker itself never touches these references — it
                // only writes to the OnceLock — but explicitly
                // dropping makes the Arc reference-count transitions
                // visible at one site instead of leaving the borrows
                // implicitly alive across the join.
                let _ = owned_accessor;
                let _ = owned_prog_accessor;
                // Join the accessor-init worker before the closure
                // returns. The worker holds an `Arc<GuestMem>` whose
                // host pointer addresses `vm.guest_mem`; that mapping
                // is dropped right after run_vm joins the freeze
                // coordinator thread (`freeze_coord_handle.join()`
                // in run_vm), so any worker still running past this
                // join would dereference freed memory through stale
                // `Arc<GuestMem>` on its next `try_init_*` retry.
                // The kill flag was flipped by the run-loop tear-down
                // path; the worker honors it between retries and
                // exits within ~100 ms (its sleep interval). On the
                // happy path the worker has long since published the
                // OnceLock and exited, so the join is a no-op.
                if let Some(handle) = accessor_init_handle {
                    let jt = std::time::Instant::now();
                    let _ = handle.join();
                    eprintln!("CLEANUP: accessor-init worker joined {:?}", jt.elapsed());
                }
                // Extract the prog accessor for collect_verifier_stats
                // and stash it in the shared slot so run_vm can pass
                // it to VmRunState.
                {
                    let slot = &prog_accessor_slot_for_coord;
                    let extracted = Arc::try_unwrap(accessors_oncelock)
                        .ok()
                        .and_then(|lock| lock.into_inner())
                        .and_then(|(_map, prog)| prog);
                    *slot.lock().unwrap_or_else(|e| e.into_inner()) = extracted;
                }
                eprintln!("CLEANUP: coord closure done {:?}", coord_exit_t.elapsed());
            })
            .context("spawn freeze coordinator thread")?;

        let watchdog = std::thread::Builder::new()
            .name("vmm-watchdog".into())
            .spawn(move || {
                if let Some(cpu) = wd_service_cpu {
                    pin_current_thread(cpu, "watchdog");
                }
                if rt_watchdog {
                    set_rt_priority(2, "watchdog");
                }
                let hard_deadline = Instant::now() + timeout;
                // Soft phase needs enough headroom for the guest to
                // flush serial and reboot. Skip when timeout < 5s.
                let soft_deadline = if timeout > Duration::from_secs(5) {
                    Some(hard_deadline - Duration::from_secs(3))
                } else {
                    None
                };
                let mut soft_fired = false;
                // Cached scheduler-attach reset deadline. Decoded
                // lazily from `watchdog_reset_for_wd` after the
                // host monitor stores a non-zero value (the
                // moment `*scx_root` flips from null to non-null
                // in guest memory). `None` means the workload's
                // clock has not started yet, so the original
                // `hard_deadline` (counted from VM boot) still
                // applies. Once `Some(reset)`, the effective
                // deadline becomes the reset value
                // (`reset_deadline.unwrap_or(hard_deadline)` — no
                // min clamp), so boot-time delays do not eat
                // into the workload budget. Cached so the per-tick
                // check is a single compare against
                // `effective_deadline` rather than re-decoding
                // the encoded `Duration::from_nanos` form.
                // Computed only when `workload_duration_for_wd`
                // is set; absent, the load is skipped entirely
                // (no workload duration → nothing to reset to).
                let mut reset_deadline: Option<Instant> = None;
                eprintln!("watchdog: started, timeout={timeout:?}");

                // Wake plumbing. `tick_tfd` is a periodic 100 ms
                // timerfd that drives the deadline-progress checks
                // (matches the legacy `thread::sleep(100ms)` cadence
                // exactly). `kill_evt_for_watchdog` and
                // `bsp_done_evt_for_wd` are fast-wake fds bumped by
                // the kill / bsp_done setters so the deadline-arm
                // path runs within microseconds of the flip rather
                // than at the next 100 ms tick. Construction failure
                // for any of these means the watchdog cannot
                // observe wake signals; surface as `tracing::error`
                // and return so the symptom is visible — the
                // deadline-armed BSP still gets kicked by the
                // freeze coordinator's own paths if those fire.
                let mut tick_tfd = match TimerFd::new() {
                    Ok(t) => t,
                    Err(e) => {
                        tracing::error!(err = %e, "watchdog: timerfd_create failed");
                        return;
                    }
                };
                let tick = Duration::from_millis(100);
                if let Err(e) = tick_tfd.reset(tick, Some(tick)) {
                    tracing::error!(err = %e, "watchdog: timerfd_settime failed");
                    return;
                }
                let epoll = match Epoll::new() {
                    Ok(e) => e,
                    Err(e) => {
                        tracing::error!(err = %e, "watchdog: epoll_create1 failed");
                        return;
                    }
                };
                let tick_fd = tick_tfd.as_raw_fd();
                let kill_fd = kill_evt_for_watchdog.as_raw_fd();
                let bsp_done_fd = bsp_done_evt_for_wd.as_raw_fd();
                if let Err(e) = epoll.ctl(
                    ControlOperation::Add,
                    tick_fd,
                    EpollEvent::new(EventSet::IN, tick_fd as u64),
                ) {
                    tracing::error!(err = %e, "watchdog: epoll_ctl add timerfd failed");
                    return;
                }
                if let Err(e) = epoll.ctl(
                    ControlOperation::Add,
                    kill_fd,
                    EpollEvent::new(EventSet::IN, kill_fd as u64),
                ) {
                    tracing::error!(err = %e, "watchdog: epoll_ctl add kill_evt failed");
                    return;
                }
                if let Err(e) = epoll.ctl(
                    ControlOperation::Add,
                    bsp_done_fd,
                    EpollEvent::new(EventSet::IN, bsp_done_fd as u64),
                ) {
                    tracing::error!(err = %e, "watchdog: epoll_ctl add bsp_done_evt failed");
                    return;
                }
                let mut epoll_buf = [EpollEvent::default(); 3];

                loop {
                    if bsp_done_for_wd.load(Ordering::Acquire) {
                        eprintln!("watchdog: BSP done, returning");
                        return;
                    }
                    // Decode a pending scheduler-attach reset
                    // when not already cached. Skip when the
                    // workload duration was not configured (no
                    // distinct workload budget; `hard_deadline`
                    // is the only deadline that matters).
                    if workload_duration_for_wd.is_some() {
                        let stored_ns = watchdog_reset_for_wd.load(Ordering::Acquire);
                        if stored_ns != 0 {
                            let candidate = run_start
                                .checked_add(Duration::from_nanos(stored_ns))
                                .unwrap_or(hard_deadline);
                            if reset_deadline.is_none()
                                || reset_deadline.is_some_and(|prev| candidate > prev)
                            {
                                reset_deadline = Some(candidate);
                                eprintln!(
                                    "watchdog: scheduler attach observed, hard \
                                     deadline reset to {:?} from VM start",
                                    candidate.saturating_duration_since(run_start),
                                );
                            }
                        }
                    }
                    let effective_deadline =
                        reset_deadline.map_or(hard_deadline, |r| r.max(hard_deadline));
                    if kill_for_watchdog.load(Ordering::Acquire)
                        || Instant::now() >= effective_deadline
                    {
                        // Either an AP set kill or hard timeout expired.
                        // Re-check bsp_done: if the BSP already exited its
                        // run loop, the VcpuFd (and kvm_run mmap backing
                        // bsp_ie) may be dropped. Writing to ie after drop
                        // is a use-after-free.
                        if bsp_done_for_wd.load(Ordering::Acquire) {
                            eprintln!("watchdog: BSP already done, returning");
                            return;
                        }
                        let hard_timeout_fired = Instant::now() >= effective_deadline;
                        let reason = if hard_timeout_fired {
                            "hard timeout expired"
                        } else {
                            "kill set by AP"
                        };
                        eprintln!("watchdog: {reason}, kicking BSP");
                        // Actionable diagnostics. Without this dump the
                        // operator-visible failure is just `timed_out =
                        // true` with no clue why. Print the deadline
                        // values (`effective_deadline` is what actually
                        // fired; `hard_deadline` is the original boot-
                        // anchored deadline before any
                        // scheduler-attach reset) plus the
                        // `timeout`/`workload_duration` knobs the
                        // operator can tune, the cause path
                        // (hard-timeout-expired vs kill-set-by-AP),
                        // and whether the deadline was reset. Both
                        // deadlines are rendered as offsets from
                        // `run_start` so the numbers line up with the
                        // wall-clock the operator sees in the test
                        // output. The `kill_set_by_AP` branch also
                        // ages `effective_deadline` against now so
                        // the operator can see how much budget was
                        // unused when the kill arrived.
                        let now = Instant::now();
                        let effective_offset =
                            effective_deadline.saturating_duration_since(run_start);
                        let hard_offset = hard_deadline.saturating_duration_since(run_start);
                        let elapsed = now.saturating_duration_since(run_start);
                        let was_reset = reset_deadline.is_some();
                        eprintln!("watchdog: deadline expired at {elapsed:?} from VM start");
                        eprintln!(
                            "  cause={reason}, hard_timeout_fired={hard_timeout_fired}, \
                             kill_set_by_AP={}",
                            !hard_timeout_fired
                        );
                        eprintln!(
                            "  effective_deadline={effective_offset:?} from VM start \
                             (reset_by_scheduler_attach={was_reset})"
                        );
                        eprintln!("  hard_deadline={hard_offset:?} from VM start (timeout knob)");
                        eprintln!(
                            "  timeout={timeout:?}, workload_duration={:?}",
                            workload_duration_for_wd
                        );
                        eprintln!(
                            "  hint: if the test body needs more wall time, increase \
                             duration (the `duration` field on `KtstrTestEntry` / \
                             `#[ktstr_test(duration_ms = ...)]`); the VM timeout is \
                             derived as max(watchdog_timeout, duration) so raising \
                             duration also extends the host watchdog deadline"
                        );
                        // Set `timed_out` ONLY for the hard-deadline
                        // branch. The "kill set by AP" path is not a
                        // watchdog timeout — propagating it as
                        // `timed_out=true` would mislabel a panic-
                        // driven kill as a deadline expiry.
                        if hard_timeout_fired {
                            timed_out_for_watchdog.store(true, Ordering::Release);
                        }
                        // Propagate kill so handle_freeze's poll loop
                        // exits and the monitor + bpf-write threads stop.
                        kill_for_watchdog.store(true, Ordering::Release);
                        let _ = kill_evt_for_watchdog.write(1);
                        if let Some(ref ie) = bsp_ie {
                            ie.set(1);
                            std::sync::atomic::fence(Ordering::Release);
                        }
                        unsafe {
                            libc::pthread_kill(bsp_tid, vcpu_signal());
                        }
                        eprintln!("watchdog: BSP kicked");
                        return;
                    }
                    // Soft deadline: request graceful shutdown by
                    // pushing `SIGNAL_VC_SHUTDOWN` into virtio-console
                    // RX. The guest's `hvc0_poll_loop` blocks on
                    // `/dev/hvc0` and recognises the byte directly —
                    // no SHM signal slot needed. The BSP keeps running
                    // so the guest can flush serial and reboot
                    // normally.
                    //
                    // Recompute the soft window from the effective
                    // deadline so a scheduler-attach reset shifts
                    // the soft deadline alongside the hard
                    // deadline. The reset can extend past
                    // hard_deadline (no min clamp), so the
                    // recomputed `effective_deadline - 3s` shifts
                    // forward whenever the reset extends; the
                    // guest still gets its 3s flush window
                    // relative to the deadline that actually
                    // fires. Skip when the original
                    // `soft_deadline` was `None` (timeout < 5s;
                    // no soft phase configured) — the reset path
                    // inherits that decision rather than
                    // synthesising a soft phase out of nothing.
                    let effective_soft = soft_deadline
                        .and_then(|_| effective_deadline.checked_sub(Duration::from_secs(3)));
                    if !soft_fired && effective_soft.is_some_and(|d| Instant::now() >= d) {
                        soft_fired = true;
                        eprintln!("watchdog: soft deadline, requesting graceful shutdown");
                        super::host_comms::request_shutdown(&wd_virtio_con);
                    }
                    // Block until the next tick or a kill_evt /
                    // bsp_done_evt write. -1 timeout: deadlines
                    // (hard + soft) are checked at the top of each
                    // iteration after the wake; the 100 ms timerfd
                    // guarantees the loop wakes at least that often
                    // even when no eventfd writes arrive, which
                    // preserves the legacy cadence exactly.
                    match epoll.wait(-1, &mut epoll_buf) {
                        Ok(n) => {
                            for ev in &epoll_buf[..n] {
                                if ev.fd() == tick_fd {
                                    // Drain the timerfd counter so
                                    // the next epoll_wait blocks
                                    // again instead of returning
                                    // immediately on the residual
                                    // ready bit.
                                    let _ = tick_tfd.wait();
                                }
                                // kill_fd / bsp_done_fd: implicitly
                                // drained because the loop body
                                // re-loads the AtomicBool source of
                                // truth on every iteration. The
                                // EventFd counter accumulates but
                                // is harmless — we only care about
                                // the edge.
                            }
                        }
                        Err(e) => {
                            if e.raw_os_error() != Some(libc::EINTR) {
                                tracing::warn!(err = %e, "watchdog: epoll_wait failed");
                                // Fall through to the next iteration
                                // so the deadline check still runs;
                                // a persistent failure is eventually
                                // caught by the hard deadline.
                            }
                        }
                    }
                }
            })
            .context("spawn watchdog thread")?;

        // BSP run loop. Wrapped in the same `with_vcpu_panic_ctx`
        // scope the APs use (symmetric panic-hook signaling) —
        // `kill` plus `bsp_done` are the pair analogous to a
        // vCPU thread's `kill` + `exited` so a BSP panic flips the
        // watchdog-observed flags before the panic=abort teardown.
        // `vcpu_panic::install_once` was already called in
        // `spawn_ap_threads` above, which runs even for a zero-AP VM,
        // so the hook is live by the time BSP enters its loop.
        eprintln!("BSP: entering run loop");
        let (exit_code, timed_out) = vcpu_panic::with_vcpu_panic_ctx(
            vcpu_panic::VcpuPanicCtx {
                kill: kill.clone(),
                exited: bsp_done.clone(),
                kill_evt: Some(kill_evt.clone()),
                exited_evt: Some(bsp_done_evt.clone()),
                // Hand the BSP's `bsp_alive` flag to the panic hook so a
                // panic-unwind path flips it to `false` BEFORE the
                // stack drop unmaps `bsp`'s `kvm_run` page. The
                // normal-exit path's post-join `bsp_alive.store(false)`
                // (see the `collect_results` finalization block) covers
                // `panic = "abort"` and the no-panic path; the panic
                // hook covers `panic = "unwind"` (test profile) where
                // the post-join store is unreachable. Mirrors
                // the AP-side `alive: Some(alive.clone())` plumbing in
                // spawn_ap_threads — every cross-thread holder of a
                // BSP `ImmediateExitHandle` (the freeze coordinator,
                // the watchdog) gates `ie.set` on this flag's
                // Acquire load, and a panic-released Release store
                // happens-before the unwind drop of `bsp`.
                alive: Some(bsp_alive.clone()),
            },
            || {
                self.run_bsp_loop(
                    &mut bsp,
                    &com1,
                    &com2,
                    Some(&virtio_con),
                    virtio_blk.as_ref(),
                    virtio_net.as_ref(),
                    &kill,
                    &freeze,
                    &watchpoint,
                    &bsp_parked,
                    &bsp_regs,
                    has_immediate_exit,
                    run_start,
                    timeout,
                    Some(&parked_evt),
                    Some(&thaw_evt),
                    Some(&kill_evt),
                    tcr_el1_cache.as_ref(),
                    &cr3_cache,
                    &timed_out_flag,
                )
            },
        );
        bsp_done.store(true, Ordering::Release);
        // Wake the freeze coordinator's epoll loop. Failure
        // (counter overflow / EAGAIN under EFD_NONBLOCK) is benign
        // — the panic-hook path may have already pushed an edge,
        // and the AtomicBool above is still authoritative for
        // `freeze_coord_bsp_done.load(Acquire)` if the eventfd
        // fails to deliver.
        let _ = bsp_done_evt.write(1);
        // Stop the monitor (wakes via kill_evt epoll) and bpf-write
        // thread (observes kill on next 200ms poll cycle).
        // Previously kill was deferred to collect_results, leaving
        // the monitor sampling at 100ms cadence through the entire
        // run_vm cleanup window (watchdog join + coord join).
        kill.store(true, Ordering::Release);
        let _ = kill_evt.write(1);
        // Sample cleanup start at the earliest moment after BSP exit so
        // every host-side teardown step lands inside the window, in
        // execution order: watchdog join (immediately below), AP joins,
        // monitor join, BPF writer join, bulk drain, exit-code and
        // crash-message extraction, and verifier-stat read (the rest
        // run inside `collect_results`). `collect_results` reads
        // `Instant::now()` at the end and the difference becomes
        // `VmResult::cleanup_duration`.
        let cleanup_start = Instant::now();
        // `code` here is the run-loop sentinel (0 only on a BSP-
        // observed `ExitAction::Shutdown`, -1 otherwise — see
        // [`BspExitReason`] and the preceding `BSP: loop exit
        // reason=...` line). The caller-visible exit code is
        // derived from bulk-port `MSG_TYPE_EXIT` or the COM2 `KTSTR_EXIT:`
        // sentinel inside [`KtstrVm::collect_results`], not from
        // this value.
        eprintln!(
            "BSP: exited run loop, code={exit_code} timed_out={timed_out} \
             (run-loop sentinel — final exit code comes from bulk port / COM2 in collect_results)"
        );

        // Join the watchdog before dropping `bsp`. The watchdog holds an
        // ImmediateExitHandle pointing into bsp's kvm_run mmap. If bsp is
        // dropped first, the watchdog may write to unmapped memory.
        let _ = watchdog.join();
        eprintln!("CLEANUP: watchdog joined");

        // Join the freeze coordinator BEFORE `bsp` falls out of scope at
        // the end of this function. The coordinator's captured BSP
        // `ImmediateExitHandle` addresses bsp's kvm_run mmap; reachable
        // from multiple paths inside `freeze_and_capture` (TLV-driven
        // CAPTURE, user watchpoint, late-trigger, even after `bsp_done`
        // flips). Without this join, any of those paths can write
        // through a freed kvm_run mapping after bsp drops — a
        // use-after-free with hostile-input semantics.
        //
        // `bsp_done.store(true)` + `bsp_done_evt.write(1)` above
        // (lines around `BSP: exited run loop`) wake the coordinator's
        // epoll loop and break it out of the outer loop on the next
        // iteration, so this join does not deadlock; the watchdog's
        // own kill/bsp_done writes are also covered.
        //
        // Flip `bsp_alive` to `false` AFTER the join completes — at
        // that point the coordinator thread is gone and the gate is
        // belt-and-braces for any future restructuring that could
        // share the BSP IE handle outside this lifecycle.
        let _ = freeze_coord_handle.join();
        eprintln!("CLEANUP: freeze_coord joined");
        bsp_alive.store(false, Ordering::Release);

        // Make sure freeze is cleared before vCPU teardown so the APs
        // don't park-loop after we kick them. The freeze coordinator
        // has already joined above so it cannot re-set freeze=true.

        // Capture the virtio-blk counter Arc before the device's
        // outer `Arc<PiMutex<VirtioBlk>>` falls out of scope. The
        // device's `counters()` accessor clones the inner
        // `Arc<VirtioBlkCounters>`; this transfers a reader-side
        // handle onto `VmRunState` so `collect_results` can attach
        // it to `VmResult` without holding the device alive past
        // its current ownership.
        let virtio_blk_counters = virtio_blk.as_ref().map(|d| d.lock().counters());
        let virtio_net_counters = virtio_net.as_ref().map(|d| d.lock().counters());

        // Best-effort final TCR_EL1 read from the post-exit BSP.
        // The BSP loop's lazy CAS already populates `tcr_el1_cache`
        // via `read_tcr_el1`; this final read covers the (rare)
        // case where the loop exited before the kernel programmed
        // the MMU (early-boot crash). On x86_64 `read_tcr_el1`
        // returns None and the cache stays None.
        if let Some(ref cache) = tcr_el1_cache
            && cache.load(Ordering::Acquire) == 0
            && let Some(val) = exit_dispatch::read_tcr_el1(&mut bsp)
            && val != 0
        {
            cache.store(val, Ordering::Release);
        }
        // Best-effort final CR3 / TTBR1_EL1 read from the post-exit
        // BSP. Mirrors the TCR_EL1 catch-up above: the BSP loop's
        // lazy CAS populates `cr3_cache` once the kernel installs
        // its post-randomization page tables; this catch-up store
        // covers the (rare) case where the loop exited before
        // `__startup_64` / `__cpu_setup` ran. Failure-dump consumers
        // that read `cr3_cache` post-exit (e.g. for late
        // `phys_base` resolution against a frozen VM) get the live
        // CR3 instead of the bootstrap zero.
        if cr3_cache.load(Ordering::Acquire) == 0
            && let Some(val) = exit_dispatch::read_cr3(&mut bsp)
            && val != 0
        {
            cr3_cache.store(val, Ordering::Release);
        }

        Ok(VmRunState {
            exit_code,
            timed_out,
            ap_threads,
            monitor_handle,
            bpf_write_handle,
            // Coordinator is already joined above (before `bsp` drops)
            // to prevent UAF on the BSP `ImmediateExitHandle`.
            // `collect_results`'s `if let Some(h) = ...` join is a
            // no-op for the `None` arm.
            freeze_coordinator: None,
            com1,
            com2,
            kill,
            kill_evt,
            freeze,
            vm,
            cleanup_start,
            virtio_blk_counters,
            virtio_net_counters,
            // Snapshot bridge owning every report stored by the
            // freeze coordinator's TLV-driven snapshot handler
            // over the run's lifetime. Forwarded to
            // `VmResult::snapshot_bridge` by `collect_results`.
            snapshot_bridge,
            tcr_el1: tcr_el1_cache,
            cr3: cr3_cache,
            vmlinux_data: vmlinux_data_for_result,
            prog_accessor: prog_accessor_slot
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .take(),
            kern_phys_base: kern_phys_base_for_result.load(Ordering::Acquire),
            // Virtio-console handle threaded into `collect_results`
            // for the post-exit `drain_bulk()` call. Carries any
            // port-1 TLV bytes the guest wrote that the freeze
            // coordinator's tx_evt-driven mid-run drain did not
            // already consume; the merge into `guest_messages` keeps
            // existing readers (eval.rs, sidecar) working without
            // any per-message-type code change.
            virtio_con,
            // Mid-run TLV entries the freeze coordinator already
            // consumed. `collect_results` merges these with the
            // post-exit bulk drain and the COM2 panic-message
            // extraction so every frame the guest published reaches
            // the verdict.
            bulk_messages: freeze_coord_bulk_messages,
            // Scheduler-stats client constructed at the top of
            // `run_vm`. Its drainer thread has been alive since the
            // guest started forwarding stats responses; the client
            // is threaded onto `VmResult` for test-code access.
            stats_client,
            // Periodic-capture count published by the coordinator
            // run-loop after every successful fire / placeholder
            // store. Read AFTER `freeze_coord_handle.join()` ran so
            // the AtomicU32's value is the final advance count;
            // `collect_results` forwards onto
            // `VmResult::periodic_fired`.
            periodic_fired: periodic_fired_slot.load(Ordering::Relaxed),
            // Configured periodic-target plumbed onto KtstrVm via
            // `KtstrVmBuilder::num_snapshots`. Forwarded to
            // `VmResult::periodic_target` so test code can compute
            // coverage as `fired / target`.
            periodic_target: self.num_snapshots,
            // Watchpoint Arc forwarded so `collect_results` can
            // invalidate `kind_host_ptr` and `request_kva` after
            // every vCPU thread joins but before `vm` drops.
            watchpoint,
        })
    }

    /// Spawn AP vCPU threads. Each thread optionally pins itself to a
    /// host CPU from `pin_targets` (indexed by AP order, 0-based), OR
    /// applies a CPU mask from `no_perf_mask` when the no-perf +
    /// `--cpu-cap` path is active. The two are mutually exclusive —
    /// perf-mode produces `pin_targets` via the PinningPlan;
    /// `--cpu-cap` no-perf produces `no_perf_mask` via the LlcPlan.
    ///
    /// Returns `(threads, freeze_handles)`. The freeze handles
    /// (per-AP `parked` flags + register-snapshot slots) are the
    /// freeze coordinator's view of each AP; they live separately
    /// from `VcpuThread` so the thread struct stays minimal —
    /// `VcpuThread` carries only what teardown (kick + join) needs.
    /// Callers that don't run a freeze coordinator (e.g. interactive
    /// shell) discard `freeze_handles`.
    #[allow(clippy::too_many_arguments)]
    pub(super) fn spawn_ap_threads(
        &self,
        vcpus: Vec<kvm_ioctls::VcpuFd>,
        has_immediate_exit: bool,
        com1: &Arc<PiMutex<console::Serial>>,
        com2: &Arc<PiMutex<console::Serial>>,
        virtio_con: Option<&Arc<PiMutex<virtio_console::VirtioConsole>>>,
        virtio_blk: Option<&Arc<PiMutex<virtio_blk::VirtioBlk>>>,
        virtio_net: Option<&Arc<PiMutex<virtio_net::VirtioNet>>>,
        kill: &Arc<AtomicBool>,
        kill_evt: &Arc<EventFd>,
        freeze: &Arc<AtomicBool>,
        watchpoint: &Arc<WatchpointArm>,
        pin_targets: &[Option<usize>],
        no_perf_mask: Option<&[usize]>,
        ap_tid_slots: &[(Arc<AtomicI32>, Arc<crate::sync::Latch>)],
        parked_evt: Option<&Arc<EventFd>>,
        thaw_evt: Option<&Arc<EventFd>>,
    ) -> Result<(Vec<VcpuThread>, ApFreezeHandles)> {
        // Register the process-wide panic hook that flips `kill` +
        // `exited` on a panicking vCPU thread before the
        // panic=abort-induced process teardown. Idempotent via
        // `Once`; safe to call on every VM spawn.
        vcpu_panic::install_once();
        let n = vcpus.len();
        debug_assert_eq!(ap_tid_slots.len(), n);
        let mut ap_threads: Vec<VcpuThread> = Vec::with_capacity(n);
        let mut freeze_parked: Vec<Arc<AtomicBool>> = Vec::with_capacity(n);
        let mut freeze_regs: Vec<Arc<std::sync::Mutex<Option<exit_dispatch::VcpuRegSnapshot>>>> =
            Vec::with_capacity(n);
        for (i, mut vcpu) in vcpus.into_iter().enumerate() {
            let ie_handle = if has_immediate_exit {
                Some(ImmediateExitHandle::from_vcpu(&mut vcpu))
            } else {
                None
            };
            let kill_clone = kill.clone();
            let kill_evt_clone = kill_evt.clone();
            let freeze_clone = freeze.clone();
            let com1_clone = com1.clone();
            let com2_clone = com2.clone();
            let vc_clone = virtio_con.cloned();
            let vblk_clone = virtio_blk.cloned();
            let vnet_clone = virtio_net.cloned();
            let exited = Arc::new(AtomicBool::new(false));
            let exited_clone = exited.clone();
            let parked = Arc::new(AtomicBool::new(false));
            let parked_clone = parked.clone();
            let regs = Arc::new(std::sync::Mutex::new(None));
            let regs_clone = regs.clone();
            // Per-AP `alive` flag mirroring the BSP `bsp_alive` gate.
            // Initialised to `true`; the AP panic hook (via
            // `VcpuPanicCtx::alive`) flips it to `false` BEFORE
            // unwinding drops `vcpu` and its `kvm_run` mmap, so the
            // freeze coordinator's pass-1 kick loop and the
            // `arm_user_watchpoint` kick gate every `ie.set` on a
            // fresh Acquire load and skip indices whose mmap is
            // about to disappear. Under `panic = "abort"` (release)
            // unwinding never runs and the flag stays `true` for
            // the life of the run; the gate is then a no-op,
            // matching the BSP belt-and-braces semantic.
            let alive = Arc::new(AtomicBool::new(true));
            let has_immediate_exit_clone = has_immediate_exit;
            let pin_cpu = pin_targets.get(i).copied().flatten();
            let mask_for_thread: Option<Vec<usize>> = no_perf_mask.map(|m| m.to_vec());
            // Per-AP shared watchpoint state. Cloned once per AP;
            // the AP polls `wp_clone.request_kva` before each
            // KVM_RUN (via the per-iteration hook in
            // `vcpu_run_loop_unified`) and self-arms via
            // [`self_arm_watchpoint`] when the freeze coordinator
            // publishes the resolved `*scx_root->exit_kind` KVA.
            // The same clone is what the `VcpuExit::Debug` arm in
            // [`exit_dispatch::classify_exit`] uses to set
            // `wp_clone.hit` so the late-trigger poll observes the
            // watchpoint fire.
            let wp_clone = watchpoint.clone();

            let rt = self.performance_mode;
            // Per-AP exit eventfd for `VcpuThread::wait_for_exit` so
            // teardown blocks in `epoll_wait` instead of sleep-polling
            // `exited`. Bumped from inside the closure right after
            // `exited.store(true)` and from the panic hook (via
            // `panic_ctx.exited_evt`) so the parent observes both
            // normal-exit and panic-classified shutdowns through the
            // same fd. EFD_NONBLOCK so a Drop-time write cannot
            // stall.
            let exit_evt =
                Arc::new(EventFd::new(EFD_NONBLOCK).context("create AP vCPU exit eventfd")?);
            let exit_evt_thread = Arc::clone(&exit_evt);
            let panic_ctx = vcpu_panic::VcpuPanicCtx {
                kill: kill.clone(),
                exited: exited.clone(),
                kill_evt: Some(kill_evt.clone()),
                exited_evt: Some(Arc::clone(&exit_evt)),
                // Hand the AP's `alive` flag to the panic hook so a
                // panic-unwind path flips it to `false` BEFORE the
                // stack drop unmaps `vcpu`'s `kvm_run` page. The
                // freeze coordinator's pass-1 kick gates each
                // `ie.set` on this flag's Acquire load.
                alive: Some(alive.clone()),
            };
            let (tid_slot_clone, tid_latch_clone) = {
                let (s, l) = &ap_tid_slots[i];
                (Arc::clone(s), Arc::clone(l))
            };
            // Clone the shared parked_evt + thaw_evt for this AP.
            // None when the caller (interactive shell) doesn't run a
            // freeze coordinator; in that case `vcpu_run_loop_unified`
            // never observes a freeze and the eventfd is unused.
            let parked_evt_clone: Option<Arc<EventFd>> = parked_evt.cloned();
            let thaw_evt_clone: Option<Arc<EventFd>> = thaw_evt.cloned();
            let handle = std::thread::Builder::new()
                .name(format!("vcpu-{}", i + 1))
                .spawn(move || {
                    register_vcpu_signal_handler();
                    // Stamp this thread's Linux TID into the per-AP
                    // slot so the monitor can open `perf_event_open`
                    // counters bound to the vCPU thread. Done
                    // BEFORE pinning / RT / KVM_RUN so the value is
                    // visible to any reader the moment the thread is
                    // schedulable. The companion `Latch::set` lets
                    // `open_vcpu_perf_capture` block in
                    // `Latch::wait_timeout` instead of sleep-polling
                    // the atomic. SAFETY: SYS_gettid is the standard
                    // syscall returning this thread's pid_t; no
                    // inputs.
                    let tid = unsafe { libc::syscall(libc::SYS_gettid) } as i32;
                    tid_slot_clone.store(tid, Ordering::Release);
                    tid_latch_clone.set();
                    if let Some(cpu) = pin_cpu {
                        pin_current_thread(cpu, &format!("vCPU {}", i + 1));
                    } else if let Some(mask) = mask_for_thread.as_deref() {
                        set_thread_cpumask(mask, &format!("vCPU {}", i + 1));
                    }
                    if rt {
                        set_rt_priority(1, &format!("vCPU {}", i + 1));
                    }
                    // The watchpoint Arc travels into the run loop
                    // via the `vcpu_run_loop_unified` parameter; the
                    // loop self-arms before each `vcpu.run()` and
                    // sets `watchpoint.hit` on `KVM_EXIT_DEBUG`. The
                    // per-AP `armed_kva` slot that tracks the
                    // currently-programmed `debugreg[0]` lives
                    // inside the loop now, so a single pre-loop
                    // attempt would have been a redundant ioctl
                    // with no effect — the coordinator typically
                    // publishes the resolved KVA AFTER the AP has
                    // entered the loop (once a sched_ext scheduler
                    // attaches and `*scx_root != 0`).
                    vcpu_panic::with_vcpu_panic_ctx(panic_ctx, || {
                        vcpu_run_loop_unified(
                            &mut vcpu,
                            &com1_clone,
                            &com2_clone,
                            vc_clone.as_ref(),
                            vblk_clone.as_ref(),
                            vnet_clone.as_ref(),
                            &kill_clone,
                            &kill_evt_clone,
                            &freeze_clone,
                            &parked_clone,
                            &regs_clone,
                            &wp_clone,
                            has_immediate_exit_clone,
                            parked_evt_clone.as_ref(),
                            thaw_evt_clone.as_ref(),
                        );
                    });
                    // wp_clone is held for the AP's entire lifetime
                    // so the strong count never drops to zero before
                    // the freeze coordinator joins.
                    drop(wp_clone);
                    exited_clone.store(true, Ordering::Release);
                    // Wake any thread blocked in `wait_for_exit` on
                    // this AP's exit_evt. Failure (counter overflow)
                    // is harmless — a previous edge already unblocks
                    // the waiter; only the edge from 0 to non-zero
                    // matters.
                    let _ = exit_evt_thread.write(1);
                    vcpu
                })
                .with_context(|| format!("spawn vCPU {} thread", i + 1))?;

            ap_threads.push(VcpuThread {
                handle,
                exited,
                immediate_exit: ie_handle,
                exit_evt,
                alive,
            });
            freeze_parked.push(parked);
            freeze_regs.push(regs);
        }
        Ok((
            ap_threads,
            ApFreezeHandles {
                parked: freeze_parked,
                regs: freeze_regs,
            },
        ))
    }

    /// Start the monitor thread if vmlinux is available.
    ///
    /// `probes_ready_evt` is the broadcast EventFd shared with the
    /// bpf-map-write thread (see [`run_vm`]); the slot-1 wait below
    /// `poll`s it instead of bare-sleeping, and writes 1 to it on
    /// detection so any other waiter blocked in `poll` wakes
    /// immediately and re-checks its own readiness condition.
    #[allow(clippy::too_many_arguments)]
    pub(super) fn start_monitor(
        &self,
        vm: &kvm::KtstrKvm,
        kill: &Arc<AtomicBool>,
        kill_evt: &Arc<EventFd>,
        run_start: Instant,
        vcpu_pthreads: Vec<libc::pthread_t>,
        perf_capture: Arc<Option<monitor::perf_counters::PerfCountersCapture>>,
        _probes_ready_evt: EventFd,
        virtio_con: Option<Arc<PiMutex<virtio_console::VirtioConsole>>>,
        sys_rdy_evt: Option<Arc<EventFd>>,
        tcr_el1: Option<Arc<std::sync::atomic::AtomicU64>>,
        cr3: Arc<std::sync::atomic::AtomicU64>,
        watchdog_reset_ns: Arc<std::sync::atomic::AtomicU64>,
        kern_phys_base_shared: Arc<std::sync::atomic::AtomicU64>,
        kern_phys_base_evt: Arc<EventFd>,
    ) -> Result<Option<JoinHandle<monitor::reader::MonitorLoopResult>>> {
        let Some(vmlinux) = find_vmlinux(&self.kernel) else {
            return Ok(None);
        };
        // Read the vmlinux bytes once and feed both the BTF loader
        // and the ELF symbol parser. The previous structure called
        // `load_btf_from_path` and `KernelSymbols::from_vmlinux` back
        // to back, each running its own `std::fs::read` — on a debug
        // vmlinux that is two ~1 GB reads through the page cache for
        // a single byte slice's worth of work.
        let vmlinux_data_arc = match super::vmlinux::cached_vmlinux_bytes(&vmlinux) {
            Some(d) => d,
            None => return Ok(None),
        };
        let vmlinux_data = &*vmlinux_data_arc;
        let elf = match goblin::elf::Elf::parse(vmlinux_data) {
            Ok(e) => e,
            Err(_) => return Ok(None),
        };
        // Single BTF parse for both `KernelOffsets` and
        // `BpfProgOffsets`. The previous structure parsed BTF twice
        // (KernelOffsets up here, BpfProgOffsets inside the spawned
        // monitor thread closure), each call hitting
        // `load_btf_from_path` and `Btf::from_bytes`. On debug-built
        // vmlinux the parse is hundreds of ms; doing it twice
        // pushed the monitor thread past the no-scheduler boot
        // window so early samples saw the rq's pre-AP-online state.
        // One parse, two `from_btf` consumers, both share the
        // resolved offsets. On a BTF sidecar cache hit the supplied
        // `elf` is unused; on a miss `load_btf_from_elf` reuses it
        // instead of running its own `Elf::parse`.
        let btf = match monitor::btf_offsets::load_btf_from_elf(&elf, vmlinux_data, &vmlinux) {
            Ok(b) => b,
            Err(_) => return Ok(None),
        };
        let offsets = monitor::btf_offsets::KernelOffsets::from_btf(&btf);
        let prog_offsets = monitor::btf_offsets::BpfProgOffsets::from_btf(&btf).ok();
        let symbols = monitor::symbols::KernelSymbols::from_elf(&elf);

        let (Ok(offsets), Ok(symbols)) = (offsets, symbols) else {
            return Ok(None);
        };

        let mem = match vm.numa_layout.as_ref() {
            Some(layout) => monitor::reader::GuestMem::from_layout(layout, &vm.guest_mem),
            None => {
                use vm_memory::GuestMemoryRegion;
                let host_base = vm
                    .guest_mem
                    .get_host_address(GuestAddress(DRAM_BASE))
                    .context("resolve guest DRAM base host address (monitor)")?;
                // Size of the first contiguous region only.
                // host_base addresses that single mapping; using the
                // sum of all region lengths would extend past the
                // mapping into host heap when multiple regions exist.
                let mem_size = vm
                    .guest_mem
                    .iter()
                    .next()
                    .context("guest_mem must have at least one region (monitor)")?
                    .len();
                // SAFETY: host_base is from GuestMemoryMmap's mapping,
                // which outlives this GuestMem (owned by `vm` until
                // return).
                unsafe { monitor::reader::GuestMem::new(host_base, mem_size) }
            }
        };
        let num_cpus = self.topology.total_cpus();
        let kill_clone = kill.clone();
        let kill_evt_clone = kill_evt.clone();
        // Clone the boot-complete eventfd handle for the monitor
        // closure. Captured by `move` into the spawned thread so
        // the `epoll_wait` dispatch can register the fd alongside
        // `kill_evt` and the timerfd. `Option::None` short-circuits
        // the pre-sample wait so the test path (no virtio-console)
        // and any `EventFd::new` failure both fall through to the
        // sample loop directly.
        let monitor_sys_rdy_evt = sys_rdy_evt.clone();
        let dump_trigger = self
            .monitor_thresholds
            .map(|thresholds| monitor::reader::DumpTrigger {
                thresholds,
                virtio_con: virtio_con.clone(),
            });

        let hz = monitor::guest_kernel_hz(Some(&self.kernel));
        // ms-precision conversion lives in [`duration_to_jiffies`];
        // see its doc for why the seconds-based form is wrong.
        let watchdog_jiffies = self.watchdog_timeout.map(|d| duration_to_jiffies(d, hz));
        let preemption_threshold_ns = monitor::vcpu_preemption_threshold_ns(Some(&self.kernel));
        let rt_monitor = self.performance_mode;
        let service_cpu = self.pinning_plan.as_ref().and_then(|p| p.service_cpu);
        // Workload duration captured for the scheduler-attach
        // watchdog reset. `Some(d)` enables the reset; the
        // monitor closure constructs a
        // [`monitor::reader::WatchdogReset`] payload pairing this
        // duration with the resolved `scx_root_pa` and the shared
        // `watchdog_reset_ns` atomic once `symbols.scx_root`
        // resolves below. `None` (the builder default) leaves
        // [`monitor::reader::MonitorConfig::watchdog_reset`] as
        // `None`, and the loop's reset detection short-circuits.
        let workload_duration = self.workload_duration;

        let handle = std::thread::Builder::new()
            .name("vmm-monitor".into())
            .spawn(move || {
                if let Some(cpu) = service_cpu {
                    pin_current_thread(cpu, "monitor");
                }
                if rt_monitor {
                    set_rt_priority(2, "monitor");
                }
                // Pre-resolution boot-complete wait, hoisted ABOVE
                // the `phys_base` / `pco_pa` / scx_root_pa /
                // watchdog_pa / `page_offset_base_pa` resolution
                // that follows. Previously this thread either
                // resolved `phys_base` immediately (with `cr3=0` →
                // `phys_base=0` → every KASLR text/data PA wrong)
                // or polled CR3 with a short busy-wait that fires
                // too early — CR3 is set in `__startup_64`, but
                // `setup_per_cpu_areas` (which populates
                // `__per_cpu_offset[]`) and KASLR randomization of
                // `page_offset_base` finish much later in
                // `start_kernel`. Resolving `pco_pa` /
                // `page_offset_base_pa` between those two events
                // produces baked-in stale PAs that the
                // per-iteration refresh inside `monitor_loop` cannot
                // recover from.
                //
                // The `MSG_TYPE_SYS_RDY` TLV frame is emitted by
                // `ktstr_guest_init` after `mount_filesystems()`
                // — strictly AFTER `__startup_64` (CR3 latch),
                // `__cpu_setup` (TCR_EL1 latch), `setup_per_cpu_areas`
                // (`__per_cpu_offset[]` populated), KASLR
                // randomization (`page_offset_base` populated), and
                // userspace init startup. By blocking here on the
                // sys_rdy eventfd, the resolution that follows runs
                // against a guest in steady state: every read in
                // `resolve_phys_base`, `resolve_page_offset_with_tcr`,
                // and the text-mapped PA recomputes lands on
                // populated guest memory.
                //
                // Three exit conditions:
                //   1. sys_rdy fires: proceed to phys_base resolve.
                //   2. kill fires: VM died before booting; return
                //      empty MonitorLoopResult immediately.
                //   3. 5 s timeout: best-effort fall through. The
                //      downstream `data_valid` gate inside
                //      `monitor_loop` still guards every walk, so
                //      reads of pre-boot zeros are tolerated and
                //      the monitor produces an empty sample set
                //      rather than chasing pointers through wrong
                //      PAs.
                //
                // `MonitorConfig::sys_rdy` is set to `None` below
                // because the wait has already happened here —
                // re-running the wait inside `monitor_loop` would
                // be a no-op (sys_rdy is edge-triggered, the eventfd
                // counter has been read by this wait and the
                // `Option::take` in the freeze-coord TOKEN_TX
                // handler also fires only once).
                if let Some(sys_rdy) = monitor_sys_rdy_evt.as_deref() {
                    use std::os::unix::io::AsRawFd;
                    use vmm_sys_util::epoll::{
                        ControlOperation, Epoll, EpollEvent, EventSet,
                    };
                    // Upfront kill check: BSP can exit before the
                    // monitor thread is scheduled (fast 1-CPU tests
                    // that fall through `test_main` in milliseconds).
                    // In that case `run_vm` has already stored
                    // kill + written kill_evt; entering the
                    // boot epoll would still wake immediately on
                    // kill_fd, but skipping the syscall trip
                    // entirely is cheaper and avoids the small
                    // window where epoll_create / epoll_ctl could
                    // race with VM teardown.
                    if kill_clone.load(std::sync::atomic::Ordering::Acquire) {
                        return monitor::reader::MonitorLoopResult {
                            samples: Vec::new(),
                            drain: crate::vmm::host_comms::BulkDrainResult {
                                entries: Vec::new(),
                            },
                            watchdog_observation: None,
                            page_offset: 0,
                            preemption_threshold_ns,
                        };
                    }
                    let kill_fd = kill_evt_clone.as_raw_fd();
                    let boot_fd = sys_rdy.as_raw_fd();
                    if let Ok(boot_epoll) = Epoll::new() {
                        let _ = boot_epoll.ctl(
                            ControlOperation::Add,
                            boot_fd,
                            EpollEvent::new(EventSet::IN, boot_fd as u64),
                        );
                        let _ = boot_epoll.ctl(
                            ControlOperation::Add,
                            kill_fd,
                            EpollEvent::new(EventSet::IN, kill_fd as u64),
                        );
                        let mut boot_buf = [EpollEvent::default(); 2];
                        // 5 s ceiling: a healthy guest emits SYS_RDY
                        // within ~3 s of boot; longer is a stuck
                        // guest. Tests that exit without sending
                        // SYS_RDY (e.g. early-init crash) must wait
                        // here only until either the eventfd fires
                        // or `run_vm` propagates the kill
                        // flag — the timeout is the fallback for
                        // the case where neither wake arrives, and
                        // tighter is better because the host VM
                        // teardown waits on this thread joining.
                        let _ = boot_epoll.wait(5_000, &mut boot_buf);
                    }
                    if kill_clone.load(std::sync::atomic::Ordering::Acquire) {
                        return monitor::reader::MonitorLoopResult {
                            samples: Vec::new(),
                            drain: crate::vmm::host_comms::BulkDrainResult {
                                entries: Vec::new(),
                            },
                            watchdog_observation: None,
                            page_offset: 0,
                            preemption_threshold_ns,
                        };
                    }
                }

                // Resolve the kernel image base. On x86_64 this is
                // the compile-time constant; on aarch64 it depends
                // on `VA_BITS_MIN` derived from `TCR_EL1.T1SZ` and
                // `TCR_EL1.TG1` (granule). After the sys_rdy wait
                // the BSP has executed many run-loop iterations
                // and the lazy CAS for `tcr_el1_cache` has fired
                // (kernel programs TCR_EL1 in `__cpu_setup` long
                // before userspace init runs).
                let tcr_el1_value = tcr_el1
                    .as_ref()
                    .map(|c| c.load(std::sync::atomic::Ordering::Acquire))
                    .unwrap_or(0);
                let start_kernel_map_for_thread =
                    monitor::symbols::start_kernel_map_for_tcr(tcr_el1_value)
                        .unwrap_or(monitor::symbols::START_KERNEL_MAP);

                // Resolve `phys_base` via a page-table walk through
                // the live BSP CR3. After the sys_rdy wait above the
                // BSP has populated `cr3_cache` via its lazy CAS —
                // `__startup_64` / `__cpu_setup` runs strictly before
                // userspace init emits SYS_RDY, and the BSP run loop
                // has executed thousands of iterations by then so
                // every iteration's CAS attempt has had a chance to
                // observe a non-zero CR3.
                //
                // Race window: in cold-cache or coverage-instrumented
                // builds the BSP can lag the kernel's userspace-init
                // emission of SYS_RDY by tens of ms — the freeze
                // coordinator promotes the SYS_RDY frame to the
                // monitor's eventfd from its TLV dispatch, which
                // runs on the freeze coord thread independent of
                // BSP run-loop progress. If the monitor reads
                // cr3_cache before the BSP has executed its first
                // run-loop iteration's lazy CAS, cr3_value is 0
                // and `phys_base` falls back to 0 for the entire
                // run. On KASLR builds that turns every text-mapped
                // PA derivation (pco_pa, scx_root_pa,
                // page_offset_base_pa) into an out-of-DRAM address;
                // every subsequent monitor read returns 0 and
                // `data_valid` never latches. Brief retry-until-
                // non-zero closes the window: by the time SYS_RDY
                // fires the BSP has been in the run loop for
                // hundreds of ms in steady state, so the vast
                // majority of paths return on the first load. The
                // 500-iteration cap (500 ms total) handles the
                // pathological case where the BSP genuinely never
                // populated the cache (early-boot crash, kill
                // before first iteration); in that case `phys_base`
                // falls back to 0 and the downstream `data_valid`
                // gate keeps every walk safe.
                // Wait for guest-reported phys_base. The guest
                // reads it from /proc/iomem and writes
                // `phys_base + 1` (biased +1 so the AtomicU64's
                // initial 0 means "not yet received") to
                // `/dev/vport0p2`; the host's virtio-console MMIO
                // handler captures the value inline on the vCPU
                // thread, independently of the TLV bulk port and the
                // freeze coordinator's iter1 vmlinux load. The 3 s
                // budget gives the kernel virtio_console driver time
                // to complete its multiport handshake under cold-
                // cache boots; on the fast path the value is already
                // visible by the time SYS_RDY fires. No KASLR/PTI
                // page-table walking is needed.
                let phys_base = {
                    let mut pb = 0u64;
                    let pb_fd = {
                        use std::os::unix::io::AsRawFd;
                        kern_phys_base_evt.as_raw_fd()
                    };
                    let kill_fd = {
                        use std::os::unix::io::AsRawFd;
                        kill_evt_clone.as_raw_fd()
                    };
                    for _ in 0..30 {
                        if kill_clone.load(std::sync::atomic::Ordering::Acquire) {
                            break;
                        }
                        let biased = kern_phys_base_shared.load(
                            std::sync::atomic::Ordering::Acquire,
                        );
                        if biased != 0 {
                            pb = biased.wrapping_sub(1);
                            break;
                        }
                        let cr3_val = cr3.load(std::sync::atomic::Ordering::Acquire);
                        if cr3_val != 0 {
                            let l5 = monitor::symbols::resolve_pgtable_l5(
                                &mem, &symbols, start_kernel_map_for_thread, 0,
                            );
                            if let Some(v) = monitor::symbols::resolve_phys_base(
                                &mem, &symbols, cr3_val, l5, tcr_el1_value,
                            )
                                && v != 0 {
                                    pb = v;
                                    break;
                                }
                        }
                        let mut pfds = [
                            libc::pollfd { fd: pb_fd, events: libc::POLLIN, revents: 0 },
                            libc::pollfd { fd: kill_fd, events: libc::POLLIN, revents: 0 },
                        ];
                        unsafe { libc::poll(pfds.as_mut_ptr(), 2, 100) };
                    }
                    pb
                };
                if phys_base != 0 {
                    let _ = kern_phys_base_shared.compare_exchange(
                        0,
                        phys_base.wrapping_add(1),
                        std::sync::atomic::Ordering::Release,
                        std::sync::atomic::Ordering::Relaxed,
                    );
                    let _ = kern_phys_base_evt.write(1);
                }

                // Derive kaslr_offset by reading the kernel's real
                // phys_base variable from guest memory. The guest
                // reported `output - LOAD_PHYSICAL_ADDR` which equals
                // `real_phys_base + kaslr_offset`. Reading the kernel's
                // own phys_base and subtracting recovers kaslr_offset.
                let kaslr_offset = if phys_base != 0
                    && let Some(pb_kva) = symbols.phys_base_kva
                {
                    let pb_pa = crate::monitor::symbols::text_kva_to_pa_with_base(
                        pb_kva,
                        start_kernel_map_for_thread,
                        phys_base,
                    );
                    let real_phys_base = mem.read_u64(pb_pa, 0);
                    if real_phys_base != 0 || phys_base == 0 {
                        phys_base.wrapping_sub(real_phys_base)
                    } else {
                        0
                    }
                } else {
                    0
                };

                // Kill check between sys_rdy wait and the long-tail
                // setup work below (page-table walks, watchdog override
                // resolve, post-wait re-resolve, BTF prog_offsets
                // consumption, monitor_loop entry). On debug builds
                // with cold caches the resolution path can spend
                // multiple seconds in `resolve_phys_base` /
                // `resolve_pgtable_l5` / `text_kva_to_pa_with_base`,
                // and `run_vm`'s `kill_evt.write(1)` cannot
                // interrupt code that is not blocked on epoll. Sample
                // the kill flag at every major boundary so a VM that
                // exits during setup tears the monitor down within
                // microseconds rather than having `monitor_handle.join`
                // block until the setup runs to completion.
                if kill_clone.load(std::sync::atomic::Ordering::Acquire) {
                    return monitor::reader::MonitorLoopResult {
                        samples: Vec::new(),
                        drain: crate::vmm::host_comms::BulkDrainResult {
                            entries: Vec::new(),
                        },
                        watchdog_observation: None,
                        page_offset: 0,
                        preemption_threshold_ns,
                    };
                }

                let page_offset = monitor::symbols::resolve_page_offset_with_tcr(
                    &mem,
                    &symbols,
                    start_kernel_map_for_thread,
                    tcr_el1_value,
                    phys_base,
                );

                // `__per_cpu_offset[]` lives in the kernel image
                // mapping (text PA). `setup_per_cpu_areas` in
                // `start_kernel` populates every slot before SMP
                // bringup IN THE GUEST — but the host monitor thread
                // spawns before the guest BSP enters KVM_RUN, so a
                // pre-loop one-shot read sees BSS zeros. Pass the
                // PAs that drive the recompute through `RqRefresh`
                // so the loop body re-reads each sample; see
                // [`monitor::reader::RqRefresh`].
                let pco_pa = monitor::symbols::text_kva_to_pa_with_base(
                    symbols.per_cpu_offset,
                    start_kernel_map_for_thread,
                    phys_base,
                );

                let watchdog_override = watchdog_jiffies.and_then(|jiffies| {
                    // 7.1+ path: deref scx_root -> scx_sched.watchdog_timeout.
                    if let Some((scx_root_kva, wd_offs)) = symbols
                        .scx_root
                        .zip(offsets.watchdog_offsets.as_ref())
                    {
                        let scx_root_pa = monitor::symbols::text_kva_to_pa_with_base(
                            scx_root_kva,
                            start_kernel_map_for_thread,
                            phys_base,
                        );
                        let resolve_pa = |kva| {
                            monitor::symbols::text_kva_to_pa_with_base(
                                kva,
                                start_kernel_map_for_thread,
                                phys_base,
                            )
                        };
                        let interval_pa = symbols.scx_watchdog_interval.map(&resolve_pa);
                        let timestamp_pa = symbols.scx_watchdog_timestamp.map(&resolve_pa);
                        let jiffies_64_pa = symbols.jiffies_64.map(&resolve_pa);
                        return Some(monitor::reader::WatchdogOverride::ScxSched {
                            scx_root_pa,
                            watchdog_offset: wd_offs.scx_sched_watchdog_timeout_off,
                            jiffies,
                            interval_pa,
                            timestamp_pa,
                            jiffies_64_pa,
                        });
                    }
                    if let Some(wdt_kva) = symbols.scx_watchdog_timeout {
                        let resolve_pa = |kva| {
                            monitor::symbols::text_kva_to_pa_with_base(
                                kva,
                                start_kernel_map_for_thread,
                                phys_base,
                            )
                        };
                        let watchdog_timeout_pa = resolve_pa(wdt_kva);
                        let interval_pa = symbols.scx_watchdog_interval.map(&resolve_pa);
                        let timestamp_pa = symbols.scx_watchdog_timestamp.map(&resolve_pa);
                        let jiffies_64_pa = symbols.jiffies_64.map(&resolve_pa);
                        return Some(monitor::reader::WatchdogOverride::StaticGlobal {
                            watchdog_timeout_pa,
                            jiffies,
                            interval_pa,
                            timestamp_pa,
                            jiffies_64_pa,
                        });
                    }
                    None
                });
                if watchdog_jiffies.is_some() && watchdog_override.is_none() {
                    tracing::warn!(
                        "no watchdog override path available — neither scx_sched.watchdog_timeout BTF field nor scx_watchdog_timeout symbol found"
                    );
                }

                // Kill check after watchdog override resolve. The
                // BTF / symbol-table lookups above can themselves
                // touch hundreds of kilobytes of vmlinux ELF, so a
                // VM that exits while we are still here would
                // otherwise have to wait for the entire setup tail
                // to drain before `monitor_handle.join` returns.
                if kill_clone.load(std::sync::atomic::Ordering::Acquire) {
                    return monitor::reader::MonitorLoopResult {
                        samples: Vec::new(),
                        drain: crate::vmm::host_comms::BulkDrainResult {
                            entries: Vec::new(),
                        },
                        watchdog_observation: None,
                        page_offset: 0,
                        preemption_threshold_ns,
                    };
                }

                // `event_pcpu_pas` derives from
                // `*scx_root -> scx_sched.pcpu` (or
                // `event_stats_cpu` on pre-6.18 kernels) plus
                // `__per_cpu_offset[]`. Both inputs change with VM
                // lifetime: `*scx_root` is null until a scheduler
                // attaches, and the percpu base table is BSS zero
                // until `setup_per_cpu_areas` runs. Stash the
                // text-mapped PA of `scx_root` plus the BTF offsets
                // and let the monitor loop refresh per-iteration.
                let event_refresh =
                    symbols
                        .scx_root
                        .zip(offsets.event_offsets.as_ref())
                        .map(|(scx_root_kva, ev)| {
                            let scx_root_pa = monitor::symbols::text_kva_to_pa_with_base(
                                scx_root_kva,
                                start_kernel_map_for_thread,
                                phys_base,
                            );
                            monitor::reader::EventRefresh {
                                scx_root_pa,
                                event_offsets: ev.clone(),
                            }
                        });
                // Scheduler-attach watchdog-reset PA, derived
                // independently of `event_refresh` so the reset
                // works on kernels without resolvable
                // `event_offsets` (e.g. older kernels lacking the
                // BTF struct, or stripped vmlinux). Always derives
                // from `symbols.scx_root` directly — the same
                // text-mapped global the kernel itself uses to
                // publish the active `scx_sched`. `None` when the
                // symbol could not be resolved (no scx support in
                // the kernel image, or `KernelSymbols::from_elf`
                // failed to find it); the loop's
                // `cfg.watchdog_reset` short-circuits in that
                // case.
                let scx_root_pa_for_reset = symbols.scx_root.map(|kva| {
                    monitor::symbols::text_kva_to_pa_with_base(
                        kva,
                        start_kernel_map_for_thread,
                        phys_base,
                    )
                });
                // `page_offset_base` is x86_64-only (a KASLR direct-map
                // base randomized by `CONFIG_RANDOMIZE_MEMORY`).
                // `KernelSymbols::from_vmlinux` returns `None` on
                // aarch64 and on kernels built without the symbol —
                // the per-iteration refresh tolerates that and
                // leaves `page_offset` at the pre-loop default.
                let page_offset_base_pa = symbols.page_offset_base_kva.map(|kva| {
                    monitor::symbols::text_kva_to_pa_with_base(
                        kva, start_kernel_map_for_thread, phys_base,
                    )
                });
                let rq_refresh = monitor::reader::RqRefresh {
                    pco_pa,
                    runqueues_kva: symbols.runqueues,
                    per_cpu_start: symbols.per_cpu_start,
                    kaslr_offset,
                    num_cpus,
                    page_offset_base_pa,
                    event: event_refresh,
                };

                let vcpu_timing = monitor::reader::VcpuTiming {
                    pthreads: vcpu_pthreads,
                };

                // The legacy SHM signal slot 1 (`SIGNAL_PROBES_READY`)
                // gate before struct_ops discovery has been removed
                // along with the SHM signal-slot infrastructure. The
                // discovery walker tolerates an empty IDR (returns an
                // empty `Vec` when no struct_ops programs are loaded
                // yet) and re-runs every monitor sample, so a race
                // with scheduler BPF program registration recovers on
                // the next cycle.

                // Discover struct_ops programs for per-cycle stats.
                // `cr3_pa` and `l5` are shared with `discover_struct_ops_stats`
                // and `ProgStatsCtx` so per-CPU `bpf_prog_stats` reads can
                // page-walk vmalloc-backed percpu.
                //
                // Re-derive the kernel image base at this point: we
                // just blocked on the guest's slot-1 signal, so the
                // BSP loop has had time to populate the TCR_EL1
                // cache even if it was still 0 at thread start.
                // This is the value that flows into ProgStatsCtx
                // and the GuestKernel constructions below, so a
                // late re-read here gets aarch64 VA_BITS=47 hosts
                // out of the early-boot fallback window.
                let start_kernel_map_post_wait = monitor::symbols::start_kernel_map_for_tcr(
                    tcr_el1
                        .as_ref()
                        .map(|c| c.load(std::sync::atomic::Ordering::Acquire))
                        .unwrap_or(0),
                )
                .unwrap_or(start_kernel_map_for_thread);
                // Use the live BSP CR3 directly (it's already a PA;
                // no `phys_base`-dependent translation needed). When
                // the retry above timed out without observing a
                // non-zero CR3, fall back to the text-symbol
                // translation of `init_top_pgt` — historical
                // behaviour, correct on non-KASLR boots. The earlier
                // post-sys_rdy retry has already waited up to 500 ms
                // for `cr3_cache` to land (warning if it didn't), so
                // a second `cr3.load` here would observe the same
                // value and the post-wait `resolve_phys_base` /
                // `cr3_pa` derivations are folded back onto the
                // pre-wait `cr3_value` / `phys_base` directly.
                let cr3_latest = cr3.load(std::sync::atomic::Ordering::Acquire);
                let cr3_pa = if cr3_latest != 0 {
                    cr3_latest & !0x1FFFu64
                } else {
                    monitor::symbols::text_kva_to_pa_with_base(
                        symbols.init_top_pgt.unwrap_or(0),
                        start_kernel_map_post_wait,
                        phys_base,
                    )
                };
                let l5 = monitor::symbols::resolve_pgtable_l5(
                    &mem,
                    &symbols,
                    start_kernel_map_post_wait,
                    phys_base,
                );
                // Kill check after the post-wait re-resolve.
                // `resolve_phys_base` and `resolve_pgtable_l5` are the
                // most expensive operations in the closure on cold
                // caches — each performs a multi-level page-table
                // walk through guest memory. Return promptly if the
                // VM has already torn down.
                if kill_clone.load(std::sync::atomic::Ordering::Acquire) {
                    return monitor::reader::MonitorLoopResult {
                        samples: Vec::new(),
                        drain: crate::vmm::host_comms::BulkDrainResult {
                            entries: Vec::new(),
                        },
                        watchdog_observation: None,
                        page_offset: 0,
                        preemption_threshold_ns,
                    };
                }
                // aarch64 TCR_EL1 (granule + T1SZ) for the
                // page-table walker. Threaded through ProgStatsCtx
                // so vmalloc-backed percpu `bpf_prog_stats`
                // translations succeed once the BSP populates the
                // cache. Always 0 on x86_64.
                let tcr_el1_val = tcr_el1
                    .as_ref()
                    .map(|c| c.load(std::sync::atomic::Ordering::Acquire))
                    .unwrap_or(0);
                // `prog_offsets` was resolved up front from the
                // single shared `Btf` parse — see the BTF load at
                // the top of `start_monitor`. A previous version
                // re-parsed BTF here via
                // `BpfProgOffsets::from_vmlinux`, doubling the
                // setup cost on every VM run. Dropping that second
                // parse trims hundreds of ms off monitor-thread
                // startup on debug-built vmlinux, so monitor_loop
                // entry — and the first sample push — lands
                // earlier in the VM lifetime. On short-lived
                // no-scheduler boots where the VM exits within a
                // second, the saved time is the difference between
                // sampling rq_clock pre-tick (zero) and post-tick
                // (real values).
                let prog_stats_ctx = prog_offsets.and_then(|prog_offsets| {
                    let prog_idr_kva = symbols.prog_idr?;
                    // The fused walker
                    // (`walk_struct_ops_runtime_stats`) re-walks
                    // `prog_idr` each sample, which is cheap on
                    // ktstr workloads (idr_next is in the
                    // dozens) and removes the staleness window
                    // the prior cached-discovery design opened.
                    // No upfront discovery — the walker
                    // returns an empty Vec when no struct_ops
                    // programs are loaded yet, and the monitor
                    // sample emits an empty `prog_stats` for
                    // those cycles.
                    //
                    // `per_cpu_offsets` left empty here: when
                    // `rq_refresh` is set on the
                    // [`monitor::reader::MonitorConfig`], the
                    // monitor loop refreshes `__per_cpu_offset[]`
                    // per iteration and threads the live array
                    // through to `walk_struct_ops_runtime_stats`,
                    // ignoring this seed.
                    Some(monitor::reader::ProgStatsCtx {
                        per_cpu_offsets: Vec::new(),
                        walk: monitor::reader::WalkContext {
                            cr3_pa,
                            page_offset,
                            l5,
                            tcr_el1: tcr_el1_val,
                        },
                        prog_idr_kva,
                        offsets: prog_offsets,
                        start_kernel_map: start_kernel_map_post_wait,
                        phys_base,
                    })
                });

                // Kill check between prog_stats_ctx construction and
                // monitor_loop entry. `monitor_loop` itself honours
                // `kill_evt` via its own epoll registration (see
                // `monitor/reader.rs`), so the check here is the
                // last guard that prevents an idle thread closure
                // from racing into the loop after the VM has been
                // told to shut down.
                if kill_clone.load(std::sync::atomic::Ordering::Acquire) {
                    return monitor::reader::MonitorLoopResult {
                        samples: Vec::new(),
                        drain: crate::vmm::host_comms::BulkDrainResult {
                            entries: Vec::new(),
                        },
                        watchdog_observation: None,
                        page_offset: 0,
                        preemption_threshold_ns,
                    };
                }

                // Construct the scheduler-attach reset payload
                // when both ingredients are present: a workload
                // duration on the VM (the test set `duration` →
                // `KtstrVm::workload_duration` is `Some`) AND a
                // resolvable `scx_root` symbol (the kernel image
                // ships scx and the symbol parser found it). Both
                // missing means there is nothing to reset to / no
                // detection point — leave the field `None` so
                // the monitor's per-iteration check
                // short-circuits.
                let watchdog_reset_cfg = workload_duration.zip(scx_root_pa_for_reset).map(
                    |(workload_duration, scx_root_pa)| monitor::reader::WatchdogReset {
                        scx_root_pa,
                        workload_duration,
                        reset_ns: watchdog_reset_ns.as_ref(),
                    },
                );
                let mon_cfg = monitor::reader::MonitorConfig {
                    // `event_pcpu_pas` left `None` here: the loop
                    // recomputes it each iteration via
                    // `rq_refresh.event` so newly attached
                    // schedulers surface event counters from the
                    // first post-attach sample without a restart.
                    event_pcpu_pas: None,
                    dump_trigger: dump_trigger.as_ref(),
                    watchdog_override: watchdog_override.as_ref(),
                    vcpu_timing: Some(&vcpu_timing),
                    // `perf_capture` is `Arc<Option<PerfCountersCapture>>`;
                    // outer deref through `Arc::as_ref` yields
                    // `&Option<PerfCountersCapture>`, inner
                    // `Option::as_ref` yields the
                    // `Option<&PerfCountersCapture>` MonitorConfig wants.
                    perf_capture: (*perf_capture).as_ref(),
                    preemption_threshold_ns,
                    prog_stats_ctx: prog_stats_ctx.as_ref(),
                    page_offset,
                    start_kernel_map: start_kernel_map_post_wait,
                    phys_base,
                    rq_refresh: Some(&rq_refresh),
                    // `sys_rdy: None` — the boot-complete wait has
                    // already happened above, BEFORE
                    // `phys_base` resolution and the text-mapped PA
                    // recomputes. Re-running the wait here would be
                    // redundant: the freeze coordinator's TOKEN_TX
                    // handler fires the eventfd exactly once
                    // (`Option::take` makes the write fire-once), and
                    // the per-iteration `page_offset` /
                    // `__per_cpu_offset[]` refresh + `data_valid`
                    // gate inside `monitor_loop` already covers the
                    // pre-boot-zero defense in depth.
                    sys_rdy: None,
                    watchdog_reset: watchdog_reset_cfg,
                };
                // `rq_pas` empty: the loop sources every per-CPU
                // PA from `rq_refresh` per iteration so the static
                // slice would be both stale and redundant.
                monitor::reader::monitor_loop(
                    &mem,
                    &[],
                    &offsets,
                    Duration::from_millis(100),
                    &kill_clone,
                    &kill_evt_clone,
                    run_start,
                    &mon_cfg,
                )
            })
            .context("spawn monitor thread")?;

        Ok(Some(handle))
    }

    /// Spawn a thread that writes to a BPF map in guest memory.
    ///
    /// Event-driven sequence:
    /// 1. Poll `BpfMapAccessorOwned::new` until kernel page tables are up
    /// 2. Poll `find_map` until the scheduler's BPF maps are discoverable
    /// 3. Write each queued value, then push `SIGNAL_BPF_WRITE_DONE`
    ///    through virtio-console RX so the guest's `hvc0_poll_loop`
    ///    sets the `bpf_map_write_done` latch; the scenario's
    ///    `wait_for_map_write` gate (`Ctx::wait_for_map_write=true`)
    ///    blocks on that latch until this thread fires.
    ///
    /// `probes_ready_evt` is the broadcast EventFd shared with the
    /// monitor thread (see [`run_vm`]); each phase below `poll`s it
    /// instead of bare-sleeping, and writes 1 to it on detection so
    /// the monitor (and any future waiter) wakes immediately to
    /// re-check its own readiness condition.
    ///
    /// `virtio_con` is the shared virtio-console device used to push
    /// the host→guest wake byte after the writes land. Replaces the
    /// legacy SHM signal slot 0 notification.
    #[allow(clippy::too_many_arguments)]
    pub(super) fn start_bpf_map_write(
        &self,
        vm: &kvm::KtstrKvm,
        kill: &Arc<AtomicBool>,
        probes_ready_evt: EventFd,
        tcr_el1: Option<Arc<std::sync::atomic::AtomicU64>>,
        cr3: Arc<std::sync::atomic::AtomicU64>,
        virtio_con: Arc<PiMutex<virtio_console::VirtioConsole>>,
        kern_phys_base: Arc<std::sync::atomic::AtomicU64>,
    ) -> Result<Option<JoinHandle<()>>> {
        if self.bpf_map_writes.is_empty() {
            return Ok(None);
        }
        let Some(vmlinux) = find_vmlinux(&self.kernel) else {
            eprintln!("bpf_map_write: vmlinux not found, skipping");
            return Ok(None);
        };

        let mem = match vm.numa_layout.as_ref() {
            Some(layout) => monitor::reader::GuestMem::from_layout(layout, &vm.guest_mem),
            None => {
                use vm_memory::GuestMemoryRegion;
                let host_base = vm
                    .guest_mem
                    .get_host_address(GuestAddress(DRAM_BASE))
                    .context("resolve guest DRAM base host address (bpf-map-write)")?;
                // Size of the first contiguous region only.
                // host_base addresses that single mapping; using the
                // sum of all region lengths would extend past the
                // mapping into host heap when multiple regions exist.
                let mem_size = vm
                    .guest_mem
                    .iter()
                    .next()
                    .context("guest_mem must have at least one region (bpf-map-write)")?
                    .len();
                // SAFETY: host_base is from GuestMemoryMmap's mapping,
                // which outlives this GuestMem (owned by `vm` until
                // return).
                unsafe { monitor::reader::GuestMem::new(host_base, mem_size) }
            }
        };
        let kill_clone = kill.clone();
        let writes = self.bpf_map_writes.clone();

        let handle = std::thread::Builder::new()
            .name("bpf-map-write".into())
            .spawn(move || {
                use crate::monitor::bpf_map::BpfMapAccessor;
                if kill_clone.load(Ordering::Acquire) {
                    return;
                }

                // Phase 1: wait for BPF map accessor (kernel booted, page tables up).
                //
                // Sleeping is replaced by `poll(POLLIN)` against the
                // shared `probes_ready_evt`: ANY waiter that detects
                // its own readiness condition writes 1 to the eventfd
                // and the level stays high (we never `read` here), so
                // this loop wakes immediately on a sibling detection
                // and re-tries the accessor construction. The 200 ms
                // timeout preserves the prior cadence as an upper
                // bound for kill / deadline observation when no other
                // detector has fired yet. On successful construction
                // we write 1 ourselves, fanning the wake out to the
                // monitor and the later phases.
                let vmlinux_data_arc = match super::vmlinux::cached_vmlinux_bytes(&vmlinux) {
                    Some(d) => d,
                    None => {
                        eprintln!("bpf_map_write: read vmlinux failed");
                        return;
                    }
                };
                let vmlinux_data = &*vmlinux_data_arc;
                let vmlinux_elf = match goblin::elf::Elf::parse(vmlinux_data) {
                    Ok(e) => e,
                    Err(e) => {
                        eprintln!("bpf_map_write: parse vmlinux ELF failed: {e:#}");
                        return;
                    }
                };
                let mem = Arc::new(mem);
                let phase1_deadline =
                    std::time::Instant::now() + std::time::Duration::from_secs(30);
                let owned = loop {
                    let biased = kern_phys_base.load(std::sync::atomic::Ordering::Acquire);
                    if biased == 0 {
                        if kill_clone.load(Ordering::Acquire) {
                            return;
                        }
                        poll_eventfd_until_ready_or_timeout(&probes_ready_evt, 200);
                        continue;
                    }
                    let pb_hint = biased.wrapping_sub(1);
                    let tcr_val = tcr_el1
                        .as_ref()
                        .map(|c| c.load(std::sync::atomic::Ordering::Acquire))
                        .unwrap_or(0);
                    let cr3_val = cr3.load(std::sync::atomic::Ordering::Acquire);
                    match monitor::bpf_map::GuestMemMapAccessorOwned::from_elf_with_hint(Arc::clone(&mem), &vmlinux_elf, vmlinux_data, &vmlinux, tcr_val, cr3_val, pb_hint) {
                        Ok(a) => {
                            let _ = probes_ready_evt.write(1);
                            break a;
                        }
                        Err(e) => {
                            if kill_clone.load(Ordering::Acquire) {
                                return;
                            }
                            if std::time::Instant::now() >= phase1_deadline {
                                eprintln!("bpf_map_write: accessor init timed out: {e:#}");
                                return;
                            }
                            poll_eventfd_until_ready_or_timeout(&probes_ready_evt, 200);
                        }
                    }
                };
                let accessor = owned.as_accessor();

                // Phase 2: resolve every queued map before signaling the
                // guest. All-or-nothing: if any map fails to resolve
                // within the deadline, the thread aborts without
                // signaling slot 0. The guest then proceeds under its
                // own timeout rather than observing a partial setup.
                // Running writes serially against partially-resolved
                // maps would let a late-discovery failure leave the
                // guest blocked waiting for slot 0 with no way to
                // recover.
                //
                // Same `poll(POLLIN)` pattern as phase 1: wake on a
                // sibling detection, fall back to the 200 ms cadence
                // for kill / deadline coverage; write 1 on each
                // successful map resolution to fan the wake out to
                // the monitor and the still-pending phases.
                let retry_deadline =
                    std::time::Instant::now() + std::time::Duration::from_secs(30);
                let mut resolved: Vec<(BpfMapWriteParams, monitor::bpf_map::BpfMapInfo)> =
                    Vec::with_capacity(writes.len());
                for params in writes.iter() {
                    let mut attempt = 0u32;
                    let map_info = loop {
                        attempt += 1;
                        if let Some(info) = accessor.find_map(&params.map_name_suffix) {
                            let _ = probes_ready_evt.write(1);
                            break info;
                        }
                        if kill_clone.load(Ordering::Acquire) {
                            eprintln!("bpf_map_write: VM exited during map search");
                            return;
                        }
                        if std::time::Instant::now() >= retry_deadline {
                            eprintln!(
                                "bpf_map_write: map *{} not found after {} attempts",
                                params.map_name_suffix, attempt,
                            );
                            return;
                        }
                        poll_eventfd_until_ready_or_timeout(&probes_ready_evt, 200);
                    };
                    eprintln!(
                        "bpf_map_write: map '{}' found after {} attempts",
                        map_info.name(), attempt,
                    );
                    resolved.push((params.clone(), map_info));
                }

                // Phase 3: run every queued write.
                //
                // The legacy SHM signal slot 1 (`SIGNAL_PROBES_READY`)
                // gate that waited for the guest's probe pipeline to
                // attach has been removed along with the SHM
                // signal-slot infrastructure. The writes now race
                // against probe attachment; replacing the rendezvous
                // with a virtio-console signal is a follow-up.

                // Log all maps for diagnostic visibility.
                let all_maps = accessor.maps();
                eprintln!(
                    "bpf_map_write: maps() found {} map(s): [{}]",
                    all_maps.len(),
                    all_maps
                        .iter()
                        .map(|m| format!("{}(type={})", m.name(), m.map_type))
                        .collect::<Vec<_>>()
                        .join(", "),
                );

                for (params, map_info) in &resolved {
                    let before = accessor.read_value_u32(map_info, params.offset);
                    let ok = accessor.write_value_u32(map_info, params.offset, params.value);
                    let after = accessor.read_value_u32(map_info, params.offset);
                    eprintln!(
                        "bpf_map_write: map '{}' write={} (value={} offset={} before={:?} after={:?})",
                        map_info.name(), ok, params.value, params.offset, before, after,
                    );
                }

                // Notify the guest that every queued write landed by
                // pushing `SIGNAL_BPF_WRITE_DONE` into virtio-console
                // RX. The guest's `hvc0_poll_loop` blocks on
                // `/dev/hvc0`, recognises the byte, and sets the
                // `bpf_map_write_done` latch. A scenario blocked on
                // [`crate::scenario::Ctx::wait_for_map_write`] resumes
                // when the latch fires. Replaces the legacy SHM signal
                // slot 0 notification.
                super::host_comms::request_bpf_map_write_done(&virtio_con);
                let _ = (&kill_clone, &probes_ready_evt, &mem);
            })
            .context("spawn bpf-map-write thread")?;

        Ok(Some(handle))
    }

    /// Unified BSP KVM_RUN loop. Returns `(exit_code, timed_out)`.
    ///
    /// `exit_code` semantics:
    ///   - `0` only when the BSP itself observed
    ///     [`ExitAction::Shutdown`] from `classify_exit` (i8042 reset
    ///     on x86_64, PSCI SystemEvent on aarch64, or
    ///     `VcpuExit::Shutdown`).
    ///   - `-1` is a sentinel meaning "BSP exited the loop without
    ///     observing Shutdown itself." This does NOT necessarily
    ///     indicate a failure — a peer vCPU that observed Shutdown
    ///     first sets the shared `kill` flag, and the BSP then exits
    ///     via the `kill.load(Acquire)` check at the top of the loop.
    ///     [`super::KtstrVm::collect_results`] overrides the run-loop
    ///     `exit_code` with the bulk-port `MSG_TYPE_EXIT` payload (or the
    ///     COM2 `KTSTR_EXIT:` sentinel) before constructing
    ///     [`super::result::VmResult`], so the value caller-visible
    ///     code reads is the guest's reported exit code, not this
    ///     local sentinel. [`BspExitReason`] is logged at break time
    ///     so an operator reading stderr can distinguish
    ///     "AP saw Shutdown first" from "BSP itself saw Fatal" or
    ///     "BSP run() returned a permanent error" without correlating
    ///     to other diagnostics.
    ///
    /// Handles arch-specific I/O dispatch (port I/O on x86_64, MMIO on
    /// aarch64). HLT/WFI checks the kill flag and continues (both arches).
    /// Shutdown is via PSCI SystemEvent (aarch64) or VcpuExit::Shutdown (x86_64).
    ///
    /// `freeze` and `bsp_parked` plumb the BSP into the failure-dump
    /// rendezvous: when the freeze coordinator latches `freeze=true`
    /// and kicks the BSP out of KVM_RUN, the loop performs the
    /// drain dance (set_immediate_exit(1)→run→set_immediate_exit(0)),
    /// stores `bsp_parked=true` (Release), then polls `freeze` on
    /// `park_timeout(10ms)` until the coordinator clears it. Same
    /// pattern as [`exit_dispatch::vcpu_run_loop_unified`] for APs.
    ///
    /// `watchpoint` carries the failure-dump trigger contract: each
    /// iteration polls `watchpoint.request_kva` and self-arms a
    /// hardware data-write watchpoint on `*scx_root->exit_kind` once
    /// the freeze coordinator has resolved its KVA. When the kernel
    /// later writes the field, KVM exits via `VcpuExit::Debug`; this
    /// loop sets `watchpoint.hit` so the freeze coordinator's
    /// late-trigger poll fires immediately. The arm is one-shot per
    /// KVA value (the per-vCPU `armed_kva` slot suppresses re-arms
    /// after the ioctl lands).
    ///
    /// `tcr_el1_cache` (aarch64 only) is populated lazily on first
    /// successful sysreg read after the guest kernel programs the
    /// MMU; subsequent iterations short-circuit on a non-zero
    /// cached value. Threads that build a `GuestKernel` for
    /// page-table walks load this atomic to feed the
    /// granule-agnostic walker.
    #[allow(clippy::too_many_arguments)]
    pub(super) fn run_bsp_loop(
        &self,
        bsp: &mut kvm_ioctls::VcpuFd,
        com1: &Arc<PiMutex<console::Serial>>,
        com2: &Arc<PiMutex<console::Serial>>,
        virtio_con: Option<&Arc<PiMutex<virtio_console::VirtioConsole>>>,
        virtio_blk: Option<&Arc<PiMutex<virtio_blk::VirtioBlk>>>,
        virtio_net: Option<&Arc<PiMutex<virtio_net::VirtioNet>>>,
        kill: &Arc<AtomicBool>,
        freeze: &Arc<AtomicBool>,
        watchpoint: &Arc<WatchpointArm>,
        bsp_parked: &Arc<AtomicBool>,
        bsp_regs: &Arc<std::sync::Mutex<Option<exit_dispatch::VcpuRegSnapshot>>>,
        has_immediate_exit: bool,
        _run_start: Instant,
        _timeout: Duration,
        parked_evt: Option<&Arc<EventFd>>,
        thaw_evt: Option<&Arc<EventFd>>,
        kill_evt: Option<&Arc<EventFd>>,
        tcr_el1_cache: Option<&Arc<std::sync::atomic::AtomicU64>>,
        cr3_cache: &Arc<std::sync::atomic::AtomicU64>,
        timed_out_flag: &Arc<AtomicBool>,
    ) -> (i32, bool) {
        let mut exit_code: i32 = -1;
        // Track which path drove the BSP out of the loop so the
        // post-loop log line is actionable. Without this, an operator
        // sees `code=-1 timed_out=false` and cannot distinguish
        // "external kill propagated from a peer vCPU's Shutdown" from
        // "BSP itself saw Fatal" — every non-Shutdown exit produces
        // the same `code=-1` sentinel.
        let mut exit_reason = BspExitReason::ExternalKill;
        // Per-BSP `armed_slots` mirrors the AP-side slots — see
        // [`super::vcpu::self_arm_watchpoint`]. Index 0 = slot 0
        // (exit_kind watchpoint); 1..=3 = user watchpoint slots
        // (`Op::WatchSnapshot` arms). All `0` until the coordinator
        // publishes resolved KVAs. `arm_failures` counts consecutive
        // non-EINTR ioctl failures; transient EINTR (signal race
        // with the SIGRTMIN kick path) does NOT increment so a
        // kicked-mid-arm vCPU keeps retrying instead of giving up
        // after the first racey iteration.
        let mut armed_slots: [u64; 4] = [0; 4];
        let mut arm_failures: u8 = 0;
        // aarch64 watchpoint single-step bookkeeping — mirrors the
        // AP-side state in
        // [`super::exit_dispatch::vcpu_run_loop_unified`]. The
        // aarch64 hardware watchpoint trap is taken BEFORE the
        // offending store retires (ARM ARM D2.10.5), so re-entering
        // KVM_RUN replays the same instruction unless we disable
        // the fired slot's WCR.E and assert
        // KVM_GUESTDBG_SINGLESTEP for one KVM_RUN; the next
        // KVM_EXIT_DEBUG carries EC=ESR_ELx_EC_SOFTSTP_LOW (0x32),
        // at which point the dispatch helper clears the flag and
        // `self_arm_watchpoint` restores WCR.E=1. Inert on x86_64
        // (the trap is taken AFTER the store, so re-entry advances
        // normally); the locals still pass through to keep the
        // per-arch helper signatures shared.
        let mut single_step_pending: bool = false;
        let mut single_step_slot: usize = 0;
        let mut armed_single_step: bool = false;

        loop {
            if kill.load(Ordering::Acquire) {
                break;
            }
            // Lazy TCR_EL1 cache populate (aarch64). On x86_64
            // `read_tcr_el1` returns None and the early-exit keeps
            // the atomic untouched. The kernel writes TCR_EL1 in
            // its boot-time MMU bring-up; before that the read
            // returns 0. Skip on subsequent iterations once the
            // atomic carries a non-zero value (CAS prevents races
            // with peer reads from other threads constructing a
            // `GuestKernel`).
            if let Some(cache) = tcr_el1_cache
                && cache.load(Ordering::Acquire) == 0
                && let Some(val) = exit_dispatch::read_tcr_el1(bsp)
                && val != 0
            {
                let _ = cache.compare_exchange(0, val, Ordering::Release, Ordering::Relaxed);
            }
            // CR3 / TTBR1_EL1 cache refresh. KVM_GET_SREGS at BSP
            // entry returns the boot-time CR3 (`PML4_START`, set by
            // `setup_sregs`); the kernel later overwrites this in
            // `__startup_64` after KASLR randomization. We need the
            // POST-randomization value for `phys_base` resolution
            // via page-table walk, so this MUST be a refresh
            // (overwrite each iteration), NOT a one-shot latch:
            // a "skip if non-zero" gate would freeze the cache at
            // the boot CR3 because get_sregs returns it on iter 1
            // before the guest has run `mov cr3, ...`. Accepting
            // every non-zero read also handles process context
            // switches (CR3 swaps to the new task's pgd) — the
            // kernel-half upper PML4 entries are shared across
            // every task's pgd, so any task's CR3 produces a valid
            // walk for kernel symbols. The lazy-CAS pattern still
            // gates on a non-zero `read_cr3` return so a transient
            // EINTR (None) does not zero the cache. Use a Release
            // store (not CAS) so concurrent readers see the latest
            // non-zero value.
            if let Some(val) = exit_dispatch::read_cr3(bsp)
                && val != 0
            {
                cr3_cache.store(val, Ordering::Release);
            }
            // Honour a pending freeze before re-entering KVM_RUN.
            // Same drain-dance + park pattern as the AP run loop —
            // delegated to the shared `exit_dispatch::handle_freeze`
            // so the two paths cannot drift.
            if freeze.load(Ordering::Acquire) {
                exit_dispatch::handle_freeze(
                    bsp,
                    has_immediate_exit,
                    kill,
                    freeze,
                    bsp_parked,
                    bsp_regs,
                    parked_evt.map(|a| a.as_ref()),
                    thaw_evt.map(|a| a.as_ref()),
                    kill_evt.map(|a| a.as_ref()),
                );
                if kill.load(Ordering::Acquire) {
                    break;
                }
            }
            // Self-arm the failure-dump watchpoint when the
            // coordinator has resolved a new KVA. Cheap (atomic load
            // and compare) when no new arm is pending. Also drives
            // the aarch64 watchpoint single-step transition: when
            // `single_step_pending` is set by the prior watchpoint
            // exit, this call reissues KVM_SET_GUEST_DEBUG with the
            // fired slot's WCR.E cleared and KVM_GUESTDBG_SINGLESTEP
            // asserted; when the SOFTSTP_LOW exit clears the flag,
            // the next call restores WCR.E=1 and drops the
            // singlestep bit.
            self_arm_watchpoint(
                bsp,
                watchpoint,
                &mut armed_slots,
                &mut arm_failures,
                single_step_pending,
                single_step_slot,
                &mut armed_single_step,
            );

            match bsp.run() {
                Ok(mut exit) => {
                    // HLT/WFI = kernel idle. Check kill flag, then continue.
                    // arm64 shutdown is PSCI reset (SystemEvent), not HLT.
                    if matches!(exit, VcpuExit::Hlt) {
                        if kill.load(Ordering::Acquire) {
                            break;
                        }
                        continue;
                    }
                    // KVM_EXIT_DEBUG fires when the armed hardware
                    // data-write watchpoint trips on a guest write
                    // to `*scx_root->exit_kind`. The kernel writes
                    // the field on BOTH error transitions
                    // (`scx_error -> SCX_EXIT_ERROR/_BPF/_STALL >=
                    // 1024`) AND clean shutdown
                    // (`scx_unregister -> SCX_EXIT_DONE = 1`). Only
                    // the error transitions should trigger the
                    // failure-dump freeze; firing on every clean
                    // test exit is a regression. Read the post-store
                    // value from the host pointer the coordinator
                    // published and gate `hit` on the error
                    // threshold. The watchpoint is left armed
                    // regardless — see the AP-side
                    // `vcpu_run_loop_unified` for the same
                    // rationale.
                    if let VcpuExit::Debug(debug_arch) = &exit {
                        exit_dispatch::dispatch_watchpoint_hit(
                            watchpoint,
                            debug_arch,
                            &armed_slots,
                            &mut single_step_pending,
                            &mut single_step_slot,
                        );
                        if kill.load(Ordering::Acquire) {
                            break;
                        }
                        continue;
                    }
                    match classify_exit(
                        com1,
                        com2,
                        virtio_con.map(|a| a.as_ref()),
                        virtio_blk.map(|a| a.as_ref()),
                        virtio_net.map(|a| a.as_ref()),
                        &mut exit,
                    ) {
                        Some(ExitAction::Continue) | None => {}
                        Some(ExitAction::Shutdown) => {
                            exit_code = 0;
                            exit_reason = BspExitReason::Shutdown;
                            break;
                        }
                        Some(ExitAction::Fatal(reason)) => {
                            if let Some(r) = reason {
                                tracing::error!(r, "BSP VM entry failed");
                            } else {
                                tracing::error!("BSP internal error");
                            }
                            // Propagate kill to peers and the freeze
                            // coordinator. Unlike the Shutdown arm
                            // (which exits with code=0 and lets
                            // run_vm drive the kill
                            // propagation), Fatal indicates an
                            // unrecoverable hardware/KVM failure and
                            // peers must shut down promptly rather
                            // than spinning until FREEZE_RENDEZVOUS_
                            // TIMEOUT. Mirrors the AP Fatal arm's
                            // kill-propagation in
                            // [`super::exit_dispatch::vcpu_run_loop_unified`].
                            kill.store(true, Ordering::Release);
                            if let Some(kev) = kill_evt {
                                let _ = kev.write(1);
                            }
                            exit_reason = BspExitReason::Fatal;
                            break;
                        }
                    }
                }
                Err(e) => {
                    if e.errno() == libc::EAGAIN || e.errno() == libc::EINTR {
                        if has_immediate_exit {
                            bsp.set_kvm_immediate_exit(0);
                        }
                        continue;
                    }
                    tracing::error!(%e, "BSP run failed");
                    exit_reason = BspExitReason::RunError;
                    break;
                }
            }
        }

        eprintln!("BSP: loop exit reason={exit_reason:?}");
        // The watchdog sets `timed_out_flag` only on its hard-
        // deadline branch (NOT on "kill set by AP"). Reading it
        // here propagates the watchdog's hard-timeout verdict
        // through the BSP return tuple → `VmRunState::timed_out`
        // → `VmResult::timed_out` so callers can distinguish a
        // watchdog-driven kill from a clean shutdown or a
        // panic-driven kill.
        let timed_out = timed_out_flag.load(Ordering::Acquire);
        (exit_code, timed_out)
    }

    /// Shutdown threads and collect output.
    pub(super) fn collect_results(&self, start: Instant, run: VmRunState) -> Result<VmResult> {
        // Whole-cleanup timer for the perf-repro tracing pipeline.
        // `cleanup_duration` below already records the post-BSP-exit
        // window via `run.cleanup_start.elapsed()`; this captures the
        // collect_results function span itself so a regression isolates
        // to either the run.cleanup_start window (set by run_vm before
        // it called us) or the function body.
        let collect_results_start = Instant::now();
        let mut exit_code = run.exit_code;
        let timed_out = run.timed_out;
        // Belt-and-braces: kill + kill_evt are already set by run_vm
        // immediately after BSP exits. Re-assert here in case a
        // future code path reaches collect_results without the
        // early-kill having fired. The two consumers that observe
        // kill_evt via epoll are the monitor sampler (reader.rs
        // monitor_loop) and the bpf-map-write thread (start_bpf_map_write).
        // The freeze coordinator is NOT alive here — run_vm joins it
        // before returning VmRunState. kill_evt is level-triggered
        // (EFD_NONBLOCK eventfd); the AtomicBool kill flag is the
        // source of truth that breaks each thread's outer loop.
        run.kill.store(true, Ordering::Release);
        let _ = run.kill_evt.write(1);
        // Clear freeze before kicking APs so any vCPU still in the
        // park loop observes `freeze=false` next iteration and exits
        // toward kill. Without this, an AP parked at the moment the
        // BSP exited would stay parked through the kill check, since
        // park_loop holds park_timeout(10ms) ignoring kill until
        // freeze clears.
        run.freeze.store(false, Ordering::Release);

        // Kick APs out of KVM_RUN. Skip APs that already exited —
        // their VcpuFd (and kvm_run mmap) may be dropped, so writing
        // to ImmediateExitHandle would hit unmapped memory. Unpark
        // each so a parked AP observes the cleared freeze flag
        // promptly without waiting for the 10ms park_timeout.
        for vt in &run.ap_threads {
            if !vt.exited.load(Ordering::Acquire) {
                vt.kick();
            }
            vt.handle.thread().unpark();
        }
        // The freeze coordinator was joined inside `run_vm` BEFORE
        // bsp dropped (preventing UAF on the BSP ImmediateExitHandle),
        // so `run.freeze_coordinator` is always `None` here. The
        // `Option`-typed field is preserved for backward compatibility
        // with paths that may construct VmRunState differently in
        // the future; the conditional join below is a no-op for the
        // `None` arm.
        if let Some(h) = run.freeze_coordinator {
            let _ = h.join();
        }
        {
            let mut remaining = run.ap_threads.len();
            if remaining > 0
                && let Ok(epoll) = Epoll::new()
            {
                for (i, vt) in run.ap_threads.iter().enumerate() {
                    if vt.exited.load(Ordering::Acquire) {
                        remaining -= 1;
                        continue;
                    }
                    let _ = epoll.ctl(
                        ControlOperation::Add,
                        vt.exit_evt.as_raw_fd(),
                        EpollEvent::new(EventSet::IN, i as u64),
                    );
                }
                if remaining > 0 {
                    let mut events = vec![EpollEvent::default(); remaining];
                    let deadline = Instant::now() + Duration::from_secs(2);
                    while remaining > 0 {
                        let left = deadline.saturating_duration_since(Instant::now());
                        if left.is_zero() {
                            break;
                        }
                        let ms = left.as_millis().min(i32::MAX as u128) as i32;
                        match epoll.wait(ms, &mut events) {
                            Ok(0) => break,
                            Ok(n) => remaining = remaining.saturating_sub(n),
                            Err(_) => break,
                        }
                        for vt in &run.ap_threads {
                            if !vt.exited.load(Ordering::Acquire) {
                                vt.kick();
                            }
                        }
                    }
                }
            }
            for vt in run.ap_threads {
                let _ = vt.handle.join();
            }
        }
        eprintln!("CLEANUP: all AP threads joined");

        // Invalidate the watchpoint slots BEFORE `run.vm` drops at
        // the end of this function. `kind_host_ptr` addresses a host
        // u32 inside `vm.guest_mem`'s mmap-backed mapping; once
        // `vm.guest_mem` drops, that mapping unmaps and dereffing
        // `kind_host_ptr` would touch unmapped memory.
        // `request_kva` is the paired guest-side KVA whose
        // translation goes through the same mapping. By this point
        // every vCPU thread has joined (the loop above blocked on
        // each `wait_for_exit` + `handle.join`) and the freeze
        // coordinator joined back in `run_vm` before `bsp` dropped,
        // so no live thread reads either field. The defense-in-depth
        // store here zeroes the slots so a stray future Arc clone
        // (or a follow-up that adds a new reader after teardown)
        // sees a sentinel `null_mut` / `0` that
        // [`super::exit_dispatch::latch_slot0_with_gate`] already
        // gates on, instead of dangling host memory. `Release`
        // ordering pairs with the `Acquire` reads inside the latch
        // path so any future reader sees a coherent view of the
        // invalidation.
        run.watchpoint
            .kind_host_ptr
            .store(std::ptr::null_mut(), Ordering::Release);
        run.watchpoint.request_kva.store(0, Ordering::Release);
        // Mirror the slot-0 invalidation across every user
        // watchpoint slot (1..=3, `Op::WatchSnapshot` arms). A
        // future reader that walks `watchpoint.user[..]` sees the
        // same `request_kva == 0` sentinel as slot 0 — the
        // resolved KVA is no longer reachable from any slot. `hit`
        // is also cleared so a stray Acquire load after teardown
        // observes "no fire pending" instead of a stale latch from
        // an earlier run that no longer has a captured report.
        // `Release` pairs with the `Acquire` reads in
        // `arm_user_watchpoint` and the latch path.
        for slot in &run.watchpoint.user {
            slot.request_kva.store(0, Ordering::Release);
            slot.hit.store(false, Ordering::Release);
        }

        let (monitor_report, mid_flight_drain) =
            match run.monitor_handle.and_then(|h| h.join().ok()) {
                Some(monitor::reader::MonitorLoopResult {
                    samples,
                    drain,
                    watchdog_observation,
                    page_offset,
                    preemption_threshold_ns,
                }) => {
                    // `preemption_threshold_ns` was resolved once
                    // inside `start_monitor` (and threaded through
                    // `monitor_loop`'s 0-fallback) so the cleanup
                    // path does NOT re-read the vmlinux to recompute
                    // CONFIG_HZ. The previous structure called
                    // `monitor::vcpu_preemption_threshold_ns(Some(
                    // &self.kernel))` here, which re-read the
                    // vmlinux ELF every cleanup just to derive the
                    // same value the monitor thread already had in
                    // hand.
                    let summary = monitor::MonitorSummary::from_samples_with_threshold(
                        &samples,
                        preemption_threshold_ns,
                    );
                    let report = monitor::MonitorReport {
                        samples,
                        summary,
                        preemption_threshold_ns,
                        watchdog_observation,
                        page_offset,
                    };
                    (Some(report), drain)
                }
                None => (None, BulkDrainResult::default()),
            };
        eprintln!("CLEANUP: monitor joined");
        let cleanup_t = std::time::Instant::now();

        if let Some(h) = run.bpf_write_handle {
            let _ = h.join();
        }
        eprintln!("CLEANUP: bpf_write joined {:?}", cleanup_t.elapsed());

        // Drain the virtio-console port-1 TX accumulator: the guest
        // wrote bulk TLV-framed messages (STIMULUS, EXIT, SCHED_EXIT,
        // PAYLOAD_METRICS, RAW_PAYLOAD_OUTPUT, etc.) to
        // `/dev/vport0p1`; the host side accumulated them into
        // `port1_tx_buf` and we parse them here through
        // `parse_tlv_stream`. Port-1 uses backpressure rather than
        // drops — every byte the guest emitted is delivered, in
        // order.
        //
        // `final_drain` (rather than `drain_bulk`) walks the avail
        // ring once before draining so chains the guest published
        // without a host-observed QUEUE_NOTIFY (the
        // `force_reboot()` race in `rust_init`'s `send_exit`-then-
        // reboot tail) are picked up instead of being lost. See
        // [`crate::vmm::virtio_console::VirtioConsole::final_drain`].
        let bulk_bytes = run.virtio_con.lock().final_drain();
        let mut bulk_drain = host_comms::parse_tlv_stream(&bulk_bytes);
        // Strip coordinator-internal control frames the freeze coord
        // mid-run filter (the TOKEN_TX dispatch in this same file)
        // already drops: SNAPSHOT_REQUEST has its matching reply
        // delivered over port-1 RX; SYS_RDY's only semantic is the
        // eventfd promotion in the coord's TOKEN_TX handler.
        // Without this filter, a late-arriving control frame that
        // the coord had not yet consumed when its outer loop
        // exited would land in `guest_messages` and surface as a
        // phantom verdict entry.
        //
        // Both filters key on
        // [`crate::vmm::wire::MsgType::is_coordinator_internal`] —
        // a single source of truth so adding a new internal control
        // frame is a one-line update at the classifier site.
        bulk_drain.entries.retain(|e| {
            // Keep when the msg_type is NOT a recognised
            // coordinator-internal control frame. Unknown
            // msg_types (None) are preserved verbatim so an
            // operator-side analyser can surface them rather
            // than silently dropping them here.
            match crate::vmm::wire::MsgType::from_wire(e.msg_type) {
                Some(t) => !t.is_coordinator_internal(),
                None => true,
            }
        });
        // Prepend the entries the freeze coordinator already parsed
        // mid-run. The coord's TOKEN_TX handler streams port-1
        // bytes through `HostAssembler` so a SCHED_EXIT can flip
        // the run-wide kill flag without waiting for VM exit;
        // those parsed frames stash here on every drain so
        // `collect_results` can recover them after the coord has
        // joined. Without this merge every guest-side EXIT / TEST
        // / PAYLOAD_METRICS / RAW_PAYLOAD_OUTPUT / PROFRAW frame
        // consumed mid-run would be silently lost — `drain_bulk()`
        // above only catches what arrived AFTER the coord stopped
        // polling, which on a typical run is empty. Mid-run
        // entries come first so the merged stream stays in
        // chronological order.
        let mut mid_run_bulk = match run.bulk_messages.lock() {
            Ok(mut g) => std::mem::take(&mut *g),
            Err(p) => std::mem::take(&mut *p.into_inner()),
        };
        mid_run_bulk.extend(bulk_drain.entries);
        bulk_drain.entries = mid_run_bulk;

        // Merge mid-flight drain (from monitor thread, port-1 byte
        // stream) with the post-exit `drain_bulk()`. Mid-flight
        // entries come first since they were drained during
        // execution.
        let (guest_messages, stimulus_events) =
            if !mid_flight_drain.entries.is_empty() || !bulk_drain.entries.is_empty() {
                let mut all_entries = mid_flight_drain.entries;
                all_entries.extend(bulk_drain.entries);
                let events: Vec<wire::StimulusEvent> = all_entries
                    .iter()
                    .filter(|e| e.msg_type == wire::MSG_TYPE_STIMULUS && e.crc_ok)
                    .filter_map(|e| wire::StimulusEvent::from_payload(&e.payload))
                    .collect();
                (
                    Some(BulkDrainResult {
                        entries: all_entries,
                    }),
                    events,
                )
            } else {
                (None, Vec::new())
            };

        let com2_bytes = run.com2.lock().output();
        let console_output = run.com1.lock().output();

        // Concatenate every CRC-valid `MSG_TYPE_STDOUT` /
        // `MSG_TYPE_STDERR` chunk from the bulk-port drain into a
        // single string and prepend the COM2 capture so panic-hook
        // bytes (the lone remaining COM2 writer) still surface in
        // `result.output`. The bulk-port chunks dominate steady-state
        // test output; COM2 is reserved for fault diagnostics that
        // cannot block on virtio backpressure.
        let mut app_output = String::new();
        if let Some(ref drain) = guest_messages {
            for e in &drain.entries {
                if !e.crc_ok {
                    continue;
                }
                match wire::MsgType::from_wire(e.msg_type) {
                    Some(wire::MsgType::Stdout) | Some(wire::MsgType::Stderr) => {
                        app_output.push_str(&String::from_utf8_lossy(&e.payload));
                    }
                    _ => {}
                }
            }
        }
        if !com2_bytes.is_empty() {
            app_output.push_str(&com2_bytes);
        }

        // Extract exit code: bulk port (primary), COM2 sentinel (fallback).
        let bulk_exit = guest_messages.as_ref().and_then(|d| {
            d.entries
                .iter()
                .rev()
                .find(|e| e.msg_type == wire::MSG_TYPE_EXIT && e.crc_ok && e.payload.len() == 4)
                .map(|e| i32::from_ne_bytes(e.payload[..4].try_into().unwrap()))
        });
        // Pre-bincode-migration: a COM2 `KTSTR_EXIT=N` sentinel line
        // served as the fallback when no binary `MSG_TYPE_EXIT`
        // frame arrived. The fallback is gone — bulk-port
        // backpressure guarantees delivery, and the guest no longer
        // emits the sentinel. A `None` here keeps `exit_code` at
        // whatever the BSP run-loop's local stored, matching the
        // pre-fallback path.
        if let Some(code) = bulk_exit {
            exit_code = code;
        }

        // Extract crash message from COM2 output. The guest panic
        // hook in `rust_init.rs` writes `PANIC: <info>\n<bt>\n` to
        // `/dev/ttyS1`; the host-side parser
        // [`crate::test_support::extract_panic_message`] strips the
        // prefix and returns the trimmed remainder.
        let crash_message =
            crate::test_support::extract_panic_message(&app_output).map(|s| s.to_string());

        // Collect BPF verifier stats from host-side memory reads.
        // Skip when no scheduler is active — struct_ops programs
        // only exist when a sched_ext scheduler attached (either via
        // a userspace binary or kernel-built enable commands).
        let has_scheduler = self.scheduler_binary.is_some() || !self.sched_enable_cmds.is_empty();
        let vs_t = std::time::Instant::now();
        let mut vs_path: &'static str = "skipped_no_scheduler";
        let verifier_stats = if has_scheduler {
            if let Some(ref prog) = run.prog_accessor {
                use crate::monitor::bpf_prog::BpfProgAccessor;
                vs_path = "prebuilt_accessor";
                eprintln!("CLEANUP: verifier_stats using pre-built accessor");
                let a = prog.as_accessor();
                a.struct_ops_progs()
            } else {
                vs_path = "fallback_full_parse";
                eprintln!("CLEANUP: verifier_stats fallback (full parse)");
                self.collect_verifier_stats(
                    &run.vm,
                    run.tcr_el1.as_ref(),
                    &run.cr3,
                    run.vmlinux_data.as_ref().map(|d| d.as_slice()),
                    run.kern_phys_base,
                )
            }
        } else {
            Vec::new()
        };
        tracing::info!(
            elapsed_ms = vs_t.elapsed().as_millis() as u64,
            path = vs_path,
            n_progs = verifier_stats.len(),
            "auto_repro: collect_verifier_stats",
        );
        eprintln!("CLEANUP: verifier_stats done {:?}", vs_t.elapsed());

        // Sample cleanup elapsed AFTER every blocking step that runs on
        // the post-BSP-exit critical path so the duration captures the
        // full host-side teardown cost, not a partial window. The full
        // ordered set is: watchdog join (in `run_vm`, before
        // `cleanup_start` is stored on `VmRunState`), AP joins, monitor
        // join, BPF writer join, bulk drain, exit-code and crash-message
        // extraction, verifier-stat read. Captured before constructing
        // the result so the `Instant::now()` here is the latest possible
        // read.
        let cleanup_duration = Some(run.cleanup_start.elapsed());
        tracing::info!(
            elapsed_ms = collect_results_start.elapsed().as_millis() as u64,
            cleanup_window_ms = cleanup_duration.map(|d| d.as_millis() as u64).unwrap_or(0),
            "auto_repro: collect_results",
        );
        eprintln!("CLEANUP: collect_results done {:?}", cleanup_t.elapsed());

        // Forward the scheduler-stats client. `run.stats_client` is
        // `Some(_)` when the run has a scheduler attached and
        // `None` otherwise; the field on `VmResult.stats_client`
        // mirrors this exactly. The drainer thread (when present)
        // continues to run until the last `Arc<ClientShared>` clone
        // drops; `Drop` on the field then writes the kill eventfd
        // and the drainer thread exits.
        let stats_client = run.stats_client;

        Ok(VmResult {
            success: !timed_out && exit_code == 0,
            exit_code,
            duration: start.elapsed(),
            timed_out,
            output: app_output,
            stderr: console_output,
            monitor: monitor_report,
            guest_messages,
            stimulus_events,
            verifier_stats,
            kvm_stats: None,
            crash_message,
            cleanup_duration,
            virtio_blk_counters: run.virtio_blk_counters,
            virtio_net_counters: run.virtio_net_counters,
            snapshot_bridge: run.snapshot_bridge,
            stats_client,
            periodic_fired: run.periodic_fired,
            periodic_target: run.periodic_target,
        })
    }

    /// Read BPF verifier stats from guest memory after VM exit.
    ///
    /// Enumerates struct_ops programs in the kernel's `prog_idr` and
    /// reads `bpf_prog_aux->verified_insns` for each.
    pub(super) fn collect_verifier_stats(
        &self,
        vm: &kvm::KtstrKvm,
        tcr_el1: Option<&Arc<std::sync::atomic::AtomicU64>>,
        cr3: &Arc<std::sync::atomic::AtomicU64>,
        cached_vmlinux_data: Option<&[u8]>,
        kern_phys_base_biased: u64,
    ) -> Vec<monitor::bpf_prog::ProgVerifierStats> {
        let vmlinux = match find_vmlinux(&self.kernel) {
            Some(v) => v,
            None => return Vec::new(),
        };
        let mem = match vm.numa_layout.as_ref() {
            Some(layout) => monitor::reader::GuestMem::from_layout(layout, &vm.guest_mem),
            None => {
                use vm_memory::GuestMemoryRegion;
                let host_base = match vm.guest_mem.get_host_address(GuestAddress(DRAM_BASE)) {
                    Ok(ptr) => ptr,
                    Err(_) => return Vec::new(),
                };
                // Size of the first contiguous region only.
                // host_base addresses that single mapping; using the
                // sum of all region lengths would extend past the
                // mapping into host heap when multiple regions exist.
                let mem_size = match vm.guest_mem.iter().next() {
                    Some(r) => r.len(),
                    None => return Vec::new(),
                };
                // SAFETY: host_base is from GuestMemoryMmap's mapping,
                // which outlives this GuestMem (borrowed via `vm` for
                // the body of this function).
                unsafe { monitor::reader::GuestMem::new(host_base, mem_size) }
            }
        };
        // TCR_EL1 (aarch64) drives the granule-agnostic page-table
        // walker. The BSP populates this Arc<AtomicU64> on first
        // successful read post-MMU-bringup; by collect_verifier_stats
        // time it is either set (kernel booted) or 0 (kernel never
        // brought MMU up, e.g. early boot crash). The walker treats
        // 0 as "no TCR available — translation unsupported", which
        // matches the boot-crash case where verifier stats are
        // unavailable anyway.
        let tcr_val = tcr_el1
            .map(|c| c.load(std::sync::atomic::Ordering::Acquire))
            .unwrap_or(0);
        let cr3_val = cr3.load(std::sync::atomic::Ordering::Acquire);
        // Fallback when the caller did not pre-load the vmlinux ELF.
        // Routes through `cached_vmlinux_bytes` so a test process that
        // boots N VMs against the same kernel pays the 50-340 MB read
        // exactly once. Without the cache this path was the dominant
        // cost on `collect_results` cleanup for nextest runs of
        // `#[ktstr_test]` cases that share a kernel.
        let owned_data;
        let vmlinux_data: &[u8] = match cached_vmlinux_data {
            Some(d) => d,
            None => match cached_vmlinux_bytes(&vmlinux) {
                Some(arc) => {
                    owned_data = arc;
                    owned_data.as_slice()
                }
                None => return Vec::new(),
            },
        };
        // Parse the vmlinux ELF once and share the result between
        // `GuestKernel` (kernel symbols + paging state) and
        // `BpfProgOffsets` (BTF section extraction on cache miss).
        // The previous structure parsed the ELF up to three times per
        // call: once inside `GuestKernel::from_vmlinux_bytes`, once
        // again via the nested `KernelSymbols::from_vmlinux_bytes`,
        // and once more via `load_btf_from_bytes` on a sidecar miss.
        // `goblin::elf::Elf::parse` is hundreds of ms on a debug
        // vmlinux, so this single parse is the cheap shared base.
        let elf = match goblin::elf::Elf::parse(vmlinux_data) {
            Ok(e) => e,
            Err(_) => return Vec::new(),
        };
        let pb_hint = if kern_phys_base_biased != 0 {
            kern_phys_base_biased.wrapping_sub(1)
        } else {
            0
        };
        let kernel = match monitor::guest::GuestKernel::from_elf_with_hint(
            Arc::new(mem),
            &elf,
            tcr_val,
            cr3_val,
            pb_hint,
        ) {
            Ok(k) => k,
            Err(_) => return Vec::new(),
        };
        // BTF sidecar cache hits skip ELF traversal entirely; on a
        // miss `load_btf_from_elf` reuses the parse above instead of
        // re-running `goblin::elf::Elf::parse(&vmlinux_data)`.
        let offsets =
            match monitor::btf_offsets::BpfProgOffsets::from_elf(&elf, vmlinux_data, &vmlinux) {
                Ok(o) => o,
                Err(_) => return Vec::new(),
            };
        let accessor =
            match monitor::bpf_prog::GuestMemProgAccessor::from_guest_kernel(&kernel, &offsets) {
                Ok(a) => a,
                Err(_) => return Vec::new(),
            };
        // Trait method — `BpfProgAccessor::struct_ops_progs` is in
        // scope at the call site via the `use monitor::bpf_prog::*`
        // glob (see top of file); calling it on the concrete type
        // dispatches statically.
        use monitor::bpf_prog::BpfProgAccessor;
        accessor.struct_ops_progs()
    }
}

#[cfg(test)]
mod crc_defense_tests;
#[cfg(test)]
mod rendezvous_tests;
#[cfg(test)]
mod snapshot_tlv_tests;
#[cfg(test)]
mod tx_dispatch_tests;

#[cfg(test)]
mod utf8_safe_truncate_len_tests {
    //! Unit coverage for [`utf8_safe_truncate_len`] — the stderr
    //! last-ditch preservation helper used by the failure-dump
    //! emit_json / emit_degraded_json closures and the end-of-coord
    //! drain. The function exists to prevent a panic when naive
    //! byte-slicing `&json[..cap]` would land inside a multi-byte
    //! UTF-8 sequence (U+FFFD from task `comm` lossy decode, kernel
    //! symbol Unicode, operator-supplied tags). This helper is what
    //! KEEPS the stderr-fallback path from converting a silent drop
    //! into a panic that skips coord cleanup. Coverage here pins the
    //! correctness of every UTF-8 boundary scenario the production
    //! callers can encounter.
    use super::utf8_safe_truncate_len;

    /// Empty input always returns 0 — `&""[..0]` is a valid empty
    /// slice. No walk needed.
    #[test]
    fn empty_input_returns_zero() {
        assert_eq!(utf8_safe_truncate_len("", 0), 0);
        assert_eq!(utf8_safe_truncate_len("", 16), 0);
        assert_eq!(utf8_safe_truncate_len("", usize::MAX), 0);
    }

    /// Cap zero always returns 0 — degenerate "no payload" case.
    /// The drain's stderr-truncation path with cap=0 would emit an
    /// empty head, which is acceptable (the structured summary
    /// line preserves the operator-critical signals).
    #[test]
    fn cap_zero_returns_zero() {
        assert_eq!(utf8_safe_truncate_len("hello", 0), 0);
        assert_eq!(utf8_safe_truncate_len("\u{FFFD}", 0), 0);
    }

    /// Cap >= str length returns str length (entire string is a
    /// valid prefix). No truncation needed; caller emits everything.
    #[test]
    fn cap_at_or_above_len_returns_full_len() {
        assert_eq!(utf8_safe_truncate_len("hi", 2), 2);
        assert_eq!(utf8_safe_truncate_len("hi", 5), 2);
        assert_eq!(utf8_safe_truncate_len("hi", usize::MAX), 2);
    }

    /// Pure-ASCII input: cap inside the string returns cap exactly
    /// (every byte index is a char boundary in ASCII).
    #[test]
    fn ascii_cap_within_len_returns_cap_exactly() {
        let s = "abcdefghij"; // 10 bytes
        assert_eq!(utf8_safe_truncate_len(s, 0), 0);
        assert_eq!(utf8_safe_truncate_len(s, 1), 1);
        assert_eq!(utf8_safe_truncate_len(s, 5), 5);
        assert_eq!(utf8_safe_truncate_len(s, 9), 9);
        assert_eq!(utf8_safe_truncate_len(s, 10), 10);
    }

    /// REPLACEMENT CHARACTER U+FFFD is 3 bytes in UTF-8
    /// (`EF BF BD`). This is the exact byte sequence
    /// `String::from_utf8_lossy` (used by task `comm` decode at
    /// `task_enrichment.rs::read_comm`) substitutes for invalid
    /// input — the most realistic vector for the original panic.
    /// Cap inside the 3-byte sequence must walk back to the
    /// boundary BEFORE the sequence.
    #[test]
    fn walks_back_past_replacement_char() {
        // "a" (1 byte) + U+FFFD (3 bytes) + "b" (1 byte) = 5 bytes
        let s = "a\u{FFFD}b";
        assert_eq!(s.len(), 5);
        assert_eq!(utf8_safe_truncate_len(s, 0), 0);
        // Cap 1 lands at "a" boundary — valid.
        assert_eq!(utf8_safe_truncate_len(s, 1), 1);
        // Cap 2 lands INSIDE U+FFFD (byte 1 of 3) — walks back to 1.
        assert_eq!(utf8_safe_truncate_len(s, 2), 1);
        // Cap 3 lands INSIDE U+FFFD (byte 2 of 3) — walks back to 1.
        assert_eq!(utf8_safe_truncate_len(s, 3), 1);
        // Cap 4 lands at the boundary AFTER U+FFFD — valid.
        assert_eq!(utf8_safe_truncate_len(s, 4), 4);
        // Cap 5 lands at end — valid.
        assert_eq!(utf8_safe_truncate_len(s, 5), 5);
    }

    /// 4-byte UTF-8 sequence (the maximum per RFC 3629). The walk
    /// must retreat at most 3 bytes to find a valid boundary —
    /// pins the worst-case retreat distance the doc claims.
    #[test]
    fn walks_back_past_4byte_sequence() {
        // U+1F600 GRINNING FACE = "\u{1F600}" = 0xF0 0x9F 0x98 0x80
        // = 4 bytes. Surround with ASCII to test boundary walks.
        let s = "x\u{1F600}y";
        assert_eq!(s.len(), 6);
        // Cap 1 at "x" boundary.
        assert_eq!(utf8_safe_truncate_len(s, 1), 1);
        // Caps 2/3/4 inside the 4-byte sequence — all walk back to 1.
        assert_eq!(utf8_safe_truncate_len(s, 2), 1);
        assert_eq!(utf8_safe_truncate_len(s, 3), 1);
        assert_eq!(utf8_safe_truncate_len(s, 4), 1);
        // Cap 5 at the boundary after the 4-byte sequence — valid.
        assert_eq!(utf8_safe_truncate_len(s, 5), 5);
        // Cap 6 at end — valid.
        assert_eq!(utf8_safe_truncate_len(s, 6), 6);
    }

    /// Slicing the returned head must not panic for any input /
    /// cap combination. Property-style coverage of the central
    /// safety guarantee: caller does `&s[..returned]` after the
    /// helper; this must never trigger Rust's char-boundary panic.
    /// Iterates a small set of inputs and caps, asserts no panic.
    #[test]
    fn slice_with_returned_len_never_panics() {
        let inputs = [
            "",
            "a",
            "\u{FFFD}",
            "a\u{FFFD}b",
            "\u{FFFD}\u{FFFD}\u{FFFD}",
            "x\u{1F600}y",
            "Hello, 世界! \u{1F600}",
        ];
        for s in inputs {
            for cap in [0, 1, 2, 3, 4, 5, 10, 100, usize::MAX] {
                let head_end = utf8_safe_truncate_len(s, cap);
                // The actual safety guarantee: this slice must not
                // panic. We assign to `_` to make the side effect
                // explicit; the slice operation itself is the
                // assertion.
                let _ = &s[..head_end];
            }
        }
    }
}

#[cfg(test)]
mod vcpu_none_indices_tests {
    //! Unit coverage for [`vcpu_none_indices`] — surfaces WHICH vCPUs
    //! failed to park during freeze rendezvous (or whose KVM_GET_REGS
    //! errored mid-shutdown) in every structured-summary stderr
    //! preservation line. An operator needs the index list, not just
    //! the count, to triage a hung VM — knowing "vCPUs [1, 3] are
    //! stalled" tells the operator which vCPU threads to inspect in
    //! the kernel log or `perf top -p`; "2 vCPUs missing" does not.
    use super::vcpu_none_indices;
    use crate::monitor::dump::VcpuRegSnapshot;

    // Build a synthetic snapshot for the Some arm. The field values
    // don't matter for this test — only the Some/None distinction
    // drives the helper's filter_map. Constructed in-crate so
    // `#[non_exhaustive]` doesn't block; tcr_el1/user_page_table_root
    // default to None (arch-conditional fields).
    fn snap() -> VcpuRegSnapshot {
        VcpuRegSnapshot {
            instruction_pointer: 0,
            stack_pointer: 0,
            page_table_root: 0,
            user_page_table_root: None,
            tcr_el1: None,
        }
    }

    /// Empty input — degenerate case (would happen if the monitor
    /// crashed before the per-vCPU snapshot vec was allocated).
    /// Returns empty Vec; caller renders `vcpu_none_indices=[]` —
    /// correct, there are no vCPUs to call "none".
    #[test]
    fn empty_vec_returns_empty() {
        let regs: Vec<Option<VcpuRegSnapshot>> = vec![];
        assert_eq!(vcpu_none_indices(&regs), Vec::<usize>::new());
    }

    /// All vCPUs parked + register-captured successfully — happy
    /// path. Returns empty Vec; operator sees `vcpu_none_indices=[]`
    /// signaling zero stalled vCPUs.
    #[test]
    fn all_some_returns_empty() {
        let regs = vec![Some(snap()), Some(snap()), Some(snap())];
        assert_eq!(vcpu_none_indices(&regs), Vec::<usize>::new());
    }

    /// All vCPUs failed (e.g. all `KVM_GET_REGS` errored or rendezvous
    /// timed out before any vCPU parked) — returns every index. The
    /// worst-case stall scenario; operator triages by re-running with
    /// an extended freeze timeout or reading kernel logs for the
    /// affected vCPU threads.
    #[test]
    fn all_none_returns_all_indices() {
        let regs: Vec<Option<VcpuRegSnapshot>> = vec![None, None, None];
        assert_eq!(vcpu_none_indices(&regs), vec![0, 1, 2]);
    }

    /// Mixed Some/None — the realistic partial-stall case. Pins the
    /// asymmetry: indices 1 and 3 stalled, 0 and 2 succeeded. A bug
    /// that swapped the filter polarity (`r.is_some()` instead of
    /// `r.is_none()`) would return [0, 2] here and would vacuously
    /// pass the `all_some` / `all_none` cases — this is the case
    /// that catches a polarity flip.
    #[test]
    fn mixed_returns_only_none_indices() {
        let regs = vec![Some(snap()), None, Some(snap()), None];
        assert_eq!(vcpu_none_indices(&regs), vec![1, 3]);
    }
}

#[cfg(test)]
mod format_path_part_tests {
    //! Unit coverage for [`format_path_part`] — the trace-summary
    //! disambiguator at the tail of the emit_json + emit_degraded_json
    //! closures. Three operator-visible states must render distinctly
    //! so a reader of the tracing::info!/warn! line alone (without
    //! scrolling to the preceding atomic-write-failed tracing::warn!)
    //! can correctly attribute the outcome. Misattribution of an FS
    //! error to "operator forgot to configure a sink" is the
    //! operator-misread class this helper prevents.
    use super::format_path_part;

    /// Successful write to a configured sink — renders ` -> {p}` so
    /// the operator sees the on-disk dump path inline with the
    /// summary.
    #[test]
    fn success_renders_path_arrow() {
        assert_eq!(
            format_path_part(Some("/tmp/dump.json"), false),
            " -> /tmp/dump.json"
        );
    }

    /// Sink configured but atomic write failed (ENOSPC / EROFS /
    /// EACCES) — renders the "see preceding warn" hint so the
    /// operator knows to scroll up for the underlying io::Error.
    #[test]
    fn write_failed_no_path_directs_to_warn() {
        assert_eq!(
            format_path_part(None, true),
            " (atomic write failed; see preceding warn)"
        );
    }

    /// Operator never wired `failure_dump_path` (verifier / shell /
    /// template default = None) — renders the quiet expected-behavior
    /// case. No capture was requested, no error to report.
    #[test]
    fn no_sink_renders_no_file_sink() {
        assert_eq!(format_path_part(None, false), " (no file sink)");
    }

    /// Defensive case — sink configured + write_failed flag set.
    /// UNREACHABLE by construction: the and_then Err arm in both
    /// emit closures sets `write_failed = true` AND returns None,
    /// while the Ok arm returns Some and never sets write_failed.
    /// If a future refactor breaks that invariant, the `_` wildcard
    /// renders the success arrow rather than misclassifying — the
    /// path string is more useful than a write-failure marker when
    /// both are present.
    #[test]
    fn defensive_path_with_write_failed_renders_arrow() {
        assert_eq!(format_path_part(Some("/x"), true), " -> /x");
    }
}

#[cfg(test)]
mod format_truncation_marker_tests {
    //! Unit coverage for [`format_truncation_marker`] — the stderr
    //! payload-head suffix used by all 3 stderr-fallback sites
    //! (emit_json / emit_degraded_json / end-of-coord drain). When
    //! the safe-truncated head drops bytes, the operator must see a
    //! marker that quantifies the loss ("truncated to N bytes; full
    //! M bytes lost — summary above") so they know the preceding
    //! summary line is the authoritative signal source, not the
    //! (truncated) payload head.
    use super::format_truncation_marker;

    /// No truncation — head_end reached the full payload. Returns
    /// empty so the caller's `eprintln!` reads cleanly without a
    /// no-op clause cluttering the output.
    #[test]
    fn no_truncation_returns_empty() {
        assert_eq!(format_truncation_marker(100, 100), "");
    }

    /// Truncation happened — head_end shorter than total. Renders
    /// the "(truncated to N bytes at UTF-8 boundary; D of M payload
    /// bytes dropped — summary above)" marker with the head size,
    /// dropped byte count, and total payload size populated. The
    /// dropped count is `total - head_end` and is rendered explicitly
    /// so an operator does not have to do mental arithmetic to
    /// recover how much of the dump was lost.
    #[test]
    fn truncation_renders_byte_counts() {
        let marker = format_truncation_marker(16, 1024);
        assert_eq!(
            marker,
            " (truncated to 16 bytes at UTF-8 boundary; 1008 of 1024 payload bytes dropped — summary above)"
        );
    }

    /// Empty payload + zero head — degenerate case (no payload to
    /// truncate). Returns empty (no-truncation branch).
    #[test]
    fn empty_payload_zero_head_returns_empty() {
        assert_eq!(format_truncation_marker(0, 0), "");
    }

    /// Defensive case — head_end claims more bytes than total. UNREACHABLE
    /// by construction: utf8_safe_truncate_len caps `bounded` at
    /// `cap.min(s.len())` so the returned head_end is always ≤ total.
    /// If a future refactor breaks that invariant, the helper returns
    /// empty (no-truncation branch) rather than emitting a nonsense
    /// marker with negative loss — the safer degradation.
    #[test]
    fn defensive_head_above_total_returns_empty() {
        assert_eq!(format_truncation_marker(200, 100), "");
    }
}

#[cfg(test)]
mod write_to_tagged_path_tests {
    //! Unit coverage for [`write_to_tagged_path`] — the on-demand
    //! dispatch sites' atomic-publish helper. When an operator wires
    //! `failure_dump_path`, the on-demand snapshot dispatch sites
    //! (TLV CAPTURE / periodic / user-watchpoint / early-snapshot
    //! Degraded) must surface visible signal regardless of whether
    //! the FS write succeeds — same contract emit_json + the drain
    //! enforce. Coverage here pins the no-sink, success, and
    //! write-failure branches.
    use super::write_to_tagged_path;
    use std::sync::atomic::{AtomicBool, Ordering};

    /// `dump_path: None` — verifier / shell / template iteration.
    /// Helper returns `Ok(None)` without touching the stderr_summary
    /// closure. Pins the "no capture requested" silent-skip
    /// invariant — this case is NOT a silent drop because no capture
    /// was ever requested.
    #[test]
    fn no_sink_returns_ok_none_without_invoking_stderr_summary() {
        let summary_called = AtomicBool::new(false);
        let result = write_to_tagged_path(
            None,
            "test_tag",
            "{\"k\":\"v\"}",
            || {
                summary_called.store(true, Ordering::Relaxed);
                String::from("test summary")
            },
            "test warn msg",
        );
        assert!(matches!(result, Ok(None)));
        assert!(
            !summary_called.load(Ordering::Relaxed),
            "stderr_summary closure must NOT fire on no-sink path"
        );
    }

    /// Happy path — sink wired, payload written atomically.
    /// Returns `Ok(Some(tagged))`, file content matches the JSON
    /// byte-for-byte, the stderr_summary closure is NOT invoked
    /// (no fallback needed). Tagged path matches
    /// `snapshot_tagged_path`'s output — the helper does not invent
    /// its own naming scheme, so the on-demand sites' tag layout
    /// stays consistent with what the existing late-trigger path
    /// produces.
    #[test]
    fn successful_write_publishes_tagged_path_with_content() {
        let dir = tempfile::tempdir().expect("tempdir");
        let base = dir.path().join("dump.failure-dump.json");
        let payload = "{\"schema\":\"single\",\"k\":\"v\"}";
        let summary_called = AtomicBool::new(false);
        let result = write_to_tagged_path(
            Some(&base),
            "ondemand_test",
            payload,
            || {
                summary_called.store(true, Ordering::Relaxed);
                String::from("test summary")
            },
            "test warn msg",
        );
        let tagged = result.expect("write succeeds").expect("path returned");
        assert!(
            !summary_called.load(Ordering::Relaxed),
            "stderr_summary must NOT fire on successful write"
        );
        let written = std::fs::read_to_string(&tagged).expect("read tagged");
        assert_eq!(written, payload);
        let expected = super::snapshot_tagged_path(&base, "ondemand_test");
        assert_eq!(tagged, expected);
        let tmp = tagged.with_extension("json.tmp");
        assert!(!tmp.exists(), "tmp sibling must not linger after rename");
    }

    /// Write-failure path — sink wired, but the parent directory
    /// can't be created because a regular file blocks the path.
    /// `File::create` on the tmp returns ENOTDIR. Helper returns
    /// `Err`, fires the stderr_summary closure so the operator sees
    /// the structured signal even when the FS write didn't land.
    /// Pins stderr-fallback discipline on the on-demand path:
    /// configured-sink-failed never silently drops.
    #[test]
    fn write_failure_invokes_stderr_summary_and_returns_err() {
        let dir = tempfile::tempdir().expect("tempdir");
        let file_in_the_way = dir.path().join("not_a_dir");
        std::fs::write(&file_in_the_way, b"").expect("create blocker file");
        let base = file_in_the_way.join("sub").join("dump.failure-dump.json");
        let summary_called = AtomicBool::new(false);
        let result = write_to_tagged_path(
            Some(&base),
            "test_tag",
            "{\"k\":\"v\"}",
            || {
                summary_called.store(true, Ordering::Relaxed);
                String::from("test summary")
            },
            "test warn msg",
        );
        assert!(result.is_err(), "expected Err on write failure");
        assert!(
            summary_called.load(Ordering::Relaxed),
            "stderr_summary closure MUST fire on write failure (no silent drop)"
        );
    }

    /// Rename-failure tmp-cleanup — File::create + sync_all succeed,
    /// but `rename(tmp, tagged)` fails because the tagged destination
    /// already exists as a non-empty directory. Linux `rename(2)`
    /// returns EISDIR when source is a file and dest is a directory
    /// (empty/non-empty distinction does not matter); ENOTEMPTY
    /// applies for the dir-replacing-non-empty-dir case (both sides
    /// directories). A non-empty dir at the dest adds belt-and-
    /// suspenders against any FS that allows file-over-empty-dir
    /// rename. Helper must clean up the tmp file so a future
    /// operator does not see a stale `.json.tmp` alongside the
    /// blocker. Pins the invariant: tmp cleanup runs on EVERY
    /// failure path, not just File::create failures.
    #[test]
    fn rename_failure_cleans_up_tmp_sibling() {
        let dir = tempfile::tempdir().expect("tempdir");
        let base = dir.path().join("dump.failure-dump.json");
        let tagged = super::snapshot_tagged_path(&base, "rename_fail_test");
        // Pre-create the tagged path as a non-empty directory so
        // rename(tmp, tagged) fails. Empty-dir rename behavior
        // varies across filesystems (Linux ext4/xfs/btrfs accept
        // file-over-empty-dir on the source side via EISDIR; some
        // older kernels rejected it). A non-empty dir is a stable
        // rename failure across every supported FS.
        std::fs::create_dir_all(&tagged).expect("create blocker dir");
        std::fs::write(tagged.join("blocker"), b"").expect("populate dir");
        let summary_called = AtomicBool::new(false);
        let result = write_to_tagged_path(
            Some(&base),
            "rename_fail_test",
            "{\"k\":\"v\"}",
            || {
                summary_called.store(true, Ordering::Relaxed);
                String::from("rename failure summary")
            },
            "rename failure warn",
        );
        assert!(result.is_err(), "expected Err on rename failure");
        assert!(
            summary_called.load(Ordering::Relaxed),
            "stderr_summary closure MUST fire on rename failure"
        );
        let tmp = tagged.with_extension("json.tmp");
        assert!(
            !tmp.exists(),
            "tmp sibling must be cleaned up after rename failure"
        );
    }

    /// UTF-8 boundary at cap — payload contains a 4-byte UTF-8
    /// sequence (U+1F600 GRINNING FACE, encoded as `0xF0 0x9F 0x98
    /// 0x80`) straddling the 16 KiB `ONDEMAND_STDERR_DUMP_CAP`
    /// boundary so the naive `&s[..CAP]` would split mid-char and
    /// panic. The helper's stderr-fallback walks back to a safe
    /// UTF-8 boundary via `utf8_safe_truncate_len`. Pins the UTF-8
    /// char-boundary panic fix on the on-demand path: the payload-
    /// head eprintln! must not panic when truncation lands inside a
    /// multi-byte sequence.
    #[test]
    fn write_failure_handles_utf8_boundary_at_cap() {
        let dir = tempfile::tempdir().expect("tempdir");
        let file_in_the_way = dir.path().join("not_a_dir");
        std::fs::write(&file_in_the_way, b"").expect("create blocker file");
        let base = file_in_the_way.join("sub").join("dump.failure-dump.json");
        // Imports the production cap so a future bump (e.g. 16 KiB
        // → 32 KiB) updates the production behavior AND this test's
        // boundary construction in lockstep. Avoids the silent
        // drift where the test still exercises the old 16 KiB
        // boundary while production truncates at 32 KiB.
        let cap = super::ONDEMAND_STDERR_DUMP_CAP;
        let mut payload = String::with_capacity(cap + 8);
        // Fill to two bytes short of cap, then push a 4-byte UTF-8
        // sequence so bytes cap-2..cap+2 form U+1F600. Bytes cap
        // and cap+1 are mid-char — naive slicing at cap would
        // panic.
        for _ in 0..(cap - 2) {
            payload.push('a');
        }
        payload.push('\u{1F600}');
        let result = write_to_tagged_path(
            Some(&base),
            "utf8_test",
            &payload,
            || String::from("utf8 boundary test summary"),
            "utf8 boundary test warn",
        );
        // Helper must return Err (write failed) WITHOUT panicking.
        // A regression in utf8_safe_truncate_len or the
        // format_truncation_marker integration would surface here as
        // a panic from the payload-head eprintln! rather than a
        // clean Err return.
        assert!(result.is_err(), "expected Err on write failure path");
    }
}