VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMGC/CPUMGCA.asm@ 552

Last change on this file since 552 was 19, checked in by vboxsync, 18 years ago

nasm.mac -> asmdefs.mac + header adjustments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.5 KB
Line 
1; $Id: CPUMGCA.asm 19 2007-01-15 13:07:05Z vboxsync $
2;; @file
3;
4; CPUM - Guest Context Assembly Routines.
5
6; Copyright (C) 2006 InnoTek Systemberatung GmbH
7;
8; This file is part of VirtualBox Open Source Edition (OSE), as
9; available from http://www.virtualbox.org. This file is free software;
10; you can redistribute it and/or modify it under the terms of the GNU
11; General Public License as published by the Free Software Foundation,
12; in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13; distribution. VirtualBox OSE is distributed in the hope that it will
14; be useful, but WITHOUT ANY WARRANTY of any kind.
15;
16; If you received this file as part of a commercial VirtualBox
17; distribution, then only the terms of your commercial VirtualBox
18; license agreement apply instead of the previous paragraph.
19
20;*******************************************************************************
21;* Header Files *
22;*******************************************************************************
23%include "VMMGC.mac"
24%include "VBox/vm.mac"
25%include "VBox/err.mac"
26%include "VBox/stam.mac"
27%include "CPUMInternal.mac"
28%include "VBox/x86.mac"
29%include "VBox/cpum.mac"
30
31
32;*******************************************************************************
33;* External Symbols *
34;*******************************************************************************
35extern IMPNAME(g_CPUM) ; VMM GC Builtin import
36extern IMPNAME(g_VM) ; VMM GC Builtin import
37extern NAME(cpumGCHandleNPAndGP) ; CPUMGC.cpp
38
39;
40; Enables write protection of Hypervisor memory pages.
41; !note! Must be commented out for Trap8 debug handler.
42;
43%define ENABLE_WRITE_PROTECTION 1
44
45BEGINCODE
46
47
48;;
49; Restores GC context before doing iret.
50;
51; @param [esp + 4] Pointer to interrupt stack frame, i.e. pointer
52; to the a struct with this layout:
53; 00h eip
54; 04h cs
55; 08h eflags
56; 0ch esp
57; 10h ss
58; 14h es (V86 only)
59; 18h ds (V86 only)
60; 1Ch fs (V86 only)
61; 20h gs (V86 only)
62;
63; @uses everything but cs, ss, esp, and eflags.
64;
65; @remark Assumes we're restoring in Ring-0 a context which is not Ring-0.
66; Further assumes flat stack and valid ds.
67
68BEGINPROC CPUMGCRestoreInt
69 ;
70 ; Update iret frame.
71 ;
72 mov eax, [esp + 4] ; get argument
73 mov edx, IMP(g_CPUM)
74
75 mov ecx, [edx + CPUM.Guest.eip]
76 mov [eax + 0h], ecx
77 mov ecx, [edx + CPUM.Guest.cs]
78 mov [eax + 4h], ecx
79 mov ecx, [edx + CPUM.Guest.eflags]
80 mov [eax + 8h], ecx
81 mov ecx, [edx + CPUM.Guest.esp]
82 mov [eax + 0ch], ecx
83 mov ecx, [edx + CPUM.Guest.ss]
84 mov [eax + 10h], ecx
85
86 test dword [edx + CPUM.Guest.eflags], X86_EFL_VM
87 jnz short CPUMGCRestoreInt_V86
88
89 ;
90 ; Load registers.
91 ;
92 ; todo: potential trouble loading invalid es,fs,gs,ds because
93 ; of a VMM imposed exception?
94 mov es, [edx + CPUM.Guest.es]
95 mov fs, [edx + CPUM.Guest.fs]
96 mov gs, [edx + CPUM.Guest.gs]
97 mov esi, [edx + CPUM.Guest.esi]
98 mov edi, [edx + CPUM.Guest.edi]
99 mov ebp, [edx + CPUM.Guest.ebp]
100 mov ebx, [edx + CPUM.Guest.ebx]
101 mov ecx, [edx + CPUM.Guest.ecx]
102 mov eax, [edx + CPUM.Guest.eax]
103 push dword [edx + CPUM.Guest.ds]
104 mov edx, [edx + CPUM.Guest.edx]
105 pop ds
106
107 ret
108
109CPUMGCRestoreInt_V86:
110 ; iret restores ds, es, fs & gs
111 mov ecx, [edx + CPUM.Guest.es]
112 mov [eax + 14h], ecx
113 mov ecx, [edx + CPUM.Guest.ds]
114 mov [eax + 18h], ecx
115 mov ecx, [edx + CPUM.Guest.fs]
116 mov [eax + 1Ch], ecx
117 mov ecx, [edx + CPUM.Guest.gs]
118 mov [eax + 20h], ecx
119 mov esi, [edx + CPUM.Guest.esi]
120 mov edi, [edx + CPUM.Guest.edi]
121 mov ebp, [edx + CPUM.Guest.ebp]
122 mov ebx, [edx + CPUM.Guest.ebx]
123 mov ecx, [edx + CPUM.Guest.ecx]
124 mov eax, [edx + CPUM.Guest.eax]
125 mov edx, [edx + CPUM.Guest.edx]
126 ret
127
128ENDPROC CPUMGCRestoreInt
129
130
131;;
132; Calls a guest trap/interrupt handler directly
133; Assumes a trap stack frame has already been setup on the guest's stack!
134;
135; @param pRegFrame [esp + 4] Original trap/interrupt context
136; @param selCS [esp + 8] Code selector of handler
137; @param pHandler [esp + 12] GC virtual address of handler
138; @param eflags [esp + 16] Callee's EFLAGS
139; @param selSS [esp + 20] Stack selector for handler
140; @param pEsp [esp + 24] Stack address for handler
141;
142; @remark This call never returns!
143;
144; CPUMGCDECL(void) CPUMGCCallGuestTrapHandler(PCPUMCTXCORE pRegFrame, uint32_t selCS, RTGCPTR pHandler, uint32_t eflags, uint32_t selSS, RTGCPTR pEsp);
145align 16
146BEGINPROC_EXPORTED CPUMGCCallGuestTrapHandler
147 mov ebp, esp
148
149 ; construct iret stack frame
150 push dword [ebp + 20] ; SS
151 push dword [ebp + 24] ; ESP
152 push dword [ebp + 16] ; EFLAGS
153 push dword [ebp + 8] ; CS
154 push dword [ebp + 12] ; EIP
155
156 ;
157 ; enable WP
158 ;
159%ifdef ENABLE_WRITE_PROTECTION
160 mov eax, cr0
161 or eax, X86_CR0_WRITE_PROTECT
162 mov cr0, eax
163%endif
164
165 ; restore CPU context (all except cs, eip, ss, esp & eflags; which are restored or overwritten by iret)
166 mov ebp, [ebp + 4] ; pRegFrame
167 mov ebx, [ebp + CPUMCTXCORE.ebx]
168 mov ecx, [ebp + CPUMCTXCORE.ecx]
169 mov edx, [ebp + CPUMCTXCORE.edx]
170 mov esi, [ebp + CPUMCTXCORE.esi]
171 mov edi, [ebp + CPUMCTXCORE.edi]
172
173 ;; @todo load segment registers *before* enabling WP.
174 TRPM_NP_GP_HANDLER NAME(cpumGCHandleNPAndGP), CPUM_HANDLER_GS | CPUM_HANDLER_CORECTX_IN_EBP
175 mov gs, [ebp + CPUMCTXCORE.gs]
176 TRPM_NP_GP_HANDLER NAME(cpumGCHandleNPAndGP), CPUM_HANDLER_FS | CPUM_HANDLER_CORECTX_IN_EBP
177 mov fs, [ebp + CPUMCTXCORE.fs]
178 TRPM_NP_GP_HANDLER NAME(cpumGCHandleNPAndGP), CPUM_HANDLER_ES | CPUM_HANDLER_CORECTX_IN_EBP
179 mov es, [ebp + CPUMCTXCORE.es]
180 TRPM_NP_GP_HANDLER NAME(cpumGCHandleNPAndGP), CPUM_HANDLER_DS | CPUM_HANDLER_CORECTX_IN_EBP
181 mov ds, [ebp + CPUMCTXCORE.ds]
182
183 mov eax, [ebp + CPUMCTXCORE.eax]
184 mov ebp, [ebp + CPUMCTXCORE.ebp]
185
186 TRPM_NP_GP_HANDLER NAME(cpumGCHandleNPAndGP), CPUM_HANDLER_IRET
187 iret
188
189ENDPROC CPUMGCCallGuestTrapHandler
190
191
192;;
193; This is a main entry point for resuming (or starting) guest
194; code execution.
195;
196; We get here directly from VMMSwitcher.asm (jmp at the end
197; of VMMSwitcher_HostToGuest).
198;
199; This call never returns!
200;
201; @param edx Pointer to CPUM structure.
202;
203align 16
204BEGINPROC_EXPORTED CPUMGCResumeGuest
205 ;
206 ; Setup iretd
207 ;
208 push dword [edx + CPUM.Guest.ss]
209 push dword [edx + CPUM.Guest.esp]
210 push dword [edx + CPUM.Guest.eflags]
211 push dword [edx + CPUM.Guest.cs]
212 push dword [edx + CPUM.Guest.eip]
213
214 ;
215 ; Restore registers.
216 ;
217 TRPM_NP_GP_HANDLER NAME(cpumGCHandleNPAndGP), CPUM_HANDLER_ES
218 mov es, [edx + CPUM.Guest.es]
219 TRPM_NP_GP_HANDLER NAME(cpumGCHandleNPAndGP), CPUM_HANDLER_FS
220 mov fs, [edx + CPUM.Guest.fs]
221 TRPM_NP_GP_HANDLER NAME(cpumGCHandleNPAndGP), CPUM_HANDLER_GS
222 mov gs, [edx + CPUM.Guest.gs]
223
224%ifdef VBOX_WITH_STATISTICS
225 ;
226 ; Statistics.
227 ;
228 push edx
229 mov edx, IMP(g_VM)
230 lea edx, [edx + VM.StatTotalQemuToGC]
231 STAM_PROFILE_ADV_STOP edx
232
233 mov edx, IMP(g_VM)
234 lea edx, [edx + VM.StatTotalInGC]
235 STAM_PROFILE_ADV_START edx
236 pop edx
237%endif
238
239 ;
240 ; enable WP
241 ;
242%ifdef ENABLE_WRITE_PROTECTION
243 mov eax, cr0
244 or eax, X86_CR0_WRITE_PROTECT
245 mov cr0, eax
246%endif
247
248 ;
249 ; Continue restore.
250 ;
251 mov esi, [edx + CPUM.Guest.esi]
252 mov edi, [edx + CPUM.Guest.edi]
253 mov ebp, [edx + CPUM.Guest.ebp]
254 mov ebx, [edx + CPUM.Guest.ebx]
255 mov ecx, [edx + CPUM.Guest.ecx]
256 mov eax, [edx + CPUM.Guest.eax]
257 push dword [edx + CPUM.Guest.ds]
258 mov edx, [edx + CPUM.Guest.edx]
259 TRPM_NP_GP_HANDLER NAME(cpumGCHandleNPAndGP), CPUM_HANDLER_DS
260 pop ds
261
262 ; restart execution.
263 TRPM_NP_GP_HANDLER NAME(cpumGCHandleNPAndGP), CPUM_HANDLER_IRET
264 iretd
265ENDPROC CPUMGCResumeGuest
266
267
268;;
269; This is a main entry point for resuming (or starting) guest
270; code execution for raw V86 mode
271;
272; We get here directly from VMMSwitcher.asm (jmp at the end
273; of VMMSwitcher_HostToGuest).
274;
275; This call never returns!
276;
277; @param edx Pointer to CPUM structure.
278;
279align 16
280BEGINPROC_EXPORTED CPUMGCResumeGuestV86
281 ;
282 ; Setup iretd
283 ;
284 push dword [edx + CPUM.Guest.gs]
285 push dword [edx + CPUM.Guest.fs]
286 push dword [edx + CPUM.Guest.ds]
287 push dword [edx + CPUM.Guest.es]
288
289 push dword [edx + CPUM.Guest.ss]
290 push dword [edx + CPUM.Guest.esp]
291
292 push dword [edx + CPUM.Guest.eflags]
293 push dword [edx + CPUM.Guest.cs]
294 push dword [edx + CPUM.Guest.eip]
295
296 ;
297 ; Restore registers.
298 ;
299
300%ifdef VBOX_WITH_STATISTICS
301 ;
302 ; Statistics.
303 ;
304 push edx
305 mov edx, IMP(g_VM)
306 lea edx, [edx + VM.StatTotalQemuToGC]
307 STAM_PROFILE_ADV_STOP edx
308
309 mov edx, IMP(g_VM)
310 lea edx, [edx + VM.StatTotalInGC]
311 STAM_PROFILE_ADV_START edx
312 pop edx
313%endif
314
315 ;
316 ; enable WP
317 ;
318%ifdef ENABLE_WRITE_PROTECTION
319 mov eax, cr0
320 or eax, X86_CR0_WRITE_PROTECT
321 mov cr0, eax
322%endif
323
324 ;
325 ; Continue restore.
326 ;
327 mov esi, [edx + CPUM.Guest.esi]
328 mov edi, [edx + CPUM.Guest.edi]
329 mov ebp, [edx + CPUM.Guest.ebp]
330 mov ecx, [edx + CPUM.Guest.ecx]
331 mov ebx, [edx + CPUM.Guest.ebx]
332 mov eax, [edx + CPUM.Guest.eax]
333 mov edx, [edx + CPUM.Guest.edx]
334
335 ; restart execution.
336 TRPM_NP_GP_HANDLER NAME(cpumGCHandleNPAndGP), CPUM_HANDLER_IRET
337 iretd
338ENDPROC CPUMGCResumeGuestV86
339
340
341;;
342; Set the Guest CPU CR2 register.
343;
344; @param eax cr2
345; @uses edx
346;
347align 16
348BEGINPROC CPUMGCSetGuestCR2Asm
349 mov edx, IMP(g_CPUM)
350 mov [edx + CPUM.Guest.cr2], eax
351 ret
352ENDPROC CPUMGCSetGuestCR2Asm
353
354
355;;
356; Get the Guest CPU CR0 register.
357;
358; @returns cr0 in eax
359; @uses eax
360;
361align 16
362BEGINPROC CPUMGCGetGuestCR0
363 mov eax, IMP(g_CPUM)
364 mov eax, [eax + CPUM.Guest.cr0]
365 ret
366ENDPROC CPUMGCGetGuestCR0
367
368
369
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