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