1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright 2016 VMware, Inc., Palo Alto, CA., USA
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <linux/objtool.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/mem_encrypt.h>
33 #include <asm/hypervisor.h>
35 #include "vmwgfx_drv.h"
36 #include "vmwgfx_msg_x86.h"
37 #include "vmwgfx_msg_arm64.h"
39 #define MESSAGE_STATUS_SUCCESS 0x0001
40 #define MESSAGE_STATUS_DORECV 0x0002
41 #define MESSAGE_STATUS_CPT 0x0010
42 #define MESSAGE_STATUS_HB 0x0080
44 #define RPCI_PROTOCOL_NUM 0x49435052
45 #define GUESTMSG_FLAG_COOKIE 0x80000000
49 #define VMW_HYPERVISOR_MAGIC 0x564D5868
51 #define VMW_PORT_CMD_MSG 30
52 #define VMW_PORT_CMD_HB_MSG 0
53 #define VMW_PORT_CMD_OPEN_CHANNEL (MSG_TYPE_OPEN << 16 | VMW_PORT_CMD_MSG)
54 #define VMW_PORT_CMD_CLOSE_CHANNEL (MSG_TYPE_CLOSE << 16 | VMW_PORT_CMD_MSG)
55 #define VMW_PORT_CMD_SENDSIZE (MSG_TYPE_SENDSIZE << 16 | VMW_PORT_CMD_MSG)
56 #define VMW_PORT_CMD_RECVSIZE (MSG_TYPE_RECVSIZE << 16 | VMW_PORT_CMD_MSG)
57 #define VMW_PORT_CMD_RECVSTATUS (MSG_TYPE_RECVSTATUS << 16 | VMW_PORT_CMD_MSG)
59 #define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
61 #define MAX_USER_MSG_LENGTH PAGE_SIZE
63 static u32 vmw_msg_enabled = 1;
86 * @channel: RPC channel
89 * Returns: 0 on success
91 static int vmw_open_channel(struct rpc_channel *channel, unsigned int protocol)
93 unsigned long eax, ebx, ecx, edx, si = 0, di = 0;
95 VMW_PORT(VMW_PORT_CMD_OPEN_CHANNEL,
96 (protocol | GUESTMSG_FLAG_COOKIE), si, di,
99 eax, ebx, ecx, edx, si, di);
101 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
104 channel->channel_id = HIGH_WORD(edx);
105 channel->cookie_high = si;
106 channel->cookie_low = di;
116 * @channel: RPC channel
118 * Returns: 0 on success
120 static int vmw_close_channel(struct rpc_channel *channel)
122 unsigned long eax, ebx, ecx, edx, si, di;
124 /* Set up additional parameters */
125 si = channel->cookie_high;
126 di = channel->cookie_low;
128 VMW_PORT(VMW_PORT_CMD_CLOSE_CHANNEL,
130 channel->channel_id << 16,
131 VMW_HYPERVISOR_MAGIC,
132 eax, ebx, ecx, edx, si, di);
134 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
141 * vmw_port_hb_out - Send the message payload either through the
142 * high-bandwidth port if available, or through the backdoor otherwise.
143 * @channel: The rpc channel.
144 * @msg: NULL-terminated message.
145 * @hb: Whether the high-bandwidth port is available.
147 * Return: The port status.
149 static unsigned long vmw_port_hb_out(struct rpc_channel *channel,
150 const char *msg, bool hb)
152 unsigned long si, di, eax, ebx, ecx, edx;
153 unsigned long msg_len = strlen(msg);
155 /* HB port can't access encrypted memory. */
156 if (hb && !mem_encrypt_active()) {
157 unsigned long bp = channel->cookie_high;
159 si = (uintptr_t) msg;
160 di = channel->cookie_low;
163 (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
165 VMWARE_HYPERVISOR_HB | (channel->channel_id << 16) |
166 VMWARE_HYPERVISOR_OUT,
167 VMW_HYPERVISOR_MAGIC, bp,
168 eax, ebx, ecx, edx, si, di);
173 /* HB port not available. Send the message 4 bytes at a time. */
174 ecx = MESSAGE_STATUS_SUCCESS << 16;
175 while (msg_len && (HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS)) {
176 unsigned int bytes = min_t(size_t, msg_len, 4);
177 unsigned long word = 0;
179 memcpy(&word, msg, bytes);
182 si = channel->cookie_high;
183 di = channel->cookie_low;
185 VMW_PORT(VMW_PORT_CMD_MSG | (MSG_TYPE_SENDPAYLOAD << 16),
187 channel->channel_id << 16,
188 VMW_HYPERVISOR_MAGIC,
189 eax, ebx, ecx, edx, si, di);
196 * vmw_port_hb_in - Receive the message payload either through the
197 * high-bandwidth port if available, or through the backdoor otherwise.
198 * @channel: The rpc channel.
199 * @reply: Pointer to buffer holding reply.
200 * @reply_len: Length of the reply.
201 * @hb: Whether the high-bandwidth port is available.
203 * Return: The port status.
205 static unsigned long vmw_port_hb_in(struct rpc_channel *channel, char *reply,
206 unsigned long reply_len, bool hb)
208 unsigned long si, di, eax, ebx, ecx, edx;
210 /* HB port can't access encrypted memory */
211 if (hb && !mem_encrypt_active()) {
212 unsigned long bp = channel->cookie_low;
214 si = channel->cookie_high;
215 di = (uintptr_t) reply;
218 (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
220 VMWARE_HYPERVISOR_HB | (channel->channel_id << 16),
221 VMW_HYPERVISOR_MAGIC, bp,
222 eax, ebx, ecx, edx, si, di);
227 /* HB port not available. Retrieve the message 4 bytes at a time. */
228 ecx = MESSAGE_STATUS_SUCCESS << 16;
230 unsigned int bytes = min_t(unsigned long, reply_len, 4);
232 si = channel->cookie_high;
233 di = channel->cookie_low;
235 VMW_PORT(VMW_PORT_CMD_MSG | (MSG_TYPE_RECVPAYLOAD << 16),
236 MESSAGE_STATUS_SUCCESS, si, di,
237 channel->channel_id << 16,
238 VMW_HYPERVISOR_MAGIC,
239 eax, ebx, ecx, edx, si, di);
241 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
244 memcpy(reply, &ebx, bytes);
254 * vmw_send_msg: Sends a message to the host
256 * @channel: RPC channel
257 * @msg: NULL terminated string
259 * Returns: 0 on success
261 static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
263 unsigned long eax, ebx, ecx, edx, si, di;
264 size_t msg_len = strlen(msg);
267 while (retries < RETRIES) {
270 /* Set up additional parameters */
271 si = channel->cookie_high;
272 di = channel->cookie_low;
274 VMW_PORT(VMW_PORT_CMD_SENDSIZE,
276 channel->channel_id << 16,
277 VMW_HYPERVISOR_MAGIC,
278 eax, ebx, ecx, edx, si, di);
280 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
281 /* Expected success. Give up. */
286 ebx = vmw_port_hb_out(channel, msg,
287 !!(HIGH_WORD(ecx) & MESSAGE_STATUS_HB));
289 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) != 0) {
291 } else if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
292 /* A checkpoint occurred. Retry. */
301 STACK_FRAME_NON_STANDARD(vmw_send_msg);
305 * vmw_recv_msg: Receives a message from the host
307 * Note: It is the caller's responsibility to call kfree() on msg.
309 * @channel: channel opened by vmw_open_channel
310 * @msg: [OUT] message received from the host
311 * @msg_len: message length
313 static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
316 unsigned long eax, ebx, ecx, edx, si, di;
325 while (retries < RETRIES) {
328 /* Set up additional parameters */
329 si = channel->cookie_high;
330 di = channel->cookie_low;
332 VMW_PORT(VMW_PORT_CMD_RECVSIZE,
334 channel->channel_id << 16,
335 VMW_HYPERVISOR_MAGIC,
336 eax, ebx, ecx, edx, si, di);
338 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
339 DRM_ERROR("Failed to get reply size for host message.\n");
343 /* No reply available. This is okay. */
344 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
348 reply = kzalloc(reply_len + 1, GFP_KERNEL);
350 DRM_ERROR("Cannot allocate memory for host message reply.\n");
356 ebx = vmw_port_hb_in(channel, reply, reply_len,
357 !!(HIGH_WORD(ecx) & MESSAGE_STATUS_HB));
358 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) {
361 if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
362 /* A checkpoint occurred. Retry. */
369 reply[reply_len] = '\0';
373 si = channel->cookie_high;
374 di = channel->cookie_low;
376 VMW_PORT(VMW_PORT_CMD_RECVSTATUS,
377 MESSAGE_STATUS_SUCCESS, si, di,
378 channel->channel_id << 16,
379 VMW_HYPERVISOR_MAGIC,
380 eax, ebx, ecx, edx, si, di);
382 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
385 if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
386 /* A checkpoint occurred. Retry. */
399 *msg_len = reply_len;
404 STACK_FRAME_NON_STANDARD(vmw_recv_msg);
408 * vmw_host_get_guestinfo: Gets a GuestInfo parameter
410 * Gets the value of a GuestInfo.* parameter. The value returned will be in
411 * a string, and it is up to the caller to post-process.
413 * @guest_info_param: Parameter to get, e.g. GuestInfo.svga.gl3
414 * @buffer: if NULL, *reply_len will contain reply size.
415 * @length: size of the reply_buf. Set to size of reply upon return
417 * Returns: 0 on success
419 int vmw_host_get_guestinfo(const char *guest_info_param,
420 char *buffer, size_t *length)
422 struct rpc_channel channel;
423 char *msg, *reply = NULL;
424 size_t reply_len = 0;
426 if (!vmw_msg_enabled)
429 if (!guest_info_param || !length)
432 msg = kasprintf(GFP_KERNEL, "info-get %s", guest_info_param);
434 DRM_ERROR("Cannot allocate memory to get guest info \"%s\".",
439 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
442 if (vmw_send_msg(&channel, msg) ||
443 vmw_recv_msg(&channel, (void *) &reply, &reply_len))
446 vmw_close_channel(&channel);
447 if (buffer && reply && reply_len > 0) {
448 /* Remove reply code, which are the first 2 characters of
451 reply_len = max(reply_len - 2, (size_t) 0);
452 reply_len = min(reply_len, *length);
455 memcpy(buffer, reply + 2, reply_len);
466 vmw_close_channel(&channel);
471 DRM_ERROR("Failed to get guest info \"%s\".", guest_info_param);
478 * vmw_host_printf: Sends a log message to the host
480 * @fmt: Regular printf format string and arguments
482 * Returns: 0 on success
485 int vmw_host_printf(const char *fmt, ...)
488 struct rpc_channel channel;
493 if (!vmw_msg_enabled)
500 log = kvasprintf(GFP_KERNEL, fmt, ap);
503 DRM_ERROR("Cannot allocate memory for the log message.\n");
507 msg = kasprintf(GFP_KERNEL, "log %s", log);
509 DRM_ERROR("Cannot allocate memory for host log message.\n");
514 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
517 if (vmw_send_msg(&channel, msg))
520 vmw_close_channel(&channel);
527 vmw_close_channel(&channel);
531 DRM_ERROR("Failed to send host log message.\n");
538 * vmw_msg_ioctl: Sends and receveives a message to/from host from/to user-space
540 * Sends a message from user-space to host.
541 * Can also receive a result from host and return that to user-space.
543 * @dev: Identifies the drm device.
544 * @data: Pointer to the ioctl argument.
545 * @file_priv: Identifies the caller.
546 * Return: Zero on success, negative error code on error.
549 int vmw_msg_ioctl(struct drm_device *dev, void *data,
550 struct drm_file *file_priv)
552 struct drm_vmw_msg_arg *arg =
553 (struct drm_vmw_msg_arg *)data;
554 struct rpc_channel channel;
558 msg = kmalloc(MAX_USER_MSG_LENGTH, GFP_KERNEL);
560 DRM_ERROR("Cannot allocate memory for log message.\n");
564 length = strncpy_from_user(msg, (void __user *)((unsigned long)arg->send),
565 MAX_USER_MSG_LENGTH);
566 if (length < 0 || length >= MAX_USER_MSG_LENGTH) {
567 DRM_ERROR("Userspace message access failure.\n");
573 if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM)) {
574 DRM_ERROR("Failed to open channel.\n");
578 if (vmw_send_msg(&channel, msg)) {
579 DRM_ERROR("Failed to send message to host.\n");
583 if (!arg->send_only) {
585 size_t reply_len = 0;
587 if (vmw_recv_msg(&channel, (void *) &reply, &reply_len)) {
588 DRM_ERROR("Failed to receive message from host.\n");
591 if (reply && reply_len > 0) {
592 if (copy_to_user((void __user *)((unsigned long)arg->receive),
594 DRM_ERROR("Failed to copy message to userspace.\n");
598 arg->receive_len = (__u32)reply_len;
603 vmw_close_channel(&channel);
609 vmw_close_channel(&channel);