vhost.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. /* Used flags */
  93. u16 used_flags;
  94. /* Last used index value we have signalled on */
  95. u16 signalled_used;
  96. /* Last used index value we have signalled on */
  97. bool signalled_used_valid;
  98. /* Log writes to used structure. */
  99. bool log_used;
  100. u64 log_addr;
  101. struct iovec iov[UIO_MAXIOV];
  102. struct iovec iotlb_iov[64];
  103. struct iovec *indirect;
  104. struct vring_used_elem *heads;
  105. /* Protected by virtqueue mutex. */
  106. struct vhost_umem *umem;
  107. struct vhost_umem *iotlb;
  108. void *private_data;
  109. u64 acked_features;
  110. /* Log write descriptors */
  111. void __user *log_base;
  112. struct vhost_log *log;
  113. /* Ring endianness. Defaults to legacy native endianness.
  114. * Set to true when starting a modern virtio device. */
  115. bool is_le;
  116. #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
  117. /* Ring endianness requested by userspace for cross-endian support. */
  118. bool user_be;
  119. #endif
  120. u32 busyloop_timeout;
  121. };
  122. struct vhost_msg_node {
  123. struct vhost_msg msg;
  124. struct vhost_virtqueue *vq;
  125. struct list_head node;
  126. };
  127. struct vhost_dev {
  128. struct mm_struct *mm;
  129. struct mutex mutex;
  130. struct vhost_virtqueue **vqs;
  131. int nvqs;
  132. struct file *log_file;
  133. struct eventfd_ctx *log_ctx;
  134. struct llist_head work_list;
  135. struct task_struct *worker;
  136. struct vhost_umem *umem;
  137. struct vhost_umem *iotlb;
  138. spinlock_t iotlb_lock;
  139. struct list_head read_list;
  140. struct list_head pending_list;
  141. wait_queue_head_t wait;
  142. };
  143. void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs, int nvqs);
  144. long vhost_dev_set_owner(struct vhost_dev *dev);
  145. bool vhost_dev_has_owner(struct vhost_dev *dev);
  146. long vhost_dev_check_owner(struct vhost_dev *);
  147. struct vhost_umem *vhost_dev_reset_owner_prepare(void);
  148. void vhost_dev_reset_owner(struct vhost_dev *, struct vhost_umem *);
  149. void vhost_dev_cleanup(struct vhost_dev *, bool locked);
  150. void vhost_dev_stop(struct vhost_dev *);
  151. long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
  152. long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp);
  153. int vhost_vq_access_ok(struct vhost_virtqueue *vq);
  154. int vhost_log_access_ok(struct vhost_dev *);
  155. int vhost_get_vq_desc(struct vhost_virtqueue *,
  156. struct iovec iov[], unsigned int iov_count,
  157. unsigned int *out_num, unsigned int *in_num,
  158. struct vhost_log *log, unsigned int *log_num);
  159. void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
  160. int vhost_vq_init_access(struct vhost_virtqueue *);
  161. int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
  162. int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
  163. unsigned count);
  164. void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
  165. unsigned int id, int len);
  166. void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
  167. struct vring_used_elem *heads, unsigned count);
  168. void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
  169. void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  170. bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
  171. bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
  172. int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
  173. unsigned int log_num, u64 len);
  174. int vq_iotlb_prefetch(struct vhost_virtqueue *vq);
  175. struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type);
  176. void vhost_enqueue_msg(struct vhost_dev *dev,
  177. struct list_head *head,
  178. struct vhost_msg_node *node);
  179. struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
  180. struct list_head *head);
  181. unsigned int vhost_chr_poll(struct file *file, struct vhost_dev *dev,
  182. poll_table *wait);
  183. ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
  184. int noblock);
  185. ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
  186. struct iov_iter *from);
  187. int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
  188. #define vq_err(vq, fmt, ...) do { \
  189. pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
  190. if ((vq)->error_ctx) \
  191. eventfd_signal((vq)->error_ctx, 1);\
  192. } while (0)
  193. enum {
  194. VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
  195. (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
  196. (1ULL << VIRTIO_RING_F_EVENT_IDX) |
  197. (1ULL << VHOST_F_LOG_ALL) |
  198. (1ULL << VIRTIO_F_ANY_LAYOUT) |
  199. (1ULL << VIRTIO_F_VERSION_1)
  200. };
  201. static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
  202. {
  203. return vq->acked_features & (1ULL << bit);
  204. }
  205. #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
  206. static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
  207. {
  208. return vq->is_le;
  209. }
  210. #else
  211. static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
  212. {
  213. return virtio_legacy_is_little_endian() || vq->is_le;
  214. }
  215. #endif
  216. /* Memory accessors */
  217. static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
  218. {
  219. return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
  220. }
  221. static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
  222. {
  223. return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
  224. }
  225. static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
  226. {
  227. return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
  228. }
  229. static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
  230. {
  231. return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
  232. }
  233. static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
  234. {
  235. return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
  236. }
  237. static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
  238. {
  239. return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
  240. }
  241. #endif