From 114468a6ada19f7b5719fffaa2f3ed71f9e9511c Mon Sep 17 00:00:00 2001 From: rami Date: Thu, 3 Oct 2024 22:13:13 -0400 Subject: [PATCH] parse program header of elf file and use load sections --- Makefile | 2 +- boot/initrd | Bin 1512 -> 1508 bytes hdd.img | Bin 0 -> 2048 bytes include/kernel/elf.h | 24 ++++++++++++++++++++++++ include/kernel/ide.h | 6 ++++++ include/kernel/kernel.h | 4 ++-- kernel/ide.c | 5 +++++ kernel/task.c | 18 ++++++++++++------ user/shell.c | 5 ++++- 9 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 hdd.img create mode 100644 include/kernel/ide.h create mode 100644 kernel/ide.c diff --git a/Makefile b/Makefile index e17f7a8..396cbb6 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ ISO := Hazel.iso CFLAGS := -ffreestanding -Wall -Wextra -Werror -I $(INCLUDEDIR) -I lib LDFLAGS := -ffreestanding -nostdlib -lgcc -T kernel/kernel.ld -QEMUFLAGS := -cdrom $(BUILDDIR)/$(ISO) -s -m 512M -serial stdio -device virtio-gpu +QEMUFLAGS := -cdrom $(BUILDDIR)/$(ISO) -s -m 512M -serial stdio -device virtio-gpu $(BUILDDIR)/$(KIMG): $(KOBJ) $(LOBJ) $(CC) $^ -o $@ $(LDFLAGS) diff --git a/boot/initrd b/boot/initrd index 0430873b8473f2cedbe31a8ff409f0eeb878982f..2c9cf00da0020428d05e47361ba05205941dcf1d 100755 GIT binary patch delta 438 zcmaFC{e*jh0^^y9ips23%nS@x8$F*hdjD=>;drs?-~a#3M_4i#7#SFPTR#AK0a*+$ z4ueH`GX4RTKt*4C{0maa(R!eiKZC6qs5Kv=_4T$4iOJPWa*XnmXEB**tAV6=z)}J) z1O5Z`yy3~v0118v8zAt~8Yt+Inv;`1S%le_)mYC+&tP&ovugbppk?d~3_zDKG4KKD z#|qts&rk$u&4*}xy)DCday642qt)bDOeSuQASoWOl)%e? z|3E!&cru(pg5SXg2)wig3WlfVB2muP%Lj|0H zG_nRSAU^`h0RPFlEE1EwSe8%z!eYaiIN6X@nK5*7D62Ye1~Bj+09CUBG1KHuR%uob X76yi}$$`wmllQO+u+9VWj6gg90)BZ# diff --git a/hdd.img b/hdd.img new file mode 100644 index 0000000000000000000000000000000000000000..e9784eb4c8849062d374dd2058af8814b024f3bd GIT binary patch literal 2048 ccmZQz7zLvtFd71*Aut*OqaiRF0wXO100;m80RR91 literal 0 HcmV?d00001 diff --git a/include/kernel/elf.h b/include/kernel/elf.h index b8b21a7..23572d5 100644 --- a/include/kernel/elf.h +++ b/include/kernel/elf.h @@ -41,6 +41,30 @@ typedef struct { Elf32_Word sh_entsize; } section_t; +typedef struct { + Elf32_Word p_type; + Elf32_Off p_offset; + Elf32_Addr p_vaddr; + Elf32_Addr p_paddr; + Elf32_Word p_filesz; + Elf32_Word p_memsz; + Elf32_Word p_flags; + Elf32_Word p_align; +} phdr_t; + +#define PT_NULL 0 +#define PT_LOAD 1 +#define PT_DYNAMIC 2 +#define PT_INTERP 3 +#define PT_NOTE 4 +#define PT_SHLIB 5 +#define PT_PHDR 6 +#define PT_TLS 7 +#define PT_LOOS 0x60000000 +#define PT_HIOS 0x6fffffff +#define PT_LOPROC 0x70000000 +#define PT_HIPROC 0x7fffffff + section_t *elf_find_section(elf_t *elf, const char *name); #endif diff --git a/include/kernel/ide.h b/include/kernel/ide.h new file mode 100644 index 0000000..43896ae --- /dev/null +++ b/include/kernel/ide.h @@ -0,0 +1,6 @@ +#ifndef HAZEL_IDE_H_ +#define HAZEL_IDE_H_ + +void ide_init(); + +#endif diff --git a/include/kernel/kernel.h b/include/kernel/kernel.h index 4ee59c4..0ce2001 100644 --- a/include/kernel/kernel.h +++ b/include/kernel/kernel.h @@ -22,8 +22,8 @@ typedef struct { uint32_t *mmap; uint32_t mmap_size; log_method_t log_method; - uint64_t ticks; - task_t *current_task; + volatile uint64_t ticks; + volatile task_t *current_task; rsdt_t *rsdt; } kernel_ctx_t; diff --git a/kernel/ide.c b/kernel/ide.c new file mode 100644 index 0000000..2522373 --- /dev/null +++ b/kernel/ide.c @@ -0,0 +1,5 @@ +#include + +void ide_init() { + +} diff --git a/kernel/task.c b/kernel/task.c index b364bd3..c4b1954 100644 --- a/kernel/task.c +++ b/kernel/task.c @@ -7,10 +7,8 @@ extern kernel_ctx_t ctx; //int task_init(task_t *task, uint32_t *img) { int task_init(task_t *task, elf_t *elf) { - if (!elf) return 0; - uint8_t *s = (uint8_t *)mmap_find_first_free_block(); - memcpy(s, elf, 0x1000); + if (!elf->e_phoff) return 0; task->eip = elf->e_entry; // Allocate a page for a stack @@ -31,9 +29,17 @@ int task_init(task_t *task, elf_t *elf) { // Map remaining entries as non-present u/s else page_dir[i] = 6; } - // Map the ELF image to its specified entry - page_dir[PAGE_DIR_INDEX(elf->e_entry)] = ((uint32_t)page_tab - KERNEL_VMA) | 7; - page_tab[PAGE_TAB_INDEX(elf->e_entry)] = ((uint32_t)s - KERNEL_VMA) | 7; + + phdr_t *phdr = (phdr_t *)((uint32_t)elf + elf->e_phoff); + for (int i = 0; i < elf->e_phnum; i++) { + if (phdr[i].p_type == PT_LOAD) { + uint8_t *s = (uint8_t *)mmap_find_first_free_block(); + memcpy(s, (uint8_t *)elf + phdr[i].p_offset, phdr[i].p_filesz); + // Map the ELF image to its specified entry + page_dir[PAGE_DIR_INDEX(phdr[i].p_vaddr)] = ((uint32_t)page_tab - KERNEL_VMA) | 7; + page_tab[PAGE_TAB_INDEX(phdr[i].p_vaddr)] = ((uint32_t)s - KERNEL_VMA) | 7; + } + } return 1; } diff --git a/user/shell.c b/user/shell.c index 9ca0db6..828d9dc 100644 --- a/user/shell.c +++ b/user/shell.c @@ -1,5 +1,7 @@ #include "../lib/strcmp.h" +static const char test[] = "Hello\n"; + int strlen(const char *str) { const char *s; for (s = str; *s; s++) @@ -86,7 +88,8 @@ void print_int(int num){ void _start() { char buf1[256] = {0}; - puts("Welcome to the Hazel user shell!\n"); + //puts("Welcome to the Hazel user shell!\n"); + puts(test); while (1) { puts("$ "); read(0, buf1, 256);