Some compilers may optimize register usage in ways that are
incompatible with the TemporaryRamSupport PPI. Using assembly code to
call the TemporaryRamMigration function prevents this issue.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jordan Justen <jordan.l.jus...@intel.com>
Cc: Jian J Wang <jian.j.w...@intel.com>
Cc: Hao Wu <hao.a...@intel.com>
Cc: Ray Ni <ray...@intel.com>
Cc: Star Zeng <star.z...@intel.com>
---
 .../Dispatcher/X64/TemporaryRamMigration.S    | 69 +++++++++++++++++
 .../Dispatcher/X64/TemporaryRamMigration.nasm | 75 +++++++++++++++++++
 MdeModulePkg/Core/Pei/PeiMain.inf             | 14 +++-
 3 files changed, 157 insertions(+), 1 deletion(-)
 create mode 100644 MdeModulePkg/Core/Pei/Dispatcher/X64/TemporaryRamMigration.S
 create mode 100644 
MdeModulePkg/Core/Pei/Dispatcher/X64/TemporaryRamMigration.nasm

diff --git a/MdeModulePkg/Core/Pei/Dispatcher/X64/TemporaryRamMigration.S 
b/MdeModulePkg/Core/Pei/Dispatcher/X64/TemporaryRamMigration.S
new file mode 100644
index 0000000000..cd9d902224
--- /dev/null
+++ b/MdeModulePkg/Core/Pei/Dispatcher/X64/TemporaryRamMigration.S
@@ -0,0 +1,69 @@
+#------------------------------------------------------------------------------
+#
+# Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.<BR>
+#
+# 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.
+#
+#------------------------------------------------------------------------------
+
+#------------------------------------------------------------------------------
+# VOID
+# EFIAPI
+# PeiTemporaryRamMigration (
+#   IN  PEI_CORE_TEMPORARY_RAM_TRANSITION  *TempRamTransitionData
+#   );
+#
+# @param[in]  RCX   Pointer to PEI_CORE_TEMPORARY_RAM_TRANSITION
+#
+# @return     None  This routine does not return
+#------------------------------------------------------------------------------
+ASM_GLOBAL ASM_PFX(PeiTemporaryRamMigration)
+ASM_PFX(PeiTemporaryRamMigration):
+
+    #
+    # We never return from this call
+    #
+    add     $8, %rsp
+
+    #
+    #   (rax) Pointer to PEI_CORE_TEMPORARY_RAM_TRANSITION
+    #
+    mov     %rcx, %rax
+
+    #
+    # We'll store the new location of TempRamTransitionData after
+    # migration in rbx. By the X64 calling convention we should
+    # normally save rbx, but we won't be returning to the caller, so
+    # we don't need to save it. By the same rule, the
+    # TemporaryRamMigration PPI call should preserve rbx for us.
+    #
+    mov     %rcx, %rbx
+    add     0x18(%rax), %rbx
+    sub     0x10(%rax), %rbx
+
+    #
+    # Setup parameters and call TemporaryRamSupport->TemporaryRamMigration
+    #   (rcx) PeiServices
+    #   (rdx) TemporaryMemoryBase
+    #   (r8)  PermanentMemoryBase
+    #   (r9)  CopySize
+    #
+    mov     0x08(%rax), %rcx
+    mov     0x10(%rax), %rdx
+    mov     0x18(%rax), %r8
+    mov     0x20(%rax), %r9
+    callq   *(%rax)
+
+    #
+    # Setup parameters and call PeiTemporaryRamMigrated
+    #   (rcx) Pointer to PEI_CORE_TEMPORARY_RAM_TRANSITION
+    #
+    mov     %rbx, %rcx
+    call    ASM_PFX(PeiTemporaryRamMigrated)
diff --git a/MdeModulePkg/Core/Pei/Dispatcher/X64/TemporaryRamMigration.nasm 
b/MdeModulePkg/Core/Pei/Dispatcher/X64/TemporaryRamMigration.nasm
new file mode 100644
index 0000000000..2783b0f403
--- /dev/null
+++ b/MdeModulePkg/Core/Pei/Dispatcher/X64/TemporaryRamMigration.nasm
@@ -0,0 +1,75 @@
+;------------------------------------------------------------------------------
+;
+; Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
+;
+; 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 <Base.h>
+
+    SECTION .text
+
+extern ASM_PFX(PeiTemporaryRamMigrated)
+
+;------------------------------------------------------------------------------
+; VOID
+; EFIAPI
+; PeiTemporaryRamMigration (
+;   IN  PEI_CORE_TEMPORARY_RAM_TRANSITION  *TempRamTransitionData
+;   );
+;
+; @param[in]  RCX   Pointer to PEI_CORE_TEMPORARY_RAM_TRANSITION
+;
+; @return     None  This routine does not return
+;------------------------------------------------------------------------------
+global ASM_PFX(PeiTemporaryRamMigration)
+ASM_PFX(PeiTemporaryRamMigration):
+
+    ;
+    ; We never return from this call
+    ;
+    add     rsp, 8
+
+    ;
+    ;   (rax) Pointer to PEI_CORE_TEMPORARY_RAM_TRANSITION
+    ;
+    mov     rax, rcx
+
+    ;
+    ; We'll store the new location of TempRamTransitionData after
+    ; migration in rbx. By the X64 calling convention we should
+    ; normally save rbx, but we won't be returning to the caller, so
+    ; we don't need to save it. By the same rule, the
+    ; TemporaryRamMigration PPI call should preserve rbx for us.
+    ;
+    mov     rbx, rcx
+    add     rbx, [rax + 0x18]
+    sub     rbx, [rax + 0x10]
+
+    ;
+    ; Setup parameters and call TemporaryRamSupport->TemporaryRamMigration
+    ;   (rcx) PeiServices
+    ;   (rdx) TemporaryMemoryBase
+    ;   (r8)  PermanentMemoryBase
+    ;   (r9)  CopySize
+    ;
+    mov     rcx, [rax + 0x08]
+    mov     rdx, [rax + 0x10]
+    mov     r8, [rax + 0x18]
+    mov     r9, [rax + 0x20]
+    call    [rax + 0x00]
+
+    ;
+    ; Setup parameters and call PeiTemporaryRamMigrated
+    ;   (rcx) Pointer to PEI_CORE_TEMPORARY_RAM_TRANSITION
+    ;
+    mov     rcx, rbx
+    call    ASM_PFX(PeiTemporaryRamMigrated)
diff --git a/MdeModulePkg/Core/Pei/PeiMain.inf 
b/MdeModulePkg/Core/Pei/PeiMain.inf
index 94b26b3572..1646f73385 100644
--- a/MdeModulePkg/Core/Pei/PeiMain.inf
+++ b/MdeModulePkg/Core/Pei/PeiMain.inf
@@ -45,7 +45,6 @@
   FwVol/FwVol.c
   FwVol/FwVol.h
   Dispatcher/Dispatcher.c
-  Dispatcher/TemporaryRamMigration.c
   Dependency/Dependency.c
   Dependency/Dependency.h
   BootMode/BootMode.c
@@ -53,6 +52,19 @@
   PciCfg2/PciCfg2.c
   PeiMain.h
 
+[Sources.Ia32]
+  Dispatcher/TemporaryRamMigration.c
+
+[Sources.X64]
+  Dispatcher/X64/TemporaryRamMigration.nasm
+  Dispatcher/X64/TemporaryRamMigration.S
+
+[Sources.ARM]
+  Dispatcher/TemporaryRamMigration.c
+
+[Sources.AARCH64]
+  Dispatcher/TemporaryRamMigration.c
+
 [Packages]
   MdePkg/MdePkg.dec
   MdeModulePkg/MdeModulePkg.dec
-- 
2.20.0.rc1

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to