From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: [musl] [PATCH] use new socket syscalls, fallback to socketcall
Date: Wed, 5 Aug 2020 22:18:47 -0400 [thread overview]
Message-ID: <20200806021844.GK6949@brightrain.aerifal.cx> (raw)
In-Reply-To: <20200804191506.GJ6949@brightrain.aerifal.cx>
[-- Attachment #1: Type: text/plain, Size: 2764 bytes --]
On Tue, Aug 04, 2020 at 03:15:06PM -0400, Rich Felker wrote:
> On Tue, Aug 04, 2020 at 08:56:42PM +0200, Markus Wichmann wrote:
> > On Tue, Aug 04, 2020 at 02:18:45PM -0400, Rich Felker wrote:
> > > Of these, only ppc, ppc64, sh, and possibly s390 (we only support
> > > 64-bit "s390x" and I'm not sure if it ever lacked the broken-down
> > > syscalls) are relevant. The rest are either unsupported by musl
> > > (including pre-EABI arm) or already using SYS_socketcall.
> >
> > According to musl source, all currently supported architectures have
> > __NR_socket (I didn't check the other calls; I just assumed that
> > __NR_socket was a stand-in for all the other ones).
> >
> > Therefore the required change can be performed by changing the
> > __socketcall macro (and __socketcall_cp of course). Something like this,
> > maybe? (If using GCC statement expressions is alright):
> >
> > #ifdef __NR_socketcall
> > #define __socketcall(nm, a, b, c, d, e, f) \
> > ({int r = __syscall(__NR_ ## nm, a, b, c, d, e, f); \
> > if (r == -ENOSYS) \
> > r = __syscall(__NR_socketcall, __SC_ ## nm, \
> > (long[6]){(long)a, (long) b, (long)c, (long)d, (long)e, (long) f});
> > r;})
> > #else
> > #define __socketcall(nm, a, b, c, d, e, f) __syscall(__NR_ ## nm, a, b, c, d, e, f)
> > #endif
>
> This would work, but we don't use statement-expressions in musl. I
> think an alternative with an inline function would be trivial, though:
>
> static inline long __socketcall(int sys, int sock, long a, long b, long c, long d, long e, long f)
> {
> long r = __syscall(sys, a, b, c, d, e, f);
> if (r != -ENOSYS) return r;
> return __syscall(SYS_socketcall, sock, ((long[6]){a, b, c, d, e, f}));
> }
> #define __socketcall(nm, a, b, c, d, e, f) __socketcall(SYS_##nm, __SC_##nm, \
> (long)(a), (long)(b), (long)(c), (long)(d), (long)(e), (long)(f))
>
> However it may (will?) end up including SYS_socketcall fallback code
> even on archs that never need it if SYS_socketcall is defined.
> Probably arch/*/syscall_arch.h should #undef SYS_socketcall if it's
> never needed. I'm not sure if there are any such archs.
>
> I kinda just thought of getting rid of the __socketcall abstraction,
> but indeed it looks like a lot of ugly boilerplate to duplicate across
> ~15 functions, so I think keeping it in src/internal/syscall.h makes
> the most sense.
OK, that roughly works. Special care is needed for SYS_accept since it
doesn't exist on i386, m68k, or s390x and needs to be emulated with
SYS_accept4. microblaze and mips (plain o32) seem to be the only archs
that define SYS_socketcall but don't ever need to use it.
Attached is a patch that seems to work implementing the above idea
with fixups. It's not heavily tested yet.
Rich
[-- Attachment #2: socketcall_fallback.diff --]
[-- Type: text/plain, Size: 3921 bytes --]
diff --git a/arch/i386/syscall_arch.h b/arch/i386/syscall_arch.h
index 69642e57..f92b7aa9 100644
--- a/arch/i386/syscall_arch.h
+++ b/arch/i386/syscall_arch.h
@@ -87,5 +87,3 @@ static inline long __syscall6(long n, long a1, long a2, long a3, long a4, long a
#define VDSO_CGT32_VER "LINUX_2.6"
#define VDSO_CGT_SYM "__vdso_clock_gettime64"
#define VDSO_CGT_VER "LINUX_2.6"
-
-#define SYSCALL_USE_SOCKETCALL
diff --git a/arch/m68k/syscall_arch.h b/arch/m68k/syscall_arch.h
index af79c306..6a9d0ae8 100644
--- a/arch/m68k/syscall_arch.h
+++ b/arch/m68k/syscall_arch.h
@@ -87,5 +87,4 @@ static inline long __syscall6(long n, long a, long b, long c, long d, long e, lo
return d0;
}
-#define SYSCALL_USE_SOCKETCALL
#define SYSCALL_IPC_BROKEN_MODE
diff --git a/arch/microblaze/syscall_arch.h b/arch/microblaze/syscall_arch.h
index 169013f8..61d8248e 100644
--- a/arch/microblaze/syscall_arch.h
+++ b/arch/microblaze/syscall_arch.h
@@ -95,3 +95,5 @@ static inline long __syscall6(long n, long a, long b, long c, long d, long e, lo
}
#define SYSCALL_IPC_BROKEN_MODE
+
+#undef SYS_socketcall
diff --git a/arch/mips/syscall_arch.h b/arch/mips/syscall_arch.h
index 380a94b3..5b7c38de 100644
--- a/arch/mips/syscall_arch.h
+++ b/arch/mips/syscall_arch.h
@@ -149,3 +149,5 @@ static inline long __syscall7(long n, long a, long b, long c, long d, long e, lo
#define SO_SNDTIMEO_OLD 0x1005
#define SO_RCVTIMEO_OLD 0x1006
+
+#undef SYS_socketcall
diff --git a/arch/s390x/syscall_arch.h b/arch/s390x/syscall_arch.h
index afb99852..83cc9a27 100644
--- a/arch/s390x/syscall_arch.h
+++ b/arch/s390x/syscall_arch.h
@@ -72,5 +72,3 @@ static inline long __syscall6(long n, long a, long b, long c, long d, long e, lo
register long r7 __asm__("r7") = f;
__asm_syscall("+r"(r2), "r"(r1), "r"(r3), "r"(r4), "r"(r5), "r"(r6), "r"(r7));
}
-
-#define SYSCALL_USE_SOCKETCALL
diff --git a/src/internal/syscall.h b/src/internal/syscall.h
index 975a0031..bd073c1a 100644
--- a/src/internal/syscall.h
+++ b/src/internal/syscall.h
@@ -2,6 +2,7 @@
#define _INTERNAL_SYSCALL_H
#include <features.h>
+#include <errno.h>
#include <sys/syscall.h>
#include "syscall_arch.h"
@@ -57,15 +58,23 @@ hidden long __syscall_ret(unsigned long),
#define __syscall_cp(...) __SYSCALL_DISP(__syscall_cp,__VA_ARGS__)
#define syscall_cp(...) __syscall_ret(__syscall_cp(__VA_ARGS__))
-#ifndef SYSCALL_USE_SOCKETCALL
-#define __socketcall(nm,a,b,c,d,e,f) __syscall(SYS_##nm, a, b, c, d, e, f)
-#define __socketcall_cp(nm,a,b,c,d,e,f) __syscall_cp(SYS_##nm, a, b, c, d, e, f)
+static inline long __alt_socketcall(int sys, int sock, int cp, long a, long b, long c, long d, long e, long f)
+{
+ long r;
+ if (cp) r = __syscall_cp(sys, a, b, c, d, e, f);
+ else r = __syscall(sys, a, b, c, d, e, f);
+ if (r != -ENOSYS) return r;
+#ifndef SYS_socketcall
+ return r;
#else
-#define __socketcall(nm,a,b,c,d,e,f) __syscall(SYS_socketcall, __SC_##nm, \
- ((long [6]){ (long)a, (long)b, (long)c, (long)d, (long)e, (long)f }))
-#define __socketcall_cp(nm,a,b,c,d,e,f) __syscall_cp(SYS_socketcall, __SC_##nm, \
- ((long [6]){ (long)a, (long)b, (long)c, (long)d, (long)e, (long)f }))
+ if (cp) return __syscall_cp(SYS_socketcall, sock, ((long[6]){a, b, c, d, e, f}));
+ else return __syscall(SYS_socketcall, sock, ((long[6]){a, b, c, d, e, f}));
#endif
+}
+#define __socketcall(nm, a, b, c, d, e, f) __alt_socketcall(SYS_##nm, __SC_##nm, 0, \
+ (long)(a), (long)(b), (long)(c), (long)(d), (long)(e), (long)(f))
+#define __socketcall_cp(nm, a, b, c, d, e, f) __alt_socketcall(SYS_##nm, __SC_##nm, 1, \
+ (long)(a), (long)(b), (long)(c), (long)(d), (long)(e), (long)(f))
/* fixup legacy 16-bit junk */
@@ -338,6 +347,10 @@ hidden long __syscall_ret(unsigned long),
#define __SC_recvmmsg 19
#define __SC_sendmmsg 20
+#ifndef SYS_accept
+#define SYS_accept SYS_accept4
+#endif
+
#ifndef SO_RCVTIMEO_OLD
#define SO_RCVTIMEO_OLD 20
#endif
prev parent reply other threads:[~2020-08-06 2:19 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-03 7:41 [musl] Add SYSCALL_USE_SOCKETCALL for old arch David Timber
2020-08-03 13:33 ` Rich Felker
[not found] ` <CAGYH49BV-SG0THP2kzbSVVRypkk0APF1MQb74geskos=dDLv6A@mail.gmail.com>
[not found] ` <CAGYH49Dd6qMK0pL+eGBzMZPxBqoZhRRfejnsvkkSsN+xcw4njg@mail.gmail.com>
[not found] ` <20200803141513.GD6949@brightrain.aerifal.cx>
[not found] ` <CAGYH49AEpS9_ndXP=x20Qpj7tx0RBEyt+iB5nEjD4odUW02wiQ@mail.gmail.com>
2020-08-04 18:18 ` Rich Felker
2020-08-04 18:56 ` Markus Wichmann
2020-08-04 19:15 ` Rich Felker
2020-08-06 2:18 ` Rich Felker [this message]
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=20200806021844.GK6949@brightrain.aerifal.cx \
--to=dalias@libc.org \
--cc=musl@lists.openwall.com \
/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.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
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 NNTP newsgroup(s).