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