From 8a7653bcfadc48e400ad7c6b1346a6c6d7f73deb Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Thu, 1 May 2025 20:19:38 -0400 Subject: ramdisk --- kernel/mboot/module.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'kernel/mboot') diff --git a/kernel/mboot/module.c b/kernel/mboot/module.c index 79d092e..cb05b45 100644 --- a/kernel/mboot/module.c +++ b/kernel/mboot/module.c @@ -1,3 +1,4 @@ +#include "comus/memory.h" #include #include "mboot.h" @@ -12,13 +13,34 @@ struct multiboot_tag_module { char cmdline[]; }; +static void *mapped_addr = NULL; +size_t initrd_len; + void *mboot_get_initrd(size_t *len) { - void *tag = locate_mboot_table(MULTIBOOT_TAG_TYPE_MODULE); + struct multiboot_tag_module *mod; + void *tag, *phys; + + // if already loaded, return + if (mapped_addr) { + *len = initrd_len; + return mapped_addr; + } + + // locate + tag = locate_mboot_table(MULTIBOOT_TAG_TYPE_MODULE); if (tag == NULL) return NULL; - struct multiboot_tag_module *mod = (struct multiboot_tag_module *)tag; - *len = mod->mod_end - mod->mod_start; - return (void *)(uintptr_t)mod->mod_start; + mod = (struct multiboot_tag_module *)tag; + phys = (void *) (uintptr_t) mod->mod_start; + initrd_len = mod->mod_end - mod->mod_start; + + // map addr + mapped_addr = kmapaddr(phys, NULL, initrd_len, F_PRESENT | F_WRITEABLE); + if (mapped_addr == NULL) + return NULL; + + *len = initrd_len; + return mapped_addr; } -- cgit v1.2.3-freya From f7a899db24b91005e57bf4d3b5494e59965c7f04 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Thu, 1 May 2025 21:34:22 -0400 Subject: changes idk --- kernel/fs/tar.c | 1 + kernel/lib/kalloc.c | 4 +++- kernel/mboot/module.c | 2 +- kernel/memory/paging.c | 3 ++- kernel/memory/physalloc.c | 21 ++++++++++++++++----- kernel/memory/virtalloc.c | 6 +++--- 6 files changed, 26 insertions(+), 11 deletions(-) (limited to 'kernel/mboot') diff --git a/kernel/fs/tar.c b/kernel/fs/tar.c index 86af81f..061dd0c 100644 --- a/kernel/fs/tar.c +++ b/kernel/fs/tar.c @@ -1,3 +1,4 @@ +#include "lib/kio.h" #include #include diff --git a/kernel/lib/kalloc.c b/kernel/lib/kalloc.c index 02e9457..8dca46b 100644 --- a/kernel/lib/kalloc.c +++ b/kernel/lib/kalloc.c @@ -1,3 +1,4 @@ +#include "lib/kio.h" #include #include @@ -34,7 +35,8 @@ static struct page_header *get_header(void *ptr) static void *alloc_new(size_t size) { - size_t pages = ((size + header_len + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE; + size_t pages = + ((size + header_len + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE; void *addr = kalloc_pages(pages); void *mem = (char *)addr + header_len; diff --git a/kernel/mboot/module.c b/kernel/mboot/module.c index cb05b45..bf15eca 100644 --- a/kernel/mboot/module.c +++ b/kernel/mboot/module.c @@ -33,7 +33,7 @@ void *mboot_get_initrd(size_t *len) return NULL; mod = (struct multiboot_tag_module *)tag; - phys = (void *) (uintptr_t) mod->mod_start; + phys = (void *)(uintptr_t)mod->mod_start; initrd_len = mod->mod_end - mod->mod_start; // map addr diff --git a/kernel/memory/paging.c b/kernel/memory/paging.c index 9dfa236..39f7638 100644 --- a/kernel/memory/paging.c +++ b/kernel/memory/paging.c @@ -1,3 +1,4 @@ +#include "lib/kio.h" #include #include @@ -353,7 +354,7 @@ static volatile struct pt *pt_alloc(volatile struct pd *pPD, void *vADDR, } pPT = alloc_phys_page(); - if (pPD == NULL) + if (pPT == NULL) return NULL; vPT = PT_MAP(pPT); diff --git a/kernel/memory/physalloc.c b/kernel/memory/physalloc.c index 8971bcf..4255339 100644 --- a/kernel/memory/physalloc.c +++ b/kernel/memory/physalloc.c @@ -1,3 +1,4 @@ +#include "lib/kio.h" #include #include #include @@ -62,7 +63,7 @@ static long page_idx(void *page) static inline bool bitmap_get(size_t i) { - return (bitmap[i / 64] >> i % 64) & 1; + return (bitmap[i / 64] >> (i % 64)) & 1; } static inline void bitmap_set(size_t i, bool v) @@ -71,9 +72,9 @@ static inline void bitmap_set(size_t i, bool v) free_memory -= PAGE_SIZE; else free_memory += PAGE_SIZE; - int idx = i / 64; - bitmap[idx] &= ~(1 << i % 64); - bitmap[idx] |= (v << i % 64); + size_t idx = i / 64; + bitmap[idx] &= ~(1 << (i % 64)); + bitmap[idx] |= (v << (i % 64)); } void *alloc_phys_page(void) @@ -105,9 +106,17 @@ void *alloc_phys_pages_exact(size_t pages) free_region_start = i; n_contiguous++; if (n_contiguous == pages) { + void *pADDR; + pADDR = page_at(free_region_start); + + if (pADDR == NULL) { + n_contiguous = 0; + continue; + } + for (size_t j = 0; j < pages; j++) bitmap_set(free_region_start + j, true); - return page_at(free_region_start); + return pADDR; } } else n_contiguous = 0; @@ -118,6 +127,7 @@ void *alloc_phys_pages_exact(size_t pages) struct phys_page_slice alloc_phys_page_withextra(size_t max_pages) { + panic("please dont use this its broken i think?!\n"); if (max_pages == 0) return PHYS_PAGE_SLICE_NULL; @@ -160,6 +170,7 @@ void free_phys_page(void *ptr) void free_phys_pages_slice(struct phys_page_slice slice) { + panic("please dont use this its broken i think?!\n"); free_phys_pages(slice.pagestart, slice.num_pages); } diff --git a/kernel/memory/virtalloc.c b/kernel/memory/virtalloc.c index 4ee75bb..da64f3b 100644 --- a/kernel/memory/virtalloc.c +++ b/kernel/memory/virtalloc.c @@ -100,9 +100,9 @@ void virtaddr_init(struct virt_ctx *ctx) ctx->used_node_count = 0; ctx->is_allocating = false; - virtaddr_take(ctx, (void *)kernel_start, - ((uint64_t)kernel_end - (uint64_t)kernel_start) / PAGE_SIZE + - 1); + virtaddr_take(ctx, (void *)0, + ((uint64_t)kernel_end + PAGE_SIZE - 1) / PAGE_SIZE * + PAGE_SIZE); } int virtaddr_clone(struct virt_ctx *old, struct virt_ctx *new) -- cgit v1.2.3-freya From 40bfcbb2cf8a39e58ec19b21956caaf4685c9b8b Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Tue, 6 May 2025 12:34:48 -0400 Subject: terrible hack to not overwrite mboot data --- kernel/include/comus/mboot.h | 10 ++++++++++ kernel/mboot/mboot.c | 19 +++++++++++++++++++ kernel/mboot/module.c | 16 +++++++++++++--- kernel/memory/physalloc.c | 24 ++++++++++++++++-------- 4 files changed, 58 insertions(+), 11 deletions(-) (limited to 'kernel/mboot') diff --git a/kernel/include/comus/mboot.h b/kernel/include/comus/mboot.h index aba3e4e..bed29f1 100644 --- a/kernel/include/comus/mboot.h +++ b/kernel/include/comus/mboot.h @@ -47,4 +47,14 @@ EFI_HANDLE mboot_get_efi_hdl(void); */ void *mboot_get_initrd(size_t *len); +/** + * Returns the physical pointer to the loaded init ram disk with size given by len + */ +void *mboot_get_initrd_phys(size_t *len); + +/** + * Gets the end of the mboot pointer + */ +void *mboot_end(void); + #endif /* mboot.h */ diff --git a/kernel/mboot/mboot.c b/kernel/mboot/mboot.c index e4547e7..8163f04 100644 --- a/kernel/mboot/mboot.c +++ b/kernel/mboot/mboot.c @@ -5,6 +5,8 @@ static volatile void *mboot = NULL; +extern char kernel_end[]; + void mboot_init(long magic, volatile void *ptr) { if (magic != MULTIBOOT2_BOOTLOADER_MAGIC) @@ -12,6 +14,23 @@ void mboot_init(long magic, volatile void *ptr) mboot = ptr; } +void *mboot_end(void) +{ + if (mboot == NULL) + return NULL; + + struct multiboot *info = (struct multiboot *)mboot; + uintptr_t mboot_end, initrd_end; + size_t initrd_len; + + mboot_end = (uintptr_t)info + info->total_size; + initrd_end = (uintptr_t)mboot_get_initrd_phys(&initrd_len); + if (initrd_end) + initrd_end += initrd_len; + + return (void *)MAX(mboot_end, initrd_end); +} + void *locate_mboot_table(uint32_t type) { if (mboot == NULL) diff --git a/kernel/mboot/module.c b/kernel/mboot/module.c index bf15eca..03c5147 100644 --- a/kernel/mboot/module.c +++ b/kernel/mboot/module.c @@ -1,4 +1,4 @@ -#include "comus/memory.h" +#include #include #include "mboot.h" @@ -16,7 +16,7 @@ struct multiboot_tag_module { static void *mapped_addr = NULL; size_t initrd_len; -void *mboot_get_initrd(size_t *len) +void *mboot_get_initrd_phys(size_t *len) { struct multiboot_tag_module *mod; void *tag, *phys; @@ -36,11 +36,21 @@ void *mboot_get_initrd(size_t *len) phys = (void *)(uintptr_t)mod->mod_start; initrd_len = mod->mod_end - mod->mod_start; + *len = initrd_len; + return phys; +} + +void *mboot_get_initrd(size_t *len) +{ + // get phys + void *phys = mboot_get_initrd_phys(len); + if (phys == NULL) + return NULL; + // map addr mapped_addr = kmapaddr(phys, NULL, initrd_len, F_PRESENT | F_WRITEABLE); if (mapped_addr == NULL) return NULL; - *len = initrd_len; return mapped_addr; } diff --git a/kernel/memory/physalloc.c b/kernel/memory/physalloc.c index 47522b8..7551c75 100644 --- a/kernel/memory/physalloc.c +++ b/kernel/memory/physalloc.c @@ -1,12 +1,14 @@ -#include "lib/kio.h" #include #include #include +#include +#include #include "physalloc.h" extern char kernel_start[]; extern char kernel_end[]; +static void *kernel_real_end = NULL; // between memory_start and kernel_start will be the bitmap static uintptr_t memory_start = 0; @@ -30,16 +32,17 @@ static int n_pages(const struct memory_segment *m) return (m->len + PAGE_SIZE - 1) / PAGE_SIZE; } -static void *page_at(int i) +static void *page_at(size_t i) { - int cur_page = 0; + size_t cur_page = 0; + const struct memory_segment *m = page_start; for (uint64_t idx = 0; idx < segment_count; idx++) { - const struct memory_segment *m = page_start; - int pages = n_pages(m); + size_t pages = n_pages(m); if (i - cur_page < pages) { return (void *)(m->addr + (PAGE_SIZE * (i - cur_page))); } cur_page += pages; + m++; } return NULL; } @@ -48,8 +51,8 @@ static long page_idx(void *page) { uintptr_t addr = (uintptr_t)page; int cur_page = 0; + const struct memory_segment *m = page_start; for (uint64_t idx = 0; idx < segment_count; idx++) { - const struct memory_segment *m = page_start; if (addr < m->addr) { return -1; } @@ -57,6 +60,7 @@ static long page_idx(void *page) return cur_page + ((addr - m->addr) / PAGE_SIZE); } cur_page += n_pages(m); + m++; } return -1; } @@ -211,7 +215,7 @@ static struct memory_segment clamp_segment(const struct memory_segment *segment) if (memory_start) start = memory_start; else - start = (uintptr_t)kernel_end; + start = (uintptr_t)kernel_real_end; if (segment->addr < start) { addr = start; @@ -243,6 +247,10 @@ void physalloc_init(struct memory_map *map) segment_count = 0; + kernel_real_end = mboot_end(); + if ((char *)kernel_real_end < kernel_end) + kernel_real_end = kernel_end; + for (uint32_t i = 0; i < map->entry_count; i++) { struct memory_segment *segment = &map->entries[i]; @@ -256,7 +264,7 @@ void physalloc_init(struct memory_map *map) long bitmap_pages = (page_count / 64 / PAGE_SIZE) + 1; long bitmap_size = bitmap_pages * PAGE_SIZE; - bitmap = (uint64_t *)page_align((uintptr_t)kernel_end); + bitmap = (uint64_t *)page_align((uintptr_t)kernel_real_end); long page_area_size = segment_count * sizeof(struct memory_segment); char *page_area_addr = (char *)bitmap + bitmap_size; -- cgit v1.2.3-freya