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