From patchwork Mon Dec 12 20:30:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 61848 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9188F3875B40 for ; Mon, 12 Dec 2022 20:34:24 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) by sourceware.org (Postfix) with ESMTPS id DA6D2384C34D for ; Mon, 12 Dec 2022 20:31:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DA6D2384C34D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f49.google.com with SMTP id h16so4491560wrz.12 for ; Mon, 12 Dec 2022 12:31:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ReM/lD5F5SDiz/ITjuWS0bKvAGMCIsLCn89EKNpsxlg=; b=aiKOirBGsUdwnD5YF3YYqNDHMg3z+c0iRxRtxhsuRyu99hpfCo+JNLliQmW9THjZmU mbmzfR/XPimxlpxWIFwD1/cGCirhsIX49NBZ/qpfrfj5baEjQqhhrFVwLgUDKGY4dyKz wKmrSBk6YS10Ox8xqq5H6NLazMuxk3XRBA/Wd4L94h4hHDbv4t7nf6dq9txLpxF8utyI j4BZRzHzY6VMl1Psp4Fn4y6YYH2l0cNEdmnj8/fGnisPoGxwEbQXEwW5c8yAgnvrCzuq S12PmzJYTUijgnMaEhOP8BCrwaQ2i40c93+QarxKwmVcUHZ9ehFTr523U2QlWXUjTcxP ohtQ== X-Gm-Message-State: ANoB5pn3anOAjCuNC4qgnZwYoAIYQtH2KH5POcU+RsiBH6/C2cJRVJ4u X7LhC07ae4L+cWAa4DqeSFuYlvTt60MUZA== X-Google-Smtp-Source: AA0mqf4rgum350JZW8v+Pe3sdKSCQhx7hLmRU+sJYUauSLov3Y7ZLPc9+LcTO2iDwFBxYZl8i7TdTA== X-Received: by 2002:a5d:5f07:0:b0:250:22e4:b89e with SMTP id cl7-20020a5d5f07000000b0025022e4b89emr2950330wrb.65.1670877093308; Mon, 12 Dec 2022 12:31:33 -0800 (PST) Received: from localhost ([2001:8a0:f912:6700:afd9:8b6d:223f:6170]) by smtp.gmail.com with ESMTPSA id v1-20020a5d6781000000b002422b462975sm9599181wru.34.2022.12.12.12.31.32 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 12 Dec 2022 12:31:33 -0800 (PST) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 26/31] gdb/testsuite/lib/my-syscalls.S: Refactor new SYSCALL macro Date: Mon, 12 Dec 2022 20:30:56 +0000 Message-Id: <20221212203101.1034916-27-pedro@palves.net> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20221212203101.1034916-1-pedro@palves.net> References: <20221212203101.1034916-1-pedro@palves.net> MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Refactor the syscall assembly code in gdb/testsuite/lib/my-syscalls.S behind a SYSCALL macro so that it's easy to add new syscalls without duplicating code. Note that the way the macro is implemented, it only works correctly for syscalls with up to 3 arguments, and, if the syscall doesn't return (the macro doesn't bother to save/restore callee-saved registers). The following patch will want to use the macro to define a wrapper for the "exit" syscall, so the limitations continue to be sufficient. Change-Id: I8acf1463b11a084d6b4579aaffb49b5d0dea3bba Reviewed-By: Andrew Burgess --- gdb/testsuite/lib/my-syscalls.S | 50 +++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/gdb/testsuite/lib/my-syscalls.S b/gdb/testsuite/lib/my-syscalls.S index 7f06f9c398b..6fb53624f31 100644 --- a/gdb/testsuite/lib/my-syscalls.S +++ b/gdb/testsuite/lib/my-syscalls.S @@ -21,38 +21,52 @@ #include -/* int my_execve (const char *file, char *argv[], char *envp[]); */ - -.global my_execve -my_execve: +/* The SYSCALL macro below current supports calling syscalls with up + to 3 arguments, and, assumes the syscall never returns, like exec + and exit. If you need to call syscalls with more arguments or you + need to call syscalls that actually return, you'll need to update + the macros. We don't bother with optimizing setting up fewer + arguments for syscalls that take fewer arguments, as we're not + optimizating for speed or space, but for maintainability. */ #if defined(__x86_64__) - mov $__NR_execve, %rax - /* rdi, rsi and rdx already contain the right arguments. */ -my_execve_syscall: - syscall - ret +#define SYSCALL(NAME, NR) \ +.global NAME ;\ +NAME: ;\ + mov $NR, %rax ;\ + /* rdi, rsi and rdx already contain the right arguments. */ \ +NAME ## _syscall: ;\ + syscall ;\ + ret ; #elif defined(__i386__) - mov $__NR_execve, %eax - mov 4(%esp), %ebx - mov 8(%esp), %ecx - mov 12(%esp), %edx -my_execve_syscall: - int $0x80 +#define SYSCALL(NAME, NR) \ +.global NAME ;\ +NAME: ;\ + mov $NR, %eax ;\ + mov 4(%esp), %ebx ;\ + mov 8(%esp), %ecx ;\ + mov 12(%esp), %edx ;\ +NAME ## _syscall: ;\ + int $0x80 ;\ ret #elif defined(__aarch64__) - mov x8, #__NR_execve - /* x0, x1 and x2 already contain the right arguments. */ -my_execve_syscall: +#define SYSCALL(NAME, NR) \ +.global NAME ;\ +NAME: ;\ + mov x8, NR ;\ + /* x0, x1 and x2 already contain the right arguments. */ \ +NAME ## _syscall: ;\ svc #0 #else # error "Unsupported architecture" #endif +SYSCALL (my_execve, __NR_execve) + .section .note.GNU-stack,"",@progbits