From d46a03f0309c53677bca58b5ba09f3495fb91d17 Mon Sep 17 00:00:00 2001 From: Ludwig Ortmann Date: Mon, 30 Sep 2013 15:44:22 +0200 Subject: [PATCH 1/3] guard rtc syscall --- cpu/native/rtc/posix-rtc.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cpu/native/rtc/posix-rtc.c b/cpu/native/rtc/posix-rtc.c index 25f6cdf65..f2d25c499 100644 --- a/cpu/native/rtc/posix-rtc.c +++ b/cpu/native/rtc/posix-rtc.c @@ -24,6 +24,7 @@ #include "debug.h" #include "rtc.h" +#include "cpu.h" static int native_rtc_enabled; @@ -58,10 +59,12 @@ void rtc_get_localtime(struct tm *localt) time_t t; if (native_rtc_enabled == 1) { + _native_in_syscall++; t = time(NULL); if (localtime_r(&t, localt) == NULL) { err(1, "rtc_get_localtime: localtime_r"); } + _native_in_syscall--; } } From 43adafe55ac413e0b64e435023d3080e652e8e5d Mon Sep 17 00:00:00 2001 From: Ludwig Ortmann Date: Mon, 30 Sep 2013 15:45:47 +0200 Subject: [PATCH 2/3] native fix cpu_switch_context_exit --- cpu/native/native_cpu.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/cpu/native/native_cpu.c b/cpu/native/native_cpu.c index a7b71cf14..f0931f5b6 100644 --- a/cpu/native/native_cpu.c +++ b/cpu/native/native_cpu.c @@ -88,14 +88,19 @@ char *thread_stack_init(void (*task_func)(void), void *stack_start, int stacksiz void cpu_switch_context_exit(void) { ucontext_t *ctx; + extern int native_interrupts_enabled; DEBUG("XXX: cpu_switch_context_exit()\n"); - //active_thread = sched_threads[0]; - sched_run(); + if ((sched_context_switch_request == 1) || (active_thread == NULL)) { + sched_run(); + } DEBUG("XXX: cpu_switch_context_exit(): calling setcontext(%s)\n\n", active_thread->name); ctx = (ucontext_t *)(active_thread->sp); - eINT(); // XXX: workaround for bug (?) in sched_task_exit + + /* the next context will have interrupts enabled due to ucontext */ + DEBUG("XXX: cpu_switch_context_exit: native_interrupts_enabled = 1;\n"); + native_interrupts_enabled = 1; if (setcontext(ctx) == -1) { err(1, "cpu_switch_context_exit(): setcontext():"); From 47b6e629829d04fe38e4399a3671faa5ac2c887d Mon Sep 17 00:00:00 2001 From: Ludwig Ortmann Date: Mon, 30 Sep 2013 15:47:04 +0200 Subject: [PATCH 3/3] fix native ctxt switches fix disableIRQ (remove context switch) fix enableIRQ (make context switch delay-safe) change interrupts-off signal-catch behaviour from error to warning add context switch memory location warning --- cpu/native/irq_cpu.c | 49 ++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/cpu/native/irq_cpu.c b/cpu/native/irq_cpu.c index 0d1da00ef..e410b6946 100644 --- a/cpu/native/irq_cpu.c +++ b/cpu/native/irq_cpu.c @@ -41,6 +41,7 @@ extern volatile tcb_t *active_thread; volatile unsigned int _native_saved_eip; ucontext_t *_native_cur_ctx, *_native_isr_ctx; +int *process_heap_address; static int pipefd[2]; @@ -164,20 +165,8 @@ unsigned disableIRQ(void) prev_state = native_interrupts_enabled; native_interrupts_enabled = 0; - // XXX: does this make sense? - if ((_native_sigpend > 0) && (_native_in_isr == 0)) { - DEBUG("\n\n\t\treturn from syscall, calling native_irq_handler\n\n"); - _native_in_syscall = 0; - DEBUG("disableIRQ: calling swapcontext()\n"); - DEBUG("disableIRQ: _native_cur_ctx == %p, _native_isr_ctx == %p\n", _native_cur_ctx, _native_isr_ctx); - makecontext(&native_isr_context, native_irq_handler, 0); - swapcontext(_native_cur_ctx, _native_isr_ctx); - } - else { - _native_in_syscall = 0; - } - DEBUG("disableIRQ(): return\n"); + _native_in_syscall = 0; return prev_state; } @@ -206,6 +195,7 @@ unsigned enableIRQ(void) //print_sigmasks(); //native_print_signals(); if ((_native_sigpend > 0) && (_native_in_isr == 0)) { + _native_cur_ctx = (ucontext_t *)active_thread->sp; DEBUG("\n\n\t\treturn from syscall, calling native_irq_handler\n\n"); _native_in_syscall = 0; DEBUG("enableIRQ: calling swapcontext()\n"); @@ -317,23 +307,23 @@ void native_isr_entry(int sig, siginfo_t *info, void *context) (void) info; /* unused at the moment */ DEBUG("\n\n\t\tnative_isr_entry\n\n"); - if (native_interrupts_enabled == 0) { - errx(1, "interrupts are off, but I caught a signal."); - } - /* save the signal */ if (write(pipefd[1], &sig, sizeof(int)) == -1) { err(1, "native_isr_entry(): write()"); } - _native_sigpend++; - /* indicate irs status */ makecontext(&native_isr_context, native_irq_handler, 0); _native_cur_ctx = (ucontext_t *)active_thread->sp; + /* XXX: Workaround safety check - whenever this happens it really + * indicates a bug in disableIRQ */ + if (native_interrupts_enabled == 0) { + warnx("interrupts are off, but I caught a signal."); + return; + } + if (_native_in_syscall == 0) { - _native_in_isr = 1; DEBUG("\n\n\t\treturn to _native_sig_leave_tramp\n\n"); #ifdef __MACH__ _native_saved_eip = ((ucontext_t *)context)->uc_mcontext->__ss.__eip; @@ -342,8 +332,18 @@ void native_isr_entry(int sig, siginfo_t *info, void *context) _native_saved_eip = ((struct sigcontext *)context)->sc_eip; ((struct sigcontext *)context)->sc_eip = (unsigned int)&_native_sig_leave_tramp; #else - _native_saved_eip = ((ucontext_t *)context)->uc_mcontext.gregs[REG_EIP]; - ((ucontext_t *)context)->uc_mcontext.gregs[REG_EIP] = (unsigned int)&_native_sig_leave_tramp; + if ( + ((void*)(((ucontext_t *)context)->uc_mcontext.gregs[REG_EIP])) + > ((void*)process_heap_address) + ) { + DEBUG("\nEIP:\t%p\nHEAP:\t%p\nnot switching\n\n", (void*)((ucontext_t *)context)->uc_mcontext.gregs[REG_EIP], (void*)process_heap_address); + } + else { + _native_in_isr = 1; + warnx("\nEIP:\t%p\nHEAP:\t%p\ngo switching\n\n", (void*)((ucontext_t *)context)->uc_mcontext.gregs[REG_EIP], (void*)process_heap_address); + _native_saved_eip = ((ucontext_t *)context)->uc_mcontext.gregs[REG_EIP]; + ((ucontext_t *)context)->uc_mcontext.gregs[REG_EIP] = (unsigned int)&_native_sig_leave_tramp; + } #endif // TODO: change sigmask? } @@ -429,6 +429,11 @@ void native_interrupt_init(void) struct sigaction sa; DEBUG("XXX: native_interrupt_init()\n"); + process_heap_address = malloc(sizeof(int)); + if (process_heap_address == NULL) { + err(EXIT_FAILURE, "native_interrupt_init: malloc"); + } + native_interrupts_enabled = 1; _native_sigpend = 0;