From patchwork Fri Oct 19 12:16:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Vivier X-Patchwork-Id: 986720 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=vivier.eu Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42c4lP24Wdz9sj4 for ; Fri, 19 Oct 2018 23:21:21 +1100 (AEDT) Received: from localhost ([::1]:49850 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gDTmI-0008Kp-QN for incoming@patchwork.ozlabs.org; Fri, 19 Oct 2018 08:21:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34624) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gDTiO-0005QB-Hr for qemu-devel@nongnu.org; Fri, 19 Oct 2018 08:17:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gDTiI-0003dO-JP for qemu-devel@nongnu.org; Fri, 19 Oct 2018 08:17:15 -0400 Received: from mout.kundenserver.de ([212.227.126.130]:40385) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gDTiI-0003am-8J for qemu-devel@nongnu.org; Fri, 19 Oct 2018 08:17:10 -0400 Received: from localhost.localdomain ([78.238.229.36]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.167]) with ESMTPSA (Nemesis) id 1N7iT4-1faH5Z0ecU-014onA; Fri, 19 Oct 2018 14:16:31 +0200 Received: from localhost.localdomain ([78.238.229.36]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.167]) with ESMTPSA (Nemesis) id 1N7iT4-1faH5Z0ecU-014onA; Fri, 19 Oct 2018 14:16:31 +0200 From: Laurent Vivier To: qemu-devel@nongnu.org Date: Fri, 19 Oct 2018 14:16:23 +0200 Message-Id: <20181019121623.29148-4-laurent@vivier.eu> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181019121623.29148-1-laurent@vivier.eu> References: <20181019121623.29148-1-laurent@vivier.eu> MIME-Version: 1.0 X-Provags-ID: V03:K1:A/0npgawVLFavHnnG9ZO6IM3kyGy4P6DiEl6q9os+mDdhPS/xQ/ 521QPzI5qX/B0J0WXiiFnWorOg6wCfUunbKWY4JXUuiLRhyuHUMuFwRUIZ9jqtefK7LwHwq IdvBRQheua2RJ73TTK4jzmFGZrfQNzlFj/uYkVJ1lDcV18dHd4zaLRJ1iXk6IOXMHpvaHnk R+rIzSlmSqHPQ08NFU9qw== X-UI-Out-Filterresults: notjunk:1; V01:K0:iG6KvTduU0s=:+hymY8QyMiHNt1tQpegcd8 6J1Nwy7WYRkp4hlZrPA2AYI/3vPTSBSqS9w+pKr6cegYDwFImb7ps9m2ioZb64/m2PpvHLipX X6rbwk1jLGa8DI4fPOsxo0hWsON4s5XO0HV0La4LgauBQGLtGCsQtaQ87vj3bW0X3CZCGKEwJ sWNULhwMdSY6o/WMD0daUHGIWRmlknr6SPeIZ7auyN01bFrwdgPKzCnIxkxGZHD0FV2uzT033 75CW7pFS5z8Z2YvgMXcjxAG46apE8MQJ1XfXK4WjCAZKCr1JsT0eFuzjz9bzkINzpg0mX2SAw UsgcjbHrIicB+33JMewrdhlPyS1ZFSe6W/qRJyoWsW6XvoL4BNyEG8d0n58lDiS559vS3K8t7 orvUSKDXmkKR9gwtDOiEJAKhag4S1eMqM1EtapDwQdvY8tf5VjzNqY5r9nS1IY+5bLAXoY/LK 71oc7dlUd3/skPcmCXzVDkKzB6lg37m76wGQBRHlwseFH/sVSS3jVEGGZLO7jqXKESuM6/mMF cKRX1ULLVMFSP7iHgv47RqGq+5CkmsLuqVGNfunmom0taCa+UCZN9s1AY1OuVG1kKI9Gwob1V Xd7vklGjt1HUYBjM5TAwrwyjfkPNi3Ubj29+nGeS67oHFn3SOJmT4rDPoneEXdBNz8UinN/CX YcfAoNgESb6z3HMTLCa2bbauGprepviLAqq6o+Qnl2HMst6mN6gocxezkCt9z7ib7xZeGOoUf jsXbY3D49cYfGPhg X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 212.227.126.130 Subject: [Qemu-devel] [2 3/3] linux-user: Implement special usbfs ioctls. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Riku Voipio , Laurent Vivier , =?utf-8?q?Cortland_T=C3=B6lva?= Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Cortland Tölva Userspace submits a USB Request Buffer to the kernel, optionally discards it, and finally reaps the URB. Thunk buffers from target to host and back. Tested by running an i386 scanner driver on ARMv7 and by running the PowerPC lsusb utility on x86_64. The discardurb ioctl is not exercised in these tests. Signed-off-by: Cortland Tölva Message-Id: <20181008163521.17341-4-cst@tolva.net> Signed-off-by: Laurent Vivier --- linux-user/ioctls.h | 8 ++ linux-user/syscall.c | 177 +++++++++++++++++++++++++++++++++++++ linux-user/syscall_defs.h | 4 + linux-user/syscall_types.h | 20 +++++ 4 files changed, 209 insertions(+) diff --git a/linux-user/ioctls.h b/linux-user/ioctls.h index 92f6177f1d..ae8951625f 100644 --- a/linux-user/ioctls.h +++ b/linux-user/ioctls.h @@ -143,6 +143,14 @@ IOCTL(USBDEVFS_SETCONFIGURATION, IOC_W, MK_PTR(TYPE_INT)) IOCTL(USBDEVFS_GETDRIVER, IOC_R, MK_PTR(MK_STRUCT(STRUCT_usbdevfs_getdriver))) + IOCTL_SPECIAL(USBDEVFS_SUBMITURB, IOC_W, do_ioctl_usbdevfs_submiturb, + MK_PTR(MK_STRUCT(STRUCT_usbdevfs_urb))) + IOCTL_SPECIAL(USBDEVFS_DISCARDURB, IOC_RW, do_ioctl_usbdevfs_discardurb, + MK_PTR(MK_STRUCT(STRUCT_usbdevfs_urb))) + IOCTL_SPECIAL(USBDEVFS_REAPURB, IOC_R, do_ioctl_usbdevfs_reapurb, + MK_PTR(TYPE_PTRVOID)) + IOCTL_SPECIAL(USBDEVFS_REAPURBNDELAY, IOC_R, do_ioctl_usbdevfs_reapurb, + MK_PTR(TYPE_PTRVOID)) IOCTL(USBDEVFS_DISCSIGNAL, IOC_W, MK_PTR(MK_STRUCT(STRUCT_usbdevfs_disconnectsignal))) IOCTL(USBDEVFS_CLAIMINTERFACE, IOC_W, MK_PTR(TYPE_INT)) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 6eb20c14c9..cf4511b0e4 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -96,6 +96,7 @@ #include #if defined(CONFIG_USBFS) #include +#include #endif #include #include @@ -4199,6 +4200,182 @@ static abi_long do_ioctl_ifconf(const IOCTLEntry *ie, uint8_t *buf_temp, return ret; } +#if defined(CONFIG_USBFS) +#if HOST_LONG_BITS > 64 +#error USBDEVFS thunks do not support >64 bit hosts yet. +#endif +struct live_urb { + uint64_t target_urb_adr; + uint64_t target_buf_adr; + char *target_buf_ptr; + struct usbdevfs_urb host_urb; +}; + +static GHashTable *usbdevfs_urb_hashtable(void) +{ + static GHashTable *urb_hashtable; + + if (!urb_hashtable) { + urb_hashtable = g_hash_table_new(g_int64_hash, g_int64_equal); + } + return urb_hashtable; +} + +static void urb_hashtable_insert(struct live_urb *urb) +{ + GHashTable *urb_hashtable = usbdevfs_urb_hashtable(); + g_hash_table_insert(urb_hashtable, urb, urb); +} + +static struct live_urb *urb_hashtable_lookup(uint64_t target_urb_adr) +{ + GHashTable *urb_hashtable = usbdevfs_urb_hashtable(); + return g_hash_table_lookup(urb_hashtable, &target_urb_adr); +} + +static void urb_hashtable_remove(struct live_urb *urb) +{ + GHashTable *urb_hashtable = usbdevfs_urb_hashtable(); + g_hash_table_remove(urb_hashtable, urb); +} + +static abi_long +do_ioctl_usbdevfs_reapurb(const IOCTLEntry *ie, uint8_t *buf_temp, + int fd, int cmd, abi_long arg) +{ + const argtype usbfsurb_arg_type[] = { MK_STRUCT(STRUCT_usbdevfs_urb) }; + const argtype ptrvoid_arg_type[] = { TYPE_PTRVOID, 0, 0 }; + struct live_urb *lurb; + void *argptr; + uint64_t hurb; + int target_size; + uintptr_t target_urb_adr; + abi_long ret; + + target_size = thunk_type_size(usbfsurb_arg_type, THUNK_TARGET); + + memset(buf_temp, 0, sizeof(uint64_t)); + ret = get_errno(safe_ioctl(fd, ie->host_cmd, buf_temp)); + if (is_error(ret)) { + return ret; + } + + memcpy(&hurb, buf_temp, sizeof(uint64_t)); + lurb = (void *)((uintptr_t)hurb - offsetof(struct live_urb, host_urb)); + if (!lurb->target_urb_adr) { + return -TARGET_EFAULT; + } + urb_hashtable_remove(lurb); + unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, + lurb->host_urb.buffer_length); + lurb->target_buf_ptr = NULL; + + /* restore the guest buffer pointer */ + lurb->host_urb.buffer = (void *)(uintptr_t)lurb->target_buf_adr; + + /* update the guest urb struct */ + argptr = lock_user(VERIFY_WRITE, lurb->target_urb_adr, target_size, 0); + if (!argptr) { + g_free(lurb); + return -TARGET_EFAULT; + } + thunk_convert(argptr, &lurb->host_urb, usbfsurb_arg_type, THUNK_TARGET); + unlock_user(argptr, lurb->target_urb_adr, target_size); + + target_size = thunk_type_size(ptrvoid_arg_type, THUNK_TARGET); + /* write back the urb handle */ + argptr = lock_user(VERIFY_WRITE, arg, target_size, 0); + if (!argptr) { + g_free(lurb); + return -TARGET_EFAULT; + } + + /* GHashTable uses 64-bit keys but thunk_convert expects uintptr_t */ + target_urb_adr = lurb->target_urb_adr; + thunk_convert(argptr, &target_urb_adr, ptrvoid_arg_type, THUNK_TARGET); + unlock_user(argptr, arg, target_size); + + g_free(lurb); + return ret; +} + +static abi_long +do_ioctl_usbdevfs_discardurb(const IOCTLEntry *ie, + uint8_t *buf_temp __attribute__((unused)), + int fd, int cmd, abi_long arg) +{ + struct live_urb *lurb; + + /* map target address back to host URB with metadata. */ + lurb = urb_hashtable_lookup(arg); + if (!lurb) { + return -TARGET_EFAULT; + } + return get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb)); +} + +static abi_long +do_ioctl_usbdevfs_submiturb(const IOCTLEntry *ie, uint8_t *buf_temp, + int fd, int cmd, abi_long arg) +{ + const argtype *arg_type = ie->arg_type; + int target_size; + abi_long ret; + void *argptr; + int rw_dir; + struct live_urb *lurb; + + /* + * each submitted URB needs to map to a unique ID for the + * kernel, and that unique ID needs to be a pointer to + * host memory. hence, we need to malloc for each URB. + * isochronous transfers have a variable length struct. + */ + arg_type++; + target_size = thunk_type_size(arg_type, THUNK_TARGET); + + /* construct host copy of urb and metadata */ + lurb = g_try_malloc0(sizeof(struct live_urb)); + if (!lurb) { + return -TARGET_ENOMEM; + } + + argptr = lock_user(VERIFY_READ, arg, target_size, 1); + if (!argptr) { + g_free(lurb); + return -TARGET_EFAULT; + } + thunk_convert(&lurb->host_urb, argptr, arg_type, THUNK_HOST); + unlock_user(argptr, arg, 0); + + lurb->target_urb_adr = arg; + lurb->target_buf_adr = (uintptr_t)lurb->host_urb.buffer; + + /* buffer space used depends on endpoint type so lock the entire buffer */ + /* control type urbs should check the buffer contents for true direction */ + rw_dir = lurb->host_urb.endpoint & USB_DIR_IN ? VERIFY_WRITE : VERIFY_READ; + lurb->target_buf_ptr = lock_user(rw_dir, lurb->target_buf_adr, + lurb->host_urb.buffer_length, 1); + if (lurb->target_buf_ptr == NULL) { + g_free(lurb); + return -TARGET_EFAULT; + } + + /* update buffer pointer in host copy */ + lurb->host_urb.buffer = lurb->target_buf_ptr; + + ret = get_errno(safe_ioctl(fd, ie->host_cmd, &lurb->host_urb)); + if (is_error(ret)) { + unlock_user(lurb->target_buf_ptr, lurb->target_buf_adr, 0); + g_free(lurb); + } else { + urb_hashtable_insert(lurb); + } + + return ret; +} +#endif /* CONFIG_USBFS */ + static abi_long do_ioctl_dm(const IOCTLEntry *ie, uint8_t *buf_temp, int fd, int cmd, abi_long arg) { diff --git a/linux-user/syscall_defs.h b/linux-user/syscall_defs.h index 2daa5ebdcc..99bbce083c 100644 --- a/linux-user/syscall_defs.h +++ b/linux-user/syscall_defs.h @@ -870,6 +870,10 @@ struct target_pollfd { #define TARGET_USBDEVFS_SETINTERFACE TARGET_IORU('U', 4) #define TARGET_USBDEVFS_SETCONFIGURATION TARGET_IORU('U', 5) #define TARGET_USBDEVFS_GETDRIVER TARGET_IOWU('U', 8) +#define TARGET_USBDEVFS_SUBMITURB TARGET_IORU('U', 10) +#define TARGET_USBDEVFS_DISCARDURB TARGET_IO('U', 11) +#define TARGET_USBDEVFS_REAPURB TARGET_IOWU('U', 12) +#define TARGET_USBDEVFS_REAPURBNDELAY TARGET_IOWU('U', 13) #define TARGET_USBDEVFS_DISCSIGNAL TARGET_IORU('U', 14) #define TARGET_USBDEVFS_CLAIMINTERFACE TARGET_IORU('U', 15) #define TARGET_USBDEVFS_RELEASEINTERFACE TARGET_IORU('U', 16) diff --git a/linux-user/syscall_types.h b/linux-user/syscall_types.h index 6f64a8bdf7..b98a23b0f1 100644 --- a/linux-user/syscall_types.h +++ b/linux-user/syscall_types.h @@ -300,6 +300,26 @@ STRUCT(usbdevfs_connectinfo, TYPE_INT, /* devnum */ TYPE_CHAR) /* slow */ +STRUCT(usbdevfs_iso_packet_desc, + TYPE_INT, /* length */ + TYPE_INT, /* actual_length */ + TYPE_INT) /* status */ + +STRUCT(usbdevfs_urb, + TYPE_CHAR, /* type */ + TYPE_CHAR, /* endpoint */ + TYPE_INT, /* status */ + TYPE_INT, /* flags */ + TYPE_PTRVOID, /* buffer */ + TYPE_INT, /* buffer_length */ + TYPE_INT, /* actual_length */ + TYPE_INT, /* start_frame */ + TYPE_INT, /* union number_of_packets stream_id */ + TYPE_INT, /* error_count */ + TYPE_INT, /* signr */ + TYPE_PTRVOID, /* usercontext */ + MK_ARRAY(MK_STRUCT(STRUCT_usbdevfs_iso_packet_desc), 0)) /* desc */ + STRUCT(usbdevfs_ioctl, TYPE_INT, /* ifno */ TYPE_INT, /* ioctl_code */