From 6e3b1839ea23ce3da9d2f663cc5a8e918a1f8e8c Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Fri, 21 Feb 2025 15:27:47 +0100 Subject: [PATCH] Added helper functions from NOVA for bit operations. --- repos/base-tukija/include/tukija/atomic.h | 60 +++++++++++++++++++++++ repos/base-tukija/include/tukija/bits.h | 35 +++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 repos/base-tukija/include/tukija/atomic.h create mode 100644 repos/base-tukija/include/tukija/bits.h diff --git a/repos/base-tukija/include/tukija/atomic.h b/repos/base-tukija/include/tukija/atomic.h new file mode 100644 index 0000000000..07517d9b3f --- /dev/null +++ b/repos/base-tukija/include/tukija/atomic.h @@ -0,0 +1,60 @@ +/* + * Atomic Operations + * + * Copyright (C) 2009-2011 Udo Steinberg + * Economic rights: Technische Universitaet Dresden (Germany) + * + * This file is part of the NOVA microhypervisor. + * + * NOVA is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License version 2 for more details. + */ + +#pragma once + +class Atomic +{ + public: + template + static inline bool cmp_swap (T &ptr, T o, T n) { return __sync_bool_compare_and_swap (&ptr, o, n); } + + template + static inline T add (T &ptr, T v) { return __sync_add_and_fetch (&ptr, v); } + + template + static inline T sub (T &ptr, T v) { return __sync_sub_and_fetch (&ptr, v); } + + template + static inline void set_mask (T &ptr, T v) { __sync_or_and_fetch (&ptr, v); } + + template + static inline void clr_mask (T &ptr, T v) { __sync_and_and_fetch (&ptr, ~v); } + + template + static inline bool test_set_bit (T &val, unsigned long bit) + { + bool ret; + asm volatile ("lock; bts%z1 %2, %1; setc %0" : "=q" (ret), "+m" (val) : "ir" (bit) : "cc"); + return ret; + } + + template + static inline bool test_clr_bit (T &val, unsigned long bit) + { + bool ret; + asm volatile ("lock; btr%z1 %2, %1; setc %0" : "=q" (ret), "+m" (val) : "ir" (bit) : "cc"); + return ret; + } + + template + static inline void store(T &ptr, T val) + { + __atomic_store_n(&ptr, val, __ATOMIC_SEQ_CST); + } +}; diff --git a/repos/base-tukija/include/tukija/bits.h b/repos/base-tukija/include/tukija/bits.h new file mode 100644 index 0000000000..237c906b55 --- /dev/null +++ b/repos/base-tukija/include/tukija/bits.h @@ -0,0 +1,35 @@ +/* Bit manipulation functions taken from NOVA + * Copyright (C) 2009-2011 Udo Steinberg + * Economic rights: Technische Universitaet Dresden (Germany) + * + * Copyright (C) 2012-2013 Udo Steinberg, Intel Corporation. + * Copyright (C) 2019-2024 Udo Steinberg, BlueRock Security, Inc. + * + * NOVA is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * NOVA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License version 2 for more details. + */ + + +#pragma once +#include + +inline long int bit_scan_forward (Tukija::mword_t val) +{ + if ((!val)) + return -1; + + asm volatile ("bsf %1, %0" : "=r" (val) : "rm" (val)); + + return val; +} + +inline Tukija::mword_t popcount(Tukija::mword_t bitset) +{ + return __builtin_popcountl(bitset); +}