diff options
author | Freya Murphy <freya@freyacat.org> | 2025-05-06 12:36:04 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2025-05-06 12:36:04 -0400 |
commit | b54fdd17a99a7feffc36a3ad067d4293ba3e9577 (patch) | |
tree | dea876cfb4607b6a7b7be76fa1d88fa39f374e5a /kernel/user.c | |
parent | terrible hack to not overwrite mboot data (diff) | |
download | comus-b54fdd17a99a7feffc36a3ad067d4293ba3e9577.tar.gz comus-b54fdd17a99a7feffc36a3ad067d4293ba3e9577.tar.bz2 comus-b54fdd17a99a7feffc36a3ad067d4293ba3e9577.zip |
add some memory logging
Diffstat (limited to 'kernel/user.c')
-rw-r--r-- | kernel/user.c | 62 |
1 files changed, 43 insertions, 19 deletions
diff --git a/kernel/user.c b/kernel/user.c index 9612710..97d8f0f 100644 --- a/kernel/user.c +++ b/kernel/user.c @@ -1,3 +1,4 @@ +#include "lib/kio.h" #include <comus/fs.h> #include <comus/procs.h> #include <comus/memory.h> @@ -14,7 +15,7 @@ #define USER_STACK_LEN (4 * PAGE_SIZE) #define BLOCK_SIZE (PAGE_SIZE * 1000) -static char *load_buffer = NULL; +static uint8_t *load_buffer = NULL; #define USER_CODE 0x18 #define USER_DATA 0x20 @@ -49,16 +50,22 @@ static int user_load_segment(struct pcb *pcb, struct file *file, int idx) // allocate memory in user process if (mem_alloc_pages_at(pcb->memctx, mem_pages, (void *)hdr.p_vaddr, - F_WRITEABLE | F_UNPRIVILEGED) == NULL) + F_WRITEABLE | F_UNPRIVILEGED) == NULL) { + ERROR("Could not allocate memory for elf segment"); return 1; + } mapADDR = kmapuseraddr(pcb->memctx, (void *)hdr.p_vaddr, mem_bytes); - if (mapADDR == NULL) + if (mapADDR == NULL) { + ERROR("Could load memory for elf segment"); return 1; + } // seek to start of segment - if (file->seek(file, hdr.p_offset, SEEK_SET) < 0) + if (file->seek(file, hdr.p_offset, SEEK_SET) < 0) { + ERROR("Could not load elf segment"); return 1; + } // load data size_t total_read = 0; @@ -66,11 +73,13 @@ static int user_load_segment(struct pcb *pcb, struct file *file, int idx) size_t read = BLOCK_SIZE; if (read > file_bytes - total_read) read = file_bytes - total_read; + TRACE("Reading %zu bytes...", read); if ((read = file->read(file, load_buffer, read)) < 1) { kunmapaddr(mapADDR); + ERROR("Could not load elf segment"); return 1; } - memcpy(mapADDR + total_read, load_buffer, read); + memcpyv(mapADDR + total_read, load_buffer, read); total_read += read; } @@ -89,10 +98,15 @@ static int user_load_segments(struct pcb *pcb, struct file *file) pcb->heap_start = NULL; pcb->heap_len = 0; - if (load_buffer == NULL) - if ((load_buffer = kalloc(BLOCK_SIZE)) == NULL) + if (load_buffer == NULL) { + load_buffer = kalloc(BLOCK_SIZE); + if (load_buffer == NULL) { + ERROR("Could not allocate user load buffer"); return 1; + } + } + TRACE("Loading %u elf segments", pcb->n_elf_segments); for (int i = 0; i < pcb->n_elf_segments; i++) if ((ret = user_load_segment(pcb, file, i))) return ret; @@ -110,12 +124,12 @@ static int validate_elf_hdr(struct pcb *pcb) Elf64_Ehdr *ehdr = &pcb->elf_header; if (strncmp((const char *)ehdr->e_ident, ELFMAG, SELFMAG)) { - WARN("Invalid ELF File."); + ERROR("Invalid ELF File."); return 1; } if (ehdr->e_ident[EI_CLASS] != ELFCLASS64) { - WARN("Unsupported ELF Class."); + ERROR("Unsupported ELF Class."); return 1; } @@ -125,17 +139,17 @@ static int validate_elf_hdr(struct pcb *pcb) } if (ehdr->e_machine != EM_X86_64) { - WARN("Unsupported ELF File target."); + ERROR("Unsupported ELF File target."); return 1; } if (ehdr->e_ident[EI_VERSION] != EV_CURRENT) { - WARN("Unsupported ELF File version."); + ERROR("Unsupported ELF File version."); return 1; } if (ehdr->e_phnum > N_ELF_SEGMENTS) { - WARN("Too many ELF segments."); + ERROR("Too many ELF segments."); return 1; } @@ -151,22 +165,30 @@ static int user_load_elf(struct pcb *pcb, struct file *file) { int ret = 0; - if (file->seek(file, 0, SEEK_SET) < 0) + if (file->seek(file, 0, SEEK_SET) < 0) { + ERROR("Cannot read ELF header."); return 1; - ret = file->read(file, (char *)&pcb->elf_header, sizeof(Elf64_Ehdr)); - if (ret < 0) + } + ret = file->read(file, &pcb->elf_header, sizeof(Elf64_Ehdr)); + if (ret < 0) { + ERROR("Cannot read ELF header."); return 1; + } if (validate_elf_hdr(pcb)) return 1; pcb->n_elf_segments = pcb->elf_header.e_phnum; - if (file->seek(file, pcb->elf_header.e_phoff, SEEK_SET) < 0) + if (file->seek(file, pcb->elf_header.e_phoff, SEEK_SET) < 0) { + ERROR("Cannot read ELF segemts"); return 1; - ret = file->read(file, (char *)&pcb->elf_segments, + } + ret = file->read(file, &pcb->elf_segments, sizeof(Elf64_Phdr) * pcb->elf_header.e_phnum); - if (ret < 0) + if (ret < 0) { + ERROR("Cannot read ELF segemts"); return 1; + } return 0; } @@ -217,8 +239,10 @@ static int user_setup_stack(struct pcb *pcb, const char **args, /* stack */ if (mem_alloc_pages_at(pcb->memctx, USER_STACK_LEN / PAGE_SIZE, (void *)(USER_STACK_TOP - USER_STACK_LEN), - F_WRITEABLE | F_UNPRIVILEGED) == NULL) + F_WRITEABLE | F_UNPRIVILEGED) == NULL) { + ERROR("Could not allocate user stack"); return 1; + } /* regs */ memset(&pcb->regs, 0, sizeof(struct cpu_regs)); |