diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/memory.h | 107 | ||||
-rw-r--r-- | include/memory/physalloc.h | 30 | ||||
-rw-r--r-- | include/memory/virtalloc.h | 24 | ||||
-rw-r--r-- | include/shim.h | 7 | ||||
-rw-r--r-- | include/term.h | 3 |
5 files changed, 171 insertions, 0 deletions
diff --git a/include/memory.h b/include/memory.h new file mode 100644 index 0000000..bec05f8 --- /dev/null +++ b/include/memory.h @@ -0,0 +1,107 @@ +#pragma once + +#include <stddef.h> +#include <stdint.h> + +struct memory_segment { + uint64_t addr; + uint64_t len; + uint32_t type; + uint32_t reserved; +} __attribute__((packed)); + +struct memory_map { + uint32_t size; + uint32_t version; + struct memory_segment entries[]; +} __attribute__((packed)); + +/** + * Initalize system memory allocator + */ +extern void memory_init(struct memory_map *map); + +/** + * Disabled cpu interupts to not interfere with + * current memory allocations. + */ +extern void memory_lock(void); + +/** + * Reenabled cpu interupts + */ +extern void memory_unlock(void); + +/** + * @returns how much memory the system has + */ +extern uint64_t memory_total(void); + +/** + * @returns how much memory is free + */ +extern uint64_t memory_free(void); + +/** + * @returns how much memory is used + */ +extern uint64_t memory_used(void); + +/** + * Allocates a single page in memory. + * @returns the page if allocated or NULL on failure + */ +extern void *alloc_page(void); + +/** + * Allocats count pages in memory + * @param count - the number of continious pages to allocate + * @returns the pages if allocated or NULL on failure + */ +extern void *alloc_pages(int count); + +/** + * Frees a single page in memory + * @param page - the pointer to the page + */ +extern void free_page(void *page); + +/** + * 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 + */ +extern void *mmap(void *addr, size_t len); + +/** + * Unmaps mapped address from the mmap function + * @param addr - the address returned from mmap + * @param len - the length allocated + */ +extern void unmap(void *addr); + +/** + * Allocates size_t bytes in memory + * @param size - the amount of bytes to allocate + * @retruns the address allocated or NULL on failure + */ +extern void *malloc(size_t size); + +/** + * Reallocates 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 set the pointer to + * @returns the address allocated or NULL on failure + */ +extern void *realloc(void *ptr, size_t size); + +/** + * Frees a allocated pointer in memory + * @param ptr - the pointer to free + */ +extern void free(void *ptr); diff --git a/include/memory/physalloc.h b/include/memory/physalloc.h new file mode 100644 index 0000000..f22432e --- /dev/null +++ b/include/memory/physalloc.h @@ -0,0 +1,30 @@ +#pragma once + +#ifndef MEMORY_INTERNAL + #error "Do not include headers from <memory/___.h>, only use <memory.h>" +#endif + +/** + * Allocates a single physical page in memory + * @preturns the physical address of the page + */ +extern void *alloc_phys_page(void); + +/** + * Allocates count physical pages in memory + * @returns the physical address of the first page + */ +extern void *alloc_phys_pages(int count); + +/** +* Frees a single physical page in memory + * @param ptr - the physical address of the page + */ +extern void free_phys_page(void *ptr); + +/** + * Frees count physical pages in memory + * @param ptr - the physical address of the first page + * @param count - the number of pages in the list + */ +extern void free_phys_pages(void *ptr, int count); diff --git a/include/memory/virtalloc.h b/include/memory/virtalloc.h new file mode 100644 index 0000000..c4bac56 --- /dev/null +++ b/include/memory/virtalloc.h @@ -0,0 +1,24 @@ +#pragma once + +#ifndef MEMORY_INTERNAL + #error "Do not include headers from <memory/___.h>, only use <memory.h>" +#endif + +/** + * Initalizes the virtual address allocator + */ +void virtaddr_init(void); + +/** + * Allocate a virtual address of length x pages + * @param pages - x pages + * @returns virt addr + */ +void *virtaddr_alloc(int pages); + +/** + * Free the virtual address from virtaddr_alloc + * @param virtaddr - the addr to free + * @returns number of pages used for virtaddr + */ +long virtaddr_free(void *virtaddr); diff --git a/include/shim.h b/include/shim.h new file mode 100644 index 0000000..a0f0156 --- /dev/null +++ b/include/shim.h @@ -0,0 +1,7 @@ +#pragma once + +#include <memory.h> + +struct boot_info { + struct memory_map *map; +}; diff --git a/include/term.h b/include/term.h new file mode 100644 index 0000000..07ebacb --- /dev/null +++ b/include/term.h @@ -0,0 +1,3 @@ +#pragma once + + |