From nobody Mon May 13 15:08:55 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1563455272; cv=none; d=zoho.com; s=zohoarc; b=f7z0w5BvaFoxWcz22qj8vSRlbe9gtTNV2NRc0qbQSOPnTd7YzOjMAeWX6NnsOBwLRrocjSspS3bhE5OjaFttGU5S3Janoglx8QAkTGXR0Dsg0wiqZibPOKzV9mgq1mwvPNR+9vs4V/rd//hYO6HdoPJUQKw9vd6U8/rykv6xBAY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1563455272; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To:ARC-Authentication-Results; bh=Cck4TXGIZld/k7LgUJPsSVr2ncya59vAY77eHouKzCs=; b=oqmH5rPemQqRWlRkxCHOinX1Cke8NEXMNNERglWHWshTB1/8A36Xq5aG4H6EK3WZEA8pnGCK7FqvXf0W+y/KbZ+406b8Nd4+VUnJDjZ8mJFcDoe1uyTHBNRlsoJLT5M6JiRB3sg/FCe5pOEmV09WnPM5LBPtHQftmov/hLzdY9c= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1563455272480537.7971260704479; Thu, 18 Jul 2019 06:07:52 -0700 (PDT) Received: from localhost ([::1]:37858 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1ho68R-0002CP-53 for importer@patchew.org; Thu, 18 Jul 2019 09:07:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55110) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1ho683-0001EC-IN for qemu-devel@nongnu.org; Thu, 18 Jul 2019 09:07:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ho681-0003b6-ND for qemu-devel@nongnu.org; Thu, 18 Jul 2019 09:07:23 -0400 Received: from mout.kundenserver.de ([212.227.126.133]:47363) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ho681-0003aX-D1 for qemu-devel@nongnu.org; Thu, 18 Jul 2019 09:07:21 -0400 Received: from localhost.localdomain ([78.238.229.36]) by mrelayeu.kundenserver.de (mreue009 [212.227.15.167]) with ESMTPSA (Nemesis) id 1Mt6wz-1ihQhI1QxV-00tVBu; Thu, 18 Jul 2019 15:06:45 +0200 From: Laurent Vivier To: qemu-devel@nongnu.org Date: Thu, 18 Jul 2019 15:06:41 +0200 Message-Id: <20190718130641.15294-1-laurent@vivier.eu> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:x03d6jUx8AGGFotJozHAkh71W6f4y3EECIiXRlsOJhzPLgERI2T ya1XV4kaIT5rk1nGiJD01GOjBdzQ1+eptB9nCmtc0JXekbnV0ynybQTSrswXt2HaaOKT4m7 hZaGwvCaUmahuDAcNofNSYxcnOeyPt30aFKfygPk3SlK69NWiFHgpWSTMbVpNXiIWsNTsKk 0GiV2eXCBKWd4YTtIFuiQ== X-UI-Out-Filterresults: notjunk:1;V03:K0:H3lJDbWVMtw=:WlizuGWlzc04zXu1PA3yQR aSZzmZHkL+DRhdUpAM+ZmL0Ogmhemr7vUUqWlmQ5yKU0xtirCj/uLexxPhML1x2IHa26hgIVu VNHspA1akYfYevHkQ1WiNWVr6dBXRqcDLa1YWXgT9nOqMBkP+KHtzelrIm6e0oz+yFIeZ5iLd hax05q7E0XbLcl9XrBzi1zMTp+yWNvnQso9hoUj8s8DMn2VduTN3MSp5WgT7zWfygFjy4d1Up QdV+gQC7O86XNgKezFbwnfJIhO+F+h4AUCL2bRp2Bzugx7YJh6rtwBp+aPu8ZY8L+Ss4FjYH8 zsDRjMKaT8tjiyUSBDolgfievQJ3jnZpDxfcSeFc37+pf6GUKjJt0tKuQMCRwL9B2kIdeFD1e ubAfcyl40koVoihPiVQHi2P667am5bZDm3SyK0rYTFdPLgyV6IVOLGsmFG03/Ve8MzFxE+i5P OvwRfPqtGDa4l+r1J69jQP/LVGijld3f+R760Rq8cAjmHjIR+TVW4/yxX73rEFGFueoFwfJ95 Pof25m3qqMfhOSlf6OWLToqnae9vHQrnp9J6OuzX/eY9kmimZZh6ufCxDUTo/Rmq/9VzYzNkU hQjPZbW0b5on0km//scts0doT+bZ8YGXviyX3vadckQuYP9k7j5+04B1VeXYtaPBoUREhpLZV 74eFGGmvoehzN1MmL/4psYvtuGBLuWipqfMpUGwH3XxTiacAHGnd/za3kUV56PZ2sSBS5qshr 6qwRaxenS88RtckTtzHkuWwOSmha7H8+kK3O62rCYcewUSZlagyvKy5+v4E= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 212.227.126.133 Subject: [Qemu-devel] [PATCH v6] linux-user: fix to handle variably sized SIOCGSTAMP with new kernels 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: Peter Maydell , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Arnd Bergmann , Gerhard Stenzel , Riku Voipio , Richard Henderson , Laurent Vivier , Christian Ehrhardt , Christian Borntraeger Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" From: Daniel P. Berrang=C3=A9 The SIOCGSTAMP symbol was previously defined in the asm-generic/sockios.h header file. QEMU sees that header indirectly via sys/socket.h In linux kernel commit 0768e17073dc527ccd18ed5f96ce85f9985e9115 the asm-generic/sockios.h header no longer defines SIOCGSTAMP. Instead it provides only SIOCGSTAMP_OLD, which only uses a 32-bit time_t on 32-bit architectures. The linux/sockios.h header then defines SIOCGSTAMP using either SIOCGSTAMP_OLD or SIOCGSTAMP_NEW as appropriate. If SIOCGSTAMP_NEW is used, then the tv_sec field is 64-bit even on 32-bit architectures To cope with this we must now convert the old and new type from the target to the host one. Signed-off-by: Daniel P. Berrang=C3=A9 Signed-off-by: Laurent Vivier Reviewed-by: Arnd Bergmann --- Notes: v6: [lv] fix IOCTLEntry entries (host_cmd and name) v5: [lv] define special _OLD values for sh define special case for SPARC64 define ioctl helpers define target__kernel_sock_timeval and target__kernel_timespec and converters to the host type always use SIOCGSTAMP and SIOCGSTAMPNS on the host =20 v4: [lv] timeval64 and timespec64 are {=C2=A0long long , long } =20 v3: [lv] redefine TARGET_SIOCGSTAMP_NEW, TARGET_SIOCGSTAMPNS_NEW, timeval64 and timespec64 to use 0x89 type and abi_llong[2] =20 v2: [dpb] implement _NEW and _OLD variants linux-user/ioctls.h | 21 +++++- linux-user/syscall.c | 140 +++++++++++++++++++++++++++++-------- linux-user/syscall_defs.h | 30 +++++++- linux-user/syscall_types.h | 6 -- 4 files changed, 159 insertions(+), 38 deletions(-) diff --git a/linux-user/ioctls.h b/linux-user/ioctls.h index 5e84dc7c3a77..3281c97ca263 100644 --- a/linux-user/ioctls.h +++ b/linux-user/ioctls.h @@ -222,8 +222,25 @@ IOCTL(SIOCGIWNAME, IOC_W | IOC_R, MK_PTR(MK_STRUCT(STRUCT_char_ifreq))) IOCTL(SIOCSPGRP, IOC_W, MK_PTR(TYPE_INT)) /* pid_t */ IOCTL(SIOCGPGRP, IOC_R, MK_PTR(TYPE_INT)) /* pid_t */ - IOCTL(SIOCGSTAMP, IOC_R, MK_PTR(MK_STRUCT(STRUCT_timeval))) - IOCTL(SIOCGSTAMPNS, IOC_R, MK_PTR(MK_STRUCT(STRUCT_timespec))) + + /* + * We can't use IOCTL_SPECIAL() because it will set + * host_cmd to XXX_OLD and XXX_NEW and these macros + * are not defined with kernel prior to 5.2. + * We must set host_cmd to the same value as in target_cmd + * otherwise the consistency check in syscall_init() + * will trigger an error. + * host_cmd is ignored by the do_ioctl_XXX() helpers. + * FIXME: create a macro to define this kind of entry + */ + { TARGET_SIOCGSTAMP_OLD, TARGET_SIOCGSTAMP_OLD, + "SIOCGSTAMP_OLD", IOC_R, do_ioctl_SIOCGSTAMP }, + { TARGET_SIOCGSTAMPNS_OLD, TARGET_SIOCGSTAMPNS_OLD, + "SIOCGSTAMPNS_OLD", IOC_R, do_ioctl_SIOCGSTAMPNS }, + { TARGET_SIOCGSTAMP_NEW, TARGET_SIOCGSTAMP_NEW, + "SIOCGSTAMP_NEW", IOC_R, do_ioctl_SIOCGSTAMP }, + { TARGET_SIOCGSTAMPNS_NEW, TARGET_SIOCGSTAMPNS_NEW, + "SIOCGSTAMPNS_NEW", IOC_R, do_ioctl_SIOCGSTAMPNS }, =20 IOCTL(RNDGETENTCNT, IOC_R, MK_PTR(TYPE_INT)) IOCTL(RNDADDTOENTCNT, IOC_W, MK_PTR(TYPE_INT)) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 39a37496fed5..8367cb138dfe 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -1126,8 +1127,9 @@ static inline abi_long copy_from_user_timeval(struct = timeval *tv, { struct target_timeval *target_tv; =20 - if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) + if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 1)) { return -TARGET_EFAULT; + } =20 __get_user(tv->tv_sec, &target_tv->tv_sec); __get_user(tv->tv_usec, &target_tv->tv_usec); @@ -1142,8 +1144,26 @@ static inline abi_long copy_to_user_timeval(abi_ulon= g target_tv_addr, { struct target_timeval *target_tv; =20 - if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) + if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) { + return -TARGET_EFAULT; + } + + __put_user(tv->tv_sec, &target_tv->tv_sec); + __put_user(tv->tv_usec, &target_tv->tv_usec); + + unlock_user_struct(target_tv, target_tv_addr, 1); + + return 0; +} + +static inline abi_long copy_to_user_timeval64(abi_ulong target_tv_addr, + const struct timeval *tv) +{ + struct target__kernel_sock_timeval *target_tv; + + if (!lock_user_struct(VERIFY_WRITE, target_tv, target_tv_addr, 0)) { return -TARGET_EFAULT; + } =20 __put_user(tv->tv_sec, &target_tv->tv_sec); __put_user(tv->tv_usec, &target_tv->tv_usec); @@ -1153,6 +1173,48 @@ static inline abi_long copy_to_user_timeval(abi_ulon= g target_tv_addr, return 0; } =20 +static inline abi_long target_to_host_timespec(struct timespec *host_ts, + abi_ulong target_addr) +{ + struct target_timespec *target_ts; + + if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) { + return -TARGET_EFAULT; + } + __get_user(host_ts->tv_sec, &target_ts->tv_sec); + __get_user(host_ts->tv_nsec, &target_ts->tv_nsec); + unlock_user_struct(target_ts, target_addr, 0); + return 0; +} + +static inline abi_long host_to_target_timespec(abi_ulong target_addr, + struct timespec *host_ts) +{ + struct target_timespec *target_ts; + + if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) { + return -TARGET_EFAULT; + } + __put_user(host_ts->tv_sec, &target_ts->tv_sec); + __put_user(host_ts->tv_nsec, &target_ts->tv_nsec); + unlock_user_struct(target_ts, target_addr, 1); + return 0; +} + +static inline abi_long host_to_target_timespec64(abi_ulong target_addr, + struct timespec *host_ts) +{ + struct target__kernel_timespec *target_ts; + + if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) { + return -TARGET_EFAULT; + } + __put_user(host_ts->tv_sec, &target_ts->tv_sec); + __put_user(host_ts->tv_nsec, &target_ts->tv_nsec); + unlock_user_struct(target_ts, target_addr, 1); + return 0; +} + static inline abi_long copy_from_user_timezone(struct timezone *tz, abi_ulong target_tz_addr) { @@ -4899,6 +4961,54 @@ static abi_long do_ioctl_kdsigaccept(const IOCTLEntr= y *ie, uint8_t *buf_temp, return get_errno(safe_ioctl(fd, ie->host_cmd, sig)); } =20 +static abi_long do_ioctl_SIOCGSTAMP(const IOCTLEntry *ie, uint8_t *buf_tem= p, + int fd, int cmd, abi_long arg) +{ + struct timeval tv; + abi_long ret; + + ret =3D get_errno(safe_ioctl(fd, SIOCGSTAMP, &tv)); + if (is_error(ret)) { + return ret; + } + + if (cmd =3D=3D (int)TARGET_SIOCGSTAMP_OLD) { + if (copy_to_user_timeval(arg, &tv)) { + return -TARGET_EFAULT; + } + } else { + if (copy_to_user_timeval64(arg, &tv)) { + return -TARGET_EFAULT; + } + } + + return ret; +} + +static abi_long do_ioctl_SIOCGSTAMPNS(const IOCTLEntry *ie, uint8_t *buf_t= emp, + int fd, int cmd, abi_long arg) +{ + struct timespec ts; + abi_long ret; + + ret =3D get_errno(safe_ioctl(fd, SIOCGSTAMPNS, &ts)); + if (is_error(ret)) { + return ret; + } + + if (cmd =3D=3D (int)TARGET_SIOCGSTAMPNS_OLD) { + if (host_to_target_timespec(arg, &ts)) { + return -TARGET_EFAULT; + } + } else{ + if (host_to_target_timespec64(arg, &ts)) { + return -TARGET_EFAULT; + } + } + + return ret; +} + #ifdef TIOCGPTPEER static abi_long do_ioctl_tiocgptpeer(const IOCTLEntry *ie, uint8_t *buf_te= mp, int fd, int cmd, abi_long arg) @@ -6271,32 +6381,6 @@ static inline abi_long target_ftruncate64(void *cpu_= env, abi_long arg1, } #endif =20 -static inline abi_long target_to_host_timespec(struct timespec *host_ts, - abi_ulong target_addr) -{ - struct target_timespec *target_ts; - - if (!lock_user_struct(VERIFY_READ, target_ts, target_addr, 1)) - return -TARGET_EFAULT; - __get_user(host_ts->tv_sec, &target_ts->tv_sec); - __get_user(host_ts->tv_nsec, &target_ts->tv_nsec); - unlock_user_struct(target_ts, target_addr, 0); - return 0; -} - -static inline abi_long host_to_target_timespec(abi_ulong target_addr, - struct timespec *host_ts) -{ - struct target_timespec *target_ts; - - if (!lock_user_struct(VERIFY_WRITE, target_ts, target_addr, 0)) - return -TARGET_EFAULT; - __put_user(host_ts->tv_sec, &target_ts->tv_sec); - __put_user(host_ts->tv_nsec, &target_ts->tv_nsec); - unlock_user_struct(target_ts, target_addr, 1); - return 0; -} - static inline abi_long target_to_host_itimerspec(struct itimerspec *host_i= tspec, abi_ulong target_addr) { diff --git a/linux-user/syscall_defs.h b/linux-user/syscall_defs.h index fffa89f2564b..06622703008a 100644 --- a/linux-user/syscall_defs.h +++ b/linux-user/syscall_defs.h @@ -209,16 +209,34 @@ struct target_linger { abi_int l_linger; /* How long to linger for */ }; =20 +#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32) +struct target_timeval { + abi_long tv_sec; + abi_int tv_usec; +}; +#define target__kernel_sock_timeval target_timeval +#else struct target_timeval { abi_long tv_sec; abi_long tv_usec; }; =20 +struct target__kernel_sock_timeval { + abi_llong tv_sec; + abi_llong tv_usec; +}; +#endif + struct target_timespec { abi_long tv_sec; abi_long tv_nsec; }; =20 +struct target__kernel_timespec { + abi_llong tv_sec; + abi_llong tv_nsec; +}; + struct target_timezone { abi_int tz_minuteswest; abi_int tz_dsttime; @@ -749,8 +767,16 @@ struct target_pollfd { #define TARGET_SIOCGPGRP 0x8904 #endif =20 -#define TARGET_SIOCGSTAMP 0x8906 /* Get stamp (timeval) */ -#define TARGET_SIOCGSTAMPNS 0x8907 /* Get stamp (timespec) */ +#if defined(TARGET_SH4) +#define TARGET_SIOCGSTAMP_OLD TARGET_IOR('s', 100, struct target_timeval) +#define TARGET_SIOCGSTAMPNS_OLD TARGET_IOR('s', 101, struct target_timespe= c) +#else +#define TARGET_SIOCGSTAMP_OLD 0x8906 +#define TARGET_SIOCGSTAMPNS_OLD 0x8907 +#endif + +#define TARGET_SIOCGSTAMP_NEW TARGET_IOR(0x89, 0x06, abi_llong[2]) +#define TARGET_SIOCGSTAMPNS_NEW TARGET_IOR(0x89, 0x07, abi_llong[2]) =20 /* Networking ioctls */ #define TARGET_SIOCADDRT 0x890B /* add routing table entry = */ diff --git a/linux-user/syscall_types.h b/linux-user/syscall_types.h index b98a23b0f1b0..4e3698382629 100644 --- a/linux-user/syscall_types.h +++ b/linux-user/syscall_types.h @@ -14,12 +14,6 @@ STRUCT(serial_icounter_struct, STRUCT(sockaddr, TYPE_SHORT, MK_ARRAY(TYPE_CHAR, 14)) =20 -STRUCT(timeval, - MK_ARRAY(TYPE_LONG, 2)) - -STRUCT(timespec, - MK_ARRAY(TYPE_LONG, 2)) - STRUCT(rtentry, TYPE_ULONG, MK_STRUCT(STRUCT_sockaddr), MK_STRUCT(STRUCT_sockaddr),= MK_STRUCT(STRUCT_sockaddr), TYPE_SHORT, TYPE_SHORT, TYPE_ULONG, TYPE_PTRVOID, TYPE_SHORT, TYPE_= PTRVOID, --=20 2.21.0