From nobody Tue Feb 10 06:59:07 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1637569602706869.3292605408823; Mon, 22 Nov 2021 00:26:42 -0800 (PST) Received: from localhost ([::1]:49200 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mp4eu-00049w-4J for importer@patchew.org; Mon, 22 Nov 2021 03:26:40 -0500 Received: from eggs.gnu.org ([209.51.188.92]:40726) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mp4bf-0001X1-Nr for qemu-devel@nongnu.org; Mon, 22 Nov 2021 03:23:22 -0500 Received: from mout.kundenserver.de ([212.227.126.130]:36781) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mp4bd-00035E-9Y for qemu-devel@nongnu.org; Mon, 22 Nov 2021 03:23:19 -0500 Received: from quad ([82.142.2.234]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.167]) with ESMTPSA (Nemesis) id 1MSswA-1nDnVp3E9W-00UHW7; Mon, 22 Nov 2021 09:23:12 +0100 From: Laurent Vivier To: qemu-devel@nongnu.org Subject: [PULL 1/5] linux-user: Split out do_getdents, do_getdents64 Date: Mon, 22 Nov 2021 09:23:06 +0100 Message-Id: <20211122082310.377809-2-laurent@vivier.eu> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211122082310.377809-1-laurent@vivier.eu> References: <20211122082310.377809-1-laurent@vivier.eu> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:EXbD8RfDaifTz8whB2UETtGdgwPtd+pceXxR4nhay8jKwdPOsNZ yP3XOkJkwjK9wr5wElj+1muY8flQoSBZt7w3v3GOr8yoQp7Mv+vzjej4YVcqfPo57sBcaMG pOiFDLH7LwF6LCpIiiCYXzXO+91VFDM9wQhJ/Z8c2C6vivcBy5ZPDopVt3ppjAIHk/bLlTG HQfIOc9l3flOtJ4xximNA== X-UI-Out-Filterresults: notjunk:1;V03:K0:SONwfYRPFhI=:ud79adI9E08AE1AD+z2Gwv c25LuXBrbb15DHv9vRgYiVLD2X6Knv74F8A4xk0HquDeX1f07AS02gGGXJv+bubSaXrlM7h/b 2VelmIicLM0rxFw2nXI9FsAOcHfO5TqLQ/pSkWCyljfVz6UtodT1u3I3JaHLoyD9vWkUXQQvJ I79IP9SOYiPRTeawAFj/6gJqopHB4wqzZ077fGA68WmUzrFGpRG9VGKqmeYW5hcq523xIyTCN PgBfUin0fRTkrlHH8sqy5BksaWRSAYq0Au43GZUg4iGrEZbr2P+/ZAHCwZz/A8rx60ncX+qY0 XqSH/nsOIe6xEtiaQsZ9g7AZe683mVjZA71jpGolmBDwadhv3MwmE1yXp7m6KkXI9IewOVBtp fe3LUuYNJ5xGue1MBXCTJp9zb/T20TZXNB5QTs+xcyLLJkS0V2F7IVZ7x3wM2telKu/OJKLW/ OJmQr/6R3peHpeOInm+WEKVcfl9r1h/TFKdRNKF3TdwCVE91DLsFRxTU74iwIoe4wdE3tsOqc /5Sy3Bn5zCgX3QynX4nOmSHKU0QwN01TjGjeSdPxBWB91qBizp0hNV54EFCFd60XaZtwo8BDf AGoSGevCIqQBCeIh/qg3ko0uQNeMQmgFUiAWjFCVMgPd+05NApoaynJNl1ukkh9zAeGvcn/Jo aJt27qhlvAQUUUJojExmz+YSqZ6e+CynU+xPKneluiaxq3boe0vcfDvBypPwhwhqbf14= 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: none client-ip=212.227.126.130; envelope-from=laurent@vivier.eu; helo=mout.kundenserver.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Laurent Vivier Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1637569603661100001 From: Richard Henderson Retain all 3 implementations of getdents for now. Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daud=C3=A9 Message-Id: <20211114103539.298686-2-richard.henderson@linaro.org> Signed-off-by: Laurent Vivier --- linux-user/syscall.c | 325 +++++++++++++++++++++++-------------------- 1 file changed, 172 insertions(+), 153 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 544f5b662ffe..a2f605dec4ca 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -8137,6 +8137,176 @@ static int host_to_target_cpu_mask(const unsigned l= ong *host_mask, return 0; } =20 +#ifdef TARGET_NR_getdents +static int do_getdents(abi_long arg1, abi_long arg2, abi_long arg3) +{ + int ret; + +#ifdef EMULATE_GETDENTS_WITH_GETDENTS +# if TARGET_ABI_BITS =3D=3D 32 && HOST_LONG_BITS =3D=3D 64 + struct target_dirent *target_dirp; + struct linux_dirent *dirp; + abi_long count =3D arg3; + + dirp =3D g_try_malloc(count); + if (!dirp) { + return -TARGET_ENOMEM; + } + + ret =3D get_errno(sys_getdents(arg1, dirp, count)); + if (!is_error(ret)) { + struct linux_dirent *de; + struct target_dirent *tde; + int len =3D ret; + int reclen, treclen; + int count1, tnamelen; + + count1 =3D 0; + de =3D dirp; + target_dirp =3D lock_user(VERIFY_WRITE, arg2, count, 0); + if (!target_dirp) { + return -TARGET_EFAULT; + } + tde =3D target_dirp; + while (len > 0) { + reclen =3D de->d_reclen; + tnamelen =3D reclen - offsetof(struct linux_dirent, d_name); + assert(tnamelen >=3D 0); + treclen =3D tnamelen + offsetof(struct target_dirent, d_name); + assert(count1 + treclen <=3D count); + tde->d_reclen =3D tswap16(treclen); + tde->d_ino =3D tswapal(de->d_ino); + tde->d_off =3D tswapal(de->d_off); + memcpy(tde->d_name, de->d_name, tnamelen); + de =3D (struct linux_dirent *)((char *)de + reclen); + len -=3D reclen; + tde =3D (struct target_dirent *)((char *)tde + treclen); + count1 +=3D treclen; + } + ret =3D count1; + unlock_user(target_dirp, arg2, ret); + } + g_free(dirp); +# else + struct linux_dirent *dirp; + abi_long count =3D arg3; + + dirp =3D lock_user(VERIFY_WRITE, arg2, count, 0); + if (!dirp) { + return -TARGET_EFAULT; + } + ret =3D get_errno(sys_getdents(arg1, dirp, count)); + if (!is_error(ret)) { + struct linux_dirent *de; + int len =3D ret; + int reclen; + de =3D dirp; + while (len > 0) { + reclen =3D de->d_reclen; + if (reclen > len) { + break; + } + de->d_reclen =3D tswap16(reclen); + tswapls(&de->d_ino); + tswapls(&de->d_off); + de =3D (struct linux_dirent *)((char *)de + reclen); + len -=3D reclen; + } + } + unlock_user(dirp, arg2, ret); +# endif +#else + /* Implement getdents in terms of getdents64 */ + struct linux_dirent64 *dirp; + abi_long count =3D arg3; + + dirp =3D lock_user(VERIFY_WRITE, arg2, count, 0); + if (!dirp) { + return -TARGET_EFAULT; + } + ret =3D get_errno(sys_getdents64(arg1, dirp, count)); + if (!is_error(ret)) { + /* + * Convert the dirent64 structs to target dirent. We do this + * in-place, since we can guarantee that a target_dirent is no + * larger than a dirent64; however this means we have to be + * careful to read everything before writing in the new format. + */ + struct linux_dirent64 *de; + struct target_dirent *tde; + int len =3D ret; + int tlen =3D 0; + + de =3D dirp; + tde =3D (struct target_dirent *)dirp; + while (len > 0) { + int namelen, treclen; + int reclen =3D de->d_reclen; + uint64_t ino =3D de->d_ino; + int64_t off =3D de->d_off; + uint8_t type =3D de->d_type; + + namelen =3D strlen(de->d_name); + treclen =3D offsetof(struct target_dirent, d_name) + namelen += 2; + treclen =3D QEMU_ALIGN_UP(treclen, sizeof(abi_long)); + + memmove(tde->d_name, de->d_name, namelen + 1); + tde->d_ino =3D tswapal(ino); + tde->d_off =3D tswapal(off); + tde->d_reclen =3D tswap16(treclen); + /* + * The target_dirent type is in what was formerly a padding + * byte at the end of the structure: + */ + *(((char *)tde) + treclen - 1) =3D type; + + de =3D (struct linux_dirent64 *)((char *)de + reclen); + tde =3D (struct target_dirent *)((char *)tde + treclen); + len -=3D reclen; + tlen +=3D treclen; + } + ret =3D tlen; + } + unlock_user(dirp, arg2, ret); +#endif + return ret; +} +#endif /* TARGET_NR_getdents */ + +#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64) +static int do_getdents64(abi_long arg1, abi_long arg2, abi_long arg3) +{ + struct linux_dirent64 *dirp; + abi_long count =3D arg3; + int ret; + + dirp =3D lock_user(VERIFY_WRITE, arg2, count, 0); + if (!dirp) { + return -TARGET_EFAULT; + } + ret =3D get_errno(sys_getdents64(arg1, dirp, count)); + if (!is_error(ret)) { + struct linux_dirent64 *de; + int len =3D ret; + int reclen; + de =3D dirp; + while (len > 0) { + reclen =3D de->d_reclen; + if (reclen > len) { + break; + } + de->d_reclen =3D tswap16(reclen); + tswap64s((uint64_t *)&de->d_ino); + tswap64s((uint64_t *)&de->d_off); + de =3D (struct linux_dirent64 *)((char *)de + reclen); + len -=3D reclen; + } + } + unlock_user(dirp, arg2, ret); + return ret; +} +#endif /* TARGET_NR_getdents64 */ + #if defined(TARGET_NR_pivot_root) && defined(__NR_pivot_root) _syscall2(int, pivot_root, const char *, new_root, const char *, put_old) #endif @@ -10227,162 +10397,11 @@ static abi_long do_syscall1(void *cpu_env, int n= um, abi_long arg1, #endif #ifdef TARGET_NR_getdents case TARGET_NR_getdents: -#ifdef EMULATE_GETDENTS_WITH_GETDENTS -#if TARGET_ABI_BITS =3D=3D 32 && HOST_LONG_BITS =3D=3D 64 - { - struct target_dirent *target_dirp; - struct linux_dirent *dirp; - abi_long count =3D arg3; - - dirp =3D g_try_malloc(count); - if (!dirp) { - return -TARGET_ENOMEM; - } - - ret =3D get_errno(sys_getdents(arg1, dirp, count)); - if (!is_error(ret)) { - struct linux_dirent *de; - struct target_dirent *tde; - int len =3D ret; - int reclen, treclen; - int count1, tnamelen; - - count1 =3D 0; - de =3D dirp; - if (!(target_dirp =3D lock_user(VERIFY_WRITE, arg2, count,= 0))) - return -TARGET_EFAULT; - tde =3D target_dirp; - while (len > 0) { - reclen =3D de->d_reclen; - tnamelen =3D reclen - offsetof(struct linux_dirent, d_= name); - assert(tnamelen >=3D 0); - treclen =3D tnamelen + offsetof(struct target_dirent, = d_name); - assert(count1 + treclen <=3D count); - tde->d_reclen =3D tswap16(treclen); - tde->d_ino =3D tswapal(de->d_ino); - tde->d_off =3D tswapal(de->d_off); - memcpy(tde->d_name, de->d_name, tnamelen); - de =3D (struct linux_dirent *)((char *)de + reclen); - len -=3D reclen; - tde =3D (struct target_dirent *)((char *)tde + treclen= ); - count1 +=3D treclen; - } - ret =3D count1; - unlock_user(target_dirp, arg2, ret); - } - g_free(dirp); - } -#else - { - struct linux_dirent *dirp; - abi_long count =3D arg3; - - if (!(dirp =3D lock_user(VERIFY_WRITE, arg2, count, 0))) - return -TARGET_EFAULT; - ret =3D get_errno(sys_getdents(arg1, dirp, count)); - if (!is_error(ret)) { - struct linux_dirent *de; - int len =3D ret; - int reclen; - de =3D dirp; - while (len > 0) { - reclen =3D de->d_reclen; - if (reclen > len) - break; - de->d_reclen =3D tswap16(reclen); - tswapls(&de->d_ino); - tswapls(&de->d_off); - de =3D (struct linux_dirent *)((char *)de + reclen); - len -=3D reclen; - } - } - unlock_user(dirp, arg2, ret); - } -#endif -#else - /* Implement getdents in terms of getdents64 */ - { - struct linux_dirent64 *dirp; - abi_long count =3D arg3; - - dirp =3D lock_user(VERIFY_WRITE, arg2, count, 0); - if (!dirp) { - return -TARGET_EFAULT; - } - ret =3D get_errno(sys_getdents64(arg1, dirp, count)); - if (!is_error(ret)) { - /* Convert the dirent64 structs to target dirent. We do t= his - * in-place, since we can guarantee that a target_dirent i= s no - * larger than a dirent64; however this means we have to be - * careful to read everything before writing in the new fo= rmat. - */ - struct linux_dirent64 *de; - struct target_dirent *tde; - int len =3D ret; - int tlen =3D 0; - - de =3D dirp; - tde =3D (struct target_dirent *)dirp; - while (len > 0) { - int namelen, treclen; - int reclen =3D de->d_reclen; - uint64_t ino =3D de->d_ino; - int64_t off =3D de->d_off; - uint8_t type =3D de->d_type; - - namelen =3D strlen(de->d_name); - treclen =3D offsetof(struct target_dirent, d_name) - + namelen + 2; - treclen =3D QEMU_ALIGN_UP(treclen, sizeof(abi_long)); - - memmove(tde->d_name, de->d_name, namelen + 1); - tde->d_ino =3D tswapal(ino); - tde->d_off =3D tswapal(off); - tde->d_reclen =3D tswap16(treclen); - /* The target_dirent type is in what was formerly a pa= dding - * byte at the end of the structure: - */ - *(((char *)tde) + treclen - 1) =3D type; - - de =3D (struct linux_dirent64 *)((char *)de + reclen); - tde =3D (struct target_dirent *)((char *)tde + treclen= ); - len -=3D reclen; - tlen +=3D treclen; - } - ret =3D tlen; - } - unlock_user(dirp, arg2, ret); - } -#endif - return ret; + return do_getdents(arg1, arg2, arg3); #endif /* TARGET_NR_getdents */ #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64) case TARGET_NR_getdents64: - { - struct linux_dirent64 *dirp; - abi_long count =3D arg3; - if (!(dirp =3D lock_user(VERIFY_WRITE, arg2, count, 0))) - return -TARGET_EFAULT; - ret =3D get_errno(sys_getdents64(arg1, dirp, count)); - if (!is_error(ret)) { - struct linux_dirent64 *de; - int len =3D ret; - int reclen; - de =3D dirp; - while (len > 0) { - reclen =3D de->d_reclen; - if (reclen > len) - break; - de->d_reclen =3D tswap16(reclen); - tswap64s((uint64_t *)&de->d_ino); - tswap64s((uint64_t *)&de->d_off); - de =3D (struct linux_dirent64 *)((char *)de + reclen); - len -=3D reclen; - } - } - unlock_user(dirp, arg2, ret); - } - return ret; + return do_getdents64(arg1, arg2, arg3); #endif /* TARGET_NR_getdents64 */ #if defined(TARGET_NR__newselect) case TARGET_NR__newselect: --=20 2.31.1