From: "Dong, Eric" <eric.dong@intel.com>
To: "devel@edk2.groups.io" <devel@edk2.groups.io>,
"lersek@redhat.com" <lersek@redhat.com>
Cc: "Ni, Ray" <ray.ni@intel.com>
Subject: Re: [edk2-devel] [Patch 3/4] UefiCpuPkg/PiSmmCpuDxeSmm: Supports detect before set new value logic.
Date: Mon, 12 Aug 2019 08:37:08 +0000 [thread overview]
Message-ID: <ED077930C258884BBCB450DB737E662259EC0D56@shsmsx102.ccr.corp.intel.com> (raw)
In-Reply-To: <0aefcf17-4050-e3fb-86a2-f721feb67760@redhat.com>
Hi Laszlo,
> -----Original Message-----
> From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf Of
> Laszlo Ersek
> Sent: Friday, August 9, 2019 11:31 PM
> To: Dong, Eric <eric.dong@intel.com>; devel@edk2.groups.io
> Cc: Ni, Ray <ray.ni@intel.com>
> Subject: Re: [edk2-devel] [Patch 3/4] UefiCpuPkg/PiSmmCpuDxeSmm:
> Supports detect before set new value logic.
>
> On 08/09/19 08:11, Eric Dong wrote:
> > REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2040
> >
> > Supports new logic which detect current value before set new value.
> > Only set new value when current value not same as new value.
> >
> > Signed-off-by: Eric Dong <eric.dong@intel.com>
> > Cc: Ray Ni <ray.ni@intel.com>
> > Cc: Laszlo Ersek <lersek@redhat.com>
> > ---
> > UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c | 135
> > ++++++++++++++++++++----------
> > 1 file changed, 92 insertions(+), 43 deletions(-)
>
> I have only superficial comments, as my understanding is that
> "UefiCpuPkg/CpuS3DataDxe", which is what OVMF uses, doesn't set up any
> register programming for S3 resume.
>
> > diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
> > b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
> > index d8c6b19ead..957f2896eb 100644
> > --- a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
> > +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
> > @@ -159,6 +159,58 @@ S3WaitForSemaphore (
> > ) != Value);
> > }
> >
> > +/**
> > + Read / write CR value.
> > +
> > + @param[in] CrIndex The CR index which need to read/write.
> > + @param[in] Read Read or write. TRUE is read.
> > + @param[in,out] CrValue CR value.
> > +
> > + @retval EFI_SUCCESS means read/write success, else return
> EFI_UNSUPPORTED.
> > +**/
> > +UINTN
> > +ReadWriteCr (
> > + IN UINT32 CrIndex,
> > + IN BOOLEAN Read,
> > + IN OUT UINTN *CrValue
> > + )
> > +{
> > + switch (CrIndex) {
> > + case 0:
> > + if (Read) {
> > + *CrValue = AsmReadCr0 ();
> > + } else {
> > + AsmWriteCr0 (*CrValue);
> > + }
> > + break;
> > + case 2:
> > + if (Read) {
> > + *CrValue = AsmReadCr2 ();
> > + } else {
> > + AsmWriteCr2 (*CrValue);
> > + }
> > + break;
> > + case 3:
> > + if (Read) {
> > + *CrValue = AsmReadCr3 ();
> > + } else {
> > + AsmWriteCr3 (*CrValue);
> > + }
> > + break;
> > + case 4:
> > + if (Read) {
> > + *CrValue = AsmReadCr4 ();
> > + } else {
> > + AsmWriteCr4 (*CrValue);
> > + }
> > + break;
> > + default:
> > + return EFI_UNSUPPORTED;;
> > + }
> > +
> > + return EFI_SUCCESS;
> > +}
> > +
> > /**
> > Initialize the CPU registers from a register table.
> >
> > @@ -188,6 +240,8 @@ ProgramProcessorRegister (
> > UINTN ProcessorIndex;
> > UINTN ValidThreadCount;
> > UINT32 *ValidCoreCountPerPackage;
> > + EFI_STATUS Status;
> > + UINT64 CurrentValue;
> >
> > //
> > // Traverse Register Table of this logical processor @@ -206,55
> > +260,50 @@ ProgramProcessorRegister (
> > // The specified register is Control Register
> > //
> > case ControlRegister:
> > - switch (RegisterTableEntry->Index) {
> > - case 0:
> > - Value = AsmReadCr0 ();
> > - Value = (UINTN) BitFieldWrite64 (
> > - Value,
> > - RegisterTableEntry->ValidBitStart,
> > - RegisterTableEntry->ValidBitStart + RegisterTableEntry-
> >ValidBitLength - 1,
> > - (UINTN) RegisterTableEntry->Value
> > - );
> > - AsmWriteCr0 (Value);
> > - break;
> > - case 2:
> > - Value = AsmReadCr2 ();
> > - Value = (UINTN) BitFieldWrite64 (
> > - Value,
> > - RegisterTableEntry->ValidBitStart,
> > - RegisterTableEntry->ValidBitStart + RegisterTableEntry-
> >ValidBitLength - 1,
> > - (UINTN) RegisterTableEntry->Value
> > - );
> > - AsmWriteCr2 (Value);
> > - break;
> > - case 3:
> > - Value = AsmReadCr3 ();
> > - Value = (UINTN) BitFieldWrite64 (
> > - Value,
> > - RegisterTableEntry->ValidBitStart,
> > - RegisterTableEntry->ValidBitStart + RegisterTableEntry-
> >ValidBitLength - 1,
> > - (UINTN) RegisterTableEntry->Value
> > - );
> > - AsmWriteCr3 (Value);
> > - break;
> > - case 4:
> > - Value = AsmReadCr4 ();
> > - Value = (UINTN) BitFieldWrite64 (
> > - Value,
> > - RegisterTableEntry->ValidBitStart,
> > - RegisterTableEntry->ValidBitStart + RegisterTableEntry-
> >ValidBitLength - 1,
> > - (UINTN) RegisterTableEntry->Value
> > - );
> > - AsmWriteCr4 (Value);
> > - break;
> > - default:
> > - break;
> > + Status = ReadWriteCr(RegisterTableEntry->Index, TRUE, &Value);
>
> (1) Space missing right after "ReadWriteCr".
1. Will fix it in my next version change.
>
> > + if (EFI_ERROR (Status)) {
> > + return;
> > + }
>
> (2) This changes the control flow.
>
> Previously, a CR reference different from CR0, CR2, CR3, and CR4 would allow
> the loop to process further entries from the register table.
>
> This change could be justified, but then it needs to be in a separate patch.
2. Good catch, this is not an expect change. Should use "continue" for it. Will update it in my next change.
>
> > + if (RegisterTableEntry->DetectIt) {
> > + CurrentValue = BitFieldRead64(
> > + Value,
> > + RegisterTableEntry->ValidBitStart,
> > + RegisterTableEntry->ValidBitStart + RegisterTableEntry-
> >ValidBitLength - 1
> > + );
> > + if (CurrentValue == RegisterTableEntry->Value) {
> > + return;
> > + }
>
> (3) Same comment here -- if the value retrieved from the recognized register
> diverges from the expected value, is that grounds enough for terminating
> the processing?
3. Good catch, here should also use "continue". Will update it in my next patch.
>
> I'd suggest splitting up this patch.
>
> - One patch could be factoring out ReadWriteCr(), without changes in
> functionality.
>
> - Another patch could be the early return, if that is not a bug in the present
> patch, but an intended change.
>
> - Another patch could be the Compare-And-Set logic.
4. I will generate the factoring out of ReadWriteCr change to a separate one in my next version changes.
>
> > }
> > + Value = (UINTN) BitFieldWrite64 (
> > + Value,
> > + RegisterTableEntry->ValidBitStart,
> > + RegisterTableEntry->ValidBitStart + RegisterTableEntry-
> >ValidBitLength - 1,
> > + RegisterTableEntry->Value
> > + );
> > + ReadWriteCr (RegisterTableEntry->Index, FALSE, &Value);
> > break;
> > //
> > // The specified register is Model Specific Register
> > //
> > case Msr:
> > + if (RegisterTableEntry->DetectIt) {
> > + Value = (UINTN)AsmReadMsr64 (RegisterTableEntry->Index);
> > + if (RegisterTableEntry->ValidBitLength >= 64) {
> > + if (Value == RegisterTableEntry->Value) {
> > + return;
> > + }
> > + } else {
> > + CurrentValue = BitFieldRead64(
> > + Value,
> > + RegisterTableEntry->ValidBitStart,
> > + RegisterTableEntry->ValidBitStart + RegisterTableEntry-
> >ValidBitLength - 1
> > + );
> > + if (CurrentValue == RegisterTableEntry->Value) {
> > + return;
> > + }
> > + }
> > + }
> > +
> > //
> > // If this function is called to restore register setting after INIT signal,
> > // there is no need to restore MSRs in register table.
> >
>
> (4) "early return" issue again -- if the MSR has the intended value already,
> that's likely no reason for ignoring the rest of the register table. I guess the
> "continue" statement could be useful.
5. yes, it should use "continue" for this case, will update it in my next version change.
>
> (5) I would suggest splitting the MSR update to a separate patch as well.
6. The logic should use "continue" instead of "return". This is follow the original logic. So I will not separate the change to different patches.
Thanks,
Eric
>
> Thanks
> Laszlo
>
>
next prev parent reply other threads:[~2019-08-12 8:37 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-09 6:11 [Patch 0/4] Add "Test then Set" mechanism Dong, Eric
2019-08-09 6:11 ` [Patch 1/4] UefiCpuPkg/RegisterCpuFeaturesLib: Add "detect before set" Micros Dong, Eric
2019-08-09 15:14 ` Laszlo Ersek
2019-08-12 7:46 ` [edk2-devel] " Dong, Eric
2019-08-12 13:01 ` Laszlo Ersek
2019-08-09 15:31 ` Laszlo Ersek
2019-08-12 8:05 ` Dong, Eric
2019-08-09 6:11 ` [Patch 2/4] UefiCpuPkg/RegisterCpuFeaturesLib: Supports detect before set new value logic Dong, Eric
2019-08-09 6:11 ` [Patch 3/4] UefiCpuPkg/PiSmmCpuDxeSmm: " Dong, Eric
2019-08-09 15:30 ` Laszlo Ersek
2019-08-12 8:37 ` Dong, Eric [this message]
2019-08-09 6:11 ` [Patch 4/4] UefiCpuPkg/CpuCommonFeaturesLib: Use "Test then set" action Dong, Eric
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=ED077930C258884BBCB450DB737E662259EC0D56@shsmsx102.ccr.corp.intel.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