summaryrefslogtreecommitdiffstats
path: root/src/VBox/VMM/VMMR0/VMMR0JmpA-x86.asm
blob: 365bfdcfcaeb56e83b5eeeba20c36816c4a673c3 (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
; $Id: VMMR0JmpA-x86.asm $
;; @file
; VMM - R0 SetJmp / LongJmp routines for X86.
;

;
; 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
;

;*******************************************************************************
;* Header Files                                                                *
;*******************************************************************************
%include "VBox/asmdefs.mac"
%include "VMMInternal.mac"
%include "VBox/err.mac"
%include "VBox/param.mac"


;*******************************************************************************
;*  Defined Constants And Macros                                               *
;*******************************************************************************
%define RESUME_MAGIC    07eadf00dh
%define STACK_PADDING   0eeeeeeeeh



BEGINCODE


;;
; The setjmp variant used for calling Ring-3.
;
; This differs from the normal setjmp in that it will resume VMMRZCallRing3 if we're
; in the middle of a ring-3 call. Another differences is the function pointer and
; argument. This has to do with resuming code and the stack frame of the caller.
;
; @returns  VINF_SUCCESS on success or whatever is passed to vmmR0CallRing3LongJmp.
; @param    pJmpBuf msc:rcx gcc:rdi x86:[esp+0x04]     Our jmp_buf.
; @param    pfn     msc:rdx gcc:rsi x86:[esp+0x08]     The function to be called when not resuming.
; @param    pvUser1 msc:r8  gcc:rdx x86:[esp+0x0c]     The argument of that function.
; @param    pvUser2 msc:r9  gcc:rcx x86:[esp+0x10]     The argument of that function.
;
BEGINPROC vmmR0CallRing3SetJmp
GLOBALNAME vmmR0CallRing3SetJmp2
GLOBALNAME vmmR0CallRing3SetJmpEx
    ;
    ; Save the registers.
    ;
    mov     edx, [esp + 4h]             ; pJmpBuf
    mov     [xDX + VMMR0JMPBUF.ebx], ebx
    mov     [xDX + VMMR0JMPBUF.esi], esi
    mov     [xDX + VMMR0JMPBUF.edi], edi
    mov     [xDX + VMMR0JMPBUF.ebp], ebp
    mov     xAX, [esp]
    mov     [xDX + VMMR0JMPBUF.eip], xAX
    lea     ecx, [esp + 4]              ; (used in resume)
    mov     [xDX + VMMR0JMPBUF.esp], ecx
    pushf
    pop     xAX
    mov     [xDX + VMMR0JMPBUF.eflags], xAX

    ;
    ; If we're not in a ring-3 call, call pfn and return.
    ;
    test    byte [xDX + VMMR0JMPBUF.fInRing3Call], 1
    jnz     .resume

    mov     ebx, edx                    ; pJmpBuf -> ebx (persistent reg)
%ifdef VMM_R0_SWITCH_STACK
    mov     esi, [ebx + VMMR0JMPBUF.pvSavedStack]
    test    esi, esi
    jz      .entry_error
 %ifdef VBOX_STRICT
    cmp     dword [esi], 0h
    jne     .entry_error
    mov     edx, esi
    mov     edi, esi
    mov     ecx, VMM_STACK_SIZE / 4
    mov     eax, STACK_PADDING
    repne stosd
 %endif
    lea     esi, [esi + VMM_STACK_SIZE - 32]
    mov     [esi + 1ch], dword 0deadbeefh ; Marker 1.
    mov     [esi + 18h], ebx            ; Save pJmpBuf pointer.
    mov     [esi + 14h], dword 00c00ffeeh ; Marker 2.
    mov     [esi + 10h], dword 0f00dbeefh ; Marker 3.
    mov     edx, [esp + 10h]            ; pvArg2
    mov     ecx, [esp + 0ch]            ; pvArg1
    mov     eax, [esp + 08h]            ; pfn
 %if 1                                  ; Use this to eat of some extra stack - handy for finding paths using lots of stack.
  %define FRAME_OFFSET 0
 %else
  %define FRAME_OFFSET 1024
 %endif
    mov     [esi - FRAME_OFFSET + 04h], edx
    mov     [esi - FRAME_OFFSET      ], ecx
    lea     esp, [esi - FRAME_OFFSET]   ; Switch stack!
    call    eax
    and     dword [esi + 1ch], byte 0   ; reset marker.

 %ifdef VBOX_STRICT
    ; Calc stack usage and check for overflows.
    mov     edi, [ebx + VMMR0JMPBUF.pvSavedStack]
    cmp     dword [edi], STACK_PADDING  ; Check for obvious stack overflow.
    jne     .stack_overflow
    mov     esi, eax                    ; save eax
    mov     eax, STACK_PADDING
    mov     ecx, VMM_STACK_SIZE / 4
    cld
    repe scasd
    shl     ecx, 2                      ; *4
    cmp     ecx, VMM_STACK_SIZE - 64    ; Less than 64 bytes left -> overflow as well.
    mov     eax, esi                    ; restore eax in case of overflow (esi remains used)
    jae     .stack_overflow_almost

    ; Update stack usage statistics.
    cmp     ecx, [ebx + VMMR0JMPBUF.cbUsedMax] ; New max usage?
    jle     .no_used_max
    mov     [ebx + VMMR0JMPBUF.cbUsedMax], ecx
.no_used_max:
    ; To simplify the average stuff, just historize before we hit div errors.
    inc     dword [ebx + VMMR0JMPBUF.cUsedTotal]
    test    [ebx + VMMR0JMPBUF.cUsedTotal], dword 0c0000000h
    jz      .no_historize
    mov     dword [ebx + VMMR0JMPBUF.cUsedTotal], 2
    mov     edi, [ebx + VMMR0JMPBUF.cbUsedAvg]
    mov     [ebx + VMMR0JMPBUF.cbUsedTotal], edi
    mov     dword [ebx + VMMR0JMPBUF.cbUsedTotal + 4], 0
.no_historize:
    add     [ebx + VMMR0JMPBUF.cbUsedTotal], ecx
    adc     dword [ebx + VMMR0JMPBUF.cbUsedTotal + 4], 0
    mov     eax, [ebx + VMMR0JMPBUF.cbUsedTotal]
    mov     edx, [ebx + VMMR0JMPBUF.cbUsedTotal + 4]
    mov     edi, [ebx + VMMR0JMPBUF.cUsedTotal]
    div     edi
    mov     [ebx + VMMR0JMPBUF.cbUsedAvg], eax

    mov     eax, esi                    ; restore eax (final, esi released)

    mov     edi, [ebx + VMMR0JMPBUF.pvSavedStack]
    mov     dword [edi], 0h             ; Reset the overflow marker.
 %endif ; VBOX_STRICT

%else  ; !VMM_R0_SWITCH_STACK
    mov     ecx, [esp + 0ch]            ; pvArg1
    mov     edx, [esp + 10h]            ; pvArg2
    mov     eax, [esp + 08h]            ; pfn
    sub     esp, 12                     ; align the stack on a 16-byte boundary.
    mov     [esp      ], ecx
    mov     [esp + 04h], edx
    call    eax
%endif ; !VMM_R0_SWITCH_STACK
    mov     edx, ebx                    ; pJmpBuf -> edx (volatile reg)

    ;
    ; Return like in the long jump but clear eip, no short cuts here.
    ;
.proper_return:
    mov     ebx, [xDX + VMMR0JMPBUF.ebx]
    mov     esi, [xDX + VMMR0JMPBUF.esi]
    mov     edi, [xDX + VMMR0JMPBUF.edi]
    mov     ebp, [xDX + VMMR0JMPBUF.ebp]
    mov     xCX, [xDX + VMMR0JMPBUF.eip]
    and     dword [xDX + VMMR0JMPBUF.eip], byte 0 ; used for valid check.
    mov     esp, [xDX + VMMR0JMPBUF.esp]
    push    dword [xDX + VMMR0JMPBUF.eflags]
    popf
    jmp     xCX

.entry_error:
    mov     eax, VERR_VMM_SET_JMP_ERROR
    jmp     .proper_return

.stack_overflow:
    mov     eax, VERR_VMM_SET_JMP_STACK_OVERFLOW
    mov     edx, ebx
    jmp     .proper_return

.stack_overflow_almost:
    mov     eax, VERR_VMM_SET_JMP_STACK_OVERFLOW
    mov     edx, ebx
    jmp     .proper_return

    ;
    ; Aborting resume.
    ;
.bad:
    and     dword [xDX + VMMR0JMPBUF.eip], byte 0 ; used for valid check.
    mov     edi, [xDX + VMMR0JMPBUF.edi]
    mov     esi, [xDX + VMMR0JMPBUF.esi]
    mov     ebx, [xDX + VMMR0JMPBUF.ebx]
    mov     eax, VERR_VMM_SET_JMP_ABORTED_RESUME
    ret

    ;
    ; Resume VMMRZCallRing3 the call.
    ;
.resume:
    ; Sanity checks.
%ifdef VMM_R0_SWITCH_STACK
    mov     eax, [xDX + VMMR0JMPBUF.pvSavedStack]
 %ifdef RT_STRICT
    cmp     dword [eax], STACK_PADDING
 %endif
    lea     eax, [eax + VMM_STACK_SIZE - 32]
    cmp     dword [eax + 1ch], 0deadbeefh       ; Marker 1.
    jne     .bad
 %ifdef RT_STRICT
    cmp     [esi + 18h], edx                    ; The saved pJmpBuf pointer.
    jne     .bad
    cmp     dword [esi + 14h], 00c00ffeeh       ; Marker 2.
    jne     .bad
    cmp     dword [esi + 10h], 0f00dbeefh       ; Marker 3.
    jne     .bad
 %endif
%else  ; !VMM_R0_SWITCH_STACK
    cmp     ecx, [xDX + VMMR0JMPBUF.SpCheck]
    jne     .bad
.espCheck_ok:
    mov     ecx, [xDX + VMMR0JMPBUF.cbSavedStack]
    cmp     ecx, VMM_STACK_SIZE
    ja      .bad
    test    ecx, 3
    jnz     .bad
    mov     edi, [xDX + VMMR0JMPBUF.esp]
    sub     edi, [xDX + VMMR0JMPBUF.SpResume]
    cmp     ecx, edi
    jne     .bad
%endif

%ifdef VMM_R0_SWITCH_STACK
    ; Switch stack.
    mov     esp, [xDX + VMMR0JMPBUF.SpResume]
%else
    ; Restore the stack.
    mov     ecx, [xDX + VMMR0JMPBUF.cbSavedStack]
    shr     ecx, 2
    mov     esi, [xDX + VMMR0JMPBUF.pvSavedStack]
    mov     edi, [xDX + VMMR0JMPBUF.SpResume]
    mov     esp, edi
    rep movsd
%endif ; !VMM_R0_SWITCH_STACK
    mov     byte [xDX + VMMR0JMPBUF.fInRing3Call], 0

    ;
    ; Continue where we left off.
    ;
%ifdef VBOX_STRICT
    pop     eax                         ; magic
    cmp     eax, RESUME_MAGIC
    je      .magic_ok
    mov     ecx, 0123h
    mov     [ecx], edx
.magic_ok:
%endif
    popf
    pop     ebx
    pop     esi
    pop     edi
    pop     ebp
    xor     eax, eax                    ; VINF_SUCCESS
    ret
ENDPROC vmmR0CallRing3SetJmp


;;
; Worker for VMMRZCallRing3.
; This will save the stack and registers.
;
; @param    pJmpBuf msc:rcx gcc:rdi x86:[ebp+8]     Pointer to the jump buffer.
; @param    rc      msc:rdx gcc:rsi x86:[ebp+c]     The return code.
;
BEGINPROC vmmR0CallRing3LongJmp
    ;
    ; Save the registers on the stack.
    ;
    push    ebp
    mov     ebp, esp
    push    edi
    push    esi
    push    ebx
    pushf
%ifdef VBOX_STRICT
    push    RESUME_MAGIC
%endif

    ;
    ; Load parameters.
    ;
    mov     edx, [ebp + 08h]            ; pJmpBuf
    mov     eax, [ebp + 0ch]            ; rc

    ;
    ; Is the jump buffer armed?
    ;
    cmp     dword [xDX + VMMR0JMPBUF.eip], byte 0
    je      .nok

    ;
    ; Sanity checks.
    ;
    mov     edi, [xDX + VMMR0JMPBUF.pvSavedStack]
    test    edi, edi                    ; darwin may set this to 0.
    jz      .nok
    mov     [xDX + VMMR0JMPBUF.SpResume], esp
%ifndef VMM_R0_SWITCH_STACK
    mov     esi, esp
    mov     ecx, [xDX + VMMR0JMPBUF.esp]
    sub     ecx, esi

    ; two sanity checks on the size.
    cmp     ecx, VMM_STACK_SIZE         ; check max size.
    jnbe    .nok

    ;
    ; Copy the stack.
    ;
    test    ecx, 3                      ; check alignment
    jnz     .nok
    mov     [xDX + VMMR0JMPBUF.cbSavedStack], ecx
    shr     ecx, 2
    rep movsd
%endif ; !VMM_R0_SWITCH_STACK

    ; Save a PC here to assist unwinding.
.unwind_point:
    mov     dword [xDX + VMMR0JMPBUF.SavedEipForUnwind], .unwind_point
    mov     ecx, [xDX + VMMR0JMPBUF.ebp]
    lea     ecx, [ecx + 4]
    mov     [xDX + VMMR0JMPBUF.UnwindRetPcLocation], ecx

    ; Save ESP & EBP to enable stack dumps
    mov     ecx, ebp
    mov     [xDX + VMMR0JMPBUF.SavedEbp], ecx
    sub     ecx, 4
    mov     [xDX + VMMR0JMPBUF.SavedEsp], ecx

    ; store the last pieces of info.
    mov     ecx, [xDX + VMMR0JMPBUF.esp]
    mov     [xDX + VMMR0JMPBUF.SpCheck], ecx
    mov     byte [xDX + VMMR0JMPBUF.fInRing3Call], 1

    ;
    ; Do the long jump.
    ;
    mov     ebx, [xDX + VMMR0JMPBUF.ebx]
    mov     esi, [xDX + VMMR0JMPBUF.esi]
    mov     edi, [xDX + VMMR0JMPBUF.edi]
    mov     ebp, [xDX + VMMR0JMPBUF.ebp]
    mov     ecx, [xDX + VMMR0JMPBUF.eip]
    mov     [xDX + VMMR0JMPBUF.UnwindRetPcValue], ecx
    mov     esp, [xDX + VMMR0JMPBUF.esp]
    push    dword [xDX + VMMR0JMPBUF.eflags]
    popf
    jmp     ecx

    ;
    ; Failure
    ;
.nok:
%ifdef VBOX_STRICT
    pop     eax                         ; magic
    cmp     eax, RESUME_MAGIC
    je      .magic_ok
    mov     ecx, 0123h
    mov     [ecx], edx
.magic_ok:
%endif
    popf
    pop     ebx
    pop     esi
    pop     edi
    mov     eax, VERR_VMM_LONG_JMP_ERROR
    leave
    ret
ENDPROC vmmR0CallRing3LongJmp