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