Files
OrangePi_CM5/arch/um/os-Linux/signal.c
T

298 lines
6.4 KiB
C
Raw Normal View History

2005-04-16 15:20:36 -07:00
/*
* Copyright (C) 2004 PathScale, Inc
2007-10-16 01:27:00 -07:00
* Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
2005-04-16 15:20:36 -07:00
* Licensed under the GPL
*/
#include <stdlib.h>
#include <stdarg.h>
2007-10-16 01:27:00 -07:00
#include <errno.h>
#include <signal.h>
#include <strings.h>
2008-02-04 22:31:12 -08:00
#include "as-layout.h"
2008-02-04 22:30:46 -08:00
#include "kern_util.h"
2006-01-18 17:42:42 -08:00
#include "os.h"
2008-05-12 14:02:00 -07:00
#include "process.h"
2007-10-16 01:27:00 -07:00
#include "sysdep/barrier.h"
#include "sysdep/sigcontext.h"
#include "user.h"
2005-04-16 15:20:36 -07:00
2008-02-04 22:31:13 -08:00
/* Copied from linux/compiler-gcc.h since we can't include it directly */
#define barrier() __asm__ __volatile__("": : :"memory")
2008-02-04 22:31:12 -08:00
void (*sig_info[NSIG])(int, struct uml_pt_regs *) = {
[SIGTRAP] = relay_signal,
[SIGFPE] = relay_signal,
[SIGILL] = relay_signal,
[SIGWINCH] = winch,
[SIGBUS] = bus_handler,
[SIGSEGV] = segv_handler,
[SIGIO] = sigio_handler,
[SIGVTALRM] = timer_handler };
2008-02-04 22:31:13 -08:00
static void sig_handler_common(int sig, struct sigcontext *sc)
2008-02-04 22:31:12 -08:00
{
2008-02-04 22:31:13 -08:00
struct uml_pt_regs r;
int save_errno = errno;
2008-02-04 22:31:12 -08:00
2008-02-04 22:31:13 -08:00
r.is_user = 0;
2008-02-04 22:31:12 -08:00
if (sig == SIGSEGV) {
2008-02-04 22:31:13 -08:00
/* For segfaults, we want the data from the sigcontext. */
copy_sc(&r, sc);
GET_FAULTINFO_FROM_SC(r.faultinfo, sc);
}
2008-02-04 22:31:12 -08:00
2008-02-04 22:31:13 -08:00
/* enable signals if sig isn't IRQ signal */
2008-02-04 22:31:12 -08:00
if ((sig != SIGIO) && (sig != SIGWINCH) && (sig != SIGVTALRM))
unblock_signals();
2008-02-04 22:31:13 -08:00
(*sig_info[sig])(sig, &r);
2008-02-04 22:31:12 -08:00
errno = save_errno;
}
2007-10-16 01:27:00 -07:00
/*
2007-10-16 01:27:27 -07:00
* These are the asynchronous signals. SIGPROF is excluded because we want to
2006-01-18 17:42:49 -08:00
* be able to profile all of UML, not just the non-critical sections. If
* profiling is not thread-safe, then that is not my problem. We can disable
* profiling when SMP is enabled in that case.
*/
#define SIGIO_BIT 0
#define SIGIO_MASK (1 << SIGIO_BIT)
#define SIGVTALRM_BIT 1
#define SIGVTALRM_MASK (1 << SIGVTALRM_BIT)
2008-02-04 22:31:09 -08:00
static int signals_enabled;
2008-02-04 22:31:16 -08:00
static unsigned int signals_pending;
2006-01-18 17:42:49 -08:00
void sig_handler(int sig, struct sigcontext *sc)
2005-04-16 15:20:36 -07:00
{
2006-01-18 17:42:49 -08:00
int enabled;
enabled = signals_enabled;
2007-10-16 01:27:00 -07:00
if (!enabled && (sig == SIGIO)) {
2008-02-04 22:31:16 -08:00
signals_pending |= SIGIO_MASK;
2006-01-18 17:42:49 -08:00
return;
}
block_signals();
2008-02-04 22:31:13 -08:00
sig_handler_common(sig, sc);
2006-01-18 17:42:49 -08:00
set_signals(enabled);
2005-04-16 15:20:36 -07:00
}
2007-10-16 01:27:27 -07:00
static void real_alarm_handler(struct sigcontext *sc)
2005-04-16 15:20:36 -07:00
{
struct uml_pt_regs regs;
2007-05-10 22:22:32 -07:00
2007-10-16 01:27:00 -07:00
if (sc != NULL)
2007-05-10 22:22:32 -07:00
copy_sc(&regs, sc);
regs.is_user = 0;
2007-05-10 22:22:32 -07:00
unblock_signals();
2007-10-16 01:27:27 -07:00
timer_handler(SIGVTALRM, &regs);
2006-01-18 17:42:49 -08:00
}
void alarm_handler(int sig, struct sigcontext *sc)
2006-01-18 17:42:49 -08:00
{
int enabled;
enabled = signals_enabled;
2007-10-16 01:27:00 -07:00
if (!signals_enabled) {
2008-02-04 22:31:16 -08:00
signals_pending |= SIGVTALRM_MASK;
2006-01-18 17:42:49 -08:00
return;
}
block_signals();
2007-10-16 01:27:27 -07:00
real_alarm_handler(sc);
2006-01-18 17:42:49 -08:00
set_signals(enabled);
2005-04-16 15:20:36 -07:00
}
2007-10-16 01:27:23 -07:00
void timer_init(void)
{
set_handler(SIGVTALRM, (__sighandler_t) alarm_handler,
2007-10-16 01:27:27 -07:00
SA_ONSTACK | SA_RESTART, SIGUSR1, SIGIO, SIGWINCH, -1);
2007-10-16 01:27:23 -07:00
}
void set_sigstack(void *sig_stack, int size)
{
stack_t stack = ((stack_t) { .ss_flags = 0,
.ss_sp = (__ptr_t) sig_stack,
.ss_size = size - sizeof(void *) });
2007-10-16 01:27:00 -07:00
if (sigaltstack(&stack, NULL) != 0)
panic("enabling signal stack failed, errno = %d\n", errno);
}
void (*handlers[_NSIG])(int sig, struct sigcontext *sc);
2007-05-10 22:22:34 -07:00
void handle_signal(int sig, struct sigcontext *sc)
{
2007-09-18 22:46:49 -07:00
unsigned long pending = 1UL << sig;
2007-05-10 22:22:34 -07:00
do {
int nested, bail;
/*
* pending comes back with one bit set for each
* interrupt that arrived while setting up the stack,
* plus a bit for this interrupt, plus the zero bit is
* set if this is a nested interrupt.
* If bail is true, then we interrupted another
* handler setting up the stack. In this case, we
* have to return, and the upper handler will deal
* with this interrupt.
*/
2007-09-18 22:46:49 -07:00
bail = to_irq_stack(&pending);
2007-10-16 01:27:00 -07:00
if (bail)
2007-05-10 22:22:34 -07:00
return;
nested = pending & 1;
pending &= ~1;
2007-10-16 01:27:00 -07:00
while ((sig = ffs(pending)) != 0){
2007-05-10 22:22:34 -07:00
sig--;
pending &= ~(1 << sig);
(*handlers[sig])(sig, sc);
}
2007-10-16 01:27:00 -07:00
/*
* Again, pending comes back with a mask of signals
2007-05-10 22:22:34 -07:00
* that arrived while tearing down the stack. If this
* is non-zero, we just go back, set up the stack
* again, and handle the new interrupts.
*/
2007-10-16 01:27:00 -07:00
if (!nested)
2007-05-10 22:22:34 -07:00
pending = from_irq_stack(nested);
2007-10-16 01:27:00 -07:00
} while (pending);
2007-05-10 22:22:34 -07:00
}
extern void hard_handler(int sig);
void set_handler(int sig, void (*handler)(int), int flags, ...)
{
struct sigaction action;
va_list ap;
2006-01-18 17:42:49 -08:00
sigset_t sig_mask;
int mask;
handlers[sig] = (void (*)(int, struct sigcontext *)) handler;
action.sa_handler = hard_handler;
sigemptyset(&action.sa_mask);
va_start(ap, flags);
2007-10-16 01:27:00 -07:00
while ((mask = va_arg(ap, int)) != -1)
sigaddset(&action.sa_mask, mask);
va_end(ap);
2008-02-04 22:31:13 -08:00
if (sig == SIGSEGV)
flags |= SA_NODEFER;
action.sa_flags = flags;
action.sa_restorer = NULL;
2007-10-16 01:27:00 -07:00
if (sigaction(sig, &action, NULL) < 0)
2006-01-18 17:42:49 -08:00
panic("sigaction failed - errno = %d\n", errno);
sigemptyset(&sig_mask);
sigaddset(&sig_mask, sig);
2007-10-16 01:27:00 -07:00
if (sigprocmask(SIG_UNBLOCK, &sig_mask, NULL) < 0)
2006-01-18 17:42:49 -08:00
panic("sigprocmask failed - errno = %d\n", errno);
}
int change_sig(int signal, int on)
{
2008-02-04 22:31:16 -08:00
sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, signal);
2008-02-04 22:31:16 -08:00
if (sigprocmask(on ? SIG_UNBLOCK : SIG_BLOCK, &sigset, NULL) < 0)
2008-02-04 22:30:35 -08:00
return -errno;
2008-02-04 22:31:16 -08:00
return 0;
}
void block_signals(void)
{
2006-01-18 17:42:49 -08:00
signals_enabled = 0;
2007-10-16 01:27:00 -07:00
/*
* This must return with signals disabled, so this barrier
2006-11-02 22:07:22 -08:00
* ensures that writes are flushed out before the return.
* This might matter if gcc figures out how to inline this and
* decides to shuffle this code into the caller.
*/
2008-02-04 22:31:09 -08:00
barrier();
}
void unblock_signals(void)
{
2006-01-18 17:42:49 -08:00
int save_pending;
2007-10-16 01:27:00 -07:00
if (signals_enabled == 1)
2006-01-18 17:42:49 -08:00
return;
2007-10-16 01:27:00 -07:00
/*
* We loop because the IRQ handler returns with interrupts off. So,
2006-01-18 17:42:49 -08:00
* interrupts may have arrived and we need to re-enable them and
2008-02-04 22:31:16 -08:00
* recheck signals_pending.
2006-01-18 17:42:49 -08:00
*/
2008-02-08 04:22:08 -08:00
while (1) {
2007-10-16 01:27:00 -07:00
/*
* Save and reset save_pending after enabling signals. This
2008-02-04 22:31:16 -08:00
* way, signals_pending won't be changed while we're reading it.
2006-01-18 17:42:49 -08:00
*/
signals_enabled = 1;
2007-10-16 01:27:00 -07:00
/*
2008-02-04 22:31:16 -08:00
* Setting signals_enabled and reading signals_pending must
2006-11-02 22:07:22 -08:00
* happen in this order.
*/
2008-02-04 22:31:09 -08:00
barrier();
2006-11-02 22:07:22 -08:00
2008-02-04 22:31:16 -08:00
save_pending = signals_pending;
2008-02-04 22:31:09 -08:00
if (save_pending == 0)
2006-01-18 17:42:49 -08:00
return;
2008-02-04 22:31:16 -08:00
signals_pending = 0;
2006-01-18 17:42:49 -08:00
2007-10-16 01:27:00 -07:00
/*
* We have pending interrupts, so disable signals, as the
2006-01-18 17:42:49 -08:00
* handlers expect them off when they are called. They will
* be enabled again above.
*/
signals_enabled = 0;
2007-10-16 01:27:00 -07:00
/*
* Deal with SIGIO first because the alarm handler might
2006-01-18 17:42:49 -08:00
* schedule, leaving the pending SIGIO stranded until we come
* back here.
*/
2007-10-16 01:27:00 -07:00
if (save_pending & SIGIO_MASK)
2008-02-04 22:31:13 -08:00
sig_handler_common(SIGIO, NULL);
2006-01-18 17:42:49 -08:00
2007-10-16 01:27:00 -07:00
if (save_pending & SIGVTALRM_MASK)
2007-10-16 01:27:27 -07:00
real_alarm_handler(NULL);
2006-01-18 17:42:49 -08:00
}
}
int get_signals(void)
{
2006-01-18 17:42:49 -08:00
return signals_enabled;
}
int set_signals(int enable)
{
int ret;
2007-10-16 01:27:00 -07:00
if (signals_enabled == enable)
2006-01-18 17:42:49 -08:00
return enable;
2006-01-18 17:42:49 -08:00
ret = signals_enabled;
2007-10-16 01:27:00 -07:00
if (enable)
2006-01-18 17:42:49 -08:00
unblock_signals();
else block_signals();
2006-01-18 17:42:49 -08:00
return ret;
}