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