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