summaryrefslogtreecommitdiff
path: root/firmware/target/hosted
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/hosted')
-rw-r--r--firmware/target/hosted/android/system-target.h5
-rw-r--r--firmware/target/hosted/sdl/system-sdl.h9
-rw-r--r--firmware/target/hosted/system-hosted.h37
-rw-r--r--firmware/target/hosted/thread-arm.c99
-rw-r--r--firmware/target/hosted/thread-unix.c316
-rw-r--r--firmware/target/hosted/thread-win32.c85
-rw-r--r--firmware/target/hosted/ypr0/system-target.h5
7 files changed, 41 insertions, 515 deletions
diff --git a/firmware/target/hosted/android/system-target.h b/firmware/target/hosted/android/system-target.h
index aba7e6d6c0..fd81b6661e 100644
--- a/firmware/target/hosted/android/system-target.h
+++ b/firmware/target/hosted/android/system-target.h
@@ -23,10 +23,7 @@
23#define __SYSTEM_TARGET_H__ 23#define __SYSTEM_TARGET_H__
24 24
25#include "kernel-unix.h" 25#include "kernel-unix.h"
26 26#include "system-hosted.h"
27static inline void commit_dcache(void) {}
28static inline void commit_discard_dcache(void) {}
29static inline void commit_discard_idcache(void) {}
30 27
31 /* don't pull in jni.h for every user of this file, it should be only needed 28 /* don't pull in jni.h for every user of this file, it should be only needed
32 * within the target tree (if at all) 29 * within the target tree (if at all)
diff --git a/firmware/target/hosted/sdl/system-sdl.h b/firmware/target/hosted/sdl/system-sdl.h
index d607e5e0a6..a20cbe5fac 100644
--- a/firmware/target/hosted/sdl/system-sdl.h
+++ b/firmware/target/hosted/sdl/system-sdl.h
@@ -41,6 +41,8 @@ int set_irq_level(int level);
41#define restore_irq(level) \ 41#define restore_irq(level) \
42 ((void)set_irq_level(level)) 42 ((void)set_irq_level(level))
43 43
44#include "system-hosted.h"
45
44void sim_enter_irq_handler(void); 46void sim_enter_irq_handler(void);
45void sim_exit_irq_handler(void); 47void sim_exit_irq_handler(void);
46void sim_kernel_shutdown(void); 48void sim_kernel_shutdown(void);
@@ -48,17 +50,10 @@ void sys_poweroff(void);
48void sys_handle_argv(int argc, char *argv[]); 50void sys_handle_argv(int argc, char *argv[]);
49void gui_message_loop(void); 51void gui_message_loop(void);
50void sim_do_exit(void) NORETURN_ATTR; 52void sim_do_exit(void) NORETURN_ATTR;
51#ifndef HAVE_SDL_THREADS
52void wait_for_interrupt(void);
53#endif
54 53
55extern bool background; /* True if the background image is enabled */ 54extern bool background; /* True if the background image is enabled */
56extern bool showremote; 55extern bool showremote;
57extern int display_zoom; 56extern int display_zoom;
58extern long start_tick; 57extern long start_tick;
59 58
60static inline void commit_dcache(void) {}
61static inline void commit_discard_dcache(void) {}
62static inline void commit_discard_idcache(void) {}
63
64#endif /* _SYSTEM_SDL_H_ */ 59#endif /* _SYSTEM_SDL_H_ */
diff --git a/firmware/target/hosted/system-hosted.h b/firmware/target/hosted/system-hosted.h
new file mode 100644
index 0000000000..e60803fde0
--- /dev/null
+++ b/firmware/target/hosted/system-hosted.h
@@ -0,0 +1,37 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2010 by Thomas Martitz
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#ifndef __SYSTEM_HOSTED_H__
23#define __SYSTEM_HOSTED_H__
24
25#include "system.h"
26
27static inline void commit_dcache(void) {}
28static inline void commit_discard_dcache(void) {}
29static inline void commit_discard_idcache(void) {}
30
31static inline void core_sleep(void)
32{
33 enable_irq();
34 wait_for_interrupt();
35}
36
37#endif
diff --git a/firmware/target/hosted/thread-arm.c b/firmware/target/hosted/thread-arm.c
deleted file mode 100644
index 8815f063d7..0000000000
--- a/firmware/target/hosted/thread-arm.c
+++ /dev/null
@@ -1,99 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 by Thom Johansen
11 * Copyright (C) 2010 by Thomas Martitz (Android-suitable core_sleep())
12 *
13 * Generic ARM threading support
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 ****************************************************************************/
24
25#include <system.h>
26
27/*---------------------------------------------------------------------------
28 * Start the thread running and terminate it if it returns
29 *---------------------------------------------------------------------------
30 */
31static void __attribute__((naked)) USED_ATTR start_thread(void)
32{
33 /* r0 = context */
34 asm volatile (
35 "ldr sp, [r0, #32] \n" /* Load initial sp */
36 "ldr r4, [r0, #40] \n" /* start in r4 since it's non-volatile */
37 "mov r1, #0 \n" /* Mark thread as running */
38 "str r1, [r0, #40] \n"
39 "mov lr, pc \n" /* Call thread function */
40 "bx r4 \n"
41 ); /* No clobber list - new thread doesn't care */
42 thread_exit();
43}
44
45/* For startup, place context pointer in r4 slot, start_thread pointer in r5
46 * slot, and thread function pointer in context.start. See load_context for
47 * what happens when thread is initially going to run. */
48#define THREAD_STARTUP_INIT(core, thread, function) \
49 ({ (thread)->context.r[0] = (uint32_t)&(thread)->context, \
50 (thread)->context.r[1] = (uint32_t)start_thread, \
51 (thread)->context.start = (uint32_t)function; })
52
53
54/*---------------------------------------------------------------------------
55 * Store non-volatile context.
56 *---------------------------------------------------------------------------
57 */
58static inline void store_context(void* addr)
59{
60 asm volatile(
61 "stmia %0, { r4-r11, sp, lr } \n"
62 : : "r" (addr)
63 );
64}
65
66/*---------------------------------------------------------------------------
67 * Load non-volatile context.
68 *---------------------------------------------------------------------------
69 */
70static inline void load_context(const void* addr)
71{
72 asm volatile(
73 "ldr r0, [%0, #40] \n" /* Load start pointer */
74 "cmp r0, #0 \n" /* Check for NULL */
75
76 /* If not already running, jump to start */
77 "ldmneia %0, { r0, pc } \n"
78 "ldmia %0, { r4-r11, sp, lr } \n" /* Load regs r4 to r14 from context */
79 : : "r" (addr) : "r0" /* only! */
80 );
81}
82
83/*
84 * this core sleep suspends the OS thread rockbox runs under, which greatly
85 * reduces cpu usage (~100% to <10%)
86 *
87 * it returns when when the tick timer is called, other interrupt-like
88 * events occur
89 *
90 * wait_for_interrupt is implemented in kernel-<platform>.c
91 **/
92
93static inline void core_sleep(void)
94{
95 enable_irq();
96 wait_for_interrupt();
97}
98
99
diff --git a/firmware/target/hosted/thread-unix.c b/firmware/target/hosted/thread-unix.c
deleted file mode 100644
index 79310e0600..0000000000
--- a/firmware/target/hosted/thread-unix.c
+++ /dev/null
@@ -1,316 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2011 by Thomas Martitz
11 *
12 * Generic unix threading support
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ****************************************************************************/
23
24#include <stdlib.h>
25#include <stdbool.h>
26#include <signal.h>
27#include <stdio.h>
28#include <setjmp.h>
29#include <unistd.h>
30#include <pthread.h>
31#include <errno.h>
32#include "debug.h"
33
34static volatile bool sig_handler_called;
35static volatile jmp_buf tramp_buf;
36static volatile jmp_buf bootstrap_buf;
37static void (*thread_func)(void);
38static const int trampoline_sig = SIGUSR1;
39static pthread_t main_thread;
40
41static struct ctx {
42 jmp_buf thread_buf;
43} thread_bufs[MAXTHREADS];
44static struct ctx* thread_context, *target_context;
45static int curr_uc;
46
47static void trampoline(int sig);
48static void bootstrap_context(void) __attribute__((noinline));
49
50/* The *_context functions are heavily based on Gnu pth
51 * http://www.gnu.org/software/pth/
52 *
53 * adjusted to work in a multi-thread environment to
54 * offer a ucontext-like API
55 */
56
57/*
58 * VARIANT 2: THE SIGNAL STACK TRICK
59 *
60 * This uses sigstack/sigaltstack() and friends and is really the
61 * most tricky part of Pth. When you understand the following
62 * stuff you're a good Unix hacker and then you've already
63 * understood the gory ingredients of Pth. So, either welcome to
64 * the club of hackers, or do yourself a favor and skip this ;)
65 *
66 * The ingenious fact is that this variant runs really on _all_ POSIX
67 * compliant systems without special platform kludges. But be _VERY_
68 * carefully when you change something in the following code. The slightest
69 * change or reordering can lead to horribly broken code. Really every
70 * function call in the following case is intended to be how it is, doubt
71 * me...
72 *
73 * For more details we strongly recommend you to read the companion
74 * paper ``Portable Multithreading -- The Signal Stack Trick for
75 * User-Space Thread Creation'' from Ralf S. Engelschall. A copy of the
76 * draft of this paper you can find in the file rse-pmt.ps inside the
77 * GNU Pth distribution.
78 */
79
80static int make_context(struct ctx *ctx, void (*f)(void), char *sp, size_t stack_size)
81{
82 struct sigaction sa;
83 struct sigaction osa;
84 stack_t ss;
85 stack_t oss;
86 sigset_t osigs;
87 sigset_t sigs;
88
89 disable_irq();
90 /*
91 * Preserve the trampoline_sig signal state, block trampoline_sig,
92 * and establish our signal handler. The signal will
93 * later transfer control onto the signal stack.
94 */
95 sigemptyset(&sigs);
96 sigaddset(&sigs, trampoline_sig);
97 sigprocmask(SIG_BLOCK, &sigs, &osigs);
98 sa.sa_handler = trampoline;
99 sigemptyset(&sa.sa_mask);
100 sa.sa_flags = SA_ONSTACK;
101 if (sigaction(trampoline_sig, &sa, &osa) != 0)
102 {
103 DEBUGF("%s(): %s\n", __func__, strerror(errno));
104 return false;
105 }
106 /*
107 * Set the new stack.
108 *
109 * For sigaltstack we're lucky [from sigaltstack(2) on
110 * FreeBSD 3.1]: ``Signal stacks are automatically adjusted
111 * for the direction of stack growth and alignment
112 * requirements''
113 *
114 * For sigstack we have to decide ourself [from sigstack(2)
115 * on Solaris 2.6]: ``The direction of stack growth is not
116 * indicated in the historical definition of struct sigstack.
117 * The only way to portably establish a stack pointer is for
118 * the application to determine stack growth direction.''
119 */
120 ss.ss_sp = sp;
121 ss.ss_size = stack_size;
122 ss.ss_flags = 0;
123 if (sigaltstack(&ss, &oss) < 0)
124 {
125 DEBUGF("%s(): %s\n", __func__, strerror(errno));
126 return false;
127 }
128
129 /*
130 * Now transfer control onto the signal stack and set it up.
131 * It will return immediately via "return" after the setjmp()
132 * was performed. Be careful here with race conditions. The
133 * signal can be delivered the first time sigsuspend() is
134 * called.
135 */
136 sig_handler_called = false;
137 main_thread = pthread_self();
138 sigfillset(&sigs);
139 sigdelset(&sigs, trampoline_sig);
140 pthread_kill(main_thread, trampoline_sig);
141 while(!sig_handler_called)
142 sigsuspend(&sigs);
143
144 /*
145 * Inform the system that we are back off the signal stack by
146 * removing the alternative signal stack. Be careful here: It
147 * first has to be disabled, before it can be removed.
148 */
149 sigaltstack(NULL, &ss);
150 ss.ss_flags = SS_DISABLE;
151 if (sigaltstack(&ss, NULL) < 0)
152 {
153 DEBUGF("%s(): %s\n", __func__, strerror(errno));
154 return false;
155 }
156 sigaltstack(NULL, &ss);
157 if (!(ss.ss_flags & SS_DISABLE))
158 {
159 DEBUGF("%s(): %s\n", __func__, strerror(errno));
160 return false;
161 }
162 if (!(oss.ss_flags & SS_DISABLE))
163 sigaltstack(&oss, NULL);
164
165 /*
166 * Restore the old trampoline_sig signal handler and mask
167 */
168 sigaction(trampoline_sig, &osa, NULL);
169 sigprocmask(SIG_SETMASK, &osigs, NULL);
170
171 /*
172 * Tell the trampoline and bootstrap function where to dump
173 * the new machine context, and what to do afterwards...
174 */
175 thread_func = f;
176 thread_context = ctx;
177
178 /*
179 * Now enter the trampoline again, but this time not as a signal
180 * handler. Instead we jump into it directly. The functionally
181 * redundant ping-pong pointer arithmentic is neccessary to avoid
182 * type-conversion warnings related to the `volatile' qualifier and
183 * the fact that `jmp_buf' usually is an array type.
184 */
185 if (setjmp(*((jmp_buf *)&bootstrap_buf)) == 0)
186 longjmp(*((jmp_buf *)&tramp_buf), 1);
187
188 /*
189 * Ok, we returned again, so now we're finished
190 */
191 enable_irq();
192 return true;
193}
194
195static void trampoline(int sig)
196{
197 (void)sig;
198 /* sanity check, no other thread should be here */
199 if (pthread_self() != main_thread)
200 return;
201
202 if (setjmp(*((jmp_buf *)&tramp_buf)) == 0)
203 {
204 sig_handler_called = true;
205 return;
206 }
207 /* longjump'd back in */
208 bootstrap_context();
209}
210
211void bootstrap_context(void)
212{
213 /* copy to local storage so we can spawn further threads
214 * in the meantime */
215 void (*thread_entry)(void) = thread_func;
216 struct ctx *t = thread_context;
217
218 /*
219 * Save current machine state (on new stack) and
220 * go back to caller until we're scheduled for real...
221 */
222 if (setjmp(t->thread_buf) == 0)
223 longjmp(*((jmp_buf *)&bootstrap_buf), 1);
224
225 /*
226 * The new thread is now running: GREAT!
227 * Now we just invoke its init function....
228 */
229 thread_entry();
230 DEBUGF("thread left\n");
231 thread_exit();
232}
233
234static inline void set_context(struct ctx *c)
235{
236 longjmp(c->thread_buf, 1);
237}
238
239static inline void swap_context(struct ctx *old, struct ctx *new)
240{
241 if (setjmp(old->thread_buf) == 0)
242 longjmp(new->thread_buf, 1);
243}
244
245static inline void get_context(struct ctx *c)
246{
247 setjmp(c->thread_buf);
248}
249
250
251static void setup_thread(struct regs *context);
252
253#define INIT_MAIN_THREAD
254static void init_main_thread(void *addr)
255{
256 /* get a context for the main thread so that we can jump to it from
257 * other threads */
258 struct regs *context = (struct regs*)addr;
259 context->uc = &thread_bufs[curr_uc++];
260 get_context(context->uc);
261}
262
263#define THREAD_STARTUP_INIT(core, thread, function) \
264 ({ (thread)->context.stack_size = (thread)->stack_size, \
265 (thread)->context.stack = (uintptr_t)(thread)->stack; \
266 (thread)->context.start = function; })
267
268
269
270/*
271 * Prepare context to make the thread runnable by calling swapcontext on it
272 */
273static void setup_thread(struct regs *context)
274{
275 void (*fn)(void) = context->start;
276 context->uc = &thread_bufs[curr_uc++];
277 while (!make_context(context->uc, fn, (char*)context->stack, context->stack_size))
278 DEBUGF("Thread creation failed. Retrying");
279}
280
281
282/*
283 * Save the ucontext_t pointer for later use in swapcontext()
284 *
285 * Cannot do getcontext() here, because jumping back to the context
286 * resumes after the getcontext call (i.e. store_context), but we need
287 * to resume from load_context()
288 */
289static inline void store_context(void* addr)
290{
291 struct regs *r = (struct regs*)addr;
292 target_context = r->uc;
293}
294
295/*
296 * Perform context switch
297 */
298static inline void load_context(const void* addr)
299{
300 struct regs *r = (struct regs*)addr;
301 if (UNLIKELY(r->start))
302 {
303 setup_thread(r);
304 r->start = NULL;
305 }
306 swap_context(target_context, r->uc);
307}
308
309/*
310 * play nice with the host and sleep while waiting for the tick */
311extern void wait_for_interrupt(void);
312static inline void core_sleep(void)
313{
314 enable_irq();
315 wait_for_interrupt();
316}
diff --git a/firmware/target/hosted/thread-win32.c b/firmware/target/hosted/thread-win32.c
deleted file mode 100644
index a60198494a..0000000000
--- a/firmware/target/hosted/thread-win32.c
+++ /dev/null
@@ -1,85 +0,0 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2010 by Thomas Martitz
11 *
12 * Generic ARM threading support
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ****************************************************************************/
23
24
25#include <windows.h>
26#include "system.h"
27
28#define INIT_MAIN_THREAD
29
30#define THREAD_STARTUP_INIT(core, thread, function) \
31 ({ (thread)->context.stack_size = (thread)->stack_size, \
32 (thread)->context.stack = (uintptr_t)(thread)->stack; \
33 (thread)->context.start = function; })
34
35static void init_main_thread(void *addr)
36{
37 struct regs *context = (struct regs*)addr;
38 /* we must convert the current main thread to a fiber to be able to
39 * schedule other fibers */
40 context->uc = ConvertThreadToFiber(NULL);
41 context->stack_size = 0;
42}
43
44static inline void store_context(void* addr)
45{
46 (void)addr;
47 /* nothing to do here, Fibers continue after the SwitchToFiber call */
48}
49
50static void start_thread(void)
51{
52 void (*func)(void) = GetFiberData();
53 func();
54 /* go out if thread function returns */
55 thread_exit();
56}
57
58/*
59 * Load context and run it
60 *
61 * Resume execution from the last load_context call for the thread
62 */
63
64static inline void load_context(const void* addr)
65{
66 struct regs *context = (struct regs*)addr;
67 if (UNLIKELY(context->start))
68 { /* need setup before switching to it */
69 context->uc = CreateFiber(context->stack_size,
70 (LPFIBER_START_ROUTINE)start_thread, context->start);
71 /* can't assign stack pointer, only stack size */
72 context->stack_size = 0;
73 context->start = NULL;
74 }
75 SwitchToFiber(context->uc);
76}
77
78/*
79 * play nice with the host and sleep while waiting for the tick */
80static inline void core_sleep(void)
81{
82 enable_irq();
83 wait_for_interrupt();
84}
85
diff --git a/firmware/target/hosted/ypr0/system-target.h b/firmware/target/hosted/ypr0/system-target.h
index efd235282e..1ef8b9aeda 100644
--- a/firmware/target/hosted/ypr0/system-target.h
+++ b/firmware/target/hosted/ypr0/system-target.h
@@ -22,10 +22,7 @@
22#define __SYSTEM_TARGET_H__ 22#define __SYSTEM_TARGET_H__
23 23
24#include "kernel-unix.h" 24#include "kernel-unix.h"
25 25#include "system-hosted.h"
26static inline void commit_dcache(void) {}
27static inline void commit_discard_dcache(void) {}
28static inline void commit_discard_idcache(void) {}
29 26
30#define NEED_GENERIC_BYTESWAPS 27#define NEED_GENERIC_BYTESWAPS
31#endif /* __SYSTEM_TARGET_H__ */ 28#endif /* __SYSTEM_TARGET_H__ */