1/*
2 * Copyright (C) 2008 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#include <linux/sched.h>
29#include <sys/linux-syscalls.h>
30#include <linux/errno.h>
31
32	.text
33	.type __pthread_clone, @function
34	.global __pthread_clone
35	.align 4
36        .ent __pthread_clone
37
38/*
39 * int __pthread_clone(int (*fn)(void*), void *child_stack,
40 *			 int flags, void *arg);
41 */
42
43__pthread_clone:
44        .set	noreorder
45        .cpload $t9
46        .set	reorder
47
48	# set up child stack
49	subu	$a1,16
50	sw	$a0,0($a1)	# fn
51	sw	$a3,4($a1)	# arg
52#	sw	$a1+16,8($a1)	# tls
53
54	/*
55	 * int sys_clone(int flags, void *child_stack, int *parent_tidptr,
56	 *	 struct user_desc *newtls, int *child_tidptr);
57	 */
58
59	move	$a0,$a2		# flags
60#	move	$a1,$a1		# child_stack
61	move	$a2,$0		# parent_tidptr
62	move	$a3,$0		# user_desc
63	and	$a0,~(CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID)
64				# make sure the kernel doesn't access child_tidptr
65
66        li	$v0,__NR_clone
67        syscall
68
69        bnez	$a3,.L__error
70
71        beqz	$v0,.L__thread_start
72
73        j $ra
74
75.L__thread_start:
76        lw	$a0,0($sp)	#  fn
77        lw	$a1,4($sp)	#  arg
78        addu	$a2,$sp,16	#  tls
79
80	# void __thread_entry(int (*func)(void*), void *arg, void *tls)
81        la	$t9, __thread_entry
82        j	$t9
83
84.L__error:
85	move	$a0,$v0
86	la	$t9,__set_errno
87	j	$t9
88
89        .end __pthread_clone
90
91
92    #
93    # This function is defined as:
94    #
95    #   pid_t  __bionic_clone( int  flags, void *child_stack,
96    #                          pid_t *pid, void *tls, pid_t *ctid,
97    #                          int  (*fn)(void *), void* arg );
98    #
99    # NOTE: This is not the same signature than the GLibc
100    #       __clone function here !! Placing 'fn' and 'arg'
101    #       at the end of the parameter list makes the
102    #       implementation much simpler.
103    #
104	.text
105	.type __bionic_clone, @function
106	.global __bionic_clone
107	.align 4
108        .ent __bionic_clone
109__bionic_clone:
110        .set	noreorder
111        .cpload $t9
112        .set	reorder
113
114	# set up child stack
115	subu	$a1,16
116	lw	$t0,20($sp)     # fn
117	lw	$t1,24($sp)     # arg
118	sw	$t0,0($a1)	# fn
119	sw	$t1,4($a1)	# arg
120
121	# remainder of arguments are correct for clone system call
122        li	$v0,__NR_clone
123        syscall
124
125        bnez	$a3,.L__error_bc
126
127        beqz	$v0,.L__thread_start_bc
128
129        j $ra
130
131.L__thread_start_bc:
132        lw	$a0,0($sp)	#  fn
133        lw	$a1,4($sp)	#  arg
134
135	# void __bionic_clone_entry(int (*func)(void*), void *arg)
136        la	$t9,__bionic_clone_entry
137        j	$t9
138
139.L__error_bc:
140	move	$a0,$v0
141	la	$t9,__set_errno
142	j	$t9
143
144        .end __bionic_clone
145
146