blob: 83e7fd01ef85f3178e3522ec0240f5b7833f8614 [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};
swissChilie5adca52021-06-16 21:00:31 -070026/**
27 * The smallest schedulable unit, a thread of a process.
28 */
swissChilie9289ee2021-03-20 21:54:28 -070029struct task
30{
31 int id;
swissChili52a03d82021-07-18 15:22:14 -070032 /// Is this task waiting for I/O?
33 bool waiting;
swissChilie9289ee2021-03-20 21:54:28 -070034 struct process *proc;
swissChilie5adca52021-06-16 21:00:31 -070035 /// Physical address of the top of the stack.
36 uint stack_top_p;
swissChilie9289ee2021-03-20 21:54:28 -070037 uint esp, ebp, eip;
swissChilie9289ee2021-03-20 21:54:28 -070038};
39
40struct ll_task_i
41{
swissChilif01ddcc2021-04-04 11:31:34 -070042 struct ll_task_i *next, *prev;
swissChilie9289ee2021-03-20 21:54:28 -070043 struct task task;
44};
45
46// extern struct process processes[1024];
47// extern struct ll_task_i *first_task, *current_task;
48
swissChiliaed6ff32021-05-29 17:51:04 -070049// Note: interrupts must be enabled BEFORE this for it to work.
50void init_tasks();
swissChilie9289ee2021-03-20 21:54:28 -070051struct process *get_process(uint pid);
52
53int get_process_id();
54int get_task_id();
55
56// For compatibility I guess
57#define getpid get_process_id
58
swissChili1b839222021-06-03 13:54:40 -070059typedef void (*task_function_t)(void *data);
60
61#define TASK_FUNCTION(f) ((task_function_t)(f))
62
63void spawn_thread(task_function_t function, void *data);
swissChilie5adca52021-06-16 21:00:31 -070064
swissChili52a03d82021-07-18 15:22:14 -070065void set_waiting(int tid, bool waiting);
66
swissChilie5adca52021-06-16 21:00:31 -070067/**
68 * Halt the current thread.
69 */
swissChilif01ddcc2021-04-04 11:31:34 -070070void kill_this_thread();
swissChilie5adca52021-06-16 21:00:31 -070071
72/**
73 * Force a task switch. Only call in a safe environment (ISR).
74 */
swissChilicfd3c3c2021-04-03 15:04:24 -070075extern void switch_task();
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);