summaryrefslogtreecommitdiffstats
path: root/firmware/target/hosted
diff options
context:
space:
mode:
authorThomas Martitz <kugel@rockbox.org>2012-01-04 18:07:21 +0100
committerThomas Martitz <kugel@rockbox.org>2012-01-22 18:46:44 +0100
commit991ae1e39553172a7dd6cd8c634aebfce892e261 (patch)
tree672a4583af663def399c4fefdbad060605397fbc /firmware/target/hosted
parenteaa83bd64775b87e943d345e2810deed44408776 (diff)
downloadrockbox-991ae1e39553172a7dd6cd8c634aebfce892e261.tar.gz
rockbox-991ae1e39553172a7dd6cd8c634aebfce892e261.zip
Create fimrware/asm directory for assembly optimized stuff.
This dir is suitable for stuff that doesn't fit the target tree, e.g. because it also builds on hosted or otherwise. It also has a generic subfolder for fallback C implementations so that not all archs need to provide asm files. SOURCES should only contain "foo.c" where foo.c includes the specific <arch>/foo.c files from the subdirs using the preprocessor. This way automatic selection of asm versions or generic C verion is possible. For the start, the thread support files are moved, since ASM threads can be used on hosted platforms as well. Since core_sleep() remains platform specific it's moved to the corresponding system.h headers. Change-Id: Iebff272f3407a6eaafeb7656ceb0ae9eca3f7cb9
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 @@
#define __SYSTEM_TARGET_H__
#include "kernel-unix.h"
-
-static inline void commit_dcache(void) {}
-static inline void commit_discard_dcache(void) {}
-static inline void commit_discard_idcache(void) {}
+#include "system-hosted.h"
/* don't pull in jni.h for every user of this file, it should be only needed
* 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);
#define restore_irq(level) \
((void)set_irq_level(level))
+#include "system-hosted.h"
+
void sim_enter_irq_handler(void);
void sim_exit_irq_handler(void);
void sim_kernel_shutdown(void);
@@ -48,17 +50,10 @@ void sys_poweroff(void);
void sys_handle_argv(int argc, char *argv[]);
void gui_message_loop(void);
void sim_do_exit(void) NORETURN_ATTR;
-#ifndef HAVE_SDL_THREADS
-void wait_for_interrupt(void);
-#endif
extern bool background; /* True if the background image is enabled */
extern bool showremote;
extern int display_zoom;
extern long start_tick;
-static inline void commit_dcache(void) {}
-static inline void commit_discard_dcache(void) {}
-static inline void commit_discard_idcache(void) {}
-
#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 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2010 by Thomas Martitz
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __SYSTEM_HOSTED_H__
+#define __SYSTEM_HOSTED_H__
+
+#include "system.h"
+
+static inline void commit_dcache(void) {}
+static inline void commit_discard_dcache(void) {}
+static inline void commit_discard_idcache(void) {}
+
+static inline void core_sleep(void)
+{
+ enable_irq();
+ wait_for_interrupt();
+}
+
+#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 @@
-/***************************************************************************
- * __________ __ ___.
- * Open \______ \ ____ ____ | | _\_ |__ _______ ___
- * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
- * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
- * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
- * \/ \/ \/ \/ \/
- * $Id$
- *
- * Copyright (C) 2005 by Thom Johansen
- * Copyright (C) 2010 by Thomas Martitz (Android-suitable core_sleep())
- *
- * Generic ARM 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 <system.h>
-
-/*---------------------------------------------------------------------------
- * Start the thread running and terminate it if it returns
- *---------------------------------------------------------------------------
- */
-static void __attribute__((naked)) USED_ATTR start_thread(void)
-{
- /* r0 = context */
- asm volatile (
- "ldr sp, [r0, #32] \n" /* Load initial sp */
- "ldr r4, [r0, #40] \n" /* start in r4 since it's non-volatile */
- "mov r1, #0 \n" /* Mark thread as running */
- "str r1, [r0, #40] \n"
- "mov lr, pc \n" /* Call thread function */
- "bx r4 \n"
- ); /* No clobber list - new thread doesn't care */
- thread_exit();
-}
-
-/* For startup, place context pointer in r4 slot, start_thread pointer in r5
- * slot, and thread function pointer in context.start. See load_context for
- * what happens when thread is initially going to run. */
-#define THREAD_STARTUP_INIT(core, thread, function) \
- ({ (thread)->context.r[0] = (uint32_t)&(thread)->context, \
- (thread)->context.r[1] = (uint32_t)start_thread, \
- (thread)->context.start = (uint32_t)function; })
-
-
-/*---------------------------------------------------------------------------
- * Store non-volatile context.
- *---------------------------------------------------------------------------
- */
-static inline void store_context(void* addr)
-{
- asm volatile(
- "stmia %0, { r4-r11, sp, lr } \n"
- : : "r" (addr)
- );
-}
-
-/*---------------------------------------------------------------------------
- * Load non-volatile context.
- *---------------------------------------------------------------------------
- */
-static inline void load_context(const void* addr)
-{
- asm volatile(
- "ldr r0, [%0, #40] \n" /* Load start pointer */
- "cmp r0, #0 \n" /* Check for NULL */
-
- /* If not already running, jump to start */
- "ldmneia %0, { r0, pc } \n"
- "ldmia %0, { r4-r11, sp, lr } \n" /* Load regs r4 to r14 from context */
- : : "r" (addr) : "r0" /* only! */
- );
-}
-
-/*
- * this core sleep suspends the OS thread rockbox runs under, which greatly
- * reduces cpu usage (~100% to <10%)
- *
- * it returns when when the tick timer is called, other interrupt-like
- * events occur
- *
- * wait_for_interrupt is implemented in kernel-<platform>.c
- **/
-
-static inline void core_sleep(void)
-{
- enable_irq();
- wait_for_interrupt();
-}
-
-
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 @@
-/***************************************************************************
- * __________ __ ___.
- * 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);
-}
-
-/*
- * play nice with the host and sleep while waiting for the tick */
-extern void wait_for_interrupt(void);
-static inline void core_sleep(void)
-{
- enable_irq();
- wait_for_interrupt();
-}
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 @@
-/***************************************************************************
- * __________ __ ___.
- * Open \______ \ ____ ____ | | _\_ |__ _______ ___
- * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
- * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
- * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
- * \/ \/ \/ \/ \/
- * $Id$
- *
- * Copyright (C) 2010 by Thomas Martitz
- *
- * Generic ARM 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 <windows.h>
-#include "system.h"
-
-#define INIT_MAIN_THREAD
-
-#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; })
-
-static void init_main_thread(void *addr)
-{
- struct regs *context = (struct regs*)addr;
- /* we must convert the current main thread to a fiber to be able to
- * schedule other fibers */
- context->uc = ConvertThreadToFiber(NULL);
- context->stack_size = 0;
-}
-
-static inline void store_context(void* addr)
-{
- (void)addr;
- /* nothing to do here, Fibers continue after the SwitchToFiber call */
-}
-
-static void start_thread(void)
-{
- void (*func)(void) = GetFiberData();
- func();
- /* go out if thread function returns */
- thread_exit();
-}
-
-/*
- * Load context and run it
- *
- * Resume execution from the last load_context call for the thread
- */
-
-static inline void load_context(const void* addr)
-{
- struct regs *context = (struct regs*)addr;
- if (UNLIKELY(context->start))
- { /* need setup before switching to it */
- context->uc = CreateFiber(context->stack_size,
- (LPFIBER_START_ROUTINE)start_thread, context->start);
- /* can't assign stack pointer, only stack size */
- context->stack_size = 0;
- context->start = NULL;
- }
- SwitchToFiber(context->uc);
-}
-
-/*
- * play nice with the host and sleep while waiting for the tick */
-static inline void core_sleep(void)
-{
- enable_irq();
- wait_for_interrupt();
-}
-
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 @@
#define __SYSTEM_TARGET_H__
#include "kernel-unix.h"
-
-static inline void commit_dcache(void) {}
-static inline void commit_discard_dcache(void) {}
-static inline void commit_discard_idcache(void) {}
+#include "system-hosted.h"
#define NEED_GENERIC_BYTESWAPS
#endif /* __SYSTEM_TARGET_H__ */