VirtualBox

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

Last change on this file since 7122 was 7122, checked in by vboxsync, 17 years ago

Correct comment (not pushad any longer).

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