]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
sparc: Add architecture support for clone3
authorLudwig Rydberg <ludwig.rydberg@gaisler.com>
Mon, 19 Jan 2026 14:47:53 +0000 (15:47 +0100)
committerAndreas Larsson <andreas@gaisler.com>
Fri, 6 Feb 2026 14:03:43 +0000 (15:03 +0100)
Add support for the clone3 system call to the SPARC architectures.

The implementation follows the pattern of the original clone syscall.
However, instead of explicitly calling kernel_clone, the clone3
handler calls the generic sys_clone3 handler in kernel/fork.
In case no stack is provided, the parents stack is reused.

The return value convention for clone3 follows the regular kernel return
value convention (in contrast to the original clone/fork on SPARC).

Closes: https://github.com/sparclinux/issues/issues/10
Signed-off-by: Ludwig Rydberg <ludwig.rydberg@gaisler.com>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Andreas Larsson <andreas@gaisler.com>
Tested-by: Andreas Larsson <andreas@gaisler.com>
Tested-by: John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Link: https://lore.kernel.org/r/20260119144753.27945-3-ludwig.rydberg@gaisler.com
Signed-off-by: Andreas Larsson <andreas@gaisler.com>
arch/sparc/include/asm/syscalls.h
arch/sparc/include/asm/unistd.h
arch/sparc/kernel/entry.S
arch/sparc/kernel/kernel.h
arch/sparc/kernel/process.c
arch/sparc/kernel/process_32.c
arch/sparc/kernel/process_64.c
arch/sparc/kernel/syscalls.S
arch/sparc/kernel/syscalls/syscall.tbl

index 35575fbfb9dce94f0ebb8ced1af496b6780d0bed..282e62b66518330517a5b13b30a16dd054722acc 100644 (file)
@@ -7,5 +7,6 @@ struct pt_regs;
 asmlinkage long sparc_fork(struct pt_regs *regs);
 asmlinkage long sparc_vfork(struct pt_regs *regs);
 asmlinkage long sparc_clone(struct pt_regs *regs);
+asmlinkage long sparc_clone3(struct pt_regs *regs);
 
 #endif /* _SPARC64_SYSCALLS_H */
index 3380411a4537b160cea0ded288df78e331015af2..d6bc76706a7aa416dab25cdb6e6e82f54cb9891e 100644 (file)
@@ -49,8 +49,6 @@
 #define __ARCH_WANT_COMPAT_STAT
 #endif
 
-#define __ARCH_BROKEN_SYS_CLONE3
-
 #ifdef __32bit_syscall_numbers__
 /* Sparc 32-bit only has the "setresuid32", "getresuid32" variants,
  * it never had the plain ones and there is no value to adding those
index a3fdee4cd6fa318575d0115c4f0e8af8f94c2b59..ea51ef52c952a1baf805167ca4334a0cf8c8ad14 100644 (file)
@@ -907,6 +907,21 @@ flush_patch_four:
        jmpl    %l1 + %lo(sparc_vfork), %g0
         add    %sp, STACKFRAME_SZ, %o0
 
+       .globl  __sys_clone3, flush_patch_five
+__sys_clone3:
+       mov     %o7, %l5
+flush_patch_five:
+       FLUSH_ALL_KERNEL_WINDOWS;
+       ld      [%curptr + TI_TASK], %o4
+       rd      %psr, %g4
+       WRITE_PAUSE
+       rd      %wim, %g5
+       WRITE_PAUSE
+       std     %g4, [%o4 + AOFF_task_thread + AOFF_thread_fork_kpsr]
+       add     %sp, STACKFRAME_SZ, %o0
+       call    sparc_clone3
+        mov    %l5, %o7
+
         .align  4
 linux_sparc_ni_syscall:
        sethi   %hi(sys_ni_syscall), %l7
index 8328a3b78a44418095c67443cd4c39895bbe13c3..4ee85051521a323cacef7b40e3ecff334f45c0a3 100644 (file)
@@ -18,6 +18,7 @@ extern int ncpus_probed;
 asmlinkage long sparc_clone(struct pt_regs *regs);
 asmlinkage long sparc_fork(struct pt_regs *regs);
 asmlinkage long sparc_vfork(struct pt_regs *regs);
+asmlinkage long sparc_clone3(struct pt_regs *regs);
 
 #ifdef CONFIG_SPARC64
 /* setup_64.c */
index 7d69877511fac96a41b1699ebeb9eca8f4a4e120..d72fa066594303c8a964dc77b4d8a337efad29b0 100644 (file)
@@ -12,6 +12,7 @@
 #include <linux/sched/task.h>
 #include <linux/sched/task_stack.h>
 #include <linux/signal.h>
+#include <linux/syscalls.h>
 
 #include "kernel.h"
 
@@ -118,3 +119,16 @@ asmlinkage long sparc_clone(struct pt_regs *regs)
 
        return ret;
 }
+
+asmlinkage long sparc_clone3(struct pt_regs *regs)
+{
+       unsigned long sz;
+       struct clone_args __user *cl_args;
+
+       synchronize_user_stack();
+
+       cl_args = (struct clone_args __user *)regs->u_regs[UREG_I0];
+       sz = regs->u_regs[UREG_I1];
+
+       return sys_clone3(cl_args, sz);
+}
index 5a28c0e91bf15f32161423a4cfe1b4f4c19688d9..dd8c6c02b0f1677a573daad360576c5adc039d6b 100644 (file)
@@ -247,6 +247,8 @@ clone_stackframe(struct sparc_stackf __user *dst,
  * Parent -->  %o0 == childs  pid, %o1 == 0
  * Child  -->  %o0 == parents pid, %o1 == 1
  *
+ * clone3() - Uses regular kernel return value conventions
+ *
  * NOTE: We have a separate fork kpsr/kwim because
  *       the parent could change these values between
  *       sys_fork invocation and when we reach here
@@ -261,11 +263,11 @@ extern void ret_from_kernel_thread(void);
 int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 {
        u64 clone_flags = args->flags;
-       unsigned long sp = args->stack;
        unsigned long tls = args->tls;
        struct thread_info *ti = task_thread_info(p);
        struct pt_regs *childregs, *regs = current_pt_regs();
        char *new_stack;
+       unsigned long sp = args->stack ? args->stack : regs->u_regs[UREG_FP];
 
 #ifndef CONFIG_SMP
        if(last_task_used_math == current) {
@@ -350,13 +352,22 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
        childregs->psr &= ~PSR_EF;
        clear_tsk_thread_flag(p, TIF_USEDFPU);
 #endif
+       /* Handle return value conventions */
+       if (regs->u_regs[UREG_G1] == __NR_clone3) {
+               /* clone3() - use regular kernel return value convention */
+
+               /* Set the return value for the child. */
+               childregs->u_regs[UREG_I0] = 0;
+       } else {
+               /* clone()/fork() - use SunOS return value convention */
 
-       /* Set the return value for the child. */
-       childregs->u_regs[UREG_I0] = current->pid;
-       childregs->u_regs[UREG_I1] = 1;
+               /* Set the return value for the child. */
+               childregs->u_regs[UREG_I0] = current->pid;
+               childregs->u_regs[UREG_I1] = 1;
 
-       /* Set the return value for the parent. */
-       regs->u_regs[UREG_I1] = 0;
+               /* Set the return value for the parent. */
+               regs->u_regs[UREG_I1] = 0;
+       }
 
        if (clone_flags & CLONE_SETTLS)
                childregs->u_regs[UREG_G7] = tls;
index 25781923788a03e1fad80b5674fa6e1ff86fefe8..e889da8e483583b93d6edb580ec72b5104d3152a 100644 (file)
@@ -564,17 +564,19 @@ barf:
  * under SunOS are nothing short of bletcherous:
  * Parent -->  %o0 == childs  pid, %o1 == 0
  * Child  -->  %o0 == parents pid, %o1 == 1
+ *
+ * clone3() - Uses regular kernel return value conventions
  */
 int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 {
        u64 clone_flags = args->flags;
-       unsigned long sp = args->stack;
        unsigned long tls = args->tls;
        struct thread_info *t = task_thread_info(p);
        struct pt_regs *regs = current_pt_regs();
        struct sparc_stackf *parent_sf;
        unsigned long child_stack_sz;
        char *child_trap_frame;
+       unsigned long sp = args->stack ? args->stack : regs->u_regs[UREG_FP];
 
        /* Calculate offset to stack_frame & pt_regs */
        child_stack_sz = (STACKFRAME_SZ + TRACEREG_SZ);
@@ -616,12 +618,25 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
        if (t->utraps)
                t->utraps[0]++;
 
-       /* Set the return value for the child. */
-       t->kregs->u_regs[UREG_I0] = current->pid;
-       t->kregs->u_regs[UREG_I1] = 1;
+       /* Handle return value conventions */
+       if (regs->u_regs[UREG_G1] == __NR_clone3) {
+               /* clone3() - use regular kernel return value convention */
+
+               /* Set the return value for the child. */
+               t->kregs->u_regs[UREG_I0] = 0;
+
+               /* Clear g1 to indicate user thread */
+               t->kregs->u_regs[UREG_G1] = 0;
+       } else {
+               /* clone()/fork() - use SunOS return value convention */
+
+               /* Set the return value for the child. */
+               t->kregs->u_regs[UREG_I0] = current->pid;
+               t->kregs->u_regs[UREG_I1] = 1;
 
-       /* Set the second return value for the parent. */
-       regs->u_regs[UREG_I1] = 0;
+               /* Set the second return value for the parent. */
+               regs->u_regs[UREG_I1] = 0;
+       }
 
        if (clone_flags & CLONE_SETTLS)
                t->kregs->u_regs[UREG_G7] = tls;
index 0e8ab0602c360bdc267680e5000e22ec5766b5c1..96fe8763d70cebceed657ac98a13d2834fe5b40c 100644 (file)
@@ -103,6 +103,12 @@ sys_clone:
        ba,pt   %xcc, sparc_clone
         add    %sp, PTREGS_OFF, %o0
 
+       .align  32
+__sys_clone3:
+       flushw
+       ba,pt   %xcc, sparc_clone3
+        add    %sp, PTREGS_OFF, %o0
+
        .globl  ret_from_fork
 ret_from_fork:
        /* Clear current_thread_info()->new_child. */
@@ -113,6 +119,8 @@ ret_from_fork:
        brnz,pt %o0, ret_sys_call
         ldx    [%g6 + TI_FLAGS], %l0
        ldx     [%sp + PTREGS_OFF + PT_V9_G1], %l1
+       brz,pt  %l1, ret_sys_call
+        nop
        call    %l1
         ldx    [%sp + PTREGS_OFF + PT_V9_G2], %o0
        ba,pt   %xcc, ret_sys_call
index 39aa26b6a50be7e02ba2400b797d128f8c6d3499..c0307bb098921f3e7072a139ebcaf6970aac2e57 100644 (file)
 432    common  fsmount                         sys_fsmount
 433    common  fspick                          sys_fspick
 434    common  pidfd_open                      sys_pidfd_open
-# 435 reserved for clone3
+435    common  clone3                          __sys_clone3
 436    common  close_range                     sys_close_range
 437    common  openat2                 sys_openat2
 438    common  pidfd_getfd                     sys_pidfd_getfd