| 1 | /* The clone3 syscall wrapper. Linux/arc version. |
| 2 | Copyright (C) 2023-2024 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of the GNU C Library. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <https://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <sysdep.h> |
| 21 | #define _ERRNO_H 1 |
| 22 | #include <bits/errno.h> |
| 23 | |
| 24 | /* The userland implementation is: |
| 25 | int clone3 (struct clone_args *cl_args, size_t size, |
| 26 | int (*func)(void *arg), void *arg); |
| 27 | |
| 28 | the kernel entry is: |
| 29 | int clone3 (struct clone_args *cl_args, size_t size); |
| 30 | |
| 31 | The parameters are passed in registers from userland: |
| 32 | r0: cl_args |
| 33 | r1: size |
| 34 | r2: func |
| 35 | r3: arg */ |
| 36 | |
| 37 | ENTRY(__clone3) |
| 38 | |
| 39 | /* Save args for the child. */ |
| 40 | mov r10, r0 /* cl_args */ |
| 41 | mov r11, r2 /* func */ |
| 42 | mov r12, r3 /* args */ |
| 43 | |
| 44 | /* Sanity check args. */ |
| 45 | breq r10, 0, L (__sys_err) /* No NULL cl_args pointer. */ |
| 46 | breq r11, 0, L (__sys_err) /* No NULL function pointer. */ |
| 47 | |
| 48 | /* Do the system call, the kernel expects: |
| 49 | r8: system call number |
| 50 | r0: cl_args |
| 51 | r1: size */ |
| 52 | mov r0, r10 |
| 53 | mov r8, __NR_clone3 |
| 54 | ARC_TRAP_INSN |
| 55 | |
| 56 | cmp r0, 0 |
| 57 | beq thread_start_clone3 /* Child returns. */ |
| 58 | blt L (__sys_err2) |
| 59 | j [blink] /* Parent returns. */ |
| 60 | |
| 61 | L (__sys_err): |
| 62 | mov r0, -EINVAL |
| 63 | L (__sys_err2): |
| 64 | b __syscall_error |
| 65 | PSEUDO_END (__clone3) |
| 66 | |
| 67 | |
| 68 | .align 4 |
| 69 | .type thread_start_clone3, %function |
| 70 | thread_start_clone3: |
| 71 | cfi_startproc |
| 72 | /* Terminate call stack by noting ra is undefined. */ |
| 73 | cfi_undefined (blink) |
| 74 | |
| 75 | /* Child jumps off to @fn with @arg as argument. */ |
| 76 | jl.d [r11] |
| 77 | mov r0, r12 |
| 78 | |
| 79 | /* exit() with result from @fn (already in r0). */ |
| 80 | mov r8, __NR_exit |
| 81 | ARC_TRAP_INSN |
| 82 | |
| 83 | /* In case it ever came back. */ |
| 84 | flag 1 |
| 85 | |
| 86 | cfi_endproc |
| 87 | .size thread_start_clone3, .-thread_start_clone3 |
| 88 | |
| 89 | libc_hidden_def (__clone3) |
| 90 | weak_alias (__clone3, clone3) |
| 91 | |