From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) by mx.groups.io with SMTP id smtpd.web09.35321.1629176932364158385 for ; Mon, 16 Aug 2021 22:08:52 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@gmail.com header.s=20161025 header.b=HmQIihhT; spf=pass (domain: gmail.com, ip: 209.85.216.41, mailfrom: kuqin12@gmail.com) Received: by mail-pj1-f41.google.com with SMTP id oa17so30186056pjb.1 for ; Mon, 16 Aug 2021 22:08:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4yfTCBxTaiN72lpbHpieQzpwo3SetA2jMnLW9ZmPMbw=; b=HmQIihhTtWS0fI5f1MlzDU5rTQHTF+9DmOEyWAqeo1ID76LeYA5IfvbiCzapzp2x+A TuCTU07VHL5phTDxYGbSLNKT2bll1X7qXN5jfumLJN/zkabMNoL/h6pyheb5pRS1kFpV VOVkIfBTKflT0Px8G3NwCUVhi5DF3oay3p/aS7GJZHoryUIP2v17HENiGmD4cc3A9hnI SDi72AdDVUn5MR2hT6e0+dpSA7JP1biARuFzfBqzLSxtUSa1D3pXfgFngqumwJeOpVKa EAE4PBtBEfKC+91Tl4dtcyl5vqYaXsAxqGC5P404jxkLAacLUHb/+5GmJV7MySeZR/TO 7NUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4yfTCBxTaiN72lpbHpieQzpwo3SetA2jMnLW9ZmPMbw=; b=X4SNba2KNFVhbQ1xIsS4w8vGCNnTzhGR2ochCOnRU1M00/DsQ1onRbvc+oodEnNtPE oh73BD1+6EO4VChTKh/rzUJo0RL0r7rseFO8YT80A+5Ktsc2oLOgUOP6BtCNhRua7234 nDLu8w7kYfXgNeKMNfKQ52XkDdF5nJX6r9xEcAzo7pHjvxBa4ILMRmxIA/fHDlAt7QcE 12n4L1odUYmV405syTdaqnSh0go0QQdwsEISRo9xeY88u59RBngjbJDBqyvkWHA71x81 zKSt4WgQuIbe0yTsGF67V43w699gGTZ0Kr0INuIOv0YVDc8y0ROMfyxbCQ21nX+Rk9IV CrMQ== X-Gm-Message-State: AOAM533q3CACFkpCfTTKOiEkc3NPKaZ5KNbdrXps8I3Pa/tsPAaz7Un1 nHSQnwTDLiNqiP2LcS1QeRhout7x5a0A0w== X-Google-Smtp-Source: ABdhPJx2uAg6e+EZdZhiBuHqVPWWSa0vcn1jvIsdc5N1sJlxoDTj9rXRa+e8AEBzFUR2moB4H2M8gA== X-Received: by 2002:a17:90a:6684:: with SMTP id m4mr1697537pjj.226.1629176931598; Mon, 16 Aug 2021 22:08:51 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([50.35.88.161]) by smtp.gmail.com with ESMTPSA id i5sm737965pjk.47.2021.08.16.22.08.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Aug 2021 22:08:50 -0700 (PDT) From: "Kun Qin" To: devel@edk2.groups.io Cc: Jian J Wang , Hao A Wu , Eric Dong , Ray Ni Subject: [PATCH v3 7/7] MdeModulePkg: PiSmmIpl: Update MessageLength calculation for MmCommunicate Date: Mon, 16 Aug 2021 22:08:07 -0700 Message-Id: <20210817050807.766-8-kuqin12@gmail.com> X-Mailer: git-send-email 2.32.0.windows.1 In-Reply-To: <20210817050807.766-1-kuqin12@gmail.com> References: <20210817050807.766-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 MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c | 187 ++++++++++++++++++++ MdeModulePkg/Core/PiSmmCore/PiSmmIpl.inf | 2 + 2 files changed, 189 insertions(+) diff --git a/MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c b/MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c index 599a0cd01d80..356efa172cfd 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. @@ -649,6 +693,148 @@ SmmCommunicationMmCommunicate2 ( CommSize); } +/** + 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. @@ -1832,6 +2018,7 @@ SmmIplEntry ( &gEfiSmmBase2ProtocolGuid, &mSmmBase2, &gEfiSmmCommunicationProtocolGuid, &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.32.0.windows.1