aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-02-02 23:25:39 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2025-02-02 23:25:39 +0530
commit728fd091725e9af27d3f872d1fd15c9be0dfa9f3 (patch)
tree7e8691004b042bbfd2960be7b26e7ab173345184
parent2dc523a6d678c63130eec97eb9722a7ad7632eeb (diff)
kernel: Make Spinlock an object
-rw-r--r--flake.nix1
-rw-r--r--kernel/include/kernel/spinlock.h12
-rw-r--r--kernel/kernel/spinlock.cc16
-rw-r--r--kernel/libk/liballoc.cc46
-rw-r--r--kernel/mm/physical_mm/physical_mm.cc10
5 files changed, 41 insertions, 44 deletions
diff --git a/flake.nix b/flake.nix
index 7a068b5..36f03b9 100644
--- a/flake.nix
+++ b/flake.nix
@@ -17,6 +17,7 @@
qemu_kvm
clang-tools
+ ccls
];
};
};
diff --git a/kernel/include/kernel/spinlock.h b/kernel/include/kernel/spinlock.h
index ed6bb86..bd255ad 100644
--- a/kernel/include/kernel/spinlock.h
+++ b/kernel/include/kernel/spinlock.h
@@ -23,12 +23,14 @@
typedef uint8_t spinlock_t;
-namespace Spinlock
+class Spinlock
{
+private:
+ spinlock_t m_lock;
-void acquire(spinlock_t volatile *lock);
-void release(spinlock_t volatile *lock);
-
-}
+public:
+ void acquire(void);
+ void release(void);
+};
#endif
diff --git a/kernel/kernel/spinlock.cc b/kernel/kernel/spinlock.cc
index c13a08f..3a9031c 100644
--- a/kernel/kernel/spinlock.cc
+++ b/kernel/kernel/spinlock.cc
@@ -16,26 +16,20 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <common.h>
#include <kernel/spinlock.h>
-namespace Spinlock
-{
-
void
-acquire(spinlock_t volatile *plock)
+Spinlock::acquire(void)
{
__asm__ volatile("cli");
- while (!__sync_bool_compare_and_swap(plock, 0, 1))
- while (*plock)
+ while (!__sync_bool_compare_and_swap(&m_lock, 0, 1))
+ while (m_lock)
__asm__ volatile("rep; nop");
}
void
-release(spinlock_t volatile *plock)
+Spinlock::release(void)
{
- __sync_bool_compare_and_swap(plock, 1, 0);
+ __sync_bool_compare_and_swap(&m_lock, 1, 0);
/* TODO: Enable interrupts here */
}
-
-}
diff --git a/kernel/libk/liballoc.cc b/kernel/libk/liballoc.cc
index ae3fe78..9bd6ec4 100644
--- a/kernel/libk/liballoc.cc
+++ b/kernel/libk/liballoc.cc
@@ -26,43 +26,23 @@
/* TODO: Kmalloc must have space for a page table *at all times*. */
-#define LIBALLOC_MAGIC 0xc001c0de
-#define MAXCOMPLETE 5
-#define MAXEXP 32
-#define MINEXP 8
-
-#define MODE_BEST 0
-#define MODE_INSTANT 1
-
-#define MODE MODE_BEST
-
namespace LibAlloc
{
-struct boundary_tag *l_freePages[MAXEXP]; //< Allowing for 2^MAXEXP blocks
-int l_completePages[MAXEXP]; //< Allowing for 2^MAXEXP blocks
-
-static unsigned int l_initialized = 0; //< Flag to indicate initialization.
-static unsigned int l_pageSize = 4096; //< Individual page size
-static unsigned int l_pageCount = 1; //< Minimum number of pages to allocate.
-
-spinlock_t lock;
+Spinlock lock;
bool kmalloc_initialized;
-#define liballoc_alloc VirtualMM::alloc_pages
-#define liballoc_free VirtualMM::free_pages
-
inline int
liballoc_lock(void)
{
- Spinlock::acquire(&lock);
+ lock.acquire();
return 0;
}
inline int
liballoc_unlock(void)
{
- Spinlock::release(&lock);
+ lock.release();
return 0;
}
@@ -83,6 +63,26 @@ initialize(void)
kfree(x);
}
+#define liballoc_alloc VirtualMM::alloc_pages
+#define liballoc_free VirtualMM::free_pages
+
+#define LIBALLOC_MAGIC 0xc001c0de
+#define MAXCOMPLETE 5
+#define MAXEXP 32
+#define MINEXP 8
+
+#define MODE_BEST 0
+#define MODE_INSTANT 1
+
+#define MODE MODE_BEST
+
+struct boundary_tag *l_freePages[MAXEXP]; //< Allowing for 2^MAXEXP blocks
+int l_completePages[MAXEXP]; //< Allowing for 2^MAXEXP blocks
+
+static unsigned int l_initialized = 0; //< Flag to indicate initialization.
+static unsigned int l_pageSize = 4096; //< Individual page size
+static unsigned int l_pageCount = 1; //< Minimum number of pages to allocate.
+
static inline int
getexp(int size)
{
diff --git a/kernel/mm/physical_mm/physical_mm.cc b/kernel/mm/physical_mm/physical_mm.cc
index fb34aef..a4cd894 100644
--- a/kernel/mm/physical_mm/physical_mm.cc
+++ b/kernel/mm/physical_mm/physical_mm.cc
@@ -40,7 +40,7 @@ uint32_t block_count = 0;
uint32_t total_free_blocks = 0;
uint32_t memory_map[MAX_BLOCKS / BITMAP_ENTRY_SIZE];
-spinlock_t memory_map_lock;
+Spinlock lock;
ALWAYS_INLINE static void
log_memory_map(free_memory_regions_t *free_memory_regions)
@@ -96,7 +96,7 @@ initialize(void)
free_memory_regions_t *free_memory_regions = MemoryMap::get_free_regions();
log_memory_map(free_memory_regions);
- Spinlock::acquire(&memory_map_lock);
+ lock.acquire();
/* All blocks are initially used */
/* TODO: Move this block to a place after block_count is set. This is why
@@ -115,7 +115,7 @@ initialize(void)
/* Deinitialize first 4MiB */
deinitialize_region(0, 4 * MiB);
- Spinlock::release(&memory_map_lock);
+ lock.release();
/* Manually loop through and calculate the number of free blocks. */
for (uint32_t i = 0; i < MAX_BLOCKS / BITMAP_ENTRY_SIZE; i++)
@@ -155,12 +155,12 @@ allocate_block(void)
return NULL;
}
- Spinlock::acquire(&memory_map_lock);
+ lock.acquire();
uint32_t block = find_free_block();
set_used(block, &total_free_blocks, memory_map);
- Spinlock::release(&memory_map_lock);
+ lock.release();
uint32_t physical_address = block * BLOCK_SIZE;
return (void *) physical_address;