gem5  v21.0.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
42 #include "sim/init_signals.hh"
43 
44 #include <sys/types.h>
45 #include <unistd.h>
46 
47 #include <csignal>
48 #include <iostream>
49 #include <string>
50 
51 #if defined(__FreeBSD__)
52 #include <sys/param.h>
53 
54 #endif
55 
56 #include "base/atomicio.hh"
57 #include "base/cprintf.hh"
58 #include "base/logging.hh"
59 #include "sim/async.hh"
60 #include "sim/backtrace.hh"
61 #include "sim/core.hh"
62 #include "sim/eventq.hh"
63 
64 // Use an separate stack for fatal signal handlers
65 static uint8_t fatalSigStack[2 * SIGSTKSZ];
66 
67 static bool
69 {
70  stack_t stack;
71 #if defined(__FreeBSD__) && (__FreeBSD_version < 1100097)
72  stack.ss_sp = (char *)fatalSigStack;
73 #else
74  stack.ss_sp = fatalSigStack;
75 #endif
76  stack.ss_size = sizeof(fatalSigStack);
77  stack.ss_flags = 0;
78 
79  return sigaltstack(&stack, NULL) == 0;
80 }
81 
82 static void
83 installSignalHandler(int signal, void (*handler)(int sigtype),
84  int flags = SA_RESTART)
85 {
86  struct sigaction sa;
87 
88  memset(&sa, 0, sizeof(sa));
89  sigemptyset(&sa.sa_mask);
90  sa.sa_handler = handler;
91  sa.sa_flags = flags;
92 
93  if (sigaction(signal, &sa, NULL) == -1)
94  panic("Failed to setup handler for signal %i\n", signal);
95 }
96 
97 static void
98 raiseFatalSignal(int signo)
99 {
100  // The signal handler should have been reset and unmasked (it was
101  // registered with SA_RESETHAND | SA_NODEFER), just raise the
102  // signal again to invoke the default handler.
103  pthread_kill(pthread_self(), signo);
104 
105  // Something is really wrong if the process is alive at this
106  // point, manually try to exit it.
107  STATIC_ERR("Failed to execute default signal handler!\n");
108  _exit(127);
109 }
110 
112 void
113 dumpStatsHandler(int sigtype)
114 {
115  async_event = true;
116  async_statdump = true;
117  /* Wake up some event queue to handle event */
118  getEventQueue(0)->wakeup();
119 }
120 
121 void
123 {
124  async_event = true;
125  async_statdump = true;
126  async_statreset = true;
127  /* Wake up some event queue to handle event */
128  getEventQueue(0)->wakeup();
129 }
130 
132 void
133 exitNowHandler(int sigtype)
134 {
135  async_event = true;
136  async_exit = true;
137  /* Wake up some event queue to handle event */
138  getEventQueue(0)->wakeup();
139 }
140 
142 void
143 abortHandler(int sigtype)
144 {
145  const EventQueue *const eq(curEventQueue());
146  if (eq) {
147  ccprintf(std::cerr, "Program aborted at tick %llu\n",
148  eq->getCurTick());
149  } else {
150  STATIC_ERR("Program aborted\n\n");
151  }
152 
153  print_backtrace();
154  raiseFatalSignal(sigtype);
155 }
156 
158 static void
159 segvHandler(int sigtype)
160 {
161  STATIC_ERR("gem5 has encountered a segmentation fault!\n\n");
162 
163  print_backtrace();
164  raiseFatalSignal(SIGSEGV);
165 }
166 
167 // Handle SIGIO
168 static void
169 ioHandler(int sigtype)
170 {
171  async_event = true;
172  async_io = true;
173  /* Wake up some event queue to handle event */
174  getEventQueue(0)->wakeup();
175 }
176 
177 /*
178  * M5 can do several special things when various signals are sent.
179  * None are mandatory.
180  */
181 void
183 {
184  // Floating point exceptions may happen on misspeculated paths, so
185  // ignore them
186  signal(SIGFPE, SIG_IGN);
187 
188  // Dump intermediate stats
190 
191  // Dump intermediate stats and reset them
193 
194  // Exit cleanly on Interrupt (Ctrl-C)
196 
197  // Print the current cycle number and a backtrace on abort. Make
198  // sure the signal is unmasked and the handler reset when a signal
199  // is delivered to be able to invoke the default handler.
200  installSignalHandler(SIGABRT, abortHandler, SA_RESETHAND | SA_NODEFER);
201 
202  // Setup a SIGSEGV handler with a private stack
203  if (setupAltStack()) {
205  SA_RESETHAND | SA_NODEFER | SA_ONSTACK);
206  } else {
207  warn("Failed to setup stack for SIGSEGV handler, "
208  "using default signal handler.\n");
209  }
210 
211  // Install a SIGIO handler to handle asynchronous file IO. See the
212  // PollQueue class.
214 }
215 
async_event
volatile bool async_event
Some asynchronous event has happened.
Definition: async.cc:29
warn
#define warn(...)
Definition: logging.hh:239
async_io
volatile bool async_io
Async I/O request (SIGIO).
Definition: async.cc:33
getEventQueue
EventQueue * getEventQueue(uint32_t index)
Function for returning eventq queue for the provided index.
Definition: eventq.cc:60
abortHandler
void abortHandler(int sigtype)
Abort signal handler.
Definition: init_signals.cc:143
atomicio.hh
async_statdump
volatile bool async_statdump
Async request to dump stats.
Definition: async.cc:30
dumpStatsHandler
void dumpStatsHandler(int sigtype)
Stats signal handler.
Definition: init_signals.cc:113
async_exit
volatile bool async_exit
Async request to exit simulator.
Definition: async.cc:32
exitNowHandler
void exitNowHandler(int sigtype)
Exit signal handler.
Definition: init_signals.cc:133
installSignalHandler
static void installSignalHandler(int signal, void(*handler)(int sigtype), int flags=SA_RESTART)
Definition: init_signals.cc:83
EventQueue::wakeup
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:927
X86ISA::stack
Bitfield< 17, 16 > stack
Definition: misc.hh:587
segvHandler
static void segvHandler(int sigtype)
Segmentation fault signal handler.
Definition: init_signals.cc:159
setupAltStack
static bool setupAltStack()
Definition: init_signals.cc:68
async_statreset
volatile bool async_statreset
Async request to reset stats.
Definition: async.cc:31
STATIC_ERR
#define STATIC_ERR(m)
Statically allocate a string and write it to STDERR.
Definition: atomicio.hh:64
ArmISA::sa
Bitfield< 3 > sa
Definition: miscregs_types.hh:386
initSignals
void initSignals()
Definition: init_signals.cc:182
curEventQueue
EventQueue * curEventQueue()
Definition: eventq.hh:85
cprintf.hh
core.hh
async.hh
ioHandler
static void ioHandler(int sigtype)
Definition: init_signals.cc:169
PowerISA::eq
Bitfield< 29 > eq
Definition: miscregs.hh:48
raiseFatalSignal
static void raiseFatalSignal(int signo)
Definition: init_signals.cc:98
fatalSigStack
static uint8_t fatalSigStack[2 *SIGSTKSZ]
Definition: init_signals.cc:65
ccprintf
void ccprintf(cp::Print &print)
Definition: cprintf.hh:127
logging.hh
init_signals.hh
EventQueue
Queue of events sorted in time order.
Definition: eventq.hh:619
dumprstStatsHandler
void dumprstStatsHandler(int sigtype)
Definition: init_signals.cc:122
backtrace.hh
print_backtrace
void print_backtrace()
Print a gem5 post-mortem report.
Definition: backtrace_glibc.cc:51
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171
eventq.hh

Generated on Tue Jun 22 2021 15:28:30 for gem5 by doxygen 1.8.17