swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "kint.h" |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 4 | #include "registers.h" |
swissChili | e20b79b | 2021-03-17 21:20:13 -0700 | [diff] [blame] | 5 | |
swissChili | aed6ff3 | 2021-05-29 17:51:04 -0700 | [diff] [blame] | 6 | #define INIT_TASKS_INTERRUPT 0x81 |
| 7 | |
| 8 | extern bool tasks_initialized; |
| 9 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 10 | /** |
| 11 | * A process. For now there is only one, the kernel. |
| 12 | */ |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 13 | struct process |
| 14 | { |
| 15 | bool exists; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 16 | int id; // kernel uses pid 0, which cannot exit |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 17 | int ring; |
| 18 | int uid; |
| 19 | char name[32]; |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 20 | uint page_directory_p; |
| 21 | // most recent (bottom) stack used by a task, next |
| 22 | // stack should be under this one. |
| 23 | // NOTE: must be PAGE ALIGNED |
| 24 | uint last_stack_pos; |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 25 | }; |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 26 | /** |
| 27 | * The smallest schedulable unit, a thread of a process. |
| 28 | */ |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 29 | struct task |
| 30 | { |
| 31 | int id; |
| 32 | struct process *proc; |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 33 | /// Physical address of the top of the stack. |
| 34 | uint stack_top_p; |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 35 | uint esp, ebp, eip; |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | struct ll_task_i |
| 39 | { |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 40 | struct ll_task_i *next, *prev; |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 41 | struct task task; |
| 42 | }; |
| 43 | |
| 44 | // extern struct process processes[1024]; |
| 45 | // extern struct ll_task_i *first_task, *current_task; |
| 46 | |
swissChili | aed6ff3 | 2021-05-29 17:51:04 -0700 | [diff] [blame] | 47 | // Note: interrupts must be enabled BEFORE this for it to work. |
| 48 | void init_tasks(); |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 49 | struct process *get_process(uint pid); |
| 50 | |
| 51 | int get_process_id(); |
| 52 | int get_task_id(); |
| 53 | |
| 54 | // For compatibility I guess |
| 55 | #define getpid get_process_id |
| 56 | |
swissChili | 1b83922 | 2021-06-03 13:54:40 -0700 | [diff] [blame] | 57 | typedef void (*task_function_t)(void *data); |
| 58 | |
| 59 | #define TASK_FUNCTION(f) ((task_function_t)(f)) |
| 60 | |
| 61 | void spawn_thread(task_function_t function, void *data); |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Halt the current thread. |
| 65 | */ |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 66 | void kill_this_thread(); |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Force a task switch. Only call in a safe environment (ISR). |
| 70 | */ |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 71 | extern void switch_task(); |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Switch to a specific task. Only call in a safe environment (ISR). |
| 75 | */ |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 76 | void switch_to_task(struct task *task); |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * Internal. Do not call. |
| 80 | */ |
swissChili | aed6ff3 | 2021-05-29 17:51:04 -0700 | [diff] [blame] | 81 | void _sys_init_tasks_h(struct registers *regs); |