VirtualBox

Ignore:
Timestamp:
Sep 11, 2019 8:46:37 AM (5 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
133262
Message:

Devices/EFI/FirmwareNew: Start upgrade process to edk2-stable201908 (compiles on Windows and works to some extent), bugref:4643

Location:
trunk/src/VBox/Devices/EFI/FirmwareNew
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/EFI/FirmwareNew

  • trunk/src/VBox/Devices/EFI/FirmwareNew/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h

    r77662 r80721  
    22Agent Module to load other modules to deploy SMM Entry Vector for X86 CPU.
    33
    4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
     4Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.<BR>
    55Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
    66
    7 This program and the accompanying materials
    8 are licensed and made available under the terms and conditions of the BSD License
    9 which accompanies this distribution.  The full text of the license may be found at
    10 http://opensource.org/licenses/bsd-license.php
    11 
    12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
    13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     7SPDX-License-Identifier: BSD-2-Clause-Patent
    148
    159**/
     
    2721#include <Protocol/SmmCpuService.h>
    2822#include <Protocol/SmmMemoryAttribute.h>
     23#include <Protocol/MmMp.h>
    2924
    3025#include <Guid/AcpiS3Context.h>
     
    3934#include <Library/BaseMemoryLib.h>
    4035#include <Library/PcdLib.h>
    41 #include <Library/CacheMaintenanceLib.h>
    4236#include <Library/MtrrLib.h>
    4337#include <Library/SmmCpuPlatformHookLib.h>
     
    5549#include <Library/SmmCpuFeaturesLib.h>
    5650#include <Library/PeCoffGetEntryPointLib.h>
     51#include <Library/RegisterCpuFeaturesLib.h>
    5752
    5853#include <AcpiCpuData.h>
    5954#include <CpuHotPlugData.h>
    6055
    61 #include <Register/Cpuid.h>
    62 #include <Register/Msr.h>
     56#include <Register/Intel/Cpuid.h>
     57#include <Register/Intel/Msr.h>
    6358
    6459#include "CpuService.h"
    6560#include "SmmProfile.h"
     61
     62//
     63// CET definition
     64//
     65#define CPUID_CET_SS   BIT7
     66#define CPUID_CET_IBT  BIT20
     67
     68#define CR4_CET_ENABLE  BIT23
     69
     70#define MSR_IA32_S_CET                     0x6A2
     71#define MSR_IA32_PL0_SSP                   0x6A4
     72#define MSR_IA32_INTERRUPT_SSP_TABLE_ADDR  0x6A8
     73
     74typedef union {
     75  struct {
     76    // enable shadow stacks
     77    UINT32  SH_STK_ENP:1;
     78    // enable the WRSS{D,Q}W instructions.
     79    UINT32  WR_SHSTK_EN:1;
     80    // enable tracking of indirect call/jmp targets to be ENDBRANCH instruction.
     81    UINT32  ENDBR_EN:1;
     82    // enable legacy compatibility treatment for indirect call/jmp tracking.
     83    UINT32  LEG_IW_EN:1;
     84    // enable use of no-track prefix on indirect call/jmp.
     85    UINT32  NO_TRACK_EN:1;
     86    // disable suppression of CET indirect branch tracking on legacy compatibility.
     87    UINT32  SUPPRESS_DIS:1;
     88    UINT32  RSVD:4;
     89    // indirect branch tracking is suppressed.
     90    // This bit can be written to 1 only if TRACKER is written as IDLE.
     91    UINT32  SUPPRESS:1;
     92    // Value of the endbranch state machine
     93    // Values: IDLE (0), WAIT_FOR_ENDBRANCH(1).
     94    UINT32  TRACKER:1;
     95    // linear address of a bitmap in memory indicating valid
     96    // pages as target of CALL/JMP_indirect that do not land on ENDBRANCH when CET is enabled
     97    // and not suppressed. Valid when ENDBR_EN is 1. Must be machine canonical when written on
     98    // parts that support 64 bit mode. On parts that do not support 64 bit mode, the bits 63:32 are
     99    // reserved and must be 0. This value is extended by 12 bits at the low end to form the base address
     100    // (this automatically aligns the address on a 4-Kbyte boundary).
     101    UINT32  EB_LEG_BITMAP_BASE_low:12;
     102    UINT32  EB_LEG_BITMAP_BASE_high:32;
     103  } Bits;
     104  UINT64   Uint64;
     105} MSR_IA32_CET;
    66106
    67107//
     
    128168//
    129169#define TSS_SIZE              104
     170#define EXCEPTION_TSS_SIZE    (TSS_SIZE + 4) // Add 4 bytes SSP
    130171#define TSS_X64_IST1_OFFSET   36
    131172#define TSS_IA32_CR3_OFFSET   28
    132173#define TSS_IA32_ESP_OFFSET   56
     174#define TSS_IA32_SSP_OFFSET   104
    133175
    134176#define CR0_WP                BIT16
     
    158200
    159201//
     202// Wrapper used to convert EFI_AP_PROCEDURE2 and EFI_AP_PROCEDURE.
     203//
     204typedef struct {
     205  EFI_AP_PROCEDURE  Procedure;
     206  VOID              *ProcedureArgument;
     207} PROCEDURE_WRAPPER;
     208
     209#define PROCEDURE_TOKEN_SIGNATURE  SIGNATURE_32 ('P', 'R', 'T', 'S')
     210
     211typedef struct {
     212  UINTN                   Signature;
     213  LIST_ENTRY              Link;
     214
     215  SPIN_LOCK               *ProcedureToken;
     216} PROCEDURE_TOKEN;
     217
     218#define PROCEDURE_TOKEN_FROM_LINK(a)  CR (a, PROCEDURE_TOKEN, Link, PROCEDURE_TOKEN_SIGNATURE)
     219
     220//
    160221// Private structure for the SMM CPU module that is stored in DXE Runtime memory
    161222// Contains the SMM Configuration Protocols that is produced.
     
    179240
    180241  EFI_SMM_CONFIGURATION_PROTOCOL  SmmConfiguration;
     242
     243  PROCEDURE_WRAPPER               *ApWrapperFunc;
     244  LIST_ENTRY                      TokenList;
     245
    181246} SMM_CPU_PRIVATE_DATA;
    182247
     
    186251extern UINTN                  mNumberOfCpus;
    187252extern EFI_SMM_CPU_PROTOCOL   mSmmCpu;
     253extern EFI_MM_MP_PROTOCOL     mSmmMp;
    188254
    189255///
     
    298364  );
    299365
    300 //
    301 //
    302 //
    303 typedef struct {
    304   UINT32                            Offset;
    305   UINT16                            Segment;
    306   UINT16                            Reserved;
    307 } IA32_FAR_ADDRESS;
    308 
    309 extern IA32_FAR_ADDRESS             gSmmJmpAddr;
    310 
    311366extern CONST UINT8                  gcSmmInitTemplate[];
    312367extern CONST UINT16                 gcSmmInitSize;
    313 extern UINT32                       gSmmCr0;
    314 extern UINT32                       gSmmCr3;
    315 extern UINT32                       gSmmCr4;
    316 extern UINTN                        gSmmInitStack;
     368X86_ASSEMBLY_PATCH_LABEL            gPatchSmmCr0;
     369extern UINT32                       mSmmCr0;
     370X86_ASSEMBLY_PATCH_LABEL            gPatchSmmCr3;
     371extern UINT32                       mSmmCr4;
     372X86_ASSEMBLY_PATCH_LABEL            gPatchSmmCr4;
     373X86_ASSEMBLY_PATCH_LABEL            gPatchSmmInitStack;
     374X86_ASSEMBLY_PATCH_LABEL            mPatchCetSupported;
     375extern BOOLEAN                      mCetSupported;
    317376
    318377/**
     
    330389typedef struct {
    331390  SPIN_LOCK                         *Busy;
    332   volatile EFI_AP_PROCEDURE         Procedure;
     391  volatile EFI_AP_PROCEDURE2        Procedure;
    333392  volatile VOID                     *Parameter;
    334393  volatile UINT32                   *Run;
    335394  volatile BOOLEAN                  *Present;
     395  SPIN_LOCK                         *Token;
     396  EFI_STATUS                        *Status;
    336397} SMM_CPU_DATA_BLOCK;
    337398
     
    355416  volatile BOOLEAN              SwitchBsp;
    356417  volatile BOOLEAN              *CandidateBsp;
     418  EFI_AP_PROCEDURE              StartupProcedure;
     419  VOID                          *StartupProcArgs;
    357420} SMM_DISPATCHER_MP_SYNC_DATA;
    358 
    359 #define MSR_SPIN_LOCK_INIT_NUM 15
    360 
    361 typedef struct {
    362   SPIN_LOCK    *SpinLock;
    363   UINT32       MsrIndex;
    364 } MP_MSR_LOCK;
    365421
    366422#define SMM_PSD_OFFSET              0xfb00
     
    375431  SPIN_LOCK            *PFLock;
    376432  SPIN_LOCK            *CodeAccessCheckLock;
    377   SPIN_LOCK            *MemoryMappedLock;
    378433} SMM_CPU_SEMAPHORE_GLOBAL;
    379434
     
    385440  volatile UINT32                   *Run;
    386441  volatile BOOLEAN                  *Present;
     442  SPIN_LOCK                         *Token;
    387443} SMM_CPU_SEMAPHORE_CPU;
    388 
    389 ///
    390 /// All MSRs semaphores' pointer and counter
    391 ///
    392 typedef struct {
    393   SPIN_LOCK            *Msr;
    394   UINTN                AvailableCounter;
    395 } SMM_CPU_SEMAPHORE_MSR;
    396444
    397445///
     
    401449  SMM_CPU_SEMAPHORE_GLOBAL          SemaphoreGlobal;
    402450  SMM_CPU_SEMAPHORE_CPU             SemaphoreCpu;
    403   SMM_CPU_SEMAPHORE_MSR             SemaphoreMsr;
    404451} SMM_CPU_SEMAPHORES;
    405452
     
    420467extern SPIN_LOCK                           *mPFLock;
    421468extern SPIN_LOCK                           *mConfigSmmCodeAccessCheckLock;
    422 extern SPIN_LOCK                           *mMemoryMappedLock;
    423469extern EFI_SMRAM_DESCRIPTOR                *mSmmCpuSmramRanges;
    424470extern UINTN                               mSmmCpuSmramRangeCount;
     
    446492  Initialize global data for MP synchronization.
    447493
    448   @param Stacks       Base address of SMI stack buffer for all processors.
    449   @param StackSize    Stack size for each processor in SMM.
     494  @param Stacks             Base address of SMI stack buffer for all processors.
     495  @param StackSize          Stack size for each processor in SMM.
     496  @param ShadowStackSize    Shadow Stack size for each processor in SMM.
    450497
    451498**/
     
    453500InitializeMpServiceData (
    454501  IN VOID        *Stacks,
    455   IN UINTN       StackSize
     502  IN UINTN       StackSize,
     503  IN UINTN       ShadowStackSize
    456504  );
    457505
     
    10651113
    10661114/**
     1115  Set ShadowStack memory.
     1116
     1117  @param[in]  Cr3              The page table base address.
     1118  @param[in]  BaseAddress      The physical address that is the start address of a memory region.
     1119  @param[in]  Length           The size in bytes of the memory region.
     1120
     1121  @retval EFI_SUCCESS           The shadow stack memory is set.
     1122**/
     1123EFI_STATUS
     1124SetShadowStack (
     1125  IN  UINTN                                      Cr3,
     1126  IN  EFI_PHYSICAL_ADDRESS                       BaseAddress,
     1127  IN  UINT64                                     Length
     1128  );
     1129
     1130/**
     1131  Set not present memory.
     1132
     1133  @param[in]  Cr3              The page table base address.
     1134  @param[in]  BaseAddress      The physical address that is the start address of a memory region.
     1135  @param[in]  Length           The size in bytes of the memory region.
     1136
     1137  @retval EFI_SUCCESS           The not present memory is set.
     1138**/
     1139EFI_STATUS
     1140SetNotPresentPage (
     1141  IN  UINTN                                      Cr3,
     1142  IN  EFI_PHYSICAL_ADDRESS                       BaseAddress,
     1143  IN  UINT64                                     Length
     1144  );
     1145
     1146/**
     1147  Initialize the shadow stack related data structure.
     1148
     1149  @param CpuIndex     The index of CPU.
     1150  @param ShadowStack  The bottom of the shadow stack for this CPU.
     1151**/
     1152VOID
     1153InitShadowStack (
     1154  IN UINTN  CpuIndex,
     1155  IN VOID   *ShadowStack
     1156  );
     1157
     1158/**
    10671159  This function set given attributes of the memory region specified by
    10681160  BaseAddress and Length.
     
    10821174                                bytes of the memory resource range specified
    10831175                                by BaseAddress and Length.
    1084                                 The bit mask of attributes is not support for
     1176                                The bit mask of attributes is not supported for
    10851177                                the memory resource range specified by
    10861178                                BaseAddress and Length.
     
    11041196                            a memory region.
    11051197  @param  Length            The size in bytes of the memory region.
    1106   @param  Attributes        The bit mask of attributes to set for the memory
     1198  @param  Attributes        The bit mask of attributes to clear for the memory
    11071199                            region.
    11081200
    1109   @retval EFI_SUCCESS           The attributes were set for the memory region.
     1201  @retval EFI_SUCCESS           The attributes were cleared for the memory region.
    11101202  @retval EFI_INVALID_PARAMETER Length is zero.
    11111203                                Attributes specified an illegal combination of
    1112                                 attributes that cannot be set together.
     1204                                attributes that cannot be cleared together.
    11131205  @retval EFI_UNSUPPORTED       The processor does not support one or more
    11141206                                bytes of the memory resource range specified
    11151207                                by BaseAddress and Length.
    1116                                 The bit mask of attributes is not support for
     1208                                The bit mask of attributes is not supported for
    11171209                                the memory resource range specified by
    11181210                                BaseAddress and Length.
     
    11291221
    11301222/**
    1131   This function retrieve the attributes of the memory region specified by
     1223  This function retrieves the attributes of the memory region specified by
    11321224  BaseAddress and Length. If different attributes are got from different part
    11331225  of the memory region, EFI_NO_MAPPING will be returned.
     
    11471239                                bytes of the memory resource range specified
    11481240                                by BaseAddress and Length.
    1149                                 The bit mask of attributes is not support for
    1150                                 the memory resource range specified by
    1151                                 BaseAddress and Length.
    11521241
    11531242**/
     
    11791268 );
    11801269
     1270/**
     1271  This function reads CR2 register when on-demand paging is enabled
     1272  for 64 bit and no action for 32 bit.
     1273
     1274  @param[out]  *Cr2  Pointer to variable to hold CR2 register value.
     1275**/
     1276VOID
     1277SaveCr2 (
     1278  OUT UINTN  *Cr2
     1279  );
     1280
     1281/**
     1282  This function writes into CR2 register when on-demand paging is enabled
     1283  for 64 bit and no action for 32 bit.
     1284
     1285  @param[in]  Cr2  Value to write into CR2 register.
     1286**/
     1287VOID
     1288RestoreCr2 (
     1289  IN UINTN  Cr2
     1290  );
     1291
     1292/**
     1293  Schedule a procedure to run on the specified CPU.
     1294
     1295  @param[in]       Procedure                The address of the procedure to run
     1296  @param[in]       CpuIndex                 Target CPU Index
     1297  @param[in,out]   ProcArguments            The parameter to pass to the procedure
     1298  @param[in,out]   Token                    This is an optional parameter that allows the caller to execute the
     1299                                            procedure in a blocking or non-blocking fashion. If it is NULL the
     1300                                            call is blocking, and the call will not return until the AP has
     1301                                            completed the procedure. If the token is not NULL, the call will
     1302                                            return immediately. The caller can check whether the procedure has
     1303                                            completed with CheckOnProcedure or WaitForProcedure.
     1304  @param[in]       TimeoutInMicroseconds    Indicates the time limit in microseconds for the APs to finish
     1305                                            execution of Procedure, either for blocking or non-blocking mode.
     1306                                            Zero means infinity. If the timeout expires before all APs return
     1307                                            from Procedure, then Procedure on the failed APs is terminated. If
     1308                                            the timeout expires in blocking mode, the call returns EFI_TIMEOUT.
     1309                                            If the timeout expires in non-blocking mode, the timeout determined
     1310                                            can be through CheckOnProcedure or WaitForProcedure.
     1311                                            Note that timeout support is optional. Whether an implementation
     1312                                            supports this feature can be determined via the Attributes data
     1313                                            member.
     1314  @param[in,out]   CpuStatus                This optional pointer may be used to get the status code returned
     1315                                            by Procedure when it completes execution on the target AP, or with
     1316                                            EFI_TIMEOUT if the Procedure fails to complete within the optional
     1317                                            timeout. The implementation will update this variable with
     1318                                            EFI_NOT_READY prior to starting Procedure on the target AP.
     1319
     1320  @retval EFI_INVALID_PARAMETER    CpuNumber not valid
     1321  @retval EFI_INVALID_PARAMETER    CpuNumber specifying BSP
     1322  @retval EFI_INVALID_PARAMETER    The AP specified by CpuNumber did not enter SMM
     1323  @retval EFI_INVALID_PARAMETER    The AP specified by CpuNumber is busy
     1324  @retval EFI_SUCCESS              The procedure has been successfully scheduled
     1325
     1326**/
     1327EFI_STATUS
     1328InternalSmmStartupThisAp (
     1329  IN      EFI_AP_PROCEDURE2              Procedure,
     1330  IN      UINTN                          CpuIndex,
     1331  IN OUT  VOID                           *ProcArguments OPTIONAL,
     1332  IN OUT  MM_COMPLETION                  *Token,
     1333  IN      UINTN                          TimeoutInMicroseconds,
     1334  IN OUT  EFI_STATUS                     *CpuStatus
     1335  );
     1336
     1337/**
     1338  Checks whether the input token is the current used token.
     1339
     1340  @param[in]  Token      This parameter describes the token that was passed into DispatchProcedure or
     1341                         BroadcastProcedure.
     1342
     1343  @retval TRUE           The input token is the current used token.
     1344  @retval FALSE          The input token is not the current used token.
     1345**/
     1346BOOLEAN
     1347IsTokenInUse (
     1348  IN SPIN_LOCK           *Token
     1349  );
     1350
     1351/**
     1352  Checks status of specified AP.
     1353
     1354  This function checks whether the specified AP has finished the task assigned
     1355  by StartupThisAP(), and whether timeout expires.
     1356
     1357  @param[in]  Token             This parameter describes the token that was passed into DispatchProcedure or
     1358                                BroadcastProcedure.
     1359
     1360  @retval EFI_SUCCESS           Specified AP has finished task assigned by StartupThisAPs().
     1361  @retval EFI_NOT_READY         Specified AP has not finished task and timeout has not expired.
     1362**/
     1363EFI_STATUS
     1364IsApReady (
     1365  IN SPIN_LOCK  *Token
     1366  );
     1367
     1368/**
     1369  Check whether it is an present AP.
     1370
     1371  @param   CpuIndex      The AP index which calls this function.
     1372
     1373  @retval  TRUE           It's a present AP.
     1374  @retval  TRUE           This is not an AP or it is not present.
     1375
     1376**/
     1377BOOLEAN
     1378IsPresentAp (
     1379  IN UINTN        CpuIndex
     1380  );
     1381
     1382/**
     1383  Worker function to execute a caller provided function on all enabled APs.
     1384
     1385  @param[in]     Procedure               A pointer to the function to be run on
     1386                                         enabled APs of the system.
     1387  @param[in]     TimeoutInMicroseconds   Indicates the time limit in microseconds for
     1388                                         APs to return from Procedure, either for
     1389                                         blocking or non-blocking mode.
     1390  @param[in,out] ProcedureArguments      The parameter passed into Procedure for
     1391                                         all APs.
     1392  @param[in,out] Token                   This is an optional parameter that allows the caller to execute the
     1393                                         procedure in a blocking or non-blocking fashion. If it is NULL the
     1394                                         call is blocking, and the call will not return until the AP has
     1395                                         completed the procedure. If the token is not NULL, the call will
     1396                                         return immediately. The caller can check whether the procedure has
     1397                                         completed with CheckOnProcedure or WaitForProcedure.
     1398  @param[in,out] CPUStatus               This optional pointer may be used to get the status code returned
     1399                                         by Procedure when it completes execution on the target AP, or with
     1400                                         EFI_TIMEOUT if the Procedure fails to complete within the optional
     1401                                         timeout. The implementation will update this variable with
     1402                                         EFI_NOT_READY prior to starting Procedure on the target AP.
     1403
     1404  @retval EFI_SUCCESS             In blocking mode, all APs have finished before
     1405                                  the timeout expired.
     1406  @retval EFI_SUCCESS             In non-blocking mode, function has been dispatched
     1407                                  to all enabled APs.
     1408  @retval others                  Failed to Startup all APs.
     1409
     1410**/
     1411EFI_STATUS
     1412InternalSmmStartupAllAPs (
     1413  IN       EFI_AP_PROCEDURE2             Procedure,
     1414  IN       UINTN                         TimeoutInMicroseconds,
     1415  IN OUT   VOID                          *ProcedureArguments OPTIONAL,
     1416  IN OUT   MM_COMPLETION                 *Token,
     1417  IN OUT   EFI_STATUS                    *CPUStatus
     1418  );
     1419
     1420/**
     1421
     1422  Register the SMM Foundation entry point.
     1423
     1424  @param[in]      Procedure            A pointer to the code stream to be run on the designated target AP
     1425                                       of the system. Type EFI_AP_PROCEDURE is defined below in Volume 2
     1426                                       with the related definitions of
     1427                                       EFI_MP_SERVICES_PROTOCOL.StartupAllAPs.
     1428                                       If caller may pass a value of NULL to deregister any existing
     1429                                       startup procedure.
     1430  @param[in,out]  ProcedureArguments   Allows the caller to pass a list of parameters to the code that is
     1431                                       run by the AP. It is an optional common mailbox between APs and
     1432                                       the caller to share information
     1433
     1434  @retval EFI_SUCCESS                  The Procedure has been set successfully.
     1435  @retval EFI_INVALID_PARAMETER        The Procedure is NULL but ProcedureArguments not NULL.
     1436
     1437**/
     1438EFI_STATUS
     1439RegisterStartupProcedure (
     1440  IN     EFI_AP_PROCEDURE    Procedure,
     1441  IN OUT VOID                *ProcedureArguments OPTIONAL
     1442  );
     1443
     1444/**
     1445  Allocate buffer for SpinLock and Wrapper function buffer.
     1446
     1447**/
     1448VOID
     1449InitializeDataForMmMp (
     1450  VOID
     1451  );
     1452
    11811453#endif
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette