Skip to content

Commit 7ac63f6

Browse files
committed
Merge branch 'x86-vmware-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 vmware updates from Ingo Molnar: "This updates the VMWARE guest driver with support for VMCALL/VMMCALL based hypercalls" * 'x86-vmware-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: input/vmmouse: Update the backdoor call with support for new instructions drm/vmwgfx: Update the backdoor call with support for new instructions x86/vmware: Add a header file for hypercall definitions x86/vmware: Update platform detection code for VMCALL/VMMCALL hypercalls
2 parents e2bddc2 + f7b15c7 commit 7ac63f6

File tree

7 files changed

+163
-49
lines changed

7 files changed

+163
-49
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17193,6 +17193,7 @@ M: "VMware, Inc." <[email protected]>
1719317193
1719417194
S: Supported
1719517195
F: arch/x86/kernel/cpu/vmware.c
17196+
F: arch/x86/include/asm/vmware.h
1719617197

1719717198
VMWARE PVRDMA DRIVER
1719817199
M: Adit Ranadive <[email protected]>

arch/x86/include/asm/cpufeatures.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@
231231
#define X86_FEATURE_VMMCALL ( 8*32+15) /* Prefer VMMCALL to VMCALL */
232232
#define X86_FEATURE_XENPV ( 8*32+16) /* "" Xen paravirtual guest */
233233
#define X86_FEATURE_EPT_AD ( 8*32+17) /* Intel Extended Page Table access-dirty bit */
234+
#define X86_FEATURE_VMCALL ( 8*32+18) /* "" Hypervisor supports the VMCALL instruction */
235+
#define X86_FEATURE_VMW_VMMCALL ( 8*32+19) /* "" VMware prefers VMMCALL hypercall instruction */
234236

235237
/* Intel-defined CPU features, CPUID level 0x00000007:0 (EBX), word 9 */
236238
#define X86_FEATURE_FSGSBASE ( 9*32+ 0) /* RDFSBASE, WRFSBASE, RDGSBASE, WRGSBASE instructions*/

arch/x86/include/asm/vmware.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* SPDX-License-Identifier: GPL-2.0 or MIT */
2+
#ifndef _ASM_X86_VMWARE_H
3+
#define _ASM_X86_VMWARE_H
4+
5+
#include <asm/cpufeatures.h>
6+
#include <asm/alternative.h>
7+
8+
/*
9+
* The hypercall definitions differ in the low word of the %edx argument
10+
* in the following way: the old port base interface uses the port
11+
* number to distinguish between high- and low bandwidth versions.
12+
*
13+
* The new vmcall interface instead uses a set of flags to select
14+
* bandwidth mode and transfer direction. The flags should be loaded
15+
* into %dx by any user and are automatically replaced by the port
16+
* number if the VMWARE_HYPERVISOR_PORT method is used.
17+
*
18+
* In short, new driver code should strictly use the new definition of
19+
* %dx content.
20+
*/
21+
22+
/* Old port-based version */
23+
#define VMWARE_HYPERVISOR_PORT "0x5658"
24+
#define VMWARE_HYPERVISOR_PORT_HB "0x5659"
25+
26+
/* Current vmcall / vmmcall version */
27+
#define VMWARE_HYPERVISOR_HB BIT(0)
28+
#define VMWARE_HYPERVISOR_OUT BIT(1)
29+
30+
/* The low bandwidth call. The low word of edx is presumed clear. */
31+
#define VMWARE_HYPERCALL \
32+
ALTERNATIVE_2("movw $" VMWARE_HYPERVISOR_PORT ", %%dx; inl (%%dx)", \
33+
"vmcall", X86_FEATURE_VMCALL, \
34+
"vmmcall", X86_FEATURE_VMW_VMMCALL)
35+
36+
/*
37+
* The high bandwidth out call. The low word of edx is presumed to have the
38+
* HB and OUT bits set.
39+
*/
40+
#define VMWARE_HYPERCALL_HB_OUT \
41+
ALTERNATIVE_2("movw $" VMWARE_HYPERVISOR_PORT_HB ", %%dx; rep outsb", \
42+
"vmcall", X86_FEATURE_VMCALL, \
43+
"vmmcall", X86_FEATURE_VMW_VMMCALL)
44+
45+
/*
46+
* The high bandwidth in call. The low word of edx is presumed to have the
47+
* HB bit set.
48+
*/
49+
#define VMWARE_HYPERCALL_HB_IN \
50+
ALTERNATIVE_2("movw $" VMWARE_HYPERVISOR_PORT_HB ", %%dx; rep insb", \
51+
"vmcall", X86_FEATURE_VMCALL, \
52+
"vmmcall", X86_FEATURE_VMW_VMMCALL)
53+
#endif

arch/x86/kernel/cpu/vmware.c

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,69 @@
3030
#include <asm/hypervisor.h>
3131
#include <asm/timer.h>
3232
#include <asm/apic.h>
33+
#include <asm/vmware.h>
3334

3435
#undef pr_fmt
3536
#define pr_fmt(fmt) "vmware: " fmt
3637

37-
#define CPUID_VMWARE_INFO_LEAF 0x40000000
38+
#define CPUID_VMWARE_INFO_LEAF 0x40000000
39+
#define CPUID_VMWARE_FEATURES_LEAF 0x40000010
40+
#define CPUID_VMWARE_FEATURES_ECX_VMMCALL BIT(0)
41+
#define CPUID_VMWARE_FEATURES_ECX_VMCALL BIT(1)
42+
3843
#define VMWARE_HYPERVISOR_MAGIC 0x564D5868
39-
#define VMWARE_HYPERVISOR_PORT 0x5658
4044

41-
#define VMWARE_PORT_CMD_GETVERSION 10
42-
#define VMWARE_PORT_CMD_GETHZ 45
43-
#define VMWARE_PORT_CMD_GETVCPU_INFO 68
44-
#define VMWARE_PORT_CMD_LEGACY_X2APIC 3
45-
#define VMWARE_PORT_CMD_VCPU_RESERVED 31
45+
#define VMWARE_CMD_GETVERSION 10
46+
#define VMWARE_CMD_GETHZ 45
47+
#define VMWARE_CMD_GETVCPU_INFO 68
48+
#define VMWARE_CMD_LEGACY_X2APIC 3
49+
#define VMWARE_CMD_VCPU_RESERVED 31
4650

4751
#define VMWARE_PORT(cmd, eax, ebx, ecx, edx) \
4852
__asm__("inl (%%dx)" : \
49-
"=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) : \
50-
"0"(VMWARE_HYPERVISOR_MAGIC), \
51-
"1"(VMWARE_PORT_CMD_##cmd), \
52-
"2"(VMWARE_HYPERVISOR_PORT), "3"(UINT_MAX) : \
53-
"memory");
53+
"=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) : \
54+
"a"(VMWARE_HYPERVISOR_MAGIC), \
55+
"c"(VMWARE_CMD_##cmd), \
56+
"d"(VMWARE_HYPERVISOR_PORT), "b"(UINT_MAX) : \
57+
"memory")
58+
59+
#define VMWARE_VMCALL(cmd, eax, ebx, ecx, edx) \
60+
__asm__("vmcall" : \
61+
"=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) : \
62+
"a"(VMWARE_HYPERVISOR_MAGIC), \
63+
"c"(VMWARE_CMD_##cmd), \
64+
"d"(0), "b"(UINT_MAX) : \
65+
"memory")
66+
67+
#define VMWARE_VMMCALL(cmd, eax, ebx, ecx, edx) \
68+
__asm__("vmmcall" : \
69+
"=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) : \
70+
"a"(VMWARE_HYPERVISOR_MAGIC), \
71+
"c"(VMWARE_CMD_##cmd), \
72+
"d"(0), "b"(UINT_MAX) : \
73+
"memory")
74+
75+
#define VMWARE_CMD(cmd, eax, ebx, ecx, edx) do { \
76+
switch (vmware_hypercall_mode) { \
77+
case CPUID_VMWARE_FEATURES_ECX_VMCALL: \
78+
VMWARE_VMCALL(cmd, eax, ebx, ecx, edx); \
79+
break; \
80+
case CPUID_VMWARE_FEATURES_ECX_VMMCALL: \
81+
VMWARE_VMMCALL(cmd, eax, ebx, ecx, edx); \
82+
break; \
83+
default: \
84+
VMWARE_PORT(cmd, eax, ebx, ecx, edx); \
85+
break; \
86+
} \
87+
} while (0)
5488

5589
static unsigned long vmware_tsc_khz __ro_after_init;
90+
static u8 vmware_hypercall_mode __ro_after_init;
5691

5792
static inline int __vmware_platform(void)
5893
{
5994
uint32_t eax, ebx, ecx, edx;
60-
VMWARE_PORT(GETVERSION, eax, ebx, ecx, edx);
95+
VMWARE_CMD(GETVERSION, eax, ebx, ecx, edx);
6196
return eax != (uint32_t)-1 && ebx == VMWARE_HYPERVISOR_MAGIC;
6297
}
6398

@@ -129,14 +164,18 @@ static void __init vmware_set_capabilities(void)
129164
{
130165
setup_force_cpu_cap(X86_FEATURE_CONSTANT_TSC);
131166
setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
167+
if (vmware_hypercall_mode == CPUID_VMWARE_FEATURES_ECX_VMCALL)
168+
setup_force_cpu_cap(X86_FEATURE_VMCALL);
169+
else if (vmware_hypercall_mode == CPUID_VMWARE_FEATURES_ECX_VMMCALL)
170+
setup_force_cpu_cap(X86_FEATURE_VMW_VMMCALL);
132171
}
133172

134173
static void __init vmware_platform_setup(void)
135174
{
136175
uint32_t eax, ebx, ecx, edx;
137176
uint64_t lpj, tsc_khz;
138177

139-
VMWARE_PORT(GETHZ, eax, ebx, ecx, edx);
178+
VMWARE_CMD(GETHZ, eax, ebx, ecx, edx);
140179

141180
if (ebx != UINT_MAX) {
142181
lpj = tsc_khz = eax | (((uint64_t)ebx) << 32);
@@ -174,10 +213,21 @@ static void __init vmware_platform_setup(void)
174213
vmware_set_capabilities();
175214
}
176215

216+
static u8 vmware_select_hypercall(void)
217+
{
218+
int eax, ebx, ecx, edx;
219+
220+
cpuid(CPUID_VMWARE_FEATURES_LEAF, &eax, &ebx, &ecx, &edx);
221+
return (ecx & (CPUID_VMWARE_FEATURES_ECX_VMMCALL |
222+
CPUID_VMWARE_FEATURES_ECX_VMCALL));
223+
}
224+
177225
/*
178226
* While checking the dmi string information, just checking the product
179227
* serial key should be enough, as this will always have a VMware
180228
* specific string when running under VMware hypervisor.
229+
* If !boot_cpu_has(X86_FEATURE_HYPERVISOR), vmware_hypercall_mode
230+
* intentionally defaults to 0.
181231
*/
182232
static uint32_t __init vmware_platform(void)
183233
{
@@ -187,8 +237,16 @@ static uint32_t __init vmware_platform(void)
187237

188238
cpuid(CPUID_VMWARE_INFO_LEAF, &eax, &hyper_vendor_id[0],
189239
&hyper_vendor_id[1], &hyper_vendor_id[2]);
190-
if (!memcmp(hyper_vendor_id, "VMwareVMware", 12))
240+
if (!memcmp(hyper_vendor_id, "VMwareVMware", 12)) {
241+
if (eax >= CPUID_VMWARE_FEATURES_LEAF)
242+
vmware_hypercall_mode =
243+
vmware_select_hypercall();
244+
245+
pr_info("hypercall mode: 0x%02x\n",
246+
(unsigned int) vmware_hypercall_mode);
247+
191248
return CPUID_VMWARE_INFO_LEAF;
249+
}
192250
} else if (dmi_available && dmi_name_in_serial("VMware") &&
193251
__vmware_platform())
194252
return 1;
@@ -200,9 +258,9 @@ static uint32_t __init vmware_platform(void)
200258
static bool __init vmware_legacy_x2apic_available(void)
201259
{
202260
uint32_t eax, ebx, ecx, edx;
203-
VMWARE_PORT(GETVCPU_INFO, eax, ebx, ecx, edx);
204-
return (eax & (1 << VMWARE_PORT_CMD_VCPU_RESERVED)) == 0 &&
205-
(eax & (1 << VMWARE_PORT_CMD_LEGACY_X2APIC)) != 0;
261+
VMWARE_CMD(GETVCPU_INFO, eax, ebx, ecx, edx);
262+
return (eax & (1 << VMWARE_CMD_VCPU_RESERVED)) == 0 &&
263+
(eax & (1 << VMWARE_CMD_LEGACY_X2APIC)) != 0;
206264
}
207265

208266
const __initconst struct hypervisor_x86 x86_hyper_vmware = {

drivers/gpu/drm/vmwgfx/vmwgfx_msg.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
#define RETRIES 3
4747

4848
#define VMW_HYPERVISOR_MAGIC 0x564D5868
49-
#define VMW_HYPERVISOR_PORT 0x5658
50-
#define VMW_HYPERVISOR_HB_PORT 0x5659
5149

5250
#define VMW_PORT_CMD_MSG 30
5351
#define VMW_PORT_CMD_HB_MSG 0
@@ -93,7 +91,7 @@ static int vmw_open_channel(struct rpc_channel *channel, unsigned int protocol)
9391

9492
VMW_PORT(VMW_PORT_CMD_OPEN_CHANNEL,
9593
(protocol | GUESTMSG_FLAG_COOKIE), si, di,
96-
VMW_HYPERVISOR_PORT,
94+
0,
9795
VMW_HYPERVISOR_MAGIC,
9896
eax, ebx, ecx, edx, si, di);
9997

@@ -126,7 +124,7 @@ static int vmw_close_channel(struct rpc_channel *channel)
126124

127125
VMW_PORT(VMW_PORT_CMD_CLOSE_CHANNEL,
128126
0, si, di,
129-
(VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
127+
channel->channel_id << 16,
130128
VMW_HYPERVISOR_MAGIC,
131129
eax, ebx, ecx, edx, si, di);
132130

@@ -160,7 +158,8 @@ static unsigned long vmw_port_hb_out(struct rpc_channel *channel,
160158
VMW_PORT_HB_OUT(
161159
(MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
162160
msg_len, si, di,
163-
VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
161+
VMWARE_HYPERVISOR_HB | (channel->channel_id << 16) |
162+
VMWARE_HYPERVISOR_OUT,
164163
VMW_HYPERVISOR_MAGIC, bp,
165164
eax, ebx, ecx, edx, si, di);
166165

@@ -181,7 +180,7 @@ static unsigned long vmw_port_hb_out(struct rpc_channel *channel,
181180

182181
VMW_PORT(VMW_PORT_CMD_MSG | (MSG_TYPE_SENDPAYLOAD << 16),
183182
word, si, di,
184-
VMW_HYPERVISOR_PORT | (channel->channel_id << 16),
183+
channel->channel_id << 16,
185184
VMW_HYPERVISOR_MAGIC,
186185
eax, ebx, ecx, edx, si, di);
187186
}
@@ -213,7 +212,7 @@ static unsigned long vmw_port_hb_in(struct rpc_channel *channel, char *reply,
213212
VMW_PORT_HB_IN(
214213
(MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
215214
reply_len, si, di,
216-
VMW_HYPERVISOR_HB_PORT | (channel->channel_id << 16),
215+
VMWARE_HYPERVISOR_HB | (channel->channel_id << 16),
217216
VMW_HYPERVISOR_MAGIC, bp,
218217
eax, ebx, ecx, edx, si, di);
219218

@@ -230,7 +229,7 @@ static unsigned long vmw_port_hb_in(struct rpc_channel *channel, char *reply,
230229

231230
VMW_PORT(VMW_PORT_CMD_MSG | (MSG_TYPE_RECVPAYLOAD << 16),
232231
MESSAGE_STATUS_SUCCESS, si, di,
233-
VMW_HYPERVISOR_PORT | (channel->channel_id << 16),
232+
channel->channel_id << 16,
234233
VMW_HYPERVISOR_MAGIC,
235234
eax, ebx, ecx, edx, si, di);
236235

@@ -269,7 +268,7 @@ static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
269268

270269
VMW_PORT(VMW_PORT_CMD_SENDSIZE,
271270
msg_len, si, di,
272-
VMW_HYPERVISOR_PORT | (channel->channel_id << 16),
271+
channel->channel_id << 16,
273272
VMW_HYPERVISOR_MAGIC,
274273
eax, ebx, ecx, edx, si, di);
275274

@@ -327,7 +326,7 @@ static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
327326

328327
VMW_PORT(VMW_PORT_CMD_RECVSIZE,
329328
0, si, di,
330-
(VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
329+
channel->channel_id << 16,
331330
VMW_HYPERVISOR_MAGIC,
332331
eax, ebx, ecx, edx, si, di);
333332

@@ -371,7 +370,7 @@ static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
371370

372371
VMW_PORT(VMW_PORT_CMD_RECVSTATUS,
373372
MESSAGE_STATUS_SUCCESS, si, di,
374-
(VMW_HYPERVISOR_PORT | (channel->channel_id << 16)),
373+
channel->channel_id << 16,
375374
VMW_HYPERVISOR_MAGIC,
376375
eax, ebx, ecx, edx, si, di);
377376

0 commit comments

Comments
 (0)