starry-kernel 0.12.1

A Linux-compatible OS kernel built on ArceOS unikernel
Documentation
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
//! `/dev/dri/card0` — virtio-gpu DRM character device.
//!
//! Single-CRTC, single-connector, single-plane driver over the existing
//! `axdisplay` framebuffer and virtio-gpu 3D transport. Covers legacy libdrm
//! (`CREATE_DUMB → ADDFB2 → SETCRTC → PAGE_FLIP`) and the atomic-KMS
//! path (`MODE_ATOMIC` + blob properties) used by modern compositors.
//!
//! Fixed IDs:
//!   crtc=0x10, encoder=0x20, connector=0x30, plane=0x40
//!
//! Simplifications vs. a real DRM driver:
//!   - Each `CREATE_DUMB` allocates its own page-aligned `GlobalPage`
//!     sized for the requested geometry; `MAP_DUMB` returns a unique
//!     monotonic offset key; `Card0::mmap(offset, length)` resolves that key
//!     back to the buffer's per-allocation physical range. On
//!     `SETCRTC` / `PAGE_FLIP` / non-`TEST_ONLY` atomic commit,
//!     `present_fb` presents the committed buffer: guest-RAM dumb
//!     buffers are memcpy'd into the axdisplay scanout framebuffer and
//!     `framebuffer_flush` kicked, while host-side virgl 3D resources
//!     (Weston/glamor GBM scanout buffers) are bound with
//!     `SET_SCANOUT` + `RESOURCE_FLUSH` — matching Linux
//!     `virtio_gpu_plane_atomic_update`. PRIME export/import retains the
//!     underlying GEM-style resource independently of per-file handles.
//!   - Property validation is permissive: value ranges aren't rigorously
//!     enforced (tests drive sensible values). Atomic rejects only
//!     unknown `(obj, prop)` pairs and obviously-bad object/blob refs.
//!   - `WAIT_VBLANK` returns immediately with a bumped sequence number;
//!     there's no real vblank source to wait on.
//!   - Mode list: one mode matching axdisplay's resolution at a
//!     synthesized 60 Hz.

use alloc::{
    borrow::Cow,
    collections::{BTreeMap, BTreeSet, VecDeque},
    format,
    string::String,
    sync::{Arc, Weak},
    vec,
    vec::Vec,
};
use core::{
    any::Any,
    sync::atomic::{AtomicU32, AtomicU64, Ordering},
};

use ax_alloc::GlobalPage;
use ax_memory_addr::{PAGE_SIZE_4K, PhysAddrRange};
use ax_runtime::hal::{mem::virt_to_phys, time::monotonic_time};
use axfs_ng_vfs::{NodeFlags, VfsError, VfsResult};
use axpoll::{IoEvents, Pollable};
use axpoll_set::PollSet;
use bytemuck::bytes_of;
use linux_raw_sys::general::O_CLOEXEC;

use super::drm::{
    DRM_CAP_ADDFB2_MODIFIERS,
    DRM_CAP_CRTC_IN_VBLANK_EVENT,
    DRM_CAP_DUMB_BUFFER,
    DRM_CAP_PRIME,
    DRM_CAP_TIMESTAMP_MONOTONIC,
    DRM_EVENT_FLIP_COMPLETE,
    DRM_FORMAT_ARGB8888,
    DRM_FORMAT_MOD_INVALID,
    DRM_FORMAT_MOD_LINEAR,
    DRM_FORMAT_XRGB8888,
    DRM_IOCTL_AUTH_MAGIC,
    DRM_IOCTL_DROP_MASTER,
    DRM_IOCTL_GEM_CLOSE,
    DRM_IOCTL_GET_CAP,
    DRM_IOCTL_GET_MAGIC,
    DRM_IOCTL_GET_UNIQUE,
    DRM_IOCTL_MODE_ADDFB2,
    DRM_IOCTL_MODE_ATOMIC,
    DRM_IOCTL_MODE_CREATE_DUMB,
    DRM_IOCTL_MODE_CREATEPROPBLOB,
    DRM_IOCTL_MODE_DESTROY_DUMB,
    DRM_IOCTL_MODE_DESTROYPROPBLOB,
    DRM_IOCTL_MODE_DIRTYFB,
    DRM_IOCTL_MODE_GETCONNECTOR,
    DRM_IOCTL_MODE_GETCRTC,
    DRM_IOCTL_MODE_GETENCODER,
    DRM_IOCTL_MODE_GETPLANE,
    DRM_IOCTL_MODE_GETPLANERESOURCES,
    DRM_IOCTL_MODE_GETPROPBLOB,
    DRM_IOCTL_MODE_GETPROPERTY,
    DRM_IOCTL_MODE_GETRESOURCES,
    DRM_IOCTL_MODE_MAP_DUMB,
    DRM_IOCTL_MODE_OBJ_GETPROPERTIES,
    DRM_IOCTL_MODE_PAGE_FLIP,
    DRM_IOCTL_MODE_RMFB,
    DRM_IOCTL_MODE_SETCRTC,
    DRM_IOCTL_PRIME_FD_TO_HANDLE,
    DRM_IOCTL_PRIME_HANDLE_TO_FD,
    DRM_IOCTL_SET_CLIENT_CAP,
    DRM_IOCTL_SET_MASTER,
    DRM_IOCTL_SET_VERSION,
    DRM_IOCTL_VERSION,
    // virtgpu structs and constants
    DRM_IOCTL_VIRTGPU_CONTEXT_INIT,
    DRM_IOCTL_VIRTGPU_EXECBUFFER,
    DRM_IOCTL_VIRTGPU_GET_CAPS,
    DRM_IOCTL_VIRTGPU_GETPARAM,
    DRM_IOCTL_VIRTGPU_MAP,
    DRM_IOCTL_VIRTGPU_RESOURCE_CREATE,
    DRM_IOCTL_VIRTGPU_RESOURCE_CREATE_BLOB,
    DRM_IOCTL_VIRTGPU_RESOURCE_INFO,
    DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST,
    DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST,
    DRM_IOCTL_VIRTGPU_WAIT,
    DRM_IOCTL_WAIT_VBLANK,
    DRM_MODE_ATOMIC_ALLOW_MODESET,
    DRM_MODE_ATOMIC_NONBLOCK,
    DRM_MODE_ATOMIC_TEST_ONLY,
    DRM_MODE_CONNECTED,
    DRM_MODE_CONNECTOR_VIRTUAL,
    DRM_MODE_ENCODER_VIRTUAL,
    DRM_MODE_FB_MODIFIERS,
    DRM_MODE_OBJECT_CONNECTOR,
    DRM_MODE_OBJECT_CRTC,
    DRM_MODE_OBJECT_FB,
    DRM_MODE_OBJECT_PLANE,
    DRM_MODE_PAGE_FLIP_EVENT,
    DRM_MODE_PROP_ATOMIC,
    DRM_MODE_PROP_BLOB,
    DRM_MODE_PROP_ENUM,
    DRM_MODE_PROP_IMMUTABLE,
    DRM_MODE_PROP_OBJECT,
    DRM_MODE_PROP_RANGE,
    DRM_MODE_PROP_SIGNED_RANGE,
    DRM_PLANE_TYPE_PRIMARY,
    DRM_PRIME_CAP_EXPORT,
    DRM_PRIME_CAP_IMPORT,
    DRM_PROP_NAME_LEN,
    DrmAuth,
    DrmEvent,
    DrmEventVblank,
    DrmGemClose,
    DrmGetCap,
    DrmModeAtomic,
    DrmModeCardRes,
    DrmModeCreateBlob,
    DrmModeCreateDumb,
    DrmModeCrtc,
    DrmModeCrtcPageFlip,
    DrmModeDestroyBlob,
    DrmModeDestroyDumb,
    DrmModeDirtyFB,
    DrmModeFbCmd2,
    DrmModeGetBlob,
    DrmModeGetConnector,
    DrmModeGetEncoder,
    DrmModeGetPlane,
    DrmModeGetPlaneRes,
    DrmModeGetProperty,
    DrmModeMapDumb,
    DrmModeModeInfo,
    DrmModeObjGetProperties,
    DrmModePropertyEnum,
    DrmPrimeHandle,
    DrmSetClientCap,
    DrmSetVersion,
    DrmUnique,
    DrmVersion,
    DrmVirtgpu3dTransferFromHost,
    DrmVirtgpu3dTransferToHost,
    DrmVirtgpu3dWait,
    DrmVirtgpuContextInit,
    DrmVirtgpuContextSetParam,
    DrmVirtgpuExecbuffer,
    DrmVirtgpuGetCaps,
    DrmVirtgpuGetparam,
    DrmVirtgpuMap,
    DrmVirtgpuResourceCreate,
    DrmVirtgpuResourceCreateBlob,
    DrmVirtgpuResourceInfo,
    DrmWaitVblank,
    VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE,
    VIRTGPU_BLOB_MEM_GUEST,
    VIRTGPU_BLOB_MEM_HOST3D,
    VIRTGPU_BLOB_MEM_HOST3D_GUEST,
    VIRTGPU_CONTEXT_PARAM_CAPSET_ID,
    VIRTGPU_CONTEXT_PARAM_NUM_RINGS,
    VIRTGPU_CONTEXT_PARAM_POLL_RINGS_MASK,
    VIRTGPU_DRM_CAPSET_DRM,
    VIRTGPU_DRM_CAPSET_VIRGL,
    VIRTGPU_DRM_CAPSET_VIRGL2,
    VIRTGPU_EXECBUF_FENCE_FD_IN,
    VIRTGPU_EXECBUF_FENCE_FD_OUT,
    VIRTGPU_PARAM_3D_FEATURES,
    VIRTGPU_PARAM_CAPSET_QUERY_FIX,
    VIRTGPU_PARAM_CONTEXT_INIT,
    VIRTGPU_PARAM_CROSS_DEVICE,
    VIRTGPU_PARAM_HOST_VISIBLE,
    VIRTGPU_PARAM_RESOURCE_BLOB,
    VIRTGPU_PARAM_SUPPORTED_CAPSET_IDS,
};
use super::sync_file::SyncFile;
use crate::{
    StarryError, StarryResult,
    file::{
        File as KernelFile, FileLike, IoDst, IoSrc, Kstat, add_file_like, current_fd_table,
        dma_buf_seek, prepare_file_like, release_locks_on_close,
    },
    mm::{VmMutPtr, VmPtr, vm_load, vm_write_slice},
    pseudofs::{DeviceMmap, DeviceOps},
    sync::Mutex,
    task::{
        UserTaskRef, current_user_task,
        future::{block_on_user, poll_io},
    },
};

pub const DRIVER_NAME: &str = "virtio_gpu";
pub const DRIVER_DATE: &str = "20260921";
pub const DRIVER_DESC: &str = "StarryOS virtio-gpu DRM driver";
// Linux's virtio_gpu driver reports DRIVER_MAJOR=0 / DRIVER_MINOR=1
// (virtgpu_drv.h); minor 1 is the version at which
// `VIRTGPU_EXECBUF_FENCE_FD_IN/OUT` was introduced. Mesa's
// `virgl_drm_get_version` (virgl_drm_winsys.c) rejects `version_major != 0`
// with -EINVAL, and uses `>= VIRGL_DRM_VERSION_FENCE_FD (0,1)` to choose the
// fd-based fence path instead of the legacy one (an 8x1 placeholder resource
// plus WAIT plus GEM_CLOSE per frame). EXECBUFFER now implements fence-fd, so
// matching Linux's stable ABI and reporting minor 1 lets Mesa use fd fences.
pub const DRIVER_VERSION_MAJOR: i32 = 0;
pub const DRIVER_VERSION_MINOR: i32 = 1;
pub const DRIVER_VERSION_PATCHLEVEL: i32 = 0;

/// Fixed object IDs advertised by GETRESOURCES / GETCONNECTOR / GETENCODER.
const CRTC_ID: u32 = 0x10;
const ENCODER_ID: u32 = 0x20;
const CONNECTOR_ID: u32 = 0x30;
const PLANE_ID: u32 = 0x40;

/// The implicit virgl context used for all 3D commands on this card.
///
/// **Must be non-zero.** virglrenderer rejects `ctx_id == 0` at context
/// creation (`virgl_renderer_context_create_with_flags` returns EINVAL for
/// First context id.  Linux starts at 1 (virglrenderer rejects id 0).
/// Allocated per-fd via `next_ctx_id` to match
/// `atomic_inc_return(&vgdev->ctx_id_cursor)` in the Linux kernel.
const FIRST_VIRGL_CTX_ID: u32 = 1;
/// First resource id owned by DRM userspace. The independent `virtio-gpu`
/// core/boot framebuffer path keeps its own id (`0xbabe`) for the boot
/// framebuffer and hands no ids to userspace, so this namespace starts low and
/// advances monotonically.
const FIRST_GPU_RESOURCE_ID: u32 = 1;

/// Which protocol a freshly created virgl context uses.
///
/// Linux's `struct virtio_gpu_fpriv` carries `context_init`, which stays 0
/// until userspace opts into the `DRM_IOCTL_VIRTGPU_CONTEXT_INIT` protocol.
/// Both kinds still send a `CTX_CREATE`; only the extra `context_init` field
/// differs, so the two are separate call shapes instead of one ambiguous
/// integer argument.
enum CreateKind {
    /// Linux `virtio_gpu_create_context()` default: `context_init = 0`,
    /// `num_rings = 1`. Selected by the legacy lazy paths whenever userspace
    /// never ran the context-init ioctl (Mesa only does so when
    /// `GETPARAM(CONTEXT_INIT)` is non-zero).
    Legacy,
    /// `DRM_IOCTL_VIRTGPU_CONTEXT_INIT`: `context_init = capset_id`, plus the
    /// user's ring count.
    Explicit { capset_id: u32, num_rings: u32 },
}

/// First dumb-buffer handle we hand out.
const FIRST_DUMB_HANDLE: u32 = 1;
/// First framebuffer id we hand out from `ADDFB2`.
const FIRST_FB_ID: u32 = 1;

/// Upper bound on `GET_CAPSET_INFO` index probing.
///
/// Linux learns the capset count from a device register and iterates exactly
/// `num_capsets` entries. The interface here only exposes per-index queries,
/// so enumeration stops at the first failing index; this bound caps how many
/// successful replies a misbehaving host can draw out.
const MAX_CAPSET_ENUM: u32 = 64;

/// Per-buffer size cap. We don't pre-reserve a heap — each
/// `CREATE_DUMB` sizes its own allocation — but we still cap individual
/// requests so a bogus width/height/bpp can't OOM the kernel. 8 MiB
/// covers 1920x1080 XRGB with headroom.
const DUMB_BUFFER_MAX_SIZE: usize = 8 * 1024 * 1024;
/// Each buffer's `MAP_DUMB` offset is a monotonic stride in this unit —
/// a synthetic, unique key into the per-card offset->buffer lookup. Must
/// be at least `DUMB_BUFFER_MAX_SIZE` so adjacent buffers don't overlap
/// when userspace mmap's `(fd, length=size_of_buffer, offset=this_key)`.
const DUMB_BUFFER_OFFSET_STRIDE: u64 = DUMB_BUFFER_MAX_SIZE as u64;

// ---- property IDs ----
// Layout: 0x1xx = plane, 0x2xx = CRTC, 0x3xx = connector.
const PROP_PLANE_TYPE: u32 = 0x100;
const PROP_PLANE_FB_ID: u32 = 0x101;
const PROP_PLANE_CRTC_ID: u32 = 0x102;
const PROP_PLANE_SRC_X: u32 = 0x103;
const PROP_PLANE_SRC_Y: u32 = 0x104;
const PROP_PLANE_SRC_W: u32 = 0x105;
const PROP_PLANE_SRC_H: u32 = 0x106;
const PROP_PLANE_CRTC_X: u32 = 0x107;
const PROP_PLANE_CRTC_Y: u32 = 0x108;
const PROP_PLANE_CRTC_W: u32 = 0x109;
const PROP_PLANE_CRTC_H: u32 = 0x10A;
/// `IN_FORMATS` — immutable blob property advertising the (format,
/// modifier) tuples this plane accepts.
const PROP_PLANE_IN_FORMATS: u32 = 0x10B;
/// Standard explicit-fence property. Only the no-fence sentinel is currently
/// supported: Starry has no sync-file producer or fence-wait implementation.
const PROP_PLANE_IN_FENCE_FD: u32 = 0x10C;

const PROP_CRTC_ACTIVE: u32 = 0x200;
const PROP_CRTC_MODE_ID: u32 = 0x201;

const PROP_CONN_CRTC_ID: u32 = 0x300;

const PLANE_PROPS: &[u32] = &[
    PROP_PLANE_TYPE,
    PROP_PLANE_FB_ID,
    PROP_PLANE_CRTC_ID,
    PROP_PLANE_SRC_X,
    PROP_PLANE_SRC_Y,
    PROP_PLANE_SRC_W,
    PROP_PLANE_SRC_H,
    PROP_PLANE_CRTC_X,
    PROP_PLANE_CRTC_Y,
    PROP_PLANE_CRTC_W,
    PROP_PLANE_CRTC_H,
    PROP_PLANE_IN_FORMATS,
    PROP_PLANE_IN_FENCE_FD,
];
const CRTC_PROPS: &[u32] = &[PROP_CRTC_ACTIVE, PROP_CRTC_MODE_ID];
const CONN_PROPS: &[u32] = &[PROP_CONN_CRTC_ID];

/// Supported pixel formats advertised via `GETPLANE.format_type_ptr`.
const SUPPORTED_FORMATS: &[u32] = &[DRM_FORMAT_XRGB8888, DRM_FORMAT_ARGB8888];

/// Upper bound on the pending-event queue. Matches Linux's
/// `file->event_space` of 4 KB ≈ 128 `drm_event_vblank`s.
const MAX_EVENTS: usize = 128;

/// First blob id we hand out from `CREATEPROPBLOB`.
const FIRST_BLOB_ID: u32 = 0x1000;

/// Upper bound on `CREATEPROPBLOB` payload size.
const MAX_BLOB_BYTES: usize = 64 * 1024;
/// Upper bound for one userspace-supplied virgl command stream.
const MAX_VIRGL_COMMAND_BYTES: usize = 16 * 1024 * 1024;

/// Metadata recorded per `CREATE_DUMB` call. Each buffer owns its own
/// page-aligned [`GlobalPage`] — no shared 128 MiB pool — so we don't
/// need a large contiguous physical region up front. The `offset` is
/// what `MAP_DUMB` returns and what `mmap` looks up: a synthetic,
/// monotonically-advancing key (not a real byte offset into anything)
/// that the mmap hook uses to locate this buffer's pages.
///
/// `pages` is `Arc<GlobalPage>`: `DESTROY_DUMB` drops Card0's strong
/// ref, but the `LinearBackend` cloned into each live VMA via
/// `DeviceMmap::Physical` keeps its own strong ref. The underlying
/// pages aren't released until every user mapping is unmapped, which
/// is exactly Linux's GEM refcount contract.
///
/// # Field semantics
///
/// Only `size`, `offset`, and `pages` are **consumed** by downstream
/// operations (`ADDFB2` reads `pages`+`size`; `mmap` reads `offset`;
/// `present_fb` reads `pages`+`size`).  The fields `width`, `height`,
/// `bpp`, and `pitch` are **metadata only** — written once by
/// `CREATE_DUMB` but never read back by any ioctl handler in this
/// driver.  They exist solely so that a human examining a debug dump
/// or a future `GET_DUMB_INFO` (if added) can see what geometry the
/// buffer was allocated for.
///
/// This matters for the `PRIME_FD_TO_HANDLE` import path: the
/// [`DrmPrimeHandle`] ioctl struct carries only `{handle, flags, fd}`
/// — it does **not** convey width/height/bpp/pitch from the exporting
/// driver.  Consequently an imported `DumbBuffer` will always have
/// these four fields set to zero.  No ioctl handler depends on them,
/// so the zero values are safe.  If a future commit adds code that
/// reads `.width` / `.height` / `.bpp` / `.pitch` from an imported
/// buffer, that code must handle the zero case (e.g. by falling back
/// to `ADDFB2`-supplied geometry).
struct DumbBuffer {
    /// Open-file description that owns this GEM handle.
    owner: u64,
    width: u32,
    height: u32,
    bpp: u32,
    pitch: u32,
    size: u64,
    /// Unique mmap-offset key for this buffer.
    offset: u64,
    /// Backing pages. Refcounted so user mappings keep them alive
    /// across `DESTROY_DUMB`.
    pages: Arc<GlobalPage>,
    /// Whether this GEM object has guest memory userspace may mmap.
    mappable: bool,
    /// Host-side resource associated with this GEM object, when any.
    resource: Option<Arc<GpuResource>>,
}

/// Per-framebuffer state retained until `RMFB`. Holds the dumb
/// buffer's backing directly so a `DESTROY_DUMB` on the source
/// handle does not invalidate the fb — Linux's GEM contract says a
/// framebuffer keeps the buffer alive for as long as the fb_id is
/// live.
struct Framebuffer {
    /// Open-file description that created this framebuffer.
    owner: u64,
    /// Total backing size in bytes.
    size: u64,
    /// Row stride (pitch) in bytes — from ADDFB2.pitches[0].
    stride: u32,
    /// Framebuffer width in pixels — from ADDFB2.width.
    width: u32,
    /// Framebuffer height in pixels — from ADDFB2.height.
    height: u32,
    /// Backing storage kind. Present copies guest RAM for dumb buffers
    /// (2D path) and binds the host texture as scanout for virgl 3D
    /// resources (`SET_SCANOUT`) — matching Linux, which always sets the
    /// resource itself as scanout.
    kind: FbBacking,
}

/// Backing storage for a DRM framebuffer.
#[derive(Clone)]
enum FbBacking {
    /// Guest RAM shared with a dumb buffer. The `Arc` keeps the pages
    /// alive until both this fb and any user mappings have been dropped.
    Dumb { pages: Arc<GlobalPage> },
    /// Host-side resource. `res_handle` is the host 2D/3D resource;
    /// `is_dumb_2d` marks a guest-backed 2D resource (dumb buffer) whose
    /// pixels must be `TRANSFER_TO_HOST_2D`'d from guest RAM before flush.
    /// 3D virgl/blob resources already hold their pixels on the host, so
    /// present must NOT transfer them (Linux `virtio_gpu_plane_atomic_update`
    /// transfers the dumb/2D case only).
    Gpu3d { resource: Arc<GpuResource> },
}

/// StarryOS kernel-side dma-buf GEM object for DRM card0.
///
/// Wraps the physical pages backing a dumb buffer so the exported fd
/// (returned by [`Self::handle_prime_handle_to_fd`]) can be mmap'd,
/// read, or passed via SCM_RIGHTS for cross-process buffer sharing.
/// Follows the same pattern as card1.rs's `ExportedGemBuffer`.
struct DmaBufGem {
    /// Physical address range of the underlying buffer.
    range: PhysAddrRange,
    /// Backing pages shared with the source dumb buffer — keeps the
    /// allocation alive even after a `DESTROY_DUMB` on the source
    /// handle.
    pages: Arc<GlobalPage>,
    /// Total size in bytes.
    size: u64,
}

impl FileLike for DmaBufGem {
    fn validate_write_access(&self) -> StarryResult {
        Err(StarryError::InvalidInput)
    }

    fn path(&self) -> Cow<'_, str> {
        "anon_inode:dmabuf".into()
    }

    fn seek(&self, pos: ax_io::SeekFrom) -> StarryResult<u64> {
        dma_buf_seek(self.size, pos)
    }

    fn device_mmap(&self, offset: u64, length: u64) -> StarryResult<DeviceMmap> {
        // Validate that the requested sub-range fits within the buffer.
        // `checked_add` guards against a wrapping length that would
        // bypass the > self.size check.
        let end = offset
            .checked_add(length)
            .ok_or(StarryError::InvalidInput)?;
        if end > self.size {
            return Err(StarryError::InvalidInput);
        }
        // Return the *full* backing range.  The generic mmap layer
        // (mmap.rs, Physical arm) adds `offset` to `range.start` and
        // clamps `length` to `range.size()`, producing the correct
        // sub-mapping of [base+offset, base+offset+length).  Returning
        // the full range (rather than a length-clamped subset) avoids
        // the double-accounting bug where the generic layer would
        // shrink or invalidate the range after shifting it.
        Ok(DeviceMmap::Physical(self.range, Some(self.pages.clone())))
    }
}

impl Pollable for DmaBufGem {
    fn poll(&self) -> IoEvents {
        IoEvents::IN | IoEvents::OUT
    }

    unsafe fn register_shared(
        &self,
        _sink: &mut dyn axpoll::SharedRegistrationSink,
        _events: IoEvents,
    ) {
    }
}

/// A mode's identity and backing travel with the candidate/committed state.
#[derive(Debug, Clone)]
struct ModeBlob {
    id: u32,
    info: DrmModeModeInfo,
    bytes: Arc<Vec<u8>>,
}

/// The single committed state shared by legacy and atomic KMS. Cloned
/// candidates keep mode references alive and publish only after validation.
#[derive(Debug, Default, Clone)]
struct ModesetState {
    crtc_active: u64,
    mode: Option<ModeBlob>,
    conn_crtc_id: u32,
    plane_fb_id: u32,
    plane_crtc_id: u32,
    plane_src_x: u64,
    plane_src_y: u64,
    plane_src_w: u64,
    plane_src_h: u64,
    plane_crtc_x: i64,
    plane_crtc_y: i64,
    plane_crtc_w: u64,
    plane_crtc_h: u64,
}

/// Metadata for a 3D GPU resource created via RESOURCE_CREATE or
/// RESOURCE_CREATE_BLOB. Tracks the association between the virtio-gpu
/// resource ID (used in virgl commands) and the GEM handle (used in
/// DRM ioctls).
#[allow(dead_code)]
struct GpuResource {
    /// Open-file description that created the original GEM handle.
    owner: u64,
    /// Virtio-gpu resource id.
    res_handle: u32,
    /// The GEM handle associated with this resource (from CREATE_DUMB or
    /// allocated by RESOURCE_CREATE).
    bo_handle: u32,
    /// Resource width in pixels.
    width: u32,
    /// Resource height in pixels.
    height: u32,
    /// Row stride in bytes.
    stride: u32,
    /// Resource size in bytes.
    size: u64,
    /// blob_mem from RESOURCE_CREATE_BLOB (`VIRTGPU_BLOB_MEM_*`); 0 for
    /// non-blob (classic 3D) resources.
    blob_mem: u32,
    /// blob_flags from RESOURCE_CREATE_BLOB (`VIRTGPU_BLOB_FLAG_*`); 0 for
    /// non-blob resources.
    blob_flags: u32,
    /// True when this resource is a guest-backed 2D resource created by
    /// CREATE_DUMB. present_fb must `TRANSFER_TO_HOST_2D` from the guest
    /// backing before `RESOURCE_FLUSH`; 3D virgl/blob resources are
    /// host-rendered and skip the transfer.
    is_dumb_2d: bool,
    /// Last synchronously submitted fence that referenced this object.
    last_fence: AtomicU64,
}

impl Drop for GpuResource {
    fn drop(&mut self) {
        if ax_display::has_display() {
            let _ = ax_display::gpu3d_resource_unref(self.res_handle);
        }
    }
}

/// Kernel-side dma-buf for a *host* 3D resource (blob or classic virgl
/// resource) exported via PRIME. Unlike [`DmaBufGem`], which wraps guest
/// RAM, the backing lives on the host GPU: a same-device import resolves
/// back to the same host resource through [`Card0::blob_aliases`], so the
/// importer (weston/Mesa) reuses the host texture zero-copy — exactly what
/// Linux does by exporting the GEM object itself (`virtgpu_prime.c`).
///
/// Each open dma-buf fd holds one reference on the host resource (Linux:
/// the `dma_buf` file pins the GEM object via `drm_gem_prime_export`).
/// [`Drop`] releases that reference when the last fd closes, so the host
/// resource is not freed while a file descriptor still refers to it — even
/// after the exporter's GEM handle is gone.
struct HostResourceDmaBuf {
    /// Canonical GEM resource. Export, import, framebuffer and scanout all
    /// clone this same Arc, so RESOURCE_UNREF occurs at the real last use.
    resource: Arc<GpuResource>,
}

impl FileLike for HostResourceDmaBuf {
    fn validate_write_access(&self) -> StarryResult {
        Err(StarryError::InvalidInput)
    }

    fn seek(&self, pos: ax_io::SeekFrom) -> StarryResult<u64> {
        dma_buf_seek(self.resource.size, pos)
    }

    fn path(&self) -> Cow<'_, str> {
        "anon_inode:dmabuf".into()
    }

    fn device_mmap(&self, _offset: u64, _length: u64) -> StarryResult<DeviceMmap> {
        // The host resource is not guest-mappable without RESOURCE_MAP_BLOB
        // (not needed by the present path — virgl reuses the host texture
        // zero-copy). Rejecting mmap is safer than mapping the guest shadow
        // pages, which do not hold the rendered content.
        Err(StarryError::Unsupported)
    }
}

impl Pollable for HostResourceDmaBuf {
    fn poll(&self) -> IoEvents {
        IoEvents::IN | IoEvents::OUT
    }

    unsafe fn register_shared(
        &self,
        _sink: &mut dyn axpoll::SharedRegistrationSink,
        _events: IoEvents,
    ) {
    }
}

/// Host-resource info for a blob dma-buf imported via `PRIME_FD_TO_HANDLE`.
///
/// Linux returns the *same* GEM object on a same-device import, so
/// `RESOURCE_INFO` on the imported handle must resolve back to the
/// exporter's host resource and blob_mem — this is what makes Mesa take the
/// `maybe_untyped` path and reuse the host texture (`virgl_drm_winsys.c`).
struct ImportedBlob {
    owner: u64,
    resource: Arc<GpuResource>,
}

/// Per-open virgl context state.
struct PerFdCtx {
    /// CONTEXT_INIT parameters. Kept for per-fd semantics (a future
    /// context-info query may report them); not read today.
    #[allow(dead_code)]
    capset_id: u32,
    #[allow(dead_code)]
    num_rings: u32,
    ctx_id: u32,
    attached_resources: BTreeSet<u32>,
}

/// Per-open DRM state, equivalent to Linux `struct drm_file` plus the
/// virtio-gpu private context. `dup` and `fork` share this Arc; reopening the
/// device creates a new handle namespace and rendering context.
struct Card0File {
    base: KernelFile,
    card: Arc<Card0>,
    file_id: u64,
    events: Mutex<VecDeque<DrmEventVblank>>,
    poll_rx: PollSet,
    context: Mutex<Option<PerFdCtx>>,
    operation: Mutex<()>,
}

pub struct Card0 {
    /// Weak self-reference used only to create per-open file descriptions.
    self_weak: Weak<Card0>,
    /// Monotonically-increasing vblank sequence.
    sequence: AtomicU32,
    /// Serializes modeset validation, scanout and publication. Lock order is
    /// state -> fbs/blobs -> display; display IRQ handling never takes state.
    /// User copies and event wakeups happen outside this sleepable mutex.
    state: Mutex<ModesetState>,
    /// `CREATE_DUMB`-allocated buffers keyed by handle. Dropping an
    /// entry releases Card0's strong ref on the backing pages; user
    /// mappings hold their own refs via `LinearBackend::retain`.
    dumbs: Mutex<BTreeMap<u32, DumbBuffer>>,
    /// Next dumb handle to hand out.
    next_dumb_handle: AtomicU32,
    /// Monotonic counter for the mmap-offset key each `MAP_DUMB`
    /// returns. Advanced by [`DUMB_BUFFER_OFFSET_STRIDE`] per allocation
    /// so no two buffers share an offset, even across destroy+recreate.
    next_offset: AtomicU64,
    /// `ADDFB2`-registered framebuffer ids, mapped to the dumb handle
    /// they were built over. Cleared on `RMFB`.
    fbs: Mutex<BTreeMap<u32, Framebuffer>>,
    /// Resource currently bound to scanout. The scanout itself owns a GEM
    /// reference independently of the originating framebuffer and handle.
    scanout_resource: Mutex<Option<Arc<GpuResource>>>,
    /// Next fb id to hand out.
    next_fb_id: AtomicU32,
    /// User-published blobs. A committed mode pins its own reference in state.
    blobs: Mutex<BTreeMap<u32, Arc<Vec<u8>>>>,
    /// Next blob id to hand out.
    next_blob_id: AtomicU32,
    /// Kernel-owned immutable blobs (e.g. plane `IN_FORMATS`) keyed by
    /// blob_id. Read-only after publish; never freed; DESTROY_BLOB
    /// refuses to remove ids in this table.
    system_blobs: Mutex<BTreeMap<u32, Arc<Vec<u8>>>>,
    /// Cached blob_id for the `IN_FORMATS` property. Allocated once
    /// under `system_blobs_init` so concurrent first-callers cannot
    /// each leak their own copy into `system_blobs`.
    in_formats_blob: AtomicU32,
    /// Serializes the lazy initialization of `in_formats_blob` so
    /// only one allocation lands in `system_blobs`.
    system_blobs_init: Mutex<()>,
    /// Registered virtio-gpu IRQ action, when the display backend advertises one.
    irq_handle: ax_lazyinit::OnceLock<ax_runtime::hal::irq::IrqHandle>,

    // ---- 3D (virgl) resource management ----
    /// 3D resources keyed by virtio-gpu resource ID. Each resource tracks
    /// its associated GEM handle, geometry, and size for transfer validation.
    gpu_resources: Mutex<BTreeMap<u32, Arc<GpuResource>>>,
    /// Imported blob dma-bufs: GEM handle (from `PRIME_FD_TO_HANDLE`) →
    /// host resource info. A same-device import is the same host resource
    /// the exporter created, so `RESOURCE_INFO` on an imported handle
    /// resolves back to the original `res_handle` + `blob_mem`.
    ///
    /// Each entry holds one reference on the host resource for the importer.
    blob_aliases: Mutex<BTreeMap<u32, ImportedBlob>>,
    /// Next virtio-gpu resource ID to allocate. Starts at 1 (0 is reserved).
    next_res_handle: AtomicU32,
    /// Next virgl context ID. Linux: `atomic_inc_return(&vgdev->ctx_id_cursor)`.
    /// Each CONTEXT_INIT call gets a unique id so multiple fds/clients
    /// don't share (and corrupt) the same virgl context state.
    next_ctx_id: AtomicU32,
    /// Stable identity assigned to each open file description.
    next_file_id: AtomicU64,
    /// Cached capset data keyed by (capset_id, version). GET_CAPS results
    /// are cached here so repeated queries don't round-trip to the host.
    capset_cache: Mutex<BTreeMap<(u32, u32), Vec<u8>>>,
}

impl Card0 {
    pub fn new() -> Arc<Self> {
        let card = Arc::new_cyclic(|weak| Self {
            self_weak: weak.clone(),
            sequence: AtomicU32::new(0),
            state: Mutex::new(ModesetState::default()),
            dumbs: Mutex::new(BTreeMap::new()),
            next_dumb_handle: AtomicU32::new(FIRST_DUMB_HANDLE),
            // Start at STRIDE rather than 0 so a zero `offset` argument
            // on `mmap` is unambiguous (it means "hasn't called
            // MAP_DUMB yet").
            next_offset: AtomicU64::new(DUMB_BUFFER_OFFSET_STRIDE),
            fbs: Mutex::new(BTreeMap::new()),
            scanout_resource: Mutex::new(None),
            next_fb_id: AtomicU32::new(FIRST_FB_ID),
            blobs: Mutex::new(BTreeMap::new()),
            next_blob_id: AtomicU32::new(FIRST_BLOB_ID),
            system_blobs: Mutex::new(BTreeMap::new()),
            in_formats_blob: AtomicU32::new(0),
            system_blobs_init: Mutex::new(()),
            irq_handle: ax_lazyinit::OnceLock::new(),
            // 3D resource management
            gpu_resources: Mutex::new(BTreeMap::new()),
            blob_aliases: Mutex::new(BTreeMap::new()),
            next_res_handle: AtomicU32::new(FIRST_GPU_RESOURCE_ID),
            next_ctx_id: AtomicU32::new(FIRST_VIRGL_CTX_ID),
            next_file_id: AtomicU64::new(1),
            capset_cache: Mutex::new(BTreeMap::new()),
        });
        card.register_irq();
        card
    }

    fn register_irq(self: &Arc<Self>) {
        if !ax_display::has_display() {
            return;
        }
        let Some(irq) = ax_display::framebuffer_irq_id() else {
            return;
        };

        let request = ax_runtime::hal::irq::IrqRequest::new(|_| {
            if ax_display::framebuffer_handle_irq() {
                ax_runtime::hal::irq::IrqReturn::Handled
            } else {
                ax_runtime::hal::irq::IrqReturn::Unhandled
            }
        })
        .share_mode(ax_runtime::hal::irq::ShareMode::Shared)
        .auto_enable(ax_runtime::hal::irq::AutoEnable::No);
        match ax_runtime::hal::irq::request_irq(irq, request) {
            Ok(handle) => {
                self.irq_handle.call_once(|| handle);
                ax_display::framebuffer_enable_irq();
                if let Some(handle) = self.irq_handle.get().copied()
                    && let Err(err) = ax_runtime::hal::irq::enable_irq(handle)
                {
                    warn!("failed to enable display irq handler for irq {irq:?}: {err:?}");
                    ax_display::framebuffer_disable_irq();
                }
            }
            Err(err) => {
                warn!("failed to register display irq handler for irq {irq:?}: {err:?}");
                ax_display::framebuffer_disable_irq();
            }
        }
    }

    /// Lazily construct the `IN_FORMATS` blob the first time a caller
    /// asks for plane properties. Holds `system_blobs_init` across the
    /// allocate-and-publish so a concurrent first-caller cannot leak
    /// a parallel copy into `system_blobs`. The blob lives there
    /// permanently — `handle_destroy_blob` refuses ids it covers.
    fn ensure_in_formats_blob(&self) -> u32 {
        let cur = self.in_formats_blob.load(Ordering::Acquire);
        if cur != 0 {
            return cur;
        }
        let _guard = self.system_blobs_init.lock();
        let cur = self.in_formats_blob.load(Ordering::Acquire);
        if cur != 0 {
            return cur;
        }
        let bytes = build_in_formats_blob();
        let id = self.next_blob_id.fetch_add(1, Ordering::Relaxed);
        self.system_blobs.lock().insert(id, Arc::new(bytes));
        self.in_formats_blob.store(id, Ordering::Release);
        id
    }
}

/// True if `inner` is the shared `/dev/dri/card0` or render-node device.
pub(crate) fn is_card0_device(inner: &dyn Any) -> bool {
    inner.is::<Card0>()
}

/// Build one Linux-style open file description for card0/renderD128.
pub(crate) fn open_card0_file(
    inner: &dyn Any,
    file: ax_fs_ng::File,
    open_flags: u32,
) -> StarryResult<Arc<dyn FileLike>> {
    let card = inner
        .downcast_ref::<Card0>()
        .ok_or(StarryError::NoSuchDevice)?;
    let card = card.self_weak.upgrade().ok_or(StarryError::NoSuchDevice)?;
    Ok(Arc::new(Card0File::new(
        KernelFile::new(file, open_flags),
        card,
    )))
}

/// Write a kernel-owned `src` into a user buffer. Returns the number of
/// bytes the kernel tried to write (for the truncated-write `*_len =
/// len(src)` convention DRM's VERSION ioctl uses).
fn write_user_string(
    current: &crate::task::UserTaskRef,
    user_ptr: u64,
    user_cap: usize,
    src: &str,
) -> VfsResult<usize> {
    let n = user_cap.min(src.len());
    if n > 0 {
        vm_write_slice(current, user_ptr as *mut u8, &src.as_bytes()[..n])
            .map_err(|_| VfsError::BadAddress)?;
    }
    Ok(src.len())
}

/// Write up to `cap` `T`s from `src` into `user_ptr`; returns the total
/// source length.
fn report_user_array<T: bytemuck::NoUninit>(
    current: &crate::task::UserTaskRef,
    user_ptr: u64,
    cap: u32,
    src: &[T],
) -> VfsResult<u32> {
    if user_ptr != 0 {
        let to_write = (cap as usize).min(src.len());
        vm_write_slice(current, user_ptr as *mut T, &src[..to_write])
            .map_err(|_| VfsError::BadAddress)?;
    }
    Ok(src.len() as u32)
}

/// Fetch a (width, height) pair from `axdisplay`. If no display device
/// was probed, returns a tiny default so `MODE_GETRESOURCES`/
/// `GETCONNECTOR` still have something coherent to report.
fn display_resolution() -> (u32, u32) {
    if ax_display::has_display() {
        let info = ax_display::framebuffer_info();
        (info.width, info.height)
    } else {
        (640, 480)
    }
}

/// VESA CVT-RBv1 (Coordinated Video Timings, Reduced Blanking — 2003)
/// constants. virtio-gpu doesn't actually drive a scanout clock but
/// userspace mode-validators reject self-inconsistent modes, so we
/// synthesize plausible values from the real resolution.
const CVT_RB_HFRONT_PORCH: u16 = 48;
const CVT_RB_HSYNC_WIDTH: u16 = 32;
const CVT_RB_HBACK_PORCH: u16 = 80;
const CVT_RB_VFRONT_PORCH: u16 = 3;
const CVT_RB_VSYNC_WIDTH: u16 = 8;
const CVT_RB_VBACK_PORCH: u16 = 6;

/// Default output refresh rate.
const DEFAULT_VREFRESH: u32 = 60;

/// Synthesized mode matching the display's current resolution.
fn current_mode() -> DrmModeModeInfo {
    let (w, h) = display_resolution();
    let mut name = [0u8; 32];
    let s = b"current";
    name[..s.len()].copy_from_slice(s);

    let hdisplay = w as u16;
    let hsync_start = hdisplay + CVT_RB_HFRONT_PORCH;
    let hsync_end = hsync_start + CVT_RB_HSYNC_WIDTH;
    let htotal = hsync_end + CVT_RB_HBACK_PORCH;

    let vdisplay = h as u16;
    let vsync_start = vdisplay + CVT_RB_VFRONT_PORCH;
    let vsync_end = vsync_start + CVT_RB_VSYNC_WIDTH;
    let vtotal = vsync_end + CVT_RB_VBACK_PORCH;

    let vrefresh: u32 = DEFAULT_VREFRESH;
    let clock = ((htotal as u32) * (vtotal as u32) * vrefresh) / 1000;

    DrmModeModeInfo {
        clock,
        hdisplay,
        hsync_start,
        hsync_end,
        htotal,
        hskew: 0,
        vdisplay,
        vsync_start,
        vsync_end,
        vtotal,
        vscan: 0,
        vrefresh,
        flags: 0,
        kind: 0,
        name,
    }
}

impl DeviceOps for Card0 {
    fn read_at(&self, _buf: &mut [u8], _offset: u64) -> VfsResult<usize> {
        Err(VfsError::BadFileDescriptor)
    }

    fn write_at(&self, _buf: &[u8], _offset: u64) -> VfsResult<usize> {
        Err(VfsError::BadFileDescriptor)
    }

    fn ioctl(&self, _current: &UserTaskRef, _cmd: u32, _arg: usize) -> VfsResult<usize> {
        // The shared device node has no Linux `drm_file` state. `open(2)`
        // wraps it in `Card0File`, which owns handles, events and context.
        Err(VfsError::NotATty)
    }

    fn mmap(&self, _offset: u64, _length: u64) -> DeviceMmap {
        DeviceMmap::None
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn flags(&self) -> NodeFlags {
        NodeFlags::NON_CACHEABLE | NodeFlags::STREAM
    }
}

impl Card0File {
    fn new(base: KernelFile, card: Arc<Card0>) -> Self {
        let file_id = card.next_file_id.fetch_add(1, Ordering::Relaxed);
        Self {
            base,
            card,
            file_id,
            events: Mutex::new(VecDeque::with_capacity(MAX_EVENTS)),
            poll_rx: PollSet::new(),
            context: Mutex::new(None),
            operation: Mutex::new(()),
        }
    }

    /// Creates a per-fd virgl context on the host and publishes it, returning
    /// the new `ctx_id`.
    ///
    /// Shared by the explicit `DRM_IOCTL_VIRTGPU_CONTEXT_INIT` handler and the
    /// legacy lazy paths so both allocate a unique host context id, generate a
    /// unique debug name, and publish `PerFdCtx` in the same order.
    ///
    /// Publish order matters: the id is reserved first (Linux
    /// `atomic_inc_return(&vgdev->ctx_id_cursor)`), the host call runs next,
    /// and only a successful host create installs the state. A failed host
    /// create therefore leaves `context` untouched — no partially built
    /// `PerFdCtx` is visible to `attach_resource` or `Drop`. Sparse ids after a
    /// failure are fine; Linux never reuses a context id either.
    ///
    /// Fails with `Unsupported` when virgl was not negotiated, matching the
    /// device core, which rejects every 3D command without that feature.
    fn create_context(&self, kind: CreateKind) -> VfsResult<u32> {
        let (context_init, num_rings) = match kind {
            CreateKind::Legacy => (0, 1),
            CreateKind::Explicit {
                capset_id,
                num_rings,
            } => (capset_id, num_rings),
        };

        // The device core rejects 3D commands without VIRGL; failing here keeps
        // the error a single, predictable `Unsupported` instead of a host
        // round-trip that can only fail.
        if !ax_display::has_virgl() {
            return Err(VfsError::Unsupported);
        }

        let ctx_id = self.card.next_ctx_id.fetch_add(1, Ordering::Relaxed);
        // Name encodes the unique `ctx_id` rather than the capset, so two
        // contexts never share the debug label and host-side error logs
        // ("starry-ctx-2") stay attributable to one client.
        let ctx_name = format!("starry-ctx-{ctx_id}");
        ax_display::gpu3d_ctx_create(ctx_id, &ctx_name, context_init).map_err(map_gpu3d_err)?;

        // Published only after the host accepted the create.
        *self.context.lock() = Some(PerFdCtx {
            capset_id: context_init,
            num_rings,
            ctx_id,
            attached_resources: BTreeSet::new(),
        });
        Ok(ctx_id)
    }

    /// Returns this fd's `ctx_id`, creating the legacy default context if
    /// userspace never ran `DRM_IOCTL_VIRTGPU_CONTEXT_INIT`.
    ///
    /// Mirrors `virtio_gpu_create_context()`: the lazy virgl paths
    /// (`RESOURCE_CREATE`, `EXECBUFFER`, the 3D transfers and host-backed
    /// blobs) call it before any `CTX`-tagged command, so they work even
    /// without an explicit context-init ioctl. Linux takes this create path on
    /// every such ioctl regardless of whether `CONTEXT_INIT` was negotiated,
    /// and it always builds the default `context_init = 0` context; an fd that
    /// already holds one just gets it reported back unchanged.
    ///
    /// The explicit `DRM_IOCTL_VIRTGPU_CONTEXT_INIT` path is what userspace
    /// uses once `GETPARAM(CONTEXT_INIT)` reports 1 (Mesa does this before its
    /// first submit), so in practice the lazy fallback is a safety net that
    /// supplies the default context when userspace skipped that ioctl.
    ///
    /// Callers must already hold `operation`, which serializes every mutable
    /// `Card0File` ioctl on this open file description; the probe-then-create
    /// sequence below relies on that serialization to stay race-free.
    fn ensure_lazy_context(&self) -> VfsResult<u32> {
        if let Some(context) = self.context.lock().as_ref() {
            return Ok(context.ctx_id);
        }
        self.create_context(CreateKind::Legacy)
    }

    /// Attaches `resource` to this fd's context when one exists, reporting
    /// whether an attach actually happened.
    ///
    /// `Ok(false)` means the fd has no context yet, so there is nothing to
    /// attach to. Callers acting in a Linux context-conditional position (for
    /// instance the PRIME import in `virtio_gpu_gem_object_open()`) must treat
    /// that as a successful no-op rather than an error. `Ok(true)` covers both
    /// a fresh attach and an already-attached resource.
    fn attach_resource_if_ready(&self, resource: &Arc<GpuResource>) -> VfsResult<bool> {
        let mut context = self.context.lock();
        let Some(context) = context.as_mut() else {
            return Ok(false);
        };
        if context.attached_resources.contains(&resource.res_handle) {
            return Ok(true);
        }
        ax_display::gpu3d_ctx_attach_resource(context.ctx_id, resource.res_handle)
            .map_err(map_gpu3d_err)?;
        context.attached_resources.insert(resource.res_handle);
        Ok(true)
    }

    /// Attaches `resource` to the fd context, failing when the fd has none.
    ///
    /// Used by paths that have already created the context, so a missing one
    /// is a real error rather than an expected state.
    fn attach_resource(&self, resource: &Arc<GpuResource>) -> VfsResult<()> {
        self.attach_resource_if_ready(resource)
            .and_then(|attached| attached.then_some(()).ok_or(VfsError::InvalidInput))
    }

    fn detach_resource(&self, resource_id: u32) {
        let mut context = self.context.lock();
        let Some(context) = context.as_mut() else {
            return;
        };
        if context.attached_resources.remove(&resource_id) {
            let _ = ax_display::gpu3d_ctx_detach_resource(context.ctx_id, resource_id);
        }
    }

    fn ioctl_inner(&self, current: &UserTaskRef, cmd: u32, arg: usize) -> VfsResult<usize> {
        let card = &self.card;
        match cmd {
            DRM_IOCTL_VERSION => handle_version(current, arg),
            DRM_IOCTL_GET_UNIQUE => handle_get_unique(current, arg),
            DRM_IOCTL_SET_VERSION => handle_set_version(current, arg),
            DRM_IOCTL_GET_CAP => handle_get_cap(current, arg),
            DRM_IOCTL_SET_CLIENT_CAP => handle_set_client_cap(current, arg),
            DRM_IOCTL_SET_MASTER | DRM_IOCTL_DROP_MASTER => Ok(0),

            DRM_IOCTL_MODE_GETRESOURCES => handle_get_resources(current, arg),
            DRM_IOCTL_MODE_GETCRTC => card.handle_get_crtc(current, arg),
            DRM_IOCTL_MODE_SETCRTC => card.handle_set_crtc(current, arg),
            DRM_IOCTL_MODE_GETENCODER => handle_get_encoder(current, arg),
            DRM_IOCTL_MODE_GETCONNECTOR => handle_get_connector(current, arg),
            DRM_IOCTL_MODE_ADDFB2 => card.handle_addfb2(self, current, arg),
            DRM_IOCTL_MODE_RMFB => card.handle_rmfb(self, current, arg),
            DRM_IOCTL_MODE_CREATE_DUMB => card.handle_create_dumb(self, current, arg),
            DRM_IOCTL_MODE_MAP_DUMB => card.handle_map_dumb(self, current, arg),
            DRM_IOCTL_MODE_DESTROY_DUMB => card.handle_destroy_dumb(self, current, arg),
            // GEM_CLOSE is the release path Mesa uses for virgl 3D/blob
            // resources (incl. PRIME imports). Linux funnels both GEM_CLOSE
            // and DESTROY_DUMB through `drm_gem_handle_delete`; we mirror
            // that by sharing one cleanup helper.
            DRM_IOCTL_GEM_CLOSE => card.handle_gem_close(self, current, arg),

            DRM_IOCTL_MODE_GETPLANERESOURCES => handle_get_plane_resources(current, arg),
            DRM_IOCTL_MODE_GETPLANE => card.handle_get_plane(current, arg),
            DRM_IOCTL_MODE_OBJ_GETPROPERTIES => card.handle_obj_get_properties(current, arg),
            DRM_IOCTL_MODE_GETPROPERTY => handle_get_property(current, arg),
            DRM_IOCTL_MODE_PAGE_FLIP => card.handle_page_flip(self, current, arg),
            DRM_IOCTL_WAIT_VBLANK => card.handle_wait_vblank(current, arg),

            DRM_IOCTL_MODE_ATOMIC => card.handle_atomic(self, current, arg),
            DRM_IOCTL_MODE_CREATEPROPBLOB => card.handle_create_blob(current, arg),
            DRM_IOCTL_MODE_DESTROYPROPBLOB => card.handle_destroy_blob(current, arg),
            DRM_IOCTL_MODE_GETPROPBLOB => card.handle_get_blob(current, arg),

            DRM_IOCTL_GET_MAGIC => handle_get_magic(current, arg),
            DRM_IOCTL_AUTH_MAGIC => handle_auth_magic(current, arg),
            DRM_IOCTL_MODE_DIRTYFB => card.handle_dirty_fb(self, current, arg),
            DRM_IOCTL_PRIME_HANDLE_TO_FD => card.handle_prime_handle_to_fd(self, current, arg),
            DRM_IOCTL_PRIME_FD_TO_HANDLE => card.handle_prime_fd_to_handle(self, current, arg),

            // ---- virtgpu 3D ioctls ----
            DRM_IOCTL_VIRTGPU_GETPARAM => card.handle_virtgpu_getparam(current, arg),
            DRM_IOCTL_VIRTGPU_CONTEXT_INIT => card.handle_virtgpu_context_init(self, current, arg),
            DRM_IOCTL_VIRTGPU_GET_CAPS => card.handle_virtgpu_get_caps(current, arg),
            DRM_IOCTL_VIRTGPU_RESOURCE_CREATE => {
                card.handle_virtgpu_resource_create(self, current, arg)
            }
            DRM_IOCTL_VIRTGPU_RESOURCE_INFO => {
                card.handle_virtgpu_resource_info(self, current, arg)
            }
            DRM_IOCTL_VIRTGPU_MAP => card.handle_virtgpu_map(self, current, arg),
            // DRM_IOCTL_VIRTGPU_EXECBUFFER is dispatched by `Card0File::ioctl`
            // on the `StarryResult` path so a fence-fd reservation can report
            // `EMFILE` (TooManyOpenFiles); `VfsError` has no such variant.
            DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST => {
                card.handle_virtgpu_transfer_to_host(self, current, arg)
            }
            DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST => {
                card.handle_virtgpu_transfer_from_host(self, current, arg)
            }
            DRM_IOCTL_VIRTGPU_WAIT => card.handle_virtgpu_wait(self, current, arg),
            DRM_IOCTL_VIRTGPU_RESOURCE_CREATE_BLOB => {
                card.handle_virtgpu_resource_create_blob(self, current, arg)
            }

            _ => {
                warn!("[card0] unsupported ioctl cmd=0x{:08x}", cmd);
                Err(VfsError::OperationNotSupported)
            }
        }
    }
}

impl Pollable for Card0File {
    fn poll(&self) -> IoEvents {
        let mut events = IoEvents::empty();
        events.set(IoEvents::IN, !self.events.lock().is_empty());
        events
    }

    unsafe fn register_shared(
        &self,
        sink: &mut dyn axpoll::SharedRegistrationSink,
        events: IoEvents,
    ) {
        if events.contains(IoEvents::IN) {
            unsafe { sink.register_shared(&self.poll_rx, IoEvents::IN) };
        }
    }

    unsafe fn register_exclusive(
        &self,
        sink: &mut dyn axpoll::ExclusiveRegistrationSink,
        events: IoEvents,
    ) {
        if events.contains(IoEvents::IN) {
            unsafe { sink.register_exclusive(&self.poll_rx, IoEvents::IN) };
        }
    }
}

impl FileLike for Card0File {
    fn validate_write_access(&self) -> StarryResult {
        self.base.validate_write_access()
    }

    fn read(&self, dst: &mut IoDst) -> StarryResult<usize> {
        let event_size = core::mem::size_of::<DrmEventVblank>();
        if dst.remaining_mut() < event_size {
            return Err(StarryError::InvalidInput);
        }
        let task = current_user_task();
        block_on_user(
            &task,
            poll_io(self, IoEvents::IN, self.nonblocking(), || {
                let mut events = self.events.lock();
                let Some(event) = events.pop_front() else {
                    return Err(StarryError::WouldBlock);
                };
                dst.write(bytes_of(&event))?;
                Ok(event_size)
            }),
        )
        .into_result()?
    }

    fn write(&self, _src: &mut IoSrc) -> StarryResult<usize> {
        Err(StarryError::BadFileDescriptor)
    }

    fn stat(&self) -> StarryResult<Kstat> {
        self.base.stat()
    }

    fn path(&self) -> Cow<'_, str> {
        self.base.path()
    }

    fn ioctl(&self, current: &UserTaskRef, cmd: u32, arg: usize) -> StarryResult<usize> {
        if arg == 0 && !matches!(cmd, DRM_IOCTL_SET_MASTER | DRM_IOCTL_DROP_MASTER) {
            return Err(StarryError::BadAddress);
        }
        let _operation = self.operation.lock();
        if cmd == DRM_IOCTL_VIRTGPU_EXECBUFFER {
            // Fence-fd reservation must surface `EMFILE`
            // (`StarryError::TooManyOpenFiles`), which the `VfsError` domain
            // cannot represent. This one command therefore runs on the
            // `StarryResult` path; the lock scope above still covers it.
            return self.card.handle_virtgpu_execbuffer(self, current, arg);
        }
        Ok(self.ioctl_inner(current, cmd, arg)?)
    }

    fn device_mmap(&self, offset: u64, length: u64) -> StarryResult<DeviceMmap> {
        let _operation = self.operation.lock();
        let dumbs = self.card.dumbs.lock();
        let buffer = dumbs
            .values()
            .find(|buffer| {
                buffer.owner == self.file_id && buffer.mappable && buffer.offset == offset
            })
            .ok_or(StarryError::InvalidInput)?;
        let range = PhysAddrRange::from_start_size(
            virt_to_phys(buffer.pages.start_vaddr()),
            length.min(buffer.pages.size() as u64) as usize,
        );
        let retain: Arc<dyn Any + Send + Sync> = buffer.pages.clone();
        // MAP_DUMB/VIRTGPU_MAP offsets are synthetic lookup keys, not byte
        // offsets within the returned physical range. Mark the mapping as
        // resolved so the generic mmap path does not add the key again.
        Ok(DeviceMmap::PhysicalResolved(range, Some(retain)))
    }

    fn open_flags(&self) -> u32 {
        self.base.open_flags()
    }

    fn nonblocking(&self) -> bool {
        self.base.nonblocking()
    }

    fn set_nonblocking(&self, nonblocking: bool) -> StarryResult {
        self.base.set_nonblocking(nonblocking)
    }
}

impl Drop for Card0File {
    fn drop(&mut self) {
        if let Some(context) = self.context.lock().take() {
            for resource_id in context.attached_resources {
                let _ = ax_display::gpu3d_ctx_detach_resource(context.ctx_id, resource_id);
            }
            let _ = ax_display::gpu3d_ctx_destroy(context.ctx_id);
        }

        let mut state = self.card.state.lock();
        {
            let mut framebuffers = self.card.fbs.lock();
            let ids = framebuffers
                .iter()
                .filter_map(|(&id, framebuffer)| (framebuffer.owner == self.file_id).then_some(id))
                .collect::<Vec<_>>();
            if ids.contains(&state.plane_fb_id) {
                let _ = self.card.clear_scanout();
                *state = ModesetState::default();
            }
            for id in &ids {
                framebuffers.remove(id);
            }
        }
        drop(state);

        let removed_dumbs = {
            let mut dumbs = self.card.dumbs.lock();
            let handles = dumbs
                .iter()
                .filter_map(|(&handle, buffer)| (buffer.owner == self.file_id).then_some(handle))
                .collect::<Vec<_>>();
            handles
                .into_iter()
                .filter_map(|handle| dumbs.remove(&handle))
                .collect::<Vec<_>>()
        };
        let removed_aliases = {
            let mut aliases = self.card.blob_aliases.lock();
            let handles = aliases
                .iter()
                .filter_map(|(&handle, alias)| (alias.owner == self.file_id).then_some(handle))
                .collect::<Vec<_>>();
            handles
                .into_iter()
                .filter_map(|handle| aliases.remove(&handle))
                .collect::<Vec<_>>()
        };
        let removed_resources = {
            let mut resources = self.card.gpu_resources.lock();
            let ids = resources
                .iter()
                .filter_map(|(&id, resource)| (resource.owner == self.file_id).then_some(id))
                .collect::<Vec<_>>();
            ids.into_iter()
                .filter_map(|id| resources.remove(&id))
                .collect::<Vec<_>>()
        };
        drop((removed_dumbs, removed_aliases, removed_resources));
    }
}

impl Card0 {
    fn clear_scanout(&self) -> VfsResult<()> {
        if !ax_display::has_display() {
            return Ok(());
        }
        let mut scanout = self.scanout_resource.lock();
        ax_display::gpu3d_set_scanout(0, 0, 0, 0, 0, 0).map_err(map_gpu3d_err)?;
        *scanout = None;
        Ok(())
    }

    /// Look up the dumb buffer behind a given `fb_id` and copy its
    /// contents into the axdisplay scanout, then trigger
    /// `framebuffer_flush`. Used by `SETCRTC`, `PAGE_FLIP`, and atomic
    /// commits — every path that userspace uses to "show this buffer
    /// now" routes through here. A follow-on PR will swap the memcpy
    /// for virtio-gpu zero-copy via `set_scanout` / `transfer_to_host`.
    fn present_fb(&self, fb_id: u32) {
        // Snapshot the fb out of the registry, then drop the lock so the
        // display calls below don't run with the map locked. Pages survive
        // a concurrent DESTROY_DUMB because the fb owns its own
        // Arc<GlobalPage> clone.
        let fb = match self.fbs.lock().get(&fb_id) {
            Some(fb) => Framebuffer {
                owner: fb.owner,
                size: fb.size,
                stride: fb.stride,
                width: fb.width,
                height: fb.height,
                kind: fb.kind.clone(),
            },
            None => return,
        };

        match &fb.kind {
            // Guest-RAM dumb buffer: copy pixels into the virtio-gpu
            // framebuffer and flush. This is the 2D CPU path (verified by
            // the Qt Widgets Gallery test).
            FbBacking::Dumb { pages } => {
                if !ax_display::has_display() {
                    return;
                };
                let mut scanout = self.scanout_resource.lock();
                if scanout.is_some()
                    && ax_display::gpu3d_set_scanout(0, 0, 0, 0, 0, 0).is_err()
                {
                    return;
                }
                if ax_display::framebuffer_restore_scanout().is_err() {
                    return;
                }
                *scanout = None;
                let src = pages.start_vaddr().as_usize() as *const u8;
                let info = ax_display::framebuffer_info();
                let dst = info.fb_base_vaddr as *mut u8;

                if fb.stride != 0 && info.stride != 0 && fb.stride as usize != info.stride {
                    // Stride mismatch — copy row by row to avoid diagonal tearing.
                    let dst_limit = info.fb_size / info.stride.max(1);
                    let rows = (fb.height as usize).min(dst_limit);
                    let bytes_per_row = (fb.stride as usize).min(info.stride);
                    for row in 0..rows {
                        unsafe {
                            core::ptr::copy_nonoverlapping(
                                src.add(row * fb.stride as usize),
                                dst.add(row * info.stride),
                                bytes_per_row,
                            );
                        }
                    }
                } else {
                    // Strides match (or one is unknown) — flat copy.
                    let copy = (fb.size as usize).min(info.fb_size);
                    unsafe {
                        core::ptr::copy_nonoverlapping(src, dst, copy);
                    }
                }
                let _ = ax_display::framebuffer_flush();
            }
            // Host-side resource (2D dumb or 3D virgl/blob): bind it as the
            // scanout and flush — Linux's `virtio_gpu_plane_atomic_update`
            // does the same. Guest-backed 2D resources additionally need a
            // TRANSFER_TO_HOST_2D (handled below); 3D virgl/blob resources
            // already hold host-side pixels and skip the transfer.
            FbBacking::Gpu3d { resource } => {
                if !ax_display::has_display() {
                    return;
                };
                // Guest-backed 2D (dumb) resources must copy pixels from
                // guest RAM into the host image before flush — QEMU only
                // fills the image on TRANSFER_TO_HOST_2D. 3D virgl/blob
                // resources already hold host-side pixels and skip this.
                if resource.is_dumb_2d {
                    let _ = ax_display::gpu3d_transfer_to_host_2d(
                        resource.res_handle,
                        0,
                        0,
                        fb.width,
                        fb.height,
                    );
                }
                let mut scanout = self.scanout_resource.lock();
                if ax_display::gpu3d_set_scanout(0, resource.res_handle, 0, 0, fb.width, fb.height).is_ok()
                {
                    *scanout = Some(resource.clone());
                }
                let _ = ax_display::gpu3d_resource_flush(
                    resource.res_handle,
                    0,
                    0,
                    fb.width,
                    fb.height,
                );
            }
        }
    }

    fn handle_create_dumb(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *mut DrmModeCreateDumb;
        let mut c: DrmModeCreateDumb = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        if c.width == 0
            || c.height == 0
            || c.bpp == 0
            || c.bpp > 64
            || !c.bpp.is_multiple_of(8)
            || c.flags != 0
        {
            return Err(VfsError::InvalidInput);
        }
        if c.width > 16384 || c.height > 16384 {
            return Err(VfsError::InvalidInput);
        }
        let bytes_per_pixel = c.bpp / 8;
        let pitch = c
            .width
            .checked_mul(bytes_per_pixel)
            .ok_or(VfsError::InvalidInput)?;
        let size = (pitch as u64)
            .checked_mul(c.height as u64)
            .ok_or(VfsError::InvalidInput)?;
        if size as usize > DUMB_BUFFER_MAX_SIZE {
            return Err(VfsError::NoMemory);
        }
        c.pitch = pitch;
        c.size = size;
        // Each buffer gets its own page-aligned `GlobalPage`. No shared
        // pool, so we don't fail on early-boot fragmentation on arches
        // whose allocator can't satisfy one large contiguous request
        // after driver probe.
        let size_aligned = (size as usize).next_multiple_of(PAGE_SIZE_4K);
        let pages = size_aligned / PAGE_SIZE_4K;
        let mut backing =
            GlobalPage::alloc_contiguous(pages, PAGE_SIZE_4K).map_err(|_| VfsError::NoMemory)?;
        // Linux DRM dumb buffers must be returned zeroed: the page
        // allocator may hand back pages that previously held kernel
        // data, and we mmap them straight into user space.
        backing.zero();
        let pages_arc = Arc::new(backing);
        let offset = self
            .next_offset
            .fetch_add(DUMB_BUFFER_OFFSET_STRIDE, Ordering::Relaxed);
        let handle = self.next_dumb_handle.fetch_add(1, Ordering::Relaxed);

        // --- Create a host-side 2D resource + attach guest backing ---
        // Mirrors Linux `virtio_gpu_mode_dumb_create` (virtgpu_gem.c:61-100)
        // which calls RESOURCE_CREATE_2D + ATTACH_BACKING so the host knows
        // about the guest pages.  Without this, virgl blits to the dumb
        // buffer fail (no host resource) and present_fb reads zeros.
        let resource = if ax_display::has_display() {
            let res_handle = self.next_res_handle.fetch_add(1, Ordering::Relaxed);
            let paddr = virt_to_phys(pages_arc.start_vaddr());
            ax_display::gpu3d_resource_create_2d(res_handle, c.width, c.height)
                .map_err(map_gpu3d_err)?;
            let resource = Arc::new(GpuResource {
                owner: file.file_id,
                res_handle,
                bo_handle: handle,
                width: c.width,
                height: c.height,
                stride: pitch,
                size,
                blob_mem: 0,
                blob_flags: 0,
                is_dumb_2d: true,
                last_fence: AtomicU64::new(0),
            });
            ax_display::gpu3d_attach_backing(res_handle, paddr.as_usize() as u64, size as u32)
                .map_err(map_gpu3d_err)?;
            Some(resource)
        } else {
            None
        };

        let buffer = DumbBuffer {
            owner: file.file_id,
            width: c.width,
            height: c.height,
            bpp: c.bpp,
            pitch: c.pitch,
            size: c.size,
            offset,
            pages: pages_arc,
            mappable: true,
            resource: resource.clone(),
        };
        c.handle = handle;
        ptr.vm_write(current, c).map_err(|_| VfsError::BadAddress)?;
        self.dumbs.lock().insert(handle, buffer);
        if let Some(resource) = resource {
            self.gpu_resources
                .lock()
                .insert(resource.res_handle, resource);
        }
        Ok(0)
    }

    /// Shared cleanup for both DESTROY_DUMB and GEM_CLOSE. Linux funnels
    /// both through the same `drm_gem_handle_delete`, so mirroring that
    /// here keeps the two release paths consistent.
    ///
    /// The handle may be an exporter's creating handle (a `gpu_resources`
    /// entry), a same-device import alias (a `blob_aliases` entry), or a
    /// dumb-buffer handle (`dumbs`). Each of these holders keeps the host
    /// resource alive: the host-side `RESOURCE_UNREF` is sent by
    /// [`GpuResource::drop`] only after the last strong reference is released.
    fn destroy_handle(&self, file: &Card0File, handle: u32) -> VfsResult<()> {
        let removed_dumb = {
            let mut dumbs = self.dumbs.lock();
            dumbs
                .get(&handle)
                .is_some_and(|buffer| buffer.owner == file.file_id)
                .then(|| dumbs.remove(&handle))
                .flatten()
        };
        let removed_alias = {
            let mut aliases = self.blob_aliases.lock();
            aliases
                .get(&handle)
                .is_some_and(|alias| alias.owner == file.file_id)
                .then(|| aliases.remove(&handle))
                .flatten()
        };
        let removed_resources = {
            let mut resources = self.gpu_resources.lock();
            let ids = resources
                .iter()
                .filter_map(|(&id, resource)| {
                    (resource.owner == file.file_id && resource.bo_handle == handle).then_some(id)
                })
                .collect::<Vec<_>>();
            let mut removed = Vec::with_capacity(ids.len());
            for id in ids {
                if let Some(resource) = resources.remove(&id) {
                    removed.push(resource);
                }
            }
            removed
        };
        let found =
            removed_dumb.is_some() || removed_alias.is_some() || !removed_resources.is_empty();
        let mut detached_ids = BTreeSet::new();
        if let Some(resource) = removed_dumb
            .as_ref()
            .and_then(|buffer| buffer.resource.as_ref())
        {
            detached_ids.insert(resource.res_handle);
        }
        if let Some(alias) = &removed_alias {
            detached_ids.insert(alias.resource.res_handle);
        }
        for resource in &removed_resources {
            detached_ids.insert(resource.res_handle);
        }
        for resource_id in detached_ids {
            if !self.resource_is_referenced_by_file(file.file_id, resource_id) {
                file.detach_resource(resource_id);
            }
        }
        drop((removed_dumb, removed_alias, removed_resources));
        if found {
            Ok(())
        } else {
            Err(VfsError::InvalidInput)
        }
    }

    fn resource_is_referenced_by_file(&self, file_id: u64, resource_id: u32) -> bool {
        if self.dumbs.lock().values().any(|buffer| {
            buffer.owner == file_id
                && buffer
                    .resource
                    .as_ref()
                    .is_some_and(|resource| resource.res_handle == resource_id)
        }) {
            return true;
        }
        if self
            .blob_aliases
            .lock()
            .values()
            .any(|alias| alias.owner == file_id && alias.resource.res_handle == resource_id)
        {
            return true;
        }
        self.gpu_resources
            .lock()
            .values()
            .any(|resource| resource.owner == file_id && resource.res_handle == resource_id)
    }

    /// DESTROY_DUMB: dumb-buffer specific release path. Linux falls through
    /// to the same `drm_gem_handle_delete` as GEM_CLOSE; StarryOS shares
    /// one cleanup helper for both.
    fn handle_destroy_dumb(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *const DrmModeDestroyDumb;
        let d: DrmModeDestroyDumb = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        self.destroy_handle(file, d.handle)?;
        Ok(0)
    }

    /// GEM_CLOSE: the only release channel Mesa uses to destroy virgl
    /// 3D/blob resources (including PRIME imports).
    fn handle_gem_close(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *const DrmGemClose;
        let c: DrmGemClose = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        self.destroy_handle(file, c.handle)?;
        Ok(0)
    }

    fn handle_map_dumb(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *mut DrmModeMapDumb;
        let mut m: DrmModeMapDumb = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        let offset = self
            .dumbs
            .lock()
            .get(&m.handle)
            .filter(|buffer| buffer.owner == file.file_id)
            .map(|buffer| buffer.offset)
            .ok_or(VfsError::InvalidInput)?;
        m.offset = offset;
        ptr.vm_write(current, m).map_err(|_| VfsError::BadAddress)?;
        Ok(0)
    }
}

fn handle_version(current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
    let ptr = arg as *mut DrmVersion;
    let mut v: DrmVersion = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
    v.version_major = DRIVER_VERSION_MAJOR;
    v.version_minor = DRIVER_VERSION_MINOR;
    v.version_patchlevel = DRIVER_VERSION_PATCHLEVEL;
    v.name_len = write_user_string(current, v.name, v.name_len, DRIVER_NAME)?;
    v.date_len = write_user_string(current, v.date, v.date_len, DRIVER_DATE)?;
    v.desc_len = write_user_string(current, v.desc, v.desc_len, DRIVER_DESC)?;
    ptr.vm_write(current, v).map_err(|_| VfsError::BadAddress)?;
    Ok(0)
}

fn handle_get_unique(current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
    let ptr = arg as *mut DrmUnique;
    let mut u: DrmUnique = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
    let unique: String = format!("{}:0", DRIVER_NAME);
    u.unique_len = write_user_string(current, u.unique, u.unique_len, &unique)?;
    ptr.vm_write(current, u).map_err(|_| VfsError::BadAddress)?;
    Ok(0)
}

fn handle_set_version(current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
    let ptr = arg as *mut DrmSetVersion;
    let mut sv: DrmSetVersion = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
    if sv.drm_di_major < 0 {
        sv.drm_di_major = 1;
    }
    if sv.drm_di_minor < 0 {
        sv.drm_di_minor = 4;
    }
    sv.drm_dd_major = DRIVER_VERSION_MAJOR;
    sv.drm_dd_minor = DRIVER_VERSION_MINOR;
    ptr.vm_write(current, sv)
        .map_err(|_| VfsError::BadAddress)?;
    Ok(0)
}

fn handle_get_cap(current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
    let ptr = arg as *mut DrmGetCap;
    let mut cap: DrmGetCap = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
    // Unknown caps return value=0 rather than EINVAL.
    cap.value = match cap.capability {
        DRM_CAP_DUMB_BUFFER => 1,
        DRM_CAP_TIMESTAMP_MONOTONIC => 1,
        DRM_CAP_CRTC_IN_VBLANK_EVENT => 1,
        DRM_CAP_ADDFB2_MODIFIERS => 1,
        DRM_CAP_PRIME => DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT,
        _ => 0,
    };
    ptr.vm_write(current, cap)
        .map_err(|_| VfsError::BadAddress)?;
    Ok(0)
}

fn handle_set_client_cap(current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
    let ptr = arg as *const DrmSetClientCap;
    let _scc: DrmSetClientCap = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
    Ok(0)
}

fn handle_get_magic(current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
    let ptr = arg as *mut DrmAuth;
    let magic = DrmAuth { magic: 1 };
    ptr.vm_write(current, magic)
        .map_err(|_| VfsError::BadAddress)?;
    Ok(0)
}

fn handle_auth_magic(_current: &crate::task::UserTaskRef, _arg: usize) -> VfsResult<usize> {
    Ok(0)
}

impl Card0 {
    /// Export a GEM handle as a dma-buf file descriptor via PRIME.
    ///
    /// A handle backed by a 3D resource (blob or classic virgl) exports the
    /// *host* resource as a [`HostResourceDmaBuf`] — mirroring Linux
    /// `virtgpu_gem_prime_export`, which exports the GEM object itself.
    /// A handle backed by guest RAM (dumb buffer) exports a [`DmaBufGem`]
    /// wrapping its physical pages.
    fn handle_prime_handle_to_fd(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *mut DrmPrimeHandle;
        let mut req: DrmPrimeHandle = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;

        let dma_buf: Arc<dyn FileLike> = {
            // 3D resource (blob or classic) → export the host resource.
            let imported = self
                .blob_aliases
                .lock()
                .get(&req.handle)
                .filter(|alias| alias.owner == file.file_id)
                .map(|alias| alias.resource.clone());
            let host_res = imported.or_else(|| {
                self.gpu_resources
                    .lock()
                    .values()
                    .find(|resource| {
                        resource.owner == file.file_id
                            && resource.bo_handle == req.handle
                            && !resource.is_dumb_2d
                            && resource.blob_mem != VIRTGPU_BLOB_MEM_GUEST
                    })
                    .cloned()
            });

            if let Some(resource) = host_res {
                Arc::new(HostResourceDmaBuf { resource })
            } else {
                // 2D dumb buffer → export guest RAM (original path).
                let dumbs = self.dumbs.lock();
                let buf = dumbs
                    .get(&req.handle)
                    .filter(|buffer| buffer.owner == file.file_id)
                    .ok_or(VfsError::InvalidInput)?;

                // Convert the dumb buffer's virtual address to a physical address
                // range that the mmap machinery can map into user space.
                // `PhysAddrRange::from_start_size(virt_to_phys(...), size)` builds
                // `{ start = pa, end = pa + size }` — the standard idiom for
                // constructing a range from a base + length.
                let range = PhysAddrRange::from_start_size(
                    virt_to_phys(buf.pages.start_vaddr()),
                    buf.size as usize,
                );
                Arc::new(DmaBufGem {
                    range,
                    pages: buf.pages.clone(),
                    size: buf.size,
                })
            }
        };

        let cloexec = req.flags & O_CLOEXEC != 0;
        let fd = add_file_like(dma_buf.clone(), cloexec).map_err(|_| VfsError::NoMemory)?;
        req.fd = fd;

        if ptr.vm_write(current, req).is_err() {
            close_exported_fd(fd, &dma_buf);
            return Err(VfsError::BadAddress);
        }
        Ok(0)
    }

    /// Import a dma-buf fd back into the card's GEM handle namespace.
    ///
    /// Resolves `req.fd` to a [`DmaBufGem`] object, then registers it in
    /// our dumbs table with a fresh handle so the calling process can use
    /// it with other DRM ioctls (e.g. `ADDFB2`).
    ///
    /// # Why this cannot be an identity mapping
    ///
    /// The prior implementation (`req.handle = req.fd as u32`) treated the
    /// fd number directly as a GEM handle. This is incorrect because:
    ///
    /// - fd numbers and GEM handles live in **separate namespaces**.  A
    ///   process may have fd 5 pointing to a socket, not a dma-buf, and
    ///   fd_to_handle would blindly mint handle=5 in the dumbs table,
    ///   creating a dangling entry that refers to un-related memory.
    /// - No type check: any fd (pipe, socket, regular file) was accepted
    ///   without verifying it is actually a dma-buf backed by our card.
    /// - No reference counting: the imported "handle" had no `Arc` bump on
    ///   the backing pages.  A concurrent `DESTROY_DUMB` on the source
    ///   handle (or `close` on the fd) could free the pages while the
    ///   importer still holds the fake handle.
    ///
    /// The current implementation uses `downcast_ref::<DmaBufGem>` to
    /// reject non-dma-buf fds and `Arc::clone` to participate in the GEM
    /// refcount contract, matching Linux's behaviour.
    fn handle_prime_fd_to_handle(
        &self,
        card_file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *mut DrmPrimeHandle;
        let mut req: DrmPrimeHandle = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;

        let file = crate::file::get_file_like(req.fd).map_err(|_| VfsError::BadFileDescriptor)?;

        // Imported *host* 3D resource (blob dma-buf): same-device import is
        // the same host resource — record an alias so RESOURCE_INFO on the
        // new handle resolves back to the exporter's res_handle + blob_mem.
        if let Some(dma) = file.as_any().downcast_ref::<HostResourceDmaBuf>() {
            let handle = self.next_dumb_handle.fetch_add(1, Ordering::Relaxed);
            // Linux `virtio_gpu_gem_object_open()`: a legacy device creates
            // the context here, because the first ioctl on this fd may be
            // `PRIME_FD_TO_HANDLE` rather than anything else that would have
            // triggered the lazy create. With `CONTEXT_INIT` negotiated Linux
            // deliberately does *not* create one (userspace owns that choice),
            // and `attach_resource()` only runs when a context exists.
            if ax_display::has_virgl() && !ax_display::has_context_init() {
                let _ctx_id = card_file.ensure_lazy_context()?;
            }
            // Import succeeds even with no context yet: the resource is
            // registered under a fresh handle and only attached to the host
            // context when this fd already has one. `attach_resource()`
            // distinguishes "no context" from an attach failure, so an
            // unattached import must not fail the ioctl.
            let attached = card_file.attach_resource_if_ready(&dma.resource)?;
            req.handle = handle;
            if ptr.vm_write(current, req).is_err() {
                if attached {
                    card_file.detach_resource(dma.resource.res_handle);
                }
                return Err(VfsError::BadAddress);
            }
            self.blob_aliases.lock().insert(
                handle,
                ImportedBlob {
                    owner: card_file.file_id,
                    resource: dma.resource.clone(),
                },
            );
            return Ok(0);
        }

        // Guest-RAM dma-buf → register in dumbs (original path).
        let dma_buf: &DmaBufGem = file
            .as_any()
            .downcast_ref::<DmaBufGem>()
            .ok_or(VfsError::InvalidInput)?;

        let handle = self.next_dumb_handle.fetch_add(1, Ordering::Relaxed);
        let offset = self
            .next_offset
            .fetch_add(DUMB_BUFFER_OFFSET_STRIDE, Ordering::Relaxed);
        let buffer =
            // NOTE: width/height/bpp/pitch are zero because the
            // PRIME_FD_TO_HANDLE ioctl does not carry geometry
            // information — the kernel only receives {handle, flags, fd}
            // from userspace and has no way to learn the original
            // CREATE_DUMB parameters.  These fields are metadata-only
            // (see the DumbBuffer doc comment) and no ioctl handler
            // reads them, so zero is safe.  A future code path that
            // inspects .width / .height / .bpp / .pitch on an
            // arbitrary buffer must tolerate zero for imports.
            DumbBuffer {
                owner: card_file.file_id,
                width: 0,
                height: 0,
                bpp: 0,
                pitch: 0,
                size: dma_buf.size,
                offset,
                pages: dma_buf.pages.clone(),
                mappable: true,
                resource: None,
            };
        req.handle = handle;

        ptr.vm_write(current, req)
            .map_err(|_| VfsError::BadAddress)?;
        self.dumbs.lock().insert(handle, buffer);
        Ok(0)
    }
}

fn close_exported_fd(fd: i32, expected: &Arc<dyn FileLike>) {
    let removed = {
        let table = current_fd_table();
        let mut table = table.write();
        let matches = table
            .get(fd as usize)
            .is_some_and(|descriptor| Arc::ptr_eq(&descriptor.inner, expected));
        matches.then(|| table.remove(fd as usize)).flatten()
    };
    if let Some(descriptor) = removed {
        release_locks_on_close(descriptor);
    }
}

fn handle_get_resources(current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
    let ptr = arg as *mut DrmModeCardRes;
    let mut r: DrmModeCardRes = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;

    let (w, h) = display_resolution();
    r.min_width = w;
    r.max_width = w;
    r.min_height = h;
    r.max_height = h;

    r.count_fbs = 0;
    r.count_crtcs = report_user_array(current, r.crtc_id_ptr, r.count_crtcs, &[CRTC_ID])?;
    r.count_encoders =
        report_user_array(current, r.encoder_id_ptr, r.count_encoders, &[ENCODER_ID])?;
    r.count_connectors = report_user_array(
        current,
        r.connector_id_ptr,
        r.count_connectors,
        &[CONNECTOR_ID],
    )?;

    ptr.vm_write(current, r).map_err(|_| VfsError::BadAddress)?;
    Ok(0)
}

impl Card0 {
    fn handle_get_crtc(&self, current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
        let ptr = arg as *mut DrmModeCrtc;
        let mut c: DrmModeCrtc = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        if c.crtc_id != CRTC_ID {
            return Err(VfsError::InvalidInput);
        }
        let state = self.state.lock().clone();
        c.gamma_size = 0;
        c.x = (state.plane_src_x >> 16) as u32;
        c.y = (state.plane_src_y >> 16) as u32;
        c.fb_id = state.plane_fb_id;
        c.mode_valid = u32::from(state.mode.is_some());
        c.mode = state
            .mode
            .as_ref()
            .map_or_else(Default::default, |mode| mode.info);
        let connectors = [CONNECTOR_ID];
        c.count_connectors = report_user_array(
            current,
            c.set_connectors_ptr,
            c.count_connectors,
            if state.conn_crtc_id != 0 {
                &connectors
            } else {
                &[]
            },
        )?;
        ptr.vm_write(current, c).map_err(|_| VfsError::BadAddress)?;
        Ok(0)
    }

    fn handle_set_crtc(&self, current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
        let ptr = arg as *mut DrmModeCrtc;
        let c: DrmModeCrtc = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        if c.crtc_id != CRTC_ID {
            return Err(VfsError::InvalidInput);
        }

        // Linux uses mode_valid to request disable, but still rejects a
        // disable request that names connectors.
        if c.mode_valid == 0 {
            if c.count_connectors != 0 {
                return Err(VfsError::InvalidInput);
            }
            let mut state = self.state.lock();
            self.clear_scanout()?;
            *state = ModesetState::default();
            return Ok(0);
        }

        // A non-disable SETCRTC must list at least one connector and
        // every listed id must exist.
        if c.count_connectors == 0 || c.set_connectors_ptr == 0 {
            return Err(VfsError::InvalidInput);
        }
        // Bound the user count so a bogus value can't try to allocate
        // unbounded kernel memory.
        if c.count_connectors > 16 {
            return Err(VfsError::InvalidInput);
        }
        let connectors: Vec<u32> = vm_load(
            current,
            c.set_connectors_ptr as *const u32,
            c.count_connectors as usize,
        )
        .map_err(|_| VfsError::BadAddress)?;
        for &id in &connectors {
            if id != CONNECTOR_ID {
                return Err(VfsError::InvalidInput);
            }
        }

        let mut state = self.state.lock();
        if c.fb_id == 0 || !self.fbs.lock().contains_key(&c.fb_id) {
            return Err(VfsError::InvalidInput);
        }
        // Legacy SETCRTC uses the same mode/plane state as an atomic commit.
        *state = ModesetState {
            crtc_active: 1,
            mode: Some(ModeBlob {
                id: self.next_blob_id.fetch_add(1, Ordering::Relaxed),
                info: c.mode,
                bytes: Arc::new(bytes_of(&c.mode).to_vec()),
            }),
            conn_crtc_id: CRTC_ID,
            plane_crtc_id: CRTC_ID,
            plane_fb_id: c.fb_id,
            plane_src_x: u64::from(c.x) << 16,
            plane_src_y: u64::from(c.y) << 16,
            plane_src_w: u64::from(c.mode.hdisplay) << 16,
            plane_src_h: u64::from(c.mode.vdisplay) << 16,
            plane_crtc_w: u64::from(c.mode.hdisplay),
            plane_crtc_h: u64::from(c.mode.vdisplay),
            ..ModesetState::default()
        };
        self.present_fb(c.fb_id);
        Ok(0)
    }
}

fn handle_get_encoder(current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
    let ptr = arg as *mut DrmModeGetEncoder;
    let mut e: DrmModeGetEncoder = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
    if e.encoder_id != ENCODER_ID {
        return Err(VfsError::InvalidInput);
    }
    e.encoder_type = DRM_MODE_ENCODER_VIRTUAL;
    e.crtc_id = CRTC_ID;
    e.possible_crtcs = 1;
    e.possible_clones = 0;
    ptr.vm_write(current, e).map_err(|_| VfsError::BadAddress)?;
    Ok(0)
}

fn handle_get_connector(current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
    let ptr = arg as *mut DrmModeGetConnector;
    let mut c: DrmModeGetConnector = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
    if c.connector_id != CONNECTOR_ID {
        return Err(VfsError::InvalidInput);
    }
    c.encoder_id = ENCODER_ID;
    c.connector_type = DRM_MODE_CONNECTOR_VIRTUAL;
    c.connector_type_id = 1;
    c.connection = DRM_MODE_CONNECTED;
    let (w, h) = display_resolution();
    c.mm_width = w;
    c.mm_height = h;
    c.subpixel = 0;

    c.count_encoders = report_user_array(current, c.encoders_ptr, c.count_encoders, &[ENCODER_ID])?;

    if c.modes_ptr != 0 && c.count_modes > 0 {
        let p = c.modes_ptr as *mut DrmModeModeInfo;
        p.vm_write(current, current_mode())
            .map_err(|_| VfsError::BadAddress)?;
    }
    c.count_modes = 1;
    c.count_props = 0;

    ptr.vm_write(current, c).map_err(|_| VfsError::BadAddress)?;
    Ok(0)
}

impl Card0 {
    fn handle_addfb2(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *mut DrmModeFbCmd2;
        let mut f: DrmModeFbCmd2 = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        let handle = f.handles[0];
        // Resolve the backing kind + capacity under the resource/dumb
        // locks so a concurrent DESTROY_DUMB can't race the fb's
        // initial Arc bump. A handle may be:
        //   1) a host 3D resource (virgl scanout — Weston/glamor's GBM
        //      buffers are host textures from RESOURCE_CREATE_3D, which
        //      also register a shadow `dumbs` entry). Check this first so
        //      present binds it with SET_SCANOUT instead of copying empty
        //      guest RAM (the shadow pages are never attached to the host).
        //   2) a plain guest-RAM dumb buffer (2D path).
        let (kind, size) = {
            let imported = self
                .blob_aliases
                .lock()
                .get(&handle)
                .filter(|alias| alias.owner == file.file_id)
                .map(|alias| alias.resource.clone());
            let owned = self
                .gpu_resources
                .lock()
                .values()
                .find(|resource| resource.owner == file.file_id && resource.bo_handle == handle)
                .cloned();
            if let Some(resource) = imported.or(owned) {
                let size = resource.size;
                (FbBacking::Gpu3d { resource }, size)
            } else {
                let dumbs = self.dumbs.lock();
                let Some(buffer) = dumbs
                    .get(&handle)
                    .filter(|buffer| buffer.owner == file.file_id)
                else {
                    return Err(VfsError::InvalidInput);
                };
                if let Some(resource) = &buffer.resource {
                    (
                        FbBacking::Gpu3d {
                            resource: resource.clone(),
                        },
                        buffer.size,
                    )
                } else {
                    (
                        FbBacking::Dumb {
                            pages: buffer.pages.clone(),
                        },
                        buffer.size,
                    )
                }
            }
        };
        // Use the plane stride from the ADDFB2 request (f.pitches[0])
        // rather than the dumb buffer's pitch.  PRIME/import buffers may
        // have a dumb pitch of 0 even when userspace supplies a valid
        // stride in the ADDFB2 call.
        let fb_stride = f.pitches[0];
        let fb_width = f.width;
        let fb_height = f.height;
        let fb_pixel_format = f.pixel_format;

        let bpp = match fb_pixel_format {
            DRM_FORMAT_XRGB8888 | DRM_FORMAT_ARGB8888 => 32u32,
            _ => {
                warn!("ADDFB2: unsupported pixel_format {:#x}", fb_pixel_format);
                return Err(VfsError::InvalidInput);
            }
        };
        let visible_bytes = fb_width * (bpp / 8);
        if fb_stride < visible_bytes {
            warn!(
                "ADDFB2: stride {} < visible bytes {} ({}bpp, {}px)",
                fb_stride, visible_bytes, bpp, fb_width
            );
            return Err(VfsError::InvalidInput);
        }
        let fb_total = fb_stride as u64 * fb_height as u64;
        // Guest-RAM buffers must actually hold the full frame. Host 3D
        // resources hold their storage on the GPU — the shadow `size` can
        // even be the kernel's PAGE_SIZE default when Mesa passes 0 — so
        // skip the capacity check there.
        if matches!(&kind, FbBacking::Dumb { .. }) && size < fb_total {
            warn!(
                "ADDFB2: buffer size {} < fb_total {} ({}stride × {}height)",
                size, fb_total, fb_stride, fb_height
            );
            return Err(VfsError::InvalidInput);
        }
        if f.flags & DRM_MODE_FB_MODIFIERS != 0 {
            for i in 0..4 {
                if f.handles[i] == 0 {
                    continue;
                }
                let m = f.modifier[i];
                if m != DRM_FORMAT_MOD_LINEAR && m != DRM_FORMAT_MOD_INVALID {
                    return Err(VfsError::InvalidInput);
                }
            }
        }
        let fb_id = self.next_fb_id.fetch_add(1, Ordering::Relaxed);
        let framebuffer = Framebuffer {
            owner: file.file_id,
            size,
            stride: fb_stride,
            width: fb_width,
            height: fb_height,
            kind,
        };
        f.fb_id = fb_id;
        ptr.vm_write(current, f).map_err(|_| VfsError::BadAddress)?;
        self.fbs.lock().insert(fb_id, framebuffer);
        Ok(0)
    }

    fn handle_rmfb(&self, file: &Card0File, current: &UserTaskRef, arg: usize) -> VfsResult<usize> {
        let ptr = arg as *const u32;
        let fb_id: u32 = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        let mut state = self.state.lock();
        let removed = {
            let mut framebuffers = self.fbs.lock();
            let owned = framebuffers
                .get(&fb_id)
                .is_some_and(|framebuffer| framebuffer.owner == file.file_id);
            if owned && state.plane_fb_id == fb_id {
                self.clear_scanout()?;
            }
            owned.then(|| framebuffers.remove(&fb_id)).flatten()
        };
        if removed.is_none() {
            return Err(VfsError::InvalidInput);
        }
        if state.plane_fb_id == fb_id {
            *state = ModesetState::default();
        }
        Ok(0)
    }
}

// ======== M4b: planes, properties, page flip, vblank ========

fn handle_get_plane_resources(current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
    let ptr = arg as *mut DrmModeGetPlaneRes;
    let mut r: DrmModeGetPlaneRes = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
    let planes: &[u32] = &[PLANE_ID];
    r.count_planes = report_user_array(current, r.plane_id_ptr, r.count_planes, planes)?;
    ptr.vm_write(current, r).map_err(|_| VfsError::BadAddress)?;
    Ok(0)
}

impl Card0 {
    fn handle_get_plane(&self, current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
        let ptr = arg as *mut DrmModeGetPlane;
        let mut p: DrmModeGetPlane = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        if p.plane_id != PLANE_ID {
            return Err(VfsError::InvalidInput);
        }
        let state = self.state.lock().clone();
        p.crtc_id = state.plane_crtc_id;
        p.fb_id = state.plane_fb_id;
        p.possible_crtcs = 1;
        p.gamma_size = 0;
        p.count_format_types = report_user_array(
            current,
            p.format_type_ptr,
            p.count_format_types,
            SUPPORTED_FORMATS,
        )?;
        ptr.vm_write(current, p).map_err(|_| VfsError::BadAddress)?;
        Ok(0)
    }

    fn handle_obj_get_properties(
        &self,
        current: &crate::task::UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *mut DrmModeObjGetProperties;
        let mut q: DrmModeObjGetProperties =
            ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;

        let state = self.state.lock().clone();
        let (prop_ids, prop_vals): (&[u32], Vec<u64>) = match (q.obj_type, q.obj_id) {
            (DRM_MODE_OBJECT_PLANE, PLANE_ID) => {
                let blob_id = self.ensure_in_formats_blob() as u64;
                (PLANE_PROPS, plane_prop_values(&state, blob_id))
            }
            (DRM_MODE_OBJECT_CRTC, CRTC_ID) => (CRTC_PROPS, crtc_prop_values(&state)),
            (DRM_MODE_OBJECT_CONNECTOR, CONNECTOR_ID) => (CONN_PROPS, conn_prop_values(&state)),
            (DRM_MODE_OBJECT_FB, fb_id) => {
                // Linux rejects objects without a property container after
                // lookup, distinguishing them from nonexistent object IDs.
                return Err(if self.fbs.lock().contains_key(&fb_id) {
                    VfsError::InvalidInput
                } else {
                    VfsError::NotFound
                });
            }
            _ => return Err(VfsError::NotFound),
        };
        report_user_array(current, q.props_ptr, q.count_props, prop_ids)?;
        report_user_array(current, q.prop_values_ptr, q.count_props, &prop_vals)?;
        q.count_props = prop_ids.len() as u32;
        ptr.vm_write(current, q).map_err(|_| VfsError::BadAddress)?;
        Ok(0)
    }
}

fn plane_prop_values(s: &ModesetState, in_formats: u64) -> Vec<u64> {
    vec![
        DRM_PLANE_TYPE_PRIMARY,
        s.plane_fb_id as u64,
        s.plane_crtc_id as u64,
        s.plane_src_x,
        s.plane_src_y,
        s.plane_src_w,
        s.plane_src_h,
        s.plane_crtc_x as u64,
        s.plane_crtc_y as u64,
        s.plane_crtc_w,
        s.plane_crtc_h,
        in_formats,
        // No fence is ever pending outside a commit request.
        u64::MAX,
    ]
}

/// Construct the `IN_FORMATS` blob payload advertising every
/// `SUPPORTED_FORMATS` × `DRM_FORMAT_MOD_LINEAR` pair.
fn build_in_formats_blob() -> Vec<u8> {
    #[repr(C)]
    #[derive(Clone, Copy, bytemuck::NoUninit)]
    struct Header {
        version: u32,
        flags: u32,
        count_formats: u32,
        formats_offset: u32,
        count_modifiers: u32,
        modifiers_offset: u32,
    }
    #[repr(C)]
    #[derive(Clone, Copy, bytemuck::NoUninit)]
    struct ModifierEntry {
        formats: u64,
        offset: u32,
        _pad: u32,
        modifier: u64,
    }
    let n_formats = SUPPORTED_FORMATS.len() as u32;
    let formats_off = size_of::<Header>() as u32;
    let modifiers_off = formats_off + n_formats * 4;
    let hdr = Header {
        version: 1,
        flags: 0,
        count_formats: n_formats,
        formats_offset: formats_off,
        count_modifiers: 1,
        modifiers_offset: modifiers_off,
    };
    let format_mask = (1u64 << n_formats) - 1;
    let me = ModifierEntry {
        formats: format_mask,
        offset: 0,
        _pad: 0,
        modifier: DRM_FORMAT_MOD_LINEAR,
    };
    let mut buf = Vec::with_capacity(
        size_of::<Header>() + (n_formats as usize) * 4 + size_of::<ModifierEntry>(),
    );
    buf.extend_from_slice(bytes_of(&hdr));
    for fmt in SUPPORTED_FORMATS {
        buf.extend_from_slice(&fmt.to_le_bytes());
    }
    buf.extend_from_slice(bytes_of(&me));
    buf
}

fn crtc_prop_values(s: &ModesetState) -> Vec<u64> {
    vec![
        s.crtc_active,
        s.mode.as_ref().map_or(0, |mode| u64::from(mode.id)),
    ]
}

fn conn_prop_values(s: &ModesetState) -> Vec<u64> {
    vec![s.conn_crtc_id as u64]
}

/// `GETPROPERTY` — describe a single property by id.
fn handle_get_property(current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
    let ptr = arg as *mut DrmModeGetProperty;
    let mut g: DrmModeGetProperty = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
    let meta = property_meta(g.prop_id).ok_or(VfsError::NotFound)?;

    g.flags = meta.flags;
    g.name = [0; DRM_PROP_NAME_LEN];
    let nb = meta.name.as_bytes();
    let n = nb.len().min(DRM_PROP_NAME_LEN - 1);
    g.name[..n].copy_from_slice(&nb[..n]);

    match meta.kind {
        PropKind::Enum(enums) => {
            g.count_values = enums.len() as u32;
            g.count_enum_blobs =
                report_user_array(current, g.enum_blob_ptr, g.count_enum_blobs, enums)?;
        }
        PropKind::RangeU64 { min, max } => {
            let limits = [min, max];
            g.count_values = report_user_array(current, g.values_ptr, g.count_values, &limits)?;
            g.count_enum_blobs = 0;
        }
        PropKind::Object(object_type) => {
            let values = [object_type as u64];
            g.count_values = report_user_array(current, g.values_ptr, g.count_values, &values)?;
            g.count_enum_blobs = 0;
        }
        PropKind::Blob => {
            g.count_values = 0;
            g.count_enum_blobs = 0;
        }
    }
    ptr.vm_write(current, g).map_err(|_| VfsError::BadAddress)?;
    Ok(0)
}

struct PropMeta {
    name: &'static str,
    flags: u32,
    kind: PropKind,
}

enum PropKind {
    Enum(&'static [DrmModePropertyEnum]),
    RangeU64 { min: u64, max: u64 },
    Object(u32),
    Blob,
}

const fn enum_entry(value: u64, name: &[u8]) -> DrmModePropertyEnum {
    let mut e = DrmModePropertyEnum {
        value,
        name: [0; DRM_PROP_NAME_LEN],
    };
    let n = if name.len() < DRM_PROP_NAME_LEN - 1 {
        name.len()
    } else {
        DRM_PROP_NAME_LEN - 1
    };
    let mut i = 0;
    while i < n {
        e.name[i] = name[i];
        i += 1;
    }
    e
}

const PLANE_TYPE_ENUMS: &[DrmModePropertyEnum] = &[
    enum_entry(0, b"Overlay"),
    enum_entry(1, b"Primary"),
    enum_entry(2, b"Cursor"),
];
fn property_meta(id: u32) -> Option<PropMeta> {
    let atomic = DRM_MODE_PROP_ATOMIC;
    let meta = match id {
        PROP_PLANE_TYPE => PropMeta {
            name: "type",
            flags: DRM_MODE_PROP_ENUM | DRM_MODE_PROP_IMMUTABLE,
            kind: PropKind::Enum(PLANE_TYPE_ENUMS),
        },
        PROP_PLANE_FB_ID => PropMeta {
            name: "FB_ID",
            flags: DRM_MODE_PROP_OBJECT | atomic,
            kind: PropKind::Object(DRM_MODE_OBJECT_FB),
        },
        PROP_PLANE_CRTC_ID => PropMeta {
            name: "CRTC_ID",
            flags: DRM_MODE_PROP_OBJECT | atomic,
            kind: PropKind::Object(DRM_MODE_OBJECT_CRTC),
        },
        PROP_PLANE_SRC_X => range_u32("SRC_X", atomic),
        PROP_PLANE_SRC_Y => range_u32("SRC_Y", atomic),
        PROP_PLANE_SRC_W => range_u32("SRC_W", atomic),
        PROP_PLANE_SRC_H => range_u32("SRC_H", atomic),
        PROP_PLANE_CRTC_X => range_u32("CRTC_X", atomic),
        PROP_PLANE_CRTC_Y => range_u32("CRTC_Y", atomic),
        PROP_PLANE_CRTC_W => range_u32("CRTC_W", atomic),
        PROP_PLANE_CRTC_H => range_u32("CRTC_H", atomic),
        PROP_PLANE_IN_FORMATS => PropMeta {
            name: "IN_FORMATS",
            flags: DRM_MODE_PROP_BLOB | DRM_MODE_PROP_IMMUTABLE,
            kind: PropKind::Blob,
        },
        PROP_PLANE_IN_FENCE_FD => PropMeta {
            name: "IN_FENCE_FD",
            // Linux declares this as a signed range [-1, INT_MAX] where -1
            // (reported as u64::MAX) means "no fence".
            flags: DRM_MODE_PROP_SIGNED_RANGE | atomic,
            kind: PropKind::RangeU64 {
                min: u64::MAX,
                max: i32::MAX as u64,
            },
        },
        PROP_CRTC_ACTIVE => PropMeta {
            name: "ACTIVE",
            // weston's drm-backend specifically rejects ACTIVE if it
            // isn't declared as a u32 range [0,1] — see submission I.
            flags: DRM_MODE_PROP_RANGE | atomic,
            kind: PropKind::RangeU64 { min: 0, max: 1 },
        },
        PROP_CRTC_MODE_ID => PropMeta {
            name: "MODE_ID",
            flags: DRM_MODE_PROP_BLOB | atomic,
            kind: PropKind::Blob,
        },
        PROP_CONN_CRTC_ID => PropMeta {
            name: "CRTC_ID",
            flags: DRM_MODE_PROP_OBJECT | atomic,
            kind: PropKind::Object(DRM_MODE_OBJECT_CRTC),
        },
        _ => return None,
    };
    Some(meta)
}

fn range_u32(name: &'static str, atomic: u32) -> PropMeta {
    PropMeta {
        name,
        flags: DRM_MODE_PROP_RANGE | atomic,
        kind: PropKind::RangeU64 {
            min: 0,
            max: u32::MAX as u64,
        },
    }
}

impl Card0 {
    fn handle_dirty_fb(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *const DrmModeDirtyFB;
        let dirty: DrmModeDirtyFB = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        if !self
            .fbs
            .lock()
            .get(&dirty.fb_id)
            .is_some_and(|framebuffer| framebuffer.owner == file.file_id)
        {
            return Err(VfsError::InvalidInput);
        }
        self.present_fb(dirty.fb_id);
        Ok(0)
    }

    fn handle_page_flip(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *const DrmModeCrtcPageFlip;
        let f: DrmModeCrtcPageFlip = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        if f.crtc_id != CRTC_ID
            || !self
                .fbs
                .lock()
                .get(&f.fb_id)
                .is_some_and(|framebuffer| framebuffer.owner == file.file_id)
        {
            return Err(VfsError::InvalidInput);
        }
        let mut state = self.state.lock();
        if state.plane_fb_id == 0 {
            return Err(VfsError::ResourceBusy);
        }
        if state.crtc_active == 0 || !self.fbs.lock().contains_key(&f.fb_id) {
            return Err(VfsError::InvalidInput);
        }
        state.plane_fb_id = f.fb_id;
        self.present_fb(f.fb_id);
        drop(state);
        if f.flags & DRM_MODE_PAGE_FLIP_EVENT != 0 {
            self.queue_flip_event(file, f.user_data);
        }
        Ok(0)
    }

    /// Enqueue a `drm_event_vblank` for the next `read()`, wake pollers.
    /// Shared by legacy PAGE_FLIP and atomic commits.
    fn queue_flip_event(&self, file: &Card0File, user_data: u64) {
        let seq = self
            .sequence
            .fetch_add(1, Ordering::Relaxed)
            .wrapping_add(1);
        let now = monotonic_time();
        let ev = DrmEventVblank {
            base: DrmEvent {
                event_type: DRM_EVENT_FLIP_COMPLETE,
                length: core::mem::size_of::<DrmEventVblank>() as u32,
            },
            user_data,
            tv_sec: now.as_secs() as u32,
            tv_usec: now.subsec_micros(),
            sequence: seq,
            crtc_id: CRTC_ID,
        };
        let enqueued = {
            let mut queue = file.events.lock();
            if queue.len() >= MAX_EVENTS {
                false
            } else {
                queue.push_back(ev);
                true
            }
        };
        if enqueued {
            // DRM event is queued before waking readers.
            unsafe { file.poll_rx.wake(IoEvents::IN) };
        }
    }

    /// `WAIT_VBLANK` — user asks to block until a given vblank sequence.
    /// We don't have a real vblank source, so just bump the sequence and
    /// return immediately with the current timestamp.
    fn handle_wait_vblank(
        &self,
        current: &crate::task::UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *mut DrmWaitVblank;
        let request: DrmWaitVblank = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;

        let is_relative = request.rep_type & crate::pseudofs::dev::drm::DRM_VBLANK_RELATIVE != 0;
        let current_sequence = self.sequence.load(Ordering::Acquire);
        let target = if is_relative {
            current_sequence.wrapping_add(request.sequence)
        } else {
            request.sequence
        };
        let raw_wait = target.wrapping_sub(current_sequence);
        let wait_count = if raw_wait == 0 || raw_wait >= i32::MAX as u32 {
            1
        } else {
            raw_wait
        };

        const FRAME_PERIOD_NS: u64 = 1_000_000_000 / 60;
        let delay =
            core::time::Duration::from_nanos(FRAME_PERIOD_NS.saturating_mul(wait_count as u64));
        crate::task::sleep(delay);
        self.sequence.fetch_add(wait_count, Ordering::AcqRel);

        let now = monotonic_time();
        let reply = DrmWaitVblank {
            rep_type: 0,
            sequence: self.sequence.load(Ordering::Acquire),
            tv_sec: now.as_secs() as i64,
            tv_usec: now.subsec_micros() as i64,
        };
        ptr.vm_write(current, reply)
            .map_err(|_| VfsError::BadAddress)?;
        Ok(0)
    }

    // ======== M4c: atomic commit + blob properties ========

    fn handle_atomic(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *const DrmModeAtomic;
        let a: DrmModeAtomic = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;

        let known = DRM_MODE_ATOMIC_TEST_ONLY
            | DRM_MODE_ATOMIC_NONBLOCK
            | DRM_MODE_ATOMIC_ALLOW_MODESET
            | DRM_MODE_PAGE_FLIP_EVENT;
        if a.flags & !known != 0 {
            return Err(VfsError::InvalidInput);
        }

        let n = a.count_objs as usize;
        let objs: Vec<u32> =
            vm_load(current, a.objs_ptr as *const u32, n).map_err(|_| VfsError::BadAddress)?;
        let counts: Vec<u32> = vm_load(current, a.count_props_ptr as *const u32, n)
            .map_err(|_| VfsError::BadAddress)?;
        let total_props: usize = counts.iter().map(|c| *c as usize).sum();
        let props: Vec<u32> = vm_load(current, a.props_ptr as *const u32, total_props)
            .map_err(|_| VfsError::BadAddress)?;
        let values: Vec<u64> = vm_load(current, a.prop_values_ptr as *const u64, total_props)
            .map_err(|_| VfsError::BadAddress)?;

        let mut state = self.state.lock();
        let mut proposed = state.clone();
        let mut idx = 0;
        for (obj_i, &obj_id) in objs.iter().enumerate() {
            let obj_type = object_type_of(obj_id).ok_or(VfsError::NotFound)?;
            for _ in 0..counts[obj_i] {
                let prop_id = props[idx];
                let value = values[idx];
                idx += 1;
                if !self.apply_prop(obj_type, prop_id, value, &mut proposed)? {
                    return Err(VfsError::InvalidInput);
                }
            }
        }

        if a.flags & DRM_MODE_ATOMIC_TEST_ONLY != 0 {
            return Ok(0);
        }
        let current_fb = proposed.plane_fb_id;
        if (current_fb == 0 || proposed.crtc_active == 0) && state.plane_fb_id != 0 {
            self.clear_scanout()?;
        }
        *state = proposed;
        if current_fb != 0 && state.crtc_active != 0 {
            self.present_fb(current_fb);
        }
        drop(state);
        if a.flags & DRM_MODE_PAGE_FLIP_EVENT != 0 {
            self.queue_flip_event(file, a.user_data);
        }
        Ok(0)
    }

    /// Apply one `(prop_id, value)` tuple onto `s`. Returns `Ok(true)`
    /// if the tuple is valid for the given object type, `Ok(false)` if
    /// the property isn't one the object exposes.
    fn apply_prop(
        &self,
        obj_type: u32,
        prop_id: u32,
        value: u64,
        s: &mut ModesetState,
    ) -> VfsResult<bool> {
        match (obj_type, prop_id) {
            (DRM_MODE_OBJECT_PLANE, PROP_PLANE_TYPE) => {
                // IMMUTABLE: accept only the plane's own type.
                if value != DRM_PLANE_TYPE_PRIMARY {
                    return Err(VfsError::InvalidInput);
                }
            }
            (DRM_MODE_OBJECT_PLANE, PROP_PLANE_FB_ID) => {
                let fb = value as u32;
                if fb != 0 && !self.fbs.lock().contains_key(&fb) {
                    return Err(VfsError::InvalidInput);
                }
                s.plane_fb_id = fb;
            }
            (DRM_MODE_OBJECT_PLANE, PROP_PLANE_CRTC_ID) => {
                let c = value as u32;
                if c != 0 && c != CRTC_ID {
                    return Err(VfsError::InvalidInput);
                }
                s.plane_crtc_id = c;
            }
            (DRM_MODE_OBJECT_PLANE, PROP_PLANE_SRC_X) => s.plane_src_x = value,
            (DRM_MODE_OBJECT_PLANE, PROP_PLANE_SRC_Y) => s.plane_src_y = value,
            (DRM_MODE_OBJECT_PLANE, PROP_PLANE_SRC_W) => s.plane_src_w = value,
            (DRM_MODE_OBJECT_PLANE, PROP_PLANE_SRC_H) => s.plane_src_h = value,
            (DRM_MODE_OBJECT_PLANE, PROP_PLANE_CRTC_X) => {
                s.plane_crtc_x = checked_i32(value)? as i64;
            }
            (DRM_MODE_OBJECT_PLANE, PROP_PLANE_CRTC_Y) => {
                s.plane_crtc_y = checked_i32(value)? as i64;
            }
            (DRM_MODE_OBJECT_PLANE, PROP_PLANE_CRTC_W) => s.plane_crtc_w = value,
            (DRM_MODE_OBJECT_PLANE, PROP_PLANE_CRTC_H) => s.plane_crtc_h = value,
            (DRM_MODE_OBJECT_PLANE, PROP_PLANE_IN_FENCE_FD) => {
                // Linux borrows a sync-file fence and never closes the user's
                // fd, including on TEST_ONLY or failure. No Starry file type
                // provides a sync-file yet, so every non-sentinel is invalid.
                if value != u64::MAX {
                    return Err(VfsError::InvalidInput);
                }
            }
            (DRM_MODE_OBJECT_CRTC, PROP_CRTC_ACTIVE) => {
                if value > 1 {
                    return Err(VfsError::InvalidInput);
                }
                s.crtc_active = value;
            }
            (DRM_MODE_OBJECT_CRTC, PROP_CRTC_MODE_ID) => {
                let id = u32::try_from(value).map_err(|_| VfsError::InvalidInput)?;
                s.mode = if id == 0 {
                    None
                } else {
                    let bytes = self
                        .blobs
                        .lock()
                        .get(&id)
                        .cloned()
                        .or_else(|| {
                            s.mode
                                .as_ref()
                                .filter(|mode| mode.id == id)
                                .map(|mode| mode.bytes.clone())
                        })
                        .ok_or(VfsError::InvalidInput)?;
                    if bytes.len() != size_of::<DrmModeModeInfo>() {
                        return Err(VfsError::InvalidInput);
                    }
                    let info = bytemuck::try_pod_read_unaligned(&bytes)
                        .map_err(|_| VfsError::InvalidInput)?;
                    Some(ModeBlob { id, info, bytes })
                };
            }
            (DRM_MODE_OBJECT_CONNECTOR, PROP_CONN_CRTC_ID) => {
                let c = value as u32;
                if c != 0 && c != CRTC_ID {
                    return Err(VfsError::InvalidInput);
                }
                s.conn_crtc_id = c;
            }
            _ => return Ok(false),
        }
        Ok(true)
    }

    fn handle_create_blob(
        &self,
        current: &crate::task::UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *mut DrmModeCreateBlob;
        let mut c: DrmModeCreateBlob = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        if c.length == 0 || c.length as usize > MAX_BLOB_BYTES {
            return Err(VfsError::InvalidInput);
        }
        let bytes: Vec<u8> = vm_load(current, c.data as *const u8, c.length as usize)
            .map_err(|_| VfsError::BadAddress)?;
        let id = self.next_blob_id.fetch_add(1, Ordering::Relaxed);
        self.blobs.lock().insert(id, Arc::new(bytes));
        c.blob_id = id;
        ptr.vm_write(current, c).map_err(|_| VfsError::BadAddress)?;
        Ok(0)
    }

    fn handle_destroy_blob(
        &self,
        current: &crate::task::UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *const DrmModeDestroyBlob;
        let d: DrmModeDestroyBlob = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        // System (kernel-owned) blobs are not user-destroyable. Linux's
        // DRM rejects ENOTSUPP for this; we map it to PermissionDenied
        // (EPERM) since VfsError lacks a finer-grained variant.
        if self.system_blobs.lock().contains_key(&d.blob_id) {
            return Err(VfsError::PermissionDenied);
        }
        // Drop the user-publish reference. If `state.mode` still
        // holds the same Arc (i.e. a commit pinned this blob as the CRTC's
        // `MODE_ID`), the blob data stays alive and
        // `GETPROPBLOB` keeps succeeding via the committed-state lookup
        // below until a later atomic commit replaces `MODE_ID`.
        self.blobs
            .lock()
            .remove(&d.blob_id)
            .ok_or(VfsError::NotFound)?;
        Ok(0)
    }

    fn handle_get_blob(&self, current: &crate::task::UserTaskRef, arg: usize) -> VfsResult<usize> {
        let ptr = arg as *mut DrmModeGetBlob;
        let mut g: DrmModeGetBlob = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        // Never hold a registry/state lock across faultable output copies.
        // Separate lookups also avoid nesting blobs -> state against commits.
        let published = self.blobs.lock().get(&g.blob_id).cloned();
        let bytes = published
            .or_else(|| {
                self.state
                    .lock()
                    .mode
                    .as_ref()
                    .filter(|mode| mode.id == g.blob_id)
                    .map(|mode| mode.bytes.clone())
            })
            .or_else(|| self.system_blobs.lock().get(&g.blob_id).cloned())
            .ok_or(VfsError::NotFound)?;
        if g.data != 0 && g.length > 0 {
            let n = (g.length as usize).min(bytes.len());
            vm_write_slice(current, g.data as *mut u8, &bytes[..n])
                .map_err(|_| VfsError::BadAddress)?;
        }
        g.length = bytes.len() as u32;
        ptr.vm_write(current, g).map_err(|_| VfsError::BadAddress)?;
        Ok(0)
    }

    fn resource_for_handle(&self, file: &Card0File, handle: u32) -> Option<Arc<GpuResource>> {
        if let Some(resource) = self
            .blob_aliases
            .lock()
            .get(&handle)
            .filter(|alias| alias.owner == file.file_id)
            .map(|alias| alias.resource.clone())
        {
            return Some(resource);
        }
        if let Some(resource) = self
            .dumbs
            .lock()
            .get(&handle)
            .filter(|buffer| buffer.owner == file.file_id)
            .and_then(|buffer| buffer.resource.clone())
        {
            return Some(resource);
        }
        self.gpu_resources
            .lock()
            .values()
            .find(|resource| resource.owner == file.file_id && resource.bo_handle == handle)
            .cloned()
    }

    // ======== virtgpu ioctl handlers ========
    //
    // These implement the 11 virtgpu private ioctls that Mesa's virgl
    // driver needs to submit 3D rendering commands. The handlers forward
    // to the ax_display 3D API which reaches the virtio-gpu driver.
    //
    // Security: Each handler validates input from userspace before use,
    // matching Linux kernel behavior (bounds checks, EINVAL for invalid
    // params, EEXIST for duplicate context init, etc.).

    /// VIRTGPU_GETPARAM — queries driver parameters.
    ///
    /// Linux: `virtgpu_getparam_ioctl()` in `virtgpu_ioctl.c`
    ///
    /// Mesa queries all parameters during initialization. Known parameters
    /// return their values; unknown parameters return `-EINVAL` (matching
    /// Linux kernel behavior — Mesa handles this gracefully).
    fn handle_virtgpu_getparam(&self, current: &UserTaskRef, arg: usize) -> VfsResult<usize> {
        let ptr = arg as *mut DrmVirtgpuGetparam;
        let g: DrmVirtgpuGetparam = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;

        let has_virgl = ax_display::has_virgl();

        let value = match g.param {
            VIRTGPU_PARAM_3D_FEATURES => {
                // Must return 1 for Mesa to use virgl path.
                if has_virgl { 1 } else { 0 }
            }
            VIRTGPU_PARAM_CAPSET_QUERY_FIX => {
                // Linux 内核总是返回 1,不管 has_virgl_3d。
                // 这影响 GET_CAPS 的行为(Mesa 用它决定查询顺序)。
                1
            }
            VIRTGPU_PARAM_RESOURCE_BLOB => {
                // Report the *actual* negotiated feature (Linux:
                // `has_resource_blob ? 1 : 0`). Without RESOURCE_BLOB the
                // device doesn't support blobs and Mesa must use the classic
                // resource path — reporting 1 here would make Mesa create
                // blobs that fail.
                if ax_display::has_resource_blob() {
                    1
                } else {
                    0
                }
            }
            VIRTGPU_PARAM_HOST_VISIBLE => {
                // RESOURCE_BLOB alone does not make host memory mappable.
                // Until RESOURCE_MAP_BLOB/BAR mapping exists, advertising
                // HOST_VISIBLE would send Mesa down a path we cannot honor.
                0
            }
            VIRTGPU_PARAM_CROSS_DEVICE => {
                // Cross-device sharing not supported yet.
                0
            }
            VIRTGPU_PARAM_CONTEXT_INIT => {
                // Report the *actual* negotiated feature (Linux:
                // `has_context_init ? 1 : 0`). Must be 1 for Mesa to use the
                // context-init protocol, but VIRGL alone does not imply it: a
                // legacy device can support virgl without CONTEXT_INIT.
                if ax_display::has_context_init() { 1 } else { 0 }
            }
            VIRTGPU_PARAM_SUPPORTED_CAPSET_IDS => {
                // Bitmask of supported capset IDs.
                // Bit 0 = reserved, bit 1 = VIRGL, bit 2 = VIRGL2.
                if has_virgl {
                    (1 << VIRTGPU_DRM_CAPSET_VIRGL) | (1 << VIRTGPU_DRM_CAPSET_VIRGL2)
                } else {
                    0
                }
            }
            _ => {
                // Unknown parameter — match Linux kernel: return -EINVAL.
                // Mesa handles this gracefully (value stays 0).
                return Err(VfsError::InvalidInput);
            }
        };

        // Linux `virtio_gpu_getparam_ioctl`: `copy_to_user((void __user *)
        // param->value, &value, sizeof(value))` — 结果写入用户指针指向的 u64,
        // 而不是写回 struct 字段。之前 `g.value = value; vm_write(g)` 把值写进
        // struct 的 value 字段(覆盖了指针),mesa 读的是指针指向的本地变量,
        // 导致所有 GETPARAM 都读到 0 → 3D_FEATURES=0 → virgl winsys 创建失败。
        if g.value == 0 {
            return Err(VfsError::BadAddress);
        }
        vm_write_slice(current, g.value as *mut u64, &[value]).map_err(|_| VfsError::BadAddress)?;
        Ok(0)
    }

    /// VIRTGPU_CONTEXT_INIT — initializes a rendering context on this fd.
    ///
    /// Linux: `virtgpu_context_init_ioctl()` in `virtgpu_ioctl.c`
    ///
    /// **Critical**: This is a pure-input ioctl with no output fields.
    /// The context is implicitly bound to the file descriptor. Each fd
    /// can only call CONTEXT_INIT once (repeated calls return -EEXIST).
    ///
    /// Mesa calls this with num_params=1 and a single parameter:
    ///   { param=VIRTGPU_CONTEXT_PARAM_CAPSET_ID, value=VIRGL2(2) or VIRGL1(1) }
    fn handle_virtgpu_context_init(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let init: DrmVirtgpuContextInit = (arg as *const DrmVirtgpuContextInit)
            .vm_read(current)
            .map_err(|_| VfsError::BadAddress)?;

        // Linux: if (!vgdev->has_context_init || !vgdev->has_virgl_3d)
        //           return -EINVAL;
        if !ax_display::has_context_init() || !ax_display::has_virgl() {
            return Err(VfsError::InvalidInput);
        }

        // Linux kernel: each fd can only call CONTEXT_INIT once.
        if file.context.lock().is_some() {
            return Err(VfsError::AlreadyExists);
        }

        // StarryOS currently implements the three context parameters below.
        if init.num_params > 3 {
            return Err(VfsError::InvalidInput);
        }
        if init.num_params > 0 && init.ctx_set_params == 0 {
            return Err(VfsError::BadAddress);
        }

        // Read the parameter array from userspace.
        let mut capset_id: u32 = 0;
        let mut num_rings: u32 = 1; // Linux default

        if init.num_params > 0 {
            let params_ptr = init.ctx_set_params as *const DrmVirtgpuContextSetParam;
            for i in 0..init.num_params as usize {
                let param: DrmVirtgpuContextSetParam = unsafe { params_ptr.add(i) }
                    .vm_read(current)
                    .map_err(|_| VfsError::BadAddress)?;
                match param.param {
                    VIRTGPU_CONTEXT_PARAM_CAPSET_ID => {
                        capset_id = param.value as u32;
                        // Linux: if (value > MAX_CAPSET_ID) return -EINVAL;
                        // MAX_CAPSET_ID in Linux v6.1 is 6 (VIRTGPU_DRM_CAPSET_DRM)
                        if capset_id > VIRTGPU_DRM_CAPSET_DRM {
                            return Err(VfsError::InvalidInput);
                        }
                        // Linux: if ((vgdev->capset_id_mask & (1ULL << value)) == 0)
                        //           return -EINVAL;
                        // 我们支持 VIRGL(1) 和 VIRGL2(2)
                        if capset_id != VIRTGPU_DRM_CAPSET_VIRGL
                            && capset_id != VIRTGPU_DRM_CAPSET_VIRGL2
                        {
                            warn!("[card0] CONTEXT_INIT: unsupported capset_id={capset_id}");
                            return Err(VfsError::InvalidInput);
                        }
                    }
                    VIRTGPU_CONTEXT_PARAM_NUM_RINGS => {
                        num_rings = param.value as u32;
                        // Sanity check: limit rings.
                        if num_rings == 0 || num_rings > 64 {
                            return Err(VfsError::InvalidInput);
                        }
                    }
                    VIRTGPU_CONTEXT_PARAM_POLL_RINGS_MASK => {
                        // Accept but ignore — we don't support polling yet.
                        let _ = param.value;
                    }
                    _ => {
                        // Unknown parameter — Linux returns -EINVAL.
                        return Err(VfsError::InvalidInput);
                    }
                }
            }
        }

        // Context creation itself is shared with the legacy lazy paths; the
        // EEXIST check above already covers a context an earlier lazy path
        // published on this fd.
        let ctx_id = file.create_context(CreateKind::Explicit {
            capset_id,
            num_rings,
        })?;

        info!(
            "[card0] CONTEXT_INIT: ctx_id={ctx_id}, capset_id={capset_id}, num_rings={num_rings}"
        );
        Ok(0)
    }

    /// VIRTGPU_GET_CAPS — retrieves capability set data.
    ///
    /// Linux: `virtgpu_get_caps_ioctl()` in `virtgpu_ioctl.c`.
    ///
    /// Semantics mirrored from Linux v7.1: the requested capset must exist in
    /// the device's real capset list with a `max_version` at least the
    /// requested version, a zero `size` is rejected, the copy uses
    /// `min(size, host_caps_size)`, and the input struct is never written
    /// back. Mesa first tries cap_set_id=2 (VIRGL2), then falls back to 1
    /// (VIRGL).
    fn handle_virtgpu_get_caps(&self, current: &UserTaskRef, arg: usize) -> VfsResult<usize> {
        let ptr = arg as *mut DrmVirtgpuGetCaps;
        let g: DrmVirtgpuGetCaps = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        // Linux never writes the request struct back, so the caller's `size`
        // is a pure input; keep it in a local instead of mutating `g`.
        let user_size = g.size;

        // `num_capsets == 0` is reported as -ENOSYS by Linux.
        if !ax_display::has_virgl() {
            return Err(VfsError::Unsupported);
        }

        // `gpu3d_capset_info` exposes GET_CAPSET_INFO by index, which is how
        // Linux enumerates `vgdev->capsets[]`. Index 0 succeeding proves the
        // device has at least one capset; the first failing index ends the
        // list, and the bound keeps a misbehaving host from looping forever.
        let mut capsets = Vec::new();
        for index in 0..MAX_CAPSET_ENUM {
            match ax_display::gpu3d_capset_info(index) {
                Ok(info) => capsets.push(info),
                Err(_) => break,
            }
        }
        if capsets.is_empty() {
            return Err(VfsError::Unsupported);
        }

        // Linux: don't allow userspace to pass 0.
        if user_size == 0 {
            return Err(VfsError::InvalidInput);
        }

        // Select by ID equality and `max_version >= requested version`,
        // exactly as Linux scans the device's capset list. A missing or
        // too-old capset is -EINVAL, not a fabricated success.
        let matched = capsets
            .iter()
            .find(|info| info.capset_id == g.cap_set_id && info.max_version >= g.cap_set_ver)
            .copied()
            .ok_or(VfsError::InvalidInput)?;

        let cache_key = (g.cap_set_id, g.cap_set_ver);
        let cached = self.capset_cache.lock().get(&cache_key).cloned();
        let cap_data = if let Some(data) = cached {
            data
        } else {
            // Ask the host for the full capset (`max_size`, not the user's
            // smaller `size`) so the cache entry is never truncated; only the
            // later copy is clamped. Truncating the query would hand Mesa an
            // incomplete capset and make it enable unsupported GL features.
            let data = ax_display::gpu3d_capset(g.cap_set_id, g.cap_set_ver, matched.max_size)
                .map_err(map_gpu3d_err)?;
            self.capset_cache.lock().insert(cache_key, data.clone());
            data
        };

        // Linux copies `min(args->size, host_caps_size)` bytes; `cap_data` is
        // that host-side blob.
        let write_size = (user_size as usize).min(cap_data.len());
        if write_size == 0 || g.addr == 0 {
            return Err(VfsError::BadAddress);
        }
        vm_write_slice(current, g.addr as *mut u8, &cap_data[..write_size])
            .map_err(|_| VfsError::BadAddress)?;

        Ok(0)
    }

    /// VIRTGPU_RESOURCE_CREATE — creates a 3D resource.
    ///
    /// Linux: `virtgpu_resource_create_ioctl()` in `virtgpu_ioctl.c`
    ///
    /// Creates a 3D resource on the host and optionally associates it with
    /// an existing GEM handle. Returns the virtio-gpu resource ID in
    /// `res_handle` (NOT the GEM handle — they are different!).
    fn handle_virtgpu_resource_create(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *mut DrmVirtgpuResourceCreate;
        let mut r: DrmVirtgpuResourceCreate =
            ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        if !ax_display::has_virgl() {
            return Err(VfsError::Unsupported);
        }
        // Linux `virtio_gpu_resource_create_ioctl()` calls
        // `virtio_gpu_create_context()` up front on the virgl path.
        let ctx_id = file.ensure_lazy_context()?;

        let res_handle = self.next_res_handle.fetch_add(1, Ordering::Relaxed);
        let size = if r.size > 0 {
            r.size as u64
        } else {
            PAGE_SIZE_4K as u64
        };
        if size > DUMB_BUFFER_MAX_SIZE as u64 {
            return Err(VfsError::InvalidInput);
        }
        let mut pages =
            GlobalPage::alloc_contiguous((size as usize).div_ceil(PAGE_SIZE_4K), PAGE_SIZE_4K)
                .map_err(|_| VfsError::NoMemory)?;
        pages.zero();
        let bo_handle = self.next_dumb_handle.fetch_add(1, Ordering::Relaxed);
        let offset = self
            .next_offset
            .fetch_add(DUMB_BUFFER_OFFSET_STRIDE, Ordering::Relaxed);
        let backing_paddr = virt_to_phys(pages.start_vaddr());
        let backing_size = pages.size();
        let pages = Arc::new(pages);

        ax_display::gpu3d_resource_create(ax_display::ResourceCreate3d {
            ctx_id,
            resource_id: res_handle,
            target: r.target,
            format: r.format,
            bind: r.bind,
            width: r.width,
            height: r.height,
            depth: r.depth,
            array_size: r.array_size,
            last_level: r.last_level,
            nr_samples: r.nr_samples,
            flags: r.flags,
        })
        .map_err(map_gpu3d_err)?;
        let resource = Arc::new(GpuResource {
            owner: file.file_id,
            res_handle,
            bo_handle,
            width: r.width,
            height: r.height,
            stride: r.stride,
            size,
            blob_mem: 0,
            blob_flags: 0,
            is_dumb_2d: false,
            last_fence: AtomicU64::new(0),
        });
        ax_display::gpu3d_attach_backing(
            res_handle,
            backing_paddr.as_usize() as u64,
            backing_size as u32,
        )
        .map_err(map_gpu3d_err)?;
        file.attach_resource(&resource)?;

        let buffer = DumbBuffer {
            owner: file.file_id,
            width: r.width,
            height: r.height,
            bpp: 32,
            pitch: r.stride,
            size,
            offset,
            pages,
            mappable: true,
            resource: Some(resource.clone()),
        };
        r.bo_handle = bo_handle;
        r.res_handle = res_handle;
        r.size = size as u32;
        if ptr.vm_write(current, r).is_err() {
            file.detach_resource(res_handle);
            return Err(VfsError::BadAddress);
        }

        self.dumbs.lock().insert(bo_handle, buffer);
        self.gpu_resources.lock().insert(res_handle, resource);

        Ok(0)
    }

    /// VIRTGPU_RESOURCE_INFO — queries resource information.
    ///
    /// Linux: `virtgpu_resource_info_ioctl()` in `virtgpu_ioctl.c`
    fn handle_virtgpu_resource_info(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *mut DrmVirtgpuResourceInfo;
        let mut info: DrmVirtgpuResourceInfo =
            ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;
        let resource = self
            .resource_for_handle(file, info.bo_handle)
            .ok_or(VfsError::NotFound)?;
        info.res_handle = resource.res_handle;
        info.size = resource.size as u32;
        info.blob_mem = resource.blob_mem;
        ptr.vm_write(current, info)
            .map_err(|_| VfsError::BadAddress)?;
        Ok(0)
    }

    /// VIRTGPU_MAP — maps a GEM handle to an mmap offset.
    ///
    /// Linux: `virtgpu_map_ioctl()` in `virtgpu_ioctl.c`
    ///
    /// Returns an offset that can be used with the mmap system call.
    /// This reuses the same offset mechanism as MAP_DUMB.
    fn handle_virtgpu_map(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *mut DrmVirtgpuMap;
        let mut m: DrmVirtgpuMap = ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;

        let dumbs = self.dumbs.lock();
        let buf = dumbs
            .get(&m.handle)
            .filter(|buffer| buffer.owner == file.file_id && buffer.mappable)
            .ok_or(VfsError::InvalidInput)?;
        m.offset = buf.offset;
        drop(dumbs);

        ptr.vm_write(current, m).map_err(|_| VfsError::BadAddress)?;
        Ok(0)
    }

    /// VIRTGPU_EXECBUFFER — submits a virgl command buffer.
    ///
    /// Linux: `virtgpu_execbuffer_ioctl()` in `virtgpu_ioctl.c`
    ///
    /// This is the core ioctl: Mesa submits VIRGL_CCMD_* command streams
    /// through this. The command buffer is read from userspace, along with
    /// an array of GEM handles that the commands reference.
    ///
    /// **Critical**: There is NO ctx_id field. The context is implicitly
    /// bound to the file descriptor.
    ///
    /// Returns `StarryResult` rather than `VfsResult`: the `FENCE_FD_OUT`
    /// descriptor pre-reservation can fail with `EMFILE`, which the `VfsError`
    /// domain cannot represent.
    fn handle_virtgpu_execbuffer(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> StarryResult<usize> {
        let ptr = arg as *mut DrmVirtgpuExecbuffer;
        let mut eb: DrmVirtgpuExecbuffer =
            ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;

        // Linux: if (vgdev->has_virgl_3d == false) return -ENOSYS;
        if !ax_display::has_virgl() {
            return Err(StarryError::Unsupported);
        }

        // Only the fence-fd in/out flags are supported. `VIRTGPU_EXECBUF_RING_IDX`
        // (0x04), the syncobj fields and any unknown flag stay unsupported and
        // must fail instead of being silently ignored (Linux checks
        // `exbuf->flags & ~VIRTGPU_EXECBUF_FLAGS`).
        let known_fence_flags = VIRTGPU_EXECBUF_FENCE_FD_IN | VIRTGPU_EXECBUF_FENCE_FD_OUT;
        if eb.flags & !known_fence_flags != 0
            || eb.ring_idx != 0
            || eb.syncobj_stride != 0
            || eb.num_in_syncobjs != 0
            || eb.num_out_syncobjs != 0
            || eb.in_syncobjs != 0
            || eb.out_syncobjs != 0
        {
            return Err(StarryError::InvalidInput);
        }
        let fence_in = eb.flags & VIRTGPU_EXECBUF_FENCE_FD_IN != 0;
        let fence_out = eb.flags & VIRTGPU_EXECBUF_FENCE_FD_OUT != 0;

        // Cheap input validation before any host side effect: a malformed
        // request must not create a context, attach a resource or submit work.
        if eb.size == 0
            || !eb.size.is_multiple_of(4)
            || eb.size as usize > MAX_VIRGL_COMMAND_BYTES
            || eb.command == 0
        {
            return Err(StarryError::InvalidInput);
        }
        if eb.num_bo_handles > 256 || (eb.num_bo_handles > 0 && eb.bo_handles == 0) {
            return Err(StarryError::InvalidInput);
        }

        // `FENCE_FD_IN` imports an existing fence fd. Only a sync_file created
        // by this driver carries one; a foreign object under that fd number is
        // -EINVAL, matching Linux `sync_file_get_fence()` returning NULL.
        if fence_in {
            if eb.fence_fd < 0 {
                return Err(StarryError::InvalidInput);
            }
            let file =
                crate::file::get_file_like(eb.fence_fd).map_err(|_| VfsError::InvalidInput)?;
            let fence = file
                .downcast_arc::<SyncFile>()
                .map_err(|_| VfsError::InvalidInput)?;
            // Every out-fence produced by this driver is already signaled by
            // the time its fd is visible, so the dependency is satisfied. An
            // unsignaled fence cannot be produced here; treat it as an invalid
            // import rather than parking the submit.
            if !fence.is_signaled() {
                return Err(StarryError::InvalidInput);
            }
        }

        // `FENCE_FD_OUT` reserves the descriptor *before* any side effect: an
        // fd shortage must fail the ioctl (with EMFILE) without having created
        // a context or queued GPU work. The reservation is released
        // automatically if a later step fails.
        let out_fence = if fence_out {
            let sync_file = Arc::new(SyncFile::new());
            let created: Arc<dyn FileLike> = sync_file.clone();
            let prepared = prepare_file_like(move || Ok(created), true)?;
            Some((prepared, sync_file))
        } else {
            None
        };

        // Read the command buffer and BO handles, then resolve every handle to
        // an owned resource — all before creating a context, so a bad command
        // buffer or GEM handle leaves no host state behind.
        let cmd_buf = vm_load(current, eb.command as *const u8, eb.size as usize)
            .map_err(|_| VfsError::BadAddress)?;
        let handles = if eb.num_bo_handles == 0 {
            Vec::new()
        } else {
            vm_load(
                current,
                eb.bo_handles as *const u32,
                eb.num_bo_handles as usize,
            )
            .map_err(|_| VfsError::BadAddress)?
        };
        let mut resources = Vec::with_capacity(handles.len());
        for handle in handles {
            let resource = self
                .resource_for_handle(file, handle)
                .ok_or(VfsError::NotFound)?;
            resources.push(resource);
        }

        // Context creation and resource attach are the first host side
        // effects; Linux reaches `virtio_gpu_create_context()` at the same
        // point, after the inputs above validated.
        let ctx_id = file.ensure_lazy_context()?;
        for resource in &resources {
            file.attach_resource(resource)?;
        }

        let fence_id = ax_display::gpu3d_submit_cmd(ctx_id, &cmd_buf).map_err(map_gpu3d_err)?;
        for resource in resources {
            resource.last_fence.store(fence_id, Ordering::Release);
        }

        // The submit completed synchronously: its host fence response was
        // already consumed, so an out-fence is signaled here and the fd only
        // becomes visible after this point. `fence_fd` is written back only
        // for `FENCE_FD_OUT`; an IN-only request keeps its input fd.
        if let Some((prepared, sync_file)) = out_fence {
            sync_file.mark_signaled();
            eb.fence_fd = prepared.fd();
            ptr.vm_write(current, eb)
                .map_err(|_| VfsError::BadAddress)?;
            prepared.install();
        } else {
            ptr.vm_write(current, eb)
                .map_err(|_| VfsError::BadAddress)?;
        }

        Ok(0)
    }

    /// VIRTGPU_TRANSFER_TO_HOST — transfers data from guest to host.
    ///
    /// Linux: `virtgpu_transfer_from_host_ioctl()` in `virtgpu_ioctl.c`
    /// (Note: Linux naming is confusing — "from_host" means "from guest
    /// memory to host" in the virtio-gpu spec.)
    fn handle_virtgpu_transfer_to_host(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let t: DrmVirtgpu3dTransferToHost = (arg as *const DrmVirtgpu3dTransferToHost)
            .vm_read(current)
            .map_err(|_| VfsError::BadAddress)?;

        // Linux: if (vgdev->has_virgl_3d == false) return -ENOSYS;
        if !ax_display::has_virgl() {
            return Err(VfsError::Unsupported);
        }

        // Linux `virtio_gpu_transfer_to_host_ioctl()` creates the context on
        // the virgl branch before forwarding the 3D transfer.
        let ctx_id = file.ensure_lazy_context()?;
        let resource = self
            .resource_for_handle(file, t.bo_handle)
            .ok_or(VfsError::NotFound)?;
        file.attach_resource(&resource)?;

        // Forward to the display driver.
        ax_display::gpu3d_transfer_to_host(ax_display::Transfer3d {
            ctx_id,
            resource_id: resource.res_handle,
            box_: ax_display::TransferBox {
                x: t.box_.x,
                y: t.box_.y,
                z: t.box_.z,
                w: t.box_.w,
                h: t.box_.h,
                d: t.box_.d,
            },
            offset: t.offset as u64,
            level: t.level,
            stride: t.stride,
            layer_stride: t.layer_stride,
        })
        .map_err(map_gpu3d_err)?;

        Ok(0)
    }

    /// VIRTGPU_TRANSFER_FROM_HOST — transfers data from host to guest.
    ///
    /// Linux: `virtgpu_transfer_to_host_ioctl()` in `virtgpu_ioctl.c`
    fn handle_virtgpu_transfer_from_host(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let t: DrmVirtgpu3dTransferFromHost = (arg as *const DrmVirtgpu3dTransferFromHost)
            .vm_read(current)
            .map_err(|_| VfsError::BadAddress)?;

        // Linux: if (vgdev->has_virgl_3d == false) return -ENOSYS;
        if !ax_display::has_virgl() {
            return Err(VfsError::Unsupported);
        }

        // Linux `virtio_gpu_transfer_from_host_ioctl()` likewise creates the
        // context before forwarding the 3D transfer.
        let ctx_id = file.ensure_lazy_context()?;
        let resource = self
            .resource_for_handle(file, t.bo_handle)
            .ok_or(VfsError::NotFound)?;
        file.attach_resource(&resource)?;

        // Forward to the display driver.
        ax_display::gpu3d_transfer_from_host(ax_display::Transfer3d {
            ctx_id,
            resource_id: resource.res_handle,
            box_: ax_display::TransferBox {
                x: t.box_.x,
                y: t.box_.y,
                z: t.box_.z,
                w: t.box_.w,
                h: t.box_.h,
                d: t.box_.d,
            },
            offset: t.offset as u64,
            level: t.level,
            stride: t.stride,
            layer_stride: t.layer_stride,
        })
        .map_err(map_gpu3d_err)?;

        Ok(0)
    }

    /// VIRTGPU_WAIT — waits for a resource to become idle.
    ///
    /// Linux: `virtgpu_wait_ioctl()` in `virtgpu_ioctl.c`
    ///
    /// We implement this as a synchronous wait (the resource is always
    /// "ready" since we process commands synchronously).
    fn handle_virtgpu_wait(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let w: DrmVirtgpu3dWait = (arg as *const DrmVirtgpu3dWait)
            .vm_read(current)
            .map_err(|_| VfsError::BadAddress)?;

        // Linux: handle=0 is invalid.
        if w.handle == 0 {
            return Err(VfsError::InvalidInput);
        }

        let has_dumb = self
            .dumbs
            .lock()
            .get(&w.handle)
            .is_some_and(|buffer| buffer.owner == file.file_id);
        let resource = self.resource_for_handle(file, w.handle);
        if !has_dumb && resource.is_none() {
            return Err(VfsError::NotFound);
        }
        // Every driver submission waits for its virtqueue completion, so a
        // returned ioctl has already completed the last fence for this GEM.
        let _last_fence = resource.map(|resource| resource.last_fence.load(Ordering::Acquire));
        Ok(0)
    }

    /// VIRTGPU_RESOURCE_CREATE_BLOB — creates a blob resource.
    ///
    /// Linux: `virtgpu_resource_create_blob_ioctl()` in `virtgpu_ioctl.c`
    ///
    /// Guest-backed blob types carry a guest memory entry; host-only blobs
    /// stay unmappable until RESOURCE_MAP_BLOB is implemented.
    fn handle_virtgpu_resource_create_blob(
        &self,
        file: &Card0File,
        current: &UserTaskRef,
        arg: usize,
    ) -> VfsResult<usize> {
        let ptr = arg as *mut DrmVirtgpuResourceCreateBlob;
        let mut b: DrmVirtgpuResourceCreateBlob =
            ptr.vm_read(current).map_err(|_| VfsError::BadAddress)?;

        // Linux: if (!vgdev->has_resource_blob) return -EINVAL;
        if !ax_display::has_resource_blob() {
            return Err(VfsError::InvalidInput);
        }
        // Linux: if (rc_blob->blob_flags & ~VIRTGPU_BLOB_FLAG_USE_MASK)
        //           return -EINVAL;
        // Note: VIRTGPU_BLOB_FLAG_USE_* 是 0x0001, 0x0002, 0x0004
        // VIRTGPU_BLOB_FLAG_USE_MASK = 0x0007
        if (b.blob_flags & !0x0007) != 0 {
            return Err(VfsError::InvalidInput);
        }
        // Linux `verify_blob()` refuses a cross-device blob when the device has
        // no RESOURCE_ASSIGN_UUID support: `!vgdev->has_resource_assign_uuid`
        // returns -EINVAL. GETPARAM already reports
        // VIRTGPU_PARAM_CROSS_DEVICE = 0 for this card, so reject the flag here
        // rather than forwarding it to a device that cannot honour it.
        if (b.blob_flags & VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE) != 0 {
            return Err(VfsError::InvalidInput);
        }

        // Validate blob memory type and determine blob category.
        let (guest_blob, host3d_blob) = match b.blob_mem {
            VIRTGPU_BLOB_MEM_GUEST => (true, false),
            VIRTGPU_BLOB_MEM_HOST3D_GUEST => (true, true),
            VIRTGPU_BLOB_MEM_HOST3D => (false, true),
            _ => {
                // Linux: default: return -EINVAL;
                return Err(VfsError::InvalidInput);
            }
        };

        // Linux: if (*host3d_blob) {
        //           if (!vgdev->has_virgl_3d) return -EINVAL;
        //           if (rc_blob->cmd_size % 4 != 0) return -EINVAL;
        if host3d_blob {
            if !ax_display::has_virgl() {
                return Err(VfsError::InvalidInput);
            }
            // cmd_size 必须 4 字节对齐
            if !b.cmd_size.is_multiple_of(4) {
                return Err(VfsError::InvalidInput);
            }
        } else {
            // Linux: if (rc_blob->blob_id != 0) return -EINVAL;
            //        if (rc_blob->cmd_size != 0) return -EINVAL;
            if b.blob_id != 0 || b.cmd_size != 0 {
                return Err(VfsError::InvalidInput);
            }
        }

        // Linux `virtio_gpu_resource_create_blob_ioctl()` calls
        // `virtio_gpu_create_context()` on the virgl path before touching the
        // command stream, so the lazy context is created even for a pure guest
        // blob even though that blob is not attached to it. Host3D variants
        // are context-owned, so they keep the resulting ctx_id; a guest-only
        // blob still sends ctx_id = 0.
        let ctx_id = if ax_display::has_virgl() {
            let ctx_id = file.ensure_lazy_context()?;
            if host3d_blob { ctx_id } else { 0 }
        } else {
            0
        };

        if b.cmd_size as usize > MAX_VIRGL_COMMAND_BYTES
            || (b.cmd_size > 0 && b.cmd == 0)
            || b.size == 0
            || b.size > DUMB_BUFFER_MAX_SIZE as u64
        {
            return Err(VfsError::InvalidInput);
        }

        // Linux copies the command stream before creating/publishing the GEM
        // object. A bad userspace pointer must not leave a host resource.
        let cmd_buf = if b.cmd_size == 0 {
            Vec::new()
        } else {
            vm_load(current, b.cmd as *const u8, b.cmd_size as usize)
                .map_err(|_| VfsError::BadAddress)?
        };

        // Allocate a guest shadow buffer so VIRTGPU_MAP/mmap on the blob's
        // GEM handle keeps working. For HOST3D the real backing lives on
        // the host and we deliberately do NOT send these pages to the
        // device (nr_entries=0): QEMU and virglrenderer reject HOST3D blobs
        // that carry an iov. The shadow is only for mmap compatibility —
        // the present path is zero-copy on the host, no CPU readback.
        let alloc_size = (b.size as usize).div_ceil(PAGE_SIZE_4K) * PAGE_SIZE_4K;
        let pages = GlobalPage::alloc_contiguous(alloc_size / PAGE_SIZE_4K, PAGE_SIZE_4K)
            .map_err(|_| VfsError::NoMemory)?;

        // Zero-initialize.
        unsafe {
            core::ptr::write_bytes(pages.start_vaddr().as_ptr() as *mut u8, 0, alloc_size);
        }

        let backing = guest_blob.then(|| ax_display::BlobMemory {
            paddr: virt_to_phys(pages.start_vaddr()).as_usize() as u64,
            length: pages.size() as u32,
        });
        let pages = Arc::new(pages);
        let bo_handle = self.next_dumb_handle.fetch_add(1, Ordering::Relaxed);
        let offset = self
            .next_offset
            .fetch_add(DUMB_BUFFER_OFFSET_STRIDE, Ordering::Relaxed);
        let res_handle = self.next_res_handle.fetch_add(1, Ordering::Relaxed);
        ax_display::gpu3d_resource_create_blob(ax_display::ResourceCreateBlob {
            ctx_id,
            resource_id: res_handle,
            blob_mem: b.blob_mem,
            blob_flags: b.blob_flags,
            size: b.size,
            blob_id: b.blob_id,
            backing,
            cmd: &cmd_buf,
        })
        .map_err(map_gpu3d_err)?;
        let resource = Arc::new(GpuResource {
            owner: file.file_id,
            res_handle,
            bo_handle,
            width: 0,
            height: 0,
            stride: 0,
            size: b.size,
            blob_mem: b.blob_mem,
            blob_flags: b.blob_flags,
            is_dumb_2d: false,
            last_fence: AtomicU64::new(0),
        });
        if host3d_blob {
            file.attach_resource(&resource)?;
        }
        let buffer = DumbBuffer {
            owner: file.file_id,
            width: 0,
            height: 0,
            bpp: 0,
            pitch: 0,
            size: b.size,
            offset,
            pages,
            mappable: guest_blob,
            resource: Some(resource.clone()),
        };

        b.bo_handle = bo_handle;
        b.res_handle = res_handle;
        if ptr.vm_write(current, b).is_err() {
            if host3d_blob {
                file.detach_resource(res_handle);
            }
            return Err(VfsError::BadAddress);
        }

        self.dumbs.lock().insert(bo_handle, buffer);
        self.gpu_resources.lock().insert(res_handle, resource);

        Ok(0)
    }
}

/// Map a fixed object id to its `DRM_MODE_OBJECT_*` type tag.
fn object_type_of(id: u32) -> Option<u32> {
    match id {
        CRTC_ID => Some(DRM_MODE_OBJECT_CRTC),
        CONNECTOR_ID => Some(DRM_MODE_OBJECT_CONNECTOR),
        PLANE_ID => Some(DRM_MODE_OBJECT_PLANE),
        _ => None,
    }
}

/// Map a GPU 3D error from the display layer to a VfsError.
fn map_gpu3d_err(err: ax_display::DisplayError) -> VfsError {
    match err {
        ax_display::DisplayError::NotSupported => VfsError::Unsupported,
        ax_display::DisplayError::NotAvailable => VfsError::WouldBlock,
        ax_display::DisplayError::Gpu3dError(kind) => match kind {
            ax_display::Gpu3dErrorKind::InvalidParam => VfsError::InvalidInput,
            ax_display::Gpu3dErrorKind::NotReady => VfsError::WouldBlock,
            _ => VfsError::Io,
        },
        _ => VfsError::Io,
    }
}

/// Narrow a userspace-supplied u64 to an i32-range signed integer.
fn checked_i32(value: u64) -> VfsResult<i32> {
    let v = value as i64;
    if (i32::MIN as i64..=i32::MAX as i64).contains(&v) {
        Ok(v as i32)
    } else {
        Err(VfsError::InvalidInput)
    }
}

// Suppress dead_code for `DumbBuffer.width/height/bpp/pitch`.  These
// four fields are metadata-only (see the struct-level doc comment) and
// are never consumed by any ioctl handler, but keeping them in the
// struct makes a potential future `GET_DUMB_INFO` possible and makes
// debug dumps informative.  The closure below signals to the compiler
// that the field access is intentional — they are not "unnecessary".
#[allow(dead_code)]
const _DUMB_BUFFER_FIELDS_USED: fn(&DumbBuffer) = |b| {
    let _ = (b.width, b.height, b.bpp, b.pitch);
    let _ = (b.size, b.offset, &b.pages);
};