VirtualBox

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

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

cidet: darwin fixes.

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