]> git.proxmox.com Git - mirror_edk2.git/blob - ArmVirtPkg/PrePi/AArch64/ModuleEntryPoint.S
ArmVirt/PrePi: make jump to CEntryPoint relative
[mirror_edk2.git] / ArmVirtPkg / PrePi / AArch64 / 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 <AsmMacroIoLibV8.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): .8byte 0
31
32 ASM_PFX(_ModuleEntryPoint):
33 //
34 // We are built as a ET_DYN PIE executable, so we need to process all
35 // relative relocations regardless of whether or not we are executing from
36 // the same offset we were linked at. This is only possible if we are
37 // running from RAM.
38 //
39 adr x8, __reloc_base
40 adr x9, __reloc_start
41 adr x10, __reloc_end
42
43 .Lreloc_loop:
44 cmp x9, x10
45 bhs .Lreloc_done
46
47 //
48 // AArch64 uses the ELF64 RELA format, which means each entry in the
49 // relocation table consists of
50 //
51 // UINT64 offset : the relative offset of the value that needs to
52 // be relocated
53 // UINT64 info : relocation type and symbol index (the latter is
54 // not used for R_AARCH64_RELATIVE relocations)
55 // UINT64 addend : value to be added to the value being relocated
56 //
57 ldp x11, x12, [x9], #24 // read offset into x11 and info into x12
58 cmp x12, #0x403 // check info == R_AARCH64_RELATIVE?
59 bne .Lreloc_loop // not a relative relocation? then skip
60
61 ldr x12, [x9, #-8] // read addend into x12
62 add x12, x12, x8 // add reloc base to addend to get relocated value
63 str x12, [x11, x8] // write relocated value at offset
64 b .Lreloc_loop
65 .Lreloc_done:
66
67 // Do early platform specific actions
68 bl ASM_PFX(ArmPlatformPeiBootAction)
69
70 // Get ID of this CPU in Multicore system
71 bl ASM_PFX(ArmReadMpidr)
72 // Keep a copy of the MpId register value
73 mov x20, x0
74
75 // Check if we can install the stack at the top of the System Memory or if we need
76 // to install the stacks at the bottom of the Firmware Device (case the FD is located
77 // at the top of the DRAM)
78 _SetupStackPosition:
79 // Compute Top of System Memory
80 ldr x1, PcdGet64 (PcdSystemMemoryBase)
81 ldr x2, PcdGet64 (PcdSystemMemorySize)
82 sub x2, x2, #1
83 add x1, x1, x2 // x1 = SystemMemoryTop = PcdSystemMemoryBase + PcdSystemMemorySize
84 adr x2, mSystemMemoryEnd
85 str x1, [x2]
86
87 // Calculate Top of the Firmware Device
88 ldr x2, PcdGet64 (PcdFdBaseAddress)
89 ldr w3, PcdGet32 (PcdFdSize)
90 sub x3, x3, #1
91 add x3, x3, x2 // x3 = FdTop = PcdFdBaseAddress + PcdFdSize
92
93 // UEFI Memory Size (stacks are allocated in this region)
94 LoadConstantToReg (FixedPcdGet32(PcdSystemMemoryUefiRegionSize), x4)
95
96 //
97 // Reserve the memory for the UEFI region (contain stacks on its top)
98 //
99
100 // Calculate how much space there is between the top of the Firmware and the Top of the System Memory
101 subs x0, x1, x3 // x0 = SystemMemoryTop - FdTop
102 b.mi _SetupStack // Jump if negative (FdTop > SystemMemoryTop). Case when the PrePi is in XIP memory outside of the DRAM
103 cmp x0, x4
104 b.ge _SetupStack
105
106 // Case the top of stacks is the FdBaseAddress
107 mov x1, x2
108
109 _SetupStack:
110 // x1 contains the top of the stack (and the UEFI Memory)
111
112 // Because the 'push' instruction is equivalent to 'stmdb' (decrement before), we need to increment
113 // one to the top of the stack. We check if incrementing one does not overflow (case of DRAM at the
114 // top of the memory space)
115 adds x21, x1, #1
116 b.cs _SetupOverflowStack
117
118 _SetupAlignedStack:
119 mov x1, x21
120 b _GetBaseUefiMemory
121
122 _SetupOverflowStack:
123 // Case memory at the top of the address space. Ensure the top of the stack is EFI_PAGE_SIZE
124 // aligned (4KB)
125 LoadConstantToReg (EFI_PAGE_MASK, x21)
126 and x21, x21, x1
127 sub x1, x1, x21
128
129 _GetBaseUefiMemory:
130 // Calculate the Base of the UEFI Memory
131 sub x21, x1, x4
132
133 _GetStackBase:
134 // r1 = The top of the Mpcore Stacks
135 // Stack for the primary core = PrimaryCoreStack
136 LoadConstantToReg (FixedPcdGet32(PcdCPUCorePrimaryStackSize), x2)
137 sub x22, x1, x2
138
139 // Stack for the secondary core = Number of Cores - 1
140 LoadConstantToReg (FixedPcdGet32(PcdCoreCount), x0)
141 sub x0, x0, #1
142 LoadConstantToReg (FixedPcdGet32(PcdCPUCoreSecondaryStackSize), x1)
143 mul x1, x1, x0
144 sub x22, x22, x1
145
146 // x22 = The base of the MpCore Stacks (primary stack & secondary stacks)
147 mov x0, x22
148 mov x1, x20
149 //ArmPlatformStackSet(StackBase, MpId, PrimaryStackSize, SecondaryStackSize)
150 LoadConstantToReg (FixedPcdGet32(PcdCPUCorePrimaryStackSize), x2)
151 LoadConstantToReg (FixedPcdGet32(PcdCPUCoreSecondaryStackSize), x3)
152 bl ASM_PFX(ArmPlatformStackSet)
153
154 // Is it the Primary Core ?
155 mov x0, x10
156 bl ASM_PFX(ArmPlatformIsPrimaryCore)
157 cmp x0, #1
158 bne _PrepareArguments
159
160 _PrepareArguments:
161 mov x0, x20
162 mov x1, x21
163 mov x2, x22
164
165 // Jump to PrePiCore C code
166 // x0 = MpId
167 // x1 = UefiMemoryBase
168 // x2 = StacksBase
169 bl ASM_PFX(CEntryPoint)
170
171 _NeverReturn:
172 b _NeverReturn