1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/* The clone3 syscall wrapper. Linux/i386 version.
Copyright (C) 2021-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/>. */
/* clone3() is even more special than fork() as it mucks with stacks
and invokes a function in the right context after its all over. */
#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 on stack from userland:
16(%esp) arg
12(%esp) func
8(%esp) size
4(%esp) cl_args
(%esp) Return address
The kernel expects:
eax: system call number
ebx: cl_args
ecx: size
*/
#define CL_ARGS 4
#define SIZE 8
#define FUNC 12
#define ARG 16
.text
ENTRY (__clone3)
/* Sanity check arguments. */
movl $-EINVAL, %eax
movl CL_ARGS(%esp), %ecx /* No NULL cl_args pointer. */
testl %ecx, %ecx
jz SYSCALL_ERROR_LABEL
/* Save the function pointer in EDX which is preserved by the
system call. */
movl FUNC(%esp), %edx /* No NULL function pointer. */
testl %edx, %edx
jz SYSCALL_ERROR_LABEL
/* Save EBX and ESI. */
pushl %ebx
cfi_adjust_cfa_offset (4)
pushl %esi
cfi_adjust_cfa_offset (4)
/* Save the function argument in ESI which is preserved by the
system call. */
movl (ARG + 8)(%esp), %esi
/* Put cl_args in EBX. */
movl %ecx, %ebx
/* Put size in ECX. */
movl (SIZE + 8)(%esp), %ecx
/* Do the system call. */
movl $SYS_ify(clone3), %eax
/* End FDE now, because in the child the unwind info will be
wrong. */
cfi_endproc
int $0x80
test %eax, %eax
/* No need to restore EBX and ESI in child. */
jz L(thread_start)
/* Restore EBX and ESI in parent. */
pop %esi
pop %ebx
jl SYSCALL_ERROR_LABEL
ret
L(thread_start):
cfi_startproc
/* Clearing frame pointer is insufficient, use CFI. */
cfi_undefined (eip)
xorl %ebp, %ebp /* Terminate the stack frame. */
/* Align stack to 16 bytes per the i386 psABI. */
andl $-16, %esp
/* The PUSH below will decrement stack pointer by 4 bytes. */
subl $12, %esp
/* Set up the argument for the function call. */
pushl %esi /* Argument. */
cfi_adjust_cfa_offset (4)
call *%edx /* Call function. */
/* Call exit with return value from function call. */
movl %eax, %ebx
movl $SYS_ify(exit), %eax
ENTER_KERNEL
cfi_endproc
cfi_startproc
PSEUDO_END (__clone3)
libc_hidden_def (__clone3)
weak_alias (__clone3, clone3)
|