Skip to content

Commit

Permalink
ds/aba: prototype CHERI MIPS LL/SC implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nwf committed Jul 30, 2019
1 parent adbfc8b commit bb81acc
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/ds/aba.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace snmalloc
"Expecting ABA to be the size of two pointers");

using Cmp = Linked;
#elif defined(__mips__) && defined(__CHERI_PURE_CAPABILITY__)
using Cmp = T*;
#else
using Cmp = T*;
#endif
Expand All @@ -40,6 +42,8 @@ namespace snmalloc
alignas(2 * sizeof(std::size_t)) std::atomic<Linked> linked;
Independent independent;
};
#elif defined(__mips__) && defined(__CHERI_PURE_CAPABILITY__)
T* raw;
#else
std::atomic<T*> raw;
#endif
Expand All @@ -56,6 +60,8 @@ namespace snmalloc
#ifdef PLATFORM_IS_X86
independent.ptr.store(x, std::memory_order_relaxed);
independent.aba.store(0, std::memory_order_relaxed);
#elif defined(__mips__) && defined(__CHERI_PURE_CAPABILITY__)
raw = x;
#else
raw.store(x, std::memory_order_relaxed);
#endif
Expand All @@ -66,19 +72,27 @@ namespace snmalloc
return
#ifdef PLATFORM_IS_X86
independent.ptr.load(std::memory_order_relaxed);
#elif defined(__mips__) && defined(__CHERI_PURE_CAPABILITY__)
raw;
#else
raw.load(std::memory_order_relaxed);
#endif
}

Cmp read()
{
return
#ifdef PLATFORM_IS_X86
Cmp{independent.ptr.load(std::memory_order_relaxed),
independent.aba.load(std::memory_order_relaxed)};
return Cmp{independent.ptr.load(std::memory_order_relaxed),
independent.aba.load(std::memory_order_relaxed)};
#elif defined(__mips__) && defined(__CHERI_PURE_CAPABILITY__)
T* tp;
asm("cllc %[tp], %[raw]\n"
: [tp] "=C"(tp)
: [raw] "C"(&this->raw)
: "memory");
return tp;
#else
raw.load(std::memory_order_relaxed);
return raw.load(std::memory_order_relaxed);
#endif
}

Expand Down Expand Up @@ -109,6 +123,25 @@ namespace snmalloc
return linked.compare_exchange_weak(
expect, xchg, std::memory_order_relaxed, std::memory_order_relaxed);
# endif
#elif defined(__mips__) && defined(__CHERI_PURE_CAPABILITY__)
int res;
T* tv = this->raw;
T* e = ptr(expect);
asm(
"cexeq %[res], %[tv], %[expect]\n\t"
"beqz %[res], 1f\n\t"
"nop\n\t"
"cscc %[res], %[value], %[raw]\n\t"
"1:\n"
: [res] "=r"(res)
:
[raw] "C"(&this->raw), [tv] "C"(tv), [expect] "C"(e), [value] "C"(value)
: "memory");
if (!res)
{
expect = read();
}
return static_cast<bool>(res);
#else
return raw.compare_exchange_weak(
expect, value, std::memory_order_relaxed, std::memory_order_relaxed);
Expand Down

0 comments on commit bb81acc

Please sign in to comment.