blob: 7511e86731bdf850bbba3b1581103be7e52b413f [file] [log] [blame]
Elliott Hughes4906e562013-10-04 14:55:30 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <asm/unistd.h>
30#include <machine/asm.h>
31
32// int __pthread_clone(void* (*fn)(void*), void* tls, int flags, void* arg);
33ENTRY(__pthread_clone)
Elliott Hughes938f38d2013-10-17 22:22:31 -070034 # Save tls.
Elliott Hughes4906e562013-10-04 14:55:30 -070035 movq %rsi, %r11
Elliott Hughes53bfdae2013-10-18 19:39:09 -070036 # Enforce 16-byte alignment for child stack.
Elliott Hughes4906e562013-10-04 14:55:30 -070037 andq $~15, %rsi
38
Elliott Hughes53bfdae2013-10-18 19:39:09 -070039 # Copy 'fn', 'arg', and 'tls' onto the child stack.
40 movq %rdi, -32(%rsi) # fn
41 movq %rcx, -24(%rsi) # arg
42 movq %r11, -16(%rsi) # tls
Elliott Hughes4906e562013-10-04 14:55:30 -070043 subq $32, %rsi
Elliott Hughes938f38d2013-10-17 22:22:31 -070044
Elliott Hughes4906e562013-10-04 14:55:30 -070045 movq %rdx, %rdi
46 movl $__NR_clone, %eax
47 syscall
48 testl %eax, %eax
49 jns 1f
50
Elliott Hughes938f38d2013-10-17 22:22:31 -070051 # An error occurred, set errno and return -1.
Elliott Hughes4906e562013-10-04 14:55:30 -070052 negl %eax
53 movl %eax, %edi
54 call __set_errno
55 orl $-1, %eax
56 jmp 2f
571:
58 jnz 2f
59
Elliott Hughes53bfdae2013-10-18 19:39:09 -070060 # We're in the child now, so call __thread_entry
Elliott Hughes938f38d2013-10-17 22:22:31 -070061 # with the arguments from the child stack moved into
Pavel Chupin33a209e2013-10-21 20:20:53 +040062 # the appropriate registers. We avoid pop here to keep
63 # the required 16-byte stack alignment.
64 movq (%rsp), %rdi # fn
65 movq 8(%rsp), %rsi # arg
66 movq 16(%rsp), %rdx # tls
Elliott Hughes4906e562013-10-04 14:55:30 -070067 call __thread_entry
68 hlt
692:
70 ret
71
Elliott Hughes53bfdae2013-10-18 19:39:09 -070072// int __bionic_clone(unsigned long clone_flags,
73// void* new_sp,
74// int* parent_tid_ptr,
75// void* new_tls,
76// int* child_tid_ptr,
77// int (*fn)(void*),
78// void* arg);
Elliott Hughes4906e562013-10-04 14:55:30 -070079ENTRY(__bionic_clone)
Elliott Hughes53bfdae2013-10-18 19:39:09 -070080 # Enforce 16-byte alignment for child stack.
Elliott Hughes4906e562013-10-04 14:55:30 -070081 andq $~15, %rsi
Elliott Hughes53bfdae2013-10-18 19:39:09 -070082
83 # Copy 'fn' and 'arg' onto the child stack.
84 movq %r9, -16(%rsi) # fn
85 movq 8(%rsp), %rax # Read 'arg'.
86 movq %rax, -8(%rsi) # Write 'arg'.
Elliott Hughes4906e562013-10-04 14:55:30 -070087
88 subq $16, %rsi
89 movq %r8, %r10
90 movq %rcx, %r8
91 movl $__NR_clone, %eax
92 syscall
93 testl %eax, %eax
94 jns 1f
95
Elliott Hughes53bfdae2013-10-18 19:39:09 -070096 # An error occurred, set errno and return -1.
Elliott Hughes4906e562013-10-04 14:55:30 -070097 negl %eax
98 movl %eax, %edi
99 call __set_errno
100 orl $-1, %eax
101 jmp 2f
Elliott Hughes4906e562013-10-04 14:55:30 -07001021:
103 jnz 2f
104
Elliott Hughes53bfdae2013-10-18 19:39:09 -0700105 # We're in the child now, so call __bionic_clone_entry
106 # with the arguments from the child stack moved into
107 # the appropriate registers.
108 popq %rdi # fn
109 popq %rsi # arg
Elliott Hughes4906e562013-10-04 14:55:30 -0700110 call __bionic_clone_entry
111 hlt
Elliott Hughes4906e562013-10-04 14:55:30 -07001122:
113 ret