summaryrefslogtreecommitdiff
path: root/kernel/include
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-04-03 16:53:51 -0400
committerFreya Murphy <freya@freyacat.org>2025-04-03 16:54:42 -0400
commit516e920cd96730cf470357aa250636d9d42a849c (patch)
tree16e177ffc8bfed0a86abe46455bcbb37c2dbed6d /kernel/include
parentboot headers moved (diff)
downloadcomus-516e920cd96730cf470357aa250636d9d42a849c.tar.gz
comus-516e920cd96730cf470357aa250636d9d42a849c.tar.bz2
comus-516e920cd96730cf470357aa250636d9d42a849c.zip
memory allocator/pager, plus other stuff
Diffstat (limited to 'kernel/include')
-rw-r--r--kernel/include/comus/asm.h64
-rw-r--r--kernel/include/comus/memory.h76
-rw-r--r--kernel/include/comus/memory/mapping.h20
3 files changed, 139 insertions, 21 deletions
diff --git a/kernel/include/comus/asm.h b/kernel/include/comus/asm.h
new file mode 100644
index 0000000..5e1dce7
--- /dev/null
+++ b/kernel/include/comus/asm.h
@@ -0,0 +1,64 @@
+#pragma once
+
+#include <stdint.h>
+
+static inline uint8_t inb(uint16_t port)
+{
+ uint8_t ret;
+ __asm__ volatile("inb %1, %0" : "=a"(ret) : "Nd"(port));
+ return ret;
+}
+
+static inline void outb(uint16_t port, uint8_t val)
+{
+ __asm__ volatile("outb %0, %1" : : "a"(val), "Nd"(port));
+}
+
+static inline uint16_t inw(uint16_t port)
+{
+ uint16_t ret;
+ __asm__ volatile("inw %1, %0" : "=a"(ret) : "Nd"(port));
+ return ret;
+}
+
+static inline void outw(uint16_t port, uint16_t val)
+{
+ __asm__ volatile("outw %0, %1" : : "a"(val), "Nd"(port));
+}
+
+static inline uint32_t inl(uint16_t port)
+{
+ uint32_t ret;
+ __asm__ volatile("inl %1, %0" : "=a"(ret) : "Nd"(port));
+ return ret;
+}
+
+static inline void outl(uint16_t port, uint32_t val)
+{
+ __asm__ volatile("outl %0, %1" : : "a"(val), "Nd"(port));
+}
+
+static inline void io_wait(void)
+{
+ outb(0x80, 0);
+}
+
+static inline void sti(void)
+{
+ __asm__ volatile("sti");
+}
+
+static inline void cli(void)
+{
+ __asm__ volatile("cli");
+}
+
+static inline void int_wait(void)
+{
+ __asm__ volatile("sti; hlt");
+}
+
+static inline void halt(void)
+{
+ __asm__ volatile("cli; hlt");
+}
diff --git a/kernel/include/comus/memory.h b/kernel/include/comus/memory.h
index 938bc6b..411c039 100644
--- a/kernel/include/comus/memory.h
+++ b/kernel/include/comus/memory.h
@@ -1 +1,75 @@
-#include <memory/mapping.h>
+/**
+ * @file memory.h
+ *
+ * @author Freya Murphy <freya@freyacat.org>
+ *
+ * Kernel memory functions
+ */
+
+#ifndef _MEMORY_H
+#define _MEMORY_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+#define MMAP_MAX_ENTRY 64
+
+struct memory_segment {
+ uint64_t addr;
+ uint64_t len;
+};
+
+struct memory_map {
+ uint32_t entry_count;
+ struct memory_segment entries[MMAP_MAX_ENTRY];
+};
+
+/**
+ * Initalize system memory allocator
+ */
+void memory_init(struct memory_map *map);
+
+/**
+ * @returns how much memory the system has
+ */
+uint64_t memory_total(void);
+
+/**
+ * @returns how much memory is free
+ */
+uint64_t memory_free(void);
+
+/**
+ * @returns how much memory is used
+ */
+uint64_t memory_used(void);
+
+/**
+ * Allocates at least len bytes of memory starting at
+ * physical address addr. Returned address can be
+ * any virtural address.
+ *
+ * @param addr - the physical address to map
+ * @param len - the minimum length to map
+ * @param writable - if this memory should be writable
+ * @param user - if this memory should be user writable
+ */
+void *mapaddr(void *addr, size_t len);
+
+/**
+ * Unmaps mapped address from the mmap function
+ * @param addr - the address returned from mmap
+ * @param len - the length allocated
+ */
+void unmapaddr(void *addr);
+
+/**
+ * Attemps to load a mapped but not yet allocated page.
+ *
+ * @param virt_addr - the virtural address from either page allocation function
+ *
+ * @returns 0 on success and a negative error code on failure.
+ */
+int load_page(void *virt_addr);
+
+#endif /* memory.h */
diff --git a/kernel/include/comus/memory/mapping.h b/kernel/include/comus/memory/mapping.h
deleted file mode 100644
index e1b7102..0000000
--- a/kernel/include/comus/memory/mapping.h
+++ /dev/null
@@ -1,20 +0,0 @@
-/**
- * @file memory.h
- *
- * @author Freya Murphy
- *
- * Kernel memory declarations
- */
-
-#ifndef _MEMORY_MAPPING_H
-#define _MEMORY_MAPPING_H
-
-// paging
-#define PAGE_SIZE 4096
-#define PAGE_PRESENT 0x1
-#define PAGE_WRITE 0x2
-#define PAGE_USER 0x4
-#define PAGE_HUGE 0x80
-#define PAGE_GLOBAL 0x100
-
-#endif