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 | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 13 | kprintf("_init_tasks\n"); |
| 14 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 15 | processes[0] = (struct process){ |
| 16 | .exists = true, |
| 17 | .id = 0, |
| 18 | .ring = 0, |
| 19 | .uid = 0, |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 20 | .page_directory_p = VIRT_TO_PHYS(kernel_page_directory), |
| 21 | // Obviously this isn't the actual stack position, but we want it to |
| 22 | // grow down from 4 gb so we will pretend that the first task has its |
| 23 | // stack at exactly 4gb and work from there. Because the new stack will |
| 24 | // be mapped to any random frame, it doesn't actually matter where we |
| 25 | // put it, we just want somewhere that won't collide with any user space |
| 26 | // stuff or our heap. |
| 27 | .last_stack_pos = 0xFFFFF000, |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 28 | }; |
| 29 | strcpy(processes[0].name, "kernel"); |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 30 | kprintf("in _init_tasks, strlen of 'kernel' is %d\n", strlen("kernel")); |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 31 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 32 | first_task = last_task = current_task = malloc(sizeof(struct ll_task_i)); |
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->next = NULL; |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 35 | first_task->prev = NULL; |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 36 | first_task->task = (struct task){ |
| 37 | .proc = &processes[0], |
| 38 | .esp = kernel_esp, |
| 39 | .ebp = kernel_ebp, |
| 40 | .eip = kernel_eip, |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 41 | .id = next_task_id++, |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 42 | }; |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 43 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 44 | kprintf("Returning from _init_tasks\n"); |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 45 | } |
| 46 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 47 | struct process *get_process(uint pid) |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 48 | { |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 49 | if (pid < 1024) |
| 50 | return &processes[pid]; |
| 51 | else |
| 52 | return NULL; |
| 53 | } |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 54 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 55 | int get_task_id() |
| 56 | { |
| 57 | return current_task->task.id; |
| 58 | } |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 59 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 60 | int get_process_id() |
| 61 | { |
| 62 | return current_task->task.proc->id; |
| 63 | } |
| 64 | |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 65 | void spawn_thread(void (*function)(void *), void *data) |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 66 | { |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 67 | asm volatile("cli"); |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 68 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 69 | struct process *proc = current_task->task.proc; |
| 70 | // Virtual address of page directory (in kernel memory) |
| 71 | uint *dir_v = PHYS_TO_VIRT(proc->page_directory_p); |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 72 | |
| 73 | // Virtual location of new stack, with space reserved for argument and |
| 74 | // return address to kill_this_thread(). |
| 75 | uint new_stack_base_v = proc->last_stack_pos; |
swissChili | c496cd7 | 2021-04-04 09:58:38 -0700 | [diff] [blame] | 76 | proc->last_stack_pos -= 0x1000; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 77 | |
| 78 | // Alloc a new page in the current process mapping to the new stack |
swissChili | c496cd7 | 2021-04-04 09:58:38 -0700 | [diff] [blame] | 79 | alloc_page(dir_v, (void *)proc->last_stack_pos); |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 80 | |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 81 | new_stack_base_v -= sizeof(uint); |
| 82 | *((uint *)new_stack_base_v) = (size_t)data; |
| 83 | new_stack_base_v -= sizeof(uint); |
| 84 | *((uint *)new_stack_base_v) = (size_t)&kill_this_thread; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 85 | |
| 86 | struct ll_task_i *ll_task = malloc(sizeof(struct ll_task_i)); |
| 87 | memset(ll_task, 0, sizeof(struct ll_task_i)); |
| 88 | struct task *task = &ll_task->task; |
| 89 | |
| 90 | task->proc = proc; |
| 91 | task->id = next_task_id++; |
| 92 | task->ebp = task->esp = new_stack_base_v; |
| 93 | task->eip = (uint)function; |
| 94 | |
| 95 | last_task->next = ll_task; |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 96 | ll_task->prev = last_task; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 97 | last_task = ll_task; |
| 98 | |
| 99 | asm volatile("sti"); |
| 100 | } |
| 101 | |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 102 | void kill_this_thread() |
| 103 | { |
| 104 | asm volatile("cli"); |
| 105 | |
| 106 | if (current_task->prev == NULL && current_task->next == NULL) |
| 107 | { |
| 108 | kpanic("You may not kill the last task in the kernel"); |
| 109 | } |
| 110 | |
| 111 | struct ll_task_i *task = first_task, |
| 112 | *old_task = current_task; |
| 113 | |
| 114 | if (current_task->prev != NULL) |
| 115 | current_task->prev->next = current_task->next; |
| 116 | |
| 117 | if (current_task->next != NULL) |
| 118 | { |
| 119 | // If this is NULL, task will be first_task, which can't be the current task |
| 120 | // because we know there are more than one task, and this is the last one. |
| 121 | current_task->next->prev = current_task->prev; |
| 122 | task = current_task->next; |
| 123 | } |
| 124 | |
| 125 | if (current_task == first_task) |
| 126 | first_task = current_task->next; |
| 127 | |
| 128 | if (current_task == last_task) |
| 129 | last_task = current_task->prev; |
| 130 | |
| 131 | current_task = task; |
| 132 | free(old_task); |
| 133 | |
| 134 | switch_to_task(¤t_task->task); |
| 135 | |
| 136 | asm volatile("sti"); |
| 137 | } |
| 138 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 139 | extern void _switch_to_task(uint page_directory, uint eip, uint ebp, uint esp); |
| 140 | |
| 141 | void switch_to_task(struct task *task) |
| 142 | { |
| 143 | _switch_to_task(task->proc->page_directory_p, task->eip, task->ebp, |
| 144 | task->esp); |
| 145 | __builtin_unreachable(); |
| 146 | } |
| 147 | |
| 148 | // WARNING: do not call this manually, it will clobber everything |
| 149 | // except esp, ebp, and eip (obviously). Use switch_task in task_api.s |
| 150 | // instead. |
| 151 | void _do_switch_task(uint eip, uint ebp, uint esp) |
| 152 | { |
| 153 | // sti is called in switch_to_task |
| 154 | asm volatile("cli"); |
| 155 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 156 | // save context for this task |
| 157 | current_task->task.ebp = ebp; |
| 158 | current_task->task.esp = esp; |
| 159 | current_task->task.eip = eip; |
| 160 | |
| 161 | if (current_task->next == NULL) |
| 162 | { |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 163 | current_task = first_task; |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | // Continue the next task |
| 168 | current_task = current_task->next; |
| 169 | } |
| 170 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 171 | switch_to_task(¤t_task->task); |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 172 | } |