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" |
swissChili | aed6ff3 | 2021-05-29 17:51:04 -0700 | [diff] [blame] | 6 | #include "pic.h" |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 7 | |
swissChili | 14d0b84 | 2023-01-01 02:22:44 -0500 | [diff] [blame] | 8 | static struct process processes[1024] = {0}; |
| 9 | static struct ll_task_i *first_task = NULL, *last_task = NULL, *current_task = NULL; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 10 | static uint next_task_id = 0; |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 11 | |
swissChili | aed6ff3 | 2021-05-29 17:51:04 -0700 | [diff] [blame] | 12 | bool tasks_initialized = false; |
| 13 | |
swissChili | aed6ff3 | 2021-05-29 17:51:04 -0700 | [diff] [blame] | 14 | void init_tasks() |
| 15 | { |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 16 | processes[0] = (struct process){ |
| 17 | .exists = true, |
| 18 | .id = 0, |
| 19 | .ring = 0, |
| 20 | .uid = 0, |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 21 | .page_directory_p = VIRT_TO_PHYS(kernel_page_directory), |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 22 | // Obviously this isn't the actual stack position, but we want |
| 23 | // it to grow down from 4 gb so we will pretend that the first |
| 24 | // task has its stack at exactly 4gb and work from |
| 25 | // there. Because the new stack will be mapped to any random |
| 26 | // frame, it doesn't actually matter where we put it, we just |
| 27 | // want somewhere that won't collide with any user space stuff |
| 28 | // or our heap. |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 29 | .last_stack_pos = 0xFFFFF000, |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 30 | }; |
| 31 | strcpy(processes[0].name, "kernel"); |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 32 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 33 | first_task = last_task = current_task = malloc(sizeof(struct ll_task_i)); |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 34 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 35 | first_task->next = NULL; |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 36 | first_task->prev = NULL; |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 37 | memset(&first_task->task, 0, sizeof(struct task)); |
swissChili | 14d0b84 | 2023-01-01 02:22:44 -0500 | [diff] [blame] | 38 | // state will be filled in upon task switch. TODO: task switch |
| 39 | // before spawning thread (or fill in CS, etc manually) to avoid |
| 40 | // #GP on task switch. New threads based on parent task |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 41 | first_task->task = (struct task){ |
| 42 | .proc = &processes[0], |
swissChili | 14d0b84 | 2023-01-01 02:22:44 -0500 | [diff] [blame] | 43 | .state = { 0 }, |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 44 | .id = next_task_id++, |
swissChili | 52a03d8 | 2021-07-18 15:22:14 -0700 | [diff] [blame] | 45 | .waiting = false, |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 46 | }; |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 47 | |
swissChili | aed6ff3 | 2021-05-29 17:51:04 -0700 | [diff] [blame] | 48 | tasks_initialized = true; |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 49 | } |
| 50 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 51 | struct process *get_process(uint pid) |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 52 | { |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 53 | if (pid < 1024) |
| 54 | return &processes[pid]; |
| 55 | else |
| 56 | return NULL; |
| 57 | } |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 58 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 59 | int get_task_id() |
| 60 | { |
| 61 | return current_task->task.id; |
| 62 | } |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 63 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 64 | int get_process_id() |
| 65 | { |
| 66 | return current_task->task.proc->id; |
| 67 | } |
| 68 | |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 69 | void spawn_thread(void (*function)(void *), void *data) |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 70 | { |
swissChili | 402a383 | 2021-05-29 21:41:31 -0700 | [diff] [blame] | 71 | asm("cli"); |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 72 | |
swissChili | b58ab67 | 2022-01-17 21:18:01 -0800 | [diff] [blame] | 73 | kprintf(DEBUG "Spawning thread %p, data=%p\n", function, data); |
| 74 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 75 | struct process *proc = current_task->task.proc; |
| 76 | // Virtual address of page directory (in kernel memory) |
| 77 | uint *dir_v = PHYS_TO_VIRT(proc->page_directory_p); |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 78 | |
swissChili | d00ec61 | 2022-01-16 21:21:29 -0800 | [diff] [blame] | 79 | // Virtual location of new stack, with space reserved for argument |
| 80 | // and return address to kill_this_thread(). |
swissChili | 14d0b84 | 2023-01-01 02:22:44 -0500 | [diff] [blame] | 81 | uint *new_stack_base_v = (uint *)proc->last_stack_pos; |
swissChili | c496cd7 | 2021-04-04 09:58:38 -0700 | [diff] [blame] | 82 | proc->last_stack_pos -= 0x1000; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 83 | |
| 84 | // Alloc a new page in the current process mapping to the new stack |
swissChili | c496cd7 | 2021-04-04 09:58:38 -0700 | [diff] [blame] | 85 | alloc_page(dir_v, (void *)proc->last_stack_pos); |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 86 | |
swissChili | 14d0b84 | 2023-01-01 02:22:44 -0500 | [diff] [blame] | 87 | kprintf(INFO "new_stack_base_v = %p, last_stack_pos = %p\n", |
| 88 | new_stack_base_v, proc->last_stack_pos); |
| 89 | |
| 90 | // Write data argument and return pointer to new threads stack |
| 91 | new_stack_base_v[-1] = (size_t)data; |
| 92 | new_stack_base_v[-2] = (size_t)&kill_this_thread; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 93 | |
swissChili | b58ab67 | 2022-01-17 21:18:01 -0800 | [diff] [blame] | 94 | kprintf(DEBUG "Set stack\n"); |
| 95 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 96 | struct ll_task_i *ll_task = malloc(sizeof(struct ll_task_i)); |
| 97 | memset(ll_task, 0, sizeof(struct ll_task_i)); |
| 98 | struct task *task = &ll_task->task; |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 99 | // New task is basically the same as the old one but with just a |
| 100 | // few changes |
| 101 | *task = current_task->task; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 102 | |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 103 | // Namely a new TID |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 104 | task->id = next_task_id++; |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 105 | // And stack, frame, and instruction pointers |
swissChili | 14d0b84 | 2023-01-01 02:22:44 -0500 | [diff] [blame] | 106 | task->state.ebp = task->state.esp = (uint)(new_stack_base_v - 2); |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 107 | task->state.eip = (uint)function; |
swissChili | 52a03d8 | 2021-07-18 15:22:14 -0700 | [diff] [blame] | 108 | task->waiting = false; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 109 | |
| 110 | last_task->next = ll_task; |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 111 | ll_task->prev = last_task; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 112 | last_task = ll_task; |
| 113 | |
swissChili | 402a383 | 2021-05-29 21:41:31 -0700 | [diff] [blame] | 114 | asm("sti"); |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 115 | } |
| 116 | |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 117 | void kill_this_thread() |
| 118 | { |
swissChili | 402a383 | 2021-05-29 21:41:31 -0700 | [diff] [blame] | 119 | asm("cli"); |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 120 | |
| 121 | if (current_task->prev == NULL && current_task->next == NULL) |
| 122 | { |
| 123 | kpanic("You may not kill the last task in the kernel"); |
| 124 | } |
| 125 | |
| 126 | struct ll_task_i *task = first_task, |
| 127 | *old_task = current_task; |
| 128 | |
| 129 | if (current_task->prev != NULL) |
| 130 | current_task->prev->next = current_task->next; |
| 131 | |
| 132 | if (current_task->next != NULL) |
| 133 | { |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 134 | // If this is NULL, task will be first_task, which can't be |
| 135 | // the current task because we know there are more than one |
| 136 | // task, and this is the last one. |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 137 | current_task->next->prev = current_task->prev; |
| 138 | task = current_task->next; |
| 139 | } |
| 140 | |
| 141 | if (current_task == first_task) |
| 142 | first_task = current_task->next; |
| 143 | |
| 144 | if (current_task == last_task) |
| 145 | last_task = current_task->prev; |
| 146 | |
| 147 | current_task = task; |
| 148 | free(old_task); |
| 149 | |
| 150 | switch_to_task(¤t_task->task); |
| 151 | |
swissChili | 402a383 | 2021-05-29 21:41:31 -0700 | [diff] [blame] | 152 | asm("sti"); |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 153 | } |
| 154 | |
swissChili | 14d0b84 | 2023-01-01 02:22:44 -0500 | [diff] [blame] | 155 | extern uint _get_cr3(); |
| 156 | |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 157 | extern void _switch_to_task(uint page_directory, struct registers ctx); |
| 158 | #if 0 |
| 159 | { |
| 160 | asm("mov %0, %%ecx" :: "g"(page_directory)); |
| 161 | asm("mov %ecx, %cr3"); |
| 162 | // "ctx" will be at the top of the stack. |
| 163 | asm("iret"); |
| 164 | } |
| 165 | #endif |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 166 | |
| 167 | void switch_to_task(struct task *task) |
| 168 | { |
swissChili | 14d0b84 | 2023-01-01 02:22:44 -0500 | [diff] [blame] | 169 | kprintf(DEBUG "switch_to_task cr3=0x%x\n", task->proc->page_directory_p); |
| 170 | |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 171 | _switch_to_task(task->proc->page_directory_p, task->state); |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 172 | __builtin_unreachable(); |
| 173 | } |
| 174 | |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 175 | void _do_switch_task(struct registers regs) |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 176 | { |
swissChili | b58ab67 | 2022-01-17 21:18:01 -0800 | [diff] [blame] | 177 | // Resetting eflags in _switch_to_task iret will switch this back |
swissChili | 402a383 | 2021-05-29 21:41:31 -0700 | [diff] [blame] | 178 | asm("cli"); |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 179 | |
swissChili | 14d0b84 | 2023-01-01 02:22:44 -0500 | [diff] [blame] | 180 | kprintf(DEBUG "switching tasks, alleged cr3=0x%x, real cr3=0x%x\n", |
| 181 | current_task->task.proc->page_directory_p, |
| 182 | _get_cr3()); |
swissChili | b58ab67 | 2022-01-17 21:18:01 -0800 | [diff] [blame] | 183 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 184 | // save context for this task |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 185 | current_task->task.state = regs; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 186 | |
swissChili | 52a03d8 | 2021-07-18 15:22:14 -0700 | [diff] [blame] | 187 | struct ll_task_i *original = current_task; |
| 188 | |
| 189 | do |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 190 | { |
swissChili | 52a03d8 | 2021-07-18 15:22:14 -0700 | [diff] [blame] | 191 | if (current_task->next == NULL) |
| 192 | { |
| 193 | current_task = first_task; |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | // Continue the next task |
| 198 | current_task = current_task->next; |
| 199 | } |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 200 | } |
swissChili | 52a03d8 | 2021-07-18 15:22:14 -0700 | [diff] [blame] | 201 | while (current_task->task.waiting); |
| 202 | |
| 203 | if (current_task == original && original->task.waiting) |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 204 | { |
swissChili | 52a03d8 | 2021-07-18 15:22:14 -0700 | [diff] [blame] | 205 | kpanic("All tasks are waiting for I/O. There must be at least 1 task using CPU at all times."); |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 206 | } |
| 207 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 208 | switch_to_task(¤t_task->task); |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 209 | } |
swissChili | 52a03d8 | 2021-07-18 15:22:14 -0700 | [diff] [blame] | 210 | |
| 211 | void set_waiting(int tid, bool waiting) |
| 212 | { |
| 213 | asm("cli"); |
| 214 | |
| 215 | for (struct ll_task_i *t = first_task; t != NULL; t = t->next) |
| 216 | { |
| 217 | if (t->task.id == tid) |
| 218 | { |
| 219 | t->task.waiting = waiting; |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | asm("sti"); |
| 225 | } |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 226 | |
| 227 | void switch_task(struct registers ctx) |
| 228 | { |
| 229 | if (tasks_initialized) |
| 230 | _do_switch_task(ctx); |
| 231 | } |