blob: d725efc3a61d96a0647cb277b2579ef052a7e764 [file] [log] [blame]
Chris Dearman645d0312014-02-05 18:51:43 -08001/*
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
Elliott Hughes851e68a2014-02-19 16:53:20 -080029#include <private/bionic_asm.h>
Chris Dearman645d0312014-02-05 18:51:43 -080030#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);
Elliott Hughes851e68a2014-02-19 16:53:20 -080045LEAF(__bionic_clone, FRAMESZ)
Elliott Hughesab413c52017-10-13 13:22:24 -070046 PTR_SUBU $sp, FRAMESZ # allocate stack frame
Chris Dearman645d0312014-02-05 18:51:43 -080047 SETUP_GP64(FRAME_GP,__bionic_clone)
48 SAVE_GP(FRAME_GP)
49
50 # set up child stack
Elliott Hughesab413c52017-10-13 13:22:24 -070051 PTR_SUBU $a1,FRAMESZ
Chris Dearman645d0312014-02-05 18:51:43 -080052#if (_MIPS_SIM == _ABIO32) || (_MIPS_SIM == _ABI32)
Elliott Hughesab413c52017-10-13 13:22:24 -070053 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
Chris Dearman645d0312014-02-05 18:51:43 -080057#else
Elliott Hughesab413c52017-10-13 13:22:24 -070058 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
Chris Dearman645d0312014-02-05 18:51:43 -080062# endif
63
64 # remainder of arguments are correct for clone system call
Elliott Hughesab413c52017-10-13 13:22:24 -070065 LI $v0,__NR_clone
Chris Dearman645d0312014-02-05 18:51:43 -080066 syscall
67
Elliott Hughesab413c52017-10-13 13:22:24 -070068 move $a0,$v0
69 bnez $a3,.L__error_bc
Chris Dearman645d0312014-02-05 18:51:43 -080070
Elliott Hughesab413c52017-10-13 13:22:24 -070071 beqz $v0,.L__thread_start_bc
Chris Dearman645d0312014-02-05 18:51:43 -080072
73 RESTORE_GP64
Elliott Hughesab413c52017-10-13 13:22:24 -070074 PTR_ADDU $sp,FRAMESZ
75 j $ra
Chris Dearman645d0312014-02-05 18:51:43 -080076
77.L__thread_start_bc:
Elliott Hughese7dccdf2014-05-27 15:47:32 -070078 # Clear return address in child so we don't unwind further.
Elliott Hughesab413c52017-10-13 13:22:24 -070079 li $ra,0
Elliott Hughese7dccdf2014-05-27 15:47:32 -070080
Elliott Hughesebc8cd12014-06-06 15:18:54 -070081 # void __start_thread(int (*func)(void*), void *arg)
Elliott Hughesab413c52017-10-13 13:22:24 -070082 PTR_L $a0,FRAME_FN($sp) # fn
83 PTR_L $a1,FRAME_ARG($sp) # arg
84 LA $t9,__start_thread
Chris Dearman645d0312014-02-05 18:51:43 -080085 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))
Elliott Hughesab413c52017-10-13 13:22:24 -070091 PTR_ADDU $sp,FRAMESZ
Chris Dearman645d0312014-02-05 18:51:43 -080092#endif
Elliott Hughesab413c52017-10-13 13:22:24 -070093 j $t9
Chris Dearman645d0312014-02-05 18:51:43 -080094
95.L__error_bc:
Elliott Hughesab413c52017-10-13 13:22:24 -070096 LA $t9,__set_errno_internal
Chris Dearman645d0312014-02-05 18:51:43 -080097 RESTORE_GP64
Elliott Hughesab413c52017-10-13 13:22:24 -070098 PTR_ADDU $sp,FRAMESZ
99 j $t9
Chris Dearman645d0312014-02-05 18:51:43 -0800100 END(__bionic_clone)
Elliott Hughes954cf0d2014-05-08 19:00:23 -0700101.hidden __bionic_clone