2 // Copyright (c) 2011-2013, ARM Limited. All rights reserved.
3 // Copyright (c) 2015, Linaro Limited. All rights reserved.
5 // This program and the accompanying materials
6 // are licensed and made available under the terms and conditions of the BSD License
7 // which accompanies this distribution. The full text of the license may be found at
8 // http://opensource.org/licenses/bsd-license.php
10 // THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include <AsmMacroIoLib.h>
17 #include <Library/PcdLib.h>
23 GCC_ASM_IMPORT(ArmPlatformIsPrimaryCore)
24 GCC_ASM_IMPORT(ArmReadMpidr)
25 GCC_ASM_IMPORT(ArmPlatformPeiBootAction)
26 GCC_ASM_IMPORT(ArmPlatformStackSet)
27 GCC_ASM_EXPORT(_ModuleEntryPoint)
28 ASM_GLOBAL ASM_PFX(mSystemMemoryEnd)
30 ASM_PFX(mSystemMemoryEnd): .quad 0
33 .long __reloc_base - __relocs
34 .long __reloc_start - __relocs
35 .long __reloc_end - __relocs
37 ASM_PFX(_ModuleEntryPoint):
39 // We are built as a ET_DYN PIE executable, so we need to process all
40 // relative relocations if we are executing from a different offset than we
41 // were linked at. This is only possible if we are running from RAM.
57 // AArch32 uses the ELF32 REL format, which means each entry in the
58 // relocation table consists of
60 // UINT32 offset : the relative offset of the value that needs to
62 // UINT32 info : relocation type and symbol index (the latter is
63 // not used for R_ARM_RELATIVE relocations)
65 ldrd r8, r9, [r5], #8 // read offset into r8 and info into r9
66 cmp r9, #23 // check info == R_ARM_RELATIVE?
67 bne .Lreloc_loop // not a relative relocation? then skip
69 ldr r9, [r8, r4] // read addend into r9
70 add r9, r9, r1 // add image base to addend to get relocated value
71 str r9, [r8, r4] // write relocated value at offset
75 // Do early platform specific actions
76 bl ASM_PFX(ArmPlatformPeiBootAction)
78 // Get ID of this CPU in Multicore system
79 bl ASM_PFX(ArmReadMpidr)
80 // Keep a copy of the MpId register value
83 // Check if we can install the stack at the top of the System Memory or if we need
84 // to install the stacks at the bottom of the Firmware Device (case the FD is located
85 // at the top of the DRAM)
87 // Compute Top of System Memory
88 ldr r12, =PcdGet64 (PcdSystemMemoryBase)
90 ldr r12, =PcdGet64 (PcdSystemMemorySize)
93 // calculate the top of memory, and record it in mSystemMemoryEnd
97 adr r12, mSystemMemoryEnd
100 // truncate the memory used by UEFI to 4 GB range
105 // Calculate Top of the Firmware Device
106 ldr r12, =PcdGet64 (PcdFdBaseAddress)
108 ldr r3, =FixedPcdGet32 (PcdFdSize)
110 add r3, r3, r2 // r3 = FdTop = PcdFdBaseAddress + PcdFdSize
112 // UEFI Memory Size (stacks are allocated in this region)
113 LoadConstantToReg (FixedPcdGet32(PcdSystemMemoryUefiRegionSize), r4)
116 // Reserve the memory for the UEFI region (contain stacks on its top)
119 // Calculate how much space there is between the top of the Firmware and the Top of the System Memory
120 subs r0, r1, r3 // r0 = SystemMemoryTop - FdTop
121 bmi _SetupStack // Jump if negative (FdTop > SystemMemoryTop). Case when the PrePi is in XIP memory outside of the DRAM
125 // Case the top of stacks is the FdBaseAddress
129 // r1 contains the top of the stack (and the UEFI Memory)
131 // Because the 'push' instruction is equivalent to 'stmdb' (decrement before), we need to increment
132 // one to the top of the stack. We check if incrementing one does not overflow (case of DRAM at the
133 // top of the memory space)
135 bcs _SetupOverflowStack
142 // Case memory at the top of the address space. Ensure the top of the stack is EFI_PAGE_SIZE
144 LoadConstantToReg (EFI_PAGE_MASK, r11)
149 // Calculate the Base of the UEFI Memory
153 // r1 = The top of the Mpcore Stacks
154 // Stack for the primary core = PrimaryCoreStack
155 LoadConstantToReg (FixedPcdGet32(PcdCPUCorePrimaryStackSize), r2)
158 // Stack for the secondary core = Number of Cores - 1
159 LoadConstantToReg (FixedPcdGet32(PcdCoreCount), r0)
161 LoadConstantToReg (FixedPcdGet32(PcdCPUCoreSecondaryStackSize), r1)
165 // r9 = The base of the MpCore Stacks (primary stack & secondary stacks)
168 //ArmPlatformStackSet(StackBase, MpId, PrimaryStackSize, SecondaryStackSize)
169 LoadConstantToReg (FixedPcdGet32(PcdCPUCorePrimaryStackSize), r2)
170 LoadConstantToReg (FixedPcdGet32(PcdCPUCoreSecondaryStackSize), r3)
171 bl ASM_PFX(ArmPlatformStackSet)
173 // Is it the Primary Core ?
175 bl ASM_PFX(ArmPlatformIsPrimaryCore)
177 bne _PrepareArguments
184 // Jump to PrePiCore C code
186 // r1 = UefiMemoryBase
188 bl ASM_PFX(CEntryPoint)