Merge pull request #229 from LudwigOrtmann/native_fix_syscall_segfault

fix some segfault sources in native
dev/timer
LudwigOrtmann 10 years ago
commit 1608790246

@ -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;

@ -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():");

@ -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--;
}
}

Loading…
Cancel
Save