]> Git Repo - qemu.git/blob - hw/virtio/dataplane/vring.c
dataplane: fix cross-endian issues
[qemu.git] / hw / virtio / dataplane / vring.c
1 /* Copyright 2012 Red Hat, Inc.
2  * Copyright IBM, Corp. 2012
3  *
4  * Based on Linux 2.6.39 vhost code:
5  * Copyright (C) 2009 Red Hat, Inc.
6  * Copyright (C) 2006 Rusty Russell IBM Corporation
7  *
8  * Author: Michael S. Tsirkin <[email protected]>
9  *         Stefan Hajnoczi <[email protected]>
10  *
11  * Inspiration, some code, and most witty comments come from
12  * Documentation/virtual/lguest/lguest.c, by Rusty Russell
13  *
14  * This work is licensed under the terms of the GNU GPL, version 2.
15  */
16
17 #include "trace.h"
18 #include "hw/hw.h"
19 #include "exec/memory.h"
20 #include "exec/address-spaces.h"
21 #include "hw/virtio/virtio-access.h"
22 #include "hw/virtio/dataplane/vring.h"
23 #include "hw/virtio/dataplane/vring-accessors.h"
24 #include "qemu/error-report.h"
25
26 /* vring_map can be coupled with vring_unmap or (if you still have the
27  * value returned in *mr) memory_region_unref.
28  */
29 static void *vring_map(MemoryRegion **mr, hwaddr phys, hwaddr len,
30                        bool is_write)
31 {
32     MemoryRegionSection section = memory_region_find(get_system_memory(), phys, len);
33
34     if (!section.mr || int128_get64(section.size) < len) {
35         goto out;
36     }
37     if (is_write && section.readonly) {
38         goto out;
39     }
40     if (!memory_region_is_ram(section.mr)) {
41         goto out;
42     }
43
44     /* Ignore regions with dirty logging, we cannot mark them dirty */
45     if (memory_region_get_dirty_log_mask(section.mr)) {
46         goto out;
47     }
48
49     *mr = section.mr;
50     return memory_region_get_ram_ptr(section.mr) + section.offset_within_region;
51
52 out:
53     memory_region_unref(section.mr);
54     *mr = NULL;
55     return NULL;
56 }
57
58 static void vring_unmap(void *buffer, bool is_write)
59 {
60     ram_addr_t addr;
61     MemoryRegion *mr;
62
63     mr = qemu_ram_addr_from_host(buffer, &addr);
64     memory_region_unref(mr);
65 }
66
67 /* Map the guest's vring to host memory */
68 bool vring_setup(Vring *vring, VirtIODevice *vdev, int n)
69 {
70     hwaddr vring_addr = virtio_queue_get_ring_addr(vdev, n);
71     hwaddr vring_size = virtio_queue_get_ring_size(vdev, n);
72     void *vring_ptr;
73
74     vring->broken = false;
75
76     vring_ptr = vring_map(&vring->mr, vring_addr, vring_size, true);
77     if (!vring_ptr) {
78         error_report("Failed to map vring "
79                      "addr %#" HWADDR_PRIx " size %" HWADDR_PRIu,
80                      vring_addr, vring_size);
81         vring->broken = true;
82         return false;
83     }
84
85     vring_init(&vring->vr, virtio_queue_get_num(vdev, n), vring_ptr, 4096);
86
87     vring->last_avail_idx = virtio_queue_get_last_avail_idx(vdev, n);
88     vring->last_used_idx = vring_get_used_idx(vdev, vring);
89     vring->signalled_used = 0;
90     vring->signalled_used_valid = false;
91
92     trace_vring_setup(virtio_queue_get_ring_addr(vdev, n),
93                       vring->vr.desc, vring->vr.avail, vring->vr.used);
94     return true;
95 }
96
97 void vring_teardown(Vring *vring, VirtIODevice *vdev, int n)
98 {
99     virtio_queue_set_last_avail_idx(vdev, n, vring->last_avail_idx);
100     virtio_queue_invalidate_signalled_used(vdev, n);
101
102     memory_region_unref(vring->mr);
103 }
104
105 /* Disable guest->host notifies */
106 void vring_disable_notification(VirtIODevice *vdev, Vring *vring)
107 {
108     if (!virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
109         vring_set_used_flags(vdev, vring, VRING_USED_F_NO_NOTIFY);
110     }
111 }
112
113 /* Enable guest->host notifies
114  *
115  * Return true if the vring is empty, false if there are more requests.
116  */
117 bool vring_enable_notification(VirtIODevice *vdev, Vring *vring)
118 {
119     if (virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
120         vring_avail_event(&vring->vr) = vring->vr.avail->idx;
121     } else {
122         vring_clear_used_flags(vdev, vring, VRING_USED_F_NO_NOTIFY);
123     }
124     smp_mb(); /* ensure update is seen before reading avail_idx */
125     return !vring_more_avail(vdev, vring);
126 }
127
128 /* This is stolen from linux/drivers/vhost/vhost.c:vhost_notify() */
129 bool vring_should_notify(VirtIODevice *vdev, Vring *vring)
130 {
131     uint16_t old, new;
132     bool v;
133     /* Flush out used index updates. This is paired
134      * with the barrier that the Guest executes when enabling
135      * interrupts. */
136     smp_mb();
137
138     if (virtio_has_feature(vdev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
139         unlikely(!vring_more_avail(vdev, vring))) {
140         return true;
141     }
142
143     if (!virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
144         return !(vring_get_avail_flags(vdev, vring) &
145                  VRING_AVAIL_F_NO_INTERRUPT);
146     }
147     old = vring->signalled_used;
148     v = vring->signalled_used_valid;
149     new = vring->signalled_used = vring->last_used_idx;
150     vring->signalled_used_valid = true;
151
152     if (unlikely(!v)) {
153         return true;
154     }
155
156     return vring_need_event(virtio_tswap16(vdev, vring_used_event(&vring->vr)),
157                             new, old);
158 }
159
160
161 static int get_desc(VirtIODevice *vdev, Vring *vring, VirtQueueElement *elem,
162                     struct vring_desc *desc)
163 {
164     unsigned *num;
165     struct iovec *iov;
166     hwaddr *addr;
167     MemoryRegion *mr;
168     int is_write = virtio_tswap16(vdev, desc->flags) & VRING_DESC_F_WRITE;
169     uint32_t len = virtio_tswap32(vdev, desc->len);
170     uint64_t desc_addr = virtio_tswap64(vdev, desc->addr);
171
172     if (is_write) {
173         num = &elem->in_num;
174         iov = &elem->in_sg[*num];
175         addr = &elem->in_addr[*num];
176     } else {
177         num = &elem->out_num;
178         iov = &elem->out_sg[*num];
179         addr = &elem->out_addr[*num];
180
181         /* If it's an output descriptor, they're all supposed
182          * to come before any input descriptors. */
183         if (unlikely(elem->in_num)) {
184             error_report("Descriptor has out after in");
185             return -EFAULT;
186         }
187     }
188
189     /* Stop for now if there are not enough iovecs available. */
190     if (*num >= VIRTQUEUE_MAX_SIZE) {
191         error_report("Invalid SG num: %u", *num);
192         return -EFAULT;
193     }
194
195     /* TODO handle non-contiguous memory across region boundaries */
196     iov->iov_base = vring_map(&mr, desc_addr, len, is_write);
197     if (!iov->iov_base) {
198         error_report("Failed to map descriptor addr %#" PRIx64 " len %u",
199                      (uint64_t)desc_addr, len);
200         return -EFAULT;
201     }
202
203     /* The MemoryRegion is looked up again and unref'ed later, leave the
204      * ref in place.  */
205     iov->iov_len = len;
206     *addr = desc_addr;
207     *num += 1;
208     return 0;
209 }
210
211 static void copy_in_vring_desc(VirtIODevice *vdev,
212                                const struct vring_desc *guest,
213                                struct vring_desc *host)
214 {
215     host->addr = virtio_ldq_p(vdev, &guest->addr);
216     host->len = virtio_ldl_p(vdev, &guest->len);
217     host->flags = virtio_lduw_p(vdev, &guest->flags);
218     host->next = virtio_lduw_p(vdev, &guest->next);
219 }
220
221 /* This is stolen from linux/drivers/vhost/vhost.c. */
222 static int get_indirect(VirtIODevice *vdev, Vring *vring,
223                         VirtQueueElement *elem, struct vring_desc *indirect)
224 {
225     struct vring_desc desc;
226     unsigned int i = 0, count, found = 0;
227     int ret;
228     uint32_t len = virtio_tswap32(vdev, indirect->len);
229     uint64_t addr = virtio_tswap64(vdev, indirect->addr);
230
231     /* Sanity check */
232     if (unlikely(len % sizeof(desc))) {
233         error_report("Invalid length in indirect descriptor: "
234                      "len %#x not multiple of %#zx",
235                      len, sizeof(desc));
236         vring->broken = true;
237         return -EFAULT;
238     }
239
240     count = len / sizeof(desc);
241     /* Buffers are chained via a 16 bit next field, so
242      * we can have at most 2^16 of these. */
243     if (unlikely(count > USHRT_MAX + 1)) {
244         error_report("Indirect buffer length too big: %d", len);
245         vring->broken = true;
246         return -EFAULT;
247     }
248
249     do {
250         struct vring_desc *desc_ptr;
251         MemoryRegion *mr;
252
253         /* Translate indirect descriptor */
254         desc_ptr = vring_map(&mr,
255                              addr + found * sizeof(desc),
256                              sizeof(desc), false);
257         if (!desc_ptr) {
258             error_report("Failed to map indirect descriptor "
259                          "addr %#" PRIx64 " len %zu",
260                          (uint64_t)addr + found * sizeof(desc),
261                          sizeof(desc));
262             vring->broken = true;
263             return -EFAULT;
264         }
265         copy_in_vring_desc(vdev, desc_ptr, &desc);
266         memory_region_unref(mr);
267
268         /* Ensure descriptor has been loaded before accessing fields */
269         barrier(); /* read_barrier_depends(); */
270
271         if (unlikely(++found > count)) {
272             error_report("Loop detected: last one at %u "
273                          "indirect size %u", i, count);
274             vring->broken = true;
275             return -EFAULT;
276         }
277
278         if (unlikely(virtio_tswap16(vdev, desc.flags)
279                      & VRING_DESC_F_INDIRECT)) {
280             error_report("Nested indirect descriptor");
281             vring->broken = true;
282             return -EFAULT;
283         }
284
285         ret = get_desc(vdev, vring, elem, &desc);
286         if (ret < 0) {
287             vring->broken |= (ret == -EFAULT);
288             return ret;
289         }
290         i = virtio_tswap16(vdev, desc.next);
291     } while (virtio_tswap16(vdev, desc.flags) & VRING_DESC_F_NEXT);
292     return 0;
293 }
294
295 static void vring_unmap_element(VirtQueueElement *elem)
296 {
297     int i;
298
299     /* This assumes that the iovecs, if changed, are never moved past
300      * the end of the valid area.  This is true if iovec manipulations
301      * are done with iov_discard_front and iov_discard_back.
302      */
303     for (i = 0; i < elem->out_num; i++) {
304         vring_unmap(elem->out_sg[i].iov_base, false);
305     }
306
307     for (i = 0; i < elem->in_num; i++) {
308         vring_unmap(elem->in_sg[i].iov_base, true);
309     }
310 }
311
312 /* This looks in the virtqueue and for the first available buffer, and converts
313  * it to an iovec for convenient access.  Since descriptors consist of some
314  * number of output then some number of input descriptors, it's actually two
315  * iovecs, but we pack them into one and note how many of each there were.
316  *
317  * This function returns the descriptor number found, or vq->num (which is
318  * never a valid descriptor number) if none was found.  A negative code is
319  * returned on error.
320  *
321  * Stolen from linux/drivers/vhost/vhost.c.
322  */
323 int vring_pop(VirtIODevice *vdev, Vring *vring,
324               VirtQueueElement *elem)
325 {
326     struct vring_desc desc;
327     unsigned int i, head, found = 0, num = vring->vr.num;
328     uint16_t avail_idx, last_avail_idx;
329     int ret;
330
331     /* Initialize elem so it can be safely unmapped */
332     elem->in_num = elem->out_num = 0;
333
334     /* If there was a fatal error then refuse operation */
335     if (vring->broken) {
336         ret = -EFAULT;
337         goto out;
338     }
339
340     /* Check it isn't doing very strange things with descriptor numbers. */
341     last_avail_idx = vring->last_avail_idx;
342     avail_idx = vring_get_avail_idx(vdev, vring);
343     barrier(); /* load indices now and not again later */
344
345     if (unlikely((uint16_t)(avail_idx - last_avail_idx) > num)) {
346         error_report("Guest moved used index from %u to %u",
347                      last_avail_idx, avail_idx);
348         ret = -EFAULT;
349         goto out;
350     }
351
352     /* If there's nothing new since last we looked. */
353     if (avail_idx == last_avail_idx) {
354         ret = -EAGAIN;
355         goto out;
356     }
357
358     /* Only get avail ring entries after they have been exposed by guest. */
359     smp_rmb();
360
361     /* Grab the next descriptor number they're advertising, and increment
362      * the index we've seen. */
363     head = vring_get_avail_ring(vdev, vring, last_avail_idx % num);
364
365     elem->index = head;
366
367     /* If their number is silly, that's an error. */
368     if (unlikely(head >= num)) {
369         error_report("Guest says index %u > %u is available", head, num);
370         ret = -EFAULT;
371         goto out;
372     }
373
374     i = head;
375     do {
376         if (unlikely(i >= num)) {
377             error_report("Desc index is %u > %u, head = %u", i, num, head);
378             ret = -EFAULT;
379             goto out;
380         }
381         if (unlikely(++found > num)) {
382             error_report("Loop detected: last one at %u vq size %u head %u",
383                          i, num, head);
384             ret = -EFAULT;
385             goto out;
386         }
387         copy_in_vring_desc(vdev, &vring->vr.desc[i], &desc);
388
389         /* Ensure descriptor is loaded before accessing fields */
390         barrier();
391
392         if (virtio_tswap16(vdev, desc.flags) & VRING_DESC_F_INDIRECT) {
393             ret = get_indirect(vdev, vring, elem, &desc);
394             if (ret < 0) {
395                 goto out;
396             }
397             continue;
398         }
399
400         ret = get_desc(vdev, vring, elem, &desc);
401         if (ret < 0) {
402             goto out;
403         }
404
405         i = virtio_tswap16(vdev, desc.next);
406     } while (virtio_tswap16(vdev, desc.flags) & VRING_DESC_F_NEXT);
407
408     /* On success, increment avail index. */
409     vring->last_avail_idx++;
410     if (virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
411         vring_avail_event(&vring->vr) =
412             virtio_tswap16(vdev, vring->last_avail_idx);
413     }
414
415     return head;
416
417 out:
418     assert(ret < 0);
419     if (ret == -EFAULT) {
420         vring->broken = true;
421     }
422     vring_unmap_element(elem);
423     return ret;
424 }
425
426 /* After we've used one of their buffers, we tell them about it.
427  *
428  * Stolen from linux/drivers/vhost/vhost.c.
429  */
430 void vring_push(VirtIODevice *vdev, Vring *vring, VirtQueueElement *elem,
431                 int len)
432 {
433     unsigned int head = elem->index;
434     uint16_t new;
435
436     vring_unmap_element(elem);
437
438     /* Don't touch vring if a fatal error occurred */
439     if (vring->broken) {
440         return;
441     }
442
443     /* The virtqueue contains a ring of used buffers.  Get a pointer to the
444      * next entry in that used ring. */
445     vring_set_used_ring_id(vdev, vring, vring->last_used_idx % vring->vr.num,
446                            head);
447     vring_set_used_ring_len(vdev, vring, vring->last_used_idx % vring->vr.num,
448                             len);
449
450     /* Make sure buffer is written before we update index. */
451     smp_wmb();
452
453     new = ++vring->last_used_idx;
454     vring_set_used_idx(vdev, vring, new);
455     if (unlikely((int16_t)(new - vring->signalled_used) < (uint16_t)1)) {
456         vring->signalled_used_valid = false;
457     }
458 }
This page took 0.04965 seconds and 4 git commands to generate.