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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/**
* @file thread.c
* @author Hanjie Wu (hanjiew)
* @author Devang Acharya (devanga)
* @brief Implementation of main thread functions
* @date 2023-02-22
*
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <mutex.h>
#include <simics.h>
#include <syscall.h>
#include <thr_internals.h>
#include <thread.h>
#include <ureg.h>
/* return value for thread swexn */
#define THREAD_EXIT_SWEXN -2
/**
* @brief stack size requested
*/
static uintptr_t thr_stack_size;
/**
* @brief stack size aligned
*/
static uintptr_t thr_stack_alloc_size;
/**
* @brief highest address of thread stacks
*/
static uintptr_t thr_stack_begin;
/**
* @brief lowest address of thread stacks
*/
static uintptr_t thr_stack_end;
/**
* @brief tcb for main thread
*/
tcb_t main_tcb;
/**
* @brief global lock for tcb operations
*/
mutex_t tcb_lock;
/**
* @brief list of unused tcbs
*/
tcb_t* free_tcbs = NULL;
/**
* @brief get esp register
*/
uintptr_t get_esp();
/**
* @brief swexn handler for multithread programs
*/
static void thr_swexn_handler(void* arg, ureg_t* reg);
/**
* @brief wrapper function for thread_fork syscall
* @param tcb tcb for new thread
* @param f user thread entry point
* @param args user arg for f
*/
int thread_fork(tcb_t* tcb, void* (*f)(void*), void* args);
/**
* @brief Get the tcb of the thread this function is called form
*
* @return tcb_t* pointer to current thread's TCB
*/
tcb_t* get_self_tcb() {
uintptr_t esp = get_esp();
/* check esp in main thread area */
if (esp > main_tcb.stack_lo) {
return &main_tcb;
}
/* calculate stack area from esp */
uintptr_t stack_end = esp + (thr_stack_begin - esp) % thr_stack_alloc_size;
/* pointer to tcb is at highest address in current thread stack */
return *(tcb_t**)(stack_end - sizeof(tcb_t*));
}
/**
* @brief Initialize thread library. Must be called once before using any thread
* functions.
*
* Thread stack structure:
* +-------------------------+-----+---------------+
* | thread stack |address of tcb |
* +-------------------------+-----+---------------+
*
*
* @param size the amount of stack space available for each thread
*
* @return int 0 on success, -1 on faliure
*/
int thr_init(uintptr_t size) {
/* set globals */
thr_stack_size = size;
thr_stack_alloc_size = ((size + PAGE_SIZE - 1) & PAGE_ALIGN_MASK);
thr_stack_begin = ((main_tcb.stack_lo) & PAGE_ALIGN_MASK);
thr_stack_end = thr_stack_begin;
/* initialize global lock - will never fail */
mutex_init(&tcb_lock);
if (swexn(ex_stack_end, thr_swexn_handler, NULL, NULL) < 0)
return -1;
return 0;
}
/**
* @brief Create a new thread with the entry point 'func', and arguments 'args'
*
* @param func thread entry point
* @param args thread entry function arguments
*
* @return -1 on failure, 0 on success
*/
int thr_create(void* (*func)(void*), void* args) {
/* holds return values for error checking and the TID to be returned */
int result;
/* holds pointer tcb for new thread */
tcb_t* thread_tcb;
mutex_lock(&tcb_lock);
/* find tcb from free list */
if (free_tcbs != NULL) {
thread_tcb = free_tcbs;
/* reuse stack area */
result = new_pages((void*)thread_tcb->stack_lo, thr_stack_alloc_size);
if (result != 0) {
mutex_unlock(&tcb_lock);
return -1;
}
/* remove from list (use tcb->right as link list) */
free_tcbs = free_tcbs->right;
}
else {
/* allocate new tcb */
thread_tcb = malloc(sizeof(tcb_t));
if (thread_tcb == NULL) {
mutex_unlock(&tcb_lock);
return -1;
}
thr_stack_end -= thr_stack_alloc_size;
result = new_pages((void*)thr_stack_end, thr_stack_alloc_size);
/* if new_pages unsuccessful */
if (result != 0) {
thr_stack_end += thr_stack_alloc_size;
free(thread_tcb);
mutex_unlock(&tcb_lock);
return -1;
}
/* set tcb stack boundaries */
thread_tcb->stack_lo = thr_stack_end;
thread_tcb->stack_hi =
thr_stack_end + thr_stack_alloc_size - sizeof(tcb_t*);
}
/* put pointer to tcb at the top of stack for get_self_tcb() use */
*(tcb_t**)(thread_tcb->stack_lo + thr_stack_alloc_size - sizeof(tcb_t*)) =
thread_tcb;
thread_tcb->waiter = 0;
thread_tcb->exited = 0;
thread_tcb->is_main = 0;
cond_init(&thread_tcb->wait_cv);
thread_tcb->mutex_next = NULL;
thread_tcb->mutex_resume = 0;
/* create new thread */
result = thread_fork(thread_tcb, func, args);
/* if thread_fork failed */
if (result < 0) {
cond_destroy(&thread_tcb->wait_cv);
remove_pages((void*)thread_tcb->stack_lo);
thread_tcb->right = free_tcbs;
free_tcbs = thread_tcb;
mutex_unlock(&tcb_lock);
return -1;
}
/* thread successfully created, return tid */
thread_tcb->tid = result;
rb_insert_tcb(thread_tcb);
mutex_unlock(&tcb_lock);
return result;
}
/**
* @brief wait thread tid to exit and get exit status
*
* @param tid thread to wait
* @param statusp output pointer for exit status
* @return int 0 on success, -1 on error
*/
int thr_join(int tid, void** statusp) {
mutex_lock(&tcb_lock);
tcb_t* tcb = rb_find_tcb(tid);
if (tcb == NULL) {
mutex_unlock(&tcb_lock);
return -1;
}
/* other thread is waiting for the thread */
if (tcb->waiter != 0) {
mutex_unlock(&tcb_lock);
return -1;
}
/* make sure others will not wait on this thread */
tcb->waiter = 1;
/* wait until thread exits */
if (tcb->exited != 1) {
cond_wait(&tcb->wait_cv, &tcb_lock);
}
/* return exited thread status if user wants it */
if (statusp != NULL) {
*statusp = tcb->exit_value;
}
/* cleanup and return */
rb_delete_tcb(tcb);
cond_destroy(&tcb->wait_cv);
if (tcb->is_main == 0) {
remove_pages((void*)tcb->stack_lo);
tcb->right = free_tcbs;
free_tcbs = tcb;
}
mutex_unlock(&tcb_lock);
return 0;
}
/**
* @brief exit from thread with given status
*
* @param status
*/
void thr_exit(void* status) {
tcb_t* tcb = get_self_tcb();
mutex_lock(&tcb_lock);
tcb->exit_value = (void*)status;
tcb->exited = 1;
cond_signal(&tcb->wait_cv);
/* call vanish after unlock and do not use stack becuase the stack may have
* been freed by other threads */
mutex_unlock_vanish(&tcb_lock);
}
/**
* @brief return the ID of the current thread
*
* @return int - ID of the current thread
*/
int thr_getid(void) {
return get_self_tcb()->tid;
}
/**
* @brief Defers execution of involing thread
*
* @param tid
* @return int
*/
int thr_yield(int tid) {
return yield(tid);
}
/**
* @brief thread entrypoint wrapper
* @param f user func
* @param arg user argument
*/
void thr_begin(void* (*f)(void*), void* arg) {
mutex_lock(&tcb_lock);
mutex_unlock(&tcb_lock);
thr_exit(f(arg));
}
/**
* @brief swexn handler
*
* @param arg
* @param reg
*/
static void thr_swexn_handler(void* arg, ureg_t* reg) {
task_vanish(THREAD_EXIT_SWEXN);
}
|