summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/os/cpu.c
blob: 2efb68889cbc4f5089433f094dfb91eb146bb244 (plain)
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright (c) 2012 by Delphix. All rights reserved.
 * Copyright 2018 Joyent, Inc.
 */

/*
 * Architecture-independent CPU control functions.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/var.h>
#include <sys/thread.h>
#include <sys/cpuvar.h>
#include <sys/cpu_event.h>
#include <sys/kstat.h>
#include <sys/uadmin.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/cmn_err.h>
#include <sys/procset.h>
#include <sys/processor.h>
#include <sys/debug.h>
#include <sys/cpupart.h>
#include <sys/lgrp.h>
#include <sys/pset.h>
#include <sys/pghw.h>
#include <sys/kmem.h>
#include <sys/kmem_impl.h>	/* to set per-cpu kmem_cache offset */
#include <sys/atomic.h>
#include <sys/callb.h>
#include <sys/vtrace.h>
#include <sys/cyclic.h>
#include <sys/bitmap.h>
#include <sys/nvpair.h>
#include <sys/pool_pset.h>
#include <sys/msacct.h>
#include <sys/time.h>
#include <sys/archsystm.h>
#include <sys/sdt.h>
#if defined(__x86) || defined(__amd64)
#include <sys/x86_archext.h>
#endif
#include <sys/callo.h>

extern int	mp_cpu_start(cpu_t *);
extern int	mp_cpu_stop(cpu_t *);
extern int	mp_cpu_poweron(cpu_t *);
extern int	mp_cpu_poweroff(cpu_t *);
extern int	mp_cpu_configure(int);
extern int	mp_cpu_unconfigure(int);
extern void	mp_cpu_faulted_enter(cpu_t *);
extern void	mp_cpu_faulted_exit(cpu_t *);

extern int cmp_cpu_to_chip(processorid_t cpuid);
#ifdef __sparcv9
extern char *cpu_fru_fmri(cpu_t *cp);
#endif

static void cpu_add_active_internal(cpu_t *cp);
static void cpu_remove_active(cpu_t *cp);
static void cpu_info_kstat_create(cpu_t *cp);
static void cpu_info_kstat_destroy(cpu_t *cp);
static void cpu_stats_kstat_create(cpu_t *cp);
static void cpu_stats_kstat_destroy(cpu_t *cp);

static int cpu_sys_stats_ks_update(kstat_t *ksp, int rw);
static int cpu_vm_stats_ks_update(kstat_t *ksp, int rw);
static int cpu_stat_ks_update(kstat_t *ksp, int rw);
static int cpu_state_change_hooks(int, cpu_setup_t, cpu_setup_t);

/*
 * cpu_lock protects ncpus, ncpus_online, cpu_flag, cpu_list, cpu_active,
 * max_cpu_seqid_ever, and dispatch queue reallocations.  The lock ordering with
 * respect to related locks is:
 *
 *	cpu_lock --> thread_free_lock  --->  p_lock  --->  thread_lock()
 *
 * Warning:  Certain sections of code do not use the cpu_lock when
 * traversing the cpu_list (e.g. mutex_vector_enter(), clock()).  Since
 * all cpus are paused during modifications to this list, a solution
 * to protect the list is too either disable kernel preemption while
 * walking the list, *or* recheck the cpu_next pointer at each
 * iteration in the loop.  Note that in no cases can any cached
 * copies of the cpu pointers be kept as they may become invalid.
 */
kmutex_t	cpu_lock;
cpu_t		*cpu_list;		/* list of all CPUs */
cpu_t		*clock_cpu_list;	/* used by clock to walk CPUs */
cpu_t		*cpu_active;		/* list of active CPUs */
cpuset_t	cpu_active_set;		/* cached set of active CPUs */
cpuset_t	cpu_available;		/* set of available CPUs */
cpuset_t	cpu_seqid_inuse;	/* which cpu_seqids are in use */

cpu_t		**cpu_seq;		/* ptrs to CPUs, indexed by seq_id */

/*
 * max_ncpus keeps the max cpus the system can have. Initially
 * it's NCPU, but since most archs scan the devtree for cpus
 * fairly early on during boot, the real max can be known before
 * ncpus is set (useful for early NCPU based allocations).
 */
int max_ncpus = NCPU;
/*
 * platforms that set max_ncpus to maxiumum number of cpus that can be
 * dynamically added will set boot_max_ncpus to the number of cpus found
 * at device tree scan time during boot.
 */
int boot_max_ncpus = -1;
int boot_ncpus = -1;
/*
 * Maximum possible CPU id.  This can never be >= NCPU since NCPU is
 * used to size arrays that are indexed by CPU id.
 */
processorid_t max_cpuid = NCPU - 1;

/*
 * Maximum cpu_seqid was given. This number can only grow and never shrink. It
 * can be used to optimize NCPU loops to avoid going through CPUs which were
 * never on-line.
 */
processorid_t max_cpu_seqid_ever = 0;

int ncpus = 1;
int ncpus_online = 1;

/*
 * CPU that we're trying to offline.  Protected by cpu_lock.
 */
cpu_t *cpu_inmotion;

/*
 * Can be raised to suppress further weakbinding, which are instead
 * satisfied by disabling preemption.  Must be raised/lowered under cpu_lock,
 * while individual thread weakbinding synchronization is done under thread
 * lock.
 */
int weakbindingbarrier;

/*
 * Variables used in pause_cpus().
 */
static volatile char safe_list[NCPU];

static struct _cpu_pause_info {
	int		cp_spl;		/* spl saved in pause_cpus() */
	volatile int	cp_go;		/* Go signal sent after all ready */
	int		cp_count;	/* # of CPUs to pause */
	ksema_t		cp_sem;		/* synch pause_cpus & cpu_pause */
	kthread_id_t	cp_paused;
	void		*(*cp_func)(void *);
} cpu_pause_info;

static kmutex_t pause_free_mutex;
static kcondvar_t pause_free_cv;


static struct cpu_sys_stats_ks_data {
	kstat_named_t cpu_ticks_idle;
	kstat_named_t cpu_ticks_user;
	kstat_named_t cpu_ticks_kernel;
	kstat_named_t cpu_ticks_wait;
	kstat_named_t cpu_nsec_idle;
	kstat_named_t cpu_nsec_user;
	kstat_named_t cpu_nsec_kernel;
	kstat_named_t cpu_nsec_dtrace;
	kstat_named_t cpu_nsec_intr;
	kstat_named_t cpu_load_intr;
	kstat_named_t wait_ticks_io;
	kstat_named_t dtrace_probes;
	kstat_named_t bread;
	kstat_named_t bwrite;
	kstat_named_t lread;
	kstat_named_t lwrite;
	kstat_named_t phread;
	kstat_named_t phwrite;
	kstat_named_t pswitch;
	kstat_named_t trap;
	kstat_named_t intr;
	kstat_named_t syscall;
	kstat_named_t sysread;
	kstat_named_t syswrite;
	kstat_named_t sysfork;
	kstat_named_t sysvfork;
	kstat_named_t sysexec;
	kstat_named_t readch;
	kstat_named_t writech;
	kstat_named_t rcvint;
	kstat_named_t xmtint;
	kstat_named_t mdmint;
	kstat_named_t rawch;
	kstat_named_t canch;
	kstat_named_t outch;
	kstat_named_t msg;
	kstat_named_t sema;
	kstat_named_t namei;
	kstat_named_t ufsiget;
	kstat_named_t ufsdirblk;
	kstat_named_t ufsipage;
	kstat_named_t ufsinopage;
	kstat_named_t procovf;
	kstat_named_t intrthread;
	kstat_named_t intrblk;
	kstat_named_t intrunpin;
	kstat_named_t idlethread;
	kstat_named_t inv_swtch;
	kstat_named_t nthreads;
	kstat_named_t cpumigrate;
	kstat_named_t xcalls;
	kstat_named_t mutex_adenters;
	kstat_named_t rw_rdfails;
	kstat_named_t rw_wrfails;
	kstat_named_t modload;
	kstat_named_t modunload;
	kstat_named_t bawrite;
	kstat_named_t iowait;
} cpu_sys_stats_ks_data_template = {
	{ "cpu_ticks_idle",	KSTAT_DATA_UINT64 },
	{ "cpu_ticks_user",	KSTAT_DATA_UINT64 },
	{ "cpu_ticks_kernel",	KSTAT_DATA_UINT64 },
	{ "cpu_ticks_wait",	KSTAT_DATA_UINT64 },
	{ "cpu_nsec_idle",	KSTAT_DATA_UINT64 },
	{ "cpu_nsec_user",	KSTAT_DATA_UINT64 },
	{ "cpu_nsec_kernel",	KSTAT_DATA_UINT64 },
	{ "cpu_nsec_dtrace",	KSTAT_DATA_UINT64 },
	{ "cpu_nsec_intr",	KSTAT_DATA_UINT64 },
	{ "cpu_load_intr",	KSTAT_DATA_UINT64 },
	{ "wait_ticks_io",	KSTAT_DATA_UINT64 },
	{ "dtrace_probes",	KSTAT_DATA_UINT64 },
	{ "bread",		KSTAT_DATA_UINT64 },
	{ "bwrite",		KSTAT_DATA_UINT64 },
	{ "lread",		KSTAT_DATA_UINT64 },
	{ "lwrite",		KSTAT_DATA_UINT64 },
	{ "phread",		KSTAT_DATA_UINT64 },
	{ "phwrite",		KSTAT_DATA_UINT64 },
	{ "pswitch",		KSTAT_DATA_UINT64 },
	{ "trap",		KSTAT_DATA_UINT64 },
	{ "intr",		KSTAT_DATA_UINT64 },
	{ "syscall",		KSTAT_DATA_UINT64 },
	{ "sysread",		KSTAT_DATA_UINT64 },
	{ "syswrite",		KSTAT_DATA_UINT64 },
	{ "sysfork",		KSTAT_DATA_UINT64 },
	{ "sysvfork",		KSTAT_DATA_UINT64 },
	{ "sysexec",		KSTAT_DATA_UINT64 },
	{ "readch",		KSTAT_DATA_UINT64 },
	{ "writech",		KSTAT_DATA_UINT64 },
	{ "rcvint",		KSTAT_DATA_UINT64 },
	{ "xmtint",		KSTAT_DATA_UINT64 },
	{ "mdmint",		KSTAT_DATA_UINT64 },
	{ "rawch",		KSTAT_DATA_UINT64 },
	{ "canch",		KSTAT_DATA_UINT64 },
	{ "outch",		KSTAT_DATA_UINT64 },
	{ "msg",		KSTAT_DATA_UINT64 },
	{ "sema",		KSTAT_DATA_UINT64 },
	{ "namei",		KSTAT_DATA_UINT64 },
	{ "ufsiget",		KSTAT_DATA_UINT64 },
	{ "ufsdirblk",		KSTAT_DATA_UINT64 },
	{ "ufsipage",		KSTAT_DATA_UINT64 },
	{ "ufsinopage",		KSTAT_DATA_UINT64 },
	{ "procovf",		KSTAT_DATA_UINT64 },
	{ "intrthread",		KSTAT_DATA_UINT64 },
	{ "intrblk",		KSTAT_DATA_UINT64 },
	{ "intrunpin",		KSTAT_DATA_UINT64 },
	{ "idlethread",		KSTAT_DATA_UINT64 },
	{ "inv_swtch",		KSTAT_DATA_UINT64 },
	{ "nthreads",		KSTAT_DATA_UINT64 },
	{ "cpumigrate",		KSTAT_DATA_UINT64 },
	{ "xcalls",		KSTAT_DATA_UINT64 },
	{ "mutex_adenters",	KSTAT_DATA_UINT64 },
	{ "rw_rdfails",		KSTAT_DATA_UINT64 },
	{ "rw_wrfails",		KSTAT_DATA_UINT64 },
	{ "modload",		KSTAT_DATA_UINT64 },
	{ "modunload",		KSTAT_DATA_UINT64 },
	{ "bawrite",		KSTAT_DATA_UINT64 },
	{ "iowait",		KSTAT_DATA_UINT64 },
};

static struct cpu_vm_stats_ks_data {
	kstat_named_t pgrec;
	kstat_named_t pgfrec;
	kstat_named_t pgin;
	kstat_named_t pgpgin;
	kstat_named_t pgout;
	kstat_named_t pgpgout;
	kstat_named_t swapin;
	kstat_named_t pgswapin;
	kstat_named_t swapout;
	kstat_named_t pgswapout;
	kstat_named_t zfod;
	kstat_named_t dfree;
	kstat_named_t scan;
	kstat_named_t rev;
	kstat_named_t hat_fault;
	kstat_named_t as_fault;
	kstat_named_t maj_fault;
	kstat_named_t cow_fault;
	kstat_named_t prot_fault;
	kstat_named_t softlock;
	kstat_named_t kernel_asflt;
	kstat_named_t pgrrun;
	kstat_named_t execpgin;
	kstat_named_t execpgout;
	kstat_named_t execfree;
	kstat_named_t anonpgin;
	kstat_named_t anonpgout;
	kstat_named_t anonfree;
	kstat_named_t fspgin;
	kstat_named_t fspgout;
	kstat_named_t fsfree;
} cpu_vm_stats_ks_data_template = {
	{ "pgrec",		KSTAT_DATA_UINT64 },
	{ "pgfrec",		KSTAT_DATA_UINT64 },
	{ "pgin",		KSTAT_DATA_UINT64 },
	{ "pgpgin",		KSTAT_DATA_UINT64 },
	{ "pgout",		KSTAT_DATA_UINT64 },
	{ "pgpgout",		KSTAT_DATA_UINT64 },
	{ "swapin",		KSTAT_DATA_UINT64 },
	{ "pgswapin",		KSTAT_DATA_UINT64 },
	{ "swapout",		KSTAT_DATA_UINT64 },
	{ "pgswapout",		KSTAT_DATA_UINT64 },
	{ "zfod",		KSTAT_DATA_UINT64 },
	{ "dfree",		KSTAT_DATA_UINT64 },
	{ "scan",		KSTAT_DATA_UINT64 },
	{ "rev",		KSTAT_DATA_UINT64 },
	{ "hat_fault",		KSTAT_DATA_UINT64 },
	{ "as_fault",		KSTAT_DATA_UINT64 },
	{ "maj_fault",		KSTAT_DATA_UINT64 },
	{ "cow_fault",		KSTAT_DATA_UINT64 },
	{ "prot_fault",		KSTAT_DATA_UINT64 },
	{ "softlock",		KSTAT_DATA_UINT64 },
	{ "kernel_asflt",	KSTAT_DATA_UINT64 },
	{ "pgrrun",		KSTAT_DATA_UINT64 },
	{ "execpgin",		KSTAT_DATA_UINT64 },
	{ "execpgout",		KSTAT_DATA_UINT64 },
	{ "execfree",		KSTAT_DATA_UINT64 },
	{ "anonpgin",		KSTAT_DATA_UINT64 },
	{ "anonpgout",		KSTAT_DATA_UINT64 },
	{ "anonfree",		KSTAT_DATA_UINT64 },
	{ "fspgin",		KSTAT_DATA_UINT64 },
	{ "fspgout",		KSTAT_DATA_UINT64 },
	{ "fsfree",		KSTAT_DATA_UINT64 },
};

/*
 * Force the specified thread to migrate to the appropriate processor.
 * Called with thread lock held, returns with it dropped.
 */
static void
force_thread_migrate(kthread_id_t tp)
{
	ASSERT(THREAD_LOCK_HELD(tp));
	if (tp == curthread) {
		THREAD_TRANSITION(tp);
		CL_SETRUN(tp);
		thread_unlock_nopreempt(tp);
		swtch();
	} else {
		if (tp->t_state == TS_ONPROC) {
			cpu_surrender(tp);
		} else if (tp->t_state == TS_RUN) {
			(void) dispdeq(tp);
			setbackdq(tp);
		}
		thread_unlock(tp);
	}
}

/*
 * Set affinity for a specified CPU.
 *
 * Specifying a cpu_id of CPU_CURRENT, allowed _only_ when setting affinity for
 * curthread, will set affinity to the CPU on which the thread is currently
 * running.  For other cpu_id values, the caller must ensure that the
 * referenced CPU remains valid, which can be done by holding cpu_lock across
 * this call.
 *
 * CPU affinity is guaranteed after return of thread_affinity_set().  If a
 * caller setting affinity to CPU_CURRENT requires that its thread not migrate
 * CPUs prior to a successful return, it should take extra precautions (such as
 * their own call to kpreempt_disable) to ensure that safety.
 *
 * A CPU affinity reference count is maintained by thread_affinity_set and
 * thread_affinity_clear (incrementing and decrementing it, respectively),
 * maintaining CPU affinity while the count is non-zero, and allowing regions
 * of code which require affinity to be nested.
 */
void
thread_affinity_set(kthread_id_t t, int cpu_id)
{
	cpu_t *cp;

	ASSERT(!(t == curthread && t->t_weakbound_cpu != NULL));

	if (cpu_id == CPU_CURRENT) {
		VERIFY3P(t, ==, curthread);
		kpreempt_disable();
		cp = CPU;
	} else {
		/*
		 * We should be asserting that cpu_lock is held here, but
		 * the NCA code doesn't acquire it.  The following assert
		 * should be uncommented when the NCA code is fixed.
		 *
		 * ASSERT(MUTEX_HELD(&cpu_lock));
		 */
		VERIFY((cpu_id >= 0) && (cpu_id < NCPU));
		cp = cpu[cpu_id];

		/* user must provide a good cpu_id */
		VERIFY(cp != NULL);
	}

	/*
	 * If there is already a hard affinity requested, and this affinity
	 * conflicts with that, panic.
	 */
	thread_lock(t);
	if (t->t_affinitycnt > 0 && t->t_bound_cpu != cp) {
		panic("affinity_set: setting %p but already bound to %p",
		    (void *)cp, (void *)t->t_bound_cpu);
	}
	t->t_affinitycnt++;
	t->t_bound_cpu = cp;

	/*
	 * Make sure we're running on the right CPU.
	 */
	if (cp != t->t_cpu || t != curthread) {
		ASSERT(cpu_id != CPU_CURRENT);
		force_thread_migrate(t);	/* drops thread lock */
	} else {
		thread_unlock(t);
	}

	if (cpu_id == CPU_CURRENT) {
		kpreempt_enable();
	}
}

/*
 *	Wrapper for backward compatibility.
 */
void
affinity_set(int cpu_id)
{
	thread_affinity_set(curthread, cpu_id);
}

/*
 * Decrement the affinity reservation count and if it becomes zero,
 * clear the CPU affinity for the current thread, or set it to the user's
 * software binding request.
 */
void
thread_affinity_clear(kthread_id_t t)
{
	register processorid_t binding;

	thread_lock(t);
	if (--t->t_affinitycnt == 0) {
		if ((binding = t->t_bind_cpu) == PBIND_NONE) {
			/*
			 * Adjust disp_max_unbound_pri if necessary.
			 */
			disp_adjust_unbound_pri(t);
			t->t_bound_cpu = NULL;
			if (t->t_cpu->cpu_part != t->t_cpupart) {
				force_thread_migrate(t);
				return;
			}
		} else {
			t->t_bound_cpu = cpu[binding];
			/*
			 * Make sure the thread is running on the bound CPU.
			 */
			if (t->t_cpu != t->t_bound_cpu) {
				force_thread_migrate(t);
				return;		/* already dropped lock */
			}
		}
	}
	thread_unlock(t);
}

/*
 * Wrapper for backward compatibility.
 */
void
affinity_clear(void)
{
	thread_affinity_clear(curthread);
}

/*
 * Weak cpu affinity.  Bind to the "current" cpu for short periods
 * of time during which the thread must not block (but may be preempted).
 * Use this instead of kpreempt_disable() when it is only "no migration"
 * rather than "no preemption" semantics that are required - disabling
 * preemption holds higher priority threads off of cpu and if the
 * operation that is protected is more than momentary this is not good
 * for realtime etc.
 *
 * Weakly bound threads will not prevent a cpu from being offlined -
 * we'll only run them on the cpu to which they are weakly bound but
 * (because they do not block) we'll always be able to move them on to
 * another cpu at offline time if we give them just a short moment to
 * run during which they will unbind.  To give a cpu a chance of offlining,
 * however, we require a barrier to weak bindings that may be raised for a
 * given cpu (offline/move code may set this and then wait a short time for
 * existing weak bindings to drop); the cpu_inmotion pointer is that barrier.
 *
 * There are few restrictions on the calling context of thread_nomigrate.
 * The caller must not hold the thread lock.  Calls may be nested.
 *
 * After weakbinding a thread must not perform actions that may block.
 * In particular it must not call thread_affinity_set; calling that when
 * already weakbound is nonsensical anyway.
 *
 * If curthread is prevented from migrating for other reasons
 * (kernel preemption disabled; high pil; strongly bound; interrupt thread)
 * then the weak binding will succeed even if this cpu is the target of an
 * offline/move request.
 */
void
thread_nomigrate(void)
{
	cpu_t *cp;
	kthread_id_t t = curthread;

again:
	kpreempt_disable();
	cp = CPU;

	/*
	 * A highlevel interrupt must not modify t_nomigrate or
	 * t_weakbound_cpu of the thread it has interrupted.  A lowlevel
	 * interrupt thread cannot migrate and we can avoid the
	 * thread_lock call below by short-circuiting here.  In either
	 * case we can just return since no migration is possible and
	 * the condition will persist (ie, when we test for these again
	 * in thread_allowmigrate they can't have changed).   Migration
	 * is also impossible if we're at or above DISP_LEVEL pil.
	 */
	if (CPU_ON_INTR(cp) || t->t_flag & T_INTR_THREAD ||
	    getpil() >= DISP_LEVEL) {
		kpreempt_enable();
		return;
	}

	/*
	 * We must be consistent with existing weak bindings.  Since we
	 * may be interrupted between the increment of t_nomigrate and
	 * the store to t_weakbound_cpu below we cannot assume that
	 * t_weakbound_cpu will be set if t_nomigrate is.  Note that we
	 * cannot assert t_weakbound_cpu == t_bind_cpu since that is not
	 * always the case.
	 */
	if (t->t_nomigrate && t->t_weakbound_cpu && t->t_weakbound_cpu != cp) {
		if (!panicstr)
			panic("thread_nomigrate: binding to %p but already "
			    "bound to %p", (void *)cp,
			    (void *)t->t_weakbound_cpu);
	}

	/*
	 * At this point we have preemption disabled and we don't yet hold
	 * the thread lock.  So it's possible that somebody else could
	 * set t_bind_cpu here and not be able to force us across to the
	 * new cpu (since we have preemption disabled).
	 */
	thread_lock(curthread);

	/*
	 * If further weak bindings are being (temporarily) suppressed then
	 * we'll settle for disabling kernel preemption (which assures
	 * no migration provided the thread does not block which it is
	 * not allowed to if using thread_nomigrate).  We must remember
	 * this disposition so we can take appropriate action in
	 * thread_allowmigrate.  If this is a nested call and the
	 * thread is already weakbound then fall through as normal.
	 * We remember the decision to settle for kpreempt_disable through
	 * negative nesting counting in t_nomigrate.  Once a thread has had one
	 * weakbinding request satisfied in this way any further (nested)
	 * requests will continue to be satisfied in the same way,
	 * even if weak bindings have recommenced.
	 */
	if (t->t_nomigrate < 0 || weakbindingbarrier && t->t_nomigrate == 0) {
		--t->t_nomigrate;
		thread_unlock(curthread);
		return;		/* with kpreempt_disable still active */
	}

	/*
	 * We hold thread_lock so t_bind_cpu cannot change.  We could,
	 * however, be running on a different cpu to which we are t_bound_cpu
	 * to (as explained above).  If we grant the weak binding request
	 * in that case then the dispatcher must favour our weak binding
	 * over our strong (in which case, just as when preemption is
	 * disabled, we can continue to run on a cpu other than the one to
	 * which we are strongbound; the difference in this case is that
	 * this thread can be preempted and so can appear on the dispatch
	 * queues of a cpu other than the one it is strongbound to).
	 *
	 * If the cpu we are running on does not appear to be a current
	 * offline target (we check cpu_inmotion to determine this - since
	 * we don't hold cpu_lock we may not see a recent store to that,
	 * so it's possible that we at times can grant a weak binding to a
	 * cpu that is an offline target, but that one request will not
	 * prevent the offline from succeeding) then we will always grant
	 * the weak binding request.  This includes the case above where
	 * we grant a weakbinding not commensurate with our strong binding.
	 *
	 * If our cpu does appear to be an offline target then we're inclined
	 * not to grant the weakbinding request just yet - we'd prefer to
	 * migrate to another cpu and grant the request there.  The
	 * exceptions are those cases where going through preemption code
	 * will not result in us changing cpu:
	 *
	 *	. interrupts have already bypassed this case (see above)
	 *	. we are already weakbound to this cpu (dispatcher code will
	 *	  always return us to the weakbound cpu)
	 *	. preemption was disabled even before we disabled it above
	 *	. we are strongbound to this cpu (if we're strongbound to
	 *	another and not yet running there the trip through the
	 *	dispatcher will move us to the strongbound cpu and we
	 *	will grant the weak binding there)
	 */
	if (cp != cpu_inmotion || t->t_nomigrate > 0 || t->t_preempt > 1 ||
	    t->t_bound_cpu == cp) {
		/*
		 * Don't be tempted to store to t_weakbound_cpu only on
		 * the first nested bind request - if we're interrupted
		 * after the increment of t_nomigrate and before the
		 * store to t_weakbound_cpu and the interrupt calls
		 * thread_nomigrate then the assertion in thread_allowmigrate
		 * would fail.
		 */
		t->t_nomigrate++;
		t->t_weakbound_cpu = cp;
		membar_producer();
		thread_unlock(curthread);
		/*
		 * Now that we have dropped the thread_lock another thread
		 * can set our t_weakbound_cpu, and will try to migrate us
		 * to the strongbound cpu (which will not be prevented by
		 * preemption being disabled since we're about to enable
		 * preemption).  We have granted the weakbinding to the current
		 * cpu, so again we are in the position that is is is possible
		 * that our weak and strong bindings differ.  Again this
		 * is catered for by dispatcher code which will favour our
		 * weak binding.
		 */
		kpreempt_enable();
	} else {
		/*
		 * Move to another cpu before granting the request by
		 * forcing this thread through preemption code.  When we
		 * get to set{front,back}dq called from CL_PREEMPT()
		 * cpu_choose() will be used to select a cpu to queue
		 * us on - that will see cpu_inmotion and take
		 * steps to avoid returning us to this cpu.
		 */
		cp->cpu_kprunrun = 1;
		thread_unlock(curthread);
		kpreempt_enable();	/* will call preempt() */
		goto again;
	}
}

void
thread_allowmigrate(void)
{
	kthread_id_t t = curthread;

	ASSERT(t->t_weakbound_cpu == CPU ||
	    (t->t_nomigrate < 0 && t->t_preempt > 0) ||
	    CPU_ON_INTR(CPU) || t->t_flag & T_INTR_THREAD ||
	    getpil() >= DISP_LEVEL);

	if (CPU_ON_INTR(CPU) || (t->t_flag & T_INTR_THREAD) ||
	    getpil() >= DISP_LEVEL)
		return;

	if (t->t_nomigrate < 0) {
		/*
		 * This thread was granted "weak binding" in the
		 * stronger form of kernel preemption disabling.
		 * Undo a level of nesting for both t_nomigrate
		 * and t_preempt.
		 */
		++t->t_nomigrate;
		kpreempt_enable();
	} else if (--t->t_nomigrate == 0) {
		/*
		 * Time to drop the weak binding.  We need to cater
		 * for the case where we're weakbound to a different
		 * cpu than that to which we're strongbound (a very
		 * temporary arrangement that must only persist until
		 * weak binding drops).  We don't acquire thread_lock
		 * here so even as this code executes t_bound_cpu
		 * may be changing.  So we disable preemption and
		 * a) in the case that t_bound_cpu changes while we
		 * have preemption disabled kprunrun will be set
		 * asynchronously, and b) if before disabling
		 * preemption we were already on a different cpu to
		 * our t_bound_cpu then we set kprunrun ourselves
		 * to force a trip through the dispatcher when
		 * preemption is enabled.
		 */
		kpreempt_disable();
		if (t->t_bound_cpu &&
		    t->t_weakbound_cpu != t->t_bound_cpu)
			CPU->cpu_kprunrun = 1;
		t->t_weakbound_cpu = NULL;
		membar_producer();
		kpreempt_enable();
	}
}

/*
 * weakbinding_stop can be used to temporarily cause weakbindings made
 * with thread_nomigrate to be satisfied through the stronger action of
 * kpreempt_disable.  weakbinding_start recommences normal weakbinding.
 */

void
weakbinding_stop(void)
{
	ASSERT(MUTEX_HELD(&cpu_lock));
	weakbindingbarrier = 1;
	membar_producer();	/* make visible before subsequent thread_lock */
}

void
weakbinding_start(void)
{
	ASSERT(MUTEX_HELD(&cpu_lock));
	weakbindingbarrier = 0;
}

void
null_xcall(void)
{
}

/*
 * This routine is called to place the CPUs in a safe place so that
 * one of them can be taken off line or placed on line.  What we are
 * trying to do here is prevent a thread from traversing the list
 * of active CPUs while we are changing it or from getting placed on
 * the run queue of a CPU that has just gone off line.  We do this by
 * creating a thread with the highest possible prio for each CPU and
 * having it call this routine.  The advantage of this method is that
 * we can eliminate all checks for CPU_ACTIVE in the disp routines.
 * This makes disp faster at the expense of making p_online() slower
 * which is a good trade off.
 */
static void
cpu_pause(int index)
{
	int s;
	struct _cpu_pause_info *cpi = &cpu_pause_info;
	volatile char *safe = &safe_list[index];
	long    lindex = index;

	ASSERT((curthread->t_bound_cpu != NULL) || (*safe == PAUSE_DIE));

	while (*safe != PAUSE_DIE) {
		*safe = PAUSE_READY;
		membar_enter();		/* make sure stores are flushed */
		sema_v(&cpi->cp_sem);	/* signal requesting thread */

		/*
		 * Wait here until all pause threads are running.  That
		 * indicates that it's safe to do the spl.  Until
		 * cpu_pause_info.cp_go is set, we don't want to spl
		 * because that might block clock interrupts needed
		 * to preempt threads on other CPUs.
		 */
		while (cpi->cp_go == 0)
			;
		/*
		 * Even though we are at the highest disp prio, we need
		 * to block out all interrupts below LOCK_LEVEL so that
		 * an intr doesn't come in, wake up a thread, and call
		 * setbackdq/setfrontdq.
		 */
		s = splhigh();
		/*
		 * if cp_func has been set then call it using index as the
		 * argument, currently only used by cpr_suspend_cpus().
		 * This function is used as the code to execute on the
		 * "paused" cpu's when a machine comes out of a sleep state
		 * and CPU's were powered off.  (could also be used for
		 * hotplugging CPU's).
		 */
		if (cpi->cp_func != NULL)
			(*cpi->cp_func)((void *)lindex);

		mach_cpu_pause(safe);

		splx(s);
		/*
		 * Waiting is at an end. Switch out of cpu_pause
		 * loop and resume useful work.
		 */
		swtch();
	}

	mutex_enter(&pause_free_mutex);
	*safe = PAUSE_DEAD;
	cv_broadcast(&pause_free_cv);
	mutex_exit(&pause_free_mutex);
}

/*
 * Allow the cpus to start running again.
 */
void
start_cpus()
{
	int i;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(cpu_pause_info.cp_paused);
	cpu_pause_info.cp_paused = NULL;
	for (i = 0; i < NCPU; i++)
		safe_list[i] = PAUSE_IDLE;
	membar_enter();			/* make sure stores are flushed */
	affinity_clear();
	splx(cpu_pause_info.cp_spl);
	kpreempt_enable();
}

/*
 * Allocate a pause thread for a CPU.
 */
static void
cpu_pause_alloc(cpu_t *cp)
{
	kthread_id_t	t;
	long		cpun = cp->cpu_id;

	/*
	 * Note, v.v_nglobpris will not change value as long as I hold
	 * cpu_lock.
	 */
	t = thread_create(NULL, 0, cpu_pause, (void *)cpun,
	    0, &p0, TS_STOPPED, v.v_nglobpris - 1);
	thread_lock(t);
	t->t_bound_cpu = cp;
	t->t_disp_queue = cp->cpu_disp;
	t->t_affinitycnt = 1;
	t->t_preempt = 1;
	thread_unlock(t);
	cp->cpu_pause_thread = t;
	/*
	 * Registering a thread in the callback table is usually done
	 * in the initialization code of the thread.  In this
	 * case, we do it right after thread creation because the
	 * thread itself may never run, and we need to register the
	 * fact that it is safe for cpr suspend.
	 */
	CALLB_CPR_INIT_SAFE(t, "cpu_pause");
}

/*
 * Free a pause thread for a CPU.
 */
static void
cpu_pause_free(cpu_t *cp)
{
	kthread_id_t	t;
	int		cpun = cp->cpu_id;

	ASSERT(MUTEX_HELD(&cpu_lock));
	/*
	 * We have to get the thread and tell it to die.
	 */
	if ((t = cp->cpu_pause_thread) == NULL) {
		ASSERT(safe_list[cpun] == PAUSE_IDLE);
		return;
	}
	thread_lock(t);
	t->t_cpu = CPU;		/* disp gets upset if last cpu is quiesced. */
	t->t_bound_cpu = NULL;	/* Must un-bind; cpu may not be running. */
	t->t_pri = v.v_nglobpris - 1;
	ASSERT(safe_list[cpun] == PAUSE_IDLE);
	safe_list[cpun] = PAUSE_DIE;
	THREAD_TRANSITION(t);
	setbackdq(t);
	thread_unlock_nopreempt(t);

	/*
	 * If we don't wait for the thread to actually die, it may try to
	 * run on the wrong cpu as part of an actual call to pause_cpus().
	 */
	mutex_enter(&pause_free_mutex);
	while (safe_list[cpun] != PAUSE_DEAD) {
		cv_wait(&pause_free_cv, &pause_free_mutex);
	}
	mutex_exit(&pause_free_mutex);
	safe_list[cpun] = PAUSE_IDLE;

	cp->cpu_pause_thread = NULL;
}

/*
 * Initialize basic structures for pausing CPUs.
 */
void
cpu_pause_init()
{
	sema_init(&cpu_pause_info.cp_sem, 0, NULL, SEMA_DEFAULT, NULL);
	/*
	 * Create initial CPU pause thread.
	 */
	cpu_pause_alloc(CPU);
}

/*
 * Start the threads used to pause another CPU.
 */
static int
cpu_pause_start(processorid_t cpu_id)
{
	int	i;
	int	cpu_count = 0;

	for (i = 0; i < NCPU; i++) {
		cpu_t		*cp;
		kthread_id_t	t;

		cp = cpu[i];
		if (!CPU_IN_SET(cpu_available, i) || (i == cpu_id)) {
			safe_list[i] = PAUSE_WAIT;
			continue;
		}

		/*
		 * Skip CPU if it is quiesced or not yet started.
		 */
		if ((cp->cpu_flags & (CPU_QUIESCED | CPU_READY)) != CPU_READY) {
			safe_list[i] = PAUSE_WAIT;
			continue;
		}

		/*
		 * Start this CPU's pause thread.
		 */
		t = cp->cpu_pause_thread;
		thread_lock(t);
		/*
		 * Reset the priority, since nglobpris may have
		 * changed since the thread was created, if someone
		 * has loaded the RT (or some other) scheduling
		 * class.
		 */
		t->t_pri = v.v_nglobpris - 1;
		THREAD_TRANSITION(t);
		setbackdq(t);
		thread_unlock_nopreempt(t);
		++cpu_count;
	}
	return (cpu_count);
}


/*
 * Pause all of the CPUs except the one we are on by creating a high
 * priority thread bound to those CPUs.
 *
 * Note that one must be extremely careful regarding code
 * executed while CPUs are paused.  Since a CPU may be paused
 * while a thread scheduling on that CPU is holding an adaptive
 * lock, code executed with CPUs paused must not acquire adaptive
 * (or low-level spin) locks.  Also, such code must not block,
 * since the thread that is supposed to initiate the wakeup may
 * never run.
 *
 * With a few exceptions, the restrictions on code executed with CPUs
 * paused match those for code executed at high-level interrupt
 * context.
 */
void
pause_cpus(cpu_t *off_cp, void *(*func)(void *))
{
	processorid_t	cpu_id;
	int		i;
	struct _cpu_pause_info	*cpi = &cpu_pause_info;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(cpi->cp_paused == NULL);
	cpi->cp_count = 0;
	cpi->cp_go = 0;
	for (i = 0; i < NCPU; i++)
		safe_list[i] = PAUSE_IDLE;
	kpreempt_disable();

	cpi->cp_func = func;

	/*
	 * If running on the cpu that is going offline, get off it.
	 * This is so that it won't be necessary to rechoose a CPU
	 * when done.
	 */
	if (CPU == off_cp)
		cpu_id = off_cp->cpu_next_part->cpu_id;
	else
		cpu_id = CPU->cpu_id;
	affinity_set(cpu_id);

	/*
	 * Start the pause threads and record how many were started
	 */
	cpi->cp_count = cpu_pause_start(cpu_id);

	/*
	 * Now wait for all CPUs to be running the pause thread.
	 */
	while (cpi->cp_count > 0) {
		/*
		 * Spin reading the count without grabbing the disp
		 * lock to make sure we don't prevent the pause
		 * threads from getting the lock.
		 */
		while (sema_held(&cpi->cp_sem))
			;
		if (sema_tryp(&cpi->cp_sem))
			--cpi->cp_count;
	}
	cpi->cp_go = 1;			/* all have reached cpu_pause */

	/*
	 * Now wait for all CPUs to spl. (Transition from PAUSE_READY
	 * to PAUSE_WAIT.)
	 */
	for (i = 0; i < NCPU; i++) {
		while (safe_list[i] != PAUSE_WAIT)
			;
	}
	cpi->cp_spl = splhigh();	/* block dispatcher on this CPU */
	cpi->cp_paused = curthread;
}

/*
 * Check whether the current thread has CPUs paused
 */
int
cpus_paused(void)
{
	if (cpu_pause_info.cp_paused != NULL) {
		ASSERT(cpu_pause_info.cp_paused == curthread);
		return (1);
	}
	return (0);
}

static cpu_t *
cpu_get_all(processorid_t cpun)
{
	ASSERT(MUTEX_HELD(&cpu_lock));

	if (cpun >= NCPU || cpun < 0 || !CPU_IN_SET(cpu_available, cpun))
		return (NULL);
	return (cpu[cpun]);
}

/*
 * Check whether cpun is a valid processor id and whether it should be
 * visible from the current zone. If it is, return a pointer to the
 * associated CPU structure.
 */
cpu_t *
cpu_get(processorid_t cpun)
{
	cpu_t *c;

	ASSERT(MUTEX_HELD(&cpu_lock));
	c = cpu_get_all(cpun);
	if (c != NULL && !INGLOBALZONE(curproc) && pool_pset_enabled() &&
	    zone_pset_get(curproc->p_zone) != cpupart_query_cpu(c))
		return (NULL);
	return (c);
}

/*
 * The following functions should be used to check CPU states in the kernel.
 * They should be invoked with cpu_lock held.  Kernel subsystems interested
 * in CPU states should *not* use cpu_get_state() and various P_ONLINE/etc
 * states.  Those are for user-land (and system call) use only.
 */

/*
 * Determine whether the CPU is online and handling interrupts.
 */
int
cpu_is_online(cpu_t *cpu)
{
	ASSERT(MUTEX_HELD(&cpu_lock));
	return (cpu_flagged_online(cpu->cpu_flags));
}

/*
 * Determine whether the CPU is offline (this includes spare and faulted).
 */
int
cpu_is_offline(cpu_t *cpu)
{
	ASSERT(MUTEX_HELD(&cpu_lock));
	return (cpu_flagged_offline(cpu->cpu_flags));
}

/*
 * Determine whether the CPU is powered off.
 */
int
cpu_is_poweredoff(cpu_t *cpu)
{
	ASSERT(MUTEX_HELD(&cpu_lock));
	return (cpu_flagged_poweredoff(cpu->cpu_flags));
}

/*
 * Determine whether the CPU is handling interrupts.
 */
int
cpu_is_nointr(cpu_t *cpu)
{
	ASSERT(MUTEX_HELD(&cpu_lock));
	return (cpu_flagged_nointr(cpu->cpu_flags));
}

/*
 * Determine whether the CPU is active (scheduling threads).
 */
int
cpu_is_active(cpu_t *cpu)
{
	ASSERT(MUTEX_HELD(&cpu_lock));
	return (cpu_flagged_active(cpu->cpu_flags));
}

/*
 * Same as above, but these require cpu_flags instead of cpu_t pointers.
 */
int
cpu_flagged_online(cpu_flag_t cpu_flags)
{
	return (cpu_flagged_active(cpu_flags) &&
	    (cpu_flags & CPU_ENABLE));
}

int
cpu_flagged_offline(cpu_flag_t cpu_flags)
{
	return (((cpu_flags & CPU_POWEROFF) == 0) &&
	    ((cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY));
}

int
cpu_flagged_poweredoff(cpu_flag_t cpu_flags)
{
	return ((cpu_flags & CPU_POWEROFF) == CPU_POWEROFF);
}

int
cpu_flagged_nointr(cpu_flag_t cpu_flags)
{
	return (cpu_flagged_active(cpu_flags) &&
	    (cpu_flags & CPU_ENABLE) == 0);
}

int
cpu_flagged_active(cpu_flag_t cpu_flags)
{
	return (((cpu_flags & (CPU_POWEROFF | CPU_FAULTED | CPU_SPARE)) == 0) &&
	    ((cpu_flags & (CPU_READY | CPU_OFFLINE)) == CPU_READY));
}

/*
 * Bring the indicated CPU online.
 */
int
cpu_online(cpu_t *cp)
{
	int	error = 0;

	/*
	 * Handle on-line request.
	 *	This code must put the new CPU on the active list before
	 *	starting it because it will not be paused, and will start
	 * 	using the active list immediately.  The real start occurs
	 *	when the CPU_QUIESCED flag is turned off.
	 */

	ASSERT(MUTEX_HELD(&cpu_lock));

	/*
	 * Put all the cpus into a known safe place.
	 * No mutexes can be entered while CPUs are paused.
	 */
	error = mp_cpu_start(cp);	/* arch-dep hook */
	if (error == 0) {
		pg_cpupart_in(cp, cp->cpu_part);
		pause_cpus(NULL, NULL);
		cpu_add_active_internal(cp);
		if (cp->cpu_flags & CPU_FAULTED) {
			cp->cpu_flags &= ~CPU_FAULTED;
			mp_cpu_faulted_exit(cp);
		}
		cp->cpu_flags &= ~(CPU_QUIESCED | CPU_OFFLINE | CPU_FROZEN |
		    CPU_SPARE);
		CPU_NEW_GENERATION(cp);
		start_cpus();
		cpu_stats_kstat_create(cp);
		cpu_create_intrstat(cp);
		lgrp_kstat_create(cp);
		cpu_state_change_notify(cp->cpu_id, CPU_ON);
		cpu_intr_enable(cp);	/* arch-dep hook */
		cpu_state_change_notify(cp->cpu_id, CPU_INTR_ON);
		cpu_set_state(cp);
		cyclic_online(cp);
		/*
		 * This has to be called only after cyclic_online(). This
		 * function uses cyclics.
		 */
		callout_cpu_online(cp);
		poke_cpu(cp->cpu_id);
	}

	return (error);
}

/*
 * Take the indicated CPU offline.
 */
int
cpu_offline(cpu_t *cp, int flags)
{
	cpupart_t *pp;
	int	error = 0;
	cpu_t	*ncp;
	int	intr_enable;
	int	cyclic_off = 0;
	int	callout_off = 0;
	int	loop_count;
	int	no_quiesce = 0;
	int	(*bound_func)(struct cpu *, int);
	kthread_t *t;
	lpl_t	*cpu_lpl;
	proc_t	*p;
	int	lgrp_diff_lpl;
	boolean_t unbind_all_threads = (flags & CPU_FORCED) != 0;

	ASSERT(MUTEX_HELD(&cpu_lock));

	/*
	 * If we're going from faulted or spare to offline, just
	 * clear these flags and update CPU state.
	 */
	if (cp->cpu_flags & (CPU_FAULTED | CPU_SPARE)) {
		if (cp->cpu_flags & CPU_FAULTED) {
			cp->cpu_flags &= ~CPU_FAULTED;
			mp_cpu_faulted_exit(cp);
		}
		cp->cpu_flags &= ~CPU_SPARE;
		cpu_set_state(cp);
		return (0);
	}

	/*
	 * Handle off-line request.
	 */
	pp = cp->cpu_part;
	/*
	 * Don't offline last online CPU in partition
	 */
	if (ncpus_online <= 1 || pp->cp_ncpus <= 1 || cpu_intr_count(cp) < 2)
		return (EBUSY);
	/*
	 * Unbind all soft-bound threads bound to our CPU and hard bound threads
	 * if we were asked to.
	 */
	error = cpu_unbind(cp->cpu_id, unbind_all_threads);
	if (error != 0)
		return (error);
	/*
	 * We shouldn't be bound to this CPU ourselves.
	 */
	if (curthread->t_bound_cpu == cp)
		return (EBUSY);

	/*
	 * Tell interested parties that this CPU is going offline.
	 */
	CPU_NEW_GENERATION(cp);
	cpu_state_change_notify(cp->cpu_id, CPU_OFF);

	/*
	 * Tell the PG subsystem that the CPU is leaving the partition
	 */
	pg_cpupart_out(cp, pp);

	/*
	 * Take the CPU out of interrupt participation so we won't find
	 * bound kernel threads.  If the architecture cannot completely
	 * shut off interrupts on the CPU, don't quiesce it, but don't
	 * run anything but interrupt thread... this is indicated by
	 * the CPU_OFFLINE flag being on but the CPU_QUIESCE flag being
	 * off.
	 */
	intr_enable = cp->cpu_flags & CPU_ENABLE;
	if (intr_enable)
		no_quiesce = cpu_intr_disable(cp);

	/*
	 * Record that we are aiming to offline this cpu.  This acts as
	 * a barrier to further weak binding requests in thread_nomigrate
	 * and also causes cpu_choose, disp_lowpri_cpu and setfrontdq to
	 * lean away from this cpu.  Further strong bindings are already
	 * avoided since we hold cpu_lock.  Since threads that are set
	 * runnable around now and others coming off the target cpu are
	 * directed away from the target, existing strong and weak bindings
	 * (especially the latter) to the target cpu stand maximum chance of
	 * being able to unbind during the short delay loop below (if other
	 * unbound threads compete they may not see cpu in time to unbind
	 * even if they would do so immediately.
	 */
	cpu_inmotion = cp;
	membar_enter();

	/*
	 * Check for kernel threads (strong or weak) bound to that CPU.
	 * Strongly bound threads may not unbind, and we'll have to return
	 * EBUSY.  Weakly bound threads should always disappear - we've
	 * stopped more weak binding with cpu_inmotion and existing
	 * bindings will drain imminently (they may not block).  Nonetheless
	 * we will wait for a fixed period for all bound threads to disappear.
	 * Inactive interrupt threads are OK (they'll be in TS_FREE
	 * state).  If test finds some bound threads, wait a few ticks
	 * to give short-lived threads (such as interrupts) chance to
	 * complete.  Note that if no_quiesce is set, i.e. this cpu
	 * is required to service interrupts, then we take the route
	 * that permits interrupt threads to be active (or bypassed).
	 */
	bound_func = no_quiesce ? disp_bound_threads : disp_bound_anythreads;

again:	for (loop_count = 0; (*bound_func)(cp, 0); loop_count++) {
		if (loop_count >= 5) {
			error = EBUSY;	/* some threads still bound */
			break;
		}

		/*
		 * If some threads were assigned, give them
		 * a chance to complete or move.
		 *
		 * This assumes that the clock_thread is not bound
		 * to any CPU, because the clock_thread is needed to
		 * do the delay(hz/100).
		 *
		 * Note: we still hold the cpu_lock while waiting for
		 * the next clock tick.  This is OK since it isn't
		 * needed for anything else except processor_bind(2),
		 * and system initialization.  If we drop the lock,
		 * we would risk another p_online disabling the last
		 * processor.
		 */
		delay(hz/100);
	}

	if (error == 0 && callout_off == 0) {
		callout_cpu_offline(cp);
		callout_off = 1;
	}

	if (error == 0 && cyclic_off == 0) {
		if (!cyclic_offline(cp)) {
			/*
			 * We must have bound cyclics...
			 */
			error = EBUSY;
			goto out;
		}
		cyclic_off = 1;
	}

	/*
	 * Call mp_cpu_stop() to perform any special operations
	 * needed for this machine architecture to offline a CPU.
	 */
	if (error == 0)
		error = mp_cpu_stop(cp);	/* arch-dep hook */

	/*
	 * If that all worked, take the CPU offline and decrement
	 * ncpus_online.
	 */
	if (error == 0) {
		/*
		 * Put all the cpus into a known safe place.
		 * No mutexes can be entered while CPUs are paused.
		 */
		pause_cpus(cp, NULL);
		/*
		 * Repeat the operation, if necessary, to make sure that
		 * all outstanding low-level interrupts run to completion
		 * before we set the CPU_QUIESCED flag.  It's also possible
		 * that a thread has weak bound to the cpu despite our raising
		 * cpu_inmotion above since it may have loaded that
		 * value before the barrier became visible (this would have
		 * to be the thread that was on the target cpu at the time
		 * we raised the barrier).
		 */
		if ((!no_quiesce && cp->cpu_intr_actv != 0) ||
		    (*bound_func)(cp, 1)) {
			start_cpus();
			(void) mp_cpu_start(cp);
			goto again;
		}
		ncp = cp->cpu_next_part;
		cpu_lpl = cp->cpu_lpl;
		ASSERT(cpu_lpl != NULL);

		/*
		 * Remove the CPU from the list of active CPUs.
		 */
		cpu_remove_active(cp);

		/*
		 * Walk the active process list and look for threads
		 * whose home lgroup needs to be updated, or
		 * the last CPU they run on is the one being offlined now.
		 */

		ASSERT(curthread->t_cpu != cp);
		for (p = practive; p != NULL; p = p->p_next) {

			t = p->p_tlist;

			if (t == NULL)
				continue;

			lgrp_diff_lpl = 0;

			do {
				ASSERT(t->t_lpl != NULL);
				/*
				 * Taking last CPU in lpl offline
				 * Rehome thread if it is in this lpl
				 * Otherwise, update the count of how many
				 * threads are in this CPU's lgroup but have
				 * a different lpl.
				 */

				if (cpu_lpl->lpl_ncpu == 0) {
					if (t->t_lpl == cpu_lpl)
						lgrp_move_thread(t,
						    lgrp_choose(t,
						    t->t_cpupart), 0);
					else if (t->t_lpl->lpl_lgrpid ==
					    cpu_lpl->lpl_lgrpid)
						lgrp_diff_lpl++;
				}
				ASSERT(t->t_lpl->lpl_ncpu > 0);

				/*
				 * Update CPU last ran on if it was this CPU
				 */
				if (t->t_cpu == cp && t->t_bound_cpu != cp)
					t->t_cpu = disp_lowpri_cpu(ncp,
					    t->t_lpl, t->t_pri, NULL);
				ASSERT(t->t_cpu != cp || t->t_bound_cpu == cp ||
				    t->t_weakbound_cpu == cp);

				t = t->t_forw;
			} while (t != p->p_tlist);

			/*
			 * Didn't find any threads in the same lgroup as this
			 * CPU with a different lpl, so remove the lgroup from
			 * the process lgroup bitmask.
			 */

			if (lgrp_diff_lpl == 0)
				klgrpset_del(p->p_lgrpset, cpu_lpl->lpl_lgrpid);
		}

		/*
		 * Walk thread list looking for threads that need to be
		 * rehomed, since there are some threads that are not in
		 * their process's p_tlist.
		 */

		t = curthread;
		do {
			ASSERT(t != NULL && t->t_lpl != NULL);

			/*
			 * Rehome threads with same lpl as this CPU when this
			 * is the last CPU in the lpl.
			 */

			if ((cpu_lpl->lpl_ncpu == 0) && (t->t_lpl == cpu_lpl))
				lgrp_move_thread(t,
				    lgrp_choose(t, t->t_cpupart), 1);

			ASSERT(t->t_lpl->lpl_ncpu > 0);

			/*
			 * Update CPU last ran on if it was this CPU
			 */

			if (t->t_cpu == cp && t->t_bound_cpu != cp) {
				t->t_cpu = disp_lowpri_cpu(ncp,
				    t->t_lpl, t->t_pri, NULL);
			}
			ASSERT(t->t_cpu != cp || t->t_bound_cpu == cp ||
			    t->t_weakbound_cpu == cp);
			t = t->t_next;

		} while (t != curthread);
		ASSERT((cp->cpu_flags & (CPU_FAULTED | CPU_SPARE)) == 0);
		cp->cpu_flags |= CPU_OFFLINE;
		disp_cpu_inactive(cp);
		if (!no_quiesce)
			cp->cpu_flags |= CPU_QUIESCED;
		ncpus_online--;
		cpu_set_state(cp);
		cpu_inmotion = NULL;
		start_cpus();
		cpu_stats_kstat_destroy(cp);
		cpu_delete_intrstat(cp);
		lgrp_kstat_destroy(cp);
	}

out:
	cpu_inmotion = NULL;

	/*
	 * If we failed, re-enable interrupts.
	 * Do this even if cpu_intr_disable returned an error, because
	 * it may have partially disabled interrupts.
	 */
	if (error && intr_enable)
		cpu_intr_enable(cp);

	/*
	 * If we failed, but managed to offline the cyclic subsystem on this
	 * CPU, bring it back online.
	 */
	if (error && cyclic_off)
		cyclic_online(cp);

	/*
	 * If we failed, but managed to offline callouts on this CPU,
	 * bring it back online.
	 */
	if (error && callout_off)
		callout_cpu_online(cp);

	/*
	 * If we failed, tell the PG subsystem that the CPU is back
	 */
	pg_cpupart_in(cp, pp);

	/*
	 * If we failed, we need to notify everyone that this CPU is back on.
	 */
	if (error != 0) {
		CPU_NEW_GENERATION(cp);
		cpu_state_change_notify(cp->cpu_id, CPU_ON);
		cpu_state_change_notify(cp->cpu_id, CPU_INTR_ON);
	}

	return (error);
}

/*
 * Mark the indicated CPU as faulted, taking it offline.
 */
int
cpu_faulted(cpu_t *cp, int flags)
{
	int	error = 0;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(!cpu_is_poweredoff(cp));

	if (cpu_is_offline(cp)) {
		cp->cpu_flags &= ~CPU_SPARE;
		cp->cpu_flags |= CPU_FAULTED;
		mp_cpu_faulted_enter(cp);
		cpu_set_state(cp);
		return (0);
	}

	if ((error = cpu_offline(cp, flags)) == 0) {
		cp->cpu_flags |= CPU_FAULTED;
		mp_cpu_faulted_enter(cp);
		cpu_set_state(cp);
	}

	return (error);
}

/*
 * Mark the indicated CPU as a spare, taking it offline.
 */
int
cpu_spare(cpu_t *cp, int flags)
{
	int	error = 0;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(!cpu_is_poweredoff(cp));

	if (cpu_is_offline(cp)) {
		if (cp->cpu_flags & CPU_FAULTED) {
			cp->cpu_flags &= ~CPU_FAULTED;
			mp_cpu_faulted_exit(cp);
		}
		cp->cpu_flags |= CPU_SPARE;
		cpu_set_state(cp);
		return (0);
	}

	if ((error = cpu_offline(cp, flags)) == 0) {
		cp->cpu_flags |= CPU_SPARE;
		cpu_set_state(cp);
	}

	return (error);
}

/*
 * Take the indicated CPU from poweroff to offline.
 */
int
cpu_poweron(cpu_t *cp)
{
	int	error = ENOTSUP;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(cpu_is_poweredoff(cp));

	error = mp_cpu_poweron(cp);	/* arch-dep hook */
	if (error == 0)
		cpu_set_state(cp);

	return (error);
}

/*
 * Take the indicated CPU from any inactive state to powered off.
 */
int
cpu_poweroff(cpu_t *cp)
{
	int	error = ENOTSUP;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(cpu_is_offline(cp));

	if (!(cp->cpu_flags & CPU_QUIESCED))
		return (EBUSY);		/* not completely idle */

	error = mp_cpu_poweroff(cp);	/* arch-dep hook */
	if (error == 0)
		cpu_set_state(cp);

	return (error);
}

/*
 * Initialize the Sequential CPU id lookup table
 */
void
cpu_seq_tbl_init()
{
	cpu_t	**tbl;

	tbl = kmem_zalloc(sizeof (struct cpu *) * max_ncpus, KM_SLEEP);
	tbl[0] = CPU;

	cpu_seq = tbl;
}

/*
 * Initialize the CPU lists for the first CPU.
 */
void
cpu_list_init(cpu_t *cp)
{
	cp->cpu_next = cp;
	cp->cpu_prev = cp;
	cpu_list = cp;
	clock_cpu_list = cp;

	cp->cpu_next_onln = cp;
	cp->cpu_prev_onln = cp;
	cpu_active = cp;

	cp->cpu_seqid = 0;
	CPUSET_ADD(cpu_seqid_inuse, 0);

	/*
	 * Bootstrap cpu_seq using cpu_list
	 * The cpu_seq[] table will be dynamically allocated
	 * when kmem later becomes available (but before going MP)
	 */
	cpu_seq = &cpu_list;

	cp->cpu_cache_offset = KMEM_CPU_CACHE_OFFSET(cp->cpu_seqid);
	cp_default.cp_cpulist = cp;
	cp_default.cp_ncpus = 1;
	cp->cpu_next_part = cp;
	cp->cpu_prev_part = cp;
	cp->cpu_part = &cp_default;

	CPUSET_ADD(cpu_available, cp->cpu_id);
	CPUSET_ADD(cpu_active_set, cp->cpu_id);
}

/*
 * Insert a CPU into the list of available CPUs.
 */
void
cpu_add_unit(cpu_t *cp)
{
	int seqid;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(cpu_list != NULL);	/* list started in cpu_list_init */

	lgrp_config(LGRP_CONFIG_CPU_ADD, (uintptr_t)cp, 0);

	/*
	 * Note: most users of the cpu_list will grab the
	 * cpu_lock to insure that it isn't modified.  However,
	 * certain users can't or won't do that.  To allow this
	 * we pause the other cpus.  Users who walk the list
	 * without cpu_lock, must disable kernel preemption
	 * to insure that the list isn't modified underneath
	 * them.  Also, any cached pointers to cpu structures
	 * must be revalidated by checking to see if the
	 * cpu_next pointer points to itself.  This check must
	 * be done with the cpu_lock held or kernel preemption
	 * disabled.  This check relies upon the fact that
	 * old cpu structures are not free'ed or cleared after
	 * then are removed from the cpu_list.
	 *
	 * Note that the clock code walks the cpu list dereferencing
	 * the cpu_part pointer, so we need to initialize it before
	 * adding the cpu to the list.
	 */
	cp->cpu_part = &cp_default;
	pause_cpus(NULL, NULL);
	cp->cpu_next = cpu_list;
	cp->cpu_prev = cpu_list->cpu_prev;
	cpu_list->cpu_prev->cpu_next = cp;
	cpu_list->cpu_prev = cp;
	start_cpus();

	for (seqid = 0; CPU_IN_SET(cpu_seqid_inuse, seqid); seqid++)
		continue;
	CPUSET_ADD(cpu_seqid_inuse, seqid);
	cp->cpu_seqid = seqid;

	if (seqid > max_cpu_seqid_ever)
		max_cpu_seqid_ever = seqid;

	ASSERT(ncpus < max_ncpus);
	ncpus++;
	cp->cpu_cache_offset = KMEM_CPU_CACHE_OFFSET(cp->cpu_seqid);
	cpu[cp->cpu_id] = cp;
	CPUSET_ADD(cpu_available, cp->cpu_id);
	cpu_seq[cp->cpu_seqid] = cp;

	/*
	 * allocate a pause thread for this CPU.
	 */
	cpu_pause_alloc(cp);

	/*
	 * So that new CPUs won't have NULL prev_onln and next_onln pointers,
	 * link them into a list of just that CPU.
	 * This is so that disp_lowpri_cpu will work for thread_create in
	 * pause_cpus() when called from the startup thread in a new CPU.
	 */
	cp->cpu_next_onln = cp;
	cp->cpu_prev_onln = cp;
	cpu_info_kstat_create(cp);
	cp->cpu_next_part = cp;
	cp->cpu_prev_part = cp;

	init_cpu_mstate(cp, CMS_SYSTEM);

	pool_pset_mod = gethrtime();
}

/*
 * Do the opposite of cpu_add_unit().
 */
void
cpu_del_unit(int cpuid)
{
	struct cpu	*cp, *cpnext;

	ASSERT(MUTEX_HELD(&cpu_lock));
	cp = cpu[cpuid];
	ASSERT(cp != NULL);

	ASSERT(cp->cpu_next_onln == cp);
	ASSERT(cp->cpu_prev_onln == cp);
	ASSERT(cp->cpu_next_part == cp);
	ASSERT(cp->cpu_prev_part == cp);

	/*
	 * Tear down the CPU's physical ID cache, and update any
	 * processor groups
	 */
	pg_cpu_fini(cp, NULL);
	pghw_physid_destroy(cp);

	/*
	 * Destroy kstat stuff.
	 */
	cpu_info_kstat_destroy(cp);
	term_cpu_mstate(cp);
	/*
	 * Free up pause thread.
	 */
	cpu_pause_free(cp);
	CPUSET_DEL(cpu_available, cp->cpu_id);
	cpu[cp->cpu_id] = NULL;
	cpu_seq[cp->cpu_seqid] = NULL;

	/*
	 * The clock thread and mutex_vector_enter cannot hold the
	 * cpu_lock while traversing the cpu list, therefore we pause
	 * all other threads by pausing the other cpus. These, and any
	 * other routines holding cpu pointers while possibly sleeping
	 * must be sure to call kpreempt_disable before processing the
	 * list and be sure to check that the cpu has not been deleted
	 * after any sleeps (check cp->cpu_next != NULL). We guarantee
	 * to keep the deleted cpu structure around.
	 *
	 * Note that this MUST be done AFTER cpu_available
	 * has been updated so that we don't waste time
	 * trying to pause the cpu we're trying to delete.
	 */
	pause_cpus(NULL, NULL);

	cpnext = cp->cpu_next;
	cp->cpu_prev->cpu_next = cp->cpu_next;
	cp->cpu_next->cpu_prev = cp->cpu_prev;
	if (cp == cpu_list)
		cpu_list = cpnext;

	/*
	 * Signals that the cpu has been deleted (see above).
	 */
	cp->cpu_next = NULL;
	cp->cpu_prev = NULL;

	start_cpus();

	CPUSET_DEL(cpu_seqid_inuse, cp->cpu_seqid);
	ncpus--;
	lgrp_config(LGRP_CONFIG_CPU_DEL, (uintptr_t)cp, 0);

	pool_pset_mod = gethrtime();
}

/*
 * Add a CPU to the list of active CPUs.
 *	This routine must not get any locks, because other CPUs are paused.
 */
static void
cpu_add_active_internal(cpu_t *cp)
{
	cpupart_t	*pp = cp->cpu_part;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(cpu_list != NULL);	/* list started in cpu_list_init */

	ncpus_online++;
	cpu_set_state(cp);
	cp->cpu_next_onln = cpu_active;
	cp->cpu_prev_onln = cpu_active->cpu_prev_onln;
	cpu_active->cpu_prev_onln->cpu_next_onln = cp;
	cpu_active->cpu_prev_onln = cp;
	CPUSET_ADD(cpu_active_set, cp->cpu_id);

	if (pp->cp_cpulist) {
		cp->cpu_next_part = pp->cp_cpulist;
		cp->cpu_prev_part = pp->cp_cpulist->cpu_prev_part;
		pp->cp_cpulist->cpu_prev_part->cpu_next_part = cp;
		pp->cp_cpulist->cpu_prev_part = cp;
	} else {
		ASSERT(pp->cp_ncpus == 0);
		pp->cp_cpulist = cp->cpu_next_part = cp->cpu_prev_part = cp;
	}
	pp->cp_ncpus++;
	if (pp->cp_ncpus == 1) {
		cp_numparts_nonempty++;
		ASSERT(cp_numparts_nonempty != 0);
	}

	pg_cpu_active(cp);
	lgrp_config(LGRP_CONFIG_CPU_ONLINE, (uintptr_t)cp, 0);

	bzero(&cp->cpu_loadavg, sizeof (cp->cpu_loadavg));
}

/*
 * Add a CPU to the list of active CPUs.
 *	This is called from machine-dependent layers when a new CPU is started.
 */
void
cpu_add_active(cpu_t *cp)
{
	pg_cpupart_in(cp, cp->cpu_part);

	pause_cpus(NULL, NULL);
	cpu_add_active_internal(cp);
	start_cpus();

	cpu_stats_kstat_create(cp);
	cpu_create_intrstat(cp);
	lgrp_kstat_create(cp);
	cpu_state_change_notify(cp->cpu_id, CPU_INIT);
}


/*
 * Remove a CPU from the list of active CPUs.
 *	This routine must not get any locks, because other CPUs are paused.
 */
/* ARGSUSED */
static void
cpu_remove_active(cpu_t *cp)
{
	cpupart_t	*pp = cp->cpu_part;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(cp->cpu_next_onln != cp);	/* not the last one */
	ASSERT(cp->cpu_prev_onln != cp);	/* not the last one */

	pg_cpu_inactive(cp);

	lgrp_config(LGRP_CONFIG_CPU_OFFLINE, (uintptr_t)cp, 0);

	if (cp == clock_cpu_list)
		clock_cpu_list = cp->cpu_next_onln;

	cp->cpu_prev_onln->cpu_next_onln = cp->cpu_next_onln;
	cp->cpu_next_onln->cpu_prev_onln = cp->cpu_prev_onln;
	if (cpu_active == cp) {
		cpu_active = cp->cpu_next_onln;
	}
	cp->cpu_next_onln = cp;
	cp->cpu_prev_onln = cp;
	CPUSET_DEL(cpu_active_set, cp->cpu_id);

	cp->cpu_prev_part->cpu_next_part = cp->cpu_next_part;
	cp->cpu_next_part->cpu_prev_part = cp->cpu_prev_part;
	if (pp->cp_cpulist == cp) {
		pp->cp_cpulist = cp->cpu_next_part;
		ASSERT(pp->cp_cpulist != cp);
	}
	cp->cpu_next_part = cp;
	cp->cpu_prev_part = cp;
	pp->cp_ncpus--;
	if (pp->cp_ncpus == 0) {
		cp_numparts_nonempty--;
		ASSERT(cp_numparts_nonempty != 0);
	}
}

/*
 * Routine used to setup a newly inserted CPU in preparation for starting
 * it running code.
 */
int
cpu_configure(int cpuid)
{
	int retval = 0;

	ASSERT(MUTEX_HELD(&cpu_lock));

	/*
	 * Some structures are statically allocated based upon
	 * the maximum number of cpus the system supports.  Do not
	 * try to add anything beyond this limit.
	 */
	if (cpuid < 0 || cpuid >= NCPU) {
		return (EINVAL);
	}

	if ((cpu[cpuid] != NULL) && (cpu[cpuid]->cpu_flags != 0)) {
		return (EALREADY);
	}

	if ((retval = mp_cpu_configure(cpuid)) != 0) {
		return (retval);
	}

	cpu[cpuid]->cpu_flags = CPU_QUIESCED | CPU_OFFLINE | CPU_POWEROFF;
	cpu_set_state(cpu[cpuid]);
	retval = cpu_state_change_hooks(cpuid, CPU_CONFIG, CPU_UNCONFIG);
	if (retval != 0)
		(void) mp_cpu_unconfigure(cpuid);

	return (retval);
}

/*
 * Routine used to cleanup a CPU that has been powered off.  This will
 * destroy all per-cpu information related to this cpu.
 */
int
cpu_unconfigure(int cpuid)
{
	int error;

	ASSERT(MUTEX_HELD(&cpu_lock));

	if (cpu[cpuid] == NULL) {
		return (ENODEV);
	}

	if (cpu[cpuid]->cpu_flags == 0) {
		return (EALREADY);
	}

	if ((cpu[cpuid]->cpu_flags & CPU_POWEROFF) == 0) {
		return (EBUSY);
	}

	if (cpu[cpuid]->cpu_props != NULL) {
		(void) nvlist_free(cpu[cpuid]->cpu_props);
		cpu[cpuid]->cpu_props = NULL;
	}

	error = cpu_state_change_hooks(cpuid, CPU_UNCONFIG, CPU_CONFIG);

	if (error != 0)
		return (error);

	return (mp_cpu_unconfigure(cpuid));
}

/*
 * Routines for registering and de-registering cpu_setup callback functions.
 *
 * Caller's context
 *	These routines must not be called from a driver's attach(9E) or
 *	detach(9E) entry point.
 *
 * NOTE: CPU callbacks should not block. They are called with cpu_lock held.
 */

/*
 * Ideally, these would be dynamically allocated and put into a linked
 * list; however that is not feasible because the registration routine
 * has to be available before the kmem allocator is working (in fact,
 * it is called by the kmem allocator init code).  In any case, there
 * are quite a few extra entries for future users.
 */
#define	NCPU_SETUPS	20

struct cpu_setup {
	cpu_setup_func_t *func;
	void *arg;
} cpu_setups[NCPU_SETUPS];

void
register_cpu_setup_func(cpu_setup_func_t *func, void *arg)
{
	int i;

	ASSERT(MUTEX_HELD(&cpu_lock));

	for (i = 0; i < NCPU_SETUPS; i++)
		if (cpu_setups[i].func == NULL)
			break;
	if (i >= NCPU_SETUPS)
		cmn_err(CE_PANIC, "Ran out of cpu_setup callback entries");

	cpu_setups[i].func = func;
	cpu_setups[i].arg = arg;
}

void
unregister_cpu_setup_func(cpu_setup_func_t *func, void *arg)
{
	int i;

	ASSERT(MUTEX_HELD(&cpu_lock));

	for (i = 0; i < NCPU_SETUPS; i++)
		if ((cpu_setups[i].func == func) &&
		    (cpu_setups[i].arg == arg))
			break;
	if (i >= NCPU_SETUPS)
		cmn_err(CE_PANIC, "Could not find cpu_setup callback to "
		    "deregister");

	cpu_setups[i].func = NULL;
	cpu_setups[i].arg = 0;
}

/*
 * Call any state change hooks for this CPU, ignore any errors.
 */
void
cpu_state_change_notify(int id, cpu_setup_t what)
{
	int i;

	ASSERT(MUTEX_HELD(&cpu_lock));

	for (i = 0; i < NCPU_SETUPS; i++) {
		if (cpu_setups[i].func != NULL) {
			cpu_setups[i].func(what, id, cpu_setups[i].arg);
		}
	}
}

/*
 * Call any state change hooks for this CPU, undo it if error found.
 */
static int
cpu_state_change_hooks(int id, cpu_setup_t what, cpu_setup_t undo)
{
	int i;
	int retval = 0;

	ASSERT(MUTEX_HELD(&cpu_lock));

	for (i = 0; i < NCPU_SETUPS; i++) {
		if (cpu_setups[i].func != NULL) {
			retval = cpu_setups[i].func(what, id,
			    cpu_setups[i].arg);
			if (retval) {
				for (i--; i >= 0; i--) {
					if (cpu_setups[i].func != NULL)
						cpu_setups[i].func(undo,
						    id, cpu_setups[i].arg);
				}
				break;
			}
		}
	}
	return (retval);
}

/*
 * Export information about this CPU via the kstat mechanism.
 */
static struct {
	kstat_named_t ci_state;
	kstat_named_t ci_state_begin;
	kstat_named_t ci_cpu_type;
	kstat_named_t ci_fpu_type;
	kstat_named_t ci_clock_MHz;
	kstat_named_t ci_chip_id;
	kstat_named_t ci_implementation;
	kstat_named_t ci_brandstr;
	kstat_named_t ci_core_id;
	kstat_named_t ci_curr_clock_Hz;
	kstat_named_t ci_supp_freq_Hz;
	kstat_named_t ci_pg_id;
#if defined(__sparcv9)
	kstat_named_t ci_device_ID;
	kstat_named_t ci_cpu_fru;
#endif
#if defined(__x86)
	kstat_named_t ci_vendorstr;
	kstat_named_t ci_family;
	kstat_named_t ci_model;
	kstat_named_t ci_step;
	kstat_named_t ci_clogid;
	kstat_named_t ci_pkg_core_id;
	kstat_named_t ci_ncpuperchip;
	kstat_named_t ci_ncoreperchip;
	kstat_named_t ci_max_cstates;
	kstat_named_t ci_curr_cstate;
	kstat_named_t ci_cacheid;
	kstat_named_t ci_sktstr;
#endif
} cpu_info_template = {
	{ "state",			KSTAT_DATA_CHAR },
	{ "state_begin",		KSTAT_DATA_LONG },
	{ "cpu_type",			KSTAT_DATA_CHAR },
	{ "fpu_type",			KSTAT_DATA_CHAR },
	{ "clock_MHz",			KSTAT_DATA_LONG },
	{ "chip_id",			KSTAT_DATA_LONG },
	{ "implementation",		KSTAT_DATA_STRING },
	{ "brand",			KSTAT_DATA_STRING },
	{ "core_id",			KSTAT_DATA_LONG },
	{ "current_clock_Hz",		KSTAT_DATA_UINT64 },
	{ "supported_frequencies_Hz",	KSTAT_DATA_STRING },
	{ "pg_id",			KSTAT_DATA_LONG },
#if defined(__sparcv9)
	{ "device_ID",			KSTAT_DATA_UINT64 },
	{ "cpu_fru",			KSTAT_DATA_STRING },
#endif
#if defined(__x86)
	{ "vendor_id",			KSTAT_DATA_STRING },
	{ "family",			KSTAT_DATA_INT32 },
	{ "model",			KSTAT_DATA_INT32 },
	{ "stepping",			KSTAT_DATA_INT32 },
	{ "clog_id",			KSTAT_DATA_INT32 },
	{ "pkg_core_id",		KSTAT_DATA_LONG },
	{ "ncpu_per_chip",		KSTAT_DATA_INT32 },
	{ "ncore_per_chip",		KSTAT_DATA_INT32 },
	{ "supported_max_cstates",	KSTAT_DATA_INT32 },
	{ "current_cstate",		KSTAT_DATA_INT32 },
	{ "cache_id",			KSTAT_DATA_INT32 },
	{ "socket_type",		KSTAT_DATA_STRING },
#endif
};

static kmutex_t cpu_info_template_lock;

static int
cpu_info_kstat_update(kstat_t *ksp, int rw)
{
	cpu_t	*cp = ksp->ks_private;
	const char *pi_state;

	if (rw == KSTAT_WRITE)
		return (EACCES);

#if defined(__x86)
	/* Is the cpu still initialising itself? */
	if (cpuid_checkpass(cp, 1) == 0)
		return (ENXIO);
#endif
	switch (cp->cpu_type_info.pi_state) {
	case P_ONLINE:
		pi_state = PS_ONLINE;
		break;
	case P_POWEROFF:
		pi_state = PS_POWEROFF;
		break;
	case P_NOINTR:
		pi_state = PS_NOINTR;
		break;
	case P_FAULTED:
		pi_state = PS_FAULTED;
		break;
	case P_SPARE:
		pi_state = PS_SPARE;
		break;
	case P_OFFLINE:
		pi_state = PS_OFFLINE;
		break;
	default:
		pi_state = "unknown";
	}
	(void) strcpy(cpu_info_template.ci_state.value.c, pi_state);
	cpu_info_template.ci_state_begin.value.l = cp->cpu_state_begin;
	(void) strncpy(cpu_info_template.ci_cpu_type.value.c,
	    cp->cpu_type_info.pi_processor_type, 15);
	(void) strncpy(cpu_info_template.ci_fpu_type.value.c,
	    cp->cpu_type_info.pi_fputypes, 15);
	cpu_info_template.ci_clock_MHz.value.l = cp->cpu_type_info.pi_clock;
	cpu_info_template.ci_chip_id.value.l =
	    pg_plat_hw_instance_id(cp, PGHW_CHIP);
	kstat_named_setstr(&cpu_info_template.ci_implementation,
	    cp->cpu_idstr);
	kstat_named_setstr(&cpu_info_template.ci_brandstr, cp->cpu_brandstr);
	cpu_info_template.ci_core_id.value.l = pg_plat_get_core_id(cp);
	cpu_info_template.ci_curr_clock_Hz.value.ui64 =
	    cp->cpu_curr_clock;
	cpu_info_template.ci_pg_id.value.l =
	    cp->cpu_pg && cp->cpu_pg->cmt_lineage ?
	    cp->cpu_pg->cmt_lineage->pg_id : -1;
	kstat_named_setstr(&cpu_info_template.ci_supp_freq_Hz,
	    cp->cpu_supp_freqs);
#if defined(__sparcv9)
	cpu_info_template.ci_device_ID.value.ui64 =
	    cpunodes[cp->cpu_id].device_id;
	kstat_named_setstr(&cpu_info_template.ci_cpu_fru, cpu_fru_fmri(cp));
#endif
#if defined(__x86)
	kstat_named_setstr(&cpu_info_template.ci_vendorstr,
	    cpuid_getvendorstr(cp));
	cpu_info_template.ci_family.value.l = cpuid_getfamily(cp);
	cpu_info_template.ci_model.value.l = cpuid_getmodel(cp);
	cpu_info_template.ci_step.value.l = cpuid_getstep(cp);
	cpu_info_template.ci_clogid.value.l = cpuid_get_clogid(cp);
	cpu_info_template.ci_ncpuperchip.value.l = cpuid_get_ncpu_per_chip(cp);
	cpu_info_template.ci_ncoreperchip.value.l =
	    cpuid_get_ncore_per_chip(cp);
	cpu_info_template.ci_pkg_core_id.value.l = cpuid_get_pkgcoreid(cp);
	cpu_info_template.ci_max_cstates.value.l = cp->cpu_m.max_cstates;
	cpu_info_template.ci_curr_cstate.value.l = cpu_idle_get_cpu_state(cp);
	cpu_info_template.ci_cacheid.value.i32 = cpuid_get_cacheid(cp);
	kstat_named_setstr(&cpu_info_template.ci_sktstr,
	    cpuid_getsocketstr(cp));
#endif

	return (0);
}

static void
cpu_info_kstat_create(cpu_t *cp)
{
	zoneid_t zoneid;

	ASSERT(MUTEX_HELD(&cpu_lock));

	if (pool_pset_enabled())
		zoneid = GLOBAL_ZONEID;
	else
		zoneid = ALL_ZONES;
	if ((cp->cpu_info_kstat = kstat_create_zone("cpu_info", cp->cpu_id,
	    NULL, "misc", KSTAT_TYPE_NAMED,
	    sizeof (cpu_info_template) / sizeof (kstat_named_t),
	    KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_VAR_SIZE, zoneid)) != NULL) {
		cp->cpu_info_kstat->ks_data_size += 2 * CPU_IDSTRLEN;
#if defined(__sparcv9)
		cp->cpu_info_kstat->ks_data_size +=
		    strlen(cpu_fru_fmri(cp)) + 1;
#endif
#if defined(__x86)
		cp->cpu_info_kstat->ks_data_size += X86_VENDOR_STRLEN;
#endif
		if (cp->cpu_supp_freqs != NULL)
			cp->cpu_info_kstat->ks_data_size +=
			    strlen(cp->cpu_supp_freqs) + 1;
		cp->cpu_info_kstat->ks_lock = &cpu_info_template_lock;
		cp->cpu_info_kstat->ks_data = &cpu_info_template;
		cp->cpu_info_kstat->ks_private = cp;
		cp->cpu_info_kstat->ks_update = cpu_info_kstat_update;
		kstat_install(cp->cpu_info_kstat);
	}
}

static void
cpu_info_kstat_destroy(cpu_t *cp)
{
	ASSERT(MUTEX_HELD(&cpu_lock));

	kstat_delete(cp->cpu_info_kstat);
	cp->cpu_info_kstat = NULL;
}

/*
 * Create and install kstats for the boot CPU.
 */
void
cpu_kstat_init(cpu_t *cp)
{
	mutex_enter(&cpu_lock);
	cpu_info_kstat_create(cp);
	cpu_stats_kstat_create(cp);
	cpu_create_intrstat(cp);
	cpu_set_state(cp);
	mutex_exit(&cpu_lock);
}

/*
 * Make visible to the zone that subset of the cpu information that would be
 * initialized when a cpu is configured (but still offline).
 */
void
cpu_visibility_configure(cpu_t *cp, zone_t *zone)
{
	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(pool_pset_enabled());
	ASSERT(cp != NULL);

	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
		zone->zone_ncpus++;
		ASSERT(zone->zone_ncpus <= ncpus);
	}
	if (cp->cpu_info_kstat != NULL)
		kstat_zone_add(cp->cpu_info_kstat, zoneid);
}

/*
 * Make visible to the zone that subset of the cpu information that would be
 * initialized when a previously configured cpu is onlined.
 */
void
cpu_visibility_online(cpu_t *cp, zone_t *zone)
{
	kstat_t *ksp;
	char name[sizeof ("cpu_stat") + 10];	/* enough for 32-bit cpuids */
	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;
	processorid_t cpun;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(pool_pset_enabled());
	ASSERT(cp != NULL);
	ASSERT(cpu_is_active(cp));

	cpun = cp->cpu_id;
	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
		zone->zone_ncpus_online++;
		ASSERT(zone->zone_ncpus_online <= ncpus_online);
	}
	(void) snprintf(name, sizeof (name), "cpu_stat%d", cpun);
	if ((ksp = kstat_hold_byname("cpu_stat", cpun, name, ALL_ZONES))
	    != NULL) {
		kstat_zone_add(ksp, zoneid);
		kstat_rele(ksp);
	}
	if ((ksp = kstat_hold_byname("cpu", cpun, "sys", ALL_ZONES)) != NULL) {
		kstat_zone_add(ksp, zoneid);
		kstat_rele(ksp);
	}
	if ((ksp = kstat_hold_byname("cpu", cpun, "vm", ALL_ZONES)) != NULL) {
		kstat_zone_add(ksp, zoneid);
		kstat_rele(ksp);
	}
	if ((ksp = kstat_hold_byname("cpu", cpun, "intrstat", ALL_ZONES)) !=
	    NULL) {
		kstat_zone_add(ksp, zoneid);
		kstat_rele(ksp);
	}
}

/*
 * Update relevant kstats such that cpu is now visible to processes
 * executing in specified zone.
 */
void
cpu_visibility_add(cpu_t *cp, zone_t *zone)
{
	cpu_visibility_configure(cp, zone);
	if (cpu_is_active(cp))
		cpu_visibility_online(cp, zone);
}

/*
 * Make invisible to the zone that subset of the cpu information that would be
 * torn down when a previously offlined cpu is unconfigured.
 */
void
cpu_visibility_unconfigure(cpu_t *cp, zone_t *zone)
{
	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(pool_pset_enabled());
	ASSERT(cp != NULL);

	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
		ASSERT(zone->zone_ncpus != 0);
		zone->zone_ncpus--;
	}
	if (cp->cpu_info_kstat)
		kstat_zone_remove(cp->cpu_info_kstat, zoneid);
}

/*
 * Make invisible to the zone that subset of the cpu information that would be
 * torn down when a cpu is offlined (but still configured).
 */
void
cpu_visibility_offline(cpu_t *cp, zone_t *zone)
{
	kstat_t *ksp;
	char name[sizeof ("cpu_stat") + 10];	/* enough for 32-bit cpuids */
	zoneid_t zoneid = zone ? zone->zone_id : ALL_ZONES;
	processorid_t cpun;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(pool_pset_enabled());
	ASSERT(cp != NULL);
	ASSERT(cpu_is_active(cp));

	cpun = cp->cpu_id;
	if (zoneid != ALL_ZONES && zoneid != GLOBAL_ZONEID) {
		ASSERT(zone->zone_ncpus_online != 0);
		zone->zone_ncpus_online--;
	}

	if ((ksp = kstat_hold_byname("cpu", cpun, "intrstat", ALL_ZONES)) !=
	    NULL) {
		kstat_zone_remove(ksp, zoneid);
		kstat_rele(ksp);
	}
	if ((ksp = kstat_hold_byname("cpu", cpun, "vm", ALL_ZONES)) != NULL) {
		kstat_zone_remove(ksp, zoneid);
		kstat_rele(ksp);
	}
	if ((ksp = kstat_hold_byname("cpu", cpun, "sys", ALL_ZONES)) != NULL) {
		kstat_zone_remove(ksp, zoneid);
		kstat_rele(ksp);
	}
	(void) snprintf(name, sizeof (name), "cpu_stat%d", cpun);
	if ((ksp = kstat_hold_byname("cpu_stat", cpun, name, ALL_ZONES))
	    != NULL) {
		kstat_zone_remove(ksp, zoneid);
		kstat_rele(ksp);
	}
}

/*
 * Update relevant kstats such that cpu is no longer visible to processes
 * executing in specified zone.
 */
void
cpu_visibility_remove(cpu_t *cp, zone_t *zone)
{
	if (cpu_is_active(cp))
		cpu_visibility_offline(cp, zone);
	cpu_visibility_unconfigure(cp, zone);
}

/*
 * Bind a thread to a CPU as requested.
 */
int
cpu_bind_thread(kthread_id_t tp, processorid_t bind, processorid_t *obind,
    int *error)
{
	processorid_t	binding;
	cpu_t		*cp = NULL;

	ASSERT(MUTEX_HELD(&cpu_lock));
	ASSERT(MUTEX_HELD(&ttoproc(tp)->p_lock));

	thread_lock(tp);

	/*
	 * Record old binding, but change the obind, which was initialized
	 * to PBIND_NONE, only if this thread has a binding.  This avoids
	 * reporting PBIND_NONE for a process when some LWPs are bound.
	 */
	binding = tp->t_bind_cpu;
	if (binding != PBIND_NONE)
		*obind = binding;	/* record old binding */

	switch (bind) {
	case PBIND_QUERY:
		/* Just return the old binding */
		thread_unlock(tp);
		return (0);

	case PBIND_QUERY_TYPE:
		/* Return the binding type */
		*obind = TB_CPU_IS_SOFT(tp) ? PBIND_SOFT : PBIND_HARD;
		thread_unlock(tp);
		return (0);

	case PBIND_SOFT:
		/*
		 *  Set soft binding for this thread and return the actual
		 *  binding
		 */
		TB_CPU_SOFT_SET(tp);
		thread_unlock(tp);
		return (0);

	case PBIND_HARD:
		/*
		 *  Set hard binding for this thread and return the actual
		 *  binding
		 */
		TB_CPU_HARD_SET(tp);
		thread_unlock(tp);
		return (0);

	default:
		break;
	}

	/*
	 * If this thread/LWP cannot be bound because of permission
	 * problems, just note that and return success so that the
	 * other threads/LWPs will be bound.  This is the way
	 * processor_bind() is defined to work.
	 *
	 * Binding will get EPERM if the thread is of system class
	 * or hasprocperm() fails.
	 */
	if (tp->t_cid == 0 || !hasprocperm(tp->t_cred, CRED())) {
		*error = EPERM;
		thread_unlock(tp);
		return (0);
	}

	binding = bind;
	if (binding != PBIND_NONE) {
		cp = cpu_get((processorid_t)binding);
		/*
		 * Make sure binding is valid and is in right partition.
		 */
		if (cp == NULL || tp->t_cpupart != cp->cpu_part) {
			*error = EINVAL;
			thread_unlock(tp);
			return (0);
		}
	}
	tp->t_bind_cpu = binding;	/* set new binding */

	/*
	 * If there is no system-set reason for affinity, set
	 * the t_bound_cpu field to reflect the binding.
	 */
	if (tp->t_affinitycnt == 0) {
		if (binding == PBIND_NONE) {
			/*
			 * We may need to adjust disp_max_unbound_pri
			 * since we're becoming unbound.
			 */
			disp_adjust_unbound_pri(tp);

			tp->t_bound_cpu = NULL;	/* set new binding */

			/*
			 * Move thread to lgroup with strongest affinity
			 * after unbinding
			 */
			if (tp->t_lgrp_affinity)
				lgrp_move_thread(tp,
				    lgrp_choose(tp, tp->t_cpupart), 1);

			if (tp->t_state == TS_ONPROC &&
			    tp->t_cpu->cpu_part != tp->t_cpupart)
				cpu_surrender(tp);
		} else {
			lpl_t	*lpl;

			tp->t_bound_cpu = cp;
			ASSERT(cp->cpu_lpl != NULL);

			/*
			 * Set home to lgroup with most affinity containing CPU
			 * that thread is being bound or minimum bounding
			 * lgroup if no affinities set
			 */
			if (tp->t_lgrp_affinity)
				lpl = lgrp_affinity_best(tp, tp->t_cpupart,
				    LGRP_NONE, B_FALSE);
			else
				lpl = cp->cpu_lpl;

			if (tp->t_lpl != lpl) {
				/* can't grab cpu_lock */
				lgrp_move_thread(tp, lpl, 1);
			}

			/*
			 * Make the thread switch to the bound CPU.
			 * If the thread is runnable, we need to
			 * requeue it even if t_cpu is already set
			 * to the right CPU, since it may be on a
			 * kpreempt queue and need to move to a local
			 * queue.  We could check t_disp_queue to
			 * avoid unnecessary overhead if it's already
			 * on the right queue, but since this isn't
			 * a performance-critical operation it doesn't
			 * seem worth the extra code and complexity.
			 *
			 * If the thread is weakbound to the cpu then it will
			 * resist the new binding request until the weak
			 * binding drops.  The cpu_surrender or requeueing
			 * below could be skipped in such cases (since it
			 * will have no effect), but that would require
			 * thread_allowmigrate to acquire thread_lock so
			 * we'll take the very occasional hit here instead.
			 */
			if (tp->t_state == TS_ONPROC) {
				cpu_surrender(tp);
			} else if (tp->t_state == TS_RUN) {
				cpu_t *ocp = tp->t_cpu;

				(void) dispdeq(tp);
				setbackdq(tp);
				/*
				 * Either on the bound CPU's disp queue now,
				 * or swapped out or on the swap queue.
				 */
				ASSERT(tp->t_disp_queue == cp->cpu_disp ||
				    tp->t_weakbound_cpu == ocp ||
				    (tp->t_schedflag & (TS_LOAD | TS_ON_SWAPQ))
				    != TS_LOAD);
			}
		}
	}

	/*
	 * Our binding has changed; set TP_CHANGEBIND.
	 */
	tp->t_proc_flag |= TP_CHANGEBIND;
	aston(tp);

	thread_unlock(tp);

	return (0);
}


cpuset_t *
cpuset_alloc(int kmflags)
{
	return (kmem_alloc(sizeof (cpuset_t), kmflags));
}

void
cpuset_free(cpuset_t *s)
{
	kmem_free(s, sizeof (cpuset_t));
}

void
cpuset_all(cpuset_t *s)
{
	int i;

	for (i = 0; i < CPUSET_WORDS; i++)
		s->cpub[i] = ~0UL;
}

void
cpuset_all_but(cpuset_t *s, const uint_t cpu)
{
	cpuset_all(s);
	CPUSET_DEL(*s, cpu);
}

void
cpuset_only(cpuset_t *s, const uint_t cpu)
{
	CPUSET_ZERO(*s);
	CPUSET_ADD(*s, cpu);
}

long
cpu_in_set(cpuset_t *s, const uint_t cpu)
{
	VERIFY(cpu < NCPU);
	return (BT_TEST(s->cpub, cpu));
}

void
cpuset_add(cpuset_t *s, const uint_t cpu)
{
	VERIFY(cpu < NCPU);
	BT_SET(s->cpub, cpu);
}

void
cpuset_del(cpuset_t *s, const uint_t cpu)
{
	VERIFY(cpu < NCPU);
	BT_CLEAR(s->cpub, cpu);
}

int
cpuset_isnull(cpuset_t *s)
{
	int i;

	for (i = 0; i < CPUSET_WORDS; i++) {
		if (s->cpub[i] != 0)
			return (0);
	}
	return (1);
}

int
cpuset_isequal(cpuset_t *s1, cpuset_t *s2)
{
	int i;

	for (i = 0; i < CPUSET_WORDS; i++) {
		if (s1->cpub[i] != s2->cpub[i])
			return (0);
	}
	return (1);
}

uint_t
cpuset_find(cpuset_t *s)
{

	uint_t	i;
	uint_t	cpu = (uint_t)-1;

	/*
	 * Find a cpu in the cpuset
	 */
	for (i = 0; i < CPUSET_WORDS; i++) {
		cpu = (uint_t)(lowbit(s->cpub[i]) - 1);
		if (cpu != (uint_t)-1) {
			cpu += i * BT_NBIPUL;
			break;
		}
	}
	return (cpu);
}

void
cpuset_bounds(cpuset_t *s, uint_t *smallestid, uint_t *largestid)
{
	int	i, j;
	uint_t	bit;

	/*
	 * First, find the smallest cpu id in the set.
	 */
	for (i = 0; i < CPUSET_WORDS; i++) {
		if (s->cpub[i] != 0) {
			bit = (uint_t)(lowbit(s->cpub[i]) - 1);
			ASSERT(bit != (uint_t)-1);
			*smallestid = bit + (i * BT_NBIPUL);

			/*
			 * Now find the largest cpu id in
			 * the set and return immediately.
			 * Done in an inner loop to avoid
			 * having to break out of the first
			 * loop.
			 */
			for (j = CPUSET_WORDS - 1; j >= i; j--) {
				if (s->cpub[j] != 0) {
					bit = (uint_t)(highbit(s->cpub[j]) - 1);
					ASSERT(bit != (uint_t)-1);
					*largestid = bit + (j * BT_NBIPUL);
					ASSERT(*largestid >= *smallestid);
					return;
				}
			}

			/*
			 * If this code is reached, a
			 * smallestid was found, but not a
			 * largestid. The cpuset must have
			 * been changed during the course
			 * of this function call.
			 */
			ASSERT(0);
		}
	}
	*smallestid = *largestid = CPUSET_NOTINSET;
}

void
cpuset_atomic_del(cpuset_t *s, const uint_t cpu)
{
	VERIFY(cpu < NCPU);
	BT_ATOMIC_CLEAR(s->cpub, (cpu))
}

void
cpuset_atomic_add(cpuset_t *s, const uint_t cpu)
{
	VERIFY(cpu < NCPU);
	BT_ATOMIC_SET(s->cpub, (cpu))
}

long
cpuset_atomic_xadd(cpuset_t *s, const uint_t cpu)
{
	long res;

	VERIFY(cpu < NCPU);
	BT_ATOMIC_SET_EXCL(s->cpub, cpu, res);
	return (res);
}

long
cpuset_atomic_xdel(cpuset_t *s, const uint_t cpu)
{
	long res;

	VERIFY(cpu < NCPU);
	BT_ATOMIC_CLEAR_EXCL(s->cpub, cpu, res);
	return (res);
}

void
cpuset_or(cpuset_t *dst, cpuset_t *src)
{
	for (int i = 0; i < CPUSET_WORDS; i++) {
		dst->cpub[i] |= src->cpub[i];
	}
}

void
cpuset_xor(cpuset_t *dst, cpuset_t *src)
{
	for (int i = 0; i < CPUSET_WORDS; i++) {
		dst->cpub[i] ^= src->cpub[i];
	}
}

void
cpuset_and(cpuset_t *dst, cpuset_t *src)
{
	for (int i = 0; i < CPUSET_WORDS; i++) {
		dst->cpub[i] &= src->cpub[i];
	}
}

void
cpuset_zero(cpuset_t *dst)
{
	for (int i = 0; i < CPUSET_WORDS; i++) {
		dst->cpub[i] = 0;
	}
}


/*
 * Unbind threads bound to specified CPU.
 *
 * If `unbind_all_threads' is true, unbind all user threads bound to a given
 * CPU. Otherwise unbind all soft-bound user threads.
 */
int
cpu_unbind(processorid_t cpu, boolean_t unbind_all_threads)
{
	processorid_t obind;
	kthread_t *tp;
	int ret = 0;
	proc_t *pp;
	int err, berr = 0;

	ASSERT(MUTEX_HELD(&cpu_lock));

	mutex_enter(&pidlock);
	for (pp = practive; pp != NULL; pp = pp->p_next) {
		mutex_enter(&pp->p_lock);
		tp = pp->p_tlist;
		/*
		 * Skip zombies, kernel processes, and processes in
		 * other zones, if called from a non-global zone.
		 */
		if (tp == NULL || (pp->p_flag & SSYS) ||
		    !HASZONEACCESS(curproc, pp->p_zone->zone_id)) {
			mutex_exit(&pp->p_lock);
			continue;
		}
		do {
			if (tp->t_bind_cpu != cpu)
				continue;
			/*
			 * Skip threads with hard binding when
			 * `unbind_all_threads' is not specified.
			 */
			if (!unbind_all_threads && TB_CPU_IS_HARD(tp))
				continue;
			err = cpu_bind_thread(tp, PBIND_NONE, &obind, &berr);
			if (ret == 0)
				ret = err;
		} while ((tp = tp->t_forw) != pp->p_tlist);
		mutex_exit(&pp->p_lock);
	}
	mutex_exit(&pidlock);
	if (ret == 0)
		ret = berr;
	return (ret);
}


/*
 * Destroy all remaining bound threads on a cpu.
 */
void
cpu_destroy_bound_threads(cpu_t *cp)
{
	extern id_t syscid;
	register kthread_id_t	t, tlist, tnext;

	/*
	 * Destroy all remaining bound threads on the cpu.  This
	 * should include both the interrupt threads and the idle thread.
	 * This requires some care, since we need to traverse the
	 * thread list with the pidlock mutex locked, but thread_free
	 * also locks the pidlock mutex.  So, we collect the threads
	 * we're going to reap in a list headed by "tlist", then we
	 * unlock the pidlock mutex and traverse the tlist list,
	 * doing thread_free's on the thread's.	 Simple, n'est pas?
	 * Also, this depends on thread_free not mucking with the
	 * t_next and t_prev links of the thread.
	 */

	if ((t = curthread) != NULL) {

		tlist = NULL;
		mutex_enter(&pidlock);
		do {
			tnext = t->t_next;
			if (t->t_bound_cpu == cp) {

				/*
				 * We've found a bound thread, carefully unlink
				 * it out of the thread list, and add it to
				 * our "tlist".	 We "know" we don't have to
				 * worry about unlinking curthread (the thread
				 * that is executing this code).
				 */
				t->t_next->t_prev = t->t_prev;
				t->t_prev->t_next = t->t_next;
				t->t_next = tlist;
				tlist = t;
				ASSERT(t->t_cid == syscid);
				/* wake up anyone blocked in thread_join */
				cv_broadcast(&t->t_joincv);
				/*
				 * t_lwp set by interrupt threads and not
				 * cleared.
				 */
				t->t_lwp = NULL;
				/*
				 * Pause and idle threads always have
				 * t_state set to TS_ONPROC.
				 */
				t->t_state = TS_FREE;
				t->t_prev = NULL;	/* Just in case */
			}

		} while ((t = tnext) != curthread);

		mutex_exit(&pidlock);

		mutex_sync();
		for (t = tlist; t != NULL; t = tnext) {
			tnext = t->t_next;
			thread_free(t);
		}
	}
}

/*
 * Update the cpu_supp_freqs of this cpu. This information is returned
 * as part of cpu_info kstats. If the cpu_info_kstat exists already, then
 * maintain the kstat data size.
 */
void
cpu_set_supp_freqs(cpu_t *cp, const char *freqs)
{
	char clkstr[sizeof ("18446744073709551615") + 1]; /* ui64 MAX */
	const char *lfreqs = clkstr;
	boolean_t kstat_exists = B_FALSE;
	kstat_t *ksp;
	size_t len;

	/*
	 * A NULL pointer means we only support one speed.
	 */
	if (freqs == NULL)
		(void) snprintf(clkstr, sizeof (clkstr), "%"PRIu64,
		    cp->cpu_curr_clock);
	else
		lfreqs = freqs;

	/*
	 * Make sure the frequency doesn't change while a snapshot is
	 * going on. Of course, we only need to worry about this if
	 * the kstat exists.
	 */
	if ((ksp = cp->cpu_info_kstat) != NULL) {
		mutex_enter(ksp->ks_lock);
		kstat_exists = B_TRUE;
	}

	/*
	 * Free any previously allocated string and if the kstat
	 * already exists, then update its data size.
	 */
	if (cp->cpu_supp_freqs != NULL) {
		len = strlen(cp->cpu_supp_freqs) + 1;
		kmem_free(cp->cpu_supp_freqs, len);
		if (kstat_exists)
			ksp->ks_data_size -= len;
	}

	/*
	 * Allocate the new string and set the pointer.
	 */
	len = strlen(lfreqs) + 1;
	cp->cpu_supp_freqs = kmem_alloc(len, KM_SLEEP);
	(void) strcpy(cp->cpu_supp_freqs, lfreqs);

	/*
	 * If the kstat already exists then update the data size and
	 * free the lock.
	 */
	if (kstat_exists) {
		ksp->ks_data_size += len;
		mutex_exit(ksp->ks_lock);
	}
}

/*
 * Indicate the current CPU's clock freqency (in Hz).
 * The calling context must be such that CPU references are safe.
 */
void
cpu_set_curr_clock(uint64_t new_clk)
{
	uint64_t old_clk;

	old_clk = CPU->cpu_curr_clock;
	CPU->cpu_curr_clock = new_clk;

	/*
	 * The cpu-change-speed DTrace probe exports the frequency in Hz
	 */
	DTRACE_PROBE3(cpu__change__speed, processorid_t, CPU->cpu_id,
	    uint64_t, old_clk, uint64_t, new_clk);
}

/*
 * processor_info(2) and p_online(2) status support functions
 *   The constants returned by the cpu_get_state() and cpu_get_state_str() are
 *   for use in communicating processor state information to userland.  Kernel
 *   subsystems should only be using the cpu_flags value directly.  Subsystems
 *   modifying cpu_flags should record the state change via a call to the
 *   cpu_set_state().
 */

/*
 * Update the pi_state of this CPU.  This function provides the CPU status for
 * the information returned by processor_info(2).
 */
void
cpu_set_state(cpu_t *cpu)
{
	ASSERT(MUTEX_HELD(&cpu_lock));
	cpu->cpu_type_info.pi_state = cpu_get_state(cpu);
	cpu->cpu_state_begin = gethrestime_sec();
	pool_cpu_mod = gethrtime();
}

/*
 * Return offline/online/other status for the indicated CPU.  Use only for
 * communication with user applications; cpu_flags provides the in-kernel
 * interface.
 */
int
cpu_get_state(cpu_t *cpu)
{
	ASSERT(MUTEX_HELD(&cpu_lock));
	if (cpu->cpu_flags & CPU_POWEROFF)
		return (P_POWEROFF);
	else if (cpu->cpu_flags & CPU_FAULTED)
		return (P_FAULTED);
	else if (cpu->cpu_flags & CPU_SPARE)
		return (P_SPARE);
	else if ((cpu->cpu_flags & (CPU_READY | CPU_OFFLINE)) != CPU_READY)
		return (P_OFFLINE);
	else if (cpu->cpu_flags & CPU_ENABLE)
		return (P_ONLINE);
	else
		return (P_NOINTR);
}

/*
 * Return processor_info(2) state as a string.
 */
const char *
cpu_get_state_str(cpu_t *cpu)
{
	const char *string;

	switch (cpu_get_state(cpu)) {
	case P_ONLINE:
		string = PS_ONLINE;
		break;
	case P_POWEROFF:
		string = PS_POWEROFF;
		break;
	case P_NOINTR:
		string = PS_NOINTR;
		break;
	case P_SPARE:
		string = PS_SPARE;
		break;
	case P_FAULTED:
		string = PS_FAULTED;
		break;
	case P_OFFLINE:
		string = PS_OFFLINE;
		break;
	default:
		string = "unknown";
		break;
	}
	return (string);
}

/*
 * Export this CPU's statistics (cpu_stat_t and cpu_stats_t) as raw and named
 * kstats, respectively.  This is done when a CPU is initialized or placed
 * online via p_online(2).
 */
static void
cpu_stats_kstat_create(cpu_t *cp)
{
	int 	instance = cp->cpu_id;
	char 	*module = "cpu";
	char 	*class = "misc";
	kstat_t	*ksp;
	zoneid_t zoneid;

	ASSERT(MUTEX_HELD(&cpu_lock));

	if (pool_pset_enabled())
		zoneid = GLOBAL_ZONEID;
	else
		zoneid = ALL_ZONES;
	/*
	 * Create named kstats
	 */
#define	CPU_STATS_KS_CREATE(name, tsize, update_func)                    \
	ksp = kstat_create_zone(module, instance, (name), class,         \
	    KSTAT_TYPE_NAMED, (tsize) / sizeof (kstat_named_t), 0,       \
	    zoneid);                                                     \
	if (ksp != NULL) {                                               \
		ksp->ks_private = cp;                                    \
		ksp->ks_update = (update_func);                          \
		kstat_install(ksp);                                      \
	} else                                                           \
		cmn_err(CE_WARN, "cpu: unable to create %s:%d:%s kstat", \
		    module, instance, (name));

	CPU_STATS_KS_CREATE("sys", sizeof (cpu_sys_stats_ks_data_template),
	    cpu_sys_stats_ks_update);
	CPU_STATS_KS_CREATE("vm", sizeof (cpu_vm_stats_ks_data_template),
	    cpu_vm_stats_ks_update);

	/*
	 * Export the familiar cpu_stat_t KSTAT_TYPE_RAW kstat.
	 */
	ksp = kstat_create_zone("cpu_stat", cp->cpu_id, NULL,
	    "misc", KSTAT_TYPE_RAW, sizeof (cpu_stat_t), 0, zoneid);
	if (ksp != NULL) {
		ksp->ks_update = cpu_stat_ks_update;
		ksp->ks_private = cp;
		kstat_install(ksp);
	}
}

static void
cpu_stats_kstat_destroy(cpu_t *cp)
{
	char ks_name[KSTAT_STRLEN];

	(void) sprintf(ks_name, "cpu_stat%d", cp->cpu_id);
	kstat_delete_byname("cpu_stat", cp->cpu_id, ks_name);

	kstat_delete_byname("cpu", cp->cpu_id, "sys");
	kstat_delete_byname("cpu", cp->cpu_id, "vm");
}

static int
cpu_sys_stats_ks_update(kstat_t *ksp, int rw)
{
	cpu_t *cp = (cpu_t *)ksp->ks_private;
	struct cpu_sys_stats_ks_data *csskd;
	cpu_sys_stats_t *css;
	hrtime_t msnsecs[NCMSTATES];
	int	i;

	if (rw == KSTAT_WRITE)
		return (EACCES);

	csskd = ksp->ks_data;
	css = &cp->cpu_stats.sys;

	/*
	 * Read CPU mstate, but compare with the last values we
	 * received to make sure that the returned kstats never
	 * decrease.
	 */

	get_cpu_mstate(cp, msnsecs);
	if (csskd->cpu_nsec_idle.value.ui64 > msnsecs[CMS_IDLE])
		msnsecs[CMS_IDLE] = csskd->cpu_nsec_idle.value.ui64;
	if (csskd->cpu_nsec_user.value.ui64 > msnsecs[CMS_USER])
		msnsecs[CMS_USER] = csskd->cpu_nsec_user.value.ui64;
	if (csskd->cpu_nsec_kernel.value.ui64 > msnsecs[CMS_SYSTEM])
		msnsecs[CMS_SYSTEM] = csskd->cpu_nsec_kernel.value.ui64;

	bcopy(&cpu_sys_stats_ks_data_template, ksp->ks_data,
	    sizeof (cpu_sys_stats_ks_data_template));

	csskd->cpu_ticks_wait.value.ui64 = 0;
	csskd->wait_ticks_io.value.ui64 = 0;

	csskd->cpu_nsec_idle.value.ui64 = msnsecs[CMS_IDLE];
	csskd->cpu_nsec_user.value.ui64 = msnsecs[CMS_USER];
	csskd->cpu_nsec_kernel.value.ui64 = msnsecs[CMS_SYSTEM];
	csskd->cpu_ticks_idle.value.ui64 =
	    NSEC_TO_TICK(csskd->cpu_nsec_idle.value.ui64);
	csskd->cpu_ticks_user.value.ui64 =
	    NSEC_TO_TICK(csskd->cpu_nsec_user.value.ui64);
	csskd->cpu_ticks_kernel.value.ui64 =
	    NSEC_TO_TICK(csskd->cpu_nsec_kernel.value.ui64);
	csskd->cpu_nsec_dtrace.value.ui64 = cp->cpu_dtrace_nsec;
	csskd->dtrace_probes.value.ui64 = cp->cpu_dtrace_probes;
	csskd->cpu_nsec_intr.value.ui64 = cp->cpu_intrlast;
	csskd->cpu_load_intr.value.ui64 = cp->cpu_intrload;
	csskd->bread.value.ui64 = css->bread;
	csskd->bwrite.value.ui64 = css->bwrite;
	csskd->lread.value.ui64 = css->lread;
	csskd->lwrite.value.ui64 = css->lwrite;
	csskd->phread.value.ui64 = css->phread;
	csskd->phwrite.value.ui64 = css->phwrite;
	csskd->pswitch.value.ui64 = css->pswitch;
	csskd->trap.value.ui64 = css->trap;
	csskd->intr.value.ui64 = 0;
	for (i = 0; i < PIL_MAX; i++)
		csskd->intr.value.ui64 += css->intr[i];
	csskd->syscall.value.ui64 = css->syscall;
	csskd->sysread.value.ui64 = css->sysread;
	csskd->syswrite.value.ui64 = css->syswrite;
	csskd->sysfork.value.ui64 = css->sysfork;
	csskd->sysvfork.value.ui64 = css->sysvfork;
	csskd->sysexec.value.ui64 = css->sysexec;
	csskd->readch.value.ui64 = css->readch;
	csskd->writech.value.ui64 = css->writech;
	csskd->rcvint.value.ui64 = css->rcvint;
	csskd->xmtint.value.ui64 = css->xmtint;
	csskd->mdmint.value.ui64 = css->mdmint;
	csskd->rawch.value.ui64 = css->rawch;
	csskd->canch.value.ui64 = css->canch;
	csskd->outch.value.ui64 = css->outch;
	csskd->msg.value.ui64 = css->msg;
	csskd->sema.value.ui64 = css->sema;
	csskd->namei.value.ui64 = css->namei;
	csskd->ufsiget.value.ui64 = css->ufsiget;
	csskd->ufsdirblk.value.ui64 = css->ufsdirblk;
	csskd->ufsipage.value.ui64 = css->ufsipage;
	csskd->ufsinopage.value.ui64 = css->ufsinopage;
	csskd->procovf.value.ui64 = css->procovf;
	csskd->intrthread.value.ui64 = 0;
	for (i = 0; i < LOCK_LEVEL - 1; i++)
		csskd->intrthread.value.ui64 += css->intr[i];
	csskd->intrblk.value.ui64 = css->intrblk;
	csskd->intrunpin.value.ui64 = css->intrunpin;
	csskd->idlethread.value.ui64 = css->idlethread;
	csskd->inv_swtch.value.ui64 = css->inv_swtch;
	csskd->nthreads.value.ui64 = css->nthreads;
	csskd->cpumigrate.value.ui64 = css->cpumigrate;
	csskd->xcalls.value.ui64 = css->xcalls;
	csskd->mutex_adenters.value.ui64 = css->mutex_adenters;
	csskd->rw_rdfails.value.ui64 = css->rw_rdfails;
	csskd->rw_wrfails.value.ui64 = css->rw_wrfails;
	csskd->modload.value.ui64 = css->modload;
	csskd->modunload.value.ui64 = css->modunload;
	csskd->bawrite.value.ui64 = css->bawrite;
	csskd->iowait.value.ui64 = css->iowait;

	return (0);
}

static int
cpu_vm_stats_ks_update(kstat_t *ksp, int rw)
{
	cpu_t *cp = (cpu_t *)ksp->ks_private;
	struct cpu_vm_stats_ks_data *cvskd;
	cpu_vm_stats_t *cvs;

	if (rw == KSTAT_WRITE)
		return (EACCES);

	cvs = &cp->cpu_stats.vm;
	cvskd = ksp->ks_data;

	bcopy(&cpu_vm_stats_ks_data_template, ksp->ks_data,
	    sizeof (cpu_vm_stats_ks_data_template));
	cvskd->pgrec.value.ui64 = cvs->pgrec;
	cvskd->pgfrec.value.ui64 = cvs->pgfrec;
	cvskd->pgin.value.ui64 = cvs->pgin;
	cvskd->pgpgin.value.ui64 = cvs->pgpgin;
	cvskd->pgout.value.ui64 = cvs->pgout;
	cvskd->pgpgout.value.ui64 = cvs->pgpgout;
	cvskd->swapin.value.ui64 = cvs->swapin;
	cvskd->pgswapin.value.ui64 = cvs->pgswapin;
	cvskd->swapout.value.ui64 = cvs->swapout;
	cvskd->pgswapout.value.ui64 = cvs->pgswapout;
	cvskd->zfod.value.ui64 = cvs->zfod;
	cvskd->dfree.value.ui64 = cvs->dfree;
	cvskd->scan.value.ui64 = cvs->scan;
	cvskd->rev.value.ui64 = cvs->rev;
	cvskd->hat_fault.value.ui64 = cvs->hat_fault;
	cvskd->as_fault.value.ui64 = cvs->as_fault;
	cvskd->maj_fault.value.ui64 = cvs->maj_fault;
	cvskd->cow_fault.value.ui64 = cvs->cow_fault;
	cvskd->prot_fault.value.ui64 = cvs->prot_fault;
	cvskd->softlock.value.ui64 = cvs->softlock;
	cvskd->kernel_asflt.value.ui64 = cvs->kernel_asflt;
	cvskd->pgrrun.value.ui64 = cvs->pgrrun;
	cvskd->execpgin.value.ui64 = cvs->execpgin;
	cvskd->execpgout.value.ui64 = cvs->execpgout;
	cvskd->execfree.value.ui64 = cvs->execfree;
	cvskd->anonpgin.value.ui64 = cvs->anonpgin;
	cvskd->anonpgout.value.ui64 = cvs->anonpgout;
	cvskd->anonfree.value.ui64 = cvs->anonfree;
	cvskd->fspgin.value.ui64 = cvs->fspgin;
	cvskd->fspgout.value.ui64 = cvs->fspgout;
	cvskd->fsfree.value.ui64 = cvs->fsfree;

	return (0);
}

static int
cpu_stat_ks_update(kstat_t *ksp, int rw)
{
	cpu_stat_t *cso;
	cpu_t *cp;
	int i;
	hrtime_t msnsecs[NCMSTATES];

	cso = (cpu_stat_t *)ksp->ks_data;
	cp = (cpu_t *)ksp->ks_private;

	if (rw == KSTAT_WRITE)
		return (EACCES);

	/*
	 * Read CPU mstate, but compare with the last values we
	 * received to make sure that the returned kstats never
	 * decrease.
	 */

	get_cpu_mstate(cp, msnsecs);
	msnsecs[CMS_IDLE] = NSEC_TO_TICK(msnsecs[CMS_IDLE]);
	msnsecs[CMS_USER] = NSEC_TO_TICK(msnsecs[CMS_USER]);
	msnsecs[CMS_SYSTEM] = NSEC_TO_TICK(msnsecs[CMS_SYSTEM]);
	if (cso->cpu_sysinfo.cpu[CPU_IDLE] < msnsecs[CMS_IDLE])
		cso->cpu_sysinfo.cpu[CPU_IDLE] = msnsecs[CMS_IDLE];
	if (cso->cpu_sysinfo.cpu[CPU_USER] < msnsecs[CMS_USER])
		cso->cpu_sysinfo.cpu[CPU_USER] = msnsecs[CMS_USER];
	if (cso->cpu_sysinfo.cpu[CPU_KERNEL] < msnsecs[CMS_SYSTEM])
		cso->cpu_sysinfo.cpu[CPU_KERNEL] = msnsecs[CMS_SYSTEM];
	cso->cpu_sysinfo.cpu[CPU_WAIT] 	= 0;
	cso->cpu_sysinfo.wait[W_IO] 	= 0;
	cso->cpu_sysinfo.wait[W_SWAP]	= 0;
	cso->cpu_sysinfo.wait[W_PIO]	= 0;
	cso->cpu_sysinfo.bread 		= CPU_STATS(cp, sys.bread);
	cso->cpu_sysinfo.bwrite 	= CPU_STATS(cp, sys.bwrite);
	cso->cpu_sysinfo.lread 		= CPU_STATS(cp, sys.lread);
	cso->cpu_sysinfo.lwrite 	= CPU_STATS(cp, sys.lwrite);
	cso->cpu_sysinfo.phread 	= CPU_STATS(cp, sys.phread);
	cso->cpu_sysinfo.phwrite 	= CPU_STATS(cp, sys.phwrite);
	cso->cpu_sysinfo.pswitch 	= CPU_STATS(cp, sys.pswitch);
	cso->cpu_sysinfo.trap 		= CPU_STATS(cp, sys.trap);
	cso->cpu_sysinfo.intr		= 0;
	for (i = 0; i < PIL_MAX; i++)
		cso->cpu_sysinfo.intr += CPU_STATS(cp, sys.intr[i]);
	cso->cpu_sysinfo.syscall	= CPU_STATS(cp, sys.syscall);
	cso->cpu_sysinfo.sysread	= CPU_STATS(cp, sys.sysread);
	cso->cpu_sysinfo.syswrite	= CPU_STATS(cp, sys.syswrite);
	cso->cpu_sysinfo.sysfork	= CPU_STATS(cp, sys.sysfork);
	cso->cpu_sysinfo.sysvfork	= CPU_STATS(cp, sys.sysvfork);
	cso->cpu_sysinfo.sysexec	= CPU_STATS(cp, sys.sysexec);
	cso->cpu_sysinfo.readch		= CPU_STATS(cp, sys.readch);
	cso->cpu_sysinfo.writech	= CPU_STATS(cp, sys.writech);
	cso->cpu_sysinfo.rcvint		= CPU_STATS(cp, sys.rcvint);
	cso->cpu_sysinfo.xmtint		= CPU_STATS(cp, sys.xmtint);
	cso->cpu_sysinfo.mdmint		= CPU_STATS(cp, sys.mdmint);
	cso->cpu_sysinfo.rawch		= CPU_STATS(cp, sys.rawch);
	cso->cpu_sysinfo.canch		= CPU_STATS(cp, sys.canch);
	cso->cpu_sysinfo.outch		= CPU_STATS(cp, sys.outch);
	cso->cpu_sysinfo.msg		= CPU_STATS(cp, sys.msg);
	cso->cpu_sysinfo.sema		= CPU_STATS(cp, sys.sema);
	cso->cpu_sysinfo.namei		= CPU_STATS(cp, sys.namei);
	cso->cpu_sysinfo.ufsiget	= CPU_STATS(cp, sys.ufsiget);
	cso->cpu_sysinfo.ufsdirblk	= CPU_STATS(cp, sys.ufsdirblk);
	cso->cpu_sysinfo.ufsipage	= CPU_STATS(cp, sys.ufsipage);
	cso->cpu_sysinfo.ufsinopage	= CPU_STATS(cp, sys.ufsinopage);
	cso->cpu_sysinfo.inodeovf	= 0;
	cso->cpu_sysinfo.fileovf	= 0;
	cso->cpu_sysinfo.procovf	= CPU_STATS(cp, sys.procovf);
	cso->cpu_sysinfo.intrthread	= 0;
	for (i = 0; i < LOCK_LEVEL - 1; i++)
		cso->cpu_sysinfo.intrthread += CPU_STATS(cp, sys.intr[i]);
	cso->cpu_sysinfo.intrblk	= CPU_STATS(cp, sys.intrblk);
	cso->cpu_sysinfo.idlethread	= CPU_STATS(cp, sys.idlethread);
	cso->cpu_sysinfo.inv_swtch	= CPU_STATS(cp, sys.inv_swtch);
	cso->cpu_sysinfo.nthreads	= CPU_STATS(cp, sys.nthreads);
	cso->cpu_sysinfo.cpumigrate	= CPU_STATS(cp, sys.cpumigrate);
	cso->cpu_sysinfo.xcalls		= CPU_STATS(cp, sys.xcalls);
	cso->cpu_sysinfo.mutex_adenters	= CPU_STATS(cp, sys.mutex_adenters);
	cso->cpu_sysinfo.rw_rdfails	= CPU_STATS(cp, sys.rw_rdfails);
	cso->cpu_sysinfo.rw_wrfails	= CPU_STATS(cp, sys.rw_wrfails);
	cso->cpu_sysinfo.modload	= CPU_STATS(cp, sys.modload);
	cso->cpu_sysinfo.modunload	= CPU_STATS(cp, sys.modunload);
	cso->cpu_sysinfo.bawrite	= CPU_STATS(cp, sys.bawrite);
	cso->cpu_sysinfo.rw_enters	= 0;
	cso->cpu_sysinfo.win_uo_cnt	= 0;
	cso->cpu_sysinfo.win_uu_cnt	= 0;
	cso->cpu_sysinfo.win_so_cnt	= 0;
	cso->cpu_sysinfo.win_su_cnt	= 0;
	cso->cpu_sysinfo.win_suo_cnt	= 0;

	cso->cpu_syswait.iowait		= CPU_STATS(cp, sys.iowait);
	cso->cpu_syswait.swap		= 0;
	cso->cpu_syswait.physio		= 0;

	cso->cpu_vminfo.pgrec		= CPU_STATS(cp, vm.pgrec);
	cso->cpu_vminfo.pgfrec		= CPU_STATS(cp, vm.pgfrec);
	cso->cpu_vminfo.pgin		= CPU_STATS(cp, vm.pgin);
	cso->cpu_vminfo.pgpgin		= CPU_STATS(cp, vm.pgpgin);
	cso->cpu_vminfo.pgout		= CPU_STATS(cp, vm.pgout);
	cso->cpu_vminfo.pgpgout		= CPU_STATS(cp, vm.pgpgout);
	cso->cpu_vminfo.swapin		= CPU_STATS(cp, vm.swapin);
	cso->cpu_vminfo.pgswapin	= CPU_STATS(cp, vm.pgswapin);
	cso->cpu_vminfo.swapout		= CPU_STATS(cp, vm.swapout);
	cso->cpu_vminfo.pgswapout	= CPU_STATS(cp, vm.pgswapout);
	cso->cpu_vminfo.zfod		= CPU_STATS(cp, vm.zfod);
	cso->cpu_vminfo.dfree		= CPU_STATS(cp, vm.dfree);
	cso->cpu_vminfo.scan		= CPU_STATS(cp, vm.scan);
	cso->cpu_vminfo.rev		= CPU_STATS(cp, vm.rev);
	cso->cpu_vminfo.hat_fault	= CPU_STATS(cp, vm.hat_fault);
	cso->cpu_vminfo.as_fault	= CPU_STATS(cp, vm.as_fault);
	cso->cpu_vminfo.maj_fault	= CPU_STATS(cp, vm.maj_fault);
	cso->cpu_vminfo.cow_fault	= CPU_STATS(cp, vm.cow_fault);
	cso->cpu_vminfo.prot_fault	= CPU_STATS(cp, vm.prot_fault);
	cso->cpu_vminfo.softlock	= CPU_STATS(cp, vm.softlock);
	cso->cpu_vminfo.kernel_asflt	= CPU_STATS(cp, vm.kernel_asflt);
	cso->cpu_vminfo.pgrrun		= CPU_STATS(cp, vm.pgrrun);
	cso->cpu_vminfo.execpgin	= CPU_STATS(cp, vm.execpgin);
	cso->cpu_vminfo.execpgout	= CPU_STATS(cp, vm.execpgout);
	cso->cpu_vminfo.execfree	= CPU_STATS(cp, vm.execfree);
	cso->cpu_vminfo.anonpgin	= CPU_STATS(cp, vm.anonpgin);
	cso->cpu_vminfo.anonpgout	= CPU_STATS(cp, vm.anonpgout);
	cso->cpu_vminfo.anonfree	= CPU_STATS(cp, vm.anonfree);
	cso->cpu_vminfo.fspgin		= CPU_STATS(cp, vm.fspgin);
	cso->cpu_vminfo.fspgout		= CPU_STATS(cp, vm.fspgout);
	cso->cpu_vminfo.fsfree		= CPU_STATS(cp, vm.fsfree);

	return (0);
}