Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: (GPL-2.0 OR CDDL-1.0) */
0002 /* Copyright (C) 2010-2016 Oracle Corporation */
0003 
0004 #ifndef __VBOXGUEST_CORE_H__
0005 #define __VBOXGUEST_CORE_H__
0006 
0007 #include <linux/input.h>
0008 #include <linux/interrupt.h>
0009 #include <linux/kernel.h>
0010 #include <linux/list.h>
0011 #include <linux/miscdevice.h>
0012 #include <linux/spinlock.h>
0013 #include <linux/wait.h>
0014 #include <linux/workqueue.h>
0015 #include <linux/vboxguest.h>
0016 #include "vmmdev.h"
0017 
0018 /*
0019  * The mainline kernel version (this version) of the vboxguest module
0020  * contained a bug where it defined VBGL_IOCTL_VMMDEV_REQUEST_BIG and
0021  * VBGL_IOCTL_LOG using _IOC(_IOC_READ | _IOC_WRITE, 'V', ...) instead
0022  * of _IO(V, ...) as the out of tree VirtualBox upstream version does.
0023  *
0024  * These _ALT definitions keep compatibility with the wrong defines the
0025  * mainline kernel version used for a while.
0026  * Note the VirtualBox userspace bits have always been built against
0027  * VirtualBox upstream's headers, so this is likely not necessary. But
0028  * we must never break our ABI so we keep these around to be 100% sure.
0029  */
0030 #define VBG_IOCTL_VMMDEV_REQUEST_BIG_ALT _IOC(_IOC_READ | _IOC_WRITE, 'V', 3, 0)
0031 #define VBG_IOCTL_LOG_ALT(s)             _IOC(_IOC_READ | _IOC_WRITE, 'V', 9, s)
0032 
0033 struct vbg_session;
0034 
0035 /** VBox guest memory balloon. */
0036 struct vbg_mem_balloon {
0037     /** Work handling VMMDEV_EVENT_BALLOON_CHANGE_REQUEST events */
0038     struct work_struct work;
0039     /** Pre-allocated vmmdev_memballoon_info req for query */
0040     struct vmmdev_memballoon_info *get_req;
0041     /** Pre-allocated vmmdev_memballoon_change req for inflate / deflate */
0042     struct vmmdev_memballoon_change *change_req;
0043     /** The current number of chunks in the balloon. */
0044     u32 chunks;
0045     /** The maximum number of chunks in the balloon. */
0046     u32 max_chunks;
0047     /**
0048      * Array of pointers to page arrays. A page * array is allocated for
0049      * each chunk when inflating, and freed when the deflating.
0050      */
0051     struct page ***pages;
0052 };
0053 
0054 /**
0055  * Per bit usage tracker for a u32 mask.
0056  *
0057  * Used for optimal handling of guest properties and event filter.
0058  */
0059 struct vbg_bit_usage_tracker {
0060     /** Per bit usage counters. */
0061     u32 per_bit_usage[32];
0062     /** The current mask according to per_bit_usage. */
0063     u32 mask;
0064 };
0065 
0066 /** VBox guest device (data) extension. */
0067 struct vbg_dev {
0068     struct device *dev;
0069     /** The base of the adapter I/O ports. */
0070     u16 io_port;
0071     /** Pointer to the mapping of the VMMDev adapter memory. */
0072     struct vmmdev_memory *mmio;
0073     /** Host version */
0074     char host_version[64];
0075     /** Host features */
0076     unsigned int host_features;
0077     /**
0078      * Dummy page and vmap address for reserved kernel virtual-address
0079      * space for the guest mappings, only used on hosts lacking vtx.
0080      */
0081     struct page *guest_mappings_dummy_page;
0082     void *guest_mappings;
0083     /** Spinlock protecting pending_events. */
0084     spinlock_t event_spinlock;
0085     /** Preallocated struct vmmdev_events for the IRQ handler. */
0086     struct vmmdev_events *ack_events_req;
0087     /** Wait-for-event list for threads waiting for multiple events. */
0088     wait_queue_head_t event_wq;
0089     /** Mask of pending events. */
0090     u32 pending_events;
0091     /** Wait-for-event list for threads waiting on HGCM async completion. */
0092     wait_queue_head_t hgcm_wq;
0093     /** Pre-allocated hgcm cancel2 req. for cancellation on timeout */
0094     struct vmmdev_hgcm_cancel2 *cancel_req;
0095     /** Mutex protecting cancel_req accesses */
0096     struct mutex cancel_req_mutex;
0097     /** Pre-allocated mouse-status request for the input-device handling. */
0098     struct vmmdev_mouse_status *mouse_status_req;
0099     /** Input device for reporting abs mouse coordinates to the guest. */
0100     struct input_dev *input;
0101 
0102     /** Memory balloon information. */
0103     struct vbg_mem_balloon mem_balloon;
0104 
0105     /** Lock for session related items in vbg_dev and vbg_session */
0106     struct mutex session_mutex;
0107     /** Events we won't permit anyone to filter out. */
0108     u32 fixed_events;
0109     /**
0110      * Usage counters for the host events (excludes fixed events),
0111      * Protected by session_mutex.
0112      */
0113     struct vbg_bit_usage_tracker event_filter_tracker;
0114     /**
0115      * The event filter last reported to the host (or UINT32_MAX).
0116      * Protected by session_mutex.
0117      */
0118     u32 event_filter_host;
0119 
0120     /**
0121      * Guest capabilities which have been switched to acquire_mode.
0122      */
0123     u32 acquire_mode_guest_caps;
0124     /**
0125      * Guest capabilities acquired by vbg_acquire_session_capabilities().
0126      * Only one session can acquire a capability at a time.
0127      */
0128     u32 acquired_guest_caps;
0129     /**
0130      * Usage counters for guest capabilities requested through
0131      * vbg_set_session_capabilities(). Indexed by capability bit
0132      * number, one count per session using a capability.
0133      * Protected by session_mutex.
0134      */
0135     struct vbg_bit_usage_tracker set_guest_caps_tracker;
0136     /**
0137      * The guest capabilities last reported to the host (or UINT32_MAX).
0138      * Protected by session_mutex.
0139      */
0140     u32 guest_caps_host;
0141 
0142     /**
0143      * Heartbeat timer which fires with interval
0144      * cNsHearbeatInterval and its handler sends
0145      * VMMDEVREQ_GUEST_HEARTBEAT to VMMDev.
0146      */
0147     struct timer_list heartbeat_timer;
0148     /** Heartbeat timer interval in ms. */
0149     int heartbeat_interval_ms;
0150     /** Preallocated VMMDEVREQ_GUEST_HEARTBEAT request. */
0151     struct vmmdev_request_header *guest_heartbeat_req;
0152 
0153     /** "vboxguest" char-device */
0154     struct miscdevice misc_device;
0155     /** "vboxuser" char-device */
0156     struct miscdevice misc_device_user;
0157 };
0158 
0159 /** The VBoxGuest per session data. */
0160 struct vbg_session {
0161     /** Pointer to the device extension. */
0162     struct vbg_dev *gdev;
0163 
0164     /**
0165      * Array containing HGCM client IDs associated with this session.
0166      * These will be automatically disconnected when the session is closed.
0167      * Protected by vbg_gdev.session_mutex.
0168      */
0169     u32 hgcm_client_ids[64];
0170     /**
0171      * Host events requested by the session.
0172      * An event type requested in any guest session will be added to the
0173      * host filter. Protected by vbg_gdev.session_mutex.
0174      */
0175     u32 event_filter;
0176     /**
0177      * Guest capabilities acquired by vbg_acquire_session_capabilities().
0178      * Only one session can acquire a capability at a time.
0179      */
0180     u32 acquired_guest_caps;
0181     /**
0182      * Guest capabilities set through vbg_set_session_capabilities().
0183      * A capability claimed by any guest session will be reported to the
0184      * host. Protected by vbg_gdev.session_mutex.
0185      */
0186     u32 set_guest_caps;
0187     /** VMMDEV_REQUESTOR_* flags */
0188     u32 requestor;
0189     /** Set on CANCEL_ALL_WAITEVENTS, protected by vbg_devevent_spinlock. */
0190     bool cancel_waiters;
0191 };
0192 
0193 int  vbg_core_init(struct vbg_dev *gdev, u32 fixed_events);
0194 void vbg_core_exit(struct vbg_dev *gdev);
0195 struct vbg_session *vbg_core_open_session(struct vbg_dev *gdev, u32 requestor);
0196 void vbg_core_close_session(struct vbg_session *session);
0197 int  vbg_core_ioctl(struct vbg_session *session, unsigned int req, void *data);
0198 int  vbg_core_set_mouse_status(struct vbg_dev *gdev, u32 features);
0199 
0200 irqreturn_t vbg_core_isr(int irq, void *dev_id);
0201 
0202 void vbg_linux_mouse_event(struct vbg_dev *gdev);
0203 
0204 /* Private (non exported) functions form vboxguest_utils.c */
0205 void *vbg_req_alloc(size_t len, enum vmmdev_request_type req_type,
0206             u32 requestor);
0207 void vbg_req_free(void *req, size_t len);
0208 int vbg_req_perform(struct vbg_dev *gdev, void *req);
0209 int vbg_hgcm_call32(
0210     struct vbg_dev *gdev, u32 requestor, u32 client_id, u32 function,
0211     u32 timeout_ms, struct vmmdev_hgcm_function_parameter32 *parm32,
0212     u32 parm_count, int *vbox_status);
0213 
0214 #endif