xref: /linux/drivers/virt/vboxguest/vboxguest_core.h (revision e9f0878c4b2004ac19581274c1ae4c61ae3ca70e)
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 struct vbg_session;
19 
20 /** VBox guest memory balloon. */
21 struct vbg_mem_balloon {
22 	/** Work handling VMMDEV_EVENT_BALLOON_CHANGE_REQUEST events */
23 	struct work_struct work;
24 	/** Pre-allocated vmmdev_memballoon_info req for query */
25 	struct vmmdev_memballoon_info *get_req;
26 	/** Pre-allocated vmmdev_memballoon_change req for inflate / deflate */
27 	struct vmmdev_memballoon_change *change_req;
28 	/** The current number of chunks in the balloon. */
29 	u32 chunks;
30 	/** The maximum number of chunks in the balloon. */
31 	u32 max_chunks;
32 	/**
33 	 * Array of pointers to page arrays. A page * array is allocated for
34 	 * each chunk when inflating, and freed when the deflating.
35 	 */
36 	struct page ***pages;
37 };
38 
39 /**
40  * Per bit usage tracker for a u32 mask.
41  *
42  * Used for optimal handling of guest properties and event filter.
43  */
44 struct vbg_bit_usage_tracker {
45 	/** Per bit usage counters. */
46 	u32 per_bit_usage[32];
47 	/** The current mask according to per_bit_usage. */
48 	u32 mask;
49 };
50 
51 /** VBox guest device (data) extension. */
52 struct vbg_dev {
53 	struct device *dev;
54 	/** The base of the adapter I/O ports. */
55 	u16 io_port;
56 	/** Pointer to the mapping of the VMMDev adapter memory. */
57 	struct vmmdev_memory *mmio;
58 	/** Host version */
59 	char host_version[64];
60 	/** Host features */
61 	unsigned int host_features;
62 	/**
63 	 * Dummy page and vmap address for reserved kernel virtual-address
64 	 * space for the guest mappings, only used on hosts lacking vtx.
65 	 */
66 	struct page *guest_mappings_dummy_page;
67 	void *guest_mappings;
68 	/** Spinlock protecting pending_events. */
69 	spinlock_t event_spinlock;
70 	/** Preallocated struct vmmdev_events for the IRQ handler. */
71 	struct vmmdev_events *ack_events_req;
72 	/** Wait-for-event list for threads waiting for multiple events. */
73 	wait_queue_head_t event_wq;
74 	/** Mask of pending events. */
75 	u32 pending_events;
76 	/** Wait-for-event list for threads waiting on HGCM async completion. */
77 	wait_queue_head_t hgcm_wq;
78 	/** Pre-allocated hgcm cancel2 req. for cancellation on timeout */
79 	struct vmmdev_hgcm_cancel2 *cancel_req;
80 	/** Mutex protecting cancel_req accesses */
81 	struct mutex cancel_req_mutex;
82 	/** Pre-allocated mouse-status request for the input-device handling. */
83 	struct vmmdev_mouse_status *mouse_status_req;
84 	/** Input device for reporting abs mouse coordinates to the guest. */
85 	struct input_dev *input;
86 
87 	/** Memory balloon information. */
88 	struct vbg_mem_balloon mem_balloon;
89 
90 	/** Lock for session related items in vbg_dev and vbg_session */
91 	struct mutex session_mutex;
92 	/** Events we won't permit anyone to filter out. */
93 	u32 fixed_events;
94 	/**
95 	 * Usage counters for the host events (excludes fixed events),
96 	 * Protected by session_mutex.
97 	 */
98 	struct vbg_bit_usage_tracker event_filter_tracker;
99 	/**
100 	 * The event filter last reported to the host (or UINT32_MAX).
101 	 * Protected by session_mutex.
102 	 */
103 	u32 event_filter_host;
104 
105 	/**
106 	 * Usage counters for guest capabilities. Indexed by capability bit
107 	 * number, one count per session using a capability.
108 	 * Protected by session_mutex.
109 	 */
110 	struct vbg_bit_usage_tracker guest_caps_tracker;
111 	/**
112 	 * The guest capabilities last reported to the host (or UINT32_MAX).
113 	 * Protected by session_mutex.
114 	 */
115 	u32 guest_caps_host;
116 
117 	/**
118 	 * Heartbeat timer which fires with interval
119 	 * cNsHearbeatInterval and its handler sends
120 	 * VMMDEVREQ_GUEST_HEARTBEAT to VMMDev.
121 	 */
122 	struct timer_list heartbeat_timer;
123 	/** Heartbeat timer interval in ms. */
124 	int heartbeat_interval_ms;
125 	/** Preallocated VMMDEVREQ_GUEST_HEARTBEAT request. */
126 	struct vmmdev_request_header *guest_heartbeat_req;
127 
128 	/** "vboxguest" char-device */
129 	struct miscdevice misc_device;
130 	/** "vboxuser" char-device */
131 	struct miscdevice misc_device_user;
132 };
133 
134 /** The VBoxGuest per session data. */
135 struct vbg_session {
136 	/** Pointer to the device extension. */
137 	struct vbg_dev *gdev;
138 
139 	/**
140 	 * Array containing HGCM client IDs associated with this session.
141 	 * These will be automatically disconnected when the session is closed.
142 	 * Protected by vbg_gdev.session_mutex.
143 	 */
144 	u32 hgcm_client_ids[64];
145 	/**
146 	 * Host events requested by the session.
147 	 * An event type requested in any guest session will be added to the
148 	 * host filter. Protected by vbg_gdev.session_mutex.
149 	 */
150 	u32 event_filter;
151 	/**
152 	 * Guest capabilities for this session.
153 	 * A capability claimed by any guest session will be reported to the
154 	 * host. Protected by vbg_gdev.session_mutex.
155 	 */
156 	u32 guest_caps;
157 	/** Does this session belong to a root process or a user one? */
158 	bool user_session;
159 	/** Set on CANCEL_ALL_WAITEVENTS, protected by vbg_devevent_spinlock. */
160 	bool cancel_waiters;
161 };
162 
163 int  vbg_core_init(struct vbg_dev *gdev, u32 fixed_events);
164 void vbg_core_exit(struct vbg_dev *gdev);
165 struct vbg_session *vbg_core_open_session(struct vbg_dev *gdev, bool user);
166 void vbg_core_close_session(struct vbg_session *session);
167 int  vbg_core_ioctl(struct vbg_session *session, unsigned int req, void *data);
168 int  vbg_core_set_mouse_status(struct vbg_dev *gdev, u32 features);
169 
170 irqreturn_t vbg_core_isr(int irq, void *dev_id);
171 
172 void vbg_linux_mouse_event(struct vbg_dev *gdev);
173 
174 /* Private (non exported) functions form vboxguest_utils.c */
175 void *vbg_req_alloc(size_t len, enum vmmdev_request_type req_type);
176 void vbg_req_free(void *req, size_t len);
177 int vbg_req_perform(struct vbg_dev *gdev, void *req);
178 int vbg_hgcm_call32(
179 	struct vbg_dev *gdev, u32 client_id, u32 function, u32 timeout_ms,
180 	struct vmmdev_hgcm_function_parameter32 *parm32, u32 parm_count,
181 	int *vbox_status);
182 
183 #endif
184