1 | /* $Id: PDMAll.cpp 13020 2008-10-06 16:27:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PDM Critical Sections
|
---|
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 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_PDM
|
---|
27 | #include "PDMInternal.h"
|
---|
28 | #include <VBox/pdm.h>
|
---|
29 | #include <VBox/mm.h>
|
---|
30 | #include <VBox/vm.h>
|
---|
31 | #include <VBox/err.h>
|
---|
32 |
|
---|
33 | #include <VBox/log.h>
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Gets the pending interrupt.
|
---|
40 | *
|
---|
41 | * @returns VBox status code.
|
---|
42 | * @param pVM VM handle.
|
---|
43 | * @param pu8Interrupt Where to store the interrupt on success.
|
---|
44 | */
|
---|
45 | VMMDECL(int) PDMGetInterrupt(PVM pVM, uint8_t *pu8Interrupt)
|
---|
46 | {
|
---|
47 | pdmLock(pVM);
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * The local APIC has a higer priority than the PIC.
|
---|
51 | */
|
---|
52 | if (VM_FF_ISSET(pVM, VM_FF_INTERRUPT_APIC))
|
---|
53 | {
|
---|
54 | VM_FF_CLEAR(pVM, VM_FF_INTERRUPT_APIC);
|
---|
55 | Assert(pVM->pdm.s.Apic.CTX_SUFF(pDevIns));
|
---|
56 | Assert(pVM->pdm.s.Apic.CTX_SUFF(pfnGetInterrupt));
|
---|
57 | int i = pVM->pdm.s.Apic.CTX_SUFF(pfnGetInterrupt)(pVM->pdm.s.Apic.CTX_SUFF(pDevIns));
|
---|
58 | AssertMsg(i <= 255 && i >= 0, ("i=%d\n", i));
|
---|
59 | if (i >= 0)
|
---|
60 | {
|
---|
61 | pdmUnlock(pVM);
|
---|
62 | *pu8Interrupt = (uint8_t)i;
|
---|
63 | return VINF_SUCCESS;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * Check the PIC.
|
---|
69 | */
|
---|
70 | if (VM_FF_ISSET(pVM, VM_FF_INTERRUPT_PIC))
|
---|
71 | {
|
---|
72 | VM_FF_CLEAR(pVM, VM_FF_INTERRUPT_PIC);
|
---|
73 | Assert(pVM->pdm.s.Pic.CTX_SUFF(pDevIns));
|
---|
74 | Assert(pVM->pdm.s.Pic.CTX_SUFF(pfnGetInterrupt));
|
---|
75 | int i = pVM->pdm.s.Pic.CTX_SUFF(pfnGetInterrupt)(pVM->pdm.s.Pic.CTX_SUFF(pDevIns));
|
---|
76 | AssertMsg(i <= 255 && i >= 0, ("i=%d\n", i));
|
---|
77 | if (i >= 0)
|
---|
78 | {
|
---|
79 | pdmUnlock(pVM);
|
---|
80 | *pu8Interrupt = (uint8_t)i;
|
---|
81 | return VINF_SUCCESS;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** @todo Figure out exactly why we can get here without anything being set. (REM) */
|
---|
86 |
|
---|
87 | pdmUnlock(pVM);
|
---|
88 | return VERR_NO_DATA;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Sets the pending interrupt.
|
---|
94 | *
|
---|
95 | * @returns VBox status code.
|
---|
96 | * @param pVM VM handle.
|
---|
97 | * @param u8Irq The IRQ line.
|
---|
98 | * @param u8Level The new level.
|
---|
99 | */
|
---|
100 | VMMDECL(int) PDMIsaSetIrq(PVM pVM, uint8_t u8Irq, uint8_t u8Level)
|
---|
101 | {
|
---|
102 | pdmLock(pVM);
|
---|
103 |
|
---|
104 | int rc = VERR_PDM_NO_PIC_INSTANCE;
|
---|
105 | if (pVM->pdm.s.Pic.CTX_SUFF(pDevIns))
|
---|
106 | {
|
---|
107 | Assert(pVM->pdm.s.Pic.CTX_SUFF(pfnSetIrq));
|
---|
108 | pVM->pdm.s.Pic.CTX_SUFF(pfnSetIrq)(pVM->pdm.s.Pic.CTX_SUFF(pDevIns), u8Irq, u8Level);
|
---|
109 | rc = VINF_SUCCESS;
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (pVM->pdm.s.IoApic.CTX_SUFF(pDevIns))
|
---|
113 | {
|
---|
114 | Assert(pVM->pdm.s.IoApic.CTX_SUFF(pfnSetIrq));
|
---|
115 | pVM->pdm.s.IoApic.CTX_SUFF(pfnSetIrq)(pVM->pdm.s.IoApic.CTX_SUFF(pDevIns), u8Irq, u8Level);
|
---|
116 | rc = VINF_SUCCESS;
|
---|
117 | }
|
---|
118 |
|
---|
119 | pdmUnlock(pVM);
|
---|
120 | return rc;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Sets the pending I/O APIC interrupt.
|
---|
126 | *
|
---|
127 | * @returns VBox status code.
|
---|
128 | * @param pVM VM handle.
|
---|
129 | * @param u8Irq The IRQ line.
|
---|
130 | * @param u8Level The new level.
|
---|
131 | */
|
---|
132 | VMMDECL(int) PDMIoApicSetIrq(PVM pVM, uint8_t u8Irq, uint8_t u8Level)
|
---|
133 | {
|
---|
134 | if (pVM->pdm.s.IoApic.CTX_SUFF(pDevIns))
|
---|
135 | {
|
---|
136 | Assert(pVM->pdm.s.IoApic.CTX_SUFF(pfnSetIrq));
|
---|
137 | pdmLock(pVM);
|
---|
138 | pVM->pdm.s.IoApic.CTX_SUFF(pfnSetIrq)(pVM->pdm.s.IoApic.CTX_SUFF(pDevIns), u8Irq, u8Level);
|
---|
139 | pdmUnlock(pVM);
|
---|
140 | return VINF_SUCCESS;
|
---|
141 | }
|
---|
142 | return VERR_PDM_NO_PIC_INSTANCE;
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Set the APIC base.
|
---|
148 | *
|
---|
149 | * @returns VBox status code.
|
---|
150 | * @param pVM VM handle.
|
---|
151 | * @param u64Base The new base.
|
---|
152 | */
|
---|
153 | VMMDECL(int) PDMApicSetBase(PVM pVM, uint64_t u64Base)
|
---|
154 | {
|
---|
155 | if (pVM->pdm.s.Apic.CTX_SUFF(pDevIns))
|
---|
156 | {
|
---|
157 | Assert(pVM->pdm.s.Apic.CTX_SUFF(pfnSetBase));
|
---|
158 | pdmLock(pVM);
|
---|
159 | pVM->pdm.s.Apic.CTX_SUFF(pfnSetBase)(pVM->pdm.s.Apic.CTX_SUFF(pDevIns), u64Base);
|
---|
160 | pdmUnlock(pVM);
|
---|
161 | return VINF_SUCCESS;
|
---|
162 | }
|
---|
163 | return VERR_PDM_NO_APIC_INSTANCE;
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Get the APIC base.
|
---|
169 | *
|
---|
170 | * @returns VBox status code.
|
---|
171 | * @param pVM VM handle.
|
---|
172 | * @param pu64Base Where to store the APIC base.
|
---|
173 | */
|
---|
174 | VMMDECL(int) PDMApicGetBase(PVM pVM, uint64_t *pu64Base)
|
---|
175 | {
|
---|
176 | if (pVM->pdm.s.Apic.CTX_SUFF(pDevIns))
|
---|
177 | {
|
---|
178 | Assert(pVM->pdm.s.Apic.CTX_SUFF(pfnGetBase));
|
---|
179 | pdmLock(pVM);
|
---|
180 | *pu64Base = pVM->pdm.s.Apic.CTX_SUFF(pfnGetBase)(pVM->pdm.s.Apic.CTX_SUFF(pDevIns));
|
---|
181 | pdmUnlock(pVM);
|
---|
182 | return VINF_SUCCESS;
|
---|
183 | }
|
---|
184 | *pu64Base = 0;
|
---|
185 | return VERR_PDM_NO_APIC_INSTANCE;
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Check if the APIC has a pending interrupt/if a TPR change would active one.
|
---|
191 | *
|
---|
192 | * @returns VINF_SUCCESS or VERR_PDM_NO_APIC_INSTANCE.
|
---|
193 | * @param pDevIns Device instance of the APIC.
|
---|
194 | * @param pfPending Pending state (out).
|
---|
195 | */
|
---|
196 | VMMDECL(int) PDMApicHasPendingIrq(PVM pVM, bool *pfPending)
|
---|
197 | {
|
---|
198 | if (pVM->pdm.s.Apic.CTX_SUFF(pDevIns))
|
---|
199 | {
|
---|
200 | Assert(pVM->pdm.s.Apic.CTX_SUFF(pfnSetTPR));
|
---|
201 | pdmLock(pVM);
|
---|
202 | *pfPending = pVM->pdm.s.Apic.CTX_SUFF(pfnHasPendingIrq)(pVM->pdm.s.Apic.CTX_SUFF(pDevIns));
|
---|
203 | pdmUnlock(pVM);
|
---|
204 | return VINF_SUCCESS;
|
---|
205 | }
|
---|
206 | return VERR_PDM_NO_APIC_INSTANCE;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Set the TPR (task priority register?).
|
---|
212 | *
|
---|
213 | * @returns VBox status code.
|
---|
214 | * @param pVM VM handle.
|
---|
215 | * @param u8TPR The new TPR.
|
---|
216 | */
|
---|
217 | VMMDECL(int) PDMApicSetTPR(PVM pVM, uint8_t u8TPR)
|
---|
218 | {
|
---|
219 | if (pVM->pdm.s.Apic.CTX_SUFF(pDevIns))
|
---|
220 | {
|
---|
221 | Assert(pVM->pdm.s.Apic.CTX_SUFF(pfnSetTPR));
|
---|
222 | pdmLock(pVM);
|
---|
223 | pVM->pdm.s.Apic.CTX_SUFF(pfnSetTPR)(pVM->pdm.s.Apic.CTX_SUFF(pDevIns), u8TPR);
|
---|
224 | pdmUnlock(pVM);
|
---|
225 | return VINF_SUCCESS;
|
---|
226 | }
|
---|
227 | return VERR_PDM_NO_APIC_INSTANCE;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Get the TPR (task priority register).
|
---|
233 | *
|
---|
234 | * @returns The current TPR.
|
---|
235 | * @param pVM VM handle.
|
---|
236 | * @param pu8TPR Where to store the TRP.
|
---|
237 | * @param pfPending Pending interrupt state (out).
|
---|
238 | */
|
---|
239 | VMMDECL(int) PDMApicGetTPR(PVM pVM, uint8_t *pu8TPR, bool *pfPending)
|
---|
240 | {
|
---|
241 | if (pVM->pdm.s.Apic.CTX_SUFF(pDevIns))
|
---|
242 | {
|
---|
243 | Assert(pVM->pdm.s.Apic.CTX_SUFF(pfnGetTPR));
|
---|
244 | pdmLock(pVM);
|
---|
245 | *pu8TPR = pVM->pdm.s.Apic.CTX_SUFF(pfnGetTPR)(pVM->pdm.s.Apic.CTX_SUFF(pDevIns));
|
---|
246 | if (pfPending)
|
---|
247 | *pfPending = pVM->pdm.s.Apic.CTX_SUFF(pfnHasPendingIrq)(pVM->pdm.s.Apic.CTX_SUFF(pDevIns));
|
---|
248 | pdmUnlock(pVM);
|
---|
249 | return VINF_SUCCESS;
|
---|
250 | }
|
---|
251 | *pu8TPR = 0;
|
---|
252 | return VERR_PDM_NO_APIC_INSTANCE;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Write MSR in APIC range.
|
---|
257 | *
|
---|
258 | * @returns VBox status code.
|
---|
259 | * @param pVM VM handle.
|
---|
260 | * @param iCpu Target CPU.
|
---|
261 | * @param u32Reg MSR to write.
|
---|
262 | * @param u64Value Value to write.
|
---|
263 | */
|
---|
264 | VMMDECL(int) PDMApicWriteMSR(PVM pVM, VMCPUID iCpu, uint32_t u32Reg, uint64_t u64Value)
|
---|
265 | {
|
---|
266 | if (pVM->pdm.s.Apic.CTX_SUFF(pDevIns))
|
---|
267 | {
|
---|
268 | Assert(pVM->pdm.s.Apic.CTX_SUFF(pfnWriteMSR));
|
---|
269 | pdmLock(pVM);
|
---|
270 | pVM->pdm.s.Apic.CTX_SUFF(pfnWriteMSR)(pVM->pdm.s.Apic.CTX_SUFF(pDevIns), iCpu, u32Reg, u64Value);
|
---|
271 | pdmUnlock(pVM);
|
---|
272 | return VINF_SUCCESS;
|
---|
273 | }
|
---|
274 | return VERR_PDM_NO_APIC_INSTANCE;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Read MSR in APIC range.
|
---|
279 | *
|
---|
280 | * @returns VBox status code.
|
---|
281 | * @param pVM VM handle.
|
---|
282 | * @param iCpu Target CPU.
|
---|
283 | * @param u32Reg MSR to read.
|
---|
284 | * @param pu64Value Value read.
|
---|
285 | */
|
---|
286 | VMMDECL(int) PDMApicReadMSR(PVM pVM, VMCPUID iCpu, uint32_t u32Reg, uint64_t *pu64Value)
|
---|
287 | {
|
---|
288 | if (pVM->pdm.s.Apic.CTX_SUFF(pDevIns))
|
---|
289 | {
|
---|
290 | Assert(pVM->pdm.s.Apic.CTX_SUFF(pfnReadMSR));
|
---|
291 | pdmLock(pVM);
|
---|
292 | pVM->pdm.s.Apic.CTX_SUFF(pfnReadMSR)(pVM->pdm.s.Apic.CTX_SUFF(pDevIns), iCpu, u32Reg, pu64Value);
|
---|
293 | pdmUnlock(pVM);
|
---|
294 | return VINF_SUCCESS;
|
---|
295 | }
|
---|
296 | return VERR_PDM_NO_APIC_INSTANCE;
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Locks PDM.
|
---|
302 | * This might call back to Ring-3 in order to deal with lock contention in GC and R3.
|
---|
303 | *
|
---|
304 | * @param pVM The VM handle.
|
---|
305 | */
|
---|
306 | void pdmLock(PVM pVM)
|
---|
307 | {
|
---|
308 | #ifdef IN_RING3
|
---|
309 | int rc = PDMCritSectEnter(&pVM->pdm.s.CritSect, VERR_INTERNAL_ERROR);
|
---|
310 | #else
|
---|
311 | int rc = PDMCritSectEnter(&pVM->pdm.s.CritSect, VERR_GENERAL_FAILURE);
|
---|
312 | if (rc == VERR_GENERAL_FAILURE)
|
---|
313 | {
|
---|
314 | # ifdef IN_GC
|
---|
315 | rc = VMMGCCallHost(pVM, VMMCALLHOST_PDM_LOCK, 0);
|
---|
316 | # else
|
---|
317 | rc = VMMR0CallHost(pVM, VMMCALLHOST_PDM_LOCK, 0);
|
---|
318 | # endif
|
---|
319 | }
|
---|
320 | #endif
|
---|
321 | AssertRC(rc);
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Locks PDM but don't go to ring-3 if it's owned by someone.
|
---|
327 | *
|
---|
328 | * @returns VINF_SUCCESS on success.
|
---|
329 | * @returns rc if we're in GC or R0 and can't get the lock.
|
---|
330 | * @param pVM The VM handle.
|
---|
331 | * @param rc The RC to return in GC or R0 when we can't get the lock.
|
---|
332 | */
|
---|
333 | int pdmLockEx(PVM pVM, int rc)
|
---|
334 | {
|
---|
335 | return PDMCritSectEnter(&pVM->pdm.s.CritSect, rc);
|
---|
336 | }
|
---|
337 |
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * Unlocks PDM.
|
---|
341 | *
|
---|
342 | * @param pVM The VM handle.
|
---|
343 | */
|
---|
344 | void pdmUnlock(PVM pVM)
|
---|
345 | {
|
---|
346 | PDMCritSectLeave(&pVM->pdm.s.CritSect);
|
---|
347 | }
|
---|
348 |
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Converts ring 3 VMM heap pointer to a guest physical address
|
---|
352 | *
|
---|
353 | * @returns VBox status code.
|
---|
354 | * @param pVM VM handle.
|
---|
355 | * @param pv Ring-3 pointer.
|
---|
356 | * @param pGCPhys GC phys address (out).
|
---|
357 | */
|
---|
358 | VMMDECL(int) PDMVMMDevHeapR3ToGCPhys(PVM pVM, RTR3PTR pv, RTGCPHYS *pGCPhys)
|
---|
359 | {
|
---|
360 | AssertReturn(pv >= pVM->pdm.s.pvVMMDevHeap && (RTR3UINTPTR)pv < (RTR3UINTPTR)pVM->pdm.s.pvVMMDevHeap + pVM->pdm.s.cbVMMDevHeap, VERR_INVALID_PARAMETER);
|
---|
361 |
|
---|
362 | *pGCPhys = (pVM->pdm.s.GCPhysVMMDevHeap + ((RTR3UINTPTR)pv - (RTR3UINTPTR)pVM->pdm.s.pvVMMDevHeap));
|
---|
363 | return VINF_SUCCESS;
|
---|
364 | }
|
---|