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
29#include <private/bionic_asm.h>
30#include <linux/errno.h>
31#include <linux/sched.h>
32
33#if (_MIPS_SIM == _ABIO32) || (_MIPS_SIM == _ABI32)
34FRAMESZ		=	MKFSIZ(NARGSAVE,0)
35FRAME_ARG	=	0*REGSZ
36FRAME_FN	=	1*REGSZ
37#else
38FRAMESZ		=	MKFSIZ(0,3)
39FRAME_GP	=	FRAMESZ-1*REGSZ
40FRAME_ARG	=	FRAMESZ-2*REGSZ
41FRAME_FN	=	FRAMESZ-3*REGSZ
42#endif
43
44// pid_t __bionic_clone(int flags, void* child_stack, pid_t* parent_tid, void* tls, pid_t* child_tid, int (*fn)(void*), void* arg);
45LEAF(__bionic_clone, FRAMESZ)
46	PTR_SUBU sp, FRAMESZ			# allocate stack frame
47	SETUP_GP64(FRAME_GP,__bionic_clone)
48	SAVE_GP(FRAME_GP)
49
50	# set up child stack
51	PTR_SUBU a1,FRAMESZ
52#if (_MIPS_SIM == _ABIO32) || (_MIPS_SIM == _ABI32)
53	PTR_L	t0,FRAMESZ+5*REGSZ(sp)	# fn
54	PRL_L	t1,FRAMESZ+6*REGSZ(sp)	# arg
55	PTR_S	t0,FRAME_FN(a1)		# fn
56	PTR_S	t1,FRAME_ARG(a1)	# arg
57#else
58	PTR_L	t0,FRAME_GP(sp)		# copy gp to child stack
59	PTR_S	t0,FRAME_GP(a1)
60	PTR_S	a5,FRAME_FN(a1)		# fn
61	PTR_S	a6,FRAME_ARG(a1)	# arg
62# endif
63
64	# remainder of arguments are correct for clone system call
65	LI	v0,__NR_clone
66	syscall
67
68	move    a0,v0
69	bnez	a3,.L__error_bc
70
71	beqz	v0,.L__thread_start_bc
72
73	RESTORE_GP64
74	PTR_ADDU sp,FRAMESZ
75	j	ra
76
77.L__thread_start_bc:
78	# Clear return address in child so we don't unwind further.
79	li	ra,0
80
81	# void __start_thread(int (*func)(void*), void *arg)
82	PTR_L	a0,FRAME_FN(sp)		#  fn
83	PTR_L	a1,FRAME_ARG(sp)	#  arg
84	LA	t9,__start_thread
85	RESTORE_GP64
86	/*
87	 * For O32 etc the child stack must have space for a0..a3 to be stored
88	 * For N64 etc, the child stack can be restored to the original value
89	 */
90#if !((_MIPS_SIM == _ABIO32) || (_MIPS_SIM == _ABI32))
91	PTR_ADDU sp,FRAMESZ
92#endif
93	j	t9
94
95.L__error_bc:
96	LA	t9,__set_errno_internal
97	RESTORE_GP64
98	PTR_ADDU sp,FRAMESZ
99	j	t9
100	END(__bionic_clone)
101.hidden __bionic_clone
102