xref: /kvmtool/virtio/core.c (revision 52f34d2c8bc1c54046a308830799f4ac3f58c81e)
139d6af07SAsias He #include <linux/virtio_ring.h>
23fdf659dSSasha Levin #include <linux/types.h>
339d6af07SAsias He #include <sys/uio.h>
402eca50cSAsias He #include <stdlib.h>
52caa836dSIngo Molnar 
6*52f34d2cSAsias He #include "kvm/guest_compat.h"
72caa836dSIngo Molnar #include "kvm/barrier.h"
839d6af07SAsias He #include "kvm/virtio.h"
902eca50cSAsias He #include "kvm/virtio-pci.h"
10755752d6SAsias He #include "kvm/virtio-mmio.h"
1102eca50cSAsias He #include "kvm/util.h"
1202eca50cSAsias He #include "kvm/kvm.h"
1302eca50cSAsias He 
1439d6af07SAsias He 
153fdf659dSSasha Levin struct vring_used_elem *virt_queue__set_used_elem(struct virt_queue *queue, u32 head, u32 len)
1639d6af07SAsias He {
1739d6af07SAsias He 	struct vring_used_elem *used_elem;
18407475bfSPekka Enberg 
1994902782SSasha Levin 	used_elem	= &queue->vring.used->ring[queue->vring.used->idx % queue->vring.num];
2039d6af07SAsias He 	used_elem->id	= head;
2139d6af07SAsias He 	used_elem->len	= len;
2294902782SSasha Levin 
2394902782SSasha Levin 	/*
2494902782SSasha Levin 	 * Use wmb to assure that used elem was updated with head and len.
2594902782SSasha Levin 	 * We need a wmb here since we can't advance idx unless we're ready
2694902782SSasha Levin 	 * to pass the used element to the guest.
2794902782SSasha Levin 	 */
2894902782SSasha Levin 	wmb();
2994902782SSasha Levin 	queue->vring.used->idx++;
3094902782SSasha Levin 
3194902782SSasha Levin 	/*
3294902782SSasha Levin 	 * Use wmb to assure used idx has been increased before we signal the guest.
3394902782SSasha Levin 	 * Without a wmb here the guest may ignore the queue since it won't see
3494902782SSasha Levin 	 * an updated idx.
3594902782SSasha Levin 	 */
3694902782SSasha Levin 	wmb();
3794902782SSasha Levin 
3839d6af07SAsias He 	return used_elem;
3939d6af07SAsias He }
4039d6af07SAsias He 
41754c8ce3SSasha Levin /*
42754c8ce3SSasha Levin  * Each buffer in the virtqueues is actually a chain of descriptors.  This
43754c8ce3SSasha Levin  * function returns the next descriptor in the chain, or vq->vring.num if we're
44754c8ce3SSasha Levin  * at the end.
45754c8ce3SSasha Levin  */
46754c8ce3SSasha Levin static unsigned next_desc(struct vring_desc *desc,
47754c8ce3SSasha Levin 			  unsigned int i, unsigned int max)
48754c8ce3SSasha Levin {
49754c8ce3SSasha Levin 	unsigned int next;
50754c8ce3SSasha Levin 
51754c8ce3SSasha Levin 	/* If this descriptor says it doesn't chain, we're done. */
52754c8ce3SSasha Levin 	if (!(desc[i].flags & VRING_DESC_F_NEXT))
53754c8ce3SSasha Levin 		return max;
54754c8ce3SSasha Levin 
55754c8ce3SSasha Levin 	/* Check they're not leading us off end of descriptors. */
56754c8ce3SSasha Levin 	next = desc[i].next;
57754c8ce3SSasha Levin 	/* Make sure compiler knows to grab that: we don't want it changing! */
58754c8ce3SSasha Levin 	wmb();
59754c8ce3SSasha Levin 
60754c8ce3SSasha Levin 	return next;
61754c8ce3SSasha Levin }
62754c8ce3SSasha Levin 
632fddfdb5SAsias He u16 virt_queue__get_head_iov(struct virt_queue *vq, struct iovec iov[], u16 *out, u16 *in, u16 head, struct kvm *kvm)
6439d6af07SAsias He {
6539d6af07SAsias He 	struct vring_desc *desc;
662fddfdb5SAsias He 	u16 idx;
67754c8ce3SSasha Levin 	u16 max;
6839d6af07SAsias He 
692fddfdb5SAsias He 	idx = head;
7039d6af07SAsias He 	*out = *in = 0;
71754c8ce3SSasha Levin 	max = vq->vring.num;
72754c8ce3SSasha Levin 	desc = vq->vring.desc;
73754c8ce3SSasha Levin 
74754c8ce3SSasha Levin 	if (desc[idx].flags & VRING_DESC_F_INDIRECT) {
75754c8ce3SSasha Levin 		max = desc[idx].len / sizeof(struct vring_desc);
76754c8ce3SSasha Levin 		desc = guest_flat_to_host(kvm, desc[idx].addr);
77754c8ce3SSasha Levin 		idx = 0;
78754c8ce3SSasha Levin 	}
7939d6af07SAsias He 
8039d6af07SAsias He 	do {
81754c8ce3SSasha Levin 		/* Grab the first descriptor, and check it's OK. */
82754c8ce3SSasha Levin 		iov[*out + *in].iov_len = desc[idx].len;
83754c8ce3SSasha Levin 		iov[*out + *in].iov_base = guest_flat_to_host(kvm, desc[idx].addr);
84754c8ce3SSasha Levin 		/* If this is an input descriptor, increment that count. */
85754c8ce3SSasha Levin 		if (desc[idx].flags & VRING_DESC_F_WRITE)
8639d6af07SAsias He 			(*in)++;
8739d6af07SAsias He 		else
8839d6af07SAsias He 			(*out)++;
89754c8ce3SSasha Levin 	} while ((idx = next_desc(desc, idx, max)) != max);
9039d6af07SAsias He 
9139d6af07SAsias He 	return head;
9239d6af07SAsias He }
937f5ffaf5SAsias He 
942fddfdb5SAsias He u16 virt_queue__get_iov(struct virt_queue *vq, struct iovec iov[], u16 *out, u16 *in, struct kvm *kvm)
952fddfdb5SAsias He {
962fddfdb5SAsias He 	u16 head;
972fddfdb5SAsias He 
982fddfdb5SAsias He 	head = virt_queue__pop(vq);
992fddfdb5SAsias He 
1002fddfdb5SAsias He 	return virt_queue__get_head_iov(vq, iov, out, in, head, kvm);
1012fddfdb5SAsias He }
1022fddfdb5SAsias He 
10308861bcfSAneesh Kumar K.V /* in and out are relative to guest */
10408861bcfSAneesh Kumar K.V u16 virt_queue__get_inout_iov(struct kvm *kvm, struct virt_queue *queue,
10508861bcfSAneesh Kumar K.V 			      struct iovec in_iov[], struct iovec out_iov[],
10608861bcfSAneesh Kumar K.V 			      u16 *in, u16 *out)
10708861bcfSAneesh Kumar K.V {
10808861bcfSAneesh Kumar K.V 	struct vring_desc *desc;
1092fddfdb5SAsias He 	u16 head, idx;
11008861bcfSAneesh Kumar K.V 
11108861bcfSAneesh Kumar K.V 	idx = head = virt_queue__pop(queue);
11208861bcfSAneesh Kumar K.V 	*out = *in = 0;
11308861bcfSAneesh Kumar K.V 	do {
11408861bcfSAneesh Kumar K.V 		desc = virt_queue__get_desc(queue, idx);
11508861bcfSAneesh Kumar K.V 		if (desc->flags & VRING_DESC_F_WRITE) {
11608861bcfSAneesh Kumar K.V 			in_iov[*in].iov_base = guest_flat_to_host(kvm,
11708861bcfSAneesh Kumar K.V 								  desc->addr);
11808861bcfSAneesh Kumar K.V 			in_iov[*in].iov_len = desc->len;
11908861bcfSAneesh Kumar K.V 			(*in)++;
12008861bcfSAneesh Kumar K.V 		} else {
12108861bcfSAneesh Kumar K.V 			out_iov[*out].iov_base = guest_flat_to_host(kvm,
12208861bcfSAneesh Kumar K.V 								    desc->addr);
12308861bcfSAneesh Kumar K.V 			out_iov[*out].iov_len = desc->len;
12408861bcfSAneesh Kumar K.V 			(*out)++;
12508861bcfSAneesh Kumar K.V 		}
12608861bcfSAneesh Kumar K.V 		if (desc->flags & VRING_DESC_F_NEXT)
12708861bcfSAneesh Kumar K.V 			idx = desc->next;
12808861bcfSAneesh Kumar K.V 		else
12908861bcfSAneesh Kumar K.V 			break;
13008861bcfSAneesh Kumar K.V 	} while (1);
1312fddfdb5SAsias He 
13208861bcfSAneesh Kumar K.V 	return head;
13308861bcfSAneesh Kumar K.V }
13408861bcfSAneesh Kumar K.V 
1351382aba0SSasha Levin int virtio__get_dev_specific_field(int offset, bool msix, u32 *config_off)
136c3a79fa1SSasha Levin {
137c3a79fa1SSasha Levin 	if (msix) {
138c3a79fa1SSasha Levin 		if (offset < 4)
139c3a79fa1SSasha Levin 			return VIRTIO_PCI_O_MSIX;
140c3a79fa1SSasha Levin 		else
141c3a79fa1SSasha Levin 			offset -= 4;
142c3a79fa1SSasha Levin 	}
143c3a79fa1SSasha Levin 
144c3a79fa1SSasha Levin 	*config_off = offset;
145c3a79fa1SSasha Levin 
146c3a79fa1SSasha Levin 	return VIRTIO_PCI_O_CONFIG;
147c3a79fa1SSasha Levin }
14851b1454fSAsias He 
14951b1454fSAsias He bool virtio_queue__should_signal(struct virt_queue *vq)
15051b1454fSAsias He {
15151b1454fSAsias He 	u16 old_idx, new_idx, event_idx;
15251b1454fSAsias He 
15351b1454fSAsias He 	old_idx		= vq->last_used_signalled;
15451b1454fSAsias He 	new_idx		= vq->vring.used->idx;
15551b1454fSAsias He 	event_idx	= vring_used_event(&vq->vring);
15651b1454fSAsias He 
15751b1454fSAsias He 	if (vring_need_event(event_idx, new_idx, old_idx)) {
15851b1454fSAsias He 		vq->last_used_signalled = new_idx;
15951b1454fSAsias He 		return true;
16051b1454fSAsias He 	}
16151b1454fSAsias He 
16251b1454fSAsias He 	return false;
16351b1454fSAsias He }
16402eca50cSAsias He 
16502eca50cSAsias He int virtio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
16602eca50cSAsias He 		struct virtio_ops *ops, enum virtio_trans trans,
16702eca50cSAsias He 		int device_id, int subsys_id, int class)
16802eca50cSAsias He {
16902eca50cSAsias He 	void *virtio;
17002eca50cSAsias He 
17102eca50cSAsias He 	switch (trans) {
17202eca50cSAsias He 	case VIRTIO_PCI:
17302eca50cSAsias He 		virtio = calloc(sizeof(struct virtio_pci), 1);
17402eca50cSAsias He 		if (!virtio)
17502eca50cSAsias He 			return -ENOMEM;
17602eca50cSAsias He 		vdev->virtio			= virtio;
17702eca50cSAsias He 		vdev->ops			= ops;
17802eca50cSAsias He 		vdev->ops->signal_vq		= virtio_pci__signal_vq;
17902eca50cSAsias He 		vdev->ops->signal_config	= virtio_pci__signal_config;
18002eca50cSAsias He 		vdev->ops->init			= virtio_pci__init;
18102eca50cSAsias He 		vdev->ops->exit			= virtio_pci__exit;
18202eca50cSAsias He 		vdev->ops->init(kvm, dev, vdev, device_id, subsys_id, class);
18302eca50cSAsias He 		break;
184755752d6SAsias He 	case VIRTIO_MMIO:
185755752d6SAsias He 		virtio = calloc(sizeof(struct virtio_mmio), 1);
186755752d6SAsias He 		if (!virtio)
187755752d6SAsias He 			return -ENOMEM;
188755752d6SAsias He 		vdev->virtio			= virtio;
189755752d6SAsias He 		vdev->ops			= ops;
190755752d6SAsias He 		vdev->ops->signal_vq		= virtio_mmio_signal_vq;
191755752d6SAsias He 		vdev->ops->signal_config	= virtio_mmio_signal_config;
192755752d6SAsias He 		vdev->ops->init			= virtio_mmio_init;
193755752d6SAsias He 		vdev->ops->exit			= virtio_mmio_exit;
194755752d6SAsias He 		vdev->ops->init(kvm, dev, vdev, device_id, subsys_id, class);
195755752d6SAsias He 		break;
19602eca50cSAsias He 	default:
19702eca50cSAsias He 		return -1;
19802eca50cSAsias He 	};
19902eca50cSAsias He 
20002eca50cSAsias He 	return 0;
20102eca50cSAsias He }
202*52f34d2cSAsias He 
203*52f34d2cSAsias He int virtio_compat_add_message(const char *device, const char *config)
204*52f34d2cSAsias He {
205*52f34d2cSAsias He 	int len = 1024;
206*52f34d2cSAsias He 	int compat_id;
207*52f34d2cSAsias He 	char *title;
208*52f34d2cSAsias He 	char *desc;
209*52f34d2cSAsias He 
210*52f34d2cSAsias He 	title = malloc(len);
211*52f34d2cSAsias He 	if (!title)
212*52f34d2cSAsias He 		return -ENOMEM;
213*52f34d2cSAsias He 
214*52f34d2cSAsias He 	desc = malloc(len);
215*52f34d2cSAsias He 	if (!desc) {
216*52f34d2cSAsias He 		free(title);
217*52f34d2cSAsias He 		return -ENOMEM;
218*52f34d2cSAsias He 	}
219*52f34d2cSAsias He 
220*52f34d2cSAsias He 	snprintf(title, len, "%s device was not detected", device);
221*52f34d2cSAsias He 	snprintf(desc,  len, "While you have requested a %s device, "
222*52f34d2cSAsias He 			     "the guest kernel did not initialize it.\n"
223*52f34d2cSAsias He 			     "Please make sure that the guest kernel was "
224*52f34d2cSAsias He 			     "compiled with %s=y enabled in its .config",
225*52f34d2cSAsias He 			     device, config);
226*52f34d2cSAsias He 
227*52f34d2cSAsias He 	compat_id = compat__add_message(title, desc);
228*52f34d2cSAsias He 
229*52f34d2cSAsias He 	free(desc);
230*52f34d2cSAsias He 	free(title);
231*52f34d2cSAsias He 
232*52f34d2cSAsias He 	return compat_id;
233*52f34d2cSAsias He }
234