SecTempRamSupport.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /** @file
  2. C functions in SEC
  3. Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiPei.h>
  7. #include <Ppi/TemporaryRamSupport.h>
  8. #include <Library/BaseMemoryLib.h>
  9. #include <Library/DebugLib.h>
  10. #include <Library/PcdLib.h>
  11. #include <Library/DebugAgentLib.h>
  12. /**
  13. Switch the stack in the temporary memory to the one in the permanent memory.
  14. This function must be invoked after the memory migration immediately. The relative
  15. position of the stack in the temporary and permanent memory is same.
  16. @param TemporaryMemoryBase Base address of the temporary memory.
  17. @param PermenentMemoryBase Base address of the permanent memory.
  18. **/
  19. VOID
  20. EFIAPI
  21. SecSwitchStack (
  22. UINT32 TemporaryMemoryBase,
  23. UINT32 PermenentMemoryBase
  24. );
  25. /**
  26. This service of the TEMPORARY_RAM_SUPPORT_PPI that migrates temporary RAM into
  27. permanent memory.
  28. @param PeiServices Pointer to the PEI Services Table.
  29. @param TemporaryMemoryBase Source Address in temporary memory from which the SEC or PEIM will copy the
  30. Temporary RAM contents.
  31. @param PermanentMemoryBase Destination Address in permanent memory into which the SEC or PEIM will copy the
  32. Temporary RAM contents.
  33. @param CopySize Amount of memory to migrate from temporary to permanent memory.
  34. @retval EFI_SUCCESS The data was successfully returned.
  35. @retval EFI_INVALID_PARAMETER PermanentMemoryBase + CopySize > TemporaryMemoryBase when
  36. TemporaryMemoryBase > PermanentMemoryBase.
  37. **/
  38. EFI_STATUS
  39. EFIAPI
  40. SecTemporaryRamSupport (
  41. IN CONST EFI_PEI_SERVICES **PeiServices,
  42. IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
  43. IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
  44. IN UINTN CopySize
  45. )
  46. {
  47. IA32_DESCRIPTOR IdtDescriptor;
  48. VOID* OldHeap;
  49. VOID* NewHeap;
  50. VOID* OldStack;
  51. VOID* NewStack;
  52. DEBUG_AGENT_CONTEXT_POSTMEM_SEC DebugAgentContext;
  53. BOOLEAN OldStatus;
  54. UINTN PeiStackSize;
  55. PeiStackSize = (UINTN)PcdGet32 (PcdPeiTemporaryRamStackSize);
  56. if (PeiStackSize == 0) {
  57. PeiStackSize = (CopySize >> 1);
  58. }
  59. ASSERT (PeiStackSize < CopySize);
  60. //
  61. // |-------------------|---->
  62. // | Stack | PeiStackSize
  63. // |-------------------|---->
  64. // | Heap | PeiTemporayRamSize
  65. // |-------------------|----> TempRamBase
  66. //
  67. // |-------------------|---->
  68. // | Heap | PeiTemporayRamSize
  69. // |-------------------|---->
  70. // | Stack | PeiStackSize
  71. // |-------------------|----> PermanentMemoryBase
  72. //
  73. OldHeap = (VOID*)(UINTN)TemporaryMemoryBase;
  74. NewHeap = (VOID*)((UINTN)PermanentMemoryBase + PeiStackSize);
  75. OldStack = (VOID*)((UINTN)TemporaryMemoryBase + CopySize - PeiStackSize);
  76. NewStack = (VOID*)(UINTN)PermanentMemoryBase;
  77. DebugAgentContext.HeapMigrateOffset = (UINTN)NewHeap - (UINTN)OldHeap;
  78. DebugAgentContext.StackMigrateOffset = (UINTN)NewStack - (UINTN)OldStack;
  79. OldStatus = SaveAndSetDebugTimerInterrupt (FALSE);
  80. //
  81. // Initialize Debug Agent to support source level debug in PEI phase after memory ready.
  82. // It will build HOB and fix up the pointer in IDT table.
  83. //
  84. InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC, (VOID *) &DebugAgentContext, NULL);
  85. //
  86. // Migrate Heap
  87. //
  88. CopyMem (NewHeap, OldHeap, CopySize - PeiStackSize);
  89. //
  90. // Migrate Stack
  91. //
  92. CopyMem (NewStack, OldStack, PeiStackSize);
  93. //
  94. // We need *not* fix the return address because currently,
  95. // The PeiCore is executed in flash.
  96. //
  97. //
  98. // Rebase IDT table in permanent memory
  99. //
  100. AsmReadIdtr (&IdtDescriptor);
  101. IdtDescriptor.Base = IdtDescriptor.Base - (UINTN)OldStack + (UINTN)NewStack;
  102. AsmWriteIdtr (&IdtDescriptor);
  103. //
  104. // Program MTRR
  105. //
  106. //
  107. // SecSwitchStack function must be invoked after the memory migration
  108. // immediately, also we need fixup the stack change caused by new call into
  109. // permanent memory.
  110. //
  111. SecSwitchStack (
  112. (UINT32) (UINTN) OldStack,
  113. (UINT32) (UINTN) NewStack
  114. );
  115. SaveAndSetDebugTimerInterrupt (OldStatus);
  116. return EFI_SUCCESS;
  117. }