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
41 #include "base/cprintf.hh"
42 #include "base/misc.hh"
43 #include "base/types.hh"
44 #include "sim/async.hh"
45 #include "sim/core.hh"
46 #include "sim/init.hh"
50 /// Stats signal handler.
52 dumpStatsHandler(int sigtype
)
55 async_statdump
= true;
59 dumprstStatsHandler(int sigtype
)
62 async_statdump
= true;
63 async_statreset
= true;
66 /// Exit signal handler.
68 exitNowHandler(int sigtype
)
74 /// Abort signal handler.
76 abortHandler(int sigtype
)
78 ccprintf(cerr
, "Program aborted at cycle %d\n", curTick
);
82 * M5 can do several special things when various signals are sent.
88 // Floating point exceptions may happen on misspeculated paths, so
90 signal(SIGFPE
, SIG_IGN
);
92 // We use SIGTRAP sometimes for debugging
93 signal(SIGTRAP
, SIG_IGN
);
95 // Dump intermediate stats
96 signal(SIGUSR1
, dumpStatsHandler
);
98 // Dump intermediate stats and reset them
99 signal(SIGUSR2
, dumprstStatsHandler
);
101 // Exit cleanly on Interrupt (Ctrl-C)
102 signal(SIGINT
, exitNowHandler
);
104 // Print out cycle number on abort
105 signal(SIGABRT
, abortHandler
);
108 // The python library is totally messed up with respect to constness,
109 // so make a simple macro to make life a little easier
110 #define PyCC(x) (const_cast<char *>(x))
112 EmbeddedPython
*EmbeddedPython::importer
= NULL
;
113 PyObject
*EmbeddedPython::importerModule
= NULL
;
114 EmbeddedPython::EmbeddedPython(const char *filename
, const char *abspath
,
115 const char *modpath
, const char *code
, int zlen
, int len
)
116 : filename(filename
), abspath(abspath
), modpath(modpath
), code(code
),
119 // if we've added the importer keep track of it because we need it
121 if (string(modpath
) == string("importer"))
124 getList().push_back(this);
127 list
<EmbeddedPython
*> &
128 EmbeddedPython::getList()
130 static list
<EmbeddedPython
*> the_list
;
135 * Uncompress and unmarshal the code object stored in the
139 EmbeddedPython::getCode() const
141 Bytef marshalled
[len
];
143 int ret
= uncompress(marshalled
, &unzlen
, (const Bytef
*)code
, zlen
);
145 panic("Could not uncompress code: %s\n", zError(ret
));
146 assert(unzlen
== (uLongf
)len
);
148 return PyMarshal_ReadObjectFromString((char *)marshalled
, len
);
152 EmbeddedPython::addModule() const
154 PyObject
*code
= getCode();
155 PyObject
*result
= PyObject_CallMethod(importerModule
, PyCC("add_module"),
156 PyCC("sssO"), filename
, abspath
, modpath
, code
);
167 * Load and initialize all of the python parts of M5, including Swig
168 * and the embedded module importer.
171 EmbeddedPython::initAll()
173 // Load the importer module
174 PyObject
*code
= importer
->getCode();
175 importerModule
= PyImport_ExecCodeModule(PyCC("importer"), code
);
176 if (!importerModule
) {
181 // Load the rest of the embedded python files into the embedded
183 list
<EmbeddedPython
*>::iterator i
= getList().begin();
184 list
<EmbeddedPython
*>::iterator end
= getList().end();
185 for (; i
!= end
; ++i
)
186 if (!(*i
)->addModule())
192 EmbeddedSwig::EmbeddedSwig(void (*init_func
)())
193 : initFunc(init_func
)
195 getList().push_back(this);
198 list
<EmbeddedSwig
*> &
199 EmbeddedSwig::getList()
201 static list
<EmbeddedSwig
*> the_list
;
206 EmbeddedSwig::initAll()
208 // initialize SWIG modules. initSwig() is autogenerated and calls
209 // all of the individual swig initialization functions.
210 list
<EmbeddedSwig
*>::iterator i
= getList().begin();
211 list
<EmbeddedSwig
*>::iterator end
= getList().end();
212 for (; i
!= end
; ++i
)
219 EmbeddedSwig::initAll();
220 return EmbeddedPython::initAll();
224 * Start up the M5 simulator. This mostly vectors into the python
228 m5Main(int argc
, char **argv
)
230 PySys_SetArgv(argc
, argv
);
232 // We have to set things up in the special __main__ module
233 PyObject
*module
= PyImport_AddModule(PyCC("__main__"));
235 panic("Could not import __main__");
236 PyObject
*dict
= PyModule_GetDict(module
);
238 // import the main m5 module
240 result
= PyRun_String("import m5", Py_file_input
, dict
, dict
);
248 result
= PyRun_String("m5.main()", Py_file_input
, dict
, dict
);
262 PyImport_ImportModule(PyCC("m5"));