sched: add kernel-mode context switching

This commit is contained in:
2023-04-30 14:27:57 +01:00
parent 2cb2d9100a
commit 085c3d2a89
12 changed files with 340 additions and 27 deletions

View File

@@ -10,6 +10,7 @@
#define TASK_NAME_MAX 64
#define PRIO_MAX 32
#define THREAD_KSTACK_ORDER VM_PAGE_4K
#ifdef __cplusplus
extern "C" {
@@ -54,8 +55,8 @@ struct task {
};
struct thread {
enum thread_state tr_state : 8;
enum thread_flags tr_flags : 8;
enum thread_state tr_state;
enum thread_flags tr_flags;
struct task *tr_parent;
unsigned int tr_id;
@@ -65,15 +66,20 @@ struct thread {
cycles_t tr_quantum_cycles, tr_quantum_target;
cycles_t tr_total_cycles;
uintptr_t tr_sp, tr_bp;
struct queue_entry tr_threads;
struct queue_entry tr_rqentry;
void *tr_kstack;
struct vm_page *tr_kstack;
};
struct runqueue {
struct queue rq_queues[PRIO_MAX];
uint32_t rq_readybits;
spin_lock_t rq_lock;
struct thread *rq_cur, *rq_idle;
};
extern kern_status_t sched_init(void);
@@ -81,13 +87,24 @@ extern void schedule(void);
extern void preempt_disable(void);
extern void preempt_enable(void);
extern void runqueue_init(struct runqueue *rq);
extern void rq_init(struct runqueue *rq);
extern struct thread *rq_dequeue(struct runqueue *rq);
extern void rq_enqueue(struct runqueue *rq, struct thread *thr);
static inline void rq_lock(struct runqueue *rq, unsigned long *flags)
{
spin_lock_irqsave(&rq->rq_lock, flags);
}
static inline void rq_unlock(struct runqueue *rq, unsigned long flags)
{
spin_unlock_irqrestore(&rq->rq_lock, flags);
}
extern struct task *task_alloc(void);
static inline struct task *task_ref(struct task *task) { return (struct task *)object_data(object_ref(object_header(task))); }
static inline void task_deref(struct task *task) { object_deref(object_header(task)); }
extern struct task *task_from_pid(unsigned int pid);
extern struct task *kernel_task(void);
extern struct task *idle_task(void);
extern cycles_t default_quantum(void);
extern bool need_resched(void);
@@ -108,6 +125,8 @@ static inline void task_unlock_irqrestore(struct task *task, unsigned long flags
}
extern struct thread *thread_alloc(void);
extern kern_status_t thread_init(struct thread *thr, uintptr_t ip);
extern int thread_priority(struct thread *thr);
#ifdef __cplusplus
}