1 /* The clone3 syscall wrapper. Linux/i386 version.
2 Copyright (C) 2021-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* clone3() is even more special than fork() as it mucks with stacks
20 and invokes a function in the right context after its all over. */
24 /* The userland implementation is:
25 int clone3 (struct clone_args *cl_args, size_t size,
26 int (*func)(void *arg), void *arg);
28 int clone3 (struct clone_args *cl_args, size_t size);
30 The parameters are passed on stack from userland:
38 eax: system call number
50 /* Sanity check arguments. */
52 movl CL_ARGS(%esp), %ecx /* No NULL cl_args pointer. */
54 jz SYSCALL_ERROR_LABEL
55 /* Save the function pointer in EDX which is preserved by the
57 movl FUNC(%esp), %edx /* No NULL function pointer. */
59 jz SYSCALL_ERROR_LABEL
61 /* Save EBX and ESI. */
63 cfi_adjust_cfa_offset (4)
65 cfi_adjust_cfa_offset (4)
67 /* Save the function argument in ESI which is preserved by the
69 movl (ARG + 8)(%esp), %esi
71 /* Put cl_args in EBX. */
74 /* Put size in ECX. */
75 movl (SIZE + 8)(%esp), %ecx
77 /* Do the system call. */
78 movl $SYS_ify(clone3), %eax
80 /* End FDE now, because in the child the unwind info will be
86 /* No need to restore EBX and ESI in child. */
89 /* Restore EBX and ESI in parent. */
92 jl SYSCALL_ERROR_LABEL
98 /* Clearing frame pointer is insufficient, use CFI. */
100 xorl %ebp, %ebp /* Terminate the stack frame. */
102 /* Align stack to 16 bytes per the i386 psABI. */
105 /* The PUSH below will decrement stack pointer by 4 bytes. */
108 /* Set up the argument for the function call. */
109 pushl %esi /* Argument. */
110 cfi_adjust_cfa_offset (4)
111 call *%edx /* Call function. */
113 /* Call exit with return value from function call. */
115 movl $SYS_ify(exit), %eax
120 PSEUDO_END (__clone3)
122 libc_hidden_def (__clone3)
123 weak_alias (__clone3, clone3)