|
Packit |
6c4009 |
/* Copyright (C) 1996-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
Contributed by David Huggins-Daines <dhd@debian.org>, 2000.
|
|
Packit |
6c4009 |
Based on the Alpha version by Richard Henderson <rth@tamu.edu>, 1996.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library. If not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* clone() is even more special than fork() as it mucks with stacks
|
|
Packit |
6c4009 |
and invokes a function in the right context after its all over. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <asm/unistd.h>
|
|
Packit |
6c4009 |
#include <sysdep.h>
|
|
Packit |
6c4009 |
#define _ERRNO_H 1
|
|
Packit |
6c4009 |
#include <bits/errno.h>
|
|
Packit |
6c4009 |
#include <tcb-offsets.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Non-thread code calls __clone with the following parameters:
|
|
Packit |
6c4009 |
int clone(int (*fn)(void *arg),
|
|
Packit |
6c4009 |
void *child_stack,
|
|
Packit |
6c4009 |
int flags,
|
|
Packit |
6c4009 |
void *arg)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
NPTL Code will call __clone with the following parameters:
|
|
Packit |
6c4009 |
int clone(int (*fn)(void *arg),
|
|
Packit |
6c4009 |
void *child_stack,
|
|
Packit |
6c4009 |
int flags,
|
|
Packit |
6c4009 |
void *arg,
|
|
Packit |
6c4009 |
int *parent_tidptr,
|
|
Packit |
6c4009 |
struct user_desc *newtls,
|
|
Packit |
6c4009 |
int *child_pidptr)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The code should not mangle the extra input registers.
|
|
Packit |
6c4009 |
Syscall expects: Input to __clone:
|
|
Packit |
6c4009 |
4(r25) - function pointer (r26, arg0)
|
|
Packit |
6c4009 |
0(r25) - argument (r23, arg3)
|
|
Packit |
6c4009 |
r26 - clone flags. (r24, arg2)
|
|
Packit |
6c4009 |
r25+64 - user stack pointer. (r25, arg1)
|
|
Packit |
6c4009 |
r24 - parent tid pointer. (stack - 52)
|
|
Packit |
6c4009 |
r23 - struct user_desc newtls pointer. (stack - 56)
|
|
Packit |
6c4009 |
r22 - child tid pointer. (stack - 60)
|
|
Packit |
6c4009 |
r20 - clone syscall number (constant)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
Return:
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
On success the thread ID of the child process is returend in
|
|
Packit |
6c4009 |
the callers context.
|
|
Packit |
6c4009 |
On error return -1, and set errno to the value returned by
|
|
Packit |
6c4009 |
the syscall.
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.text
|
|
Packit |
6c4009 |
ENTRY(__clone)
|
|
Packit |
6c4009 |
/* Prologue */
|
|
Packit |
6c4009 |
stwm %r4, 64(%sp)
|
|
Packit |
6c4009 |
.cfi_def_cfa_offset -64
|
|
Packit |
6c4009 |
.cfi_offset 4, 0
|
|
Packit |
6c4009 |
stw %sp, -4(%sp)
|
|
Packit |
6c4009 |
#ifdef PIC
|
|
Packit |
6c4009 |
stw %r19, -32(%sp)
|
|
Packit |
6c4009 |
.cfi_offset 19, 32
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Sanity check arguments. */
|
|
Packit |
6c4009 |
comib,=,n 0, %arg0, .LerrorSanity /* no NULL function pointers */
|
|
Packit |
6c4009 |
comib,=,n 0, %arg1, .LerrorSanity /* no NULL stack pointers */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Save the function pointer, arg, and flags on the new stack. */
|
|
Packit |
6c4009 |
stwm %r26, 64(%r25)
|
|
Packit |
6c4009 |
stw %r23, -60(%r25)
|
|
Packit |
6c4009 |
stw %r24, -56(%r25)
|
|
Packit |
6c4009 |
/* Clone arguments are (int flags, void * child_stack) */
|
|
Packit |
6c4009 |
copy %r24, %r26 /* flags are first */
|
|
Packit |
6c4009 |
/* User stack pointer is in the correct register already */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Load args from stack... */
|
|
Packit |
6c4009 |
ldw -116(%sp), %r24 /* Load parent_tidptr */
|
|
Packit |
6c4009 |
ldw -120(%sp), %r23 /* Load newtls */
|
|
Packit |
6c4009 |
ldw -124(%sp), %r22 /* Load child_tidptr */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Save the PIC register. */
|
|
Packit |
6c4009 |
#ifdef PIC
|
|
Packit |
6c4009 |
copy %r19, %r4 /* parent */
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Do the system call */
|
|
Packit |
6c4009 |
ble 0x100(%sr2, %r0)
|
|
Packit |
6c4009 |
ldi __NR_clone, %r20
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ldi -4096, %r1
|
|
Packit |
6c4009 |
comclr,>>= %r1, %ret0, %r0 /* Note: unsigned compare. */
|
|
Packit |
6c4009 |
b,n .LerrorRest
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Restore the PIC register. */
|
|
Packit |
6c4009 |
#ifdef PIC
|
|
Packit |
6c4009 |
copy %r4, %r19 /* parent */
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
comib,=,n 0, %ret0, .LthreadStart
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Successful return from the parent
|
|
Packit |
6c4009 |
No need to restore the PIC register,
|
|
Packit |
6c4009 |
since we return immediately. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ldw -84(%sp), %rp
|
|
Packit |
6c4009 |
bv %r0(%rp)
|
|
Packit |
6c4009 |
ldwm -64(%sp), %r4
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.LerrorRest:
|
|
Packit |
6c4009 |
/* Something bad happened -- no child created */
|
|
Packit |
6c4009 |
bl __syscall_error, %rp
|
|
Packit |
6c4009 |
sub %r0, %ret0, %arg0
|
|
Packit |
6c4009 |
ldw -84(%sp), %rp
|
|
Packit |
6c4009 |
/* Return after setting errno, ret0 is set to -1 by __syscall_error. */
|
|
Packit |
6c4009 |
bv %r0(%rp)
|
|
Packit |
6c4009 |
ldwm -64(%sp), %r4
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.LerrorSanity:
|
|
Packit |
6c4009 |
/* Sanity checks failed, return -1, and set errno to EINVAL. */
|
|
Packit |
6c4009 |
bl __syscall_error, %rp
|
|
Packit |
6c4009 |
ldi EINVAL, %arg0
|
|
Packit |
6c4009 |
ldw -84(%sp), %rp
|
|
Packit |
6c4009 |
bv %r0(%rp)
|
|
Packit |
6c4009 |
ldwm -64(%sp), %r4
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.LthreadStart:
|
|
Packit |
6c4009 |
/* Load up the arguments. */
|
|
Packit |
6c4009 |
ldw -60(%sp), %arg0
|
|
Packit |
6c4009 |
ldw -64(%sp), %r22
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* $$dyncall fixes child's PIC register */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Call the user's function */
|
|
Packit |
6c4009 |
#ifdef PIC
|
|
Packit |
6c4009 |
copy %r19, %r4
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
bl $$dyncall, %r31
|
|
Packit |
6c4009 |
copy %r31, %rp
|
|
Packit |
6c4009 |
#ifdef PIC
|
|
Packit |
6c4009 |
copy %r4, %r19
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
copy %r28, %r26
|
|
Packit |
6c4009 |
ble 0x100(%sr2, %r0)
|
|
Packit |
6c4009 |
ldi __NR_exit, %r20
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* We should not return from exit.
|
|
Packit |
6c4009 |
We do not restore r4, or the stack state. */
|
|
Packit |
6c4009 |
iitlbp %r0, (%sr0, %r0)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
PSEUDO_END(__clone)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
libc_hidden_def (__clone)
|
|
Packit |
6c4009 |
weak_alias (__clone, clone)
|