1 /* SPDX-License-Identifier: (GPL-2.0 OR CDDL-1.0) */
2 /* Copyright (C) 2010-2016 Oracle Corporation */
4 #ifndef __VBOXGUEST_CORE_H__
5 #define __VBOXGUEST_CORE_H__
7 #include <linux/input.h>
8 #include <linux/interrupt.h>
9 #include <linux/kernel.h>
10 #include <linux/list.h>
11 #include <linux/miscdevice.h>
12 #include <linux/spinlock.h>
13 #include <linux/wait.h>
14 #include <linux/workqueue.h>
15 #include <linux/vboxguest.h>
19 * The mainline kernel version (this version) of the vboxguest module
20 * contained a bug where it defined VBGL_IOCTL_VMMDEV_REQUEST_BIG and
21 * VBGL_IOCTL_LOG using _IOC(_IOC_READ | _IOC_WRITE, 'V', ...) instead
22 * of _IO(V, ...) as the out of tree VirtualBox upstream version does.
24 * These _ALT definitions keep compatibility with the wrong defines the
25 * mainline kernel version used for a while.
26 * Note the VirtualBox userspace bits have always been built against
27 * VirtualBox upstream's headers, so this is likely not necessary. But
28 * we must never break our ABI so we keep these around to be 100% sure.
30 #define VBG_IOCTL_VMMDEV_REQUEST_BIG_ALT _IOC(_IOC_READ | _IOC_WRITE, 'V', 3, 0)
31 #define VBG_IOCTL_LOG_ALT(s) _IOC(_IOC_READ | _IOC_WRITE, 'V', 9, s)
35 /** VBox guest memory balloon. */
36 struct vbg_mem_balloon {
37 /** Work handling VMMDEV_EVENT_BALLOON_CHANGE_REQUEST events */
38 struct work_struct work;
39 /** Pre-allocated vmmdev_memballoon_info req for query */
40 struct vmmdev_memballoon_info *get_req;
41 /** Pre-allocated vmmdev_memballoon_change req for inflate / deflate */
42 struct vmmdev_memballoon_change *change_req;
43 /** The current number of chunks in the balloon. */
45 /** The maximum number of chunks in the balloon. */
48 * Array of pointers to page arrays. A page * array is allocated for
49 * each chunk when inflating, and freed when the deflating.
55 * Per bit usage tracker for a u32 mask.
57 * Used for optimal handling of guest properties and event filter.
59 struct vbg_bit_usage_tracker {
60 /** Per bit usage counters. */
61 u32 per_bit_usage[32];
62 /** The current mask according to per_bit_usage. */
66 /** VBox guest device (data) extension. */
69 /** The base of the adapter I/O ports. */
71 /** Pointer to the mapping of the VMMDev adapter memory. */
72 struct vmmdev_memory *mmio;
74 char host_version[64];
76 unsigned int host_features;
78 * Dummy page and vmap address for reserved kernel virtual-address
79 * space for the guest mappings, only used on hosts lacking vtx.
81 struct page *guest_mappings_dummy_page;
83 /** Spinlock protecting pending_events. */
84 spinlock_t event_spinlock;
85 /** Preallocated struct vmmdev_events for the IRQ handler. */
86 struct vmmdev_events *ack_events_req;
87 /** Wait-for-event list for threads waiting for multiple events. */
88 wait_queue_head_t event_wq;
89 /** Mask of pending events. */
91 /** Wait-for-event list for threads waiting on HGCM async completion. */
92 wait_queue_head_t hgcm_wq;
93 /** Pre-allocated hgcm cancel2 req. for cancellation on timeout */
94 struct vmmdev_hgcm_cancel2 *cancel_req;
95 /** Mutex protecting cancel_req accesses */
96 struct mutex cancel_req_mutex;
97 /** Pre-allocated mouse-status request for the input-device handling. */
98 struct vmmdev_mouse_status *mouse_status_req;
99 /** Input device for reporting abs mouse coordinates to the guest. */
100 struct input_dev *input;
102 /** Memory balloon information. */
103 struct vbg_mem_balloon mem_balloon;
105 /** Lock for session related items in vbg_dev and vbg_session */
106 struct mutex session_mutex;
107 /** Events we won't permit anyone to filter out. */
110 * Usage counters for the host events (excludes fixed events),
111 * Protected by session_mutex.
113 struct vbg_bit_usage_tracker event_filter_tracker;
115 * The event filter last reported to the host (or UINT32_MAX).
116 * Protected by session_mutex.
118 u32 event_filter_host;
121 * Usage counters for guest capabilities. Indexed by capability bit
122 * number, one count per session using a capability.
123 * Protected by session_mutex.
125 struct vbg_bit_usage_tracker guest_caps_tracker;
127 * The guest capabilities last reported to the host (or UINT32_MAX).
128 * Protected by session_mutex.
133 * Heartbeat timer which fires with interval
134 * cNsHearbeatInterval and its handler sends
135 * VMMDEVREQ_GUEST_HEARTBEAT to VMMDev.
137 struct timer_list heartbeat_timer;
138 /** Heartbeat timer interval in ms. */
139 int heartbeat_interval_ms;
140 /** Preallocated VMMDEVREQ_GUEST_HEARTBEAT request. */
141 struct vmmdev_request_header *guest_heartbeat_req;
143 /** "vboxguest" char-device */
144 struct miscdevice misc_device;
145 /** "vboxuser" char-device */
146 struct miscdevice misc_device_user;
149 /** The VBoxGuest per session data. */
151 /** Pointer to the device extension. */
152 struct vbg_dev *gdev;
155 * Array containing HGCM client IDs associated with this session.
156 * These will be automatically disconnected when the session is closed.
157 * Protected by vbg_gdev.session_mutex.
159 u32 hgcm_client_ids[64];
161 * Host events requested by the session.
162 * An event type requested in any guest session will be added to the
163 * host filter. Protected by vbg_gdev.session_mutex.
167 * Guest capabilities for this session.
168 * A capability claimed by any guest session will be reported to the
169 * host. Protected by vbg_gdev.session_mutex.
172 /** Does this session belong to a root process or a user one? */
174 /** Set on CANCEL_ALL_WAITEVENTS, protected by vbg_devevent_spinlock. */
178 int vbg_core_init(struct vbg_dev *gdev, u32 fixed_events);
179 void vbg_core_exit(struct vbg_dev *gdev);
180 struct vbg_session *vbg_core_open_session(struct vbg_dev *gdev, bool user);
181 void vbg_core_close_session(struct vbg_session *session);
182 int vbg_core_ioctl(struct vbg_session *session, unsigned int req, void *data);
183 int vbg_core_set_mouse_status(struct vbg_dev *gdev, u32 features);
185 irqreturn_t vbg_core_isr(int irq, void *dev_id);
187 void vbg_linux_mouse_event(struct vbg_dev *gdev);
189 /* Private (non exported) functions form vboxguest_utils.c */
190 void *vbg_req_alloc(size_t len, enum vmmdev_request_type req_type);
191 void vbg_req_free(void *req, size_t len);
192 int vbg_req_perform(struct vbg_dev *gdev, void *req);
194 struct vbg_dev *gdev, u32 client_id, u32 function, u32 timeout_ms,
195 struct vmmdev_hgcm_function_parameter32 *parm32, u32 parm_count,