summaryrefslogtreecommitdiff
path: root/kernel/include
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-04-15 22:20:59 -0400
committerFreya Murphy <freya@freyacat.org>2025-04-15 22:20:59 -0400
commit2dbf529c33aa3e24beff944758d586bb0608c1be (patch)
tree5b3381c3c8d9a74ccb988c8945fe0681c3ee7301 /kernel/include
parentfix %n (diff)
downloadcomus-2dbf529c33aa3e24beff944758d586bb0608c1be.tar.gz
comus-2dbf529c33aa3e24beff944758d586bb0608c1be.tar.bz2
comus-2dbf529c33aa3e24beff944758d586bb0608c1be.zip
expand memory manager work with userspace (more then one ctx)
Diffstat (limited to 'kernel/include')
-rw-r--r--kernel/include/comus/memory.h101
-rw-r--r--kernel/include/lib/kio.h10
-rw-r--r--kernel/include/lib/klib.h25
3 files changed, 110 insertions, 26 deletions
diff --git a/kernel/include/comus/memory.h b/kernel/include/comus/memory.h
index 77d64ff..ee413e3 100644
--- a/kernel/include/comus/memory.h
+++ b/kernel/include/comus/memory.h
@@ -15,6 +15,16 @@
#define MMAP_MAX_ENTRY 64
#define PAGE_SIZE 4096
+#define F_PRESENT 0x001
+#define F_WRITEABLE 0x002
+#define F_UNPRIVILEGED 0x004
+#define F_WRITETHROUGH 0x008
+#define F_CACHEDISABLE 0x010
+#define F_ACCESSED 0x020
+#define F_DIRTY 0x040
+#define F_MEGABYTE 0x080
+#define F_GLOBAL 0x100
+
struct memory_segment {
uint64_t addr;
uint64_t len;
@@ -25,6 +35,9 @@ struct memory_map {
struct memory_segment entries[MMAP_MAX_ENTRY];
};
+typedef struct mem_ctx_s *mem_ctx_t;
+extern mem_ctx_t kernel_mem_ctx;
+
/**
* Initalize system memory allocator
*/
@@ -46,62 +59,91 @@ uint64_t memory_free(void);
uint64_t memory_used(void);
/**
+ * Allocate a new memory context
+ *
+ * @returns pointer context or NULL on failure
+ */
+mem_ctx_t alloc_mem_ctx(void);
+
+/**
+ * Free a memory context
+ *
+ * @param ctx - pointer to the memory context
+ */
+void free_mem_ctx(mem_ctx_t ctx);
+
+/**
* 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
+ * @param phys - the physical address to map
+ * @param virt - the virtural address to map (or NULL for any virt addr)
+ * @param len - the minimum length in bytes to map
+ * @param flags - memory flags (F_PRESENT will always be set)
+ * @returns the mapped vitural address to phys, or NULL on failure
*/
-void *kmapaddr(void *addr, size_t len);
+void *mem_mapaddr(mem_ctx_t ctx, void *phys, void *virt, size_t len,
+ unsigned int flags);
/**
- * Unmaps mapped address from the mmap function
- * @param addr - the address returned from mmap
- * @param len - the length allocated
+ * Unmaps mapped address from the kmapaddr function
+ * @param virt - the vitural address returned from kmapaddr
*/
-void kunmapaddr(void *addr);
+void mem_unmapaddr(mem_ctx_t ctx, void *virt);
/**
- * Allocates size_t bytes in memory
+ * Allocate a single page of memory with the given paging structure
*
- * @param size - the amount of bytes to allocate
- * @returns the address allocated or NULL on failure
+ * @returns the vitural address aloocated or NULL on failure
*/
-void *kalloc(size_t size);
+void *mem_alloc_page(mem_ctx_t ctx);
/**
- * Rellocates a given allocated ptr to a new size of bytes in memory.
- * If ptr is NULL it will allocate new memory.
+ * Allocate size_t amount of contiguous virtual pages with the given paging structure
*
- * @param ptr - the pointer to reallocate
- * @param size - the amount of bytes to reallocate to
+ * @param count - the number of pages to allocate
* @returns the address allocated or NULL on failure
*/
-void *krealloc(void *ptr, size_t size);
+void *mem_alloc_pages(mem_ctx_t ctx, size_t count);
/**
- * Frees an allocated pointer in memory
+ * Free allocated pages with the given paging structure.
*
- * @param ptr - the pointer to free
+ * @param ptr - the pointer provided by alloc_page or alloc_pages
*/
-void kfree(void *ptr);
+void mem_free_pages(mem_ctx_t ctx, void *ptr);
/**
- * Attemps to load a mapped but not yet allocated page.
+ * Load a not allocated but properly mapped page
*
- * @param virt_addr - the virtural address from either page allocation function
+ * @returns 0 on success, negative error code on failure
+ */
+int mem_load_page(mem_ctx_t ctx, void *virt);
+
+/**
+ * Allocates at least len bytes of memory starting at
+ * physical address addr. Returned address can be
+ * any virtural address.
*
- * @returns 0 on success and a negative error code on failure.
+ * @param phys - the physical address to map
+ * @param virt - the virtural address to map (or NULL for any virt addr)
+ * @param len - the minimum length in bytes to map
+ * @param flags - memory flags (F_PRESENT will always be set)
+ * @returns the mapped vitural address to phys, or NULL on failure
*/
-int kload_page(void *virt_addr);
+void *kmapaddr(void *phys, void *virt, size_t len, unsigned int flags);
+
+/**
+ * Unmaps mapped address from the kmapaddr function
+ * @param virt - the vitural address returned from kmapaddr
+ */
+void kunmapaddr(void *virt);
/**
* Allocate a single page of memory
*
- * @returns the address allocated or NULL on failure
+ * @returns the vitural address allocated or NULL on failure
*/
void *kalloc_page(void);
@@ -120,4 +162,11 @@ void *kalloc_pages(size_t count);
*/
void kfree_pages(void *ptr);
+/**
+ * Load a not allocated but properly mapped page
+ *
+ * @returns 0 on success, negative error code on failure
+ */
+int kload_page(void *virt);
+
#endif /* memory.h */
diff --git a/kernel/include/lib/kio.h b/kernel/include/lib/kio.h
index 66efc7b..1b10a39 100644
--- a/kernel/include/lib/kio.h
+++ b/kernel/include/lib/kio.h
@@ -26,6 +26,16 @@ void kputc(char c);
*/
void kputs(const char *s);
+#ifdef TRACING
+#define TRACE(format, ...) \
+ do { \
+ kprintf("[TRACE] %s ", __FUNCTION__); \
+ kprintf(format, ##__VA_ARGS__); \
+ } while (0)
+#else
+#define TRACE(format, ...)
+#endif
+
/**
* prints out a formatted string
*
diff --git a/kernel/include/lib/klib.h b/kernel/include/lib/klib.h
index 7f66e90..0d9797b 100644
--- a/kernel/include/lib/klib.h
+++ b/kernel/include/lib/klib.h
@@ -231,4 +231,29 @@ void log_backtrace(void);
*/
void log_backtrace_ex(void *ip, void *bp);
+/**
+ * Allocates size_t bytes in memory
+ *
+ * @param size - the amount of bytes to allocate
+ * @returns the address allocated or NULL on failure
+ */
+void *kalloc(size_t size);
+
+/**
+ * Rellocates a given allocated ptr to a new size of bytes in memory.
+ * If ptr is NULL it will allocate new memory.
+ *
+ * @param ptr - the pointer to reallocate
+ * @param size - the amount of bytes to reallocate to
+ * @returns the address allocated or NULL on failure
+ */
+void *krealloc(void *ptr, size_t size);
+
+/**
+ * Frees an allocated pointer in memory
+ *
+ * @param ptr - the pointer to free
+ */
+void kfree(void *ptr);
+
#endif /* klib.h */