VirtualBox

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

Last change on this file was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
Line 
1; $Id: cidet-appA.asm 106061 2024-09-16 14:03:52Z vboxsync $
2;; @file
3; CPU Instruction Decoding & Execution Tests - Ring-3 Driver Application, Assembly Code.
4;
5
6;
7; Copyright (C) 2009-2024 Oracle and/or its affiliates.
8;
9; This file is part of VirtualBox base platform packages, as
10; available from https://www.virtualbox.org.
11;
12; This program is free software; you can redistribute it and/or
13; modify it under the terms of the GNU General Public License
14; as published by the Free Software Foundation, in version 3 of the
15; License.
16;
17; This program is distributed in the hope that it will be useful, but
18; WITHOUT ANY WARRANTY; without even the implied warranty of
19; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20; General Public License for more details.
21;
22; You should have received a copy of the GNU General Public License
23; along with this program; if not, see <https://www.gnu.org/licenses>.
24;
25; The contents of this file may alternatively be used under the terms
26; of the Common Development and Distribution License Version 1.0
27; (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28; in the VirtualBox distribution, in which case the provisions of the
29; CDDL are applicable instead of those of the GPL.
30;
31; You may elect to license modified versions of this file under the
32; terms and conditions of either the GPL or the CDDL or both.
33;
34; SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35;
36
37
38;*******************************************************************************
39;* Header Files *
40;*******************************************************************************
41%include "iprt/asmdefs.mac"
42%include "iprt/x86.mac"
43%include "cidet.mac"
44
45
46;*******************************************************************************
47;* Global Variables *
48;*******************************************************************************
49%ifdef RT_ARCH_X86
50;; Used by CidetAppSaveAndRestoreCtx when we have a tricky target stack.
51g_uTargetEip dd 0
52g_uTargetCs dw 0
53%endif
54
55
56;;
57; Leave GS alone on 64-bit darwin (gs is 0, no ldt or gdt entry to load that'll
58; restore the lower 32-bits of the base when saving and restoring the register).
59%ifdef RT_OS_DARWIN
60 %ifdef RT_ARCH_AMD64
61 %define CIDET_LEAVE_GS_ALONE
62 %endif
63%endif
64
65
66
67BEGINCODE
68
69;;
70; ASSUMES that it's called and the EIP/RIP is found on the stack.
71;
72; @param pSaveCtx ds:xCX The context to save; DS, xDX and xCX have
73; already been saved by the caller.
74; @param pRestoreCtx ds:xDX The context to restore.
75;
76BEGINPROC CidetAppSaveAndRestoreCtx
77 ;
78 ; Save the stack pointer and program counter first so we can later
79 ; bypass this step if we need to.
80 ;
81 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8], xAX ; need scratch register.
82 lea xAX, [xSP + xCB]
83 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8], xAX
84 mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_SS * 2], ss
85 mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_CS * 2], cs
86 mov xAX, [xSP]
87 mov [xCX + CIDETCPUCTX.rip], xAX
88 jmp CidetAppSaveAndRestoreCtx_1
89
90GLOBALNAME CidetAppSaveAndRestoreCtx_NoSsSpCsIp
91 mov [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8], xAX
92CidetAppSaveAndRestoreCtx_1:
93
94 ; Flags.
95%ifdef RT_ARCH_AMD64
96 pushfq
97%else
98 pushfd
99%endif
100 pop xAX
101 mov [xCX + CIDETCPUCTX.rfl], xAX
102
103 ; Segment registers.
104 mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_ES * 2], es
105 mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_FS * 2], fs
106 mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_GS * 2], gs
107
108 ; Remaining GPRs.
109 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xBX * 8], xBX
110 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xBP * 8], xBP
111 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xSI * 8], xSI
112 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xDI * 8], xDI
113%ifdef RT_ARCH_AMD64
114 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x8 * 8], r8
115 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x9 * 8], r9
116 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x10 * 8], r10
117 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x11 * 8], r11
118 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x12 * 8], r12
119 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x13 * 8], r13
120 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x14 * 8], r14
121 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x15 * 8], r15
122 xor eax, eax
123 mov [xCX + CIDETCPUCTX.cr2], rax
124 %ifndef CIDET_REDUCED_CTX
125 mov [xCX + CIDETCPUCTX.cr0], rax
126 mov [xCX + CIDETCPUCTX.cr3], rax
127 mov [xCX + CIDETCPUCTX.cr4], rax
128 mov [xCX + CIDETCPUCTX.cr8], rax
129 mov [xCX + CIDETCPUCTX.dr0], rax
130 mov [xCX + CIDETCPUCTX.dr1], rax
131 mov [xCX + CIDETCPUCTX.dr2], rax
132 mov [xCX + CIDETCPUCTX.dr3], rax
133 mov [xCX + CIDETCPUCTX.dr6], rax
134 mov [xCX + CIDETCPUCTX.dr7], rax
135 mov [xCX + CIDETCPUCTX.tr], ax
136 mov [xCX + CIDETCPUCTX.ldtr], ax
137 %endif
138%else
139 xor eax, eax
140 mov [xCX + CIDETCPUCTX.rfl + 4], eax
141 mov [xCX + CIDETCPUCTX.rip + 4], eax
142 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8 + 4], eax
143 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xCX * 8 + 4], eax
144 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8 + 4], eax
145 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xBX * 8 + 4], eax
146 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8 + 4], eax
147 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xBP * 8 + 4], eax
148 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xSI * 8 + 4], eax
149 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xDI * 8 + 4], eax
150 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x8 * 8 ], eax
151 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x8 * 8 + 4], eax
152 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x9 * 8 ], eax
153 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x9 * 8 + 4], eax
154 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x10 * 8 ], eax
155 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x10 * 8 + 4], eax
156 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x11 * 8 ], eax
157 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x11 * 8 + 4], eax
158 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x12 * 8 ], eax
159 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x12 * 8 + 4], eax
160 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x13 * 8 ], eax
161 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x13 * 8 + 4], eax
162 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x14 * 8 ], eax
163 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x14 * 8 + 4], eax
164 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x15 * 8 ], eax
165 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_x15 * 8 + 4], eax
166 mov [xCX + CIDETCPUCTX.cr2 ], eax
167 mov [xCX + CIDETCPUCTX.cr2 + 4], eax
168 %ifndef CIDET_REDUCED_CTX
169 mov [xCX + CIDETCPUCTX.cr0 ], eax
170 mov [xCX + CIDETCPUCTX.cr0 + 4], eax
171 mov [xCX + CIDETCPUCTX.cr3 ], eax
172 mov [xCX + CIDETCPUCTX.cr3 + 4], eax
173 mov [xCX + CIDETCPUCTX.cr4 ], eax
174 mov [xCX + CIDETCPUCTX.cr4 + 4], eax
175 mov [xCX + CIDETCPUCTX.cr8 ], eax
176 mov [xCX + CIDETCPUCTX.cr8 + 4], eax
177 mov [xCX + CIDETCPUCTX.dr0 ], eax
178 mov [xCX + CIDETCPUCTX.dr0 + 4], eax
179 mov [xCX + CIDETCPUCTX.dr1 ], eax
180 mov [xCX + CIDETCPUCTX.dr1 + 4], eax
181 mov [xCX + CIDETCPUCTX.dr2 ], eax
182 mov [xCX + CIDETCPUCTX.dr2 + 4], eax
183 mov [xCX + CIDETCPUCTX.dr3 ], eax
184 mov [xCX + CIDETCPUCTX.dr3 + 4], eax
185 mov [xCX + CIDETCPUCTX.dr6 ], eax
186 mov [xCX + CIDETCPUCTX.dr6 + 4], eax
187 mov [xCX + CIDETCPUCTX.dr7 ], eax
188 mov [xCX + CIDETCPUCTX.dr7 + 4], eax
189 mov [xCX + CIDETCPUCTX.tr], ax
190 mov [xCX + CIDETCPUCTX.ldtr], ax
191 %endif
192%endif
193 dec xAX
194 mov [xCX + CIDETCPUCTX.uErr], xAX
195%ifdef RT_ARCH_X86
196 mov [xCX + CIDETCPUCTX.uErr + 4], eax
197%endif
198 mov [xCX + CIDETCPUCTX.uXcpt], eax
199
200 ;
201 ; Restore the other state (pointer in xDX).
202 ;
203NAME(CidetAppSaveAndRestoreCtx_Restore):
204
205 ; Restore ES, FS, and GS.
206 mov es, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_ES * 2]
207 mov fs, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_FS * 2]
208%ifndef CIDET_LEAVE_GS_ALONE
209 mov gs, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_GS * 2]
210%endif
211
212 ; Restore most GPRs (except xCX, xAX and xSP).
213 mov xCX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xCX * 8]
214 mov xBX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xBX * 8]
215 mov xBP, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xBP * 8]
216 mov xSI, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xSI * 8]
217 mov xDI, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xDI * 8]
218%ifdef RT_ARCH_AMD64
219 mov r8, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x8 * 8]
220 mov r9, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x9 * 8]
221 mov r10, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x10 * 8]
222 mov r11, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x11 * 8]
223 mov r12, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x12 * 8]
224 mov r13, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x13 * 8]
225 mov r14, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x14 * 8]
226 mov r15, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_x15 * 8]
227%endif
228
229%ifdef RT_ARCH_AMD64
230 ; Create an iret frame which restores SS:RSP, RFLAGS, and CS:RIP.
231 movzx eax, word [xDX + CIDETCPUCTX.aSRegs + X86_SREG_SS * 2]
232 push xAX
233 push qword [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8]
234 push qword [xDX + CIDETCPUCTX.rfl]
235 movzx eax, word [xDX + CIDETCPUCTX.aSRegs + X86_SREG_CS * 2]
236 push xAX
237 push qword [xDX + CIDETCPUCTX.rip]
238
239 ; Restore DS, xAX and xDX then do the iret.
240 mov ds, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2]
241 mov xAX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8]
242 mov xDX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8]
243 iretq
244%else
245 ; In 32-bit mode iret doesn't restore CS:ESP for us, so we have to
246 ; make a choice whether the SS:ESP is more important than EFLAGS.
247 cmp byte [xDX + CIDETCPUCTX.fTrickyStack], 0
248 jne .tricky_stack
249
250 mov ss, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_SS * 2]
251 mov xSP, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8]
252
253 push dword [xDX + CIDETCPUCTX.rfl] ; iret frame
254 movzx eax, word [xDX + CIDETCPUCTX.aSRegs + X86_SREG_CS * 2] ; iret frame
255 push xAX ; iret frame
256 push dword [xDX + CIDETCPUCTX.rip] ; iret frame
257
258 mov xAX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8]
259 mov ds, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2]
260 mov xDX, [cs:xDX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8]
261 iretd
262
263.tricky_stack:
264 mov xAX, [xDX + CIDETCPUCTX.rip]
265 mov [g_uTargetEip], xAX
266 mov ax, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_CS * 2]
267 mov [g_uTargetCs], ax
268 push dword [xDX + CIDETCPUCTX.rfl]
269 popfd
270 mov ss, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_SS * 2]
271 mov xSP, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xSP * 8]
272 mov xAX, [xDX + CIDETCPUCTX.aGRegs + X86_GREG_xAX * 8]
273 mov ds, [xDX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2]
274 mov xDX, [cs:xDX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8]
275 jmp far [cs:g_uTargetEip]
276%endif
277ENDPROC CidetAppSaveAndRestoreCtx
278
279
280;;
281; C callable version of CidetAppSaveAndRestoreCtx more or less.
282;
283; @param pSaveCtx x86:esp+4 gcc:rdi msc:rcx
284; @param pRestoreCtx x86:esp+8 gcc:rsi msc:rdx
285BEGINPROC CidetAppExecute
286%ifdef RT_ARCH_X86
287 mov ecx, [esp + 4]
288 mov edx, [esp + 8]
289%elifdef ASM_CALL64_GCC
290 mov rcx, rdi
291 mov rdx, rsi
292%elifndef ASM_CALL64_MSC
293 %error "unsupport arch."
294%endif
295 mov word [xCX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2], ds
296 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xDX * 8], xDX
297 mov [xCX + CIDETCPUCTX.aGRegs + X86_GREG_xCX * 8], xCX
298 jmp NAME(CidetAppSaveAndRestoreCtx)
299ENDPROC CidetAppExecute
300
301
302;;
303; C callable restore function.
304;
305; @param pRestoreCtx x86:esp+4 gcc:rdi msc:rcx
306BEGINPROC CidetAppRestoreCtx
307%ifdef RT_ARCH_X86
308 mov edx, [esp + 4]
309%elifdef ASM_CALL64_GCC
310 mov rdx, rdi
311%elifdef ASM_CALL64_MSC
312 mov rdx, rcx
313%else
314 %error "unsupport arch."
315%endif
316 mov ds, [cs:xDX + CIDETCPUCTX.aSRegs + X86_SREG_DS * 2]
317 jmp NAME(CidetAppSaveAndRestoreCtx_Restore)
318ENDPROC CidetAppRestoreCtx
319
Note: See TracBrowser for help on using the repository browser.

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