2012-02-08 12:40:44 +04:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
*
|
|
|
|
* Signal Handling
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <system.h>
|
|
|
|
#include <signal.h>
|
2013-03-21 08:24:55 +04:00
|
|
|
#include <logging.h>
|
2012-02-08 12:40:44 +04:00
|
|
|
|
|
|
|
void enter_signal_handler(uintptr_t location, int signum, uintptr_t stack) {
|
2012-02-09 05:30:35 +04:00
|
|
|
IRQ_OFF;
|
2012-02-08 12:40:44 +04:00
|
|
|
asm volatile(
|
|
|
|
"mov %2, %%esp\n"
|
|
|
|
"pushl %1\n" /* argument count */
|
2012-02-16 06:50:31 +04:00
|
|
|
"pushl $" STRSTR(SIGNAL_RETURN) "\n"
|
2012-02-08 12:40:44 +04:00
|
|
|
"mov $0x23, %%ax\n" /* Segment selector */
|
|
|
|
"mov %%ax, %%ds\n"
|
|
|
|
"mov %%ax, %%es\n"
|
|
|
|
"mov %%ax, %%fs\n"
|
|
|
|
"mov %%ax, %%gs\n"
|
|
|
|
"mov %%esp, %%eax\n" /* Stack -> EAX */
|
|
|
|
"pushl $0x23\n" /* Segment selector again */
|
|
|
|
"pushl %%eax\n"
|
|
|
|
"pushf\n" /* Push flags */
|
|
|
|
"popl %%eax\n" /* Fix the Interrupt flag */
|
|
|
|
"orl $0x200, %%eax\n"
|
|
|
|
"pushl %%eax\n"
|
|
|
|
"pushl $0x1B\n"
|
|
|
|
"pushl %0\n" /* Push the entry point */
|
|
|
|
"iret\n"
|
|
|
|
: : "m"(location), "m"(signum), "r"(stack) : "%ax", "%esp", "%eax");
|
2012-02-17 08:43:41 +04:00
|
|
|
|
|
|
|
kprintf("Failed to jump to signal handler!\n");
|
2012-02-08 12:40:44 +04:00
|
|
|
}
|
|
|
|
|
2012-02-17 11:21:11 +04:00
|
|
|
static uint8_t volatile lock;
|
|
|
|
static uint8_t volatile lock_b;
|
|
|
|
|
2012-02-08 12:40:44 +04:00
|
|
|
void handle_signal(process_t * proc, signal_t * sig) {
|
2012-03-10 23:36:16 +04:00
|
|
|
uintptr_t handler = sig->handler;
|
|
|
|
uintptr_t signum = sig->signum;
|
|
|
|
free(sig);
|
|
|
|
|
2012-02-09 06:09:28 +04:00
|
|
|
if (proc->finished) {
|
|
|
|
return;
|
|
|
|
}
|
2012-02-08 12:40:44 +04:00
|
|
|
|
2012-03-10 23:36:16 +04:00
|
|
|
if (signum == 0 || signum > NUMSIGNALS) {
|
2012-02-12 04:27:28 +04:00
|
|
|
/* Ignore */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-10 23:36:16 +04:00
|
|
|
if (!handler) {
|
|
|
|
kprintf("[debug] Process %d killed by unhandled signal (%d).\n", proc->id, signum);
|
2012-03-10 23:34:15 +04:00
|
|
|
kexit(128 + signum);
|
2012-02-09 06:09:28 +04:00
|
|
|
__builtin_unreachable();
|
2012-02-08 12:40:44 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-10 23:36:16 +04:00
|
|
|
if (handler == 1) /* Ignore */ {
|
2012-02-08 12:40:44 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-21 08:24:55 +04:00
|
|
|
debug_print(NOTICE, "handling signal in process %d (%d)", proc->id, signum);
|
|
|
|
|
2012-02-13 04:47:01 +04:00
|
|
|
uintptr_t stack = 0xFFFF0000;
|
|
|
|
if (proc->syscall_registers->useresp < 0x10000100) {
|
|
|
|
stack = proc->image.user_stack;
|
|
|
|
} else {
|
|
|
|
stack = proc->syscall_registers->useresp;
|
|
|
|
}
|
2012-02-08 12:40:44 +04:00
|
|
|
|
|
|
|
/* Not marked as ignored, must call signal */
|
2012-03-10 22:47:33 +04:00
|
|
|
enter_signal_handler(handler, signum, stack);
|
2012-02-08 12:40:44 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
list_t * rets_from_sig;
|
|
|
|
|
|
|
|
void return_from_signal_handler() {
|
2012-02-17 08:43:41 +04:00
|
|
|
#if 0
|
2012-02-08 12:40:44 +04:00
|
|
|
kprintf("[debug] Return From Signal for process %d\n", current_process->id);
|
2012-02-17 08:43:41 +04:00
|
|
|
#endif
|
2012-02-08 12:40:44 +04:00
|
|
|
|
|
|
|
if (__builtin_expect(!rets_from_sig, 0)) {
|
|
|
|
rets_from_sig = list_create();
|
|
|
|
}
|
|
|
|
|
2012-02-17 11:21:11 +04:00
|
|
|
spin_lock(&lock);
|
2012-02-09 02:07:54 +04:00
|
|
|
list_insert(rets_from_sig, (process_t *)current_process);
|
2012-02-17 11:21:11 +04:00
|
|
|
spin_unlock(&lock);
|
2012-02-08 12:40:44 +04:00
|
|
|
|
|
|
|
switch_next();
|
|
|
|
}
|
|
|
|
|
|
|
|
void fix_signal_stacks() {
|
2012-02-09 06:09:28 +04:00
|
|
|
uint8_t redo_me = 0;
|
2012-02-08 12:40:44 +04:00
|
|
|
if (rets_from_sig) {
|
2012-02-17 11:21:11 +04:00
|
|
|
spin_lock(&lock_b);
|
2012-02-08 12:40:44 +04:00
|
|
|
while (rets_from_sig->head) {
|
2012-02-17 11:21:11 +04:00
|
|
|
spin_lock(&lock);
|
2012-02-08 12:40:44 +04:00
|
|
|
node_t * n = list_dequeue(rets_from_sig);
|
2012-02-17 11:21:11 +04:00
|
|
|
spin_unlock(&lock);
|
|
|
|
if (!n) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-02-08 12:40:44 +04:00
|
|
|
process_t * p = n->value;
|
2012-02-09 06:09:28 +04:00
|
|
|
free(n);
|
|
|
|
if (p == current_process) {
|
|
|
|
redo_me = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2012-02-08 12:40:44 +04:00
|
|
|
p->thread.esp = p->signal_state.esp;
|
|
|
|
p->thread.eip = p->signal_state.eip;
|
|
|
|
p->thread.ebp = p->signal_state.ebp;
|
2012-02-09 02:07:54 +04:00
|
|
|
memcpy((void *)(p->image.stack - KERNEL_STACK_SIZE), p->signal_kstack, KERNEL_STACK_SIZE);
|
2012-02-08 12:40:44 +04:00
|
|
|
free(p->signal_kstack);
|
2012-02-21 09:31:00 +04:00
|
|
|
p->signal_kstack = NULL;
|
2012-02-08 12:40:44 +04:00
|
|
|
make_process_ready(p);
|
|
|
|
}
|
2012-02-17 11:21:11 +04:00
|
|
|
spin_unlock(&lock_b);
|
2012-02-08 12:40:44 +04:00
|
|
|
}
|
2012-02-09 06:09:28 +04:00
|
|
|
if (redo_me) {
|
2012-02-17 11:21:11 +04:00
|
|
|
spin_lock(&lock);
|
2012-02-09 06:09:28 +04:00
|
|
|
list_insert(rets_from_sig, (process_t *)current_process);
|
2012-02-17 11:21:11 +04:00
|
|
|
spin_unlock(&lock);
|
|
|
|
switch_next();
|
2012-02-09 06:09:28 +04:00
|
|
|
}
|
2012-02-08 12:40:44 +04:00
|
|
|
}
|