]> git.proxmox.com Git - mirror_edk2.git/blob - ArmVirtPkg/PrePi/Arm/ModuleEntryPoint.S
a0176af91c8f02882f88d963accaf64e1f1d458f
[mirror_edk2.git] / ArmVirtPkg / PrePi / Arm / ModuleEntryPoint.S
1 //
2 // Copyright (c) 2011-2013, ARM Limited. All rights reserved.
3 // Copyright (c) 2015, Linaro Limited. All rights reserved.
4 //
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
9 //
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.
12 //
13 //
14
15 #include <AsmMacroIoLib.h>
16 #include <Base.h>
17 #include <Library/PcdLib.h>
18 #include <AutoGen.h>
19
20 .text
21 .align 3
22
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)
29
30 ASM_PFX(mSystemMemoryEnd): .quad 0
31
32 __relocs:
33 .long __reloc_base - __relocs
34 .long __reloc_start - __relocs
35 .long __reloc_end - __relocs
36
37 ASM_PFX(_ModuleEntryPoint):
38 //
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.
42 //
43
44 adr r12, __relocs
45 ldrd r4, r5, [r12]
46 ldr r6, [r12, #8]
47
48 add r4, r4, r12
49 add r5, r5, r12
50 add r6, r6, r12
51
52 .Lreloc_loop:
53 cmp r5, r6
54 bhs .Lreloc_done
55
56 //
57 // AArch32 uses the ELF32 REL format, which means each entry in the
58 // relocation table consists of
59 //
60 // UINT32 offset : the relative offset of the value that needs to
61 // be relocated
62 // UINT32 info : relocation type and symbol index (the latter is
63 // not used for R_ARM_RELATIVE relocations)
64 //
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
68
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
72 b .Lreloc_loop
73 .Lreloc_done:
74
75 // Do early platform specific actions
76 bl ASM_PFX(ArmPlatformPeiBootAction)
77
78 // Get ID of this CPU in Multicore system
79 bl ASM_PFX(ArmReadMpidr)
80 // Keep a copy of the MpId register value
81 mov r10, r0
82
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)
86 _SetupStackPosition:
87 // Compute Top of System Memory
88 ldr r12, =PcdGet64 (PcdSystemMemoryBase)
89 ldr r1, [r12]
90 ldr r12, =PcdGet64 (PcdSystemMemorySize)
91 ldrd r2, r3, [r12]
92
93 // calculate the top of memory, and record it in mSystemMemoryEnd
94 adds r2, r2, r1
95 sub r2, r2, #1
96 addcs r3, r3, #1
97 adr r12, mSystemMemoryEnd
98 strd r2, r3, [r12]
99
100 // truncate the memory used by UEFI to 4 GB range
101 teq r3, #0
102 movne r1, #-1
103 moveq r1, r2
104
105 // Calculate Top of the Firmware Device
106 ldr r12, =PcdGet64 (PcdFdBaseAddress)
107 ldr r2, [r12]
108 ldr r3, =FixedPcdGet32 (PcdFdSize)
109 sub r3, r3, #1
110 add r3, r3, r2 // r3 = FdTop = PcdFdBaseAddress + PcdFdSize
111
112 // UEFI Memory Size (stacks are allocated in this region)
113 LoadConstantToReg (FixedPcdGet32(PcdSystemMemoryUefiRegionSize), r4)
114
115 //
116 // Reserve the memory for the UEFI region (contain stacks on its top)
117 //
118
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
122 cmp r0, r4
123 bge _SetupStack
124
125 // Case the top of stacks is the FdBaseAddress
126 mov r1, r2
127
128 _SetupStack:
129 // r1 contains the top of the stack (and the UEFI Memory)
130
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)
134 adds r11, r1, #1
135 bcs _SetupOverflowStack
136
137 _SetupAlignedStack:
138 mov r1, r11
139 b _GetBaseUefiMemory
140
141 _SetupOverflowStack:
142 // Case memory at the top of the address space. Ensure the top of the stack is EFI_PAGE_SIZE
143 // aligned (4KB)
144 LoadConstantToReg (EFI_PAGE_MASK, r11)
145 and r11, r11, r1
146 sub r1, r1, r11
147
148 _GetBaseUefiMemory:
149 // Calculate the Base of the UEFI Memory
150 sub r11, r1, r4
151
152 _GetStackBase:
153 // r1 = The top of the Mpcore Stacks
154 // Stack for the primary core = PrimaryCoreStack
155 LoadConstantToReg (FixedPcdGet32(PcdCPUCorePrimaryStackSize), r2)
156 sub r9, r1, r2
157
158 // Stack for the secondary core = Number of Cores - 1
159 LoadConstantToReg (FixedPcdGet32(PcdCoreCount), r0)
160 sub r0, r0, #1
161 LoadConstantToReg (FixedPcdGet32(PcdCPUCoreSecondaryStackSize), r1)
162 mul r1, r1, r0
163 sub r9, r9, r1
164
165 // r9 = The base of the MpCore Stacks (primary stack & secondary stacks)
166 mov r0, r9
167 mov r1, r10
168 //ArmPlatformStackSet(StackBase, MpId, PrimaryStackSize, SecondaryStackSize)
169 LoadConstantToReg (FixedPcdGet32(PcdCPUCorePrimaryStackSize), r2)
170 LoadConstantToReg (FixedPcdGet32(PcdCPUCoreSecondaryStackSize), r3)
171 bl ASM_PFX(ArmPlatformStackSet)
172
173 // Is it the Primary Core ?
174 mov r0, r10
175 bl ASM_PFX(ArmPlatformIsPrimaryCore)
176 cmp r0, #1
177 bne _PrepareArguments
178
179 _PrepareArguments:
180 mov r0, r10
181 mov r1, r11
182 mov r2, r9
183
184 // Jump to PrePiCore C code
185 // r0 = MpId
186 // r1 = UefiMemoryBase
187 // r2 = StacksBase
188 bl ASM_PFX(CEntryPoint)
189
190 _NeverReturn:
191 b _NeverReturn