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