MdePkg/BaseLib: add PatchInstructionX86()
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / X64 / MpFuncs.S
1 #------------------------------------------------------------------------------
2 #
3 # Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
4 # This program and the accompanying materials
5 # are licensed and made available under the terms and conditions of the BSD License
6 # which accompanies this distribution. The full text of the license may be found at
7 # http://opensource.org/licenses/bsd-license.php.
8 #
9 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 #
12 # Module Name:
13 #
14 # MpFuncs.S
15 #
16 # Abstract:
17 #
18 # This is the assembly code for Multi-processor S3 support
19 #
20 #------------------------------------------------------------------------------
21
22 .equ VacantFlag, 0x0
23 .equ NotVacantFlag, 0xff
24
25 .equ LockLocation, RendezvousFunnelProcEnd - RendezvousFunnelProcStart
26 .equ StackStartAddressLocation, RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x08
27 .equ StackSizeLocation, RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x10
28 .equ CProcedureLocation, RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x18
29 .equ GdtrLocation, RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x20
30 .equ IdtrLocation, RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x2A
31 .equ BufferStartLocation, RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x34
32 .equ Cr3OffsetLocation, RendezvousFunnelProcEnd - RendezvousFunnelProcStart + 0x38
33
34 #-------------------------------------------------------------------------------------
35 #RendezvousFunnelProc procedure follows. All APs execute their procedure. This
36 #procedure serializes all the AP processors through an Init sequence. It must be
37 #noted that APs arrive here very raw...ie: real mode, no stack.
38 #ALSO THIS PROCEDURE IS EXECUTED BY APs ONLY ON 16 BIT MODE. HENCE THIS PROC
39 #IS IN MACHINE CODE.
40 #-------------------------------------------------------------------------------------
41 #RendezvousFunnelProc (&WakeUpBuffer,MemAddress);
42
43 .code:
44
45 ASM_GLOBAL ASM_PFX(RendezvousFunnelProc)
46 ASM_PFX(RendezvousFunnelProc):
47 RendezvousFunnelProcStart:
48
49 # At this point CS = 0x(vv00) and ip= 0x0.
50
51 .byte 0x8c,0xc8 # mov ax, cs
52 .byte 0x8e,0xd8 # mov ds, ax
53 .byte 0x8e,0xc0 # mov es, ax
54 .byte 0x8e,0xd0 # mov ss, ax
55 .byte 0x33,0xc0 # xor ax, ax
56 .byte 0x8e,0xe0 # mov fs, ax
57 .byte 0x8e,0xe8 # mov gs, ax
58
59 flat32Start:
60
61 .byte 0xBE
62 .word BufferStartLocation
63 .byte 0x66,0x8B,0x14 # mov edx,dword ptr [si] ; EDX is keeping the start address of wakeup buffer
64
65 .byte 0xBE
66 .word Cr3OffsetLocation
67 .byte 0x66,0x8B,0xC # mov ecx,dword ptr [si] ; ECX is keeping the value of CR3
68
69 .byte 0xBE
70 .word GdtrLocation
71 .byte 0x66 # db 66h
72 .byte 0x2E,0xF,0x1,0x14 # lgdt fword ptr cs:[si]
73
74 .byte 0xBE
75 .word IdtrLocation
76 .byte 0x66 # db 66h
77 .byte 0x2E,0xF,0x1,0x1C # lidt fword ptr cs:[si]
78
79 .byte 0x33,0xC0 # xor ax, ax
80 .byte 0x8E,0xD8 # mov ds, ax
81
82 .byte 0xF,0x20,0xC0 # mov eax, cr0 ; Get control register 0
83 .byte 0x66,0x83,0xC8,0x1 # or eax, 000000001h ; Set PE bit (bit #0)
84 .byte 0xF,0x22,0xC0 # mov cr0, eax
85
86 FLAT32_JUMP:
87
88 .byte 0x66,0x67,0xEA # far jump
89 .long 0x0 # 32-bit offset
90 .word 0x20 # 16-bit selector
91
92 PMODE_ENTRY: # protected mode entry point
93
94 .byte 0x66,0xB8,0x18,0x0 # mov ax, 18h
95 .byte 0x66,0x8E,0xD8 # mov ds, ax
96 .byte 0x66,0x8E,0xC0 # mov es, ax
97 .byte 0x66,0x8E,0xE0 # mov fs, ax
98 .byte 0x66,0x8E,0xE8 # mov gs, ax
99 .byte 0x66,0x8E,0xD0 # mov ss, ax ; Flat mode setup.
100
101 .byte 0xF,0x20,0xE0 # mov eax, cr4
102 .byte 0xF,0xBA,0xE8,0x5 # bts eax, 5
103 .byte 0xF,0x22,0xE0 # mov cr4, eax
104
105 .byte 0xF,0x22,0xD9 # mov cr3, ecx
106
107 .byte 0x8B,0xF2 # mov esi, edx ; Save wakeup buffer address
108
109 .byte 0xB9
110 .long 0xC0000080 # mov ecx, 0c0000080h ; EFER MSR number.
111 .byte 0xF,0x32 # rdmsr ; Read EFER.
112 .byte 0xF,0xBA,0xE8,0x8 # bts eax, 8 ; Set LME=1.
113 .byte 0xF,0x30 # wrmsr ; Write EFER.
114
115 .byte 0xF,0x20,0xC0 # mov eax, cr0 ; Read CR0.
116 .byte 0xF,0xBA,0xE8,0x1F # bts eax, 31 ; Set PG=1.
117 .byte 0xF,0x22,0xC0 # mov cr0, eax ; Write CR0.
118
119 LONG_JUMP:
120
121 .byte 0x67,0xEA # far jump
122 .long 0x0 # 32-bit offset
123 .word 0x38 # 16-bit selector
124
125 LongModeStart:
126
127 movw $0x30,%ax
128 .byte 0x66
129 movw %ax,%ds
130 .byte 0x66
131 movw %ax,%es
132 .byte 0x66
133 movw %ax,%ss
134
135 movl %esi,%edi
136 addl $LockLocation, %edi
137 movb $NotVacantFlag, %al
138 TestLock:
139 xchgb (%edi), %al
140 cmpb $NotVacantFlag, %al
141 jz TestLock
142
143 ProgramStack:
144
145 movl %esi,%edi
146 addl $StackSizeLocation, %edi
147 movq (%edi), %rax
148 movl %esi,%edi
149 addl $StackStartAddressLocation, %edi
150 addq (%edi), %rax
151 movq %rax, %rsp
152 movq %rax, (%edi)
153
154 Releaselock:
155
156 movb $VacantFlag, %al
157 movl %esi,%edi
158 addl $LockLocation, %edi
159 xchgb (%edi), %al
160
161 #
162 # Call assembly function to initialize FPU.
163 #
164 movabsq $ASM_PFX(InitializeFloatingPointUnits), %rax
165 subq $0x20, %rsp
166 call *%rax
167 addq $0x20, %rsp
168 #
169 # Call C Function
170 #
171 movl %esi,%edi
172 addl $CProcedureLocation, %edi
173 movq (%edi), %rax
174
175 testq %rax, %rax
176 jz GoToSleep
177
178 subq $0x20, %rsp
179 call *%rax
180 addq $0x20, %rsp
181
182 GoToSleep:
183 cli
184 hlt
185 jmp .-2
186
187 RendezvousFunnelProcEnd:
188
189
190 #-------------------------------------------------------------------------------------
191 # AsmGetAddressMap (&AddressMap);
192 #-------------------------------------------------------------------------------------
193 # comments here for definition of address map
194 ASM_GLOBAL ASM_PFX(AsmGetAddressMap)
195 ASM_PFX(AsmGetAddressMap):
196 movabsq $RendezvousFunnelProcStart, %rax
197 movq %rax, (%rcx)
198 movq $(PMODE_ENTRY - RendezvousFunnelProcStart), 0x08(%rcx)
199 movq $(FLAT32_JUMP - RendezvousFunnelProcStart), 0x10(%rcx)
200 movq $(RendezvousFunnelProcEnd - RendezvousFunnelProcStart), 0x18(%rcx)
201 movq $(LongModeStart - RendezvousFunnelProcStart), 0x20(%rcx)
202 movq $(LONG_JUMP - RendezvousFunnelProcStart), 0x28(%rcx)
203 ret
204