blob: d538e01b72e6481b9256bfdd0917e1d8b4ef3abf [file] [log] [blame]
swissChilie20b79b2021-03-17 21:20:13 -07001#include "task.h"
2#include "alloc.h"
3#include "io.h"
swissChilicfd3c3c2021-04-03 15:04:24 -07004#include "log.h"
swissChilie20b79b2021-03-17 21:20:13 -07005#include "paging.h"
swissChiliaed6ff32021-05-29 17:51:04 -07006#include "pic.h"
swissChilie20b79b2021-03-17 21:20:13 -07007
swissChilie9289ee2021-03-20 21:54:28 -07008struct process processes[1024] = {0};
9struct ll_task_i *first_task = NULL, *last_task = NULL, *current_task = NULL;
swissChilicfd3c3c2021-04-03 15:04:24 -070010static uint next_task_id = 0;
swissChilie9289ee2021-03-20 21:54:28 -070011
swissChiliaed6ff32021-05-29 17:51:04 -070012bool tasks_initialized = false;
13
swissChili1e8b7562021-12-22 21:22:57 -080014void _init_tasks(struct registers *regs);
swissChiliaed6ff32021-05-29 17:51:04 -070015
16void init_tasks()
17{
18 add_interrupt_handler(INIT_TASKS_INTERRUPT, _sys_init_tasks_h);
19
swissChiliee6d10d2021-05-29 18:05:16 -070020 asm("int $0x81");
swissChiliaed6ff32021-05-29 17:51:04 -070021}
22
23void _sys_init_tasks_h(struct registers *regs)
24{
swissChili1e8b7562021-12-22 21:22:57 -080025 _init_tasks(regs);
swissChiliaed6ff32021-05-29 17:51:04 -070026}
27
swissChili1e8b7562021-12-22 21:22:57 -080028void _init_tasks(struct registers *regs)
swissChilie20b79b2021-03-17 21:20:13 -070029{
swissChilie9289ee2021-03-20 21:54:28 -070030 processes[0] = (struct process){
31 .exists = true,
32 .id = 0,
33 .ring = 0,
34 .uid = 0,
swissChilicfd3c3c2021-04-03 15:04:24 -070035 .page_directory_p = VIRT_TO_PHYS(kernel_page_directory),
swissChili1e8b7562021-12-22 21:22:57 -080036 // 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.
swissChilicfd3c3c2021-04-03 15:04:24 -070043 .last_stack_pos = 0xFFFFF000,
swissChilie9289ee2021-03-20 21:54:28 -070044 };
45 strcpy(processes[0].name, "kernel");
swissChilie20b79b2021-03-17 21:20:13 -070046
swissChilie9289ee2021-03-20 21:54:28 -070047 first_task = last_task = current_task = malloc(sizeof(struct ll_task_i));
swissChilie20b79b2021-03-17 21:20:13 -070048
swissChilie9289ee2021-03-20 21:54:28 -070049 first_task->next = NULL;
swissChilif01ddcc2021-04-04 11:31:34 -070050 first_task->prev = NULL;
swissChili1e8b7562021-12-22 21:22:57 -080051 memset(&first_task->task, 0, sizeof(struct task));
swissChilie9289ee2021-03-20 21:54:28 -070052 first_task->task = (struct task){
53 .proc = &processes[0],
swissChili1e8b7562021-12-22 21:22:57 -080054 .state = *regs,
swissChilicfd3c3c2021-04-03 15:04:24 -070055 .id = next_task_id++,
swissChili52a03d82021-07-18 15:22:14 -070056 .waiting = false,
swissChilie9289ee2021-03-20 21:54:28 -070057 };
swissChilie20b79b2021-03-17 21:20:13 -070058
swissChiliaed6ff32021-05-29 17:51:04 -070059 tasks_initialized = true;
swissChilie20b79b2021-03-17 21:20:13 -070060}
61
swissChilie9289ee2021-03-20 21:54:28 -070062struct process *get_process(uint pid)
swissChilie20b79b2021-03-17 21:20:13 -070063{
swissChilie9289ee2021-03-20 21:54:28 -070064 if (pid < 1024)
65 return &processes[pid];
66 else
67 return NULL;
68}
swissChilie20b79b2021-03-17 21:20:13 -070069
swissChilie9289ee2021-03-20 21:54:28 -070070int get_task_id()
71{
72 return current_task->task.id;
73}
swissChilie20b79b2021-03-17 21:20:13 -070074
swissChilie9289ee2021-03-20 21:54:28 -070075int get_process_id()
76{
77 return current_task->task.proc->id;
78}
79
swissChilif01ddcc2021-04-04 11:31:34 -070080void spawn_thread(void (*function)(void *), void *data)
swissChilie9289ee2021-03-20 21:54:28 -070081{
swissChili402a3832021-05-29 21:41:31 -070082 asm("cli");
swissChilie9289ee2021-03-20 21:54:28 -070083
swissChilicfd3c3c2021-04-03 15:04:24 -070084 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);
swissChilif01ddcc2021-04-04 11:31:34 -070087
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;
swissChilic496cd72021-04-04 09:58:38 -070091 proc->last_stack_pos -= 0x1000;
swissChilicfd3c3c2021-04-03 15:04:24 -070092
93 // Alloc a new page in the current process mapping to the new stack
swissChilic496cd72021-04-04 09:58:38 -070094 alloc_page(dir_v, (void *)proc->last_stack_pos);
swissChilicfd3c3c2021-04-03 15:04:24 -070095
swissChilif01ddcc2021-04-04 11:31:34 -070096 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;
swissChilicfd3c3c2021-04-03 15:04:24 -0700100
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;
swissChili1e8b7562021-12-22 21:22:57 -0800104 // New task is basically the same as the old one but with just a
105 // few changes
106 *task = current_task->task;
swissChilicfd3c3c2021-04-03 15:04:24 -0700107
swissChili1e8b7562021-12-22 21:22:57 -0800108 // Namely a new TID
swissChilicfd3c3c2021-04-03 15:04:24 -0700109 task->id = next_task_id++;
swissChili1e8b7562021-12-22 21:22:57 -0800110 // And stack, frame, and instruction pointers
111 task->state.ebp = task->state.esp = new_stack_base_v;
112 task->state.eip = (uint)function;
swissChili52a03d82021-07-18 15:22:14 -0700113 task->waiting = false;
swissChilicfd3c3c2021-04-03 15:04:24 -0700114
115 last_task->next = ll_task;
swissChilif01ddcc2021-04-04 11:31:34 -0700116 ll_task->prev = last_task;
swissChilicfd3c3c2021-04-03 15:04:24 -0700117 last_task = ll_task;
118
swissChili402a3832021-05-29 21:41:31 -0700119 asm("sti");
swissChilicfd3c3c2021-04-03 15:04:24 -0700120}
121
swissChilif01ddcc2021-04-04 11:31:34 -0700122void kill_this_thread()
123{
swissChili402a3832021-05-29 21:41:31 -0700124 asm("cli");
swissChilif01ddcc2021-04-04 11:31:34 -0700125
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 {
swissChili1e8b7562021-12-22 21:22:57 -0800139 // 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.
swissChilif01ddcc2021-04-04 11:31:34 -0700142 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(&current_task->task);
156
swissChili402a3832021-05-29 21:41:31 -0700157 asm("sti");
swissChilif01ddcc2021-04-04 11:31:34 -0700158}
159
swissChili1e8b7562021-12-22 21:22:57 -0800160extern 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
swissChilicfd3c3c2021-04-03 15:04:24 -0700169
170void switch_to_task(struct task *task)
171{
swissChili1e8b7562021-12-22 21:22:57 -0800172 _switch_to_task(task->proc->page_directory_p, task->state);
swissChilicfd3c3c2021-04-03 15:04:24 -0700173 __builtin_unreachable();
174}
175
swissChili1e8b7562021-12-22 21:22:57 -0800176void _do_switch_task(struct registers regs)
swissChilicfd3c3c2021-04-03 15:04:24 -0700177{
178 // sti is called in switch_to_task
swissChili402a3832021-05-29 21:41:31 -0700179 asm("cli");
swissChilicfd3c3c2021-04-03 15:04:24 -0700180
swissChilicfd3c3c2021-04-03 15:04:24 -0700181 // save context for this task
swissChili1e8b7562021-12-22 21:22:57 -0800182 current_task->task.state = regs;
swissChilicfd3c3c2021-04-03 15:04:24 -0700183
swissChili52a03d82021-07-18 15:22:14 -0700184 struct ll_task_i *original = current_task;
185
186 do
swissChilicfd3c3c2021-04-03 15:04:24 -0700187 {
swissChili52a03d82021-07-18 15:22:14 -0700188 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 }
swissChilicfd3c3c2021-04-03 15:04:24 -0700197 }
swissChili52a03d82021-07-18 15:22:14 -0700198 while (current_task->task.waiting);
199
200 if (current_task == original && original->task.waiting)
swissChilicfd3c3c2021-04-03 15:04:24 -0700201 {
swissChili52a03d82021-07-18 15:22:14 -0700202 kpanic("All tasks are waiting for I/O. There must be at least 1 task using CPU at all times.");
swissChilicfd3c3c2021-04-03 15:04:24 -0700203 }
204
swissChilicfd3c3c2021-04-03 15:04:24 -0700205 switch_to_task(&current_task->task);
swissChilie20b79b2021-03-17 21:20:13 -0700206}
swissChili52a03d82021-07-18 15:22:14 -0700207
208void 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}
swissChili1e8b7562021-12-22 21:22:57 -0800223
224void switch_task(struct registers ctx)
225{
226 if (tasks_initialized)
227 _do_switch_task(ctx);
228}