From patchwork Sun Jun 4 05:34:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 102944 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1987139vqr; Sat, 3 Jun 2023 22:42:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4NpYWUgQDfqKEXRB/gDSnJCpnT/gBrwF0k5r0B52PEPLlE7UUyWgdGbxnSTbfXyVLIQD1l X-Received: by 2002:a25:874a:0:b0:ba8:7122:2917 with SMTP id e10-20020a25874a000000b00ba871222917mr9323970ybn.0.1685857372065; Sat, 03 Jun 2023 22:42:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685857372; cv=none; d=google.com; s=arc-20160816; b=sIBdgaXZvYDhomNSg6l1KwLZLRTfMyoVc7fa86AbH7Ngf/riYaXHdEA2oHJAAMtdwu FFmfQa58mmPR39YsPEFyxjui/GI2LzhcVMNdkBRPKowB5PAT3ZzAq9JUNZ/zF6kI42Nz DrxtQgp2wYy2IkJfhN9/ESczkcY8IqNGAF5I7emIhbhr9XqPSlkccu8zwj9TMKoQx4z4 ebSbPIFIqTnPOuHcAwt/hK0xtPgY5Fu2JmNgaDvE/E5h0fFotvKweKOWnIycPGAKYs2k oZbHBFDcBKBB8LtCp/0P0KoGEVPZYReATzASYivpjy6wCudZI9YkQHfHZMhj9WFHLXGa FEvA== 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=hhlAESCkobmzUn008FNagAcuFwvqA/T6Fg/nWzHPFa8=; b=BLVTmlNpZF4Lpmo7XPleIDkKOUqOMQJDBFSEvrtBt+p7U306r1n5bphaFSY5vTVTqk zlVG1WyFW5yELi1td2mRLb/waHQbfU2nHLWdXvXJU48He6KLLaIVOgoqPrjAwXlOBzte MrSow3KLQyDEUKN3k/MptpwW11Aaq7A2yMlBMtKjhMIQ7ifIE8R8e5JEkTtDu16bsaTr agr5jtYVUCKSeplb9E/MWnENAJw4Qe7MQuF9ZhxRzpuK25GfgqWiW2qN8cHqNTcDU16m bKW0hC8AwoXq6+koZdK7nOz/dRinSDQPARO9VpoVrvLZtE84InMh77dA/0WlqEETBWhO u2dQ== 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 x15-20020a170902ec8f00b001a95babe3c0si3706977plg.371.2023.06.03.22.42.40; Sat, 03 Jun 2023 22:42:52 -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 S229799AbjFDFf3 (ORCPT + 99 others); Sun, 4 Jun 2023 01:35:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229462AbjFDFfZ (ORCPT ); Sun, 4 Jun 2023 01:35:25 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.154.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B85FCE; Sat, 3 Jun 2023 22:35:23 -0700 (PDT) X-QQ-mid: bizesmtp83t1685856912tz2mpqb9 Received: from linux-lab-host.localdomain ( [61.141.77.49]) by bizesmtp.qq.com (ESMTP) with id ; Sun, 04 Jun 2023 13:35:11 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: rZJGTgY0+YN6Hgm0UEoWeI0raRxwz+OClFuVPySOxGuRMnsmCqEXyxefSu3x0 2WU1GagREmkvIIMdLAcKZPPfzXOjtPC6QHE3ohpmFo0EwDu8CwIBqVcnio0loP9RgMSMEle vfF2rNJrqkaUHui8aQBWgK5qof0qpL61KoggBuPyvGXjWlRfC+JUz68VKJvriGEPu22kSbc RqLABcHytpqcY77XZL3DpY4doFeZYFXTqw9icVDCrprILlhn+oHUh/o89uCtTCHgHw1Z4/g qH5mWGlhL8RN+5kTi7TjkC8GBo1nfTwYXvNmegQ1933fDtiBVCTz03KNUfGCKHNsjcRg/aK RsT2h465n546UCWpcxgDIIFqF9rZUXUyBTxm19I3slRZRZGF2/hpk36ULoPt4VGRjQLLtme X-QQ-GoodBg: 0 X-BIZMAIL-ID: 350183426812210734 From: Zhangjin Wu To: w@1wt.eu Cc: falcon@tinylab.org, arnd@arndb.de, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de Subject: [PATCH 1/4] tools/nolibc: unistd.h: add __syscall() and __syscall_ret() helpers Date: Sun, 4 Jun 2023 13:34:29 +0800 Message-Id: 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_MSPIKE_H3, RCVD_IN_MSPIKE_WL,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?1767749579395308002?= X-GMAIL-MSGID: =?utf-8?q?1767749579395308002?= most of the library routines share the same code model, let's add some macros to simplify the coding and shrink the code lines too. One added for syscall return, one added for syscall call, both of them can get the typeof 'return value' automatically. To get the return type of syscalls, __auto_type is better than typeof(), but it is not supported by the old compilers (before 2013, see [1]), so, use typeof() here. [1]: https://gcc.gnu.org/legacy-ml/gcc-patches/2013-11/msg01378.html Signed-off-by: Zhangjin Wu --- tools/include/nolibc/sys.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 1d6f33f58629..937a8578e3d4 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -28,6 +28,21 @@ #include "errno.h" #include "types.h" +/* Syscall call and return helpers */ +#define __syscall_ret(ret) \ +({ \ + if (ret < 0) { \ + SET_ERRNO(-ret); \ + ret = (typeof(ret))-1; \ + } \ + ret; \ +}) + +#define __syscall(name, ...) \ +({ \ + typeof(sys_##name(__VA_ARGS__)) ret = sys_##name(__VA_ARGS__); \ + __syscall_ret(ret); \ +}) /* Functions in this file only describe syscalls. They're declared static so * that the compiler usually decides to inline them while still being allowed