123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #include <stdint.h>
- #include <assert.h>
- #include "se_cdefs.h"
- SGX_ACCESS_VERSION(tstdcxx, 3)
- #ifdef __arm__
- extern "C" int __cxa_guard_acquire(volatile int32_t *guard_object)
- {
- if ((1<<31) == *guard_object) { return 0; }
-
-
- if (__sync_bool_compare_and_swap(guard_object, 0, 1))
- {
- return 1;
- }
-
-
- while (__sync_bool_compare_and_swap(guard_object, (1<<31), (1<<31)))
- {
-
- if (__sync_bool_compare_and_swap(guard_object, 0, 1))
- {
- return 1;
- }
- sched_yield();
- }
- return 0;
- }
- extern "C" void __cxa_guard_abort(int32_t *guard_object)
- {
- assert(__sync_bool_compare_and_swap(guard_object, 1, 0));
- }
- extern "C" void __cxa_guard_release(int32_t *guard_object)
- {
- assert(__sync_bool_compare_and_swap(guard_object, 1, (1<<31)));
- }
- #else
- static int32_t *low_32_bits(volatile int64_t *ptr)
- {
- int32_t *low= (int32_t*)ptr;
-
-
- int one = 1;
- if (*(char*)&one != 1)
- {
- low++;
- }
- return low;
- }
- extern "C" int __cxa_guard_acquire(volatile int64_t *guard_object)
- {
- char first_byte = (*guard_object) >> 56;
- if (1 == first_byte) { return 0; }
- int32_t *lock = low_32_bits(guard_object);
-
-
-
-
- while (!__sync_bool_compare_and_swap_4(lock, 0, 1))
- {
- if (1 == ((*guard_object) >> 56))
- {
- break;
- }
-
-
-
- }
-
-
- first_byte = (*guard_object) >> 56;
- return (1 != first_byte);
- }
- extern "C" void __cxa_guard_abort(int64_t *guard_object)
- {
- int32_t *lock = low_32_bits(guard_object);
- *lock = 0;
- }
- extern "C" void __cxa_guard_release(int64_t *guard_object)
- {
-
- *guard_object |= ((int64_t)1) << 56;
- __cxa_guard_abort(guard_object);
- }
- #endif
|