1 | ; $Id: cidet-appA.asm 53548 2014-12-16 17:55:40Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; CPU Instruction Decoding & Execution Tests - Ring-3 Driver Application, Assembly Code.
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2009-2014 Oracle Corporation
|
---|
8 | ;
|
---|
9 | ; This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | ; available from http://www.virtualbox.org. This file is free software;
|
---|
11 | ; you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | ; General Public License (GPL) as published by the Free Software
|
---|
13 | ; Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | ; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | ; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | ;
|
---|
17 | ; The contents of this file may alternatively be used under the terms
|
---|
18 | ; of the Common Development and Distribution License Version 1.0
|
---|
19 | ; (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | ; VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | ; CDDL are applicable instead of those of the GPL.
|
---|
22 | ;
|
---|
23 | ; You may elect to license modified versions of this file under the
|
---|
24 | ; terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | ;
|
---|
26 |
|
---|
27 |
|
---|
28 | ;*******************************************************************************
|
---|
29 | ;* Header Files *
|
---|
30 | ;*******************************************************************************
|
---|
31 | %include "iprt/asmdefs.mac"
|
---|
32 | %include "iprt/x86.mac"
|
---|
33 | %include "cidet.mac"
|
---|
34 |
|
---|
35 |
|
---|
36 | ;*******************************************************************************
|
---|
37 | ;* Global Variables *
|
---|
38 | ;*******************************************************************************
|
---|
39 | %ifdef RT_ARCH_X86
|
---|
40 | ;; Used by CidetAppSaveAndRestoreCtx when we have a tricky target stack.
|
---|
41 | g_uTargetEip dd 0
|
---|
42 | g_uTargetCs dw 0
|
---|
43 | %endif
|
---|
44 |
|
---|
45 |
|
---|
46 | ;;
|
---|
47 | ; ASSUMES that it's called and the EIP/RIP is found on the stack.
|
---|
48 | ;
|
---|
49 | ; @param pSaveCtx ds:xCX The context to save; DS, xDX and xCX have
|
---|
50 | ; already been saved by the caller.
|
---|
51 | ; @param pRestoreCtx ds:xDX The context to restore.
|
---|
52 | ;
|
---|
53 | BEGINPROC CidetAppSaveAndRestoreCtx
|
---|
54 | ;
|
---|
55 | ; Save the stack pointer and program counter first so we can later
|
---|
56 | ; bypass this step if we need to.
|
---|
57 | ;
|
---|
58 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8], xAX ; need scratch register.
|
---|
59 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8], xSP
|
---|
60 | mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_SS * 2], ss
|
---|
61 | mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_CS * 2], cs
|
---|
62 | mov xAX, [xSP]
|
---|
63 | mov [xCX + CIDETCPUCTX.rip], xAX
|
---|
64 | jmp CidetAppSaveAndRestoreCtx_1
|
---|
65 |
|
---|
66 | GLOBALNAME CidetAppSaveAndRestoreCtx_NoSsSpCsIp
|
---|
67 | mov [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8], xAX
|
---|
68 | CidetAppSaveAndRestoreCtx_1:
|
---|
69 |
|
---|
70 | ; Flags.
|
---|
71 | %ifdef RT_ARCH_AMD64
|
---|
72 | pushfq
|
---|
73 | %else
|
---|
74 | pushfd
|
---|
75 | %endif
|
---|
76 | pop xAX
|
---|
77 | mov [xCX + CIDETCPUCTX.rfl], xAX
|
---|
78 |
|
---|
79 | ; Segment registers.
|
---|
80 | mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_ES * 2], es
|
---|
81 | mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_FS * 2], fs
|
---|
82 | mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_GS * 2], gs
|
---|
83 |
|
---|
84 | ; Remaining GPRs.
|
---|
85 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xBX * 8], xBX
|
---|
86 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xBP * 8], xBP
|
---|
87 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xSI * 8], xSI
|
---|
88 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xDI * 8], xDI
|
---|
89 | %ifdef RT_ARCH_AMD64
|
---|
90 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x8 * 8], r8
|
---|
91 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x9 * 8], r9
|
---|
92 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x10 * 8], r10
|
---|
93 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x11 * 8], r11
|
---|
94 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x12 * 8], r12
|
---|
95 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x13 * 8], r13
|
---|
96 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x14 * 8], r14
|
---|
97 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x15 * 8], r15
|
---|
98 | xor eax, eax
|
---|
99 | mov [xCX + CIDETCPUCTX.cr0], rax
|
---|
100 | mov [xCX + CIDETCPUCTX.cr2], rax
|
---|
101 | mov [xCX + CIDETCPUCTX.cr3], rax
|
---|
102 | mov [xCX + CIDETCPUCTX.cr4], rax
|
---|
103 | mov [xCX + CIDETCPUCTX.cr8], rax
|
---|
104 | %else
|
---|
105 | xor eax, eax
|
---|
106 | mov [xCX + CIDETCPUCTX.rfl + 4], eax
|
---|
107 | mov [xCX + CIDETCPUCTX.rip + 4], eax
|
---|
108 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8 + 4], eax
|
---|
109 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xCX * 8 + 4], eax
|
---|
110 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8 + 4], eax
|
---|
111 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xBX * 8 + 4], eax
|
---|
112 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8 + 4], eax
|
---|
113 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xBP * 8 + 4], eax
|
---|
114 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xSI * 8 + 4], eax
|
---|
115 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xDI * 8 + 4], eax
|
---|
116 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x8 * 8 ], eax
|
---|
117 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x8 * 8 + 4], eax
|
---|
118 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x9 * 8 ], eax
|
---|
119 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x9 * 8 + 4], eax
|
---|
120 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x10 * 8 ], eax
|
---|
121 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x10 * 8 + 4], eax
|
---|
122 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x11 * 8 ], eax
|
---|
123 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x11 * 8 + 4], eax
|
---|
124 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x12 * 8 ], eax
|
---|
125 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x12 * 8 + 4], eax
|
---|
126 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x13 * 8 ], eax
|
---|
127 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x13 * 8 + 4], eax
|
---|
128 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x14 * 8 ], eax
|
---|
129 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x14 * 8 + 4], eax
|
---|
130 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x15 * 8 ], eax
|
---|
131 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x15 * 8 + 4], eax
|
---|
132 | mov [xCX + CIDETCPUCTX.cr0 ], eax
|
---|
133 | mov [xCX + CIDETCPUCTX.cr0 + 4], eax
|
---|
134 | mov [xCX + CIDETCPUCTX.cr2 ], eax
|
---|
135 | mov [xCX + CIDETCPUCTX.cr2 + 4], eax
|
---|
136 | mov [xCX + CIDETCPUCTX.cr3 ], eax
|
---|
137 | mov [xCX + CIDETCPUCTX.cr3 + 4], eax
|
---|
138 | mov [xCX + CIDETCPUCTX.cr4 ], eax
|
---|
139 | mov [xCX + CIDETCPUCTX.cr4 + 4], eax
|
---|
140 | mov [xCX + CIDETCPUCTX.cr8 ], eax
|
---|
141 | mov [xCX + CIDETCPUCTX.cr8 + 4], eax
|
---|
142 | %endif
|
---|
143 | mov [xCX + CIDETCPUCTX.tr], ax
|
---|
144 | mov [xCX + CIDETCPUCTX.ldtr], ax
|
---|
145 |
|
---|
146 | ;
|
---|
147 | ; Restore the other state.
|
---|
148 | ;
|
---|
149 | NAME(CidetAppSaveAndRestoreCtx_Restore):
|
---|
150 |
|
---|
151 | ; Restore ES, FS, and GS.
|
---|
152 | mov es, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_ES * 2]
|
---|
153 | mov fs, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_FS * 2]
|
---|
154 | mov gs, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_GS * 2]
|
---|
155 |
|
---|
156 | ; Restore most GPRs (except xCX, xAX and xSP).
|
---|
157 | mov xCX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xCX * 8]
|
---|
158 | mov xBX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xBX * 8]
|
---|
159 | mov xBP, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xBP * 8]
|
---|
160 | mov xSI, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xSI * 8]
|
---|
161 | mov xDI, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xDI * 8]
|
---|
162 | %ifdef RT_ARCH_AMD64
|
---|
163 | mov r8, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x8 * 8]
|
---|
164 | mov r9, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x9 * 8]
|
---|
165 | mov r10, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x10 * 8]
|
---|
166 | mov r11, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x11 * 8]
|
---|
167 | mov r12, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x12 * 8]
|
---|
168 | mov r13, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x13 * 8]
|
---|
169 | mov r14, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x14 * 8]
|
---|
170 | mov r15, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x15 * 8]
|
---|
171 | %endif
|
---|
172 |
|
---|
173 | %ifdef RT_ARCH_AMD64
|
---|
174 | ; Create an iret frame which restores SS:RSP, RFLAGS, and CS:RIP.
|
---|
175 | movzx eax, word [xDX + CIDETCPUCTX.aSRegs + X86_SREG_SS * 2]
|
---|
176 | push xAX
|
---|
177 | push qword [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8]
|
---|
178 | push qword [xDX + CIDETCPUCTX.rfl]
|
---|
179 | movzx eax, word [xDX + CIDETCPUCTX.aSRegs + X86_SREG_CS * 2]
|
---|
180 | push xAX
|
---|
181 | push qword [xDX + CIDETCPUCTX.rip]
|
---|
182 |
|
---|
183 | ; Restore DS, xAX and xDX then do the iret.
|
---|
184 | mov ds, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2]
|
---|
185 | mov xAX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8]
|
---|
186 | mov xDX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8]
|
---|
187 | iretq
|
---|
188 | %else
|
---|
189 | ; In 32-bit mode iret doesn't restore CS:ESP for us, so we have to
|
---|
190 | ; make a choice whether the SS:ESP is more important than EFLAGS.
|
---|
191 | cmp byte [xDX + CIDETCPUCTX.fTrickyStack], 0
|
---|
192 | jne .tricky_stack
|
---|
193 |
|
---|
194 | mov ss, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_SS * 2]
|
---|
195 | mov xSP, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8]
|
---|
196 |
|
---|
197 | push dword [xDX + CIDETCPUCTX.rfl] ; iret frame
|
---|
198 | movzx eax, word [xDX + CIDETCPUCTX.aSRegs + X86_SREG_CS * 2] ; iret frame
|
---|
199 | push xAX ; iret frame
|
---|
200 | push dword [xDX + CIDETCPUCTX.rip] ; iret frame
|
---|
201 |
|
---|
202 | mov xAX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8]
|
---|
203 | mov ds, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2]
|
---|
204 | mov xDX, [cs:xDX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8]
|
---|
205 | iretd
|
---|
206 |
|
---|
207 | .tricky_stack:
|
---|
208 | mov xAX, [xDX + CIDETCPUCTX.rip]
|
---|
209 | mov [g_uTargetEip], xAX
|
---|
210 | mov ax, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_CS * 2]
|
---|
211 | mov [g_uTargetCs], ax
|
---|
212 | push dword [xDX + CIDETCPUCTX.rfl]
|
---|
213 | popfd
|
---|
214 | mov ss, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_SS * 2]
|
---|
215 | mov xSP, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8]
|
---|
216 | mov xAX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8]
|
---|
217 | mov ds, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2]
|
---|
218 | mov xDX, [cs:xDX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8]
|
---|
219 | jmp far [cs:g_uTargetEip]
|
---|
220 | %endif
|
---|
221 | ENDPROC CidetAppSaveAndRestoreCtx
|
---|
222 |
|
---|
223 |
|
---|
224 | ;;
|
---|
225 | ; C callable version of CidetAppSaveAndRestoreCtx more or less.
|
---|
226 | ;
|
---|
227 | ; @param pSaveCtx x86:esp+4 gcc:rdi msc:rcx
|
---|
228 | ; @param pRestoreCtx x86:esp+8 gcc:rsi msc:rdx
|
---|
229 | BEGINPROC CidetAppExecute
|
---|
230 | %ifdef RT_ARCH_X86
|
---|
231 | mov ecx, [esp + 4]
|
---|
232 | mov edx, [esp + 8]
|
---|
233 | %elifdef ASM_CALL64_GCC
|
---|
234 | mov rcx, rdi
|
---|
235 | mov rdx, rsi
|
---|
236 | %elifndef ASM_CALL64_MSC
|
---|
237 | %error "unsupport arch."
|
---|
238 | %endif
|
---|
239 | mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2], ds
|
---|
240 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8], xDX
|
---|
241 | mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xCX * 8], xCX
|
---|
242 | mov [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8], xSP
|
---|
243 | jmp NAME(CidetAppSaveAndRestoreCtx)
|
---|
244 | ENDPROC CidetAppExecute
|
---|
245 |
|
---|
246 |
|
---|
247 | ;;
|
---|
248 | ; C callable restore function.
|
---|
249 | ;
|
---|
250 | ; @param pRestoreCtx x86:esp+4 gcc:rdi msc:rcx
|
---|
251 | BEGINPROC CidetAppRestoreCtx
|
---|
252 | %ifdef RT_ARCH_X86
|
---|
253 | mov edx, [esp + 4]
|
---|
254 | %elifdef ASM_CALL64_GCC
|
---|
255 | mov rdx, rdi
|
---|
256 | %elifndef ASM_CALL64_MSC
|
---|
257 | %error "unsupport arch."
|
---|
258 | %endif
|
---|
259 | mov ds, [cs:xDX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2]
|
---|
260 | jmp NAME(CidetAppSaveAndRestoreCtx_Restore)
|
---|
261 | ENDPROC CidetAppRestoreCtx
|
---|
262 |
|
---|