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