Files
KatauOS/include/task.h
T

84 lines
2.3 KiB
C

#ifndef TASK_H
#define TASK_H
#include <stdint.h>
#include "../include/fat.h"
// Сегменты для ядра и пользователя
#define SEG_KCODE 0x08 // Сегмент кода ядра
#define SEG_KDATA 0x10 // Сегмент данных ядра
#define SEG_UCODE 0x18 // Сегмент кода пользователя
#define SEG_UDATA 0x20 // Сегмент данных пользователя
#define DPL_KERNEL 0 // Уровень привилегий ring 0
#define DPL_USER 3 // Уровень привилегий ring 3
// Флаги процессора
#define FL_IF 0x202 // Разрешить прерывания
#define MAX_OPEN_FILES 32
#define KSTACKSIZE 4096
typedef enum TaskState
{
Ready,
Running,
Waiting,
Terminated
} TaskState;
typedef void (*EntryPoint)(void);
// Структура контекста для переключения между процессами
typedef struct __attribute__((packed)) Context {
uint32_t edi;
uint32_t esi;
uint32_t ebx;
uint32_t ebp;
uint32_t eip;
} Context;
// Структура trap frame для перехода в ring 3
typedef struct __attribute__((packed)) TrapFrame {
uint32_t gs,fs,es,ds;
uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax;
uint32_t interrupt, error;
uint32_t eip, cs, eflags, usermode_esp, usermode_ss;
} TrapFrame;
typedef struct Process
{
uint32_t pid; // Process ID
uint32_t kesp;
uint32_t kesp_bottom;
uint32_t *pagedir;
TaskState state; // Состояние процесса
char* kstack; // Стек ядра
Context* context; // Контекст для переключения
TrapFrame* tf; // Trap frame (только для ring 3)
uint32_t ring; // Уровень привилегий (0 или 3)
struct Process* next; // Следующий процесс
file_t* file_descriptors[MAX_OPEN_FILES];
dir_t* cwd;
} Process;
extern Process* current;
extern uint32_t pid_counter;
extern Process* queue;
Process* task_create(uint32_t func, uint32_t user_esp, uint32_t ring, uint32_t* pagedir);
void task_kill(Process* proc);
void scheduler_init();
void schedule();
void scheduler_lock();
void scheduler_unlock();
#endif