AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
kthread.c
Go to the documentation of this file.
1
10
11#include <core/system.h>
12#include <core/arch.h>
13#include <core/panic.h>
14#include <core/string.h>
15#include <mm/vm.h>
16#include <sys/kthread.h>
17#include <sys/proc.h>
18#include <sys/sched.h>
19#include <ds/queue.h>
20
21MALLOC_DECLARE(M_PROC);
22
23struct proc *kproc;
24
27static int kproc_init(void)
28{
29 struct proc *proc = kmalloc(sizeof(struct proc), &M_PROC, M_ZERO);
30
31 if (!proc)
32 return -ENOMEM;
33
34 proc->pid = 0; /* never allocated: init is 1 */
35 proc->name = strdup("[kernel]");
36
37 if (!proc->name)
38 panic("no memory for process 0");
40 proc->running = 1;
41
42 for (int i = 0; i < SIG_MAX; ++i)
43 proc->sigaction[i].sa_handler = SIG_DFL;
44
45 /* the kernel can't boot without process 0 */
46 if (!proc->name || session_new(proc) || !enqueue(procs, proc))
47 panic("kthread: can't make process 0");
48
49 kproc = proc;
50
51 return 0;
52}
53
56static void kthread_start(void)
57{
58 curthread->kentry(curthread->karg);
59 panic("a kernel thread returned");
60}
61
71int kthread_create(const char *name, void (*fn)(void *), void *arg, struct thread **ref)
72{
73 int err;
74 struct thread *thread = NULL;
75
76 if (!kproc && (err = kproc_init()))
77 return err;
78
79 if ((err = thread_new(kproc, &thread)))
80 return err;
81
82 thread->name = name;
83 thread->kentry = fn;
84 thread->karg = arg;
85
87
88 if (!thread->arch) {
89 queue_remove(&kproc->threads, thread);
91 return -ENOMEM;
92 }
93
94 /* the scheduler switches to it as to any thread that was switched
95 * away from: its saved context is its start */
96 thread->spawned = 1;
99
100 if (ref)
101 *ref = thread;
102
103 return 0;
104}
void arch_kthread_create(struct thread *thread, uintptr_t entry)
Sets up a kernel thread's CPU state.
Definition thread.c:113
#define panic(s)
Definition panic.h:15
static void kthread_start(void)
Runs a kernel thread's function: every kernel thread starts here, on its own stack (arch_kthread_crea...
Definition kthread.c:56
static int kproc_init(void)
Makes process 0, the kernel threads' process, the first time a kernel thread is made.
Definition kthread.c:27
int kthread_create(const char *name, void(*fn)(void *), void *arg, struct thread **ref)
Makes a kernel thread, and makes it ready to run.
Definition kthread.c:71
struct proc * kproc
Definition kthread.c:23
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
Definition kvmem.c:123
#define MALLOC_DECLARE(type)
Definition kvmem.h:29
#define M_ZERO
Definition kvmem.h:26
void kfree(void *)
Frees an object that kmalloc() gave.
Definition kvmem.c:146
int session_new(struct proc *proc)
Makes a new session, with a new process group, both led by a process and with its ID,...
Definition proc.c:596
struct queue * procs
Definition proc.c:37
#define curthread
Definition sched.h:12
void sched_thread_ready(struct thread *thread)
Puts a thread at the end of the ready list, to run in its turn.
Definition sched.c:298
#define SIG_MAX
Definition signal.h:16
#define SIG_DFL
Definition signal.h:26
#define NULL
Definition stddef.h:27
__SIZE_TYPE__ uintptr_t
Definition stddef.h:12
A process.
Definition proc.h:75
pid_t pid
the process's ID
Definition proc.h:77
struct aquila_sigaction sigaction[SIG_MAX+1]
its signal actions
Definition proc.h:157
struct vm_space vm_space
its address space
Definition proc.h:129
int running
whether it's running
Definition proc.h:163
char * name
its name: the program it runs
Definition proc.h:84
struct queue threads
its threads
Definition proc.h:136
A thread.
Definition thread.h:23
state_t state
its state
Definition thread.h:28
void(*) kentry(void *arg)
Definition thread.h:35
void * karg
Definition thread.h:36
int spawned
whether it has run yet
Definition thread.h:87
const char * name
A kernel thread's (process 0's): its name, and what it runs.
Definition thread.h:34
void * arch
the architecture's own data: its registers, its kernel stack
Definition thread.h:84
struct pmap * pmap
its page tables (the architecture's)
Definition vm.h:27
int thread_new(struct proc *proc, struct thread **rthread)
Makes a new thread in a process.
Definition thread.c:27
@ RUNNABLE
Definition thread.h:16
struct vm_space kvm_space
Definition vmm.c:17