]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
3a4d5c94 MT |
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> | |
3a4d5c94 MT |
9 | #include <linux/poll.h> |
10 | #include <linux/file.h> | |
3a4d5c94 MT |
11 | #include <linux/uio.h> |
12 | #include <linux/virtio_config.h> | |
13 | #include <linux/virtio_ring.h> | |
60063497 | 14 | #include <linux/atomic.h> |
3a4d5c94 | 15 | |
c23f3445 TH |
16 | struct vhost_work; |
17 | typedef void (*vhost_work_fn_t)(struct vhost_work *work); | |
18 | ||
04b96e55 | 19 | #define VHOST_WORK_QUEUED 1 |
c23f3445 | 20 | struct vhost_work { |
04b96e55 | 21 | struct llist_node node; |
c23f3445 | 22 | vhost_work_fn_t fn; |
04b96e55 | 23 | unsigned long flags; |
c23f3445 TH |
24 | }; |
25 | ||
3a4d5c94 MT |
26 | /* Poll a file (eventfd or socket) */ |
27 | /* Note: there's nothing vhost specific about this structure. */ | |
28 | struct vhost_poll { | |
29 | poll_table table; | |
30 | wait_queue_head_t *wqh; | |
ac6424b9 | 31 | wait_queue_entry_t wait; |
c23f3445 | 32 | struct vhost_work work; |
58e3b602 | 33 | __poll_t mask; |
c23f3445 | 34 | struct vhost_dev *dev; |
3a4d5c94 MT |
35 | }; |
36 | ||
163049ae SH |
37 | void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn); |
38 | void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work); | |
526d3e7f | 39 | bool vhost_has_work(struct vhost_dev *dev); |
163049ae | 40 | |
c23f3445 | 41 | void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, |
58e3b602 | 42 | __poll_t mask, struct vhost_dev *dev); |
2b8b328b | 43 | int vhost_poll_start(struct vhost_poll *poll, struct file *file); |
3a4d5c94 MT |
44 | void vhost_poll_stop(struct vhost_poll *poll); |
45 | void vhost_poll_flush(struct vhost_poll *poll); | |
46 | void vhost_poll_queue(struct vhost_poll *poll); | |
6ac1afbf | 47 | void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work); |
26b36604 | 48 | long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp); |
3a4d5c94 MT |
49 | |
50 | struct vhost_log { | |
51 | u64 addr; | |
52 | u64 len; | |
53 | }; | |
54 | ||
a9709d68 JW |
55 | #define START(node) ((node)->start) |
56 | #define LAST(node) ((node)->last) | |
57 | ||
58 | struct vhost_umem_node { | |
59 | struct rb_node rb; | |
60 | struct list_head link; | |
61 | __u64 start; | |
62 | __u64 last; | |
63 | __u64 size; | |
64 | __u64 userspace_addr; | |
6b1e6cc7 JW |
65 | __u32 perm; |
66 | __u32 flags_padding; | |
a9709d68 JW |
67 | __u64 __subtree_last; |
68 | }; | |
69 | ||
70 | struct vhost_umem { | |
f808c13f | 71 | struct rb_root_cached umem_tree; |
a9709d68 | 72 | struct list_head umem_list; |
6b1e6cc7 | 73 | int numem; |
a9709d68 JW |
74 | }; |
75 | ||
f8894913 JW |
76 | enum vhost_uaddr_type { |
77 | VHOST_ADDR_DESC = 0, | |
78 | VHOST_ADDR_AVAIL = 1, | |
79 | VHOST_ADDR_USED = 2, | |
80 | VHOST_NUM_ADDRS = 3, | |
81 | }; | |
82 | ||
3a4d5c94 MT |
83 | /* The virtqueue structure describes a queue attached to a device. */ |
84 | struct vhost_virtqueue { | |
85 | struct vhost_dev *dev; | |
86 | ||
87 | /* The actual ring of buffers. */ | |
88 | struct mutex mutex; | |
89 | unsigned int num; | |
90 | struct vring_desc __user *desc; | |
91 | struct vring_avail __user *avail; | |
92 | struct vring_used __user *used; | |
f8894913 | 93 | const struct vhost_umem_node *meta_iotlb[VHOST_NUM_ADDRS]; |
3a4d5c94 | 94 | struct file *kick; |
3a4d5c94 MT |
95 | struct eventfd_ctx *call_ctx; |
96 | struct eventfd_ctx *error_ctx; | |
97 | struct eventfd_ctx *log_ctx; | |
98 | ||
99 | struct vhost_poll poll; | |
100 | ||
101 | /* The routine to call when the Guest pings us, or timeout. */ | |
c23f3445 | 102 | vhost_work_fn_t handle_kick; |
3a4d5c94 MT |
103 | |
104 | /* Last available index we saw. */ | |
105 | u16 last_avail_idx; | |
106 | ||
107 | /* Caches available index value from user. */ | |
108 | u16 avail_idx; | |
109 | ||
110 | /* Last index we used. */ | |
111 | u16 last_used_idx; | |
112 | ||
113 | /* Used flags */ | |
114 | u16 used_flags; | |
115 | ||
8ea8cf89 MT |
116 | /* Last used index value we have signalled on */ |
117 | u16 signalled_used; | |
118 | ||
119 | /* Last used index value we have signalled on */ | |
120 | bool signalled_used_valid; | |
121 | ||
3a4d5c94 MT |
122 | /* Log writes to used structure. */ |
123 | bool log_used; | |
124 | u64 log_addr; | |
125 | ||
e0e9b406 | 126 | struct iovec iov[UIO_MAXIOV]; |
6b1e6cc7 | 127 | struct iovec iotlb_iov[64]; |
e0e9b406 | 128 | struct iovec *indirect; |
e0e9b406 | 129 | struct vring_used_elem *heads; |
22fa90c7 | 130 | /* Protected by virtqueue mutex. */ |
a9709d68 | 131 | struct vhost_umem *umem; |
6b1e6cc7 | 132 | struct vhost_umem *iotlb; |
22fa90c7 | 133 | void *private_data; |
bd82752a | 134 | u64 acked_features; |
429711ae | 135 | u64 acked_backend_features; |
3a4d5c94 MT |
136 | /* Log write descriptors */ |
137 | void __user *log_base; | |
e0e9b406 | 138 | struct vhost_log *log; |
2751c988 GK |
139 | |
140 | /* Ring endianness. Defaults to legacy native endianness. | |
141 | * Set to true when starting a modern virtio device. */ | |
142 | bool is_le; | |
143 | #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY | |
144 | /* Ring endianness requested by userspace for cross-endian support. */ | |
145 | bool user_be; | |
146 | #endif | |
03088137 | 147 | u32 busyloop_timeout; |
3a4d5c94 MT |
148 | }; |
149 | ||
6b1e6cc7 | 150 | struct vhost_msg_node { |
429711ae JW |
151 | union { |
152 | struct vhost_msg msg; | |
153 | struct vhost_msg_v2 msg_v2; | |
154 | }; | |
6b1e6cc7 JW |
155 | struct vhost_virtqueue *vq; |
156 | struct list_head node; | |
157 | }; | |
158 | ||
3a4d5c94 | 159 | struct vhost_dev { |
3a4d5c94 MT |
160 | struct mm_struct *mm; |
161 | struct mutex mutex; | |
3ab2e420 | 162 | struct vhost_virtqueue **vqs; |
3a4d5c94 | 163 | int nvqs; |
3a4d5c94 | 164 | struct eventfd_ctx *log_ctx; |
04b96e55 | 165 | struct llist_head work_list; |
c23f3445 | 166 | struct task_struct *worker; |
a9709d68 | 167 | struct vhost_umem *umem; |
6b1e6cc7 JW |
168 | struct vhost_umem *iotlb; |
169 | spinlock_t iotlb_lock; | |
170 | struct list_head read_list; | |
171 | struct list_head pending_list; | |
172 | wait_queue_head_t wait; | |
3a4d5c94 MT |
173 | }; |
174 | ||
59566b6e | 175 | void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs, int nvqs); |
54db63c2 | 176 | long vhost_dev_set_owner(struct vhost_dev *dev); |
05c05351 | 177 | bool vhost_dev_has_owner(struct vhost_dev *dev); |
3a4d5c94 | 178 | long vhost_dev_check_owner(struct vhost_dev *); |
a9709d68 JW |
179 | struct vhost_umem *vhost_dev_reset_owner_prepare(void); |
180 | void vhost_dev_reset_owner(struct vhost_dev *, struct vhost_umem *); | |
f6f93f75 | 181 | void vhost_dev_cleanup(struct vhost_dev *); |
b211616d | 182 | void vhost_dev_stop(struct vhost_dev *); |
935cdee7 | 183 | long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp); |
26b36604 | 184 | long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp); |
ddd3d408 SH |
185 | bool vhost_vq_access_ok(struct vhost_virtqueue *vq); |
186 | bool vhost_log_access_ok(struct vhost_dev *); | |
3a4d5c94 | 187 | |
47283bef | 188 | int vhost_get_vq_desc(struct vhost_virtqueue *, |
d5675bd2 MT |
189 | struct iovec iov[], unsigned int iov_count, |
190 | unsigned int *out_num, unsigned int *in_num, | |
191 | struct vhost_log *log, unsigned int *log_num); | |
8dd014ad | 192 | void vhost_discard_vq_desc(struct vhost_virtqueue *, int n); |
3a4d5c94 | 193 | |
80f7d030 | 194 | int vhost_vq_init_access(struct vhost_virtqueue *); |
3a4d5c94 | 195 | int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len); |
8dd014ad DS |
196 | int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads, |
197 | unsigned count); | |
3a4d5c94 | 198 | void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *, |
8dd014ad DS |
199 | unsigned int id, int len); |
200 | void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *, | |
201 | struct vring_used_elem *heads, unsigned count); | |
202 | void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *); | |
8ea8cf89 | 203 | void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *); |
d4a60603 | 204 | bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *); |
8ea8cf89 | 205 | bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *); |
3a4d5c94 MT |
206 | |
207 | int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, | |
208 | unsigned int log_num, u64 len); | |
6b1e6cc7 JW |
209 | int vq_iotlb_prefetch(struct vhost_virtqueue *vq); |
210 | ||
211 | struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type); | |
212 | void vhost_enqueue_msg(struct vhost_dev *dev, | |
213 | struct list_head *head, | |
214 | struct vhost_msg_node *node); | |
215 | struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev, | |
216 | struct list_head *head); | |
afc9a42b | 217 | __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev, |
6b1e6cc7 JW |
218 | poll_table *wait); |
219 | ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to, | |
220 | int noblock); | |
221 | ssize_t vhost_chr_write_iter(struct vhost_dev *dev, | |
222 | struct iov_iter *from); | |
223 | int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled); | |
3a4d5c94 | 224 | |
3a4d5c94 MT |
225 | #define vq_err(vq, fmt, ...) do { \ |
226 | pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \ | |
227 | if ((vq)->error_ctx) \ | |
228 | eventfd_signal((vq)->error_ctx, 1);\ | |
229 | } while (0) | |
230 | ||
231 | enum { | |
8ea8cf89 MT |
232 | VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | |
233 | (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | | |
234 | (1ULL << VIRTIO_RING_F_EVENT_IDX) | | |
4e9fa50c MT |
235 | (1ULL << VHOST_F_LOG_ALL) | |
236 | (1ULL << VIRTIO_F_ANY_LAYOUT) | | |
237 | (1ULL << VIRTIO_F_VERSION_1) | |
3a4d5c94 MT |
238 | }; |
239 | ||
bd82752a | 240 | static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit) |
3a4d5c94 | 241 | { |
bd82752a | 242 | return vq->acked_features & (1ULL << bit); |
3a4d5c94 | 243 | } |
e05fd12b | 244 | |
429711ae JW |
245 | static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit) |
246 | { | |
247 | return vq->acked_backend_features & (1ULL << bit); | |
248 | } | |
249 | ||
e407f39a | 250 | #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY |
ab27c07f GK |
251 | static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq) |
252 | { | |
2751c988 | 253 | return vq->is_le; |
ab27c07f | 254 | } |
e407f39a MT |
255 | #else |
256 | static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq) | |
257 | { | |
258 | return virtio_legacy_is_little_endian() || vq->is_le; | |
259 | } | |
260 | #endif | |
ab27c07f | 261 | |
e05fd12b MT |
262 | /* Memory accessors */ |
263 | static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val) | |
264 | { | |
ab27c07f | 265 | return __virtio16_to_cpu(vhost_is_little_endian(vq), val); |
e05fd12b MT |
266 | } |
267 | ||
268 | static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val) | |
269 | { | |
ab27c07f | 270 | return __cpu_to_virtio16(vhost_is_little_endian(vq), val); |
e05fd12b MT |
271 | } |
272 | ||
273 | static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val) | |
274 | { | |
ab27c07f | 275 | return __virtio32_to_cpu(vhost_is_little_endian(vq), val); |
e05fd12b MT |
276 | } |
277 | ||
278 | static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val) | |
279 | { | |
ab27c07f | 280 | return __cpu_to_virtio32(vhost_is_little_endian(vq), val); |
e05fd12b MT |
281 | } |
282 | ||
283 | static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val) | |
284 | { | |
ab27c07f | 285 | return __virtio64_to_cpu(vhost_is_little_endian(vq), val); |
e05fd12b MT |
286 | } |
287 | ||
288 | static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val) | |
289 | { | |
ab27c07f | 290 | return __cpu_to_virtio64(vhost_is_little_endian(vq), val); |
e05fd12b | 291 | } |
3a4d5c94 | 292 | #endif |