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 	 * Usage counters for guest capabilities. Indexed by capability bit
122 	 * number, one count per session using a capability.
123 	 * Protected by session_mutex.
124 	 */
125 	struct vbg_bit_usage_tracker guest_caps_tracker;
126 	/**
127 	 * The guest capabilities last reported to the host (or UINT32_MAX).
128 	 * Protected by session_mutex.
129 	 */
130 	u32 guest_caps_host;
131 
132 	/**
133 	 * Heartbeat timer which fires with interval
134 	 * cNsHearbeatInterval and its handler sends
135 	 * VMMDEVREQ_GUEST_HEARTBEAT to VMMDev.
136 	 */
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;
142 
143 	/** "vboxguest" char-device */
144 	struct miscdevice misc_device;
145 	/** "vboxuser" char-device */
146 	struct miscdevice misc_device_user;
147 };
148 
149 /** The VBoxGuest per session data. */
150 struct vbg_session {
151 	/** Pointer to the device extension. */
152 	struct vbg_dev *gdev;
153 
154 	/**
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.
158 	 */
159 	u32 hgcm_client_ids[64];
160 	/**
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.
164 	 */
165 	u32 event_filter;
166 	/**
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.
170 	 */
171 	u32 guest_caps;
172 	/** Does this session belong to a root process or a user one? */
173 	bool user_session;
174 	/** Set on CANCEL_ALL_WAITEVENTS, protected by vbg_devevent_spinlock. */
175 	bool cancel_waiters;
176 };
177 
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);
184 
185 irqreturn_t vbg_core_isr(int irq, void *dev_id);
186 
187 void vbg_linux_mouse_event(struct vbg_dev *gdev);
188 
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);
193 int vbg_hgcm_call32(
194 	struct vbg_dev *gdev, u32 client_id, u32 function, u32 timeout_ms,
195 	struct vmmdev_hgcm_function_parameter32 *parm32, u32 parm_count,
196 	int *vbox_status);
197 
198 #endif
199