]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm
UefiCpuPkg/MpInitLib: Fix X64 XCODE5/NASM compatibility issues
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / X64 / MpFuncs.nasm
index f19c75f91397db3b2653708b0009dc6807227f80..0b14a534668bb03557164ae90ec8b9e20d6bb758 100644 (file)
@@ -1,5 +1,5 @@
 ;------------------------------------------------------------------------------ ;\r
-; Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
+; Copyright (c) 2015 - 2017, 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
@@ -57,7 +57,7 @@ BITS 16
     mov        di,  CodeSegmentLocation\r
     mov        edx, [di]\r
     mov        di,  ax\r
-    sub        di,  02h  \r
+    sub        di,  02h\r
     mov        [di],dx                         ; Patch long mode CS\r
     sub        di,  04h\r
     add        eax, ebx\r
@@ -69,6 +69,19 @@ o32 lgdt       [cs:si]
     mov        si, IdtrLocation\r
 o32 lidt       [cs:si]\r
 \r
+    mov        si, EnableExecuteDisableLocation\r
+    cmp        byte [si], 0\r
+    jz         SkipEnableExecuteDisableBit\r
+\r
+    ;\r
+    ; Enable execute disable bit\r
+    ;\r
+    mov        ecx, 0c0000080h             ; EFER MSR number\r
+    rdmsr                                  ; Read EFER\r
+    bts        eax, 11                     ; Enable Execute Disable Bit\r
+    wrmsr                                  ; Write EFER\r
+\r
+SkipEnableExecuteDisableBit:\r
 \r
     mov        di,  DataSegmentLocation\r
     mov        edi, [di]                   ; Save long mode DS in edi\r
@@ -107,6 +120,11 @@ LongModeStart:
     mov        ss,  ax\r
 \r
     mov        esi, ebx\r
+    lea        edi, [esi + InitFlagLocation]\r
+    cmp        qword [edi], 1       ; ApInitConfig\r
+    jnz        GetApicId\r
+\r
+    ; AP init\r
     mov        edi, esi\r
     add        edi, LockLocation\r
     mov        rax, NotVacantFlag\r
@@ -116,34 +134,74 @@ TestLock:
     cmp        rax, NotVacantFlag\r
     jz         TestLock\r
 \r
-    mov        edi, esi\r
-    add        edi, NumApsExecutingLoction\r
-    inc        dword [edi]\r
-    mov        ebx, [edi]\r
+    lea        ecx, [esi + NumApsExecutingLocation]\r
+    inc        dword [ecx]\r
+    mov        ebx, [ecx]\r
 \r
-ProgramStack:\r
+Releaselock:\r
+    mov        rax, VacantFlag\r
+    xchg       qword [edi], rax\r
+    ; program stack\r
     mov        edi, esi\r
     add        edi, StackSizeLocation\r
-    mov        rax, qword [edi]\r
+    mov        eax, dword [edi]\r
+    mov        ecx, ebx\r
+    inc        ecx\r
+    mul        ecx                               ; EAX = StackSize * (CpuNumber + 1)\r
     mov        edi, esi\r
     add        edi, StackStartAddressLocation\r
     add        rax, qword [edi]\r
     mov        rsp, rax\r
-    mov        qword [edi], rax\r
+    jmp        CProcedureInvoke\r
+\r
+GetApicId:\r
+    mov        eax, 0\r
+    cpuid\r
+    cmp        eax, 0bh\r
+    jb         NoX2Apic             ; CPUID level below CPUID_EXTENDED_TOPOLOGY\r
+\r
+    mov        eax, 0bh\r
+    xor        ecx, ecx\r
+    cpuid\r
+    test       ebx, 0ffffh\r
+    jz         NoX2Apic             ; CPUID.0BH:EBX[15:0] is zero\r
+\r
+    ; Processor is x2APIC capable; 32-bit x2APIC ID is already in EDX\r
+    jmp        GetProcessorNumber\r
+\r
+NoX2Apic:\r
+    ; Processor is not x2APIC capable, so get 8-bit APIC ID\r
+    mov        eax, 1\r
+    cpuid\r
+    shr        ebx, 24\r
+    mov        edx, ebx\r
+\r
+GetProcessorNumber:\r
+    ;\r
+    ; Get processor number for this AP\r
+    ; Note that BSP may become an AP due to SwitchBsp()\r
+    ;\r
+    xor         ebx, ebx\r
+    lea         eax, [esi + CpuInfoLocation]\r
+    mov         edi, [eax]\r
+\r
+GetNextProcNumber:\r
+    cmp         dword [edi], edx                      ; APIC ID match?\r
+    jz          ProgramStack\r
+    add         edi, 20\r
+    inc         ebx\r
+    jmp         GetNextProcNumber    \r
 \r
-Releaselock:\r
-    mov        rax, VacantFlag\r
-    mov        edi, esi\r
-    add        edi, LockLocation\r
-    xchg       qword [edi], rax\r
+ProgramStack:\r
+    mov         rsp, qword [edi + 12]\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               ; 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
+    mov        rax, qword [esi + InitializeFloatingPointUnitsAddress]\r
     sub        rsp, 20h\r
     call       rax               ; Call assembly function to initialize FPU per UEFI spec\r
     add        rsp, 20h\r
@@ -157,26 +215,85 @@ CProcedureInvoke:
     mov        rax, qword [edi]\r
 \r
     sub        rsp, 20h\r
-    call       rax               ; invoke C function\r
+    call       rax               ; Invoke C function\r
     add        rsp, 20h\r
-    jmp        $\r
+    jmp        $                 ; Should never reach here\r
 \r
 RendezvousFunnelProcEnd:\r
 \r
+;-------------------------------------------------------------------------------------\r
+;  AsmRelocateApLoop (MwaitSupport, ApTargetCState, PmCodeSegment, TopOfApStack, CountTofinish);\r
+;-------------------------------------------------------------------------------------\r
+global ASM_PFX(AsmRelocateApLoop)\r
+ASM_PFX(AsmRelocateApLoop):\r
+AsmRelocateApLoopStart:\r
+    mov        rax, [rsp + 40]   ; CountTofinish\r
+    lock dec   dword [rax]       ; (*CountTofinish)--\r
+    mov        rsp, r9\r
+    push       rcx\r
+    push       rdx\r
+\r
+    lea        rsi, [PmEntry]    ; rsi <- The start address of transition code\r
+\r
+    push       r8\r
+    push       rsi\r
+    DB         0x48\r
+    retf\r
+BITS 32\r
+PmEntry:\r
+    mov        eax, cr0\r
+    btr        eax, 31           ; Clear CR0.PG\r
+    mov        cr0, eax          ; Disable paging and caches\r
+\r
+    mov        ebx, edx          ; Save EntryPoint to rbx, for rdmsr will overwrite rdx\r
+    mov        ecx, 0xc0000080\r
+    rdmsr\r
+    and        ah, ~ 1           ; Clear LME\r
+    wrmsr\r
+    mov        eax, cr4\r
+    and        al, ~ (1 << 5)    ; Clear PAE\r
+    mov        cr4, eax\r
+\r
+    pop        edx\r
+    add        esp, 4\r
+    pop        ecx,\r
+    add        esp, 4\r
+    cmp        cl, 1              ; Check mwait-monitor support\r
+    jnz        HltLoop\r
+    mov        ebx, edx           ; Save C-State to ebx\r
+MwaitLoop:\r
+    mov        eax, esp           ; Set Monitor Address\r
+    xor        ecx, ecx           ; ecx = 0\r
+    xor        edx, edx           ; edx = 0\r
+    monitor\r
+    mov        eax, ebx           ; Mwait Cx, Target C-State per eax[7:4]\r
+    shl        eax, 4\r
+    mwait\r
+    jmp        MwaitLoop\r
+HltLoop:\r
+    cli\r
+    hlt\r
+    jmp        HltLoop\r
+BITS 64\r
+AsmRelocateApLoopEnd:\r
+\r
 ;-------------------------------------------------------------------------------------\r
 ;  AsmGetAddressMap (&AddressMap);\r
 ;-------------------------------------------------------------------------------------\r
 global ASM_PFX(AsmGetAddressMap)\r
 ASM_PFX(AsmGetAddressMap):\r
-    mov        rax, ASM_PFX(RendezvousFunnelProc)\r
+    lea        rax, [ASM_PFX(RendezvousFunnelProc)]\r
     mov        qword [rcx], rax\r
     mov        qword [rcx +  8h], LongModeStart - RendezvousFunnelProcStart\r
     mov        qword [rcx + 10h], RendezvousFunnelProcEnd - RendezvousFunnelProcStart\r
+    lea        rax, [ASM_PFX(AsmRelocateApLoop)]\r
+    mov        qword [rcx + 18h], rax\r
+    mov        qword [rcx + 20h], AsmRelocateApLoopEnd - AsmRelocateApLoopStart\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
+;about to become an AP. It switches itstack with the current AP.\r
 ;AsmExchangeRole (IN   CPU_EXCHANGE_INFO    *MyInfo, IN   CPU_EXCHANGE_INFO    *OthersInfo);\r
 ;-------------------------------------------------------------------------------------\r
 global ASM_PFX(AsmExchangeRole)\r