summaryrefslogtreecommitdiffstats
path: root/firmware/asm/thread-unix.c
blob: 3c5e7c96ee7985c2133f9cf37f42c2c252ba82d9 (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2011 by Thomas Martitz
 *
 * Generic unix threading support
 *
 * 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 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdlib.h>
#include <stdbool.h>
#include <signal.h>
#include <stdio.h>
#include <setjmp.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include "debug.h"

static volatile bool sig_handler_called;
static volatile jmp_buf tramp_buf;
static volatile jmp_buf bootstrap_buf;
static void (*thread_func)(void);
static const int trampoline_sig = SIGUSR1;
static pthread_t main_thread;

static struct ctx {
    jmp_buf thread_buf;
} thread_bufs[MAXTHREADS];
static struct ctx* thread_context, *target_context;
static int curr_uc;

static void trampoline(int sig);
static void bootstrap_context(void) __attribute__((noinline));

/* The *_context functions are heavily based on Gnu pth
 * http://www.gnu.org/software/pth/
 *
 * adjusted to work in a multi-thread environment to
 * offer a ucontext-like API
 */

/*
 * VARIANT 2: THE SIGNAL STACK TRICK
 *
 * This uses sigstack/sigaltstack() and friends and is really the
 * most tricky part of Pth. When you understand the following
 * stuff you're a good Unix hacker and then you've already
 * understood the gory ingredients of Pth.  So, either welcome to
 * the club of hackers, or do yourself a favor and skip this ;)
 *
 * The ingenious fact is that this variant runs really on _all_ POSIX
 * compliant systems without special platform kludges.  But be _VERY_
 * carefully when you change something in the following code. The slightest
 * change or reordering can lead to horribly broken code.  Really every
 * function call in the following case is intended to be how it is, doubt
 * me...
 *
 * For more details we strongly recommend you to read the companion
 * paper ``Portable Multithreading -- The Signal Stack Trick for
 * User-Space Thread Creation'' from Ralf S. Engelschall. A copy of the
 * draft of this paper you can find in the file rse-pmt.ps inside the
 * GNU Pth distribution.
 */

static int make_context(struct ctx *ctx, void (*f)(void), char *sp, size_t stack_size)
{
    struct sigaction sa;
    struct sigaction osa;
    stack_t ss;
    stack_t oss;
    sigset_t osigs;
    sigset_t sigs;

    disable_irq();
    /*
     * Preserve the trampoline_sig signal state, block trampoline_sig,
     * and establish our signal handler. The signal will
     * later transfer control onto the signal stack.
     */
    sigemptyset(&sigs);
    sigaddset(&sigs, trampoline_sig);
    sigprocmask(SIG_BLOCK, &sigs, &osigs);
    sa.sa_handler = trampoline;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_ONSTACK;
    if (sigaction(trampoline_sig, &sa, &osa) != 0)
    {
        DEBUGF("%s(): %s\n", __func__, strerror(errno));
        return false;
    }
    /*
     * Set the new stack.
     *
     * For sigaltstack we're lucky [from sigaltstack(2) on
     * FreeBSD 3.1]: ``Signal stacks are automatically adjusted
     * for the direction of stack growth and alignment
     * requirements''
     *
     * For sigstack we have to decide ourself [from sigstack(2)
     * on Solaris 2.6]: ``The direction of stack growth is not
     * indicated in the historical definition of struct sigstack.
     * The only way to portably establish a stack pointer is for
     * the application to determine stack growth direction.''
     */
    ss.ss_sp    = sp;
    ss.ss_size  = stack_size;
    ss.ss_flags = 0;
    if (sigaltstack(&ss, &oss) < 0)
    {
        DEBUGF("%s(): %s\n", __func__, strerror(errno));
        return false;
    }

    /*
     * Now transfer control onto the signal stack and set it up.
     * It will return immediately via "return" after the setjmp()
     * was performed. Be careful here with race conditions.  The
     * signal can be delivered the first time sigsuspend() is
     * called.
     */
    sig_handler_called = false;
    main_thread = pthread_self();
    sigfillset(&sigs);
    sigdelset(&sigs, trampoline_sig);
    pthread_kill(main_thread, trampoline_sig);
    while(!sig_handler_called)
        sigsuspend(&sigs);

    /*
     * Inform the system that we are back off the signal stack by
     * removing the alternative signal stack. Be careful here: It
     * first has to be disabled, before it can be removed.
     */
    sigaltstack(NULL, &ss);
    ss.ss_flags = SS_DISABLE;
    if (sigaltstack(&ss, NULL) < 0)
    {
        DEBUGF("%s(): %s\n", __func__, strerror(errno));
        return false;
    }
    sigaltstack(NULL, &ss);
    if (!(ss.ss_flags & SS_DISABLE))
    {
        DEBUGF("%s(): %s\n", __func__, strerror(errno));
        return false;
    }
    if (!(oss.ss_flags & SS_DISABLE))
        sigaltstack(&oss, NULL);

    /*
     * Restore the old trampoline_sig signal handler and mask
     */
    sigaction(trampoline_sig, &osa, NULL);
    sigprocmask(SIG_SETMASK, &osigs, NULL);

    /*
     * Tell the trampoline and bootstrap function where to dump
     * the new machine context, and what to do afterwards...
     */
    thread_func = f;
    thread_context  = ctx;

    /*
     * Now enter the trampoline again, but this time not as a signal
     * handler. Instead we jump into it directly. The functionally
     * redundant ping-pong pointer arithmentic is neccessary to avoid
     * type-conversion warnings related to the `volatile' qualifier and
     * the fact that `jmp_buf' usually is an array type.
     */
    if (setjmp(*((jmp_buf *)&bootstrap_buf)) == 0)
        longjmp(*((jmp_buf *)&tramp_buf), 1);

    /*
     * Ok, we returned again, so now we're finished
     */
    enable_irq();
    return true;
}

static void trampoline(int sig)
{
    (void)sig;
    /* sanity check, no other thread should be here */
    if (pthread_self() != main_thread)
        return;

    if (setjmp(*((jmp_buf *)&tramp_buf)) == 0)
    {
        sig_handler_called = true;
        return;
    }
    /* longjump'd back in */
    bootstrap_context();
}

void bootstrap_context(void)
{
    /* copy to local storage so we can spawn further threads
     * in the meantime */
    void (*thread_entry)(void) = thread_func;
    struct ctx *t = thread_context;
    
    /*
     * Save current machine state (on new stack) and
     * go back to caller until we're scheduled for real...
     */
    if (setjmp(t->thread_buf) == 0)
        longjmp(*((jmp_buf *)&bootstrap_buf), 1);

    /*
     * The new thread is now running: GREAT!
     * Now we just invoke its init function....
     */
    thread_entry();
    DEBUGF("thread left\n");
    thread_exit();
}

static inline void set_context(struct ctx *c)
{
    longjmp(c->thread_buf, 1);
}

static inline void swap_context(struct ctx *old, struct ctx *new)
{
    if (setjmp(old->thread_buf) == 0)
        longjmp(new->thread_buf, 1);
}

static inline void get_context(struct ctx *c)
{
    setjmp(c->thread_buf);
}


static void setup_thread(struct regs *context);

#define INIT_MAIN_THREAD
static void init_main_thread(void *addr)
{
    /* get a context for the main thread so that we can jump to it from
     * other threads */
    struct regs *context = (struct regs*)addr;
    context->uc = &thread_bufs[curr_uc++];
    get_context(context->uc);
}

#define THREAD_STARTUP_INIT(core, thread, function) \
    ({ (thread)->context.stack_size = (thread)->stack_size, \
       (thread)->context.stack = (uintptr_t)(thread)->stack; \
       (thread)->context.start = function; })



/*
 * Prepare context to make the thread runnable by calling swapcontext on it
 */
static void setup_thread(struct regs *context)
{
    void (*fn)(void) = context->start;
    context->uc = &thread_bufs[curr_uc++];
    while (!make_context(context->uc, fn, (char*)context->stack, context->stack_size))
        DEBUGF("Thread creation failed. Retrying");
}


/*
 * Save the ucontext_t pointer for later use in swapcontext()
 *
 * Cannot do getcontext() here, because jumping back to the context
 * resumes after the getcontext call (i.e. store_context), but we need
 * to resume from load_context()
 */
static inline void store_context(void* addr)
{
    struct regs *r = (struct regs*)addr;
    target_context = r->uc;
}

/*
 * Perform context switch
 */
static inline void load_context(const void* addr)
{
    struct regs *r = (struct regs*)addr;
    if (UNLIKELY(r->start))
    {
        setup_thread(r);
        r->start = NULL;
    }
    swap_context(target_context, r->uc);
}