···191 return iov->iov_base;192}193000194/* The virtio configuration space is defined to be little-endian. x86 is195 * little-endian too, but it's nice to be explicit so we have these helpers. */196#define cpu_to_le16(v16) (v16)···693 unsigned int *out_num, unsigned int *in_num)694{695 unsigned int i, head;0696697 /* Check it isn't doing very strange things with descriptor numbers. */698- if ((u16)(vq->vring.avail->idx - vq->last_avail_idx) > vq->vring.num)0699 errx(1, "Guest moved used index from %u to %u",700- vq->last_avail_idx, vq->vring.avail->idx);701702 /* If there's nothing new since last we looked, return invalid. */703- if (vq->vring.avail->idx == vq->last_avail_idx)704 return vq->vring.num;705706 /* Grab the next descriptor number they're advertising, and increment707 * the index we've seen. */708- head = vq->vring.avail->ring[vq->last_avail_idx++ % vq->vring.num];0709710 /* If their number is silly, that's a fatal mistake. */711 if (head >= vq->vring.num)···986 for (vq = dev->vq; vq; vq = vq->next) {987 memset(vq->vring.desc, 0,988 vring_size(vq->config.num, getpagesize()));989- vq->last_avail_idx = 0;990 }991 } else if (dev->desc->status & VIRTIO_CONFIG_S_FAILED) {992 warnx("Device %s configuration FAILED", dev->name);
···191 return iov->iov_base;192}193194+/* Wrapper for the last available index. Makes it easier to change. */195+#define lg_last_avail(vq) ((vq)->last_avail_idx)196+197/* The virtio configuration space is defined to be little-endian. x86 is198 * little-endian too, but it's nice to be explicit so we have these helpers. */199#define cpu_to_le16(v16) (v16)···690 unsigned int *out_num, unsigned int *in_num)691{692 unsigned int i, head;693+ u16 last_avail;694695 /* Check it isn't doing very strange things with descriptor numbers. */696+ last_avail = lg_last_avail(vq);697+ if ((u16)(vq->vring.avail->idx - last_avail) > vq->vring.num)698 errx(1, "Guest moved used index from %u to %u",699+ last_avail, vq->vring.avail->idx);700701 /* If there's nothing new since last we looked, return invalid. */702+ if (vq->vring.avail->idx == last_avail)703 return vq->vring.num;704705 /* Grab the next descriptor number they're advertising, and increment706 * the index we've seen. */707+ head = vq->vring.avail->ring[last_avail % vq->vring.num];708+ lg_last_avail(vq)++;709710 /* If their number is silly, that's a fatal mistake. */711 if (head >= vq->vring.num)···980 for (vq = dev->vq; vq; vq = vq->next) {981 memset(vq->vring.desc, 0,982 vring_size(vq->config.num, getpagesize()));983+ lg_last_avail(vq) = 0;984 }985 } else if (dev->desc->status & VIRTIO_CONFIG_S_FAILED) {986 warnx("Device %s configuration FAILED", dev->name);