1 ;------------------------------------------------------------------------------ ;
2 ; Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.<BR>
3 ; SPDX-License-Identifier: BSD-2-Clause-Patent
11 ; This is the assembly code for MP support
13 ;-------------------------------------------------------------------------------
16 extern ASM_PFX(InitializeFloatingPointUnits)
22 ;-------------------------------------------------------------------------------------
23 ;RendezvousFunnelProc procedure follows. All APs execute their procedure. This
24 ;procedure serializes all the AP processors through an Init sequence. It must be
25 ;noted that APs arrive here very raw...ie: real mode, no stack.
26 ;ALSO THIS PROCEDURE IS EXECUTED BY APs ONLY ON 16 BIT MODE. HENCE THIS PROC
28 ;-------------------------------------------------------------------------------------
29 global ASM_PFX(RendezvousFunnelProc)
30 ASM_PFX(RendezvousFunnelProc):
31 RendezvousFunnelProcStart:
32 ; At this point CS = 0x(vv00) and ip= 0x0.
33 ; Save BIST information to ebp firstly
36 mov ebp, eax ; Save BIST information
46 mov si, BufferStartLocation
49 mov si, DataSegmentLocation
53 ; Get start address of 32-bit code in low memory (<1MB)
55 mov edi, ModeTransitionMemoryLocation
64 ; Switch to protected mode
66 mov eax, cr0 ; Get control register 0
67 or eax, 000000003h ; Set PE bit (bit #0) & MP
70 ; Switch to 32-bit code (>1MB)
74 ; Following code must be copied to memory with type of EfiBootServicesCode.
75 ; This is required if NX is enabled for EfiBootServicesCode of memory.
78 Flat32Start: ; protected mode entry point
86 ; Enable execute disable bit
88 mov esi, EnableExecuteDisableLocation
89 cmp byte [ebx + esi], 0
90 jz SkipEnableExecuteDisableBit
92 mov ecx, 0c0000080h ; EFER MSR number
94 bts eax, 11 ; Enable Execute Disable Bit
97 SkipEnableExecuteDisableBit:
104 mov esi, Enable5LevelPagingLocation
105 cmp byte [ebx + esi], 0
106 jz SkipEnable5LevelPaging
109 ; Enable 5 Level Paging
111 bts eax, 12 ; Set LA57=1.
113 SkipEnable5LevelPaging:
120 mov esi, Cr3Location ; Save CR3 in ecx
122 mov cr3, ecx ; Load CR3
127 mov ecx, 0c0000080h ; EFER MSR number
129 bts eax, 8 ; Set LME=1
135 mov eax, cr0 ; Read CR0
136 bts eax, 31 ; Set PG=1
137 mov cr0, eax ; Write CR0
140 ; Far jump to 64-bit code
142 mov edi, ModeHighMemoryLocation
149 lea edi, [esi + InitFlagLocation]
150 cmp qword [edi], 1 ; ApInitConfig
153 ; Increment the number of APs executing here as early as possible
154 ; This is decremented in C code when AP is finished executing
156 add edi, NumApsExecutingLocation
161 add edi, LockLocation
162 mov rax, NotVacantFlag
165 xchg qword [edi], rax
166 cmp rax, NotVacantFlag
169 lea ecx, [esi + ApIndexLocation]
175 xchg qword [edi], rax
178 add edi, StackSizeLocation
182 mul ecx ; EAX = StackSize * (CpuNumber + 1)
184 add edi, StackStartAddressLocation
193 jb NoX2Apic ; CPUID level below CPUID_EXTENDED_TOPOLOGY
199 jz NoX2Apic ; CPUID.0BH:EBX[15:0] is zero
201 ; Processor is x2APIC capable; 32-bit x2APIC ID is already in EDX
202 jmp GetProcessorNumber
205 ; Processor is not x2APIC capable, so get 8-bit APIC ID
213 ; Get processor number for this AP
214 ; Note that BSP may become an AP due to SwitchBsp()
217 lea eax, [esi + CpuInfoLocation]
221 cmp dword [edi], edx ; APIC ID match?
225 jmp GetNextProcNumber
228 mov rsp, qword [edi + 12]
231 push rbp ; Push BIST data at top of AP stack
232 xor rbp, rbp ; Clear ebp for call stack trace
236 mov rax, qword [esi + InitializeFloatingPointUnitsAddress]
238 call rax ; Call assembly function to initialize FPU per UEFI spec
241 mov edx, ebx ; edx is ApIndex
243 add ecx, LockLocation ; rcx is address of exchange info data buffer
246 add edi, ApProcedureLocation
250 call rax ; Invoke C function
252 jmp $ ; Should never reach here
254 RendezvousFunnelProcEnd:
256 ;-------------------------------------------------------------------------------------
257 ; AsmRelocateApLoop (MwaitSupport, ApTargetCState, PmCodeSegment, TopOfApStack, CountTofinish);
258 ;-------------------------------------------------------------------------------------
259 global ASM_PFX(AsmRelocateApLoop)
260 ASM_PFX(AsmRelocateApLoop):
261 AsmRelocateApLoopStart:
262 cli ; Disable interrupt before switching to 32-bit mode
263 mov rax, [rsp + 40] ; CountTofinish
264 lock dec dword [rax] ; (*CountTofinish)--
269 lea rsi, [PmEntry] ; rsi <- The start address of transition code
278 btr eax, 31 ; Clear CR0.PG
279 mov cr0, eax ; Disable paging and caches
281 mov ebx, edx ; Save EntryPoint to rbx, for rdmsr will overwrite rdx
284 and ah, ~ 1 ; Clear LME
287 and al, ~ (1 << 5) ; Clear PAE
294 cmp cl, 1 ; Check mwait-monitor support
296 mov ebx, edx ; Save C-State to ebx
299 mov eax, esp ; Set Monitor Address
300 xor ecx, ecx ; ecx = 0
301 xor edx, edx ; edx = 0
303 mov eax, ebx ; Mwait Cx, Target C-State per eax[7:4]
312 AsmRelocateApLoopEnd:
314 ;-------------------------------------------------------------------------------------
315 ; AsmGetAddressMap (&AddressMap);
316 ;-------------------------------------------------------------------------------------
317 global ASM_PFX(AsmGetAddressMap)
318 ASM_PFX(AsmGetAddressMap):
319 lea rax, [ASM_PFX(RendezvousFunnelProc)]
321 mov qword [rcx + 8h], LongModeStart - RendezvousFunnelProcStart
322 mov qword [rcx + 10h], RendezvousFunnelProcEnd - RendezvousFunnelProcStart
323 lea rax, [ASM_PFX(AsmRelocateApLoop)]
324 mov qword [rcx + 18h], rax
325 mov qword [rcx + 20h], AsmRelocateApLoopEnd - AsmRelocateApLoopStart
326 mov qword [rcx + 28h], Flat32Start - RendezvousFunnelProcStart
329 ;-------------------------------------------------------------------------------------
330 ;AsmExchangeRole procedure follows. This procedure executed by current BSP, that is
331 ;about to become an AP. It switches its stack with the current AP.
332 ;AsmExchangeRole (IN CPU_EXCHANGE_INFO *MyInfo, IN CPU_EXCHANGE_INFO *OthersInfo);
333 ;-------------------------------------------------------------------------------------
334 global ASM_PFX(AsmExchangeRole)
335 ASM_PFX(AsmExchangeRole):
336 ; DO NOT call other functions in this function, since 2 CPU may use 1 stack
337 ; at the same time. If 1 CPU try to call a function, stack will be corrupted.
361 ; rsi contains MyInfo pointer
364 ; rdi contains OthersInfo pointer
367 ;Store EFLAGS, GDTR and IDTR regiter to stack
372 ; Store the its StackPointer
375 ; update its switch state to STORED
376 mov byte [rsi], CPU_SWITCH_STATE_STORED
379 ; wait until the other CPU finish storing its state
380 cmp byte [rdi], CPU_SWITCH_STATE_STORED
383 jmp WaitForOtherStored
386 ; Since another CPU already stored its state, load them
393 ; load its future StackPointer
396 ; update the other CPU's switch state to LOADED
397 mov byte [rdi], CPU_SWITCH_STATE_LOADED
400 ; wait until the other CPU finish loading new state,
401 ; otherwise the data in stack may corrupt
402 cmp byte [rsi], CPU_SWITCH_STATE_LOADED
405 jmp WaitForOtherLoaded
408 ; since the other CPU already get the data it want, leave this procedure