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.100; helo=mga07.intel.com; envelope-from=hao.a.wu@intel.com; receiver=edk2-devel@lists.01.org Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) (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 2FC8321E1B776 for ; Wed, 18 Oct 2017 22:46:36 -0700 (PDT) Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP; 18 Oct 2017 22:50:13 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.43,400,1503385200"; d="scan'208";a="164860283" Received: from fmsmsx103.amr.corp.intel.com ([10.18.124.201]) by fmsmga006.fm.intel.com with ESMTP; 18 Oct 2017 22:50:13 -0700 Received: from fmsmsx151.amr.corp.intel.com (10.18.125.4) by FMSMSX103.amr.corp.intel.com (10.18.124.201) with Microsoft SMTP Server (TLS) id 14.3.319.2; Wed, 18 Oct 2017 22:50:13 -0700 Received: from shsmsx103.ccr.corp.intel.com (10.239.4.69) by FMSMSX151.amr.corp.intel.com (10.18.125.4) with Microsoft SMTP Server (TLS) id 14.3.319.2; Wed, 18 Oct 2017 22:50:12 -0700 Received: from shsmsx104.ccr.corp.intel.com ([169.254.5.152]) by SHSMSX103.ccr.corp.intel.com ([169.254.4.213]) with mapi id 14.03.0319.002; Thu, 19 Oct 2017 13:50:11 +0800 From: "Wu, Hao A" To: "Ni, Ruiyu" , "edk2-devel@lists.01.org" Thread-Topic: [PATCH] UefiCpuPkg/MtrrLib: Fix bug that may incorrectly set <1MB attribute Thread-Index: AQHTSITP2ehdyybZFUaZpWcotuu5x6Lqqvng Date: Thu, 19 Oct 2017 05:50:10 +0000 Message-ID: References: <20171019024849.346408-1-ruiyu.ni@intel.com> In-Reply-To: <20171019024849.346408-1-ruiyu.ni@intel.com> Accept-Language: zh-CN, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [PATCH] UefiCpuPkg/MtrrLib: Fix bug that may incorrectly set <1MB attribute 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: Thu, 19 Oct 2017 05:46:36 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Hao Wu Best Regards, Hao Wu > -----Original Message----- > From: Ni, Ruiyu > Sent: Thursday, October 19, 2017 10:49 AM > To: edk2-devel@lists.01.org > Cc: Wu, Hao A > Subject: [PATCH] UefiCpuPkg/MtrrLib: Fix bug that may incorrectly set <1M= B > attribute >=20 > MtrrLibSetBelow1MBMemoryAttribute() may be called multiple times. > It's possible that in a 2nd call, Modified[0] is set to TRUE in > 1st call but ClearMasks[0] and OrMasks[0] is uninitialized in > 2nd call. It causes FixedSettings->Mtrr[0] be set to random > data. >=20 > The patch fixes this issue by introducing a local Modified[] > array and only updates FixedSettings->Mtrr[] when LocalModified[i] > is TRUE. >=20 > Contributed-under: TianoCore Contribution Agreement 1.1 > Signed-off-by: Ruiyu Ni > Cc: Hao A Wu > --- > UefiCpuPkg/Library/MtrrLib/MtrrLib.c | 19 +++++++++++++++---- > 1 file changed, 15 insertions(+), 4 deletions(-) >=20 > diff --git a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c > b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c > index cb22558103..200becdd4a 100644 > --- a/UefiCpuPkg/Library/MtrrLib/MtrrLib.c > +++ b/UefiCpuPkg/Library/MtrrLib/MtrrLib.c > @@ -2114,22 +2114,32 @@ MtrrLibSetBelow1MBMemoryAttribute ( > UINT64 OrMask; > UINT64 ClearMasks[ARRAY_SIZE (mMtrrLibFixedMtrrTabl= e)]; > UINT64 OrMasks[ARRAY_SIZE (mMtrrLibFixedMtrrTable)]= ; > + BOOLEAN LocalModified[ARRAY_SIZE (mMtrrLibFixedMtrrT= able)]; >=20 > ASSERT (BaseAddress < BASE_1MB); >=20 > + SetMem (LocalModified, sizeof (LocalModified), FALSE); > + > + // > + // (Value & ~0 | 0) still equals to (Value) > + // > + SetMem64 (ClearMasks, sizeof (ClearMasks), 0); > + SetMem64 (OrMasks, sizeof (OrMasks), 0); > + > MsrIndex =3D (UINT32)-1; > while ((BaseAddress < BASE_1MB) && (Length !=3D 0)) { > Status =3D MtrrLibProgramFixedMtrr (Type, &BaseAddress, &Length, > &MsrIndex, &ClearMask, &OrMask); > if (RETURN_ERROR (Status)) { > return Status; > } > - ClearMasks[MsrIndex] =3D ClearMask; > - OrMasks[MsrIndex] =3D OrMask; > - Modified[MsrIndex] =3D TRUE; > + ClearMasks[MsrIndex] =3D ClearMask; > + OrMasks[MsrIndex] =3D OrMask; > + Modified[MsrIndex] =3D TRUE; > + LocalModified[MsrIndex] =3D TRUE; > } >=20 > for (MsrIndex =3D 0; MsrIndex < ARRAY_SIZE (mMtrrLibFixedMtrrTable); > MsrIndex++) { > - if (Modified[MsrIndex]) { > + if (LocalModified[MsrIndex]) { > FixedSettings->Mtrr[MsrIndex] =3D (FixedSettings->Mtrr[MsrIndex] & > ~ClearMasks[MsrIndex]) | OrMasks[MsrIndex]; > } > } > @@ -2354,6 +2364,7 @@ MtrrSetMemoryAttributesInMtrrSettings ( > // > // 3. Apply the below-1MB memory attribute settings. > // > + ZeroMem (WorkingFixedSettings.Mtrr, sizeof (WorkingFixedSettings.Mtrr)= ); > for (Index =3D 0; Index < RangeCount; Index++) { > if (Ranges[Index].BaseAddress >=3D BASE_1MB) { > continue; > -- > 2.12.2.windows.2