summaryrefslogtreecommitdiffstats
path: root/src/libc++/builtins.C
blob: 22336fa51097c23ac21dcdba049c8d66437866f9 (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
#include <stdint.h>
#include <stdlib.h>

#include <arch/ppc.H>
#include <util/locked/list.H>
//#include <kernel/console.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;
    void * dso_handle;

    DtorEntry_t * next;
    DtorEntry_t * prev;
};

Util::Locked::List<DtorEntry_t, DtorEntry_t::key_type> g_dtorRegistry;


/**
 * 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)
{
    DtorEntry_t * entry = NULL;
    // A module is never exited by different threads so 
    // assume no locking needed here.
    while( NULL != (entry = g_dtorRegistry.find(i_dso_handle)) )
    {
        g_dtorRegistry.erase(entry);  // remove from list
        (*(entry->dtor))(entry->arg);
        delete entry;
    }
}


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)
    {
        // printk("Register dtor for %p\n",i_dso_handle);
        DtorEntry_t * entry = new DtorEntry_t;
        entry->key = i_dso_handle;
        entry->dtor = i_dtor;
        entry->arg = i_arg;
        g_dtorRegistry.insert(entry);
    }
    return 0;
}



OpenPOWER on IntegriCloud