VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/utils/cpu/cidet-appA.asm@ 53563

Last change on this file since 53563 was 53563, checked in by vboxsync, 10 years ago

cidet: More hacking.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
1; $Id: cidet-appA.asm 53563 2014-12-18 02:31:37Z 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.
41g_uTargetEip dd 0
42g_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;
53BEGINPROC 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 lea xAX, [xSP + xCB]
60 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8], xAX
61 mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_SS * 2], ss
62 mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_CS * 2], cs
63 mov xAX, [xSP]
64 mov [xCX + CIDETCPUCTX.rip], xAX
65 jmp CidetAppSaveAndRestoreCtx_1
66
67GLOBALNAME CidetAppSaveAndRestoreCtx_NoSsSpCsIp
68 mov [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8], xAX
69CidetAppSaveAndRestoreCtx_1:
70
71 ; Flags.
72%ifdef RT_ARCH_AMD64
73 pushfq
74%else
75 pushfd
76%endif
77 pop xAX
78 mov [xCX + CIDETCPUCTX.rfl], xAX
79
80 ; Segment registers.
81 mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_ES * 2], es
82 mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_FS * 2], fs
83 mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_GS * 2], gs
84
85 ; Remaining GPRs.
86 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xBX * 8], xBX
87 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xBP * 8], xBP
88 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xSI * 8], xSI
89 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xDI * 8], xDI
90%ifdef RT_ARCH_AMD64
91 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x8 * 8], r8
92 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x9 * 8], r9
93 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x10 * 8], r10
94 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x11 * 8], r11
95 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x12 * 8], r12
96 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x13 * 8], r13
97 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x14 * 8], r14
98 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x15 * 8], r15
99 xor eax, eax
100 mov [xCX + CIDETCPUCTX.cr0], rax
101 mov [xCX + CIDETCPUCTX.cr2], rax
102 mov [xCX + CIDETCPUCTX.cr3], rax
103 mov [xCX + CIDETCPUCTX.cr4], rax
104 mov [xCX + CIDETCPUCTX.cr8], rax
105%else
106 xor eax, eax
107 mov [xCX + CIDETCPUCTX.rfl + 4], eax
108 mov [xCX + CIDETCPUCTX.rip + 4], eax
109 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8 + 4], eax
110 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xCX * 8 + 4], eax
111 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8 + 4], eax
112 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xBX * 8 + 4], eax
113 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8 + 4], eax
114 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xBP * 8 + 4], eax
115 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xSI * 8 + 4], eax
116 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xDI * 8 + 4], eax
117 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x8 * 8 ], eax
118 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x8 * 8 + 4], eax
119 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x9 * 8 ], eax
120 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x9 * 8 + 4], eax
121 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x10 * 8 ], eax
122 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x10 * 8 + 4], eax
123 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x11 * 8 ], eax
124 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x11 * 8 + 4], eax
125 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x12 * 8 ], eax
126 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x12 * 8 + 4], eax
127 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x13 * 8 ], eax
128 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x13 * 8 + 4], eax
129 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x14 * 8 ], eax
130 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x14 * 8 + 4], eax
131 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x15 * 8 ], eax
132 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x15 * 8 + 4], eax
133 mov [xCX + CIDETCPUCTX.cr0 ], eax
134 mov [xCX + CIDETCPUCTX.cr0 + 4], eax
135 mov [xCX + CIDETCPUCTX.cr2 ], eax
136 mov [xCX + CIDETCPUCTX.cr2 + 4], eax
137 mov [xCX + CIDETCPUCTX.cr3 ], eax
138 mov [xCX + CIDETCPUCTX.cr3 + 4], eax
139 mov [xCX + CIDETCPUCTX.cr4 ], eax
140 mov [xCX + CIDETCPUCTX.cr4 + 4], eax
141 mov [xCX + CIDETCPUCTX.cr8 ], eax
142 mov [xCX + CIDETCPUCTX.cr8 + 4], eax
143%endif
144 mov [xCX + CIDETCPUCTX.tr], ax
145 mov [xCX + CIDETCPUCTX.ldtr], ax
146
147 ;
148 ; Restore the other state (pointer in xDX).
149 ;
150NAME(CidetAppSaveAndRestoreCtx_Restore):
151
152 ; Restore ES, FS, and GS.
153 mov es, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_ES * 2]
154 mov fs, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_FS * 2]
155 mov gs, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_GS * 2]
156
157 ; Restore most GPRs (except xCX, xAX and xSP).
158 mov xCX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xCX * 8]
159 mov xBX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xBX * 8]
160 mov xBP, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xBP * 8]
161 mov xSI, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xSI * 8]
162 mov xDI, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xDI * 8]
163%ifdef RT_ARCH_AMD64
164 mov r8, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x8 * 8]
165 mov r9, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x9 * 8]
166 mov r10, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x10 * 8]
167 mov r11, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x11 * 8]
168 mov r12, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x12 * 8]
169 mov r13, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x13 * 8]
170 mov r14, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x14 * 8]
171 mov r15, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x15 * 8]
172%endif
173
174%ifdef RT_ARCH_AMD64
175 ; Create an iret frame which restores SS:RSP, RFLAGS, and CS:RIP.
176 movzx eax, word [xDX + CIDETCPUCTX.aSRegs + X86_SREG_SS * 2]
177 push xAX
178 push qword [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8]
179 push qword [xDX + CIDETCPUCTX.rfl]
180 movzx eax, word [xDX + CIDETCPUCTX.aSRegs + X86_SREG_CS * 2]
181 push xAX
182 push qword [xDX + CIDETCPUCTX.rip]
183
184 ; Restore DS, xAX and xDX then do the iret.
185 mov ds, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2]
186 mov xAX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8]
187 mov xDX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8]
188 iretq
189%else
190 ; In 32-bit mode iret doesn't restore CS:ESP for us, so we have to
191 ; make a choice whether the SS:ESP is more important than EFLAGS.
192 cmp byte [xDX + CIDETCPUCTX.fTrickyStack], 0
193 jne .tricky_stack
194
195 mov ss, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_SS * 2]
196 mov xSP, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8]
197
198 push dword [xDX + CIDETCPUCTX.rfl] ; iret frame
199 movzx eax, word [xDX + CIDETCPUCTX.aSRegs + X86_SREG_CS * 2] ; iret frame
200 push xAX ; iret frame
201 push dword [xDX + CIDETCPUCTX.rip] ; iret frame
202
203 mov xAX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8]
204 mov ds, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2]
205 mov xDX, [cs:xDX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8]
206 iretd
207
208.tricky_stack:
209 mov xAX, [xDX + CIDETCPUCTX.rip]
210 mov [g_uTargetEip], xAX
211 mov ax, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_CS * 2]
212 mov [g_uTargetCs], ax
213 push dword [xDX + CIDETCPUCTX.rfl]
214 popfd
215 mov ss, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_SS * 2]
216 mov xSP, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8]
217 mov xAX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8]
218 mov ds, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2]
219 mov xDX, [cs:xDX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8]
220 jmp far [cs:g_uTargetEip]
221%endif
222ENDPROC CidetAppSaveAndRestoreCtx
223
224
225;;
226; C callable version of CidetAppSaveAndRestoreCtx more or less.
227;
228; @param pSaveCtx x86:esp+4 gcc:rdi msc:rcx
229; @param pRestoreCtx x86:esp+8 gcc:rsi msc:rdx
230BEGINPROC CidetAppExecute
231%ifdef RT_ARCH_X86
232 mov ecx, [esp + 4]
233 mov edx, [esp + 8]
234%elifdef ASM_CALL64_GCC
235 mov rcx, rdi
236 mov rdx, rsi
237%elifndef ASM_CALL64_MSC
238 %error "unsupport arch."
239%endif
240 mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2], ds
241 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8], xDX
242 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xCX * 8], xCX
243 jmp NAME(CidetAppSaveAndRestoreCtx)
244ENDPROC CidetAppExecute
245
246
247;;
248; C callable restore function.
249;
250; @param pRestoreCtx x86:esp+4 gcc:rdi msc:rcx
251BEGINPROC CidetAppRestoreCtx
252%ifdef RT_ARCH_X86
253 mov edx, [esp + 4]
254%elifdef ASM_CALL64_GCC
255 mov rdx, rdi
256%elifdef ASM_CALL64_MSC
257 mov rdx, rcx
258%else
259 %error "unsupport arch."
260%endif
261 mov ds, [cs:xDX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2]
262 jmp NAME(CidetAppSaveAndRestoreCtx_Restore)
263ENDPROC CidetAppRestoreCtx
264
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette