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
|
/*
* Copyright 2018, Jérôme Duval, jerome.duval@gmail.com.
* Copyright 2002-2010, Axel Dörfler, axeld@pinc-software.de.
* Copyright 2013, Paweł Dziepak, pdziepak@quarnos.org.
* Copyright 2012, Alex Smith, alex@alex-smith.me.uk.
* Distributed under the terms of the MIT License.
*
* Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
* Distributed under the terms of the NewOS License.
*/
#include <cpu.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <ACPI.h>
#include <boot_device.h>
#include <commpage.h>
#include <debug.h>
#include <elf.h>
#include <safemode.h>
#include <smp.h>
#include <util/BitUtils.h>
#include <vm/vm.h>
#include <vm/vm_types.h>
#include <vm/VMAddressSpace.h>
#include <arch_system_info.h>
#include <arch/x86/apic.h>
#include <boot/kernel_args.h>
#include "paging/X86PagingStructures.h"
#include "paging/X86VMTranslationMap.h"
#define DUMP_FEATURE_STRING 1
#define DUMP_CPU_TOPOLOGY 1
#define DUMP_CPU_PATCHLEVEL 1
/* cpu vendor info */
struct cpu_vendor_info {
const char *vendor;
const char *ident_string[2];
};
static const struct cpu_vendor_info vendor_info[VENDOR_NUM] = {
{ "Intel", { "GenuineIntel" } },
{ "AMD", { "AuthenticAMD" } },
{ "Cyrix", { "CyrixInstead" } },
{ "UMC", { "UMC UMC UMC" } },
{ "NexGen", { "NexGenDriven" } },
{ "Centaur", { "CentaurHauls" } },
{ "Rise", { "RiseRiseRise" } },
{ "Transmeta", { "GenuineTMx86", "TransmetaCPU" } },
{ "NSC", { "Geode by NSC" } },
};
#define K8_SMIONCMPHALT (1ULL << 27)
#define K8_C1EONCMPHALT (1ULL << 28)
#define K8_CMPHALT (K8_SMIONCMPHALT | K8_C1EONCMPHALT)
struct set_mtrr_parameter {
int32 index;
uint64 base;
uint64 length;
uint8 type;
};
struct set_mtrrs_parameter {
const x86_mtrr_info* infos;
uint32 count;
uint8 defaultType;
};
#ifdef __x86_64__
extern addr_t _stac;
extern addr_t _clac;
#endif
extern "C" void x86_reboot(void);
// from arch.S
void (*gCpuIdleFunc)(void);
#ifndef __x86_64__
void (*gX86SwapFPUFunc)(void* oldState, const void* newState) = x86_noop_swap;
bool gHasSSE = false;
#endif
static uint32 sCpuRendezvous;
static uint32 sCpuRendezvous2;
static uint32 sCpuRendezvous3;
static vint32 sTSCSyncRendezvous;
/* Some specials for the double fault handler */
static uint8* sDoubleFaultStacks;
static const size_t kDoubleFaultStackSize = 4096; // size per CPU
static x86_cpu_module_info* sCpuModule;
/* CPU topology information */
static uint32 (*sGetCPUTopologyID)(int currentCPU);
static uint32 sHierarchyMask[CPU_TOPOLOGY_LEVELS];
static uint32 sHierarchyShift[CPU_TOPOLOGY_LEVELS];
/* Cache topology information */
static uint32 sCacheSharingMask[CPU_MAX_CACHE_LEVEL];
static void* sUcodeData = NULL;
static size_t sUcodeDataSize = 0;
static struct intel_microcode_header* sLoadedUcodeUpdate;
static spinlock sUcodeUpdateLock = B_SPINLOCK_INITIALIZER;
static status_t
acpi_shutdown(bool rebootSystem)
{
if (debug_debugger_running() || !are_interrupts_enabled())
return B_ERROR;
acpi_module_info* acpi;
if (get_module(B_ACPI_MODULE_NAME, (module_info**)&acpi) != B_OK)
return B_NOT_SUPPORTED;
status_t status;
if (rebootSystem) {
status = acpi->reboot();
} else {
status = acpi->prepare_sleep_state(ACPI_POWER_STATE_OFF, NULL, 0);
if (status == B_OK) {
//cpu_status state = disable_interrupts();
status = acpi->enter_sleep_state(ACPI_POWER_STATE_OFF);
//restore_interrupts(state);
}
}
put_module(B_ACPI_MODULE_NAME);
return status;
}
/*! Disable CPU caches, and invalidate them. */
static void
disable_caches()
{
x86_write_cr0((x86_read_cr0() | CR0_CACHE_DISABLE)
& ~CR0_NOT_WRITE_THROUGH);
wbinvd();
arch_cpu_global_TLB_invalidate();
}
/*! Invalidate CPU caches, and enable them. */
static void
enable_caches()
{
wbinvd();
arch_cpu_global_TLB_invalidate();
x86_write_cr0(x86_read_cr0()
& ~(CR0_CACHE_DISABLE | CR0_NOT_WRITE_THROUGH));
}
static void
set_mtrr(void* _parameter, int cpu)
{
struct set_mtrr_parameter* parameter
= (struct set_mtrr_parameter*)_parameter;
// wait until all CPUs have arrived here
smp_cpu_rendezvous(&sCpuRendezvous);
// One CPU has to reset sCpuRendezvous3 -- it is needed to prevent the CPU
// that initiated the call_all_cpus() from doing that again and clearing
// sCpuRendezvous2 before the last CPU has actually left the loop in
// smp_cpu_rendezvous();
if (cpu == 0)
atomic_set((int32*)&sCpuRendezvous3, 0);
disable_caches();
sCpuModule->set_mtrr(parameter->index, parameter->base, parameter->length,
parameter->type);
enable_caches();
// wait until all CPUs have arrived here
smp_cpu_rendezvous(&sCpuRendezvous2);
smp_cpu_rendezvous(&sCpuRendezvous3);
}
static void
set_mtrrs(void* _parameter, int cpu)
{
set_mtrrs_parameter* parameter = (set_mtrrs_parameter*)_parameter;
// wait until all CPUs have arrived here
smp_cpu_rendezvous(&sCpuRendezvous);
// One CPU has to reset sCpuRendezvous3 -- it is needed to prevent the CPU
// that initiated the call_all_cpus() from doing that again and clearing
// sCpuRendezvous2 before the last CPU has actually left the loop in
// smp_cpu_rendezvous();
if (cpu == 0)
atomic_set((int32*)&sCpuRendezvous3, 0);
disable_caches();
sCpuModule->set_mtrrs(parameter->defaultType, parameter->infos,
parameter->count);
enable_caches();
// wait until all CPUs have arrived here
smp_cpu_rendezvous(&sCpuRendezvous2);
smp_cpu_rendezvous(&sCpuRendezvous3);
}
static void
init_mtrrs(void* _unused, int cpu)
{
// wait until all CPUs have arrived here
smp_cpu_rendezvous(&sCpuRendezvous);
// One CPU has to reset sCpuRendezvous3 -- it is needed to prevent the CPU
// that initiated the call_all_cpus() from doing that again and clearing
// sCpuRendezvous2 before the last CPU has actually left the loop in
// smp_cpu_rendezvous();
if (cpu == 0)
atomic_set((int32*)&sCpuRendezvous3, 0);
disable_caches();
sCpuModule->init_mtrrs();
enable_caches();
// wait until all CPUs have arrived here
smp_cpu_rendezvous(&sCpuRendezvous2);
smp_cpu_rendezvous(&sCpuRendezvous3);
}
uint32
x86_count_mtrrs(void)
{
if (sCpuModule == NULL)
return 0;
return sCpuModule->count_mtrrs();
}
void
x86_set_mtrr(uint32 index, uint64 base, uint64 length, uint8 type)
{
struct set_mtrr_parameter parameter;
parameter.index = index;
parameter.base = base;
parameter.length = length;
parameter.type = type;
sCpuRendezvous = sCpuRendezvous2 = 0;
call_all_cpus(&set_mtrr, ¶meter);
}
status_t
x86_get_mtrr(uint32 index, uint64* _base, uint64* _length, uint8* _type)
{
// the MTRRs are identical on all CPUs, so it doesn't matter
// on which CPU this runs
return sCpuModule->get_mtrr(index, _base, _length, _type);
}
void
x86_set_mtrrs(uint8 defaultType, const x86_mtrr_info* infos, uint32 count)
{
if (sCpuModule == NULL)
return;
struct set_mtrrs_parameter parameter;
parameter.defaultType = defaultType;
parameter.infos = infos;
parameter.count = count;
sCpuRendezvous = sCpuRendezvous2 = 0;
call_all_cpus(&set_mtrrs, ¶meter);
}
void
x86_init_fpu(void)
{
// All x86_64 CPUs support SSE, don't need to bother checking for it.
#ifndef __x86_64__
if (!x86_check_feature(IA32_FEATURE_FPU, FEATURE_COMMON)) {
// No FPU... time to install one in your 386?
dprintf("%s: Warning: CPU has no reported FPU.\n", __func__);
gX86SwapFPUFunc = x86_noop_swap;
return;
}
if (!x86_check_feature(IA32_FEATURE_SSE, FEATURE_COMMON)
|| !x86_check_feature(IA32_FEATURE_FXSR, FEATURE_COMMON)) {
dprintf("%s: CPU has no SSE... just enabling FPU.\n", __func__);
// we don't have proper SSE support, just enable FPU
x86_write_cr0(x86_read_cr0() & ~(CR0_FPU_EMULATION | CR0_MONITOR_FPU));
gX86SwapFPUFunc = x86_fnsave_swap;
return;
}
#endif
dprintf("%s: CPU has SSE... enabling FXSR and XMM.\n", __func__);
#ifndef __x86_64__
// enable OS support for SSE
x86_write_cr4(x86_read_cr4() | CR4_OS_FXSR | CR4_OS_XMM_EXCEPTION);
x86_write_cr0(x86_read_cr0() & ~(CR0_FPU_EMULATION | CR0_MONITOR_FPU));
gX86SwapFPUFunc = x86_fxsave_swap;
gHasSSE = true;
#endif
}
#if DUMP_FEATURE_STRING
static void
dump_feature_string(int currentCPU, cpu_ent* cpu)
{
char features[512];
features[0] = 0;
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_FPU)
strlcat(features, "fpu ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_VME)
strlcat(features, "vme ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_DE)
strlcat(features, "de ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_PSE)
strlcat(features, "pse ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_TSC)
strlcat(features, "tsc ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_MSR)
strlcat(features, "msr ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_PAE)
strlcat(features, "pae ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_MCE)
strlcat(features, "mce ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_CX8)
strlcat(features, "cx8 ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_APIC)
strlcat(features, "apic ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_SEP)
strlcat(features, "sep ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_MTRR)
strlcat(features, "mtrr ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_PGE)
strlcat(features, "pge ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_MCA)
strlcat(features, "mca ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_CMOV)
strlcat(features, "cmov ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_PAT)
strlcat(features, "pat ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_PSE36)
strlcat(features, "pse36 ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_PSN)
strlcat(features, "psn ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_CLFSH)
strlcat(features, "clfsh ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_DS)
strlcat(features, "ds ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_ACPI)
strlcat(features, "acpi ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_MMX)
strlcat(features, "mmx ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_FXSR)
strlcat(features, "fxsr ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_SSE)
strlcat(features, "sse ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_SSE2)
strlcat(features, "sse2 ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_SS)
strlcat(features, "ss ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_HTT)
strlcat(features, "htt ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_TM)
strlcat(features, "tm ", sizeof(features));
if (cpu->arch.feature[FEATURE_COMMON] & IA32_FEATURE_PBE)
strlcat(features, "pbe ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_SSE3)
strlcat(features, "sse3 ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_PCLMULQDQ)
strlcat(features, "pclmulqdq ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_DTES64)
strlcat(features, "dtes64 ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_MONITOR)
strlcat(features, "monitor ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_DSCPL)
strlcat(features, "dscpl ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_VMX)
strlcat(features, "vmx ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_SMX)
strlcat(features, "smx ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_EST)
strlcat(features, "est ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_TM2)
strlcat(features, "tm2 ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_SSSE3)
strlcat(features, "ssse3 ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_CNXTID)
strlcat(features, "cnxtid ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_FMA)
strlcat(features, "fma ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_CX16)
strlcat(features, "cx16 ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_XTPR)
strlcat(features, "xtpr ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_PDCM)
strlcat(features, "pdcm ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_PCID)
strlcat(features, "pcid ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_DCA)
strlcat(features, "dca ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_SSE4_1)
strlcat(features, "sse4_1 ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_SSE4_2)
strlcat(features, "sse4_2 ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_X2APIC)
strlcat(features, "x2apic ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_MOVBE)
strlcat(features, "movbe ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_POPCNT)
strlcat(features, "popcnt ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_TSCDEADLINE)
strlcat(features, "tscdeadline ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_AES)
strlcat(features, "aes ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_XSAVE)
strlcat(features, "xsave ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_OSXSAVE)
strlcat(features, "osxsave ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_AVX)
strlcat(features, "avx ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_F16C)
strlcat(features, "f16c ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_RDRND)
strlcat(features, "rdrnd ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_HYPERVISOR)
strlcat(features, "hypervisor ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT_AMD] & IA32_FEATURE_AMD_EXT_SYSCALL)
strlcat(features, "syscall ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT_AMD] & IA32_FEATURE_AMD_EXT_NX)
strlcat(features, "nx ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT_AMD] & IA32_FEATURE_AMD_EXT_MMXEXT)
strlcat(features, "mmxext ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT_AMD] & IA32_FEATURE_AMD_EXT_FFXSR)
strlcat(features, "ffxsr ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT_AMD] & IA32_FEATURE_AMD_EXT_PDPE1GB)
strlcat(features, "pdpe1gb ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT_AMD] & IA32_FEATURE_AMD_EXT_LONG)
strlcat(features, "long ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT_AMD] & IA32_FEATURE_AMD_EXT_3DNOWEXT)
strlcat(features, "3dnowext ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT_AMD] & IA32_FEATURE_AMD_EXT_3DNOW)
strlcat(features, "3dnow ", sizeof(features));
if (cpu->arch.feature[FEATURE_6_EAX] & IA32_FEATURE_DTS)
strlcat(features, "dts ", sizeof(features));
if (cpu->arch.feature[FEATURE_6_EAX] & IA32_FEATURE_ITB)
strlcat(features, "itb ", sizeof(features));
if (cpu->arch.feature[FEATURE_6_EAX] & IA32_FEATURE_ARAT)
strlcat(features, "arat ", sizeof(features));
if (cpu->arch.feature[FEATURE_6_EAX] & IA32_FEATURE_PLN)
strlcat(features, "pln ", sizeof(features));
if (cpu->arch.feature[FEATURE_6_EAX] & IA32_FEATURE_ECMD)
strlcat(features, "ecmd ", sizeof(features));
if (cpu->arch.feature[FEATURE_6_EAX] & IA32_FEATURE_PTM)
strlcat(features, "ptm ", sizeof(features));
if (cpu->arch.feature[FEATURE_6_ECX] & IA32_FEATURE_APERFMPERF)
strlcat(features, "aperfmperf ", sizeof(features));
if (cpu->arch.feature[FEATURE_6_ECX] & IA32_FEATURE_EPB)
strlcat(features, "epb ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_TSC_ADJUST)
strlcat(features, "tsc_adjust ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_SGX)
strlcat(features, "sgx ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_BMI1)
strlcat(features, "bmi1 ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_HLE)
strlcat(features, "hle ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_AVX2)
strlcat(features, "avx2 ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_SMEP)
strlcat(features, "smep ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_BMI2)
strlcat(features, "bmi2 ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_ERMS)
strlcat(features, "erms ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_INVPCID)
strlcat(features, "invpcid ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_RTM)
strlcat(features, "rtm ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_CQM)
strlcat(features, "cqm ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_MPX)
strlcat(features, "mpx ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_RDT_A)
strlcat(features, "rdt_a ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_AVX512F)
strlcat(features, "avx512f ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_AVX512DQ)
strlcat(features, "avx512dq ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_RDSEED)
strlcat(features, "rdseed ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_ADX)
strlcat(features, "adx ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_SMAP)
strlcat(features, "smap ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_AVX512IFMA)
strlcat(features, "avx512ifma ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_PCOMMIT)
strlcat(features, "pcommit ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_CLFLUSHOPT)
strlcat(features, "cflushopt ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_CLWB)
strlcat(features, "clwb ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_INTEL_PT)
strlcat(features, "intel_pt ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_AVX512PF)
strlcat(features, "avx512pf ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_AVX512ER)
strlcat(features, "avx512er ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_AVX512CD)
strlcat(features, "avx512cd ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_SHA_NI)
strlcat(features, "sha_ni ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_AVX512BW)
strlcat(features, "avx512bw ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EBX] & IA32_FEATURE_AVX512VI)
strlcat(features, "avx512vi ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EDX] & IA32_FEATURE_IBRS)
strlcat(features, "ibrs ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EDX] & IA32_FEATURE_STIBP)
strlcat(features, "stibp ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EDX] & IA32_FEATURE_L1D_FLUSH)
strlcat(features, "l1d_flush ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EDX] & IA32_FEATURE_ARCH_CAPABILITIES)
strlcat(features, "msr_arch ", sizeof(features));
if (cpu->arch.feature[FEATURE_7_EDX] & IA32_FEATURE_SSBD)
strlcat(features, "ssbd ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT_8_EBX] & IA32_FEATURE_CLZERO)
strlcat(features, "clzero ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT_8_EBX] & IA32_FEATURE_IBPB)
strlcat(features, "ibpb ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT_8_EBX] & IA32_FEATURE_AMD_SSBD)
strlcat(features, "amd_ssbd ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT_8_EBX] & IA32_FEATURE_VIRT_SSBD)
strlcat(features, "virt_ssbd ", sizeof(features));
if (cpu->arch.feature[FEATURE_EXT_8_EBX] & IA32_FEATURE_AMD_SSB_NO)
strlcat(features, "amd_ssb_no ", sizeof(features));
dprintf("CPU %d: features: %s\n", currentCPU, features);
}
#endif // DUMP_FEATURE_STRING
static void
compute_cpu_hierarchy_masks(int maxLogicalID, int maxCoreID)
{
ASSERT(maxLogicalID >= maxCoreID);
const int kMaxSMTID = maxLogicalID / maxCoreID;
sHierarchyMask[CPU_TOPOLOGY_SMT] = kMaxSMTID - 1;
sHierarchyShift[CPU_TOPOLOGY_SMT] = 0;
sHierarchyMask[CPU_TOPOLOGY_CORE] = (maxCoreID - 1) * kMaxSMTID;
sHierarchyShift[CPU_TOPOLOGY_CORE]
= count_set_bits(sHierarchyMask[CPU_TOPOLOGY_SMT]);
const uint32 kSinglePackageMask = sHierarchyMask[CPU_TOPOLOGY_SMT]
| sHierarchyMask[CPU_TOPOLOGY_CORE];
sHierarchyMask[CPU_TOPOLOGY_PACKAGE] = ~kSinglePackageMask;
sHierarchyShift[CPU_TOPOLOGY_PACKAGE] = count_set_bits(kSinglePackageMask);
}
static uint32
get_cpu_legacy_initial_apic_id(int /* currentCPU */)
{
cpuid_info cpuid;
get_current_cpuid(&cpuid, 1, 0);
return cpuid.regs.ebx >> 24;
}
static inline status_t
detect_amd_cpu_topology(uint32 maxBasicLeaf, uint32 maxExtendedLeaf)
{
sGetCPUTopologyID = get_cpu_legacy_initial_apic_id;
cpuid_info cpuid;
get_current_cpuid(&cpuid, 1, 0);
int maxLogicalID = next_power_of_2((cpuid.regs.ebx >> 16) & 0xff);
int maxCoreID = 1;
if (maxExtendedLeaf >= 0x80000008) {
get_current_cpuid(&cpuid, 0x80000008, 0);
maxCoreID = (cpuid.regs.ecx >> 12) & 0xf;
if (maxCoreID != 0)
maxCoreID = 1 << maxCoreID;
else
maxCoreID = next_power_of_2((cpuid.regs.edx & 0xf) + 1);
}
if (maxExtendedLeaf >= 0x80000001) {
get_current_cpuid(&cpuid, 0x80000001, 0);
if (x86_check_feature(IA32_FEATURE_AMD_EXT_CMPLEGACY,
FEATURE_EXT_AMD_ECX))
maxCoreID = maxLogicalID;
}
compute_cpu_hierarchy_masks(maxLogicalID, maxCoreID);
return B_OK;
}
static void
detect_amd_cache_topology(uint32 maxExtendedLeaf)
{
if (!x86_check_feature(IA32_FEATURE_AMD_EXT_TOPOLOGY, FEATURE_EXT_AMD_ECX))
return;
if (maxExtendedLeaf < 0x8000001d)
return;
uint8 hierarchyLevels[CPU_MAX_CACHE_LEVEL];
int maxCacheLevel = 0;
int currentLevel = 0;
int cacheType;
do {
cpuid_info cpuid;
get_current_cpuid(&cpuid, 0x8000001d, currentLevel);
cacheType = cpuid.regs.eax & 0x1f;
if (cacheType == 0)
break;
int cacheLevel = (cpuid.regs.eax >> 5) & 0x7;
int coresCount = next_power_of_2(((cpuid.regs.eax >> 14) & 0x3f) + 1);
hierarchyLevels[cacheLevel - 1]
= coresCount * (sHierarchyMask[CPU_TOPOLOGY_SMT] + 1);
maxCacheLevel = std::max(maxCacheLevel, cacheLevel);
currentLevel++;
} while (true);
for (int i = 0; i < maxCacheLevel; i++)
sCacheSharingMask[i] = ~uint32(hierarchyLevels[i] - 1);
gCPUCacheLevelCount = maxCacheLevel;
}
static uint32
get_intel_cpu_initial_x2apic_id(int /* currentCPU */)
{
cpuid_info cpuid;
get_current_cpuid(&cpuid, 11, 0);
return cpuid.regs.edx;
}
static inline status_t
detect_intel_cpu_topology_x2apic(uint32 maxBasicLeaf)
{
if (maxBasicLeaf < 11)
return B_UNSUPPORTED;
uint8 hierarchyLevels[CPU_TOPOLOGY_LEVELS] = { 0 };
int currentLevel = 0;
int levelType;
unsigned int levelsSet = 0;
do {
cpuid_info cpuid;
get_current_cpuid(&cpuid, 11, currentLevel);
if (currentLevel == 0 && cpuid.regs.ebx == 0)
return B_UNSUPPORTED;
levelType = (cpuid.regs.ecx >> 8) & 0xff;
int levelValue = cpuid.regs.eax & 0x1f;
switch (levelType) {
case 1: // SMT
hierarchyLevels[CPU_TOPOLOGY_SMT] = levelValue;
levelsSet |= 1;
break;
case 2: // core
hierarchyLevels[CPU_TOPOLOGY_CORE] = levelValue;
levelsSet |= 2;
break;
}
currentLevel++;
} while (levelType != 0 && levelsSet != 3);
sGetCPUTopologyID = get_intel_cpu_initial_x2apic_id;
for (int i = 1; i < CPU_TOPOLOGY_LEVELS; i++) {
if ((levelsSet & (1u << i)) != 0)
continue;
hierarchyLevels[i] = hierarchyLevels[i - 1];
}
for (int i = 0; i < CPU_TOPOLOGY_LEVELS; i++) {
uint32 mask = ~uint32(0);
if (i < CPU_TOPOLOGY_LEVELS - 1)
mask = (1u << hierarchyLevels[i]) - 1;
if (i > 0)
mask &= ~sHierarchyMask[i - 1];
sHierarchyMask[i] = mask;
sHierarchyShift[i] = i > 0 ? hierarchyLevels[i - 1] : 0;
}
return B_OK;
}
static inline status_t
detect_intel_cpu_topology_legacy(uint32 maxBasicLeaf)
{
sGetCPUTopologyID = get_cpu_legacy_initial_apic_id;
cpuid_info cpuid;
get_current_cpuid(&cpuid, 1, 0);
int maxLogicalID = next_power_of_2((cpuid.regs.ebx >> 16) & 0xff);
int maxCoreID = 1;
if (maxBasicLeaf >= 4) {
get_current_cpuid(&cpuid, 4, 0);
maxCoreID = next_power_of_2((cpuid.regs.eax >> 26) + 1);
}
compute_cpu_hierarchy_masks(maxLogicalID, maxCoreID);
return B_OK;
}
static void
detect_intel_cache_topology(uint32 maxBasicLeaf)
{
if (maxBasicLeaf < 4)
return;
uint8 hierarchyLevels[CPU_MAX_CACHE_LEVEL];
int maxCacheLevel = 0;
int currentLevel = 0;
int cacheType;
do {
cpuid_info cpuid;
get_current_cpuid(&cpuid, 4, currentLevel);
cacheType = cpuid.regs.eax & 0x1f;
if (cacheType == 0)
break;
int cacheLevel = (cpuid.regs.eax >> 5) & 0x7;
hierarchyLevels[cacheLevel - 1]
= next_power_of_2(((cpuid.regs.eax >> 14) & 0x3f) + 1);
maxCacheLevel = std::max(maxCacheLevel, cacheLevel);
currentLevel++;
} while (true);
for (int i = 0; i < maxCacheLevel; i++)
sCacheSharingMask[i] = ~uint32(hierarchyLevels[i] - 1);
gCPUCacheLevelCount = maxCacheLevel;
}
static uint32
get_simple_cpu_topology_id(int currentCPU)
{
return currentCPU;
}
static inline int
get_topology_level_id(uint32 id, cpu_topology_level level)
{
ASSERT(level < CPU_TOPOLOGY_LEVELS);
return (id & sHierarchyMask[level]) >> sHierarchyShift[level];
}
static void
detect_cpu_topology(int currentCPU, cpu_ent* cpu, uint32 maxBasicLeaf,
uint32 maxExtendedLeaf)
{
if (currentCPU == 0) {
memset(sCacheSharingMask, 0xff, sizeof(sCacheSharingMask));
status_t result = B_UNSUPPORTED;
if (x86_check_feature(IA32_FEATURE_HTT, FEATURE_COMMON)) {
if (cpu->arch.vendor == VENDOR_AMD) {
result = detect_amd_cpu_topology(maxBasicLeaf, maxExtendedLeaf);
if (result == B_OK)
detect_amd_cache_topology(maxExtendedLeaf);
}
if (cpu->arch.vendor == VENDOR_INTEL) {
result = detect_intel_cpu_topology_x2apic(maxBasicLeaf);
if (result != B_OK)
result = detect_intel_cpu_topology_legacy(maxBasicLeaf);
if (result == B_OK)
detect_intel_cache_topology(maxBasicLeaf);
}
}
if (result != B_OK) {
dprintf("No CPU topology information available.\n");
sGetCPUTopologyID = get_simple_cpu_topology_id;
sHierarchyMask[CPU_TOPOLOGY_PACKAGE] = ~uint32(0);
}
}
ASSERT(sGetCPUTopologyID != NULL);
int topologyID = sGetCPUTopologyID(currentCPU);
cpu->topology_id[CPU_TOPOLOGY_SMT]
= get_topology_level_id(topologyID, CPU_TOPOLOGY_SMT);
cpu->topology_id[CPU_TOPOLOGY_CORE]
= get_topology_level_id(topologyID, CPU_TOPOLOGY_CORE);
cpu->topology_id[CPU_TOPOLOGY_PACKAGE]
= get_topology_level_id(topologyID, CPU_TOPOLOGY_PACKAGE);
unsigned int i;
for (i = 0; i < gCPUCacheLevelCount; i++)
cpu->cache_id[i] = topologyID & sCacheSharingMask[i];
for (; i < CPU_MAX_CACHE_LEVEL; i++)
cpu->cache_id[i] = -1;
#if DUMP_CPU_TOPOLOGY
dprintf("CPU %d: apic id %d, package %d, core %d, smt %d\n", currentCPU,
topologyID, cpu->topology_id[CPU_TOPOLOGY_PACKAGE],
cpu->topology_id[CPU_TOPOLOGY_CORE],
cpu->topology_id[CPU_TOPOLOGY_SMT]);
if (gCPUCacheLevelCount > 0) {
char cacheLevels[256];
unsigned int offset = 0;
for (i = 0; i < gCPUCacheLevelCount; i++) {
offset += snprintf(cacheLevels + offset,
sizeof(cacheLevels) - offset,
" L%d id %d%s", i + 1, cpu->cache_id[i],
i < gCPUCacheLevelCount - 1 ? "," : "");
if (offset >= sizeof(cacheLevels))
break;
}
dprintf("CPU %d: cache sharing:%s\n", currentCPU, cacheLevels);
}
#endif
}
static void
detect_intel_patch_level(cpu_ent* cpu)
{
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_HYPERVISOR) {
cpu->arch.patch_level = 0;
return;
}
x86_write_msr(IA32_MSR_UCODE_REV, 0);
cpuid_info cpuid;
get_current_cpuid(&cpuid, 1, 0);
uint64 value = x86_read_msr(IA32_MSR_UCODE_REV);
cpu->arch.patch_level = value >> 32;
}
static void
detect_amd_patch_level(cpu_ent* cpu)
{
if (cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_HYPERVISOR) {
cpu->arch.patch_level = 0;
return;
}
uint64 value = x86_read_msr(IA32_MSR_UCODE_REV);
cpu->arch.patch_level = value >> 32;
}
static struct intel_microcode_header*
find_microcode_intel(addr_t data, size_t size, uint32 patchLevel)
{
// 9.11.3 Processor Identification
cpuid_info cpuid;
get_current_cpuid(&cpuid, 1, 0);
uint32 signature = cpuid.regs.eax;
// 9.11.4 Platform Identification
uint64 platformBits = (x86_read_msr(IA32_MSR_PLATFORM_ID) >> 50) & 0x7;
uint64 mask = 1 << platformBits;
while (size > 0) {
if (size < sizeof(struct intel_microcode_header)) {
dprintf("find_microcode_intel update is too small for header\n");
break;
}
struct intel_microcode_header* header =
(struct intel_microcode_header*)data;
uint32 totalSize = header->total_size;
uint32 dataSize = header->data_size;
if (dataSize == 0) {
dataSize = 2000;
totalSize = sizeof(struct intel_microcode_header)
+ dataSize;
}
if (totalSize > size) {
dprintf("find_microcode_intel update is too small for data\n");
break;
}
uint32* dwords = (uint32*)data;
// prepare the next update
size -= totalSize;
data += totalSize;
if (header->loader_revision != 1) {
dprintf("find_microcode_intel incorrect loader version\n");
continue;
}
// 9.11.6 The microcode update data requires a 16-byte boundary
// alignment.
if (((addr_t)header % 16) != 0) {
dprintf("find_microcode_intel incorrect alignment\n");
continue;
}
uint32 sum = 0;
for (uint32 i = 0; i < totalSize / 4; i++) {
sum += dwords[i];
}
if (sum != 0) {
dprintf("find_microcode_intel incorrect checksum\n");
continue;
}
if (patchLevel > header->update_revision) {
dprintf("find_microcode_intel update_revision is lower\n");
continue;
}
if (signature == header->processor_signature
&& (mask & header->processor_flags) != 0) {
return header;
}
if (totalSize <= (sizeof(struct intel_microcode_header) + dataSize
+ sizeof(struct intel_microcode_extended_signature_header))) {
continue;
}
struct intel_microcode_extended_signature_header* extSigHeader =
(struct intel_microcode_extended_signature_header*)((addr_t)header
+ sizeof(struct intel_microcode_header) + dataSize);
struct intel_microcode_extended_signature* extended_signature =
(struct intel_microcode_extended_signature*)((addr_t)extSigHeader
+ sizeof(struct intel_microcode_extended_signature_header));
for (uint32 i = 0; i < extSigHeader->extended_signature_count; i++) {
if (signature == extended_signature[i].processor_signature
&& (mask & extended_signature[i].processor_flags) != 0)
return header;
}
}
return NULL;
}
static void
load_microcode_intel(int currentCPU, cpu_ent* cpu)
{
// serialize for HT cores
if (currentCPU != 0)
acquire_spinlock(&sUcodeUpdateLock);
detect_intel_patch_level(cpu);
uint32 revision = cpu->arch.patch_level;
struct intel_microcode_header* update = sLoadedUcodeUpdate;
if (update == NULL) {
update = find_microcode_intel((addr_t)sUcodeData, sUcodeDataSize,
revision);
}
if (update != NULL) {
addr_t data = (addr_t)update + sizeof(struct intel_microcode_header);
wbinvd();
x86_write_msr(IA32_MSR_UCODE_WRITE, data);
detect_intel_patch_level(cpu);
if (revision == cpu->arch.patch_level) {
dprintf("CPU %d: update failed\n", currentCPU);
} else {
if (sLoadedUcodeUpdate == NULL)
sLoadedUcodeUpdate = update;
dprintf("CPU %d: updated from revision %" B_PRIu32 " to %" B_PRIu32
"\n", currentCPU, revision, cpu->arch.patch_level);
}
} else {
dprintf("CPU %d: no update found\n", currentCPU);
}
if (currentCPU != 0)
release_spinlock(&sUcodeUpdateLock);
}
static void
load_microcode_amd(int currentCPU, cpu_ent* cpu)
{
dprintf("CPU %d: no update found\n", currentCPU);
}
static void
load_microcode(int currentCPU)
{
if (sUcodeData == NULL)
return;
cpu_ent* cpu = get_cpu_struct();
if ((cpu->arch.feature[FEATURE_EXT] & IA32_FEATURE_EXT_HYPERVISOR) != 0)
return;
if (cpu->arch.vendor == VENDOR_INTEL)
load_microcode_intel(currentCPU, cpu);
else if (cpu->arch.vendor == VENDOR_AMD)
load_microcode_amd(currentCPU, cpu);
}
static void
detect_cpu(int currentCPU)
{
cpu_ent* cpu = get_cpu_struct();
char vendorString[17];
cpuid_info cpuid;
// clear out the cpu info data
cpu->arch.vendor = VENDOR_UNKNOWN;
cpu->arch.vendor_name = "UNKNOWN VENDOR";
cpu->arch.feature[FEATURE_COMMON] = 0;
cpu->arch.feature[FEATURE_EXT] = 0;
cpu->arch.feature[FEATURE_EXT_AMD] = 0;
cpu->arch.feature[FEATURE_7_EBX] = 0;
cpu->arch.feature[FEATURE_7_ECX] = 0;
cpu->arch.feature[FEATURE_7_EDX] = 0;
cpu->arch.model_name[0] = 0;
// print some fun data
get_current_cpuid(&cpuid, 0, 0);
uint32 maxBasicLeaf = cpuid.eax_0.max_eax;
// build the vendor string
memset(vendorString, 0, sizeof(vendorString));
memcpy(vendorString, cpuid.eax_0.vendor_id, sizeof(cpuid.eax_0.vendor_id));
// get the family, model, stepping
get_current_cpuid(&cpuid, 1, 0);
cpu->arch.type = cpuid.eax_1.type;
cpu->arch.family = cpuid.eax_1.family;
cpu->arch.extended_family = cpuid.eax_1.extended_family;
cpu->arch.model = cpuid.eax_1.model;
cpu->arch.extended_model = cpuid.eax_1.extended_model;
cpu->arch.stepping = cpuid.eax_1.stepping;
dprintf("CPU %d: type %d family %d extended_family %d model %d "
"extended_model %d stepping %d, string '%s'\n",
currentCPU, cpu->arch.type, cpu->arch.family,
cpu->arch.extended_family, cpu->arch.model,
cpu->arch.extended_model, cpu->arch.stepping, vendorString);
// figure out what vendor we have here
for (int32 i = 0; i < VENDOR_NUM; i++) {
if (vendor_info[i].ident_string[0]
&& !strcmp(vendorString, vendor_info[i].ident_string[0])) {
cpu->arch.vendor = (x86_vendors)i;
cpu->arch.vendor_name = vendor_info[i].vendor;
break;
}
if (vendor_info[i].ident_string[1]
&& !strcmp(vendorString, vendor_info[i].ident_string[1])) {
cpu->arch.vendor = (x86_vendors)i;
cpu->arch.vendor_name = vendor_info[i].vendor;
break;
}
}
// see if we can get the model name
get_current_cpuid(&cpuid, 0x80000000, 0);
uint32 maxExtendedLeaf = cpuid.eax_0.max_eax;
if (maxExtendedLeaf >= 0x80000004) {
// build the model string (need to swap ecx/edx data before copying)
unsigned int temp;
memset(cpu->arch.model_name, 0, sizeof(cpu->arch.model_name));
get_current_cpuid(&cpuid, 0x80000002, 0);
temp = cpuid.regs.edx;
cpuid.regs.edx = cpuid.regs.ecx;
cpuid.regs.ecx = temp;
memcpy(cpu->arch.model_name, cpuid.as_chars, sizeof(cpuid.as_chars));
get_current_cpuid(&cpuid, 0x80000003, 0);
temp = cpuid.regs.edx;
cpuid.regs.edx = cpuid.regs.ecx;
cpuid.regs.ecx = temp;
memcpy(cpu->arch.model_name + 16, cpuid.as_chars,
sizeof(cpuid.as_chars));
get_current_cpuid(&cpuid, 0x80000004, 0);
temp = cpuid.regs.edx;
cpuid.regs.edx = cpuid.regs.ecx;
cpuid.regs.ecx = temp;
memcpy(cpu->arch.model_name + 32, cpuid.as_chars,
sizeof(cpuid.as_chars));
// some cpus return a right-justified string
int32 i = 0;
while (cpu->arch.model_name[i] == ' ')
i++;
if (i > 0) {
memmove(cpu->arch.model_name, &cpu->arch.model_name[i],
strlen(&cpu->arch.model_name[i]) + 1);
}
dprintf("CPU %d: vendor '%s' model name '%s'\n",
currentCPU, cpu->arch.vendor_name, cpu->arch.model_name);
} else {
strlcpy(cpu->arch.model_name, "unknown", sizeof(cpu->arch.model_name));
}
// load feature bits
get_current_cpuid(&cpuid, 1, 0);
cpu->arch.feature[FEATURE_COMMON] = cpuid.eax_1.features; // edx
cpu->arch.feature[FEATURE_EXT] = cpuid.eax_1.extended_features; // ecx
if (maxExtendedLeaf >= 0x80000001) {
get_current_cpuid(&cpuid, 0x80000001, 0);
if (cpu->arch.vendor == VENDOR_AMD)
cpu->arch.feature[FEATURE_EXT_AMD_ECX] = cpuid.regs.ecx; // ecx
cpu->arch.feature[FEATURE_EXT_AMD] = cpuid.regs.edx; // edx
if (cpu->arch.vendor != VENDOR_AMD)
cpu->arch.feature[FEATURE_EXT_AMD] &= IA32_FEATURES_INTEL_EXT;
}
if (maxBasicLeaf >= 5) {
get_current_cpuid(&cpuid, 5, 0);
cpu->arch.feature[FEATURE_5_ECX] = cpuid.regs.ecx;
}
if (maxBasicLeaf >= 6) {
get_current_cpuid(&cpuid, 6, 0);
cpu->arch.feature[FEATURE_6_EAX] = cpuid.regs.eax;
cpu->arch.feature[FEATURE_6_ECX] = cpuid.regs.ecx;
}
if (maxBasicLeaf >= 7) {
get_current_cpuid(&cpuid, 7, 0);
cpu->arch.feature[FEATURE_7_EBX] = cpuid.regs.ebx;
cpu->arch.feature[FEATURE_7_ECX] = cpuid.regs.ecx;
cpu->arch.feature[FEATURE_7_EDX] = cpuid.regs.edx;
}
if (maxExtendedLeaf >= 0x80000007) {
get_current_cpuid(&cpuid, 0x80000007, 0);
cpu->arch.feature[FEATURE_EXT_7_EDX] = cpuid.regs.edx;
}
if (maxExtendedLeaf >= 0x80000008) {
get_current_cpuid(&cpuid, 0x80000008, 0);
cpu->arch.feature[FEATURE_EXT_8_EBX] = cpuid.regs.ebx;
}
detect_cpu_topology(currentCPU, cpu, maxBasicLeaf, maxExtendedLeaf);
if (cpu->arch.vendor == VENDOR_INTEL)
detect_intel_patch_level(cpu);
else if (cpu->arch.vendor == VENDOR_AMD)
detect_amd_patch_level(cpu);
#if DUMP_FEATURE_STRING
dump_feature_string(currentCPU, cpu);
#endif
#if DUMP_CPU_PATCHLEVEL
dprintf("CPU %d: patch_level %" B_PRIu32 "\n", currentCPU,
cpu->arch.patch_level);
#endif
}
bool
x86_check_feature(uint32 feature, enum x86_feature_type type)
{
cpu_ent* cpu = get_cpu_struct();
#if 0
int i;
dprintf("x86_check_feature: feature 0x%x, type %d\n", feature, type);
for (i = 0; i < FEATURE_NUM; i++) {
dprintf("features %d: 0x%x\n", i, cpu->arch.feature[i]);
}
#endif
return (cpu->arch.feature[type] & feature) != 0;
}
void*
x86_get_double_fault_stack(int32 cpu, size_t* _size)
{
*_size = kDoubleFaultStackSize;
return sDoubleFaultStacks + kDoubleFaultStackSize * cpu;
}
/*! Returns the index of the current CPU. Can only be called from the double
fault handler.
*/
int32
x86_double_fault_get_cpu(void)
{
addr_t stack = x86_get_stack_frame();
return (stack - (addr_t)sDoubleFaultStacks) / kDoubleFaultStackSize;
}
// #pragma mark -
status_t
arch_cpu_preboot_init_percpu(kernel_args* args, int cpu)
{
// On SMP system we want to synchronize the CPUs' TSCs, so system_time()
// will return consistent values.
if (smp_get_num_cpus() > 1) {
// let the first CPU prepare the rendezvous point
if (cpu == 0)
sTSCSyncRendezvous = smp_get_num_cpus() - 1;
// One CPU after the other will drop out of this loop and be caught by
// the loop below, until the last CPU (0) gets there. Save for +/- a few
// cycles the CPUs should pass the second loop at the same time.
while (sTSCSyncRendezvous != cpu) {
}
sTSCSyncRendezvous = cpu - 1;
while (sTSCSyncRendezvous != -1) {
}
// reset TSC to 0
x86_write_msr(IA32_MSR_TSC, 0);
}
x86_descriptors_preboot_init_percpu(args, cpu);
return B_OK;
}
static void
halt_idle(void)
{
asm("hlt");
}
static void
amdc1e_noarat_idle(void)
{
uint64 msr = x86_read_msr(K8_MSR_IPM);
if (msr & K8_CMPHALT)
x86_write_msr(K8_MSR_IPM, msr & ~K8_CMPHALT);
halt_idle();
}
static bool
detect_amdc1e_noarat()
{
cpu_ent* cpu = get_cpu_struct();
if (cpu->arch.vendor != VENDOR_AMD)
return false;
// Family 0x12 and higher processors support ARAT
// Family lower than 0xf processors doesn't support C1E
// Family 0xf with model <= 0x40 procssors doesn't support C1E
uint32 family = cpu->arch.family + cpu->arch.extended_family;
uint32 model = (cpu->arch.extended_model << 4) | cpu->arch.model;
return (family < 0x12 && family > 0xf) || (family == 0xf && model > 0x40);
}
status_t
arch_cpu_init_percpu(kernel_args* args, int cpu)
{
load_microcode(cpu);
detect_cpu(cpu);
if (!gCpuIdleFunc) {
if (detect_amdc1e_noarat())
gCpuIdleFunc = amdc1e_noarat_idle;
else
gCpuIdleFunc = halt_idle;
}
#ifdef __x86_64__
// if RDTSCP is available write cpu number in TSC_AUX
if (x86_check_feature(IA32_FEATURE_AMD_EXT_RDTSCP, FEATURE_EXT_AMD))
x86_write_msr(IA32_MSR_TSC_AUX, cpu);
#endif
return __x86_patch_errata_percpu(cpu);
}
status_t
arch_cpu_init(kernel_args* args)
{
if (args->ucode_data != NULL
&& args->ucode_data_size > 0) {
sUcodeData = args->ucode_data;
sUcodeDataSize = args->ucode_data_size;
} else {
dprintf("CPU: no microcode provided\n");
}
// init the TSC -> system_time() conversion factors
uint32 conversionFactor = args->arch_args.system_time_cv_factor;
uint64 conversionFactorNsecs = (uint64)conversionFactor * 1000;
#ifdef __x86_64__
// The x86_64 system_time() implementation uses 64-bit multiplication and
// therefore shifting is not necessary for low frequencies (it's also not
// too likely that there'll be any x86_64 CPUs clocked under 1GHz).
__x86_setup_system_time((uint64)conversionFactor << 32,
conversionFactorNsecs);
#else
if (conversionFactorNsecs >> 32 != 0) {
// the TSC frequency is < 1 GHz, which forces us to shift the factor
__x86_setup_system_time(conversionFactor, conversionFactorNsecs >> 16,
true);
} else {
// the TSC frequency is >= 1 GHz
__x86_setup_system_time(conversionFactor, conversionFactorNsecs, false);
}
#endif
// Initialize descriptor tables.
x86_descriptors_init(args);
return B_OK;
}
#ifdef __x86_64__
static void
enable_smap(void* dummy, int cpu)
{
x86_write_cr4(x86_read_cr4() | IA32_CR4_SMAP);
}
static void
enable_smep(void* dummy, int cpu)
{
x86_write_cr4(x86_read_cr4() | IA32_CR4_SMEP);
}
#endif
status_t
arch_cpu_init_post_vm(kernel_args* args)
{
uint32 i;
// allocate an area for the double fault stacks
virtual_address_restrictions virtualRestrictions = {};
virtualRestrictions.address_specification = B_ANY_KERNEL_ADDRESS;
physical_address_restrictions physicalRestrictions = {};
create_area_etc(B_SYSTEM_TEAM, "double fault stacks",
kDoubleFaultStackSize * smp_get_num_cpus(), B_FULL_LOCK,
B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA, CREATE_AREA_DONT_WAIT, 0,
&virtualRestrictions, &physicalRestrictions,
(void**)&sDoubleFaultStacks);
X86PagingStructures* kernelPagingStructures
= static_cast<X86VMTranslationMap*>(
VMAddressSpace::Kernel()->TranslationMap())->PagingStructures();
// Set active translation map on each CPU.
for (i = 0; i < args->num_cpus; i++) {
gCPU[i].arch.active_paging_structures = kernelPagingStructures;
kernelPagingStructures->AddReference();
}
if (!apic_available())
x86_init_fpu();
// else fpu gets set up in smp code
#ifdef __x86_64__
// if available enable SMEP (Supervisor Memory Execution Protection)
if (x86_check_feature(IA32_FEATURE_SMEP, FEATURE_7_EBX)) {
if (!get_safemode_boolean(B_SAFEMODE_DISABLE_SMEP_SMAP, false)) {
dprintf("enable SMEP\n");
call_all_cpus_sync(&enable_smep, NULL);
} else
dprintf("SMEP disabled per safemode setting\n");
}
// if available enable SMAP (Supervisor Memory Access Protection)
if (x86_check_feature(IA32_FEATURE_SMAP, FEATURE_7_EBX)) {
if (!get_safemode_boolean(B_SAFEMODE_DISABLE_SMEP_SMAP, false)) {
dprintf("enable SMAP\n");
call_all_cpus_sync(&enable_smap, NULL);
arch_altcodepatch_replace(ALTCODEPATCH_TAG_STAC, &_stac, 3);
arch_altcodepatch_replace(ALTCODEPATCH_TAG_CLAC, &_clac, 3);
} else
dprintf("SMAP disabled per safemode setting\n");
}
#endif
return B_OK;
}
status_t
arch_cpu_init_post_modules(kernel_args* args)
{
// initialize CPU module
void* cookie = open_module_list("cpu");
while (true) {
char name[B_FILE_NAME_LENGTH];
size_t nameLength = sizeof(name);
if (read_next_module_name(cookie, name, &nameLength) != B_OK
|| get_module(name, (module_info**)&sCpuModule) == B_OK)
break;
}
close_module_list(cookie);
// initialize MTRRs if available
if (x86_count_mtrrs() > 0) {
sCpuRendezvous = sCpuRendezvous2 = 0;
call_all_cpus(&init_mtrrs, NULL);
}
size_t threadExitLen = (addr_t)x86_end_userspace_thread_exit
- (addr_t)x86_userspace_thread_exit;
addr_t threadExitPosition = fill_commpage_entry(
COMMPAGE_ENTRY_X86_THREAD_EXIT, (const void*)x86_userspace_thread_exit,
threadExitLen);
// add the functions to the commpage image
image_id image = get_commpage_image();
elf_add_memory_image_symbol(image, "commpage_thread_exit",
threadExitPosition, threadExitLen, B_SYMBOL_TYPE_TEXT);
return B_OK;
}
void
arch_cpu_user_TLB_invalidate(void)
{
x86_write_cr3(x86_read_cr3());
}
void
arch_cpu_global_TLB_invalidate(void)
{
uint32 flags = x86_read_cr4();
if (flags & IA32_CR4_GLOBAL_PAGES) {
// disable and reenable the global pages to flush all TLBs regardless
// of the global page bit
x86_write_cr4(flags & ~IA32_CR4_GLOBAL_PAGES);
x86_write_cr4(flags | IA32_CR4_GLOBAL_PAGES);
} else {
cpu_status state = disable_interrupts();
arch_cpu_user_TLB_invalidate();
restore_interrupts(state);
}
}
void
arch_cpu_invalidate_TLB_range(addr_t start, addr_t end)
{
int32 num_pages = end / B_PAGE_SIZE - start / B_PAGE_SIZE;
while (num_pages-- >= 0) {
invalidate_TLB(start);
start += B_PAGE_SIZE;
}
}
void
arch_cpu_invalidate_TLB_list(addr_t pages[], int num_pages)
{
int i;
for (i = 0; i < num_pages; i++) {
invalidate_TLB(pages[i]);
}
}
status_t
arch_cpu_shutdown(bool rebootSystem)
{
if (acpi_shutdown(rebootSystem) == B_OK)
return B_OK;
if (!rebootSystem) {
#ifndef __x86_64__
return apm_shutdown();
#else
return B_NOT_SUPPORTED;
#endif
}
cpu_status state = disable_interrupts();
// try to reset the system using the keyboard controller
out8(0xfe, 0x64);
// Give some time to the controller to do its job (0.5s)
snooze(500000);
// if that didn't help, try it this way
x86_reboot();
restore_interrupts(state);
return B_ERROR;
}
void
arch_cpu_sync_icache(void* address, size_t length)
{
// instruction cache is always consistent on x86
}
|