git: 77b2c2f81451 - main - Add sched_getcpu()

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 10 Nov 2021 19:34:22 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=77b2c2f81451db8119e4ea6398fe76813db790de

commit 77b2c2f81451db8119e4ea6398fe76813db790de
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2021-10-22 15:35:53 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2021-11-10 19:18:54 +0000

    Add sched_getcpu()
    
    for compatibility with Linux.
    
    Reviewed by:    jhb
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D32901
---
 include/sched.h                        |  1 +
 lib/libc/aarch64/sys/Makefile.inc      |  3 ++-
 lib/libc/arm/sys/Makefile.inc          |  3 ++-
 lib/libc/gen/Symbol.map                |  1 +
 lib/libc/gen/sched_getcpu_gen.c        | 36 ++++++++++++++++++++++++++++++++++
 lib/libc/include/libc_private.h        |  1 +
 lib/libc/mips/sys/Makefile.inc         |  3 ++-
 lib/libc/powerpc/sys/Makefile.inc      |  4 +++-
 lib/libc/powerpc64/sys/Makefile.inc    |  4 +++-
 lib/libc/riscv/sys/Makefile.inc        |  3 ++-
 lib/libc/sys/Makefile.inc              |  3 +++
 lib/libc/x86/sys/Makefile.inc          |  3 ++-
 sys/compat/freebsd32/capabilities.conf |  1 +
 sys/compat/freebsd32/syscalls.master   |  2 +-
 sys/kern/kern_synch.c                  |  7 +++++++
 sys/kern/syscalls.master               |  3 +++
 16 files changed, 70 insertions(+), 8 deletions(-)

diff --git a/include/sched.h b/include/sched.h
index 531ecb1bb9a3..1c41cda79046 100644
--- a/include/sched.h
+++ b/include/sched.h
@@ -47,6 +47,7 @@ __BEGIN_DECLS
 #if __BSD_VISIBLE
 int sched_getaffinity(pid_t pid, size_t cpusetsz, cpuset_t *cpuset);
 int sched_setaffinity(int pid, size_t cpusetsz, const cpuset_t *cpuset);
+int sched_getcpu(void);
 #endif /* __BSD_VISIBLE */
 __END_DECLS
 
diff --git a/lib/libc/aarch64/sys/Makefile.inc b/lib/libc/aarch64/sys/Makefile.inc
index 623a06b7faf8..97aff8ba92fe 100644
--- a/lib/libc/aarch64/sys/Makefile.inc
+++ b/lib/libc/aarch64/sys/Makefile.inc
@@ -2,7 +2,8 @@
 
 MIASM:=	${MIASM:Nfreebsd[467]_*}
 
-SRCS+=	__vdso_gettc.c
+SRCS+=	__vdso_gettc.c \
+	sched_getcpu_gen.c
 
 MDASM=	cerror.S \
 	syscall.S \
diff --git a/lib/libc/arm/sys/Makefile.inc b/lib/libc/arm/sys/Makefile.inc
index be6a58f8aaf5..c6e854e7954a 100644
--- a/lib/libc/arm/sys/Makefile.inc
+++ b/lib/libc/arm/sys/Makefile.inc
@@ -1,6 +1,7 @@
 # $FreeBSD$
 
-SRCS+=	__vdso_gettc.c
+SRCS+=	__vdso_gettc.c \
+	sched_getcpu_gen.c
 
 MDASM= Ovfork.S cerror.S syscall.S
 
diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map
index a4630749ae7e..b6c3c252be7c 100644
--- a/lib/libc/gen/Symbol.map
+++ b/lib/libc/gen/Symbol.map
@@ -438,6 +438,7 @@ FBSD_1.6 {
 FBSD_1.7 {
 	 sched_getaffinity;
 	 sched_setaffinity;
+	 sched_getcpu;
 };
 
 FBSDprivate_1.0 {
diff --git a/lib/libc/gen/sched_getcpu_gen.c b/lib/libc/gen/sched_getcpu_gen.c
new file mode 100644
index 000000000000..ef1bebc2915a
--- /dev/null
+++ b/lib/libc/gen/sched_getcpu_gen.c
@@ -0,0 +1,36 @@
+/*-
+ * Copyright (c) 2021 The FreeBSD Foundation
+ *
+ * This software were developed by Konstantin Belousov <kib@FreeBSD.org>
+ * under sponsorship from the FreeBSD Foundation.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sched.h>
+#include "libc_private.h"
+
+int
+sched_getcpu(void)
+{
+	return (__sys_sched_getcpu());
+}
diff --git a/lib/libc/include/libc_private.h b/lib/libc/include/libc_private.h
index 5a524c0211d1..61c1eb438142 100644
--- a/lib/libc/include/libc_private.h
+++ b/lib/libc/include/libc_private.h
@@ -371,6 +371,7 @@ __ssize_t	__sys_recv(int, void *, __size_t, int);
 __ssize_t	__sys_recvfrom(int, void *, __size_t, int, struct sockaddr *,
 		    __socklen_t *);
 __ssize_t	__sys_recvmsg(int, struct msghdr *, int);
+int		__sys_sched_getcpu(void);
 int		__sys_select(int, struct fd_set *, struct fd_set *,
 		    struct fd_set *, struct timeval *);
 __ssize_t	__sys_sendmsg(int, const struct msghdr *, int);
diff --git a/lib/libc/mips/sys/Makefile.inc b/lib/libc/mips/sys/Makefile.inc
index d7cc70d01b07..fa6d9b32739d 100644
--- a/lib/libc/mips/sys/Makefile.inc
+++ b/lib/libc/mips/sys/Makefile.inc
@@ -1,6 +1,7 @@
 # $FreeBSD$
 
-SRCS+=	trivial-vdso_tc.c
+SRCS+=	trivial-vdso_tc.c \
+	sched_getcpu_gen.c
 
 MDASM=  Ovfork.S cerror.S syscall.S
 
diff --git a/lib/libc/powerpc/sys/Makefile.inc b/lib/libc/powerpc/sys/Makefile.inc
index 99a141b5cb1e..6f07795b8870 100644
--- a/lib/libc/powerpc/sys/Makefile.inc
+++ b/lib/libc/powerpc/sys/Makefile.inc
@@ -1,4 +1,6 @@
 # $FreeBSD$
 
-SRCS+=	__vdso_gettc.c
+SRCS+=	__vdso_gettc.c \
+	sched_getcpu_gen.c
+
 MDASM+=	cerror.S
diff --git a/lib/libc/powerpc64/sys/Makefile.inc b/lib/libc/powerpc64/sys/Makefile.inc
index 99a141b5cb1e..6f07795b8870 100644
--- a/lib/libc/powerpc64/sys/Makefile.inc
+++ b/lib/libc/powerpc64/sys/Makefile.inc
@@ -1,4 +1,6 @@
 # $FreeBSD$
 
-SRCS+=	__vdso_gettc.c
+SRCS+=	__vdso_gettc.c \
+	sched_getcpu_gen.c
+
 MDASM+=	cerror.S
diff --git a/lib/libc/riscv/sys/Makefile.inc b/lib/libc/riscv/sys/Makefile.inc
index b322cece382c..2eb12bf11cad 100644
--- a/lib/libc/riscv/sys/Makefile.inc
+++ b/lib/libc/riscv/sys/Makefile.inc
@@ -1,6 +1,7 @@
 # $FreeBSD$
 
-SRCS+=	__vdso_gettc.c
+SRCS+=	__vdso_gettc.c \
+	sched_getcpu_gen.c
 
 MDASM=	cerror.S \
 	syscall.S \
diff --git a/lib/libc/sys/Makefile.inc b/lib/libc/sys/Makefile.inc
index 58ba333080b7..5c472f835029 100644
--- a/lib/libc/sys/Makefile.inc
+++ b/lib/libc/sys/Makefile.inc
@@ -36,6 +36,9 @@ SRCS+=	\
 
 SRCS+= getdents.c lstat.c mknod.c stat.c
 
+NOASM+=  sched_getcpu.o
+PSEUDO+= _sched_getcpu.o
+
 SRCS+= fstat.c fstatat.c fstatfs.c getfsstat.c statfs.c
 NOASM+= fstat.o fstatat.o fstatfs.o getfsstat.o statfs.o
 PSEUDO+= _fstat.o _fstatat.o _fstatfs.o _getfsstat.o _statfs.o
diff --git a/lib/libc/x86/sys/Makefile.inc b/lib/libc/x86/sys/Makefile.inc
index eff3a1085ec6..2945a6871e0d 100644
--- a/lib/libc/x86/sys/Makefile.inc
+++ b/lib/libc/x86/sys/Makefile.inc
@@ -4,7 +4,8 @@
 
 SRCS+= \
 	__vdso_gettc.c \
-	pkru.c
+	pkru.c \
+	sched_getcpu_gen.c
 
 MAN+=	\
 	pkru.3
diff --git a/sys/compat/freebsd32/capabilities.conf b/sys/compat/freebsd32/capabilities.conf
index f53530eb7fa7..3ec922d54920 100644
--- a/sys/compat/freebsd32/capabilities.conf
+++ b/sys/compat/freebsd32/capabilities.conf
@@ -567,6 +567,7 @@ sbrk
 ##
 sched_get_priority_max
 sched_get_priority_min
+sched_getcpu
 
 ##
 ## Allow various thread/process scheduler operations.
diff --git a/sys/compat/freebsd32/syscalls.master b/sys/compat/freebsd32/syscalls.master
index 3e53de2dc966..5cb7c059a670 100644
--- a/sys/compat/freebsd32/syscalls.master
+++ b/sys/compat/freebsd32/syscalls.master
@@ -1181,5 +1181,5 @@
 				    const struct spacectl_range32 *rqsr, \
 				    int flags, \
 				    struct spacectl_range32 *rmsr); }
-
+581	AUE_NULL	NOPROTO	{ int sched_getcpu(void); }
 ; vim: syntax=off
diff --git a/sys/kern/kern_synch.c b/sys/kern/kern_synch.c
index 930bd940d2ab..e78878987b57 100644
--- a/sys/kern/kern_synch.c
+++ b/sys/kern/kern_synch.c
@@ -686,3 +686,10 @@ sys_yield(struct thread *td, struct yield_args *uap)
 	td->td_retval[0] = 0;
 	return (0);
 }
+
+int
+sys_sched_getcpu(struct thread *td, struct sched_getcpu_args *uap)
+{
+	td->td_retval[0] = td->td_oncpu;
+	return (0);
+}
diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master
index 0176916b850a..0965d5771c5a 100644
--- a/sys/kern/syscalls.master
+++ b/sys/kern/syscalls.master
@@ -3261,6 +3261,9 @@
 		    _Out_opt_ struct spacectl_range *rmsr,
 		);
 	}
+581	AUE_NULL	STD|CAPENABLED {
+		int sched_getcpu(void);
+	}
 
 ; Please copy any additions and changes to the following compatability tables:
 ; sys/compat/freebsd32/syscalls.master