public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Laszlo Ersek <lersek@redhat.com>
To: edk2-devel-01 <edk2-devel@lists.01.org>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>,
	Eric Dong <eric.dong@intel.com>,
	Jiewen Yao <jiewen.yao@intel.com>,
	Leif Lindholm <leif.lindholm@linaro.org>,
	Liming Gao <liming.gao@intel.com>,
	Michael D Kinney <michael.d.kinney@intel.com>,
	Ruiyu Ni <ruiyu.ni@intel.com>
Subject: [PATCH 02/14] MdePkg/BaseLib: add PatchInstructionX86()
Date: Fri,  2 Feb 2018 15:39:42 +0100	[thread overview]
Message-ID: <20180202143954.7357-3-lersek@redhat.com> (raw)
In-Reply-To: <20180202143954.7357-1-lersek@redhat.com>

Some edk2 modules generate X86 machine code at module execution time by:

- compiling "template" code with NASM at module build time,

- linking the object code into the module,

- and patching the immediate (constant) operands of some instructions when
  the module is executed.

Add a helper function to BaseLib so that the C code performing the
patching is easier to read and maintain.

The implementation in this patch is taken mainly from Mike Kinney's
mailing list message at
<http://mid.mail-archive.com/E92EE9817A31E24EB0585FDF735412F5B895C360@ORSMSX113.amr.corp.intel.com>.

Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Leif Lindholm <leif.lindholm@linaro.org>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=866
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
---
 MdePkg/Library/BaseLib/BaseLib.inf           |  2 +
 MdePkg/Include/Library/BaseLib.h             | 42 +++++++++
 MdePkg/Library/BaseLib/X86PatchInstruction.c | 89 ++++++++++++++++++++
 3 files changed, 133 insertions(+)

diff --git a/MdePkg/Library/BaseLib/BaseLib.inf b/MdePkg/Library/BaseLib/BaseLib.inf
index fbfb0063b75f..4353e242f458 100644
--- a/MdePkg/Library/BaseLib/BaseLib.inf
+++ b/MdePkg/Library/BaseLib/BaseLib.inf
@@ -431,6 +431,7 @@ [Sources.Ia32]
   X86DisablePaging64.c
   X86DisablePaging32.c
   X86RdRand.c
+  X86PatchInstruction.c
 
 [Sources.X64]
   X64/Thunk16.nasm
@@ -757,6 +758,7 @@ [Sources.X64]
   X86DisablePaging64.c
   X86DisablePaging32.c
   X86RdRand.c
+  X86PatchInstruction.c
   X64/GccInline.c | GCC
   X64/Thunk16.S | XCODE 
   X64/SwitchStack.nasm| GCC
diff --git a/MdePkg/Include/Library/BaseLib.h b/MdePkg/Include/Library/BaseLib.h
index e4455e71d5c3..0dcb394c9280 100644
--- a/MdePkg/Include/Library/BaseLib.h
+++ b/MdePkg/Include/Library/BaseLib.h
@@ -9068,5 +9068,47 @@ AsmWriteTr (
   IN UINT16 Selector
   );
 
+/**
+  Patch the immediate operand of an IA32 or X64 instruction such that the byte,
+  word, dword or qword operand is encoded at the end of the instruction's
+  binary representation.
+
+  This function should be used to update object code that was compiled with
+  NASM from assembly source code. Example:
+
+  NASM source code:
+
+        mov     eax, strict dword 0 ; the imm32 zero operand will be patched
+    ASM_PFX(gPatchCr3):
+        mov     cr3, eax
+
+  C source code:
+
+    extern UINT8 gPatchCr3;
+    PatchInstructionX86 (&gPatchCr3, AsmReadCr3 (), 4);
+
+  @param[out] InstructionEnd  Pointer to the byte one past the instruction to
+                              patch. The immediate operand to patch is expected
+                              to comprise the trailing bytes of the
+                              instruction. If InstructionEnd is closer to
+                              address 0 than ValueSize permits, then ASSERT().
+
+  @param[in] PatchValue       The constant to write to the immediate operand.
+                              The caller is responsible for ensuring that
+                              PatchValue can be represented in the byte, word,
+                              dword or qword operand (as indicated through
+                              ValueSize); otherwise ASSERT().
+
+  @param[in] ValueSize        The size of the operand in bytes; must be 1, 2,
+                              4, or 8. ASSERT() otherwise.
+**/
+VOID
+EFIAPI
+PatchInstructionX86 (
+  OUT VOID   *InstructionEnd,
+  IN  UINT64 PatchValue,
+  IN  UINTN  ValueSize
+  );
+
 #endif // defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
 #endif // !defined (__BASE_LIB__)
diff --git a/MdePkg/Library/BaseLib/X86PatchInstruction.c b/MdePkg/Library/BaseLib/X86PatchInstruction.c
new file mode 100644
index 000000000000..82c86244c9c0
--- /dev/null
+++ b/MdePkg/Library/BaseLib/X86PatchInstruction.c
@@ -0,0 +1,89 @@
+/** @file
+  IA-32/x64 PatchInstructionX86()
+
+  Copyright (C) 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (C) 2018, Red Hat, Inc.
+
+  This program and the accompanying materials are licensed and made available
+  under the terms and conditions of the BSD License which accompanies this
+  distribution.  The full text of the license may be found at
+  http://opensource.org/licenses/bsd-license.php.
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
+  WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+**/
+
+#include "BaseLibInternals.h"
+
+/**
+  Patch the immediate operand of an IA32 or X64 instruction such that the byte,
+  word, dword or qword operand is encoded at the end of the instruction's
+  binary representation.
+
+  This function should be used to update object code that was compiled with
+  NASM from assembly source code. Example:
+
+  NASM source code:
+
+        mov     eax, strict dword 0 ; the imm32 zero operand will be patched
+    ASM_PFX(gPatchCr3):
+        mov     cr3, eax
+
+  C source code:
+
+    extern UINT8 gPatchCr3;
+    PatchInstructionX86 (&gPatchCr3, AsmReadCr3 (), 4);
+
+  @param[out] InstructionEnd  Pointer to the byte one past the instruction to
+                              patch. The immediate operand to patch is expected
+                              to comprise the trailing bytes of the
+                              instruction. If InstructionEnd is closer to
+                              address 0 than ValueSize permits, then ASSERT().
+
+  @param[in] PatchValue       The constant to write to the immediate operand.
+                              The caller is responsible for ensuring that
+                              PatchValue can be represented in the byte, word,
+                              dword or qword operand (as indicated through
+                              ValueSize); otherwise ASSERT().
+
+  @param[in] ValueSize        The size of the operand in bytes; must be 1, 2,
+                              4, or 8. ASSERT() otherwise.
+**/
+VOID
+EFIAPI
+PatchInstructionX86 (
+  OUT VOID   *InstructionEnd,
+  IN  UINT64 PatchValue,
+  IN  UINTN  ValueSize
+  )
+{
+  //
+  // The equality ((UINTN)InstructionEnd == ValueSize) would assume a zero-size
+  // instruction at address 0; forbid it.
+  //
+  ASSERT ((UINTN)InstructionEnd > ValueSize);
+
+  switch (ValueSize) {
+  case 1:
+    ASSERT (PatchValue <= MAX_UINT8);
+    *((UINT8 *)InstructionEnd - 1) = (UINT8)PatchValue;
+    break;
+
+  case 2:
+    ASSERT (PatchValue <= MAX_UINT16);
+    WriteUnaligned16 ((UINT16 *)InstructionEnd - 1, (UINT16)PatchValue);
+    break;
+
+  case 4:
+    ASSERT (PatchValue <= MAX_UINT32);
+    WriteUnaligned32 ((UINT32 *)InstructionEnd - 1, (UINT32)PatchValue);
+    break;
+
+  case 8:
+    WriteUnaligned64 ((UINT64 *)InstructionEnd - 1, PatchValue);
+    break;
+
+  default:
+    ASSERT (FALSE);
+  }
+}
-- 
2.14.1.3.gb7cf6e02401b




  parent reply	other threads:[~2018-02-02 14:34 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-02 14:39 [PATCH 00/14] rid PiSmmCpuDxeSmm of DB-encoded instructions Laszlo Ersek
2018-02-02 14:39 ` [PATCH 01/14] MdePkg/BaseLib.h: state preprocessing conditions in comments after #endifs Laszlo Ersek
2018-02-02 14:39 ` Laszlo Ersek [this message]
2018-02-02 14:39 ` [PATCH 03/14] UefiCpuPkg/PiSmmCpuDxeSmm: remove *.S and *.asm assembly files Laszlo Ersek
2018-03-22 23:45   ` Laszlo Ersek
2018-02-02 14:39 ` [PATCH 04/14] UefiCpuPkg/PiSmmCpuDxeSmm: patch "gSmbase" with PatchInstructionX86() Laszlo Ersek
2018-02-02 14:39 ` [PATCH 05/14] UefiCpuPkg/PiSmmCpuDxeSmm: patch "gSmiStack" " Laszlo Ersek
2018-02-02 14:39 ` [PATCH 06/14] UefiCpuPkg/PiSmmCpuDxeSmm: patch "gSmiCr3" " Laszlo Ersek
2018-02-02 14:39 ` [PATCH 07/14] UefiCpuPkg/PiSmmCpuDxeSmm: patch "XdSupported" " Laszlo Ersek
2018-02-02 14:39 ` [PATCH 08/14] UefiCpuPkg/PiSmmCpuDxeSmm: remove unneeded DBs from X64 SmmStartup() Laszlo Ersek
2018-02-02 14:39 ` [PATCH 09/14] UefiCpuPkg/PiSmmCpuDxeSmm: patch "gSmmCr3" with PatchInstructionX86() Laszlo Ersek
2018-02-02 14:39 ` [PATCH 10/14] UefiCpuPkg/PiSmmCpuDxeSmm: patch "gSmmCr4" " Laszlo Ersek
2018-02-02 14:39 ` [PATCH 11/14] UefiCpuPkg/PiSmmCpuDxeSmm: patch "gSmmCr0" " Laszlo Ersek
2018-02-02 14:39 ` [PATCH 12/14] UefiCpuPkg/PiSmmCpuDxeSmm: eliminate "gSmmJmpAddr" and related DBs Laszlo Ersek
2018-02-02 14:39 ` [PATCH 13/14] UefiCpuPkg/PiSmmCpuDxeSmm: patch "gSmmInitStack" with PatchInstructionX86() Laszlo Ersek
2018-02-02 14:39 ` [PATCH 14/14] UefiCpuPkg/PiSmmCpuDxeSmm: remove DBs from SmmRelocationSemaphoreComplete32() Laszlo Ersek
2018-02-03  0:45 ` [PATCH 00/14] rid PiSmmCpuDxeSmm of DB-encoded instructions Kinney, Michael D
2018-02-05 10:28   ` Laszlo Ersek
2018-02-05 18:22     ` Kinney, Michael D
2018-02-05 19:23       ` Laszlo Ersek
2018-03-23  0:29         ` Kinney, Michael D

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=20180202143954.7357-3-lersek@redhat.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