ModuleEntryPoint.S 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <AsmMacroIoLibV8.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 regardless of whether or not we are executing from
  13. // the same offset we were linked at. This is only possible if we are
  14. // running from RAM.
  15. //
  16. adr x8, __reloc_base
  17. adr x9, __reloc_start
  18. adr x10, __reloc_end
  19. .Lreloc_loop:
  20. cmp x9, x10
  21. bhs .Lreloc_done
  22. //
  23. // AArch64 uses the ELF64 RELA format, which means each entry in the
  24. // relocation table consists of
  25. //
  26. // UINT64 offset : the relative offset of the value that needs to
  27. // be relocated
  28. // UINT64 info : relocation type and symbol index (the latter is
  29. // not used for R_AARCH64_RELATIVE relocations)
  30. // UINT64 addend : value to be added to the value being relocated
  31. //
  32. ldp x11, x12, [x9], #24 // read offset into x11 and info into x12
  33. cmp x12, #0x403 // check info == R_AARCH64_RELATIVE?
  34. bne .Lreloc_loop // not a relative relocation? then skip
  35. ldr x12, [x9, #-8] // read addend into x12
  36. add x12, x12, x8 // add reloc base to addend to get relocated value
  37. str x12, [x11, x8] // write relocated value at offset
  38. b .Lreloc_loop
  39. .Lreloc_done:
  40. bl ASM_PFX(DiscoverDramFromDt)
  41. // Get ID of this CPU in Multicore system
  42. bl ASM_PFX(ArmReadMpidr)
  43. // Keep a copy of the MpId register value
  44. mov x20, x0
  45. // Check if we can install the stack at the top of the System Memory or if we need
  46. // to install the stacks at the bottom of the Firmware Device (case the FD is located
  47. // at the top of the DRAM)
  48. _SetupStackPosition:
  49. // Compute Top of System Memory
  50. ldr x1, PcdGet64 (PcdSystemMemoryBase)
  51. ldr x2, PcdGet64 (PcdSystemMemorySize)
  52. sub x2, x2, #1
  53. add x1, x1, x2 // x1 = SystemMemoryTop = PcdSystemMemoryBase + PcdSystemMemorySize
  54. // Calculate Top of the Firmware Device
  55. ldr x2, PcdGet64 (PcdFdBaseAddress)
  56. MOV32 (w3, FixedPcdGet32 (PcdFdSize) - 1)
  57. add x3, x3, x2 // x3 = FdTop = PcdFdBaseAddress + PcdFdSize
  58. // UEFI Memory Size (stacks are allocated in this region)
  59. MOV32 (x4, FixedPcdGet32(PcdSystemMemoryUefiRegionSize))
  60. //
  61. // Reserve the memory for the UEFI region (contain stacks on its top)
  62. //
  63. // Calculate how much space there is between the top of the Firmware and the Top of the System Memory
  64. subs x0, x1, x3 // x0 = SystemMemoryTop - FdTop
  65. b.mi _SetupStack // Jump if negative (FdTop > SystemMemoryTop). Case when the PrePi is in XIP memory outside of the DRAM
  66. cmp x0, x4
  67. b.ge _SetupStack
  68. // Case the top of stacks is the FdBaseAddress
  69. mov x1, x2
  70. _SetupStack:
  71. // x1 contains the top of the stack (and the UEFI Memory)
  72. // Because the 'push' instruction is equivalent to 'stmdb' (decrement before), we need to increment
  73. // one to the top of the stack. We check if incrementing one does not overflow (case of DRAM at the
  74. // top of the memory space)
  75. adds x21, x1, #1
  76. b.cs _SetupOverflowStack
  77. _SetupAlignedStack:
  78. mov x1, x21
  79. b _GetBaseUefiMemory
  80. _SetupOverflowStack:
  81. // Case memory at the top of the address space. Ensure the top of the stack is EFI_PAGE_SIZE
  82. // aligned (4KB)
  83. and x1, x1, ~EFI_PAGE_MASK
  84. _GetBaseUefiMemory:
  85. // Calculate the Base of the UEFI Memory
  86. sub x21, x1, x4
  87. _GetStackBase:
  88. // r1 = The top of the Mpcore Stacks
  89. mov sp, x1
  90. // Stack for the primary core = PrimaryCoreStack
  91. MOV32 (x2, FixedPcdGet32(PcdCPUCorePrimaryStackSize))
  92. sub x22, x1, x2
  93. mov x0, x20
  94. mov x1, x21
  95. mov x2, x22
  96. // Set the frame pointer to NULL so any backtraces terminate here
  97. mov x29, xzr
  98. // Jump to PrePiCore C code
  99. // x0 = MpId
  100. // x1 = UefiMemoryBase
  101. // x2 = StacksBase
  102. bl ASM_PFX(CEntryPoint)
  103. _NeverReturn:
  104. b _NeverReturn
  105. // VOID
  106. // DiscoverDramFromDt (
  107. // VOID *DeviceTreeBaseAddress, // passed by loader in x0
  108. // VOID *ImageBase // passed by FDF trampoline in x1
  109. // );
  110. ASM_PFX(DiscoverDramFromDt):
  111. //
  112. // If we are booting from RAM using the Linux kernel boot protocol, x0 will
  113. // point to the DTB image in memory. Otherwise, use the default value defined
  114. // by the platform.
  115. //
  116. cbnz x0, 0f
  117. ldr x0, PcdGet64 (PcdDeviceTreeInitialBaseAddress)
  118. 0:mov x29, x30 // preserve LR
  119. mov x28, x0 // preserve DTB pointer
  120. mov x27, x1 // preserve base of image pointer
  121. //
  122. // The base of the runtime image has been preserved in x1. Check whether
  123. // the expected magic number can be found in the header.
  124. //
  125. ldr w8, .LArm64LinuxMagic
  126. ldr w9, [x1, #0x38]
  127. cmp w8, w9
  128. bne .Lout
  129. //
  130. //
  131. // OK, so far so good. We have confirmed that we likely have a DTB and are
  132. // booting via the arm64 Linux boot protocol. Update the base-of-image PCD
  133. // to the actual relocated value, and add the shift of PcdFdBaseAddress to
  134. // PcdFvBaseAddress as well
  135. //
  136. adr x8, PcdGet64 (PcdFdBaseAddress)
  137. adr x9, PcdGet64 (PcdFvBaseAddress)
  138. ldr x6, [x8]
  139. ldr x7, [x9]
  140. sub x7, x7, x6
  141. add x7, x7, x1
  142. str x1, [x8]
  143. str x7, [x9]
  144. //
  145. // Discover the memory size and offset from the DTB, and record in the
  146. // respective PCDs. This will also return false if a corrupt DTB is
  147. // encountered. Since we are calling a C function, use the window at the
  148. // beginning of the FD image as a temp stack.
  149. //
  150. adr x1, PcdGet64 (PcdSystemMemoryBase)
  151. adr x2, PcdGet64 (PcdSystemMemorySize)
  152. mov sp, x7
  153. bl FindMemnode
  154. cbz x0, .Lout
  155. //
  156. // Copy the DTB to the slack space right after the 64 byte arm64/Linux style
  157. // image header at the base of this image (defined in the FDF), and record the
  158. // pointer in PcdDeviceTreeInitialBaseAddress.
  159. //
  160. adr x8, PcdGet64 (PcdDeviceTreeInitialBaseAddress)
  161. add x27, x27, #0x40
  162. str x27, [x8]
  163. mov x0, x27
  164. mov x1, x28
  165. bl CopyFdt
  166. .Lout:
  167. ret x29
  168. .LArm64LinuxMagic:
  169. .byte 0x41, 0x52, 0x4d, 0x64