From 55977d515897e515d11e97c21abe2a8dfba291cf Mon Sep 17 00:00:00 2001 From: Andrei Tatar Date: Wed, 24 May 2023 13:45:17 +0300 Subject: [PATCH] Bring back the fix for GCC atomic builtins MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Commit d53cbfe6035f ("Update to LLVM 14.0.6") removed the patch that adapts atomic.c to use GCC builtins instead of clang, however its functionality is still required. This change brings back this functionality in a cleaner and more future-proof way, by providing a header that implements clang builtins in terms of GCC ones. Signed-off-by: Andrei Tatar Reviewed-by: Stefan Jumarea Reviewed-by: Maria Sfiraiala Reviewed-by: Razvan Deaconescu Approved-by: Eduard Vintilă Tested-by: Unikraft CI GitHub-Closes: #12 --- Makefile.uk | 2 + include/gcc_atomics.h | 106 ++++++++++++++++++ ...1-Add-header-for-GCC-atomic-builtins.patch | 32 ++++++ 3 files changed, 140 insertions(+) create mode 100644 include/gcc_atomics.h create mode 100644 patches/0001-Add-header-for-GCC-atomic-builtins.patch diff --git a/Makefile.uk b/Makefile.uk index 285b6d3..8be4857 100644 --- a/Makefile.uk +++ b/Makefile.uk @@ -67,6 +67,8 @@ CINCLUDES-$(CONFIG_LIBCOMPILER_RT) += -I$(LIBCOMPILER_RT_SRC)/lib/builtins CXXINCLUDES-$(CONFIG_LIBCOMPILER_RT) += -I$(LIBCOMPILER_RT_SRC)/lib CXXINCLUDES-$(CONFIG_LIBCOMPILER_RT) += -I$(LIBCOMPILER_RT_SRC)/lib/builtins +LIBCOMPILER_RT_CINCLUDES-$(call have_gcc) += -iquote$(LIBCOMPILER_RT_BASE)/include + ################################################################################ # Global flags ################################################################################ diff --git a/include/gcc_atomics.h b/include/gcc_atomics.h new file mode 100644 index 0000000..0b35ba9 --- /dev/null +++ b/include/gcc_atomics.h @@ -0,0 +1,106 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Author(s): Andrei Tatar + * + * Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* GCC and clang have different builtins for atomic operations. + * lib-compiler-rt, being part of LLVM, naturally uses the clang-specific + * __c11_* builtins. This header provides translations between the two, allowing + * compiler-rt to be built with GCC. + * + * Each #define is conditioned by the unavailability of the __c11* builtin, + * for future-proofing if/when GCC implements them. + */ + +#ifndef __UKPATCH_GCC_ATOMICS_H__ +#define __UKPATCH_GCC_ATOMICS_H__ + +#ifndef __c11_atomic_compare_exchange_strong +#define __c11_atomic_compare_exchange_strong(object, expected, desired, success_order, fail_order) \ + __atomic_compare_exchange_n(object, expected, desired, 0, success_order, fail_order) +#endif + +#ifndef __c11_atomic_compare_exchange_weak +#define __c11_atomic_compare_exchange_weak(object, expected, desired, success_order, fail_order) \ + __atomic_compare_exchange_n(object, expected, desired, 1, success_order, fail_order) +#endif + +#ifndef __c11_atomic_exchange +#define __c11_atomic_exchange(object, desired, order) \ + __atomic_exchange_n(object, desired, order) +#endif + +#ifndef __c11_atomic_fetch_add +#define __c11_atomic_fetch_add(object, operand, order) \ + __atomic_fetch_add(object, operand, order) +#endif + +#ifndef __c11_atomic_fetch_and +#define __c11_atomic_fetch_and(object, operand, order) \ + __atomic_fetch_and(object, operand, order) +#endif + +#ifndef __c11_atomic_fetch_nand +#define __c11_atomic_fetch_nand(object, operand, order) \ + __atomic_fetch_and(object, operand, order) +#endif + +#ifndef __c11_atomic_fetch_or +#define __c11_atomic_fetch_or(object, operand, order) \ + __atomic_fetch_or(object, operand, order) +#endif + +#ifndef __c11_atomic_fetch_sub +#define __c11_atomic_fetch_sub(object, operand, order) \ + __atomic_fetch_sub(object, operand, order) +#endif + +#ifndef __c11_atomic_fetch_xor +#define __c11_atomic_fetch_xor(object, operand, order) \ + __atomic_fetch_xor(object, operand, order) +#endif + +#ifndef __c11_atomic_load +#define __c11_atomic_load(object, order) \ + __atomic_load_n(object, order) +#endif + +#ifndef __c11_atomic_store +#define __c11_atomic_store(object, desired, order) \ + __atomic_store_n(object, desired, order) +#endif + +#ifndef __c11_atomic_thread_fence +#define __c11_atomic_thread_fence(order) \ + __atomic_thread_fence(order) +#endif + +#endif /* __UKPATCH_GCC_ATOMICS_H__ */ diff --git a/patches/0001-Add-header-for-GCC-atomic-builtins.patch b/patches/0001-Add-header-for-GCC-atomic-builtins.patch new file mode 100644 index 0000000..265b806 --- /dev/null +++ b/patches/0001-Add-header-for-GCC-atomic-builtins.patch @@ -0,0 +1,32 @@ +From 4d39ccc51bac5fb2356d2bf7e29a919e8430c96d Mon Sep 17 00:00:00 2001 +From: Andrei Tatar +Date: Wed, 24 May 2023 13:05:03 +0300 +Subject: [PATCH] Add header for GCC atomic builtins + +compiler-rt relies on clang's __c11_atomic builtins which GCC does not +have. This patch adds a unikraft header which implements these builtins +in terms of GCC ones. + +Signed-off-by: Andrei Tatar +--- + compiler-rt/lib/builtins/atomic.c | 4 ++++ + 1 file changed, 4 insertions(+) + +diff --git a/compiler-rt/lib/builtins/atomic.c b/compiler-rt/lib/builtins/atomic.c +index 8dc2f4fc0..59667f888 100644 +--- a/lib/builtins/atomic.c ++++ b/lib/builtins/atomic.c +@@ -29,6 +29,10 @@ + + #include "assembly.h" + ++#if defined(__GNUC__) && !defined(__clang__) ++#include "gcc_atomics.h" ++#endif /* __GNUC__ && !defined(__clang__) */ ++ + // We use __builtin_mem* here to avoid dependencies on libc-provided headers. + #define memcpy __builtin_memcpy + #define memcmp __builtin_memcmp +-- +2.40.1 + -- 2.39.5