vboxguest_core.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. * Usage counters for guest capabilities. Indexed by capability bit
  112. * number, one count per session using a capability.
  113. * Protected by session_mutex.
  114. */
  115. struct vbg_bit_usage_tracker guest_caps_tracker;
  116. /**
  117. * The guest capabilities last reported to the host (or UINT32_MAX).
  118. * Protected by session_mutex.
  119. */
  120. u32 guest_caps_host;
  121. /**
  122. * Heartbeat timer which fires with interval
  123. * cNsHearbeatInterval and its handler sends
  124. * VMMDEVREQ_GUEST_HEARTBEAT to VMMDev.
  125. */
  126. struct timer_list heartbeat_timer;
  127. /** Heartbeat timer interval in ms. */
  128. int heartbeat_interval_ms;
  129. /** Preallocated VMMDEVREQ_GUEST_HEARTBEAT request. */
  130. struct vmmdev_request_header *guest_heartbeat_req;
  131. /** "vboxguest" char-device */
  132. struct miscdevice misc_device;
  133. /** "vboxuser" char-device */
  134. struct miscdevice misc_device_user;
  135. };
  136. /** The VBoxGuest per session data. */
  137. struct vbg_session {
  138. /** Pointer to the device extension. */
  139. struct vbg_dev *gdev;
  140. /**
  141. * Array containing HGCM client IDs associated with this session.
  142. * These will be automatically disconnected when the session is closed.
  143. * Protected by vbg_gdev.session_mutex.
  144. */
  145. u32 hgcm_client_ids[64];
  146. /**
  147. * Host events requested by the session.
  148. * An event type requested in any guest session will be added to the
  149. * host filter. Protected by vbg_gdev.session_mutex.
  150. */
  151. u32 event_filter;
  152. /**
  153. * Guest capabilities for this session.
  154. * A capability claimed by any guest session will be reported to the
  155. * host. Protected by vbg_gdev.session_mutex.
  156. */
  157. u32 guest_caps;
  158. /** Does this session belong to a root process or a user one? */
  159. bool user_session;
  160. /** Set on CANCEL_ALL_WAITEVENTS, protected by vbg_devevent_spinlock. */
  161. bool cancel_waiters;
  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. irqreturn_t vbg_core_isr(int irq, void *dev_id);
  170. void vbg_linux_mouse_event(struct vbg_dev *gdev);
  171. /* Private (non exported) functions form vboxguest_utils.c */
  172. void *vbg_req_alloc(size_t len, enum vmmdev_request_type req_type);
  173. void vbg_req_free(void *req, size_t len);
  174. int vbg_req_perform(struct vbg_dev *gdev, void *req);
  175. int vbg_hgcm_call32(
  176. struct vbg_dev *gdev, u32 client_id, u32 function, u32 timeout_ms,
  177. struct vmmdev_hgcm_function_parameter32 *parm32, u32 parm_count,
  178. int *vbox_status);
  179. #endif