-;------------------------------------------------------------------------------ ;
-; 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)
-
-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.
-BITS 16
- 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 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 esi, ebx
- mov edi, esi
- add edi, LockLocation
- mov eax, NotVacantFlag
-
-TestLock:
- xchg [edi], eax
- cmp eax, NotVacantFlag
- jz TestLock
-
- mov edi, esi
- add edi, NumApsExecutingLoction
- inc dword [edi]
- mov ebx, [edi]
-
-ProgramStack:
- mov edi, esi
- add edi, StackSizeLocation
- mov eax, [edi]
- mov edi, esi
- add edi, StackStartAddressLocation
- add eax, [edi]
- mov esp, eax
- mov [edi], eax
-
-Releaselock:
- mov eax, VacantFlag
- mov edi, esi
- add edi, LockLocation
- xchg [edi], eax
-
-CProcedureInvoke:
- push ebp ; push BIST data at top of AP stack
- xor ebp, ebp ; clear ebp for call stack trace
- push ebp
- mov ebp, esp
-
- mov eax, ASM_PFX(InitializeFloatingPointUnits)
- call eax ; Call assembly function to initialize FPU per UEFI spec
-
- push ebx ; Push NumApsExecuting
- mov eax, esi
- add eax, LockLocation
- push eax ; push address of exchange info data buffer
-
- mov edi, esi
- add edi, ApProcedureLocation
- mov eax, [edi]
-
- call eax ; invoke C function
-
- jmp $ ; never reach here
-RendezvousFunnelProcEnd:
-
-global ASM_PFX(AsmCliHltLoop)
-ASM_PFX(AsmCliHltLoop):
- cli
- hlt
- jmp $-2
-
-;-------------------------------------------------------------------------------------
-; AsmGetAddressMap (&AddressMap);
-;-------------------------------------------------------------------------------------
-global ASM_PFX(AsmGetAddressMap)
-ASM_PFX(AsmGetAddressMap):
- pushad
- mov ebp,esp
-
- mov ebx, [ebp + 24h]
- mov dword [ebx], RendezvousFunnelProcStart
- mov dword [ebx + 4h], Flat32Start - RendezvousFunnelProcStart
- mov dword [ebx + 8h], 0
- mov dword [ebx + 0ch], RendezvousFunnelProcEnd - RendezvousFunnelProcStart
-
- popad
- ret
-
-;-------------------------------------------------------------------------------------
-;AsmExchangeRole procedure follows. This procedure executed by current BSP, that is
-;about to become an AP. It switches it'stack with the current AP.
-;AsmExchangeRole (IN CPU_EXCHANGE_INFO *MyInfo, IN CPU_EXCHANGE_INFO *OthersInfo);
-;-------------------------------------------------------------------------------------
-global ASM_PFX(AsmExchangeRole)
-ASM_PFX(AsmExchangeRole):
- ; DO NOT call other functions in this function, since 2 CPU may use 1 stack
- ; at the same time. If 1 CPU try to call a function, stack will be corrupted.
- pushad
- mov ebp,esp
-
- ; esi contains MyInfo pointer
- mov esi, [ebp + 24h]
-
- ; edi contains OthersInfo pointer
- mov edi, [ebp + 28h]
-
- ;Store EFLAGS, GDTR and IDTR register to stack
- pushfd
- mov eax, cr4
- push eax ; push cr4 firstly
- mov eax, cr0
- push eax
-
- sgdt [esi + 8]
- sidt [esi + 14]
-
- ; Store the its StackPointer
- mov [esi + 4],esp
-
- ; update its switch state to STORED
- mov byte [esi], CPU_SWITCH_STATE_STORED
-
-WaitForOtherStored:
- ; wait until the other CPU finish storing its state
- cmp byte [edi], CPU_SWITCH_STATE_STORED
- jz OtherStored
- pause
- jmp WaitForOtherStored
-
-OtherStored:
- ; Since another CPU already stored its state, load them
- ; load GDTR value
- lgdt [edi + 8]
-
- ; load IDTR value
- lidt [edi + 14]
-
- ; load its future StackPointer
- mov esp, [edi + 4]
-
- ; update the other CPU's switch state to LOADED
- mov byte [edi], CPU_SWITCH_STATE_LOADED
-
-WaitForOtherLoaded:
- ; wait until the other CPU finish loading new state,
- ; otherwise the data in stack may corrupt
- cmp byte [esi], CPU_SWITCH_STATE_LOADED
- jz OtherLoaded
- pause
- jmp WaitForOtherLoaded
-
-OtherLoaded:
- ; since the other CPU already get the data it want, leave this procedure
- pop eax
- mov cr0, eax
- pop eax
- mov cr4, eax
- popfd
-
- popad
- ret
-
-global ASM_PFX(AsmInitializeGdt)
-ASM_PFX(AsmInitializeGdt):
- push ebp
- mov ebp, esp
- pushad
- mov edi, [ebp + 8] ; Load GDT register
-
- lgdt [edi] ; and update the GDTR
-
- push PROTECT_MODE_CS
- mov eax, ASM_PFX(SetCodeSelectorFarJump)
- push eax
- retf
-ASM_PFX(SetCodeSelectorFarJump):
- mov ax, PROTECT_MODE_DS ; Update the Base for the new selectors, too
- mov ds, ax
- mov es, ax
- mov fs, ax
- mov gs, ax
- mov ss, ax
-
- popad
- pop ebp
- 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
+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
+BITS 16\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 si, GdtrLocation\r
+o32 lgdt [cs:si]\r
+\r
+ mov si, IdtrLocation\r
+o32 lidt [cs:si]\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 esi, ebx\r
+ mov edi, esi\r
+ add edi, LockLocation\r
+ mov eax, NotVacantFlag\r
+\r
+TestLock:\r
+ xchg [edi], eax\r
+ cmp eax, 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 eax, [edi]\r
+ mov edi, esi\r
+ add edi, StackStartAddressLocation\r
+ add eax, [edi]\r
+ mov esp, eax\r
+ mov [edi], eax\r
+\r
+Releaselock:\r
+ mov eax, VacantFlag\r
+ mov edi, esi\r
+ add edi, LockLocation\r
+ xchg [edi], eax\r
+\r
+CProcedureInvoke:\r
+ push ebp ; push BIST data at top of AP stack\r
+ xor ebp, ebp ; clear ebp for call stack trace\r
+ push ebp\r
+ mov ebp, esp\r
+\r
+ mov eax, ASM_PFX(InitializeFloatingPointUnits)\r
+ call eax ; Call assembly function to initialize FPU per UEFI spec\r
+\r
+ push ebx ; Push NumApsExecuting\r
+ mov eax, esi\r
+ add eax, LockLocation\r
+ push eax ; push address of exchange info data buffer\r
+\r
+ mov edi, esi\r
+ add edi, ApProcedureLocation\r
+ mov eax, [edi]\r
+\r
+ call eax ; invoke C function\r
+\r
+ jmp $ ; never reach here\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
+ pushad\r
+ mov ebp,esp\r
+\r
+ mov ebx, [ebp + 24h]\r
+ mov dword [ebx], RendezvousFunnelProcStart\r
+ mov dword [ebx + 4h], Flat32Start - RendezvousFunnelProcStart\r
+ mov dword [ebx + 8h], 0\r
+ mov dword [ebx + 0ch], RendezvousFunnelProcEnd - RendezvousFunnelProcStart\r
+\r
+ popad\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
+ pushad\r
+ mov ebp,esp\r
+\r
+ ; esi contains MyInfo pointer\r
+ mov esi, [ebp + 24h]\r
+\r
+ ; edi contains OthersInfo pointer\r
+ mov edi, [ebp + 28h]\r
+\r
+ ;Store EFLAGS, GDTR and IDTR register to stack\r
+ pushfd\r
+ mov eax, cr4\r
+ push eax ; push cr4 firstly\r
+ mov eax, cr0\r
+ push eax\r
+\r
+ sgdt [esi + 8]\r
+ sidt [esi + 14]\r
+\r
+ ; Store the its StackPointer\r
+ mov [esi + 4],esp\r
+\r
+ ; update its switch state to STORED\r
+ mov byte [esi], CPU_SWITCH_STATE_STORED\r
+\r
+WaitForOtherStored:\r
+ ; wait until the other CPU finish storing its state\r
+ cmp byte [edi], 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 [edi + 8]\r
+\r
+ ; load IDTR value\r
+ lidt [edi + 14]\r
+\r
+ ; load its future StackPointer\r
+ mov esp, [edi + 4]\r
+\r
+ ; update the other CPU's switch state to LOADED\r
+ mov byte [edi], 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 [esi], 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
+ pop eax\r
+ mov cr0, eax\r
+ pop eax\r
+ mov cr4, eax\r
+ popfd\r
+\r
+ popad\r
+ ret\r
+\r
+global ASM_PFX(AsmInitializeGdt)\r
+ASM_PFX(AsmInitializeGdt):\r
+ push ebp\r
+ mov ebp, esp\r
+ pushad\r
+ mov edi, [ebp + 8] ; Load GDT register\r
+\r
+ lgdt [edi] ; and update the GDTR\r
+\r
+ push PROTECT_MODE_CS\r
+ mov eax, ASM_PFX(SetCodeSelectorFarJump)\r
+ push eax\r
+ retf\r
+ASM_PFX(SetCodeSelectorFarJump):\r
+ mov ax, PROTECT_MODE_DS ; Update the Base for the new selectors, too\r
+ mov ds, ax\r
+ mov es, ax\r
+ mov fs, ax\r
+ mov gs, ax\r
+ mov ss, ax\r
+\r
+ popad\r
+ pop ebp\r
+ ret\r