blob: 312618013f82fd58832f3a59289344ef21247d9c [file] [log] [blame]
swissChilie20b79b2021-03-17 21:20:13 -07001#pragma once
2
3#include "kint.h"
swissChilie9289ee2021-03-20 21:54:28 -07004#include "registers.h"
swissChilie20b79b2021-03-17 21:20:13 -07005
swissChiliaed6ff32021-05-29 17:51:04 -07006#define INIT_TASKS_INTERRUPT 0x81
7
8extern bool tasks_initialized;
9
swissChilie5adca52021-06-16 21:00:31 -070010/**
11 * A process. For now there is only one, the kernel.
12 */
swissChilie9289ee2021-03-20 21:54:28 -070013struct process
14{
15 bool exists;
swissChilicfd3c3c2021-04-03 15:04:24 -070016 int id; // kernel uses pid 0, which cannot exit
swissChilie9289ee2021-03-20 21:54:28 -070017 int ring;
18 int uid;
19 char name[32];
swissChilicfd3c3c2021-04-03 15:04:24 -070020 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;
swissChilie9289ee2021-03-20 21:54:28 -070025};
swissChilib58ab672022-01-17 21:18:01 -080026
swissChilie5adca52021-06-16 21:00:31 -070027/**
28 * The smallest schedulable unit, a thread of a process.
29 */
swissChilie9289ee2021-03-20 21:54:28 -070030struct task
31{
32 int id;
swissChili52a03d82021-07-18 15:22:14 -070033 /// Is this task waiting for I/O?
34 bool waiting;
swissChilie9289ee2021-03-20 21:54:28 -070035 struct process *proc;
swissChilie5adca52021-06-16 21:00:31 -070036 /// Physical address of the top of the stack.
37 uint stack_top_p;
swissChili1e8b7562021-12-22 21:22:57 -080038 struct registers state;
swissChilie9289ee2021-03-20 21:54:28 -070039};
40
41struct ll_task_i
42{
swissChilif01ddcc2021-04-04 11:31:34 -070043 struct ll_task_i *next, *prev;
swissChilie9289ee2021-03-20 21:54:28 -070044 struct task task;
45};
46
47// extern struct process processes[1024];
48// extern struct ll_task_i *first_task, *current_task;
49
swissChiliaed6ff32021-05-29 17:51:04 -070050// Note: interrupts must be enabled BEFORE this for it to work.
51void init_tasks();
swissChilie9289ee2021-03-20 21:54:28 -070052struct process *get_process(uint pid);
53
54int get_process_id();
55int get_task_id();
56
57// For compatibility I guess
58#define getpid get_process_id
59
swissChili1b839222021-06-03 13:54:40 -070060typedef void (*task_function_t)(void *data);
61
62#define TASK_FUNCTION(f) ((task_function_t)(f))
63
64void spawn_thread(task_function_t function, void *data);
swissChilie5adca52021-06-16 21:00:31 -070065
swissChili52a03d82021-07-18 15:22:14 -070066void set_waiting(int tid, bool waiting);
67
swissChilie5adca52021-06-16 21:00:31 -070068/**
69 * Halt the current thread.
70 */
swissChilif01ddcc2021-04-04 11:31:34 -070071void kill_this_thread();
swissChilie5adca52021-06-16 21:00:31 -070072
73/**
swissChili1e8b7562021-12-22 21:22:57 -080074 * Force a task switch
75 * @param ctx The context to switch out of
swissChilie5adca52021-06-16 21:00:31 -070076 */
swissChili1e8b7562021-12-22 21:22:57 -080077void switch_task(struct registers ctx);
swissChilie5adca52021-06-16 21:00:31 -070078
79/**
80 * Switch to a specific task. Only call in a safe environment (ISR).
81 */
swissChilif01ddcc2021-04-04 11:31:34 -070082void switch_to_task(struct task *task);
swissChilie5adca52021-06-16 21:00:31 -070083
84/**
85 * Internal. Do not call.
86 */
swissChiliaed6ff32021-05-29 17:51:04 -070087void _sys_init_tasks_h(struct registers *regs);