From patchwork Wed May 24 18:02:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 98651 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp111437vqr; Wed, 24 May 2023 11:49:50 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4jeYzUZEN1/jNCVEJBJeSfoMfUTVRGyVbUq/mT6YId89NJ3T0kXgOB5Do6D0fhVSV4ALdq X-Received: by 2002:a05:6a00:2d29:b0:62d:8376:3712 with SMTP id fa41-20020a056a002d2900b0062d83763712mr4457736pfb.28.1684954190322; Wed, 24 May 2023 11:49:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684954190; cv=none; d=google.com; s=arc-20160816; b=XV6sO+kTlDWNNAIyLT/YFXLwzzRVjBkKOyN8zvd+Uy84CFx6TewJ15Gai7N1tiqZNi Vmn5KCX+POOyKQXJlrn6Vpj1yEnk4AouuqUlBHjcqMzW3sekch5G2Thr1jvtUUn7sS/c AZ44HNyAvzT8PkY0POvucg4Ka8mOAKxButq+LlFGZbDU+PclJYREuAJkk8LgRCNfv+yo SuODgSXSFthx+SQIoiTDOFdCc9qy3u44vnhcoFdq0H9Wl1TbcoFrM5Lm4llc7593LSFa scYi6nvHA67xdTDfjJWLTS4P27iGsWnGzuw64vet/Wxw7YueFqC20FVH1+jOvPeEH3zc 0ivw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=AUo+IwcsEqkYN6G9evmPu5tCkV+wB2Q05NMkeV3YFoo=; b=Dwzb7TPRKlT03RqbKHxjVOeywzs8E3YdXxvSu8G340KlQHS9ay7Fxp0fOsEEOzlV8F 8ekn+k12Q+tv1GgeaRafDHppGQ3DSE7ZFNDmBUxsjg/u3ex3IfF61CtyR4nBx9BluydE rAUPP/NqXaNLFhaiutNXP33+wjreFQR6NqAmygcI75OtJtEkwebd33Kb2pKv99qRQK3c 4SKFmVRTSJjb71TlT9MyT5ul0qUUVMgCOyciLVT1ZGLSFfeZh9SsZARp6yVRUkoI7kS9 wfeaLV0Tqc7ft3ROQFbkPSGfY86iJbwtfXpd9HyXkuG0+YvwU2e+/y8y24G/nnwVzbxR uKgA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p9-20020a639509000000b005340840c0c8si2372834pgd.327.2023.05.24.11.49.38; Wed, 24 May 2023 11:49:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235865AbjEXSCe (ORCPT + 99 others); Wed, 24 May 2023 14:02:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236522AbjEXSCc (ORCPT ); Wed, 24 May 2023 14:02:32 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.155.67.158]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E189130; Wed, 24 May 2023 11:02:29 -0700 (PDT) X-QQ-mid: bizesmtp77t1684951344tml8b7ly Received: from linux-lab-host.localdomain ( [116.30.125.36]) by bizesmtp.qq.com (ESMTP) with id ; Thu, 25 May 2023 02:02:21 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: Xz3VOcA7Mr0KaqdkQOpzZ+GPBYPXJApKLC+NLroSL35n8g1DvXnk4MI0nnhSq eCA8abIJEj5/MMX0E0nOiq1r3tRW1Dzuj8kKIhMISCxuR2R+vSH+y9iUTtbM2YLkEZQFbl4 Gwj6XvvvAj46eYzyPG97x4/H3sFl0piFoY4Qhzz2TCtETlBg073vgdEKgIponO7WU5ihXTG 7USD/hZHC2KRfmWeEOarJtk8QF/O+zSJAue55HVToyJnWjy0g89EeUu6x7BvEne9Q56B8z+ H1qfJhUBHAXsIGNQEAnBLXLdMPINADkwqTscOIIy4rgBZdHW6p9iKZ9pgDu/AIDCanwCLue Hp87XNtP5SGMjLlCeXCoFJZ6Z7QZPlB5zQMCyidc1VKdP9xYKKKx3ND7Gh9AA== X-QQ-GoodBg: 0 X-BIZMAIL-ID: 6618908142304380197 From: Zhangjin Wu To: w@1wt.eu Cc: falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, palmer@dabbelt.com, paul.walmsley@sifive.com, thomas@t-8ch.de Subject: [PATCH 12/13] tools/nolibc: sys_wait4: riscv: use __NR_waitid for rv32 Date: Thu, 25 May 2023 02:02:17 +0800 Message-Id: <7a55e771c3cacf2e5b1a4750efac747289dbc54b.1684949268.git.falcon@tinylab.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrsz:qybglogicsvrsz3a-3 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766802524553588425?= X-GMAIL-MSGID: =?utf-8?q?1766802524553588425?= rv32 uses the generic include/uapi/asm-generic/unistd.h and it has no __NR_wait4 after kernel commit d4c08b9776b3 ("riscv: Use latest system call ABI"), use __NR_waitid instead. This code is based on sysdeps/unix/sysv/linux/wait4.c of glibc. Notes: The kernel wait4 syscall has the 'pid == INT_MIN' path and returns -ESRCH, but the kernel waitid syscall has no such path, to let this __NR_waitid based sys_wait4 has the same return value and pass the 'waitpid_min' test, we emulate such path in our new nolibc __NR_waitid branch. Signed-off-by: Zhangjin Wu --- tools/include/nolibc/sys.h | 54 ++++++++++++++++++++++++++++++++++++ tools/include/nolibc/types.h | 15 +++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 00c7197dcd50..2642b380c6aa 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -12,6 +12,7 @@ /* system includes */ #include +#include /* for siginfo_t */ #include /* for SIGCHLD */ #include #include @@ -1333,7 +1334,60 @@ int unlink(const char *path) static __attribute__((unused)) pid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage) { +#ifdef __NR_wait4 return my_syscall4(__NR_wait4, pid, status, options, rusage); +#elif defined(__NR_waitid) + siginfo_t infop; + int idtype = P_PID; + int ret; + + /* emulate the 'pid == INT_MIN' path of wait4 */ + if (pid == INT_MIN) + return -ESRCH; + + if (pid < -1) { + idtype = P_PGID; + pid *= -1; + } else if (pid == -1) { + idtype = P_ALL; + } else if (pid == 0) { + idtype = P_PGID; + } + + options |= WEXITED; + + ret = my_syscall5(__NR_waitid, idtype, pid, &infop, options, rusage); + if (ret < 0) + return ret; + + if (status) { + switch (infop.si_code) { + case CLD_EXITED: + *status = W_EXITCODE(infop.si_status, 0); + break; + case CLD_DUMPED: + *status = __WCOREFLAG | infop.si_status; + break; + case CLD_KILLED: + *status = infop.si_status; + break; + case CLD_TRAPPED: + case CLD_STOPPED: + *status = W_STOPCODE(infop.si_status); + break; + case CLD_CONTINUED: + *status = __W_CONTINUED; + break; + default: + *status = 0; + break; + } + } + + return infop.si_pid; +#else +#error Neither __NR_wait4 nor __NR_waitid defined, cannot implement sys_wait4() +#endif } static __attribute__((unused)) diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h index ee914391439c..c4f95c267607 100644 --- a/tools/include/nolibc/types.h +++ b/tools/include/nolibc/types.h @@ -92,8 +92,21 @@ #define WTERMSIG(status) ((status) & 0x7f) #define WIFSIGNALED(status) ((status) - 1 < 0xff) -/* waitpid() flags */ +/* waitpid() and waitid() flags */ #define WNOHANG 1 +#define WEXITED 0x00000004 + +/* first argument for waitid() */ +#define P_ALL 0 +#define P_PID 1 +#define P_PGID 2 +#define P_PIDFD 3 + +/* Macros used on waitid's status setting */ +#define W_EXITCODE(ret, sig) ((ret) << 8 | (sig)) +#define W_STOPCODE(sig) ((sig) << 8 | 0x7f) +#define __W_CONTINUED 0xffff +#define __WCOREFLAG 0x80 /* standard exit() codes */ #define EXIT_SUCCESS 0