]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/Microcode.c
UefiCpuPkg/Microcode: Fix incorrect checksum issue for extended table
[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 //
163 // Save an in-complete CheckSum32 from CheckSum Part1 for common parts.
164 //
165 if (MicrocodeEntryPoint->DataSize == 0) {
166 InCompleteCheckSum32 = CalculateSum32 (
167 (UINT32 *) MicrocodeEntryPoint,
168 sizeof (CPU_MICROCODE_HEADER) + 2000
169 );
170 } else {
171 InCompleteCheckSum32 = CalculateSum32 (
172 (UINT32 *) MicrocodeEntryPoint,
173 sizeof (CPU_MICROCODE_HEADER) + MicrocodeEntryPoint->DataSize
174 );
175 }
176 InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorSignature.Uint32;
177 InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorFlags;
178 InCompleteCheckSum32 -= MicrocodeEntryPoint->Checksum;
179
180 do {
181 //
182 // Check if the microcode is for the Cpu and the version is newer
183 // and the update can be processed on the platform
184 //
185 CorrectMicrocode = FALSE;
186 if (MicrocodeEntryPoint->HeaderVersion == 0x1) {
187 //
188 // It is the microcode header. It is not the padding data between microcode patches
189 // because the padding data should not include 0x00000001 and it should be the repeated
190 // byte format (like 0xXYXYXYXY....).
191 //
192 if (MicrocodeEntryPoint->ProcessorSignature.Uint32 == Eax.Uint32 &&
193 MicrocodeEntryPoint->UpdateRevision > LatestRevision &&
194 (MicrocodeEntryPoint->ProcessorFlags & (1 << PlatformId))
195 ) {
196 //
197 // Calculate CheckSum Part1.
198 //
199 CheckSum32 = InCompleteCheckSum32;
200 CheckSum32 += MicrocodeEntryPoint->ProcessorSignature.Uint32;
201 CheckSum32 += MicrocodeEntryPoint->ProcessorFlags;
202 CheckSum32 += MicrocodeEntryPoint->Checksum;
203 if (CheckSum32 == 0) {
204 CorrectMicrocode = TRUE;
205 ProcessorFlags = MicrocodeEntryPoint->ProcessorFlags;
206 }
207 } else if ((MicrocodeEntryPoint->DataSize != 0) &&
208 (MicrocodeEntryPoint->UpdateRevision > LatestRevision)) {
209 ExtendedTableLength = MicrocodeEntryPoint->TotalSize - (MicrocodeEntryPoint->DataSize +
210 sizeof (CPU_MICROCODE_HEADER));
211 if (ExtendedTableLength != 0) {
212 //
213 // Extended Table exist, check if the CPU in support list
214 //
215 ExtendedTableHeader = (CPU_MICROCODE_EXTENDED_TABLE_HEADER *) ((UINT8 *) (MicrocodeEntryPoint)
216 + MicrocodeEntryPoint->DataSize + sizeof (CPU_MICROCODE_HEADER));
217 //
218 // Calculate Extended Checksum
219 //
220 if ((ExtendedTableLength % 4) == 0) {
221 //
222 // Calculate CheckSum Part2.
223 //
224 CheckSum32 = CalculateSum32 ((UINT32 *) ExtendedTableHeader, ExtendedTableLength);
225 if (CheckSum32 == 0) {
226 //
227 // Checksum correct
228 //
229 ExtendedTableCount = ExtendedTableHeader->ExtendedSignatureCount;
230 ExtendedTable = (CPU_MICROCODE_EXTENDED_TABLE *) (ExtendedTableHeader + 1);
231 for (Index = 0; Index < ExtendedTableCount; Index ++) {
232 //
233 // Calculate CheckSum Part3.
234 //
235 CheckSum32 = InCompleteCheckSum32;
236 CheckSum32 += ExtendedTable->ProcessorSignature.Uint32;
237 CheckSum32 += ExtendedTable->ProcessorFlag;
238 CheckSum32 += ExtendedTable->Checksum;
239 if (CheckSum32 == 0) {
240 //
241 // Verify Header
242 //
243 if ((ExtendedTable->ProcessorSignature.Uint32 == Eax.Uint32) &&
244 (ExtendedTable->ProcessorFlag & (1 << PlatformId)) ) {
245 //
246 // Find one
247 //
248 CorrectMicrocode = TRUE;
249 ProcessorFlags = ExtendedTable->ProcessorFlag;
250 break;
251 }
252 }
253 ExtendedTable ++;
254 }
255 }
256 }
257 }
258 }
259 } else {
260 //
261 // It is the padding data between the microcode patches for microcode patches alignment.
262 // Because the microcode patch is the multiple of 1-KByte, the padding data should not
263 // exist if the microcode patch alignment value is not larger than 1-KByte. So, the microcode
264 // alignment value should be larger than 1-KByte. We could skip SIZE_1KB padding data to
265 // find the next possible microcode patch header.
266 //
267 MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (((UINTN) MicrocodeEntryPoint) + SIZE_1KB);
268 continue;
269 }
270 //
271 // Get the next patch.
272 //
273 if (MicrocodeEntryPoint->DataSize == 0) {
274 TotalSize = 2048;
275 } else {
276 TotalSize = MicrocodeEntryPoint->TotalSize;
277 }
278
279 if (CorrectMicrocode) {
280 LatestRevision = MicrocodeEntryPoint->UpdateRevision;
281 MicrocodeData = (VOID *) ((UINTN) MicrocodeEntryPoint + sizeof (CPU_MICROCODE_HEADER));
282 }
283
284 MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (((UINTN) MicrocodeEntryPoint) + TotalSize);
285 } while (((UINTN) MicrocodeEntryPoint < MicrocodeEnd));
286
287 Done:
288 if (LatestRevision > CurrentRevision) {
289 //
290 // BIOS only authenticate updates that contain a numerically larger revision
291 // than the currently loaded revision, where Current Signature < New Update
292 // Revision. A processor with no loaded update is considered to have a
293 // revision equal to zero.
294 //
295 ASSERT (MicrocodeData != NULL);
296 AsmWriteMsr64 (
297 MSR_IA32_BIOS_UPDT_TRIG,
298 (UINT64) (UINTN) MicrocodeData
299 );
300 //
301 // Get and check new microcode signature
302 //
303 CurrentRevision = GetCurrentMicrocodeSignature ();
304 if (CurrentRevision != LatestRevision) {
305 AcquireSpinLock(&CpuMpData->MpLock);
306 DEBUG ((EFI_D_ERROR, "Updated microcode signature [0x%08x] does not match \
307 loaded microcode signature [0x%08x]\n", CurrentRevision, LatestRevision));
308 ReleaseSpinLock(&CpuMpData->MpLock);
309 }
310 }
311
312 if (IsBspCallIn && (LatestRevision != 0)) {
313 //
314 // Save BSP processor info and microcode info for later AP use.
315 //
316 CpuMpData->ProcessorSignature = Eax.Uint32;
317 CpuMpData->ProcessorFlags = ProcessorFlags;
318 CpuMpData->MicrocodeDataAddress = (UINTN) MicrocodeData;
319 CpuMpData->MicrocodeRevision = LatestRevision;
320 DEBUG ((DEBUG_INFO, "BSP Microcode:: signature [0x%08x], ProcessorFlags [0x%08x], \
321 MicroData [0x%08x], Revision [0x%08x]\n", Eax.Uint32, ProcessorFlags, (UINTN) MicrocodeData, LatestRevision));
322 }
323 }