Skip to content

Commit

Permalink
Various adjustments to load external elf
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamos82 committed Dec 8, 2024
1 parent 58399e0 commit da09497
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 14 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ examples:
mkdir -p $(BUILD_FOLDER)/examples
${ASM_COMPILER} -g -felf64 -Fdwarf examples/example_syscall.s -o $(BUILD_FOLDER)/examples/example_syscall.o
$(X_LD) -g $(BUILD_FOLDER)/examples/example_syscall.o -o $(BUILD_FOLDER)/examples/example_syscall.elf -e loop -T examples/linker_script_$(SMALL_PAGES).ld
#ld --section-alignment 0x200000 -g $(BUILD_FOLDER)/examples/example_syscall.o -o $(BUILD_FOLDER)/examples/example_syscall.elf -e loop

debug: DEBUG=1
debug: CFLAGS += $(C_DEBUG_FLAGS)
Expand All @@ -67,7 +68,7 @@ debug: $(BUILD_FOLDER)/$(ISO_IMAGE_FILENAME)
# qemu-system-x86_64 -monitor unix:qemu-monitor-socket,server,nowait -cpu qemu64,+x2apic -cdrom $(BUILD_FOLDER)/$(ISO_IMAGE_FILENAME) -serial file:dreamos64.log -m 1G -d int -no-reboot -no-shutdown
$(QEMU_SYSTEM) -monitor unix:qemu-monitor-socket,server,nowait -cpu qemu64,+x2apic -cdrom $(BUILD_FOLDER)/$(ISO_IMAGE_FILENAME) -serial stdio -m 2G -no-reboot -no-shutdown

$(BUILD_FOLDER)/$(ISO_IMAGE_FILENAME): examples $(BUILD_FOLDER)/kernel.bin grub.cfg
$(BUILD_FOLDER)/$(ISO_IMAGE_FILENAME): $(BUILD_FOLDER)/kernel.bin grub.cfg examples
mkdir -p $(BUILD_FOLDER)/isofiles/boot/grub
cp grub.cfg $(BUILD_FOLDER)/isofiles/boot/grub
cp $(BUILD_FOLDER)/kernel.bin $(BUILD_FOLDER)/isofiles/boot
Expand Down Expand Up @@ -109,7 +110,7 @@ tests:
${TOOLCHAIN} ${TESTFLAGS} tests/test_kheap.c tests/test_common.c src/kernel/mem/kheap.c src/kernel/mem/bitmap.c src/kernel/mem/pmm.c src/kernel/mem/mmap.c src/kernel/mem/vmm_util.c -o tests/test_kheap.o
${TOOLCHAIN} ${TESTFLAGS} tests/test_vm.c tests/test_common.c src/kernel/arch/x86_64/system/vm.c src/kernel/mem/vmm_util.c -o tests/test_vm.o
${TOOLCHAIN} ${TESTFLAGS} tests/test_vfs.c tests/test_common.c src/fs/vfs.c src/drivers/fs/ustar.c -o tests/test_vfs.o
${TOOLCHAIN} ${TESTFLAGS} tests/test_utils.c src/kernel/mem/vmm_util.c -o tests/test_utils.o
${TOOLCHAIN} ${TESTFLAGS} tests/test_utils.c tests/test_common.c src/kernel/mem/vmm_util.c -o tests/test_utils.o
${TOOLCHAIN} ${TESTFLAGS} tests/test_window.c tests/test_common.c src/kernel/graphics/window.c -o tests/test_window.o
./tests/test_mem.o && ./tests/test_kheap.o && ./tests/test_number_conversion.o && ./tests/test_vm.o && ./tests/test_vfs.o && ./tests/test_utils.o && ./tests/test_window.o

Expand Down
2 changes: 1 addition & 1 deletion docs/kernel/Syscalls.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The syscalls are called using the interrupt vector `0x80`. Arguments depend on t

# Syscalls List

## 0x01 TEST
## 0x00 TEST

The first syscall is reserved for test purpose, and it should be never used.

Expand Down
1 change: 1 addition & 0 deletions src/include/kernel/mem/vmm_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
size_t get_number_of_pages_from_size(size_t size);
size_t align_value_to_page(size_t value);
size_t align_up(size_t value, size_t alignment);
size_t align_down(size_t value, size_t alignment);
bool is_address_aligned(size_t value, size_t alignment);

size_t vm_parse_flags( size_t flags );
Expand Down
4 changes: 2 additions & 2 deletions src/kernel/arch/x86_64/mem/vmm_mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void *map_phys_to_virt_addr_hh(void* physical_address, void* address, size_t fla
uint8_t user_mode_status = 0;

if ( !is_address_higher_half((uint64_t) address) ) {
pretty_log(Verbose, "address is in lower half");
pretty_logf(Verbose, "address is in lower half: 0x%x", address);
flags = flags | VMM_FLAGS_USER_LEVEL;
user_mode_status = VMM_FLAGS_USER_LEVEL;
}
Expand Down Expand Up @@ -84,7 +84,7 @@ void *map_phys_to_virt_addr_hh(void* physical_address, void* address, size_t fla
}
#elif SMALL_PAGES == 0
pd_root[pd_e] = (uint64_t) (physical_address) | HUGEPAGE_BIT | flags | user_mode_status;
pretty_logf(Verbose, " PD Flags: 0x%x entry value pd_root[0x%x]: 0x%x", flags, pd_e, pd_root[pd_e]);
pretty_logf(Verbose, " PD Flags: 0x%x entry value pd_root[0x%x]: 0x%x - address: 0x%x", flags, pd_e, pd_root[pd_e], address);
return address;
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/loaders/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void load_elf(uintptr_t elf_start, uint64_t size) {
Elf64_Phdr *cur_phdr = read_phdr(elf_header, 0);
pretty_logf(Verbose, "\t[cur_phdr]: Type: 0x%x, Flags: 0x%x - Vaddr: 0x%x - aligned: 0x%x - p_align: 0x%x - p_memsz: 0%x - p_offset: 0x%x", cur_phdr->p_type, cur_phdr->p_flags, cur_phdr->p_vaddr, align_value_to_page(cur_phdr->p_vaddr), cur_phdr->p_align, cur_phdr->p_memsz, cur_phdr->p_offset);
cur_phdr = read_phdr(elf_header, 1);
pretty_logf(Verbose, "\t[cur_phdr]: Type: 0x%x, Flags: 0x%x - Vaddr: 0x%x - aligned: 0x%x - p_align: 0x%x - p_memsz: 0%x - p_offset: 0x%x", cur_phdr->p_type, cur_phdr->p_flags, cur_phdr->p_vaddr, align_value_to_page(cur_phdr->p_vaddr), cur_phdr->p_align, cur_phdr->p_memsz, cur_phdr->p_offset);
//pretty_logf(Verbose, "\t[cur_phdr]: Type: 0x%x, Flags: 0x%x - Vaddr: 0x%x - aligned: 0x%x - p_align: 0x%x - p_memsz: 0%x - p_offset: 0x%x", cur_phdr->p_type, cur_phdr->p_flags, cur_phdr->p_vaddr, align_value_to_page(cur_phdr->p_vaddr), cur_phdr->p_align, cur_phdr->p_memsz, cur_phdr->p_offset);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,11 @@ void kernel_start(unsigned long addr, unsigned long magic){
char a = 'a';
task_t* idle_task = create_task_from_func("idle", idle, &a, true);
idle_thread = idle_task->threads;
//task_t* userspace_task = create_task_from_func("userspace_idle", NULL, &a, false);
#if PAGE_SIZE_IN_BYTES == 0x200000
task_t* userspace_task = create_task_from_func("userspace_idle", NULL, &a, false);
#else
task_t* elf_task = create_task_from_elf("elf_idle", NULL, (Elf64_Ehdr *) (uintptr_t) hhdm_get_variable(elf_module_start_phys));
#endif
//create_thread("ledi", noop2, &c, eldi_task);
//create_task("sleeper", noop3, &d);
//execute_runtime_tests();
Expand Down
4 changes: 4 additions & 0 deletions src/kernel/mem/vmm_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ size_t align_up(size_t value, size_t alignment) {
return ((value + alignment - 1) / alignment) * alignment;
}

size_t align_down(size_t value, size_t alignment) {
return (value / alignment) * alignment;
}

bool is_address_aligned(size_t value, size_t alignment) {
if (value % alignment == 0) {
return true;
Expand Down
4 changes: 4 additions & 0 deletions src/kernel/scheduling/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ task_t *create_task_from_elf(char *name, void *args, Elf64_Ehdr *elf_header){
uint64_t vaddr_address = align_value_to_page(phdr.p_vaddr);
for (int j = 0; j < mem_pages; j++) {
pretty_logf(Verbose, "[%d]: Mapping: offset: 0x%x (virtual: 0x%x) in vaddr: 0x%x", j, hhdm_get_phys_address((uintptr_t) offset_address), offset_address, vaddr_address);
if ( !is_address_aligned((size_t) hhdm_get_phys_address(offset_address), PAGE_SIZE_IN_BYTES)) {
pretty_log(Fatal, "Error: module elf phys address is not page aligned");
}

map_phys_to_virt_addr_hh(hhdm_get_phys_address(offset_address), (void *) vaddr_address, VMM_FLAGS_USER_LEVEL | vmm_hdr_flags | VMM_FLAGS_PRESENT, (uint64_t *) new_task->vmm_data.root_table_hhdm);
//I need a mem copy. I need to fopy the content of elf_header + phdr.p_offset into phdr.p_vaddr
offset_address += (uint64_t) phdr.p_align;
Expand Down
3 changes: 1 addition & 2 deletions src/kernel/scheduling/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ thread_t* create_thread(char* thread_name, void (*_entry_point)(void *), void* a
// This piece of code is temporary, just to test a userspace task, it run just an infinite loop.
pretty_logf(Verbose, "vmm_data address: 0x%x", &(parent_task->vmm_data));
if (is_elf ) {
pretty_logf(Verbose, "Preparing to launch an ELF. entry_pint = 0x%x", (uint64_t) _entry_point);
pretty_logf(Verbose, "Preparing to launch an ELF. entry_point = 0x%x", (uint64_t) _entry_point);
new_thread->execution_frame->rip = (uint64_t)_entry_point;
} else {
new_thread->execution_frame->rip = prepare_userspace_function(&(parent_task->vmm_data));
Expand Down Expand Up @@ -75,7 +75,6 @@ thread_t* create_thread(char* thread_name, void (*_entry_point)(void *), void* a
while(1);
}
// We need to allocate a new stack for each thread
//void* stack_pointer = kmalloc(THREAD_DEFAULT_STACK_SIZE);
void* stack_pointer = vmm_alloc(THREAD_DEFAULT_STACK_SIZE, VMM_FLAGS_PRESENT | VMM_FLAGS_WRITE_ENABLE | VMM_FLAGS_STACK, &(parent_task->vmm_data));
if (stack_pointer == NULL) {
pretty_log(Fatal, "rsp is null - PANIC!");
Expand Down
17 changes: 12 additions & 5 deletions tests/test_utils.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
#include <test_common.h>
#include <vmm_util.h>
#include <assert.h>
#include <stdio.h>

void test_utils();

int main() {
printf("Testing VMM Utility function -\n");
test_utils();
}

void test_utils() {
size_t number_of_pages = get_number_of_pages_from_size(0x900);
printf("\t [test_utils] (get_number_of_pages_from_size): Testing number of pages for 0x900, should be 1: %ld\n", number_of_pages);
printf("\t[test_utils] (get_number_of_pages_from_size): Testing number of pages for 0x900, should be 1: %ld\n", number_of_pages);
assert(number_of_pages == 1);
number_of_pages = get_number_of_pages_from_size(0x0);
printf("\t [test_utils] (get_number_of_pages_from_size): Testing number of pages for 0x0, should be 0: %ld\n", number_of_pages);
printf("\t[test_utils] (get_number_of_pages_from_size): Testing number of pages for 0x0, should be 0: %ld\n", number_of_pages);
assert(align_value_to_page(0x100) == 0x200000);
printf("\t [test_utils] (align_value_to_page): Testing alignment for 0x100, should be 0x200000: %lx\n", align_value_to_page(0x100));
printf("\t[test_utils] (align_value_to_page): Testing alignment for 0x100, should be 0x200000: %lx\n", align_value_to_page(0x100));
assert(align_value_to_page(0x200015) == 0x400000);
printf("\t [test_utils] (align_value_to_page): Testing alignment for 0x100, should be 0x200015: %lx\n", align_value_to_page(0x200015));

pretty_assert(0x200000, align_down(0x3c7000, 0x200000), ==, "Testing align_down");
pretty_assert(0x600000, align_down(0x6c7000, 0x200000), ==, "Testing align_down");
}

0 comments on commit da09497

Please sign in to comment.