gem5 v24.0.0.0
Loading...
Searching...
No Matches
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/eventq.hh"
62
63namespace gem5
64{
65
66// Use an separate stack for fatal signal handlers
67
68static bool
70{
71 const auto stack_size = 2 * SIGSTKSZ;
72 static uint8_t *fatal_sig_stack = new uint8_t[stack_size];
73 stack_t stack;
74#if defined(__FreeBSD__) && (__FreeBSD_version < 1100097)
75 stack.ss_sp = (char *)fatal_sig_stack;
76#else
77 stack.ss_sp = fatal_sig_stack;
78#endif
79 stack.ss_size = stack_size;
80 stack.ss_flags = 0;
81
82 return sigaltstack(&stack, NULL) == 0;
83}
84
85static void
86installSignalHandler(int signal, void (*handler)(int sigtype),
87 int flags = SA_RESTART,
88 struct sigaction *old_sa = NULL)
89{
90 struct sigaction sa;
91
92 memset(&sa, 0, sizeof(sa));
93 sigemptyset(&sa.sa_mask);
94 sa.sa_handler = handler;
95 sa.sa_flags = flags;
96
97 if (sigaction(signal, &sa, old_sa) == -1)
98 panic("Failed to setup handler for signal %i\n", signal);
99}
100
101static void
103{
104 // The signal handler should have been reset and unmasked (it was
105 // registered with SA_RESETHAND | SA_NODEFER), just raise the
106 // signal again to invoke the default handler.
107 STATIC_ERR("For more info on how to address this issue, please visit "
108 "https://www.gem5.org/documentation/general_docs/common-errors/ \n\n");
109 pthread_kill(pthread_self(), signo);
110
111 // Something is really wrong if the process is alive at this
112 // point, manually try to exit it.
113 STATIC_ERR("Failed to execute default signal handler!\n");
114 _exit(127);
115}
116
118void
120{
121 async_event = true;
122 async_statdump = true;
123 /* Wake up some event queue to handle event */
124 getEventQueue(0)->wakeup();
125}
126
127void
129{
130 async_event = true;
131 async_statdump = true;
132 async_statreset = true;
133 /* Wake up some event queue to handle event */
134 getEventQueue(0)->wakeup();
135}
136
138void
139exitNowHandler(int sigtype)
140{
141 async_event = true;
142 async_exit = true;
143 /* Wake up some event queue to handle event */
144 getEventQueue(0)->wakeup();
145}
146
148void
149abortHandler(int sigtype)
150{
151 const EventQueue *const eq(curEventQueue());
152 if (eq) {
153 ccprintf(std::cerr, "Program aborted at tick %llu\n",
154 eq->getCurTick());
155 } else {
156 STATIC_ERR("Program aborted\n\n");
157 }
158
160 raiseFatalSignal(sigtype);
161}
162
164static void
165segvHandler(int sigtype)
166{
167 STATIC_ERR("gem5 has encountered a segmentation fault!\n\n");
168
170 raiseFatalSignal(SIGSEGV);
171}
172
173// Handle SIGIO
174static void
175ioHandler(int sigtype)
176{
177 async_event = true;
178 async_io = true;
179 /* Wake up some event queue to handle event */
180 getEventQueue(0)->wakeup();
181}
182
183/*
184 * M5 can do several special things when various signals are sent.
185 * None are mandatory.
186 */
187void
189{
190 // Floating point exceptions may happen on misspeculated paths, so
191 // ignore them
192 signal(SIGFPE, SIG_IGN);
193
194 // Dump intermediate stats
196
197 // Dump intermediate stats and reset them
199
200 // Print the current cycle number and a backtrace on abort. Make
201 // sure the signal is unmasked and the handler reset when a signal
202 // is delivered to be able to invoke the default handler.
203 installSignalHandler(SIGABRT, abortHandler, SA_RESETHAND | SA_NODEFER);
204
205 // Setup a SIGSEGV handler with a private stack
206 if (setupAltStack()) {
208 SA_RESETHAND | SA_NODEFER | SA_ONSTACK);
209 } else {
210 warn("Failed to setup stack for SIGSEGV handler, "
211 "using default signal handler.\n");
212 }
213
214 // Install a SIGIO handler to handle asynchronous file IO. See the
215 // PollQueue class.
217}
218
219struct sigaction old_int_sa;
220
222{
223 // Exit cleanly on Interrupt (Ctrl-C)
224 installSignalHandler(SIGINT, exitNowHandler, SA_RESTART, &old_int_sa);
225}
226
228{
229 // Restore the old SIGINT handler
230 sigaction(SIGINT, &old_int_sa, NULL);
231}
232
233
234} // namespace gem5
This file defines flags used to handle asynchronous simulator events.
#define STATIC_ERR(m)
Statically allocate a string and write it to STDERR.
Definition atomicio.hh:67
Queue of events sorted in time order.
Definition eventq.hh:616
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:923
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:188
uint8_t flags
Definition helpers.cc:87
#define warn(...)
Definition logging.hh:256
Bitfield< 3 > sa
Bitfield< 29 > eq
Definition misc.hh:58
Bitfield< 17, 16 > stack
Definition misc.hh:602
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
static void installSignalHandler(int signal, void(*handler)(int sigtype), int flags=SA_RESTART, struct sigaction *old_sa=NULL)
volatile bool async_event
Some asynchronous event has happened.
Definition async.cc:32
static void raiseFatalSignal(int signo)
volatile bool async_statdump
Async request to dump stats.
Definition async.cc:33
void dumpStatsHandler(int sigtype)
Stats signal handler.
void restoreSigInt()
static void ioHandler(int sigtype)
void print_backtrace()
Print a gem5 post-mortem report.
void dumprstStatsHandler(int sigtype)
static bool setupAltStack()
static void segvHandler(int sigtype)
Segmentation fault signal handler.
void exitNowHandler(int sigtype)
Exit signal handler.
volatile bool async_io
Async I/O request (SIGIO).
Definition async.cc:36
volatile bool async_exit
Async request to exit simulator.
Definition async.cc:35
volatile bool async_statreset
Async request to reset stats.
Definition async.cc:34
EventQueue * curEventQueue()
Definition eventq.hh:91
void initSigInt()
void initSignals()
struct sigaction old_int_sa
EventQueue * getEventQueue(uint32_t index)
Function for returning eventq queue for the provided index.
Definition eventq.cc:62
void ccprintf(cp::Print &print)
Definition cprintf.hh:130
void abortHandler(int sigtype)
Abort signal handler.

Generated on Tue Jun 18 2024 16:24:06 for gem5 by doxygen 1.11.0