1*cb14a3feSDimitry Andric#include <string.h>
2*cb14a3feSDimitry Andric#include <sys/auxv.h>
3*cb14a3feSDimitry Andric#include <sys/system_properties.h>
4*cb14a3feSDimitry Andric
5*cb14a3feSDimitry Andricstatic bool __isExynos9810(void) {
6*cb14a3feSDimitry Andric  char arch[PROP_VALUE_MAX];
7*cb14a3feSDimitry Andric  return __system_property_get("ro.arch", arch) > 0 &&
8*cb14a3feSDimitry Andric    strncmp(arch, "exynos9810", sizeof("exynos9810") - 1) == 0;
9*cb14a3feSDimitry Andric}
10*cb14a3feSDimitry Andric
11*cb14a3feSDimitry Andricstatic void CONSTRUCTOR_ATTRIBUTE init_have_lse_atomics(void) {
12*cb14a3feSDimitry Andric  unsigned long hwcap = getauxval(AT_HWCAP);
13*cb14a3feSDimitry Andric  _Bool result = (hwcap & HWCAP_ATOMICS) != 0;
14*cb14a3feSDimitry Andric  if (result) {
15*cb14a3feSDimitry Andric    // Some cores in the Exynos 9810 CPU are ARMv8.2 and others are ARMv8.0;
16*cb14a3feSDimitry Andric    // only the former support LSE atomics.  However, the kernel in the
17*cb14a3feSDimitry Andric    // initial Android 8.0 release of Galaxy S9/S9+ devices incorrectly
18*cb14a3feSDimitry Andric    // reported the feature as being supported.
19*cb14a3feSDimitry Andric    //
20*cb14a3feSDimitry Andric    // The kernel appears to have been corrected to mark it unsupported as of
21*cb14a3feSDimitry Andric    // the Android 9.0 release on those devices, and this issue has not been
22*cb14a3feSDimitry Andric    // observed anywhere else. Thus, this workaround may be removed if
23*cb14a3feSDimitry Andric    // compiler-rt ever drops support for Android 8.0.
24*cb14a3feSDimitry Andric    if (__isExynos9810())
25*cb14a3feSDimitry Andric      result = false;
26*cb14a3feSDimitry Andric  }
27*cb14a3feSDimitry Andric  __aarch64_have_lse_atomics = result;
28*cb14a3feSDimitry Andric}
29