diff --git a/Makefile b/Makefile index 475b07e..cc9589c 100644 --- a/Makefile +++ b/Makefile @@ -198,6 +198,21 @@ compile_test: all test: compile_test ./testing_libft +help: + @printf "\033[32m -\033[0m all: Compile iso\n" + @printf "\033[32m -\033[0m clean: clean .o files\n" + @printf "\033[32m -\033[0m fclean: clean .o files & delete iso\n" + @printf "\033[32m -\033[0m re: launch clean & all\n" + @printf "\033[32m -\033[0m run: build & run the project into qemu\n" + @printf "\033[32m -\033[0m run_max_memory: build & run the project into qemu with 4GB of ram\n" + @printf "\033[32m -\033[0m run_debug: build & run the project into qemu with debug flags\n" + @printf "\033[32m -\033[0m run_debug_gdb: build & run the project into qemu with debug flags and gdb\n" + @printf "\033[32m -\033[0m relaunch: launch fclean & run\n" + @printf "\033[32m -\033[0m check: launch checks based on logs\n" + @printf "\033[32m -\033[0m compile_tests: compile tests\n" + @printf "\033[32m -\033[0m test: launch tests\n" + @printf "\033[32m -\033[0m help: print this help\n" + .SILENT: .PHONY: all re fclean clean diff --git a/srcs/kernel/gdt/gdt.c b/srcs/kernel/gdt/gdt.c index 393ef89..f911e54 100644 --- a/srcs/kernel/gdt/gdt.c +++ b/srcs/kernel/gdt/gdt.c @@ -16,7 +16,8 @@ GDTEntry_t gdt_entries[7]; -/* Set the value of one GDT entry. +/** + * Set the value of one GDT entry. * @param num the entry we fill (between 0 and 4) */ static void gdt_set_gate(uint32_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran) @@ -40,8 +41,11 @@ void init_gdt() { GDTDescriptor_t gdt_ptr; + // Disable interruptions + asm volatile ("cli"); + // Size is 5 because we have 5 entries - gdt_ptr.size = (sizeof(GDTEntry_t) * 5) - 1; + gdt_ptr.size = (sizeof(GDTEntry_t) * 7) - 1; // Offset if the adress of the first entry; gdt_ptr.offset = (uint32_t)GDT_BASE; @@ -55,7 +59,7 @@ void init_gdt() gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); // Kernel Data segment // 0x96 -> 1001 0110 in binary: -> Ring 0, Data segment - // For more info about how acces byte is defined, have a look here: + // For more info about how access byte is defined, have a look here: // https://wiki.osdev.org/Global_Descriptor_Table gdt_set_gate(3, 0, 0xFFFFFFFF, 0x96, 0xCF); // Kernel stack segment @@ -68,10 +72,12 @@ void init_gdt() // 0xF6 -> 1111 0110 in binary: -> Ring 3, Data segment gdt_set_gate(6, 0, 0xFFFFFFFF, 0xF6, 0xCF); // User stack segment - memcpy((char *)gdt_ptr.offset, (char *)gdt_entries, sizeof(GDTEntry_t) * 5); + memcpy((char *)gdt_ptr.offset, (char *)gdt_entries, sizeof(GDTEntry_t) * 7); // Load new created GDT gdt_flush((uint32_t)&gdt_ptr); - printk(KERN_INFO, "GDT has been initialised at %p", gdt_ptr.offset); + // Re-enable interruptions + asm volatile ("sti"); + printk(KERN_INFO, "GDT has been initialised at %p with %d entries", gdt_ptr.offset, gdt_ptr.size); } diff --git a/srcs/kernel/kernel.c b/srcs/kernel/kernel.c index 3a22602..9b8f396 100644 --- a/srcs/kernel/kernel.c +++ b/srcs/kernel/kernel.c @@ -58,10 +58,11 @@ int test_function1() { //int i = 0; //while (i != 10) { - terminal_writestr("Hello from function test 1"); + //terminal_writestr("Hello from function test 1"); //sleep(2); // i++; //} + test_syscalls(); return 0; } @@ -121,7 +122,7 @@ void k_main(unsigned long magic, unsigned long addr) { //init_shell(); // Voluntary Page fault, do not uncomment - /*uint32_t *ptr = (uint32_t*)0xA0000000; + /*uint32_t *ptr = (uint32_t*)0xA0000000;0x001012d8 uint32_t do_page_fault = *ptr; (void)do_page_fault; asm volatile ("int $0xE");*/ diff --git a/srcs/kernel/memory/vmm/vmm.c b/srcs/kernel/memory/vmm/vmm.c index e420d33..9b3be91 100644 --- a/srcs/kernel/memory/vmm/vmm.c +++ b/srcs/kernel/memory/vmm/vmm.c @@ -14,14 +14,9 @@ #include "../pmm/pmm.h" void munmap(void *ptr, unsigned int size) { - (void)ptr; - (void)size; pmm_unset_pages(ptr, size); - return ; } void *mmap(unsigned int size, int flags) { - (void)size; - (void)flags; return pmm_next_fit(size, flags); } diff --git a/srcs/kernel/processes/processes.c b/srcs/kernel/processes/processes.c index 4354be7..3b51ac3 100644 --- a/srcs/kernel/processes/processes.c +++ b/srcs/kernel/processes/processes.c @@ -65,6 +65,22 @@ t_process *find_process_by_pid(unsigned long pid) { return NULL; } +void show_process_registers(t_process *process) { + printd(KERN_INFO, "---- Displaying registers for: %s (%d) -----", process->name, process->pid); + printd(KERN_INFO, "- eax: %p", process->regs.eax); + printd(KERN_INFO, "- ecx: %p", process->regs.ecx); + printd(KERN_INFO, "- edx: %p", process->regs.edx); + printd(KERN_INFO, "- ebx: %p", process->regs.ebx); + printd(KERN_INFO, "- esp: %p", process->regs.esp); + printd(KERN_INFO, "- ebp: %p", process->regs.ebp); + printd(KERN_INFO, "- esi: %p", process->regs.esi); + printd(KERN_INFO, "- edi: %p", process->regs.edi); + printd(KERN_INFO, "- eflags: %p", process->regs.eflags); + printd(KERN_INFO, "- cr3: %p", process->regs.cr3); + printd(KERN_INFO, "- eip: %p", process->regs.eip); + +} + /* * Create a process * Return the pid of created process @@ -101,11 +117,17 @@ unsigned long create_process(char *name, t_process *parent, unsigned int ownerId process->status = STATUS_RUN; process->ownerId = ownerId; process->priority = 0; - printk(KERN_INFO, "Process created at %p, id %d", process, process->pid); + printk(KERN_INFO, "Process created at %p, id %d, memory page from %p to %p", process, process->pid, start_memory, (char*)start_memory + PAGESIZE); copy_kernel_to_process_page_directory(get_kernel_struct()->kernel_page_directory, process->page_directory); + + // Used for testing in struct !! + //process->regs.ecx = 0xdeadbeef; + process->regs.esp = (uint32_t)start_memory; + process->regs.ebp = (uint32_t)start_memory; process->regs.eip = (uint32_t)functionStart; process->regs.eflags = 0x206; // enable interrupt process->regs.cr3 = process->page_directory->page_directory[0]; + show_process_registers(process); add_process_to_queue(process); return process->pid; } diff --git a/srcs/kernel/processes/processes.h b/srcs/kernel/processes/processes.h index c61d291..6aa92ea 100644 --- a/srcs/kernel/processes/processes.h +++ b/srcs/kernel/processes/processes.h @@ -43,16 +43,16 @@ typedef struct s_page_directory { typedef struct s_process_registers { uint32_t eax; - uint32_t ecx; - uint32_t edx; - uint32_t ebx; - uint32_t esp; - uint32_t ebp; - uint32_t esi; - uint32_t edi; - uint32_t eflags; - uint32_t cr3; - uint32_t eip; + uint32_t ecx; // 4 Offset + uint32_t edx; // 8 Offset + uint32_t ebx; // 12 Offset + uint32_t esp; // 16 Offset + uint32_t ebp; // 20 Offset + uint32_t esi; // 24 Offset + uint32_t edi; // 28 Offset + uint32_t eflags; // 32 Offset + uint32_t cr3; // 36 Offset + uint32_t eip; // 40 Offset } t_process_registers; typedef struct s_process { diff --git a/srcs/kernel/processes/processes_asm.s b/srcs/kernel/processes/processes_asm.s index f25ef3a..1e245b4 100644 --- a/srcs/kernel/processes/processes_asm.s +++ b/srcs/kernel/processes/processes_asm.s @@ -12,51 +12,53 @@ GLOBAL switch_regs -switch_regs: - mov ecx, [ebp + 4] - mov edx, [ebp + 8] - mov ebx, [ebp + 12] - mov esi, [ebp + 24] - mov edi, [ebp + 28] - ;mov eip, [ebp + 40] - ;mov esp, [ebp + 16] +extern printd + +message: + db 'Ecx is %p', 0 +switch_regs: + ; Debug message + ;push dword [eax + 4] + ;push message + ;push 0x1 + ;call printd - ; Restore registers - ;mov eax, [ebp + 0] - ;mov ebp, [ebp + 20] + ; Move general registers + mov ecx, [eax + 4] + mov edx, [eax + 8] + mov ebx, [eax + 12] + mov ebp, [eax + 20] + mov esi, [eax + 24] + mov edi, [eax + 28] + ; Load eflags + push dword [ebp + 32] ; Push new eflags on the (old ?) stack + popfd ; Pop last value on stack & Load it into eflags - ; load eflags - ;mov eax, [ebp + 32] - ;push eax - ;popfd ; Right now, eax, ebp, esp are not restored yet ; Enter usermode from here(make sure the registers are restored correctly for the user process !) - mov ax, 0x23 - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - - push 0x23 - ; Push user esp - mov eax, [ebp + 16] - push eax - mov eax, [ebp + 32] - push eax - ;pushfd - push 0x1b - ; Push eip - mov eax, [ebp + 40] - push eax - ; Enter usermode from here(make sure the registers are restored correctly for the user process !) - - ; Load eax here - mov eax, [ebp + 0] - ; Now, restore ebp - mov ebp, [ebp + 20] - ; sti - iret + mov ax, 0x23 ; ???? + mov ds, ax ; ???? + mov es, ax ; ???? + mov fs, ax ; ???? + mov gs, ax ; ???? + + push 0x23 ; ???? + ; Push user esp on (new ?) stack + push dword [eax + 16] ; esp + ; Push user eflags on (new ?) stack + push dword [ebp + 32] ; eflags + ; pushfd ; push all eflags on the stack + push 0x1b ; ???? + ; Push eip on (new ?) stack + push dword [ebp + 40] ; eip + + ; Load eax here + mov eax, [eax + 0] + ; Now, restore ebp + mov ebp, [eax + 20] + ; sti + iret diff --git a/srcs/kernel/processes/scheduler.c b/srcs/kernel/processes/scheduler.c index a0f642e..0b84b79 100644 --- a/srcs/kernel/processes/scheduler.c +++ b/srcs/kernel/processes/scheduler.c @@ -11,12 +11,16 @@ void put_process_end_list(t_scheduler_queue *process) { } void context_switch(t_process *process) { + printd(KERN_INFO, "before: process->regs is %p (or %p ?)", process->regs, &process->regs); if (process->regs.cr3 != 0) { printd(KERN_INFO, "regs.cr3 of process '%s' is not null. Loading page directory for this process.", process->name); printd(KERN_INFO, "Switching for page table at addr %p (or %p)", process->regs.cr3, process->page_directory->page_directory); enable_paging(process->page_directory->page_directory); + printd(KERN_INFO, "Enabled paging for %s", process->name); } - //switch_regs(&process->regs); + printd(KERN_INFO, "process->regs is %p (or %p ?)", process->regs, &process->regs); + printd(KERN_INFO, "retry compute %p", (char *) process + 64); + switch_regs((t_process_registers *)((char *) process + 64)); } void scheduler_loop() { diff --git a/srcs/lib/printk/include/printk.h b/srcs/lib/printk/include/printk.h index 5091da5..384f6a8 100644 --- a/srcs/lib/printk/include/printk.h +++ b/srcs/lib/printk/include/printk.h @@ -27,7 +27,7 @@ # define FT_PRINTF_STAR 32 # define FT_PRINTF_ZERO 64 -# define DEBUG_LOG 0 +# define DEBUG_LOG 1 typedef struct s_output { diff --git a/tests/check_logs_debug.py b/tests/check_logs_debug.py index 9f880d1..8955b23 100644 --- a/tests/check_logs_debug.py +++ b/tests/check_logs_debug.py @@ -14,8 +14,8 @@ for item in logsToSearch: if item in lines: - print("'{}' found in logs\n".format(item)) + print("'{}' found in logs".format(item)) else: - print("'{}' NOT found in logs\n".format(item)) + print("'{}' NOT found in logs".format(item)) exit(1) exit(0)