blob: 2ae0e851c6c545eb73bc986cbd0d625903b0d54c [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
62 # the appropriate registers.
Elliott Hughes53bfdae2013-10-18 19:39:09 -070063 popq %rdi # fn
64 popq %rsi # arg
65 popq %rdx # tls
Elliott Hughes4906e562013-10-04 14:55:30 -070066 call __thread_entry
67 hlt
682:
69 ret
70
Elliott Hughes53bfdae2013-10-18 19:39:09 -070071// int __bionic_clone(unsigned long clone_flags,
72// void* new_sp,
73// int* parent_tid_ptr,
74// void* new_tls,
75// int* child_tid_ptr,
76// int (*fn)(void*),
77// void* arg);
Elliott Hughes4906e562013-10-04 14:55:30 -070078ENTRY(__bionic_clone)
Elliott Hughes53bfdae2013-10-18 19:39:09 -070079 # Enforce 16-byte alignment for child stack.
Elliott Hughes4906e562013-10-04 14:55:30 -070080 andq $~15, %rsi
Elliott Hughes53bfdae2013-10-18 19:39:09 -070081
82 # Copy 'fn' and 'arg' onto the child stack.
83 movq %r9, -16(%rsi) # fn
84 movq 8(%rsp), %rax # Read 'arg'.
85 movq %rax, -8(%rsi) # Write 'arg'.
Elliott Hughes4906e562013-10-04 14:55:30 -070086
87 subq $16, %rsi
88 movq %r8, %r10
89 movq %rcx, %r8
90 movl $__NR_clone, %eax
91 syscall
92 testl %eax, %eax
93 jns 1f
94
Elliott Hughes53bfdae2013-10-18 19:39:09 -070095 # An error occurred, set errno and return -1.
Elliott Hughes4906e562013-10-04 14:55:30 -070096 negl %eax
97 movl %eax, %edi
98 call __set_errno
99 orl $-1, %eax
100 jmp 2f
Elliott Hughes4906e562013-10-04 14:55:30 -07001011:
102 jnz 2f
103
Elliott Hughes53bfdae2013-10-18 19:39:09 -0700104 # We're in the child now, so call __bionic_clone_entry
105 # with the arguments from the child stack moved into
106 # the appropriate registers.
107 popq %rdi # fn
108 popq %rsi # arg
Elliott Hughes4906e562013-10-04 14:55:30 -0700109 call __bionic_clone_entry
110 hlt
Elliott Hughes4906e562013-10-04 14:55:30 -07001112:
112 ret