AquilaOS documentation
How it works, and its code
Toggle main menu visibility
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
21
MALLOC_DECLARE
(M_PROC);
22
23
struct
proc
*
kproc
;
24
27
static
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"
);
39
proc
->
vm_space
.
pmap
=
kvm_space
.
pmap
;
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
56
static
void
kthread_start
(
void
)
57
{
58
curthread
->kentry(
curthread
->karg);
59
panic
(
"a kernel thread returned"
);
60
}
61
71
int
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
86
arch_kthread_create
(
thread
, (
uintptr_t
)
kthread_start
);
87
88
if
(!
thread
->
arch
) {
89
queue_remove(&
kproc
->
threads
,
thread
);
90
kfree
(
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;
97
thread
->
state
=
RUNNABLE
;
98
sched_thread_ready
(
thread
);
99
100
if
(ref)
101
*ref =
thread
;
102
103
return
0;
104
}
arch_kthread_create
void arch_kthread_create(struct thread *thread, uintptr_t entry)
Sets up a kernel thread's CPU state.
Definition
thread.c:113
panic
#define panic(s)
Definition
panic.h:15
arch.h
panic.h
system.h
kthread_start
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
kproc_init
static int kproc_init(void)
Makes process 0, the kernel threads' process, the first time a kernel thread is made.
Definition
kthread.c:27
kthread.h
kthread_create
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
kproc
struct proc * kproc
Definition
kthread.c:23
kmalloc
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
Definition
kvmem.c:123
MALLOC_DECLARE
#define MALLOC_DECLARE(type)
Definition
kvmem.h:29
M_ZERO
#define M_ZERO
Definition
kvmem.h:26
kfree
void kfree(void *)
Frees an object that kmalloc() gave.
Definition
kvmem.c:146
proc.h
session_new
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
procs
struct queue * procs
Definition
proc.c:37
queue.h
sched.h
curthread
#define curthread
Definition
sched.h:12
sched_thread_ready
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
SIG_MAX
#define SIG_MAX
Definition
signal.h:16
SIG_DFL
#define SIG_DFL
Definition
signal.h:26
NULL
#define NULL
Definition
stddef.h:27
uintptr_t
__SIZE_TYPE__ uintptr_t
Definition
stddef.h:12
string.h
proc
A process.
Definition
proc.h:75
proc::pid
pid_t pid
the process's ID
Definition
proc.h:77
proc::sigaction
struct aquila_sigaction sigaction[SIG_MAX+1]
its signal actions
Definition
proc.h:157
proc::vm_space
struct vm_space vm_space
its address space
Definition
proc.h:129
proc::running
int running
whether it's running
Definition
proc.h:163
proc::name
char * name
its name: the program it runs
Definition
proc.h:84
proc::threads
struct queue threads
its threads
Definition
proc.h:136
thread
A thread.
Definition
thread.h:23
thread::state
state_t state
its state
Definition
thread.h:28
thread::kentry
void(*) kentry(void *arg)
Definition
thread.h:35
thread::karg
void * karg
Definition
thread.h:36
thread::spawned
int spawned
whether it has run yet
Definition
thread.h:87
thread::name
const char * name
A kernel thread's (process 0's): its name, and what it runs.
Definition
thread.h:34
thread::arch
void * arch
the architecture's own data: its registers, its kernel stack
Definition
thread.h:84
vm_space::pmap
struct pmap * pmap
its page tables (the architecture's)
Definition
vm.h:27
thread_new
int thread_new(struct proc *proc, struct thread **rthread)
Makes a new thread in a process.
Definition
thread.c:27
RUNNABLE
@ RUNNABLE
Definition
thread.h:16
vm.h
kvm_space
struct vm_space kvm_space
Definition
vmm.c:17
kernel
sys
kthread.c
Generated by
1.18.0