gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
init_signals.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, 2015 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2000-2005 The Regents of The University of Michigan
15  * Copyright (c) 2008 The Hewlett-Packard Development Company
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are
20  * met: redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer;
22  * redistributions in binary form must reproduce the above copyright
23  * notice, this list of conditions and the following disclaimer in the
24  * documentation and/or other materials provided with the distribution;
25  * neither the name of the copyright holders nor the names of its
26  * contributors may be used to endorse or promote products derived from
27  * this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Authors: Nathan Binkert
42  */
43 
44 #include "sim/init_signals.hh"
45 
46 #include <sys/types.h>
47 #include <unistd.h>
48 
49 #include <csignal>
50 #include <iostream>
51 #include <string>
52 
53 #include "base/atomicio.hh"
54 #include "base/cprintf.hh"
55 #include "base/misc.hh"
56 #include "sim/async.hh"
57 #include "sim/backtrace.hh"
58 #include "sim/core.hh"
59 #include "sim/eventq.hh"
60 
61 using namespace std;
62 
63 // Use an separate stack for fatal signal handlers
64 static uint8_t fatalSigStack[2 * SIGSTKSZ];
65 
66 static bool
68 {
69  stack_t stack;
70  stack.ss_sp = fatalSigStack;
71  stack.ss_size = sizeof(fatalSigStack);
72  stack.ss_flags = 0;
73 
74  return sigaltstack(&stack, NULL) == 0;
75 }
76 
77 static void
78 installSignalHandler(int signal, void (*handler)(int sigtype),
79  int flags = SA_RESTART)
80 {
81  struct sigaction sa;
82 
83  memset(&sa, 0, sizeof(sa));
84  sigemptyset(&sa.sa_mask);
85  sa.sa_handler = handler;
86  sa.sa_flags = flags;
87 
88  if (sigaction(signal, &sa, NULL) == -1)
89  panic("Failed to setup handler for signal %i\n", signal);
90 }
91 
92 static void
93 raiseFatalSignal(int signo)
94 {
95  // The signal handler should have been reset and unmasked (it was
96  // registered with SA_RESETHAND | SA_NODEFER), just raise the
97  // signal again to invoke the default handler.
98  pthread_kill(pthread_self(), signo);
99 
100  // Something is really wrong if the process is alive at this
101  // point, manually try to exit it.
102  STATIC_ERR("Failed to execute default signal handler!\n");
103  _exit(127);
104 }
105 
107 void
108 dumpStatsHandler(int sigtype)
109 {
110  async_event = true;
111  async_statdump = true;
112  /* Wake up some event queue to handle event */
113  getEventQueue(0)->wakeup();
114 }
115 
116 void
118 {
119  async_event = true;
120  async_statdump = true;
121  async_statreset = true;
122  /* Wake up some event queue to handle event */
123  getEventQueue(0)->wakeup();
124 }
125 
127 void
128 exitNowHandler(int sigtype)
129 {
130  async_event = true;
131  async_exit = true;
132  /* Wake up some event queue to handle event */
133  getEventQueue(0)->wakeup();
134 }
135 
137 void
138 abortHandler(int sigtype)
139 {
140  const EventQueue *const eq(curEventQueue());
141  if (eq) {
142  ccprintf(cerr, "Program aborted at tick %llu\n", eq->getCurTick());
143  } else {
144  STATIC_ERR("Program aborted\n\n");
145  }
146 
147  print_backtrace();
148  raiseFatalSignal(sigtype);
149 }
150 
152 static void
153 segvHandler(int sigtype)
154 {
155  STATIC_ERR("gem5 has encountered a segmentation fault!\n\n");
156 
157  print_backtrace();
158  raiseFatalSignal(SIGSEGV);
159 }
160 
161 // Handle SIGIO
162 static void
163 ioHandler(int sigtype)
164 {
165  async_event = true;
166  async_io = true;
167  /* Wake up some event queue to handle event */
168  getEventQueue(0)->wakeup();
169 }
170 
171 /*
172  * M5 can do several special things when various signals are sent.
173  * None are mandatory.
174  */
175 void
177 {
178  // Floating point exceptions may happen on misspeculated paths, so
179  // ignore them
180  signal(SIGFPE, SIG_IGN);
181 
182  // Dump intermediate stats
184 
185  // Dump intermediate stats and reset them
187 
188  // Exit cleanly on Interrupt (Ctrl-C)
190 
191  // Print the current cycle number and a backtrace on abort. Make
192  // sure the signal is unmasked and the handler reset when a signal
193  // is delivered to be able to invoke the default handler.
194  installSignalHandler(SIGABRT, abortHandler, SA_RESETHAND | SA_NODEFER);
195 
196  // Setup a SIGSEGV handler with a private stack
197  if (setupAltStack()) {
199  SA_RESETHAND | SA_NODEFER | SA_ONSTACK);
200  } else {
201  warn("Failed to setup stack for SIGSEGV handler, "
202  "using default signal handler.\n");
203  }
204 
205  // Install a SIGIO handler to handle asynchronous file IO. See the
206  // PollQueue class.
208 }
209 
void ccprintf(cp::Print &print)
Definition: cprintf.hh:130
void dumprstStatsHandler(int sigtype)
Bitfield< 29 > eq
Definition: miscregs.hh:50
EventQueue * getEventQueue(uint32_t index)
Function for returning eventq queue for the provided index.
Definition: eventq.cc:64
void initSignals()
void dumpStatsHandler(int sigtype)
Stats signal handler.
#define panic(...)
Definition: misc.hh:153
void print_backtrace()
Print a gem5 post-mortem report.
This file defines flags used to handle asynchronous simulator events.
void abortHandler(int sigtype)
Abort signal handler.
volatile bool async_exit
Async request to exit simulator.
Definition: async.cc:34
static void ioHandler(int sigtype)
static void segvHandler(int sigtype)
Segmentation fault signal handler.
static bool setupAltStack()
Definition: init_signals.cc:67
static uint8_t fatalSigStack[2 *SIGSTKSZ]
Definition: init_signals.cc:64
static void raiseFatalSignal(int signo)
Definition: init_signals.cc:93
#define warn(...)
Definition: misc.hh:219
virtual void wakeup(Tick when=(Tick)-1)
Function to signal that the event loop should be woken up because an event has been scheduled by an a...
Definition: eventq.hh:664
static void installSignalHandler(int signal, void(*handler)(int sigtype), int flags=SA_RESTART)
Definition: init_signals.cc:78
Queue of events sorted in time order.
Definition: eventq.hh:488
EventQueue * curEventQueue()
Definition: eventq.hh:84
volatile bool async_statdump
Async request to dump stats.
Definition: async.cc:32
Bitfield< 17, 16 > stack
Definition: misc.hh:588
#define STATIC_ERR(m)
Statically allocate a string and write it to STDERR.
Definition: atomicio.hh:66
volatile bool async_io
Async I/O request (SIGIO).
Definition: async.cc:35
Tick getCurTick() const
Definition: eventq.hh:615
volatile bool async_statreset
Async request to reset stats.
Definition: async.cc:33
volatile bool async_event
Some asynchronous event has happened.
Definition: async.cc:31
void exitNowHandler(int sigtype)
Exit signal handler.

Generated on Fri Jun 9 2017 13:03:51 for gem5 by doxygen 1.8.6