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 | b58ab67 | 2022-01-17 21:18:01 -0800 | [diff] [blame] | 26 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 27 | /** |
| 28 | * The smallest schedulable unit, a thread of a process. |
| 29 | */ |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 30 | struct task |
| 31 | { |
| 32 | int id; |
swissChili | 52a03d8 | 2021-07-18 15:22:14 -0700 | [diff] [blame] | 33 | /// Is this task waiting for I/O? |
| 34 | bool waiting; |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 35 | struct process *proc; |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 36 | /// Physical address of the top of the stack. |
| 37 | uint stack_top_p; |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 38 | struct registers state; |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | struct ll_task_i |
| 42 | { |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 43 | struct ll_task_i *next, *prev; |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 44 | struct task task; |
| 45 | }; |
| 46 | |
| 47 | // extern struct process processes[1024]; |
| 48 | // extern struct ll_task_i *first_task, *current_task; |
| 49 | |
swissChili | aed6ff3 | 2021-05-29 17:51:04 -0700 | [diff] [blame] | 50 | // Note: interrupts must be enabled BEFORE this for it to work. |
| 51 | void init_tasks(); |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 52 | struct process *get_process(uint pid); |
| 53 | |
| 54 | int get_process_id(); |
| 55 | int get_task_id(); |
| 56 | |
| 57 | // For compatibility I guess |
| 58 | #define getpid get_process_id |
| 59 | |
swissChili | 1b83922 | 2021-06-03 13:54:40 -0700 | [diff] [blame] | 60 | typedef void (*task_function_t)(void *data); |
| 61 | |
| 62 | #define TASK_FUNCTION(f) ((task_function_t)(f)) |
| 63 | |
| 64 | void spawn_thread(task_function_t function, void *data); |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 65 | |
swissChili | 52a03d8 | 2021-07-18 15:22:14 -0700 | [diff] [blame] | 66 | void set_waiting(int tid, bool waiting); |
| 67 | |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Halt the current thread. |
| 70 | */ |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 71 | void kill_this_thread(); |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 74 | * Force a task switch |
| 75 | * @param ctx The context to switch out of |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 76 | */ |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 77 | void switch_task(struct registers ctx); |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Switch to a specific task. Only call in a safe environment (ISR). |
| 81 | */ |
swissChili | f01ddcc | 2021-04-04 11:31:34 -0700 | [diff] [blame] | 82 | void switch_to_task(struct task *task); |
swissChili | e5adca5 | 2021-06-16 21:00:31 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Internal. Do not call. |
| 86 | */ |
swissChili | aed6ff3 | 2021-05-29 17:51:04 -0700 | [diff] [blame] | 87 | void _sys_init_tasks_h(struct registers *regs); |