Added helper functions from NOVA for bit operations.

This commit is contained in:
Michael Mueller
2025-02-21 15:27:47 +01:00
parent 775f92d1a9
commit 6e3b1839ea
2 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
/*
* Atomic Operations
*
* Copyright (C) 2009-2011 Udo Steinberg <udo@hypervisor.org>
* 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 <typename T>
static inline bool cmp_swap (T &ptr, T o, T n) { return __sync_bool_compare_and_swap (&ptr, o, n); }
template <typename T>
static inline T add (T &ptr, T v) { return __sync_add_and_fetch (&ptr, v); }
template <typename T>
static inline T sub (T &ptr, T v) { return __sync_sub_and_fetch (&ptr, v); }
template <typename T>
static inline void set_mask (T &ptr, T v) { __sync_or_and_fetch (&ptr, v); }
template <typename T>
static inline void clr_mask (T &ptr, T v) { __sync_and_and_fetch (&ptr, ~v); }
template <typename T>
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 <typename T>
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 <typename T>
static inline void store(T &ptr, T val)
{
__atomic_store_n(&ptr, val, __ATOMIC_SEQ_CST);
}
};

View File

@@ -0,0 +1,35 @@
/* Bit manipulation functions taken from NOVA
* Copyright (C) 2009-2011 Udo Steinberg <udo@hypervisor.org>
* 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 <tukija/stdint.h>
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);
}