vhost.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #ifndef _VHOST_H
  2. #define _VHOST_H
  3. #include <linux/eventfd.h>
  4. #include <linux/vhost.h>
  5. #include <linux/mm.h>
  6. #include <linux/mutex.h>
  7. #include <linux/poll.h>
  8. #include <linux/file.h>
  9. #include <linux/uio.h>
  10. #include <linux/virtio_config.h>
  11. #include <linux/virtio_ring.h>
  12. #include <linux/atomic.h>
  13. struct vhost_work;
  14. typedef void (*vhost_work_fn_t)(struct vhost_work *work);
  15. #define VHOST_WORK_QUEUED 1
  16. struct vhost_work {
  17. struct llist_node node;
  18. vhost_work_fn_t fn;
  19. wait_queue_head_t done;
  20. int flushing;
  21. unsigned queue_seq;
  22. unsigned done_seq;
  23. unsigned long flags;
  24. };
  25. /* Poll a file (eventfd or socket) */
  26. /* Note: there's nothing vhost specific about this structure. */
  27. struct vhost_poll {
  28. poll_table table;
  29. wait_queue_head_t *wqh;
  30. wait_queue_t wait;
  31. struct vhost_work work;
  32. unsigned long mask;
  33. struct vhost_dev *dev;
  34. };
  35. void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
  36. void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
  37. bool vhost_has_work(struct vhost_dev *dev);
  38. void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
  39. unsigned long mask, struct vhost_dev *dev);
  40. int vhost_poll_start(struct vhost_poll *poll, struct file *file);
  41. void vhost_poll_stop(struct vhost_poll *poll);
  42. void vhost_poll_flush(struct vhost_poll *poll);
  43. void vhost_poll_queue(struct vhost_poll *poll);
  44. void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work);
  45. long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
  46. struct vhost_log {
  47. u64 addr;
  48. u64 len;
  49. };
  50. #define START(node) ((node)->start)
  51. #define LAST(node) ((node)->last)
  52. struct vhost_umem_node {
  53. struct rb_node rb;
  54. struct list_head link;
  55. __u64 start;
  56. __u64 last;
  57. __u64 size;
  58. __u64 userspace_addr;
  59. __u32 perm;
  60. __u32 flags_padding;
  61. __u64 __subtree_last;
  62. };
  63. struct vhost_umem {
  64. struct rb_root umem_tree;
  65. struct list_head umem_list;
  66. int numem;
  67. };
  68. /* The virtqueue structure describes a queue attached to a device. */
  69. struct vhost_virtqueue {
  70. struct vhost_dev *dev;
  71. /* The actual ring of buffers. */
  72. struct mutex mutex;
  73. unsigned int num;
  74. struct vring_desc __user *desc;
  75. struct vring_avail __user *avail;
  76. struct vring_used __user *used;
  77. struct file *kick;
  78. struct file *call;
  79. struct file *error;
  80. struct eventfd_ctx *call_ctx;
  81. struct eventfd_ctx *error_ctx;
  82. struct eventfd_ctx *log_ctx;
  83. struct vhost_poll poll;
  84. /* The routine to call when the Guest pings us, or timeout. */
  85. vhost_work_fn_t handle_kick;
  86. /* Last available index we saw. */
  87. u16 last_avail_idx;
  88. /* Caches available index value from user. */
  89. u16 avail_idx;
  90. /* Last index we used. */
  91. u16 last_used_idx;
  92. /* Last used evet we've seen */
  93. u16 last_used_event;
  94. /* Used flags */
  95. u16 used_flags;
  96. /* Last used index value we have signalled on */
  97. u16 signalled_used;
  98. /* Last used index value we have signalled on */
  99. bool signalled_used_valid;
  100. /* Log writes to used structure. */
  101. bool log_used;
  102. u64 log_addr;
  103. struct iovec iov[UIO_MAXIOV];
  104. struct iovec iotlb_iov[64];
  105. struct iovec *indirect;
  106. struct vring_used_elem *heads;
  107. /* Protected by virtqueue mutex. */
  108. struct vhost_umem *umem;
  109. struct vhost_umem *iotlb;
  110. void *private_data;
  111. u64 acked_features;
  112. /* Log write descriptors */
  113. void __user *log_base;
  114. struct vhost_log *log;
  115. /* Ring endianness. Defaults to legacy native endianness.
  116. * Set to true when starting a modern virtio device. */
  117. bool is_le;
  118. #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
  119. /* Ring endianness requested by userspace for cross-endian support. */
  120. bool user_be;
  121. #endif
  122. u32 busyloop_timeout;
  123. };
  124. struct vhost_msg_node {
  125. struct vhost_msg msg;
  126. struct vhost_virtqueue *vq;
  127. struct list_head node;
  128. };
  129. struct vhost_dev {
  130. struct mm_struct *mm;
  131. struct mutex mutex;
  132. struct vhost_virtqueue **vqs;
  133. int nvqs;
  134. struct file *log_file;
  135. struct eventfd_ctx *log_ctx;
  136. struct llist_head work_list;
  137. struct task_struct *worker;
  138. struct vhost_umem *umem;
  139. struct vhost_umem *iotlb;
  140. spinlock_t iotlb_lock;
  141. struct list_head read_list;
  142. struct list_head pending_list;
  143. wait_queue_head_t wait;
  144. };
  145. void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs, int nvqs);
  146. long vhost_dev_set_owner(struct vhost_dev *dev);
  147. bool vhost_dev_has_owner(struct vhost_dev *dev);
  148. long vhost_dev_check_owner(struct vhost_dev *);
  149. struct vhost_umem *vhost_dev_reset_owner_prepare(void);
  150. void vhost_dev_reset_owner(struct vhost_dev *, struct vhost_umem *);
  151. void vhost_dev_cleanup(struct vhost_dev *, bool locked);
  152. void vhost_dev_stop(struct vhost_dev *);
  153. long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
  154. long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
  155. int vhost_vq_access_ok(struct vhost_virtqueue *vq);
  156. int vhost_log_access_ok(struct vhost_dev *);
  157. int vhost_get_vq_desc(struct vhost_virtqueue *,
  158. struct iovec iov[], unsigned int iov_count,
  159. unsigned int *out_num, unsigned int *in_num,
  160. struct vhost_log *log, unsigned int *log_num);
  161. void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
  162. int vhost_vq_init_access(struct vhost_virtqueue *);
  163. int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
  164. int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
  165. unsigned count);
  166. void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
  167. unsigned int id, int len);
  168. void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
  169. struct vring_used_elem *heads, unsigned count);
  170. void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
  171. void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  172. bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
  173. bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  174. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  175. unsigned int log_num, u64 len);
  176. int vq_iotlb_prefetch(struct vhost_virtqueue *vq);
  177. struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type);
  178. void vhost_enqueue_msg(struct vhost_dev *dev,
  179. struct list_head *head,
  180. struct vhost_msg_node *node);
  181. struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
  182. struct list_head *head);
  183. unsigned int vhost_chr_poll(struct file *file, struct vhost_dev *dev,
  184. poll_table *wait);
  185. ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
  186. int noblock);
  187. ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
  188. struct iov_iter *from);
  189. int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
  190. #define vq_err(vq, fmt, ...) do { \
  191. pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
  192. if ((vq)->error_ctx) \
  193. eventfd_signal((vq)->error_ctx, 1);\
  194. } while (0)
  195. enum {
  196. VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
  197. (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
  198. (1ULL << VIRTIO_RING_F_EVENT_IDX) |
  199. (1ULL << VHOST_F_LOG_ALL) |
  200. (1ULL << VIRTIO_F_ANY_LAYOUT) |
  201. (1ULL << VIRTIO_F_VERSION_1)
  202. };
  203. static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
  204. {
  205. return vq->acked_features & (1ULL << bit);
  206. }
  207. #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
  208. static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
  209. {
  210. return vq->is_le;
  211. }
  212. #else
  213. static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
  214. {
  215. return virtio_legacy_is_little_endian() || vq->is_le;
  216. }
  217. #endif
  218. /* Memory accessors */
  219. static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
  220. {
  221. return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
  222. }
  223. static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
  224. {
  225. return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
  226. }
  227. static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
  228. {
  229. return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
  230. }
  231. static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
  232. {
  233. return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
  234. }
  235. static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
  236. {
  237. return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
  238. }
  239. static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
  240. {
  241. return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
  242. }
  243. #endif