From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.115; helo=mga14.intel.com; envelope-from=jiewen.yao@intel.com; receiver=edk2-devel@lists.01.org Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (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 6E92C223972A3 for ; Tue, 6 Feb 2018 22:55:42 -0800 (PST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Feb 2018 23:01:25 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,471,1511856000"; d="scan'208";a="199309670" Received: from fmsmsx105.amr.corp.intel.com ([10.18.124.203]) by orsmga005.jf.intel.com with ESMTP; 06 Feb 2018 23:01:25 -0800 Received: from fmsmsx119.amr.corp.intel.com (10.18.124.207) by FMSMSX105.amr.corp.intel.com (10.18.124.203) with Microsoft SMTP Server (TLS) id 14.3.319.2; Tue, 6 Feb 2018 23:01:24 -0800 Received: from shsmsx104.ccr.corp.intel.com (10.239.4.70) by FMSMSX119.amr.corp.intel.com (10.18.124.207) with Microsoft SMTP Server (TLS) id 14.3.319.2; Tue, 6 Feb 2018 23:01:24 -0800 Received: from shsmsx102.ccr.corp.intel.com ([169.254.2.124]) by SHSMSX104.ccr.corp.intel.com ([169.254.5.125]) with mapi id 14.03.0319.002; Wed, 7 Feb 2018 15:01:22 +0800 From: "Yao, Jiewen" To: "Wang, Jian J" , "edk2-devel@lists.01.org" CC: "Dong, Eric" , Laszlo Ersek , "Ni, Ruiyu" Thread-Topic: [PATCH v2] UefiCpuPkg/PiSmmCpuDxeSmm: fix infinite loop issue in SMM profile Thread-Index: AQHTnyYl3il2t0C5r0W8REBjRRQUZqOYhJBg Date: Wed, 7 Feb 2018 07:01:21 +0000 Message-ID: <74D8A39837DF1E4DA445A8C0B3885C503AAB7C27@shsmsx102.ccr.corp.intel.com> References: <20180206084016.18408-1-jian.j.wang@intel.com> In-Reply-To: <20180206084016.18408-1-jian.j.wang@intel.com> Accept-Language: zh-CN, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiODk1OWNkOGMtMzM3NC00ZDQ0LThhMTEtM2VjMzBmNGUwMGE1IiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX05UIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE3LjIuNS4xOCIsIlRydXN0ZWRMYWJlbEhhc2giOiJrRzlnczZPSG5HcHlNc05jXC9jaVwvT1FPOVJWU0VHbFdQb0NkbW55eW11QUlXMkE0SlVJdHVLa1ZsWVwvV0JpZ1hMIn0= x-ctpclassification: CTP_NT dlp-product: dlpe-windows dlp-version: 11.0.0.116 dlp-reaction: no-action x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [PATCH v2] UefiCpuPkg/PiSmmCpuDxeSmm: fix infinite loop issue in SMM profile X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 07 Feb 2018 06:55:42 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Jiewen.yao@intel.com > -----Original Message----- > From: Wang, Jian J > Sent: Tuesday, February 6, 2018 4:40 PM > To: edk2-devel@lists.01.org > Cc: Dong, Eric ; Laszlo Ersek ; N= i, > Ruiyu ; Yao, Jiewen > Subject: [PATCH v2] UefiCpuPkg/PiSmmCpuDxeSmm: fix infinite loop issue in > SMM profile >=20 > > v2: > > Reduce the number of page to update/restore from 3 to 2 because DF > > has no effect in this issue. >=20 > The infinite loop is caused by the memory instruction, such as > "rep mov", operating on memory block crossing boundary of NON-PRESENT > pages. Because the address triggering page fault set in CR2 will be in > the first page, SmmProfilePFHandler() will only change the first page > into PRESENT. The page following will be still in NON-PRESENT status. >=20 > Since SmmProfilePFHandler() will setup single-step trap for the > instruction causing #PF, when the handler returns back to the > instruction and re-execute it, both #DB and #PF will be triggered > because the instruction wants to access both first and second page > but only first page is PRESENT. >=20 > Normally #DB exception will be handled first and its handler will > change first page back to NON-PRESENT status. Then #PF is handled > and its handler will change first page to PRESENT status again and > setup another single-step for the instruction triggering #PF. Then > the whole system falls into an infinite loop and the memory operation > will never move on. >=20 > This patch fix above situation by always changing 2 pages to PRESENT > status instead of just 1 page. Those 2 pages include the page causing > #PF and the page after it. >=20 > Cc: Eric Dong > Cc: Laszlo Ersek > Cc: Ruiyu Ni > Cc: Jiewen Yao > Contributed-under: TianoCore Contribution Agreement 1.1 > Signed-off-by: Jian J Wang > --- > UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c | 21 +++++++++++++++++---- > 1 file changed, 17 insertions(+), 4 deletions(-) >=20 > diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c > b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c > index 9588eaf029..c90167f160 100644 > --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c > +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmProfile.c > @@ -1302,6 +1302,8 @@ SmmProfilePFHandler ( > { > UINT64 *PageTable; > UINT64 PFAddress; > + UINT64 RestoreAddress; > + UINTN RestorePageNumber; > UINTN CpuIndex; > UINTN Index; > UINT64 InstructionAddress; > @@ -1331,10 +1333,21 @@ SmmProfilePFHandler ( > PFAddress =3D AsmReadCr2 (); > CpuIndex =3D GetCpuIndex (); >=20 > - if (PFAddress <=3D 0xFFFFFFFF) { > - RestorePageTableBelow4G (PageTable, PFAddress, CpuIndex, ErrorCode); > - } else { > - RestorePageTableAbove4G (PageTable, PFAddress, CpuIndex, ErrorCode, > &IsValidPFAddress); > + // > + // Memory operation cross pages, like "rep mov" instruction, will caus= e > + // infinite loop between this and Debug Trap handler. We have to make = sure > + // that current page and the page followed are both in PRESENT state. > + // > + RestorePageNumber =3D 2; > + RestoreAddress =3D PFAddress; > + while (RestorePageNumber > 0) { > + if (RestoreAddress <=3D 0xFFFFFFFF) { > + RestorePageTableBelow4G (PageTable, RestoreAddress, CpuIndex, > ErrorCode); > + } else { > + RestorePageTableAbove4G (PageTable, RestoreAddress, CpuIndex, > ErrorCode, &IsValidPFAddress); > + } > + RestoreAddress +=3D EFI_PAGE_SIZE; > + RestorePageNumber--; > } >=20 > if (!IsValidPFAddress) { > -- > 2.14.1.windows.1