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
17ASM_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  // 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   x20, x0
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  ldr   x1, PcdGet64 (PcdSystemMemoryBase)
66  ldr   x2, PcdGet64 (PcdSystemMemorySize)
67  sub   x2, x2, #1
68  add   x1, x1, x2      // x1 = SystemMemoryTop = PcdSystemMemoryBase + PcdSystemMemorySize
69
70  // Calculate Top of the Firmware Device
71  ldr   x2, PcdGet64 (PcdFdBaseAddress)
72  MOV32 (w3, FixedPcdGet32 (PcdFdSize) - 1)
73  add   x3, x3, x2      // x3 = FdTop = PcdFdBaseAddress + PcdFdSize
74
75  // UEFI Memory Size (stacks are allocated in this region)
76  MOV32 (x4, FixedPcdGet32(PcdSystemMemoryUefiRegionSize))
77
78  //
79  // Reserve the memory for the UEFI region (contain stacks on its top)
80  //
81
82  // Calculate how much space there is between the top of the Firmware and the Top of the System Memory
83  subs  x0, x1, x3   // x0 = SystemMemoryTop - FdTop
84  b.mi  _SetupStack  // Jump if negative (FdTop > SystemMemoryTop). Case when the PrePi is in XIP memory outside of the DRAM
85  cmp   x0, x4
86  b.ge  _SetupStack
87
88  // Case the top of stacks is the FdBaseAddress
89  mov   x1, x2
90
91_SetupStack:
92  // x1 contains the top of the stack (and the UEFI Memory)
93
94  // Because the 'push' instruction is equivalent to 'stmdb' (decrement before), we need to increment
95  // one to the top of the stack. We check if incrementing one does not overflow (case of DRAM at the
96  // top of the memory space)
97  adds  x21, x1, #1
98  b.cs  _SetupOverflowStack
99
100_SetupAlignedStack:
101  mov   x1, x21
102  b     _GetBaseUefiMemory
103
104_SetupOverflowStack:
105  // Case memory at the top of the address space. Ensure the top of the stack is EFI_PAGE_SIZE
106  // aligned (4KB)
107  and   x1, x1, ~EFI_PAGE_MASK
108
109_GetBaseUefiMemory:
110  // Calculate the Base of the UEFI Memory
111  sub   x21, x1, x4
112
113_GetStackBase:
114  // r1 = The top of the Mpcore Stacks
115  // Stack for the primary core = PrimaryCoreStack
116  MOV32 (x2, FixedPcdGet32(PcdCPUCorePrimaryStackSize))
117  sub   x22, x1, x2
118
119  // Stack for the secondary core = Number of Cores - 1
120  MOV32 (x1, (FixedPcdGet32(PcdCoreCount) - 1) * FixedPcdGet32(PcdCPUCoreSecondaryStackSize))
121  sub   x22, x22, x1
122
123  // x22 = The base of the MpCore Stacks (primary stack & secondary stacks)
124  mov   x0, x22
125  mov   x1, x20
126  //ArmPlatformStackSet(StackBase, MpId, PrimaryStackSize, SecondaryStackSize)
127  MOV32 (x2, FixedPcdGet32(PcdCPUCorePrimaryStackSize))
128  MOV32 (x3, FixedPcdGet32(PcdCPUCoreSecondaryStackSize))
129  bl    ASM_PFX(ArmPlatformStackSet)
130
131  // Is it the Primary Core ?
132  mov   x0, x10
133  bl    ASM_PFX(ArmPlatformIsPrimaryCore)
134  cmp   x0, #1
135  bne   _PrepareArguments
136
137_PrepareArguments:
138  mov   x0, x20
139  mov   x1, x21
140  mov   x2, x22
141
142  // Jump to PrePiCore C code
143  //    x0 = MpId
144  //    x1 = UefiMemoryBase
145  //    x2 = StacksBase
146  bl    ASM_PFX(CEntryPoint)
147
148_NeverReturn:
149  b _NeverReturn
150