AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
sched.c
Go to the documentation of this file.
1
9
10#include <core/arch.h>
11#include <core/time.h>
12#include <mm/mm.h>
13#include <sys/cpu.h>
14#include <sys/proc.h>
15#include <sys/sched.h>
16#include <sys/lock.h>
17#include <platform/virt.h>
18
21
22extern struct aarch64_regs *aarch64_irq_regs;
23void aarch64_preempt(void);
24
28static void sched_tick(void)
29{
31
32 /* set the wall clock from the real time clock again, every 2^16
33 * ticks */
34 if (timer_ticks && !(timer_ticks & 0xFFFF))
36
39 curcpu()->arch.tick_switch = 1;
40}
41
42/* Starts the timer tick, every 2 ms. */
44{
46}
47
51static void __attribute__((noreturn)) __arch_idle(void)
52{
53 if (kernel_lock_held())
55
56 for (;;)
57 asm volatile("msr daifclr, #2; wfi; msr daifset, #2" ::: "memory");
58}
59
60/* Idles the CPU until an interrupt makes a thread ready. It doesn't
61 * return. */
62void arch_idle(void)
63{
65 aarch64_goto((uintptr_t) __arch_idle, curcpu()->arch.idle_stack);
66}
67
69static void __arch_cur_thread_kill(void)
70{
71 /* a thread that called `thread_exit()` is a zombie already, and stays
72 * on its owner's thread list for `thread_join()`; otherwise its
73 * process is exiting, and `proc_kill()` took it off the list */
74 int keep = curthread->state == ZOMBIE;
75
77
78 if (!keep)
80
83}
84
85/* Ends the running thread. It is freed from the CPU's idle stack, since
86 * its own stack is freed with it. It doesn't return. */
88{
89 aarch64_goto((uintptr_t) __arch_cur_thread_kill, curcpu()->arch.idle_stack);
90}
91
92/* Puts the running thread at the end of the ready list, and runs the
93 * next one. */
94void arch_yield(void)
95{
97}
98
99/* Puts the running thread to sleep, and runs the next ready one. It
100 * returns once the thread is woken up and chosen again. */
101void arch_sleep(void)
102{
103 struct aarch64_thread *arch = curthread->arch;
104
105 if (!aarch64_ctx_save(&arch->ctx))
106 kernel_idle();
107}
int aarch64_ctx_save(struct aarch64_ctx *ctx)
void aarch64_goto(uintptr_t fn, uintptr_t sp)
#define REGS_USER(r)
Definition cpu.h:29
void arch_yield(void)
Puts the current thread at the end of the ready list, and runs the next ready thread.
Definition sched.c:94
static void sched_tick(void)
Runs the timer tick: charges the running process for it, and asks for a switch, which happens once th...
Definition sched.c:28
void arch_idle(void)
Makes this CPU idle, on its idle stack, until an interrupt makes a thread ready.
Definition sched.c:62
static uint64_t timer_period
Definition sched.c:19
static void __arch_idle(void)
Runs the idle loop, on the CPU's idle stack.
Definition sched.c:51
void arch_sleep(void)
Puts the current thread to sleep.
Definition sched.c:101
static void __arch_cur_thread_kill(void)
Frees the running thread, from the CPU's idle stack, and idles.
Definition sched.c:69
void arch_cur_thread_kill(void)
Ends the current thread.
Definition sched.c:87
void arch_sched_init(void)
Starts the timer tick on every CPU, and the clock where it needs to be started.
Definition sched.c:43
static uint64_t timer_ticks
Definition sched.c:20
void kfree(void *)
Frees an object that kmalloc() gave.
Definition kvmem.c:146
void kernel_unlock(void)
Lets go of the kernel lock, for the next CPU waiting for it.
Definition lock.c:46
int kernel_lock_held(void)
Says whether this CPU holds the kernel lock.
Definition lock.c:57
void kernel_idle(void)
Gives up the CPU when the current thread can't run anymore (it sleeps, or it exited): the next ready ...
Definition sched.c:332
#define curthread
Definition sched.h:12
void sched_timer_tick(uint32_t period, int user)
Runs the boot CPU's timer tick.
Definition sched.c:74
#define NULL
Definition stddef.h:27
__SIZE_TYPE__ uintptr_t
Definition stddef.h:12
unsigned long long int uint64_t
Definition stddef.h:23
What an exception saves on the kernel stack (cpu/vectors.S): the context it interrupted.
Definition cpu.h:12
A thread's aarch64 state (thread->arch).
Definition arch.h:25
struct aarch64_ctx ctx
Definition arch.h:29
int thread_kill(struct thread *thread)
Releases what a thread holds (its poll() registrations, its CPU state and kernel stack) and makes it ...
Definition thread.c:59
@ ZOMBIE
Definition thread.h:19
void time_resync(void)
Sets the wall clock from the real time clock again, if they are 2 seconds or more apart.
Definition time.c:54
struct aarch64_regs * aarch64_irq_regs
Definition trap.c:183
void aarch64_preempt(void)
Switches away from this CPU's running thread, which stays ready, to the next ready thread.
Definition trap.c:188
uint64_t timer_setup(uint64_t period_ns, void(*handler)(void))
Starts the tick.
Definition timer.c:37