summaryrefslogtreecommitdiffstats
path: root/src/VBox/VMM/VMMRC/VMMRCA.asm
blob: ca482f105e047aa6628a7060563ffc0139703297 (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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
; $Id: VMMRCA.asm $
;; @file
; VMMRC - Raw-mode Context Virtual Machine Monitor assembly routines.
;

;
; Copyright (C) 2006-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.
;

;*******************************************************************************
;* Header Files                                                                *
;*******************************************************************************
%include "VBox/asmdefs.mac"
%include "iprt/x86.mac"
%include "VBox/sup.mac"
%include "VBox/vmm/vm.mac"
%include "VMMInternal.mac"
%include "VMMRC.mac"


;*******************************************************************************
;*   Defined Constants And Macros                                              *
;*******************************************************************************
;; save all registers before loading special values for the faulting.
%macro SaveAndLoadAll 0
    pushad
    push    ds
    push    es
    push    fs
    push    gs
    call    NAME(vmmGCTestLoadRegs)
%endmacro

;; restore all registers after faulting.
%macro RestoreAll 0
    pop     gs
    pop     fs
    pop     es
    pop     ds
    popad
%endmacro


;*******************************************************************************
;* External Symbols                                                            *
;*******************************************************************************
extern IMPNAME(g_VM)
extern IMPNAME(g_Logger)
extern IMPNAME(g_RelLogger)
extern NAME(RTLogLogger)
extern NAME(vmmRCProbeFireHelper)
extern NAME(TRPMRCTrapHyperHandlerSetEIP)


BEGINCODE

;/**
; * Internal GC logger worker: Logger wrapper.
; */
;VMMRCDECL(void) vmmGCLoggerWrapper(const char *pszFormat, ...);
EXPORTEDNAME vmmGCLoggerWrapper
%ifdef __YASM__
%ifdef ASM_FORMAT_ELF
    push    dword IMP(g_Logger)         ; YASM BUG #67! YASMCHECK!
%else
    push    IMP(g_Logger)
%endif
%else
    push    IMP(g_Logger)
%endif
    call    NAME(RTLogLogger)
    add     esp, byte 4
    ret
ENDPROC vmmGCLoggerWrapper


;/**
; * Internal GC logger worker: Logger (release) wrapper.
; */
;VMMRCDECL(void) vmmGCRelLoggerWrapper(const char *pszFormat, ...);
EXPORTEDNAME vmmGCRelLoggerWrapper
%ifdef __YASM__
%ifdef ASM_FORMAT_ELF
    push    dword IMP(g_RelLogger)         ; YASM BUG #67! YASMCHECK!
%else
    push    IMP(g_RelLogger)
%endif
%else
    push    IMP(g_RelLogger)
%endif
    call    NAME(RTLogLogger)
    add     esp, byte 4
    ret
ENDPROC vmmGCRelLoggerWrapper


;;
; Enables write protection.
BEGINPROC vmmGCEnableWP
    push    eax
    mov     eax, cr0
    or      eax, X86_CR0_WRITE_PROTECT
    mov     cr0, eax
    pop     eax
    ret
ENDPROC vmmGCEnableWP


;;
; Disables write protection.
BEGINPROC vmmGCDisableWP
    push    eax
    mov     eax, cr0
    and     eax, ~X86_CR0_WRITE_PROTECT
    mov     cr0, eax
    pop     eax
    ret
ENDPROC vmmGCDisableWP


;;
; Load special register set expected upon faults.
; All registers are changed.
BEGINPROC vmmGCTestLoadRegs
    mov     eax, ss
    mov     ds, eax
    mov     es, eax
    mov     fs, eax
    mov     gs, eax
    mov     edi, 001234567h
    mov     esi, 042000042h
    mov     ebp, 0ffeeddcch
    mov     ebx, 089abcdefh
    mov     ecx, 0ffffaaaah
    mov     edx, 077778888h
    mov     eax, 0f0f0f0f0h
    ret
ENDPROC vmmGCTestLoadRegs


;;
; A Trap 3 testcase.
GLOBALNAME vmmGCTestTrap3
    SaveAndLoadAll

    int     3
EXPORTEDNAME vmmGCTestTrap3_FaultEIP

    RestoreAll
    mov     eax, 0ffffffffh
    ret
ENDPROC vmmGCTestTrap3


;;
; A Trap 8 testcase.
GLOBALNAME vmmGCTestTrap8
    SaveAndLoadAll

    sub     esp, byte 8
    sidt    [esp]
    mov     word [esp], 111 ; make any #PF double fault.
    lidt    [esp]
    add     esp, byte 8

    COM_S_CHAR '!'

    xor     eax, eax
EXPORTEDNAME vmmGCTestTrap8_FaultEIP
    mov     eax, [eax]


    COM_S_CHAR '2'

    RestoreAll
    mov     eax, 0ffffffffh
    ret
ENDPROC vmmGCTestTrap8


;;
; A simple Trap 0d testcase.
GLOBALNAME vmmGCTestTrap0d
    SaveAndLoadAll

    push    ds
EXPORTEDNAME vmmGCTestTrap0d_FaultEIP
    ltr     [esp]
    pop     eax

    RestoreAll
    mov     eax, 0ffffffffh
    ret
ENDPROC vmmGCTestTrap0d


;;
; A simple Trap 0e testcase.
GLOBALNAME vmmGCTestTrap0e
    SaveAndLoadAll

    xor     eax, eax
EXPORTEDNAME vmmGCTestTrap0e_FaultEIP
    mov     eax, [eax]

    RestoreAll
    mov     eax, 0ffffffffh
    ret

EXPORTEDNAME vmmGCTestTrap0e_ResumeEIP
    RestoreAll
    xor     eax, eax
    ret
ENDPROC vmmGCTestTrap0e



;;
; Safely reads an MSR.
; @returns  boolean
; @param    uMsr        The MSR to red.
; @param    pu64Value   Where to return the value on success.
;
GLOBALNAME vmmRCSafeMsrRead
    push    ebp
    mov     ebp, esp
    pushf
    cli
    push    esi
    push    edi
    push    ebx
    push    ebp

    mov     ecx, [ebp + 8]              ; The MSR to read.
    mov     eax, 0deadbeefh
    mov     edx, 0deadbeefh

TRPM_GP_HANDLER NAME(TRPMRCTrapHyperHandlerSetEIP), .trapped
    rdmsr

    mov     ecx, [ebp + 0ch]            ; Where to store the result.
    mov     [ecx], eax
    mov     [ecx + 4], edx

    mov     eax, 1
.return:
    pop     ebp
    pop     ebx
    pop     edi
    pop     esi
    popf
    leave
    ret

.trapped:
    mov     eax, 0
    jmp     .return
ENDPROC vmmRCSafeMsrRead


;;
; Safely writes an MSR.
; @returns  boolean
; @param    uMsr        The MSR to red.
; @param    u64Value    The value to write.
;
GLOBALNAME vmmRCSafeMsrWrite
    push    ebp
    mov     ebp, esp
    pushf
    cli
    push    esi
    push    edi
    push    ebx
    push    ebp

    mov     ecx, [ebp + 8]              ; The MSR to write to.
    mov     eax, [ebp + 12]             ; The value to write.
    mov     edx, [ebp + 16]

TRPM_GP_HANDLER NAME(TRPMRCTrapHyperHandlerSetEIP), .trapped
    wrmsr

    mov     eax, 1
.return:
    pop     ebp
    pop     ebx
    pop     edi
    pop     esi
    popf
    leave
    ret

.trapped:
    mov     eax, 0
    jmp     .return
ENDPROC vmmRCSafeMsrWrite



;;
; The raw-mode context equivalent of SUPTracerFireProbe.
;
; See also SUPLibTracerA.asm.
;
EXPORTEDNAME VMMRCProbeFire
        push    ebp
        mov     ebp, esp

        ;
        ; Save edx and eflags so we can use them.
        ;
        pushf
        push    edx

        ;
        ; Get the address of the tracer context record after first checking
        ; that host calls hasn't been disabled.
        ;
        mov     edx, IMP(g_VM)
        add     edx, [edx + VM.offVMCPU]
        cmp     dword [edx + VMCPU.vmm + VMMCPU.cCallRing3Disabled], 0
        jnz     .return
        add     edx, VMCPU.vmm + VMMCPU.TracerCtx

        ;
        ; Save the X86 context.
        ;
        mov     [edx + SUPDRVTRACERUSRCTX32.u.X86.eax], eax
        mov     [edx + SUPDRVTRACERUSRCTX32.u.X86.ecx], ecx
        pop     eax
        mov     [edx + SUPDRVTRACERUSRCTX32.u.X86.edx], eax
        mov     [edx + SUPDRVTRACERUSRCTX32.u.X86.ebx], ebx
        mov     [edx + SUPDRVTRACERUSRCTX32.u.X86.esi], esi
        mov     [edx + SUPDRVTRACERUSRCTX32.u.X86.edi], edi
        pop     eax
        mov     [edx + SUPDRVTRACERUSRCTX32.u.X86.eflags], eax
        mov     eax, [ebp + 4]
        mov     [edx + SUPDRVTRACERUSRCTX32.u.X86.eip], eax
        mov     eax, [ebp]
        mov     [edx + SUPDRVTRACERUSRCTX32.u.X86.ebp], eax
        lea     eax, [ebp + 4*2]
        mov     [edx + SUPDRVTRACERUSRCTX32.u.X86.esp], eax

        mov     ecx, [ebp + 4*2]
        mov     [edx + SUPDRVTRACERUSRCTX32.u.X86.uVtgProbeLoc], ecx

        mov     eax, [ecx + 4]          ; VTGPROBELOC::idProbe.
        mov     [edx + SUPDRVTRACERUSRCTX32.idProbe], eax
        mov     dword [edx + SUPDRVTRACERUSRCTX32.cBits], 32

        ; Copy the arguments off the stack.
%macro COPY_ONE_ARG 1
        mov     eax, [ebp + 12 + %1 * 4]
        mov     [edx + SUPDRVTRACERUSRCTX32.u.X86.aArgs + %1*4], eax
%endmacro
        COPY_ONE_ARG 0
        COPY_ONE_ARG 1
        COPY_ONE_ARG 2
        COPY_ONE_ARG 3
        COPY_ONE_ARG 4
        COPY_ONE_ARG 5
        COPY_ONE_ARG 6
        COPY_ONE_ARG 7
        COPY_ONE_ARG 8
        COPY_ONE_ARG 9
        COPY_ONE_ARG 10
        COPY_ONE_ARG 11
        COPY_ONE_ARG 12
        COPY_ONE_ARG 13
        COPY_ONE_ARG 14
        COPY_ONE_ARG 15
        COPY_ONE_ARG 16
        COPY_ONE_ARG 17
        COPY_ONE_ARG 18
        COPY_ONE_ARG 19

        ;
        ; Call the helper (too lazy to do the VMM structure stuff).
        ;
        mov     ecx, IMP(g_VM)
        push    ecx
        call    NAME(vmmRCProbeFireHelper)

.return:
        leave
        ret
ENDPROC      VMMRCProbeFire