VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMSwitcher/PAEand32Bit.mac@ 41935

Last change on this file since 41935 was 41933, checked in by vboxsync, 12 years ago

VMMSwitcher: Drop the unused assembly switcher functions taking guest or host contexts as arguments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 28.5 KB
Line 
1; $Id: PAEand32Bit.mac 41933 2012-06-27 18:37:33Z vboxsync $
2;; @file
3; VMM - World Switchers, template for PAE and 32-Bit.
4;
5
6;
7; Copyright (C) 2006-2012 Oracle Corporation
8;
9; This file is part of VirtualBox Open Source Edition (OSE), as
10; available from http://www.virtualbox.org. This file is free software;
11; you can redistribute it and/or modify it under the terms of the GNU
12; General Public License (GPL) as published by the Free Software
13; Foundation, in version 2 as it comes in the "COPYING" file of the
14; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16;
17
18;%define DEBUG_STUFF 1
19
20;*******************************************************************************
21;* Header Files *
22;*******************************************************************************
23%include "VBox/asmdefs.mac"
24%include "VBox/apic.mac"
25%include "iprt/x86.mac"
26%include "VBox/vmm/cpum.mac"
27%include "VBox/vmm/stam.mac"
28%include "VBox/vmm/vm.mac"
29%include "CPUMInternal.mac"
30%include "VMMSwitcher.mac"
31
32%undef NEED_ID
33%ifdef NEED_PAE_ON_32BIT_HOST
34%define NEED_ID
35%endif
36%ifdef NEED_32BIT_ON_PAE_HOST
37%define NEED_ID
38%endif
39
40
41
42;
43; Start the fixup records
44; We collect the fixups in the .data section as we go along
45; It is therefore VITAL that no-one is using the .data section
46; for anything else between 'Start' and 'End'.
47;
48BEGINDATA
49GLOBALNAME Fixups
50
51
52
53BEGINCODE
54GLOBALNAME Start
55
56;;
57; The C interface.
58;
59BEGINPROC vmmR0HostToGuest
60
61%ifdef DEBUG_STUFF
62 COM_S_NEWLINE
63 COM_S_CHAR '^'
64%endif
65
66%ifdef VBOX_WITH_STATISTICS
67 ;
68 ; Switcher stats.
69 ;
70 FIXUP FIX_HC_VM_OFF, 1, VM.StatSwitcherToGC
71 mov edx, 0ffffffffh
72 STAM_PROFILE_ADV_START edx
73%endif
74
75 ;
76 ; Call worker.
77 ;
78 FIXUP FIX_HC_CPUM_OFF, 1, 0
79 mov edx, 0ffffffffh
80 push cs ; allow for far return and restore cs correctly.
81 call NAME(vmmR0HostToGuestAsm)
82
83%ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
84 CPUM_FROM_CPUMCPU(edx)
85 ; Restore blocked Local APIC NMI vectors
86 ; Do this here to ensure the host CS is already restored
87 mov ecx, [edx + CPUM.fApicDisVectors]
88 mov edx, [edx + CPUM.pvApicBase]
89 shr ecx, 1
90 jnc gth_nolint0
91 and dword [edx + APIC_REG_LVT_LINT0], ~APIC_REG_LVT_MASKED
92gth_nolint0:
93 shr ecx, 1
94 jnc gth_nolint1
95 and dword [edx + APIC_REG_LVT_LINT1], ~APIC_REG_LVT_MASKED
96gth_nolint1:
97 shr ecx, 1
98 jnc gth_nopc
99 and dword [edx + APIC_REG_LVT_PC], ~APIC_REG_LVT_MASKED
100gth_nopc:
101 shr ecx, 1
102 jnc gth_notherm
103 and dword [edx + APIC_REG_LVT_THMR], ~APIC_REG_LVT_MASKED
104gth_notherm:
105%endif
106
107%ifdef VBOX_WITH_STATISTICS
108 ;
109 ; Switcher stats.
110 ;
111 FIXUP FIX_HC_VM_OFF, 1, VM.StatSwitcherToHC
112 mov edx, 0ffffffffh
113 STAM_PROFILE_ADV_STOP edx
114%endif
115
116 ret
117ENDPROC vmmR0HostToGuest
118
119
120
121; *****************************************************************************
122; vmmR0HostToGuestAsm
123;
124; Phase one of the switch from host to guest context (host MMU context)
125;
126; INPUT:
127; - edx virtual address of CPUM structure (valid in host context)
128;
129; USES/DESTROYS:
130; - eax, ecx, edx
131;
132; ASSUMPTION:
133; - current CS and DS selectors are wide open
134;
135; *****************************************************************************
136ALIGNCODE(16)
137BEGINPROC vmmR0HostToGuestAsm
138 ;;
139 ;; Save CPU host context
140 ;; Skip eax, edx and ecx as these are not preserved over calls.
141 ;;
142 CPUMCPU_FROM_CPUM(edx)
143 ; general registers.
144 mov [edx + CPUMCPU.Host.ebx], ebx
145 mov [edx + CPUMCPU.Host.edi], edi
146 mov [edx + CPUMCPU.Host.esi], esi
147 mov [edx + CPUMCPU.Host.esp], esp
148 mov [edx + CPUMCPU.Host.ebp], ebp
149 ; selectors.
150 mov [edx + CPUMCPU.Host.ds], ds
151 mov [edx + CPUMCPU.Host.es], es
152 mov [edx + CPUMCPU.Host.fs], fs
153 mov [edx + CPUMCPU.Host.gs], gs
154 mov [edx + CPUMCPU.Host.ss], ss
155 ; special registers.
156 sldt [edx + CPUMCPU.Host.ldtr]
157 sidt [edx + CPUMCPU.Host.idtr]
158 sgdt [edx + CPUMCPU.Host.gdtr]
159 str [edx + CPUMCPU.Host.tr]
160 ; flags
161 pushfd
162 pop dword [edx + CPUMCPU.Host.eflags]
163
164 ; Block Local APIC NMI vectors
165 xor edi, edi
166
167%ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
168 mov esi, edx
169 CPUM_FROM_CPUMCPU(edx)
170 mov ebx, [edx + CPUM.pvApicBase]
171 or ebx, ebx
172 jz htg_noapic
173 mov eax, [ebx + APIC_REG_LVT_LINT0]
174 mov ecx, eax
175 and ecx, (APIC_REG_LVT_MASKED | APIC_REG_LVT_MODE_MASK)
176 cmp ecx, APIC_REG_LVT_MODE_NMI
177 jne htg_nolint0
178 or edi, 0x01
179 or eax, APIC_REG_LVT_MASKED
180 mov [ebx + APIC_REG_LVT_LINT0], eax
181 mov eax, [ebx + APIC_REG_LVT_LINT0] ; write completion
182htg_nolint0:
183 mov eax, [ebx + APIC_REG_LVT_LINT1]
184 mov ecx, eax
185 and ecx, (APIC_REG_LVT_MASKED | APIC_REG_LVT_MODE_MASK)
186 cmp ecx, APIC_REG_LVT_MODE_NMI
187 jne htg_nolint1
188 or edi, 0x02
189 or eax, APIC_REG_LVT_MASKED
190 mov [ebx + APIC_REG_LVT_LINT1], eax
191 mov eax, [ebx + APIC_REG_LVT_LINT1] ; write completion
192htg_nolint1:
193 mov eax, [ebx + APIC_REG_LVT_PC]
194 mov ecx, eax
195 and ecx, (APIC_REG_LVT_MASKED | APIC_REG_LVT_MODE_MASK)
196 cmp ecx, APIC_REG_LVT_MODE_NMI
197 jne htg_nopc
198 or edi, 0x04
199 or eax, APIC_REG_LVT_MASKED
200 mov [ebx + APIC_REG_LVT_PC], eax
201 mov eax, [ebx + APIC_REG_LVT_PC] ; write completion
202htg_nopc:
203 mov eax, [ebx + APIC_REG_VERSION]
204 shr eax, 16
205 cmp al, 5
206 jb htg_notherm
207 mov eax, [ebx + APIC_REG_LVT_THMR]
208 mov ecx, eax
209 and ecx, (APIC_REG_LVT_MASKED | APIC_REG_LVT_MODE_MASK)
210 cmp ecx, APIC_REG_LVT_MODE_NMI
211 jne htg_notherm
212 or edi, 0x08
213 or eax, APIC_REG_LVT_MASKED
214 mov [ebx + APIC_REG_LVT_THMR], eax
215 mov eax, [ebx + APIC_REG_LVT_THMR] ; write completion
216htg_notherm:
217 mov [edx + CPUM.fApicDisVectors], edi
218htg_noapic:
219 mov edx, esi
220%endif
221
222 FIXUP FIX_NO_SYSENTER_JMP, 0, htg_no_sysenter - NAME(Start) ; this will insert a jmp htg_no_sysenter if host doesn't use sysenter.
223 ; save MSR_IA32_SYSENTER_CS register.
224 mov ecx, MSR_IA32_SYSENTER_CS
225 mov ebx, edx ; save edx
226 rdmsr ; edx:eax <- MSR[ecx]
227 mov [ebx + CPUMCPU.Host.SysEnter.cs], eax
228 mov [ebx + CPUMCPU.Host.SysEnter.cs + 4], edx
229 xor eax, eax ; load 0:0 to cause #GP upon sysenter
230 xor edx, edx
231 wrmsr
232 xchg ebx, edx ; restore edx
233 jmp short htg_no_sysenter
234
235ALIGNCODE(16)
236htg_no_sysenter:
237
238 FIXUP FIX_NO_SYSCALL_JMP, 0, htg_no_syscall - NAME(Start) ; this will insert a jmp htg_no_syscall if host doesn't use syscall.
239 ; clear MSR_K6_EFER_SCE.
240 mov ebx, edx ; save edx
241 mov ecx, MSR_K6_EFER
242 rdmsr ; edx:eax <- MSR[ecx]
243 and eax, ~MSR_K6_EFER_SCE
244 wrmsr
245 mov edx, ebx ; restore edx
246 jmp short htg_no_syscall
247
248ALIGNCODE(16)
249htg_no_syscall:
250
251 ;; handle use flags.
252 mov esi, [edx + CPUMCPU.fUseFlags] ; esi == use flags.
253 and esi, ~CPUM_USED_FPU ; Clear CPUM_USED_* flags. ;;@todo FPU check can be optimized to use cr0 flags!
254 mov [edx + CPUMCPU.fUseFlags], esi
255
256 ; debug registers.
257 test esi, CPUM_USE_DEBUG_REGS | CPUM_USE_DEBUG_REGS_HOST
258 jz htg_debug_regs_no
259 jmp htg_debug_regs_save_dr7and6
260htg_debug_regs_no:
261
262 ; control registers.
263 mov eax, cr0
264 mov [edx + CPUMCPU.Host.cr0], eax
265 ;mov eax, cr2 ; assume host os don't suff things in cr2. (safe)
266 ;mov [edx + CPUMCPU.Host.cr2], eax
267 mov eax, cr3
268 mov [edx + CPUMCPU.Host.cr3], eax
269 mov eax, cr4
270 mov [edx + CPUMCPU.Host.cr4], eax
271
272 ;;
273 ;; Start switching to VMM context.
274 ;;
275
276 ;
277 ; Change CR0 and CR4 so we can correctly emulate FPU/MMX/SSE[23] exceptions
278 ; Also disable WP. (eax==cr4 now)
279 ; Note! X86_CR4_PSE and X86_CR4_PAE are important if the host thinks so :-)
280 ; Note! X86_CR4_VMXE must not be touched in case the CPU is in vmx root mode
281 ;
282 and eax, X86_CR4_MCE | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_VMXE
283 mov ecx, [edx + CPUMCPU.Guest.cr4]
284 ;; @todo Switcher cleanup: Determine base CR4 during CPUMR0Init / VMMR3SelectSwitcher putting it
285 ; in CPUMCPU.Hyper.cr4 (which isn't currently being used). That should
286 ; simplify this operation a bit (and improve locality of the data).
287
288 ;
289 ; CR4.AndMask and CR4.OrMask are set in CPUMR3Init based on the presence of
290 ; FXSAVE support on the host CPU
291 ;
292 CPUM_FROM_CPUMCPU(edx)
293 and ecx, [edx + CPUM.CR4.AndMask]
294 or eax, ecx
295 or eax, [edx + CPUM.CR4.OrMask]
296 mov cr4, eax
297
298 CPUMCPU_FROM_CPUM(edx)
299 mov eax, [edx + CPUMCPU.Guest.cr0]
300 and eax, X86_CR0_EM
301 or eax, X86_CR0_PE | X86_CR0_PG | X86_CR0_TS | X86_CR0_ET | X86_CR0_NE | X86_CR0_MP
302 mov cr0, eax
303
304 ; Load new gdt so we can do far jump to guest code after cr3 reload.
305 lgdt [edx + CPUMCPU.Hyper.gdtr]
306 DEBUG_CHAR('1') ; trashes esi
307
308 ; Store the hypervisor cr3 for later loading
309 mov ebp, [edx + CPUMCPU.Hyper.cr3]
310
311 ;;
312 ;; Load Intermediate memory context.
313 ;;
314 FIXUP SWITCHER_FIX_INTER_CR3_HC, 1
315 mov eax, 0ffffffffh
316 mov cr3, eax
317 DEBUG_CHAR('2') ; trashes esi
318
319%ifdef NEED_ID
320 ;;
321 ;; Jump to identity mapped location
322 ;;
323 FIXUP FIX_HC_2_ID_NEAR_REL, 1, NAME(IDEnterTarget) - NAME(Start)
324 jmp near NAME(IDEnterTarget)
325
326 ; We're now on identity mapped pages!
327ALIGNCODE(16)
328GLOBALNAME IDEnterTarget
329 DEBUG_CHAR('3')
330 mov edx, cr4
331%ifdef NEED_PAE_ON_32BIT_HOST
332 or edx, X86_CR4_PAE
333%else
334 and edx, ~X86_CR4_PAE
335%endif
336 mov eax, cr0
337 and eax, (~X86_CR0_PG) & 0xffffffff ; prevent yasm warning
338 mov cr0, eax
339 DEBUG_CHAR('4')
340 mov cr4, edx
341 FIXUP SWITCHER_FIX_INTER_CR3_GC, 1
342 mov edx, 0ffffffffh
343 mov cr3, edx
344 or eax, X86_CR0_PG
345 DEBUG_CHAR('5')
346 mov cr0, eax
347 DEBUG_CHAR('6')
348%endif
349
350 ;;
351 ;; Jump to guest code mapping of the code and load the Hypervisor CS.
352 ;;
353 FIXUP FIX_GC_FAR32, 1, NAME(FarJmpGCTarget) - NAME(Start)
354 jmp 0fff8h:0deadfaceh
355
356
357 ;;
358 ;; When we arrive at this label we're at the
359 ;; guest code mapping of the switching code.
360 ;;
361ALIGNCODE(16)
362GLOBALNAME FarJmpGCTarget
363 DEBUG_CHAR('-')
364 ; load final cr3 and do far jump to load cs.
365 mov cr3, ebp ; ebp set above
366 DEBUG_CHAR('0')
367
368 ;;
369 ;; We're in VMM MMU context and VMM CS is loaded.
370 ;; Setup the rest of the VMM state.
371 ;;
372 FIXUP FIX_GC_CPUMCPU_OFF, 1, 0
373 mov edx, 0ffffffffh
374 ; Activate guest IDT
375 DEBUG_CHAR('1')
376 lidt [edx + CPUMCPU.Hyper.idtr]
377 ; Load selectors
378 DEBUG_CHAR('2')
379 FIXUP FIX_HYPER_DS, 1
380 mov eax, 0ffffh
381 mov ds, eax
382 mov es, eax
383 xor eax, eax
384 mov gs, eax
385 mov fs, eax
386
387 ; Setup stack.
388 DEBUG_CHAR('3')
389 mov eax, [edx + CPUMCPU.Hyper.ss.Sel]
390 mov ss, ax
391 mov esp, [edx + CPUMCPU.Hyper.esp]
392
393 ; Restore TSS selector; must mark it as not busy before using ltr (!)
394 DEBUG_CHAR('4')
395 FIXUP FIX_GC_TSS_GDTE_DW2, 2
396 and dword [0ffffffffh], ~0200h ; clear busy flag (2nd type2 bit)
397 DEBUG_CHAR('5')
398 ltr word [edx + CPUMCPU.Hyper.tr.Sel]
399 DEBUG_CHAR('6')
400
401 ; Activate the ldt (now we can safely crash).
402 lldt [edx + CPUMCPU.Hyper.ldtr.Sel]
403 DEBUG_CHAR('7')
404
405 ;; use flags.
406 mov esi, [edx + CPUMCPU.fUseFlags]
407
408 ; debug registers
409 test esi, CPUM_USE_DEBUG_REGS
410 jz htg_debug_regs_guest_no
411 jmp htg_debug_regs_guest
412htg_debug_regs_guest_no:
413 DEBUG_CHAR('9')
414
415%ifdef VBOX_WITH_NMI
416 ;
417 ; Setup K7 NMI.
418 ;
419 mov esi, edx
420 ; clear all PerfEvtSeln registers
421 xor eax, eax
422 xor edx, edx
423 mov ecx, MSR_K7_PERFCTR0
424 wrmsr
425 mov ecx, MSR_K7_PERFCTR1
426 wrmsr
427 mov ecx, MSR_K7_PERFCTR2
428 wrmsr
429 mov ecx, MSR_K7_PERFCTR3
430 wrmsr
431
432 mov eax, RT_BIT(20) | RT_BIT(17) | RT_BIT(16) | 076h
433 mov ecx, MSR_K7_EVNTSEL0
434 wrmsr
435 mov eax, 02329B000h
436 mov edx, 0fffffffeh ; -1.6GHz * 5
437 mov ecx, MSR_K7_PERFCTR0
438 wrmsr
439
440 FIXUP FIX_GC_APIC_BASE_32BIT, 1
441 mov eax, 0f0f0f0f0h
442 add eax, 0340h ; APIC_LVTPC
443 mov dword [eax], 0400h ; APIC_DM_NMI
444
445 xor edx, edx
446 mov eax, RT_BIT(20) | RT_BIT(17) | RT_BIT(16) | 076h | RT_BIT(22) ;+EN
447 mov ecx, MSR_K7_EVNTSEL0
448 wrmsr
449
450 mov edx, esi
451%endif
452
453 ; General registers.
454 mov ebx, [edx + CPUMCPU.Hyper.ebx]
455 mov ebp, [edx + CPUMCPU.Hyper.ebp]
456 mov esi, [edx + CPUMCPU.Hyper.esi]
457 mov edi, [edx + CPUMCPU.Hyper.edi]
458 push dword [edx + CPUMCPU.Hyper.eflags]
459 popfd
460 DEBUG_CHAR('!')
461
462 ;;
463 ;; Return to the VMM code which either called the switcher or
464 ;; the code set up to run by HC.
465 ;;
466%ifdef DEBUG_STUFF
467 COM_S_PRINT ';eip='
468 mov eax, [edx + CPUMCPU.Hyper.eip]
469 COM_S_DWORD_REG eax
470 COM_S_CHAR ';'
471%endif
472 mov eax, [edx + CPUMCPU.Hyper.eip]
473 ; callees expect CPUM ptr
474 CPUM_FROM_CPUMCPU(edx)
475
476%ifdef VBOX_WITH_STATISTICS
477 FIXUP FIX_GC_VM_OFF, 1, VM.StatSwitcherToGC
478 mov edx, 0ffffffffh
479 STAM_PROFILE_ADV_STOP edx
480 FIXUP FIX_GC_CPUM_OFF, 1, 0
481 mov edx, 0ffffffffh
482%endif
483 jmp eax
484
485;;
486; Detour for saving the host DR7 and DR6.
487; esi and edx must be preserved.
488htg_debug_regs_save_dr7and6:
489DEBUG_S_CHAR('s');
490 mov eax, dr7 ; not sure, but if I read the docs right this will trap if GD is set. FIXME!!!
491 mov [edx + CPUMCPU.Host.dr7], eax
492 xor eax, eax ; clear everything. (bit 12? is read as 1...)
493 mov dr7, eax
494 mov eax, dr6 ; just in case we save the state register too.
495 mov [edx + CPUMCPU.Host.dr6], eax
496 jmp htg_debug_regs_no
497
498;;
499; Detour for saving host DR0-3 and loading hypervisor debug registers.
500; esi and edx must be preserved.
501htg_debug_regs_guest:
502 DEBUG_S_CHAR('D')
503 DEBUG_S_CHAR('R')
504 DEBUG_S_CHAR('x')
505 ; save host DR0-3.
506 mov eax, dr0
507 mov [edx + CPUMCPU.Host.dr0], eax
508 mov ebx, dr1
509 mov [edx + CPUMCPU.Host.dr1], ebx
510 mov ecx, dr2
511 mov [edx + CPUMCPU.Host.dr2], ecx
512 mov eax, dr3
513 mov [edx + CPUMCPU.Host.dr3], eax
514
515 ; load hyper DR0-7
516 mov ebx, [edx + CPUMCPU.Hyper.dr]
517 mov dr0, ebx
518 mov ecx, [edx + CPUMCPU.Hyper.dr + 8*1]
519 mov dr1, ecx
520 mov eax, [edx + CPUMCPU.Hyper.dr + 8*2]
521 mov dr2, eax
522 mov ebx, [edx + CPUMCPU.Hyper.dr + 8*3]
523 mov dr3, ebx
524 ;mov eax, [edx + CPUMCPU.Hyper.dr + 8*6]
525 mov ecx, 0ffff0ff0h
526 mov dr6, ecx
527 mov eax, [edx + CPUMCPU.Hyper.dr + 8*7]
528 mov dr7, eax
529 jmp htg_debug_regs_guest_no
530
531ENDPROC vmmR0HostToGuestAsm
532
533
534;;
535; Trampoline for doing a call when starting the hyper visor execution.
536;
537; Push any arguments to the routine.
538; Push the argument frame size (cArg * 4).
539; Push the call target (_cdecl convention).
540; Push the address of this routine.
541;
542;
543ALIGNCODE(16)
544BEGINPROC vmmGCCallTrampoline
545%ifdef DEBUG_STUFF
546 COM_S_CHAR 'c'
547 COM_S_CHAR 't'
548 COM_S_CHAR '!'
549%endif
550
551 ; call routine
552 pop eax ; call address
553 mov esi, edx ; save edx
554 pop edi ; argument count.
555%ifdef DEBUG_STUFF
556 COM_S_PRINT ';eax='
557 COM_S_DWORD_REG eax
558 COM_S_CHAR ';'
559%endif
560 call eax ; do call
561 add esp, edi ; cleanup stack
562
563 ; return to the host context.
564 push byte 0 ; eip
565 mov edx, esi ; CPUM pointer
566
567%ifdef DEBUG_STUFF
568 COM_S_CHAR '`'
569%endif
570 jmp NAME(VMMGCGuestToHostAsm) ; eax = returncode.
571ENDPROC vmmGCCallTrampoline
572
573
574
575;;
576; The C interface.
577;
578ALIGNCODE(16)
579BEGINPROC vmmGCGuestToHost
580%ifdef DEBUG_STUFF
581 push esi
582 COM_NEWLINE
583 DEBUG_CHAR('b')
584 DEBUG_CHAR('a')
585 DEBUG_CHAR('c')
586 DEBUG_CHAR('k')
587 DEBUG_CHAR('!')
588 COM_NEWLINE
589 pop esi
590%endif
591 mov eax, [esp + 4]
592 jmp NAME(VMMGCGuestToHostAsm)
593ENDPROC vmmGCGuestToHost
594
595
596;;
597; VMMGCGuestToHostAsm
598;
599; This is an alternative entry point which we'll be using
600; when the we have saved the guest state already or we haven't
601; been messing with the guest at all.
602;
603; @param eax Return code.
604; @uses eax, edx, ecx (or it may use them in the future)
605;
606ALIGNCODE(16)
607BEGINPROC VMMGCGuestToHostAsm
608 DEBUG_CHAR('%')
609
610%ifdef VBOX_WITH_STATISTICS
611 FIXUP FIX_GC_VM_OFF, 1, VM.StatTotalInGC
612 mov edx, 0ffffffffh
613 STAM_PROFILE_ADV_STOP edx
614
615 FIXUP FIX_GC_VM_OFF, 1, VM.StatTotalGCToQemu
616 mov edx, 0ffffffffh
617 STAM_PROFILE_ADV_START edx
618
619 FIXUP FIX_GC_VM_OFF, 1, VM.StatSwitcherToHC
620 mov edx, 0ffffffffh
621 STAM_PROFILE_ADV_START edx
622%endif
623
624 ;
625 ; Load the CPUMCPU pointer.
626 ;
627 FIXUP FIX_GC_CPUMCPU_OFF, 1, 0
628 mov edx, 0ffffffffh
629
630 pop dword [edx + CPUMCPU.Hyper.eip] ; call return from stack
631
632 ; general registers which we care about.
633 mov dword [edx + CPUMCPU.Hyper.ebx], ebx
634 mov dword [edx + CPUMCPU.Hyper.esi], esi
635 mov dword [edx + CPUMCPU.Hyper.edi], edi
636 mov dword [edx + CPUMCPU.Hyper.ebp], ebp
637 mov dword [edx + CPUMCPU.Hyper.esp], esp
638
639 ; special registers which may change.
640 ; str [edx + CPUMCPU.Hyper.tr] - double fault only, and it won't be right then either.
641 sldt [edx + CPUMCPU.Hyper.ldtr.Sel]
642
643 ; No need to save CRx here. They are set dynamically according to Guest/Host requirements.
644 ; FPU context is saved before restore of host saving (another) branch.
645
646%ifdef VBOX_WITH_NMI
647 ;
648 ; Disarm K7 NMI.
649 ;
650 mov esi, edx
651 mov edi, eax
652
653 xor edx, edx
654 xor eax, eax
655 mov ecx, MSR_K7_EVNTSEL0
656 wrmsr
657
658 mov eax, edi
659 mov edx, esi
660%endif
661
662
663 ;;
664 ;; Load Intermediate memory context.
665 ;;
666 mov edi, eax ; save return code in EDI (careful with COM_DWORD_REG from here on!)
667 mov ecx, [edx + CPUMCPU.Host.cr3]
668 FIXUP SWITCHER_FIX_INTER_CR3_GC, 1
669 mov eax, 0ffffffffh
670 mov cr3, eax
671 DEBUG_CHAR('?')
672
673 ;; We're now in intermediate memory context!
674%ifdef NEED_ID
675 ;;
676 ;; Jump to identity mapped location
677 ;;
678 FIXUP FIX_GC_2_ID_NEAR_REL, 1, NAME(IDExitTarget) - NAME(Start)
679 jmp near NAME(IDExitTarget)
680
681 ; We're now on identity mapped pages!
682ALIGNCODE(16)
683GLOBALNAME IDExitTarget
684 DEBUG_CHAR('1')
685 mov edx, cr4
686%ifdef NEED_PAE_ON_32BIT_HOST
687 and edx, ~X86_CR4_PAE
688%else
689 or edx, X86_CR4_PAE
690%endif
691 mov eax, cr0
692 and eax, (~X86_CR0_PG) & 0xffffffff ; prevent yasm warning
693 mov cr0, eax
694 DEBUG_CHAR('2')
695 mov cr4, edx
696 FIXUP SWITCHER_FIX_INTER_CR3_HC, 1
697 mov edx, 0ffffffffh
698 mov cr3, edx
699 or eax, X86_CR0_PG
700 DEBUG_CHAR('3')
701 mov cr0, eax
702 DEBUG_CHAR('4')
703
704 ;;
705 ;; Jump to HC mapping.
706 ;;
707 FIXUP FIX_ID_2_HC_NEAR_REL, 1, NAME(HCExitTarget) - NAME(Start)
708 jmp near NAME(HCExitTarget)
709%else
710 ;;
711 ;; Jump to HC mapping.
712 ;;
713 FIXUP FIX_GC_2_HC_NEAR_REL, 1, NAME(HCExitTarget) - NAME(Start)
714 jmp near NAME(HCExitTarget)
715%endif
716
717
718 ;
719 ; When we arrive here we're at the host context
720 ; mapping of the switcher code.
721 ;
722ALIGNCODE(16)
723GLOBALNAME HCExitTarget
724 DEBUG_CHAR('9')
725 ; load final cr3
726 mov cr3, ecx
727 DEBUG_CHAR('@')
728
729
730 ;;
731 ;; Restore Host context.
732 ;;
733 ; Load CPUM pointer into edx
734 FIXUP FIX_HC_CPUM_OFF, 1, 0
735 mov edx, 0ffffffffh
736 CPUMCPU_FROM_CPUM(edx)
737 ; activate host gdt and idt
738 lgdt [edx + CPUMCPU.Host.gdtr]
739 DEBUG_CHAR('0')
740 lidt [edx + CPUMCPU.Host.idtr]
741 DEBUG_CHAR('1')
742 ; Restore TSS selector; must mark it as not busy before using ltr (!)
743%if 1 ; ASSUME that this is supposed to be 'BUSY'. (saves 20-30 ticks on the T42p)
744 movzx eax, word [edx + CPUMCPU.Host.tr] ; eax <- TR
745 and al, 0F8h ; mask away TI and RPL bits, get descriptor offset.
746 add eax, [edx + CPUMCPU.Host.gdtr + 2] ; eax <- GDTR.address + descriptor offset.
747 and dword [eax + 4], ~0200h ; clear busy flag (2nd type2 bit)
748 ltr word [edx + CPUMCPU.Host.tr]
749%else
750 movzx eax, word [edx + CPUMCPU.Host.tr] ; eax <- TR
751 and al, 0F8h ; mask away TI and RPL bits, get descriptor offset.
752 add eax, [edx + CPUMCPU.Host.gdtr + 2] ; eax <- GDTR.address + descriptor offset.
753 mov ecx, [eax + 4] ; ecx <- 2nd descriptor dword
754 mov ebx, ecx ; save original value
755 and ecx, ~0200h ; clear busy flag (2nd type2 bit)
756 mov [eax + 4], ecx ; not using xchg here is paranoia..
757 ltr word [edx + CPUMCPU.Host.tr]
758 xchg [eax + 4], ebx ; using xchg is paranoia too...
759%endif
760 ; activate ldt
761 DEBUG_CHAR('2')
762 lldt [edx + CPUMCPU.Host.ldtr]
763 ; Restore segment registers
764 mov eax, [edx + CPUMCPU.Host.ds]
765 mov ds, eax
766 mov eax, [edx + CPUMCPU.Host.es]
767 mov es, eax
768 mov eax, [edx + CPUMCPU.Host.fs]
769 mov fs, eax
770 mov eax, [edx + CPUMCPU.Host.gs]
771 mov gs, eax
772 ; restore stack
773 lss esp, [edx + CPUMCPU.Host.esp]
774
775
776 FIXUP FIX_NO_SYSENTER_JMP, 0, gth_sysenter_no - NAME(Start) ; this will insert a jmp gth_sysenter_no if host doesn't use sysenter.
777 ; restore MSR_IA32_SYSENTER_CS register.
778 mov ecx, MSR_IA32_SYSENTER_CS
779 mov eax, [edx + CPUMCPU.Host.SysEnter.cs]
780 mov ebx, [edx + CPUMCPU.Host.SysEnter.cs + 4]
781 xchg edx, ebx ; save/load edx
782 wrmsr ; MSR[ecx] <- edx:eax
783 xchg edx, ebx ; restore edx
784 jmp short gth_sysenter_no
785
786ALIGNCODE(16)
787gth_sysenter_no:
788
789 FIXUP FIX_NO_SYSCALL_JMP, 0, gth_syscall_no - NAME(Start) ; this will insert a jmp gth_syscall_no if host doesn't use syscall.
790 ; set MSR_K6_EFER_SCE.
791 mov ebx, edx ; save edx
792 mov ecx, MSR_K6_EFER
793 rdmsr
794 or eax, MSR_K6_EFER_SCE
795 wrmsr
796 mov edx, ebx ; restore edx
797 jmp short gth_syscall_no
798
799ALIGNCODE(16)
800gth_syscall_no:
801
802 ; Restore FPU if guest has used it.
803 ; Using fxrstor should ensure that we're not causing unwanted exception on the host.
804 mov esi, [edx + CPUMCPU.fUseFlags] ; esi == use flags.
805 test esi, CPUM_USED_FPU
806 jz near gth_fpu_no
807 mov ecx, cr0
808 and ecx, ~(X86_CR0_TS | X86_CR0_EM)
809 mov cr0, ecx
810
811 FIXUP FIX_NO_FXSAVE_JMP, 0, gth_no_fxsave - NAME(Start) ; this will insert a jmp gth_no_fxsave if fxsave isn't supported.
812 fxsave [edx + CPUMCPU.Guest.fpu]
813 fxrstor [edx + CPUMCPU.Host.fpu]
814 jmp near gth_fpu_no
815
816gth_no_fxsave:
817 fnsave [edx + CPUMCPU.Guest.fpu]
818 mov eax, [edx + CPUMCPU.Host.fpu] ; control word
819 not eax ; 1 means exception ignored (6 LS bits)
820 and eax, byte 03Fh ; 6 LS bits only
821 test eax, [edx + CPUMCPU.Host.fpu + 4] ; status word
822 jz gth_no_exceptions_pending
823
824 ; technically incorrect, but we certainly don't want any exceptions now!!
825 and dword [edx + CPUMCPU.Host.fpu + 4], ~03Fh
826
827gth_no_exceptions_pending:
828 frstor [edx + CPUMCPU.Host.fpu]
829 jmp short gth_fpu_no
830
831ALIGNCODE(16)
832gth_fpu_no:
833
834 ; Control registers.
835 ; Would've liked to have these higher up in case of crashes, but
836 ; the fpu stuff must be done before we restore cr0.
837 mov ecx, [edx + CPUMCPU.Host.cr4]
838 mov cr4, ecx
839 mov ecx, [edx + CPUMCPU.Host.cr0]
840 mov cr0, ecx
841 ;mov ecx, [edx + CPUMCPU.Host.cr2] ; assumes this is waste of time.
842 ;mov cr2, ecx
843
844 ; restore debug registers (if modified) (esi must still be fUseFlags!)
845 ; (must be done after cr4 reload because of the debug extension.)
846 test esi, CPUM_USE_DEBUG_REGS | CPUM_USE_DEBUG_REGS_HOST
847 jz short gth_debug_regs_no
848 jmp gth_debug_regs_restore
849gth_debug_regs_no:
850
851 ; restore general registers.
852 mov eax, edi ; restore return code. eax = return code !!
853 mov edi, [edx + CPUMCPU.Host.edi]
854 mov esi, [edx + CPUMCPU.Host.esi]
855 mov ebx, [edx + CPUMCPU.Host.ebx]
856 mov ebp, [edx + CPUMCPU.Host.ebp]
857 push dword [edx + CPUMCPU.Host.eflags]
858 popfd
859
860%ifdef DEBUG_STUFF
861; COM_S_CHAR '4'
862%endif
863 retf
864
865;;
866; Detour for restoring the host debug registers.
867; edx and edi must be preserved.
868gth_debug_regs_restore:
869 DEBUG_S_CHAR('d')
870 xor eax, eax
871 mov dr7, eax ; paranoia or not?
872 test esi, CPUM_USE_DEBUG_REGS
873 jz short gth_debug_regs_dr7
874 DEBUG_S_CHAR('r')
875 mov eax, [edx + CPUMCPU.Host.dr0]
876 mov dr0, eax
877 mov ebx, [edx + CPUMCPU.Host.dr1]
878 mov dr1, ebx
879 mov ecx, [edx + CPUMCPU.Host.dr2]
880 mov dr2, ecx
881 mov eax, [edx + CPUMCPU.Host.dr3]
882 mov dr3, eax
883gth_debug_regs_dr7:
884 mov ebx, [edx + CPUMCPU.Host.dr6]
885 mov dr6, ebx
886 mov ecx, [edx + CPUMCPU.Host.dr7]
887 mov dr7, ecx
888 jmp gth_debug_regs_no
889
890ENDPROC VMMGCGuestToHostAsm
891
892
893GLOBALNAME End
894;
895; The description string (in the text section).
896;
897NAME(Description):
898 db SWITCHER_DESCRIPTION
899 db 0
900
901extern NAME(Relocate)
902
903;
904; End the fixup records.
905;
906BEGINDATA
907 db FIX_THE_END ; final entry.
908GLOBALNAME FixupsEnd
909
910;;
911; The switcher definition structure.
912ALIGNDATA(16)
913GLOBALNAME Def
914 istruc VMMSWITCHERDEF
915 at VMMSWITCHERDEF.pvCode, RTCCPTR_DEF NAME(Start)
916 at VMMSWITCHERDEF.pvFixups, RTCCPTR_DEF NAME(Fixups)
917 at VMMSWITCHERDEF.pszDesc, RTCCPTR_DEF NAME(Description)
918 at VMMSWITCHERDEF.pfnRelocate, RTCCPTR_DEF NAME(Relocate)
919 at VMMSWITCHERDEF.enmType, dd SWITCHER_TYPE
920 at VMMSWITCHERDEF.cbCode, dd NAME(End) - NAME(Start)
921 at VMMSWITCHERDEF.offR0HostToGuest, dd NAME(vmmR0HostToGuest) - NAME(Start)
922 at VMMSWITCHERDEF.offGCGuestToHost, dd NAME(vmmGCGuestToHost) - NAME(Start)
923 at VMMSWITCHERDEF.offGCCallTrampoline, dd NAME(vmmGCCallTrampoline) - NAME(Start)
924 at VMMSWITCHERDEF.offGCGuestToHostAsm, dd NAME(VMMGCGuestToHostAsm) - NAME(Start)
925 ; disasm help
926 at VMMSWITCHERDEF.offHCCode0, dd 0
927%ifdef NEED_ID
928 at VMMSWITCHERDEF.cbHCCode0, dd NAME(IDEnterTarget) - NAME(Start)
929%else
930 at VMMSWITCHERDEF.cbHCCode0, dd NAME(FarJmpGCTarget) - NAME(Start)
931%endif
932 at VMMSWITCHERDEF.offHCCode1, dd NAME(HCExitTarget) - NAME(Start)
933 at VMMSWITCHERDEF.cbHCCode1, dd NAME(End) - NAME(HCExitTarget)
934%ifdef NEED_ID
935 at VMMSWITCHERDEF.offIDCode0, dd NAME(IDEnterTarget) - NAME(Start)
936 at VMMSWITCHERDEF.cbIDCode0, dd NAME(FarJmpGCTarget) - NAME(IDEnterTarget)
937 at VMMSWITCHERDEF.offIDCode1, dd NAME(IDExitTarget) - NAME(Start)
938 at VMMSWITCHERDEF.cbIDCode1, dd NAME(HCExitTarget) - NAME(IDExitTarget)
939%else
940 at VMMSWITCHERDEF.offIDCode0, dd 0
941 at VMMSWITCHERDEF.cbIDCode0, dd 0
942 at VMMSWITCHERDEF.offIDCode1, dd 0
943 at VMMSWITCHERDEF.cbIDCode1, dd 0
944%endif
945 at VMMSWITCHERDEF.offGCCode, dd NAME(FarJmpGCTarget) - NAME(Start)
946%ifdef NEED_ID
947 at VMMSWITCHERDEF.cbGCCode, dd NAME(IDExitTarget) - NAME(FarJmpGCTarget)
948%else
949 at VMMSWITCHERDEF.cbGCCode, dd NAME(HCExitTarget) - NAME(FarJmpGCTarget)
950%endif
951
952 iend
953
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