From 920bd0748827d93ec992b3e11be72937f05b4509 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Thu, 17 Apr 2025 15:42:51 +0200 Subject: [PATCH] Added spinlock from NOVA for locking CIP. --- repos/base-tukija/include/tukija/spinlock.hpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 repos/base-tukija/include/tukija/spinlock.hpp diff --git a/repos/base-tukija/include/tukija/spinlock.hpp b/repos/base-tukija/include/tukija/spinlock.hpp new file mode 100644 index 0000000000..8d6d56a89a --- /dev/null +++ b/repos/base-tukija/include/tukija/spinlock.hpp @@ -0,0 +1,52 @@ +/* + * Generic Spinlock + * + * 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 + +namespace Tukija { + class Spinlock; +} + +class Tukija::Spinlock +{ + private: + unsigned short val; + + public: + inline Spinlock() : val (0) {} + + __attribute__((noinline)) + void lock() + { + unsigned short tmp = 0x100; + + asm volatile (" lock; xadd %0, %1; " + "1: cmpb %h0, %b0; " + " je 2f; " + " pause; " + " movb %1, %b0; " + " jmp 1b; " + "2: " + : "+Q" (tmp), "+m" (val) : : "memory"); + } + + inline void unlock() + { + asm volatile ("incb %0" : "+m" (val) : : "memory"); + } +};