support for contiki-node simulation on 64-bit architectures (contributed by Jesper Karlsson and David Gustafsson)

This commit is contained in:
fros4943 2008-11-28 16:41:26 +00:00
parent eb2ecc69a2
commit 6150d13254

View file

@ -28,15 +28,39 @@
* *
* This file is part of the Contiki operating system. * This file is part of the Contiki operating system.
* *
* @(#)$Id: cooja_mtarch.c,v 1.5 2007/04/02 08:47:28 fros4943 Exp $ * @(#)$Id: cooja_mtarch.c,v 1.6 2008/11/28 16:41:26 fros4943 Exp $
*/ */
#include <stddef.h>
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "sys/cooja_mt.h" #include "sys/cooja_mt.h"
#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 */
struct frame { struct frame {
unsigned long flags; unsigned long flags;
#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 */
unsigned long ebp; unsigned long ebp;
unsigned long edi; unsigned long edi;
unsigned long esi; unsigned long esi;
@ -44,6 +68,7 @@ struct frame {
unsigned long ecx; unsigned long ecx;
unsigned long ebx; unsigned long ebx;
unsigned long eax; unsigned long eax;
#endif /* ON_64BIT_ARCH */
unsigned long retaddr; unsigned long retaddr;
unsigned long retaddr2; unsigned long retaddr2;
unsigned long data; unsigned long data;
@ -56,20 +81,24 @@ cooja_mtarch_init(void)
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
void void
cooja_mtarch_start(struct cooja_mtarch_thread *t, cooja_mtarch_start(struct cooja_mtarch_thread *t,
void (*function)(void *), void *data) void (*function)(void *), void *data)
{ {
struct frame *f = (struct frame *)&t->stack[COOJA_MTARCH_STACKSIZE - sizeof(struct frame)/4]; struct frame *f = (struct frame *)&t->stack[COOJA_MTARCH_STACKSIZE - sizeof(struct frame)/4];
int i; int i;
for(i = 0; i < COOJA_MTARCH_STACKSIZE; ++i) { for(i = 0; i < COOJA_MTARCH_STACKSIZE; ++i) {
t->stack[i] = i; t->stack[i] = i;
} }
memset(f, 0, sizeof(struct frame)); memset(f, 0, sizeof(struct frame));
f->retaddr = (unsigned long)function; f->retaddr = (unsigned long)function;
f->data = (unsigned long)data; f->data = (unsigned long)data;
t->sp = (unsigned long)&f->flags; t->sp = (unsigned long)&f->flags;
#if ON_64BIT_ARCH
f->rbp = (unsigned long)&f->rax;
#else /* ON_64BIT_ARCH */
f->ebp = (unsigned long)&f->eax; f->ebp = (unsigned long)&f->eax;
#endif /* ON_64BIT_ARCH */
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
static struct cooja_mtarch_thread *cooja_running_thread; static struct cooja_mtarch_thread *cooja_running_thread;
@ -77,39 +106,78 @@ static struct cooja_mtarch_thread *cooja_running_thread;
void cooja_sw(void) void cooja_sw(void)
{ {
/* Store registers */ /* Store registers */
#if ON_64BIT_ARCH
__asm__ ( __asm__ (
"pushl %eax\n\t" "pushq %rax\n\t"
"pushl %ebx\n\t" "pushq %rbx\n\t"
"pushl %ecx\n\t" "pushq %rcx\n\t"
"pushl %edx\n\t" "pushq %rdx\n\t"
"pushl %esi\n\t" "pushq %rsi\n\t"
"pushl %edi\n\t" "pushq %rdi\n\t"
"pushl %ebp\n\t" "pushq %rbp\n\t"
"pushl %ebp\n\t"); "pushq %rbp\n\t");
#else /* ON_64BIT_ARCH */
__asm__ (
"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 */
/* Switch stack pointer */ /* Switch stack pointer */
#if ON_64BIT_ARCH
__asm__ ("movq %0, %%rax\n\t" : : "m" (cooja_running_thread));
__asm__ (
"movq 0(%rax), %rbx\n\t"
"movq %rsp, 0(%rax)\n\t"
"movq %rbx, %rsp\n\t"
);
#else /* ON_64BIT_ARCH */
__asm__ ("movl %0, %%eax\n\t" : : "m" (cooja_running_thread)); __asm__ ("movl %0, %%eax\n\t" : : "m" (cooja_running_thread));
__asm__ ( __asm__ (
"movl 0(%eax), %ebx\n\t" "movl 0(%eax), %ebx\n\t"
"movl %esp, 0(%eax)\n\t" "movl %esp, 0(%eax)\n\t"
"movl %ebx, %esp\n\t" "movl %ebx, %esp\n\t"
); );
#endif /* ON_64BIT_ARCH */
/* Restore stored registers and return "our" way */
/* Restore previous registers */
#if ON_64BIT_ARCH
__asm__ ( __asm__ (
"popl %ebp\n\t" "popq %rbp\n\t"
"popl %ebp\n\t" "popq %rbp\n\t"
"popl %edi\n\t" "popq %rdi\n\t"
"popl %esi\n\t" "popq %rsi\n\t"
"popl %edx\n\t" "popq %rdx\n\t"
"popl %ecx\n\t" "popq %rcx\n\t"
"popl %ebx\n\t" "popq %rbx\n\t"
"popl %eax\n\t" "popq %rax\n\t"
"leave\n\t" "leave\n\t"
"ret\n\t" "ret\n\t"
); );
#else /* ON_64BIT_ARCH */
__asm__ (
"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 */
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
void void
cooja_mtarch_exec(struct cooja_mtarch_thread *t) cooja_mtarch_exec(struct cooja_mtarch_thread *t)