xref: /linux/drivers/virt/vboxguest/vboxguest_core.h (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
1 /* SPDX-License-Identifier: (GPL-2.0 OR CDDL-1.0) */
2 /* Copyright (C) 2010-2016 Oracle Corporation */
3 
4 #ifndef __VBOXGUEST_CORE_H__
5 #define __VBOXGUEST_CORE_H__
6 
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>
16 #include "vmmdev.h"
17 
18 /*
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.
23  *
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.
29  */
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)
32 
33 struct vbg_session;
34 
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. */
44 	u32 chunks;
45 	/** The maximum number of chunks in the balloon. */
46 	u32 max_chunks;
47 	/**
48 	 * Array of pointers to page arrays. A page * array is allocated for
49 	 * each chunk when inflating, and freed when the deflating.
50 	 */
51 	struct page ***pages;
52 };
53 
54 /**
55  * Per bit usage tracker for a u32 mask.
56  *
57  * Used for optimal handling of guest properties and event filter.
58  */
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. */
63 	u32 mask;
64 };
65 
66 /** VBox guest device (data) extension. */
67 struct vbg_dev {
68 	struct device *dev;
69 	/** The base of the adapter I/O ports. */
70 	u16 io_port;
71 	/** Pointer to the mapping of the VMMDev adapter memory. */
72 	struct vmmdev_memory *mmio;
73 	/** Host version */
74 	char host_version[64];
75 	/** Host features */
76 	unsigned int host_features;
77 	/**
78 	 * Dummy page and vmap address for reserved kernel virtual-address
79 	 * space for the guest mappings, only used on hosts lacking vtx.
80 	 */
81 	struct page *guest_mappings_dummy_page;
82 	void *guest_mappings;
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. */
90 	u32 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;
101 
102 	/** Memory balloon information. */
103 	struct vbg_mem_balloon mem_balloon;
104 
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. */
108 	u32 fixed_events;
109 	/**
110 	 * Usage counters for the host events (excludes fixed events),
111 	 * Protected by session_mutex.
112 	 */
113 	struct vbg_bit_usage_tracker event_filter_tracker;
114 	/**
115 	 * The event filter last reported to the host (or UINT32_MAX).
116 	 * Protected by session_mutex.
117 	 */
118 	u32 event_filter_host;
119 
120 	/**
121 	 * Guest capabilities which have been switched to acquire_mode.
122 	 */
123 	u32 acquire_mode_guest_caps;
124 	/**
125 	 * Guest capabilities acquired by vbg_acquire_session_capabilities().
126 	 * Only one session can acquire a capability at a time.
127 	 */
128 	u32 acquired_guest_caps;
129 	/**
130 	 * Usage counters for guest capabilities requested through
131 	 * vbg_set_session_capabilities(). Indexed by capability bit
132 	 * number, one count per session using a capability.
133 	 * Protected by session_mutex.
134 	 */
135 	struct vbg_bit_usage_tracker set_guest_caps_tracker;
136 	/**
137 	 * The guest capabilities last reported to the host (or UINT32_MAX).
138 	 * Protected by session_mutex.
139 	 */
140 	u32 guest_caps_host;
141 
142 	/**
143 	 * Heartbeat timer which fires with interval
144 	 * cNsHearbeatInterval and its handler sends
145 	 * VMMDEVREQ_GUEST_HEARTBEAT to VMMDev.
146 	 */
147 	struct timer_list heartbeat_timer;
148 	/** Heartbeat timer interval in ms. */
149 	int heartbeat_interval_ms;
150 	/** Preallocated VMMDEVREQ_GUEST_HEARTBEAT request. */
151 	struct vmmdev_request_header *guest_heartbeat_req;
152 
153 	/** "vboxguest" char-device */
154 	struct miscdevice misc_device;
155 	/** "vboxuser" char-device */
156 	struct miscdevice misc_device_user;
157 };
158 
159 /** The VBoxGuest per session data. */
160 struct vbg_session {
161 	/** Pointer to the device extension. */
162 	struct vbg_dev *gdev;
163 
164 	/**
165 	 * Array containing HGCM client IDs associated with this session.
166 	 * These will be automatically disconnected when the session is closed.
167 	 * Protected by vbg_gdev.session_mutex.
168 	 */
169 	u32 hgcm_client_ids[64];
170 	/**
171 	 * Host events requested by the session.
172 	 * An event type requested in any guest session will be added to the
173 	 * host filter. Protected by vbg_gdev.session_mutex.
174 	 */
175 	u32 event_filter;
176 	/**
177 	 * Guest capabilities acquired by vbg_acquire_session_capabilities().
178 	 * Only one session can acquire a capability at a time.
179 	 */
180 	u32 acquired_guest_caps;
181 	/**
182 	 * Guest capabilities set through vbg_set_session_capabilities().
183 	 * A capability claimed by any guest session will be reported to the
184 	 * host. Protected by vbg_gdev.session_mutex.
185 	 */
186 	u32 set_guest_caps;
187 	/** VMMDEV_REQUESTOR_* flags */
188 	u32 requestor;
189 	/** Set on CANCEL_ALL_WAITEVENTS, protected by vbg_devevent_spinlock. */
190 	bool cancel_waiters;
191 };
192 
193 int  vbg_core_init(struct vbg_dev *gdev, u32 fixed_events);
194 void vbg_core_exit(struct vbg_dev *gdev);
195 struct vbg_session *vbg_core_open_session(struct vbg_dev *gdev, u32 requestor);
196 void vbg_core_close_session(struct vbg_session *session);
197 int  vbg_core_ioctl(struct vbg_session *session, unsigned int req, void *data);
198 int  vbg_core_set_mouse_status(struct vbg_dev *gdev, u32 features);
199 
200 irqreturn_t vbg_core_isr(int irq, void *dev_id);
201 
202 void vbg_linux_mouse_event(struct vbg_dev *gdev);
203 
204 /* Private (non exported) functions form vboxguest_utils.c */
205 void *vbg_req_alloc(size_t len, enum vmmdev_request_type req_type,
206 		    u32 requestor);
207 void vbg_req_free(void *req, size_t len);
208 int vbg_req_perform(struct vbg_dev *gdev, void *req);
209 int vbg_hgcm_call32(
210 	struct vbg_dev *gdev, u32 requestor, u32 client_id, u32 function,
211 	u32 timeout_ms, struct vmmdev_hgcm_function_parameter32 *parm32,
212 	u32 parm_count, int *vbox_status);
213 
214 #endif
215