-;------------------------------------------------------------------------------ ;
-; Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
-; This program and the accompanying materials
-; are licensed and made available under the terms and conditions of the BSD License
-; which accompanies this distribution. The full text of the license may be found at
-; http://opensource.org/licenses/bsd-license.php.
-;
-; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
-; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-;
-; Module Name:
-;
-; MpFuncs.nasm
-;
-; Abstract:
-;
-; This is the assembly code for MP support
-;
-;-------------------------------------------------------------------------------
-
-%include "MpEqu.inc"
-extern ASM_PFX(InitializeFloatingPointUnits)
-
-DEFAULT REL
-
-SECTION .text
-
-;-------------------------------------------------------------------------------------
-;RendezvousFunnelProc procedure follows. All APs execute their procedure. This
-;procedure serializes all the AP processors through an Init sequence. It must be
-;noted that APs arrive here very raw...ie: real mode, no stack.
-;ALSO THIS PROCEDURE IS EXECUTED BY APs ONLY ON 16 BIT MODE. HENCE THIS PROC
-;IS IN MACHINE CODE.
-;-------------------------------------------------------------------------------------
-global ASM_PFX(RendezvousFunnelProc)
-ASM_PFX(RendezvousFunnelProc):
-RendezvousFunnelProcStart:
-; At this point CS = 0x(vv00) and ip= 0x0.
-; Save BIST information to ebp firstly
-BITS 16
-
- mov eax, 1234h
- mov ebp, eax ; save BIST information
-
- mov ax, cs
- mov ds, ax
- mov es, ax
- mov ss, ax
- xor ax, ax
- mov fs, ax
- mov gs, ax
-
- mov si, BufferStartLocation
- mov ebx, [si]
-
- mov di, PmodeOffsetLocation
- mov eax, [di]
- mov di, ax
- sub di, 06h
- add eax, ebx
- mov [di],eax
-
- mov di, LmodeOffsetLocation
- mov eax, [di]
- mov di, ax
- sub di, 06h
- add eax, ebx
- mov [di],eax
-
-
- mov si, Cr3Location
- mov ecx,[si] ; ECX is keeping the value of CR3
-
- mov si, GdtrLocation
-o32 lgdt [cs:si]
-
- mov si, IdtrLocation
-o32 lidt [cs:si]
-
-
- xor ax, ax
- mov ds, ax
-
- mov eax, cr0 ;Get control register 0
- or eax, 000000003h ;Set PE bit (bit #0) & MP
- mov cr0, eax
-
- jmp PROTECT_MODE_CS:strict dword 0 ; far jump to protected mode
-BITS 32
-Flat32Start: ; protected mode entry point
- mov ax, PROTECT_MODE_DS
- mov ds, ax
- mov es, ax
- mov fs, ax
- mov gs, ax
- mov ss, ax
-
- mov eax, cr4
- bts eax, 5
- mov cr4, eax
-
- mov cr3, ecx
-
-
- mov ecx, 0c0000080h ; EFER MSR number.
- rdmsr ; Read EFER.
- bts eax, 8 ; Set LME=1.
- wrmsr ; Write EFER.
-
- mov eax, cr0 ; Read CR0.
- bts eax, 31 ; Set PG=1.
- mov cr0, eax ; Write CR0.
-
- jmp LONG_MODE_CS:strict dword 0 ; far jump to long mode
-BITS 64
-LongModeStart:
- mov ax, LONG_MODE_DS
- mov ds, ax
- mov es, ax
- mov ss, ax
-
- mov esi, ebx
- mov edi, esi
- add edi, LockLocation
- mov rax, NotVacantFlag
-
-TestLock:
- xchg qword [edi], rax
- cmp rax, NotVacantFlag
- jz TestLock
-
- mov edi, esi
- add edi, NumApsExecutingLoction
- inc dword [edi]
- mov ebx, [edi]
-
-ProgramStack:
- mov edi, esi
- add edi, StackSizeLocation
- mov rax, qword [edi]
- mov edi, esi
- add edi, StackStartAddressLocation
- add rax, qword [edi]
- mov rsp, rax
- mov qword [edi], rax
-
-Releaselock:
- mov rax, VacantFlag
- mov edi, esi
- add edi, LockLocation
- xchg qword [edi], rax
-
-CProcedureInvoke:
- push rbp ; push BIST data at top of AP stack
- xor rbp, rbp ; clear ebp for call stack trace
- push rbp
- mov rbp, rsp
-
- mov rax, ASM_PFX(InitializeFloatingPointUnits)
- sub rsp, 20h
- call rax ; Call assembly function to initialize FPU per UEFI spec
- add rsp, 20h
-
- mov edx, ebx ; edx is NumApsExecuting
- mov ecx, esi
- add ecx, LockLocation ; rcx is address of exchange info data buffer
-
- mov edi, esi
- add edi, ApProcedureLocation
- mov rax, qword [edi]
-
- sub rsp, 20h
- call rax ; invoke C function
- add rsp, 20h
-
-RendezvousFunnelProcEnd:
-
-;-------------------------------------------------------------------------------------
-; AsmGetAddressMap (&AddressMap);
-;-------------------------------------------------------------------------------------
-global ASM_PFX(AsmGetAddressMap)
-ASM_PFX(AsmGetAddressMap):
- mov rax, ASM_PFX(RendezvousFunnelProc)
- mov qword [rcx], rax
- mov qword [rcx + 8h], Flat32Start - RendezvousFunnelProcStart
- mov qword [rcx + 10h], LongModeStart - RendezvousFunnelProcStart
- mov qword [rcx + 18h], RendezvousFunnelProcEnd - RendezvousFunnelProcStart
- ret
-
-global ASM_PFX(AsmInitializeGdt)
-ASM_PFX(AsmInitializeGdt):
- push rbp
- mov rbp, rsp
-
- lgdt [rcx] ; update the GDTR
-
- sub rsp, 0x10
- mov rax, ASM_PFX(SetCodeSelectorFarJump)
- mov [rsp], rax
- mov rdx, LONG_MODE_CS
- mov [rsp + 4], dx ; get new CS
- jmp far dword [rsp] ; far jump with new CS
-ASM_PFX(SetCodeSelectorFarJump):
- add rsp, 0x10
-
- mov rax, LONG_MODE_DS ; get new DS
- mov ds, ax
- mov es, ax
- mov fs, ax
- mov gs, ax
- mov ss, ax
-
- pop rbp
-
- ret
+;------------------------------------------------------------------------------ ;\r
+; Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+; This program and the accompanying materials\r
+; are licensed and made available under the terms and conditions of the BSD License\r
+; which accompanies this distribution. The full text of the license may be found at\r
+; http://opensource.org/licenses/bsd-license.php.\r
+;\r
+; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+;\r
+; Module Name:\r
+;\r
+; MpFuncs.nasm\r
+;\r
+; Abstract:\r
+;\r
+; This is the assembly code for MP support\r
+;\r
+;-------------------------------------------------------------------------------\r
+\r
+%include "MpEqu.inc"\r
+extern ASM_PFX(InitializeFloatingPointUnits)\r
+\r
+DEFAULT REL\r
+\r
+SECTION .text\r
+\r
+;-------------------------------------------------------------------------------------\r
+;RendezvousFunnelProc procedure follows. All APs execute their procedure. This\r
+;procedure serializes all the AP processors through an Init sequence. It must be\r
+;noted that APs arrive here very raw...ie: real mode, no stack.\r
+;ALSO THIS PROCEDURE IS EXECUTED BY APs ONLY ON 16 BIT MODE. HENCE THIS PROC\r
+;IS IN MACHINE CODE.\r
+;-------------------------------------------------------------------------------------\r
+global ASM_PFX(RendezvousFunnelProc)\r
+ASM_PFX(RendezvousFunnelProc):\r
+RendezvousFunnelProcStart:\r
+; At this point CS = 0x(vv00) and ip= 0x0.\r
+; Save BIST information to ebp firstly\r
+BITS 16\r
+\r
+ mov eax, 1234h\r
+ mov ebp, eax ; save BIST information\r
+\r
+ mov ax, cs\r
+ mov ds, ax\r
+ mov es, ax\r
+ mov ss, ax\r
+ xor ax, ax\r
+ mov fs, ax\r
+ mov gs, ax\r
+\r
+ mov si, BufferStartLocation\r
+ mov ebx, [si]\r
+\r
+ mov di, PmodeOffsetLocation\r
+ mov eax, [di]\r
+ mov di, ax\r
+ sub di, 06h\r
+ add eax, ebx\r
+ mov [di],eax\r
+\r
+ mov di, LmodeOffsetLocation\r
+ mov eax, [di]\r
+ mov di, ax\r
+ sub di, 06h\r
+ add eax, ebx\r
+ mov [di],eax\r
+\r
+\r
+ mov si, Cr3Location\r
+ mov ecx,[si] ; ECX is keeping the value of CR3\r
+\r
+ mov si, GdtrLocation\r
+o32 lgdt [cs:si]\r
+\r
+ mov si, IdtrLocation\r
+o32 lidt [cs:si]\r
+\r
+\r
+ xor ax, ax\r
+ mov ds, ax\r
+\r
+ mov eax, cr0 ;Get control register 0\r
+ or eax, 000000003h ;Set PE bit (bit #0) & MP\r
+ mov cr0, eax\r
+\r
+ jmp PROTECT_MODE_CS:strict dword 0 ; far jump to protected mode\r
+BITS 32\r
+Flat32Start: ; protected mode entry point\r
+ mov ax, PROTECT_MODE_DS\r
+ mov ds, ax\r
+ mov es, ax\r
+ mov fs, ax\r
+ mov gs, ax\r
+ mov ss, ax\r
+\r
+ mov eax, cr4\r
+ bts eax, 5\r
+ mov cr4, eax\r
+\r
+ mov cr3, ecx\r
+\r
+\r
+ mov ecx, 0c0000080h ; EFER MSR number.\r
+ rdmsr ; Read EFER.\r
+ bts eax, 8 ; Set LME=1.\r
+ wrmsr ; Write EFER.\r
+\r
+ mov eax, cr0 ; Read CR0.\r
+ bts eax, 31 ; Set PG=1.\r
+ mov cr0, eax ; Write CR0.\r
+\r
+ jmp LONG_MODE_CS:strict dword 0 ; far jump to long mode\r
+BITS 64\r
+LongModeStart:\r
+ mov ax, LONG_MODE_DS\r
+ mov ds, ax\r
+ mov es, ax\r
+ mov ss, ax\r
+\r
+ mov esi, ebx\r
+ mov edi, esi\r
+ add edi, LockLocation\r
+ mov rax, NotVacantFlag\r
+\r
+TestLock:\r
+ xchg qword [edi], rax\r
+ cmp rax, NotVacantFlag\r
+ jz TestLock\r
+\r
+ mov edi, esi\r
+ add edi, NumApsExecutingLoction\r
+ inc dword [edi]\r
+ mov ebx, [edi]\r
+\r
+ProgramStack:\r
+ mov edi, esi\r
+ add edi, StackSizeLocation\r
+ mov rax, qword [edi]\r
+ mov edi, esi\r
+ add edi, StackStartAddressLocation\r
+ add rax, qword [edi]\r
+ mov rsp, rax\r
+ mov qword [edi], rax\r
+\r
+Releaselock:\r
+ mov rax, VacantFlag\r
+ mov edi, esi\r
+ add edi, LockLocation\r
+ xchg qword [edi], rax\r
+\r
+CProcedureInvoke:\r
+ push rbp ; push BIST data at top of AP stack\r
+ xor rbp, rbp ; clear ebp for call stack trace\r
+ push rbp\r
+ mov rbp, rsp\r
+\r
+ mov rax, ASM_PFX(InitializeFloatingPointUnits)\r
+ sub rsp, 20h\r
+ call rax ; Call assembly function to initialize FPU per UEFI spec\r
+ add rsp, 20h\r
+\r
+ mov edx, ebx ; edx is NumApsExecuting\r
+ mov ecx, esi\r
+ add ecx, LockLocation ; rcx is address of exchange info data buffer\r
+\r
+ mov edi, esi\r
+ add edi, ApProcedureLocation\r
+ mov rax, qword [edi]\r
+\r
+ sub rsp, 20h\r
+ call rax ; invoke C function\r
+ add rsp, 20h\r
+\r
+RendezvousFunnelProcEnd:\r
+\r
+global ASM_PFX(AsmCliHltLoop)\r
+ASM_PFX(AsmCliHltLoop):\r
+ cli\r
+ hlt\r
+ jmp $-2\r
+\r
+;-------------------------------------------------------------------------------------\r
+; AsmGetAddressMap (&AddressMap);\r
+;-------------------------------------------------------------------------------------\r
+global ASM_PFX(AsmGetAddressMap)\r
+ASM_PFX(AsmGetAddressMap):\r
+ mov rax, ASM_PFX(RendezvousFunnelProc)\r
+ mov qword [rcx], rax\r
+ mov qword [rcx + 8h], Flat32Start - RendezvousFunnelProcStart\r
+ mov qword [rcx + 10h], LongModeStart - RendezvousFunnelProcStart\r
+ mov qword [rcx + 18h], RendezvousFunnelProcEnd - RendezvousFunnelProcStart\r
+ ret\r
+\r
+;-------------------------------------------------------------------------------------\r
+;AsmExchangeRole procedure follows. This procedure executed by current BSP, that is\r
+;about to become an AP. It switches it'stack with the current AP.\r
+;AsmExchangeRole (IN CPU_EXCHANGE_INFO *MyInfo, IN CPU_EXCHANGE_INFO *OthersInfo);\r
+;-------------------------------------------------------------------------------------\r
+global ASM_PFX(AsmExchangeRole)\r
+ASM_PFX(AsmExchangeRole):\r
+ ; DO NOT call other functions in this function, since 2 CPU may use 1 stack\r
+ ; at the same time. If 1 CPU try to call a function, stack will be corrupted.\r
+\r
+ push rax\r
+ push rbx\r
+ push rcx\r
+ push rdx\r
+ push rsi\r
+ push rdi\r
+ push rbp\r
+ push r8\r
+ push r9\r
+ push r10\r
+ push r11\r
+ push r12\r
+ push r13\r
+ push r14\r
+ push r15\r
+\r
+ mov rax, cr0\r
+ push rax\r
+\r
+ mov rax, cr4\r
+ push rax\r
+\r
+ ; rsi contains MyInfo pointer\r
+ mov rsi, rcx\r
+\r
+ ; rdi contains OthersInfo pointer\r
+ mov rdi, rdx\r
+\r
+ ;Store EFLAGS, GDTR and IDTR regiter to stack\r
+ pushfq\r
+ sgdt [rsi + 16]\r
+ sidt [rsi + 26]\r
+\r
+ ; Store the its StackPointer\r
+ mov [rsi + 8], rsp\r
+\r
+ ; update its switch state to STORED\r
+ mov byte [rsi], CPU_SWITCH_STATE_STORED\r
+\r
+WaitForOtherStored:\r
+ ; wait until the other CPU finish storing its state\r
+ cmp byte [rdi], CPU_SWITCH_STATE_STORED\r
+ jz OtherStored\r
+ pause\r
+ jmp WaitForOtherStored\r
+\r
+OtherStored:\r
+ ; Since another CPU already stored its state, load them\r
+ ; load GDTR value\r
+ lgdt [rdi + 16]\r
+\r
+ ; load IDTR value\r
+ lidt [rdi + 26]\r
+\r
+ ; load its future StackPointer\r
+ mov rsp, [rdi + 8]\r
+\r
+ ; update the other CPU's switch state to LOADED\r
+ mov byte [rdi], CPU_SWITCH_STATE_LOADED\r
+\r
+WaitForOtherLoaded:\r
+ ; wait until the other CPU finish loading new state,\r
+ ; otherwise the data in stack may corrupt\r
+ cmp byte [rsi], CPU_SWITCH_STATE_LOADED\r
+ jz OtherLoaded\r
+ pause\r
+ jmp WaitForOtherLoaded\r
+\r
+OtherLoaded:\r
+ ; since the other CPU already get the data it want, leave this procedure\r
+ popfq\r
+\r
+ pop rax\r
+ mov cr4, rax\r
+\r
+ pop rax\r
+ mov cr0, rax\r
+\r
+ pop r15\r
+ pop r14\r
+ pop r13\r
+ pop r12\r
+ pop r11\r
+ pop r10\r
+ pop r9\r
+ pop r8\r
+ pop rbp\r
+ pop rdi\r
+ pop rsi\r
+ pop rdx\r
+ pop rcx\r
+ pop rbx\r
+ pop rax\r
+\r
+ ret\r
+\r
+global ASM_PFX(AsmInitializeGdt)\r
+ASM_PFX(AsmInitializeGdt):\r
+ push rbp\r
+ mov rbp, rsp\r
+\r
+ lgdt [rcx] ; update the GDTR\r
+\r
+ sub rsp, 0x10\r
+ mov rax, ASM_PFX(SetCodeSelectorFarJump)\r
+ mov [rsp], rax\r
+ mov rdx, LONG_MODE_CS\r
+ mov [rsp + 4], dx ; get new CS\r
+ jmp far dword [rsp] ; far jump with new CS\r
+ASM_PFX(SetCodeSelectorFarJump):\r
+ add rsp, 0x10\r
+\r
+ mov rax, LONG_MODE_DS ; get new DS\r
+ mov ds, ax\r
+ mov es, ax\r
+ mov fs, ax\r
+ mov gs, ax\r
+ mov ss, ax\r
+\r
+ pop rbp\r
+\r
+ ret\r