From nobody Tue Nov 26 18:21:01 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+47093+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+47093+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1568166316; cv=none; d=zoho.com; s=zohoarc; b=idMKDr7dh/LcZj/42mCt014+Zp5IwHyGqFXogfQiFVrP/OL5ZymzO96f4doOmcz3MrdW3mWPBUGMAv0jlOBn+975pLpODEc+Bro7M9ewAs7p/vCWv5bE6a6bc54E3bYCZpPuR2vzoLtfqQnYerfwTgzywripnxKpKJzzY7IL6AY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1568166316; h=Content-Transfer-Encoding:Cc:Date:From:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To:ARC-Authentication-Results; bh=/uKJSGb128FZzsuH/GS6Ujt1xhz0TIaHbK3BFM0qUYA=; b=Zg4pj9WdXkPr3GsTmHB1dYp/h/+LigqzJYbdHW5R3WVcIDOPue4MqAEKZdSgewv/QKrOpijj21C8ys9dwobSelOWECGYA2i53jmE9gt1vRqvwau6bqx60/vAI7YVmRWpRPIrfJSobQeXBAMsZzFrspboLmtvLhP6bcCtXxnCw+s= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+47093+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1568166316263685.1271767941297; Tue, 10 Sep 2019 18:45:16 -0700 (PDT) Return-Path: X-Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by groups.io with SMTP; Tue, 10 Sep 2019 18:45:15 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Sep 2019 18:45:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,491,1559545200"; d="scan'208";a="214511538" X-Received: from ydong10-win10.ccr.corp.intel.com ([10.239.158.133]) by fmsmga002.fm.intel.com with ESMTP; 10 Sep 2019 18:45:13 -0700 From: "Dong, Eric" To: devel@edk2.groups.io Cc: Ray Ni , Laszlo Ersek Subject: [edk2-devel] [Patch] UefiCpuPkg/CpuDxe: clean up PAGE_TABLE_LIB_PAGING_CONTEXT usage. Date: Wed, 11 Sep 2019 09:45:11 +0800 Message-Id: <20190911014511.9356-1-eric.dong@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: 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,eric.dong@intel.com Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1568166315; bh=4i88B3895us/7T26qFJwtqfOfxhXhTdSPV3+dr8fVEg=; h=Cc:Date:From:Reply-To:Subject:To; b=FqmXDDiTzTtc2lUDC0/UTbilEmWz9YnXrn77Yv4pybMkg4nwdsbld/dVidncS/z7OdN Ji7oTNPdioLvSQlRoY25JcxrBOmCvqcjaHzm62rnE+FqkomgjlijaukSHNLLGg1AWvA+Q /n27qL/hpn/G85JNZGvNWDG7Q5m7TaCBdEc= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D1039 Current implementation not checks system mode before using PAGE_TABLE_LIB_PAGING_CONTEXT.ContextData.X64 or PAGE_TABLE_LIB_PAGING_CONTEXT.ContextData.Ia32. This patch check the mode before using the correct one. Cc: Ray Ni Cc: Laszlo Ersek Signed-off-by: Eric Dong --- UefiCpuPkg/CpuDxe/CpuPageTable.c | 52 +++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/UefiCpuPkg/CpuDxe/CpuPageTable.c b/UefiCpuPkg/CpuDxe/CpuPageTa= ble.c index ec5cd424fc..308f93b1cd 100644 --- a/UefiCpuPkg/CpuDxe/CpuPageTable.c +++ b/UefiCpuPkg/CpuDxe/CpuPageTable.c @@ -155,6 +155,8 @@ GetCurrentPagingContext ( MSR_IA32_EFER_REGISTER MsrEfer; IA32_CR4 Cr4; IA32_CR0 Cr0; + UINT32 *Attributes; + UINTN *PageTableBase; =20 // // Don't retrieve current paging context from processor if in SMM mode. @@ -163,29 +165,33 @@ GetCurrentPagingContext ( ZeroMem (&mPagingContext, sizeof(mPagingContext)); if (sizeof(UINTN) =3D=3D sizeof(UINT64)) { mPagingContext.MachineType =3D IMAGE_FILE_MACHINE_X64; + Attributes =3D &mPagingContext.ContextData.X64.Attributes; + PageTableBase =3D &mPagingContext.ContextData.X64.PageTableBase; } else { mPagingContext.MachineType =3D IMAGE_FILE_MACHINE_I386; + Attributes =3D &mPagingContext.ContextData.Ia32.Attributes; + PageTableBase =3D (UINTN *)&mPagingContext.ContextData.Ia32.PageTabl= eBase; } =20 Cr0.UintN =3D AsmReadCr0 (); Cr4.UintN =3D AsmReadCr4 (); =20 if (Cr0.Bits.PG !=3D 0) { - mPagingContext.ContextData.X64.PageTableBase =3D (AsmReadCr3 () & PA= GING_4K_ADDRESS_MASK_64); + *PageTableBase =3D (AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64); } else { - mPagingContext.ContextData.X64.PageTableBase =3D 0; + *PageTableBase =3D 0; } if (Cr0.Bits.WP !=3D 0) { - mPagingContext.ContextData.Ia32.Attributes |=3D PAGE_TABLE_LIB_PAGIN= G_CONTEXT_IA32_X64_ATTRIBUTES_WP_ENABLE; + *Attributes |=3D PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_W= P_ENABLE; } if (Cr4.Bits.PSE !=3D 0) { - mPagingContext.ContextData.Ia32.Attributes |=3D PAGE_TABLE_LIB_PAGIN= G_CONTEXT_IA32_X64_ATTRIBUTES_PSE; + *Attributes |=3D PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_P= SE; } if (Cr4.Bits.PAE !=3D 0) { - mPagingContext.ContextData.Ia32.Attributes |=3D PAGE_TABLE_LIB_PAGIN= G_CONTEXT_IA32_X64_ATTRIBUTES_PAE; + *Attributes |=3D PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_P= AE; } if (Cr4.Bits.LA57 !=3D 0) { - mPagingContext.ContextData.Ia32.Attributes |=3D PAGE_TABLE_LIB_PAGIN= G_CONTEXT_IA32_X64_ATTRIBUTES_5_LEVEL; + *Attributes |=3D PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_5= _LEVEL; } =20 AsmCpuid (CPUID_EXTENDED_FUNCTION, &RegEax, NULL, NULL, NULL); @@ -197,12 +203,12 @@ GetCurrentPagingContext ( MsrEfer.Uint64 =3D AsmReadMsr64(MSR_CORE_IA32_EFER); if (MsrEfer.Bits.NXE !=3D 0) { // XD activated - mPagingContext.ContextData.Ia32.Attributes |=3D PAGE_TABLE_LIB_P= AGING_CONTEXT_IA32_X64_ATTRIBUTES_XD_ACTIVATED; + *Attributes |=3D PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUT= ES_XD_ACTIVATED; } } =20 if (RegEdx.Bits.Page1GB !=3D 0) { - mPagingContext.ContextData.Ia32.Attributes |=3D PAGE_TABLE_LIB_PAG= ING_CONTEXT_IA32_X64_ATTRIBUTES_PAGE_1G_SUPPORT; + *Attributes |=3D PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES= _PAGE_1G_SUPPORT; } } } @@ -395,6 +401,7 @@ ConvertPageEntryAttribute ( { UINT64 CurrentPageEntry; UINT64 NewPageEntry; + UINT32 *PageAttributes; =20 CurrentPageEntry =3D *PageEntry; NewPageEntry =3D CurrentPageEntry; @@ -438,7 +445,13 @@ ConvertPageEntryAttribute ( break; } } - if ((PagingContext->ContextData.Ia32.Attributes & PAGE_TABLE_LIB_PAGING_= CONTEXT_IA32_X64_ATTRIBUTES_XD_ACTIVATED) !=3D 0) { + + if (PagingContext->MachineType =3D=3D IMAGE_FILE_MACHINE_X64) { + PageAttributes =3D &PagingContext->ContextData.X64.Attributes; + } else { + PageAttributes =3D &PagingContext->ContextData.Ia32.Attributes; + } + if ((*PageAttributes & PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES= _XD_ACTIVATED) !=3D 0) { if ((Attributes & EFI_MEMORY_XP) !=3D 0) { switch (PageAction) { case PageActionAssign: @@ -1338,15 +1351,24 @@ InitializePageTableLib ( ) { PAGE_TABLE_LIB_PAGING_CONTEXT CurrentPagingContext; + UINT32 *Attributes; + UINTN *PageTableBase; =20 GetCurrentPagingContext (&CurrentPagingContext); =20 + if (CurrentPagingContext.MachineType =3D=3D IMAGE_FILE_MACHINE_X64) { + Attributes =3D &CurrentPagingContext.ContextData.X64.Attributes; + PageTableBase =3D &CurrentPagingContext.ContextData.X64.PageTableBase; + } else { + Attributes =3D &CurrentPagingContext.ContextData.Ia32.Attributes; + PageTableBase =3D (UINTN *)&CurrentPagingContext.ContextData.Ia32.Page= TableBase; + } + // // Reserve memory of page tables for future uses, if paging is enabled. // - if (CurrentPagingContext.ContextData.X64.PageTableBase !=3D 0 && - (CurrentPagingContext.ContextData.Ia32.Attributes & - PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE) !=3D 0) { + if ((*PageTableBase !=3D 0) && + (*Attributes & PAGE_TABLE_LIB_PAGING_CONTEXT_IA32_X64_ATTRIBUTES_PAE= ) !=3D 0) { DisableReadOnlyPageWriteProtect (); InitializePageTablePool (1); EnableReadOnlyPageWriteProtect (); @@ -1361,10 +1383,10 @@ InitializePageTableLib ( ASSERT (mLastPFEntryPointer !=3D NULL); } =20 - DEBUG ((DEBUG_INFO, "CurrentPagingContext:\n", CurrentPagingContext.Mach= ineType)); + DEBUG ((DEBUG_INFO, "CurrentPagingContext:\n")); DEBUG ((DEBUG_INFO, " MachineType - 0x%x\n", CurrentPagingContext.Mac= hineType)); - DEBUG ((DEBUG_INFO, " PageTableBase - 0x%x\n", CurrentPagingContext.Con= textData.X64.PageTableBase)); - DEBUG ((DEBUG_INFO, " Attributes - 0x%x\n", CurrentPagingContext.Con= textData.X64.Attributes)); + DEBUG ((DEBUG_INFO, " PageTableBase - 0x%x\n", *PageTableBase)); + DEBUG ((DEBUG_INFO, " Attributes - 0x%x\n", *Attributes)); =20 return ; } --=20 2.21.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#47093): https://edk2.groups.io/g/devel/message/47093 Mute This Topic: https://groups.io/mt/34099078/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-