1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _VHOST_H
3 #define _VHOST_H
4
5 #include <linux/eventfd.h>
6 #include <linux/vhost.h>
7 #include <linux/mm.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>
17
18 struct vhost_work;
19 typedef void (*vhost_work_fn_t)(struct vhost_work *work);
20
21 #define VHOST_WORK_QUEUED 1
22 struct vhost_work {
23 struct llist_node node;
24 vhost_work_fn_t fn;
25 unsigned long flags;
26 };
27
28 /* Poll a file (eventfd or socket) */
29 /* Note: there's nothing vhost specific about this structure. */
30 struct vhost_poll {
31 poll_table table;
32 wait_queue_head_t *wqh;
33 wait_queue_entry_t wait;
34 struct vhost_work work;
35 __poll_t mask;
36 struct vhost_dev *dev;
37 };
38
39 void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
40 void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
41 bool vhost_has_work(struct vhost_dev *dev);
42
43 void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
44 __poll_t mask, struct vhost_dev *dev);
45 int vhost_poll_start(struct vhost_poll *poll, struct file *file);
46 void vhost_poll_stop(struct vhost_poll *poll);
47 void vhost_poll_flush(struct vhost_poll *poll);
48 void vhost_poll_queue(struct vhost_poll *poll);
49 void vhost_work_dev_flush(struct vhost_dev *dev);
50
51 struct vhost_log {
52 u64 addr;
53 u64 len;
54 };
55
56 enum vhost_uaddr_type {
57 VHOST_ADDR_DESC = 0,
58 VHOST_ADDR_AVAIL = 1,
59 VHOST_ADDR_USED = 2,
60 VHOST_NUM_ADDRS = 3,
61 };
62
63 struct vhost_vring_call {
64 struct eventfd_ctx *ctx;
65 struct irq_bypass_producer producer;
66 };
67
68 /* The virtqueue structure describes a queue attached to a device. */
69 struct vhost_virtqueue {
70 struct vhost_dev *dev;
71
72 /* The actual ring of buffers. */
73 struct mutex mutex;
74 unsigned int num;
75 vring_desc_t __user *desc;
76 vring_avail_t __user *avail;
77 vring_used_t __user *used;
78 const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS];
79 struct file *kick;
80 struct vhost_vring_call call_ctx;
81 struct eventfd_ctx *error_ctx;
82 struct eventfd_ctx *log_ctx;
83
84 struct vhost_poll poll;
85
86 /* The routine to call when the Guest pings us, or timeout. */
87 vhost_work_fn_t handle_kick;
88
89 /* Last available index we saw.
90 * Values are limited to 0x7fff, and the high bit is used as
91 * a wrap counter when using VIRTIO_F_RING_PACKED. */
92 u16 last_avail_idx;
93
94 /* Caches available index value from user. */
95 u16 avail_idx;
96
97 /* Last index we used.
98 * Values are limited to 0x7fff, and the high bit is used as
99 * a wrap counter when using VIRTIO_F_RING_PACKED. */
100 u16 last_used_idx;
101
102 /* Used flags */
103 u16 used_flags;
104
105 /* Last used index value we have signalled on */
106 u16 signalled_used;
107
108 /* Last used index value we have signalled on */
109 bool signalled_used_valid;
110
111 /* Log writes to used structure. */
112 bool log_used;
113 u64 log_addr;
114
115 struct iovec iov[UIO_MAXIOV];
116 struct iovec iotlb_iov[64];
117 struct iovec *indirect;
118 struct vring_used_elem *heads;
119 /* Protected by virtqueue mutex. */
120 struct vhost_iotlb *umem;
121 struct vhost_iotlb *iotlb;
122 void *private_data;
123 u64 acked_features;
124 u64 acked_backend_features;
125 /* Log write descriptors */
126 void __user *log_base;
127 struct vhost_log *log;
128 struct iovec log_iov[64];
129
130 /* Ring endianness. Defaults to legacy native endianness.
131 * Set to true when starting a modern virtio device. */
132 bool is_le;
133 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
134 /* Ring endianness requested by userspace for cross-endian support. */
135 bool user_be;
136 #endif
137 u32 busyloop_timeout;
138 };
139
140 struct vhost_msg_node {
141 union {
142 struct vhost_msg msg;
143 struct vhost_msg_v2 msg_v2;
144 };
145 struct vhost_virtqueue *vq;
146 struct list_head node;
147 };
148
149 struct vhost_dev {
150 struct mm_struct *mm;
151 struct mutex mutex;
152 struct vhost_virtqueue **vqs;
153 int nvqs;
154 struct eventfd_ctx *log_ctx;
155 struct llist_head work_list;
156 struct task_struct *worker;
157 struct vhost_iotlb *umem;
158 struct vhost_iotlb *iotlb;
159 spinlock_t iotlb_lock;
160 struct list_head read_list;
161 struct list_head pending_list;
162 wait_queue_head_t wait;
163 int iov_limit;
164 int weight;
165 int byte_weight;
166 u64 kcov_handle;
167 bool use_worker;
168 int (*msg_handler)(struct vhost_dev *dev,
169 struct vhost_iotlb_msg *msg);
170 };
171
172 bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
173 void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
174 int nvqs, int iov_limit, int weight, int byte_weight,
175 bool use_worker,
176 int (*msg_handler)(struct vhost_dev *dev,
177 struct vhost_iotlb_msg *msg));
178 long vhost_dev_set_owner(struct vhost_dev *dev);
179 bool vhost_dev_has_owner(struct vhost_dev *dev);
180 long vhost_dev_check_owner(struct vhost_dev *);
181 struct vhost_iotlb *vhost_dev_reset_owner_prepare(void);
182 void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *iotlb);
183 void vhost_dev_cleanup(struct vhost_dev *);
184 void vhost_dev_stop(struct vhost_dev *);
185 long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
186 long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
187 bool vhost_vq_access_ok(struct vhost_virtqueue *vq);
188 bool vhost_log_access_ok(struct vhost_dev *);
189 void vhost_clear_msg(struct vhost_dev *dev);
190
191 int vhost_get_vq_desc(struct vhost_virtqueue *,
192 struct iovec iov[], unsigned int iov_count,
193 unsigned int *out_num, unsigned int *in_num,
194 struct vhost_log *log, unsigned int *log_num);
195 void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
196
197 bool vhost_vq_is_setup(struct vhost_virtqueue *vq);
198 int vhost_vq_init_access(struct vhost_virtqueue *);
199 int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
200 int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
201 unsigned count);
202 void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
203 unsigned int id, int len);
204 void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
205 struct vring_used_elem *heads, unsigned count);
206 void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
207 void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
208 bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
209 bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
210
211 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
212 unsigned int log_num, u64 len,
213 struct iovec *iov, int count);
214 int vq_meta_prefetch(struct vhost_virtqueue *vq);
215
216 struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type);
217 void vhost_enqueue_msg(struct vhost_dev *dev,
218 struct list_head *head,
219 struct vhost_msg_node *node);
220 struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
221 struct list_head *head);
222 void vhost_set_backend_features(struct vhost_dev *dev, u64 features);
223
224 __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
225 poll_table *wait);
226 ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
227 int noblock);
228 ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
229 struct iov_iter *from);
230 int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled);
231
232 void vhost_iotlb_map_free(struct vhost_iotlb *iotlb,
233 struct vhost_iotlb_map *map);
234
235 #define vq_err(vq, fmt, ...) do { \
236 pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \
237 if ((vq)->error_ctx) \
238 eventfd_signal((vq)->error_ctx, 1);\
239 } while (0)
240
241 enum {
242 VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
243 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
244 (1ULL << VIRTIO_RING_F_EVENT_IDX) |
245 (1ULL << VHOST_F_LOG_ALL) |
246 (1ULL << VIRTIO_F_ANY_LAYOUT) |
247 (1ULL << VIRTIO_F_VERSION_1)
248 };
249
250 /**
251 * vhost_vq_set_backend - Set backend.
252 *
253 * @vq Virtqueue.
254 * @private_data The private data.
255 *
256 * Context: Need to call with vq->mutex acquired.
257 */
vhost_vq_set_backend(struct vhost_virtqueue * vq,void * private_data)258 static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq,
259 void *private_data)
260 {
261 vq->private_data = private_data;
262 }
263
264 /**
265 * vhost_vq_get_backend - Get backend.
266 *
267 * @vq Virtqueue.
268 *
269 * Context: Need to call with vq->mutex acquired.
270 * Return: Private data previously set with vhost_vq_set_backend.
271 */
vhost_vq_get_backend(struct vhost_virtqueue * vq)272 static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq)
273 {
274 return vq->private_data;
275 }
276
vhost_has_feature(struct vhost_virtqueue * vq,int bit)277 static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
278 {
279 return vq->acked_features & (1ULL << bit);
280 }
281
vhost_backend_has_feature(struct vhost_virtqueue * vq,int bit)282 static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit)
283 {
284 return vq->acked_backend_features & (1ULL << bit);
285 }
286
287 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
vhost_is_little_endian(struct vhost_virtqueue * vq)288 static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
289 {
290 return vq->is_le;
291 }
292 #else
vhost_is_little_endian(struct vhost_virtqueue * vq)293 static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
294 {
295 return virtio_legacy_is_little_endian() || vq->is_le;
296 }
297 #endif
298
299 /* Memory accessors */
vhost16_to_cpu(struct vhost_virtqueue * vq,__virtio16 val)300 static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
301 {
302 return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
303 }
304
cpu_to_vhost16(struct vhost_virtqueue * vq,u16 val)305 static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
306 {
307 return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
308 }
309
vhost32_to_cpu(struct vhost_virtqueue * vq,__virtio32 val)310 static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
311 {
312 return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
313 }
314
cpu_to_vhost32(struct vhost_virtqueue * vq,u32 val)315 static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
316 {
317 return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
318 }
319
vhost64_to_cpu(struct vhost_virtqueue * vq,__virtio64 val)320 static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
321 {
322 return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
323 }
324
cpu_to_vhost64(struct vhost_virtqueue * vq,u64 val)325 static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
326 {
327 return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
328 }
329 #endif
330