vboxguest_core.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* SPDX-License-Identifier: (GPL-2.0 OR CDDL-1.0) */
  2. /* Copyright (C) 2010-2016 Oracle Corporation */
  3. #ifndef __VBOXGUEST_CORE_H__
  4. #define __VBOXGUEST_CORE_H__
  5. #include <linux/input.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/kernel.h>
  8. #include <linux/list.h>
  9. #include <linux/miscdevice.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/wait.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/vboxguest.h>
  14. #include "vmmdev.h"
  15. /*
  16. * The mainline kernel version (this version) of the vboxguest module
  17. * contained a bug where it defined VBGL_IOCTL_VMMDEV_REQUEST_BIG and
  18. * VBGL_IOCTL_LOG using _IOC(_IOC_READ | _IOC_WRITE, 'V', ...) instead
  19. * of _IO(V, ...) as the out of tree VirtualBox upstream version does.
  20. *
  21. * These _ALT definitions keep compatibility with the wrong defines the
  22. * mainline kernel version used for a while.
  23. * Note the VirtualBox userspace bits have always been built against
  24. * VirtualBox upstream's headers, so this is likely not necessary. But
  25. * we must never break our ABI so we keep these around to be 100% sure.
  26. */
  27. #define VBG_IOCTL_VMMDEV_REQUEST_BIG_ALT _IOC(_IOC_READ | _IOC_WRITE, 'V', 3, 0)
  28. #define VBG_IOCTL_LOG_ALT(s) _IOC(_IOC_READ | _IOC_WRITE, 'V', 9, s)
  29. struct vbg_session;
  30. /** VBox guest memory balloon. */
  31. struct vbg_mem_balloon {
  32. /** Work handling VMMDEV_EVENT_BALLOON_CHANGE_REQUEST events */
  33. struct work_struct work;
  34. /** Pre-allocated vmmdev_memballoon_info req for query */
  35. struct vmmdev_memballoon_info *get_req;
  36. /** Pre-allocated vmmdev_memballoon_change req for inflate / deflate */
  37. struct vmmdev_memballoon_change *change_req;
  38. /** The current number of chunks in the balloon. */
  39. u32 chunks;
  40. /** The maximum number of chunks in the balloon. */
  41. u32 max_chunks;
  42. /**
  43. * Array of pointers to page arrays. A page * array is allocated for
  44. * each chunk when inflating, and freed when the deflating.
  45. */
  46. struct page ***pages;
  47. };
  48. /**
  49. * Per bit usage tracker for a u32 mask.
  50. *
  51. * Used for optimal handling of guest properties and event filter.
  52. */
  53. struct vbg_bit_usage_tracker {
  54. /** Per bit usage counters. */
  55. u32 per_bit_usage[32];
  56. /** The current mask according to per_bit_usage. */
  57. u32 mask;
  58. };
  59. /** VBox guest device (data) extension. */
  60. struct vbg_dev {
  61. struct device *dev;
  62. /** The base of the adapter I/O ports. */
  63. u16 io_port;
  64. /** Pointer to the mapping of the VMMDev adapter memory. */
  65. struct vmmdev_memory *mmio;
  66. /** Host version */
  67. char host_version[64];
  68. /** Host features */
  69. unsigned int host_features;
  70. /**
  71. * Dummy page and vmap address for reserved kernel virtual-address
  72. * space for the guest mappings, only used on hosts lacking vtx.
  73. */
  74. struct page *guest_mappings_dummy_page;
  75. void *guest_mappings;
  76. /** Spinlock protecting pending_events. */
  77. spinlock_t event_spinlock;
  78. /** Preallocated struct vmmdev_events for the IRQ handler. */
  79. struct vmmdev_events *ack_events_req;
  80. /** Wait-for-event list for threads waiting for multiple events. */
  81. wait_queue_head_t event_wq;
  82. /** Mask of pending events. */
  83. u32 pending_events;
  84. /** Wait-for-event list for threads waiting on HGCM async completion. */
  85. wait_queue_head_t hgcm_wq;
  86. /** Pre-allocated hgcm cancel2 req. for cancellation on timeout */
  87. struct vmmdev_hgcm_cancel2 *cancel_req;
  88. /** Mutex protecting cancel_req accesses */
  89. struct mutex cancel_req_mutex;
  90. /** Pre-allocated mouse-status request for the input-device handling. */
  91. struct vmmdev_mouse_status *mouse_status_req;
  92. /** Input device for reporting abs mouse coordinates to the guest. */
  93. struct input_dev *input;
  94. /** Memory balloon information. */
  95. struct vbg_mem_balloon mem_balloon;
  96. /** Lock for session related items in vbg_dev and vbg_session */
  97. struct mutex session_mutex;
  98. /** Events we won't permit anyone to filter out. */
  99. u32 fixed_events;
  100. /**
  101. * Usage counters for the host events (excludes fixed events),
  102. * Protected by session_mutex.
  103. */
  104. struct vbg_bit_usage_tracker event_filter_tracker;
  105. /**
  106. * The event filter last reported to the host (or UINT32_MAX).
  107. * Protected by session_mutex.
  108. */
  109. u32 event_filter_host;
  110. /**
  111. * Guest capabilities which have been switched to acquire_mode.
  112. */
  113. u32 acquire_mode_guest_caps;
  114. /**
  115. * Guest capabilities acquired by vbg_acquire_session_capabilities().
  116. * Only one session can acquire a capability at a time.
  117. */
  118. u32 acquired_guest_caps;
  119. /**
  120. * Usage counters for guest capabilities requested through
  121. * vbg_set_session_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 set_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. * Heartbeat timer which fires with interval
  133. * cNsHearbeatInterval and its handler sends
  134. * VMMDEVREQ_GUEST_HEARTBEAT to VMMDev.
  135. */
  136. struct timer_list heartbeat_timer;
  137. /** Heartbeat timer interval in ms. */
  138. int heartbeat_interval_ms;
  139. /** Preallocated VMMDEVREQ_GUEST_HEARTBEAT request. */
  140. struct vmmdev_request_header *guest_heartbeat_req;
  141. /** "vboxguest" char-device */
  142. struct miscdevice misc_device;
  143. /** "vboxuser" char-device */
  144. struct miscdevice misc_device_user;
  145. };
  146. /** The VBoxGuest per session data. */
  147. struct vbg_session {
  148. /** Pointer to the device extension. */
  149. struct vbg_dev *gdev;
  150. /**
  151. * Array containing HGCM client IDs associated with this session.
  152. * These will be automatically disconnected when the session is closed.
  153. * Protected by vbg_gdev.session_mutex.
  154. */
  155. u32 hgcm_client_ids[64];
  156. /**
  157. * Host events requested by the session.
  158. * An event type requested in any guest session will be added to the
  159. * host filter. Protected by vbg_gdev.session_mutex.
  160. */
  161. u32 event_filter;
  162. /**
  163. * Guest capabilities acquired by vbg_acquire_session_capabilities().
  164. * Only one session can acquire a capability at a time.
  165. */
  166. u32 acquired_guest_caps;
  167. /**
  168. * Guest capabilities set through vbg_set_session_capabilities().
  169. * A capability claimed by any guest session will be reported to the
  170. * host. Protected by vbg_gdev.session_mutex.
  171. */
  172. u32 set_guest_caps;
  173. /** VMMDEV_REQUESTOR_* flags */
  174. u32 requestor;
  175. /** Set on CANCEL_ALL_WAITEVENTS, protected by vbg_devevent_spinlock. */
  176. bool cancel_waiters;
  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, u32 requestor);
  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. irqreturn_t vbg_core_isr(int irq, void *dev_id);
  185. void vbg_linux_mouse_event(struct vbg_dev *gdev);
  186. /* Private (non exported) functions form vboxguest_utils.c */
  187. void *vbg_req_alloc(size_t len, enum vmmdev_request_type req_type,
  188. u32 requestor);
  189. void vbg_req_free(void *req, size_t len);
  190. int vbg_req_perform(struct vbg_dev *gdev, void *req);
  191. int vbg_hgcm_call32(
  192. struct vbg_dev *gdev, u32 requestor, u32 client_id, u32 function,
  193. u32 timeout_ms, struct vmmdev_hgcm_function_parameter32 *parm32,
  194. u32 parm_count, int *vbox_status);
  195. #endif