drm/vmwgfx: add ioctl for messaging from/to guest userspace to/from host
authorRoland Scheidegger <sroland@vmware.com>
Thu, 21 Nov 2019 16:44:56 +0000 (17:44 +0100)
committerThomas Hellstrom <thellstrom@vmware.com>
Wed, 15 Jan 2020 10:54:16 +0000 (11:54 +0100)
Up to now, guest userspace does logging directly to host using essentially
the same rather complex port assembly stuff as the kernel.
We'd rather use the same mechanism than duplicate it (it may also change in
the future), hence add a new ioctl for relaying guest/host messaging
(logging is just one application of it).

Signed-off-by: Roland Scheidegger <sroland@vmware.com>
Reviewed-by: Thomas Hellstrom <thellstrom@vmware.com>
Signed-off-by: Thomas Hellstrom <thellstrom@vmware.com>
drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
drivers/gpu/drm/vmwgfx/vmwgfx_drv.h
drivers/gpu/drm/vmwgfx/vmwgfx_msg.c
include/uapi/drm/vmwgfx_drm.h

index bc3d851..827458f 100644 (file)
 #define DRM_IOCTL_VMW_GB_SURFACE_REF_EXT                               \
        DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_GB_SURFACE_REF_EXT,         \
                union drm_vmw_gb_surface_reference_ext_arg)
+#define DRM_IOCTL_VMW_MSG                                              \
+       DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_MSG,                        \
+               struct drm_vmw_msg_arg)
 
 /**
  * The core DRM version of this macro doesn't account for
@@ -243,6 +246,9 @@ static const struct drm_ioctl_desc vmw_ioctls[] = {
        VMW_IOCTL_DEF(VMW_GB_SURFACE_REF_EXT,
                      vmw_gb_surface_reference_ext_ioctl,
                      DRM_RENDER_ALLOW),
+       VMW_IOCTL_DEF(VMW_MSG,
+                     vmw_msg_ioctl,
+                     DRM_RENDER_ALLOW),
 };
 
 static const struct pci_device_id vmw_pci_id_list[] = {
index a31e726..bfcb12d 100644 (file)
@@ -1403,6 +1403,8 @@ int vmw_bo_cpu_blit(struct ttm_buffer_object *dst,
 int vmw_host_get_guestinfo(const char *guest_info_param,
                           char *buffer, size_t *length);
 int vmw_host_log(const char *log);
+int vmw_msg_ioctl(struct drm_device *dev, void *data,
+                 struct drm_file *file_priv);
 
 /* VMW logging */
 
index d634411..e9f448a 100644 (file)
@@ -57,6 +57,8 @@
 
 #define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
 
+#define MAX_USER_MSG_LENGTH    PAGE_SIZE
+
 static u32 vmw_msg_enabled = 1;
 
 enum rpc_msg_type {
@@ -517,3 +519,84 @@ out_open:
 
        return -EINVAL;
 }
+
+
+/**
+ * vmw_msg_ioctl: Sends and receveives a message to/from host from/to user-space
+ *
+ * Sends a message from user-space to host.
+ * Can also receive a result from host and return that to user-space.
+ *
+ * @dev: Identifies the drm device.
+ * @data: Pointer to the ioctl argument.
+ * @file_priv: Identifies the caller.
+ * Return: Zero on success, negative error code on error.
+ */
+
+int vmw_msg_ioctl(struct drm_device *dev, void *data,
+                 struct drm_file *file_priv)
+{
+       struct drm_vmw_msg_arg *arg =
+               (struct drm_vmw_msg_arg *) data;
+       struct rpc_channel channel;
+       char *msg;
+       int length;
+
+       msg = kmalloc(MAX_USER_MSG_LENGTH, GFP_KERNEL);
+       if (!msg) {
+               DRM_ERROR("Cannot allocate memory for log message.\n");
+               return -ENOMEM;
+       }
+
+       length = strncpy_from_user(msg, (void __user *)((unsigned long)arg->send),
+                                  MAX_USER_MSG_LENGTH);
+       if (length < 0 || length >= MAX_USER_MSG_LENGTH) {
+               DRM_ERROR("Userspace message access failure.\n");
+               kfree(msg);
+               return -EINVAL;
+       }
+
+
+       if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM)) {
+               DRM_ERROR("Failed to open channel.\n");
+               goto out_open;
+       }
+
+       if (vmw_send_msg(&channel, msg)) {
+               DRM_ERROR("Failed to send message to host.\n");
+               goto out_msg;
+       }
+
+       if (!arg->send_only) {
+               char *reply = NULL;
+               size_t reply_len = 0;
+
+               if (vmw_recv_msg(&channel, (void *) &reply, &reply_len)) {
+                       DRM_ERROR("Failed to receive message from host.\n");
+                       goto out_msg;
+               }
+               if (reply && reply_len > 0) {
+                       if (copy_to_user((void __user *)((unsigned long)arg->receive),
+                                                        reply, reply_len)) {
+                               DRM_ERROR("Failed to copy message to userspace.\n");
+                               kfree(reply);
+                               goto out_msg;
+                       }
+                       arg->receive_len = (__u32)reply_len;
+               }
+               kfree(reply);
+       }
+
+       vmw_close_channel(&channel);
+       kfree(msg);
+
+       return 0;
+
+out_msg:
+       vmw_close_channel(&channel);
+out_open:
+       kfree(msg);
+
+       return -EINVAL;
+}
+
index 02cab33..fcb741e 100644 (file)
@@ -71,6 +71,7 @@ extern "C" {
 #define DRM_VMW_CREATE_EXTENDED_CONTEXT 26
 #define DRM_VMW_GB_SURFACE_CREATE_EXT   27
 #define DRM_VMW_GB_SURFACE_REF_EXT      28
+#define DRM_VMW_MSG                     29
 
 /*************************************************************************/
 /**
@@ -1213,6 +1214,22 @@ union drm_vmw_gb_surface_reference_ext_arg {
        struct drm_vmw_surface_arg req;
 };
 
+/**
+ * struct drm_vmw_msg_arg
+ *
+ * @send: Pointer to user-space msg string (null terminated).
+ * @receive: Pointer to user-space receive buffer.
+ * @send_only: Boolean whether this is only sending or receiving too.
+ *
+ * Argument to the DRM_VMW_MSG ioctl.
+ */
+struct drm_vmw_msg_arg {
+       __u64 send;
+       __u64 receive;
+       __s32 send_only;
+       __u32 receive_len;
+};
+
 #if defined(__cplusplus)
 }
 #endif