1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/*
* Copyright (c) 2012 Richard Braun.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <kern/assert.h>
#include <kern/error.h>
#include <kern/init.h>
#include <kern/kmem.h>
#include <kern/list.h>
#include <kern/macros.h>
#include <kern/param.h>
#include <kern/sprintf.h>
#include <kern/stddef.h>
#include <kern/string.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <machine/cpu.h>
#include <machine/tcb.h>
struct thread_runq thread_runqs[MAX_CPUS];
/*
* Statically allocating the idle thread structures enables their use as
* "current" threads during system bootstrap, which prevents preemption
* control functions from crashing.
*/
static struct thread thread_idles[MAX_CPUS] __initdata;
/*
* Caches for allocated threads and their stacks.
*/
static struct kmem_cache thread_cache;
static struct kmem_cache thread_stack_cache;
static void __init
thread_runq_init(struct thread_runq *runq, struct thread *idle)
{
/* Consider preemption disabled during initialization */
idle->flags = 0;
idle->preempt = 1;
runq->current = idle;
runq->idle = idle;
list_init(&runq->threads);
}
static void
thread_runq_enqueue(struct thread_runq *runq, struct thread *thread)
{
assert(!cpu_intr_enabled());
list_insert_tail(&runq->threads, &thread->runq_node);
}
static struct thread *
thread_runq_dequeue(struct thread_runq *runq)
{
struct thread *thread;
assert(!cpu_intr_enabled());
if (list_empty(&runq->threads))
thread = NULL;
else {
thread = list_first_entry(&runq->threads, struct thread, runq_node);
list_remove(&thread->runq_node);
}
return thread;
}
void __init
thread_bootstrap(void)
{
size_t i;
for (i = 0; i < ARRAY_SIZE(thread_runqs); i++)
thread_runq_init(&thread_runqs[i], &thread_idles[i]);
}
void __init
thread_setup(void)
{
kmem_cache_init(&thread_cache, "thread", sizeof(struct thread),
CPU_L1_SIZE, NULL, NULL, NULL, 0);
kmem_cache_init(&thread_stack_cache, "thread_stack", STACK_SIZE,
CPU_L1_SIZE, NULL, NULL, NULL, 0);
}
static void
thread_main(void)
{
struct thread_runq *runq;
struct thread *thread;
assert(!cpu_intr_enabled());
runq = thread_runq_local();
thread = runq->current;
cpu_intr_enable();
thread->fn(thread->arg);
/* TODO Thread destruction */
for (;;)
cpu_idle();
}
static void
thread_init(struct thread *thread, struct task *task, void *stack,
const char *name, void (*fn)(void *), void *arg)
{
tcb_init(&thread->tcb, stack, thread_main);
if (name == NULL)
name = task->name;
thread->flags = 0;
thread->preempt = 0;
thread->task = task;
thread->stack = stack;
strlcpy(thread->name, name, sizeof(thread->name));
thread->fn = fn;
thread->arg = arg;
task_add_thread(task, thread);
}
int
thread_create(struct thread **threadp, struct task *task, const char *name,
void (*fn)(void *), void *arg)
{
struct thread *thread;
unsigned long flags;
void *stack;
int error;
thread = kmem_cache_alloc(&thread_cache);
if (thread == NULL) {
error = ERROR_NOMEM;
goto error_thread;
}
stack = kmem_cache_alloc(&thread_stack_cache);
if (stack == NULL) {
error = ERROR_NOMEM;
goto error_stack;
}
thread_init(thread, task, stack, name, fn, arg);
flags = cpu_intr_save();
thread_runq_enqueue(&thread_runqs[cpu_id()], thread);
cpu_intr_restore(flags);
*threadp = thread;
return 0;
error_stack:
kmem_cache_free(&thread_cache, thread);
error_thread:
return error;
}
static void
thread_idle(void *arg)
{
(void)arg;
for (;;)
cpu_idle();
}
static void __init
thread_setup_idle(void)
{
char name[THREAD_NAME_SIZE];
struct thread_runq *runq;
void *stack;
stack = kmem_cache_alloc(&thread_stack_cache);
if (stack == NULL)
panic("thread: unable to allocate idle thread stack");
snprintf(name, sizeof(name), "idle%u", cpu_id());
runq = thread_runq_local();
thread_init(runq->idle, kernel_task, stack, name, thread_idle, NULL);
}
void __init
thread_run(void)
{
struct thread_runq *runq;
struct thread *thread;
assert(cpu_intr_enabled());
thread_setup_idle();
cpu_intr_disable();
runq = thread_runq_local();
thread = thread_runq_dequeue(runq);
if (thread == NULL)
thread = runq->idle;
runq->current = thread;
tcb_load(&thread->tcb);
}
void
thread_schedule(void)
{
struct thread_runq *runq;
struct thread *prev, *next;
unsigned long flags;
assert(thread_preempt_enabled());
flags = cpu_intr_save();
runq = thread_runq_local();
prev = runq->current;
assert(prev != NULL);
if (prev != runq->idle)
thread_runq_enqueue(runq, prev);
next = thread_runq_dequeue(runq);
if (next == NULL)
next = runq->idle;
if (prev != next)
tcb_switch(&prev->tcb, &next->tcb);
cpu_intr_restore(flags);
}
void
thread_reschedule(void)
{
struct thread_runq *runq;
struct thread *thread;
assert(!cpu_intr_enabled());
runq = thread_runq_local();
thread = runq->current;
assert(thread != NULL);
if ((thread->preempt == 0) && (thread->flags & THREAD_RESCHEDULE))
thread_schedule();
}
void
thread_tick(void)
{
struct thread_runq *runq;
struct thread *thread;
assert(!cpu_intr_enabled());
runq = thread_runq_local();
thread = runq->current;
assert(thread != NULL);
thread->flags |= THREAD_RESCHEDULE;
}
|