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; |
| 35 | first_task->task = (struct task){ |
| 36 | .proc = &processes[0], |
| 37 | .esp = kernel_esp, |
| 38 | .ebp = kernel_ebp, |
| 39 | .eip = kernel_eip, |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 40 | .id = next_task_id++, |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 41 | }; |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 42 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 43 | kprintf("Returning from _init_tasks\n"); |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 44 | } |
| 45 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 46 | struct process *get_process(uint pid) |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 47 | { |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 48 | if (pid < 1024) |
| 49 | return &processes[pid]; |
| 50 | else |
| 51 | return NULL; |
| 52 | } |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 53 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 54 | int get_task_id() |
| 55 | { |
| 56 | return current_task->task.id; |
| 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_process_id() |
| 60 | { |
| 61 | return current_task->task.proc->id; |
| 62 | } |
| 63 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 64 | void spawn_thread(void (*function)()) |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 65 | { |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 66 | asm volatile("cli"); |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 67 | |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 68 | struct process *proc = current_task->task.proc; |
| 69 | // Virtual address of page directory (in kernel memory) |
| 70 | uint *dir_v = PHYS_TO_VIRT(proc->page_directory_p); |
| 71 | // Virtual location of new stack |
swissChili | c496cd7 | 2021-04-04 09:58:38 -0700 | [diff] [blame] | 72 | uint new_stack_base_v = proc->last_stack_pos - sizeof(uint); |
| 73 | proc->last_stack_pos -= 0x1000; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 74 | |
| 75 | // Alloc a new page in the current process mapping to the new stack |
swissChili | c496cd7 | 2021-04-04 09:58:38 -0700 | [diff] [blame] | 76 | alloc_page(dir_v, (void *)proc->last_stack_pos); |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 77 | |
| 78 | // <TEST>: see if we can assign to the new stack memory (ie: did mapping succeed) |
| 79 | uint *base = (uint *)new_stack_base_v; |
| 80 | |
| 81 | kprintf("base = 0x%x\n", base); |
| 82 | |
| 83 | *base = 0; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 84 | // </TEST> |
| 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; |
| 96 | last_task = ll_task; |
| 97 | |
| 98 | asm volatile("sti"); |
| 99 | } |
| 100 | |
| 101 | extern void _switch_to_task(uint page_directory, uint eip, uint ebp, uint esp); |
| 102 | |
| 103 | void switch_to_task(struct task *task) |
| 104 | { |
| 105 | _switch_to_task(task->proc->page_directory_p, task->eip, task->ebp, |
| 106 | task->esp); |
| 107 | __builtin_unreachable(); |
| 108 | } |
| 109 | |
| 110 | // WARNING: do not call this manually, it will clobber everything |
| 111 | // except esp, ebp, and eip (obviously). Use switch_task in task_api.s |
| 112 | // instead. |
| 113 | void _do_switch_task(uint eip, uint ebp, uint esp) |
| 114 | { |
| 115 | // sti is called in switch_to_task |
| 116 | asm volatile("cli"); |
| 117 | |
| 118 | kprintf("\nin _do_switch_task(%d, %d, %d)\n", eip, ebp, esp); |
| 119 | |
| 120 | // save context for this task |
| 121 | current_task->task.ebp = ebp; |
| 122 | current_task->task.esp = esp; |
| 123 | current_task->task.eip = eip; |
| 124 | |
| 125 | if (current_task->next == NULL) |
| 126 | { |
| 127 | // Start from the first task if there are more tasks, or just return |
| 128 | if (current_task == first_task) |
| 129 | return; // No context switch necessary |
| 130 | |
| 131 | current_task = first_task; |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | // Continue the next task |
| 136 | current_task = current_task->next; |
| 137 | } |
| 138 | |
| 139 | kprintf("Will switch to task id %d\n", current_task->task.id); |
| 140 | |
| 141 | switch_to_task(¤t_task->task); |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 142 | } |