blob: b74a8b4d055fb4a219141abaa2776871b0c2279d [file] [log] [blame]
David 'Digit' Turner03eabfe2009-05-28 15:54:03 +02001/*
2 * Copyright (C) 2009 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#ifndef _BIONIC_PREINIT_H
30#define _BIONIC_PREINIT_H
31
32#include "pthread_internal.h"
33#include "bionic_tls.h"
34#include <asm/page.h>
35
36/* this function is used to perform a minimal initialization of the
37 * the C library. This must be performed before any other call to
38 * other functions, in either the dynamic linker's startup code
39 * or libc_init_static.c
40 *
41 * 'main_thread' and 'tls_area' must be persistent variables,
42 * which means they must be either global, or allocated in the
43 * main thread's stack frame and never, ever, freed before
44 * program exit.
45 *
46 * VERY IMPORTANT NOTE:
47 *
48 * THIS IMPLEMENTATION SHOULD NOT USE GLOBAL VARIABLES.
49 *
50 * The reason is the dynamic linker's tricky usage of C library
51 * functions and later renaming of their symbols. See the
52 * "C Library Usage Restriction" section in bionic/linker/README.TXT
53 * for details.
54 */
55
56extern unsigned __get_sp(void);
57extern pid_t gettid(void);
58
59static __inline__ void
60__libc_preinit( pthread_internal_t* main_thread,
61 void* tls_area[BIONIC_TLS_SLOTS] )
62{
63 pthread_attr_t thread_attr;
64
65 /* Setup the main thread's information */
66 unsigned stacktop = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE;
67 unsigned stacksize = 128 * 1024;
68 unsigned stackbottom = stacktop - stacksize;
69
70 pthread_attr_init(&thread_attr);
71 pthread_attr_setstack(&thread_attr, (void*)stackbottom, stacksize);
72 _init_thread(main_thread, gettid(), &thread_attr, (void*)stackbottom);
73 __init_tls(tls_area, main_thread);
74}
75
76#endif /* _BIONIC_INIT_H */