swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 1 | #include "task.h" |
| 2 | #include "alloc.h" |
| 3 | #include "io.h" |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 4 | #include "log.h" |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 5 | #include "paging.h" |
| 6 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 7 | struct process processes[1024] = {0}; |
| 8 | struct ll_task_i *first_task = NULL, *last_task = NULL, *current_task = NULL; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 9 | static uint next_task_id = 0; |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 10 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 11 | void _init_tasks(uint kernel_esp, uint kernel_ebp, uint kernel_eip) |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 12 | { |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 13 | asm volatile("cli"); |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 14 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 15 | kprintf("_init_tasks\n"); |
| 16 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 17 | processes[0] = (struct process){ |
| 18 | .exists = true, |
| 19 | .id = 0, |
| 20 | .ring = 0, |
| 21 | .uid = 0, |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 22 | .page_directory_p = VIRT_TO_PHYS(kernel_page_directory), |
| 23 | // Obviously this isn't the actual stack position, but we want it to |
| 24 | // grow down from 4 gb so we will pretend that the first task has its |
| 25 | // stack at exactly 4gb and work from there. Because the new stack will |
| 26 | // be mapped to any random frame, it doesn't actually matter where we |
| 27 | // put it, we just want somewhere that won't collide with any user space |
| 28 | // stuff or our heap. |
| 29 | .last_stack_pos = 0xFFFFF000, |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 30 | }; |
| 31 | strcpy(processes[0].name, "kernel"); |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 32 | kprintf("in _init_tasks, strlen of 'kernel' is %d\n", strlen("kernel")); |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 33 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 34 | first_task = last_task = current_task = malloc(sizeof(struct ll_task_i)); |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 35 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 36 | first_task->next = NULL; |
| 37 | first_task->task = (struct task){ |
| 38 | .proc = &processes[0], |
| 39 | .esp = kernel_esp, |
| 40 | .ebp = kernel_ebp, |
| 41 | .eip = kernel_eip, |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 42 | .id = next_task_id++, |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 43 | }; |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 44 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 45 | kprintf("Returning from _init_tasks\n"); |
| 46 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 47 | asm volatile("sti"); |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 48 | } |
| 49 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 50 | struct process *get_process(uint pid) |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 51 | { |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 52 | if (pid < 1024) |
| 53 | return &processes[pid]; |
| 54 | else |
| 55 | return NULL; |
| 56 | } |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 57 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 58 | int get_task_id() |
| 59 | { |
| 60 | return current_task->task.id; |
| 61 | } |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 62 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 63 | int get_process_id() |
| 64 | { |
| 65 | return current_task->task.proc->id; |
| 66 | } |
| 67 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 68 | void spawn_thread(void (*function)()) |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 69 | { |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 70 | asm volatile("cli"); |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 71 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 72 | struct process *proc = current_task->task.proc; |
| 73 | // Virtual address of page directory (in kernel memory) |
| 74 | uint *dir_v = PHYS_TO_VIRT(proc->page_directory_p); |
| 75 | // Virtual location of new stack |
| 76 | uint new_stack_base_v = proc->last_stack_pos = proc->last_stack_pos - 0x1000; |
| 77 | |
| 78 | // Alloc a new page in the current process mapping to the new stack |
| 79 | alloc_page(dir_v, (void *)new_stack_base_v); |
| 80 | |
| 81 | // <TEST>: see if we can assign to the new stack memory (ie: did mapping succeed) |
| 82 | uint *base = (uint *)new_stack_base_v; |
| 83 | |
| 84 | kprintf("base = 0x%x\n", base); |
| 85 | |
| 86 | *base = 0; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 87 | // </TEST> |
| 88 | |
| 89 | struct ll_task_i *ll_task = malloc(sizeof(struct ll_task_i)); |
| 90 | memset(ll_task, 0, sizeof(struct ll_task_i)); |
| 91 | struct task *task = &ll_task->task; |
| 92 | |
| 93 | task->proc = proc; |
| 94 | task->id = next_task_id++; |
| 95 | task->ebp = task->esp = new_stack_base_v; |
| 96 | task->eip = (uint)function; |
| 97 | |
| 98 | last_task->next = ll_task; |
| 99 | last_task = ll_task; |
| 100 | |
| 101 | asm volatile("sti"); |
| 102 | } |
| 103 | |
| 104 | extern void _switch_to_task(uint page_directory, uint eip, uint ebp, uint esp); |
| 105 | |
| 106 | void switch_to_task(struct task *task) |
| 107 | { |
| 108 | _switch_to_task(task->proc->page_directory_p, task->eip, task->ebp, |
| 109 | task->esp); |
| 110 | __builtin_unreachable(); |
| 111 | } |
| 112 | |
| 113 | // WARNING: do not call this manually, it will clobber everything |
| 114 | // except esp, ebp, and eip (obviously). Use switch_task in task_api.s |
| 115 | // instead. |
| 116 | void _do_switch_task(uint eip, uint ebp, uint esp) |
| 117 | { |
| 118 | // sti is called in switch_to_task |
| 119 | asm volatile("cli"); |
| 120 | |
| 121 | kprintf("\nin _do_switch_task(%d, %d, %d)\n", eip, ebp, esp); |
| 122 | |
| 123 | // save context for this task |
| 124 | current_task->task.ebp = ebp; |
| 125 | current_task->task.esp = esp; |
| 126 | current_task->task.eip = eip; |
| 127 | |
| 128 | if (current_task->next == NULL) |
| 129 | { |
| 130 | // Start from the first task if there are more tasks, or just return |
| 131 | if (current_task == first_task) |
| 132 | return; // No context switch necessary |
| 133 | |
| 134 | current_task = first_task; |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | // Continue the next task |
| 139 | current_task = current_task->next; |
| 140 | } |
| 141 | |
| 142 | kprintf("Will switch to task id %d\n", current_task->task.id); |
| 143 | |
| 144 | switch_to_task(¤t_task->task); |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 145 | } |