ModuleEntryPoint.S 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // Copyright (c) 2011-2013, ARM Limited. All rights reserved.
  3. // Copyright (c) 2015-2016, Linaro Limited. All rights reserved.
  4. //
  5. // SPDX-License-Identifier: BSD-2-Clause-Patent
  6. //
  7. //
  8. #include <AsmMacroIoLib.h>
  9. ASM_FUNC(_ModuleEntryPoint)
  10. //
  11. // We are built as a ET_DYN PIE executable, so we need to process all
  12. // relative relocations if we are executing from a different offset than we
  13. // were linked at. This is only possible if we are running from RAM.
  14. //
  15. ADRL (r4, __reloc_base)
  16. ADRL (r5, __reloc_start)
  17. ADRL (r6, __reloc_end)
  18. .Lreloc_loop:
  19. cmp r5, r6
  20. bhs .Lreloc_done
  21. //
  22. // AArch32 uses the ELF32 REL format, which means each entry in the
  23. // relocation table consists of
  24. //
  25. // UINT32 offset : the relative offset of the value that needs to
  26. // be relocated
  27. // UINT32 info : relocation type and symbol index (the latter is
  28. // not used for R_ARM_RELATIVE relocations)
  29. //
  30. ldrd r8, r9, [r5], #8 // read offset into r8 and info into r9
  31. cmp r9, #23 // check info == R_ARM_RELATIVE?
  32. bne .Lreloc_loop // not a relative relocation? then skip
  33. ldr r9, [r8, r4] // read addend into r9
  34. add r9, r9, r1 // add image base to addend to get relocated value
  35. str r9, [r8, r4] // write relocated value at offset
  36. b .Lreloc_loop
  37. .Lreloc_done:
  38. // Do early platform specific actions
  39. bl ASM_PFX(ArmPlatformPeiBootAction)
  40. // Get ID of this CPU in Multicore system
  41. bl ASM_PFX(ArmReadMpidr)
  42. // Keep a copy of the MpId register value
  43. mov r10, r0
  44. // Check if we can install the stack at the top of the System Memory or if we need
  45. // to install the stacks at the bottom of the Firmware Device (case the FD is located
  46. // at the top of the DRAM)
  47. _SetupStackPosition:
  48. // Compute Top of System Memory
  49. LDRL (r1, PcdGet64 (PcdSystemMemoryBase))
  50. ADRL (r12, PcdGet64 (PcdSystemMemorySize))
  51. ldrd r2, r3, [r12]
  52. // calculate the top of memory
  53. adds r2, r2, r1
  54. sub r2, r2, #1
  55. addcs r3, r3, #1
  56. // truncate the memory used by UEFI to 4 GB range
  57. teq r3, #0
  58. movne r1, #-1
  59. moveq r1, r2
  60. // Calculate Top of the Firmware Device
  61. LDRL (r2, PcdGet64 (PcdFdBaseAddress))
  62. MOV32 (r3, FixedPcdGet32 (PcdFdSize) - 1)
  63. add r3, r3, r2 // r3 = FdTop = PcdFdBaseAddress + PcdFdSize
  64. // UEFI Memory Size (stacks are allocated in this region)
  65. MOV32 (r4, FixedPcdGet32(PcdSystemMemoryUefiRegionSize))
  66. //
  67. // Reserve the memory for the UEFI region (contain stacks on its top)
  68. //
  69. // Calculate how much space there is between the top of the Firmware and the Top of the System Memory
  70. subs r0, r1, r3 // r0 = SystemMemoryTop - FdTop
  71. bmi _SetupStack // Jump if negative (FdTop > SystemMemoryTop). Case when the PrePi is in XIP memory outside of the DRAM
  72. cmp r0, r4
  73. bge _SetupStack
  74. // Case the top of stacks is the FdBaseAddress
  75. mov r1, r2
  76. _SetupStack:
  77. // r1 contains the top of the stack (and the UEFI Memory)
  78. // Because the 'push' instruction is equivalent to 'stmdb' (decrement before), we need to increment
  79. // one to the top of the stack. We check if incrementing one does not overflow (case of DRAM at the
  80. // top of the memory space)
  81. adds r11, r1, #1
  82. bcs _SetupOverflowStack
  83. _SetupAlignedStack:
  84. mov r1, r11
  85. b _GetBaseUefiMemory
  86. _SetupOverflowStack:
  87. // Case memory at the top of the address space. Ensure the top of the stack is EFI_PAGE_SIZE
  88. // aligned (4KB)
  89. MOV32 (r11, (~EFI_PAGE_MASK) & 0xffffffff)
  90. and r1, r1, r11
  91. _GetBaseUefiMemory:
  92. // Calculate the Base of the UEFI Memory
  93. sub r11, r1, r4
  94. _GetStackBase:
  95. // r1 = The top of the Mpcore Stacks
  96. mov sp, r1
  97. // Stack for the primary core = PrimaryCoreStack
  98. MOV32 (r2, FixedPcdGet32(PcdCPUCorePrimaryStackSize))
  99. sub r9, r1, r2
  100. mov r0, r10
  101. mov r1, r11
  102. mov r2, r9
  103. // Jump to PrePiCore C code
  104. // r0 = MpId
  105. // r1 = UefiMemoryBase
  106. // r2 = StacksBase
  107. bl ASM_PFX(CEntryPoint)
  108. _NeverReturn:
  109. b _NeverReturn
  110. ASM_PFX(ArmPlatformPeiBootAction):
  111. //
  112. // If we are booting from RAM using the Linux kernel boot protocol, r0 will
  113. // point to the DTB image in memory. Otherwise, use the default value defined
  114. // by the platform.
  115. //
  116. teq r0, #0
  117. bne 0f
  118. LDRL (r0, PcdGet64 (PcdDeviceTreeInitialBaseAddress))
  119. 0:mov r11, r14 // preserve LR
  120. mov r10, r0 // preserve DTB pointer
  121. mov r9, r1 // preserve base of image pointer
  122. //
  123. // The base of the runtime image has been preserved in r1. Check whether
  124. // the expected magic number can be found in the header.
  125. //
  126. ldr r8, .LArm32LinuxMagic
  127. ldr r7, [r1, #0x24]
  128. cmp r7, r8
  129. bne .Lout
  130. //
  131. //
  132. // OK, so far so good. We have confirmed that we likely have a DTB and are
  133. // booting via the ARM Linux boot protocol. Update the base-of-image PCD
  134. // to the actual relocated value, and add the shift of PcdFdBaseAddress to
  135. // PcdFvBaseAddress as well
  136. //
  137. ADRL (r8, PcdGet64 (PcdFdBaseAddress))
  138. ADRL (r7, PcdGet64 (PcdFvBaseAddress))
  139. ldr r6, [r8]
  140. ldr r5, [r7]
  141. sub r5, r5, r6
  142. add r5, r5, r1
  143. str r1, [r8]
  144. str r5, [r7]
  145. //
  146. // Discover the memory size and offset from the DTB, and record in the
  147. // respective PCDs. This will also return false if a corrupt DTB is
  148. // encountered. Since we are calling a C function, use the window at the
  149. // beginning of the FD image as a temp stack.
  150. //
  151. ADRL (r1, PcdGet64 (PcdSystemMemoryBase))
  152. ADRL (r2, PcdGet64 (PcdSystemMemorySize))
  153. mov sp, r5
  154. bl FindMemnode
  155. teq r0, #0
  156. beq .Lout
  157. //
  158. // Copy the DTB to the slack space right after the 64 byte arm64/Linux style
  159. // image header at the base of this image (defined in the FDF), and record the
  160. // pointer in PcdDeviceTreeInitialBaseAddress.
  161. //
  162. ADRL (r8, PcdGet64 (PcdDeviceTreeInitialBaseAddress))
  163. add r9, r9, #0x40
  164. str r9, [r8]
  165. mov r0, r9
  166. mov r1, r10
  167. bl CopyFdt
  168. .Lout:
  169. bx r11
  170. .LArm32LinuxMagic:
  171. .byte 0x18, 0x28, 0x6f, 0x01