public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Lendacky, Thomas via groups.io" <thomas.lendacky=amd.com@groups.io>
To: devel@edk2.groups.io, ray.ni@intel.com, "Kinney,
	Michael D" <michael.d.kinney@intel.com>,
	"Gao, Liming" <liming.gao@intel.com>,
	"Liu, Zhiguang" <zhiguang.liu@intel.com>,
	"Dong, Eric" <eric.dong@intel.com>,
	"Kumar, Rahul R" <rahul.r.kumar@intel.com>,
	Gerd Hoffmann <kraxel@redhat.com>,
	Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Michael Roth <michael.roth@amd.com>
Subject: Re: [edk2-devel] [PATCH 1/2] UefiCpuPkg/MpInitLib: Use AsmCpuidEx() for CPUID_EXTENDED_TOPOLOGY leaf
Date: Sat, 20 Jan 2024 10:03:29 -0600	[thread overview]
Message-ID: <72123232-5a27-7778-591d-69fc7fda0881@amd.com> (raw)
In-Reply-To: <MN6PR11MB8244774B15D9B80C2701DFEC8C772@MN6PR11MB8244.namprd11.prod.outlook.com>

On 1/20/24 00:48, Ni, Ray via groups.io wrote:
> Mike, Tom,
> How about AsmCpuId() adds ASSERT() to reject those CPUID leaves that do not have sub-leaf?

Do you mean those CPUID leaves that do have a sub-leaf?

The worry I have here is that it becomes a maintenance concern having to 
update the list of leaves with sub-leaves whenever a new CPUID leaf with 
sub-leaves is introduced.

> 
> Another concern I have if AsmCpuid() zeros ECX is callers would call AsmCpuid(leaf) instead of AsmCpuidEx (leaf, 0).
> This would cause the caller code a mess.

I think it's just a mindset that I have from kernel programming. In Linux, 
the AsmCpuid() equivalent will explicitly zero the ECX register so that 
you get the zero sub-leaf if that leaf has sub-leaves.

Using AsmCpuid() where ECX is non-zero and possibly random, could make 
debugging harder. By zeroing ECX, you will get repeatable return values to 
help with debugging when things aren't going right.

You could then make note that AsmCpuid() always returns the zero sub-leaf 
of any CPUID leaf that has sub-leaves.

I agree in principal that if the CPUID leaf has sub-leaves you should be 
using AsmCpuidEx(). But as Mike points out, if you happen to use 
AsmCpuid() by mistake, then the behavior and returned values are not 
deterministic if ECX is not zeroed.

Thanks,
Tom

> 
> Thanks,
> Ray
> From: Kinney, Michael D <michael.d.kinney@intel.com>
> Sent: Saturday, January 20, 2024 9:49 AM
> To: Ni, Ray <ray.ni@intel.com>; Tom Lendacky <thomas.lendacky@amd.com>; devel@edk2.groups.io; Gao, Liming <liming.gao@intel.com>; Liu, Zhiguang <zhiguang.liu@intel.com>; Dong, Eric <eric.dong@intel.com>; Kumar, Rahul R <rahul.r.kumar@intel.com>; Gerd Hoffmann <kraxel@redhat.com>; Ard Biesheuvel <ardb+tianocore@kernel.org>
> Cc: Michael Roth <michael.roth@amd.com>; Kinney, Michael D <michael.d.kinney@intel.com>
> Subject: RE: [edk2-devel] [PATCH 1/2] UefiCpuPkg/MpInitLib: Use AsmCpuidEx() for CPUID_EXTENDED_TOPOLOGY leaf
> 
> The issue is if AsmCpuid() is called for an Index value that does depend on ECX.  That would be a bug on the caller's part and would not have deterministic behavior because ECX on input is not deterministic.  That is the condition that would be good to catch.
> 
> Mike
> 
> From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> Sent: Friday, January 19, 2024 3:49 PM
> To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; Tom Lendacky <thomas.lendacky@amd.com<mailto:thomas.lendacky@amd.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Gao, Liming <liming.gao@intel.com<mailto:liming.gao@intel.com>>; Liu, Zhiguang <zhiguang.liu@intel.com<mailto:zhiguang.liu@intel.com>>; Dong, Eric <eric.dong@intel.com<mailto:eric.dong@intel.com>>; Kumar, Rahul R <rahul.r.kumar@intel.com<mailto:rahul.r.kumar@intel.com>>; Gerd Hoffmann <kraxel@redhat.com<mailto:kraxel@redhat.com>>; Ard Biesheuvel <ardb+tianocore@kernel.org<mailto:ardb+tianocore@kernel.org>>
> Cc: Michael Roth <michael.roth@amd.com<mailto:michael.roth@amd.com>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
> Subject: Re: [edk2-devel] [PATCH 1/2] UefiCpuPkg/MpInitLib: Use AsmCpuidEx() for CPUID_EXTENDED_TOPOLOGY leaf
> 
> Mike,
> For a certain Cupid leaf that does not have sub leaf, Cupid instruction does not consume ecx and it always fills ecx with a determined value, defined by sdm.
> So, I don't see any hurt to deterministic behavior if not zeroing ecx in AsmCpuid.
> 
> thanks,
> ray
> ________________________________
> From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
> Sent: Saturday, January 20, 2024 7:16:14 AM
> To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; Tom Lendacky <thomas.lendacky@amd.com<mailto:thomas.lendacky@amd.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>>; Gao, Liming <liming.gao@intel.com<mailto:liming.gao@intel.com>>; Liu, Zhiguang <zhiguang.liu@intel.com<mailto:zhiguang.liu@intel.com>>; Dong, Eric <eric.dong@intel.com<mailto:eric.dong@intel.com>>; Kumar, Rahul R <rahul.r.kumar@intel.com<mailto:rahul.r.kumar@intel.com>>; Gerd Hoffmann <kraxel@redhat.com<mailto:kraxel@redhat.com>>; Ard Biesheuvel <ardb+tianocore@kernel.org<mailto:ardb+tianocore@kernel.org>>
> Cc: Michael Roth <michael.roth@amd.com<mailto:michael.roth@amd.com>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
> Subject: RE: [edk2-devel] [PATCH 1/2] UefiCpuPkg/MpInitLib: Use AsmCpuidEx() for CPUID_EXTENDED_TOPOLOGY leaf
> 
> Hi Ray,
> 
> It is about having deterministic behavior if a call if made for
> a CPUID EAX value that does depend on ECX.  If ECX is not zeroed,
> then it will have a random value that may return different
> information.
> 
> The problem statement from Tom is not about zeroing ECX.  It is
> about avoiding code bugs where AsmCpuid() is called for an Index
> value that is documented to depend on ECX.  In this case, we would
> want an error condition so the developer knows they should use
> AsmCpuidEx() instead.
> 
>  From looking at the Intel SDM, there is a small set of Index
> values that do not look at ECX at all.  We could consider
> adding an ASSERT() condition in AsmCpuid() if Index is
> a value that depends on ECX.  Perhaps in DEBUG_CODE() so
> it is not always present.
> 
> Mike
> 
>> -----Original Message-----
>> From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
>> Sent: Friday, January 19, 2024 2:01 AM
>> To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; Tom Lendacky
>> <thomas.lendacky@amd.com<mailto:thomas.lendacky@amd.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Gao, Liming
>> <liming.gao@intel.com<mailto:liming.gao@intel.com>>; Liu, Zhiguang <zhiguang.liu@intel.com<mailto:zhiguang.liu@intel.com>>; Dong,
>> Eric <eric.dong@intel.com<mailto:eric.dong@intel.com>>; Kumar, Rahul R <rahul.r.kumar@intel.com<mailto:rahul.r.kumar@intel.com>>;
>> Gerd Hoffmann <kraxel@redhat.com<mailto:kraxel@redhat.com>>; Ard Biesheuvel
>> <ardb+tianocore@kernel.org<mailto:ardb+tianocore@kernel.org>>
>> Cc: Michael Roth <michael.roth@amd.com<mailto:michael.roth@amd.com>>
>> Subject: RE: [edk2-devel] [PATCH 1/2] UefiCpuPkg/MpInitLib: Use
>> AsmCpuidEx() for CPUID_EXTENDED_TOPOLOGY leaf
>>
>> Mike,
>> I agree with your words after "However".
>> Zeroing ECX in AsmCpuid() is confusing to future code maintainer: If
>> CPUID instruction does
>> not consume "ECX", why is it needed to zero "ECX"?
>>
>> Thanks,
>> Ray
>>> -----Original Message-----
>>> From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
>>> Sent: Friday, January 19, 2024 7:11 AM
>>> To: Tom Lendacky <thomas.lendacky@amd.com<mailto:thomas.lendacky@amd.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>;
>>> Gao, Liming <liming.gao@intel.com<mailto:liming.gao@intel.com>>; Liu, Zhiguang
>> <zhiguang.liu@intel.com<mailto:zhiguang.liu@intel.com>>;
>>> Dong, Eric <eric.dong@intel.com<mailto:eric.dong@intel.com>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; Kumar,
>> Rahul R
>>> <rahul.r.kumar@intel.com<mailto:rahul.r.kumar@intel.com>>; Gerd Hoffmann <kraxel@redhat.com<mailto:kraxel@redhat.com>>; Ard
>>> Biesheuvel <ardb+tianocore@kernel.org<mailto:ardb+tianocore@kernel.org>>
>>> Cc: Michael Roth <michael.roth@amd.com<mailto:michael.roth@amd.com>>; Kinney, Michael D
>>> <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
>>> Subject: RE: [edk2-devel] [PATCH 1/2] UefiCpuPkg/MpInitLib: Use
>>> AsmCpuidEx() for CPUID_EXTENDED_TOPOLOGY leaf
>>>
>>> Hi Tom,
>>>
>>> I do not see any harm in zeroing ECX in AsmCpuid().
>>>
>>> If it is not zeroed, then it would have an undefined value.
>>>
>>> However, calling AsmCpuid() for any Index that evaluates ECX
>>> (including a check for 0) should never be done.  If ECX is
>>> evaluated for a given Index, then AsmCpuIdEx() must be used.
>>>
>>> Mike
>>>
>>>> -----Original Message-----
>>>> From: Tom Lendacky <thomas.lendacky@amd.com<mailto:thomas.lendacky@amd.com>>
>>>> Sent: Wednesday, January 17, 2024 1:26 PM
>>>> To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Kinney, Michael D
>>>> <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; Gao, Liming <liming.gao@intel.com<mailto:liming.gao@intel.com>>;
>> Liu,
>>>> Zhiguang <zhiguang.liu@intel.com<mailto:zhiguang.liu@intel.com>>; Dong, Eric <eric.dong@intel.com<mailto:eric.dong@intel.com>>;
>> Ni,
>>>> Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; Kumar, Rahul R <rahul.r.kumar@intel.com<mailto:rahul.r.kumar@intel.com>>;
>> Gerd
>>>> Hoffmann <kraxel@redhat.com<mailto:kraxel@redhat.com>>; Ard Biesheuvel
>>> <ardb+tianocore@kernel.org<mailto:ardb+tianocore@kernel.org>>
>>>> Cc: Michael Roth <michael.roth@amd.com<mailto:michael.roth@amd.com>>
>>>> Subject: Re: [edk2-devel] [PATCH 1/2] UefiCpuPkg/MpInitLib: Use
>>>> AsmCpuidEx() for CPUID_EXTENDED_TOPOLOGY leaf
>>>>
>>>> On 11/28/23 08:35, Lendacky, Thomas via groups.io wrote:
>>>>> On 11/6/23 17:15, Tom Lendacky wrote:
>>>>>> On 11/6/23 16:45, Lendacky, Thomas via groups.io wrote:
>>>>>>> The CPUID_EXTENDED_TOPOLOGY CPUID leaf takes a subleaf as input
>>> when
>>>>>>> returning CPUID information. However, the AsmCpuid() function
>> does
>>>> not
>>>>>>> zero out ECX before the CPUID instruction, so the input leaf is
>> used
>>>> as
>>>>>>> the sub-leaf for the CPUID request and returns erroneous/invalid
>>>> CPUID
>>>>>>> data, since the intent of the request was to get data related to
>>>> sub-leaf
>>>>>>> 0. Instead, use AsmCpuidEx() for the CPUID_EXTENDED_TOPOLOGY
>> leaf.
>>>>>>
>>>>>> Alternatively, the AsmCpuid() function could be changed to XOR
>> ECX
>>>>>> before invoking the CPUID instruction. This would ensure that the
>> 0
>>>>>> sub-leaf is returned for any CPUID leaves that support sub-
>> leaves.
>>>>>> Thoughts?
>>>>>>
>>>>>> Adding some additional maintainers for their thoughts, too.
>>>>>
>>>>> Any thoughts on this approach (as a separate, unrelated patch) to
>>>>> eliminate future issues that could pop up?
>>>>>
>>>>> Seems like zeroing out ECX before calling CPUID would be an
>>>> appropriate
>>>>> thing to do, but I'm not sure if that will have any impact on the
>>>> existing
>>>>> code base... it shouldn't, but you never know.
>>>>
>>>> Just a re-ping for thoughts on this.
>>>>
>>>> Thanks,
>>>> Tom
>>>>
>>>>>
>>>>> Thanks,
>>>>> Tom
>>>>>
>>>>>>
>>>>>> Thanks,
>>>>>> Tom
> 
> 
> 
> 
> 
> 


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#114117): https://edk2.groups.io/g/devel/message/114117
Mute This Topic: https://groups.io/mt/102432782/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



  reply	other threads:[~2024-01-20 16:03 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-06 22:45 [edk2-devel] [PATCH 0/2] SEV-SNP guest support fixes Lendacky, Thomas via groups.io
2023-11-06 22:45 ` [edk2-devel] [PATCH 1/2] UefiCpuPkg/MpInitLib: Use AsmCpuidEx() for CPUID_EXTENDED_TOPOLOGY leaf Lendacky, Thomas via groups.io
2023-11-28 10:31   ` Ni, Ray
2023-11-06 22:45 ` [edk2-devel] [PATCH 2/2] UefiCpuPkg/MpInitLib: Copy SEV-ES save area pointer during APIC ID sorting Lendacky, Thomas via groups.io
2023-11-28 10:33   ` Ni, Ray
     [not found] ` <17952A20A9E21541.12603@groups.io>
2023-11-06 23:15   ` [edk2-devel] [PATCH 1/2] UefiCpuPkg/MpInitLib: Use AsmCpuidEx() for CPUID_EXTENDED_TOPOLOGY leaf Lendacky, Thomas via groups.io
2023-11-07  1:19     ` Michael D Kinney
2023-11-28 14:35     ` Lendacky, Thomas via groups.io
     [not found]     ` <179BD02AA4207037.22216@groups.io>
2024-01-17 21:26       ` Lendacky, Thomas via groups.io
2024-01-18 23:10         ` Michael D Kinney
2024-01-19 10:00           ` Ni, Ray
2024-01-19 23:16             ` Michael D Kinney
2024-01-19 23:48               ` Ni, Ray
2024-01-20  1:49                 ` Michael D Kinney
2024-01-20  6:48                   ` Ni, Ray
2024-01-20 16:03                     ` Lendacky, Thomas via groups.io [this message]
2024-01-21  3:00                       ` Ni, Ray
2023-11-07  9:55 ` [edk2-devel] [PATCH 0/2] SEV-SNP guest support fixes Gerd Hoffmann
2023-11-17 21:43   ` Lendacky, Thomas via groups.io
2023-11-27 19:00     ` Lendacky, Thomas via groups.io

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=72123232-5a27-7778-591d-69fc7fda0881@amd.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox