From nobody Tue Apr 15 19:52:16 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600877869; cv=none; d=zohomail.com; s=zohoarc; b=JhiySmrDJQv/Doh0MFV9e5cmTU2P41cf05MpGhpsGQMTLZSCvLIj4jwC+yx4h8xkIoGDJn4lurxk3FFwiHDyvH0fbJbAehyxzCIbCcpDAIrVwA+6vU1oD5uRZVPwLajg+PnyYzAVtn1fIAIrODjF1gntI2RBO1GoaYbhHCSumkc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600877869; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=JCJTKSr4m6Lo/ardMebkNQMF2hNJMygPKyEp7VBX7Ag=; b=jTVg9MWDJyTmGo0RPs1JEjYhVU7dDZHjfPc+h0AtymydgqWJljlqNX5UhnIiDZ58akjNv8lzaj3S1x2GlNRQMyfddCBStCTzgODaDBx9bE5jAuc5bN3J/7xKiICmAPl3n1APhzRJ8o1lMOhHc0huXt3Hwnqec0p+WWvk00le4Cg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1600877869422619.2520893778268; Wed, 23 Sep 2020 09:17:49 -0700 (PDT) Received: from localhost ([::1]:32828 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kL7Sl-0002LC-TJ for importer@patchew.org; Wed, 23 Sep 2020 12:17:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47082) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kL7MH-0003RJ-EK for qemu-devel@nongnu.org; Wed, 23 Sep 2020 12:11:05 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:50132) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kL7MF-0006DS-8M for qemu-devel@nongnu.org; Wed, 23 Sep 2020 12:11:05 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-236-slz-vFffOyakDavPMxWTBA-1; Wed, 23 Sep 2020 12:10:49 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 599271084C80; Wed, 23 Sep 2020 16:10:48 +0000 (UTC) Received: from localhost (ovpn-113-77.ams2.redhat.com [10.36.113.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id E1F2578827; Wed, 23 Sep 2020 16:10:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600877462; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JCJTKSr4m6Lo/ardMebkNQMF2hNJMygPKyEp7VBX7Ag=; b=cJMHlTtOyc8nZo2cC3l1JQhUxIUckIAdNNR3xLZyUlvr2JAUxmbDb5K2kl7y7/hfZ708t7 ZtQ3Yj43vAdGLz4bP7BmG1t9Q18MZ1rF+QEVIDoYwYqXRDkKlftD+2QVnXxBQIlEZYs5aW Itiwu5JGmu3CzSHGNzt0Vd2Lu0ThCrE= X-MC-Unique: slz-vFffOyakDavPMxWTBA-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org, Peter Maydell Subject: [PULL 02/13] libvhost-user: handle endianness as mandated by the spec Date: Wed, 23 Sep 2020 17:10:20 +0100 Message-Id: <20200923161031.69474-3-stefanha@redhat.com> In-Reply-To: <20200923161031.69474-1-stefanha@redhat.com> References: <20200923161031.69474-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=stefanha@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=63.128.21.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/23 00:53:58 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1.228, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MIME_BASE64_TEXT=1.741, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stefan Hajnoczi , Marc Hartmayer , "Dr. David Alan Gilbert" , qemu-block@nongnu.org, "Michael S . Tsirkin" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Marc Hartmayer Since virtio existed even before it got standardized, the virtio standard defines the following types of virtio devices: + legacy device (pre-virtio 1.0) + non-legacy or VIRTIO 1.0 device + transitional device (which can act both as legacy and non-legacy) Virtio 1.0 defines the fields of the virtqueues as little endian, while legacy uses guest's native endian [1]. Currently libvhost-user does not handle virtio endianness at all, i.e. it works only if the native endianness matches with whatever is actually needed. That means things break spectacularly on big-endian targets. Let us handle virtio endianness for non-legacy as required by the virtio specification [1] and fence legacy virtio, as there is no safe way to figure out the needed endianness conversions for all cases. The fencing of legacy virtio devices is done in `vu_set_features_exec`. [1] https://docs.oasis-open.org/virtio/virtio/v1.1/cs01/virtio-v1.1-cs01.ht= ml#x1-210003 Reviewed-by: Michael S. Tsirkin Signed-off-by: Marc Hartmayer Message-id: 20200901150019.29229-3-mhartmay@linux.ibm.com Signed-off-by: Stefan Hajnoczi --- contrib/libvhost-user/libvhost-user.c | 77 +++++++++++++++------------ 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/contrib/libvhost-user/libvhost-user.c b/contrib/libvhost-user/= libvhost-user.c index 9d30ff2283..daa575520c 100644 --- a/contrib/libvhost-user/libvhost-user.c +++ b/contrib/libvhost-user/libvhost-user.c @@ -42,6 +42,7 @@ =20 #include "qemu/atomic.h" #include "qemu/osdep.h" +#include "qemu/bswap.h" #include "qemu/memfd.h" =20 #include "libvhost-user.h" @@ -539,6 +540,14 @@ vu_set_features_exec(VuDev *dev, VhostUserMsg *vmsg) DPRINT("u64: 0x%016"PRIx64"\n", vmsg->payload.u64); =20 dev->features =3D vmsg->payload.u64; + if (!vu_has_feature(dev, VIRTIO_F_VERSION_1)) { + /* + * We only support devices conforming to VIRTIO 1.0 or + * later + */ + vu_panic(dev, "virtio legacy devices aren't supported by libvhost-= user"); + return false; + } =20 if (!(dev->features & VHOST_USER_F_PROTOCOL_FEATURES)) { vu_set_enable_all_rings(dev, true); @@ -1074,7 +1083,7 @@ vu_set_vring_addr_exec(VuDev *dev, VhostUserMsg *vmsg) return false; } =20 - vq->used_idx =3D vq->vring.used->idx; + vq->used_idx =3D lduw_le_p(&vq->vring.used->idx); =20 if (vq->last_avail_idx !=3D vq->used_idx) { bool resume =3D dev->iface->queue_is_processed_in_order && @@ -1191,7 +1200,7 @@ vu_check_queue_inflights(VuDev *dev, VuVirtq *vq) return 0; } =20 - vq->used_idx =3D vq->vring.used->idx; + vq->used_idx =3D lduw_le_p(&vq->vring.used->idx); vq->resubmit_num =3D 0; vq->resubmit_list =3D NULL; vq->counter =3D 0; @@ -2021,13 +2030,13 @@ vu_queue_started(const VuDev *dev, const VuVirtq *v= q) static inline uint16_t vring_avail_flags(VuVirtq *vq) { - return vq->vring.avail->flags; + return lduw_le_p(&vq->vring.avail->flags); } =20 static inline uint16_t vring_avail_idx(VuVirtq *vq) { - vq->shadow_avail_idx =3D vq->vring.avail->idx; + vq->shadow_avail_idx =3D lduw_le_p(&vq->vring.avail->idx); =20 return vq->shadow_avail_idx; } @@ -2035,7 +2044,7 @@ vring_avail_idx(VuVirtq *vq) static inline uint16_t vring_avail_ring(VuVirtq *vq, int i) { - return vq->vring.avail->ring[i]; + return lduw_le_p(&vq->vring.avail->ring[i]); } =20 static inline uint16_t @@ -2123,12 +2132,12 @@ virtqueue_read_next_desc(VuDev *dev, struct vring_d= esc *desc, int i, unsigned int max, unsigned int *next) { /* If this descriptor says it doesn't chain, we're done. */ - if (!(desc[i].flags & VRING_DESC_F_NEXT)) { + if (!(lduw_le_p(&desc[i].flags) & VRING_DESC_F_NEXT)) { return VIRTQUEUE_READ_DESC_DONE; } =20 /* Check they're not leading us off end of descriptors. */ - *next =3D desc[i].next; + *next =3D lduw_le_p(&desc[i].next); /* Make sure compiler knows to grab that: we don't want it changing! */ smp_wmb(); =20 @@ -2171,8 +2180,8 @@ vu_queue_get_avail_bytes(VuDev *dev, VuVirtq *vq, uns= igned int *in_bytes, } desc =3D vq->vring.desc; =20 - if (desc[i].flags & VRING_DESC_F_INDIRECT) { - if (desc[i].len % sizeof(struct vring_desc)) { + if (lduw_le_p(&desc[i].flags) & VRING_DESC_F_INDIRECT) { + if (ldl_le_p(&desc[i].len) % sizeof(struct vring_desc)) { vu_panic(dev, "Invalid size for indirect buffer table"); goto err; } @@ -2185,8 +2194,8 @@ vu_queue_get_avail_bytes(VuDev *dev, VuVirtq *vq, uns= igned int *in_bytes, =20 /* loop over the indirect descriptor table */ indirect =3D 1; - desc_addr =3D desc[i].addr; - desc_len =3D desc[i].len; + desc_addr =3D ldq_le_p(&desc[i].addr); + desc_len =3D ldl_le_p(&desc[i].len); max =3D desc_len / sizeof(struct vring_desc); read_len =3D desc_len; desc =3D vu_gpa_to_va(dev, &read_len, desc_addr); @@ -2213,10 +2222,10 @@ vu_queue_get_avail_bytes(VuDev *dev, VuVirtq *vq, u= nsigned int *in_bytes, goto err; } =20 - if (desc[i].flags & VRING_DESC_F_WRITE) { - in_total +=3D desc[i].len; + if (lduw_le_p(&desc[i].flags) & VRING_DESC_F_WRITE) { + in_total +=3D ldl_le_p(&desc[i].len); } else { - out_total +=3D desc[i].len; + out_total +=3D ldl_le_p(&desc[i].len); } if (in_total >=3D max_in_bytes && out_total >=3D max_out_bytes= ) { goto done; @@ -2367,7 +2376,7 @@ vring_used_flags_set_bit(VuVirtq *vq, int mask) =20 flags =3D (uint16_t *)((char*)vq->vring.used + offsetof(struct vring_used, flags)); - *flags |=3D mask; + stw_le_p(flags, lduw_le_p(flags) | mask); } =20 static inline void @@ -2377,7 +2386,7 @@ vring_used_flags_unset_bit(VuVirtq *vq, int mask) =20 flags =3D (uint16_t *)((char*)vq->vring.used + offsetof(struct vring_used, flags)); - *flags &=3D ~mask; + stw_le_p(flags, lduw_le_p(flags) & ~mask); } =20 static inline void @@ -2387,7 +2396,7 @@ vring_set_avail_event(VuVirtq *vq, uint16_t val) return; } =20 - *((uint16_t *) &vq->vring.used->ring[vq->vring.num]) =3D val; + stw_le_p(&vq->vring.used->ring[vq->vring.num], val); } =20 void @@ -2476,14 +2485,14 @@ vu_queue_map_desc(VuDev *dev, VuVirtq *vq, unsigned= int idx, size_t sz) struct vring_desc desc_buf[VIRTQUEUE_MAX_SIZE]; int rc; =20 - if (desc[i].flags & VRING_DESC_F_INDIRECT) { - if (desc[i].len % sizeof(struct vring_desc)) { + if (lduw_le_p(&desc[i].flags) & VRING_DESC_F_INDIRECT) { + if (ldl_le_p(&desc[i].len) % sizeof(struct vring_desc)) { vu_panic(dev, "Invalid size for indirect buffer table"); } =20 /* loop over the indirect descriptor table */ - desc_addr =3D desc[i].addr; - desc_len =3D desc[i].len; + desc_addr =3D ldq_le_p(&desc[i].addr); + desc_len =3D ldl_le_p(&desc[i].len); max =3D desc_len / sizeof(struct vring_desc); read_len =3D desc_len; desc =3D vu_gpa_to_va(dev, &read_len, desc_addr); @@ -2505,10 +2514,10 @@ vu_queue_map_desc(VuDev *dev, VuVirtq *vq, unsigned= int idx, size_t sz) =20 /* Collect all the descriptors */ do { - if (desc[i].flags & VRING_DESC_F_WRITE) { + if (lduw_le_p(&desc[i].flags) & VRING_DESC_F_WRITE) { virtqueue_map_desc(dev, &in_num, iov + out_num, VIRTQUEUE_MAX_SIZE - out_num, true, - desc[i].addr, desc[i].len); + ldq_le_p(&desc[i].addr), ldl_le_p(&desc[i].= len)); } else { if (in_num) { vu_panic(dev, "Incorrect order for descriptors"); @@ -2516,7 +2525,7 @@ vu_queue_map_desc(VuDev *dev, VuVirtq *vq, unsigned i= nt idx, size_t sz) } virtqueue_map_desc(dev, &out_num, iov, VIRTQUEUE_MAX_SIZE, false, - desc[i].addr, desc[i].len); + ldq_le_p(&desc[i].addr), ldl_le_p(&desc[i].= len)); } =20 /* If we've got too many, that implies a descriptor loop. */ @@ -2712,14 +2721,14 @@ vu_log_queue_fill(VuDev *dev, VuVirtq *vq, max =3D vq->vring.num; i =3D elem->index; =20 - if (desc[i].flags & VRING_DESC_F_INDIRECT) { - if (desc[i].len % sizeof(struct vring_desc)) { + if (lduw_le_p(&desc[i].flags) & VRING_DESC_F_INDIRECT) { + if (ldl_le_p(&desc[i].len) % sizeof(struct vring_desc)) { vu_panic(dev, "Invalid size for indirect buffer table"); } =20 /* loop over the indirect descriptor table */ - desc_addr =3D desc[i].addr; - desc_len =3D desc[i].len; + desc_addr =3D ldq_le_p(&desc[i].addr); + desc_len =3D ldl_le_p(&desc[i].len); max =3D desc_len / sizeof(struct vring_desc); read_len =3D desc_len; desc =3D vu_gpa_to_va(dev, &read_len, desc_addr); @@ -2745,9 +2754,9 @@ vu_log_queue_fill(VuDev *dev, VuVirtq *vq, return; } =20 - if (desc[i].flags & VRING_DESC_F_WRITE) { - min =3D MIN(desc[i].len, len); - vu_log_write(dev, desc[i].addr, min); + if (lduw_le_p(&desc[i].flags) & VRING_DESC_F_WRITE) { + min =3D MIN(ldl_le_p(&desc[i].len), len); + vu_log_write(dev, ldq_le_p(&desc[i].addr), min); len -=3D min; } =20 @@ -2772,15 +2781,15 @@ vu_queue_fill(VuDev *dev, VuVirtq *vq, =20 idx =3D (idx + vq->used_idx) % vq->vring.num; =20 - uelem.id =3D elem->index; - uelem.len =3D len; + stl_le_p(&uelem.id, elem->index); + stl_le_p(&uelem.len, len); vring_used_write(dev, vq, &uelem, idx); } =20 static inline void vring_used_idx_set(VuDev *dev, VuVirtq *vq, uint16_t val) { - vq->vring.used->idx =3D val; + stw_le_p(&vq->vring.used->idx, val); vu_log_write(dev, vq->vring.log_guest_addr + offsetof(struct vring_used, id= x), sizeof(vq->vring.used->idx)); --=20 2.26.2