]> git.proxmox.com Git - mirror_edk2.git/blob - ArmVirtPkg/PrePi/Arm/ModuleEntryPoint.S
ArmVirtPkg/PrePi: use correct callee saved regs
[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 StartupAddr: .long ASM_PFX(CEntryPoint)
31 ASM_PFX(mSystemMemoryEnd): .quad 0
32
33 __relocs:
34 .long __reloc_base - __relocs
35 .long __reloc_start - __relocs
36 .long __reloc_end - __relocs
37
38 ASM_PFX(_ModuleEntryPoint):
39 //
40 // We are built as a ET_DYN PIE executable, so we need to process all
41 // relative relocations if we are executing from a different offset than we
42 // were linked at. This is only possible if we are running from RAM.
43 //
44
45 adr r12, __relocs
46 ldrd r4, r5, [r12]
47 ldr r6, [r12, #8]
48
49 add r4, r4, r12
50 add r5, r5, r12
51 add r6, r6, r12
52
53 .Lreloc_loop:
54 cmp r5, r6
55 bhs .Lreloc_done
56
57 //
58 // AArch32 uses the ELF32 REL format, which means each entry in the
59 // relocation table consists of
60 //
61 // UINT32 offset : the relative offset of the value that needs to
62 // be relocated
63 // UINT32 info : relocation type and symbol index (the latter is
64 // not used for R_ARM_RELATIVE relocations)
65 //
66 ldrd r8, r9, [r5], #8 // read offset into r8 and info into r9
67 cmp r9, #23 // check info == R_ARM_RELATIVE?
68 bne .Lreloc_loop // not a relative relocation? then skip
69
70 ldr r9, [r8, r4] // read addend into r9
71 add r9, r9, r1 // add image base to addend to get relocated value
72 str r9, [r8, r4] // write relocated value at offset
73 b .Lreloc_loop
74 .Lreloc_done:
75
76 // Do early platform specific actions
77 bl ASM_PFX(ArmPlatformPeiBootAction)
78
79 // Get ID of this CPU in Multicore system
80 bl ASM_PFX(ArmReadMpidr)
81 // Keep a copy of the MpId register value
82 mov r10, r0
83
84 // Check if we can install the stack at the top of the System Memory or if we need
85 // to install the stacks at the bottom of the Firmware Device (case the FD is located
86 // at the top of the DRAM)
87 _SetupStackPosition:
88 // Compute Top of System Memory
89 ldr r12, =PcdGet64 (PcdSystemMemoryBase)
90 ldr r1, [r12]
91 ldr r12, =PcdGet64 (PcdSystemMemorySize)
92 ldrd r2, r3, [r12]
93
94 // calculate the top of memory, and record it in mSystemMemoryEnd
95 adds r2, r2, r1
96 sub r2, r2, #1
97 addcs r3, r3, #1
98 adr r12, mSystemMemoryEnd
99 strd r2, r3, [r12]
100
101 // truncate the memory used by UEFI to 4 GB range
102 teq r3, #0
103 movne r1, #-1
104 moveq r1, r2
105
106 // Calculate Top of the Firmware Device
107 ldr r12, =PcdGet64 (PcdFdBaseAddress)
108 ldr r2, [r12]
109 ldr r3, =FixedPcdGet32 (PcdFdSize)
110 sub r3, r3, #1
111 add r3, r3, r2 // r3 = FdTop = PcdFdBaseAddress + PcdFdSize
112
113 // UEFI Memory Size (stacks are allocated in this region)
114 LoadConstantToReg (FixedPcdGet32(PcdSystemMemoryUefiRegionSize), r4)
115
116 //
117 // Reserve the memory for the UEFI region (contain stacks on its top)
118 //
119
120 // Calculate how much space there is between the top of the Firmware and the Top of the System Memory
121 subs r0, r1, r3 // r0 = SystemMemoryTop - FdTop
122 bmi _SetupStack // Jump if negative (FdTop > SystemMemoryTop). Case when the PrePi is in XIP memory outside of the DRAM
123 cmp r0, r4
124 bge _SetupStack
125
126 // Case the top of stacks is the FdBaseAddress
127 mov r1, r2
128
129 _SetupStack:
130 // r1 contains the top of the stack (and the UEFI Memory)
131
132 // Because the 'push' instruction is equivalent to 'stmdb' (decrement before), we need to increment
133 // one to the top of the stack. We check if incrementing one does not overflow (case of DRAM at the
134 // top of the memory space)
135 adds r11, r1, #1
136 bcs _SetupOverflowStack
137
138 _SetupAlignedStack:
139 mov r1, r11
140 b _GetBaseUefiMemory
141
142 _SetupOverflowStack:
143 // Case memory at the top of the address space. Ensure the top of the stack is EFI_PAGE_SIZE
144 // aligned (4KB)
145 LoadConstantToReg (EFI_PAGE_MASK, r11)
146 and r11, r11, r1
147 sub r1, r1, r11
148
149 _GetBaseUefiMemory:
150 // Calculate the Base of the UEFI Memory
151 sub r11, r1, r4
152
153 _GetStackBase:
154 // r1 = The top of the Mpcore Stacks
155 // Stack for the primary core = PrimaryCoreStack
156 LoadConstantToReg (FixedPcdGet32(PcdCPUCorePrimaryStackSize), r2)
157 sub r9, r1, r2
158
159 // Stack for the secondary core = Number of Cores - 1
160 LoadConstantToReg (FixedPcdGet32(PcdCoreCount), r0)
161 sub r0, r0, #1
162 LoadConstantToReg (FixedPcdGet32(PcdCPUCoreSecondaryStackSize), r1)
163 mul r1, r1, r0
164 sub r9, r9, r1
165
166 // r9 = The base of the MpCore Stacks (primary stack & secondary stacks)
167 mov r0, r9
168 mov r1, r10
169 //ArmPlatformStackSet(StackBase, MpId, PrimaryStackSize, SecondaryStackSize)
170 LoadConstantToReg (FixedPcdGet32(PcdCPUCorePrimaryStackSize), r2)
171 LoadConstantToReg (FixedPcdGet32(PcdCPUCoreSecondaryStackSize), r3)
172 bl ASM_PFX(ArmPlatformStackSet)
173
174 // Is it the Primary Core ?
175 mov r0, r10
176 bl ASM_PFX(ArmPlatformIsPrimaryCore)
177 cmp r0, #1
178 bne _PrepareArguments
179
180 _PrepareArguments:
181 mov r0, r10
182 mov r1, r11
183 mov r2, r9
184
185 // Move sec startup address into a data register
186 // Ensure we're jumping to FV version of the code (not boot remapped alias)
187 ldr r4, StartupAddr
188
189 // Jump to PrePiCore C code
190 // r0 = MpId
191 // r1 = UefiMemoryBase
192 // r2 = StacksBase
193 blx r4
194
195 _NeverReturn:
196 b _NeverReturn