virtio-9p: Avoid SEGV when log file couldn't be opened
[qemu/mdroth.git] / hw / virtio.c
blob4475bb3e4488761206912515a451d54305f579ad
1 /*
2 * Virtio Support
4 * Copyright IBM, Corp. 2007
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include <inttypes.h>
16 #include "virtio.h"
17 #include "sysemu.h"
19 /* The alignment to use between consumer and producer parts of vring.
20 * x86 pagesize again. */
21 #define VIRTIO_PCI_VRING_ALIGN 4096
23 /* QEMU doesn't strictly need write barriers since everything runs in
24 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
25 * KVM or if kqemu gets SMP support.
26 * In any case, we must prevent the compiler from reordering the code.
27 * TODO: we likely need some rmb()/mb() as well.
30 #define wmb() __asm__ __volatile__("": : :"memory")
32 typedef struct VRingDesc
34 uint64_t addr;
35 uint32_t len;
36 uint16_t flags;
37 uint16_t next;
38 } VRingDesc;
40 typedef struct VRingAvail
42 uint16_t flags;
43 uint16_t idx;
44 uint16_t ring[0];
45 } VRingAvail;
47 typedef struct VRingUsedElem
49 uint32_t id;
50 uint32_t len;
51 } VRingUsedElem;
53 typedef struct VRingUsed
55 uint16_t flags;
56 uint16_t idx;
57 VRingUsedElem ring[0];
58 } VRingUsed;
60 typedef struct VRing
62 unsigned int num;
63 target_phys_addr_t desc;
64 target_phys_addr_t avail;
65 target_phys_addr_t used;
66 } VRing;
68 struct VirtQueue
70 VRing vring;
71 target_phys_addr_t pa;
72 uint16_t last_avail_idx;
73 int inuse;
74 uint16_t vector;
75 void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
76 VirtIODevice *vdev;
77 EventNotifier guest_notifier;
78 EventNotifier host_notifier;
81 /* virt queue functions */
82 static void virtqueue_init(VirtQueue *vq)
84 target_phys_addr_t pa = vq->pa;
86 vq->vring.desc = pa;
87 vq->vring.avail = pa + vq->vring.num * sizeof(VRingDesc);
88 vq->vring.used = vring_align(vq->vring.avail +
89 offsetof(VRingAvail, ring[vq->vring.num]),
90 VIRTIO_PCI_VRING_ALIGN);
93 static inline uint64_t vring_desc_addr(target_phys_addr_t desc_pa, int i)
95 target_phys_addr_t pa;
96 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, addr);
97 return ldq_phys(pa);
100 static inline uint32_t vring_desc_len(target_phys_addr_t desc_pa, int i)
102 target_phys_addr_t pa;
103 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, len);
104 return ldl_phys(pa);
107 static inline uint16_t vring_desc_flags(target_phys_addr_t desc_pa, int i)
109 target_phys_addr_t pa;
110 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, flags);
111 return lduw_phys(pa);
114 static inline uint16_t vring_desc_next(target_phys_addr_t desc_pa, int i)
116 target_phys_addr_t pa;
117 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, next);
118 return lduw_phys(pa);
121 static inline uint16_t vring_avail_flags(VirtQueue *vq)
123 target_phys_addr_t pa;
124 pa = vq->vring.avail + offsetof(VRingAvail, flags);
125 return lduw_phys(pa);
128 static inline uint16_t vring_avail_idx(VirtQueue *vq)
130 target_phys_addr_t pa;
131 pa = vq->vring.avail + offsetof(VRingAvail, idx);
132 return lduw_phys(pa);
135 static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
137 target_phys_addr_t pa;
138 pa = vq->vring.avail + offsetof(VRingAvail, ring[i]);
139 return lduw_phys(pa);
142 static inline void vring_used_ring_id(VirtQueue *vq, int i, uint32_t val)
144 target_phys_addr_t pa;
145 pa = vq->vring.used + offsetof(VRingUsed, ring[i].id);
146 stl_phys(pa, val);
149 static inline void vring_used_ring_len(VirtQueue *vq, int i, uint32_t val)
151 target_phys_addr_t pa;
152 pa = vq->vring.used + offsetof(VRingUsed, ring[i].len);
153 stl_phys(pa, val);
156 static uint16_t vring_used_idx(VirtQueue *vq)
158 target_phys_addr_t pa;
159 pa = vq->vring.used + offsetof(VRingUsed, idx);
160 return lduw_phys(pa);
163 static inline void vring_used_idx_increment(VirtQueue *vq, uint16_t val)
165 target_phys_addr_t pa;
166 pa = vq->vring.used + offsetof(VRingUsed, idx);
167 stw_phys(pa, vring_used_idx(vq) + val);
170 static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
172 target_phys_addr_t pa;
173 pa = vq->vring.used + offsetof(VRingUsed, flags);
174 stw_phys(pa, lduw_phys(pa) | mask);
177 static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
179 target_phys_addr_t pa;
180 pa = vq->vring.used + offsetof(VRingUsed, flags);
181 stw_phys(pa, lduw_phys(pa) & ~mask);
184 void virtio_queue_set_notification(VirtQueue *vq, int enable)
186 if (enable)
187 vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
188 else
189 vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
192 int virtio_queue_ready(VirtQueue *vq)
194 return vq->vring.avail != 0;
197 int virtio_queue_empty(VirtQueue *vq)
199 return vring_avail_idx(vq) == vq->last_avail_idx;
202 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
203 unsigned int len, unsigned int idx)
205 unsigned int offset;
206 int i;
208 offset = 0;
209 for (i = 0; i < elem->in_num; i++) {
210 size_t size = MIN(len - offset, elem->in_sg[i].iov_len);
212 cpu_physical_memory_unmap(elem->in_sg[i].iov_base,
213 elem->in_sg[i].iov_len,
214 1, size);
216 offset += elem->in_sg[i].iov_len;
219 for (i = 0; i < elem->out_num; i++)
220 cpu_physical_memory_unmap(elem->out_sg[i].iov_base,
221 elem->out_sg[i].iov_len,
222 0, elem->out_sg[i].iov_len);
224 idx = (idx + vring_used_idx(vq)) % vq->vring.num;
226 /* Get a pointer to the next entry in the used ring. */
227 vring_used_ring_id(vq, idx, elem->index);
228 vring_used_ring_len(vq, idx, len);
231 void virtqueue_flush(VirtQueue *vq, unsigned int count)
233 /* Make sure buffer is written before we update index. */
234 wmb();
235 vring_used_idx_increment(vq, count);
236 vq->inuse -= count;
239 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
240 unsigned int len)
242 virtqueue_fill(vq, elem, len, 0);
243 virtqueue_flush(vq, 1);
246 static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
248 uint16_t num_heads = vring_avail_idx(vq) - idx;
250 /* Check it isn't doing very strange things with descriptor numbers. */
251 if (num_heads > vq->vring.num) {
252 fprintf(stderr, "Guest moved used index from %u to %u",
253 idx, vring_avail_idx(vq));
254 exit(1);
257 return num_heads;
260 static unsigned int virtqueue_get_head(VirtQueue *vq, unsigned int idx)
262 unsigned int head;
264 /* Grab the next descriptor number they're advertising, and increment
265 * the index we've seen. */
266 head = vring_avail_ring(vq, idx % vq->vring.num);
268 /* If their number is silly, that's a fatal mistake. */
269 if (head >= vq->vring.num) {
270 fprintf(stderr, "Guest says index %u is available", head);
271 exit(1);
274 return head;
277 static unsigned virtqueue_next_desc(target_phys_addr_t desc_pa,
278 unsigned int i, unsigned int max)
280 unsigned int next;
282 /* If this descriptor says it doesn't chain, we're done. */
283 if (!(vring_desc_flags(desc_pa, i) & VRING_DESC_F_NEXT))
284 return max;
286 /* Check they're not leading us off end of descriptors. */
287 next = vring_desc_next(desc_pa, i);
288 /* Make sure compiler knows to grab that: we don't want it changing! */
289 wmb();
291 if (next >= max) {
292 fprintf(stderr, "Desc next is %u", next);
293 exit(1);
296 return next;
299 int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes)
301 unsigned int idx;
302 int total_bufs, in_total, out_total;
304 idx = vq->last_avail_idx;
306 total_bufs = in_total = out_total = 0;
307 while (virtqueue_num_heads(vq, idx)) {
308 unsigned int max, num_bufs, indirect = 0;
309 target_phys_addr_t desc_pa;
310 int i;
312 max = vq->vring.num;
313 num_bufs = total_bufs;
314 i = virtqueue_get_head(vq, idx++);
315 desc_pa = vq->vring.desc;
317 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) {
318 if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) {
319 fprintf(stderr, "Invalid size for indirect buffer table\n");
320 exit(1);
323 /* If we've got too many, that implies a descriptor loop. */
324 if (num_bufs >= max) {
325 fprintf(stderr, "Looped descriptor");
326 exit(1);
329 /* loop over the indirect descriptor table */
330 indirect = 1;
331 max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc);
332 num_bufs = i = 0;
333 desc_pa = vring_desc_addr(desc_pa, i);
336 do {
337 /* If we've got too many, that implies a descriptor loop. */
338 if (++num_bufs > max) {
339 fprintf(stderr, "Looped descriptor");
340 exit(1);
343 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) {
344 if (in_bytes > 0 &&
345 (in_total += vring_desc_len(desc_pa, i)) >= in_bytes)
346 return 1;
347 } else {
348 if (out_bytes > 0 &&
349 (out_total += vring_desc_len(desc_pa, i)) >= out_bytes)
350 return 1;
352 } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max);
354 if (!indirect)
355 total_bufs = num_bufs;
356 else
357 total_bufs++;
360 return 0;
363 int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem)
365 unsigned int i, head, max;
366 target_phys_addr_t desc_pa = vq->vring.desc;
367 target_phys_addr_t len;
369 if (!virtqueue_num_heads(vq, vq->last_avail_idx))
370 return 0;
372 /* When we start there are none of either input nor output. */
373 elem->out_num = elem->in_num = 0;
375 max = vq->vring.num;
377 i = head = virtqueue_get_head(vq, vq->last_avail_idx++);
379 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) {
380 if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) {
381 fprintf(stderr, "Invalid size for indirect buffer table\n");
382 exit(1);
385 /* loop over the indirect descriptor table */
386 max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc);
387 desc_pa = vring_desc_addr(desc_pa, i);
388 i = 0;
391 do {
392 struct iovec *sg;
393 int is_write = 0;
395 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) {
396 elem->in_addr[elem->in_num] = vring_desc_addr(desc_pa, i);
397 sg = &elem->in_sg[elem->in_num++];
398 is_write = 1;
399 } else
400 sg = &elem->out_sg[elem->out_num++];
402 /* Grab the first descriptor, and check it's OK. */
403 sg->iov_len = vring_desc_len(desc_pa, i);
404 len = sg->iov_len;
406 sg->iov_base = cpu_physical_memory_map(vring_desc_addr(desc_pa, i),
407 &len, is_write);
409 if (sg->iov_base == NULL || len != sg->iov_len) {
410 fprintf(stderr, "virtio: trying to map MMIO memory\n");
411 exit(1);
414 /* If we've got too many, that implies a descriptor loop. */
415 if ((elem->in_num + elem->out_num) > max) {
416 fprintf(stderr, "Looped descriptor");
417 exit(1);
419 } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max);
421 elem->index = head;
423 vq->inuse++;
425 return elem->in_num + elem->out_num;
428 /* virtio device */
429 static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector)
431 if (vdev->binding->notify) {
432 vdev->binding->notify(vdev->binding_opaque, vector);
436 void virtio_update_irq(VirtIODevice *vdev)
438 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
441 void virtio_reset(void *opaque)
443 VirtIODevice *vdev = opaque;
444 int i;
446 if (vdev->reset)
447 vdev->reset(vdev);
449 vdev->guest_features = 0;
450 vdev->queue_sel = 0;
451 vdev->status = 0;
452 vdev->isr = 0;
453 vdev->config_vector = VIRTIO_NO_VECTOR;
454 virtio_notify_vector(vdev, vdev->config_vector);
456 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
457 vdev->vq[i].vring.desc = 0;
458 vdev->vq[i].vring.avail = 0;
459 vdev->vq[i].vring.used = 0;
460 vdev->vq[i].last_avail_idx = 0;
461 vdev->vq[i].pa = 0;
462 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
466 uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
468 uint8_t val;
470 vdev->get_config(vdev, vdev->config);
472 if (addr > (vdev->config_len - sizeof(val)))
473 return (uint32_t)-1;
475 memcpy(&val, vdev->config + addr, sizeof(val));
476 return val;
479 uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
481 uint16_t val;
483 vdev->get_config(vdev, vdev->config);
485 if (addr > (vdev->config_len - sizeof(val)))
486 return (uint32_t)-1;
488 memcpy(&val, vdev->config + addr, sizeof(val));
489 return val;
492 uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
494 uint32_t val;
496 vdev->get_config(vdev, vdev->config);
498 if (addr > (vdev->config_len - sizeof(val)))
499 return (uint32_t)-1;
501 memcpy(&val, vdev->config + addr, sizeof(val));
502 return val;
505 void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
507 uint8_t val = data;
509 if (addr > (vdev->config_len - sizeof(val)))
510 return;
512 memcpy(vdev->config + addr, &val, sizeof(val));
514 if (vdev->set_config)
515 vdev->set_config(vdev, vdev->config);
518 void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
520 uint16_t val = data;
522 if (addr > (vdev->config_len - sizeof(val)))
523 return;
525 memcpy(vdev->config + addr, &val, sizeof(val));
527 if (vdev->set_config)
528 vdev->set_config(vdev, vdev->config);
531 void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
533 uint32_t val = data;
535 if (addr > (vdev->config_len - sizeof(val)))
536 return;
538 memcpy(vdev->config + addr, &val, sizeof(val));
540 if (vdev->set_config)
541 vdev->set_config(vdev, vdev->config);
544 void virtio_queue_set_addr(VirtIODevice *vdev, int n, target_phys_addr_t addr)
546 vdev->vq[n].pa = addr;
547 virtqueue_init(&vdev->vq[n]);
550 target_phys_addr_t virtio_queue_get_addr(VirtIODevice *vdev, int n)
552 return vdev->vq[n].pa;
555 int virtio_queue_get_num(VirtIODevice *vdev, int n)
557 return vdev->vq[n].vring.num;
560 void virtio_queue_notify(VirtIODevice *vdev, int n)
562 if (n < VIRTIO_PCI_QUEUE_MAX && vdev->vq[n].vring.desc) {
563 vdev->vq[n].handle_output(vdev, &vdev->vq[n]);
567 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
569 return n < VIRTIO_PCI_QUEUE_MAX ? vdev->vq[n].vector :
570 VIRTIO_NO_VECTOR;
573 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector)
575 if (n < VIRTIO_PCI_QUEUE_MAX)
576 vdev->vq[n].vector = vector;
579 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
580 void (*handle_output)(VirtIODevice *, VirtQueue *))
582 int i;
584 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
585 if (vdev->vq[i].vring.num == 0)
586 break;
589 if (i == VIRTIO_PCI_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
590 abort();
592 vdev->vq[i].vring.num = queue_size;
593 vdev->vq[i].handle_output = handle_output;
595 return &vdev->vq[i];
598 void virtio_irq(VirtQueue *vq)
600 vq->vdev->isr |= 0x01;
601 virtio_notify_vector(vq->vdev, vq->vector);
604 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
606 /* Always notify when queue is empty (when feature acknowledge) */
607 if ((vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT) &&
608 (!(vdev->guest_features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) ||
609 (vq->inuse || vring_avail_idx(vq) != vq->last_avail_idx)))
610 return;
612 vdev->isr |= 0x01;
613 virtio_notify_vector(vdev, vq->vector);
616 void virtio_notify_config(VirtIODevice *vdev)
618 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
619 return;
621 vdev->isr |= 0x03;
622 virtio_notify_vector(vdev, vdev->config_vector);
625 void virtio_save(VirtIODevice *vdev, QEMUFile *f)
627 int i;
629 if (vdev->binding->save_config)
630 vdev->binding->save_config(vdev->binding_opaque, f);
632 qemu_put_8s(f, &vdev->status);
633 qemu_put_8s(f, &vdev->isr);
634 qemu_put_be16s(f, &vdev->queue_sel);
635 qemu_put_be32s(f, &vdev->guest_features);
636 qemu_put_be32(f, vdev->config_len);
637 qemu_put_buffer(f, vdev->config, vdev->config_len);
639 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
640 if (vdev->vq[i].vring.num == 0)
641 break;
644 qemu_put_be32(f, i);
646 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
647 if (vdev->vq[i].vring.num == 0)
648 break;
650 qemu_put_be32(f, vdev->vq[i].vring.num);
651 qemu_put_be64(f, vdev->vq[i].pa);
652 qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
653 if (vdev->binding->save_queue)
654 vdev->binding->save_queue(vdev->binding_opaque, i, f);
658 int virtio_load(VirtIODevice *vdev, QEMUFile *f)
660 int num, i, ret;
661 uint32_t features;
662 uint32_t supported_features =
663 vdev->binding->get_features(vdev->binding_opaque);
665 if (vdev->binding->load_config) {
666 ret = vdev->binding->load_config(vdev->binding_opaque, f);
667 if (ret)
668 return ret;
671 qemu_get_8s(f, &vdev->status);
672 qemu_get_8s(f, &vdev->isr);
673 qemu_get_be16s(f, &vdev->queue_sel);
674 qemu_get_be32s(f, &features);
675 if (features & ~supported_features) {
676 fprintf(stderr, "Features 0x%x unsupported. Allowed features: 0x%x\n",
677 features, supported_features);
678 return -1;
680 if (vdev->set_features)
681 vdev->set_features(vdev, features);
682 vdev->guest_features = features;
683 vdev->config_len = qemu_get_be32(f);
684 qemu_get_buffer(f, vdev->config, vdev->config_len);
686 num = qemu_get_be32(f);
688 for (i = 0; i < num; i++) {
689 vdev->vq[i].vring.num = qemu_get_be32(f);
690 vdev->vq[i].pa = qemu_get_be64(f);
691 qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
693 if (vdev->vq[i].pa) {
694 virtqueue_init(&vdev->vq[i]);
696 if (vdev->binding->load_queue) {
697 ret = vdev->binding->load_queue(vdev->binding_opaque, i, f);
698 if (ret)
699 return ret;
703 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
704 return 0;
707 void virtio_cleanup(VirtIODevice *vdev)
709 if (vdev->config)
710 qemu_free(vdev->config);
711 qemu_free(vdev->vq);
714 VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
715 size_t config_size, size_t struct_size)
717 VirtIODevice *vdev;
718 int i;
720 vdev = qemu_mallocz(struct_size);
722 vdev->device_id = device_id;
723 vdev->status = 0;
724 vdev->isr = 0;
725 vdev->queue_sel = 0;
726 vdev->config_vector = VIRTIO_NO_VECTOR;
727 vdev->vq = qemu_mallocz(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX);
728 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
729 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
730 vdev->vq[i].vdev = vdev;
733 vdev->name = name;
734 vdev->config_len = config_size;
735 if (vdev->config_len)
736 vdev->config = qemu_mallocz(config_size);
737 else
738 vdev->config = NULL;
740 return vdev;
743 void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding,
744 void *opaque)
746 vdev->binding = binding;
747 vdev->binding_opaque = opaque;
750 target_phys_addr_t virtio_queue_get_desc_addr(VirtIODevice *vdev, int n)
752 return vdev->vq[n].vring.desc;
755 target_phys_addr_t virtio_queue_get_avail_addr(VirtIODevice *vdev, int n)
757 return vdev->vq[n].vring.avail;
760 target_phys_addr_t virtio_queue_get_used_addr(VirtIODevice *vdev, int n)
762 return vdev->vq[n].vring.used;
765 target_phys_addr_t virtio_queue_get_ring_addr(VirtIODevice *vdev, int n)
767 return vdev->vq[n].vring.desc;
770 target_phys_addr_t virtio_queue_get_desc_size(VirtIODevice *vdev, int n)
772 return sizeof(VRingDesc) * vdev->vq[n].vring.num;
775 target_phys_addr_t virtio_queue_get_avail_size(VirtIODevice *vdev, int n)
777 return offsetof(VRingAvail, ring) +
778 sizeof(uint64_t) * vdev->vq[n].vring.num;
781 target_phys_addr_t virtio_queue_get_used_size(VirtIODevice *vdev, int n)
783 return offsetof(VRingUsed, ring) +
784 sizeof(VRingUsedElem) * vdev->vq[n].vring.num;
787 target_phys_addr_t virtio_queue_get_ring_size(VirtIODevice *vdev, int n)
789 return vdev->vq[n].vring.used - vdev->vq[n].vring.desc +
790 virtio_queue_get_used_size(vdev, n);
793 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n)
795 return vdev->vq[n].last_avail_idx;
798 void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx)
800 vdev->vq[n].last_avail_idx = idx;
803 VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)
805 return vdev->vq + n;
808 EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
810 return &vq->guest_notifier;
812 EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
814 return &vq->host_notifier;