1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/eventfd.h>
6 #include <linux/vhost.h>
8 #include <linux/mutex.h>
9 #include <linux/poll.h>
10 #include <linux/file.h>
11 #include <linux/uio.h>
12 #include <linux/virtio_config.h>
13 #include <linux/virtio_ring.h>
14 #include <linux/atomic.h>
15 #include <linux/vhost_iotlb.h>
16 #include <linux/irqbypass.h>
20 typedef void (*vhost_work_fn_t)(struct vhost_work *work);
22 #define VHOST_WORK_QUEUED 1
24 struct llist_node node;
30 struct vhost_task *vtsk;
31 struct llist_head work_list;
35 /* Poll a file (eventfd or socket) */
36 /* Note: there's nothing vhost specific about this structure. */
39 wait_queue_head_t *wqh;
40 wait_queue_entry_t wait;
41 struct vhost_work work;
43 struct vhost_dev *dev;
46 void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
47 bool vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
49 void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
50 __poll_t mask, struct vhost_dev *dev);
51 int vhost_poll_start(struct vhost_poll *poll, struct file *file);
52 void vhost_poll_stop(struct vhost_poll *poll);
53 void vhost_poll_queue(struct vhost_poll *poll);
54 void vhost_dev_flush(struct vhost_dev *dev);
61 enum vhost_uaddr_type {
68 struct vhost_vring_call {
69 struct eventfd_ctx *ctx;
70 struct irq_bypass_producer producer;
73 /* The virtqueue structure describes a queue attached to a device. */
74 struct vhost_virtqueue {
75 struct vhost_dev *dev;
76 struct vhost_worker *worker;
78 /* The actual ring of buffers. */
81 vring_desc_t __user *desc;
82 vring_avail_t __user *avail;
83 vring_used_t __user *used;
84 const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS];
86 struct vhost_vring_call call_ctx;
87 struct eventfd_ctx *error_ctx;
88 struct eventfd_ctx *log_ctx;
90 struct vhost_poll poll;
92 /* The routine to call when the Guest pings us, or timeout. */
93 vhost_work_fn_t handle_kick;
95 /* Last available index we saw.
96 * Values are limited to 0x7fff, and the high bit is used as
97 * a wrap counter when using VIRTIO_F_RING_PACKED. */
100 /* Caches available index value from user. */
103 /* Last index we used.
104 * Values are limited to 0x7fff, and the high bit is used as
105 * a wrap counter when using VIRTIO_F_RING_PACKED. */
111 /* Last used index value we have signalled on */
114 /* Last used index value we have signalled on */
115 bool signalled_used_valid;
117 /* Log writes to used structure. */
121 struct iovec iov[UIO_MAXIOV];
122 struct iovec iotlb_iov[64];
123 struct iovec *indirect;
124 struct vring_used_elem *heads;
125 /* Protected by virtqueue mutex. */
126 struct vhost_iotlb *umem;
127 struct vhost_iotlb *iotlb;
130 u64 acked_backend_features;
131 /* Log write descriptors */
132 void __user *log_base;
133 struct vhost_log *log;
134 struct iovec log_iov[64];
136 /* Ring endianness. Defaults to legacy native endianness.
137 * Set to true when starting a modern virtio device. */
139 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
140 /* Ring endianness requested by userspace for cross-endian support. */
143 u32 busyloop_timeout;
146 struct vhost_msg_node {
148 struct vhost_msg msg;
149 struct vhost_msg_v2 msg_v2;
151 struct vhost_virtqueue *vq;
152 struct list_head node;
156 struct mm_struct *mm;
158 struct vhost_virtqueue **vqs;
160 struct eventfd_ctx *log_ctx;
161 struct vhost_worker *worker;
162 struct vhost_iotlb *umem;
163 struct vhost_iotlb *iotlb;
164 spinlock_t iotlb_lock;
165 struct list_head read_list;
166 struct list_head pending_list;
167 wait_queue_head_t wait;
172 int (*msg_handler)(struct vhost_dev *dev, u32 asid,
173 struct vhost_iotlb_msg *msg);
176 bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
177 void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
178 int nvqs, int iov_limit, int weight, int byte_weight,
180 int (*msg_handler)(struct vhost_dev *dev, u32 asid,
181 struct vhost_iotlb_msg *msg));
182 long vhost_dev_set_owner(struct vhost_dev *dev);
183 bool vhost_dev_has_owner(struct vhost_dev *dev);
184 long vhost_dev_check_owner(struct vhost_dev *);
185 struct vhost_iotlb *vhost_dev_reset_owner_prepare(void);
186 void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *iotlb);
187 void vhost_dev_cleanup(struct vhost_dev *);
188 void vhost_dev_stop(struct vhost_dev *);
189 long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
190 long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
191 bool vhost_vq_access_ok(struct vhost_virtqueue *vq);
192 bool vhost_log_access_ok(struct vhost_dev *);
193 void vhost_clear_msg(struct vhost_dev *dev);
195 int vhost_get_vq_desc(struct vhost_virtqueue *,
196 struct iovec iov[], unsigned int iov_count,
197 unsigned int *out_num, unsigned int *in_num,
198 struct vhost_log *log, unsigned int *log_num);
199 void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
201 bool vhost_vq_work_queue(struct vhost_virtqueue *vq, struct vhost_work *work);
202 bool vhost_vq_has_work(struct vhost_virtqueue *vq);
203 bool vhost_vq_is_setup(struct vhost_virtqueue *vq);
204 int vhost_vq_init_access(struct vhost_virtqueue *);
205 int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
206 int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
208 void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
209 unsigned int id, int len);
210 void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
211 struct vring_used_elem *heads, unsigned count);
212 void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
213 void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
214 bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
215 bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
217 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
218 unsigned int log_num, u64 len,
219 struct iovec *iov, int count);
220 int vq_meta_prefetch(struct vhost_virtqueue *vq);
222 struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type);
223 void vhost_enqueue_msg(struct vhost_dev *dev,
224 struct list_head *head,
225 struct vhost_msg_node *node);
226 struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
227 struct list_head *head);
228 void vhost_set_backend_features(struct vhost_dev *dev, u64 features);
230 __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
232 ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
234 ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
235 struct iov_iter *from);
236 int vhost_init_device_iotlb(struct vhost_dev *d);
238 void vhost_iotlb_map_free(struct vhost_iotlb *iotlb,
239 struct vhost_iotlb_map *map);
241 #define vq_err(vq, fmt, ...) do { \
242 pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
243 if ((vq)->error_ctx) \
244 eventfd_signal((vq)->error_ctx, 1);\
248 VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
249 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
250 (1ULL << VIRTIO_RING_F_EVENT_IDX) |
251 (1ULL << VHOST_F_LOG_ALL) |
252 (1ULL << VIRTIO_F_ANY_LAYOUT) |
253 (1ULL << VIRTIO_F_VERSION_1)
257 * vhost_vq_set_backend - Set backend.
260 * @private_data The private data.
262 * Context: Need to call with vq->mutex acquired.
264 static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq,
267 vq->private_data = private_data;
271 * vhost_vq_get_backend - Get backend.
275 * Context: Need to call with vq->mutex acquired.
276 * Return: Private data previously set with vhost_vq_set_backend.
278 static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq)
280 return vq->private_data;
283 static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
285 return vq->acked_features & (1ULL << bit);
288 static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit)
290 return vq->acked_backend_features & (1ULL << bit);
293 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
294 static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
299 static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
301 return virtio_legacy_is_little_endian() || vq->is_le;
305 /* Memory accessors */
306 static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
308 return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
311 static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
313 return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
316 static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
318 return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
321 static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
323 return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
326 static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
328 return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
331 static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
333 return __cpu_to_virtio64(vhost_is_little_endian(vq), val);