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