-
Notifications
You must be signed in to change notification settings - Fork 22
/
chopstx-gnu-linux.c
351 lines (304 loc) · 7.46 KB
/
chopstx-gnu-linux.c
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* chopstx-gnu-linux.c - Threads and only threads: Arch specific code
* for GNU/Linux emulation
*
* Copyright (C) 2017, 2018 Flying Stone Technology
* Author: NIIBE Yutaka <[email protected]>
*
* This file is a part of Chopstx, a thread library for embedded.
*
* Chopstx 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.
*
* Chopstx 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/>.
*
* As additional permission under GNU GPL version 3 section 7, you may
* distribute non-source form of the Program without the copy of the
* GNU GPL normally required by section 4, provided you inform the
* receipents of GNU GPL by a written offer.
*
*/
#include <unistd.h>
#include <ucontext.h>
#include <signal.h>
#include <sys/time.h>
/* Data Memory Barrier. */
static void
chx_dmb (void)
{
}
static sigset_t ss_cur;
static void
chx_systick_reset (void)
{
const struct itimerval it = { {0, 0}, {0, 0} };
setitimer (ITIMER_REAL, &it, 0);
}
static void
chx_systick_reload (uint32_t ticks)
{
struct itimerval it;
it.it_value.tv_sec = 0;
it.it_value.tv_usec = (ticks / MHZ);
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 0;
setitimer (ITIMER_REAL, &it, 0);
}
static uint32_t
chx_systick_get (void)
{
struct itimerval it;
getitimer (ITIMER_REAL, &it);
return it.it_value.tv_usec * 72;
}
static uint32_t
usec_to_ticks (uint32_t usec)
{
return usec * MHZ;
}
static void
chx_enable_intr (uint8_t irq_num)
{
sigdelset (&ss_cur, irq_num);
}
static void
chx_clr_intr (uint8_t irq_num)
{ /* Clear pending interrupt. */
(void)irq_num;
}
static void
chx_disable_intr (uint8_t irq_num)
{
sigaddset (&ss_cur, irq_num);
}
static void
chx_set_intr_prio (uint8_t n)
{
(void)n;
}
static void
chx_prio_init (void)
{
}
static void
chx_cpu_sched_lock (void)
{
sigset_t ss;
sigfillset (&ss);
pthread_sigmask (SIG_BLOCK, &ss, &ss_cur);
}
static void
chx_cpu_sched_unlock (void)
{
pthread_sigmask (SIG_SETMASK, &ss_cur, NULL);
}
static void
idle (void)
{
for (;;)
pause ();
}
void
chx_handle_intr (uint32_t irq_num)
{
struct chx_pq *p;
chx_disable_intr (irq_num);
chx_spin_lock (&q_intr.lock);
for (p = q_intr.q.next; p != (struct chx_pq *)&q_intr.q; p = p->next)
if (p->v == irq_num)
{ /* should be one at most. */
struct chx_px *px = (struct chx_px *)p;
ll_dequeue (p);
chx_wakeup (p);
chx_spin_unlock (&q_intr.lock);
chx_request_preemption (px->master->prio);
return;
}
chx_spin_unlock (&q_intr.lock);
}
static ucontext_t idle_tc;
static char idle_stack[4096];
struct chx_thread main_thread;
void
chx_sigmask (ucontext_t *uc)
{
/* Modify oldmask to SS_CUR, so that the signal mask will
* be set to SS_CUR.
*
* In user-level, sigset_t is big, but only the first word
* is used by the kernel.
*/
memcpy (&uc->uc_sigmask, &ss_cur, sizeof (uint64_t));
}
static void
sigalrm_handler (int sig, siginfo_t *siginfo, void *arg)
{
extern void chx_timer_expired (void);
ucontext_t *uc = arg;
(void)sig;
(void)siginfo;
chx_timer_expired ();
chx_sigmask (uc);
}
static void
chx_init_arch (struct chx_thread *tp)
{
struct sigaction sa;
sigemptyset (&ss_cur);
sa.sa_sigaction = sigalrm_handler;
sigfillset (&sa.sa_mask);
sa.sa_flags = SA_SIGINFO|SA_RESTART;
sigaction (SIGALRM, &sa, NULL);
getcontext (&idle_tc);
idle_tc.uc_stack.ss_sp = idle_stack;
idle_tc.uc_stack.ss_size = sizeof (idle_stack);
idle_tc.uc_link = NULL;
makecontext (&idle_tc, idle, 0);
getcontext (&tp->tc);
}
static void
chx_request_preemption (uint16_t prio)
{
struct chx_thread *tp, *tp_prev;
ucontext_t *tcp;
if (running && (uint16_t)running->prio >= prio)
return;
/* Change the context to another thread with higher priority. */
tp = tp_prev = running;
if (tp)
{
if (tp->flag_sched_rr)
{
if (tp->state == THREAD_RUNNING)
{
chx_timer_dequeue (tp);
chx_ready_enqueue (tp);
}
}
else
chx_ready_push (tp);
running = NULL;
}
tp = running = chx_ready_pop ();
if (tp)
{
tcp = &tp->tc;
if (tp->flag_sched_rr)
{
chx_spin_lock (&q_timer.lock);
tp = chx_timer_insert (tp, PREEMPTION_USEC);
chx_spin_unlock (&q_timer.lock);
}
}
else
tcp = &idle_tc;
if (tp_prev)
{
/*
* The swapcontext implementation may reset sigmask in the
* middle of its execution, unfortunately. It is best if
* sigmask restore is done at the end of the routine, but we
* can't assume that.
*
* Thus, there might be a race condition with regards to the
* user context TCP, if signal mask is cleared and signal comes
* in. To avoid this situation, we block signals.
*
* We don't need to fill the mask here. It keeps the condition
* of blocking signals before&after swapcontext call. It is
* done by the signal mask for sigaction, the initial creation
* of the thread, and the condition of chx_sched function which
* mandates holding cpu_sched_lock.
*/
swapcontext (&tp_prev->tc, tcp);
}
else if (tp)
{
setcontext (tcp);
}
}
/*
* chx_sched: switch to another thread.
*
* There are two cases:
* YIELD=0 (SLEEP): Current RUNNING thread is already connected to
* something (mutex, cond, intr, etc.)
* YIELD=1 (YIELD): Current RUNNING thread is active,
* it is needed to be enqueued to READY queue.
*
* Returns:
* 1 on wakeup by others.
* 0 on normal wakeup.
* -1 on cancellation.
*/
static uintptr_t
chx_sched (uint32_t yield)
{
struct chx_thread *tp, *tp_prev;
uintptr_t v;
ucontext_t *tcp;
tp = tp_prev = running;
if (yield)
{
if (tp->flag_sched_rr)
chx_timer_dequeue (tp);
chx_ready_enqueue (tp);
}
running = tp = chx_ready_pop ();
if (tp)
{
v = tp->v;
if (tp->flag_sched_rr)
{
chx_spin_lock (&q_timer.lock);
tp = chx_timer_insert (tp, PREEMPTION_USEC);
chx_spin_unlock (&q_timer.lock);
}
tcp = &tp->tc;
}
else
{
v = 0;
tcp = &idle_tc;
}
swapcontext (&tp_prev->tc, tcp);
chx_cpu_sched_unlock ();
return v;
}
static void __attribute__((__noreturn__))
chx_thread_start (voidfunc thread_entry, void *arg)
{
chx_cpu_sched_unlock ();
thread_entry (arg);
chopstx_exit (0);
}
static struct chx_thread *
chopstx_create_arch (uintptr_t stack_addr, size_t stack_size,
voidfunc thread_entry, void *arg)
{
struct chx_thread *tp;
tp = malloc (sizeof (struct chx_thread));
if (!tp)
chx_fatal (CHOPSTX_ERR_THREAD_CREATE);
/*
* Calling getcontext with sched_lock held, the context is with
* signal blocked. The sigmask will be cleared in chx_thread_start.
*/
chx_cpu_sched_lock ();
getcontext (&tp->tc);
tp->tc.uc_stack.ss_sp = (void *)stack_addr;
tp->tc.uc_stack.ss_size = stack_size;
tp->tc.uc_link = NULL;
makecontext (&tp->tc, (void (*)(void))chx_thread_start,
4, thread_entry, arg);
chx_cpu_sched_unlock ();
return tp;
}