Path: blob/main/sys/contrib/ck/include/gcc/aarch64/ck_pr_llsc.h
48422 views
/*1* Copyright 2009-2016 Samy Al Bahra.2* Copyright 2013-2016 Olivier Houchard.3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#ifndef CK_PR_AARCH64_LLSC_H28#define CK_PR_AARCH64_LLSC_H2930#ifndef CK_PR_H31#error Do not include this file directly, use ck_pr.h32#endif3334CK_CC_INLINE static bool35ck_pr_cas_64_2_value(uint64_t target[2], uint64_t compare[2], uint64_t set[2], uint64_t value[2])36{37uint64_t tmp1, tmp2;3839__asm__ __volatile__("1:"40"ldxp %0, %1, [%4]\n"41"mov %2, %0\n"42"mov %3, %1\n"43"eor %0, %0, %5\n"44"eor %1, %1, %6\n"45"orr %1, %0, %1\n"46"mov %w0, #0\n"47"cbnz %1, 2f\n"48"stxp %w0, %7, %8, [%4]\n"49"cbnz %w0, 1b\n"50"mov %w0, #1\n"51"2:"52: "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])53: "r" (target), "r" (compare[0]), "r" (compare[1]), "r" (set[0]), "r" (set[1])54: "cc", "memory");5556return (tmp1);57}5859CK_CC_INLINE static bool60ck_pr_cas_ptr_2_value(void *target, void *compare, void *set, void *value)61{62return (ck_pr_cas_64_2_value(CK_CPP_CAST(uint64_t *, target),63CK_CPP_CAST(uint64_t *, compare),64CK_CPP_CAST(uint64_t *, set),65CK_CPP_CAST(uint64_t *, value)));66}6768CK_CC_INLINE static bool69ck_pr_cas_64_2(uint64_t target[2], uint64_t compare[2], uint64_t set[2])70{71uint64_t tmp1, tmp2;7273__asm__ __volatile__("1:"74"ldxp %0, %1, [%2]\n"75"eor %0, %0, %3\n"76"eor %1, %1, %4\n"77"orr %1, %0, %1\n"78"mov %w0, #0\n"79"cbnz %1, 2f\n"80"stxp %w0, %5, %6, [%2]\n"81"cbnz %w0, 1b\n"82"mov %w0, #1\n"83"2:"84: "=&r" (tmp1), "=&r" (tmp2)85: "r" (target), "r" (compare[0]), "r" (compare[1]), "r" (set[0]), "r" (set[1])86: "cc", "memory");8788return (tmp1);89}90CK_CC_INLINE static bool91ck_pr_cas_ptr_2(void *target, void *compare, void *set)92{93return (ck_pr_cas_64_2(CK_CPP_CAST(uint64_t *, target),94CK_CPP_CAST(uint64_t *, compare),95CK_CPP_CAST(uint64_t *, set)));96}979899#define CK_PR_CAS(N, M, T, W, R) \100CK_CC_INLINE static bool \101ck_pr_cas_##N##_value(M *target, T compare, T set, M *value) \102{ \103T previous; \104T tmp; \105__asm__ __volatile__("1:\n" \106"ldxr" W " %" R "0, [%2]\n" \107"cmp %" R "0, %" R "4\n" \108"b.ne 2f\n" \109"stxr" W " %w1, %" R "3, [%2]\n" \110"cbnz %w1, 1b\n" \111"2:" \112: "=&r" (previous), \113"=&r" (tmp) \114: "r" (target), \115"r" (set), \116"r" (compare) \117: "memory", "cc"); \118*(T *)value = previous; \119return (previous == compare); \120} \121CK_CC_INLINE static bool \122ck_pr_cas_##N(M *target, T compare, T set) \123{ \124T previous; \125T tmp; \126__asm__ __volatile__( \127"1:" \128"ldxr" W " %" R "0, [%2]\n" \129"cmp %" R "0, %" R "4\n" \130"b.ne 2f\n" \131"stxr" W " %w1, %" R "3, [%2]\n" \132"cbnz %w1, 1b\n" \133"2:" \134: "=&r" (previous), \135"=&r" (tmp) \136: "r" (target), \137"r" (set), \138"r" (compare) \139: "memory", "cc"); \140return (previous == compare); \141}142143CK_PR_CAS(ptr, void, void *, "", "")144145#define CK_PR_CAS_S(N, M, W, R) CK_PR_CAS(N, M, M, W, R)146CK_PR_CAS_S(64, uint64_t, "", "")147#ifndef CK_PR_DISABLE_DOUBLE148CK_PR_CAS_S(double, double, "", "")149#endif150CK_PR_CAS_S(32, uint32_t, "", "w")151CK_PR_CAS_S(uint, unsigned int, "", "w")152CK_PR_CAS_S(int, int, "", "w")153CK_PR_CAS_S(16, uint16_t, "h", "w")154CK_PR_CAS_S(8, uint8_t, "b", "w")155CK_PR_CAS_S(short, short, "h", "w")156CK_PR_CAS_S(char, char, "b", "w")157158159#undef CK_PR_CAS_S160#undef CK_PR_CAS161162#define CK_PR_FAS(N, M, T, W, R) \163CK_CC_INLINE static T \164ck_pr_fas_##N(M *target, T v) \165{ \166T previous; \167T tmp; \168__asm__ __volatile__("1:" \169"ldxr" W " %" R "0, [%2]\n"\170"stxr" W " %w1, %" R "3, [%2]\n"\171"cbnz %w1, 1b\n" \172: "=&r" (previous), \173"=&r" (tmp) \174: "r" (target), \175"r" (v) \176: "memory", "cc"); \177return (previous); \178}179180CK_PR_FAS(64, uint64_t, uint64_t, "", "")181CK_PR_FAS(32, uint32_t, uint32_t, "", "w")182CK_PR_FAS(ptr, void, void *, "", "")183CK_PR_FAS(int, int, int, "", "w")184CK_PR_FAS(uint, unsigned int, unsigned int, "", "w")185CK_PR_FAS(16, uint16_t, uint16_t, "h", "w")186CK_PR_FAS(8, uint8_t, uint8_t, "b", "w")187CK_PR_FAS(short, short, short, "h", "w")188CK_PR_FAS(char, char, char, "b", "w")189190191#undef CK_PR_FAS192193#define CK_PR_UNARY(O, N, M, T, I, W, R) \194CK_CC_INLINE static void \195ck_pr_##O##_##N(M *target) \196{ \197T previous = 0; \198T tmp = 0; \199__asm__ __volatile__("1:" \200"ldxr" W " %" R "0, [%2]\n"\201I "\n" \202"stxr" W " %w1, %" R "0, [%2]\n" \203"cbnz %w1, 1b\n" \204: "=&r" (previous), \205"=&r" (tmp) \206: "r" (target) \207: "memory", "cc"); \208return; \209}210211CK_PR_UNARY(inc, ptr, void, void *, "add %0, %0, #1", "", "")212CK_PR_UNARY(dec, ptr, void, void *, "sub %0, %0, #1", "", "")213CK_PR_UNARY(not, ptr, void, void *, "mvn %0, %0", "", "")214CK_PR_UNARY(inc, 64, uint64_t, uint64_t, "add %0, %0, #1", "", "")215CK_PR_UNARY(dec, 64, uint64_t, uint64_t, "sub %0, %0, #1", "", "")216CK_PR_UNARY(not, 64, uint64_t, uint64_t, "mvn %0, %0", "", "")217218#define CK_PR_UNARY_S(S, T, W) \219CK_PR_UNARY(inc, S, T, T, "add %w0, %w0, #1", W, "w") \220CK_PR_UNARY(dec, S, T, T, "sub %w0, %w0, #1", W, "w") \221CK_PR_UNARY(not, S, T, T, "mvn %w0, %w0", W, "w") \222223CK_PR_UNARY_S(32, uint32_t, "")224CK_PR_UNARY_S(uint, unsigned int, "")225CK_PR_UNARY_S(int, int, "")226CK_PR_UNARY_S(16, uint16_t, "h")227CK_PR_UNARY_S(8, uint8_t, "b")228CK_PR_UNARY_S(short, short, "h")229CK_PR_UNARY_S(char, char, "b")230231#undef CK_PR_UNARY_S232#undef CK_PR_UNARY233234#define CK_PR_BINARY(O, N, M, T, I, W, R) \235CK_CC_INLINE static void \236ck_pr_##O##_##N(M *target, T delta) \237{ \238T previous; \239T tmp; \240__asm__ __volatile__("1:" \241"ldxr" W " %" R "0, [%2]\n"\242I " %" R "0, %" R "0, %" R "3\n" \243"stxr" W " %w1, %" R "0, [%2]\n" \244"cbnz %w1, 1b\n" \245: "=&r" (previous), \246"=&r" (tmp) \247: "r" (target), \248"r" (delta) \249: "memory", "cc"); \250return; \251}252253CK_PR_BINARY(and, ptr, void, uintptr_t, "and", "", "")254CK_PR_BINARY(add, ptr, void, uintptr_t, "add", "", "")255CK_PR_BINARY(or, ptr, void, uintptr_t, "orr", "", "")256CK_PR_BINARY(sub, ptr, void, uintptr_t, "sub", "", "")257CK_PR_BINARY(xor, ptr, void, uintptr_t, "eor", "", "")258CK_PR_BINARY(and, 64, uint64_t, uint64_t, "and", "", "")259CK_PR_BINARY(add, 64, uint64_t, uint64_t, "add", "", "")260CK_PR_BINARY(or, 64, uint64_t, uint64_t, "orr", "", "")261CK_PR_BINARY(sub, 64, uint64_t, uint64_t, "sub", "", "")262CK_PR_BINARY(xor, 64, uint64_t, uint64_t, "eor", "", "")263264#define CK_PR_BINARY_S(S, T, W) \265CK_PR_BINARY(and, S, T, T, "and", W, "w") \266CK_PR_BINARY(add, S, T, T, "add", W, "w") \267CK_PR_BINARY(or, S, T, T, "orr", W, "w") \268CK_PR_BINARY(sub, S, T, T, "sub", W, "w") \269CK_PR_BINARY(xor, S, T, T, "eor", W, "w")270271CK_PR_BINARY_S(32, uint32_t, "")272CK_PR_BINARY_S(uint, unsigned int, "")273CK_PR_BINARY_S(int, int, "")274CK_PR_BINARY_S(16, uint16_t, "h")275CK_PR_BINARY_S(8, uint8_t, "b")276CK_PR_BINARY_S(short, short, "h")277CK_PR_BINARY_S(char, char, "b")278279#undef CK_PR_BINARY_S280#undef CK_PR_BINARY281282CK_CC_INLINE static void *283ck_pr_faa_ptr(void *target, uintptr_t delta)284{285uintptr_t previous, r, tmp;286287__asm__ __volatile__("1:"288"ldxr %0, [%3]\n"289"add %1, %4, %0\n"290"stxr %w2, %1, [%3]\n"291"cbnz %w2, 1b\n"292: "=&r" (previous),293"=&r" (r),294"=&r" (tmp)295: "r" (target),296"r" (delta)297: "memory", "cc");298299return (void *)(previous);300}301302CK_CC_INLINE static uint64_t303ck_pr_faa_64(uint64_t *target, uint64_t delta)304{305uint64_t previous, r, tmp;306307__asm__ __volatile__("1:"308"ldxr %0, [%3]\n"309"add %1, %4, %0\n"310"stxr %w2, %1, [%3]\n"311"cbnz %w2, 1b;"312: "=&r" (previous),313"=&r" (r),314"=&r" (tmp)315: "r" (target),316"r" (delta)317: "memory", "cc");318319return (previous);320}321322#define CK_PR_FAA(S, T, W) \323CK_CC_INLINE static T \324ck_pr_faa_##S(T *target, T delta) \325{ \326T previous, r, tmp; \327__asm__ __volatile__("1:" \328"ldxr" W " %w0, [%3]\n" \329"add %w1, %w4, %w0\n" \330"stxr" W " %w2, %w1, [%3]\n" \331"cbnz %w2, 1b\n" \332: "=&r" (previous), \333"=&r" (r), \334"=&r" (tmp) \335: "r" (target), \336"r" (delta) \337: "memory", "cc"); \338return (previous); \339}340341CK_PR_FAA(32, uint32_t, "")342CK_PR_FAA(uint, unsigned int, "")343CK_PR_FAA(int, int, "")344CK_PR_FAA(16, uint16_t, "h")345CK_PR_FAA(8, uint8_t, "b")346CK_PR_FAA(short, short, "h")347CK_PR_FAA(char, char, "b")348349#undef CK_PR_FAA350351#endif /* CK_PR_AARCH64_LLSC_H */352353354