From 2dbf529c33aa3e24beff944758d586bb0608c1be Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Tue, 15 Apr 2025 22:20:59 -0400 Subject: expand memory manager work with userspace (more then one ctx) --- kernel/memory/memory.c | 48 +++++++++++++++++- kernel/memory/memory.h | 8 +++ kernel/memory/paging.c | 81 +++++++++++++++++++---------- kernel/memory/paging.h | 10 ---- kernel/memory/physalloc.c | 5 +- kernel/memory/virtalloc.c | 126 +++++++++++++++++++++------------------------- kernel/memory/virtalloc.h | 44 ++++++++++++++-- 7 files changed, 211 insertions(+), 111 deletions(-) create mode 100644 kernel/memory/memory.h (limited to 'kernel/memory') diff --git a/kernel/memory/memory.c b/kernel/memory/memory.c index 145ce2b..b4ecb0d 100644 --- a/kernel/memory/memory.c +++ b/kernel/memory/memory.c @@ -1,21 +1,67 @@ +#include "lib/klib.h" #include #include #include #include +#include "memory.h" #include "paging.h" #include "virtalloc.h" #include "physalloc.h" +mem_ctx_t kernel_mem_ctx; +struct mem_ctx_s _kernel_mem_ctx; +extern volatile char kernel_pml4[]; +extern struct virt_ctx kernel_virt_ctx; + +void *kmapaddr(void *phys, void *virt, size_t len, unsigned int flags) +{ + return mem_mapaddr(kernel_mem_ctx, phys, virt, len, flags); +} + +void kunmapaddr(void *virt) +{ + mem_unmapaddr(kernel_mem_ctx, virt); +} + +void *kalloc_page(void) +{ + return mem_alloc_page(kernel_mem_ctx); +} + +void *kalloc_pages(size_t count) +{ + return mem_alloc_pages(kernel_mem_ctx, count); +} + +void kfree_pages(void *ptr) +{ + mem_free_pages(kernel_mem_ctx, ptr); +} + +int kload_page(void *virt) +{ + return mem_load_page(kernel_mem_ctx, virt); +} + +mem_ctx_t alloc_mem_ctx(void) +{ + panic("alloc_mem_ctx not yet implemented"); +} + void memory_init(void) { struct memory_map mmap; if (mboot_get_mmap(&mmap)) panic("failed to load memory map"); + kernel_mem_ctx = &_kernel_mem_ctx; + kernel_mem_ctx->pml4 = kernel_pml4; + kernel_mem_ctx->virtctx = &kernel_virt_ctx; + cli(); paging_init(); - virtaddr_init(); + virtaddr_init(kernel_mem_ctx->virtctx); physalloc_init(&mmap); sti(); } diff --git a/kernel/memory/memory.h b/kernel/memory/memory.h new file mode 100644 index 0000000..c39656d --- /dev/null +++ b/kernel/memory/memory.h @@ -0,0 +1,8 @@ + +#include +#include "virtalloc.h" + +struct mem_ctx_s { + struct virt_ctx *virtctx; + volatile char *pml4; +}; diff --git a/kernel/memory/paging.c b/kernel/memory/paging.c index 5c4fa5c..2671cc2 100644 --- a/kernel/memory/paging.c +++ b/kernel/memory/paging.c @@ -1,9 +1,13 @@ #include #include +#include +#include + #include "virtalloc.h" #include "physalloc.h" #include "paging.h" +#include "memory.h" // PAGE MAP LEVEL 4 ENTRY struct pml4e { @@ -514,34 +518,56 @@ static inline void *page_align(void *addr) return (void *)a; } -void *kmapaddr(void *addr, size_t len) +void *mem_mapaddr(mem_ctx_t ctx, void *phys, void *virt, size_t len, + unsigned int flags) { - void *phys = page_align(addr); - ptrdiff_t error = (char *)addr - (char *)phys; + TRACE("PHYS %16p VIRT %16p LEN %zu FLAGS %08x \n", phys, virt, len, flags); + + long pages; + ptrdiff_t error; + void *aligned_phys; + + // get length and physical page aligned address + aligned_phys = page_align(phys); + error = (char *)phys - (char *)aligned_phys; len += error; - long pages = len / PAGE_SIZE + 1; - void *virt = virtaddr_alloc(pages); - if (virt == NULL) { + pages = len / PAGE_SIZE + 1; + + // get page aligned (or allocate) vitural address + if (virt == NULL) + virt = virtaddr_alloc(ctx->virtctx, pages); + if (virt == NULL) return NULL; - } - if (map_pages(kernel_pml4, virt, phys, F_WRITEABLE, pages)) { - virtaddr_free(virt); + + if (map_pages((volatile struct pml4e *)ctx->pml4, virt, aligned_phys, + F_WRITEABLE | flags, pages)) { + virtaddr_free(ctx->virtctx, virt); return NULL; } + return (char *)virt + error; } -void kunmapaddr(void *addr) +void mem_unmapaddr(mem_ctx_t ctx, void *virt) { - long pages = virtaddr_free(addr); + TRACE("VIRT %16p\n", virt); + + long pages = virtaddr_free(ctx->virtctx, virt); if (pages < 1) return; - unmap_pages(kernel_pml4, addr, pages); + unmap_pages(kernel_pml4, virt, pages); } -void *kalloc_pages(size_t count) +void *mem_alloc_page(mem_ctx_t ctx) { - void *virt = virtaddr_alloc(count); + return mem_alloc_pages(ctx, 1); +} + +void *mem_alloc_pages(mem_ctx_t ctx, size_t count) +{ + TRACE("COUNT %zu\n", count); + + void *virt = virtaddr_alloc(ctx->virtctx, count); if (virt == NULL) return NULL; //void *phys = alloc_phys_pages(count); @@ -549,32 +575,33 @@ void *kalloc_pages(size_t count) // virtaddr_free(virt); // return NULL; //} - if (map_pages(kernel_pml4, virt, + if (map_pages((volatile struct pml4e *)ctx->pml4, virt, //phys, //F_WRITEABLE, NULL, F_WRITEABLE, count)) { - virtaddr_free(virt); + virtaddr_free(ctx->virtctx, virt); return NULL; } return virt; } -void *kalloc_page(void) +void mem_free_pages(mem_ctx_t ctx, void *virt) { - return kalloc_pages(1); -} + TRACE("VIRT %16p\n", virt); -void kfree_pages(void *virt) -{ - long pages = virtaddr_free(virt); - if (pages < 1) - return; - unmap_pages(kernel_pml4, virt, pages); + long pages = virtaddr_free(ctx->virtctx, virt); + if (pages == 1) + unmap_page((volatile struct pml4e *)ctx->pml4, virt); + else if (pages > 1) + unmap_pages((volatile struct pml4e *)ctx->pml4, virt, pages); } -int kload_page(void *virt_addr) +int mem_load_page(mem_ctx_t ctx, void *virt_addr) { - volatile struct pte *page = get_page(kernel_pml4, virt_addr); + TRACE("VIRT %16p\n", virt_addr); + + volatile struct pte *page = + get_page((volatile struct pml4e *)ctx->pml4, virt_addr); if (page == NULL) return -1; if (page->loaded) diff --git a/kernel/memory/paging.h b/kernel/memory/paging.h index be6fd06..b54d422 100644 --- a/kernel/memory/paging.h +++ b/kernel/memory/paging.h @@ -9,16 +9,6 @@ #ifndef PAGING_H_ #define PAGING_H_ -#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 - void paging_init(void); #endif /* paging.h */ diff --git a/kernel/memory/physalloc.c b/kernel/memory/physalloc.c index a907077..7083c21 100644 --- a/kernel/memory/physalloc.c +++ b/kernel/memory/physalloc.c @@ -185,9 +185,10 @@ void physalloc_init(struct memory_map *map) memory_start = page_align((uintptr_t)page_area_addr + page_area_size); - bitmap = kmapaddr(bitmap, bitmap_size); + bitmap = kmapaddr(bitmap, NULL, bitmap_size, F_WRITEABLE); memset(bitmap, 0, bitmap_size); - page_area_addr = kmapaddr(page_area_addr, page_area_size); + page_area_addr = + kmapaddr(page_area_addr, NULL, page_area_size, F_WRITEABLE); memset(page_area_addr, 0, page_area_size); page_start = (struct memory_segment *)page_area_addr; diff --git a/kernel/memory/virtalloc.c b/kernel/memory/virtalloc.c index 6b7fd20..1c83427 100644 --- a/kernel/memory/virtalloc.c +++ b/kernel/memory/virtalloc.c @@ -3,45 +3,29 @@ #include "virtalloc.h" -struct addr_node { - uintptr_t start; - uintptr_t end; - struct addr_node *next; - struct addr_node *prev; - uint8_t is_alloc; // if node is storing allocated data - uint8_t is_used; // if node is in use by virtalloc -}; - -#define BSS_NODES 64 -static struct addr_node bootstrap_nodes[BSS_NODES]; -static struct addr_node *alloc_nodes = NULL; -static size_t free_node_start = 0; -static size_t alloc_node_count = 0; -static size_t used_node_count = 0; -static bool is_allocating = false; - -static struct addr_node *start_node = NULL; - -static struct addr_node *get_node_idx(int idx) +struct virt_ctx kernel_virt_ctx; + +static struct virt_addr_node *get_node_idx(struct virt_ctx *ctx, int idx) { - if (idx < BSS_NODES) { - return &bootstrap_nodes[idx]; + if (idx < BOOTSTRAP_VIRT_ALLOC_NODES) { + return &ctx->bootstrap_nodes[idx]; } else { - return &alloc_nodes[idx - BSS_NODES]; + return &ctx->alloc_nodes[idx - BOOTSTRAP_VIRT_ALLOC_NODES]; } } -static void update_node_ptrs(struct addr_node *old, struct addr_node *new, - int old_len, int new_len) +static void update_node_ptrs(struct virt_addr_node *old, + struct virt_addr_node *new, int old_len, + int new_len) { if (old == NULL) return; int idx = 0; for (int i = 0; i < old_len; i++) { - struct addr_node *o = &old[i]; + struct virt_addr_node *o = &old[i]; if (o && !o->is_used) continue; - struct addr_node *n = &new[idx++]; + struct virt_addr_node *n = &new[idx++]; *n = *o; if (n->prev != NULL) n->prev->next = n; @@ -49,37 +33,38 @@ static void update_node_ptrs(struct addr_node *old, struct addr_node *new, n->next->prev = n; } for (int i = idx; i < new_len; i++) { - struct addr_node *n = &new[idx++]; + struct virt_addr_node *n = &new[idx++]; n->is_used = false; } } -static struct addr_node *get_node(void) +static struct virt_addr_node *get_node(struct virt_ctx *ctx) { - size_t count = BSS_NODES + alloc_node_count; + size_t count = BOOTSTRAP_VIRT_ALLOC_NODES + ctx->alloc_node_count; - if (!is_allocating && used_node_count + 16 >= count) { - is_allocating = true; - int new_alloc = alloc_node_count * 2; + if (!ctx->is_allocating && ctx->used_node_count + 16 >= count) { + ctx->is_allocating = true; + int new_alloc = ctx->alloc_node_count * 2; if (new_alloc < 8) new_alloc = 8; - struct addr_node *new_nodes; - new_nodes = kalloc(sizeof(struct addr_node) * new_alloc); + struct virt_addr_node *new_nodes; + new_nodes = kalloc(sizeof(struct virt_addr_node) * new_alloc); if (new_nodes == NULL) panic("virt addr alloc nodes is null"); - update_node_ptrs(alloc_nodes, new_nodes, alloc_node_count, new_alloc); - kfree(alloc_nodes); - alloc_nodes = new_nodes; - alloc_node_count = new_alloc; - is_allocating = false; - count = BSS_NODES + alloc_node_count; + update_node_ptrs(ctx->alloc_nodes, new_nodes, ctx->alloc_node_count, + new_alloc); + kfree(ctx->alloc_nodes); + ctx->alloc_nodes = new_nodes; + ctx->alloc_node_count = new_alloc; + ctx->is_allocating = false; + count = BOOTSTRAP_VIRT_ALLOC_NODES + ctx->alloc_node_count; } - size_t idx = free_node_start; + size_t idx = ctx->free_node_start; for (; idx < count; idx++) { - struct addr_node *node = get_node_idx(idx); + struct virt_addr_node *node = get_node_idx(ctx, idx); if (!node->is_used) { - used_node_count++; + ctx->used_node_count++; return node; } } @@ -87,15 +72,15 @@ static struct addr_node *get_node(void) panic("could not get virtaddr node"); } -static void free_node(struct addr_node *node) +static void free_node(struct virt_ctx *ctx, struct virt_addr_node *node) { node->is_used = false; - used_node_count--; + ctx->used_node_count--; } -void virtaddr_init(void) +void virtaddr_init(struct virt_ctx *ctx) { - struct addr_node init = { + struct virt_addr_node init = { .start = 0x400000, // third page table .end = 0x1000000000000, // 48bit memory address max .next = NULL, @@ -103,48 +88,53 @@ void virtaddr_init(void) .is_alloc = false, .is_used = true, }; - memsetv(bootstrap_nodes, 0, sizeof(bootstrap_nodes)); - bootstrap_nodes[0] = init; - start_node = &bootstrap_nodes[0]; + memsetv(ctx->bootstrap_nodes, 0, sizeof(ctx->bootstrap_nodes)); + ctx->bootstrap_nodes[0] = init; + ctx->alloc_nodes = NULL; + ctx->start_node = &ctx->bootstrap_nodes[0]; + ctx->free_node_start = 0; + ctx->alloc_node_count = 0; + ctx->used_node_count = 0; + ctx->is_allocating = false; } -static void merge_back(struct addr_node *node) +static void merge_back(struct virt_ctx *ctx, struct virt_addr_node *node) { while (node->prev) { if (node->is_alloc != node->prev->is_alloc) break; - struct addr_node *temp = node->prev; + struct virt_addr_node *temp = node->prev; node->start = temp->start; node->prev = temp->prev; if (temp->prev) temp->prev->next = node; - free_node(temp); + free_node(ctx, temp); } if (node->prev == NULL) { - start_node = node; + ctx->start_node = node; } } -static void merge_forward(struct addr_node *node) +static void merge_forward(struct virt_ctx *ctx, struct virt_addr_node *node) { while (node->next) { if (node->is_alloc != node->next->is_alloc) break; - struct addr_node *temp = node->next; + struct virt_addr_node *temp = node->next; node->end = temp->end; node->next = temp->next; if (temp->next) temp->next->prev = node; - free_node(temp); + free_node(ctx, temp); } } -void *virtaddr_alloc(int n_pages) +void *virtaddr_alloc(struct virt_ctx *ctx, int n_pages) { if (n_pages < 1) return NULL; long n_length = n_pages * PAGE_SIZE; - struct addr_node *node = start_node; + struct virt_addr_node *node = ctx->start_node; for (; node != NULL; node = node->next) { long length = node->end - node->start; @@ -152,11 +142,11 @@ void *virtaddr_alloc(int n_pages) continue; if (length >= n_length) { - struct addr_node *new = get_node(); + struct virt_addr_node *new = get_node(ctx); if (node->prev != NULL) { node->prev->next = new; } else { - start_node = new; + ctx->start_node = new; } new->next = node; new->prev = node->prev; @@ -168,8 +158,8 @@ void *virtaddr_alloc(int n_pages) new->is_used = true; new->next = node; void *mem = (void *)new->start; - merge_back(new); - merge_forward(new); + merge_back(ctx, new); + merge_forward(ctx, new); return mem; } } @@ -177,21 +167,21 @@ void *virtaddr_alloc(int n_pages) return NULL; } -long virtaddr_free(void *virtaddr) +long virtaddr_free(struct virt_ctx *ctx, void *virtaddr) { uintptr_t virt = (uintptr_t)virtaddr; if (virt % PAGE_SIZE) return -1; // not page aligned, we did not give this out!!! - struct addr_node *node = start_node; + struct virt_addr_node *node = ctx->start_node; for (; node != NULL; node = node->next) { if (node->start == virt) { int length = node->end - node->start; int pages = length / PAGE_SIZE; - merge_back(node); - merge_forward(node); + merge_back(ctx, node); + merge_forward(ctx, node); return pages; } } diff --git a/kernel/memory/virtalloc.h b/kernel/memory/virtalloc.h index a5ca840..9f974c5 100644 --- a/kernel/memory/virtalloc.h +++ b/kernel/memory/virtalloc.h @@ -9,23 +9,61 @@ #ifndef VIRTALLOC_H_ #define VIRTALLOC_H_ +#include +#include +#include + +#define BOOTSTRAP_VIRT_ALLOC_NODES 64 + +struct virt_addr_node { + /// first virtural address + uintptr_t start; + /// last virtural address + uintptr_t end; + /// next node in linked list + struct virt_addr_node *next; + /// prev node in linked list + struct virt_addr_node *prev; + /// if this node is storing any allocated data + uint8_t is_alloc; + /// if this node is in use by virtalloc + uint8_t is_used; +}; + +struct virt_ctx { + /// bootstrap nodes for the context (not in heap) + struct virt_addr_node bootstrap_nodes[BOOTSTRAP_VIRT_ALLOC_NODES]; + /// heap allocated nodes + struct virt_addr_node *alloc_nodes; + /// start node + struct virt_addr_node *start_node; + /// index of first free node + size_t free_node_start; + /// number of heap allocated nodes + size_t alloc_node_count; + /// number of used nodes + size_t used_node_count; + /// if we are currently allocating (recursion check) + bool is_allocating; +}; + /** * Initalizes the virtual address allocator */ -void virtaddr_init(void); +void virtaddr_init(struct virt_ctx *ctx); /** * Allocate a virtual address of length x pages * @param pages - x pages * @returns virt addr */ -void *virtaddr_alloc(int pages); +void *virtaddr_alloc(struct virt_ctx *ctx, 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); +long virtaddr_free(struct virt_ctx *ctx, void *virtaddr); #endif /* virtalloc.h */ -- cgit v1.2.3-freya From fea6e5f3d527e3fb33209d437f130c02c9e83680 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Tue, 15 Apr 2025 22:49:39 -0400 Subject: more stub mem_ctx fns --- kernel/include/comus/memory.h | 20 +++++++++++++++++++- kernel/memory/memory.c | 19 +++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) (limited to 'kernel/memory') diff --git a/kernel/include/comus/memory.h b/kernel/include/comus/memory.h index ee413e3..dec872d 100644 --- a/kernel/include/comus/memory.h +++ b/kernel/include/comus/memory.h @@ -11,6 +11,7 @@ #include #include +#include #define MMAP_MAX_ENTRY 64 #define PAGE_SIZE 4096 @@ -63,7 +64,24 @@ uint64_t memory_used(void); * * @returns pointer context or NULL on failure */ -mem_ctx_t alloc_mem_ctx(void); +mem_ctx_t mem_ctx_alloc(void); + +/** + * Clone a current memory context into a new one + * + * @param ctx - the memory context + * @param cow - mark all of the pages as copy on write + * + * @returns pointer context or NULL on failure + */ +mem_ctx_t mem_ctx_clone(mem_ctx_t ctx, bool cow); + +/** + * Free a memory context into a new one + * + * @param ctx - the memory context + */ +void mem_ctx_free(mem_ctx_t ctx); /** * Free a memory context diff --git a/kernel/memory/memory.c b/kernel/memory/memory.c index b4ecb0d..208001b 100644 --- a/kernel/memory/memory.c +++ b/kernel/memory/memory.c @@ -44,9 +44,24 @@ int kload_page(void *virt) return mem_load_page(kernel_mem_ctx, virt); } -mem_ctx_t alloc_mem_ctx(void) +mem_ctx_t mem_ctx_alloc(void) { - panic("alloc_mem_ctx not yet implemented"); + panic("not yet implemented"); +} + +mem_ctx_t mem_ctx_clone(mem_ctx_t ctx, bool cow) +{ + (void) ctx; + (void) cow; + + panic("not yet implemented"); +} + +void mem_ctx_free(mem_ctx_t ctx) +{ + (void) ctx; + + panic("not yet implemented"); } void memory_init(void) -- cgit v1.2.3-freya From 210d325b15041eda279d36e1b155095238b2b393 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Tue, 15 Apr 2025 22:50:36 -0400 Subject: fmt --- kernel/cpu/idt.c | 3 ++- kernel/memory/memory.c | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'kernel/memory') diff --git a/kernel/cpu/idt.c b/kernel/cpu/idt.c index 250f939..8b4465f 100644 --- a/kernel/cpu/idt.c +++ b/kernel/cpu/idt.c @@ -110,7 +110,8 @@ char *EXCEPTIONS[] = { "Reserved", }; -void idt_exception_handler(uint64_t exception, uint64_t code, struct cpu_regs *state) +void idt_exception_handler(uint64_t exception, uint64_t code, + struct cpu_regs *state) { uint64_t cr2; diff --git a/kernel/memory/memory.c b/kernel/memory/memory.c index 208001b..2d560f4 100644 --- a/kernel/memory/memory.c +++ b/kernel/memory/memory.c @@ -51,15 +51,15 @@ mem_ctx_t mem_ctx_alloc(void) mem_ctx_t mem_ctx_clone(mem_ctx_t ctx, bool cow) { - (void) ctx; - (void) cow; + (void)ctx; + (void)cow; panic("not yet implemented"); } void mem_ctx_free(mem_ctx_t ctx) { - (void) ctx; + (void)ctx; panic("not yet implemented"); } -- cgit v1.2.3-freya From 730aafc1758e4096853c2491345901a296b4cc3e Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Wed, 16 Apr 2025 16:48:56 -0400 Subject: remove all printing to report functions --- kernel/drivers/acpi.c | 37 +++++++++++++++++++++++++++---- kernel/drivers/pci.c | 4 ++++ kernel/include/comus/drivers/acpi.h | 5 +++++ kernel/include/comus/drivers/pci.h | 5 +++++ kernel/include/comus/memory.h | 6 ++++++ kernel/main.c | 14 ++++++++++++ kernel/mboot/mmap.c | 15 +------------ kernel/memory/physalloc.c | 43 +++++++++++++++++++++++++++++++------ 8 files changed, 105 insertions(+), 24 deletions(-) (limited to 'kernel/memory') diff --git a/kernel/drivers/acpi.c b/kernel/drivers/acpi.c index bd9fe54..41f7ba1 100644 --- a/kernel/drivers/acpi.c +++ b/kernel/drivers/acpi.c @@ -1,7 +1,9 @@ +#include "lib/kio.h" #include #include #include #include +#include struct acpi_header { uint32_t signature; @@ -295,7 +297,6 @@ static void acpi_load_table(uint64_t addr) kunmapaddr(mapped); return; } - kprintf("%.*s: %#016lx\n", 4, (char *)&mapped->signature, (size_t)temp); acpi_handle_table(mapped); } @@ -311,20 +312,48 @@ void acpi_init(void *rootsdp) } if (rsdp->revision == 0) { state.version = 0; - kprintf("ACPI 1.0\n"); acpi_load_table(rsdp->rsdt_addr); } else if (rsdp->revision == 2) { state.version = 2; struct xsdp *xsdp = (struct xsdp *)rsdp; - kprintf("ACPI 2.0\n"); acpi_load_table(xsdp->xsdt_addr); } else { panic("invalid acpi rev: %d\n", rsdp->revision); } - kprintf("\n"); outb(state.fadt->smi_command_port, state.fadt->acpi_enable); } +void acpi_report(void) +{ + if (state.version == 0) { + kprintf("ACPI 1.0\n"); + kprintf("%.*s: %#016lx\n", 4, (char *) &state.sdt.rsdt->h.signature, + (uintptr_t) state.sdt.rsdt); + } else { + kprintf("ACPI 2.0\n"); + kprintf("%.*s: %#016lx\n", 4, (char *) &state.sdt.xsdt->h.signature, + (uintptr_t) state.sdt.xsdt); + } + + if (state.fadt) + kprintf("%.*s: %#016lx\n", 4, (char *) &state.fadt->h.signature, + (uintptr_t) state.fadt); + if (state.dsdt) + kprintf("%.*s: %#016lx\n", 4, (char *) &state.dsdt->h.signature, + (uintptr_t) state.dsdt); + if (state.apic) + kprintf("%.*s: %#016lx\n", 4, (char *) &state.apic->h.signature, + (uintptr_t) state.apic); + if (state.hept) + kprintf("%.*s: %#016lx\n", 4, (char *) &state.hept->h.signature, + (uintptr_t) state.hept); + if (state.waet) + kprintf("%.*s: %#016lx\n", 4, (char *) &state.waet->h.signature, + (uintptr_t) state.waet); + + kprintf("\n"); +} + void acpi_shutdown(void) { outw((unsigned int)state.fadt->pm1_a_control_block, diff --git a/kernel/drivers/pci.c b/kernel/drivers/pci.c index e84ca11..ce2583f 100644 --- a/kernel/drivers/pci.c +++ b/kernel/drivers/pci.c @@ -134,6 +134,10 @@ void pci_init(void) } } } +} + +void pci_report(void) +{ kprintf("PCI DEVICES\n"); for (size_t i = 0; i < pci_table_next; i++) { print_device(&pci_table[i]); diff --git a/kernel/include/comus/drivers/acpi.h b/kernel/include/comus/drivers/acpi.h index bb11860..f4948f7 100644 --- a/kernel/include/comus/drivers/acpi.h +++ b/kernel/include/comus/drivers/acpi.h @@ -14,6 +14,11 @@ */ void acpi_init(void *rsdp); +/** + * Report ACPI tables + */ +void acpi_report(void); + /** * Shutdowns down the system */ diff --git a/kernel/include/comus/drivers/pci.h b/kernel/include/comus/drivers/pci.h index 34bcdda..b06aca9 100644 --- a/kernel/include/comus/drivers/pci.h +++ b/kernel/include/comus/drivers/pci.h @@ -56,6 +56,11 @@ struct pci_device { */ void pci_init(void); +/** + * Report all PCI devices + */ +void pci_report(void); + bool pci_findby_class(struct pci_device *dest, uint8_t class, uint8_t subclass, size_t *offset); bool pci_findby_id(struct pci_device *dest, uint16_t device, uint16_t vendor, diff --git a/kernel/include/comus/memory.h b/kernel/include/comus/memory.h index dec872d..ceca4aa 100644 --- a/kernel/include/comus/memory.h +++ b/kernel/include/comus/memory.h @@ -29,6 +29,7 @@ struct memory_segment { uint64_t addr; uint64_t len; + uint32_t type; }; struct memory_map { @@ -59,6 +60,11 @@ uint64_t memory_free(void); */ uint64_t memory_used(void); +/** + * Reports system memory usage and map + */ +void memory_report(void); + /** * Allocate a new memory context * diff --git a/kernel/main.c b/kernel/main.c index 4f91d95..f600320 100644 --- a/kernel/main.c +++ b/kernel/main.c @@ -1,10 +1,21 @@ +#include "lib/kio.h" #include #include #include #include +#include +#include #include #include +void kreport(void) +{ + cpu_report(); + memory_report(); + acpi_report(); + pci_report(); +} + void main(long magic, volatile void *mboot) { // initalize idt and pic @@ -22,6 +33,9 @@ void main(long magic, volatile void *mboot) // load file systems fs_init(); + // report system state + kreport(); + // halt kprintf("halting...\n"); } diff --git a/kernel/mboot/mmap.c b/kernel/mboot/mmap.c index e93421d..8959c49 100644 --- a/kernel/mboot/mmap.c +++ b/kernel/mboot/mmap.c @@ -20,11 +20,6 @@ struct multiboot_tag_mmap { struct multiboot_mmap_entry entries[]; }; -static const char *segment_type[] = { "Reserved", "Free", - "Reserved", "ACPI Reserved", - "Hibernation", "Defective", - "Unknown" }; - int mboot_get_mmap(struct memory_map *res) { void *tag = locate_mboot_table(MULTIBOOT_TAG_TYPE_MMAP); @@ -35,22 +30,14 @@ int mboot_get_mmap(struct memory_map *res) int idx = 0; uintptr_t i = (uintptr_t)mmap->entries; - kprintf("MEMORY MAP\n"); char buf[20]; for (; i < (uintptr_t)mmap->entries + mmap->size; i += mmap->entry_size, idx++) { struct multiboot_mmap_entry *seg = (struct multiboot_mmap_entry *)i; const char *type = NULL; - if (seg->type > 6) - type = segment_type[6]; - else - type = segment_type[seg->type]; - kprintf("ADDR: %16p LEN: %4s TYPE: %s (%d)\n", (void *)seg->addr, - btoa(seg->len, buf), type, seg->type); - if (seg->type != 1 || seg->len < 1) - continue; res->entries[idx].addr = seg->addr; res->entries[idx].len = seg->len; + res->entries[idx].type = seg->type; } res->entry_count = idx; diff --git a/kernel/memory/physalloc.c b/kernel/memory/physalloc.c index 7083c21..328502f 100644 --- a/kernel/memory/physalloc.c +++ b/kernel/memory/physalloc.c @@ -1,3 +1,4 @@ +#include "lib/kio.h" #include #include #include @@ -16,8 +17,14 @@ static uint64_t total_memory; static uint64_t free_memory; static uint64_t page_count; static uint64_t segment_count; +struct memory_map phys_mmap; struct memory_segment *page_start; +static const char *segment_type_str[] = { "Reserved", "Free", + "Reserved", "ACPI Reserved", + "Hibernation", "Defective", + "Unknown" }; + static int n_pages(const struct memory_segment *m) { return m->len / PAGE_SIZE; @@ -115,6 +122,10 @@ void free_phys_pages(void *ptr, int pages) static bool segment_invalid(const struct memory_segment *segment) { + if (segment->len < 1) + return true; + if (segment->type != 1) + return true; if (segment->addr < kaddr(kernel_start)) return true; if (segment->addr + segment->len < memory_start) @@ -161,6 +172,7 @@ void physalloc_init(struct memory_map *map) free_memory = 0; page_count = 0; page_start = NULL; + phys_mmap = *map; segment_count = 0; @@ -209,12 +221,6 @@ void physalloc_init(struct memory_map *map) total_memory = page_count * PAGE_SIZE; page_count -= bitmap_pages; free_memory = page_count * PAGE_SIZE; - - char buf[20]; - kprintf("\nMEMORY USAGE\n"); - kprintf("mem total: %s\n", btoa(memory_total(), buf)); - kprintf("mem free: %s\n", btoa(memory_free(), buf)); - kprintf("mem used: %s\n\n", btoa(memory_used(), buf)); } uint64_t memory_total(void) @@ -231,3 +237,28 @@ uint64_t memory_used(void) { return total_memory - free_memory; } + +void memory_report(void) +{ + char buf[20]; + + kprintf("MEMORY MAP\n"); + for (uint32_t i = 0; i < phys_mmap.entry_count; i++) { + struct memory_segment *seg; + const char *type_str; + + seg = &phys_mmap.entries[i]; + if (seg->type > 6) + type_str = segment_type_str[6]; + else + type_str = segment_type_str[seg->type]; + + kprintf("ADDR: %16p LEN: %4s TYPE: %s (%d)\n", (void *)seg->addr, + btoa(seg->len, buf), type_str, seg->type); + } + + kprintf("\nMEMORY USAGE\n"); + kprintf("mem total: %s\n", btoa(memory_total(), buf)); + kprintf("mem free: %s\n", btoa(memory_free(), buf)); + kprintf("mem used: %s\n\n", btoa(memory_used(), buf)); +} -- cgit v1.2.3-freya From 0ddc618b1d60ef6729326aa4e45b8280fcf51775 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Wed, 16 Apr 2025 16:51:25 -0400 Subject: fmt --- kernel/cpu/cpu.c | 130 ++++++++++++++++++++++++---------------------- kernel/drivers/acpi.c | 28 +++++----- kernel/memory/physalloc.c | 6 +-- 3 files changed, 84 insertions(+), 80 deletions(-) (limited to 'kernel/memory') diff --git a/kernel/cpu/cpu.c b/kernel/cpu/cpu.c index 3835b68..136a1d8 100644 --- a/kernel/cpu/cpu.c +++ b/kernel/cpu/cpu.c @@ -18,19 +18,19 @@ static inline void sse_init(void) { size_t cr0, cr4; __asm__ volatile("mov %%cr0, %0" : "=r"(cr0)); - cr0 &= ~0x4; // clear coprocessor emulation - cr0 |= 0x2; // set coprocessor monitoring - __asm__ volatile("mov %0, %%cr0" :: "r"(cr0)); + cr0 &= ~0x4; // clear coprocessor emulation + cr0 |= 0x2; // set coprocessor monitoring + __asm__ volatile("mov %0, %%cr0" ::"r"(cr0)); __asm__ volatile("mov %%cr4, %0" : "=r"(cr4)); cr4 |= 1 << 9; // set CR4.OSFXSR cr4 |= 1 << 10; // set CR4.OSXMMEXCPT - __asm__ volatile("mov %0, %%cr4" :: "r"(cr4)); + __asm__ volatile("mov %0, %%cr4" ::"r"(cr4)); } static inline void fxsave_init(void) { static char fxsave_region[512] __attribute__((aligned(16))); - __asm__ volatile("fxsave %0" :: "m"(fxsave_region)); + __asm__ volatile("fxsave %0" ::"m"(fxsave_region)); } static inline void xsave_init(void) @@ -38,23 +38,21 @@ static inline void xsave_init(void) size_t cr4; __asm__ volatile("mov %%cr4, %0" : "=r"(cr4)); cr4 |= 1 << 18; // set CR4.OSXSAVE - __asm__ volatile("mov %0, %%cr4" :: "r"(cr4)); + __asm__ volatile("mov %0, %%cr4" ::"r"(cr4)); } static inline void avx_init(void) { - __asm__ volatile( - "pushq %rax;" - "pushq %rcx;" - "pushq %rdx;" - "xorq %rcx, %rcx;" - "xgetbv;" - "orq $7, %rax;" - "xsetbv;" - "popq %rdx;" - "popq %rcx;" - "popq %rax;" - ); + __asm__ volatile("pushq %rax;" + "pushq %rcx;" + "pushq %rdx;" + "xorq %rcx, %rcx;" + "xgetbv;" + "orq $7, %rax;" + "xsetbv;" + "popq %rdx;" + "popq %rcx;" + "popq %rax;"); } void cpu_init(void) @@ -119,30 +117,37 @@ void cpu_report(void) kputs("\n\n"); } -static inline void cpuid(uint32_t leaf, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) +static inline void cpuid(uint32_t leaf, uint32_t *eax, uint32_t *ebx, + uint32_t *ecx, uint32_t *edx) { - __asm__ volatile("cpuid" - : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) - : "0" (leaf)); + __asm__ volatile("cpuid" + : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx) + : "0"(leaf)); } -static inline void cpuid_count(uint32_t leaf, uint32_t subleaf, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx) +static inline void cpuid_count(uint32_t leaf, uint32_t subleaf, uint32_t *eax, + uint32_t *ebx, uint32_t *ecx, uint32_t *edx) { - __asm__ volatile("cpuid" - : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) - : "0" (leaf), "2" (subleaf)); + __asm__ volatile("cpuid" + : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx) + : "0"(leaf), "2"(subleaf)); } void cpu_vendor(char vendor[12]) { uint32_t ignore; - cpuid(0, &ignore, (uint32_t *)(vendor + 0), (uint32_t *)(vendor + 8), (uint32_t *)(vendor + 4)); + cpuid(0, &ignore, (uint32_t *)(vendor + 0), (uint32_t *)(vendor + 8), + (uint32_t *)(vendor + 4)); } -void cpu_name(char name[48]) { - cpuid(0x80000002, (uint32_t *)(name + 0), (uint32_t *)(name + 4), (uint32_t *)(name + 8), (uint32_t *)(name + 12)); - cpuid(0x80000003, (uint32_t *)(name + 16), (uint32_t *)(name + 20), (uint32_t *)(name + 24), (uint32_t *)(name + 28)); - cpuid(0x80000004, (uint32_t *)(name + 32), (uint32_t *)(name + 36), (uint32_t *)(name + 40), (uint32_t *)(name + 44)); +void cpu_name(char name[48]) +{ + cpuid(0x80000002, (uint32_t *)(name + 0), (uint32_t *)(name + 4), + (uint32_t *)(name + 8), (uint32_t *)(name + 12)); + cpuid(0x80000003, (uint32_t *)(name + 16), (uint32_t *)(name + 20), + (uint32_t *)(name + 24), (uint32_t *)(name + 28)); + cpuid(0x80000004, (uint32_t *)(name + 32), (uint32_t *)(name + 36), + (uint32_t *)(name + 40), (uint32_t *)(name + 44)); } void cpu_feats(struct cpu_feat *feats) @@ -156,19 +161,19 @@ void cpu_feats(struct cpu_feat *feats) cpuid(1, &ignore, &ignore, &ecx_1, &edx_1); cpuid_count(7, 0, &ignore, &ebx_7, &ignore, &ignore); - feats->fpu = edx_1 & (1<<0) ? 1 : 0; - feats->mmx = edx_1 & (1<<23) ? 1 : 0; - feats->sse = edx_1 & (1<<25) ? 1 : 0; - feats->sse2 = edx_1 & (1<<26) ? 1 : 0; - feats->sse3 = ecx_1 & (1<<0) ? 1 : 0; - feats->ssse3 = ecx_1 & (1<<9) ? 1 : 0; - feats->sse41 = ecx_1 & (1<<19) ? 1 : 0; - feats->sse42 = ecx_1 & (1<<20) ? 1 : 0; - feats->sse4a = ecx_1 & (1<<6) ? 1 : 0; - feats->avx = ecx_1 & (1<<28) ? 1 : 0; - feats->xsave = ecx_1 & (1<<26) ? 1 : 0; - feats->avx2 = ebx_7 & (1<<5) ? 1 : 0; - feats->avx512 = ebx_7 & (7<<16) ? 1 : 0; + feats->fpu = edx_1 & (1 << 0) ? 1 : 0; + feats->mmx = edx_1 & (1 << 23) ? 1 : 0; + feats->sse = edx_1 & (1 << 25) ? 1 : 0; + feats->sse2 = edx_1 & (1 << 26) ? 1 : 0; + feats->sse3 = ecx_1 & (1 << 0) ? 1 : 0; + feats->ssse3 = ecx_1 & (1 << 9) ? 1 : 0; + feats->sse41 = ecx_1 & (1 << 19) ? 1 : 0; + feats->sse42 = ecx_1 & (1 << 20) ? 1 : 0; + feats->sse4a = ecx_1 & (1 << 6) ? 1 : 0; + feats->avx = ecx_1 & (1 << 28) ? 1 : 0; + feats->xsave = ecx_1 & (1 << 26) ? 1 : 0; + feats->avx2 = ebx_7 & (1 << 5) ? 1 : 0; + feats->avx512 = ebx_7 & (7 << 16) ? 1 : 0; } void cpu_print_regs(struct cpu_regs *regs) @@ -192,42 +197,41 @@ void cpu_print_regs(struct cpu_regs *regs) kprintf("rip: %#016lx (%lu)\n", regs->rip, regs->rip); kprintf("rflags: %#016lx (%lu)\n", regs->rflags, regs->rflags); kputs("rflags: "); - if (regs->rflags & (1<<0)) + if (regs->rflags & (1 << 0)) kputs("CF "); - if (regs->rflags & (1<<2)) + if (regs->rflags & (1 << 2)) kputs("PF "); - if (regs->rflags & (1<<4)) + if (regs->rflags & (1 << 4)) kputs("AF "); - if (regs->rflags & (1<<6)) + if (regs->rflags & (1 << 6)) kputs("ZF "); - if (regs->rflags & (1<<7)) + if (regs->rflags & (1 << 7)) kputs("SF "); - if (regs->rflags & (1<<8)) + if (regs->rflags & (1 << 8)) kputs("TF "); - if (regs->rflags & (1<<9)) + if (regs->rflags & (1 << 9)) kputs("IF "); - if (regs->rflags & (1<<10)) + if (regs->rflags & (1 << 10)) kputs("DF "); - if (regs->rflags & (1<<11)) + if (regs->rflags & (1 << 11)) kputs("OF "); - if (regs->rflags & (3<<12)) + if (regs->rflags & (3 << 12)) kputs("IOPL "); - if (regs->rflags & (1<<14)) + if (regs->rflags & (1 << 14)) kputs("NT "); - if (regs->rflags & (1<<15)) + if (regs->rflags & (1 << 15)) kputs("MD "); - if (regs->rflags & (1<<16)) + if (regs->rflags & (1 << 16)) kputs("RF "); - if (regs->rflags & (1<<17)) + if (regs->rflags & (1 << 17)) kputs("VM "); - if (regs->rflags & (1<<18)) + if (regs->rflags & (1 << 18)) kputs("AC "); - if (regs->rflags & (1<<19)) + if (regs->rflags & (1 << 19)) kputs("VIF "); - if (regs->rflags & (1<<20)) + if (regs->rflags & (1 << 20)) kputs("VIP "); - if (regs->rflags & (1<<21)) + if (regs->rflags & (1 << 21)) kputs("ID "); kputs("\n"); } - diff --git a/kernel/drivers/acpi.c b/kernel/drivers/acpi.c index 41f7ba1..b4b219a 100644 --- a/kernel/drivers/acpi.c +++ b/kernel/drivers/acpi.c @@ -327,29 +327,29 @@ void acpi_report(void) { if (state.version == 0) { kprintf("ACPI 1.0\n"); - kprintf("%.*s: %#016lx\n", 4, (char *) &state.sdt.rsdt->h.signature, - (uintptr_t) state.sdt.rsdt); + kprintf("%.*s: %#016lx\n", 4, (char *)&state.sdt.rsdt->h.signature, + (uintptr_t)state.sdt.rsdt); } else { kprintf("ACPI 2.0\n"); - kprintf("%.*s: %#016lx\n", 4, (char *) &state.sdt.xsdt->h.signature, - (uintptr_t) state.sdt.xsdt); + kprintf("%.*s: %#016lx\n", 4, (char *)&state.sdt.xsdt->h.signature, + (uintptr_t)state.sdt.xsdt); } if (state.fadt) - kprintf("%.*s: %#016lx\n", 4, (char *) &state.fadt->h.signature, - (uintptr_t) state.fadt); + kprintf("%.*s: %#016lx\n", 4, (char *)&state.fadt->h.signature, + (uintptr_t)state.fadt); if (state.dsdt) - kprintf("%.*s: %#016lx\n", 4, (char *) &state.dsdt->h.signature, - (uintptr_t) state.dsdt); + kprintf("%.*s: %#016lx\n", 4, (char *)&state.dsdt->h.signature, + (uintptr_t)state.dsdt); if (state.apic) - kprintf("%.*s: %#016lx\n", 4, (char *) &state.apic->h.signature, - (uintptr_t) state.apic); + kprintf("%.*s: %#016lx\n", 4, (char *)&state.apic->h.signature, + (uintptr_t)state.apic); if (state.hept) - kprintf("%.*s: %#016lx\n", 4, (char *) &state.hept->h.signature, - (uintptr_t) state.hept); + kprintf("%.*s: %#016lx\n", 4, (char *)&state.hept->h.signature, + (uintptr_t)state.hept); if (state.waet) - kprintf("%.*s: %#016lx\n", 4, (char *) &state.waet->h.signature, - (uintptr_t) state.waet); + kprintf("%.*s: %#016lx\n", 4, (char *)&state.waet->h.signature, + (uintptr_t)state.waet); kprintf("\n"); } diff --git a/kernel/memory/physalloc.c b/kernel/memory/physalloc.c index 328502f..676e68e 100644 --- a/kernel/memory/physalloc.c +++ b/kernel/memory/physalloc.c @@ -21,9 +21,9 @@ struct memory_map phys_mmap; struct memory_segment *page_start; static const char *segment_type_str[] = { "Reserved", "Free", - "Reserved", "ACPI Reserved", - "Hibernation", "Defective", - "Unknown" }; + "Reserved", "ACPI Reserved", + "Hibernation", "Defective", + "Unknown" }; static int n_pages(const struct memory_segment *m) { -- cgit v1.2.3-freya