<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:DengXian;
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"\@DengXian";
        panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style>
</head>
<body lang="EN-US" link="blue" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">I second the request for explanation.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">- Bret <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="mso-element:para-border-div;border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="border:none;padding:0in"><b>From: </b><a href="mailto:lersek=redhat.com@groups.io">Laszlo Ersek via groups.io</a><br>
<b>Sent: </b>Monday, November 2, 2020 10:05 AM<br>
<b>To: </b><a href="mailto:w.sheng@intel.com">Sheng Wei</a>; <a href="mailto:devel@edk2.groups.io">
devel@edk2.groups.io</a><br>
<b>Cc: </b><a href="mailto:eric.dong@intel.com">Dong, Eric</a>; <a href="mailto:ray.ni@intel.com">
Ni, Ray</a>; <a href="mailto:rahul1.kumar@intel.com">Rahul Kumar</a>; <a href="mailto:jiewen.yao@intel.com">
Yao, Jiewen</a><br>
<b>Subject: </b>[EXTERNAL] Re: [edk2-devel] [PATCH v3 2/2] UefiCpuPkg/PiSmmCpuDxeSmm: Return level paging type for Internal CR3</p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-bottom:12.0pt">On 11/02/20 05:53, Sheng Wei wrote:<br>
> When the functions called from entrypoint the page table is<br>
> set to mInternalCr3, mInternalIs5LevelPaging reflects<br>
> the page table type pointed by mInternalCr3.<br>
> <br>
> REF: <a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbugzilla.tianocore.org%2Fshow_bug.cgi%3Fid%3D3015&amp;data=04%7C01%7CBret.Barkelew%40microsoft.com%7C633ad8bea21243a5479c08d87f59df4e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637399371257436398%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=J5khqRr2cyZ%2FFpOXfX4V3juer6n4wvDxTRiQos%2BGJww%3D&amp;reserved=0">
https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbugzilla.tianocore.org%2Fshow_bug.cgi%3Fid%3D3015&amp;data=04%7C01%7CBret.Barkelew%40microsoft.com%7C633ad8bea21243a5479c08d87f59df4e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637399371257436398%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=J5khqRr2cyZ%2FFpOXfX4V3juer6n4wvDxTRiQos%2BGJww%3D&amp;reserved=0</a><br>
> <br>
> Change-Id: I9e44c6385a05930850f5ba60d10ed3e391b628bb<br>
<br>
(1) None of the patches should contain such a "Change-Id" line; they are<br>
meaningless for upstream edk2.<br>
<br>
They can be removed by the maintainer just before merging, of course.<br>
<br>
<br>
(2) However, I still have absolutely no idea what the problem is.<br>
<br>
Ray provided some great feedback here:<br>
<br>
<a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fedk2.groups.io%2Fg%2Fdevel%2Fmessage%2F66617&amp;data=04%7C01%7CBret.Barkelew%40microsoft.com%7C633ad8bea21243a5479c08d87f59df4e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637399371257436398%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=1jszRyYlHbQY5Kl%2F1uDuILrUFyIqFqbGbGmR2KDx5%2BE%3D&amp;reserved=0">https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fedk2.groups.io%2Fg%2Fdevel%2Fmessage%2F66617&amp;data=04%7C01%7CBret.Barkelew%40microsoft.com%7C633ad8bea21243a5479c08d87f59df4e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637399371257436398%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=1jszRyYlHbQY5Kl%2F1uDuILrUFyIqFqbGbGmR2KDx5%2BE%3D&amp;reserved=0</a><br>
<br>
In particular:<br>
<br>
    Better to explain the case when the functions called from entrypoint<br>
    the page table is set to mInternalCr3, gSmmFeatureEnable5LevelPaging<br>
    reflects the page table type pointed by mInternalCr3.<br>
<br>
And this has not been *explained*.<br>
<br>
The commit message should include a reminder *why* we sometimes use<br>
"mInternalCr3" (i.e., when it is nonzero), and why we use the CR3<br>
register in other cases. This reminder is not related to the change in<br>
this patch, it should re-state the *original* use case for "mInternalCr3".<br>
<br>
And then in the next paragraph, the commit message should explain that,<br>
*IF* we use "mInternalCr3", rather than CR3, *then* accessing CR4 for<br>
5-level paging is wrong -- because in that case, we should save the<br>
5-level paging status similarly (I guess?) to how we save "mInternalCr3".<br>
<br>
So, on the surface, the change seems to make sense, but without knowing<br>
why we use "mInternalCr3" in the first place, I find it difficult to<br>
reason about this change.<br>
<br>
...<br>
<br>
- According to git-blame, "mInternalCr3" comes from edk2 commit<br>
3eb69b081c68 ("UefiCpuPkg/PiSmmCpu: Add Shadow Stack Support for X86<br>
SMM.", 2019-02-28), and it is related to<br>
<<a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbugzilla.tianocore.org%2Fshow_bug.cgi%3Fid%3D1521&amp;data=04%7C01%7CBret.Barkelew%40microsoft.com%7C633ad8bea21243a5479c08d87f59df4e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637399371257436398%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=0SJTtV%2BOacJuIIrwErqO1z3HVQY3MakyWsyGRwSPOMc%3D&amp;reserved=0">https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbugzilla.tianocore.org%2Fshow_bug.cgi%3Fid%3D1521&amp;data=04%7C01%7CBret.Barkelew%40microsoft.com%7C633ad8bea21243a5479c08d87f59df4e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637399371257436398%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=0SJTtV%2BOacJuIIrwErqO1z3HVQY3MakyWsyGRwSPOMc%3D&amp;reserved=0</a>>.<br>
<br>
- Also according to git-blame, the original "Enable5LevelPaging"<br>
assignment, based on "Cr4.Bits.LA57", comes from commit 4eee0cc7cc0d<br>
("UefiCpuPkg/PiSmmCpu: Enable 5 level paging when CPU supports",<br>
2019-07-12), related to<br>
<<a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbugzilla.tianocore.org%2Fshow_bug.cgi%3Fid%3D1946&amp;data=04%7C01%7CBret.Barkelew%40microsoft.com%7C633ad8bea21243a5479c08d87f59df4e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637399371257436398%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=gbfshsEySndGIWKsCkbs%2F0%2BV2tA4bPflmH0Y3xcsEVI%3D&amp;reserved=0">https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbugzilla.tianocore.org%2Fshow_bug.cgi%3Fid%3D1946&amp;data=04%7C01%7CBret.Barkelew%40microsoft.com%7C633ad8bea21243a5479c08d87f59df4e%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637399371257436398%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=gbfshsEySndGIWKsCkbs%2F0%2BV2tA4bPflmH0Y3xcsEVI%3D&amp;reserved=0</a>>.<br>
<br>
Commit 3eb69b081c68 (the "CET ShadowStack" feature) *precedes* commit<br>
4eee0cc7cc0d (the 5-level paging feature) in the git commit history.<br>
<br>
Therefore the bug was introduced commit 4eee0cc7cc0d -- the 5-level<br>
paging enablement did not take CET ShadowStack support in consideration.<br>
<br>
In other words, this patch fixes (or "completes") 5-level paging support<br>
for "CET ShadowStack".<br>
<br>
Is that correct?<br>
<br>
If so, then *WHY* is none of the expressions "CET ShadowStack" and<br>
"5-level paging" present in any of the subject lines? Why are there no<br>
references to commits 3eb69b081c68 and 4eee0cc7cc0d in the commit<br>
messages? Why does Bug 3015 not reference Bug 1521 and Bug 1946?<br>
<br>
After all, if I understand correctly, this patch covers a corner case in<br>
the intersection of two features. Why is that not documented clearly?<br>
<br>
I want to see a statement such as "if we are using a shadow stack, then<br>
we need to use a CR4 value that matches the shadow stack, for<br>
determining whether 5-level paging is enabled or not".<br>
<br>
Or *something* like this.<br>
<br>
<br>
> Signed-off-by: Sheng Wei <w.sheng@intel.com><br>
> Cc: Eric Dong <eric.dong@intel.com><br>
> Cc: Ray Ni <ray.ni@intel.com><br>
> Cc: Laszlo Ersek <lersek@redhat.com><br>
> Cc: Rahul Kumar <rahul1.kumar@intel.com><br>
> Cc: Jiewen Yao <jiewen.yao@intel.com><br>
> ---<br>
>  UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h         | 10 ++++++<br>
>  UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 42 ++++++++++++++++++++--<br>
>  UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c            |  2 ++<br>
>  3 files changed, 51 insertions(+), 3 deletions(-)<br>
> <br>
> diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h<br>
> index 7fb3a2d9e4..3eb6af62a7 100644<br>
> --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h<br>
> +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h<br>
> @@ -951,6 +951,16 @@ GetPageTableBase (<br>
>    VOID<br>
>    );<br>
>  <br>
> +/**<br>
> +  This function set the internal page table type to 5 level paging or 4 level paging.<br>
> +<br>
> +  @param Is5LevelPaging TRUE means 5 level paging. FALSE means 4 level paging.<br>
> +**/<br>
> +VOID<br>
> +SetPageTableType (<br>
> +  IN BOOLEAN  Is5LevelPaging<br>
> +  );<br>
> +<br>
>  /**<br>
>    This function sets the attributes for the memory region specified by BaseAddress and<br>
>    Length from their current attributes to the attributes specified by Attributes.<br>
> diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c<br>
> index d67f036aea..91c0fd6587 100644<br>
> --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c<br>
> +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c<br>
> @@ -33,6 +33,7 @@ PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] = {<br>
>  };<br>
>  <br>
>  UINTN  mInternalCr3;<br>
> +BOOLEAN mInternalIs5LevelPaging = FALSE;<br>
>  <br>
>  /**<br>
>    Set the internal page table base address.<br>
> @@ -65,6 +66,43 @@ GetPageTableBase (<br>
>    return (AsmReadCr3 () & PAGING_4K_ADDRESS_MASK_64);<br>
>  }<br>
>  <br>
> +/**<br>
> +  This function set the internal page table type to 5 level paging or 4 level paging.<br>
> +<br>
> +  @param Is5LevelPaging TRUE means 5 level paging. FALSE means 4 level paging.<br>
> +**/<br>
> +VOID<br>
> +SetPageTableType (<br>
> +  IN BOOLEAN  Is5LevelPaging<br>
> +  )<br>
> +{<br>
> +  mInternalIs5LevelPaging = Is5LevelPaging;<br>
> +}<br>
> +<br>
> +/**<br>
> +  Return if the page table is 5 level paging.<br>
> +<br>
> +  @return TRUE  The page table base is 5 level paging.<br>
> +  @return FALSE The page table base is 4 level paging.<br>
> +**/<br>
> +STATIC<br>
> +BOOLEAN<br>
> +Is5LevelPageTableBase (<br>
> +  VOID<br>
> +  )<br>
> +{<br>
> +  IA32_CR4              Cr4;<br>
> +<br>
> +  // If mInternalCr3 is non zero, it will not use the page table from CR3.<br>
> +  // So, return the page level type from mInternalIs5LevelPaging instead of the CR4 LA57 bit.<br>
<br>
(3) Invalid comment style. Missing empty "//" lines before and after.<br>
<br>
> +  if (mInternalCr3 != 0) {<br>
> +    return mInternalIs5LevelPaging;<br>
> +  }<br>
> +<br>
> +  Cr4.UintN = AsmReadCr4 ();<br>
> +  return (BOOLEAN) (Cr4.Bits.LA57 == 1);<br>
> +}<br>
> +<br>
>  /**<br>
>    Return length according to page attributes.<br>
>  <br>
> @@ -131,7 +169,6 @@ GetPageTableEntry (<br>
>    UINT64                *L3PageTable;<br>
>    UINT64                *L4PageTable;<br>
>    UINT64                *L5PageTable;<br>
> -  IA32_CR4              Cr4;<br>
>    BOOLEAN               Enable5LevelPaging;<br>
>  <br>
>    Index5 = ((UINTN)RShiftU64 (Address, 48)) & PAGING_PAE_INDEX_MASK;<br>
> @@ -140,8 +177,7 @@ GetPageTableEntry (<br>
>    Index2 = ((UINTN)Address >> 21) & PAGING_PAE_INDEX_MASK;<br>
>    Index1 = ((UINTN)Address >> 12) & PAGING_PAE_INDEX_MASK;<br>
>  <br>
> -  Cr4.UintN = AsmReadCr4 ();<br>
> -  Enable5LevelPaging = (BOOLEAN) (Cr4.Bits.LA57 == 1);<br>
> +  Enable5LevelPaging = Is5LevelPageTableBase();<br>
<br>
(4) Missing space character before the opening parenthesis.<br>
<br>
>  <br>
>    if (sizeof(UINTN) == sizeof(UINT64)) {<br>
>      if (Enable5LevelPaging) {<br>
> diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c<br>
> index 810985df20..6f2f4adb7d 100644<br>
> --- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c<br>
> +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c<br>
> @@ -387,6 +387,8 @@ SmmInitPageTable (<br>
>    SetSubEntriesNum (Pml4Entry, 3);<br>
>    PTEntry = Pml4Entry;<br>
>  <br>
> +  SetPageTableType(m5LevelPagingNeeded);<br>
<br>
(5) Missing space character before the opening parenthesis.<br>
<br>
> +<br>
>    if (m5LevelPagingNeeded) {<br>
>      //<br>
>      // Fill PML5 entry<br>
> <br>
<br>
Laszlo<br>
<br>
<br>
<br>
<br>
<br>
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>


 <div width="1" style="color:white;clear:both">_._,_._,_</div> <hr> Groups.io Links:<p>   You receive all messages sent to this group.    <p> <a target="_blank" href="https://edk2.groups.io/g/devel/message/66873">View/Reply Online (#66873)</a> |    |  <a target="_blank" href="https://groups.io/mt/77988646/1813853">Mute This Topic</a>  | <a href="https://edk2.groups.io/g/devel/post">New Topic</a><br>    <a href="https://edk2.groups.io/g/devel/editsub/1813853">Your Subscription</a> | <a href="mailto:devel+owner@edk2.groups.io">Contact Group Owner</a> |  <a href="https://edk2.groups.io/g/devel/unsub">Unsubscribe</a>  [edk2-devel-archive@redhat.com]<br> <div width="1" style="color:white;clear:both">_._,_._,_</div>