VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMSwitcher/AMD64ToPAE.asm@ 9026

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

The Big Sun Rebranding Header Change

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