/* Copyright (C) 2021 Rain */
/* This file is part of XNIX. */
/*
XNIX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) and later version.
*/
/*
XNIX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
/*
You should have received a copy of the GNU General Public License
along with XNIX. If not, see .
*/
#ifndef _SCHED_H
#define _SCHED_H
#include
#include
#include
#include
/*
* switch_to(next): switch to task next.
*
* STEPS:
* save that registers that will use.
* call __do
* do: set data segments.
* return;
*
* return to next task.
*
* NOTES:
* we don't need to restore / save registers, because
* tasks restored them.
*
* this function may called from an interrupt-handler
* (timer IRQ), so we must sti first, else tasks won't
* switch again.
*
* __do maybe changes some registers, like %rbp
* (maybe doesn't), so we also should save/restore them.
* */
#define switch_to(next) \
__asm__ __volatile__ ( \
"cli\n\t" \
"pushq %%rbp\n\t" \
"pushq %%rax\n\t" \
"movq %%rsp, %0\n\t" \
"movq %2, %%rsp\n\t" \
"leaq goback(%%rip), %%rax\n\t" \
/* fake return address */ \
"pushq %3\n\t" \
"jmp __do\n\t" \
"__do:\n\t" \
"movw %4, %%ds\n\t" \
"movw %4, %%es\n\t" \
"movw %4, %%fs\n\t" \
"movw %4, %%gs\n\t" \
"sti\n\t" \
"retq\n\t" \
"goback:\n\t" \
"popq %%rax\n\t" \
"popq %%rbp\n\t" \
: "=m" (current->rsp), \
"=m" (current->rip) \
: "m" ((next)->rsp), \
"m" ((next)->rip), \
"r" ((next)->dataseg) \
)
/* standard run time for user tasks */
/* 1 jiffy = 10ms */
#define USER_STD_TIME 1
struct task {
/* died */
#define STATE_ZOMBIE 0
/* it's running on cpu */
#define STATE_RUNNING 1
/* waiting, but unlike STATE_UNINTERRUPTIBLE, you can wake_up() */
#define STATE_INTERRUPTIBLE 2
/* waiting, and can't wake_up() */
#define STATE_UNINTERRUPTIABLE 3
/* you can schedule this task any time */
#define STATE_READY 4
__uint32_t state;
/* total run time in kernel space (jiffies) */
__uint32_t ktime;
/* total run time in user space (0 in kernel tasks) */
__uint32_t utime;
/*
* 0 - 16
* 8 is the default priorty for all user tasks,
* 0 is for all kernel tasks.
* */
__uint32_t priority;
/* signal (a small bitmap) */
__uint32_t signal;
/* mask of signal (can be set by signal() or sigaction(), but SIGKILL) */
__uint32_t signal_mask;
int exit_code;
#define INIT_PID 0
/* pid and his father's pid */
__uint32_t pid, father;
/* point to its tss */
struct tss *tss;
__uint64_t rsp0;
__uint64_t rsp;
__uint64_t rip;
__uint64_t cr3;
__uint16_t dataseg, codeseg;
struct task *next;
};
extern __uint32_t next_pid;
extern struct task init_task;
/* unlike linux we use a pointer to save current task */
extern struct task *current;
extern struct task *task_queues[16];
int task_init(void);
void set_tss(struct tss *, __uint64_t);
#define ltr(nr) __asm__ __volatile__ ("ltr %%ax" :: "a" (nr))
#endif