init_signals.cc revision 10453
1/*
2 * Copyright (c) 2012 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 <csignal>
45#include <iostream>
46#include <string>
47
48#include "base/cprintf.hh"
49#include "sim/async.hh"
50#include "sim/core.hh"
51#include "sim/init_signals.hh"
52
53using namespace std;
54
55/// Stats signal handler.
56void
57dumpStatsHandler(int sigtype)
58{
59    async_event = true;
60    async_statdump = true;
61}
62
63void
64dumprstStatsHandler(int sigtype)
65{
66    async_event = true;
67    async_statdump = true;
68    async_statreset = true;
69}
70
71/// Exit signal handler.
72void
73exitNowHandler(int sigtype)
74{
75    async_event = true;
76    async_exit = true;
77}
78
79/// Abort signal handler.
80void
81abortHandler(int sigtype)
82{
83    ccprintf(cerr, "Program aborted at cycle %d\n", curTick());
84}
85
86// Handle SIGIO
87static void
88ioHandler(int sigtype)
89{
90    async_event = true;
91    async_io = true;
92}
93
94static void
95installSignalHandler(int signal, void (*handler)(int sigtype))
96{
97    struct sigaction sa;
98
99    memset(&sa, 0, sizeof(sa));
100    sigemptyset(&sa.sa_mask);
101    sa.sa_handler = handler;
102    sa.sa_flags = SA_RESTART;
103
104    if (sigaction(signal, &sa, NULL) == -1)
105        panic("Failed to setup handler for signal %i\n", signal);
106}
107
108/*
109 * M5 can do several special things when various signals are sent.
110 * None are mandatory.
111 */
112void
113initSignals()
114{
115    // Floating point exceptions may happen on misspeculated paths, so
116    // ignore them
117    signal(SIGFPE, SIG_IGN);
118
119    // We use SIGTRAP sometimes for debugging
120    signal(SIGTRAP, SIG_IGN);
121
122    // Dump intermediate stats
123    installSignalHandler(SIGUSR1, dumpStatsHandler);
124
125    // Dump intermediate stats and reset them
126    installSignalHandler(SIGUSR2, dumprstStatsHandler);
127
128    // Exit cleanly on Interrupt (Ctrl-C)
129    installSignalHandler(SIGINT, exitNowHandler);
130
131    // Print out cycle number on abort
132    installSignalHandler(SIGABRT, abortHandler);
133
134    // Install a SIGIO handler to handle asynchronous file IO. See the
135    // PollQueue class.
136    installSignalHandler(SIGIO, ioHandler);
137}
138
139