1 | /* $Id: VBoxCpuReportMsrLinux.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MsrLinux - Linux-specific MSR access.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2022 Oracle Corporation
|
---|
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 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include "VBoxCpuReport.h"
|
---|
23 |
|
---|
24 | #include <iprt/err.h>
|
---|
25 | #include <iprt/file.h>
|
---|
26 | #include <iprt/thread.h>
|
---|
27 |
|
---|
28 | #ifndef RT_OS_WINDOWS
|
---|
29 | # include <unistd.h>
|
---|
30 | #else /* RT_OS_WINDOWS: for test compiling this file on windows */
|
---|
31 | # include <io.h>
|
---|
32 | int pread(int, void *, size_t, off_t);
|
---|
33 | int pwrite(int, void const *, size_t, off_t);
|
---|
34 | #endif
|
---|
35 | #include <fcntl.h>
|
---|
36 | #include <errno.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Defined Constants And Macros *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | #define MSR_DEV_NAME "/dev/cpu/0/msr"
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************************************************************************
|
---|
46 | * Global Variables *
|
---|
47 | *********************************************************************************************************************************/
|
---|
48 | /** The /dev/xxx/msr file descriptor. */
|
---|
49 | static int g_fdMsr;
|
---|
50 |
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * @interface_method_impl{VBCPUREPMSRACCESSORS,pfnMsrProberRead}
|
---|
54 | */
|
---|
55 | static DECLCALLBACK(int) linuxMsrProberRead(uint32_t uMsr, RTCPUID idCpu, uint64_t *puValue, bool *pfGp)
|
---|
56 | {
|
---|
57 | int rc = VINF_SUCCESS;
|
---|
58 |
|
---|
59 | if (idCpu != NIL_RTCPUID)
|
---|
60 | return VERR_INVALID_PARAMETER;
|
---|
61 |
|
---|
62 | if (g_fdMsr < 0)
|
---|
63 | return VERR_INVALID_STATE;
|
---|
64 |
|
---|
65 | *pfGp = true;
|
---|
66 | if (pread(g_fdMsr, puValue, sizeof(*puValue), uMsr) != sizeof(*puValue))
|
---|
67 | rc = VERR_READ_ERROR;
|
---|
68 | else
|
---|
69 | *pfGp = false;
|
---|
70 |
|
---|
71 | return RT_SUCCESS(rc) && !pfGp;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * @interface_method_impl{VBCPUREPMSRACCESSORS,pfnMsrProberWrite}
|
---|
77 | */
|
---|
78 | static DECLCALLBACK(int) linuxMsrProberWrite(uint32_t uMsr, RTCPUID idCpu, uint64_t uValue, bool *pfGp)
|
---|
79 | {
|
---|
80 | int rc = VINF_SUCCESS;
|
---|
81 |
|
---|
82 | if (idCpu != NIL_RTCPUID)
|
---|
83 | return VERR_INVALID_PARAMETER;
|
---|
84 |
|
---|
85 | if (g_fdMsr < 0)
|
---|
86 | return VERR_INVALID_STATE;
|
---|
87 |
|
---|
88 | *pfGp = true;
|
---|
89 | if (pwrite(g_fdMsr, &uValue, sizeof(uValue), uMsr) != sizeof(uValue))
|
---|
90 | rc = VERR_WRITE_ERROR;
|
---|
91 | else
|
---|
92 | *pfGp = false;
|
---|
93 |
|
---|
94 | return RT_SUCCESS(rc) && !pfGp;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * @interface_method_impl{VBCPUREPMSRACCESSORS,pfnMsrProberModify}
|
---|
99 | */
|
---|
100 | static DECLCALLBACK(int) linuxMsrProberModify(uint32_t uMsr, RTCPUID idCpu, uint64_t fAndMask, uint64_t fOrMask,
|
---|
101 | PSUPMSRPROBERMODIFYRESULT pResult)
|
---|
102 | {
|
---|
103 | int rc = VINF_SUCCESS;
|
---|
104 | uint64_t uBefore, uWrite, uAfter;
|
---|
105 | int rcBefore, rcWrite, rcAfter, rcRestore;
|
---|
106 |
|
---|
107 | if (idCpu != NIL_RTCPUID)
|
---|
108 | return VERR_INVALID_PARAMETER;
|
---|
109 |
|
---|
110 | if (g_fdMsr < 0)
|
---|
111 | return VERR_INVALID_STATE;
|
---|
112 |
|
---|
113 | #if 0
|
---|
114 | vbCpuRepDebug("MSR %#x\n", uMsr);
|
---|
115 | RTThreadSleep(10);
|
---|
116 | #endif
|
---|
117 | rcBefore = pread(g_fdMsr, &uBefore, sizeof(uBefore), uMsr);
|
---|
118 | uWrite = (uBefore & fAndMask) | fOrMask;
|
---|
119 | rcWrite = pwrite(g_fdMsr, &uWrite, sizeof(uWrite), uMsr);
|
---|
120 | rcAfter = pread(g_fdMsr, &uAfter, sizeof(uAfter), uMsr);
|
---|
121 | rcRestore = pwrite(g_fdMsr, &uBefore, sizeof(uBefore), uMsr);
|
---|
122 |
|
---|
123 | #if 0
|
---|
124 | vbCpuRepDebug("MSR: %#x, %#llx -> %#llx -> %#llx (%d/%d/%d/%d)\n",
|
---|
125 | uMsr, uBefore, uWrite, uAfter,
|
---|
126 | rcBefore, rcWrite != sizeof(uWrite), rcAfter, rcRestore);
|
---|
127 | #endif
|
---|
128 | pResult->uBefore = uBefore;
|
---|
129 | pResult->uWritten = uWrite;
|
---|
130 | pResult->uAfter = uAfter;
|
---|
131 | pResult->fBeforeGp = rcBefore != sizeof(uBefore);
|
---|
132 | pResult->fModifyGp = rcWrite != sizeof(uWrite);
|
---|
133 | pResult->fAfterGp = rcAfter != sizeof(uAfter);
|
---|
134 | pResult->fRestoreGp = rcRestore != sizeof(uBefore);
|
---|
135 |
|
---|
136 | return rc;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * @interface_method_impl{VBCPUREPMSRACCESSORS,pfnTerm}
|
---|
142 | */
|
---|
143 | static DECLCALLBACK(void) linuxMsrProberTerm(void)
|
---|
144 | {
|
---|
145 | if (g_fdMsr >= 0)
|
---|
146 | {
|
---|
147 | close(g_fdMsr);
|
---|
148 | g_fdMsr = -1;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | int VbCpuRepMsrProberInitPlatform(PVBCPUREPMSRACCESSORS pMsrAccessors)
|
---|
153 | {
|
---|
154 | RTFILE hFile;
|
---|
155 | int rc = RTFileOpen(&hFile, MSR_DEV_NAME, RTFILE_O_READWRITE | RTFILE_O_DENY_NONE | RTFILE_O_OPEN);
|
---|
156 | if (RT_SUCCESS(rc))
|
---|
157 | {
|
---|
158 | g_fdMsr = RTFileToNative(hFile);
|
---|
159 | Assert(g_fdMsr != -1);
|
---|
160 |
|
---|
161 | pMsrAccessors->fAtomic = false; /* Can't modify/restore MSRs without trip to R3. */
|
---|
162 | pMsrAccessors->pfnMsrProberRead = linuxMsrProberRead;
|
---|
163 | pMsrAccessors->pfnMsrProberWrite = linuxMsrProberWrite;
|
---|
164 | pMsrAccessors->pfnMsrProberModify = linuxMsrProberModify;
|
---|
165 | pMsrAccessors->pfnTerm = linuxMsrProberTerm;
|
---|
166 | return VINF_SUCCESS;
|
---|
167 | }
|
---|
168 | vbCpuRepDebug("warning: Failed to open " MSR_DEV_NAME ": %Rrc\n", rc);
|
---|
169 | return rc;
|
---|
170 | }
|
---|