1 | /* $Id: DBGFCpu.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DBGF - Debugger Facility, CPU State Accessors.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_DBGF
|
---|
33 | #define VMCPU_INCL_CPUM_GST_CTX /* For CPUM_IMPORT_EXTRN_RET(). */
|
---|
34 | #include <VBox/vmm/dbgf.h>
|
---|
35 | #include <VBox/vmm/cpum.h>
|
---|
36 | #include "DBGFInternal.h"
|
---|
37 | #include <VBox/vmm/vm.h>
|
---|
38 | #include <VBox/vmm/uvm.h>
|
---|
39 | #include <iprt/errcore.h>
|
---|
40 | #include <VBox/log.h>
|
---|
41 | #include <VBox/param.h>
|
---|
42 | #include <iprt/assert.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Wrapper around CPUMGetGuestMode.
|
---|
47 | *
|
---|
48 | * @returns VINF_SUCCESS.
|
---|
49 | * @param pVM The cross context VM structure.
|
---|
50 | * @param idCpu The current CPU ID.
|
---|
51 | * @param penmMode Where to return the mode.
|
---|
52 | */
|
---|
53 | static DECLCALLBACK(int) dbgfR3CpuGetMode(PVM pVM, VMCPUID idCpu, CPUMMODE *penmMode)
|
---|
54 | {
|
---|
55 | Assert(idCpu == VMMGetCpuId(pVM));
|
---|
56 | PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
|
---|
57 | CPUM_IMPORT_EXTRN_RET(pVCpu, CPUMCTX_EXTRN_CR0 | CPUMCTX_EXTRN_EFER);
|
---|
58 | *penmMode = CPUMGetGuestMode(pVCpu);
|
---|
59 | return VINF_SUCCESS;
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Get the current CPU mode.
|
---|
65 | *
|
---|
66 | * @returns The CPU mode on success, CPUMMODE_INVALID on failure.
|
---|
67 | * @param pUVM The user mode VM handle.
|
---|
68 | * @param idCpu The target CPU ID.
|
---|
69 | */
|
---|
70 | VMMR3DECL(CPUMMODE) DBGFR3CpuGetMode(PUVM pUVM, VMCPUID idCpu)
|
---|
71 | {
|
---|
72 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, CPUMMODE_INVALID);
|
---|
73 | VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, CPUMMODE_INVALID);
|
---|
74 | AssertReturn(idCpu < pUVM->pVM->cCpus, CPUMMODE_INVALID);
|
---|
75 |
|
---|
76 | CPUMMODE enmMode;
|
---|
77 | int rc = VMR3ReqPriorityCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3CpuGetMode, 3, pUVM->pVM, idCpu, &enmMode);
|
---|
78 | if (RT_FAILURE(rc))
|
---|
79 | return CPUMMODE_INVALID;
|
---|
80 | return enmMode;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Wrapper around CPUMIsGuestIn64BitCode.
|
---|
86 | *
|
---|
87 | * @returns VINF_SUCCESS.
|
---|
88 | * @param pVM The cross context VM structure.
|
---|
89 | * @param idCpu The current CPU ID.
|
---|
90 | * @param pfIn64BitCode Where to return the result.
|
---|
91 | */
|
---|
92 | static DECLCALLBACK(int) dbgfR3CpuIn64BitCode(PVM pVM, VMCPUID idCpu, bool *pfIn64BitCode)
|
---|
93 | {
|
---|
94 | Assert(idCpu == VMMGetCpuId(pVM));
|
---|
95 | PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
|
---|
96 | CPUM_IMPORT_EXTRN_RET(pVCpu, CPUMCTX_EXTRN_CS | CPUMCTX_EXTRN_EFER);
|
---|
97 | *pfIn64BitCode = CPUMIsGuestIn64BitCode(pVCpu);
|
---|
98 | return VINF_SUCCESS;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Checks if the given CPU is executing 64-bit code or not.
|
---|
104 | *
|
---|
105 | * @returns true / false accordingly.
|
---|
106 | * @param pUVM The user mode VM handle.
|
---|
107 | * @param idCpu The target CPU ID.
|
---|
108 | */
|
---|
109 | VMMR3DECL(bool) DBGFR3CpuIsIn64BitCode(PUVM pUVM, VMCPUID idCpu)
|
---|
110 | {
|
---|
111 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
|
---|
112 | VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, false);
|
---|
113 | AssertReturn(idCpu < pUVM->pVM->cCpus, false);
|
---|
114 |
|
---|
115 | bool fIn64BitCode;
|
---|
116 | int rc = VMR3ReqPriorityCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3CpuIn64BitCode, 3, pUVM->pVM, idCpu, &fIn64BitCode);
|
---|
117 | if (RT_FAILURE(rc))
|
---|
118 | return false;
|
---|
119 | return fIn64BitCode;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Wrapper around CPUMIsGuestInV86Code.
|
---|
125 | *
|
---|
126 | * @returns VINF_SUCCESS.
|
---|
127 | * @param pVM The cross context VM structure.
|
---|
128 | * @param idCpu The current CPU ID.
|
---|
129 | * @param pfInV86Code Where to return the result.
|
---|
130 | */
|
---|
131 | static DECLCALLBACK(int) dbgfR3CpuInV86Code(PVM pVM, VMCPUID idCpu, bool *pfInV86Code)
|
---|
132 | {
|
---|
133 | Assert(idCpu == VMMGetCpuId(pVM));
|
---|
134 | PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
|
---|
135 | CPUM_IMPORT_EXTRN_RET(pVCpu, CPUMCTX_EXTRN_RFLAGS);
|
---|
136 | *pfInV86Code = CPUMIsGuestInV86ModeEx(CPUMQueryGuestCtxPtr(pVCpu));
|
---|
137 | return VINF_SUCCESS;
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Checks if the given CPU is executing V8086 code or not.
|
---|
143 | *
|
---|
144 | * @returns true / false accordingly.
|
---|
145 | * @param pUVM The user mode VM handle.
|
---|
146 | * @param idCpu The target CPU ID.
|
---|
147 | */
|
---|
148 | VMMR3DECL(bool) DBGFR3CpuIsInV86Code(PUVM pUVM, VMCPUID idCpu)
|
---|
149 | {
|
---|
150 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
|
---|
151 | VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, false);
|
---|
152 | AssertReturn(idCpu < pUVM->pVM->cCpus, false);
|
---|
153 |
|
---|
154 | bool fInV86Code;
|
---|
155 | int rc = VMR3ReqPriorityCallWaitU(pUVM, idCpu, (PFNRT)dbgfR3CpuInV86Code, 3, pUVM->pVM, idCpu, &fInV86Code);
|
---|
156 | if (RT_FAILURE(rc))
|
---|
157 | return false;
|
---|
158 | return fInV86Code;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Get the number of CPUs (or threads if you insist).
|
---|
164 | *
|
---|
165 | * @returns The number of CPUs
|
---|
166 | * @param pUVM The user mode VM handle.
|
---|
167 | */
|
---|
168 | VMMR3DECL(VMCPUID) DBGFR3CpuGetCount(PUVM pUVM)
|
---|
169 | {
|
---|
170 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, 1);
|
---|
171 | return pUVM->cCpus;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Returns the state of the given CPU as a human readable string.
|
---|
177 | *
|
---|
178 | * @returns Pointer to the human readable CPU state string.
|
---|
179 | * @param pUVM The user mode VM handle.
|
---|
180 | * @param idCpu The target CPU ID.
|
---|
181 | */
|
---|
182 | VMMR3DECL(const char *) DBGFR3CpuGetState(PUVM pUVM, VMCPUID idCpu)
|
---|
183 | {
|
---|
184 | UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
|
---|
185 | VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, NULL);
|
---|
186 | AssertReturn(idCpu < pUVM->pVM->cCpus, NULL);
|
---|
187 |
|
---|
188 | PVMCPU pVCpu = VMMGetCpuById(pUVM->pVM, idCpu);
|
---|
189 | VMCPUSTATE enmCpuState = (VMCPUSTATE)ASMAtomicReadU32((volatile uint32_t *)&pVCpu->enmState);
|
---|
190 |
|
---|
191 | switch (enmCpuState)
|
---|
192 | {
|
---|
193 | case VMCPUSTATE_INVALID: return "<INVALID>";
|
---|
194 | case VMCPUSTATE_STOPPED: return "Stopped";
|
---|
195 | case VMCPUSTATE_STARTED: return "Started";
|
---|
196 | case VMCPUSTATE_STARTED_HM: return "Started (HM)";
|
---|
197 | case VMCPUSTATE_STARTED_EXEC: return "Started (Exec)";
|
---|
198 | case VMCPUSTATE_STARTED_EXEC_NEM: return "Started (Exec NEM)";
|
---|
199 | case VMCPUSTATE_STARTED_EXEC_NEM_WAIT: return "Started (Exec NEM Wait)";
|
---|
200 | case VMCPUSTATE_STARTED_EXEC_NEM_CANCELED: return "Started (Exec NEM Canceled)";
|
---|
201 | case VMCPUSTATE_STARTED_HALTED: return "Started (Halted)";
|
---|
202 | case VMCPUSTATE_END: return "END";
|
---|
203 | default: break;
|
---|
204 | }
|
---|
205 |
|
---|
206 | AssertMsgFailedReturn(("Unknown CPU state %u\n", enmCpuState), "<UNKNOWN>");
|
---|
207 | }
|
---|
208 |
|
---|