1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2009
3*49cdfc7eSAndroid Build Coastguard Worker * Some wrappers for clone functionality. Thrown together by Serge Hallyn
4*49cdfc7eSAndroid Build Coastguard Worker * <[email protected]> based on existing clone usage in ltp.
5*49cdfc7eSAndroid Build Coastguard Worker *
6*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
7*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
8*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
9*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
12*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
15*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker #ifndef _GNU_SOURCE
21*49cdfc7eSAndroid Build Coastguard Worker # define _GNU_SOURCE
22*49cdfc7eSAndroid Build Coastguard Worker #endif
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <sched.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <stdarg.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
32*49cdfc7eSAndroid Build Coastguard Worker #include "tst_clone.h"
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker #undef clone /* we want to use clone() */
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker /*
37*49cdfc7eSAndroid Build Coastguard Worker * The ia64 port has never included a prototype for __clone2(). It was updated
38*49cdfc7eSAndroid Build Coastguard Worker * to take eight parameters in glibc commit:
39*49cdfc7eSAndroid Build Coastguard Worker *
40*49cdfc7eSAndroid Build Coastguard Worker * commit 625f22fc7f8e0d61e3e6cff2c65468b91dbad426
41*49cdfc7eSAndroid Build Coastguard Worker * Author: Ulrich Drepper <[email protected]>
42*49cdfc7eSAndroid Build Coastguard Worker * Date: Mon Mar 3 19:53:27 2003 +0000
43*49cdfc7eSAndroid Build Coastguard Worker *
44*49cdfc7eSAndroid Build Coastguard Worker * The first release that contained this commit was glibc-2.3.3 which is old
45*49cdfc7eSAndroid Build Coastguard Worker * enough to assume that __clone2() takes eight parameters.
46*49cdfc7eSAndroid Build Coastguard Worker */
47*49cdfc7eSAndroid Build Coastguard Worker #if defined(__ia64__)
48*49cdfc7eSAndroid Build Coastguard Worker extern int __clone2(int (*fn) (void *arg), void *child_stack_base,
49*49cdfc7eSAndroid Build Coastguard Worker size_t child_stack_size, int flags, void *arg,
50*49cdfc7eSAndroid Build Coastguard Worker pid_t *parent_tid, void *tls, pid_t *child_tid);
51*49cdfc7eSAndroid Build Coastguard Worker #endif
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker /*
54*49cdfc7eSAndroid Build Coastguard Worker * ltp_clone: wrapper for clone to hide the architecture dependencies.
55*49cdfc7eSAndroid Build Coastguard Worker * 1. hppa takes bottom of stack and no stacksize (stack grows up)
56*49cdfc7eSAndroid Build Coastguard Worker * 2. __ia64__ takes bottom of stack and uses clone2
57*49cdfc7eSAndroid Build Coastguard Worker * 3. all others take top of stack (stack grows down)
58*49cdfc7eSAndroid Build Coastguard Worker */
59*49cdfc7eSAndroid Build Coastguard Worker static int
ltp_clone_(unsigned long flags,int (* fn)(void * arg),void * arg,size_t stack_size,void * stack,pid_t * ptid,void * tls,pid_t * ctid)60*49cdfc7eSAndroid Build Coastguard Worker ltp_clone_(unsigned long flags, int (*fn)(void *arg), void *arg,
61*49cdfc7eSAndroid Build Coastguard Worker size_t stack_size, void *stack, pid_t *ptid, void *tls, pid_t *ctid)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker int ret;
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker #if defined(__ia64__)
66*49cdfc7eSAndroid Build Coastguard Worker ret = __clone2(fn, stack, stack_size, flags, arg, ptid, tls, ctid);
67*49cdfc7eSAndroid Build Coastguard Worker #else
68*49cdfc7eSAndroid Build Coastguard Worker # if defined(__hppa__) || defined(__metag__)
69*49cdfc7eSAndroid Build Coastguard Worker /*
70*49cdfc7eSAndroid Build Coastguard Worker * These arches grow their stack up, so don't need to adjust the base.
71*49cdfc7eSAndroid Build Coastguard Worker * XXX: This should be made into a runtime test.
72*49cdfc7eSAndroid Build Coastguard Worker */
73*49cdfc7eSAndroid Build Coastguard Worker # else
74*49cdfc7eSAndroid Build Coastguard Worker /*
75*49cdfc7eSAndroid Build Coastguard Worker * For archs where stack grows downwards, stack points to the topmost
76*49cdfc7eSAndroid Build Coastguard Worker * address of the memory space set up for the child stack.
77*49cdfc7eSAndroid Build Coastguard Worker */
78*49cdfc7eSAndroid Build Coastguard Worker if (stack)
79*49cdfc7eSAndroid Build Coastguard Worker stack += stack_size;
80*49cdfc7eSAndroid Build Coastguard Worker # endif
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker ret = clone(fn, stack, flags, arg, ptid, tls, ctid);
83*49cdfc7eSAndroid Build Coastguard Worker #endif
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker return ret;
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker
ltp_clone(unsigned long flags,int (* fn)(void * arg),void * arg,size_t stack_size,void * stack)88*49cdfc7eSAndroid Build Coastguard Worker int ltp_clone(unsigned long flags, int (*fn)(void *arg), void *arg,
89*49cdfc7eSAndroid Build Coastguard Worker size_t stack_size, void *stack)
90*49cdfc7eSAndroid Build Coastguard Worker {
91*49cdfc7eSAndroid Build Coastguard Worker return ltp_clone_(flags, fn, arg, stack_size, stack, NULL, NULL, NULL);
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker
ltp_clone7(unsigned long flags,int (* fn)(void * arg),void * arg,size_t stack_size,void * stack,...)94*49cdfc7eSAndroid Build Coastguard Worker int ltp_clone7(unsigned long flags, int (*fn)(void *arg), void *arg,
95*49cdfc7eSAndroid Build Coastguard Worker size_t stack_size, void *stack, ...)
96*49cdfc7eSAndroid Build Coastguard Worker {
97*49cdfc7eSAndroid Build Coastguard Worker pid_t *ptid, *ctid;
98*49cdfc7eSAndroid Build Coastguard Worker void *tls;
99*49cdfc7eSAndroid Build Coastguard Worker va_list arg_clone;
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker va_start(arg_clone, stack);
102*49cdfc7eSAndroid Build Coastguard Worker ptid = va_arg(arg_clone, pid_t *);
103*49cdfc7eSAndroid Build Coastguard Worker tls = va_arg(arg_clone, void *);
104*49cdfc7eSAndroid Build Coastguard Worker ctid = va_arg(arg_clone, pid_t *);
105*49cdfc7eSAndroid Build Coastguard Worker va_end(arg_clone);
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker return ltp_clone_(flags, fn, arg, stack_size, stack, ptid, tls, ctid);
108*49cdfc7eSAndroid Build Coastguard Worker }
109*49cdfc7eSAndroid Build Coastguard Worker
110*49cdfc7eSAndroid Build Coastguard Worker /*
111*49cdfc7eSAndroid Build Coastguard Worker * ltp_alloc_stack: allocate stack of size 'size', that is sufficiently
112*49cdfc7eSAndroid Build Coastguard Worker * aligned for all arches. User is responsible for freeing allocated
113*49cdfc7eSAndroid Build Coastguard Worker * memory.
114*49cdfc7eSAndroid Build Coastguard Worker * Returns pointer to new stack. On error, returns NULL with errno set.
115*49cdfc7eSAndroid Build Coastguard Worker */
ltp_alloc_stack(size_t size)116*49cdfc7eSAndroid Build Coastguard Worker void *ltp_alloc_stack(size_t size)
117*49cdfc7eSAndroid Build Coastguard Worker {
118*49cdfc7eSAndroid Build Coastguard Worker void *ret = NULL;
119*49cdfc7eSAndroid Build Coastguard Worker int err;
120*49cdfc7eSAndroid Build Coastguard Worker
121*49cdfc7eSAndroid Build Coastguard Worker err = posix_memalign(&ret, 64, size);
122*49cdfc7eSAndroid Build Coastguard Worker if (err)
123*49cdfc7eSAndroid Build Coastguard Worker errno = err;
124*49cdfc7eSAndroid Build Coastguard Worker
125*49cdfc7eSAndroid Build Coastguard Worker return ret;
126*49cdfc7eSAndroid Build Coastguard Worker }
127