blob: 2ed0958918a43271fd679c2a38ec3f6d6f59543e [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 -07006extern bool tasks_initialized;
7
swissChilie5adca52021-06-16 21:00:31 -07008/**
9 * A process. For now there is only one, the kernel.
10 */
swissChilie9289ee2021-03-20 21:54:28 -070011struct process
12{
13 bool exists;
swissChilicfd3c3c2021-04-03 15:04:24 -070014 int id; // kernel uses pid 0, which cannot exit
swissChilie9289ee2021-03-20 21:54:28 -070015 int ring;
16 int uid;
17 char name[32];
swissChilicfd3c3c2021-04-03 15:04:24 -070018 uint page_directory_p;
19 // most recent (bottom) stack used by a task, next
20 // stack should be under this one.
21 // NOTE: must be PAGE ALIGNED
22 uint last_stack_pos;
swissChilie9289ee2021-03-20 21:54:28 -070023};
swissChilib58ab672022-01-17 21:18:01 -080024
swissChilie5adca52021-06-16 21:00:31 -070025/**
26 * The smallest schedulable unit, a thread of a process.
27 */
swissChilie9289ee2021-03-20 21:54:28 -070028struct task
29{
30 int id;
swissChili52a03d82021-07-18 15:22:14 -070031 /// Is this task waiting for I/O?
32 bool waiting;
swissChilie9289ee2021-03-20 21:54:28 -070033 struct process *proc;
swissChilie5adca52021-06-16 21:00:31 -070034 /// Physical address of the top of the stack.
35 uint stack_top_p;
swissChili1e8b7562021-12-22 21:22:57 -080036 struct registers state;
swissChilie9289ee2021-03-20 21:54:28 -070037};
38
39struct ll_task_i
40{
swissChilif01ddcc2021-04-04 11:31:34 -070041 struct ll_task_i *next, *prev;
swissChilie9289ee2021-03-20 21:54:28 -070042 struct task task;
43};
44
45// extern struct process processes[1024];
46// extern struct ll_task_i *first_task, *current_task;
47
swissChiliaed6ff32021-05-29 17:51:04 -070048// Note: interrupts must be enabled BEFORE this for it to work.
49void init_tasks();
swissChilie9289ee2021-03-20 21:54:28 -070050struct process *get_process(uint pid);
51
52int get_process_id();
53int get_task_id();
54
55// For compatibility I guess
56#define getpid get_process_id
57
swissChili1b839222021-06-03 13:54:40 -070058typedef void (*task_function_t)(void *data);
59
60#define TASK_FUNCTION(f) ((task_function_t)(f))
61
62void spawn_thread(task_function_t function, void *data);
swissChilie5adca52021-06-16 21:00:31 -070063
swissChili52a03d82021-07-18 15:22:14 -070064void set_waiting(int tid, bool waiting);
65
swissChilie5adca52021-06-16 21:00:31 -070066/**
67 * Halt the current thread.
68 */
swissChilif01ddcc2021-04-04 11:31:34 -070069void kill_this_thread();
swissChilie5adca52021-06-16 21:00:31 -070070
71/**
swissChili1e8b7562021-12-22 21:22:57 -080072 * Force a task switch
73 * @param ctx The context to switch out of
swissChilie5adca52021-06-16 21:00:31 -070074 */
swissChili1e8b7562021-12-22 21:22:57 -080075void switch_task(struct registers ctx);
swissChilie5adca52021-06-16 21:00:31 -070076
77/**
78 * Switch to a specific task. Only call in a safe environment (ISR).
79 */
swissChilif01ddcc2021-04-04 11:31:34 -070080void switch_to_task(struct task *task);
swissChilie5adca52021-06-16 21:00:31 -070081
82/**
83 * Internal. Do not call.
84 */
swissChiliaed6ff32021-05-29 17:51:04 -070085void _sys_init_tasks_h(struct registers *regs);