]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/Microcode.c
UefiCpuPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / Microcode.c
1 /** @file
2 Implementation of loading microcode on processors.
3
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "MpLib.h"
10
11 /**
12 Get microcode update signature of currently loaded microcode update.
13
14 @return Microcode signature.
15 **/
16 UINT32
17 GetCurrentMicrocodeSignature (
18 VOID
19 )
20 {
21 MSR_IA32_BIOS_SIGN_ID_REGISTER BiosSignIdMsr;
22
23 AsmWriteMsr64 (MSR_IA32_BIOS_SIGN_ID, 0);
24 AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, NULL);
25 BiosSignIdMsr.Uint64 = AsmReadMsr64 (MSR_IA32_BIOS_SIGN_ID);
26 return BiosSignIdMsr.Bits.MicrocodeUpdateSignature;
27 }
28
29 /**
30 Detect whether specified processor can find matching microcode patch and load it.
31
32 Microcode Payload as the following format:
33 +----------------------------------------+------------------+
34 | CPU_MICROCODE_HEADER | |
35 +----------------------------------------+ CheckSum Part1 |
36 | Microcode Binary | |
37 +----------------------------------------+------------------+
38 | CPU_MICROCODE_EXTENDED_TABLE_HEADER | |
39 +----------------------------------------+ CheckSum Part2 |
40 | CPU_MICROCODE_EXTENDED_TABLE | |
41 | ... | |
42 +----------------------------------------+------------------+
43
44 There may by multiple CPU_MICROCODE_EXTENDED_TABLE in this format.
45 The count of CPU_MICROCODE_EXTENDED_TABLE is indicated by ExtendedSignatureCount
46 of CPU_MICROCODE_EXTENDED_TABLE_HEADER structure.
47
48 When we are trying to verify the CheckSum32 with extended table.
49 We should use the fields of exnteded table to replace the corresponding
50 fields in CPU_MICROCODE_HEADER structure, and recalculate the
51 CheckSum32 with CPU_MICROCODE_HEADER + Microcode Binary. We named
52 it as CheckSum Part3.
53
54 The CheckSum Part2 is used to verify the CPU_MICROCODE_EXTENDED_TABLE_HEADER
55 and CPU_MICROCODE_EXTENDED_TABLE parts. We should make sure CheckSum Part2
56 is correct before we are going to verify each CPU_MICROCODE_EXTENDED_TABLE.
57
58 Only ProcessorSignature, ProcessorFlag and CheckSum are different between
59 CheckSum Part1 and CheckSum Part3. To avoid multiple computing CheckSum Part3.
60 Save an in-complete CheckSum32 from CheckSum Part1 for common parts.
61 When we are going to calculate CheckSum32, just should use the corresponding part
62 of the ProcessorSignature, ProcessorFlag and CheckSum with in-complete CheckSum32.
63
64 Notes: CheckSum32 is not a strong verification.
65 It does not guarantee that the data has not been modified.
66 CPU has its own mechanism to verify Microcode Binary part.
67
68 @param[in] CpuMpData The pointer to CPU MP Data structure.
69 @param[in] IsBspCallIn Indicate whether the caller is BSP or not.
70 **/
71 VOID
72 MicrocodeDetect (
73 IN CPU_MP_DATA *CpuMpData,
74 IN BOOLEAN IsBspCallIn
75 )
76 {
77 UINT32 ExtendedTableLength;
78 UINT32 ExtendedTableCount;
79 CPU_MICROCODE_EXTENDED_TABLE *ExtendedTable;
80 CPU_MICROCODE_EXTENDED_TABLE_HEADER *ExtendedTableHeader;
81 CPU_MICROCODE_HEADER *MicrocodeEntryPoint;
82 UINTN MicrocodeEnd;
83 UINTN Index;
84 UINT8 PlatformId;
85 CPUID_VERSION_INFO_EAX Eax;
86 UINT32 CurrentRevision;
87 UINT32 LatestRevision;
88 UINTN TotalSize;
89 UINT32 CheckSum32;
90 UINT32 InCompleteCheckSum32;
91 BOOLEAN CorrectMicrocode;
92 VOID *MicrocodeData;
93 MSR_IA32_PLATFORM_ID_REGISTER PlatformIdMsr;
94 UINT32 ProcessorFlags;
95 UINT32 ThreadId;
96
97 //
98 // set ProcessorFlags to suppress incorrect compiler/analyzer warnings
99 //
100 ProcessorFlags = 0;
101
102 if (CpuMpData->MicrocodePatchRegionSize == 0) {
103 //
104 // There is no microcode patches
105 //
106 return;
107 }
108
109 CurrentRevision = GetCurrentMicrocodeSignature ();
110 if (CurrentRevision != 0 && !IsBspCallIn) {
111 //
112 // Skip loading microcode if it has been loaded successfully
113 //
114 return;
115 }
116
117 GetProcessorLocationByApicId (GetInitialApicId (), NULL, NULL, &ThreadId);
118 if (ThreadId != 0) {
119 //
120 // Skip loading microcode if it is not the first thread in one core.
121 //
122 return;
123 }
124
125 ExtendedTableLength = 0;
126 //
127 // Here data of CPUID leafs have not been collected into context buffer, so
128 // GetProcessorCpuid() cannot be used here to retrieve CPUID data.
129 //
130 AsmCpuid (CPUID_VERSION_INFO, &Eax.Uint32, NULL, NULL, NULL);
131
132 //
133 // The index of platform information resides in bits 50:52 of MSR IA32_PLATFORM_ID
134 //
135 PlatformIdMsr.Uint64 = AsmReadMsr64 (MSR_IA32_PLATFORM_ID);
136 PlatformId = (UINT8) PlatformIdMsr.Bits.PlatformId;
137
138 //
139 // Check whether AP has same processor with BSP.
140 // If yes, direct use microcode info saved by BSP.
141 //
142 if (!IsBspCallIn) {
143 if ((CpuMpData->ProcessorSignature == Eax.Uint32) &&
144 (CpuMpData->ProcessorFlags & (1 << PlatformId)) != 0) {
145 MicrocodeData = (VOID *)(UINTN) CpuMpData->MicrocodeDataAddress;
146 LatestRevision = CpuMpData->MicrocodeRevision;
147 goto Done;
148 }
149 }
150
151 LatestRevision = 0;
152 MicrocodeData = NULL;
153 MicrocodeEnd = (UINTN) (CpuMpData->MicrocodePatchAddress + CpuMpData->MicrocodePatchRegionSize);
154 MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (UINTN) CpuMpData->MicrocodePatchAddress;
155
156 do {
157 //
158 // Check if the microcode is for the Cpu and the version is newer
159 // and the update can be processed on the platform
160 //
161 CorrectMicrocode = FALSE;
162
163 if (MicrocodeEntryPoint->DataSize == 0) {
164 TotalSize = sizeof (CPU_MICROCODE_HEADER) + 2000;
165 } else {
166 TotalSize = sizeof (CPU_MICROCODE_HEADER) + MicrocodeEntryPoint->DataSize;
167 }
168
169 ///
170 /// Check overflow and whether TotalSize is aligned with 4 bytes.
171 ///
172 if ( ((UINTN)MicrocodeEntryPoint + TotalSize) > MicrocodeEnd ||
173 (TotalSize & 0x3) != 0
174 ) {
175 MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (((UINTN) MicrocodeEntryPoint) + SIZE_1KB);
176 continue;
177 }
178
179 //
180 // Save an in-complete CheckSum32 from CheckSum Part1 for common parts.
181 //
182 InCompleteCheckSum32 = CalculateSum32 (
183 (UINT32 *) MicrocodeEntryPoint,
184 TotalSize
185 );
186 InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorSignature.Uint32;
187 InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorFlags;
188 InCompleteCheckSum32 -= MicrocodeEntryPoint->Checksum;
189
190 if (MicrocodeEntryPoint->HeaderVersion == 0x1) {
191 //
192 // It is the microcode header. It is not the padding data between microcode patches
193 // because the padding data should not include 0x00000001 and it should be the repeated
194 // byte format (like 0xXYXYXYXY....).
195 //
196 if (MicrocodeEntryPoint->ProcessorSignature.Uint32 == Eax.Uint32 &&
197 MicrocodeEntryPoint->UpdateRevision > LatestRevision &&
198 (MicrocodeEntryPoint->ProcessorFlags & (1 << PlatformId))
199 ) {
200 //
201 // Calculate CheckSum Part1.
202 //
203 CheckSum32 = InCompleteCheckSum32;
204 CheckSum32 += MicrocodeEntryPoint->ProcessorSignature.Uint32;
205 CheckSum32 += MicrocodeEntryPoint->ProcessorFlags;
206 CheckSum32 += MicrocodeEntryPoint->Checksum;
207 if (CheckSum32 == 0) {
208 CorrectMicrocode = TRUE;
209 ProcessorFlags = MicrocodeEntryPoint->ProcessorFlags;
210 }
211 } else if ((MicrocodeEntryPoint->DataSize != 0) &&
212 (MicrocodeEntryPoint->UpdateRevision > LatestRevision)) {
213 ExtendedTableLength = MicrocodeEntryPoint->TotalSize - (MicrocodeEntryPoint->DataSize +
214 sizeof (CPU_MICROCODE_HEADER));
215 if (ExtendedTableLength != 0) {
216 //
217 // Extended Table exist, check if the CPU in support list
218 //
219 ExtendedTableHeader = (CPU_MICROCODE_EXTENDED_TABLE_HEADER *) ((UINT8 *) (MicrocodeEntryPoint)
220 + MicrocodeEntryPoint->DataSize + sizeof (CPU_MICROCODE_HEADER));
221 //
222 // Calculate Extended Checksum
223 //
224 if ((ExtendedTableLength % 4) == 0) {
225 //
226 // Calculate CheckSum Part2.
227 //
228 CheckSum32 = CalculateSum32 ((UINT32 *) ExtendedTableHeader, ExtendedTableLength);
229 if (CheckSum32 == 0) {
230 //
231 // Checksum correct
232 //
233 ExtendedTableCount = ExtendedTableHeader->ExtendedSignatureCount;
234 ExtendedTable = (CPU_MICROCODE_EXTENDED_TABLE *) (ExtendedTableHeader + 1);
235 for (Index = 0; Index < ExtendedTableCount; Index ++) {
236 //
237 // Calculate CheckSum Part3.
238 //
239 CheckSum32 = InCompleteCheckSum32;
240 CheckSum32 += ExtendedTable->ProcessorSignature.Uint32;
241 CheckSum32 += ExtendedTable->ProcessorFlag;
242 CheckSum32 += ExtendedTable->Checksum;
243 if (CheckSum32 == 0) {
244 //
245 // Verify Header
246 //
247 if ((ExtendedTable->ProcessorSignature.Uint32 == Eax.Uint32) &&
248 (ExtendedTable->ProcessorFlag & (1 << PlatformId)) ) {
249 //
250 // Find one
251 //
252 CorrectMicrocode = TRUE;
253 ProcessorFlags = ExtendedTable->ProcessorFlag;
254 break;
255 }
256 }
257 ExtendedTable ++;
258 }
259 }
260 }
261 }
262 }
263 } else {
264 //
265 // It is the padding data between the microcode patches for microcode patches alignment.
266 // Because the microcode patch is the multiple of 1-KByte, the padding data should not
267 // exist if the microcode patch alignment value is not larger than 1-KByte. So, the microcode
268 // alignment value should be larger than 1-KByte. We could skip SIZE_1KB padding data to
269 // find the next possible microcode patch header.
270 //
271 MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (((UINTN) MicrocodeEntryPoint) + SIZE_1KB);
272 continue;
273 }
274 //
275 // Get the next patch.
276 //
277 if (MicrocodeEntryPoint->DataSize == 0) {
278 TotalSize = 2048;
279 } else {
280 TotalSize = MicrocodeEntryPoint->TotalSize;
281 }
282
283 if (CorrectMicrocode) {
284 LatestRevision = MicrocodeEntryPoint->UpdateRevision;
285 MicrocodeData = (VOID *) ((UINTN) MicrocodeEntryPoint + sizeof (CPU_MICROCODE_HEADER));
286 }
287
288 MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (((UINTN) MicrocodeEntryPoint) + TotalSize);
289 } while (((UINTN) MicrocodeEntryPoint < MicrocodeEnd));
290
291 Done:
292 if (LatestRevision > CurrentRevision) {
293 //
294 // BIOS only authenticate updates that contain a numerically larger revision
295 // than the currently loaded revision, where Current Signature < New Update
296 // Revision. A processor with no loaded update is considered to have a
297 // revision equal to zero.
298 //
299 ASSERT (MicrocodeData != NULL);
300 AsmWriteMsr64 (
301 MSR_IA32_BIOS_UPDT_TRIG,
302 (UINT64) (UINTN) MicrocodeData
303 );
304 //
305 // Get and check new microcode signature
306 //
307 CurrentRevision = GetCurrentMicrocodeSignature ();
308 if (CurrentRevision != LatestRevision) {
309 AcquireSpinLock(&CpuMpData->MpLock);
310 DEBUG ((EFI_D_ERROR, "Updated microcode signature [0x%08x] does not match \
311 loaded microcode signature [0x%08x]\n", CurrentRevision, LatestRevision));
312 ReleaseSpinLock(&CpuMpData->MpLock);
313 }
314 }
315
316 if (IsBspCallIn && (LatestRevision != 0)) {
317 //
318 // Save BSP processor info and microcode info for later AP use.
319 //
320 CpuMpData->ProcessorSignature = Eax.Uint32;
321 CpuMpData->ProcessorFlags = ProcessorFlags;
322 CpuMpData->MicrocodeDataAddress = (UINTN) MicrocodeData;
323 CpuMpData->MicrocodeRevision = LatestRevision;
324 DEBUG ((DEBUG_INFO, "BSP Microcode:: signature [0x%08x], ProcessorFlags [0x%08x], \
325 MicroData [0x%08x], Revision [0x%08x]\n", Eax.Uint32, ProcessorFlags, (UINTN) MicrocodeData, LatestRevision));
326 }
327 }