On 2/16/2019 3:43 PM, Ni, Ray wrote:
(Sent third times to make sure Andrew and Laszlo are in the TO list.)

I also met this issue.
I found three solutions:
1. Forcing PeiMain CC flag to "-O0" works.
2. Changing EmulatorPkg/Sec to not produce TemporaryRamSupportPpi also works. 3. Implement the temporary migration routine as below in EmulatorPkg/Sec module.

EFI_STATUS
EFIAPI
SecTemporaryRamSupport (
   IN CONST EFI_PEI_SERVICES   **PeiServices,
   IN EFI_PHYSICAL_ADDRESS     TemporaryMemoryBase,
   IN EFI_PHYSICAL_ADDRESS     PermanentMemoryBase,
   IN UINTN                    CopySize
   )
{
   VOID                             *OldHeap;
   VOID                             *NewHeap;
   VOID                             *OldStack;
   VOID                             *NewStack;
   UINTN                            StackMigrateOffset;
   BASE_LIBRARY_JUMP_BUFFER         JumpBuffer;

   DEBUG ((EFI_D_INFO,
     "TemporaryRamMigration(0x%Lx, 0x%Lx, 0x%Lx)\n",
     TemporaryMemoryBase,
     PermanentMemoryBase,
     (UINT64)CopySize
     ));

   //
   // Assume Host prepare the stack and heap in the temprary ram that stack
   // is below heap (stack is in smaller address).
   // Stack/heap migration depends on the stack/heap location information
   // in the temporary ram.
   //
   OldStack = (VOID*)(UINTN)TemporaryMemoryBase;
   NewStack = (VOID*)((UINTN)PermanentMemoryBase);

   OldHeap = (VOID*)((UINTN)TemporaryMemoryBase + (CopySize >> 1));
   NewHeap = (VOID*)((UINTN)PermanentMemoryBase + (CopySize >> 1));

   StackMigrateOffset = (UINTN)NewStack - (UINTN)OldStack;

   //
   // Migrate Heap and Stack
   //
   CopyMem (NewHeap, OldHeap, CopySize >> 1);
   CopyMem (NewStack, OldStack, CopySize >> 1);

   //
   // Use SetJump()/LongJump() to switch to a new stack.
   //
   if (SetJump (&JumpBuffer) == 0) {
#if defined (MDE_CPU_IA32)
     JumpBuffer.Esp = JumpBuffer.Esp + StackMigrateOffset;
     JumpBuffer.Ebp = JumpBuffer.Ebp + StackMigrateOffset;
#endif
#if defined (MDE_CPU_X64)
     JumpBuffer.Rsp = JumpBuffer.Rsp + StackMigrateOffset;
     JumpBuffer.Rbp = JumpBuffer.Rbp + StackMigrateOffset;
#endif
     LongJump (&JumpBuffer, (UINTN)-1);
   }

   ZeroMem ((VOID *)(UINTN) TemporaryMemoryBase, CopySize);

   return EFI_SUCCESS;
}


Andrew,
I'd like to know why you chose to produce the migration PPI from
EmulatorPkg/Sec module.
Based on PI spec and current PeiCore implementation, PeiCore can do the migration when PPI is absent.



Study the PeiCore migration logic a bit more, I found since PeiCore knows the exact size of new stack in permanent memory, it migrates the old stack to the top of new stack. But the migration logic in above C code (since it doesn't know the size of new stack, CopySize is the size of temporary memory) may copy the old stack to the middle in new stack.

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

Reply via email to