summaryrefslogtreecommitdiffstats
path: root/UefiCpuPkg/Library/MpInitLib/Microcode.c
blob: 11720560afee7ccd28b37d4bc0955c27211aff4d (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
/** @file
  Implementation of loading microcode on processors.

  Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include "MpLib.h"

/**
  Detect whether specified processor can find matching microcode patch and load it.

  @param[in]  CpuMpData        The pointer to CPU MP Data structure.
  @param[in]  ProcessorNumber  The handle number of the processor. The range is
                               from 0 to the total number of logical processors
                               minus 1.
**/
VOID
MicrocodeDetect (
  IN CPU_MP_DATA  *CpuMpData,
  IN UINTN        ProcessorNumber
  )
{
  CPU_MICROCODE_HEADER        *Microcode;
  UINTN                       MicrocodeEnd;
  CPU_AP_DATA                 *BspData;
  UINT32                      LatestRevision;
  CPU_MICROCODE_HEADER        *LatestMicrocode;
  UINT32                      ThreadId;
  EDKII_PEI_MICROCODE_CPU_ID  MicrocodeCpuId;

  if (CpuMpData->MicrocodePatchRegionSize == 0) {
    //
    // There is no microcode patches
    //
    return;
  }

  GetProcessorLocationByApicId (GetInitialApicId (), NULL, NULL, &ThreadId);
  if (ThreadId != 0) {
    //
    // Skip loading microcode if it is not the first thread in one core.
    //
    return;
  }

  GetProcessorMicrocodeCpuId (&MicrocodeCpuId);

  if (ProcessorNumber != (UINTN)CpuMpData->BspNumber) {
    //
    // Direct use microcode of BSP if AP is the same as BSP.
    // Assume BSP calls this routine() before AP.
    //
    BspData = &(CpuMpData->CpuData[CpuMpData->BspNumber]);
    if ((BspData->ProcessorSignature == MicrocodeCpuId.ProcessorSignature) &&
        (BspData->PlatformId == MicrocodeCpuId.PlatformId) &&
        (BspData->MicrocodeEntryAddr != 0))
    {
      LatestMicrocode = (CPU_MICROCODE_HEADER *)(UINTN)BspData->MicrocodeEntryAddr;
      LatestRevision  = LatestMicrocode->UpdateRevision;
      goto LoadMicrocode;
    }
  }

  //
  // BSP or AP which is different from BSP runs here
  // Use 0 as the starting revision to search for microcode because MicrocodePatchInfo HOB needs
  // the latest microcode location even it's loaded to the processor.
  //
  LatestRevision  = 0;
  LatestMicrocode = NULL;
  Microcode       = (CPU_MICROCODE_HEADER *)(UINTN)CpuMpData->MicrocodePatchAddress;
  MicrocodeEnd    = (UINTN)Microcode + (UINTN)CpuMpData->MicrocodePatchRegionSize;

  do {
    if (!IsValidMicrocode (Microcode, MicrocodeEnd - (UINTN)Microcode, LatestRevision, &MicrocodeCpuId, 1, TRUE)) {
      //
      // It is the padding data between the microcode patches for microcode patches alignment.
      // Because the microcode patch is the multiple of 1-KByte, the padding data should not
      // exist if the microcode patch alignment value is not larger than 1-KByte. So, the microcode
      // alignment value should be larger than 1-KByte. We could skip SIZE_1KB padding data to
      // find the next possible microcode patch header.
      //
      Microcode = (CPU_MICROCODE_HEADER *)((UINTN)Microcode + SIZE_1KB);
      continue;
    }

    LatestMicrocode = Microcode;
    LatestRevision  = LatestMicrocode->UpdateRevision;

    Microcode = (CPU_MICROCODE_HEADER *)(((UINTN)Microcode) + GetMicrocodeLength (Microcode));
  } while ((UINTN)Microcode < MicrocodeEnd);

LoadMicrocode:
  if (LatestRevision != 0) {
    //
    // Save the detected microcode patch entry address (including the microcode
    // patch header) for each processor even it's the same as the loaded one.
    // It will be used when building the microcode patch cache HOB.
    //
    CpuMpData->CpuData[ProcessorNumber].MicrocodeEntryAddr = (UINTN)LatestMicrocode;
  }

  if (LatestRevision > GetProcessorMicrocodeSignature ()) {
    //
    // BIOS only authenticate updates that contain a numerically larger revision
    // than the currently loaded revision, where Current Signature < New Update
    // Revision. A processor with no loaded update is considered to have a
    // revision equal to zero.
    //
    LoadMicrocode (LatestMicrocode);
  }

  //
  // It's possible that the microcode fails to load. Just capture the CPU microcode revision after loading.
  //
  CpuMpData->CpuData[ProcessorNumber].MicrocodeRevision = GetProcessorMicrocodeSignature ();
}

/**
  Actual worker function that shadows the required microcode patches into memory.

  @param[in, out]  CpuMpData        The pointer to CPU MP Data structure.
  @param[in]       Patches          The pointer to an array of information on
                                    the microcode patches that will be loaded
                                    into memory.
  @param[in]       PatchCount       The number of microcode patches that will
                                    be loaded into memory.
  @param[in]       TotalLoadSize    The total size of all the microcode patches
                                    to be loaded.
**/
VOID
ShadowMicrocodePatchWorker (
  IN OUT CPU_MP_DATA           *CpuMpData,
  IN     MICROCODE_PATCH_INFO  *Patches,
  IN     UINTN                 PatchCount,
  IN     UINTN                 TotalLoadSize
  )
{
  UINTN  Index;
  VOID   *MicrocodePatchInRam;
  UINT8  *Walker;

  ASSERT ((Patches != NULL) && (PatchCount != 0));

  MicrocodePatchInRam = AllocatePages (EFI_SIZE_TO_PAGES (TotalLoadSize));
  if (MicrocodePatchInRam == NULL) {
    return;
  }

  //
  // Load all the required microcode patches into memory
  //
  for (Walker = MicrocodePatchInRam, Index = 0; Index < PatchCount; Index++) {
    CopyMem (
      Walker,
      (VOID *)Patches[Index].Address,
      Patches[Index].Size
      );
    Walker += Patches[Index].Size;
  }

  //
  // Update the microcode patch related fields in CpuMpData
  //
  CpuMpData->MicrocodePatchAddress    = (UINTN)MicrocodePatchInRam;
  CpuMpData->MicrocodePatchRegionSize = TotalLoadSize;

  DEBUG ((
    DEBUG_INFO,
    "%a: Required microcode patches have been loaded at 0x%lx, with size 0x%lx.\n",
    __func__,
    CpuMpData->MicrocodePatchAddress,
    CpuMpData->MicrocodePatchRegionSize
    ));

  return;
}

/**
  Shadow the required microcode patches data into memory according to PCD
  PcdCpuMicrocodePatchAddress and PcdCpuMicrocodePatchRegionSize.

  @param[in, out]  CpuMpData    The pointer to CPU MP Data structure.
**/
VOID
ShadowMicrocodePatchByPcd (
  IN OUT CPU_MP_DATA  *CpuMpData
  )
{
  UINTN                       Index;
  CPU_MICROCODE_HEADER        *MicrocodeEntryPoint;
  UINTN                       MicrocodeEnd;
  UINTN                       TotalSize;
  MICROCODE_PATCH_INFO        *PatchInfoBuffer;
  UINTN                       MaxPatchNumber;
  UINTN                       PatchCount;
  UINTN                       TotalLoadSize;
  EDKII_PEI_MICROCODE_CPU_ID  *MicrocodeCpuIds;
  BOOLEAN                     Valid;

  //
  // Initialize the microcode patch related fields in CpuMpData as the values
  // specified by the PCD pair. If the microcode patches are loaded into memory,
  // these fields will be updated.
  //
  CpuMpData->MicrocodePatchAddress    = PcdGet64 (PcdCpuMicrocodePatchAddress);
  CpuMpData->MicrocodePatchRegionSize = PcdGet64 (PcdCpuMicrocodePatchRegionSize);

  MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *)(UINTN)CpuMpData->MicrocodePatchAddress;
  MicrocodeEnd        = (UINTN)MicrocodeEntryPoint +
                        (UINTN)CpuMpData->MicrocodePatchRegionSize;
  if ((MicrocodeEntryPoint == NULL) || ((UINTN)MicrocodeEntryPoint == MicrocodeEnd)) {
    //
    // There is no microcode patches
    //
    return;
  }

  PatchCount      = 0;
  MaxPatchNumber  = DEFAULT_MAX_MICROCODE_PATCH_NUM;
  TotalLoadSize   = 0;
  PatchInfoBuffer = AllocatePool (MaxPatchNumber * sizeof (MICROCODE_PATCH_INFO));
  if (PatchInfoBuffer == NULL) {
    return;
  }

  MicrocodeCpuIds = AllocatePages (
                      EFI_SIZE_TO_PAGES (CpuMpData->CpuCount * sizeof (EDKII_PEI_MICROCODE_CPU_ID))
                      );
  if (MicrocodeCpuIds == NULL) {
    FreePool (PatchInfoBuffer);
    return;
  }

  for (Index = 0; Index < CpuMpData->CpuCount; Index++) {
    MicrocodeCpuIds[Index].PlatformId         = CpuMpData->CpuData[Index].PlatformId;
    MicrocodeCpuIds[Index].ProcessorSignature = CpuMpData->CpuData[Index].ProcessorSignature;
  }

  //
  // Process the header of each microcode patch within the region.
  // The purpose is to decide which microcode patch(es) will be loaded into memory.
  // Microcode checksum is not verified because it's slow when performing on flash.
  //
  do {
    Valid = IsValidMicrocode (
              MicrocodeEntryPoint,
              MicrocodeEnd - (UINTN)MicrocodeEntryPoint,
              0,
              MicrocodeCpuIds,
              CpuMpData->CpuCount,
              FALSE
              );
    if (!Valid) {
      //
      // Padding data between the microcode patches, skip 1KB to check next entry.
      //
      MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *)(((UINTN)MicrocodeEntryPoint) + SIZE_1KB);
      continue;
    }

    PatchCount++;
    if (PatchCount > MaxPatchNumber) {
      //
      // Current 'PatchInfoBuffer' cannot hold the information, double the size
      // and allocate a new buffer.
      //
      if (MaxPatchNumber > MAX_UINTN / 2 / sizeof (MICROCODE_PATCH_INFO)) {
        //
        // Overflow check for MaxPatchNumber
        //
        goto OnExit;
      }

      PatchInfoBuffer = ReallocatePool (
                          MaxPatchNumber * sizeof (MICROCODE_PATCH_INFO),
                          2 * MaxPatchNumber * sizeof (MICROCODE_PATCH_INFO),
                          PatchInfoBuffer
                          );
      if (PatchInfoBuffer == NULL) {
        goto OnExit;
      }

      MaxPatchNumber = MaxPatchNumber * 2;
    }

    TotalSize = GetMicrocodeLength (MicrocodeEntryPoint);

    //
    // Store the information of this microcode patch
    //
    PatchInfoBuffer[PatchCount - 1].Address = (UINTN)MicrocodeEntryPoint;
    PatchInfoBuffer[PatchCount - 1].Size    = TotalSize;
    TotalLoadSize                          += TotalSize;

    //
    // Process the next microcode patch
    //
    MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *)((UINTN)MicrocodeEntryPoint + TotalSize);
  } while ((UINTN)MicrocodeEntryPoint < MicrocodeEnd);

  if (PatchCount != 0) {
    DEBUG ((
      DEBUG_INFO,
      "%a: 0x%x microcode patches will be loaded into memory, with size 0x%x.\n",
      __func__,
      PatchCount,
      TotalLoadSize
      ));

    ShadowMicrocodePatchWorker (CpuMpData, PatchInfoBuffer, PatchCount, TotalLoadSize);
  }

OnExit:
  if (PatchInfoBuffer != NULL) {
    FreePool (PatchInfoBuffer);
  }

  FreePages (MicrocodeCpuIds, EFI_SIZE_TO_PAGES (CpuMpData->CpuCount * sizeof (EDKII_PEI_MICROCODE_CPU_ID)));
}

/**
  Shadow the required microcode patches data into memory.

  @param[in, out]  CpuMpData    The pointer to CPU MP Data structure.
**/
VOID
ShadowMicrocodeUpdatePatch (
  IN OUT CPU_MP_DATA  *CpuMpData
  )
{
  EFI_STATUS  Status;

  Status = PlatformShadowMicrocode (CpuMpData);
  if (EFI_ERROR (Status)) {
    ShadowMicrocodePatchByPcd (CpuMpData);
  }
}

/**
  Get the cached microcode patch base address and size from the microcode patch
  information cache HOB.

  @param[out] Address       Base address of the microcode patches data.
                            It will be updated if the microcode patch
                            information cache HOB is found.
  @param[out] RegionSize    Size of the microcode patches data.
                            It will be updated if the microcode patch
                            information cache HOB is found.

  @retval  TRUE     The microcode patch information cache HOB is found.
  @retval  FALSE    The microcode patch information cache HOB is not found.

**/
BOOLEAN
GetMicrocodePatchInfoFromHob (
  UINT64  *Address,
  UINT64  *RegionSize
  )
{
  EFI_HOB_GUID_TYPE          *GuidHob;
  EDKII_MICROCODE_PATCH_HOB  *MicrocodePathHob;

  GuidHob = GetFirstGuidHob (&gEdkiiMicrocodePatchHobGuid);
  if (GuidHob == NULL) {
    DEBUG ((DEBUG_INFO, "%a: Microcode patch cache HOB is not found.\n", __func__));
    return FALSE;
  }

  MicrocodePathHob = GET_GUID_HOB_DATA (GuidHob);

  *Address    = MicrocodePathHob->MicrocodePatchAddress;
  *RegionSize = MicrocodePathHob->MicrocodePatchRegionSize;

  DEBUG ((
    DEBUG_INFO,
    "%a: MicrocodeBase = 0x%lx, MicrocodeSize = 0x%lx\n",
    __func__,
    *Address,
    *RegionSize
    ));

  return TRUE;
}