9os

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit d9aeebc9c944482cd9e40f7666f5f4226aa5c04d
parent 6a7b5188c8a42da909a5b03cd337e8bc6a32fc8c
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue,  8 Sep 2020 15:05:03 +0200

sched: First version of sched

This version only provided the basic definition of process
and the helper fnctions that can be used from other modules.

Change-Id: Ib32d319a7ef003dc94fa299e143c7773ea00d7e6

Diffstat:
Minclude/9os/9os.h | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
Msrc/kernel/Makefile | 5+++--
Asrc/kernel/sched.c | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 163 insertions(+), 33 deletions(-)

diff --git a/include/9os/9os.h b/include/9os/9os.h @@ -14,6 +14,10 @@ #define NELEM(tab) (sizeof(tab) / sizeof((tab)[0])) +#define NR_TASKS 16 +#define NR_PROCS 32 +#define NR_WINS 32 + #define LINELEN 80 #define PAGESIZE 4096 #define IENABLE 1 @@ -22,6 +26,7 @@ typedef struct context Context; typedef struct proc Proc; typedef struct task Task; +typedef struct win Win; enum regidx { X0, @@ -65,37 +70,37 @@ enum regidx { NR_REGS }; -enum tstates { - DORMANT, - READY, - RUNNING, - WAITING, - FAULTED, +enum pstates { + PSYSTEM = 1 << 0, }; -struct context { - unsigned long long r[NR_REGS]; +enum pmodes { + PDISABLED, + PCOLD_START, + PWARM_START, + PIDLE, + PNORMAL, }; -struct proc { - int pid; - char *name; - /* memory requirements */ - long period; - long duration; - /* communication ports */ - /* healt monitor table */ - long affinity; +enum tflags { + TLOCK_PREEMP = 1 << 0, + TENABLED = 1 << 1, +}; - void *entry; - long flags; - int locklvl; - int mode; - int start; +enum tstates { + TDORMANT, + TREADY, + TRUNNING, + TWAITING, + FAULTED, +}; -#define NR_TASKS 10 - Task *tasks[NR_TASKS]; - Task *errhdl; +enum start_reason { + SNORMAL, +}; + +struct context { + unsigned long long r[NR_REGS]; }; struct task { @@ -111,16 +116,39 @@ struct task { long period; long capacity; - int faultmode; long deadline; + long deadtime; + int state; - long affinity; + unsigned long affinity; + + int flags; Context ctx; }; -struct window { +struct proc { + int pid; + char *name; + /* memory requirements */ + long period; + long duration; + /* communication ports */ + /* healt monitor table */ + unsigned long affinity; + + void *entry; + long flags; + int locklevel; + int mode; + int start; + + Task tasks[NR_TASKS]; + Task *errhdl; +}; + +struct win { int wid; long offset; long duration; @@ -154,9 +182,22 @@ extern void lock(mutex_t *m); extern void unlock(mutex_t *m); extern int trylock(mutex_t *m); -/* dlang.c */ +/* sched.c */ +extern Proc *getproc(int); +extern Proc *getnproc(int); +extern Task *getntask(Proc *, int); +extern Task *gettask(Proc *, int); +extern void sched(void); + +/* globals */ +extern Proc proctab[]; +extern Win wintab[]; extern unsigned in_debug; extern jmp_buf dbgrecover; - -/* panic.c */ extern const char *const regnames[]; + +/* per cpu globals */ +extern Proc *proc; +extern Task *task; +extern int cpuid; +extern long long now; diff --git a/src/kernel/Makefile b/src/kernel/Makefile @@ -3,10 +3,11 @@ PROJECTDIR=../.. include $(PROJECTDIR)/scripts/rules.mk OBJS =\ + dlang.o\ + ecstr.o\ panic.o\ svc.o\ - ecstr.o\ - dlang.o\ + sched.o\ dev/builtin.o\ DIRS =\ diff --git a/src/kernel/sched.c b/src/kernel/sched.c @@ -0,0 +1,88 @@ +#include <9os/9os.h> + +Proc proctab[NR_PROCS] = { + [0] = { + .pid = 1, + .name = "init", + .period = 20, + .duration = 4, + .affinity = -1, + .mode = PCOLD_START, + }, + [1] = { + .pid = 2, + .name = "/bin/sh", + .period = 20, + .duration = 4, + .affinity = -1, + .mode = PCOLD_START, + }, +}; + +Win wintab[NR_WINS]; + +/* per cpu globals */ +Proc *proc; +Task *task; +long long now; +int cpuid; + +Proc * +getnproc(int n) +{ + Proc *pp; + + for (pp = proctab; pp < &proctab[NR_PROCS]; ++pp) { + if (pp->mode == PDISABLED) + continue; + if (n == 0) + return pp; + n--; + } + + return NULL; +} + +Proc * +getproc(int pid) +{ + Proc *pp; + + for (pp = proctab; pp < &proctab[NR_PROCS]; ++pp) { + if (pp->mode != PDISABLED && pp->pid == pid) + return pp; + } + + return NULL; +} + +Task * +getntask(Proc *pp, int n) +{ + Task *tp, *tasks = pp->tasks; + + for (tp = tasks; tp < &tasks[NR_TASKS]; ++tp) { + if ((tp->flags & TENABLED) == 0) + continue; + if (n == 0) + return tp; + n--; + } + + return NULL; +} + +Task * +gettask(Proc *pp, int tid) +{ + Task *tp, *tasks = pp->tasks; + + for (tp = tasks; tp < &tasks[NR_TASKS]; ++tp) { + if ((tp->flags & TENABLED) != 0) + continue; + if (tp->tid == tid) + return tp; + } + + return NULL; +}