From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: redhat.com, ip: 209.132.183.28, mailfrom: lersek@redhat.com) Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by groups.io with SMTP; Wed, 18 Sep 2019 10:57:48 -0700 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 69082316D8C2; Wed, 18 Sep 2019 17:57:48 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-125-22.rdu2.redhat.com [10.10.125.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id B268854453; Wed, 18 Sep 2019 17:57:47 +0000 (UTC) Subject: Re: [edk2-devel] [Patch V3] UefiCpuPkg/CpuExceptionHandlerLib: Fix split lock To: devel@edk2.groups.io, john.e.lofgren@intel.com References: <20190918154323.20804-1-john.e.lofgren@intel.com> From: "Laszlo Ersek" Message-ID: <0fc23d42-0c19-6550-e642-a697c73947c1@redhat.com> Date: Wed, 18 Sep 2019 19:57:46 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20190918154323.20804-1-john.e.lofgren@intel.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Wed, 18 Sep 2019 17:57:48 +0000 (UTC) Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 09/18/19 17:43, John E Lofgren wrote: > REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2150 > V3 changes: > change to mov instruction (non locking instuction) instead > of xchg to simplify design. This patch should have been posted as "v4" actually -- it differs from what you originally posted as v3. Therefore it cannot be considered v3. The changelog in the patch would say, v4: The v3 posting didn't do what it promised to do, so do it now for real. v3: Anyway, not a deal breaker. More comments below. > V2 changes: > Add xchg 16 bit instructions to handle sgdt and sidt base > 63:48 bits and 47:32 bits. > Add comment to explain why xchg 64bit isnt being used > > Split lock happens when a locking instruction is used on mis-aligned data > that crosses two cachelines. If close source platform enables Alignment Check > Exception(#AC), They can hit a double fault due to split lock being in > CpuExceptionHandlerLib. > > sigt and sgdt saves 10 bytes to memory, 8 bytes is base and 2 bytes is limit. > The data is mis-aligned, can cross two cacheline, and a xchg > instruction(locking instuction) is being utilize. > > Signed-off-by: John E Lofgren > --- > UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm | 14 ++++++++------ > 1 file changed, 8 insertions(+), 6 deletions(-) > > diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm > index 4db1a09f28..19198f2731 100644 > --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm > +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm > @@ -184,17 +184,19 @@ HasErrorCode: > push rax > push rax > sidt [rsp] > - xchg rax, [rsp + 2] > - xchg rax, [rsp] > - xchg rax, [rsp + 8] > + mov bx, word [rsp] > + mov rax, qword [rsp + 2] > + mov qword [rsp], rax > + mov word [rsp + 8], bx > > xor rax, rax > push rax > push rax > sgdt [rsp] > - xchg rax, [rsp + 2] > - xchg rax, [rsp] > - xchg rax, [rsp + 8] > + mov bx, word [rsp] > + mov rax, qword [rsp + 2] > + mov qword [rsp], rax > + mov word [rsp + 8], bx > > ;; UINT64 Ldtr, Tr; > xor rax, rax > I think it would be nice to learn why XCHG was used in the first place. Then again, whatever it was preferred for, it could not have been locking, as the three XCHG instructions are not atomic as a whole (i.e. they are not locked all together). Another reason for XCHG could be that they wanted to use just one register -- but I totally don't see the point of not using BX too. So: Reviewed-by: Laszlo Ersek Thanks, Laszlo