Skip to content

Commit

Permalink
Improved context switch process & advanced in debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
lubenard committed Nov 7, 2023
1 parent 244c419 commit e6b60dd
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 67 deletions.
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 11 additions & 5 deletions srcs/kernel/gdt/gdt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;

Expand All @@ -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

Expand All @@ -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);
}
5 changes: 3 additions & 2 deletions srcs/kernel/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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");*/
Expand Down
5 changes: 0 additions & 5 deletions srcs/kernel/memory/vmm/vmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
24 changes: 23 additions & 1 deletion srcs/kernel/processes/processes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
20 changes: 10 additions & 10 deletions srcs/kernel/processes/processes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
82 changes: 42 additions & 40 deletions srcs/kernel/processes/processes_asm.s
Original file line number Diff line number Diff line change
Expand Up @@ -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

6 changes: 5 additions & 1 deletion srcs/kernel/processes/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion srcs/lib/printk/include/printk.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions tests/check_logs_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit e6b60dd

Please sign in to comment.