From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 0FAA03858C50 for ; Mon, 17 Apr 2023 16:00:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0FAA03858C50 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681747249; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=t5qy7/Qafi3W550VgwY+1VnKQbGKiDFc+PaxQg+CJLE=; b=QQApWps5CqyBva61WNEsvzbU0FaSXGr+Qght0m1c8OeAsw65C4Ptoz/8mIhBKTk7CUMQ1h 5iGR3Kf7rgY4dB2cLaqIIAk6GBbQwKUnMFg42xqXiywGEkOckTCd1gHNFtsVpgk/CJVrtT pMaU8Q9HsqjNIqWpiRAI+eLDO2QsNCE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-477-KkEtNsKONLWKN9Y2FzMD2Q-1; Mon, 17 Apr 2023 12:00:45 -0400 X-MC-Unique: KkEtNsKONLWKN9Y2FzMD2Q-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E14A3101A552; Mon, 17 Apr 2023 16:00:44 +0000 (UTC) Received: from oak (unknown [10.22.34.175]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C5D5A492B0C; Mon, 17 Apr 2023 16:00:44 +0000 (UTC) Date: Mon, 17 Apr 2023 12:00:43 -0400 From: Joe Simmons-Talbott To: "H.J. Lu" Cc: libc-alpha@sourceware.org Subject: Re: [PATCH v3 1/2] x86_64: Set the syscall register right before doing the syscall. Message-ID: <20230417160043.GC2106608@oak> References: <20230411133004.2268170-1-josimmon@redhat.com> <20230417153451.1450817-1-josimmon@redhat.com> <20230417153451.1450817-2-josimmon@redhat.com> MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP,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 server2.sourceware.org List-Id: On Mon, Apr 17, 2023 at 08:54:17AM -0700, H.J. Lu wrote: > On Mon, Apr 17, 2023 at 8:35 AM Joe Simmons-Talbott via Libc-alpha > wrote: > > > > To make identifying syscalls easier during call tree analysis load the > > syscall number just before performing the syscall. > > > > Compiler optimizations can place quite a few instructions between the > > setting of the syscall number and the syscall instruction. During call > > tree analysis the number of instructions between the two can lead to > > more difficulty for both tools and humans in properly identifying the > > syscall number. Having the syscall number set in the prior instruction > > to the syscall instruction makes this task easier and less error prone. > > Being able to reliably identify syscalls made by a given API will make > > it easier to understand and verify the safety and security of glibc. > > --- > > sysdeps/unix/sysv/linux/x86_64/sysdep.h | 33 +++++++++++++++++++++++++ > > 1 file changed, 33 insertions(+) > > > > diff --git a/sysdeps/unix/sysv/linux/x86_64/sysdep.h b/sysdeps/unix/sysv/linux/x86_64/sysdep.h > > index cfb51be8c5..800a56723f 100644 > > --- a/sysdeps/unix/sysv/linux/x86_64/sysdep.h > > +++ b/sysdeps/unix/sysv/linux/x86_64/sysdep.h > > @@ -250,12 +250,20 @@ > > (long int) resultvar; \ > > }) > > > > +#define MSTR_HELPER(x) #x > > +#define MSTR(x) MSTR_HELPER(x) > > + > > #undef internal_syscall1 > > #define internal_syscall1(number, arg1) \ > > ({ \ > > unsigned long int resultvar; \ > > TYPEFY (arg1, __arg1) = ARGIFY (arg1); \ > > register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \ > > + if (__builtin_constant_p(number)) \ > > + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ > > + : /* no outputs */ \ > > + : "i" (number) \ > > + : "eax"); \ > > asm volatile ( \ > > "syscall\n\t" \ > > : "=a" (resultvar) \ > > @@ -272,6 +280,11 @@ > > TYPEFY (arg1, __arg1) = ARGIFY (arg1); \ > > register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \ > > register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \ > > + if (__builtin_constant_p(number)) \ > > + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ > > + : /* no outputs */ \ > > + : "i" (number) \ > > + : "eax"); \ > > asm volatile ( \ > > "syscall\n\t" \ > > : "=a" (resultvar) \ > > @@ -290,6 +303,11 @@ > > register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \ > > register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \ > > register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \ > > + if (__builtin_constant_p(number)) \ > > + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ > > + : /* no outputs */ \ > > + : "i" (number) \ > > + : "eax"); \ > > asm volatile ( \ > > "syscall\n\t" \ > > : "=a" (resultvar) \ > > @@ -310,6 +328,11 @@ > > register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \ > > register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \ > > register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \ > > + if (__builtin_constant_p(number)) \ > > + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ > > + : /* no outputs */ \ > > + : "i" (number) \ > > + : "eax"); \ > > asm volatile ( \ > > "syscall\n\t" \ > > : "=a" (resultvar) \ > > @@ -332,6 +355,11 @@ > > register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \ > > register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \ > > register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \ > > + if (__builtin_constant_p(number)) \ > > + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ > > + : /* no outputs */ \ > > + : "i" (number) \ > > + : "eax"); \ > > asm volatile ( \ > > "syscall\n\t" \ > > : "=a" (resultvar) \ > > @@ -357,6 +385,11 @@ > > register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \ > > register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \ > > register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \ > > + if (__builtin_constant_p(number)) \ > > + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ > > + : /* no outputs */ \ > > + : "i" (number) \ > > + : "eax"); \ > > asm volatile ( \ > > "syscall\n\t" \ > > : "=a" (resultvar) \ > > -- > > 2.39.2 > > > > Won't the compiler load EAX twice when number is a constant? > Yes. I'll have a new version combining the two asm sections into one like for aarch64 soon. Joe