summaryrefslogtreecommitdiffstats
path: root/src/VBox/ValidationKit/bootsectors/bootsector2-cpu-instr-1-template.mac
blob: 7b0ea8c592ba3c01ca710db9cbc65e2160d7d484 (plain)
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
; $Id: bootsector2-cpu-instr-1-template.mac $
;; @file
; Bootsector test for misc instruction - multi mode template.
;

;
; Copyright (C) 2007-2019 Oracle Corporation
;
; This file is part of VirtualBox Open Source Edition (OSE), as
; available from http://www.virtualbox.org. This file is free software;
; you can redistribute it and/or modify it under the terms of the GNU
; General Public License (GPL) as published by the Free Software
; Foundation, in version 2 as it comes in the "COPYING" file of the
; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
;
; The contents of this file may alternatively be used under the terms
; of the Common Development and Distribution License Version 1.0
; (CDDL) only, as it comes in the "COPYING.CDDL" file of the
; VirtualBox OSE distribution, in which case the provisions of the
; CDDL are applicable instead of those of the GPL.
;
; You may elect to license modified versions of this file under the
; terms and conditions of either the GPL or the CDDL or both.
;


%include "bootsector2-template-header.mac"



;;
; Memory fence instructions (SSE2).
;
; @uses No registers, but BS2_SEL_SPARE0 is trashed.
;
BEGINPROC TMPL_NM(TestMemFences)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    xBX
        push    xCX
        push    xDX
        push    xDI
        push    xSI
        sub     xSP, 80h                ; iret stack frame space.
        mov     xSI, xSP                ; Save the stack register.

        mov     xAX, .s_szSubTestName
        call    TMPL_NM_CMN(TestSub)

        ;
        ; SSE2 supported?
        ;
        mov     eax, 1
        xor     ecx, ecx
        cpuid
        test    edx, X86_CPUID_FEATURE_EDX_SSE2
        jz      .skip

        ;
        ; Check that the standard instruction encodings work.
        ;
        mov     xBX, [xSP + 10h]
        mov     [xSP], xAX
        mfence
        mov     [xSP], xCX
        mov     xBX, [xSP + 08h]
        sfence
        mov     [xSP], xDX
        mov     xBX, [xSP]
        lfence
        mov     bx, [xSP + 04h]


        ;
        ; The instruction encodings in the intel manual may open the RM as well
        ; as prefixes open to interpretation.  AMD sets RM=0 in their docs.
        ;
        ;       lfence = 0f,ea,e8
        ;       mfence = 0f,ea,f0
        ;       sfence = 0f,ea,f8
        ;       (RM is the lower 3 bits of the last byte.)

%assign MY_RM 0xe8
%rep 18h
        db                                                       0fh, 0aeh, MY_RM
        db                                 X86_OP_PRF_CS,        0fh, 0aeh, MY_RM
        db                                 X86_OP_PRF_DS,        0fh, 0aeh, MY_RM
        db                                 X86_OP_PRF_ES,        0fh, 0aeh, MY_RM
        db                                 X86_OP_PRF_FS,        0fh, 0aeh, MY_RM
        db                                 X86_OP_PRF_GS,        0fh, 0aeh, MY_RM
        db                                 X86_OP_PRF_SS,        0fh, 0aeh, MY_RM
        db                                 X86_OP_PRF_SIZE_ADDR, 0fh, 0aeh, MY_RM
        BS2_TRAP_INSTR X86_XCPT_UD, 0,  db X86_OP_PRF_SIZE_OP,   0fh, 0aeh, MY_RM ; (used in group)
        BS2_TRAP_INSTR X86_XCPT_UD, 0,  db X86_OP_PRF_LOCK,      0fh, 0aeh, MY_RM ; (used in group)
        BS2_TRAP_INSTR X86_XCPT_UD, 0,  db X86_OP_PRF_REPNZ,     0fh, 0aeh, MY_RM ; (used in group)
        BS2_TRAP_INSTR X86_XCPT_UD, 0,  db X86_OP_PRF_REPZ,      0fh, 0aeh, MY_RM ; (used in group)
%ifdef TMPL_64BIT
 %assign MY_REX 0x40
 %rep 10h
        ; Rex prefixes doesn't change anything.
        db                                                       MY_REX, 0fh, 0aeh, MY_RM
        db                                 X86_OP_PRF_CS,        MY_REX, 0fh, 0aeh, MY_RM
        db                                 X86_OP_PRF_DS,        MY_REX, 0fh, 0aeh, MY_RM
        db                                 X86_OP_PRF_ES,        MY_REX, 0fh, 0aeh, MY_RM
        db                                 X86_OP_PRF_FS,        MY_REX, 0fh, 0aeh, MY_RM
        db                                 X86_OP_PRF_GS,        MY_REX, 0fh, 0aeh, MY_RM
        db                                 X86_OP_PRF_SS,        MY_REX, 0fh, 0aeh, MY_RM
        db                                 X86_OP_PRF_SIZE_ADDR, MY_REX, 0fh, 0aeh, MY_RM
        BS2_TRAP_INSTR X86_XCPT_UD, 0,  db X86_OP_PRF_SIZE_OP,   MY_REX, 0fh, 0aeh, MY_RM ; (used in group)
        BS2_TRAP_INSTR X86_XCPT_UD, 0,  db X86_OP_PRF_LOCK,      MY_REX, 0fh, 0aeh, MY_RM ; (used in group)
        BS2_TRAP_INSTR X86_XCPT_UD, 0,  db X86_OP_PRF_REPNZ,     MY_REX, 0fh, 0aeh, MY_RM ; (used in group)
        BS2_TRAP_INSTR X86_XCPT_UD, 0,  db X86_OP_PRF_REPZ,      MY_REX, 0fh, 0aeh, MY_RM ; (used in group)
  %assign MY_REX (MY_REX + 1)
 %endrep
%endif
 %assign MY_RM (MY_RM + 1)
%endrep

        ;
        ; Done.
        ;
        call    TMPL_NM_CMN(TestSubDone)
.done:
        mov     xSP, xSI
        add     xSP, 80h
        pop     xSI
        pop     xDI
        pop     xDX
        pop     xCX
        pop     xBX
        pop     sAX
        leave
        ret

.skip:
        mov     xAX, .s_szSse2Missing
        call    TMPL_NM_CMN(TestSubDone)
        jmp     .done

.s_szSubTestName:
        db      TMPL_MODE_STR, ', mfence et al.', 0
.s_szSse2Missing:
        db      'SSE2 is missing', 0
ENDPROC   TMPL_NM(TestMemFences)


;;
; Proving intel manual wrong about using REX.X for BSWAP R8-R15 on 64-bit.
; Checking the 'undefined' 16-bit bswap behavior.
;
; @uses No registers, but BS2_SEL_SPARE0 is trashed.
;
BEGINPROC TMPL_NM(TestBSwap)
        push    xBP
        mov     xBP, xSP
        push    sAX
        push    xBX
        push    xCX
        push    xDX
        push    xDI
        push    xSI
        sub     xSP, 80h                ; iret stack frame space.
        mov     xSI, xSP                ; Save the stack register.

        mov     xAX, .s_szSubTestName
        call    TMPL_NM_CMN(TestSub)

        ;
        ; Assert sanity.
        ;
        mov     eax, 11223344h
        bswap   eax
        TEST_ASSERT_SIMPLE eax, 44332211h, jz, "32-bit BSWAP EAX"

        ;
        ; Buggy manual (325383-041US, December 2011).
        ;
%ifdef TMPL_64BIT
        push    r8

        mov     r8d, 55667788h
        mov     eax, 55667788h
        db X86_OP_REX_X
        bswap   eax                     ; does it access r8 or eax?
        TEST_ASSERT_SIMPLE eax, 88776655h, jz, "REX.X BSWAP EAX - Wrong EAX."
        TEST_ASSERT_SIMPLE r8,  55667788h, jz, "REX.X BSWAP EAX - Wrong R8."

        mov     r8d, 55667788h
        mov     eax, 55667788h
        db X86_OP_REX_R
        bswap   eax                     ; does it access r8 or eax?
        TEST_ASSERT_SIMPLE eax, 88776655h, jz, "REX.R BSWAP EAX - Wrong EAX."
        TEST_ASSERT_SIMPLE r8,  55667788h, jz, "REX.R BSWAP EAX - Wrong R8."

        mov     r8d, 55667788h
        mov     eax, 55667788h
        db X86_OP_REX_B
        bswap   eax                     ; does it access r8 or eax?
        TEST_ASSERT_SIMPLE rax, 55667788h, jz, "REX.B BSWAP R8D - Wrong RAX."
        TEST_ASSERT_SIMPLE r8d, 88776655h, jz, "REX.B BSWAP R8D - Wrong R8D."

        pop     r8
%endif

        ;
        ; 'Undefined' 16-bit behavior.
        ;
        ; Zeroing of the lower 16-bits has been observed on:
        ;       - Intel(R) Core(TM) i7-3960X CPU @ 3.30GHz
        ;
%ifndef TestBSwap16_defined
 %define TestBSwap16_defined
 %macro TestBSwap16 3,
        mov     %3, %2                  ; save the primary register.
  %ifdef TMPL_64BIT
        mov     %2, 0ffffffff98765432h  ; Set the upper bit as well.
  %else
        mov     %2, 98765432h
  %endif
  %ifndef TMPL_16BIT
        db X86_OP_PRF_SIZE_OP
  %endif
        bswap   %1
        xchg    %2, %3                  ; Restore and save the result (xSP).
        TEST_ASSERT_SIMPLE %3, 98760000h, jz, "Unexpected 16-bit BSWAP error."
 %endmacro
%endif

        TestBSwap16 eax, sAX, sSI
        TestBSwap16 ebx, sBX, sSI
        TestBSwap16 ecx, sCX, sSI
        TestBSwap16 edx, sDX, sSI
        TestBSwap16 esp, sSP, sSI
        TestBSwap16 ebp, sBP, sSI
        TestBSwap16 edi, sDI, sSI
        TestBSwap16 esi, sSI, sDI
%ifdef TMPL_64BIT
        TestBSwap16 r8d,   r8, rax
        TestBSwap16 r9d,   r9, rax
        TestBSwap16 r10d, r10, rax
        TestBSwap16 r11d, r11, rax
        TestBSwap16 r12d, r12, rax
        TestBSwap16 r13d, r13, rax
        TestBSwap16 r14d, r14, rax
        TestBSwap16 r15d, r15, rax
%endif

        ;
        ; Done.
        ;
        call    TMPL_NM_CMN(TestSubDone)
.done:
        mov     xSP, xSI
        add     xSP, 80h
        pop     xSI
        pop     xDI
        pop     xDX
        pop     xCX
        pop     xBX
        pop     sAX
        leave
        ret

.s_szSubTestName:
        db      TMPL_MODE_STR, ', bswap', 0
ENDPROC   TMPL_NM(TestBSwap)


;;
; Do the tests for this mode.
;
; @uses nothing
;
BEGINCODELOW
BITS 16
BEGINPROC TMPL_NM(DoTestsForMode_rm)
        push    bp
        mov     bp, sp
        push    ax

        ;
        ; Check if the mode and NX is supported, do the switch.
        ;
        call    TMPL_NM(Bs2IsModeSupported_rm)
        jz      .done
        call    TMPL_NM(Bs2EnterMode_rm)
BITS TMPL_BITS

        ;
        ; Test exception handler basics using INT3 and #BP.
        ;

        call    TMPL_NM(TestMemFences)
        call    TMPL_NM(TestBSwap)

        ;
        ; Back to real mode.
        ;
        call    TMPL_NM(Bs2ExitMode)
BITS 16
        call    Bs2DisableNX_r86

.done:
        pop     ax
        leave
        ret
ENDPROC TMPL_NM(DoTestsForMode_rm)
TMPL_BEGINCODE
BITS TMPL_BITS

%include "bootsector2-template-footer.mac"