1 | /* $Id: MMRamGC.cpp 23 2007-01-15 14:08:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MMRamGC - Guest Context Ram access Routines, pair for MMRamGCA.asm.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006 InnoTek Systemberatung 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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_MM
|
---|
27 | #include <VBox/mm.h>
|
---|
28 | #include <VBox/cpum.h>
|
---|
29 | #include <VBox/trpm.h>
|
---|
30 | #include "MMInternal.h"
|
---|
31 | #include <VBox/vm.h>
|
---|
32 | #include <VBox/pgm.h>
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <VBox/param.h>
|
---|
36 | #include <VBox/err.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /*******************************************************************************
|
---|
40 | * Internal Functions *
|
---|
41 | *******************************************************************************/
|
---|
42 | static DECLCALLBACK(int) mmgcramTrap0eHandler(PVM pVM, PCPUMCTXCORE pRegFrame);
|
---|
43 |
|
---|
44 | DECLASM(void) MMGCRamReadNoTrapHandler_EndProc(void);
|
---|
45 | DECLASM(void) MMGCRamWriteNoTrapHandler_EndProc(void);
|
---|
46 |
|
---|
47 | DECLASM(void) MMGCRamRead_Error(void);
|
---|
48 | DECLASM(void) MMGCRamWrite_Error(void);
|
---|
49 |
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Install MMGCRam Hypervisor page fault handler for normal working
|
---|
53 | * of MMGCRamRead and MMGCRamWrite calls.
|
---|
54 | * This handler will be authomatically removed at page fault.
|
---|
55 | * In other case it must be removed by MMGCRamDeregisterTrapHandler call.
|
---|
56 | *
|
---|
57 | * @param pVM VM handle.
|
---|
58 | */
|
---|
59 | MMGCDECL(void) MMGCRamRegisterTrapHandler(PVM pVM)
|
---|
60 | {
|
---|
61 | TRPMGCSetTempHandler(pVM, 0xe, mmgcramTrap0eHandler);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Remove MMGCRam Hypervisor page fault handler.
|
---|
66 | * See description of MMGCRamRegisterTrapHandler call.
|
---|
67 | *
|
---|
68 | * @param pVM VM handle.
|
---|
69 | */
|
---|
70 | MMGCDECL(void) MMGCRamDeregisterTrapHandler(PVM pVM)
|
---|
71 | {
|
---|
72 | TRPMGCSetTempHandler(pVM, 0xe, NULL);
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Read data in guest context with #PF control.
|
---|
78 | *
|
---|
79 | * @returns VBox status.
|
---|
80 | * @param pVM The VM handle.
|
---|
81 | * @param pDst Where to store the readed data.
|
---|
82 | * @param pSrc Pointer to the data to read.
|
---|
83 | * @param cb Size of data to read, only 1/2/4/8 is valid.
|
---|
84 | */
|
---|
85 | MMGCDECL(int) MMGCRamRead(PVM pVM, void *pDst, void *pSrc, size_t cb)
|
---|
86 | {
|
---|
87 | int rc;
|
---|
88 |
|
---|
89 | TRPMSaveTrap(pVM); /* save the current trap info, because it will get trashed if our access failed. */
|
---|
90 |
|
---|
91 | MMGCRamRegisterTrapHandler(pVM);
|
---|
92 | rc = MMGCRamReadNoTrapHandler(pDst, pSrc, cb);
|
---|
93 | MMGCRamDeregisterTrapHandler(pVM);
|
---|
94 | if (VBOX_FAILURE(rc))
|
---|
95 | TRPMRestoreTrap(pVM);
|
---|
96 |
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Write data in guest context with #PF control.
|
---|
102 | *
|
---|
103 | * @returns VBox status.
|
---|
104 | * @param pVM The VM handle.
|
---|
105 | * @param pDst Where to write the data.
|
---|
106 | * @param pSrc Pointer to the data to write.
|
---|
107 | * @param cb Size of data to write, only 1/2/4 is valid.
|
---|
108 | */
|
---|
109 | MMGCDECL(int) MMGCRamWrite(PVM pVM, void *pDst, void *pSrc, size_t cb)
|
---|
110 | {
|
---|
111 | int rc;
|
---|
112 |
|
---|
113 | TRPMSaveTrap(pVM); /* save the current trap info, because it will get trashed if our access failed. */
|
---|
114 |
|
---|
115 | MMGCRamRegisterTrapHandler(pVM);
|
---|
116 | rc = MMGCRamWriteNoTrapHandler(pDst, pSrc, cb);
|
---|
117 | MMGCRamDeregisterTrapHandler(pVM);
|
---|
118 | if (VBOX_FAILURE(rc))
|
---|
119 | TRPMRestoreTrap(pVM);
|
---|
120 |
|
---|
121 | /*
|
---|
122 | * And mark the relevant guest page as accessed and dirty.
|
---|
123 | */
|
---|
124 | PGMGstModifyPage(pVM, pDst, cb, X86_PTE_A | X86_PTE_D, ~(uint64_t)(X86_PTE_A | X86_PTE_D));
|
---|
125 |
|
---|
126 | return rc;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * \#PF Handler for servicing traps inside MMGCRamReadNoTrapHandler and MMGCRamWriteNoTrapHandler functions.
|
---|
132 | *
|
---|
133 | * @internal
|
---|
134 | */
|
---|
135 | DECLCALLBACK(int) mmgcramTrap0eHandler(PVM pVM, PCPUMCTXCORE pRegFrame)
|
---|
136 | {
|
---|
137 | /*
|
---|
138 | * Check where the trap was occurred.
|
---|
139 | */
|
---|
140 | if ( (uintptr_t)&MMGCRamReadNoTrapHandler < (uintptr_t)pRegFrame->eip
|
---|
141 | && (uintptr_t)pRegFrame->eip < (uintptr_t)&MMGCRamReadNoTrapHandler_EndProc)
|
---|
142 | {
|
---|
143 | /*
|
---|
144 | * Page fault inside MMGCRamRead() func.
|
---|
145 | */
|
---|
146 | RTGCUINT uErrorCode = TRPMGetErrorCode(pVM);
|
---|
147 |
|
---|
148 | /* Must be read violation. */
|
---|
149 | if (uErrorCode & X86_TRAP_PF_RW)
|
---|
150 | return VERR_INTERNAL_ERROR;
|
---|
151 |
|
---|
152 | /* Return execution to func at error label. */
|
---|
153 | pRegFrame->eip = (uintptr_t)&MMGCRamRead_Error;
|
---|
154 | return VINF_SUCCESS;
|
---|
155 | }
|
---|
156 | else if ( (uintptr_t)&MMGCRamWriteNoTrapHandler < (uintptr_t)pRegFrame->eip
|
---|
157 | && (uintptr_t)pRegFrame->eip < (uintptr_t)&MMGCRamWriteNoTrapHandler_EndProc)
|
---|
158 | {
|
---|
159 | /*
|
---|
160 | * Page fault inside MMGCRamWrite() func.
|
---|
161 | */
|
---|
162 | RTGCUINT uErrorCode = TRPMGetErrorCode(pVM);
|
---|
163 |
|
---|
164 | /* Must be write violation. */
|
---|
165 | if (!(uErrorCode & X86_TRAP_PF_RW))
|
---|
166 | return VERR_INTERNAL_ERROR;
|
---|
167 |
|
---|
168 | /* Return execution to func at error label. */
|
---|
169 | pRegFrame->eip = (uintptr_t)&MMGCRamWrite_Error;
|
---|
170 | return VINF_SUCCESS;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /* #PF is not handled - kill the Hypervisor. */
|
---|
174 | return VERR_INTERNAL_ERROR;
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|