summaryrefslogtreecommitdiffstats
path: root/src/libc++/builtins.C
blob: 02953d72d54ba246066534b905cd85432ef6f545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/libc++/builtins.C $                                       */
/*                                                                        */
/* IBM CONFIDENTIAL                                                       */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2010,2013              */
/*                                                                        */
/* p1                                                                     */
/*                                                                        */
/* Object Code Only (OCO) source materials                                */
/* Licensed Internal Code Source Materials                                */
/* IBM HostBoot Licensed Internal Code                                    */
/*                                                                        */
/* The source code for this program is not published or otherwise         */
/* divested of its trade secrets, irrespective of what has been           */
/* deposited with the U.S. Copyright Office.                              */
/*                                                                        */
/* Origin: 30                                                             */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#include <stdint.h>
#include <stdlib.h>

#include <arch/ppc.H>
#include <util/locked/list.H>
#include <sys/sync.h>

void* operator new(size_t s)
{
    return malloc(s);
};

void* operator new[](size_t s)
{
    return malloc(s);
};

void* operator new(size_t s, void *place)
{
    return place;
}

void* operator new[](size_t s, void *place)
{
    return place;
}

void operator delete(void* p)
{
    return free(p);
};

void operator delete[](void* p)
{
    return free(p);
};

extern "C" int __cxa_guard_acquire(volatile uint64_t* gv)
{
    // States:
    //     0 -> uninitialized
    //     1 -> locked
    //     2 -> unlocked and initialized

    uint32_t v = __sync_val_compare_and_swap((volatile uint32_t*)gv, 0, 1);
    if (v == 0)
	return 1;
    if (v == 2)
	return 0;

    // Wait for peer thread to perform initialization (state 2).
    while(2 != *(volatile uint32_t*)gv);

    // Instruction barrier to ensure value is set before later loads execute.
    isync();

    return 0;
}

extern "C" void __cxa_guard_release(volatile uint64_t* gv)
{
    // Memory barrier to ensure all preceeding writes have completed before
    // releasing guard.
    lwsync();

    (*(volatile uint32_t*)gv) = 2;
    return;
}

extern "C" void __cxa_pure_virtual()
{
    // TODO: Add better code for invalid pure virtual call.
    while(1);
}

// ----------------------------------------------------------------------------
//   Module exit support
// ----------------------------------------------------------------------------


// This one is just for the base object.  Each module has it's own giving
// each module a unique value for __dso_handle.
void*   __dso_handle = (void*) &__dso_handle;

struct DtorEntry_t
{
    typedef void * key_type;
    key_type    key;
    void (*dtor)(void*);
    void * arg;

    DtorEntry_t * next;
    DtorEntry_t * prev;
};

Util::Locked::List<DtorEntry_t, DtorEntry_t::key_type> g_dtorRegistry;
mutex_t g_dtorLock = MUTEX_INITIALIZER;


/**
 * Call all the static destructors for the module identified by i_dso_handle
 * @param[in] i_dso_handle  unique identifier for a module
 * @post matching dtor functions called and then removed from the dtor registry
 */
void call_dtors(void * i_dso_handle)
{
    mutex_lock(&g_dtorLock);

    DtorEntry_t * entry = NULL;
    while( NULL != (entry = g_dtorRegistry.find(i_dso_handle)) )
    {
        g_dtorRegistry.erase(entry);  // remove from list
        (*(entry->dtor))(entry->arg);
        delete entry;
    }

    mutex_unlock(&g_dtorLock);
}


extern "C" int __cxa_atexit(void (*i_dtor)(void*),
                            void* i_arg,
                            void* i_dso_handle)
{
    // Base kernel code may try to call this before mem heap is
    // available - so don't add it.
    // TODO - Only need dtors for extended image modules
    if(i_dso_handle != __dso_handle)
    {
        DtorEntry_t * entry = new DtorEntry_t();
        entry->key = i_dso_handle;
        entry->dtor = i_dtor;
        entry->arg = i_arg;

        mutex_lock(&g_dtorLock);
        g_dtorRegistry.insert(entry);
        mutex_unlock(&g_dtorLock);
    }
    return 0;
}



OpenPOWER on IntegriCloud