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
|
#include <stdint.h>
extern "C" int __cxa_guard_acquire(volatile uint64_t* gv)
{
// 0 -> uninitialized
// 1 -> locked
// 2 -> unlocked and initialized
register volatile void* guard = gv;
register uint32_t c = 0;
asm volatile(
"__cxa_guard_acquire_begin:"
"lwarx %0,0,%1;" // Load guard with reserve
"cmpi 0,%0,0;" // Compare with 0
"bne+ __cxa_guard_acquire_finish;" // != 0, goto "finished"
"li %0, 1;" // Set to 1.
"stwcx. %0,0,%1;" // Store against reserve
"bne- __cxa_guard_acquire_begin;" // goto begin if failed store.
"li %0, 3;" // Set to 3 -> success in lock
"__cxa_guard_acquire_finish:"
: "+r" (c) : "r" (guard): "memory","cc"
);
while (2 > c)
{
asm volatile("lwz %0, 0(%1);" : "=r" (c) : "r" (guard));
}
return (3 == c ? 1 : 0); // 3 means success in lock, return 1 (obtained)
// 2 means initialized, return 0
}
extern "C" void __cxa_guard_release(volatile uint64_t* gv)
{
register volatile void* guard = gv;
register uint32_t c = 2;
asm volatile("stw %0, 0(%1)" :: "r"(c) , "r" (guard): "memory");
return;
}
extern "C" int __cxa_atexit(void (*)(void*), void*, void*)
{
return 0;
}
void* __dso_handle = (void*) &__dso_handle;
|