Home | History | Annotate | Download | only in X64
      1 #------------------------------------------------------------------------------
      2 # X64 assembly file for AP startup vector.
      3 #
      4 # Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
      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 
     16 .set                   VacantFlag,       0x0
     17 .set                   NotVacantFlag,    0xff
     18 
     19 .set                   LockLocation,              RendezvousFunnelProcEnd - RendezvousFunnelProcStart
     20 .set                   StackStartAddressLocation, RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x08
     21 .set                   StackSizeLocation,         RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x10
     22 .set                   CProcedureLocation,        RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x18
     23 .set                   GdtrLocation,              RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x20
     24 .set                   IdtrLocation,              RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x2A
     25 .set                   BufferStartLocation,       RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x34
     26 .set                   Cr3OffsetLocation,         RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x38
     27 .set                   ProcessorNumberLocation,   RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x38
     28 
     29 #-------------------------------------------------------------------------------------
     30 
     31 #-------------------------------------------------------------------------------------
     32 #RendezvousFunnelProc  procedure follows. All APs execute their procedure. This
     33 #procedure serializes all the AP processors through an Init sequence. It must be
     34 #noted that APs arrive here very raw...ie: real mode, no stack.
     35 #ALSO THIS PROCEDURE IS EXECUTED BY APs ONLY ON 16 BIT MODE. HENCE THIS PROC
     36 #IS IN MACHINE CODE.
     37 #-------------------------------------------------------------------------------------
     38 #RendezvousFunnelProc (&WakeUpBuffer,MemAddress);
     39 
     40 .text
     41 
     42 ASM_GLOBAL ASM_PFX(RendezvousFunnelProc)
     43 ASM_PFX(RendezvousFunnelProc):
     44 RendezvousFunnelProcStart:
     45 
     46 # At this point CS = 0x(vv00) and ip= 0x0.
     47 
     48         .byte 0x8c,0xc8               # mov        ax,  cs
     49         .byte 0x8e,0xd8               # mov        ds,  ax
     50         .byte 0x8e,0xc0               # mov        es,  ax
     51         .byte 0x8e,0xd0               # mov        ss,  ax
     52         .byte 0x33,0xc0               # xor        ax,  ax
     53         .byte 0x8e,0xe0               # mov        fs,  ax
     54         .byte 0x8e,0xe8               # mov        gs,  ax
     55 
     56 # Switch to flat mode.
     57 
     58         .byte 0xBE
     59         .word BufferStartLocation
     60         .byte 0x66,0x8B,0x14          # mov        edx,dword ptr [si]          ; EDX is keeping the start address of wakeup buffer
     61 
     62         .byte 0xBE
     63         .word Cr3OffsetLocation
     64         .byte 0x66,0x8B,0xC           # mov        ecx,dword ptr [si]          ; ECX is keeping the value of CR3
     65 
     66         .byte 0xBE
     67         .word GdtrLocation
     68         .byte 0x66                    # db         66h
     69         .byte 0x2E,0xF,0x1,0x14       # lgdt       fword ptr cs:[si]
     70 
     71         .byte 0xBE
     72         .word IdtrLocation
     73         .byte 0x66                    # db         66h
     74         .byte 0x2E,0xF,0x1,0x1C       # lidt       fword ptr cs:[si]
     75 
     76         .byte 0x33,0xC0               # xor        ax,  ax
     77         .byte 0x8E,0xD8               # mov        ds,  ax
     78 
     79         .byte 0xF,0x20,0xC0           # mov        eax, cr0                    ; Get control register 0
     80         .byte 0x66,0x83,0xC8,0x1      # or         eax, 000000001h             ; Set PE bit (bit #0)
     81         .byte 0xF,0x22,0xC0           # mov        cr0, eax
     82 
     83 FLAT32_JUMP:
     84 
     85         .byte 0x66,0x67,0xEA          # far jump
     86         .long 0x0                     # 32-bit offset
     87         .word 0x20                    # 16-bit selector
     88 
     89 ProtectedModeStart:
     90 
     91         .byte 0x66,0xB8,0x18,0x0      # mov        ax,  18h
     92         .byte 0x66,0x8E,0xD8          # mov        ds,  ax
     93         .byte 0x66,0x8E,0xC0          # mov        es,  ax
     94         .byte 0x66,0x8E,0xE0          # mov        fs,  ax
     95         .byte 0x66,0x8E,0xE8          # mov        gs,  ax
     96         .byte 0x66,0x8E,0xD0          # mov        ss,  ax                     ; Flat mode setup.
     97 
     98         .byte 0xF,0x20,0xE0           # mov        eax, cr4
     99         .byte 0xF,0xBA,0xE8,0x5       # bts        eax, 5
    100         .byte 0xF,0x22,0xE0           # mov        cr4, eax
    101 
    102         .byte 0xF,0x22,0xD9           # mov        cr3, ecx
    103 
    104         .byte 0x8B,0xF2               # mov        esi, edx                    ; Save wakeup buffer address
    105 
    106         .byte 0xB9
    107         .long 0xC0000080              # mov        ecx, 0c0000080h             ; EFER MSR number.
    108         .byte 0xF,0x32                # rdmsr                                  ; Read EFER.
    109         .byte 0xF,0xBA,0xE8,0x8       # bts        eax, 8                      ; Set LME=1.
    110         .byte 0xF,0x30                # wrmsr                                  ; Write EFER.
    111 
    112         .byte 0xF,0x20,0xC0           # mov        eax, cr0                    ; Read CR0.
    113         .byte 0xF,0xBA,0xE8,0x1F      # bts        eax, 31                     ; Set PG=1.
    114         .byte 0xF,0x22,0xC0           # mov        cr0, eax                    ; Write CR0.
    115 
    116 LONG_JUMP:
    117 
    118         .byte 0x67,0xEA               # far jump
    119         .long 0x0                     # 32-bit offset
    120         .word 0x38                    # 16-bit selector
    121 
    122 LongModeStart:
    123 
    124         movw        $0x30,%ax
    125         .byte       0x66
    126         movw        %ax,%ds
    127         .byte       0x66
    128         movw        %ax,%es
    129         .byte       0x66
    130         movw        %ax,%ss
    131 
    132         #
    133         # ProgramStack
    134         #
    135         movl        $0x1b, %ecx
    136         rdmsr
    137 
    138         btl         $10, %eax         # Check for x2apic mode
    139         jnc         LegacyApicMode
    140         movl        $0x802, %ecx      # Read APIC_ID
    141         rdmsr
    142         movl        %eax, %ebx        # ebx == apicid
    143         jmp         GetCpuNumber
    144 
    145 LegacyApicMode:
    146         andl        $0xfffff000, %eax
    147         addl        $0x20, %eax
    148         movl        (%eax), %ebx
    149         shrl        $24, %ebx         # ebx == apicid
    150 
    151 GetCpuNumber:
    152         xorq        %rcx, %rcx
    153         movl        %esi,%edi
    154         addl        $ProcessorNumberLocation, %edi
    155         movl        (%edi, %ebx, 4), %ecx
    156 
    157         movl        %esi,%edi
    158         addl        $StackSizeLocation, %edi
    159         movq        (%edi), %rax
    160         incq        %rcx
    161         mulq        %rcx
    162 
    163         movl        %esi,%edi
    164         addl        $StackStartAddressLocation, %edi
    165         movq        (%edi), %rbx
    166         addq        %rbx, %rax
    167 
    168         movq        %rax, %rsp
    169 
    170         #
    171         # Call C Function
    172         #
    173         movl        %esi,%edi
    174         addl        $CProcedureLocation, %edi
    175         movq        (%edi), %rax
    176 
    177         testq       %rax, %rax
    178         jz          GoToSleep
    179 
    180         subq        $0x20, %rsp
    181         call        *%rax
    182         addq        $0x20, %rsp
    183 
    184 GoToSleep:
    185 
    186         cli
    187         hlt
    188         jmp         .-2
    189 
    190 RendezvousFunnelProcEnd:
    191 
    192 
    193 #-------------------------------------------------------------------------------------
    194 #  AsmGetAddressMap (&AddressMap);
    195 #-------------------------------------------------------------------------------------
    196 # comments here for definition of address map
    197 ASM_GLOBAL ASM_PFX(AsmGetAddressMap)
    198 ASM_PFX(AsmGetAddressMap):
    199 #ifdef __APPLE__
    200         int          $3
    201 #else
    202         movq         $RendezvousFunnelProcStart, %rax
    203         movq         %rax, (%rcx)
    204         movq         $(ProtectedModeStart - RendezvousFunnelProcStart), 0x08(%rcx)
    205         movq         $(FLAT32_JUMP - RendezvousFunnelProcStart), 0x10(%rcx)
    206         movq         $(LongModeStart - RendezvousFunnelProcStart), 0x18(%rcx)
    207         movq         $(LONG_JUMP - RendezvousFunnelProcStart), 0x20(%rcx)
    208         movq         $(RendezvousFunnelProcEnd - RendezvousFunnelProcStart), 0x28(%rcx)
    209 #endif
    210         ret
    211