2006-09-29 16:32:15 +02:00
|
|
|
/*
|
2007-04-02 10:47:28 +02:00
|
|
|
* Copyright (c) 2005, Swedish Institute of Computer Science
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
2009-03-13 15:40:07 +01:00
|
|
|
* @(#)$Id: cooja_mtarch.c,v 1.7 2009/03/13 14:42:06 fros4943 Exp $
|
2006-09-29 16:32:15 +02:00
|
|
|
*/
|
|
|
|
|
2008-11-28 17:41:26 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
2006-09-29 16:32:15 +02:00
|
|
|
#include <stdio.h>
|
2007-04-02 10:47:28 +02:00
|
|
|
#include <string.h>
|
2006-09-29 16:32:15 +02:00
|
|
|
#include "sys/cooja_mt.h"
|
|
|
|
|
2008-11-28 17:41:26 +01:00
|
|
|
#ifndef __WORDSIZE
|
|
|
|
#define __WORDSIZE 32
|
|
|
|
#endif /* __WORDSIZE */
|
|
|
|
|
|
|
|
#ifndef ON_64BIT_ARCH
|
|
|
|
#if __WORDSIZE == 64
|
|
|
|
#define ON_64BIT_ARCH 1
|
|
|
|
#else /* ON_64BIT_ARCH */
|
|
|
|
#define ON_64BIT_ARCH 0
|
|
|
|
#endif /* __WORDSIZE == 64 */
|
|
|
|
#endif /* ON_64BIT_ARCH */
|
|
|
|
|
2006-09-29 16:32:15 +02:00
|
|
|
struct frame {
|
|
|
|
unsigned long flags;
|
2008-11-28 17:41:26 +01:00
|
|
|
#if ON_64BIT_ARCH
|
|
|
|
unsigned long rbp;
|
|
|
|
unsigned long rdi;
|
|
|
|
unsigned long rsi;
|
|
|
|
unsigned long rdx;
|
|
|
|
unsigned long rcx;
|
|
|
|
unsigned long rbx;
|
|
|
|
unsigned long rax;
|
|
|
|
#else /* ON_64BIT_ARCH */
|
2006-09-29 16:32:15 +02:00
|
|
|
unsigned long ebp;
|
|
|
|
unsigned long edi;
|
|
|
|
unsigned long esi;
|
|
|
|
unsigned long edx;
|
|
|
|
unsigned long ecx;
|
|
|
|
unsigned long ebx;
|
|
|
|
unsigned long eax;
|
2008-11-28 17:41:26 +01:00
|
|
|
#endif /* ON_64BIT_ARCH */
|
2006-09-29 16:32:15 +02:00
|
|
|
unsigned long retaddr;
|
|
|
|
unsigned long retaddr2;
|
|
|
|
unsigned long data;
|
|
|
|
};
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
cooja_mtarch_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
cooja_mtarch_start(struct cooja_mtarch_thread *t,
|
2008-11-28 17:41:26 +01:00
|
|
|
void (*function)(void *), void *data)
|
2006-09-29 16:32:15 +02:00
|
|
|
{
|
2009-03-13 15:40:07 +01:00
|
|
|
struct frame *f = (struct frame *)&t->stack[COOJA_MTARCH_STACKSIZE - sizeof(struct frame)/sizeof(unsigned long)];
|
2006-09-29 16:32:15 +02:00
|
|
|
int i;
|
2008-11-28 17:41:26 +01:00
|
|
|
|
2006-09-29 16:32:15 +02:00
|
|
|
for(i = 0; i < COOJA_MTARCH_STACKSIZE; ++i) {
|
|
|
|
t->stack[i] = i;
|
|
|
|
}
|
2008-11-28 17:41:26 +01:00
|
|
|
|
2006-09-29 16:32:15 +02:00
|
|
|
memset(f, 0, sizeof(struct frame));
|
|
|
|
f->retaddr = (unsigned long)function;
|
|
|
|
f->data = (unsigned long)data;
|
|
|
|
t->sp = (unsigned long)&f->flags;
|
2008-11-28 17:41:26 +01:00
|
|
|
#if ON_64BIT_ARCH
|
|
|
|
f->rbp = (unsigned long)&f->rax;
|
|
|
|
#else /* ON_64BIT_ARCH */
|
2006-09-29 16:32:15 +02:00
|
|
|
f->ebp = (unsigned long)&f->eax;
|
2008-11-28 17:41:26 +01:00
|
|
|
#endif /* ON_64BIT_ARCH */
|
2006-09-29 16:32:15 +02:00
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static struct cooja_mtarch_thread *cooja_running_thread;
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2007-04-02 10:47:28 +02:00
|
|
|
void cooja_sw(void)
|
|
|
|
{
|
|
|
|
/* Store registers */
|
2008-11-28 17:41:26 +01:00
|
|
|
#if ON_64BIT_ARCH
|
|
|
|
__asm__ (
|
|
|
|
"pushq %rax\n\t"
|
|
|
|
"pushq %rbx\n\t"
|
|
|
|
"pushq %rcx\n\t"
|
|
|
|
"pushq %rdx\n\t"
|
|
|
|
"pushq %rsi\n\t"
|
|
|
|
"pushq %rdi\n\t"
|
|
|
|
"pushq %rbp\n\t"
|
|
|
|
"pushq %rbp\n\t");
|
|
|
|
#else /* ON_64BIT_ARCH */
|
2007-04-02 10:47:28 +02:00
|
|
|
__asm__ (
|
2008-11-28 17:41:26 +01:00
|
|
|
"pushl %eax\n\t"
|
|
|
|
"pushl %ebx\n\t"
|
|
|
|
"pushl %ecx\n\t"
|
|
|
|
"pushl %edx\n\t"
|
|
|
|
"pushl %esi\n\t"
|
|
|
|
"pushl %edi\n\t"
|
|
|
|
"pushl %ebp\n\t"
|
|
|
|
"pushl %ebp\n\t");
|
|
|
|
#endif /* ON_64BIT_ARCH */
|
|
|
|
|
2007-04-02 10:47:28 +02:00
|
|
|
/* Switch stack pointer */
|
2008-11-28 17:41:26 +01:00
|
|
|
#if ON_64BIT_ARCH
|
|
|
|
__asm__ ("movq %0, %%rax\n\t" : : "m" (cooja_running_thread));
|
|
|
|
__asm__ (
|
2009-03-13 15:40:07 +01:00
|
|
|
"movq (%rax), %rbx\n\t"
|
|
|
|
"movq %rsp, (%rax)\n\t"
|
2008-11-28 17:41:26 +01:00
|
|
|
"movq %rbx, %rsp\n\t"
|
|
|
|
);
|
|
|
|
#else /* ON_64BIT_ARCH */
|
2007-04-02 10:47:28 +02:00
|
|
|
__asm__ ("movl %0, %%eax\n\t" : : "m" (cooja_running_thread));
|
|
|
|
__asm__ (
|
2009-03-13 15:40:07 +01:00
|
|
|
"movl (%eax), %ebx\n\t"
|
|
|
|
"movl %esp, (%eax)\n\t"
|
2008-11-28 17:41:26 +01:00
|
|
|
"movl %ebx, %esp\n\t"
|
|
|
|
);
|
|
|
|
#endif /* ON_64BIT_ARCH */
|
|
|
|
|
|
|
|
/* Restore previous registers */
|
|
|
|
#if ON_64BIT_ARCH
|
|
|
|
__asm__ (
|
|
|
|
"popq %rbp\n\t"
|
|
|
|
"popq %rbp\n\t"
|
|
|
|
"popq %rdi\n\t"
|
|
|
|
"popq %rsi\n\t"
|
|
|
|
"popq %rdx\n\t"
|
|
|
|
"popq %rcx\n\t"
|
|
|
|
"popq %rbx\n\t"
|
|
|
|
"popq %rax\n\t"
|
|
|
|
|
|
|
|
"leave\n\t"
|
|
|
|
"ret\n\t"
|
|
|
|
);
|
|
|
|
#else /* ON_64BIT_ARCH */
|
2007-04-02 10:47:28 +02:00
|
|
|
__asm__ (
|
2008-11-28 17:41:26 +01:00
|
|
|
"popl %ebp\n\t"
|
|
|
|
"popl %ebp\n\t"
|
|
|
|
"popl %edi\n\t"
|
|
|
|
"popl %esi\n\t"
|
|
|
|
"popl %edx\n\t"
|
|
|
|
"popl %ecx\n\t"
|
|
|
|
"popl %ebx\n\t"
|
|
|
|
"popl %eax\n\t"
|
|
|
|
|
|
|
|
"leave\n\t"
|
|
|
|
"ret\n\t"
|
|
|
|
);
|
|
|
|
#endif /* ON_64BIT_ARCH */
|
|
|
|
|
2007-04-02 10:47:28 +02:00
|
|
|
}
|
2008-11-28 17:41:26 +01:00
|
|
|
|
2007-04-02 10:47:28 +02:00
|
|
|
/*--------------------------------------------------------------------------*/
|
2006-09-29 16:32:15 +02:00
|
|
|
void
|
|
|
|
cooja_mtarch_exec(struct cooja_mtarch_thread *t)
|
|
|
|
{
|
|
|
|
cooja_running_thread = t;
|
|
|
|
cooja_sw();
|
|
|
|
cooja_running_thread = NULL;
|
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
cooja_mtarch_remove(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
cooja_mtarch_yield(void)
|
|
|
|
{
|
|
|
|
cooja_sw();
|
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
cooja_mtarch_pstop(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
cooja_mtarch_pstart(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
cooja_mtarch_stack_usage(struct cooja_mt_thread *t)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < COOJA_MTARCH_STACKSIZE; ++i) {
|
|
|
|
if(t->thread.stack[i] != i) {
|
|
|
|
return COOJA_MTARCH_STACKSIZE - i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|