blob: b0d0f973bf3698a08f64fe461fddb80862718e44 [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;
32 struct process *proc;
swissChilie5adca52021-06-16 21:00:31 -070033 /// Physical address of the top of the stack.
34 uint stack_top_p;
swissChilie9289ee2021-03-20 21:54:28 -070035 uint esp, ebp, eip;
swissChilie9289ee2021-03-20 21:54:28 -070036};
37
38struct ll_task_i
39{
swissChilif01ddcc2021-04-04 11:31:34 -070040 struct ll_task_i *next, *prev;
swissChilie9289ee2021-03-20 21:54:28 -070041 struct task task;
42};
43
44// extern struct process processes[1024];
45// extern struct ll_task_i *first_task, *current_task;
46
swissChiliaed6ff32021-05-29 17:51:04 -070047// Note: interrupts must be enabled BEFORE this for it to work.
48void init_tasks();
swissChilie9289ee2021-03-20 21:54:28 -070049struct process *get_process(uint pid);
50
51int get_process_id();
52int get_task_id();
53
54// For compatibility I guess
55#define getpid get_process_id
56
swissChili1b839222021-06-03 13:54:40 -070057typedef void (*task_function_t)(void *data);
58
59#define TASK_FUNCTION(f) ((task_function_t)(f))
60
61void spawn_thread(task_function_t function, void *data);
swissChilie5adca52021-06-16 21:00:31 -070062
63/**
64 * Halt the current thread.
65 */
swissChilif01ddcc2021-04-04 11:31:34 -070066void kill_this_thread();
swissChilie5adca52021-06-16 21:00:31 -070067
68/**
69 * Force a task switch. Only call in a safe environment (ISR).
70 */
swissChilicfd3c3c2021-04-03 15:04:24 -070071extern void switch_task();
swissChilie5adca52021-06-16 21:00:31 -070072
73/**
74 * Switch to a specific task. Only call in a safe environment (ISR).
75 */
swissChilif01ddcc2021-04-04 11:31:34 -070076void switch_to_task(struct task *task);
swissChilie5adca52021-06-16 21:00:31 -070077
78/**
79 * Internal. Do not call.
80 */
swissChiliaed6ff32021-05-29 17:51:04 -070081void _sys_init_tasks_h(struct registers *regs);