forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreads.c
More file actions
228 lines (181 loc) · 4.97 KB
/
threads.c
File metadata and controls
228 lines (181 loc) · 4.97 KB
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
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <asm/host_ops.h>
#include <asm/cpu.h>
#include <asm/sched.h>
static int init_ti(struct thread_info *ti)
{
ti->sched_sem = lkl_ops->sem_alloc(0);
if (!ti->sched_sem)
return -ENOMEM;
ti->dead = false;
ti->prev_sched = NULL;
ti->tid = 0;
return 0;
}
unsigned long *alloc_thread_stack_node(struct task_struct *task, int node)
{
struct thread_info *ti;
ti = kmalloc(sizeof(*ti), GFP_KERNEL);
if (!ti)
return NULL;
if (init_ti(ti)) {
kfree(ti);
return NULL;
}
ti->task = task;
return (unsigned long *)ti;
}
/*
* The only new tasks created are kernel threads that have a predefined starting
* point thus no stack copy is required.
*/
void setup_thread_stack(struct task_struct *p, struct task_struct *org)
{
struct thread_info *ti = task_thread_info(p);
struct thread_info *org_ti = task_thread_info(org);
ti->flags = org_ti->flags;
ti->preempt_count = org_ti->preempt_count;
ti->addr_limit = org_ti->addr_limit;
}
static void kill_thread(struct thread_info *ti)
{
if (!test_ti_thread_flag(ti, TIF_HOST_THREAD)) {
ti->dead = true;
lkl_ops->sem_up(ti->sched_sem);
lkl_ops->thread_join(ti->tid);
}
lkl_ops->sem_free(ti->sched_sem);
}
void free_thread_stack(struct task_struct *tsk)
{
struct thread_info *ti = task_thread_info(tsk);
kill_thread(ti);
kfree(ti);
}
struct thread_info *_current_thread_info = &init_thread_union.thread_info;
/*
* schedule() expects the return of this function to be the task that we
* switched away from. Returning prev is not going to work because we are
* actually going to return the previous taks that was scheduled before the
* task we are going to wake up, and not the current task, e.g.:
*
* swapper -> init: saved prev on swapper stack is swapper
* init -> ksoftirqd0: saved prev on init stack is init
* ksoftirqd0 -> swapper: returned prev is swapper
*/
static struct task_struct *abs_prev = &init_task;
struct task_struct *__switch_to(struct task_struct *prev,
struct task_struct *next)
{
struct thread_info *_prev = task_thread_info(prev);
struct thread_info *_next = task_thread_info(next);
unsigned long _prev_flags = _prev->flags;
struct lkl_jmp_buf _prev_jb;
_current_thread_info = task_thread_info(next);
_next->prev_sched = prev;
abs_prev = prev;
BUG_ON(!_next->tid);
lkl_cpu_change_owner(_next->tid);
if (test_bit(TIF_SCHED_JB, &_prev_flags)) {
/* Atomic. Must be done before wakeup next */
clear_ti_thread_flag(_prev, TIF_SCHED_JB);
_prev_jb = _prev->sched_jb;
}
lkl_ops->sem_up(_next->sched_sem);
if (test_bit(TIF_SCHED_JB, &_prev_flags)) {
lkl_ops->jmp_buf_longjmp(&_prev_jb, 1);
} else {
lkl_ops->sem_down(_prev->sched_sem);
}
if (_prev->dead)
lkl_ops->thread_exit();
return abs_prev;
}
int host_task_stub(void *unused)
{
return 0;
}
void switch_to_host_task(struct task_struct *task)
{
if (WARN_ON(!test_tsk_thread_flag(task, TIF_HOST_THREAD)))
return;
task_thread_info(task)->tid = lkl_ops->thread_self();
if (current == task)
return;
wake_up_process(task);
thread_sched_jb();
lkl_ops->sem_down(task_thread_info(task)->sched_sem);
schedule_tail(abs_prev);
}
struct thread_bootstrap_arg {
struct thread_info *ti;
int (*f)(void *);
void *arg;
};
static void thread_bootstrap(void *_tba)
{
struct thread_bootstrap_arg *tba = (struct thread_bootstrap_arg *)_tba;
struct thread_info *ti = tba->ti;
int (*f)(void *) = tba->f;
void *arg = tba->arg;
lkl_ops->sem_down(ti->sched_sem);
kfree(tba);
if (ti->prev_sched)
schedule_tail(ti->prev_sched);
f(arg);
do_exit(0);
}
int copy_thread(unsigned long clone_flags, unsigned long esp,
unsigned long unused, struct task_struct *p)
{
struct thread_info *ti = task_thread_info(p);
struct thread_bootstrap_arg *tba;
if ((int (*)(void *))esp == host_task_stub) {
set_ti_thread_flag(ti, TIF_HOST_THREAD);
return 0;
}
tba = kmalloc(sizeof(*tba), GFP_KERNEL);
if (!tba)
return -ENOMEM;
tba->f = (int (*)(void *))esp;
tba->arg = (void *)unused;
tba->ti = ti;
ti->tid = lkl_ops->thread_create(thread_bootstrap, tba);
if (!ti->tid) {
kfree(tba);
return -ENOMEM;
}
return 0;
}
void show_stack(struct task_struct *task, unsigned long *esp)
{
}
/**
* This is called before the kernel initializes, so no kernel calls (including
* printk) can't be made yet.
*/
void threads_init(void)
{
int ret;
struct thread_info *ti = &init_thread_union.thread_info;
ret = init_ti(ti);
if (ret < 0)
lkl_printf("lkl: failed to allocate init schedule semaphore\n");
ti->tid = lkl_ops->thread_self();
}
void threads_cleanup(void)
{
struct task_struct *p, *t;
for_each_process_thread(p, t) {
struct thread_info *ti = task_thread_info(t);
if (t->pid != 1 && !test_ti_thread_flag(ti, TIF_HOST_THREAD))
WARN(!(t->flags & PF_KTHREAD),
"non kernel thread task %s\n", t->comm);
WARN(t->state == TASK_RUNNING,
"thread %s still running while halting\n", t->comm);
kill_thread(ti);
}
lkl_ops->sem_free(init_thread_union.thread_info.sched_sem);
}