From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id AEA77D80101 for ; Fri, 5 Jan 2024 02:03:53 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=h6fv7Cf3Kywhd8zmWbEhCE0yg8Zyrau7BpPdZrfWQdk=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1704420232; v=1; b=lPf1UgrdThzGut3WwZHTx+oQCBRRXmS5+U5GWL8/ShoLn3nvH7By9QJt4C6Slmoui/pXGuXy Rc+tL8Qq5JN/kzkq6xvBizkc23gSIL2UGZjlZev2BXg4IEa2TMVaKuSD1epV2UgjNxIat4BoKZ2 liTk479fk5e+z6oA7I6AW8+k= X-Received: by 127.0.0.2 with SMTP id LjJrYY7687511xFkgk2Q0NkF; Thu, 04 Jan 2024 18:03:52 -0800 X-Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.24]) by mx.groups.io with SMTP id smtpd.web11.14893.1704420231517793923 for ; Thu, 04 Jan 2024 18:03:51 -0800 X-IronPort-AV: E=McAfee;i="6600,9927,10943"; a="400172578" X-IronPort-AV: E=Sophos;i="6.04,332,1695711600"; d="scan'208";a="400172578" X-Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Jan 2024 18:03:42 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10943"; a="773716649" X-IronPort-AV: E=Sophos;i="6.04,332,1695711600"; d="scan'208";a="773716649" X-Received: from zjin7-devs.sh.intel.com ([10.239.154.113]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Jan 2024 18:03:40 -0800 From: "Zhi Jin" To: devel@edk2.groups.io Cc: Zhi Jin , Ray Ni , Laszlo Ersek , Rahul Kumar , Gerd Hoffmann , Jiaxin Wu Subject: [edk2-devel] [PATCH 1/1] UefiCpuPkg/PiSmmCpuDxeSmm: Optimize PatchSmmSaveStateMap and FlushTlbForAll Date: Fri, 5 Jan 2024 10:04:09 +0800 Message-Id: <20240105020409.902684-1-zhi.jin@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,zhi.jin@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: DiScYyI6wisolEZgfVkWPszkx7686176AA= Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=lPf1Ugrd; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none) PatchSmmSaveStateMap patches the SMM entry (code) and SmmSaveState region (data) for each core, which can be improved to flush TLB once after all the memory entries have been patched. FlushTlbForAll flushes TLB for each core in serial, which can be improved to flush TLB in parrallel. Cc: Ray Ni Cc: Laszlo Ersek Cc: Rahul Kumar Cc: Gerd Hoffmann Cc: Jiaxin Wu Signed-off-by: Zhi Jin --- .../PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 95 ++++++++++++------- 1 file changed, 63 insertions(+), 32 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index 15f998e501..d4066436f5 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -547,17 +547,14 @@ FlushTlbForAll ( VOID ) { - UINTN Index; - FlushTlbOnCurrentProcessor (NULL); - - for (Index = 0; Index < gSmst->NumberOfCpus; Index++) { - if (Index != gSmst->CurrentlyExecutingCpu) { - // Force to start up AP in blocking mode, - SmmBlockingStartupThisAp (FlushTlbOnCurrentProcessor, Index, NULL); - // Do not check return status, because AP might not be present in some corner cases. - } - } + InternalSmmStartupAllAPs ( + (EFI_AP_PROCEDURE2)FlushTlbOnCurrentProcessor, + 0, + NULL, + NULL, + NULL + ); } /** @@ -799,71 +796,105 @@ PatchSmmSaveStateMap ( UINTN TileCodeSize; UINTN TileDataSize; UINTN TileSize; + UINTN PageTableBase; - TileCodeSize = GetSmiHandlerSize (); - TileCodeSize = ALIGN_VALUE (TileCodeSize, SIZE_4KB); - TileDataSize = (SMRAM_SAVE_STATE_MAP_OFFSET - SMM_PSD_OFFSET) + sizeof (SMRAM_SAVE_STATE_MAP); - TileDataSize = ALIGN_VALUE (TileDataSize, SIZE_4KB); - TileSize = TileDataSize + TileCodeSize - 1; - TileSize = 2 * GetPowerOfTwo32 ((UINT32)TileSize); + TileCodeSize = GetSmiHandlerSize (); + TileCodeSize = ALIGN_VALUE (TileCodeSize, SIZE_4KB); + TileDataSize = (SMRAM_SAVE_STATE_MAP_OFFSET - SMM_PSD_OFFSET) + sizeof (SMRAM_SAVE_STATE_MAP); + TileDataSize = ALIGN_VALUE (TileDataSize, SIZE_4KB); + TileSize = TileDataSize + TileCodeSize - 1; + TileSize = 2 * GetPowerOfTwo32 ((UINT32)TileSize); + PageTableBase = AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64; DEBUG ((DEBUG_INFO, "PatchSmmSaveStateMap:\n")); for (Index = 0; Index < mMaxNumberOfCpus - 1; Index++) { // // Code // - SmmSetMemoryAttributes ( + ConvertMemoryPageAttributes ( + PageTableBase, + mPagingMode, mCpuHotPlugData.SmBase[Index] + SMM_HANDLER_OFFSET, TileCodeSize, - EFI_MEMORY_RO + EFI_MEMORY_RO, + TRUE, + NULL ); - SmmClearMemoryAttributes ( + ConvertMemoryPageAttributes ( + PageTableBase, + mPagingMode, mCpuHotPlugData.SmBase[Index] + SMM_HANDLER_OFFSET, TileCodeSize, - EFI_MEMORY_XP + EFI_MEMORY_XP, + FALSE, + NULL ); // // Data // - SmmClearMemoryAttributes ( + ConvertMemoryPageAttributes ( + PageTableBase, + mPagingMode, mCpuHotPlugData.SmBase[Index] + SMM_HANDLER_OFFSET + TileCodeSize, TileSize - TileCodeSize, - EFI_MEMORY_RO + EFI_MEMORY_RO, + FALSE, + NULL ); - SmmSetMemoryAttributes ( + ConvertMemoryPageAttributes ( + PageTableBase, + mPagingMode, mCpuHotPlugData.SmBase[Index] + SMM_HANDLER_OFFSET + TileCodeSize, TileSize - TileCodeSize, - EFI_MEMORY_XP + EFI_MEMORY_XP, + TRUE, + NULL ); } // // Code // - SmmSetMemoryAttributes ( + ConvertMemoryPageAttributes ( + PageTableBase, + mPagingMode, mCpuHotPlugData.SmBase[mMaxNumberOfCpus - 1] + SMM_HANDLER_OFFSET, TileCodeSize, - EFI_MEMORY_RO + EFI_MEMORY_RO, + TRUE, + NULL ); - SmmClearMemoryAttributes ( + ConvertMemoryPageAttributes ( + PageTableBase, + mPagingMode, mCpuHotPlugData.SmBase[mMaxNumberOfCpus - 1] + SMM_HANDLER_OFFSET, TileCodeSize, - EFI_MEMORY_XP + EFI_MEMORY_XP, + FALSE, + NULL ); // // Data // - SmmClearMemoryAttributes ( + ConvertMemoryPageAttributes ( + PageTableBase, + mPagingMode, mCpuHotPlugData.SmBase[mMaxNumberOfCpus - 1] + SMM_HANDLER_OFFSET + TileCodeSize, SIZE_32KB - TileCodeSize, - EFI_MEMORY_RO + EFI_MEMORY_RO, + FALSE, + NULL ); - SmmSetMemoryAttributes ( + ConvertMemoryPageAttributes ( + PageTableBase, + mPagingMode, mCpuHotPlugData.SmBase[mMaxNumberOfCpus - 1] + SMM_HANDLER_OFFSET + TileCodeSize, SIZE_32KB - TileCodeSize, - EFI_MEMORY_XP + EFI_MEMORY_XP, + TRUE, + NULL ); } -- 2.39.2 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#113205): https://edk2.groups.io/g/devel/message/113205 Mute This Topic: https://groups.io/mt/103535844/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-