From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.20; helo=mga02.intel.com; envelope-from=ruiyu.ni@intel.com; receiver=edk2-devel@lists.01.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 6A0DF202E6180 for ; Mon, 16 Oct 2017 18:43:22 -0700 (PDT) Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 Oct 2017 18:46:57 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.43,389,1503385200"; d="scan'208";a="161228237" Received: from ray-dev.ccr.corp.intel.com ([10.239.9.7]) by orsmga005.jf.intel.com with ESMTP; 16 Oct 2017 18:46:56 -0700 From: Ruiyu Ni To: edk2-devel@lists.01.org Cc: Steven Shi , Laszlo Ersek Date: Tue, 17 Oct 2017 09:46:54 +0800 Message-Id: <20171017014654.274700-1-ruiyu.ni@intel.com> X-Mailer: git-send-email 2.12.2.windows.2 Subject: [PATCH] UefiCpuPkg/MtrrLib: Fix MtrrDebugPrintAllMtrrsWorker to avoid hang X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 17 Oct 2017 01:43:22 -0000 ARRAY_SIZE(Mtrrs->Variables.Mtrr) was used in MtrrDebugPrintAllMtrrsWorker() to parse the MTRR registers. Instead, the actual variable MTRR count should be used. Otherwise, the uninitialized random data in MtrrSetting may cause MtrrLibSetMemoryType() hang. Steven Shi found this bug in QEMU when using Q35 chip. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ruiyu Ni Cc: Steven Shi Cc: Laszlo Ersek --- UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c index 2fd1d0153e..cb22558103 100644 --- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c +++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c @@ -2776,6 +2776,7 @@ MtrrDebugPrintAllMtrrsWorker ( UINTN RangeCount; UINT64 MtrrValidBitsMask; UINT64 MtrrValidAddressMask; + UINT32 VariableMtrrCount; MTRR_MEMORY_RANGE Ranges[ ARRAY_SIZE (mMtrrLibFixedMtrrTable) * sizeof (UINT64) + 2 * ARRAY_SIZE (Mtrrs->Variables.Mtrr) + 1 ]; @@ -2785,6 +2786,8 @@ MtrrDebugPrintAllMtrrsWorker ( return; } + VariableMtrrCount = GetVariableMtrrCountWorker (); + if (MtrrSetting != NULL) { Mtrrs = MtrrSetting; } else { @@ -2802,7 +2805,7 @@ MtrrDebugPrintAllMtrrsWorker ( DEBUG((DEBUG_CACHE, "Fixed MTRR[%02d] : %016lx\n", Index, Mtrrs->Fixed.Mtrr[Index])); } - for (Index = 0; Index < ARRAY_SIZE (Mtrrs->Variables.Mtrr); Index++) { + for (Index = 0; Index < VariableMtrrCount; Index++) { if (((MSR_IA32_MTRR_PHYSMASK_REGISTER *)&Mtrrs->Variables.Mtrr[Index].Mask)->Bits.V == 0) { // // If mask is not valid, then do not display range @@ -2829,11 +2832,11 @@ MtrrDebugPrintAllMtrrsWorker ( RangeCount = 1; MtrrLibGetRawVariableRanges ( - &Mtrrs->Variables, ARRAY_SIZE (Mtrrs->Variables.Mtrr), + &Mtrrs->Variables, VariableMtrrCount, MtrrValidBitsMask, MtrrValidAddressMask, RawVariableRanges ); MtrrLibApplyVariableMtrrs ( - RawVariableRanges, ARRAY_SIZE (RawVariableRanges), + RawVariableRanges, VariableMtrrCount, Ranges, ARRAY_SIZE (Ranges), &RangeCount ); -- 2.12.2.windows.2