2002-04-22 12:07:34 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Ulf Ralberg
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "thread.h"
|
2002-07-15 22:21:18 +00:00
|
|
|
#include "panic.h"
|
2002-04-22 12:07:34 +00:00
|
|
|
|
2002-06-25 12:04:23 +00:00
|
|
|
struct regs
|
2002-04-22 12:07:34 +00:00
|
|
|
{
|
2002-06-25 12:04:23 +00:00
|
|
|
unsigned int r[7]; /* Registers r8 thru r14 */
|
|
|
|
void *sp; /* Stack pointer (r15) */
|
|
|
|
unsigned int mach;
|
|
|
|
unsigned int macl;
|
|
|
|
unsigned int sr; /* Status register */
|
|
|
|
void* pr; /* Procedure register */
|
|
|
|
};
|
2002-04-22 12:07:34 +00:00
|
|
|
|
2002-07-15 22:21:18 +00:00
|
|
|
int num_threads;
|
2002-06-25 12:04:23 +00:00
|
|
|
static int current_thread;
|
2002-08-01 08:14:56 +00:00
|
|
|
static struct regs thread_contexts[MAXTHREADS] __attribute__ ((section(".idata")));
|
2002-07-15 22:21:18 +00:00
|
|
|
char *thread_name[MAXTHREADS];
|
|
|
|
void *thread_stack[MAXTHREADS];
|
|
|
|
int thread_stack_size[MAXTHREADS];
|
|
|
|
static char main_thread_name[] = "main";
|
|
|
|
|
|
|
|
extern int stackbegin[];
|
|
|
|
extern int stackend[];
|
2002-04-22 12:07:34 +00:00
|
|
|
|
2002-08-01 08:14:56 +00:00
|
|
|
void switch_thread(void) __attribute__ ((section(".icode")));
|
|
|
|
|
2002-04-22 12:07:34 +00:00
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Store non-volatile context.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2002-06-25 12:04:23 +00:00
|
|
|
static inline void store_context(void* addr)
|
2002-04-22 12:07:34 +00:00
|
|
|
{
|
2002-04-25 13:20:43 +00:00
|
|
|
asm volatile ("add #48, %0\n\t"
|
|
|
|
"sts.l pr, @-%0\n\t"
|
2002-04-25 12:23:37 +00:00
|
|
|
"stc.l sr, @-%0\n\t"
|
2002-04-25 12:43:55 +00:00
|
|
|
"sts.l macl,@-%0\n\t"
|
|
|
|
"sts.l mach,@-%0\n\t"
|
2002-04-25 12:23:37 +00:00
|
|
|
"mov.l r15, @-%0\n\t"
|
|
|
|
"mov.l r14, @-%0\n\t"
|
|
|
|
"mov.l r13, @-%0\n\t"
|
|
|
|
"mov.l r12, @-%0\n\t"
|
|
|
|
"mov.l r11, @-%0\n\t"
|
|
|
|
"mov.l r10, @-%0\n\t"
|
|
|
|
"mov.l r9, @-%0\n\t"
|
2002-04-25 13:20:43 +00:00
|
|
|
"mov.l r8, @-%0" : : "r" (addr));
|
2002-04-22 12:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Load non-volatile context.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2002-06-25 12:04:23 +00:00
|
|
|
static inline void load_context(void* addr)
|
2002-04-22 12:07:34 +00:00
|
|
|
{
|
2002-04-25 12:23:37 +00:00
|
|
|
asm volatile ("mov.l @%0+,r8\n\t"
|
|
|
|
"mov.l @%0+,r9\n\t"
|
|
|
|
"mov.l @%0+,r10\n\t"
|
|
|
|
"mov.l @%0+,r11\n\t"
|
|
|
|
"mov.l @%0+,r12\n\t"
|
|
|
|
"mov.l @%0+,r13\n\t"
|
|
|
|
"mov.l @%0+,r14\n\t"
|
|
|
|
"mov.l @%0+,r15\n\t"
|
2002-04-25 12:43:55 +00:00
|
|
|
"lds.l @%0+,mach\n\t"
|
|
|
|
"lds.l @%0+,macl\n\t"
|
2002-04-25 12:30:39 +00:00
|
|
|
"ldc.l @%0+,sr\n\t"
|
2002-04-25 13:20:43 +00:00
|
|
|
"mov.l @%0,%0\n\t"
|
|
|
|
"lds %0,pr\n\t"
|
|
|
|
"mov.l %0, @(0, r15)" : "+r" (addr));
|
2002-04-22 12:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
* Switch thread in round robin fashion.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2002-06-07 14:56:10 +00:00
|
|
|
void switch_thread(void)
|
2002-04-22 12:07:34 +00:00
|
|
|
{
|
2002-06-25 12:04:23 +00:00
|
|
|
int current;
|
|
|
|
int next;
|
2002-07-15 22:21:18 +00:00
|
|
|
unsigned int *stackptr;
|
2002-04-22 12:07:34 +00:00
|
|
|
|
2002-06-25 12:04:23 +00:00
|
|
|
next = current = current_thread;
|
|
|
|
if (++next >= num_threads)
|
|
|
|
next = 0;
|
|
|
|
current_thread = next;
|
|
|
|
store_context(&thread_contexts[current]);
|
|
|
|
load_context(&thread_contexts[next]);
|
2002-07-15 22:21:18 +00:00
|
|
|
|
|
|
|
stackptr = thread_stack[next];
|
|
|
|
|
|
|
|
if(stackptr[0] != 0xdeadbeef)
|
|
|
|
panicf("Stkov %s", thread_name[next]);
|
2002-04-22 12:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
2002-04-25 13:25:54 +00:00
|
|
|
* Create thread.
|
2002-04-22 12:07:34 +00:00
|
|
|
* Return 0 if context area could be allocated, else -1.
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*/
|
2002-07-15 22:21:18 +00:00
|
|
|
int create_thread(void* function, void* stack, int stack_size, char *name)
|
2002-04-22 12:07:34 +00:00
|
|
|
{
|
2002-07-01 21:14:20 +00:00
|
|
|
unsigned int i;
|
|
|
|
unsigned int stacklen;
|
|
|
|
unsigned int *stackptr;
|
2002-07-15 22:21:18 +00:00
|
|
|
struct regs *regs;
|
2002-07-01 21:14:20 +00:00
|
|
|
|
2002-06-25 12:04:23 +00:00
|
|
|
if (num_threads >= MAXTHREADS)
|
2002-04-22 23:41:29 +00:00
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
{
|
2002-07-01 21:14:20 +00:00
|
|
|
/* Munge the stack to make it easy to spot stack overflows */
|
|
|
|
stacklen = stack_size / 4;
|
|
|
|
stackptr = stack;
|
|
|
|
for(i = 0;i < stacklen;i++)
|
|
|
|
{
|
|
|
|
stackptr[i] = 0xdeadbeef;
|
|
|
|
}
|
|
|
|
|
2002-07-15 22:21:18 +00:00
|
|
|
/* Store interesting information */
|
|
|
|
thread_name[num_threads] = name;
|
|
|
|
thread_stack[num_threads] = stack;
|
|
|
|
thread_stack_size[num_threads] = stack_size;
|
|
|
|
regs = &thread_contexts[num_threads++];
|
2002-06-25 12:04:23 +00:00
|
|
|
store_context(regs);
|
2002-07-15 22:21:18 +00:00
|
|
|
/* Subtract 4 to leave room for the PR push in load_context()
|
2002-05-28 13:59:24 +00:00
|
|
|
Align it on an even 32 bit boundary */
|
2002-06-25 12:04:23 +00:00
|
|
|
regs->sp = (void*)(((unsigned int)stack + stack_size - 4) & ~3);
|
|
|
|
regs->sr = 0;
|
|
|
|
regs->pr = function;
|
2002-04-22 23:41:29 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2002-04-22 12:07:34 +00:00
|
|
|
}
|
2002-06-07 14:56:10 +00:00
|
|
|
|
|
|
|
void init_threads(void)
|
|
|
|
{
|
2002-06-25 12:04:23 +00:00
|
|
|
num_threads = 1; /* We have 1 thread to begin with */
|
|
|
|
current_thread = 0; /* The current thread is number 0 */
|
2002-07-15 22:21:18 +00:00
|
|
|
thread_name[0] = main_thread_name;
|
|
|
|
thread_stack[0] = stackbegin;
|
|
|
|
thread_stack_size[0] = (int)stackend - (int)stackbegin;
|
|
|
|
}
|
|
|
|
|
|
|
|
int thread_stack_usage(int threadnum)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
unsigned int *stackptr = thread_stack[threadnum];
|
|
|
|
|
|
|
|
if(threadnum >= num_threads)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for(i = 0;i < thread_stack_size[threadnum]/sizeof(int);i++)
|
|
|
|
{
|
|
|
|
if(stackptr[i] != 0xdeadbeef)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((thread_stack_size[threadnum] - i * 4) * 100) /
|
|
|
|
thread_stack_size[threadnum];
|
2002-06-07 14:56:10 +00:00
|
|
|
}
|