Skip to content

Commit

Permalink
Add paraemter to create supervisor/user tasks/threads
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamos82 committed Nov 18, 2023
1 parent d7fae9a commit 5ef8041
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/include/kernel/scheduling/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct task_t {

extern size_t next_task_id;

task_t* create_task( char *name, void (*_entry_point)(void *), void *args );
task_t* create_task( char *name, void (*_entry_point)(void *), void *args, bool is_supervisor );
task_t* get_task( size_t task_id );

bool add_thread_to_task_by_id( size_t task_id, thread_t* thread );
Expand Down
2 changes: 1 addition & 1 deletion src/include/kernel/scheduling/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct thread_t {
extern size_t next_thread_id;


thread_t* create_thread(char* thread_name, void (*_entry_point)(void *) , void* arg, struct task_t* parent_task);
thread_t* create_thread(char* thread_name, void (*_entry_point)(void *) , void* arg, struct task_t* parent_task, bool is_supervisor);
void thread_execution_wrapper( void (*)(void *), void*);
void thread_suicide_trap();
void thread_sleep(size_t millis);
Expand Down
10 changes: 4 additions & 6 deletions src/kernel/arch/x86_64/mem/vmm_mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ void *map_phys_to_virt_addr_hh(void* physical_address, void* address, size_t fla
pml4_root = kernel_settings.paging.hhdm_page_root_address;
}

loglinef(Verbose, "(%s): here", __FUNCTION__);

if (pml4_root != NULL) {
pml4_table = pml4_root;
loglinef(Verbose, "(%s): Entries values pml4_e: 0x%d pdpr_e: 0x%d pd_e: 0x%d", __FUNCTION__, pml4_e, pdpr_e, pd_e);
loglinef(Verbose, "(%s):\taddress: 0x%x", __FUNCTION__, address);
loglinef(Verbose, "(%s):\tpml4_root address: 0x%x", __FUNCTION__, pml4_root);
loglinef(Verbose, "(%s):\tpml4_root[pml4_e] = 0x%x pml4_table[pml4_e] = 0x%x", __FUNCTION__, pml4_root[pml4_e], pml4_table[pml4_e]);
//loglinef(Verbose, "(%s):\tpml4_root address: 0x%x", __FUNCTION__, pml4_root);
//loglinef(Verbose, "(%s):\tpml4_root[pml4_e] = 0x%x pml4_table[pml4_e] = 0x%x", __FUNCTION__, pml4_root[pml4_e], pml4_table[pml4_e]);
loglinef(Verbose, "(%s):\tpdpr base_address: 0x%x", __FUNCTION__, pml4_root[pml4_e] & VM_PAGE_TABLE_BASE_ADDRESS_MASK);

if ( !(pml4_root[pml4_e] & 0b1) ) {
Expand Down Expand Up @@ -67,7 +65,7 @@ void *map_phys_to_virt_addr_hh(void* physical_address, void* address, size_t fla
pt_root = new_table_hhdm;
#elif SMALL_PAGES == 0
pd_root[pd_e] = (uint64_t) (physical_address) | HUGEPAGE_BIT | flags | user_mode_status;
loglinef(Verbose, "(%s): PD Flags: 0x%x entry value: 0x%x", __FUNCTION__, flags, pd_root[pd_e]);
loglinef(Verbose, "(%s): PD Flags: 0x%x entry value: 0x%x", __FUNCTION__, flags, pd_root[pd_e]);
#endif
}

Expand Down Expand Up @@ -180,7 +178,7 @@ void *map_vaddress(void *virtual_address, size_t flags, uint64_t *pml4_root){

void map_vaddress_range(void *virtual_address, size_t flags, size_t required_pages, uint64_t *pml4_root) {
for(size_t i = 0; i < required_pages; i++) {
map_vaddress(virtual_address + (i * PAGE_SIZE_IN_BYTES), flags, NULL);
map_vaddress(virtual_address + (i * PAGE_SIZE_IN_BYTES), flags, pml4_root);
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,19 @@ void kernel_start(unsigned long addr, unsigned long magic){
char b = 'b';
char c = 'c';
char d = 'd';
task_t* idle_task = create_task("idle", noop, &a);
task_t* idle_task = create_task("idle", noop, &a, true);
idle_thread = idle_task->threads;
task_t* eldi_task = create_task("eldi", noop2, &b);
create_thread("ledi", noop2, &c, eldi_task);
create_task("sleeper", noop3, &d);
print_thread_list(eldi_task->task_id);
int fd_id = open("/home/ivan/testfile.txt", 0);
//task_t* eldi_task = create_task("eldi", noop2, &b);
//create_thread("ledi", noop2, &c, eldi_task);
//create_task("sleeper", noop3, &d);
//print_thread_list(eldi_task->task_id);
/*int fd_id = open("/home/ivan/testfile.txt", 0);
loglinef(Verbose, "(kernel_main) Obtained fd id: %d fs_fd_id: %d", fd_id, vfs_opened_files[fd_id].fs_specific_id);
char buffer[15] = "";
read(fd_id, buffer, 15);
loglinef(Verbose, "(kernel_main) Output of read: %s", buffer);
int result = close(fd_id);
loglinef(Verbose, "(kernel_main) Closing file with id: %d", result);
loglinef(Verbose, "(kernel_main) Closing file with id: %d", result);*/
//execute_runtime_tests();
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);
Expand All @@ -219,11 +219,11 @@ void kernel_start(unsigned long addr, unsigned long magic){
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");
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]);
//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);
}
13 changes: 9 additions & 4 deletions src/kernel/scheduling/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern uint64_t p4_table[];
extern uint64_t p3_table[];
extern uint64_t p3_table_hh[];

task_t* create_task(char *name, void (*_entry_point)(void *), void *args) {
task_t* create_task(char *name, void (*_entry_point)(void *), void *args, bool is_supervisor) {
//disable interrupts while creating a task
asm("cli");
task_t* new_task = (task_t*) kmalloc(sizeof(task_t));
Expand All @@ -23,9 +23,14 @@ task_t* create_task(char *name, void (*_entry_point)(void *), void *args) {
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);
prepare_virtual_memory_environment(new_task);
vmm_init(VMM_LEVEL_USER, &(new_task->vmm_data));
if ( is_supervisor ){
vmm_init(VMM_LEVEL_SUPERVISOR, &(new_task->vmm_data));
} else {
vmm_init(VMM_LEVEL_USER, &(new_task->vmm_data));
}

if( _entry_point != NULL) {
thread_t* thread = create_thread(name, _entry_point, args, new_task);
thread_t* thread = create_thread(name, _entry_point, args, new_task, is_supervisor);
new_task->threads = thread;
}
scheduler_add_task(new_task);
Expand Down Expand Up @@ -130,7 +135,7 @@ void print_thread_list(size_t task_id) {
if (task != NULL) {
thread_t* thread = task->threads;
while(thread != NULL) {
loglinef(Verbose, "(print_thread_list)\tThread; %d - %s", thread->tid, thread->thread_name);
loglinef(Verbose, "(%s)\tThread; %d - %s", __FUNCTION__, thread->tid, thread->thread_name);
thread = thread->next;
}
}
Expand Down
71 changes: 43 additions & 28 deletions src/kernel/scheduling/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@
#include <kernel.h>
#include <vmm.h>

thread_t* create_thread(char* thread_name, void (*_entry_point)(void *), void* arg, task_t* parent_task) {
unsigned char code_to_run[] = {
0xeb, 0xfe
};

thread_t* create_thread(char* thread_name, void (*_entry_point)(void *), void* arg, task_t* parent_task, bool is_supervisor) {
// 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");
}

thread_t *new_thread = kmalloc(sizeof(thread_t));
new_thread->tid = next_thread_id++;
new_thread->parent_task = parent_task;
Expand All @@ -20,21 +28,25 @@ 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: %u", (char) *((char*) arg), arg, thread_name, _entry_point);
loglinef(Verbose, "(create_thread): 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;
new_thread->execution_frame->rip = (uint64_t) thread_execution_wrapper;
new_thread->execution_frame->rdi = (uint64_t) _entry_point;
new_thread->execution_frame->rsi = (uint64_t) arg;
new_thread->execution_frame->rip = (uint64_t) code_to_run;
// rdi and rsi are the two arguments passed to the thread_execution_wrapper function
new_thread->execution_frame->rdi = 0;
new_thread->execution_frame->rsi = 0;
new_thread->execution_frame->rflags = 0x202;
// For user mode cs is 1b and ss is 23
new_thread->execution_frame->ss = 0x10;
//new_thread->execution_frame->ss = 0x23;
new_thread->execution_frame->cs = 0x08;
//new_thread->execution_frame->cs = 0x1B;
if ( is_supervisor ) {
new_thread->execution_frame->ss = 0x10;
new_thread->execution_frame->cs = 0x08;
} else {
// For user mode cs is 1b and ss is 23
new_thread->execution_frame->ss = 0x23;
new_thread->execution_frame->cs = 0x1B;
}

// Every thread need it's kernel stack allocated (aka rsp0 field of the TSS)
new_thread->rsp0 = kmalloc(THREAD_DEFAULT_STACK_SIZE);
Expand All @@ -43,8 +55,8 @@ 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, &(parent_task->vmm_data));
//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));
if (stack_pointer == NULL) {
loglinef(Fatal, "(create_thread): rsp is null - PANIC!");
while(1);
Expand All @@ -54,7 +66,7 @@ thread_t* create_thread(char* thread_name, void (*_entry_point)(void *), void* a
new_thread->stack = (uintptr_t)stack_pointer + THREAD_DEFAULT_STACK_SIZE;
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);
if (parent_task != NULL) {
add_thread_to_task(parent_task, new_thread);
} else {
Expand Down Expand Up @@ -98,16 +110,18 @@ void noop(void *v) {
char str[2];
str[0] = *c;
str[1] = '\0';
while(1) {
//loglinef(Verbose, "(%s): inside noop2", __FUNCTION__);
/* while(1) {
//loglinef(Verbose, "Task: %c - %d", (char) *c, i);
#if USE_FRAMEBUFFER == 1
_fb_printStr(str, 0, 12, 0x000000, 0xE169CD);
#endif
}
}*/
asm("nop");
}

void noop2(void *v) {
//loglinef(Verbose, "(%s): inside noop2", __FUNCTION__);
char* c = (char*)v;
int i=0;
char str[2];
Expand All @@ -117,15 +131,16 @@ void noop2(void *v) {
while(i < 100) {
i++;
//loglinef(Verbose, "Task2: %c - %d", (char) *c, i);
#if USE_FRAMEBUFFER == 1
/*#if USE_FRAMEBUFFER == 1
_fb_printStr(str, 0, 12, 0x000000, 0xE169CD);
#endif
#endif*/

}
}

void noop3(void *v) {
char* c = (char*)v;
asm("nop");
/*char* c = (char*)v;
int i=0;
char str[4];
str[0] = (char) *c;
Expand All @@ -134,9 +149,9 @@ void noop3(void *v) {
str[3] = '\0';
while(i < 10000) {
i++;
//loglinef(Verbose, "Task2: %c - %d", (char) *c, i);
loglinef(Verbose, "Task2: %c - %d", (char) *c, i);
#if USE_FRAMEBUFFER == 1
_fb_printStr(str, 0, 12, 0x000000, 0xE169CD);
//_fb_printStr(str, 0, 12, 0x000000, 0xE169CD);
#endif
}
loglinef(Verbose, "(noop3) Going to sleep %d", get_kernel_uptime());
Expand All @@ -145,24 +160,24 @@ void noop3(void *v) {
i = 0;
while(i < 1000) {
i++;
//loglinef(Verbose, "Task2: r- %d", (char) *c, i);
loglinef(Verbose, "Task2: r- %d", (char) *c, i);
#if USE_FRAMEBUFFER == 1
_fb_printStr("r", 1, 12, 0x000000, 0xE169CD);
//_fb_printStr("r", 1, 12, 0x000000, 0xE169CD);
#endif
}
loglinef(Verbose, "(%s): allocating 100 bytes", __FUNCTION__);
vmm_alloc(100, VMM_FLAGS_PRESENT | VMM_FLAGS_WRITE_ENABLE, NULL);
loglinef(Verbose, "(%s): end allocating 100 bytes", __FUNCTION__);
uint64_t *test_addr = (uint64_t *) vmm_alloc(2097253, 0, NULL);
test_addr[0] = 5;
loglinef(Verbose, "(noop3): test_addr[0] = %d", test_addr[0]);
//vmm_alloc(100, VMM_FLAGS_PRESENT | VMM_FLAGS_WRITE_ENABLE, NULL);
//loglinef(Verbose, "(%s): end allocating 100 bytes", __FUNCTION__);
//uint64_t *test_addr = (uint64_t *) vmm_alloc(2097253, 0, NULL);
//test_addr[0] = 5;
//loglinef(Verbose, "(noop3): test_addr[0] = %d", test_addr[0]);
task_t *current_task = current_executing_thread->parent_task;
uint64_t *tmp_var = vmm_alloc(0x1000, VMM_FLAGS_PRESENT | VMM_FLAGS_WRITE_ENABLE, &(current_task->vmm_data));
loglinef(Verbose, "(%s) task name: %s - Tmp var address returned by vmm_alloc: 0x%x", __FUNCTION__, current_task->task_name, tmp_var);
tmp_var[0] = 0x1234;
loglinef(Verbose, "(%s) Tmp var address returned by vmm_alloc: 0x%x==0x1234 ", __FUNCTION__, tmp_var[0]);
loglinef(Verbose, "(%s) Tmp var address returned by vmm_alloc: 0x%x==0x1234 ", __FUNCTION__, tmp_var[0]);*/
}

char *get_thread_status(thread_t *thread) {
Expand Down

0 comments on commit 5ef8041

Please sign in to comment.