diff --git a/docs/kernel/Initialization.md b/docs/kernel/Initialization.md index 6d91b27..14460a6 100644 --- a/docs/kernel/Initialization.md +++ b/docs/kernel/Initialization.md @@ -2,7 +2,7 @@ ## Loading the kernel -The kernel is loaded by grub2, using the multibtoo2 specifications. +The kernel is loaded by grub2, using the multiboot2 specifications. The tags that are enabled at boot are: diff --git a/src/include/kernel/mem/vmm.h b/src/include/kernel/mem/vmm.h index b259648..d7cdc95 100644 --- a/src/include/kernel/mem/vmm.h +++ b/src/include/kernel/mem/vmm.h @@ -27,7 +27,8 @@ typedef enum { VMM_FLAGS_PRESENT = (1 << 0), VMM_FLAGS_WRITE_ENABLE = (1 << 1), VMM_FLAGS_USER_LEVEL = (1 << 2), - VMM_FLAGS_ADDRESS_ONLY = (1 << 7) + VMM_FLAGS_ADDRESS_ONLY = (1 << 7), + VMM_FLAGS_STACK = (1 << 8) } paging_flags_t; typedef enum { @@ -86,6 +87,7 @@ uint8_t check_virt_address_status(uint64_t virtual_address); void vmm_direct_map_physical_memory(); bool is_address_only(size_t flags); +bool is_address_stack(size_t flags); void *vmm_get_variable_from_direct_map ( size_t phys_address ); diff --git a/src/kernel/arch/x86_64/cpu/tss.c b/src/kernel/arch/x86_64/cpu/tss.c index 7741728..fc6dbe7 100644 --- a/src/kernel/arch/x86_64/cpu/tss.c +++ b/src/kernel/arch/x86_64/cpu/tss.c @@ -66,7 +66,5 @@ void load_tss() { gdt64[TSS_ENTRY_HIGH] = entry_high; loglinef(Verbose, "(%s) Loading TSS Register", __FUNCTION__); loglinef(Verbose, "(%s) kernel_tss address = 0x%x", __FUNCTION__, &kernel_tss); - loglinef(Verbose, "(%s) gdt64[4] = 0x%x", __FUNCTION__, (uint64_t)gdt64[TSS_ENTRY_LOW]); - loglinef(Verbose, "(%s) gdt64[5] = 0x%x", __FUNCTION__, (uint64_t)gdt64[TSS_ENTRY_HIGH]); _load_task_register(); } diff --git a/src/kernel/main.c b/src/kernel/main.c index a64ec62..7a815a1 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -189,18 +189,16 @@ void kernel_start(unsigned long addr, unsigned long magic){ loglinef(Verbose, "(kernel_main) (END of Mapped memory: 0x%x)", end_of_mapped_memory); vfs_init(); uint64_t unix_timestamp = read_rtc_time(); + #if USE_FRAMEBUFFER == 1 _fb_printStrAndNumber("Epoch time: ", unix_timestamp, 0, 5, 0xf5c4f1, 0x000000); #endif init_scheduler(); char a = 'a'; - char b = 'b'; - char c = 'c'; - char d = 'd'; task_t* idle_task = create_task("idle", noop, &a, true); idle_thread = idle_task->threads; //task_t* eldi_task = create_task("eldi", noop2, &b); - task_t* userspace_task = create_task("userspace_idle", NULL, &b, false); + task_t* userspace_task = create_task("userspace_idle", NULL, &a, false); //create_thread("ledi", noop2, &c, eldi_task); //create_task("sleeper", noop3, &d); //print_thread_list(eldi_task->task_id); @@ -208,16 +206,8 @@ void kernel_start(unsigned long addr, unsigned long magic){ start_apic_timer(kernel_settings.apic_timer.timer_ticks_base, APIC_TIMER_SET_PERIODIC, kernel_settings.apic_timer.timer_divisor); loglinef(Verbose, "(kernel_main) (END of Mapped memory: 0x%x)", end_of_mapped_memory); loglinef(Verbose, "(kernel_main) init_basic_system: Memory lower (in kb): %d - upper (in kb): %d", tagmem->mem_lower, tagmem->mem_upper); - // Testing that the hhdm is actually working struct multiboot_tag_basic_meminfo *virt_phys_addr = (struct multiboot_tag_basic_meminfo *) hhdm_get_variable( (size_t) multiboot_basic_meminfo ); - loglinef(Verbose, "(kernel_main) init_basic_system: Memory lower (in kb): %d - upper (in kb): %d", virt_phys_addr->mem_lower, virt_phys_addr->mem_upper); logline(Info, "(kernel_main) Init end!! Starting infinite loop"); - //prepare_userspace_function(); - //uint64_t* vm_root_vaddress = (uint64_t *) vmm_alloc(PAGE_SIZE_IN_BYTES, VMM_FLAGS_ADDRESS_ONLY, NULL); - //void* temp_var = pmm_alloc_frame(); - //map_phys_to_virt_addr_hh(temp_var, (void *) vm_root_vaddress, VMM_FLAGS_PRESENT | VMM_FLAGS_WRITE_ENABLE, NULL); - //vm_root_vaddress[0] = 5; - //loglinef(Verbose, "(%s): vm_root_vaddress value: 0x%x", __FUNCTION__, vm_root_vaddress[0]); - //map_phys_to_hh(); + while(1); } diff --git a/src/kernel/mem/vmm.c b/src/kernel/mem/vmm.c index 0b8465a..204e2c1 100644 --- a/src/kernel/mem/vmm.c +++ b/src/kernel/mem/vmm.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -157,11 +158,23 @@ void *vmm_alloc(size_t size, size_t flags, VmmInfo *vmm_info) { loglinef(Verbose, "(vmm_alloc) newly allocated item base: %x, next available address: %x", vmm_info->status.vmm_cur_container->vmm_root[vmm_info->status.vmm_cur_index].base, vmm_info->status.next_available_address); vmm_info->status.vmm_cur_index++; + if ( is_address_stack(flags) ) { + loglinef(Verbose, "(%s): The address will be a stack", __FUNCTION__); + return (void *) address_to_return + THREAD_DEFAULT_STACK_SIZE; + } + return (void *) address_to_return; } bool is_address_only(size_t flags) { - if(flags & VMM_FLAGS_ADDRESS_ONLY) { + if ( flags & VMM_FLAGS_ADDRESS_ONLY ) { + return true; + } + return false; +} + +bool is_address_stack(size_t flags) { + if ( flags & VMM_FLAGS_STACK ) { return true; } return false; diff --git a/src/kernel/scheduling/thread.c b/src/kernel/scheduling/thread.c index 9493536..2f5c5e2 100644 --- a/src/kernel/scheduling/thread.c +++ b/src/kernel/scheduling/thread.c @@ -64,14 +64,14 @@ thread_t* create_thread(char* thread_name, void (*_entry_point)(void *), void* a } // 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, &(parent_task->vmm_data)); + 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) { loglinef(Fatal, "(create_thread): rsp is null - PANIC!"); while(1); } // The stack grow backward, so the pointer will be the end of the stack - new_thread->stack = (uintptr_t)stack_pointer + THREAD_DEFAULT_STACK_SIZE; + new_thread->stack = (uintptr_t)stack_pointer; new_thread->execution_frame->rsp = (uint64_t) new_thread->stack; new_thread->execution_frame->rbp = 0; loglinef(Verbose, "(%s): thread: %s stack address returned: 0x%x", __FUNCTION__, new_thread->thread_name, new_thread->execution_frame->rsp); diff --git a/src/userpsace/test_userspace.c b/src/userpsace/test_userspace.c index cc08a12..0f3f967 100644 --- a/src/userpsace/test_userspace.c +++ b/src/userpsace/test_userspace.c @@ -21,7 +21,5 @@ uint64_t prepare_userspace_function(VmmInfo *vmm_info) { code_page[1] = infinite_loop[1]; char *user_code_page = vmm_alloc(PAGE_SIZE_IN_BYTES, VMM_FLAGS_ADDRESS_ONLY | VMM_FLAGS_WRITE_ENABLE | VMM_FLAGS_PRESENT | VMM_FLAGS_USER_LEVEL, vmm_info); map_phys_to_virt_addr_hh(temp_var, (void *) user_code_page,VMM_FLAGS_USER_LEVEL | VMM_FLAGS_PRESENT | VMM_FLAGS_WRITE_ENABLE, (uint64_t *) vmm_info->root_table_hhdm); - //loglinef(Verbose, "(%s): user_code_page[0]: 0x%x - user_code_page[1]: 0x%x", __FUNCTION__, user_code_page[0], user_code_page[1]); - loglinef(Verbose, "(%s): leaving prparing userspace function", __FUNCTION__); return (uint64_t) user_code_page; }