1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
; $Id: env-common.mac $
;; @file
; Instruction Test Environment - Common Bits.
;
;
; Copyright (C) 2006-2023 Oracle and/or its affiliates.
;
; This file is part of VirtualBox base platform packages, as
; available from https://www.virtualbox.org.
;
; This program is free software; you can redistribute it and/or
; modify it under the terms of the GNU General Public License
; as published by the Free Software Foundation, in version 3 of the
; License.
;
; This program is distributed in the hope that it will be useful, but
; WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
; General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, see <https://www.gnu.org/licenses>.
;
; SPDX-License-Identifier: GPL-3.0-only
;
%ifndef ___env_common_mac
%define ___env_common_mac
%include "iprt/x86.mac"
;*******************************************************************************
;* Defined Constants And Macros *
;*******************************************************************************
%ifdef RT_ARCH_AMD64
%define MY_PUSH_FLAGS pushfq
%define MY_POP_FLAGS popfq
%define MY_PUSH_FLAGS_SIZE 8
%macro MY_PUSH_ALL 0
push rbp
mov rbp, rsp
push rax
push rbx
push rcx
push rdx
push rsi
push rdi
push r8
push r9
push r10
push r11
push r12
push r13
push r14
push r15
pushfq
%endm
%macro MY_POP_ALL 0
popfq
pop r15
pop r14
pop r13
pop r12
pop r11
pop r10
pop r9
pop r8
pop rdi
pop rsi
pop rdx
pop rcx
pop rbx
pop rax
pop rbp
%endm
%else
%define MY_PUSH_FLAGS pushfd
%define MY_POP_FLAGS popfd
%define MY_PUSH_FLAGS_SIZE 4
%macro MY_PUSH_ALL 0
push eBP
mov xBP, xSP
push eax
push ebx
push ecx
push edx
push esi
push edi
pushfd
%endm
%macro MY_POP_ALL 0
popfd
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax
pop ebp
%endm
%endif
;*******************************************************************************
;* Internal Functions *
;*******************************************************************************
VBINSTST_BEGINCODE
;;
; Report bad register value.
;
; Primary purpose is save all registers and convert from our stack-based to
; the correct calling convention for the environment.
;
; This function will clean up the stack upon return (to save space in the caller).
;
; @param uExpected
; @param uActual
; @param uRegisterNo
;
VBINSTST_BEGINPROC Common_BadValue
MY_PUSH_ALL
mov xAX, xSP ; 16-byte align the stack and reserve space for arguments and stuff.
sub xSP, 40h
and xSP, ~15
mov [xSP + 38h], xAX
%ifdef ASM_CALL64_GCC
mov r8d, [VBINSTST_NAME(g_uVBInsTstSubTestIndicator) wrt rip]
mov rcx, [rbp + 10h] ; expected
mov rdx, [rbp + 18h] ; actual
mov rsi, [rbp + 20h] ; reg#
lea rdi, [.szFmt wrt rip]
VBINSTST_CALL_FN_FAILURE_4
%elifdef ASM_CALL64_MSC
mov r10d, [VBINSTST_NAME(g_uVBInsTstSubTestIndicator) wrt rip]
mov [rsp + 20h], r10
mov r9, [rbp + 10h] ; expected
mov r8, [rbp + 18h] ; actual
mov rdx, [rbp + 20h] ; reg#
lea rcx, [.szFmt wrt rip]
VBINSTST_CALL_FN_FAILURE_4
%elifdef ASM_CALL64_BS2
mov sBX, [VBINSTST_NAME(g_uVBInsTstSubTestIndicator) xWrtRIP]
mov sCX, [xBP + xCB + xCB] ; expected
mov sAX, [xBP + xCB + xCB + sCB*1] ; actual
mov sDX, [xBP + xCB + xCB + sCB*2] ; reg#
lea sSI, [.szFmt xWrtRIP]
mov qword [xSP + xCB + 3*sCB], sBX
mov qword [xSP + xCB + 2*sCB], sCX
mov qword [xSP + xCB + 1*sCB], sAX
mov qword [xSP + xCB], sDX
mov [xSP], sSI
VBINSTST_CALL_FN_FAILURE_4
%else
mov sBX, [VBINSTST_NAME(g_uVBInsTstSubTestIndicator)]
mov sCX, [xBP + xCB + xCB] ; expected
mov sAX, [xBP + xCB + xCB + sCB*1] ; actual
mov sDX, [xBP + xCB + xCB + sCB*2] ; reg#
mov [xSP + xCB + 3*sCB], sBX
mov [xSP + xCB + 2*sCB], sCX
mov [xSP + xCB + 1*sCB], sAX
mov [xSP + xCB], sDX
mov [xSP], RTCCPTR_PRE .szFmt
VBINSTST_CALL_FN_FAILURE_4
%endif
mov xSP, [xSP + 38h]
MY_POP_ALL
ret 3*sCB
%if ARCH_BITS == 64
.szFmt: db 'Bad register 0x%RX32 value 0x%RX64, expected 0x%RX64 (line %RU64)', 13, 0
%else
.szFmt: db 'Bad register 0x%RX32 value 0x%RX32, expected 0x%RX32 (line %RU32)', 13, 0
%endif
VBINSTST_ENDPROC Common_BadValue
%ifdef VBINSTST_CAN_DO_TRAPS
;;
; Report a missing TRAP.
;
; Primary purpose is save all registers and convert from our stack-based to
; the correct calling convention for the environment.
;
; This function will clean up the stack upon return (to save space in the caller).
;
; @param uExpected
;
VBINSTST_BEGINPROC Common_MissingTrap
MY_PUSH_ALL
mov xAX, xSP ; 16-byte align the stack and reserve space for arguments and stuff.
sub xSP, 40h
and xSP, ~15
mov [xSP + 38h], xAX
%ifdef ASM_CALL64_GCC
mov rdx, [VBINSTST_NAME(g_uVBInsTstSubTestIndicator) wrt rip]
movzx rsi, byte [rbp + 10h] ; expected
lea rdi, [.szFmt wrt rip]
VBINSTST_CALL_FN_FAILURE_2
%elifdef ASM_CALL64_MSC
mov r8d, [VBINSTST_NAME(g_uVBInsTstSubTestIndicator) wrt rip]
movzx rdx, byte [rbp + 10h] ; expected
lea rcx, [.szFmt wrt rip]
VBINSTST_CALL_FN_FAILURE_2
%elifdef ASM_CALL64_BS2
mov sBX, [VBINSTST_NAME(g_uVBInsTstSubTestIndicator) xWrtRIP]
mov sDX, [xBP + xCB + xCB] ; expected
lea sSI, [.szFmt xWrtRIP]
mov qword [xSP + xCB + 1*sCB], sBX
mov qword [xSP + xCB], sDX
mov [xSP], sSI
VBINSTST_CALL_FN_FAILURE_2
%else
mov sBX, [VBINSTST_NAME(g_uVBInsTstSubTestIndicator)]
mov sDX, [xBP + xCB + xCB] ; expected
mov [xSP + xCB + 1*sCB], sBX
mov [xSP + xCB], sDX
mov [xSP], RTCCPTR_PRE .szFmt
VBINSTST_CALL_FN_FAILURE_2
%endif
mov xSP, [xSP + 38h]
MY_POP_ALL
ret 1*sCB
%if ARCH_BITS == 64
.szFmt: db 'Missing trap %RX8 (line %RU64)', 13, 0
%else
.szFmt: db 'Missing trap %RX8 (line %RU32)', 13, 0
%endif
VBINSTST_ENDPROC Common_MissingTrap
%macro Common_MissingTrapTemplate 1
VBINSTST_BEGINPROC Common_MissingTrap_%1
push %1
call VBINSTST_NAME(Common_MissingTrap)
ret
VBINSTST_ENDPROC Common_MissingTrap_%1
%endmacro
Common_MissingTrapTemplate X86_XCPT_DE
Common_MissingTrapTemplate X86_XCPT_DB
Common_MissingTrapTemplate X86_XCPT_NMI
Common_MissingTrapTemplate X86_XCPT_BP
Common_MissingTrapTemplate X86_XCPT_OF
Common_MissingTrapTemplate X86_XCPT_BR
Common_MissingTrapTemplate X86_XCPT_UD
Common_MissingTrapTemplate X86_XCPT_NM
;Common_MissingTrapTemplate X86_XCPT_DF
;Common_MissingTrapTemplate X86_XCPT_CO_SEG_OVERRUN
Common_MissingTrapTemplate X86_XCPT_TS
Common_MissingTrapTemplate X86_XCPT_NP
Common_MissingTrapTemplate X86_XCPT_SS
Common_MissingTrapTemplate X86_XCPT_GP
Common_MissingTrapTemplate X86_XCPT_PF
Common_MissingTrapTemplate X86_XCPT_MF
Common_MissingTrapTemplate X86_XCPT_AC
;Common_MissingTrapTemplate X86_XCPT_MC
Common_MissingTrapTemplate X86_XCPT_XF
%endif ; VBINSTST_CAN_DO_TRAPS
;
; Global data variables used by Common_SetupMemReadUxx.
; For address calculation reasons, these must be qword aligned.
;
VBINSTST_BEGINDATA
align 64
dd 09d8af498h, 09ab3e5f8h
VBINSTST_GLOBALNAME_EX g_u64Data, data hidden
dq 0
dd 07d7af797h, 096b36562h
VBINSTST_GLOBALNAME_EX g_u32Data, data hidden
dd 0
dd 012305987h
VBINSTST_GLOBALNAME_EX g_u16Data, data hidden
dw 0
dw 05865h
dw 03863h
dw 02679h
VBINSTST_GLOBALNAME_EX g_u8Data, data hidden
db 0
db 90h
dw 0865ah
dd 058daffe2h
VBINSTST_BEGINCODE
;;
; Sets up g_u8Data.
; @param uValue
VBINSTST_BEGINPROC Common_SetupMemReadU8
push sAX
mov ax, [xSP + sCB + xCB]
mov [VBINSTST_NAME(g_u8Data) xWrtRIP], ax
pop sAX
ret sCB
VBINSTST_ENDPROC Common_SetupMemReadU8
;;
; Sets up g_u16Data.
; @param uValue
VBINSTST_BEGINPROC Common_SetupMemReadU16
push sAX
mov ax, [xSP + sCB + xCB]
mov [VBINSTST_NAME(g_u16Data) xWrtRIP], ax
pop sAX
ret sCB
VBINSTST_ENDPROC Common_SetupMemReadU16
;;
; Sets up g_u32Data.
; @param uValue
VBINSTST_BEGINPROC Common_SetupMemReadU32
push sAX
mov eax, [xSP + sCB + xCB]
mov [VBINSTST_NAME(g_u32Data) xWrtRIP], eax
pop sAX
ret sCB
VBINSTST_ENDPROC Common_SetupMemReadU32
;;
; Sets up g_u64Data.
; @param uValue
VBINSTST_BEGINPROC Common_SetupMemReadU64
push sAX
%ifdef RT_ARCH_AMD64
mov rax, [xSP + sCB + xCB]
mov [VBINSTST_NAME(g_u64Data) xWrtRIP], rax
%else
mov eax, [xSP + sCB + xCB]
mov [VBINSTST_NAME(g_u64Data) xWrtRIP], eax
mov eax, [xSP + sCB + xCB + 4]
mov [VBINSTST_NAME(g_u64Data) + 4 xWrtRIP], eax
%endif
pop sAX
ret sCB
VBINSTST_ENDPROC Common_SetupMemReadU64
%endif
|