summaryrefslogtreecommitdiff
path: root/firmware/asm
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/asm
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/asm')
-rw-r--r--firmware/asm/arm/thread.c94
-rw-r--r--firmware/asm/arm/thread.h36
-rw-r--r--firmware/asm/m68k/thread.c101
-rw-r--r--firmware/asm/m68k/thread.h31
-rw-r--r--firmware/asm/mips/thread-mips32.c111
-rw-r--r--firmware/asm/mips/thread.c6
-rw-r--r--firmware/asm/mips/thread.h31
-rw-r--r--firmware/asm/sh/thread.c96
-rw-r--r--firmware/asm/sh/thread.h30
-rw-r--r--firmware/asm/thread-unix.c307
-rw-r--r--firmware/asm/thread-win32.c76
-rw-r--r--firmware/asm/thread.c21
-rw-r--r--firmware/asm/thread.h57
13 files changed, 997 insertions, 0 deletions
diff --git a/firmware/asm/arm/thread.c b/firmware/asm/arm/thread.c
new file mode 100644
index 0000000000..fd443f2873
--- /dev/null
+++ b/firmware/asm/arm/thread.c
@@ -0,0 +1,94 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 by Thom Johansen
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 * Start the thread running and terminate it if it returns
26 *---------------------------------------------------------------------------
27 */
28static void __attribute__((naked)) USED_ATTR start_thread(void)
29{
30 /* r0 = context */
31 asm volatile (
32 "ldr sp, [r0, #32] \n" /* Load initial sp */
33 "ldr r4, [r0, #40] \n" /* start in r4 since it's non-volatile */
34 "mov r1, #0 \n" /* Mark thread as running */
35 "str r1, [r0, #40] \n"
36#if NUM_CORES > 1
37 "ldr r0, =commit_discard_idcache \n" /* Invalidate this core's cache. */
38 "mov lr, pc \n" /* This could be the first entry into */
39 "bx r0 \n" /* plugin or codec code for this core. */
40#endif
41 "mov lr, pc \n" /* Call thread function */
42 "bx r4 \n"
43 ); /* No clobber list - new thread doesn't care */
44 thread_exit();
45#if 0
46 asm volatile (".ltorg"); /* Dump constant pool */
47#endif
48}
49
50/* For startup, place context pointer in r4 slot, start_thread pointer in r5
51 * slot, and thread function pointer in context.start. See load_context for
52 * what happens when thread is initially going to run. */
53#define THREAD_STARTUP_INIT(core, thread, function) \
54 ({ (thread)->context.r[0] = (uint32_t)&(thread)->context, \
55 (thread)->context.r[1] = (uint32_t)start_thread, \
56 (thread)->context.start = (uint32_t)function; })
57
58
59/*---------------------------------------------------------------------------
60 * Store non-volatile context.
61 *---------------------------------------------------------------------------
62 */
63static inline void store_context(void* addr)
64{
65 asm volatile(
66 "stmia %0, { r4-r11, sp, lr } \n"
67 : : "r" (addr)
68 );
69}
70
71/*---------------------------------------------------------------------------
72 * Load non-volatile context.
73 *---------------------------------------------------------------------------
74 */
75static inline void load_context(const void* addr)
76{
77 asm volatile(
78 "ldr r0, [%0, #40] \n" /* Load start pointer */
79 "cmp r0, #0 \n" /* Check for NULL */
80
81 /* If not already running, jump to start */
82#if ARM_ARCH == 4 && defined(USE_THUMB)
83 "ldmneia %0, { r0, r12 } \n"
84 "bxne r12 \n"
85#else
86 "ldmneia %0, { r0, pc } \n"
87#endif
88
89 "ldmia %0, { r4-r11, sp, lr } \n" /* Load regs r4 to r14 from context */
90 : : "r" (addr) : "r0" /* only! */
91 );
92}
93
94
diff --git a/firmware/asm/arm/thread.h b/firmware/asm/arm/thread.h
new file mode 100644
index 0000000000..ec9bbcb3cd
--- /dev/null
+++ b/firmware/asm/arm/thread.h
@@ -0,0 +1,36 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Ulf Ralberg
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#include "config.h"
23
24struct regs
25{
26 uint32_t r[8]; /* 0-28 - Registers r4-r11 */
27 uint32_t sp; /* 32 - Stack pointer (r13) */
28 uint32_t lr; /* 36 - r14 (lr) */
29 uint32_t start; /* 40 - Thread start address, or NULL when started */
30};
31
32#if (CONFIG_PLATFORM & PLATFORM_HOSTED)
33 #define DEFAULT_STACK_SIZE 0x1000 /* Bytes */
34#else
35 #define DEFAULT_STACK_SIZE 0x400 /* Bytes */
36#endif
diff --git a/firmware/asm/m68k/thread.c b/firmware/asm/m68k/thread.c
new file mode 100644
index 0000000000..7df89001d7
--- /dev/null
+++ b/firmware/asm/m68k/thread.c
@@ -0,0 +1,101 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2004 by Linus Nielsen Feltzing
11 *
12 * Coldfire processor 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 * Start the thread running and terminate it if it returns
26 *---------------------------------------------------------------------------
27 */
28void start_thread(void); /* Provide C access to ASM label */
29static void USED_ATTR __start_thread(void)
30{
31 /* a0=macsr, a1=context */
32 asm volatile (
33 "start_thread: \n" /* Start here - no naked attribute */
34 "move.l %a0, %macsr \n" /* Set initial mac status reg */
35 "lea.l 48(%a1), %a1 \n"
36 "move.l (%a1)+, %sp \n" /* Set initial stack */
37 "move.l (%a1), %a2 \n" /* Fetch thread function pointer */
38 "clr.l (%a1) \n" /* Mark thread running */
39 "jsr (%a2) \n" /* Call thread function */
40 );
41 thread_exit();
42}
43
44/* Set EMAC unit to fractional mode with saturation for each new thread,
45 * since that's what'll be the most useful for most things which the dsp
46 * will do. Codecs should still initialize their preferred modes
47 * explicitly. Context pointer is placed in d2 slot and start_thread
48 * pointer in d3 slot. thread function pointer is placed in context.start.
49 * See load_context for what happens when thread is initially going to
50 * run.
51 */
52#define THREAD_STARTUP_INIT(core, thread, function) \
53 ({ (thread)->context.macsr = EMAC_FRACTIONAL | EMAC_SATURATE, \
54 (thread)->context.d[0] = (uint32_t)&(thread)->context, \
55 (thread)->context.d[1] = (uint32_t)start_thread, \
56 (thread)->context.start = (uint32_t)(function); })
57
58/*---------------------------------------------------------------------------
59 * Store non-volatile context.
60 *---------------------------------------------------------------------------
61 */
62static inline void store_context(void* addr)
63{
64 asm volatile (
65 "move.l %%macsr,%%d0 \n"
66 "movem.l %%d0/%%d2-%%d7/%%a2-%%a7,(%0) \n"
67 : : "a" (addr) : "d0" /* only! */
68 );
69}
70
71/*---------------------------------------------------------------------------
72 * Load non-volatile context.
73 *---------------------------------------------------------------------------
74 */
75static inline void load_context(const void* addr)
76{
77 asm volatile (
78 "move.l 52(%0), %%d0 \n" /* Get start address */
79 "beq.b 1f \n" /* NULL -> already running */
80 "movem.l (%0), %%a0-%%a2 \n" /* a0=macsr, a1=context, a2=start_thread */
81 "jmp (%%a2) \n" /* Start the thread */
82 "1: \n"
83 "movem.l (%0), %%d0/%%d2-%%d7/%%a2-%%a7 \n" /* Load context */
84 "move.l %%d0, %%macsr \n"
85 : : "a" (addr) : "d0" /* only! */
86 );
87}
88
89/*---------------------------------------------------------------------------
90 * Call this from asm to make sure the sp is pointing to the
91 * correct place before the context is saved.
92 *---------------------------------------------------------------------------
93 */
94static inline void _profile_thread_stopped(int current_thread)
95{
96 asm volatile ("move.l %[id], -(%%sp)\n\t"
97 "jsr profile_thread_stopped\n\t"
98 "addq.l #4, %%sp\n\t"
99 :: [id] "r" (current_thread)
100 : "cc", "memory");
101}
diff --git a/firmware/asm/m68k/thread.h b/firmware/asm/m68k/thread.h
new file mode 100644
index 0000000000..9bdbed0c3e
--- /dev/null
+++ b/firmware/asm/m68k/thread.h
@@ -0,0 +1,31 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Ulf Ralberg
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
22struct regs
23{
24 uint32_t macsr; /* 0 - EMAC status register */
25 uint32_t d[6]; /* 4-24 - d2-d7 */
26 uint32_t a[5]; /* 28-44 - a2-a6 */
27 uint32_t sp; /* 48 - Stack pointer (a7) */
28 uint32_t start; /* 52 - Thread start address, or NULL when started */
29};
30
31#define DEFAULT_STACK_SIZE 0x400 /* Bytes */
diff --git a/firmware/asm/mips/thread-mips32.c b/firmware/asm/mips/thread-mips32.c
new file mode 100644
index 0000000000..e754df7e29
--- /dev/null
+++ b/firmware/asm/mips/thread-mips32.c
@@ -0,0 +1,111 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2008 by Maurus Cuelenaere
11 *
12 * 32-bit MIPS 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 * Start the thread running and terminate it if it returns
26 *---------------------------------------------------------------------------
27 */
28
29void start_thread(void); /* Provide C access to ASM label */
30static void USED_ATTR _start_thread(void)
31{
32 /* t1 = context */
33 asm volatile (
34 "start_thread: \n"
35 ".set noreorder \n"
36 ".set noat \n"
37 "lw $8, 4($9) \n" /* Fetch thread function pointer ($8 = t0, $9 = t1) */
38 "lw $29, 36($9) \n" /* Set initial sp(=$29) */
39 "jalr $8 \n" /* Start the thread */
40 "sw $0, 44($9) \n" /* Clear start address */
41 ".set at \n"
42 ".set reorder \n"
43 );
44 thread_exit();
45}
46
47/* Place context pointer in s0 slot, function pointer in s1 slot, and
48 * start_thread pointer in context_start */
49#define THREAD_STARTUP_INIT(core, thread, function) \
50 ({ (thread)->context.r[0] = (uint32_t)&(thread)->context, \
51 (thread)->context.r[1] = (uint32_t)(function), \
52 (thread)->context.start = (uint32_t)start_thread; })
53
54/*---------------------------------------------------------------------------
55 * Store non-volatile context.
56 *---------------------------------------------------------------------------
57 */
58static inline void store_context(void* addr)
59{
60 asm volatile (
61 ".set noreorder \n"
62 ".set noat \n"
63 "sw $16, 0(%0) \n" /* s0 */
64 "sw $17, 4(%0) \n" /* s1 */
65 "sw $18, 8(%0) \n" /* s2 */
66 "sw $19, 12(%0) \n" /* s3 */
67 "sw $20, 16(%0) \n" /* s4 */
68 "sw $21, 20(%0) \n" /* s5 */
69 "sw $22, 24(%0) \n" /* s6 */
70 "sw $23, 28(%0) \n" /* s7 */
71 "sw $30, 32(%0) \n" /* fp */
72 "sw $29, 36(%0) \n" /* sp */
73 "sw $31, 40(%0) \n" /* ra */
74 ".set at \n"
75 ".set reorder \n"
76 : : "r" (addr)
77 );
78}
79
80/*---------------------------------------------------------------------------
81 * Load non-volatile context.
82 *---------------------------------------------------------------------------
83 */
84static inline void load_context(const void* addr)
85{
86 asm volatile (
87 ".set noat \n"
88 ".set noreorder \n"
89 "lw $8, 44(%0) \n" /* Get start address ($8 = t0) */
90 "beqz $8, running \n" /* NULL -> already running */
91 "nop \n"
92 "jr $8 \n"
93 "move $9, %0 \n" /* t1 = context */
94 "running: \n"
95 "lw $16, 0(%0) \n" /* s0 */
96 "lw $17, 4(%0) \n" /* s1 */
97 "lw $18, 8(%0) \n" /* s2 */
98 "lw $19, 12(%0) \n" /* s3 */
99 "lw $20, 16(%0) \n" /* s4 */
100 "lw $21, 20(%0) \n" /* s5 */
101 "lw $22, 24(%0) \n" /* s6 */
102 "lw $23, 28(%0) \n" /* s7 */
103 "lw $30, 32(%0) \n" /* fp */
104 "lw $29, 36(%0) \n" /* sp */
105 "lw $31, 40(%0) \n" /* ra */
106 ".set at \n"
107 ".set reorder \n"
108 : : "r" (addr) : "t0", "t1"
109 );
110}
111
diff --git a/firmware/asm/mips/thread.c b/firmware/asm/mips/thread.c
new file mode 100644
index 0000000000..37480da10a
--- /dev/null
+++ b/firmware/asm/mips/thread.c
@@ -0,0 +1,6 @@
1#if CPU_MIPS == 32
2 #include "thread-mips32.c"
3#else
4 #error Missing thread impl
5#endif
6
diff --git a/firmware/asm/mips/thread.h b/firmware/asm/mips/thread.h
new file mode 100644
index 0000000000..ac37560a68
--- /dev/null
+++ b/firmware/asm/mips/thread.h
@@ -0,0 +1,31 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Ulf Ralberg
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
22struct regs
23{
24 uint32_t r[9]; /* 0-32 - Registers s0-s7, fp */
25 uint32_t sp; /* 36 - Stack pointer */
26 uint32_t ra; /* 40 - Return address */
27 uint32_t start; /* 44 - Thread start address, or NULL when started */
28};
29
30#define DEFAULT_STACK_SIZE 0x400 /* Bytes */
31
diff --git a/firmware/asm/sh/thread.c b/firmware/asm/sh/thread.c
new file mode 100644
index 0000000000..e63470c4a1
--- /dev/null
+++ b/firmware/asm/sh/thread.c
@@ -0,0 +1,96 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Ulf Ralberg
11 *
12 * SH processor 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 * Start the thread running and terminate it if it returns
26 *---------------------------------------------------------------------------
27 */
28void start_thread(void); /* Provide C access to ASM label */
29static void USED_ATTR __start_thread(void)
30{
31 /* r8 = context */
32 asm volatile (
33 "_start_thread: \n" /* Start here - no naked attribute */
34 "mov.l @(4, r8), r0 \n" /* Fetch thread function pointer */
35 "mov.l @(28, r8), r15 \n" /* Set initial sp */
36 "mov #0, r1 \n" /* Start the thread */
37 "jsr @r0 \n"
38 "mov.l r1, @(36, r8) \n" /* Clear start address */
39 );
40 thread_exit();
41}
42
43/* Place context pointer in r8 slot, function pointer in r9 slot, and
44 * start_thread pointer in context_start */
45#define THREAD_STARTUP_INIT(core, thread, function) \
46 ({ (thread)->context.r[0] = (uint32_t)&(thread)->context, \
47 (thread)->context.r[1] = (uint32_t)(function), \
48 (thread)->context.start = (uint32_t)start_thread; })
49
50/*---------------------------------------------------------------------------
51 * Store non-volatile context.
52 *---------------------------------------------------------------------------
53 */
54static inline void store_context(void* addr)
55{
56 asm volatile (
57 "add #36, %0 \n" /* Start at last reg. By the time routine */
58 "sts.l pr, @-%0 \n" /* is done, %0 will have the original value */
59 "mov.l r15,@-%0 \n"
60 "mov.l r14,@-%0 \n"
61 "mov.l r13,@-%0 \n"
62 "mov.l r12,@-%0 \n"
63 "mov.l r11,@-%0 \n"
64 "mov.l r10,@-%0 \n"
65 "mov.l r9, @-%0 \n"
66 "mov.l r8, @-%0 \n"
67 : : "r" (addr)
68 );
69}
70
71/*---------------------------------------------------------------------------
72 * Load non-volatile context.
73 *---------------------------------------------------------------------------
74 */
75static inline void load_context(const void* addr)
76{
77 asm volatile (
78 "mov.l @(36, %0), r0 \n" /* Get start address */
79 "tst r0, r0 \n"
80 "bt .running \n" /* NULL -> already running */
81 "jmp @r0 \n" /* r8 = context */
82 ".running: \n"
83 "mov.l @%0+, r8 \n" /* Executes in delay slot and outside it */
84 "mov.l @%0+, r9 \n"
85 "mov.l @%0+, r10 \n"
86 "mov.l @%0+, r11 \n"
87 "mov.l @%0+, r12 \n"
88 "mov.l @%0+, r13 \n"
89 "mov.l @%0+, r14 \n"
90 "mov.l @%0+, r15 \n"
91 "lds.l @%0+, pr \n"
92 : : "r" (addr) : "r0" /* only! */
93 );
94}
95
96
diff --git a/firmware/asm/sh/thread.h b/firmware/asm/sh/thread.h
new file mode 100644
index 0000000000..aa5fe519c6
--- /dev/null
+++ b/firmware/asm/sh/thread.h
@@ -0,0 +1,30 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Ulf Ralberg
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
22struct regs
23{
24 uint32_t r[7]; /* 0-24 - Registers r8 thru r14 */
25 uint32_t sp; /* 28 - Stack pointer (r15) */
26 uint32_t pr; /* 32 - Procedure register */
27 uint32_t start; /* 36 - Thread start address, or NULL when started */
28};
29
30#define DEFAULT_STACK_SIZE 0x400 /* Bytes */
diff --git a/firmware/asm/thread-unix.c b/firmware/asm/thread-unix.c
new file mode 100644
index 0000000000..3c5e7c96ee
--- /dev/null
+++ b/firmware/asm/thread-unix.c
@@ -0,0 +1,307 @@
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}
diff --git a/firmware/asm/thread-win32.c b/firmware/asm/thread-win32.c
new file mode 100644
index 0000000000..9125819ade
--- /dev/null
+++ b/firmware/asm/thread-win32.c
@@ -0,0 +1,76 @@
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}
diff --git a/firmware/asm/thread.c b/firmware/asm/thread.c
new file mode 100644
index 0000000000..49e71d73af
--- /dev/null
+++ b/firmware/asm/thread.c
@@ -0,0 +1,21 @@
1#if defined(CPU_ARM)
2 #include "arm/thread.c"
3#elif defined(CPU_COLDFIRE)
4 #include "m68k/thread.c"
5#elif CONFIG_CPU == SH7034
6 #include "sh/thread.c"
7#elif defined(CPU_MIPS)
8 #include "mips/thread.c"
9#else
10
11/* generic thread.c */
12
13#if defined(HAVE_WIN32_FIBER_THREADS)
14 #include "thread-win32.c"
15#elif defined(HAVE_SIGALTSTACK_THREADS)
16 #include "thread-unix.c"
17#else
18 #error Missing thread impl
19#endif
20
21#endif
diff --git a/firmware/asm/thread.h b/firmware/asm/thread.h
new file mode 100644
index 0000000000..9bdff3881e
--- /dev/null
+++ b/firmware/asm/thread.h
@@ -0,0 +1,57 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Ulf Ralberg
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 __ASM_THREAD_H__
23#define __ASM_THREAD_H__
24#include "config.h"
25
26#if defined(CPU_ARM)
27 #include "arm/thread.h"
28#elif defined(CPU_COLDFIRE)
29 #include "m68k/thread.h"
30#elif CONFIG_CPU == SH7034
31 #include "sh/thread.h"
32#elif defined(CPU_MIPS)
33 #include "mips/thread.h"
34#else
35
36/* generic thread.h */
37
38struct regs
39{
40 void (*start)(void); /* thread's entry point, or NULL when started */
41 void* uc; /* host thread handle */
42 uintptr_t sp; /* Stack pointer, unused */
43 size_t stack_size; /* stack size, not always used */
44 uintptr_t stack; /* pointer to start of the stack buffer */
45};
46
47#ifdef HAVE_SIGALTSTACK_THREADS
48 #include <signal.h>
49 /* MINSIGSTKSZ for the OS to deliver the signal + 0x3000 for us */
50 #define DEFAULT_STACK_SIZE (MINSIGSTKSZ+0x3000) /* Bytes */
51#elif defined(HAVE_WIN32_FIBER_THREADS)
52 #define DEFAULT_STACK_SIZE 0x1000 /* Bytes */
53#endif
54
55#endif /* __ASM_THREAD_H__ */
56
57#endif