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