public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/5] Add clone3 support for multiple architectures
@ 2023-02-03 17:12 Adhemerval Zanella
  2023-02-03 17:12 ` [PATCH 1/5] powerpc64: Add the clone3 wrapper Adhemerval Zanella
                   ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: Adhemerval Zanella @ 2023-02-03 17:12 UTC (permalink / raw)
  To: libc-alpha

It enables the posix_spawn optimization on newer kernels.

Adhemerval Zanella (5):
  powerpc64: Add the clone3 wrapper
  s390x: Add the clone3 wrapper
  riscv: Add the clone3 wrapper
  arm: Add the clone3 wrapper
  mips: Add the clone3 wrapper

 sysdeps/unix/sysv/linux/arm/clone3.S          |  80 +++++++++
 sysdeps/unix/sysv/linux/arm/sysdep.h          |   1 +
 sysdeps/unix/sysv/linux/mips/clone3.S         | 139 ++++++++++++++++
 sysdeps/unix/sysv/linux/mips/sysdep.h         |   2 +
 .../sysv/linux/powerpc/powerpc64/clone3.S     | 157 ++++++++++++++++++
 sysdeps/unix/sysv/linux/powerpc/sysdep.h      |   1 +
 sysdeps/unix/sysv/linux/riscv/clone3.S        |  80 +++++++++
 sysdeps/unix/sysv/linux/riscv/sysdep.h        |   1 +
 sysdeps/unix/sysv/linux/s390/s390-64/clone3.S |  81 +++++++++
 sysdeps/unix/sysv/linux/s390/sysdep.h         |   1 +
 10 files changed, 543 insertions(+)
 create mode 100644 sysdeps/unix/sysv/linux/arm/clone3.S
 create mode 100644 sysdeps/unix/sysv/linux/mips/clone3.S
 create mode 100644 sysdeps/unix/sysv/linux/powerpc/powerpc64/clone3.S
 create mode 100644 sysdeps/unix/sysv/linux/riscv/clone3.S
 create mode 100644 sysdeps/unix/sysv/linux/s390/s390-64/clone3.S

-- 
2.34.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 1/5] powerpc64: Add the clone3 wrapper
  2023-02-03 17:12 [PATCH 0/5] Add clone3 support for multiple architectures Adhemerval Zanella
@ 2023-02-03 17:12 ` Adhemerval Zanella
  2023-02-03 21:13   ` Paul E Murphy
  2023-02-03 17:12 ` [PATCH 2/5] s390x: " Adhemerval Zanella
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 13+ messages in thread
From: Adhemerval Zanella @ 2023-02-03 17:12 UTC (permalink / raw)
  To: libc-alpha

It follows the internal signature:

  extern int clone3 (struct clone_args *__cl_args, size_t __size,
 int (*__func) (void *__arg), void *__arg);

The powerpc64 ABI requires an initial stackframe so the child can
store/restore the TOC.  It is create prior calling clone3 by
adjusting the stack size (since kernel will compute the stack as
stack plus size).

Checked on powerpc64-linux-gnu (power8, kernel 6.0) and
powerpc64le-linux-gnu (power9, kernel 4.18).
---
 .../sysv/linux/powerpc/powerpc64/clone3.S     | 157 ++++++++++++++++++
 sysdeps/unix/sysv/linux/powerpc/sysdep.h      |   1 +
 2 files changed, 158 insertions(+)
 create mode 100644 sysdeps/unix/sysv/linux/powerpc/powerpc64/clone3.S

diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/clone3.S b/sysdeps/unix/sysv/linux/powerpc/powerpc64/clone3.S
new file mode 100644
index 0000000000..3c50fe392f
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/clone3.S
@@ -0,0 +1,157 @@
+/* The clone3 syscall wrapper.  Linux/powerpc64 version.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sysdep.h>
+#define _ERRNO_H        1
+#include <bits/errno.h>
+
+/* The userland implementation is:
+   int clone3 (struct clone_args *cl_args, size_t size,
+               int (*func)(void *arg), void *arg);
+
+   the kernel entry is:
+   int clone3 (struct clone_args *cl_args, size_t size);
+
+   The parameters are passed in registers from userland:
+   r3: cl_args
+   r4: size
+   r5: func
+   r6: arg  */
+
+#ifdef USE_PPC_SCV
+/* This is equivalent to DO_CALL_SCV, but we cannot use the macro here
+   because it uses CFI directives and we just called cfi_endproc.  */
+# define DO_CLONE3_SVC_CALL(jumpfalse)				\
+	CHECK_SCV_SUPPORT r28 jumpfalse;			\
+	mflr 	r9;						\
+	std 	r9, FRAME_LR_SAVE(r1);				\
+	.machine "push";					\
+	.machine "power9";					\
+	scv 	0;						\
+	.machine "pop";						\
+	ld 	r9, FRAME_LR_SAVE(r1);				\
+	mtlr 	r9;						\
+	/* With scv an, an error is a value -4095 <= x < 0.  */	\
+	cmpdi	cr1, r3, 0;					\
+	b	1f;
+#else
+# define DO_CLONE3_SVC_CALL(fail_branch)
+#endif
+
+        .text
+ENTRY(__clone3)
+	CALL_MCOUNT 4
+
+	/* Sanity checks args.  */
+	cmpdi	cr0, r3, 0
+	cmpdi	cr1, r5, 0
+	cror	cr0*4+eq, cr1*4+eq, cr0*4+eq
+	beq	cr0,L(badargs)
+
+	/* Save some regs in the "red zone".  */
+#ifdef USE_PPC_SCV
+	std	r28, -24(r1)
+	cfi_offset (r28, -24)
+#endif
+	std	r29, -16(r1)
+	std	r30, -8(r1)
+	cfi_offset (r29, -16)
+	cfi_offset (r30, -8)
+
+	/* Save func and arg across syscall.  */
+	mr	r30, r5		/* Function in r30.  */
+	mr	r29, r6		/* Argument in r29.  */
+
+	/* End FDE now, because in the child the unwind info will be
+	   wrong.  */
+	cfi_endproc
+
+	/* Setup a minimum stack frame for child.  It needs to first calculate
+	   the effective stack address, write the start empty backchain pointer,
+	   and update the frame size in the input cl_args.  */
+	ld	r7, 40(r3)	/* Load stack value.  */
+	ld	r8, 48(r3)	/* Load stack_size value.  */
+	li	r10, 0
+	add	r7, r7, r8	/* Calculate effective stack address.  */
+	std	r10, -FRAME_MIN_SIZE_PARM(r7)
+	addi	r8, r8, -FRAME_MIN_SIZE_PARM
+	std	r8, 48(r3)
+
+	/* Do the system call, the kernel expects:
+	   r0: system call numer
+	   r3: cl_args
+	   r4: size  */
+	li	r0, SYS_ify(clone3)
+	DO_CLONE3_SVC_CALL (0f)
+0:      DO_CALL_SC
+
+	/* With sc, error is indicated by cr0.SO.  */
+	cmpdi	cr1, r3, 0
+	crandc	cr1*4+eq, cr1*4+eq, cr0*4+so
+
+1:	bne-	cr1,L(parent)
+
+	/* Child, load the function and arguments.  */
+	std	r2, FRAME_TOC_SAVE(r1)
+	PPC64_LOAD_FUNCPTR r30
+	mr	r3, r29
+	bctrl
+	ld	r2, FRAME_TOC_SAVE(r1)
+
+	li	r0, SYS_ify(exit)
+	DO_CLONE3_SVC_CALL (2f)
+2:	DO_CALL_SC
+	/* We won't ever get here but provide a nop so that the linker
+	   will insert a toc adjusting stub if necessary.  */
+	nop
+
+L(badargs):
+	cfi_startproc
+	li	r3, EINVAL
+	TAIL_CALL_SYSCALL_ERROR
+
+L(parent):
+	/* Check if scv is available.  */
+	cmpdi cr1, r28, 0
+
+	/* Parent.  Restore registers & return.  */
+#ifdef USE_PPC_SCV
+	cfi_offset (r28, -24)
+	ld	r28, -24(r1)
+	cfi_restore (r28)
+#endif
+	cfi_offset (r29,-16)
+	cfi_offset (r30,-8)
+	ld	r29, -16(r1)
+	ld	r30, -8(r1)
+	cfi_restore (r29)
+	cfi_restore (r30)
+
+#ifdef USE_PPC_SCV
+	beq	cr1, 0f
+	RET_SCV
+	b	1f
+#endif
+0:	RET_SC
+1:	TAIL_CALL_SYSCALL_ERROR
+
+PSEUDO_END (__clone3)
+
+libc_hidden_def (__clone3)
+weak_alias (__clone3, clone3)
diff --git a/sysdeps/unix/sysv/linux/powerpc/sysdep.h b/sysdeps/unix/sysv/linux/powerpc/sysdep.h
index 9859cb8171..96a0edc38f 100644
--- a/sysdeps/unix/sysv/linux/powerpc/sysdep.h
+++ b/sysdeps/unix/sysv/linux/powerpc/sysdep.h
@@ -214,6 +214,7 @@
 #if defined(__PPC64__) || defined(__powerpc64__)
 #define HAVE_CLOCK_GETRES64_VSYSCALL	"__kernel_clock_getres"
 #define HAVE_CLOCK_GETTIME64_VSYSCALL	"__kernel_clock_gettime"
+#define HAVE_CLONE3_WRAPPER		1
 #else
 #define HAVE_CLOCK_GETRES_VSYSCALL	"__kernel_clock_getres"
 #define HAVE_CLOCK_GETTIME_VSYSCALL	"__kernel_clock_gettime"
-- 
2.34.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 2/5] s390x: Add the clone3 wrapper
  2023-02-03 17:12 [PATCH 0/5] Add clone3 support for multiple architectures Adhemerval Zanella
  2023-02-03 17:12 ` [PATCH 1/5] powerpc64: Add the clone3 wrapper Adhemerval Zanella
@ 2023-02-03 17:12 ` Adhemerval Zanella
  2023-05-25 17:36   ` Adhemerval Zanella Netto
  2023-02-03 17:12 ` [PATCH 3/5] riscv: " Adhemerval Zanella
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 13+ messages in thread
From: Adhemerval Zanella @ 2023-02-03 17:12 UTC (permalink / raw)
  To: libc-alpha

It follows the internal signature:

  extern int clone3 (struct clone_args *__cl_args, size_t __size,
 int (*__func) (void *__arg), void *__arg);

Checked on s390x-linux-gnu.
---
 sysdeps/unix/sysv/linux/s390/s390-64/clone3.S | 81 +++++++++++++++++++
 sysdeps/unix/sysv/linux/s390/sysdep.h         |  1 +
 2 files changed, 82 insertions(+)
 create mode 100644 sysdeps/unix/sysv/linux/s390/s390-64/clone3.S

diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/clone3.S b/sysdeps/unix/sysv/linux/s390/s390-64/clone3.S
new file mode 100644
index 0000000000..417fdcb785
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/s390/s390-64/clone3.S
@@ -0,0 +1,81 @@
+/* The clone3 syscall wrapper.  Linux/s390x version.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sysdep.h>
+#define _ERRNO_H        1
+#include <bits/errno.h>
+
+/* The userland implementation is:
+   int clone3 (struct clone_args *cl_args, size_t size,
+               int (*func)(void *arg), void *arg);
+
+   the kernel entry is:
+   int clone3 (struct clone_args *cl_args, size_t size);
+
+   The parameters are passed in registers from userland:
+   r2: cl_args
+   r3: size
+   r4: func
+   r5: arg  */
+
+        .text
+ENTRY(__clone3)
+	stg	%r6, 48(%r15)
+
+	/* Sanity check args.  */
+	ltgr	%r2, %r2
+	je	error
+	ltgr	%r6, %r4
+	je	error
+
+	/* Do the system call, the kernel expects:
+	   r1: system call number
+	   r2: cl_args
+	   r3: size  */
+	lghi	%r1, SYS_ify(clone3)
+	svc	0
+	ltgr	%r2,%r2			/* check return code */
+	jz	thread_start
+	lg	%r6, 48(%r15)
+	jgm	SYSCALL_ERROR_LABEL
+	br	%r14
+error:
+	lghi	%r2,-EINVAL
+	jg	SYSCALL_ERROR_LABEL
+PSEUDO_END (__clone3)
+
+	.align 4
+	.type thread_start, %function
+thread_start:
+	cfi_startproc
+	/* Mark r14 as undefined in order to stop unwinding here.  */
+	cfi_undefined (r14)
+
+	/* func is in gpr 6, arg in gpr 5.  */
+	lgr	%r2, %r5
+	aghi	%r15, -160
+	xc	0(8,%r15),0(%r15)
+	basr	%r14, %r6
+
+	DO_CALL (exit, 1)
+	cfi_endproc
+	.size thread_start, .-thread_start
+
+libc_hidden_def (__clone3)
+weak_alias (__clone3, clone3)
diff --git a/sysdeps/unix/sysv/linux/s390/sysdep.h b/sysdeps/unix/sysv/linux/s390/sysdep.h
index 3bde8ba19c..b099fea9c6 100644
--- a/sysdeps/unix/sysv/linux/s390/sysdep.h
+++ b/sysdeps/unix/sysv/linux/s390/sysdep.h
@@ -72,6 +72,7 @@
 #ifdef __s390x__
 #define HAVE_CLOCK_GETRES64_VSYSCALL	"__kernel_clock_getres"
 #define HAVE_CLOCK_GETTIME64_VSYSCALL	"__kernel_clock_gettime"
+#define HAVE_CLONE3_WRAPPER		1
 #else
 #define HAVE_CLOCK_GETRES_VSYSCALL	"__kernel_clock_getres"
 #define HAVE_CLOCK_GETTIME_VSYSCALL	"__kernel_clock_gettime"
-- 
2.34.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 3/5] riscv: Add the clone3 wrapper
  2023-02-03 17:12 [PATCH 0/5] Add clone3 support for multiple architectures Adhemerval Zanella
  2023-02-03 17:12 ` [PATCH 1/5] powerpc64: Add the clone3 wrapper Adhemerval Zanella
  2023-02-03 17:12 ` [PATCH 2/5] s390x: " Adhemerval Zanella
@ 2023-02-03 17:12 ` Adhemerval Zanella
  2023-05-25 17:36   ` Adhemerval Zanella Netto
  2023-05-29 14:04   ` Palmer Dabbelt
  2023-02-03 17:12 ` [PATCH 4/5] arm: " Adhemerval Zanella
  2023-02-03 17:12 ` [PATCH 5/5] mips: " Adhemerval Zanella
  4 siblings, 2 replies; 13+ messages in thread
From: Adhemerval Zanella @ 2023-02-03 17:12 UTC (permalink / raw)
  To: libc-alpha

It follows the internal signature:

  extern int clone3 (struct clone_args *__cl_args, size_t __size,
 int (*__func) (void *__arg), void *__arg);

Checked on riscv64-linux-gnu-rv64imafdc-lp64d.
---
 sysdeps/unix/sysv/linux/riscv/clone3.S | 80 ++++++++++++++++++++++++++
 sysdeps/unix/sysv/linux/riscv/sysdep.h |  1 +
 2 files changed, 81 insertions(+)
 create mode 100644 sysdeps/unix/sysv/linux/riscv/clone3.S

diff --git a/sysdeps/unix/sysv/linux/riscv/clone3.S b/sysdeps/unix/sysv/linux/riscv/clone3.S
new file mode 100644
index 0000000000..ee5780ee2f
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/clone3.S
@@ -0,0 +1,80 @@
+/* The clone3 syscall wrapper.  Linux/RISC-V version.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <asm/errno.h>
+#include <sys/asm.h>
+#include <sysdep.h>
+
+/* The userland implementation is:
+   int clone3 (struct clone_args *cl_args, size_t size,
+               int (*func)(void *arg), void *arg);
+
+   the kernel entry is:
+   int clone3 (struct clone_args *cl_args, size_t size);
+
+   The parameters are passed in registers from userland:
+   a0: cl_args
+   a1: size
+   a2: func
+   a3: arg  */
+
+        .text
+ENTRY(__clone3)
+	/* Sanity check args.  */
+	beqz	a0, L(invalid)	/* No NULL cl_args pointer.  */
+	beqz	a2, L(invalid)  /* No NULL function pointer.  */
+
+	/* Do the system call, the kernel expects:
+	   a7: system call number
+	   a0: cl_args
+	   a1: size  */
+	li	a7, __NR_clone3
+	scall
+
+	bltz	a0, L(error)
+	beqz	a0, L(thread_start)
+
+	ret
+
+L(invalid):
+	li	a0, -EINVAL
+L(error):
+	tail	__syscall_error
+END (__clone3)
+
+ENTRY(__thread_start_clone3)
+L(thread_start):
+	/* Terminate call stack by noting ra is undefined.  Use a dummy
+	   .cfi_label to force starting the FDE.  */
+	.cfi_label .Ldummy
+	cfi_undefined (ra)
+
+	/* Restore the arg for user's function and call the user's
+	   function.  */
+	mv		a1, a2	/* Function pointer.  */
+	mv		a0, a3	/* Argument pointer.  */
+	jalr		a1
+
+	/* Call exit with the function's return value.  */
+	li		a7, __NR_exit
+	scall
+END(__thread_start_clone3)
+
+libc_hidden_def (__clone3)
+weak_alias (__clone3, clone3)
diff --git a/sysdeps/unix/sysv/linux/riscv/sysdep.h b/sysdeps/unix/sysv/linux/riscv/sysdep.h
index 4af5fe5dbc..e96a930409 100644
--- a/sysdeps/unix/sysv/linux/riscv/sysdep.h
+++ b/sysdeps/unix/sysv/linux/riscv/sysdep.h
@@ -151,6 +151,7 @@
 
 /* RV32 does not support the gettime VDSO syscalls.  */
 # endif
+# define HAVE_CLONE3_WRAPPER		1
 
 /* List of system calls which are supported as vsyscalls (for RV32 and
    RV64).  */
-- 
2.34.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 4/5] arm: Add the clone3 wrapper
  2023-02-03 17:12 [PATCH 0/5] Add clone3 support for multiple architectures Adhemerval Zanella
                   ` (2 preceding siblings ...)
  2023-02-03 17:12 ` [PATCH 3/5] riscv: " Adhemerval Zanella
@ 2023-02-03 17:12 ` Adhemerval Zanella
  2023-05-25 17:36   ` Adhemerval Zanella Netto
  2023-02-03 17:12 ` [PATCH 5/5] mips: " Adhemerval Zanella
  4 siblings, 1 reply; 13+ messages in thread
From: Adhemerval Zanella @ 2023-02-03 17:12 UTC (permalink / raw)
  To: libc-alpha

It follows the internal signature:

  extern int clone3 (struct clone_args *__cl_args, size_t __size,
		    int (*__func) (void *__arg), void *__arg);

Checked on arm-linux-gnueabihf.
---
 sysdeps/unix/sysv/linux/arm/clone3.S | 80 ++++++++++++++++++++++++++++
 sysdeps/unix/sysv/linux/arm/sysdep.h |  1 +
 2 files changed, 81 insertions(+)
 create mode 100644 sysdeps/unix/sysv/linux/arm/clone3.S

diff --git a/sysdeps/unix/sysv/linux/arm/clone3.S b/sysdeps/unix/sysv/linux/arm/clone3.S
new file mode 100644
index 0000000000..f236d18390
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/arm/clone3.S
@@ -0,0 +1,80 @@
+/* The clone3 syscall wrapper.  Linux/arm version.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sysdep.h>
+#define _ERRNO_H	1
+#include <bits/errno.h>
+
+/* The userland implementation is:
+   int clone3 (struct clone_args *cl_args, size_t size,
+               int (*func)(void *arg), void *arg);
+
+   the kernel entry is:
+   int clone3 (struct clone_args *cl_args, size_t size);
+
+   The parameters are passed in registers from userland:
+   r0: cl_args
+   r1: size
+   r2: func
+   r3: arg  */
+
+        .text
+ENTRY(__clone3)
+	/* Sanity check args.  */
+	cmp	r0, #0
+	ite	ne
+	cmpne	r1, #0
+	moveq	r0, #-EINVAL
+	beq	PLTJMP(syscall_error)
+
+	/* Do the syscall, the kernel expects:
+	   r7: system call number:
+	   r0: cl_args
+	   r1: size  */
+	push    { r7 }
+	cfi_adjust_cfa_offset (4)
+	cfi_rel_offset (r7, 0)
+	ldr     r7, =SYS_ify(clone3)
+	swi	0x0
+	cfi_endproc
+
+	cmp	r0, #0
+	beq	1f
+	pop     {r7}
+	blt	PLTJMP(C_SYMBOL_NAME(__syscall_error))
+	RETINSTR(, lr)
+
+	cfi_startproc
+PSEUDO_END (__clone3)
+
+1:
+	.fnstart
+	.cantunwind
+	mov	r0, r3
+	mov	ip, r2
+	BLX (ip)
+
+	/* And we are done, passing the return value through r0.  */
+	ldr	r7, =SYS_ify(exit)
+	swi	0x0
+
+	.fnend
+
+libc_hidden_def (__clone3)
+weak_alias (__clone3, clone3)
diff --git a/sysdeps/unix/sysv/linux/arm/sysdep.h b/sysdeps/unix/sysv/linux/arm/sysdep.h
index 2f321881c8..57fc5f16bd 100644
--- a/sysdeps/unix/sysv/linux/arm/sysdep.h
+++ b/sysdeps/unix/sysv/linux/arm/sysdep.h
@@ -362,6 +362,7 @@ __local_syscall_error:						\
 #define HAVE_CLOCK_GETTIME_VSYSCALL	"__vdso_clock_gettime"
 #define HAVE_CLOCK_GETTIME64_VSYSCALL	"__vdso_clock_gettime64"
 #define HAVE_GETTIMEOFDAY_VSYSCALL	"__vdso_gettimeofday"
+#define HAVE_CLONE3_WRAPPER		1
 
 #define LOAD_ARGS_0()
 #define ASM_ARGS_0
-- 
2.34.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 5/5] mips: Add the clone3 wrapper
  2023-02-03 17:12 [PATCH 0/5] Add clone3 support for multiple architectures Adhemerval Zanella
                   ` (3 preceding siblings ...)
  2023-02-03 17:12 ` [PATCH 4/5] arm: " Adhemerval Zanella
@ 2023-02-03 17:12 ` Adhemerval Zanella
  2023-05-25 17:36   ` Adhemerval Zanella Netto
  4 siblings, 1 reply; 13+ messages in thread
From: Adhemerval Zanella @ 2023-02-03 17:12 UTC (permalink / raw)
  To: libc-alpha

It follows the internal signature:

extern int clone3 (struct clone_args *__cl_args, size_t __size,
                   int (*__func) (void *__arg), void *__arg);

Checked on mips64el-linux-gnueabihf and mipsel-linux-gnu.
---
 sysdeps/unix/sysv/linux/mips/clone3.S | 139 ++++++++++++++++++++++++++
 sysdeps/unix/sysv/linux/mips/sysdep.h |   2 +
 2 files changed, 141 insertions(+)
 create mode 100644 sysdeps/unix/sysv/linux/mips/clone3.S

diff --git a/sysdeps/unix/sysv/linux/mips/clone3.S b/sysdeps/unix/sysv/linux/mips/clone3.S
new file mode 100644
index 0000000000..492d6d8b77
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/mips/clone3.S
@@ -0,0 +1,139 @@
+/* The clone3 syscall wrapper.  Linux/mips version.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sys/asm.h>
+#include <sysdep.h>
+#define _ERRNO_H        1
+#include <bits/errno.h>
+
+/* The userland implementation is:
+   int clone3 (struct clone_args *cl_args, size_t size,
+               int (*func)(void *arg), void *arg);
+
+   the kernel entry is:
+   int clone3 (struct clone_args *cl_args, size_t size);
+
+   The parameters are passed in registers from userland:
+   a0/$4: cl_args
+   a1/$5: size
+   a2/$6: func
+   a3/$7: arg  */
+
+	.text
+	.set		nomips16
+#if _MIPS_SIM == _ABIO32
+# define EXTRA_LOCALS 1
+#else
+# define EXTRA_LOCALS 0
+#endif
+#define FRAMESZ ((NARGSAVE*SZREG)+ALSZ)&ALMASK
+GPOFF= FRAMESZ-(1*SZREG)
+NESTED(__clone3, SZREG, sp)
+#ifdef __PIC__
+	SETUP_GP
+#endif
+#if FRAMESZ
+	PTR_SUBU sp, FRAMESZ
+	cfi_adjust_cfa_offset (FRAMESZ)
+#endif
+	SETUP_GP64_STACK (GPOFF, __clone)
+#ifdef __PIC__
+	SAVE_GP (GPOFF)
+#endif
+#ifdef PROF
+	.set	noat
+	move	$1,ra
+	jal	_mcount
+	.set	at
+#endif
+
+	/* Sanity check args.  */
+	li	v0, EINVAL
+	beqz	a0, L(error)	/* No NULL cl_args pointer.  */
+	beqz	a2, L(error)	/* No NULL function pointer.  */
+
+	move	$8, a3		/* a3 is set to 0/1 for syscall success/error
+				   while a4/$8 is returned unmodified.  */
+
+	/* Do the system call, the kernel expects:
+	   v0: system call number
+	   a0: cl_args
+	   a1: size  */
+	li		v0, __NR_clone3
+	cfi_endproc
+	syscall
+
+	bnez		a3, L(error)
+	beqz		v0, L(thread_start_clone3)
+
+	/* Successful return from the parent */
+	cfi_startproc
+#if FRAMESZ
+	cfi_adjust_cfa_offset (FRAMESZ)
+#endif
+	SETUP_GP64_STACK_CFI (GPOFF)
+	cfi_remember_state
+	RESTORE_GP64_STACK
+#if FRAMESZ
+	PTR_ADDU	sp, FRAMESZ
+	cfi_adjust_cfa_offset (-FRAMESZ)
+#endif
+	ret
+
+L(error):
+	cfi_restore_state
+#ifdef __PIC__
+	PTR_LA		t9,__syscall_error
+	RESTORE_GP64_STACK
+	PTR_ADDU	sp, FRAMESZ
+	cfi_adjust_cfa_offset (-FRAMESZ)
+	jr		t9
+#else
+	RESTORE_GP64_STACK
+	PTR_ADDU	sp, FRAMESZ
+	cfi_adjust_cfa_offset (-FRAMESZ)
+	j		__syscall_error
+#endif
+END (__clone3)
+
+/* Load up the arguments to the function.  Put this block of code in
+   its own function so that we can terminate the stack trace with our
+   debug info.  */
+
+ENTRY(__thread_start_clone3)
+L(thread_start_clone3):
+	cfi_undefined ($31)
+	/* cp is already loaded.  */
+	SAVE_GP (GPOFF)
+	/* The stackframe has been created on entry of clone3.  */
+
+	/* Restore the arg for user's function.  */
+	move		t9, a2		/* Function pointer.  */
+	move		a0, $8		/* Argument pointer.  */
+
+	/* Call the user's function.  */
+	jal		t9
+
+	move		a0, v0
+	li		v0, __NR_exit
+	syscall
+END(__thread_start_clone3)
+
+libc_hidden_def (__clone3)
+weak_alias (__clone3, clone3)
diff --git a/sysdeps/unix/sysv/linux/mips/sysdep.h b/sysdeps/unix/sysv/linux/mips/sysdep.h
index ff84a91b31..673aa08b57 100644
--- a/sysdeps/unix/sysv/linux/mips/sysdep.h
+++ b/sysdeps/unix/sysv/linux/mips/sysdep.h
@@ -28,3 +28,5 @@
 #endif
 #define HAVE_GETTIMEOFDAY_VSYSCALL      "__vdso_gettimeofday"
 #define HAVE_CLOCK_GETRES_VSYSCALL      "__vdso_clock_getres"
+
+#define HAVE_CLONE3_WRAPPER		1
-- 
2.34.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/5] powerpc64: Add the clone3 wrapper
  2023-02-03 17:12 ` [PATCH 1/5] powerpc64: Add the clone3 wrapper Adhemerval Zanella
@ 2023-02-03 21:13   ` Paul E Murphy
  0 siblings, 0 replies; 13+ messages in thread
From: Paul E Murphy @ 2023-02-03 21:13 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha



On 2/3/23 11:12 AM, Adhemerval Zanella via Libc-alpha wrote:
> It follows the internal signature:
> 
>    extern int clone3 (struct clone_args *__cl_args, size_t __size,
>   int (*__func) (void *__arg), void *__arg);
> 
> The powerpc64 ABI requires an initial stackframe so the child can
> store/restore the TOC.  It is create prior calling clone3 by
> adjusting the stack size (since kernel will compute the stack as
> stack plus size).
> 
> Checked on powerpc64-linux-gnu (power8, kernel 6.0) and
> powerpc64le-linux-gnu (power9, kernel 4.18).

Thanks. LGTM.

Reviewed-by: Paul E. Murphy <murphyp@linux.ibm.com>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 2/5] s390x: Add the clone3 wrapper
  2023-02-03 17:12 ` [PATCH 2/5] s390x: " Adhemerval Zanella
@ 2023-05-25 17:36   ` Adhemerval Zanella Netto
  0 siblings, 0 replies; 13+ messages in thread
From: Adhemerval Zanella Netto @ 2023-05-25 17:36 UTC (permalink / raw)
  To: libc-alpha

Ping.

On 03/02/23 14:12, Adhemerval Zanella wrote:
> It follows the internal signature:
> 
>   extern int clone3 (struct clone_args *__cl_args, size_t __size,
>  int (*__func) (void *__arg), void *__arg);
> 
> Checked on s390x-linux-gnu.
> ---
>  sysdeps/unix/sysv/linux/s390/s390-64/clone3.S | 81 +++++++++++++++++++
>  sysdeps/unix/sysv/linux/s390/sysdep.h         |  1 +
>  2 files changed, 82 insertions(+)
>  create mode 100644 sysdeps/unix/sysv/linux/s390/s390-64/clone3.S
> 
> diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/clone3.S b/sysdeps/unix/sysv/linux/s390/s390-64/clone3.S
> new file mode 100644
> index 0000000000..417fdcb785
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/s390/s390-64/clone3.S
> @@ -0,0 +1,81 @@
> +/* The clone3 syscall wrapper.  Linux/s390x version.
> +   Copyright (C) 2023 Free Software Foundation, Inc.
> +
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <sysdep.h>
> +#define _ERRNO_H        1
> +#include <bits/errno.h>
> +
> +/* The userland implementation is:
> +   int clone3 (struct clone_args *cl_args, size_t size,
> +               int (*func)(void *arg), void *arg);
> +
> +   the kernel entry is:
> +   int clone3 (struct clone_args *cl_args, size_t size);
> +
> +   The parameters are passed in registers from userland:
> +   r2: cl_args
> +   r3: size
> +   r4: func
> +   r5: arg  */
> +
> +        .text
> +ENTRY(__clone3)
> +	stg	%r6, 48(%r15)
> +
> +	/* Sanity check args.  */
> +	ltgr	%r2, %r2
> +	je	error
> +	ltgr	%r6, %r4
> +	je	error
> +
> +	/* Do the system call, the kernel expects:
> +	   r1: system call number
> +	   r2: cl_args
> +	   r3: size  */
> +	lghi	%r1, SYS_ify(clone3)
> +	svc	0
> +	ltgr	%r2,%r2			/* check return code */
> +	jz	thread_start
> +	lg	%r6, 48(%r15)
> +	jgm	SYSCALL_ERROR_LABEL
> +	br	%r14
> +error:
> +	lghi	%r2,-EINVAL
> +	jg	SYSCALL_ERROR_LABEL
> +PSEUDO_END (__clone3)
> +
> +	.align 4
> +	.type thread_start, %function
> +thread_start:
> +	cfi_startproc
> +	/* Mark r14 as undefined in order to stop unwinding here.  */
> +	cfi_undefined (r14)
> +
> +	/* func is in gpr 6, arg in gpr 5.  */
> +	lgr	%r2, %r5
> +	aghi	%r15, -160
> +	xc	0(8,%r15),0(%r15)
> +	basr	%r14, %r6
> +
> +	DO_CALL (exit, 1)
> +	cfi_endproc
> +	.size thread_start, .-thread_start
> +
> +libc_hidden_def (__clone3)
> +weak_alias (__clone3, clone3)
> diff --git a/sysdeps/unix/sysv/linux/s390/sysdep.h b/sysdeps/unix/sysv/linux/s390/sysdep.h
> index 3bde8ba19c..b099fea9c6 100644
> --- a/sysdeps/unix/sysv/linux/s390/sysdep.h
> +++ b/sysdeps/unix/sysv/linux/s390/sysdep.h
> @@ -72,6 +72,7 @@
>  #ifdef __s390x__
>  #define HAVE_CLOCK_GETRES64_VSYSCALL	"__kernel_clock_getres"
>  #define HAVE_CLOCK_GETTIME64_VSYSCALL	"__kernel_clock_gettime"
> +#define HAVE_CLONE3_WRAPPER		1
>  #else
>  #define HAVE_CLOCK_GETRES_VSYSCALL	"__kernel_clock_getres"
>  #define HAVE_CLOCK_GETTIME_VSYSCALL	"__kernel_clock_gettime"

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 3/5] riscv: Add the clone3 wrapper
  2023-02-03 17:12 ` [PATCH 3/5] riscv: " Adhemerval Zanella
@ 2023-05-25 17:36   ` Adhemerval Zanella Netto
  2023-05-29 14:04   ` Palmer Dabbelt
  1 sibling, 0 replies; 13+ messages in thread
From: Adhemerval Zanella Netto @ 2023-05-25 17:36 UTC (permalink / raw)
  To: libc-alpha

Ping.

On 03/02/23 14:12, Adhemerval Zanella wrote:
> It follows the internal signature:
> 
>   extern int clone3 (struct clone_args *__cl_args, size_t __size,
>  int (*__func) (void *__arg), void *__arg);
> 
> Checked on riscv64-linux-gnu-rv64imafdc-lp64d.
> ---
>  sysdeps/unix/sysv/linux/riscv/clone3.S | 80 ++++++++++++++++++++++++++
>  sysdeps/unix/sysv/linux/riscv/sysdep.h |  1 +
>  2 files changed, 81 insertions(+)
>  create mode 100644 sysdeps/unix/sysv/linux/riscv/clone3.S
> 
> diff --git a/sysdeps/unix/sysv/linux/riscv/clone3.S b/sysdeps/unix/sysv/linux/riscv/clone3.S
> new file mode 100644
> index 0000000000..ee5780ee2f
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/riscv/clone3.S
> @@ -0,0 +1,80 @@
> +/* The clone3 syscall wrapper.  Linux/RISC-V version.
> +   Copyright (C) 2023 Free Software Foundation, Inc.
> +
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <asm/errno.h>
> +#include <sys/asm.h>
> +#include <sysdep.h>
> +
> +/* The userland implementation is:
> +   int clone3 (struct clone_args *cl_args, size_t size,
> +               int (*func)(void *arg), void *arg);
> +
> +   the kernel entry is:
> +   int clone3 (struct clone_args *cl_args, size_t size);
> +
> +   The parameters are passed in registers from userland:
> +   a0: cl_args
> +   a1: size
> +   a2: func
> +   a3: arg  */
> +
> +        .text
> +ENTRY(__clone3)
> +	/* Sanity check args.  */
> +	beqz	a0, L(invalid)	/* No NULL cl_args pointer.  */
> +	beqz	a2, L(invalid)  /* No NULL function pointer.  */
> +
> +	/* Do the system call, the kernel expects:
> +	   a7: system call number
> +	   a0: cl_args
> +	   a1: size  */
> +	li	a7, __NR_clone3
> +	scall
> +
> +	bltz	a0, L(error)
> +	beqz	a0, L(thread_start)
> +
> +	ret
> +
> +L(invalid):
> +	li	a0, -EINVAL
> +L(error):
> +	tail	__syscall_error
> +END (__clone3)
> +
> +ENTRY(__thread_start_clone3)
> +L(thread_start):
> +	/* Terminate call stack by noting ra is undefined.  Use a dummy
> +	   .cfi_label to force starting the FDE.  */
> +	.cfi_label .Ldummy
> +	cfi_undefined (ra)
> +
> +	/* Restore the arg for user's function and call the user's
> +	   function.  */
> +	mv		a1, a2	/* Function pointer.  */
> +	mv		a0, a3	/* Argument pointer.  */
> +	jalr		a1
> +
> +	/* Call exit with the function's return value.  */
> +	li		a7, __NR_exit
> +	scall
> +END(__thread_start_clone3)
> +
> +libc_hidden_def (__clone3)
> +weak_alias (__clone3, clone3)
> diff --git a/sysdeps/unix/sysv/linux/riscv/sysdep.h b/sysdeps/unix/sysv/linux/riscv/sysdep.h
> index 4af5fe5dbc..e96a930409 100644
> --- a/sysdeps/unix/sysv/linux/riscv/sysdep.h
> +++ b/sysdeps/unix/sysv/linux/riscv/sysdep.h
> @@ -151,6 +151,7 @@
>  
>  /* RV32 does not support the gettime VDSO syscalls.  */
>  # endif
> +# define HAVE_CLONE3_WRAPPER		1
>  
>  /* List of system calls which are supported as vsyscalls (for RV32 and
>     RV64).  */

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 4/5] arm: Add the clone3 wrapper
  2023-02-03 17:12 ` [PATCH 4/5] arm: " Adhemerval Zanella
@ 2023-05-25 17:36   ` Adhemerval Zanella Netto
  0 siblings, 0 replies; 13+ messages in thread
From: Adhemerval Zanella Netto @ 2023-05-25 17:36 UTC (permalink / raw)
  To: libc-alpha

Ping.

On 03/02/23 14:12, Adhemerval Zanella wrote:
> It follows the internal signature:
> 
>   extern int clone3 (struct clone_args *__cl_args, size_t __size,
> 		    int (*__func) (void *__arg), void *__arg);
> 
> Checked on arm-linux-gnueabihf.
> ---
>  sysdeps/unix/sysv/linux/arm/clone3.S | 80 ++++++++++++++++++++++++++++
>  sysdeps/unix/sysv/linux/arm/sysdep.h |  1 +
>  2 files changed, 81 insertions(+)
>  create mode 100644 sysdeps/unix/sysv/linux/arm/clone3.S
> 
> diff --git a/sysdeps/unix/sysv/linux/arm/clone3.S b/sysdeps/unix/sysv/linux/arm/clone3.S
> new file mode 100644
> index 0000000000..f236d18390
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/arm/clone3.S
> @@ -0,0 +1,80 @@
> +/* The clone3 syscall wrapper.  Linux/arm version.
> +   Copyright (C) 2023 Free Software Foundation, Inc.
> +
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <sysdep.h>
> +#define _ERRNO_H	1
> +#include <bits/errno.h>
> +
> +/* The userland implementation is:
> +   int clone3 (struct clone_args *cl_args, size_t size,
> +               int (*func)(void *arg), void *arg);
> +
> +   the kernel entry is:
> +   int clone3 (struct clone_args *cl_args, size_t size);
> +
> +   The parameters are passed in registers from userland:
> +   r0: cl_args
> +   r1: size
> +   r2: func
> +   r3: arg  */
> +
> +        .text
> +ENTRY(__clone3)
> +	/* Sanity check args.  */
> +	cmp	r0, #0
> +	ite	ne
> +	cmpne	r1, #0
> +	moveq	r0, #-EINVAL
> +	beq	PLTJMP(syscall_error)
> +
> +	/* Do the syscall, the kernel expects:
> +	   r7: system call number:
> +	   r0: cl_args
> +	   r1: size  */
> +	push    { r7 }
> +	cfi_adjust_cfa_offset (4)
> +	cfi_rel_offset (r7, 0)
> +	ldr     r7, =SYS_ify(clone3)
> +	swi	0x0
> +	cfi_endproc
> +
> +	cmp	r0, #0
> +	beq	1f
> +	pop     {r7}
> +	blt	PLTJMP(C_SYMBOL_NAME(__syscall_error))
> +	RETINSTR(, lr)
> +
> +	cfi_startproc
> +PSEUDO_END (__clone3)
> +
> +1:
> +	.fnstart
> +	.cantunwind
> +	mov	r0, r3
> +	mov	ip, r2
> +	BLX (ip)
> +
> +	/* And we are done, passing the return value through r0.  */
> +	ldr	r7, =SYS_ify(exit)
> +	swi	0x0
> +
> +	.fnend
> +
> +libc_hidden_def (__clone3)
> +weak_alias (__clone3, clone3)
> diff --git a/sysdeps/unix/sysv/linux/arm/sysdep.h b/sysdeps/unix/sysv/linux/arm/sysdep.h
> index 2f321881c8..57fc5f16bd 100644
> --- a/sysdeps/unix/sysv/linux/arm/sysdep.h
> +++ b/sysdeps/unix/sysv/linux/arm/sysdep.h
> @@ -362,6 +362,7 @@ __local_syscall_error:						\
>  #define HAVE_CLOCK_GETTIME_VSYSCALL	"__vdso_clock_gettime"
>  #define HAVE_CLOCK_GETTIME64_VSYSCALL	"__vdso_clock_gettime64"
>  #define HAVE_GETTIMEOFDAY_VSYSCALL	"__vdso_gettimeofday"
> +#define HAVE_CLONE3_WRAPPER		1
>  
>  #define LOAD_ARGS_0()
>  #define ASM_ARGS_0

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 5/5] mips: Add the clone3 wrapper
  2023-02-03 17:12 ` [PATCH 5/5] mips: " Adhemerval Zanella
@ 2023-05-25 17:36   ` Adhemerval Zanella Netto
  0 siblings, 0 replies; 13+ messages in thread
From: Adhemerval Zanella Netto @ 2023-05-25 17:36 UTC (permalink / raw)
  To: libc-alpha

Ping.

On 03/02/23 14:12, Adhemerval Zanella wrote:
> It follows the internal signature:
> 
> extern int clone3 (struct clone_args *__cl_args, size_t __size,
>                    int (*__func) (void *__arg), void *__arg);
> 
> Checked on mips64el-linux-gnueabihf and mipsel-linux-gnu.
> ---
>  sysdeps/unix/sysv/linux/mips/clone3.S | 139 ++++++++++++++++++++++++++
>  sysdeps/unix/sysv/linux/mips/sysdep.h |   2 +
>  2 files changed, 141 insertions(+)
>  create mode 100644 sysdeps/unix/sysv/linux/mips/clone3.S
> 
> diff --git a/sysdeps/unix/sysv/linux/mips/clone3.S b/sysdeps/unix/sysv/linux/mips/clone3.S
> new file mode 100644
> index 0000000000..492d6d8b77
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/mips/clone3.S
> @@ -0,0 +1,139 @@
> +/* The clone3 syscall wrapper.  Linux/mips version.
> +   Copyright (C) 2023 Free Software Foundation, Inc.
> +
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <sys/asm.h>
> +#include <sysdep.h>
> +#define _ERRNO_H        1
> +#include <bits/errno.h>
> +
> +/* The userland implementation is:
> +   int clone3 (struct clone_args *cl_args, size_t size,
> +               int (*func)(void *arg), void *arg);
> +
> +   the kernel entry is:
> +   int clone3 (struct clone_args *cl_args, size_t size);
> +
> +   The parameters are passed in registers from userland:
> +   a0/$4: cl_args
> +   a1/$5: size
> +   a2/$6: func
> +   a3/$7: arg  */
> +
> +	.text
> +	.set		nomips16
> +#if _MIPS_SIM == _ABIO32
> +# define EXTRA_LOCALS 1
> +#else
> +# define EXTRA_LOCALS 0
> +#endif
> +#define FRAMESZ ((NARGSAVE*SZREG)+ALSZ)&ALMASK
> +GPOFF= FRAMESZ-(1*SZREG)
> +NESTED(__clone3, SZREG, sp)
> +#ifdef __PIC__
> +	SETUP_GP
> +#endif
> +#if FRAMESZ
> +	PTR_SUBU sp, FRAMESZ
> +	cfi_adjust_cfa_offset (FRAMESZ)
> +#endif
> +	SETUP_GP64_STACK (GPOFF, __clone)
> +#ifdef __PIC__
> +	SAVE_GP (GPOFF)
> +#endif
> +#ifdef PROF
> +	.set	noat
> +	move	$1,ra
> +	jal	_mcount
> +	.set	at
> +#endif
> +
> +	/* Sanity check args.  */
> +	li	v0, EINVAL
> +	beqz	a0, L(error)	/* No NULL cl_args pointer.  */
> +	beqz	a2, L(error)	/* No NULL function pointer.  */
> +
> +	move	$8, a3		/* a3 is set to 0/1 for syscall success/error
> +				   while a4/$8 is returned unmodified.  */
> +
> +	/* Do the system call, the kernel expects:
> +	   v0: system call number
> +	   a0: cl_args
> +	   a1: size  */
> +	li		v0, __NR_clone3
> +	cfi_endproc
> +	syscall
> +
> +	bnez		a3, L(error)
> +	beqz		v0, L(thread_start_clone3)
> +
> +	/* Successful return from the parent */
> +	cfi_startproc
> +#if FRAMESZ
> +	cfi_adjust_cfa_offset (FRAMESZ)
> +#endif
> +	SETUP_GP64_STACK_CFI (GPOFF)
> +	cfi_remember_state
> +	RESTORE_GP64_STACK
> +#if FRAMESZ
> +	PTR_ADDU	sp, FRAMESZ
> +	cfi_adjust_cfa_offset (-FRAMESZ)
> +#endif
> +	ret
> +
> +L(error):
> +	cfi_restore_state
> +#ifdef __PIC__
> +	PTR_LA		t9,__syscall_error
> +	RESTORE_GP64_STACK
> +	PTR_ADDU	sp, FRAMESZ
> +	cfi_adjust_cfa_offset (-FRAMESZ)
> +	jr		t9
> +#else
> +	RESTORE_GP64_STACK
> +	PTR_ADDU	sp, FRAMESZ
> +	cfi_adjust_cfa_offset (-FRAMESZ)
> +	j		__syscall_error
> +#endif
> +END (__clone3)
> +
> +/* Load up the arguments to the function.  Put this block of code in
> +   its own function so that we can terminate the stack trace with our
> +   debug info.  */
> +
> +ENTRY(__thread_start_clone3)
> +L(thread_start_clone3):
> +	cfi_undefined ($31)
> +	/* cp is already loaded.  */
> +	SAVE_GP (GPOFF)
> +	/* The stackframe has been created on entry of clone3.  */
> +
> +	/* Restore the arg for user's function.  */
> +	move		t9, a2		/* Function pointer.  */
> +	move		a0, $8		/* Argument pointer.  */
> +
> +	/* Call the user's function.  */
> +	jal		t9
> +
> +	move		a0, v0
> +	li		v0, __NR_exit
> +	syscall
> +END(__thread_start_clone3)
> +
> +libc_hidden_def (__clone3)
> +weak_alias (__clone3, clone3)
> diff --git a/sysdeps/unix/sysv/linux/mips/sysdep.h b/sysdeps/unix/sysv/linux/mips/sysdep.h
> index ff84a91b31..673aa08b57 100644
> --- a/sysdeps/unix/sysv/linux/mips/sysdep.h
> +++ b/sysdeps/unix/sysv/linux/mips/sysdep.h
> @@ -28,3 +28,5 @@
>  #endif
>  #define HAVE_GETTIMEOFDAY_VSYSCALL      "__vdso_gettimeofday"
>  #define HAVE_CLOCK_GETRES_VSYSCALL      "__vdso_clock_getres"
> +
> +#define HAVE_CLONE3_WRAPPER		1

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 3/5] riscv: Add the clone3 wrapper
  2023-02-03 17:12 ` [PATCH 3/5] riscv: " Adhemerval Zanella
  2023-05-25 17:36   ` Adhemerval Zanella Netto
@ 2023-05-29 14:04   ` Palmer Dabbelt
  2023-05-29 20:38     ` Adhemerval Zanella Netto
  1 sibling, 1 reply; 13+ messages in thread
From: Palmer Dabbelt @ 2023-05-29 14:04 UTC (permalink / raw)
  To: adhemerval.zanella; +Cc: libc-alpha

On Fri, 03 Feb 2023 09:12:35 PST (-0800), adhemerval.zanella@linaro.org wrote:
> It follows the internal signature:
>
>   extern int clone3 (struct clone_args *__cl_args, size_t __size,
>  int (*__func) (void *__arg), void *__arg);
>
> Checked on riscv64-linux-gnu-rv64imafdc-lp64d.

Due to a bug we only had clone3 on rv64 until 59a4e0d5511b ("RISC-V: 
Include clone3() on rv32"), which landed in 5.15 and was then backported 
all the way to 5.4.

> ---
>  sysdeps/unix/sysv/linux/riscv/clone3.S | 80 ++++++++++++++++++++++++++
>  sysdeps/unix/sysv/linux/riscv/sysdep.h |  1 +
>  2 files changed, 81 insertions(+)
>  create mode 100644 sysdeps/unix/sysv/linux/riscv/clone3.S
>
> diff --git a/sysdeps/unix/sysv/linux/riscv/clone3.S b/sysdeps/unix/sysv/linux/riscv/clone3.S
> new file mode 100644
> index 0000000000..ee5780ee2f
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/riscv/clone3.S
> @@ -0,0 +1,80 @@
> +/* The clone3 syscall wrapper.  Linux/RISC-V version.
> +   Copyright (C) 2023 Free Software Foundation, Inc.
> +
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <asm/errno.h>
> +#include <sys/asm.h>
> +#include <sysdep.h>
> +
> +/* The userland implementation is:
> +   int clone3 (struct clone_args *cl_args, size_t size,
> +               int (*func)(void *arg), void *arg);
> +
> +   the kernel entry is:
> +   int clone3 (struct clone_args *cl_args, size_t size);
> +
> +   The parameters are passed in registers from userland:
> +   a0: cl_args
> +   a1: size
> +   a2: func
> +   a3: arg  */
> +
> +        .text
> +ENTRY(__clone3)
> +	/* Sanity check args.  */
> +	beqz	a0, L(invalid)	/* No NULL cl_args pointer.  */
> +	beqz	a2, L(invalid)  /* No NULL function pointer.  */
> +
> +	/* Do the system call, the kernel expects:
> +	   a7: system call number
> +	   a0: cl_args
> +	   a1: size  */
> +	li	a7, __NR_clone3

IIUC this only builds with kernel headers 5.15 or newer on rv32.  I'm 
not sure if that's a problem: unless I'm missing something we support 
building for those systems now, but rv32 is pretty esoteric so I'm not 
sure anyone cares -- maybe someone uses 5.15 on rv32 (some rv64 hardware 
is still being released with 5.10-based vendor kernels), but 5.4 is 
pretty old for us.

> +	scall
> +
> +	bltz	a0, L(error)
> +	beqz	a0, L(thread_start)
> +
> +	ret
> +
> +L(invalid):
> +	li	a0, -EINVAL
> +L(error):
> +	tail	__syscall_error
> +END (__clone3)
> +
> +ENTRY(__thread_start_clone3)
> +L(thread_start):
> +	/* Terminate call stack by noting ra is undefined.  Use a dummy
> +	   .cfi_label to force starting the FDE.  */
> +	.cfi_label .Ldummy
> +	cfi_undefined (ra)
> +
> +	/* Restore the arg for user's function and call the user's
> +	   function.  */
> +	mv		a1, a2	/* Function pointer.  */

I think we don't need that mv, we can just call a2 directly?  Unless I'm 
missing some reason the thread main needs to see a pointer to itself?

> +	mv		a0, a3	/* Argument pointer.  */
> +	jalr		a1
> +
> +	/* Call exit with the function's return value.  */
> +	li		a7, __NR_exit
> +	scall
> +END(__thread_start_clone3)
> +
> +libc_hidden_def (__clone3)
> +weak_alias (__clone3, clone3)
> diff --git a/sysdeps/unix/sysv/linux/riscv/sysdep.h b/sysdeps/unix/sysv/linux/riscv/sysdep.h
> index 4af5fe5dbc..e96a930409 100644
> --- a/sysdeps/unix/sysv/linux/riscv/sysdep.h
> +++ b/sysdeps/unix/sysv/linux/riscv/sysdep.h
> @@ -151,6 +151,7 @@
>
>  /* RV32 does not support the gettime VDSO syscalls.  */
>  # endif
> +# define HAVE_CLONE3_WRAPPER		1
>
>  /* List of system calls which are supported as vsyscalls (for RV32 and
>     RV64).  */

That's just some minor comments though, so

Reviewed-by: Palmer Dabbelt <palmer@rivosinc.com>

Thanks! (and sorry I missed it for a bit)

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 3/5] riscv: Add the clone3 wrapper
  2023-05-29 14:04   ` Palmer Dabbelt
@ 2023-05-29 20:38     ` Adhemerval Zanella Netto
  0 siblings, 0 replies; 13+ messages in thread
From: Adhemerval Zanella Netto @ 2023-05-29 20:38 UTC (permalink / raw)
  To: Palmer Dabbelt; +Cc: libc-alpha



On 29/05/23 11:04, Palmer Dabbelt wrote:
> On Fri, 03 Feb 2023 09:12:35 PST (-0800), adhemerval.zanella@linaro.org wrote:
>> It follows the internal signature:
>>
>>   extern int clone3 (struct clone_args *__cl_args, size_t __size,
>>  int (*__func) (void *__arg), void *__arg);
>>
>> Checked on riscv64-linux-gnu-rv64imafdc-lp64d.
> 
> Due to a bug we only had clone3 on rv64 until 59a4e0d5511b ("RISC-V: Include clone3() on rv32"), which landed in 5.15 and was then backported all the way to 5.4.
> 
>> ---
>>  sysdeps/unix/sysv/linux/riscv/clone3.S | 80 ++++++++++++++++++++++++++
>>  sysdeps/unix/sysv/linux/riscv/sysdep.h |  1 +
>>  2 files changed, 81 insertions(+)
>>  create mode 100644 sysdeps/unix/sysv/linux/riscv/clone3.S
>>
>> diff --git a/sysdeps/unix/sysv/linux/riscv/clone3.S b/sysdeps/unix/sysv/linux/riscv/clone3.S
>> new file mode 100644
>> index 0000000000..ee5780ee2f
>> --- /dev/null
>> +++ b/sysdeps/unix/sysv/linux/riscv/clone3.S
>> @@ -0,0 +1,80 @@
>> +/* The clone3 syscall wrapper.  Linux/RISC-V version.
>> +   Copyright (C) 2023 Free Software Foundation, Inc.
>> +
>> +   This file is part of the GNU C Library.
>> +
>> +   The GNU C Library is free software; you can redistribute it and/or
>> +   modify it under the terms of the GNU Lesser General Public
>> +   License as published by the Free Software Foundation; either
>> +   version 2.1 of the License, or (at your option) any later version.
>> +
>> +   The GNU C Library is distributed in the hope that it will be useful,
>> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> +   Lesser General Public License for more details.
>> +
>> +   You should have received a copy of the GNU Lesser General Public
>> +   License along with the GNU C Library; if not, see
>> +   <https://www.gnu.org/licenses/>.  */
>> +
>> +#include <asm/errno.h>
>> +#include <sys/asm.h>
>> +#include <sysdep.h>
>> +
>> +/* The userland implementation is:
>> +   int clone3 (struct clone_args *cl_args, size_t size,
>> +               int (*func)(void *arg), void *arg);
>> +
>> +   the kernel entry is:
>> +   int clone3 (struct clone_args *cl_args, size_t size);
>> +
>> +   The parameters are passed in registers from userland:
>> +   a0: cl_args
>> +   a1: size
>> +   a2: func
>> +   a3: arg  */
>> +
>> +        .text
>> +ENTRY(__clone3)
>> +    /* Sanity check args.  */
>> +    beqz    a0, L(invalid)    /* No NULL cl_args pointer.  */
>> +    beqz    a2, L(invalid)  /* No NULL function pointer.  */
>> +
>> +    /* Do the system call, the kernel expects:
>> +       a7: system call number
>> +       a0: cl_args
>> +       a1: size  */
>> +    li    a7, __NR_clone3
> 
> IIUC this only builds with kernel headers 5.15 or newer on rv32.  I'm not sure if that's a problem: unless I'm missing something we support building for those systems now, but rv32 is pretty esoteric so I'm not sure anyone cares -- maybe someone uses 5.15 on rv32 (some rv64 hardware is still being released with 5.10-based vendor kernels), but 5.4 is pretty old for us.

The __NR_XXX macros are now provided by arch-syscall.h, which in turn is generated
by the kernel header. And we already have clone3 for rv32 since 3387c40a8bb.

> 
>> +    scall
>> +
>> +    bltz    a0, L(error)
>> +    beqz    a0, L(thread_start)
>> +
>> +    ret
>> +
>> +L(invalid):
>> +    li    a0, -EINVAL
>> +L(error):
>> +    tail    __syscall_error
>> +END (__clone3)
>> +
>> +ENTRY(__thread_start_clone3)
>> +L(thread_start):
>> +    /* Terminate call stack by noting ra is undefined.  Use a dummy
>> +       .cfi_label to force starting the FDE.  */
>> +    .cfi_label .Ldummy
>> +    cfi_undefined (ra)
>> +
>> +    /* Restore the arg for user's function and call the user's
>> +       function.  */
>> +    mv        a1, a2    /* Function pointer.  */
> 
> I think we don't need that mv, we can just call a2 directly?  Unless I'm missing some reason the thread main needs to see a pointer to itself?

We can, I will change it.

> 
>> +    mv        a0, a3    /* Argument pointer.  */
>> +    jalr        a1
>> +
>> +    /* Call exit with the function's return value.  */
>> +    li        a7, __NR_exit
>> +    scall
>> +END(__thread_start_clone3)
>> +
>> +libc_hidden_def (__clone3)
>> +weak_alias (__clone3, clone3)
>> diff --git a/sysdeps/unix/sysv/linux/riscv/sysdep.h b/sysdeps/unix/sysv/linux/riscv/sysdep.h
>> index 4af5fe5dbc..e96a930409 100644
>> --- a/sysdeps/unix/sysv/linux/riscv/sysdep.h
>> +++ b/sysdeps/unix/sysv/linux/riscv/sysdep.h
>> @@ -151,6 +151,7 @@
>>
>>  /* RV32 does not support the gettime VDSO syscalls.  */
>>  # endif
>> +# define HAVE_CLONE3_WRAPPER        1
>>
>>  /* List of system calls which are supported as vsyscalls (for RV32 and
>>     RV64).  */
> 
> That's just some minor comments though, so
> 
> Reviewed-by: Palmer Dabbelt <palmer@rivosinc.com>

Thanks.

> 
> Thanks! (and sorry I missed it for a bit)

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2023-05-29 20:38 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-03 17:12 [PATCH 0/5] Add clone3 support for multiple architectures Adhemerval Zanella
2023-02-03 17:12 ` [PATCH 1/5] powerpc64: Add the clone3 wrapper Adhemerval Zanella
2023-02-03 21:13   ` Paul E Murphy
2023-02-03 17:12 ` [PATCH 2/5] s390x: " Adhemerval Zanella
2023-05-25 17:36   ` Adhemerval Zanella Netto
2023-02-03 17:12 ` [PATCH 3/5] riscv: " Adhemerval Zanella
2023-05-25 17:36   ` Adhemerval Zanella Netto
2023-05-29 14:04   ` Palmer Dabbelt
2023-05-29 20:38     ` Adhemerval Zanella Netto
2023-02-03 17:12 ` [PATCH 4/5] arm: " Adhemerval Zanella
2023-05-25 17:36   ` Adhemerval Zanella Netto
2023-02-03 17:12 ` [PATCH 5/5] mips: " Adhemerval Zanella
2023-05-25 17:36   ` Adhemerval Zanella Netto

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).