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 struct vhost_task; 20 typedef void (*vhost_work_fn_t)(struct vhost_work *work); 21 22 #define VHOST_WORK_QUEUED 1 23 struct vhost_work { 24 struct llist_node node; 25 vhost_work_fn_t fn; 26 unsigned long flags; 27 }; 28 29 struct vhost_worker { 30 struct vhost_task *vtsk; 31 struct llist_head work_list; 32 u64 kcov_handle; 33 }; 34 35 /* Poll a file (eventfd or socket) */ 36 /* Note: there's nothing vhost specific about this structure. */ 37 struct vhost_poll { 38 poll_table table; 39 wait_queue_head_t *wqh; 40 wait_queue_entry_t wait; 41 struct vhost_work work; 42 __poll_t mask; 43 struct vhost_dev *dev; 44 }; 45 46 void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn); 47 void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work); 48 bool vhost_has_work(struct vhost_dev *dev); 49 50 void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, 51 __poll_t mask, struct vhost_dev *dev); 52 int vhost_poll_start(struct vhost_poll *poll, struct file *file); 53 void vhost_poll_stop(struct vhost_poll *poll); 54 void vhost_poll_queue(struct vhost_poll *poll); 55 void vhost_dev_flush(struct vhost_dev *dev); 56 57 struct vhost_log { 58 u64 addr; 59 u64 len; 60 }; 61 62 enum vhost_uaddr_type { 63 VHOST_ADDR_DESC = 0, 64 VHOST_ADDR_AVAIL = 1, 65 VHOST_ADDR_USED = 2, 66 VHOST_NUM_ADDRS = 3, 67 }; 68 69 struct vhost_vring_call { 70 struct eventfd_ctx *ctx; 71 struct irq_bypass_producer producer; 72 }; 73 74 /* The virtqueue structure describes a queue attached to a device. */ 75 struct vhost_virtqueue { 76 struct vhost_dev *dev; 77 78 /* The actual ring of buffers. */ 79 struct mutex mutex; 80 unsigned int num; 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]; 85 struct file *kick; 86 struct vhost_vring_call call_ctx; 87 struct eventfd_ctx *error_ctx; 88 struct eventfd_ctx *log_ctx; 89 90 struct vhost_poll poll; 91 92 /* The routine to call when the Guest pings us, or timeout. */ 93 vhost_work_fn_t handle_kick; 94 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. */ 98 u16 last_avail_idx; 99 100 /* Caches available index value from user. */ 101 u16 avail_idx; 102 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. */ 106 u16 last_used_idx; 107 108 /* Used flags */ 109 u16 used_flags; 110 111 /* Last used index value we have signalled on */ 112 u16 signalled_used; 113 114 /* Last used index value we have signalled on */ 115 bool signalled_used_valid; 116 117 /* Log writes to used structure. */ 118 bool log_used; 119 u64 log_addr; 120 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; 128 void *private_data; 129 u64 acked_features; 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]; 135 136 /* Ring endianness. Defaults to legacy native endianness. 137 * Set to true when starting a modern virtio device. */ 138 bool is_le; 139 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY 140 /* Ring endianness requested by userspace for cross-endian support. */ 141 bool user_be; 142 #endif 143 u32 busyloop_timeout; 144 }; 145 146 struct vhost_msg_node { 147 union { 148 struct vhost_msg msg; 149 struct vhost_msg_v2 msg_v2; 150 }; 151 struct vhost_virtqueue *vq; 152 struct list_head node; 153 }; 154 155 struct vhost_dev { 156 struct mm_struct *mm; 157 struct mutex mutex; 158 struct vhost_virtqueue **vqs; 159 int nvqs; 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; 168 int iov_limit; 169 int weight; 170 int byte_weight; 171 bool use_worker; 172 int (*msg_handler)(struct vhost_dev *dev, u32 asid, 173 struct vhost_iotlb_msg *msg); 174 }; 175 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, 179 bool use_worker, 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); 194 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); 200 201 bool vhost_vq_is_setup(struct vhost_virtqueue *vq); 202 int vhost_vq_init_access(struct vhost_virtqueue *); 203 int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len); 204 int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads, 205 unsigned count); 206 void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *, 207 unsigned int id, int len); 208 void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *, 209 struct vring_used_elem *heads, unsigned count); 210 void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *); 211 void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *); 212 bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *); 213 bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *); 214 215 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, 216 unsigned int log_num, u64 len, 217 struct iovec *iov, int count); 218 int vq_meta_prefetch(struct vhost_virtqueue *vq); 219 220 struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type); 221 void vhost_enqueue_msg(struct vhost_dev *dev, 222 struct list_head *head, 223 struct vhost_msg_node *node); 224 struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev, 225 struct list_head *head); 226 void vhost_set_backend_features(struct vhost_dev *dev, u64 features); 227 228 __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev, 229 poll_table *wait); 230 ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to, 231 int noblock); 232 ssize_t vhost_chr_write_iter(struct vhost_dev *dev, 233 struct iov_iter *from); 234 int vhost_init_device_iotlb(struct vhost_dev *d); 235 236 void vhost_iotlb_map_free(struct vhost_iotlb *iotlb, 237 struct vhost_iotlb_map *map); 238 239 #define vq_err(vq, fmt, ...) do { \ 240 pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \ 241 if ((vq)->error_ctx) \ 242 eventfd_signal((vq)->error_ctx, 1);\ 243 } while (0) 244 245 enum { 246 VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | 247 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | 248 (1ULL << VIRTIO_RING_F_EVENT_IDX) | 249 (1ULL << VHOST_F_LOG_ALL) | 250 (1ULL << VIRTIO_F_ANY_LAYOUT) | 251 (1ULL << VIRTIO_F_VERSION_1) 252 }; 253 254 /** 255 * vhost_vq_set_backend - Set backend. 256 * 257 * @vq Virtqueue. 258 * @private_data The private data. 259 * 260 * Context: Need to call with vq->mutex acquired. 261 */ 262 static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq, 263 void *private_data) 264 { 265 vq->private_data = private_data; 266 } 267 268 /** 269 * vhost_vq_get_backend - Get backend. 270 * 271 * @vq Virtqueue. 272 * 273 * Context: Need to call with vq->mutex acquired. 274 * Return: Private data previously set with vhost_vq_set_backend. 275 */ 276 static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq) 277 { 278 return vq->private_data; 279 } 280 281 static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit) 282 { 283 return vq->acked_features & (1ULL << bit); 284 } 285 286 static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit) 287 { 288 return vq->acked_backend_features & (1ULL << bit); 289 } 290 291 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY 292 static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq) 293 { 294 return vq->is_le; 295 } 296 #else 297 static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq) 298 { 299 return virtio_legacy_is_little_endian() || vq->is_le; 300 } 301 #endif 302 303 /* Memory accessors */ 304 static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val) 305 { 306 return __virtio16_to_cpu(vhost_is_little_endian(vq), val); 307 } 308 309 static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val) 310 { 311 return __cpu_to_virtio16(vhost_is_little_endian(vq), val); 312 } 313 314 static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val) 315 { 316 return __virtio32_to_cpu(vhost_is_little_endian(vq), val); 317 } 318 319 static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val) 320 { 321 return __cpu_to_virtio32(vhost_is_little_endian(vq), val); 322 } 323 324 static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val) 325 { 326 return __virtio64_to_cpu(vhost_is_little_endian(vq), val); 327 } 328 329 static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val) 330 { 331 return __cpu_to_virtio64(vhost_is_little_endian(vq), val); 332 } 333 #endif 334