2013-03-06 10:29:49 +01:00
|
|
|
/**
|
|
|
|
* Native CPU kernel_intern.h and sched.h implementation
|
|
|
|
*
|
2013-03-13 21:56:56 +01:00
|
|
|
* in-process preemptive context switching utilizes POSIX ucontexts.
|
|
|
|
* (ucontext provides for architecture independent stack handling)
|
|
|
|
*
|
2013-03-06 10:29:49 +01:00
|
|
|
* Copyright (C) 2013 Ludwig Ortmann
|
|
|
|
*
|
2013-06-18 17:21:38 +02:00
|
|
|
* This file subject to the terms and conditions of the GNU Lesser General
|
|
|
|
* Public License. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
2013-03-06 10:29:49 +01:00
|
|
|
*
|
2013-03-13 21:56:56 +01:00
|
|
|
* @ingroup native_cpu
|
2013-03-06 10:29:49 +01:00
|
|
|
* @{
|
|
|
|
* @file
|
|
|
|
* @author Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
|
|
|
|
*/
|
2013-03-06 01:08:15 +01:00
|
|
|
#include <stdio.h>
|
2013-05-15 17:45:43 +02:00
|
|
|
#ifdef __MACH__
|
|
|
|
#define _XOPEN_SOURCE
|
|
|
|
#endif
|
2013-03-06 01:08:15 +01:00
|
|
|
#include <ucontext.h>
|
2013-05-15 17:45:43 +02:00
|
|
|
#ifdef __MACH__
|
|
|
|
#undef _XOPEN_SOURCE
|
|
|
|
#endif
|
2013-03-06 01:08:15 +01:00
|
|
|
#include <err.h>
|
|
|
|
|
2013-07-16 16:36:37 +02:00
|
|
|
#include "kernel_internal.h"
|
2013-03-06 01:08:15 +01:00
|
|
|
#include "sched.h"
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "cpu-conf.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
extern volatile tcb_t *active_thread;
|
2013-05-14 18:31:47 +02:00
|
|
|
static ucontext_t end_context;
|
2013-09-30 15:31:40 +02:00
|
|
|
static char __end_stack[SIGSTKSZ];
|
2013-03-06 01:08:15 +01:00
|
|
|
|
2013-08-08 11:08:33 +02:00
|
|
|
#ifdef MODULE_UART0
|
2013-06-26 23:29:09 +02:00
|
|
|
fd_set _native_rfds;
|
|
|
|
#endif
|
|
|
|
|
2013-03-06 01:08:15 +01:00
|
|
|
/**
|
|
|
|
* TODO: implement
|
|
|
|
*/
|
|
|
|
void thread_print_stack(void)
|
|
|
|
{
|
|
|
|
DEBUG("XXX: thread_print_stack()\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-01 18:35:25 +02:00
|
|
|
char *thread_stack_init(void (*task_func)(void), void *stack_start, int stacksize)
|
2013-03-06 01:08:15 +01:00
|
|
|
{
|
|
|
|
unsigned int *stk;
|
|
|
|
ucontext_t *p;
|
|
|
|
|
|
|
|
DEBUG("thread_stack_init()\n");
|
|
|
|
|
|
|
|
stk = stack_start;
|
|
|
|
|
|
|
|
#ifdef NATIVESPONTOP
|
2013-06-21 03:52:57 +02:00
|
|
|
p = (ucontext_t *)stk;
|
|
|
|
stk += sizeof(ucontext_t) / sizeof(void *);
|
2013-03-06 01:08:15 +01:00
|
|
|
stacksize -= sizeof(ucontext_t);
|
|
|
|
#else
|
2013-06-21 03:52:57 +02:00
|
|
|
p = (ucontext_t *)(stk + ((stacksize - sizeof(ucontext_t)) / sizeof(void *)));
|
2013-03-06 01:08:15 +01:00
|
|
|
stacksize -= sizeof(ucontext_t);
|
|
|
|
#endif
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (getcontext(p) == -1) {
|
2013-03-06 01:08:15 +01:00
|
|
|
err(1, "thread_stack_init(): getcontext()");
|
|
|
|
}
|
|
|
|
|
|
|
|
p->uc_stack.ss_sp = stk;
|
|
|
|
p->uc_stack.ss_size = stacksize;
|
|
|
|
p->uc_stack.ss_flags = 0;
|
2013-05-14 18:31:47 +02:00
|
|
|
p->uc_link = &end_context;
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (sigemptyset(&(p->uc_sigmask)) == -1) {
|
2013-03-06 01:08:15 +01:00
|
|
|
err(1, "thread_stack_init(): sigemptyset()");
|
|
|
|
}
|
|
|
|
|
|
|
|
makecontext(p, task_func, 0);
|
|
|
|
|
|
|
|
return (char *) p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cpu_switch_context_exit(void)
|
|
|
|
{
|
2013-04-15 20:08:46 +02:00
|
|
|
ucontext_t *ctx;
|
|
|
|
|
2013-03-06 01:08:15 +01:00
|
|
|
DEBUG("XXX: cpu_switch_context_exit()\n");
|
|
|
|
//active_thread = sched_threads[0];
|
|
|
|
sched_run();
|
|
|
|
|
|
|
|
DEBUG("XXX: cpu_switch_context_exit(): calling setcontext(%s)\n\n", active_thread->name);
|
2013-06-21 03:52:57 +02:00
|
|
|
ctx = (ucontext_t *)(active_thread->sp);
|
2013-05-14 18:31:47 +02:00
|
|
|
eINT(); // XXX: workaround for bug (?) in sched_task_exit
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (setcontext(ctx) == -1) {
|
2013-03-06 01:08:15 +01:00
|
|
|
err(1, "cpu_switch_context_exit(): setcontext():");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_yield()
|
|
|
|
{
|
2013-04-15 20:08:46 +02:00
|
|
|
/**
|
|
|
|
* XXX: check whether it is advisable to switch context for sched_run()
|
|
|
|
*/
|
|
|
|
ucontext_t *oc, *nc;
|
2013-03-06 01:08:15 +01:00
|
|
|
|
|
|
|
DEBUG("thread_yield()\n");
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
oc = (ucontext_t *)(active_thread->sp);
|
2013-04-15 20:08:46 +02:00
|
|
|
|
2013-03-06 01:08:15 +01:00
|
|
|
sched_run();
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
nc = (ucontext_t *)(active_thread->sp);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (nc != oc) {
|
2013-04-15 20:08:46 +02:00
|
|
|
DEBUG("thread_yield(): calling swapcontext(%s)\n\n", active_thread->name);
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (swapcontext(oc, nc) == -1) {
|
2013-04-15 20:08:46 +02:00
|
|
|
err(1, "thread_yield(): swapcontext()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DEBUG("thread_yield(): old = new, returning to context (%s)\n\n", active_thread->name);
|
2013-03-06 01:08:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void native_cpu_init()
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (getcontext(&end_context) == -1) {
|
2013-05-14 18:31:47 +02:00
|
|
|
err(1, "end_context(): getcontext()");
|
2013-03-06 01:08:15 +01:00
|
|
|
}
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2013-09-30 15:31:40 +02:00
|
|
|
end_context.uc_stack.ss_sp = __end_stack;
|
2013-05-14 18:31:47 +02:00
|
|
|
end_context.uc_stack.ss_size = SIGSTKSZ;
|
|
|
|
end_context.uc_stack.ss_flags = 0;
|
|
|
|
makecontext(&end_context, sched_task_exit, 0);
|
|
|
|
|
2013-09-30 15:31:40 +02:00
|
|
|
DEBUG("RIOT native cpu initialized.");
|
2013-03-06 01:08:15 +01:00
|
|
|
}
|
2013-03-13 21:56:56 +01:00
|
|
|
/** @} */
|