From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Subject: [RFC PATCH 2/4] vhost: add byteswap routines Date: Wed, 29 Oct 2014 09:38:43 +0100 Message-ID: <1414571925-16918-3-git-send-email-clg@fr.ibm.com> References: <1414571925-16918-1-git-send-email-clg@fr.ibm.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, agraf@suse.de, paulus@samba.org, gkurz@linux.vnet.ibm.com.patch, aik@ozlabs.ru, =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= To: "Michael S. Tsirkin" Return-path: In-Reply-To: <1414571925-16918-1-git-send-email-clg@fr.ibm.com> Sender: kvm-ppc-owner@vger.kernel.org List-Id: kvm.vger.kernel.org This patch adds a few helper routines around get_user and put_user to ease byteswapping. Signed-off-by: C=C3=A9dric Le Goater --- I am not sure these routines belong to this file. There is room for=20 improvement to remove the ugly switch (sizeof(*(ptr))). Please=20 comment ! drivers/vhost/vhost.c | 93 +++++++++++++++++++++++++++++++++++++++++= ++++++++ 1 file changed, 93 insertions(+) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index 72c21b790ba3..afcb3368370c 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -25,6 +25,7 @@ #include #include #include +#include =20 #include "vhost.h" =20 @@ -1001,6 +1002,98 @@ int vhost_log_write(struct vhost_virtqueue *vq, = struct vhost_log *log, } EXPORT_SYMBOL_GPL(vhost_log_write); =20 +#define vq_get_user(vq, x, ptr) \ +({ \ + int ret =3D get_user(x, ptr); \ + if (vq->byteswap) { \ + switch (sizeof(*(ptr))) { \ + case 2: \ + x =3D swab16(x); \ + break; \ + case 4: \ + x =3D swab32(x); \ + break; \ + case 8: \ + x =3D swab64(x); \ + break; \ + default: \ + break; \ + } \ + } \ + ret; \ +}) + +#define vq_put_user(vq, x, ptr) \ +({ \ + __typeof__(*(ptr)) y =3D (x); \ + if (vq->byteswap) { \ + switch (sizeof(*(ptr))) { \ + case 2: \ + y =3D swab16(x); \ + break; \ + case 4: \ + y =3D swab32(x); \ + break; \ + case 8: \ + y =3D swab64(x); \ + break; \ + default: \ + break; \ + } \ + } \ + put_user(y, ptr); \ +}) + +#define __vq_get_user(vq, x, ptr) \ +({ \ + int ret =3D __get_user(x, ptr); \ + if (vq->byteswap) { \ + switch (sizeof(*(ptr))) { \ + case 2: \ + x =3D swab16(x); \ + break; \ + case 4: \ + x =3D swab32(x); \ + break; \ + case 8: \ + x =3D swab64(x); \ + break; \ + default: \ + break; \ + } \ + } \ + ret; \ +}) + +#define __vq_put_user(vq, x, ptr) \ +({ \ + __typeof__(*(ptr)) y =3D (x); \ + if (vq->byteswap) { \ + switch (sizeof(*(ptr))) { \ + case 2: \ + y =3D swab16(x); \ + break; \ + case 4: \ + y =3D swab32(x); \ + break; \ + case 8: \ + y =3D swab64(x); \ + break; \ + default: \ + break; \ + } \ + } \ + __put_user(y, ptr); \ +}) + +static void vring_desc_swap(struct vring_desc *desc) +{ + desc->addr =3D swab64(desc->addr); + desc->len =3D swab32(desc->len); + desc->flags =3D swab16(desc->flags); + desc->next =3D swab16(desc->next); +} + static int vhost_update_used_flags(struct vhost_virtqueue *vq) { void __user *used; --=20 1.7.10.4