From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by mx.groups.io with SMTP id smtpd.web08.1507.1641521749147974069 for ; Thu, 06 Jan 2022 18:15:49 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20210112 header.b=C7omQA8a; spf=pass (domain: gmail.com, ip: 209.85.214.182, mailfrom: kuqin12@gmail.com) Received: by mail-pl1-f182.google.com with SMTP id z3so3781905plg.8 for ; Thu, 06 Jan 2022 18:15:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=EWGXm/fLrygUr1evKdVVkUPJTCy4DvyIy6XQUGlI6oQ=; b=C7omQA8aAnR4gVJAoOqfFxRHzgsC8vHRjycadz40OoSvusJZ5ZfOI0Lj+Ebc+DvXaO yLccPPSn6Q+wGHv+63VcOyarbrBpnpecxgde/zUhzokHd0yujVIj4k2z8PgXtrYEUWiY CTo5sGMNicbe/1mNjuZ5pte7Uo9I5OEjDxa2Kt9W7crK3fyTNZGoMUm4gm36mqJc+po4 cbt0kRAIx4K6fZhFHN0Wf8gFecOYbdxCQml2JBYcPeEMfvsLt7aM48KAj+xws/IdKEjz 9kkeLyzarvqgXerI0GZoM09RLYOWN40NfOO0ehiVF6f6q7HNOmXuF5SfjUVxcEoD+OBP Gd/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EWGXm/fLrygUr1evKdVVkUPJTCy4DvyIy6XQUGlI6oQ=; b=TCuNkku9pf6jBzIGxqcO9uRNe9sEf9wXDueqAEZK4IrbBWXyImPrSoNlGt8MIUVkd8 owEmpV/VgluKv9u+GS4k68AOJD7GJoJi/1uqJLdrc/sx2/4yWhjOsxRQSnKzNPsQpt8x US1OZXWCkxM4q8XMWjCiO4XE+IAlOZa7RI+A4u/m2Tu8v+e7Jqp6Y5Whg4TwpUcJboHp kvsAjZjZl71W5ooZJFSejMp1i2PuhjAmR/nEmviaPoCZaCt38sba4dGLdfmOzJp6FdED Xfbfd3FrEu+YM/QnJMUgJ1LKNJyuahqFXZBzR/sw73redamRVlVBNVDjuAucC1RP8Kt/ WCzA== X-Gm-Message-State: AOAM533lBG/ctgk4n7dnu9WezN9pkRZ2J1XXdVFj7mNBqAPEuXZeKjYO T+z4T6eTyML2fDc7ANMxVyU3qYuMX2U= X-Google-Smtp-Source: ABdhPJwEth6429jIr7+Bv8hEFH/P+MQ2c6tCRGnNxnO+M2zU0Mw1U54zbeXEhucPqvpihWpXGu2mAQ== X-Received: by 2002:a17:903:22c1:b0:148:c3cc:54c6 with SMTP id y1-20020a17090322c100b00148c3cc54c6mr63546310plg.98.1641521748559; Thu, 06 Jan 2022 18:15:48 -0800 (PST) Return-Path: Received: from localhost.localdomain ([50.35.74.198]) by smtp.gmail.com with ESMTPSA id j3sm3894875pfc.196.2022.01.06.18.15.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jan 2022 18:15:48 -0800 (PST) From: "Kun Qin" To: devel@edk2.groups.io Cc: Jian J Wang , Hao A Wu , Eric Dong , Ray Ni Subject: [PATCH v4 7/7] MdeModulePkg: PiSmmIpl: Update MessageLength calculation for MmCommunicate Date: Thu, 6 Jan 2022 18:15:32 -0800 Message-Id: <20220107021532.1322-8-kuqin12@gmail.com> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20220107021532.1322-1-kuqin12@gmail.com> References: <20220107021532.1322-1-kuqin12@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3398 REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3430 This change added support of installing `EFI_MM_COMMUNICATION3_PROTOCOL`. MmCommunicate v3 routine that calculates message length is also updated to remove ambiguity in contrast to v1 routine. Cc: Jian J Wang Cc: Hao A Wu Cc: Eric Dong Cc: Ray Ni Signed-off-by: Kun Qin --- Notes: v3: - Newly added v3 communicate protocol instance v4: - Rebased with uncrustify changes. MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c | 190 ++++++++++++++++++++ MdeModulePkg/Core/PiSmmCore/PiSmmIpl.inf | 2 + 2 files changed, 192 insertions(+) diff --git a/MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c b/MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c index 4f00cebaf5ed..910f54bed5fb 100644 --- a/MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c +++ b/MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -34,6 +35,7 @@ #include #include #include +#include #include "PiSmmCorePrivateData.h" @@ -146,6 +148,41 @@ SmmCommunicationMmCommunicate2 ( IN OUT UINTN *CommSize OPTIONAL ); +/** + Communicates with a registered handler. + + This function provides a service to send and receive messages from a registered UEFI service. + + @param[in] This The EFI_MM_COMMUNICATION3_PROTOCOL instance. + @param[in, out] CommBufferPhysical Physical address of the MM communication buffer, of which content must + start with EFI_MM_COMMUNICATE_HEADER_V3. + @param[in, out] CommBufferVirtual Virtual address of the MM communication buffer, of which content must + start with EFI_MM_COMMUNICATE_HEADER_V3. + @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data + being returned. Zero if the handler does not wish to reply with any data. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The message was successfully posted. + @retval EFI_INVALID_PARAMETER CommBufferPhysical was NULL or CommBufferVirtual was NULL. + @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation. + If this error is returned, the MessageLength field + in the CommBuffer header or the integer pointed by + CommSize, are updated to reflect the maximum payload + size the implementation can accommodate. + @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter, + if not omitted, are in address range that cannot be + accessed by the MM environment. + +**/ +EFI_STATUS +EFIAPI +MmCommunicationMmCommunicate3 ( + IN CONST EFI_MM_COMMUNICATION3_PROTOCOL *This, + IN OUT VOID *CommBufferPhysical, + IN OUT VOID *CommBufferVirtual, + IN OUT UINTN *CommSize OPTIONAL + ); + /** Event notification that is fired every time a gEfiSmmConfigurationProtocol installs. @@ -275,6 +312,13 @@ EFI_MM_COMMUNICATION2_PROTOCOL mMmCommunication2 = { SmmCommunicationMmCommunicate2 }; +// +// PI 1.7 MM Communication Protocol 3 instance +// +EFI_MM_COMMUNICATION3_PROTOCOL mMmCommunication3 = { + MmCommunicationMmCommunicate3 +}; + // // SMM Core Private Data structure that contains the data shared between // the SMM IPL and the SMM Core. @@ -651,6 +695,150 @@ SmmCommunicationMmCommunicate2 ( ); } +/** + Communicates with a registered handler. + + This function provides a service to send and receive messages from a registered UEFI service. + + @param[in] This The EFI_MM_COMMUNICATION3_PROTOCOL instance. + @param[in, out] CommBufferPhysical Physical address of the MM communication buffer, of which content must + start with EFI_MM_COMMUNICATE_HEADER_V3. + @param[in, out] CommBufferVirtual Virtual address of the MM communication buffer, of which content must + start with EFI_MM_COMMUNICATE_HEADER_V3. + @param[in, out] CommSize The size of the data buffer being passed in. On exit, the size of data + being returned. Zero if the handler does not wish to reply with any data. + This parameter is optional and may be NULL. + + @retval EFI_SUCCESS The message was successfully posted. + @retval EFI_INVALID_PARAMETER CommBufferPhysical was NULL or CommBufferVirtual was NULL. + @retval EFI_BAD_BUFFER_SIZE The buffer is too large for the MM implementation. + If this error is returned, the MessageLength field + in the CommBuffer header or the integer pointed by + CommSize, are updated to reflect the maximum payload + size the implementation can accommodate. + @retval EFI_ACCESS_DENIED The CommunicateBuffer parameter or CommSize parameter, + if not omitted, are in address range that cannot be + accessed by the MM environment. + +**/ +EFI_STATUS +EFIAPI +MmCommunicationMmCommunicate3 ( + IN CONST EFI_MM_COMMUNICATION3_PROTOCOL *This, + IN OUT VOID *CommBufferPhysical, + IN OUT VOID *CommBufferVirtual, + IN OUT UINTN *CommSize OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_MM_COMMUNICATE_HEADER_V3 *CommunicateHeader; + BOOLEAN OldInSmm; + UINTN TempCommSize; + UINT64 LongCommSize; + + // + // Check parameters + // + if (CommBufferPhysical == NULL) { + return EFI_INVALID_PARAMETER; + } + + CommunicateHeader = (EFI_MM_COMMUNICATE_HEADER_V3 *)CommBufferPhysical; + + if (CommSize == NULL) { + Status = SafeUint64Add (sizeof (EFI_MM_COMMUNICATE_HEADER_V3), CommunicateHeader->MessageSize, &LongCommSize); + if (EFI_ERROR (Status)) { + return EFI_INVALID_PARAMETER; + } + + Status = SafeUint64ToUintn (LongCommSize, &TempCommSize); + if (EFI_ERROR (Status)) { + return EFI_INVALID_PARAMETER; + } + } else { + TempCommSize = *CommSize; + // + // CommSize must hold the entire EFI_MM_COMMUNICATE_HEADER_V3 + // + if (TempCommSize < sizeof (EFI_MM_COMMUNICATE_HEADER_V3)) { + return EFI_INVALID_PARAMETER; + } + } + + // + // If not already in SMM, then generate a Software SMI + // + if (!gSmmCorePrivate->InSmm && gSmmCorePrivate->SmmEntryPointRegistered) { + // + // Put arguments for Software SMI in gSmmCorePrivate + // + gSmmCorePrivate->CommunicationBuffer = CommBufferPhysical; + gSmmCorePrivate->BufferSize = TempCommSize; + + // + // Generate Software SMI + // + Status = mSmmControl2->Trigger (mSmmControl2, NULL, NULL, FALSE, 0); + if (EFI_ERROR (Status)) { + return EFI_UNSUPPORTED; + } + + // + // Return status from software SMI + // + if (CommSize != NULL) { + *CommSize = gSmmCorePrivate->BufferSize; + } + + return gSmmCorePrivate->ReturnStatus; + } + + // + // If we are in SMM, then the execution mode must be physical, which means that + // OS established virtual addresses can not be used. If SetVirtualAddressMap() + // has been called, then a direct invocation of the Software SMI is not allowed, + // so return EFI_INVALID_PARAMETER. + // + if (EfiGoneVirtual ()) { + return EFI_INVALID_PARAMETER; + } + + // + // If we are not in SMM, don't allow call SmiManage() directly when SMRAM is closed or locked. + // + if ((!gSmmCorePrivate->InSmm) && (!mSmmAccess->OpenState || mSmmAccess->LockState)) { + return EFI_INVALID_PARAMETER; + } + + // + // Save current InSmm state and set InSmm state to TRUE + // + OldInSmm = gSmmCorePrivate->InSmm; + gSmmCorePrivate->InSmm = TRUE; + + // + // Before SetVirtualAddressMap(), we are in SMM or SMRAM is open and unlocked, call SmiManage() directly. + // + TempCommSize -= sizeof (EFI_MM_COMMUNICATE_HEADER_V3); + Status = gSmmCorePrivate->Smst->SmiManage ( + &CommunicateHeader->MessageGuid, + NULL, + CommunicateHeader->MessageData, + &TempCommSize + ); + TempCommSize += sizeof (EFI_MM_COMMUNICATE_HEADER_V3); + if (CommSize != NULL) { + *CommSize = TempCommSize; + } + + // + // Restore original InSmm state + // + gSmmCorePrivate->InSmm = OldInSmm; + + return (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_NOT_FOUND; +} + /** Event notification that is fired when GUIDed Event Group is signaled. @@ -1859,6 +2047,8 @@ SmmIplEntry ( &mSmmCommunication, &gEfiMmCommunication2ProtocolGuid, &mMmCommunication2, + &gEfiMmCommunication3ProtocolGuid, + &mMmCommunication3, NULL ); ASSERT_EFI_ERROR (Status); diff --git a/MdeModulePkg/Core/PiSmmCore/PiSmmIpl.inf b/MdeModulePkg/Core/PiSmmCore/PiSmmIpl.inf index 6109d6b5449c..afab228cc04c 100644 --- a/MdeModulePkg/Core/PiSmmCore/PiSmmIpl.inf +++ b/MdeModulePkg/Core/PiSmmCore/PiSmmIpl.inf @@ -46,11 +46,13 @@ [LibraryClasses] DxeServicesLib PcdLib ReportStatusCodeLib + SafeIntLib [Protocols] gEfiSmmBase2ProtocolGuid ## PRODUCES gEfiSmmCommunicationProtocolGuid ## PRODUCES gEfiMmCommunication2ProtocolGuid ## PRODUCES + gEfiMmCommunication3ProtocolGuid ## PRODUCES gEfiSmmAccess2ProtocolGuid ## CONSUMES ## NOTIFY ## CONSUMES -- 2.34.1.windows.1