From: "H.J. Lu" <hjl.tools@gmail.com>
To: Joe Simmons-Talbott <josimmon@redhat.com>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH v3 1/2] x86_64: Set the syscall register right before doing the syscall.
Date: Mon, 17 Apr 2023 11:38:24 -0700 [thread overview]
Message-ID: <CAMe9rOoOfvd0gPUvM2Th2FMOvZcaMmui22OC80G4toVoBisNtg@mail.gmail.com> (raw)
In-Reply-To: <20230417160043.GC2106608@oak>
[-- Attachment #1: Type: text/plain, Size: 7331 bytes --]
On Mon, Apr 17, 2023 at 9:00 AM Joe Simmons-Talbott <josimmon@redhat.com> wrote:
>
> 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
> > <libc-alpha@sourceware.org> 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
>
There is no need for __builtin_constant_p. Please try this.
--
H.J.
[-- Attachment #2: syscall.patch --]
[-- Type: text/x-patch, Size: 4217 bytes --]
diff --git a/sysdeps/unix/sysv/linux/x86_64/sysdep.h b/sysdeps/unix/sysv/linux/x86_64/sysdep.h
index cfb51be8c5..58975bdf3f 100644
--- a/sysdeps/unix/sysv/linux/x86_64/sysdep.h
+++ b/sysdeps/unix/sysv/linux/x86_64/sysdep.h
@@ -243,9 +243,9 @@
({ \
unsigned long int resultvar; \
asm volatile ( \
- "syscall\n\t" \
+ "mov %1, %k0\n\tsyscall\n\t" \
: "=a" (resultvar) \
- : "0" (number) \
+ : "g" (number) \
: "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
(long int) resultvar; \
})
@@ -257,9 +257,9 @@
TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
asm volatile ( \
- "syscall\n\t" \
+ "mov %1, %k0\n\tsyscall\n\t" \
: "=a" (resultvar) \
- : "0" (number), "r" (_a1) \
+ : "g" (number), "r" (_a1) \
: "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
(long int) resultvar; \
})
@@ -273,9 +273,9 @@
register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
asm volatile ( \
- "syscall\n\t" \
+ "mov %1, %k0\n\tsyscall\n\t" \
: "=a" (resultvar) \
- : "0" (number), "r" (_a1), "r" (_a2) \
+ : "g" (number), "r" (_a1), "r" (_a2) \
: "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
(long int) resultvar; \
})
@@ -291,9 +291,9 @@
register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
asm volatile ( \
- "syscall\n\t" \
+ "mov %1, %k0\n\tsyscall\n\t" \
: "=a" (resultvar) \
- : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3) \
+ : "g" (number), "r" (_a1), "r" (_a2), "r" (_a3) \
: "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
(long int) resultvar; \
})
@@ -311,9 +311,9 @@
register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
asm volatile ( \
- "syscall\n\t" \
+ "mov %1, %k0\n\tsyscall\n\t" \
: "=a" (resultvar) \
- : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4) \
+ : "g" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4) \
: "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
(long int) resultvar; \
})
@@ -333,9 +333,9 @@
register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
asm volatile ( \
- "syscall\n\t" \
+ "mov %1, %k0\n\tsyscall\n\t" \
: "=a" (resultvar) \
- : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4), \
+ : "g" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4), \
"r" (_a5) \
: "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
(long int) resultvar; \
@@ -358,9 +358,9 @@
register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
asm volatile ( \
- "syscall\n\t" \
+ "mov %1, %k0\n\tsyscall\n\t" \
: "=a" (resultvar) \
- : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4), \
+ : "g" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4), \
"r" (_a5), "r" (_a6) \
: "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
(long int) resultvar; \
diff --git a/sysdeps/x86_64/nptl/tls.h b/sysdeps/x86_64/nptl/tls.h
index 1403f939f7..f206b11295 100644
--- a/sysdeps/x86_64/nptl/tls.h
+++ b/sysdeps/x86_64/nptl/tls.h
@@ -149,9 +149,9 @@ _Static_assert (offsetof (tcbhead_t, __glibc_unused2) == 0x80,
_head->self = _thrdescr; \
\
/* It is a simple syscall to set the %fs value for the thread. */ \
- asm volatile ("syscall" \
+ asm volatile ("mov %1, %k0\n\tsyscall" \
: "=a" (_result) \
- : "0" ((unsigned long int) __NR_arch_prctl), \
+ : "i" (__NR_arch_prctl), \
"D" ((unsigned long int) ARCH_SET_FS), \
"S" (_thrdescr) \
: "memory", "cc", "r11", "cx"); \
next prev parent reply other threads:[~2023-04-17 18:39 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-11 13:30 [PATCH 0/2] x86_64: aarch64: Set call number just before syscall Joe Simmons-Talbott
2023-04-11 13:30 ` [PATCH 1/2] x86_64: Set the syscall register right before doing the syscall Joe Simmons-Talbott
2023-04-17 22:35 ` Noah Goldstein
2023-04-17 22:36 ` Noah Goldstein
2023-04-11 13:30 ` [PATCH 2/2] aarch64: " Joe Simmons-Talbott
2023-04-11 13:50 ` Florian Weimer
2023-04-11 14:15 ` Adhemerval Zanella Netto
2023-04-11 15:43 ` Szabolcs Nagy
2023-04-11 16:03 ` Florian Weimer
2023-04-11 16:39 ` Adhemerval Zanella Netto
2023-04-12 15:27 ` Joe Simmons-Talbott
2023-04-12 21:11 ` [PATCH v2 0/2] x86_64: aarch64: Set call number just before syscall Joe Simmons-Talbott
2023-04-12 21:11 ` [PATCH v2 1/2] x86_64: Set the syscall register right before doing the syscall Joe Simmons-Talbott
2023-04-12 21:11 ` [PATCH v2 2/2] aarch64: " Joe Simmons-Talbott
2023-04-17 15:34 ` [PATCH v3 0/2] x86_64: aarch64: Set call number just before syscall Joe Simmons-Talbott
2023-04-17 15:34 ` [PATCH v3 1/2] x86_64: Set the syscall register right before doing the syscall Joe Simmons-Talbott
2023-04-17 15:54 ` H.J. Lu
2023-04-17 16:00 ` Joe Simmons-Talbott
2023-04-17 18:38 ` H.J. Lu [this message]
2023-04-17 15:34 ` [PATCH v3 2/2] aarch64: " Joe Simmons-Talbott
2023-04-17 21:20 ` [PATCH v4 0/2] x86_64: aarch64: Set call number just before syscall Joe Simmons-Talbott
2023-04-17 21:20 ` [PATCH v4 1/2] x86_64: Set the syscall register right before doing the syscall Joe Simmons-Talbott
2023-04-17 21:20 ` [PATCH v4 2/2] aarch64: " Joe Simmons-Talbott
2023-04-18 12:57 ` Szabolcs Nagy
2023-04-18 19:33 ` Joe Simmons-Talbott
2023-04-19 13:58 ` [PATCH v5 0/3] x86_64: aarch64: Set call number just before syscall Joe Simmons-Talbott
2023-04-19 13:58 ` [PATCH v5 1/3] x86_64: Set the syscall register right before doing the syscall Joe Simmons-Talbott
2023-04-19 15:35 ` H.J. Lu
2023-04-19 15:48 ` Joe Simmons-Talbott
2023-04-19 13:58 ` [PATCH v5 2/3] aarch64: " Joe Simmons-Talbott
2023-04-19 14:56 ` Szabolcs Nagy
2023-04-19 15:21 ` Joe Simmons-Talbott
2023-04-19 13:58 ` [PATCH v5 3/3] nptl: Use direct syscall numbers in setxid Joe Simmons-Talbott
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAMe9rOoOfvd0gPUvM2Th2FMOvZcaMmui22OC80G4toVoBisNtg@mail.gmail.com \
--to=hjl.tools@gmail.com \
--cc=josimmon@redhat.com \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).