Skip to content

Commit

Permalink
fix log in kheap and update scheduler files with pretty_log
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamos82 committed Dec 7, 2023
1 parent 446fe9b commit 1b280e1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/kernel/mem/kheap.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void *kmalloc(size_t size) {
KHeapMemoryNode *current_node = kernel_heap_start;
// If size is 0 we don't need to do anything
if( size == 0 ) {
pretty_logf(Verbose, "Size is null");
pretty_log(Verbose, "Size is null");
return NULL;
}

Expand Down
30 changes: 15 additions & 15 deletions src/kernel/scheduling/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ cpu_status_t* schedule(cpu_status_t* cur_status) {
thread_t* prev_executing_thread;
thread_t* thread_to_execute = idle_thread;
uint16_t prev_thread_tid = -1;
//loglinef(Verbose, "Cur thread: %u %s", current_thread->tid, current_thread->thread_name);
//loglinef(Verbose, "(schedule) ---Cur stack: 0x%x", cur_status->rsp);
//pretty_logf(Verbose, "Cur thread: %u %s", current_thread->tid, current_thread->thread_name);
//pretty_logf(Verbose, "---Cur stack: 0x%x", cur_status->rsp);
// First let's check if the current task need to be scheduled or not;
if (current_executing_thread->status == SLEEP) {
// If the task has been placed to sleep it needs to be scheduled
//loglinef(Verbose, "(%s): Current thread %d status is sleeping!", __FUNCTION__ ,current_executing_thread->tid);
//loglinef(Verbose, "Current thread %d status is sleeping!" ,current_executing_thread->tid);
current_executing_thread->ticks = SCHEDULER_NUMBER_OF_TICKS;
}

Expand All @@ -69,10 +69,10 @@ cpu_status_t* schedule(cpu_status_t* cur_status) {

while (current_thread->tid != prev_thread_tid) {
if (current_thread->status == SLEEP) {
loglinef(Verbose, "(schedule) This thread %d is sleeping", current_thread->tid);
//loglinef(Verbose, "Current uptime: %d - wakeup: %d", get_kernel_uptime(), current_thread->wakeup_time);
pretty_logf(Verbose, "This thread %d is sleeping", current_thread->tid);
//pretty_logf(Verbose, "Current uptime: %d - wakeup: %d", get_kernel_uptime(), current_thread->wakeup_time);
if ( get_kernel_uptime() > current_thread->wakeup_time) {
//loglinef(Verbose, "(schedule) --->WAKING UP: %d - thread_name: %s", current_thread->tid, current_thread->thread_name);
//pretty_logf(Verbose, "--->WAKING UP: %d - thread_name: %s", current_thread->tid, current_thread->thread_name);
current_thread->status = READY;
thread_to_execute = current_thread;
break;
Expand All @@ -90,7 +90,7 @@ cpu_status_t* schedule(cpu_status_t* cur_status) {
current_thread = scheduler_get_next_thread();
}

loglinef(Verbose, "(%s): Current thread %d status: %d name: %s!", __FUNCTION__ , current_thread->status, current_thread->tid, current_thread->thread_name);
pretty_logf(Verbose, "Current thread %d status: %d name: %s!", current_thread->status, current_thread->tid, current_thread->thread_name);

// We have found a thread to run, let's update it's status
thread_to_execute->status = RUN;
Expand All @@ -99,19 +99,19 @@ cpu_status_t* schedule(cpu_status_t* cur_status) {
current_executing_thread = thread_to_execute;
task_t *current_task = current_executing_thread->parent_task;
// ... every task has it's own addressing space, so we need to update the cr3 register
loglinef(Verbose, "(%s) Loading cr3: 0x%x", __FUNCTION__, current_task->vm_root_page_table);
pretty_logf(Verbose, "Loading cr3: 0x%x", current_task->vm_root_page_table);
load_cr3(current_task->vm_root_page_table);
loglinef(Verbose, "(%s): current_thread->execution_frame->rip: 0x%x, vmm_data is: 0x%x", __FUNCTION__, current_executing_thread->execution_frame->rip, &(current_task->vmm_data));
pretty_logf(Verbose, "current_thread->execution_frame->rip: 0x%x, vmm_data is: 0x%x", current_executing_thread->execution_frame->rip, &(current_task->vmm_data));
// ... and finally we need to update the tss structure with the current thread rsp0
kernel_tss.rsp0 = (uint64_t) current_executing_thread->rsp0;
//loglinef(Verbose, "(schedule) leaving schedule...");
loglinef(Verbose, "(schedule) next task to run: %d->(%s)", current_executing_thread->tid, current_executing_thread->thread_name);
//pretty_log(Verbose, "leaving schedule...");
pretty_logf(Verbose, "next task to run: %d->(%s)", current_executing_thread->tid, current_executing_thread->thread_name);
return current_executing_thread->execution_frame;
}

void scheduler_add_task(task_t* task) {
if (root_task == NULL) {
loglinef(Verbose, "(scheduler_add_task) First task being added: %s", task->task_name);
pretty_logf(Verbose, "(scheduler_add_task) First task being added: %s", task->task_name);
}
task->next = root_task;
root_task = task;
Expand All @@ -121,15 +121,15 @@ void scheduler_add_thread(thread_t* thread) {
thread->next = thread_list;
thread_list_size++;
thread_list = thread;
loglinef(Verbose, "(scheduler_add_thread) Adding thread: %s - %d", thread_list->thread_name, thread_list->tid);
pretty_logf(Verbose, "(scheduler_add_thread) Adding thread: %s - %d", thread_list->thread_name, thread_list->tid);
if (current_executing_thread == NULL) {
//This means that there are no tasks on the queue yet.
current_executing_thread = thread;
}
}

void scheduler_delete_thread(size_t thread_id) {
loglinef(Verbose, "(scheduler_delete_thread) Called with thread id: %d", thread_id);
pretty_logf(Verbose, "(scheduler_delete_thread) Called with thread id: %d", thread_id);
thread_t *thread_item = thread_list;
thread_t *prev_item = NULL;

Expand Down Expand Up @@ -184,7 +184,7 @@ size_t scheduler_get_queue_size() {
}

void scheduler_yield() {
logline(Verbose, "(scheulder_yield) Interrupting current_thread");
pretty_log(Verbose, "(scheulder_yield) Interrupting current_thread");
asm("int $0x20");
}

26 changes: 13 additions & 13 deletions src/kernel/scheduling/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ task_t* create_task(char *name, void (*_entry_point)(void *), void *args, bool i
strcpy(new_task->task_name, name);
new_task->parent = NULL;
new_task->task_id = next_task_id++;
loglinef(Verbose, "(create_task) Task created with name: %s - Task id: %d", new_task->task_name, new_task->task_id);
pretty_logf(Verbose, "Task created with name: %s - Task id: %d", new_task->task_name, new_task->task_id);
prepare_virtual_memory_environment(new_task);
if ( is_supervisor ){
vmm_init(VMM_LEVEL_SUPERVISOR, &(new_task->vmm_data));
} else {
vmm_init(VMM_LEVEL_USER, &(new_task->vmm_data));
}
if( is_supervisor) {
loglinef(Verbose, "(%s): creating new thread", __FUNCTION__);
pretty_log(Verbose, "creating new thread");
thread_t* thread = create_thread(name, _entry_point, args, new_task, is_supervisor);
new_task->threads = thread;
} else {
loglinef(Verbose, "(%s): creating new thread userspace", __FUNCTION__);
pretty_log(Verbose, "creating new thread userspace");
thread_t* thread = create_thread(name, NULL, args, new_task, is_supervisor);
new_task->threads = thread;
}
Expand All @@ -48,15 +48,15 @@ void prepare_virtual_memory_environment(task_t* task) {
// 1. Prepare resources: allocatin an array of VM_PAGES_PER_TABLE
// Make sure this address is physical, then it needs to be mapped to a virtual one.a
task->vm_root_page_table = pmm_alloc_frame();
loglinef(Verbose, "(%s) vm_root_page_table address: %x", __FUNCTION__, task->vm_root_page_table);
pretty_logf(Verbose, "vm_root_page_table address: %x", task->vm_root_page_table);
//identity_map_phys_address(task->vm_root_page_table, 0);
// I will get the page frame first, then get virtual address to map it to with vmm_alloc, and then do the mapping on the virtual address.
// Tecnically the vmm_allos is not needed, since i have the direct memory map already accessible, so i just need to access it through the direct map.

//void* vm_root_vaddress = vmm_alloc(PAGE_SIZE_IN_BYTES, VMM_FLAGS_ADDRESS_ONLY, NULL);
void* vm_root_vaddress = hhdm_get_variable ((uintptr_t) task->vm_root_page_table);
task->vmm_data.root_table_hhdm = (uintptr_t) vm_root_vaddress;
loglinef(Verbose, "(%s) vm_root_vaddress: %x", __FUNCTION__, vm_root_vaddress);
pretty_logf(Verbose, "vm_root_vaddress: %x", vm_root_vaddress);
//map_phys_to_virt_addr(task->vm_root_page_table, vm_root_vaddress, VMM_FLAGS_PRESENT | VMM_FLAGS_WRITE_ENABLE, NULL);

// 2. We will map the whole higher half of the kernel, this means from pml4 item 256 to 511
Expand All @@ -66,12 +66,12 @@ void prepare_virtual_memory_environment(task_t* task) {
((uint64_t *)vm_root_vaddress)[i] = 0x00;
} else if ( i == 510 ) {
((uint64_t *)vm_root_vaddress)[i] = (uint64_t) (task->vm_root_page_table) | VMM_FLAGS_PRESENT | VMM_FLAGS_WRITE_ENABLE;
loglinef(Verbose, "(%s): Mapping recursive entry: 0x%x", __FUNCTION__, ((uint64_t *)vm_root_vaddress)[i]);
pretty_logf(Verbose, "Mapping recursive entry: 0x%x", ((uint64_t *)vm_root_vaddress)[i]);
} else {
((uint64_t *)vm_root_vaddress)[i] = p4_table[i];
}
if (p4_table[i] != 0) {
loglinef(Verbose, "(prepare_virtual_memory_environment): %d: o:0x%x - c:0x%x - t:0x%x", i, p4_table[i], kernel_settings.paging.page_root_address[i], ((uint64_t*)vm_root_vaddress)[i]);
pretty_logf(Verbose, "(prepare_virtual_memory_environment): %d: o:0x%x - c:0x%x - t:0x%x", i, p4_table[i], kernel_settings.paging.page_root_address[i], ((uint64_t*)vm_root_vaddress)[i]);
}
}
}
Expand All @@ -91,16 +91,16 @@ bool remove_thread_from_task(size_t thread_id, task_t *task) {
// We don't freethe thread here, because is the scheduler in charge of deleting DEAD threads
thread_t *cur_thread = task->threads;
thread_t *prev_thread = cur_thread;
loglinef( Verbose, "(%s) Removing thread with thread id: %d, from task: %d with name: %s", __FUNCTION__, thread_id, task->task_id, task->task_name);
pretty_logf( Verbose, "Removing thread with thread id: %d, from task: %d with name: %s", thread_id, task->task_id, task->task_name);
while ( cur_thread != NULL ) {
if ( cur_thread->tid == thread_id ) {
loglinef( Verbose, "(%s) Found thread to remove thread name: %s", __FUNCTION__, cur_thread->thread_name);
pretty_logf( Verbose, "Found thread to remove thread name: %s", cur_thread->thread_name);
if ( cur_thread == task->threads) {
loglinef( Verbose, "(%s), Is the first thread in the queue addr_value: 0x%x", __FUNCTION__, cur_thread->next_sibling);
pretty_logf( Verbose, "Is the first thread in the queue addr_value: 0x%x", cur_thread->next_sibling);
task->threads = cur_thread->next_sibling;
return true;;
} else {
loglinef( Verbose, "(%s), Is in the middle, mergin prev and cur->next_sibling", __FUNCTION__);
pretty_log( Verbose, "Is in the middle, merging prev and cur->next_sibling");
prev_thread->next_sibling = cur_thread->next_sibling;
}
}
Expand All @@ -126,7 +126,7 @@ task_t* get_task(size_t task_id) {
}
task_t* cur_task = root_task;
while ( cur_task != NULL ) {
loglinef(Verbose, "(get_task) Searching task: %d", cur_task->task_id);
pretty_logf(Verbose, "(get_task) Searching task: %d", cur_task->task_id);
if ( cur_task->task_id == task_id ) {
return cur_task;
}
Expand All @@ -140,7 +140,7 @@ void print_thread_list(size_t task_id) {
if (task != NULL) {
thread_t* thread = task->threads;
while(thread != NULL) {
loglinef(Verbose, "(%s)\tThread; %d - %s", __FUNCTION__, thread->tid, thread->thread_name);
pretty_logf(Verbose, "\tThread; %d - %s", thread->tid, thread->thread_name);
thread = thread->next;
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/kernel/scheduling/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ thread_t* create_thread(char* thread_name, void (*_entry_point)(void *), void* a
// The first part is pretty trivial mostly bureaucray. Setting basic thread information like name, tid, parent...
// Just like when registtering a new born child :D
if ( parent_task == NULL) {
loglinef(Fatal, "(%s): Cannot create thread without parent task", __FUNCTION__);
pretty_log(Fatal, "Cannot create thread without parent task");
}

thread_t *new_thread = kmalloc(sizeof(thread_t));
Expand All @@ -29,18 +29,18 @@ thread_t* create_thread(char* thread_name, void (*_entry_point)(void *), void* a
new_thread->next = NULL;
new_thread->next_sibling = NULL;
new_thread->ticks = 0;
loglinef(Verbose, "(create_thread): Creating thread with arg: %c - arg: %x - name: %s - rip: %x", (char) *((char*) arg), arg, thread_name, _entry_point);
pretty_logf(Verbose, "Creating thread with arg: %c - arg: %x - name: %s - rip: %x", (char) *((char*) arg), arg, thread_name, _entry_point);

//Here we create a new execution frame to be used when switching to a newly created task
new_thread->execution_frame = kmalloc(sizeof(cpu_status_t));
new_thread->execution_frame->interrupt_number = 0x101;
new_thread->execution_frame->error_code = 0x0;
if (!is_supervisor) {
loglinef(Verbose, "(%s): vmm_data address: 0x%x", __FUNCTION__, &(parent_task->vmm_data));
pretty_logf(Verbose, "vmm_data address: 0x%x", &(parent_task->vmm_data));
new_thread->execution_frame->rip = prepare_userspace_function(&(parent_task->vmm_data));
loglinef(Verbose, "(%s): using userspace function address: 0x%x", __FUNCTION__, new_thread->execution_frame->rip);
pretty_logf(Verbose, "using userspace function address: 0x%x", new_thread->execution_frame->rip);
} else {
loglinef(Verbose, "(%s): using idle function", __FUNCTION__);
pretty_log(Verbose, "using idle function");
new_thread->execution_frame->rip = (uint64_t) code_to_run;
}
// rdi and rsi are the two arguments passed to the thread_execution_wrapper function
Expand All @@ -59,26 +59,26 @@ thread_t* create_thread(char* thread_name, void (*_entry_point)(void *), void* a
// Every thread need it's kernel stack allocated (aka rsp0 field of the TSS)
new_thread->rsp0 = kmalloc(THREAD_DEFAULT_STACK_SIZE) + THREAD_DEFAULT_STACK_SIZE;
if (new_thread->rsp0 == NULL) {
loglinef(Fatal, "(create_thread): rsp0 is null - PANIC!");
pretty_log(Fatal, "rsp0 is null - PANIC!");
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) {
loglinef(Fatal, "(create_thread): rsp is null - PANIC!");
pretty_log(Fatal, "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;
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);
pretty_logf(Verbose, "thread: %s stack address returned: 0x%x", new_thread->thread_name, new_thread->execution_frame->rsp);
if (parent_task != NULL) {
add_thread_to_task(parent_task, new_thread);
} else {
loglinef(Fatal, "(%s): Cannot create thread without parent task");
pretty_log(Fatal, "Cannot create thread without parent task");
}

scheduler_add_thread(new_thread);
Expand All @@ -89,7 +89,7 @@ void thread_sleep(size_t millis) {
current_executing_thread->status = SLEEP;
uint64_t kernel_uptime = get_kernel_uptime();
current_executing_thread->wakeup_time = kernel_uptime + millis; // To change with millis since boot + millis
loglinef(Verbose, "(thread_sleep) Kernel uptime is: %u - wakeup time is: %u", kernel_uptime, current_executing_thread->wakeup_time);
pretty_logf(Verbose, "(thread_sleep) Kernel uptime is: %u - wakeup time is: %u", kernel_uptime, current_executing_thread->wakeup_time);
scheduler_yield();
}

Expand All @@ -99,7 +99,7 @@ void thread_wakeup(thread_t* thread) {

void thread_suicide_trap() {
current_executing_thread->status = DEAD;
loglinef(Verbose, "(thread_suicide_trap) Suicide function called on thread: %d name: %s - Status: %s", current_executing_thread->tid, current_executing_thread->thread_name, get_thread_status(current_executing_thread));
pretty_logf(Verbose, "(thread_suicide_trap) Suicide function called on thread: %d name: %s - Status: %s", current_executing_thread->tid, current_executing_thread->thread_name, get_thread_status(current_executing_thread));
while(1);
}

Expand Down

0 comments on commit 1b280e1

Please sign in to comment.