2 * Copyright (c) 2000-2005 The Regents of The University of Michigan
3 * Copyright (c) 2008 The Hewlett-Packard Development Company
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Authors: Nathan Binkert
40 #include "base/cprintf.hh"
41 #include "base/misc.hh"
42 #include "base/types.hh"
43 #include "sim/async.hh"
44 #include "sim/core.hh"
45 #include "sim/init.hh"
49 /// Stats signal handler.
51 dumpStatsHandler(int sigtype
)
54 async_statdump
= true;
58 dumprstStatsHandler(int sigtype
)
61 async_statdump
= true;
62 async_statreset
= true;
65 /// Exit signal handler.
67 exitNowHandler(int sigtype
)
73 /// Abort signal handler.
75 abortHandler(int sigtype
)
77 ccprintf(cerr
, "Program aborted at cycle %d\n", curTick
);
81 * M5 can do several special things when various signals are sent.
87 // Floating point exceptions may happen on misspeculated paths, so
89 signal(SIGFPE
, SIG_IGN
);
91 // We use SIGTRAP sometimes for debugging
92 signal(SIGTRAP
, SIG_IGN
);
94 // Dump intermediate stats
95 signal(SIGUSR1
, dumpStatsHandler
);
97 // Dump intermediate stats and reset them
98 signal(SIGUSR2
, dumprstStatsHandler
);
100 // Exit cleanly on Interrupt (Ctrl-C)
101 signal(SIGINT
, exitNowHandler
);
103 // Print out cycle number on abort
104 signal(SIGABRT
, abortHandler
);
108 * Uncompress and unmarshal the code object stored in the
112 getCode(const EmbeddedPyModule
*pymod
)
114 assert(pymod
->zlen
== pymod
->code_end
- pymod
->code
);
115 Bytef
*marshalled
= new Bytef
[pymod
->mlen
];
116 uLongf unzlen
= pymod
->mlen
;
117 int ret
= uncompress(marshalled
, &unzlen
, (const Bytef
*)pymod
->code
,
120 panic("Could not uncompress code: %s\n", zError(ret
));
121 assert(unzlen
== (uLongf
)pymod
->mlen
);
123 return PyMarshal_ReadObjectFromString((char *)marshalled
, pymod
->mlen
);
126 // The python library is totally messed up with respect to constness,
127 // so make a simple macro to make life a little easier
128 #define PyCC(x) (const_cast<char *>(x))
131 * Load and initialize all of the python parts of M5, including Swig
132 * and the embedded module importer.
137 extern void initSwig();
139 // initialize SWIG modules. initSwig() is autogenerated and calls
140 // all of the individual swig initialization functions.
143 // Load the importer module
144 PyObject
*code
= getCode(&embeddedPyImporter
);
145 PyObject
*module
= PyImport_ExecCodeModule(PyCC("importer"), code
);
151 // Load the rest of the embedded python files into the embedded
153 const EmbeddedPyModule
*pymod
= &embeddedPyModules
[0];
154 while (pymod
->filename
) {
155 PyObject
*code
= getCode(pymod
);
156 PyObject
*result
= PyObject_CallMethod(module
, PyCC("add_module"),
157 PyCC("ssO"), pymod
->filename
, pymod
->modpath
, code
);
170 * Start up the M5 simulator. This mostly vectors into the python
174 m5Main(int argc
, char **argv
)
176 PySys_SetArgv(argc
, argv
);
178 // We have to set things up in the special __main__ module
179 PyObject
*module
= PyImport_AddModule(PyCC("__main__"));
181 panic("Could not import __main__");
182 PyObject
*dict
= PyModule_GetDict(module
);
184 // import the main m5 module
186 result
= PyRun_String("import m5", Py_file_input
, dict
, dict
);
194 result
= PyRun_String("m5.main()", Py_file_input
, dict
, dict
);
208 PyImport_ImportModule(PyCC("m5"));