MdePkg/BaseLib: add PatchInstructionX86()
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / X64 / SmmInit.asm
1 ;------------------------------------------------------------------------------ ;
2 ; Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
3 ; This program and the accompanying materials
4 ; are licensed and made available under the terms and conditions of the BSD License
5 ; which accompanies this distribution. The full text of the license may be found at
6 ; http://opensource.org/licenses/bsd-license.php.
7 ;
8 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
10 ;
11 ; Module Name:
12 ;
13 ; SmmInit.Asm
14 ;
15 ; Abstract:
16 ;
17 ; Functions for relocating SMBASE's for all processors
18 ;
19 ;-------------------------------------------------------------------------------
20
21 EXTERNDEF SmmInitHandler:PROC
22 EXTERNDEF gSmmCr0:DWORD
23 EXTERNDEF gSmmCr3:DWORD
24 EXTERNDEF gSmmCr4:DWORD
25 EXTERNDEF gSmmJmpAddr:QWORD
26 EXTERNDEF gcSmmInitTemplate:BYTE
27 EXTERNDEF gcSmmInitSize:WORD
28 EXTERNDEF mRebasedFlag:PTR BYTE
29 EXTERNDEF mSmmRelocationOriginalAddress:QWORD
30 EXTERNDEF mRebasedFlagAddr32:DWORD
31 EXTERNDEF mSmmRelocationOriginalAddressPtr32:DWORD
32 EXTERNDEF gSmmInitStack:QWORD
33 EXTERNDEF gcSmiInitGdtr:FWORD
34
35 .code
36
37 gcSmiInitGdtr LABEL FWORD
38 DW 0
39 DQ 0
40
41 SmmStartup PROC
42 DB 66h, 0b8h ; mov eax, imm32
43 gSmmCr3 DD ?
44 mov cr3, rax
45 DB 66h, 2eh
46 lgdt fword ptr [ebp + (offset gcSmiInitGdtr - SmmStartup)]
47 DB 66h, 0b8h ; mov eax, imm32
48 gSmmCr4 DD ?
49 or ah, 2 ; enable XMM registers access
50 mov cr4, rax
51 DB 66h
52 mov ecx, 0c0000080h ; IA32_EFER MSR
53 rdmsr
54 or ah, 1 ; set LME bit
55 wrmsr
56 DB 66h, 0b8h ; mov eax, imm32
57 gSmmCr0 DD ?
58 mov cr0, rax ; enable protected mode & paging
59 DB 66h, 0eah ; far jmp to long mode
60 gSmmJmpAddr DQ @LongMode
61 @LongMode: ; long-mode starts here
62 DB 48h, 0bch ; mov rsp, imm64
63 gSmmInitStack DQ ?
64 and sp, 0fff0h ; make sure RSP is 16-byte aligned
65 ;
66 ; Accoring to X64 calling convention, XMM0~5 are volatile, we need to save
67 ; them before calling C-function.
68 ;
69 sub rsp, 60h
70 movdqa [rsp], xmm0
71 movdqa [rsp + 10h], xmm1
72 movdqa [rsp + 20h], xmm2
73 movdqa [rsp + 30h], xmm3
74 movdqa [rsp + 40h], xmm4
75 movdqa [rsp + 50h], xmm5
76
77 add rsp, -20h
78 call SmmInitHandler
79 add rsp, 20h
80
81 ;
82 ; Restore XMM0~5 after calling C-function.
83 ;
84 movdqa xmm0, [rsp]
85 movdqa xmm1, [rsp + 10h]
86 movdqa xmm2, [rsp + 20h]
87 movdqa xmm3, [rsp + 30h]
88 movdqa xmm4, [rsp + 40h]
89 movdqa xmm5, [rsp + 50h]
90
91 rsm
92 SmmStartup ENDP
93
94 gcSmmInitTemplate LABEL BYTE
95
96 _SmmInitTemplate PROC
97 DB 66h, 2eh, 8bh, 2eh ; mov ebp, cs:[@F]
98 DW @L1 - _SmmInitTemplate + 8000h
99 DB 66h, 81h, 0edh, 00h, 00h, 03h, 00 ; sub ebp, 30000h
100 jmp bp ; jmp ebp actually
101 @L1:
102 DQ SmmStartup
103 _SmmInitTemplate ENDP
104
105 gcSmmInitSize DW $ - gcSmmInitTemplate
106
107 SmmRelocationSemaphoreComplete PROC
108 push rax
109 mov rax, mRebasedFlag
110 mov byte ptr [rax], 1
111 pop rax
112 jmp [mSmmRelocationOriginalAddress]
113 SmmRelocationSemaphoreComplete ENDP
114
115 ;
116 ; Semaphore code running in 32-bit mode
117 ;
118 SmmRelocationSemaphoreComplete32 PROC
119 ;
120 ; mov byte ptr [], 1
121 ;
122 db 0c6h, 05h
123 mRebasedFlagAddr32 dd 0
124 db 1
125 ;
126 ; jmp dword ptr []
127 ;
128 db 0ffh, 25h
129 mSmmRelocationOriginalAddressPtr32 dd 0
130 SmmRelocationSemaphoreComplete32 ENDP
131
132 END